Skip to content

fix(jobs): derive SyncLectureMetadata's scope once, so the log and the query cannot disagree - #720

Merged
sydevs-bot merged 2 commits into
mainfrom
claude/fix-sync-lecture-scope
Sep 7, 2026
Merged

sydevs-bot merged 2 commits into
mainfrom
claude/fix-sync-lecture-scope

Conversation

@sydevs-bot

@sydevs-bot sydevs-bot commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

Closes #715.

The failure this prevents

SyncLectureMetadata's opening log line dereferenced lectureIds behind a guard that can never be false:

const where: Where =
  Array.isArray(lectureIds) && lectureIds.length > 0
    ? { and: [{ type: { equals: 'full' } }, { id: { in: lectureIds } }] }
    : { type: { equals: 'full' } }

scope: where ? `lectureIds[${lectureIds!.length}]` : 'all lectures',

where is assigned an object on both branches, so it is always truthy and the 'all lectures' arm was unreachable. On a run with no input, lectureIds is undefined and lectureIds!.length throws a TypeError — before a single lecture is read.

The monthly schedule (0 3 1 * *, queue monthly) is exactly that run. It passes no input, so the job has been throwing on its own first log line on every scheduled sweep.

The change

The scope is decided once, and both readers use it:

const scopedIds = Array.isArray(lectureIds) && lectureIds.length > 0 ? lectureIds : null

const where: Where = scopedIds
  ? { and: [{ type: { equals: 'full' } }, { id: { in: scopedIds } }] }
  : { type: { equals: 'full' } }

scope: scopedIds ? `lectureIds[${scopedIds.length}]` : 'all lectures',

The first revision fixed the instance — it tested lectureIds?.length instead of where. Review pointed out that the cause survived: "is this run scoped?" was still asked twice, ten lines apart, in two differently-written expressions, which is how the two came to disagree in the first place. They could still disagree, too: lectureIds arrives from an unvalidated json input field, so { lectureIds: "abc" } takes the unfiltered where branch while the log line reads lectureIds[3] — the same lie about scope, one input shape over.

Same one line of behaviour change. The ! assertion goes with it, and the filtering behaviour is unchanged.

Why no test caught it

Every case in tests/int/sync-lecture-metadata.int.spec.ts passed lectureIds, so the one path that runs in production was uncovered. Nothing had called runTask(payload) at all — that, not the helper's old input ?? {} default, is why this was invisible. The handler reads input?.lectureIds, so {} and an absent input were never distinguishable to it.

The helper now passes input through, so it can express the scheduled shape:

const runTask = (payload: Payload, input?: { lectureIds?: number[] }) =>
  runTaskHandler(SyncLectureMetadata, { payload, input })

and the new case is await runTask(payload), asserting the logged scope. It is no longer the only case in the file that bypasses the helper.

Test results

The new case was confirmed failing first, by reintroducing the defect at the site that causes it:

Defect reintroduced Result
scope: where ? … back in place runs unscoped when the schedule passes no input at all → TypeError: Cannot read properties of undefined (reading 'length'). 1 failed, 8 passed
Fix restored 9 passed

That table was measured on the first revision (2cfcf46), against a real Postgres.

Re-run after the review changes (8084a0e5):

  • pnpm lint ✓
  • pnpm typecheck ✓ · pnpm typecheck:tests ✓
  • pnpm test:unit ✓ — 125 files, 1601 tests
  • tests/int/sync-lecture-metadata.int.spec.ts — not re-run. No Postgres is reachable from the session that made these changes. CI runs it against its postgres:18 container.
  • Migration contract: unchanged from the first revision. Nothing in this diff reaches the schema.

Notes for reviewer

This was flagged by two independent review passes on #714, which edits the line above it. It is filed separately because #714 is a type-level refactor and this is a behaviour fix. The two branches both touch SyncLectureMetadata.ts within a few lines, so whichever merges second will want a trivial conflict resolution.

🤖 Written by the sydevs autonomous loop — see what this is

🤖 Generated with Claude Code

https://claude.ai/code/session_015PxdvGktTVasmM1TrVUwi2

… log

The opening log line read `where ? ... : 'all lectures'`. `where` is an
object on both branches of the ternary above it, so it is always truthy
and the unscoped arm was unreachable. On a run with no input,
`lectureIds!.length` then threw a TypeError before the first lecture was
read — and the monthly schedule (`0 3 1 * *`) is exactly that run.

Every case in the int spec passed `lectureIds`, so the one path that runs
in production had no cover. The new case runs the handler with no input
at all and asserts the logged scope. Reintroducing `where ?` turns it red
with the production TypeError, which is how it was confirmed.

Closes #715

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01EgER1gbKXnNKyM8qGLvwhV
@railway-app

railway-app Bot commented Sep 6, 2026 •

Copy link
Copy Markdown

🚅 Deployed to the SahajCloud-pr-720 environment in sahajcloud

Service Status Web Updated
SahajCloud ✅ Success (View Logs) Web Sep 7, 2026 at 1:17 pm UTC
1 service not affected by this PR
  • mailpit

@sydevs-bot sydevs-bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

🧐 Adversarial review

Holistic assessment. Right-sized: 24/-1 across two files, and every line of it traces to #715's acceptance criteria — no refactor smuggled in, no config knob, where genuinely untouched. The diagnosis holds up against the code: where is an object on both branches, the unscoped arm was dead, and the monthly 0 3 1 * * run is the only caller that hits it. The new case is non-vacuous and was confirmed red at the causing site. Nothing here touches a cross-repo contract.

The one shape question is that the fix repairs the instance and leaves the cause in place: whether the run is scoped is still decided twice, in two differently-written expressions, ten lines apart — which is precisely how they came to disagree. Deriving it once costs the same line of behaviour change and also retires the ! assertion and the explanatory comment. Everything else is small.

Findings

  • should-fix — SyncLectureMetadata.ts:79: the scoped-ness test is still duplicated (Array.isArray(lectureIds) && lectureIds.length > 0 vs lectureIds?.length), and the two can still disagree for a non-array json input. Deriving it once removes the class, not the instance. [thread]
  • consider — SyncLectureMetadata.ts:77-78: the added comment explains the deleted code rather than the code that is there; #715, the PR body and the commit message already hold that story. [thread]
  • consider — sync-lecture-metadata.int.spec.ts:185: the only case in the file that bypasses runTask, over a distinction (undefined vs {} input) the handler cannot observe. [thread]

What was checked

  • #715 read in full and measured against the diff — both acceptance criteria (scope: 'all lectures' on an unscoped run, scoped runs still logging lectureIds[N]) are met by the one changed line, and the "fails when the defect is reintroduced" criterion is evidenced in the PR body's table.
  • src/jobs/SyncLectureMetadata/SyncLectureMetadata.ts read whole at 2cfcf46. Confirmed the fix does not alter where (lines 70-73) and so cannot change which lectures are swept; confirmed lectureIds?.length and Array.isArray(…) && length > 0 agree for every number[] | undefined input, and diverge only for a non-array truthy with a length.
  • Does the class generalise? grep -rn "where ?" src/, grep -rn '!\.length' src/, and grep -rn 'scope:' src/jobs/ each return this line and nothing else. This is the only instance in src/ — no follow-up ticket is warranted.
  • tests/int/sync-lecture-metadata.int.spec.ts read whole, plus tests/utils/taskRunner.ts. Confirmed the new case is self-sufficient (creates its own lecture, so totalProcessed > 0 does not depend on test order), that vi.spyOn is installed after setup and restored in finally, and that the unscoped sweep re-syncing earlier tests' lectures is harmless because no later case reads one.
  • #714's diff checked for overlap. It rewrites lines 1, 13-22, 48-58 and 67 of the same file and ~line 103 of the same spec — it does not touch line 77, so this fix is not duplicated there. One correction to the PR body: at ten lines and ~70 lines of separation respectively, the two branches should merge without a textual conflict in either order.
  • Contract surfaces: none. Internal job, no generated-type flow, no endpoint or embed surface.
  • Reviewer profile read from workflow/skills/adversarial-review/reviewer-profile.md.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

…e scheduled shape

Addresses the review on #720.

The fix in 2cfcf46 made the log line and the query agree for the array
case. It left the cause in place: whether a run is scoped was still decided
twice, ten lines apart, in two differently-written expressions. `input` is
an unvalidated `json` field, so the two could still disagree — a manual run
passing `{ lectureIds: "abc" }` takes the unfiltered `where` branch while
the log line reads `lectureIds[3]`, the same lie about scope one input
shape over.

`scopedIds` is now derived once and both readers use it. That retires the
`!` assertion and the comment explaining the deleted code.

The spec's `runTask` helper no longer defaults `input` to `{}`, so
`runTask(payload)` reproduces the scheduled run's literally absent input,
and the new case stops being the only one that bypasses the helper. The
handler reads `input?.lectureIds`, so `{}` and absent were never
distinguishable — the default was not what hid this defect. Nothing had
called `runTask(payload)` at all.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_015PxdvGktTVasmM1TrVUwi2
@railway-app
railway-app Bot temporarily deployed to sahajcloud / SahajCloud-pr-720 September 7, 2026 13:14 Destroyed
@sydevs-bot sydevs-bot changed the title fix(jobs): test lectureIds, not where, in SyncLectureMetadata's scope log fix(jobs): derive SyncLectureMetadata's scope once, so the log and the query cannot disagree Sep 7, 2026
@sydevs-bot

Copy link
Copy Markdown
Contributor Author

Revised in 8084a0e5. All 3 review findings adopted, no push-back.

The scope is now derived once as scopedIds, so the query and the log line cannot describe different runs — including for the non-array json input the review named. The spec's runTask helper passes input through instead of defaulting to {}, and the new case uses it.

Title updated: the change is no longer "test lectureIds, not where".

One correction to the PR body, from the third finding: the helper's input ?? {} default was never the reason this went uncaught. The handler reads input?.lectureIds, so {} and an absent input look identical to it — nothing had called runTask(payload) at all.

The integration spec was not re-run here: no Postgres is reachable from this session. Lint, both typechecks and the unit lane are green; CI runs the int lane.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

@Ardnived Ardnived removed the awaiting label Sep 7, 2026
@sydevs-bot
sydevs-bot merged commit 9eac10b into main Sep 7, 2026
7 checks passed
@github-project-automation github-project-automation Bot moved this from In progress to Done in SYDevelopers Technical Roadmap Sep 7, 2026
sydevs-bot pushed a commit that referenced this pull request Sep 7, 2026
#720 and #717 landed on main while this PR was open. #720 rewrote the
scope derivation in SyncLectureMetadata.ts, a few lines from this
branch's own edits to the same file.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017yU3grG7LTeRqSccKfdm91

# Conflicts:
#	src/jobs/SyncLectureMetadata/SyncLectureMetadata.ts
Ardnived pushed a commit that referenced this pull request Nov 5, 2026
…e scheduled shape

Addresses the review on #720.

The fix in e9c503d made the log line and the query agree for the array
case. It left the cause in place: whether a run is scoped was still decided
twice, ten lines apart, in two differently-written expressions. `input` is
an unvalidated `json` field, so the two could still disagree — a manual run
passing `{ lectureIds: "abc" }` takes the unfiltered `where` branch while
the log line reads `lectureIds[3]`, the same lie about scope one input
shape over.

`scopedIds` is now derived once and both readers use it. That retires the
`!` assertion and the comment explaining the deleted code.

The spec's `runTask` helper no longer defaults `input` to `{}`, so
`runTask(payload)` reproduces the scheduled run's literally absent input,
and the new case stops being the only one that bypasses the helper. The
handler reads `input?.lectureIds`, so `{}` and absent were never
distinguishable — the default was not what hid this defect. Nothing had
called `runTask(payload)` at all.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_015PxdvGktTVasmM1TrVUwi2
Ardnived pushed a commit that referenced this pull request Nov 5, 2026
#720 and #717 landed on main while this PR was open. #720 rewrote the
scope derivation in SyncLectureMetadata.ts, a few lines from this
branch's own edits to the same file.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017yU3grG7LTeRqSccKfdm91

# Conflicts:
#	src/jobs/SyncLectureMetadata/SyncLectureMetadata.ts

This branch was successfully deployed

No deployments
sahajcloud / SahajCloud-pr-720 — 8084a0e5 Deployed Sep 7, 2026 by railway-app[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

fix(jobs): SyncLectureMetadata throws on its own log line when the monthly run passes no input

3 participants