Fix unsafe cast in job monitor shutdown path#709
Merged
Conversation
The monitors Map used `Promise<JobRunRecord>` but the `.catch()` handler returned `undefined` during shutdown, requiring an unsafe double cast. Widen the Map type to `Promise<JobRunRecord | undefined>` and fall back to a DB lookup in `waitForTerminal` when the monitor resolves to `undefined` — which also fixes a latent bug where the caller would silently receive `undefined` typed as `JobRunRecord`. Co-Authored-By: Claude Opus 4.6 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
undefined as unknown as JobRunRecorddouble cast inapps/server/src/jobs/service.tswhere the monitor's.catch()handler returnsundefinedduring shutdownmonitorsMap type fromPromise<JobRunRecord>toPromise<JobRunRecord | undefined>to reflect the actual runtime behaviorwaitForTerminalto fall back to a DB lookup when the monitor resolves toundefined, which also fixes a latent bug where the caller would silently receiveundefinedtyped asJobRunRecordWhy this is tech debt
The unsafe cast masked a type-level lie — the Map claimed to hold
Promise<JobRunRecord>but could actually resolve toundefined. This meant TypeScript couldn't catch callers that failed to handle theundefinedcase. The fix aligns the types with reality and adds a safe fallback.What's next
The backlog is empty after this fix. The next run will re-audit for new tech debt.
Test plan
pnpm run check— type checking passespnpm run test— 180 unit tests passpnpm run test:e2e— 171 E2E tests pass (12 pre-existing skips)🤖 Generated with Claude Code