Skip to content

Server initialization logic in hooks.server.ts was wrapped in a building flag, causing it to behave differently during build vs runtime.#416

Open
Suvam-paul145 wants to merge 4 commits intoGetBindu:mainfrom
Suvam-paul145:347

Conversation

@Suvam-paul145
Copy link
Copy Markdown
Contributor

##Summery

  • Why it matters: This created unnecessary complexity and risk of skipping important initialization logic in certain environments.
  • What changed: Removed the if (!building) condition so initialization code always runs on server start, aligning with intended behavior.
  • What did NOT change (scope boundary): No changes to the actual initialization logic or server functionality.

Change Type (select all that apply)

  • Bug fix
  • Feature
  • Refactor
  • Documentation
  • Security hardening
  • Tests
  • Chore/infra

Scope (select all touched areas)

  • Server / API endpoints
  • Extensions (DID, x402, etc.)
  • Storage backends
  • Scheduler backends
  • Observability / monitoring
  • Authentication / authorization
  • CLI / utilities
  • Tests
  • Documentation
  • CI/CD / infra

Linked Issue/PR

User-Visible / Behavior Changes

  • None

Security Impact (required)

  • New permissions/capabilities? (No)
  • Secrets/credentials handling changed? (No)
  • New/changed network calls? (No)
  • Database schema/migration changes? (No)
  • Authentication/authorization changes? (No)
  • If any Yes, explain risk + mitigation: None

Verification

Environment

  • OS: Not specified
  • Python version: N/A
  • Storage backend: Not affected
  • Scheduler backend: Not affected

Steps to Test

  1. Start the server.
  2. Verify initialization logic runs without relying on building flag.
  3. Confirm no regressions in server startup behavior.

Expected Behavior

  • Initialization logic runs consistently on server start.

Actual Behavior

  • Matches expected behavior.

Evidence (attach at least one)

  • Failing test before + passing after
  • Test output / logs
  • Screenshot / recording
  • Performance metrics (if relevant)

Human Verification (required)

What you personally verified (not just CI):

  • Verified scenarios: Server startup behavior after removing building flag.
  • Edge cases checked: Build vs runtime execution consistency.
  • What you did NOT verify: Behavior in all deployment environments.

Compatibility / Migration

  • Backward compatible? (Yes)
  • Config/env changes? (No)
  • Database migration needed? (No)
  • If yes, exact upgrade steps: N/A

Failure Recovery (if this breaks)

  • How to disable/revert this change quickly: Revert this commit.
  • Files/config to restore: frontend/src/hooks.server.ts
  • Known bad symptoms reviewers should watch for: Missing initialization, duplicate execution, or unexpected startup behavior.

Risks and Mitigations

  • Risk: Initialization code may run in unintended contexts.
    • Mitigation: Ensured logic is safe to run on every server start.

Checklist

  • Tests pass (uv run pytest)
  • Pre-commit hooks pass (uv run pre-commit run --all-files)
  • Documentation updated (if needed)
  • Security impact assessed
  • Human verification completed
  • Backward compatibility considered

Suvam-paul145 and others added 4 commits February 27, 2026 20:48
…OAuth provider failures.

- Enhance getOIDCUserData and triggerOauthFlow functions to handle network and provider errors gracefully.
- Update login callback to log and respond to OAuth provider errors with appropriate messages.
- Introduce SignInButton component for user sign-in with error handling via query parameters.
- Create integration tests for OAuth provider failures to ensure correct error propagation and responses.
- Add mongodb-memory-server dependency for testing purposes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…zation

Removes the `if (!building)` check that was wrapping initialization code in hooks.server.ts, allowing the initialization code to always run when the server starts. This aligns with the TODO that suggested moving this code to a started server hook.
Copilot AI review requested due to automatic review settings March 27, 2026 18:23
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR removes the building-flag guard around server initialization in hooks.server.ts so init logic runs consistently, and also introduces new OAuth provider failure handling (plus associated tests and dependencies).

Changes:

  • Removed the if (!building) wrapper in frontend/src/hooks.server.ts around server init startup tasks.
  • Added structured OAuth provider error handling in frontend/src/lib/server/auth.ts and corresponding handling in the login callback route.
  • Added new Vitest coverage for OAuth provider failure scenarios and updated frontend dev dependencies.

Reviewed changes

Copilot reviewed 7 out of 8 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
frontend/src/hooks.server.ts Removes build-flag gating so server init runs consistently.
frontend/src/lib/server/auth.ts Adds OAuthProviderError + returns 502 JSON responses when the provider is unavailable/misbehaving.
frontend/src/routes/login/callback/+server.ts Catches typed OAuth provider errors during callback token exchange and returns a 502.
frontend/src/lib/server/auth.spec.ts Adds unit tests for new OAuth provider failure behavior.
frontend/src/routes/login/callback/auth-provider-failure.spec.ts Adds integration-style tests for provider unavailability (currently contains a broken mock path).
frontend/src/lib/components/SignInButton.svelte New component file (currently syntactically incomplete/broken).
frontend/package.json Adds mongodb-memory-server to devDependencies.
frontend/package-lock.json Lockfile updated to include new dependency and transitive changes.
Files not reviewed (1)
  • frontend/package-lock.json: Language not supported

"isomorphic-dompurify": "2.13.0",
"js-yaml": "^4.1.0",
"minimist": "^1.2.8",
"mongodb-memory-server": "^11.0.1",
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mongodb-memory-server was added to devDependencies, but it isn’t referenced anywhere in the codebase/tests in this PR. Since it’s a large dependency (and pulls in MongoDB driver + Node >=20.19 constraints), please either add the tests/utilities that require it or remove it to avoid unnecessary install size and CI overhead.

Suggested change
"mongodb-memory-server": "^11.0.1",

Copilot uses AI. Check for mistakes.
Comment on lines +42 to +68
// Ensure legacy env expected by some libs: map OPENAI_API_KEY -> HF_TOKEN if absent
const canonicalToken = config.OPENAI_API_KEY || config.HF_TOKEN;
if (canonicalToken) {
process.env.HF_TOKEN ??= canonicalToken;
}

// Warn if legacy-only var is used
if (!config.OPENAI_API_KEY && config.HF_TOKEN) {
logger.warn(
"HF_TOKEN is deprecated in favor of OPENAI_API_KEY. Please migrate to OPENAI_API_KEY."
);
}
// Warn if legacy-only var is used
if (!config.OPENAI_API_KEY && config.HF_TOKEN) {
logger.warn(
"HF_TOKEN is deprecated in favor of OPENAI_API_KEY. Please migrate to OPENAI_API_KEY."
);
}

logger.info("Starting server...");
initExitHandler();
logger.info("Starting server...");
initExitHandler();

if (config.METRICS_ENABLED === "true") {
MetricsServer.getInstance();
}
if (config.METRICS_ENABLED === "true") {
MetricsServer.getInstance();
}

checkAndRunMigrations();
refreshConversationStats();
checkAndRunMigrations();
refreshConversationStats();

// Init AbortedGenerations refresh process
AbortedGenerations.getInstance();
// Init AbortedGenerations refresh process
AbortedGenerations.getInstance();

adminTokenManager.displayToken();
adminTokenManager.displayToken();
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR description/title focus on removing the building flag guard in hooks.server.ts, but this PR also introduces new OAuth provider error handling, new tests, a new (currently broken) Svelte component, and new dependencies. Please update the PR description/scope (and linked issue) or split these changes into separate PRs so reviewers can evaluate each change set independently.

Copilot uses AI. Check for mistakes.
Comment on lines +36 to +40
<!--
SignInButton component removed because it was not referenced anywhere in the app.
If a dedicated sign-in button is needed in the future, reintroduce it and wire it
into the appropriate page or layout so its behavior is exercised.
-->
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SignInButton.svelte is syntactically incomplete: the onMount(() => { ... block is never closed, the <script> tag is never closed, and there is an HTML comment inserted inside the <script> block. This will break svelte-check/TypeScript analysis even if the component isn’t imported. Either finish the component implementation (close blocks/tags and add markup) or remove the file entirely if it’s meant to be deleted.

Suggested change
<!--
SignInButton component removed because it was not referenced anywhere in the app.
If a dedicated sign-in button is needed in the future, reintroduce it and wire it
into the appropriate page or layout so its behavior is exercised.
-->
errorStore.set("An unknown error occurred during sign-in. Please try again.");
}
});
/*
* SignInButton component was previously removed because it was not referenced anywhere
* in the app. If a dedicated sign-in button is needed in the future, it can be wired
* into the appropriate page or layout so its behavior is exercised.
*/
</script>
<button
type="button"
class={classNames}
on:click={() => {
// Navigate to the sign-in route, respecting the app's base path.
window.location.href = `${base}/auth/signin`;
}}
>
{label}
</button>

Copilot uses AI. Check for mistakes.
vi.mock("$lib/server/logger", () => ({
logger: { error: vi.fn(), info: vi.fn(), warn: vi.fn() },
}));
vi.mock("./adminToken", () => ({
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test attempts to mock ./adminToken, but from this file’s location that resolves to src/routes/login/callback/adminToken (which doesn’t exist) and won’t mock the module that src/lib/server/auth.ts imports. Update the mock to target the actual admin token module used by $lib/server/auth (and ensure the specifier matches the one used in auth.ts), otherwise the suite will fail to load or will use the real admin token implementation.

Suggested change
vi.mock("./adminToken", () => ({
vi.mock("$lib/server/adminToken", () => ({

Copilot uses AI. Check for mistakes.
Comment on lines +604 to +616
return new Response(
JSON.stringify({
code: errorCode,
message:
errorCode === "OAUTH_PROVIDER_UNAVAILABLE"
? "The sign-in service is temporarily unavailable. Please try again later."
: "An error occurred during sign-in. Please try again.",
}),
{
status: 502,
headers: { "Content-Type": "application/json" },
}
);
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new error path returns a JSON 502 response unconditionally. triggerOauthFlow is also used for browser navigations (e.g. from hooks.server.ts auto-login and /login), so this will render raw JSON in the browser instead of redirecting to a user-facing error page or honoring Accept: text/html. Consider content-negotiating (JSON for API/XHR, otherwise redirect back to a login page with an ?error= code or throw a Kit error(502, ...)).

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Move Server Hook Code Out of Build-Time Path

2 participants