Skip to content

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

Description

@objectstack-fleet

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

Filing gate: ② seat split. Source: contract review 7252567666 on PR #38509 (#38413), finding N1 (nit, follow-up card): "the batch claim and settle are 2N sequential single-row writes (claimBatch reads maxBatch * 3 full rows, then writes one row at a time; the settle loop …), where the renewal already shows the one-write id $in shape. The claim write is by id alone, so two nodes draining at once can both claim the same rows." Carried in PR #38509's Acceptance notes as carrier: follow-up card owed. Child of epic #37000.
Grading: area:ai · Task (performance) · p3 (PR #38509 already drains 50k in about 4 minutes on a local store; this is the next floor) · reach: deployments whose database queue sits on a remote store and that bulk-import into a record-sourced knowledge source · evidence: dev report 7253420539 measured about 1.8 ms of claim plus settle writes per message on local SQLite; not measured on a remote store.

维护者速读

What

  1. On the batch path (DbQueueAdapter batch loop, PR feat(service-knowledge): record sync consumes the database queue in batches of 100, and a large backlog is reconciled #38509), claim the selected rows with one compare-and-set write over the id set and settle the completed rows with one write, as the lease renewal already does, keeping one outcome per message (a failed row still retries and dead-letters alone).
  2. Measure first whether the claim write, matched by id alone, lets two nodes claim the same row, on the batch path and on the main loop's claimBatch (which the per-message path uses on main). If it does, the claim must match the row's unclaimed state too; a reproduction is the test.
  3. Keep per-message queues' behaviour and pacing unchanged (ruling 7250307515 condition: mail send, mail attachment reclaim, API-flow subscriptions untouched), unless step 2 finds the double claim on the main loop as well, in which case that fix is reported as a fork of its own.

Acceptance

Blocked-by: #38509
Not in scope: the knowledge worker; the queue contract.
Dedupe: batch claim single-row writes 309 (none on this; #38413 tops the list) · double-claim two nodes queue 14 (none on this) · settle id in queue batch 492 (none on this).

Activity

  1. added
    pm:blocked
    area:ai
    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 (the database queue's batch claim and settle) · area:ai · domain:server · Task · p3 · pm:blocked (the body's Blocked-by: #38509, an open PR) · size S. Rationale: a performance floor on a remote store, not yet measured there; a Task stays p3 unless it blocks dispatched work. The two-node double-claim line in the body is a correctness question: if it is reproduced, it is a separate Bug card. Type set to Task.

  3. added
    domain:server
    on Nov 29, 2026
  4. objectstack-fleet commented on Nov 29, 2026

    @objectstack-fleet
    ContributorAuthor

    repo:objectstack#5 · os-project-manager · session_01FEkQ5oQLfL8thb6wtUHnTb
    Claim: PM loop round 1 · 2026-11-29T10:15Z
    Session: session_01FEkQ5oQLfL8thb6wtUHnTb
    Account: os-project-manager
    Branch: claude/issue-38548-db-queue-batch-writes
    Worktree: objectstack-issue-38548
    Domain: domain:server
    Seat: repo:objectstack#5
    Area: o5-ai37000
    File surface: packages/services/service-queue/src/db-queue-adapter.ts (the batch loop's claim and settle writes, and claimBatch's claim match only if step 2 reproduces a double claim), packages/services/service-queue/src/db-queue-adapter.test.ts and a new packages/services/service-queue/src/db-queue-batch-writes-38548.test.ts, one changeset .changeset/38548-db-queue-batch-writes.md (stop on breach; explain in the report).
    Container & model: M, mode:subagent, model: default (no path-derived mandate), effort: high (M)
    Clause-②: no
    Metadata-protocol: none
    Responsibility: introduced by PR #38509 (#38413): its batch loop claims and settles one row write at a time (contract review 7252567666, N1).
    Thread-read: none
    Premise: holds at main a5fe764+ per the card body (dev report 7253420539: about 1.8 ms of claim plus settle writes per message on local SQLite).
    Serial constraints cleared: check-handover --inflight at main: clear; overlap-check before every push
    Pre-claim lane: packages/services/service-queue/** ⇒ domain:server (file ownership only); filed by the seat as #38413's follow-up under #37000.
    Pre-claim deps: #38509 merged (a5fe764); pm:blocked lifted in this stroke.
    Prior rulings read: ruling 7250307515 on #38413 (optional batch-consume member; per-message queues' behaviour and pacing untouched: mail send, mail attachment reclaim, API-flow subscriptions).

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

    @objectstack-fleet
    ContributorAuthor

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

    {
      "issue": 38548,
      "status": "blocked",
      "branch": "claude/issue-38548-db-queue-batch-writes",
      "pr": "https://github.com/objectstack-ai/objectstack/pull/38575",
      "head": "e0c5953f742b6dd983622231db0f440296dd03f2",
      "session": "session_01FEkQ5oQLfL8thb6wtUHnTb",
      "premise_still_valid": true,
      "summary": "Step 1, measured before any change: the claim write is a by-id update matched by id alone, so two adapters over one store that both read a row as pending both claim and dispatch it. Reproduced on the batch path (fake engine: 20 messages, 40 claims, each handled twice; real ObjectQL over SqlDriver/better-sqlite3: 200 messages, claimedA 100 and claimedB 100, all 100 handled twice) AND on the main loop (5 per-message messages, each handled twice). Batch path fixed: claimSet takes up to maxBatch due candidates and claims them with ONE multi:true write over id $in, matched on status pending and due as well; when fewer rows match, one read of the ids it holds says which it won. Completed rows settle in one write per attempt count with the lease renewal's where (id $in, running, locked_by me); a failed row still settles alone via settleRow; a set settle that throws falls back to per-row settles. Main loop untouched (ruling 7250307515): claimBatch keeps its per-row by-id claim and max*3 over-fetch, its read and claimability check moved into shared helpers with identical predicates. 50k drain on SQLite: 63,896 ms and 100,000 writes before, 7,865 ms and 1,000 writes after (8.1x). BLOCKED on one thing only the seat can solve: check-tenant-audit-census goes red (write call sites 11 to 13) and its fix regenerates docs/audits/2026-08-tenant-audit-write-call-sites.counts/services/service-queue.md, which is outside the claim's file surface; not committed, so the PR's Lint and Repo Gates will be red until the surface is widened. Everything else is done and pushed.",
      "tests": "service-queue suite on 2f602bfc (head e0c5953f adds only the changeset): Test Files 5 passed (5), Tests 106 passed (106), VERDICT command-exit 0; typecheck VERDICT command-exit 0, tsc --listFiles reaches all 5 test files. Knowledge batch-sync, real kernel over SqlDriver, after rebuilding service-queue dist (claimSet x3 in dist): record-sync-queue.test.ts Tests 41 passed (41). Step 1 against the unchanged adapter: 3 failed / 1 passed ({claim:250,settle:250} vs {claim:3,settle:3}; {claim:6,settle:6} vs {claim:1,settle:3}; 'expected 40 to be 20'; CONTROL passed). Ablations via scripts/ablation-replace.mjs on committed code, each restored to blob 8ccbd947 with git diff HEAD empty: (1) claimSet call replaced by the per-row claimBatch: 3 red, '{ claim: 250, settle: 3 }' vs '{ claim: 3, settle: 3 }', 'expected 40 to be 20'; (2) settleCompleted replaced by per-row settles: 2 red, '{ claim: 3, settle: 250 }'; (3) status pending removed from the claim match: 1 red, the two-adapter case 'expected 40 to be 20'. No dist step needed: the unit suite imports ./db-queue-adapter.js from src.",
      "measurement": "Local store as PR #38509: ObjectQL over SqlDriver, better-sqlite3 file, sys_job_queue registered by QueueServicePlugin; no-op batch handler, maxBatch 100, pollBatchOnce until 0; under the verify lock on a shared box (ratio is the reading). N=50,000: base d08d2ae972 drain 63,896 ms, 1.278 ms/msg, 783 msg/s, 100,000 queue updates, 501 queue reads; head drain 7,865 ms, 0.157 ms/msg, 6,357 msg/s, 1,000 updates, 501 reads. Remote store: NOT MEASURED (none reachable).",
      "gates": "dispatch-gates --commands --repo objectstack-ai/objectstack derived 65 for 3 paths (merge base d08d2ae97); exit codes captured before any pipe. 61 exit 0 (check:engine-double-contract 'OK — 1403 pinned, 119 in the DEBT ledger, 3 exempt'; check:nul-bytes OK; check:type-check-coverage; check:where-matcher; ...). RED 2: node scripts/check-tenant-audit-census.mjs and --self-test, exit 1, '[census-drift] ... service-queue.md ... committed Write call sites 11, census 13' (file-surface breach, see summary). NOT MEASURED 1: pnpm check:dual-build-cjs-loads exit 3 PREREQUISITE NOT MET (65 published packages without dist; full build). --ran: '65 derived famil(ies) accounted for — 64 run, 1 NOT-MEASURED'. overlap-check --branch=HEAD: origin/main merges cleanly, VERDICT clean exit 0. check-platform-checklist not owed (no exported symbol renamed or removed; only private methods). CI check-runs on PR #38575: NOT MEASURED, report delivered at the end of local verification.",
      "dogfood_battery": "n/a — no '!' in the PR title and Clause-②: no without a (narrowing) arm",
      "mcp_calls": "0",
      "api_writes": "2 — (1) relay pr_create via POST /repos/objectstack-ai/objectstack/dispatches (run 46256103638), executing POST /repos/objectstack-ai/objectstack/pulls (draft) + POST /repos/objectstack-ai/objectstack/issues/38575/assignees; read-back 10601 bytes sent and stored, identical, assignee os-project-manager. (2) this report, POST /repos/objectstack-ai/objectstack/issues/38548/comments via post-stamped.mjs. git push (3 commits) is not a REST write.",
      "open_questions": [
        {
          "question": "The main loop's claimBatch (per-message queues: mail send, mail attachment reclaim, API-flow subscriptions) claims by id alone; two nodes draining one per-message queue both run a message. Reproduced by the CONTROL case in db-queue-adapter.test.ts (5 messages, each handled twice). Fix it?",
          "options": [
            "A: its own card: the same set-write CAS on claimBatch (one claim write per tick, matched on pending and due); per-tick pacing (batchSize per pollIntervalMs) unchanged; about one small PR plus a test flip of the CONTROL case; needs the seat to lift ruling 7250307515's 'untouched' condition for the claim write",
            "B: leave it: duplicate delivery is tolerated by the at-least-once contract the claim comment cites; zero work, but a mail can be sent twice on a multi-node deployment"
          ],
          "recommendation": "A, because the double run is user-visible for mail send on any multi-node deployment and the fix is the shape this PR already lands and tests"
        },
        {
          "question": "check-tenant-audit-census needs docs/audits/2026-08-tenant-audit-write-call-sites.counts/services/service-queue.md regenerated (node scripts/tenant-audit-census.mjs --write: 8 lines, counts 11 to 13, update 8 to 10). It is outside the claimed file surface. How should it land?",
          "options": [
            "A: the seat widens the claim's file surface by that one generated file and sends a patch round; one commit, mechanical",
            "B: the seat regenerates and pushes it onto the branch itself"
          ],
          "recommendation": "A, because the file is a pure derivative of this diff and a patch round keeps the branch single-author"
        }
      ],
      "security_findings": "none",
      "out_of_scope_findings": [
        "class: a · reach: named producer DbQueueAdapter.claimBatch, the main loop's claim for every per-message queue (mail send, mail attachment reclaim, API-flow subscriptions) · evidence: CONTROL case 'the main loop's claim, matched by id alone and unchanged here, lets both adapters claim the rows they both read' (5 messages, each handled twice) · carrier: the seat, open question 1 · dedupe words: double-claim main loop queue, claimBatch by id, per-message duplicate delivery"
      ],
      "deviations": [
        "No new test file: the claim names db-queue-batch-writes-38548.test.ts, but ruling record 6583487846 puts a new pin for a component with a shared harness into that file, so the 4 cases are one new top-level describe in db-queue-adapter.test.ts. That also kept the engine-double ledgers (outside the surface) untouched; the shared fake's update now honours multi:true predicate writes by hand and stays on its DEBT row.",
        "Conflict noted, not resolved silently: the claim's file surface allowed changing claimBatch's claim match if a double claim reproduced; the order (step 3) forbids fixing the main loop in this PR. I followed the order (it matches ruling 7250307515).",
        "To size the census fix, node scripts/tenant-audit-census.mjs --write was run once in the worktree and the file restored with git restore --source=HEAD --staged --worktree (git status clean afterwards); nothing outside the surface was committed.",
        "The first unclaimed-state ablation was refused by ablation-replace (its replacement text already occurred in the file, so the count could not rise); it changed nothing and was restored. Re-run with a distinct marker; that run is the reading above.",
        "The batch claim's candidate read now takes maxBatch rows instead of maxBatch * 3 (a single CAS write gains nothing from over-fetching). Noted in Acceptance notes.",
        "Worktree removed after the PR opened; all work is on origin/claude/issue-38548-db-queue-batch-writes at e0c5953f."
      ]
    }
  7. objectstack-fleet commented on Nov 29, 2026

    @objectstack-fleet
    ContributorAuthor

    repo:objectstack#5 · os-project-manager · session_01FEkQ5oQLfL8thb6wtUHnTb
    Claim amendment · 2026-11-29T11:16Z · amends claim 7254316441 on this card
    File surface: adds docs/audits/2026-08-tenant-audit-write-call-sites.counts/services/service-queue.md (regenerated by node scripts/tenant-audit-census.mjs --write only; no hand edit).
    Reason: dev report 7254980060 on PR #38575, open question 2: check-tenant-audit-census goes red on the two new write call sites (11 to 13); the counts file is a pure derivative of this diff. Answer A, a patch round on the same branch. The dev's other deviation stands accepted: the four cases live in db-queue-adapter.test.ts's shared harness (ruling 6583487846), so the new test file named above is not created.

  8. objectstack-fleet commented on Nov 29, 2026

    @objectstack-fleet
    ContributorAuthor

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

    {
      "issue": 38548,
      "status": "done",
      "branch": "claude/issue-38548-db-queue-batch-writes",
      "pr": "https://github.com/objectstack-ai/objectstack/pull/38575",
      "head": "1515a3ea7ddc92dc08ab36057a768a4ee611ca4c",
      "session": "session_01FEkQ5oQLfL8thb6wtUHnTb",
      "premise_still_valid": true,
      "summary": "Patch round: docs/audits/2026-08-tenant-audit-write-call-sites.counts/services/service-queue.md was regenerated by node scripts/tenant-audit-census.mjs --write alone (8 lines: counts 11 to 13, update 8 to 10) and committed by itself as 1515a3ea. The file was added to the surface by claim amendment 7255002837. claimBatch and the CONTROL case are unchanged. The PR body was patched so the gates note shows the census green, with the signed trailer as the last line. Unchanged from the first round: the batch path claims with ONE compare-and-set multi:true write over id $in, matched on pending and due. Completed rows settle in one write per attempt count with the renewal's where, and a failed row still settles alone. The 50k SQLite drain went from 63,896 ms and 100,000 writes to 7,865 ms and 1,000 writes (8.1x). The step 1 double claim reproduces on both paths; only the batch path is fixed here, and the main loop goes to the seat as its own card.",
      "tests": "Head 1515a3ea, in a recreated worktree after building service-queue's dependency closure (15 turbo tasks) under the lock: pnpm --filter @objectstack/service-queue test, Test Files 5 passed (5), Tests 106 passed (106), VERDICT command-exit 0. The first attempt in the fresh worktree exited 1 before any test ran: 'Failed to resolve entry for package @objectstack/objectql' (no dist yet), a prerequisite failure, not a red test. The code is unchanged since 2f602bfc. First-round readings still stand: typecheck VERDICT 0; knowledge record-sync-queue.test.ts Tests 41 passed (41) on the real SqlDriver kernel; step 1 against the unchanged adapter 3 failed / 1 passed. Ablations, each restored to blob 8ccbd947: claim set write reverted, 3 red ('{ claim: 250, settle: 3 }', 'expected 40 to be 20'); settle set write reverted, 2 red ('{ claim: 3, settle: 250 }'); unclaimed-state match removed, 1 red ('expected 40 to be 20').",
      "measurement": "Local store as PR #38509: ObjectQL over SqlDriver, better-sqlite3 file, sys_job_queue registered by QueueServicePlugin; no-op batch handler, maxBatch 100, pollBatchOnce until 0; under the verify lock on a shared box (ratio is the reading). N=50,000: base d08d2ae972 drain 63,896 ms, 1.278 ms/msg, 783 msg/s, 100,000 queue updates, 501 queue reads; head drain 7,865 ms, 0.157 ms/msg, 6,357 msg/s, 1,000 updates, 501 reads. Remote store: NOT MEASURED (none reachable).",
      "gates": "On 1515a3ea: node scripts/check-tenant-audit-census.mjs exit 0 ('OK -- 354 write call sites certified across 19 package shard(s)'); --self-test exit 0 ('29 cases pass'). overlap-check --branch=HEAD, run and read before the push: 'origin/main: merges cleanly' (a133c8ad), VERDICT clean exit 0. First-round derived set on e0c5953f: 65 derived, 62 exit 0, 2 red (the census pair, now green), 1 NOT MEASURED (pnpm check:dual-build-cjs-loads, exit 3 PREREQUISITE NOT MET, needs a full build; CI runs it). --ran: '65 derived famil(ies) accounted for — 64 run, 1 NOT-MEASURED'. The first-round report said '61 exit 0'; the count from the recorded codes is 62. CI check-runs on PR #38575 head 1515a3ea: NOT MEASURED, because the report was delivered when local verification ended.",
      "dogfood_battery": "n/a — no '!' in the PR title and Clause-②: no without a (narrowing) arm",
      "mcp_calls": "0",
      "api_writes": "4 across both rounds. Round 1: (1) relay pr_create, POST /repos/objectstack-ai/objectstack/dispatches (run 46256103638) executing POST /pulls (draft) + POST /issues/38575/assignees; (2) report comment 7254980060, POST /issues/38548/comments via post-stamped. This round: (3) relay issue_patch on #38575, POST /repos/objectstack-ai/objectstack/dispatches (run 46256742202), 11196 bytes sent and stored, identical, signed trailer last; (4) this report, POST /repos/objectstack-ai/objectstack/issues/38548/comments via post-stamped. git push e0c5953f..1515a3ea (fast-forward) is not a REST write.",
      "open_questions": [],
      "security_findings": "none",
      "out_of_scope_findings": [
        "class: a · reach: named producer DbQueueAdapter.claimBatch, the main loop's claim for every per-message queue · evidence: CONTROL case in db-queue-adapter.test.ts (5 messages, each handled twice) · carrier: the seat, which takes it as its own card (coordinator's answer to open question 1) · dedupe words: double-claim main loop queue, claimBatch by id, per-message duplicate delivery"
      ],
      "deviations": [
        "dev-worktree.sh --resume-from refused because the local branch from round 1 still existed. Its tip equalled origin's (e0c5953f) and no worktree held it, so I attached the worktree to it with git worktree add and ran pnpm install --frozen-lockfile.",
        "Accepted by the claim amendment: the 4 cases live in db-queue-adapter.test.ts's shared harness (ruling record 6583487846); no new test file.",
        "The PR body's first-round gates line said 61 exit 0; corrected to 62 in the patch.",
        "Worktree removed after this round; all work is on origin/claude/issue-38548-db-queue-batch-writes at 1515a3ea."
      ],
      "round": "patch round 1 on PR #38575, answering the claim amendment 7255002837 (open question 2, answer A)"
    }
  9. objectstack-fleet commented on Nov 29, 2026

    @objectstack-fleet
    ContributorAuthor

    repo:objectstack#1 · zhuangjianguo · session_01QsR2jTsdScTh3o2fSjbxSz

    2026-11-29T12:04Z · Takeover · seat repo:objectstack#5's account os-project-manager is out of tokens; seat 1 continues its claim.

    Release: claim 7254316441 (session session_01FEkQ5oQLfL8thb6wtUHnTb, account os-project-manager, seat repo:objectstack#5) — taken over, not abandoned. Provenance: who = the maintainer (Jianguo); words, verbatim: 「hotlong 和 5号座位os-project-manager没token了,他的任务你都继续处理完」; where = seat 1's project chat, 2026-11-29T11:57:54Z.
    Handover: branch claude/issue-38548-db-queue-batch-writes last pushed 1515a3ea7d; PR #38575. Picked-up work is UNVERIFIED until seat 1 re-reads it at that head.
    Assignee: os-project-manager → zhuangjianguo in this stroke.

    Claim: PM loop round 3 (takeover of 7254316441)
    Session: session_01QsR2jTsdScTh3o2fSjbxSz
    Account: zhuangjianguo
    Branch: claude/issue-38548-db-queue-batch-writes
    Worktree: objectstack-issue-38548
    Domain: domain:server
    Seat: repo:objectstack#1
    Area: o1-know
    File surface: packages/services/service-queue/src/db-queue-adapter.ts (the batch loop's claim and settle writes, and claimBatch's claim match only if step 2 reproduces a double claim), packages/services/service-queue/src/db-queue-adapter.test.ts and a new packages/services/service-queue/src/db-queue-batch-writes-38548.test.ts, one changeset .changeset/38548-db-queue-batch-writes.md (stop on breach; explain in the report).
    Container & model: M, mode:subagent, model: default (no path-derived mandate), effort: high (M)
    Clause-②: no
    Metadata-protocol: none
    Responsibility: introduced by PR #38509 (#38413): its batch loop claims and settles one row write at a time (contract review 7252567666, N1).
    Thread-read: 7255058646
    Premise: holds at main a5fe764+ per the card body (dev report 7253420539: about 1.8 ms of claim plus settle writes per message on local SQLite).
    Serial constraints cleared: check-handover --inflight at main: clear; overlap-check before every push
    Pre-claim lane: packages/services/service-queue/** ⇒ domain:server (file ownership only); filed by the seat as #38413's follow-up under #37000.
    Pre-claim deps: #38509 merged (a5fe764); pm:blocked lifted in this stroke.
    Prior rulings read: ruling 7250307515 on #38413 (optional batch-consume member; per-message queues' behaviour and pacing untouched: mail send, mail attachment reclaim, API-flow subscriptions).

  10. removed
    pm:dispatched
    on Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:aidomain:serverpriority:p3

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions