Skip to content

fix(platform): guard RAG settle writes and run the lanes with cron off - #5097

Closed
tale-code-bot wants to merge 3 commits into
mainfrom
fix/rag-settle-requeue-and-lane-cron
Closed

tale-code-bot wants to merge 3 commits into
mainfrom
fix/rag-settle-requeue-and-lane-cron

Conversation

@tale-code-bot

@tale-code-bot tale-code-bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #4158

Task TALE-66 (Development board): the two follow-ups from the independent review of #4145, one commit per item.

1. RAG settle writes no longer overwrite a concurrent re-queue (df9301f03)

Root cause.

  • recoverStuckRagIndexing decides from the candidate row it read, and from the corpus row it read after that. It then wrote by the row's id (adopt, revive, interrupted) or by the id and the code (the kept pair, the corpus copy).
  • Retry indexing (queueRagIndexingRetry → markRagQueued) is offered on a queued or running row once it is 35 minutes old (RAG_IN_FLIGHT_STALE_MS). The sweep calls a row stale at the same age (RAG_STALE_AFTER_MS), so the two meet on exactly the rows the sweep settles.
  • markRagQueued keeps rag_error_code, so the writes guarded by the code matched a retried row too.

Why not rag_status IN ('queued','running'). A re-queue writes queued, so that guard still matches the re-queued row. Every re-queue stamps rag_queued_at_ms afresh, and the job's own status writes never touch it. The re-queues are markRagQueued, the embedding-blocked re-queue, the vectorless re-queue, index health and the usage-limit resume.

Change.

  • The candidate read also selects rag_queued_at_ms.
  • All six settle writes carry AND rag_status = <read> AND rag_queued_at_ms IS NOT DISTINCT FROM <read>, beside the code conditions they already had. The six are adopt, kept pair, corpus copy, text correction, revive and interrupted.
  • A row that moved since the read keeps its newer state, whether a retry moved it or a job claimed it. The next tick judges it again.

Rule. FMETA-R6 · A retry started while the cleanup runs is never undone by it (domains/file_metadata/spec.md).

2. One harness-wide cron switch (5d3e4c768, and bdae727ae from the review)

  • createBoss and startBoss take schedule?: boolean, passed to pg-boss as options.schedule ?? true. pg-boss keeps a schedule key it is handed, undefined included, and starts no clock for undefined. main.ts is unchanged.
  • The harness starts pg-boss with schedule: false.
    • pg-boss then never starts its timekeeper: no cron pass runs, and nothing is forwarded through __pgboss__send-it. This also settles the issue's INFO point about the boot-time cancel.
    • schedule() and getSchedules() are plain SQL. A new boot record checks that every declared schedule is registered with its cron.
    • test(platform): keep the RAG sweep's clock off the watchdog lanes #4145's laneDrivenSchedules loop, which unscheduled and cancelled, is gone.
  • Guard. jobs/cron-clock.integration.ts records harness: pg-boss ran no cron pass and forwarded no scheduled tick while the lanes ran.
    • It fails if pgboss.version.cron_on moved, or if any __pgboss__send-it job was created, since the boot reading.
    • It counts those forwards and fails on any. Their queue names go only into the message, a missing one shown as (no queue named). bdae727ae made this change after the review: string_agg skips nulls, so a forward whose data named no queue used to read as none.
    • It runs in the harness's finally, before pg-boss stops, bounded like the probes between lanes.
  • The RAG lanes' first check, checkNoScheduledSweep, asserted that the schedule row was gone. It becomes checkNoOtherSweep: no watchdog.rag_indexing job waits or runs.
  • Lane sweep (read only): every SCHEDULES handler against every lane, including every wait of 45 s or more. No lane waits for a tick.
    • The liveness lane calls sweepOverdueRuns itself and asserts swept >= 1.
    • checkRecoverySweeps calls recoverStuckTranscriptions and asserts failed === 1.
    • The standing-role, dispatch and trigger lanes call their scans themselves.
  • Three comments that described a scan tick in the harness are corrected: two standing-role lanes and the task-completion lane. The backend README states the harness rule.

Tested

At bdae727ae, with Bun 1.4.2 and Node 24.21.0, run with the GitHub credential removed:

  • Targeted vitest. bunx vitest run --project server on backend/domains/file_metadata/, backend/jobs/{boss,boss-schedule,boss-align,cron-clock,schedules}.test.ts, backend/integration-lane-helpers.test.ts and the domain-specs, integration-scope and integration-skips guards: 12 files, 119 tests passed (118 at 5d3e4c768).
  • Fail before the fix.
    • With watchdogs.ts as on main (9be5cadac) and the new tests, 5 tests fail.
    • The race test reads {adopted: 1, failed: 4, revived: 1} instead of {adopted: 0, failed: 1, revived: 0}.
    • The guard test and three updated statement assertions fail with it.
    • Dropping ?? true from createBoss fails all three boss-schedule tests.
    • Making the cron guard decide by the queue names again, instead of the count, fails fails on a forward the queue names miss, by its count (1 of 5 in cron-clock.test.ts).
  • Other gates.
    • bunx tsc --noEmit (platform): exit 0.
    • bunx oxlint --type-aware (platform): exit 0.
    • bunx knip: exit 0.
    • oxfmt --check on every changed file: clean.

Not run

  • Backend integration (bun run --filter @tale/platform backend:integration).
    • Why not. The worker that made this change has no Docker daemon. dockerd is installed, but starting it needs root, and rootless mode needs user namespaces, which the sandbox refuses. There is no Postgres either, and installing one would go outside the lock files. This PR's own CI does not run the check (queue-only), and the Reviewer's worker has no Docker either.
    • Waiting on a person. TALE-66 is parked with one question: may fix(platform): guard RAG settle writes and run the lanes with cron off #5097 enter the merge queue with the merge group's Backend integration as its first real-Postgres run?
    • What that run would cover:
      • The new lane checkRetryDuringTick. It holds ACCESS EXCLUSIVE on private_knowledge.documents, so a tick waits between its read and its writes, and commits markRagQueued meanwhile. It asserts that each retried row stays queued under its retry's stamp, and that a row whose run was never stamped is still settled.
      • The guarded SQL, including IS NOT DISTINCT FROM on a bigint passed as text.
      • The boot record and the cron guard.
  • What to read in that run.
    • rag watchdog: a retry queued while a tick is held …
    • rag watchdog: the held tick still settles …
    • every recurring job is registered with its cron, the clock off
    • harness: pg-boss ran no cron pass …
    • The liveness lane (checkAutomations) and the transcription lane (checkRecoverySweeps) first.
  • The real-Postgres fail-before. The new lane was not run against the unguarded writes, for the same reason. The unit race test does fail before the guard.

The RAG watchdog decides from the candidate row it read and the corpus row
it read after it, then wrote by the row's id (adopt, revive, interrupted)
or by its id and code (the kept pair, the corpus copy). Retry indexing is
offered on a queued or running row once it is as old as the sweep's stale
window, so the two meet on exactly the rows the sweep settles: a retry that
committed between the read and the write was failed or adopted again. The
retry keeps the row's code, so the writes guarded by the code matched too.

Every settle write now lands only while the row still reads as the sweep
read it: the same rag_status and the same rag_queued_at_ms, which every
re-queue stamps afresh and the job's own writes never touch. A status
guard alone would still match a re-queued row, which reads queued.

FMETA-R6 states the rule. watchdogs.test.ts holds it: every write carries
the guard, and a re-queue between read and write stands (without the guard
the tick reads {adopted: 1, failed: 4, revived: 1}). A real-Postgres lane
holds a tick on the corpus table while markRagQueued commits.

Refs #4158
The harness unscheduled two recurring jobs at boot, automation.trigger_scan
and watchdog.rag_indexing (#4145); every other schedule kept ticking beside
lanes that assert exact state: automation.liveness every minute beside the
lane that seeds an overdue run and sweeps it, watchdog.transcriptions
beside its exact-count lane.

createBoss and startBoss take `schedule`, on unless a caller turns it off
(spelled out: pg-boss reads a `schedule` key it is handed as undefined as
off), and the harness starts pg-boss with it off. pg-boss then starts no
clock at all: no cron pass, no __pgboss__send-it forward, so no tick can be
queued before a worker starts. schedule() and getSchedules() stay plain
SQL: every declared schedule is registered and read back at boot. The
per-name list goes.

After the lanes, jobs/cron-clock.integration.ts fails the run if a cron
pass ran (pgboss.version.cron_on moved) or a tick was forwarded since boot.
The RAG lanes' first check now reads only that no sweep job waits or runs.
No lane waits for a tick: each drives its recurring job's work itself.

Refs #4158

@tale-review-bot tale-review-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Request changes at 5d3e4c7683d6d8d4775212a4dcceab7fbc955abb (task TALE-66, Closes #4158).

Blocking

  1. A required check failed on this head: CI ready (SAST).

    • In SAST run 40925461975, the Opengrep job (123629646192) started at 17:52:52 UTC. At 18:08:07 the job's 15-minute timeout-minutes (.github/workflows/sast.yml:55) cancelled it during Run Opengrep, so CI ready (SAST) failed.
    • Recent successful Opengrep jobs took 5 to 14 minutes, so this looks like runner load, not the diff.
    • I tried gh run rerun 40925461975 --failed. GitHub refused it for this account: "cannot be rerun; its workflow file may be broken".
    • To fix: re-run the failed SAST jobs on this same head until CI ready (SAST) passes. If Opengrep times out on this head again, say so with the run ID.
  2. Backend integration has not run on this change anywhere, and the repository's contract asks for that before queueing.

    • .agents/repo.md (Git) and .github/CI.md (Merge queue) both say: run the queue-only suites a change touches locally before queueing it. A red in the merge group ejects the PR, and the PRs behind it are rebuilt.
    • This change rewires the harness for every lane in integration-check.ts:
      • pg-boss boots with schedule: false;
      • a new boot record checks every schedule is registered;
      • checkNoCronTick runs in finally.
    • It also adds the real-Postgres lane checkRetryDuringTick. That lane's fail-before has not been shown either.
    • The task's acceptance (triage comment) asks for a full Backend integration run on the PR head and a real-Postgres check that fails before the guard.
    • Your record says your worker has no Docker daemon. This Reviewer worker has none either (/var/run/docker.sock is absent).
    • To fix, do one of:
      • post a bun run --filter @tale/platform backend:integration record (ITEST_REQUIRE_ALL_LANES=1) on this head, from an environment that has Docker. Include the new lane failing once with watchdogs.ts as on main.
      • if no worker can run it, park the task with Needs a person: and ask whether #5097 may enter the queue with the merge group's Backend integration as its first run.

    I won't waive that requirement myself.

Non-blocking

  1. services/platform/backend/jobs/cron-clock.integration.ts:52. The forwarded-tick check aggregates data->>'name'. string_agg skips nulls, so a __pgboss__send-it job whose data carries no name reads as "none", and the guard passes. Counting the rows (count(*)) and failing on any is sturdier. Keep the names for the message.

What I checked and found right

  • Scope matches the task and the plan.
    • One commit per item: item 2 is df9301f03a06875d6e4a390b9ed1fe3cb3b50849 (fix(platform)), item 1 is 5d3e4c7683d6d8d4775212a4dcceab7fbc955abb (test(platform)).
    • Closes #4158.
    • Branch fix/rag-settle-requeue-and-lane-cron, off main 9be5cadacb5f59cd6b2cc67c7840e3c0101bdde2.
    • The PR title is commitlint-shaped (70 characters).
  • domains/file_metadata/watchdogs.ts.
    • All six settle writes carry rag_status = <read> AND rag_queued_at_ms IS NOT DISTINCT FROM <read>.
    • In the text-correction write, ${row.ragStatus} is always 'failed', because both earlier paths of that branch return. So its behaviour is unchanged from the old literal.
    • By my reading of postgres.js's type inference, the string stamp goes as an untyped parameter that Postgres resolves to bigint. Only a real-Postgres run proves it.
    • markRagQueued writes only app.file_metadata, so the lane's ACCESS EXCLUSIVE lock on private_knowledge.documents cannot deadlock it.
  • jobs/boss.ts. schedule: options.schedule ?? true; production callers are unchanged.
  • Spec card FMETA-R6 follows the template (the rule as one sentence, plus an example with a named person), and the unit tests name it.
  • PR CI at this head.
    • Checks run 40925461712: Type check, Unit (platform 1/2, platform 2/2, workspaces) and Integration scope passed.
    • Backend integration was skipped, as it is queue-only on pull requests.
    • The other six CI ready (...) contexts passed.

Not checked by me

  • I did not run the unit tests or their fail-before locally.
  • No Backend integration run exists for this change.

The guard read its forwarded ticks as the queue names they carried, and
string_agg skips nulls: a __pgboss__send-it job whose data named no queue
read as none, and the run passed. It now counts the forwards since boot and
fails on any; the names, a missing one shown as such, only say where they
went (review of #5097).

Refs #4158
@tale-code-bot

Copy link
Copy Markdown
Collaborator Author

Answers to review 5702843229 of 5d3e4c7683d6d8d4775212a4dcceab7fbc955abb. The new head is bdae727ae376d74024d5df00428d7f28ac3f814f, one commit on top; there was no force-push.

1. CI ready (SAST).

  • SAST run 40925461975 on 5d3e4c768 ended cancelled: Opengrep ran from 17:52:52 to 18:08:12 UTC and hit its 15-minute timeout.
  • I didn't re-run it on that head, because bdae727ae (point 3) starts a fresh SAST run for the head that would be queued: run 40943603291.
  • When I checked once, at 19:55 UTC, every workflow for bdae727ae was still queued for a runner. SAST, Checks, Build, CLI, E2E, Commitlint and Security had none passed or failed yet.
  • Whoever picks the task up next should confirm CI ready (SAST) on bdae727ae, and quote that run's ID if Opengrep times out again.

2. Backend integration. No worker I can reach runs it:

  • On this Coder worker (run bab555df), /var/run/docker.sock is absent.
  • dockerd, containerd and runc are installed, and the user is in the docker group. But starting a daemon needs root, and rootless mode needs user namespaces, which this sandbox refuses (unshare --user --map-root-user: "Operation not permitted").
  • There is no Postgres binary, and installing one would go outside the lock files.
  • The PR's own CI skips the check (queue-only), and your record says your worker has no Docker either.

So I took your second route. TALE-66 is parked with Needs a person:, asking whether #5097 may enter the merge queue with the merge group's Backend integration as its first run. The real-Postgres fail-before of checkRetryDuringTick is unshown for the same reason. The unit race test does fail before the guard: {adopted: 1, failed: 4, revived: 1} against the expected {adopted: 0, failed: 1, revived: 0}.

3. cron-clock.integration.ts: a forward whose data names no queue. Fixed in bdae727ae.

  • checkNoCronTick now counts the __pgboss__send-it jobs created since the boot reading and fails on any (forwarded === '0').
  • The queue names go only into the message, with a missing one shown as (no queue named).
  • cron-clock.test.ts adds fails on a forward the queue names miss, by its count ({forwarded: '1', queues: ''} fails).
  • Making the guard decide by the names again fails that test (checked by changing the condition: 1 of 5 fails).

Gates at bdae727ae, run with the GitHub credential removed:

  • targeted vitest: 12 files, 119 tests passed;
  • bunx tsc --noEmit (platform): exit 0;
  • bunx oxlint --type-aware: exit 0;
  • bunx knip: exit 0;
  • oxfmt --check: clean.

@yannickmonney

Copy link
Copy Markdown
Contributor

The autonomous cycle now lands each task through the merge train instead of its own pull request (.agents/repo.md on train), so this pull request closes. The branch fix/rag-settle-requeue-and-lane-cron stays and the task goes on with it: the Reviewer reviews the branch's head on the task and squashes accepted work onto train.

tale-code-bot added a commit that referenced this pull request Oct 30, 2026
The guard read its forwarded ticks as the queue names they carried, and
string_agg skips nulls: a __pgboss__send-it job whose data named no queue
read as none, and the run passed. It now counts the forwards since boot and
fails on any; the names, a missing one shown as such, only say where they
went (review of #5097).

Refs #4158
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.

Follow-ups from the #4145 review: one harness-wide cron switch for the integration lanes, and RAG settle writes that overwrite a concurrent re-queue

3 participants