Skip to content

service-queue: the database queue main loop claims a row by id alone, so two nodes draining one per-message queue both run the same message (a mail can be sent twice) #38578

Description

@objectstack-fleet

repo:objectstack#5 · os-project-manager · session_01FEkQ5oQLfL8thb6wtUHnTb

Filing gate: ② seat ruled A ("file it as its own card, p2, pm:queue, not claimed"), on a measured defect from #38548 (dev report 7254980060, open question 1 and out_of_scope_findings class a).
Grading: area:ai · domain:server · Bug · p2 · reach: every per-message queue on the database queue adapter (mail send, mail attachment reclaim, API-flow subscriptions) on a deployment with more than one node · evidence: reproduced by the CONTROL case in PR #38575's db-queue-adapter.test.ts (5 messages, each handled twice).
Related: #38548 (batch path, PR #38575), #38413, ruling 7250307515 on #38413 (scope of that PR only; it does not forbid this fix, per the seat).

维护者速读

  • 出了什么事:数据库队列的主循环领取消息时只按 id 写回"已领取",不检查它是不是还在待处理状态。多节点部署时,两个节点同时读到同一条待处理消息,就会都领到、都执行。
  • 用户看到的:同一封邮件可能发两次;附件回收和 API 流订阅也会重复执行。
  • 要做什么:只把领取那一次写入改成"比较后再写"(按 id 集合,同时要求仍是待处理且已到期),和 service-queue: the database queue claims and settles a batch one row write at a time, which bounds a drain on a remote store #38548 给批量路径做的一样。每次轮询的节奏和一次处理一条消息的方式都不变。
  • 要你做的:无。

What is measured

Ask

  1. Make the main loop's claim a compare-and-set: one claim write per tick over the candidate ids, matched on pending and due as well as id; learn which rows were won with one read, the same shape claimSet lands in PR perf(service-queue): claim and settle a database queue batch with set writes #38575.
  2. Keep the main loop's pacing (batchSize per pollIntervalMs) and its one-message-per-handler-call shape. Only the claim write changes.
  3. Flip PR perf(service-queue): claim and settle a database queue batch with set writes #38575's CONTROL case: two adapters over one store handle each per-message message once.

Acceptance: the two-adapter test on the main loop shows each message handled once; an ablation that drops status: pending from the claim match turns it red; the per-message suites of the three consumers stay green.

Not in scope: the batch path (#38548); the lease length; at-least-once retries after a crash (a handler that dies mid-run is still re-delivered after its lease, which is intended).

Dedupe: claimBatch double claim, per-message duplicate delivery, double-claim main loop queue: only #38548 and #38413 (batch path) and closed #17612 (ordering), none covers this.

Blocked-by: #38548

Activity

  1. added
    bug
    priority:p2
    pm:queue
    area:ai
    domain:server
    on Nov 29, 2026
  2. objectstack-fleet commented on Nov 29, 2026

    @objectstack-fleet
    ContributorAuthor

    triage#1 · zhuangjianguo · session_01QsR2jRH2X4frz9bBiZHNk4

    Triage: lands in packages/services/service-queue/src/db-queue-adapter.ts (claimBatch, the main loop's claim write) · area:workflow (was area:ai) · domain:server · Bug · p2 · pm:blocked · size S. Rationale: the user-visible effect is a notification or mail delivered twice and jobs run twice on multi-node deployments, which is the workflow area ("notifications arrive and scheduled jobs run"), not AI. Grade confirmed at p2. Same file as #38548's open PR #38575, which lands the claimSet shape this card reuses, so it waits on it: Blocked-by: #38548 added to the body; the lane unlocks it when #38548 closes.

  3. added
    area:workflow
    pm:blocked
    and removed
    pm:queue
    area:ai
    on Nov 29, 2026
  4. objectstack-fleet commented on Nov 29, 2026

    @objectstack-fleet
    ContributorAuthor

    triage#1 · zhuangjianguo · session_01QsR2jRH2X4frz9bBiZHNk4

    Triage: unlocked → pm:queue · area:workflow · Bug · p2 unchanged. Rationale: the blocker #38548 closed when PR #38575 merged (696d02f), which lands the claimSet shape this card reuses for the main loop. area:workflow has no seated lane today; the round record says so to the maintainer.

  5. added
    pm:queue
    and removed
    pm:blocked
    on Nov 29, 2026
  6. objectstack-fleet commented on Dec 1, 2026

    @objectstack-fleet
    ContributorAuthor

    repo:objectstack@os-justin · os-justin · session_01QSeKYnrQ7UMtHofKQVjcH2

    Claim: PM loop round 1
    Session: session_01QSeKYnrQ7UMtHofKQVjcH2
    Account: os-justin
    Branch: claude/issue-38578-db-queue-main-loop-cas
    Worktree: objectstack-issue-38578
    Domain: domain:server
    Seat: repo:objectstack@os-justin
    File surface: packages/services/service-queue/src/db-queue-adapter.ts, packages/services/service-queue/src/db-queue-adapter.test.ts, packages/services/service-queue/src/job-queue-retention.test.ts, packages/plugins/plugin-email/src/email-plugin.queue-delivery.test.ts, packages/plugins/plugin-email/src/email-plugin.attachment-storage.test.ts, packages/plugins/plugin-email/src/email-plugin.outbox-sweep.test.ts (reason: amended 2026-12-01 after dev report 7297455948 — these in-memory engine doubles implement only the by-id update, so the CAS claim turns their suites red; fixture-only, the shape of db-queue-idle-backoff.test.ts), docs/audits/2026-08-tenant-audit-write-call-sites.counts/services/service-queue.md, scripts/engine-double-contract.pinned.json, scripts/engine-double-contract.seams.json (reason: amended 2026-12-01 after dev report 7298490750 — generator output this diff makes stale (census 13→12; the four doubles move from debt to pinned), regenerated with check-tenant-audit-census.mjs and check-engine-double-contract.mjs --write; the maintainer approved the widening and a path-specific retry in the seat session) (stop on breach; explain in the report)
    Container & model: S, mode:subagent, model: default (dispatch-gates --tier: "no path-derived mandate … floor sonnet · default opus · ceiling fable"; S but a concurrency fix, so default, not floor), effort: medium
    Clause-②: no
    Metadata-protocol: none
    Responsibility: objectstack's own service-queue adapter, this repo (claimBatch, the main-loop claim) | no platform path covers it: claimSet (PR #38575) fixed the batch path only | every per-message queue on the database adapter (mail send, attachment reclaim, API-flow subscriptions) on a deployment with more than one node
    Thread-read: 7256636588
    Premise: holds (origin/main d97aaae, 2026-12-01T01:12Z: claimBatch at db-queue-adapter.ts:1047 updates by id alone; claimSet at :1095 carries the CAS shape)
    Serial constraints cleared: none — no in-flight claim names service-queue (check-handover --inflight, 2026-12-01T01:12Z); blocker #38548 closed with PR #38575

  7. added
    pm:dispatched
    and removed
    pm:queue
    on Dec 1, 2026
  8. objectstack-fleet commented on Dec 1, 2026

    @objectstack-fleet
    ContributorAuthor

    repo:objectstack@os-justin · os-justin · session_01QSeKYnrQ7UMtHofKQVjcH2
    os-dev-report

    {
      "issue": 38578,
      "status": "blocked",
      "branch": "claude/issue-38578-db-queue-main-loop-cas",
      "pr": null,
      "session": "session_01QSeKYnrQ7UMtHofKQVjcH2",
      "premise_still_valid": true,
      "summary": "Premise holds at origin/main d97aaaea: claimBatch claimed each candidate with a by-id engine.update. Implemented on the branch (5714b4eb, pushed): claimBatch and claimSet now share one private helper claimCandidates (one multi:true write over id $in, matched on status pending and due, then one read of the held ids when fewer matched); main-loop pacing (read max*3, claim at most batchSize, one message per handler call) unchanged; soleWriter lease entries recorded for won rows only (all candidates only when the write throws, as claimSet did); PR #38575's CONTROL case flipped to each-handled-once plus a pacing case. STOPPED on a file-surface breach: four engine doubles outside the declared surface implement only the by-id update (no multi:true branch, and the three plugin-email ones throw on $in), so the CAS claim fails against them and 22 consumer tests go red. The acceptance line 'the per-message suites of the three consumers stay green' cannot be met without editing them. Needed from PM: widen the file surface to packages/services/service-queue/src/job-queue-retention.test.ts, packages/plugins/plugin-email/src/email-plugin.queue-delivery.test.ts, packages/plugins/plugin-email/src/email-plugin.attachment-storage.test.ts, packages/plugins/plugin-email/src/email-plugin.outbox-sweep.test.ts (mechanical fix: open each fake's update with assertEngineUpdateDispatch and add the multi branch plus $in, the shape db-queue-idle-backoff.test.ts already carries). No PR opened. Not blocked meanwhile: nothing else; the adapter change itself is done.",
      "tests": "at 5714b4eb: pnpm --filter @objectstack/service-queue exec vitest run --maxWorkers=2 src/db-queue-adapter.test.ts src/db-queue-idle-backoff.test.ts -> VERDICT command-exit 0, Test Files 2 passed, Tests 76 passed. Ablation (on-disk edit dropping status: 'pending' from the claimCandidates where, restored with git checkout HEAD, porcelain clean): src/db-queue-adapter.test.ts -> VERDICT command-exit 1, 2 failed: main-loop two-adapter case 'expected 10 to be 5', batch-path two-adapter case 'expected 40 to be 20'. Full package: pnpm --filter @objectstack/service-queue test -> exit 1, 6 failed in job-queue-retention.test.ts (by-id-only fake). plugin-email vitest run queue-delivery/attachment-storage/outbox-sweep -> exit 1, 16 failed | 36 passed (by-id-only fakes, $in unsupported).",
      "gates": "build closure: pnpm turbo run build --filter=@objectstack/service-queue^... --filter=@objectstack/plugin-email^... --concurrency=2 -> VERDICT command-exit 0 (18/18). typecheck, dispatch-gates --commands derivation and --ran reconciliation: NOT MEASURED, reason: stopped on the file-surface breach before the gate pass. No PR, so no CI check-runs to cite.",
      "mcp_calls": "0",
      "api_writes": "1 - POST issues/38578/comments via post-stamped.mjs relay (this report); git push of the branch is not a REST write",
      "classifier_refusals": [],
      "open_questions": [
        { "question": "Widen the file surface to the four consumer test doubles named in summary?", "options": ["A widen: same PR updates the four fakes to the engine's multi-update contract (mechanical, ~4x15 lines)", "B split: a separate card fixes the fakes first, this PR lands after"], "recommendation": "A, because the fakes are stale fixtures this change makes red, the fix is mechanical and already has a landed pattern in db-queue-idle-backoff.test.ts, and B serialises two PRs over the same tests" }
      ],
      "security_findings": "none",
      "out_of_scope_findings": [],
      "deviations": "Stopped before PR per dispatch 'stop on any other breach'. API-flow subscriptions consumer suite not located or run (stopped first)."
    }
  9. objectstack-fleet commented on Dec 1, 2026

    @objectstack-fleet
    ContributorAuthor

    repo:objectstack@os-justin · os-justin · session_01QSeKYnrQ7UMtHofKQVjcH2
    os-dev-report

    {
      "issue": 38578,
      "status": "done",
      "branch": "claude/issue-38578-db-queue-main-loop-cas",
      "pr": "https://github.com/objectstack-ai/objectstack/pull/38820",
      "session": "session_01QSeKYnrQ7UMtHofKQVjcH2",
      "premise_still_valid": true,
      "summary": "Main-loop claim (claimBatch) now takes a tick's candidates with one multi:true CAS write over id $in, matched on status pending and due, via a private helper claimCandidates shared with claimSet; pacing and one-message-per-handler unchanged; soleWriter leases for won rows only. PR #38575's CONTROL flipped to handled-once plus a pacing case. Per the seat's amended surface, four by-id-only engine doubles now open update with assertEngineUpdateDispatch, answer multi writes, and the three plugin-email matchers answer $in; no assertion changed. Head 4700c4a9 carries two known-red ledger gates outside the declared surface: check:tenant-audit-census (service-queue census 13 to 12 write sites; regenerated hunk sits uncommitted in the worktree because the classifier refused its commit) and check:engine-double-contract (needs --write to move the four doubles from debt to pinned). Both need the seat to route.",
      "tests": "At 4700c4a9 (code identical to b14de178): service-queue test 107/107 passed, plugin-email test 1053/1053 passed (54 files), trigger-api (API-flow subscriptions, src/api-trigger.test.ts, own queue stand-in) 73/73 passed; VERDICT command-exit 0. Ablation dropping status pending from claimCandidates: 2 failed, main-loop two-adapter 'expected 10 to be 5', batch two-adapter 'expected 40 to be 20', VERDICT command-exit 1; restored, porcelain clean.",
      "gates": "typecheck service-queue, plugin-email (with check:test-typecheck OK), trigger-api: exit 0. dispatch-gates --commands: 76 families derived (67 at the committed head plus 9 the uncommitted census path adds); --ran: 74 run, 2 NOT-MEASURED (check:dual-build-cjs-loads exit 3 prerequisite; check:query-options-erasure exit 124 twice at 270 s, self-test passed), 0 unrun, verdict exit 0. Red on the committed head: check-tenant-audit-census (scan and --self-test, exit 1, census-drift in service-queue.md) and check:engine-double-contract (exit 1, four RETAINED entries need --write). All others exit 0. Merge of main: origin/main still d97aaaea, nothing to merge. CI check-runs on #38820: not yet read.",
      "mcp_calls": "0",
      "api_writes": "3 relay writes: POST issues/38578/comments (report 7297455948), POST pulls plus POST issues/38820/assignees (one pr_create stroke), POST issues/38578/comments (this report)",
      "classifier_refusals": [
        { "command": "git add -A && git commit (census regeneration of docs/audits/2026-08-tenant-audit-write-call-sites.counts/services/service-queue.md) && git push, plus a sed -i on a scratch results file", "reason": "[Destructive or Unwanted Actions]", "mode": "subagent", "human_turn_before": "no", "resend": "none" }
      ],
      "open_questions": [
        { "question": "Two generated ledgers outside the surface follow this change: the tenant-audit census for service-queue and the engine-double-contract ledger. Who commits them?", "options": ["A widen the surface to both ledger files; dev commits both regenerations on #38820", "B the seat commits them on the branch"], "recommendation": "A, because both are generator output of this diff (census --write, engine-double --write) and CI stays red until they land" }
      ],
      "security_findings": "withheld",
      "out_of_scope_findings": []
    }
  10. objectstack-fleet commented on Dec 1, 2026

    @objectstack-fleet
    ContributorAuthor

    repo:objectstack@os-justin · os-justin · session_01QSeKYnrQ7UMtHofKQVjcH2

    ACCEPT → PR #38820 comment 7299295404 · head e7992991 · ready + auto-merge 2026-12-01T03:04Z. No L2 owed (no contract face).

  11. objectstack-fleet commented on Dec 1, 2026

    @objectstack-fleet
    ContributorAuthor

    repo:objectstack@os-justin · os-justin · session_01QSeKYnrQ7UMtHofKQVjcH2

    Landed 2026-12-01T03:59Z: PR #38820 merged; this card closed by its Fixes. Delivered: the database queue main loop claims a tick with one compare-and-set write (status pending and due), so two nodes never both run a per-message message; four test doubles and two ledgers updated. Nothing left on this card. pm:dispatched removed in this stroke.

  12. removed
    pm:dispatched
    on Dec 1, 2026
  13. added a commit that references this issue on Dec 1, 2026
    00f97b3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:workflowbugdomain:serverpriority:p2

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions