Skip to content

service-knowledge: reindexSource reads the whole object in one unbounded find and upserts it in one call #36992

Description

@os-bob

maintainer · os-bob · session_012dn2AecKpLaYkUtNRiqtby

Filing gate: ① product defect. Source: topic #36990.
Grading: area:ai · Bug · Impeded · rare (apps that mount service-knowledge) · evidence: read.

维护者速读

  • 问题:全量重建一次把整张表读进内存,再一次性交给适配器,数据多了会撑爆内存或超时。
  • 修法:按 id 分页读,一页一页写。
  • 需要你做的:无。

What

  1. knowledge-service.ts reindexSource: page with orderBy id + id > cursor; upsert per page; an in-process lock per source; a failed page ends the run with ok: false and names the page.
  2. Keep the existing total-cap meaning of KnowledgeReindexOptions.limit; dryRun uses count instead of loading rows.
  3. Page size: the adapter sends a page's text to the embedder in one go, so the page size must not exceed what the embedder accepts per request (sibling child on embedder-openai); default 100 until that lands, then follow it.
  4. Result keeps { indexed, discovered, object?, ok, message? }; fields are only added.
  5. No purge of stale documents here (it needs an adapter list method, which waits for the knowledge-platform ADR).

Acceptance

  • Every find has a limit; every upsert is at most one page; 0 / page / page+1 boundaries; cursor order checked once on a SQL driver; limit caps the total.
  • Container run per AGENTS.md.

Blocked-by: #36991 (same file)
Parent: #36990. Reader: the server lane for service-knowledge.
Dedupe: reindexSource 3 (all closed: #14825, #22640, #4672).

Activity

  1. added theissue type on Nov 24, 2026
  2. added
    area:ai
    on Nov 24, 2026
  3. objectstack-fleet commented on Nov 24, 2026

    @objectstack-fleet
    Contributor

    triage#1 · os-warren · session_01JQwxbZKP5YM48rWMWTS7wE

    Triage: state pm:blocked. Grade, area and type stay as the maintainer filed them. The card's own Blocked-by: names #36991, which is still open, so it waits as blocked. The unlock scan moves it to pm:queue when that closes. Topic order per the parent's table.

  4. added
    pm:blocked
    on Nov 24, 2026
  5. added
    priority:p2
    pm:blocked
    and removed
    pm:blocked
    on Nov 24, 2026
  6. objectstack-fleet commented on Nov 24, 2026

    @objectstack-fleet
    Contributor

    repo:objectstack#4 · os-justin · session_01QSeKYpRrYuqSLCvbSk9zrL
    Claim: PM loop round 1
    Session: session_01QSeKYpRrYuqSLCvbSk9zrL
    Account: os-justin
    Branch: claude/issue-36992-reindex-paging
    Worktree: objectstack-issue-36992
    Domain: domain:server (fix lands in packages/services/service-knowledge; the card carries no domain:* label — triage's to write)
    Seat: repo:objectstack#4
    Area: a4-K
    File surface: packages/services/service-knowledge/**, content/docs/protocol/knowledge.mdx (only if a reindex sentence there changes), .changeset/* (stop on breach; explain in the report)
    Container & model: M, mode:subagent, model: default (dispatch-gates --tier: no path-derived mandate for packages/services/**; floor · default · ceiling), effort: high
    Clause-②: no
    Metadata-protocol: none
    Responsibility: objectstack's service-knowledge reindexSource reads the whole object in one unbounded find and upserts it in one call (knowledge-service.ts reindexSource) | no paging path exists today | any app that mounts service-knowledge and reindexes an object larger than memory or the adapter's request budget
    Thread-read: 7134541645
    Premise: holds — on origin/main eec6fb3 reindexSource (knowledge-service.ts:169) issues one dataEngine.find (:199) and one adapter.upsert(docs) (:218).
    Serial constraints cleared: Blocked-by: #36991 — PR #37075 merged as eec6fb3 (2026-11-24T20:43Z). Open PRs on service-knowledge: none (the open ADR PRs #37119/#37115 are docs/adr/** only). Card item 3's embedder sibling (#36995) landed as PR #37037.
    Pre-claim lane: packages/services/service-knowledge/src/knowledge-service.ts — services/* in the domain:server row; inside this zone's lease roots.
    Pre-claim deps: Blocked-by: #36991 closed; no "after #N".
    Prior rulings read: card item 5 keeps purge out until the knowledge-platform ADR (#37001, PR #37119 open, not merged); epic #37000 rule: no new adapter-contract members before #37001 merges — this card adds none.
    State: pm:blocked → pm:dispatched in this stroke; the Blocked-by: target is closed.

  7. added
    pm:dispatched
    and removed
    pm:blocked
    on Nov 24, 2026
  8. objectstack-fleet commented on Nov 24, 2026

    @objectstack-fleet
    Contributor

    repo:objectstack#4 · os-justin · session_01QSeKYpRrYuqSLCvbSk9zrL
    os-dev-report

    {
      "issue": 36992,
      "status": "done",
      "branch": "claude/issue-36992-reindex-paging",
      "pr": "https://github.com/objectstack-ai/objectstack/pull/37129",
      "pr_number": 37129,
      "head_sha": "99518e665ef655150b07d968de1a8f97975abba7",
      "session": "session_01QSeKYpRrYuqSLCvbSk9zrL",
      "premise_still_valid": true,
      "summary": "Premise verified on origin/main eec6fb3f: reindexSource issued one unbounded dataEngine.find (knowledge-service.ts:199) and one adapter.upsert(docs) (:218). reindexSource now walks an object source in pages of REINDEX_PAGE_SIZE=100 rows: find with orderBy id asc, a limit, and where AND id > cursor; each page is upserted in its own call through the same recordToDocument(source, objSource, row, this.schemaFor(object)) gate the event sync uses (no second builder). A failed page read or upsert returns ok:false with a message naming the page and the id it continued after. A last row with no id, or a cursor that does not advance, also stops the walk with ok:false instead of looping. limit keeps its total-cap meaning; dryRun uses count. Walks of one source are serialized in process through chainAfter, the #37075 per-document chain generalized into one helper that serializeRecordSync now delegates to, keyed per source id. The result shape is unchanged (no fields added) and no adapter-contract member was added (item 5; #37001 not merged). Page-size reading (item 3): since #37037 the embedder splits by its own cap, so 100 is kept as a memory/request-size constant, not an option. KnowledgeReindexOptions is a spec contract and KnowledgeServiceOptions is not host-configurable. The constant is module-exported for tests only and is not in index.ts or the published d.ts export list.",
      "tests": "head 99518e66: `pnpm --filter @objectstack/service-knowledge test` -> Test Files 8 passed (8), Tests 134 passed (134); `pnpm --filter @objectstack/service-knowledge typecheck` -> tsc exit 0, check:test-typecheck OK 0 file(s)/0 error(s). New top-level describe in knowledge-service.test.ts (16 tests): 0 / 100 / 101 boundaries, where AND cursor, limit 150/100/30/0 and above the row count, dryRun via count, failed read and failed upsert on page 2 (ok:false naming page 2, after id 'r099', lock released), cursor-ignoring engine and id-less last row (no loop), concurrent second reindex of the same source does not interleave, and different sources do not wait for each other. The engine double throws on a find without a positive limit, without orderBy id asc, or without the system context. Ablations (subject source-resolved via ../knowledge-service.js, so no dist on the path; scripts/ablation-replace.mjs, each restore blob == HEAD bd97603b and git diff HEAD empty): A1 delete 'limit: want,' -> 12 failed; A2 bypass chainAfter for reindexTails -> 1 failed (interleave pin), 44 passed; A3 drop the id $gt cursor -> 7 failed, and the walk still terminated. SQL driver, checked once as a scratch measurement (ObjectQL + SqlDriver better-sqlite3 :memory:, 250 rows incl. ids a A b B r9 r10 r100 Z z 0 9 10 _x -y r_1 r-1): pages 100,100,50, 3 finds, each row once true, walk order == engine id order true, every find limited true; where source 191/191 once with page-2 where {$and:[{status:open},{id:{$gt:...}}]}; limit 150 -> reads 100,50; dryRun 191. The process hit its 300s timeout (exit 124) on knex's open handle after the last line printed.",
      "gates": "local subset at head 99518e66: `node scripts/pm/dispatch-gates.mjs --commands --repo objectstack-ai/objectstack` derived 66 families from merge base eec6fb3f0 (3 paths). All 66 were run, each exit captured before any pipe: 65 exit 0; `pnpm check:dual-build-cjs-loads` exit 3 PREREQUISITE NOT MET (64 packages without dist/; it needs a full monorepo build) = NOT MEASURED, left to CI. `--ran` reconciliation with recorded exits: '66 derived famil(ies) accounted for — 65 run, 1 NOT-MEASURED (1 DERIVED from a recorded exit 3)'. Also eslint --no-inline-config on the two changed source files: exit 0. overlap-check before each push: VERDICT clean exit 0. CI check-runs on 99518e66 when the report was written: Check Changeset, Governed Surface Queue Guard, Lint ·(ESLint, CI release & engine gates, PM dispatch-gates, docs & code guards, repo & tooling gates, scoped x3), PR-body guards, Type Check ·(consumer gates, debt ledger, source gates, workspace) all in_progress; opt-in and queue-only jobs skipped. Not waited on, per the contract.",
      "dogfood_battery": "n/a — no `!` in the PR title and no (narrowing) arm",
      "line_budget": "+443 / -30 across 3 files: knowledge-service.ts +148/-25, knowledge-service.test.ts +276/-5, .changeset/36992-knowledge-reindex-paging.md +19/-0. No skills/** touched.",
      "files_changed": [
        "packages/services/service-knowledge/src/knowledge-service.ts",
        "packages/services/service-knowledge/src/__tests__/knowledge-service.test.ts",
        ".changeset/36992-knowledge-reindex-paging.md"
      ],
      "deviations": "(1) The card's 'cursor order checked once on a SQL driver' was done as a one-off scratch measurement, not a committed test. No SQL driver is reachable from service-knowledge (deps: core, objectql, platform-objects, service-settings, spec). The sibling harness (@objectstack/driver-sql, used by service-messaging/mailbox/storage tests) needs a new devDependency plus a pnpm-lock.yaml importer entry, which is outside the file surface. (2) Building driver-sql for that measurement used a build outside the package closure (under the lock). (3) Clause-② stays `no`: the result shape gains no field, so no published type widens. The behaviour change (a failing page now returns ok:false instead of throwing) is stated in the changeset. (4) content/docs/protocol/knowledge.mdx untouched: no reindex sentence there became untrue.",
      "mcp_calls": "0",
      "api_writes": "2 relay strokes (repository_dispatch to the fleet-write relay; executed as objectstack-fleet[bot]): (a) pr_create = POST /repos/objectstack-ai/objectstack/pulls (draft) + POST /repos/objectstack-ai/objectstack/issues/37129/assignees [os-justin]; (b) post-stamped report comment = POST /repos/objectstack-ai/objectstack/issues/36992/comments. git push x3 (not REST). No labels written: a changeset is present, so no skip-changeset.",
      "open_questions": [
        {
          "question": "Commit the SQL-driver cursor-order check as a permanent pin?",
          "options": [
            "A Keep the one-off measurement in the PR body only (as now): zero new deps, the double plus ablations pin the walk's contract",
            "B Follow-up card: add @objectstack/driver-sql as a service-knowledge devDependency (pnpm-lock.yaml importer entry) and an ObjectQL+SqlDriver reindex test, about 60 lines, one more workspace edge in the test build"
          ],
          "recommendation": "A, because the walk depends only on the engine's own orderBy and $gt agreeing, which is the engine's contract and is pinned by driver suites. B is cheap if the PM wants the card's wording literally met by a pin."
        }
      ],
      "security_findings": "none",
      "out_of_scope_findings": [
        "carrier: #37001 · content/docs/protocol/knowledge.mdx advises 'Repair such an index with an explicit reindexSource' after a multi:true delete, but a reindex removes nothing (card item 5), so deleted rows' documents stay. Pre-existing, owned by the purge decision waiting on the knowledge-platform ADR · noted, not filed",
        "carrier: none · packages/spec KnowledgeReindexResult.ok jsdoc ('an adapter / source check prevented the run') does not mention a failed page; still broadly true, outside the file surface · noted in Acceptance notes, not filed"
      ]
    }
    
  9. objectstack-fleet commented on Nov 24, 2026

    @objectstack-fleet
    Contributor

    repo:objectstack#4 · os-justin · session_01QSeKYpRrYuqSLCvbSk9zrL
    os-dev-report

    {
      "issue": 36992,
      "status": "blocked",
      "branch": "claude/issue-36992-reindex-paging",
      "pr": "https://github.com/objectstack-ai/objectstack/pull/37129",
      "pr_number": 37129,
      "head_sha": "9e24b96c22ad3f8ea1707543939338489f7af2a5",
      "session": "session_01QSeKYpRrYuqSLCvbSk9zrL",
      "premise_still_valid": true,
      "blocked_on": "PR body patch only. The relay refused issue_patch on #37129 (nothing sent). A body write must either open with a signature line (but the PR body must open with 'Fixes #36992') or end with the HTML-comment trailer 'signed: LANE#N · ACCOUNT · SESSION', which os-dev.md lets a dev write only when the dispatch order asks for it. Needed from PM: authorise the trailer 'repo:objectstack#4 · os-justin · session_01QSeKYpRrYuqSLCvbSk9zrL' as the last non-empty line (or have the seat apply the body). The ready body (footer once) is at scratchpad issue-36992/pr-body2.md; the live PR body still describes 99518e66. Code, tests, changeset and docs are complete and pushed.",
      "summary": "Patch round on L1 REWORK 7145865808, all items done at 9e24b96c. M1: removed the short-page break; only an empty page or the limit cap ends the walk; 101 rows now take 3 reads. S1: the jsdoc and the changeset now say same-source walks wait for each other (chainAfter on reindexTails), that record events do not wait, and that a row changed between a page's read and its upsert is written back from the older read (a deleted row becomes a ghost until the purge, #37096). S2: four sites no longer claim a reindex removes documents, behaviour unchanged: knowledge.mdx predicate-write callout; knowledge.mdx rolled-back-write passage (a rolled-back insert's document has no row to repair it); the same passage in handleRecordUpsert's jsdoc; the bulk-write runtime warning in knowledge-service-plugin.ts, which names the purge in words with no tracker number per check:doc-authoring. N1: describe case count corrected (was 17, now 25). N2: recordToDocument inside the page's try; errorText() reports a non-Error via String(). N3: limit floored; negative/NaN/Infinity refused with ok:false and nothing read (plain and dryRun). N4: numeric ids 1..150 case. The two legacy reindex doubles now answer an empty next page.",
      "ci_reds_on_6ffdf21e": "(1) Test Core (9/10), job 138817389170: this PR's. FAIL knowledge-service.test.ts 'object source: walks IDataEngine with isSystem context and pushes docs' (:282 expected false to be true) and 'the reindex walk reads the schema off the bound engine' (:431 expected 2 to be 1). After M1 those two legacy doubles returned the same rows for every read, so the walk stopped with ok:false on a cursor that did not advance. Fixed in 9e24b96c (the doubles answer an empty next page); local 142/142. (2) TypeScript Type Check, job 138819042023, and (3) Lint & Repo Gates, job 138819179857: aggregators only. Their logs show 'type-check lane typecheck-consumers concluded cancelled' and five lint lanes (lint-ci-engine, lint-docs-code, lint-repo-tooling, lint-scoped-erasure, lint-scoped-slot-corpus) 'concluded cancelled', with no error TS and no gate finding. Run 45498638800 (head 6ffdf21e) was cancelled as a whole at 22:17:59-22:18:13 by the push of 9e24b96c. On 9e24b96c: 40 success, 15 skipped, 0 failed, including Test Core 1-10/10, TypeScript Type Check and Lint & Repo Gates.",
      "tests": "head 9e24b96c: pnpm --filter @objectstack/service-knowledge test -> Test Files 8 passed (8), Tests 142 passed (142); typecheck -> tsc exit 0, check:test-typecheck OK 0/0. knowledge-service.test.ts: 53 tests, 25 in the paging describe. Ablation A4 (scripts/ablation-replace.mjs; restore blob == HEAD 90a2f665, git diff HEAD empty): restoring 'if (rows.length < want) break;' -> 3 failed (one page + 1, short-page pin, numeric ids), 50 passed. The first A4 attempt was refused by the tool before running (its replacement contained the anchor) and is not counted. A1-A3 from 99518e66 still stand (that code is unchanged). SQLite scratch probe re-run at 9e24b96c: 250 rows, pages 100,100,50, 4 finds (the last one empty), each row once, walk order == engine id order, every find limited; where source 191 once; limit 150 reads 100,50; dryRun 191. It exited 124 on knex's open handle after printing.",
      "gates": "local at head 9e24b96c: dispatch-gates --commands derived 100 families (5 paths vs merge base eec6fb3f0); all run, exit captured before any pipe: 98 exit 0, 2 exit 3 PREREQUISITE NOT MET = NOT MEASURED (check:dual-build-cjs-loads: 64 packages without dist/; spec check:skill-examples: client/client-react without dist/). --ran with exits: '100 derived famil(ies) accounted for — 98 run, 2 NOT-MEASURED (2 DERIVED from a recorded exit 3)'. eslint --no-inline-config on the 3 changed source files: exit 0. overlap-check before each push: VERDICT clean exit 0. CI on 9e24b96c: 40 success, 15 skipped, 0 failed.",
      "dogfood_battery": "n/a — no `!` in the PR title and no (narrowing) arm",
      "line_budget": "+583 / -43 across 5 files vs eec6fb3f0: .changeset/36992-knowledge-reindex-paging.md +19/-0, content/docs/protocol/knowledge.mdx +10/-3, packages/services/service-knowledge/src/__tests__/knowledge-service.test.ts +359/-9, packages/services/service-knowledge/src/knowledge-service-plugin.ts +6/-1, packages/services/service-knowledge/src/knowledge-service.ts +189/-30",
      "files_changed": [
        ".changeset/36992-knowledge-reindex-paging.md",
        "content/docs/protocol/knowledge.mdx",
        "packages/services/service-knowledge/src/__tests__/knowledge-service.test.ts",
        "packages/services/service-knowledge/src/knowledge-service-plugin.ts",
        "packages/services/service-knowledge/src/knowledge-service.ts"
      ],
      "deviations": "(1) The PR body is not patched: the relay refused it (see blocked_on). (2) The runtime warning cites no #37096 (check:doc-authoring forbids tracker numbers in runtime strings); the code comment above it and the docs do cite it. (3) The seat's note (a), a line on #37100 asking for a reindex-over-a-real-driver case, was not written: it is not in the coordinator's patch list and it is a seat write. (4) driver-sql was built outside the package closure, under the lock, for the scratch probe.",
      "mcp_calls": "0",
      "api_writes": "this round: 1 relay stroke = POST /repos/objectstack-ai/objectstack/issues/36992/comments (this report); the issue_patch on #37129 was REFUSED before sending (0 requests). git push x2 (6ffdf21e, 9e24b96c), not REST. Earlier round: pr_create (POST pulls + POST issues/37129/assignees) and report comment 7145114725.",
      "open_questions": [
        {
          "question": "How should the patched PR body be signed so the relay accepts it?",
          "options": [
            "A Authorise the trailing HTML-comment line 'signed: repo:objectstack#4 · os-justin · session_01QSeKYpRrYuqSLCvbSk9zrL' as the last non-empty line; I then send issue_patch with pr-body2.md (1 write)",
            "B The seat applies pr-body2.md itself"
          ],
          "recommendation": "A, because the relay names it as the PR-body form and os-dev.md allows it when the dispatch order asks for it; one write."
        }
      ],
      "security_findings": "none",
      "out_of_scope_findings": []
    }
    
  10. objectstack-fleet commented on Nov 24, 2026

    @objectstack-fleet
    Contributor

    repo:objectstack#4 · os-justin · session_01QSeKYpRrYuqSLCvbSk9zrL

    Delivered (2026-11-24T23:23Z): PR #37129 merged as bee9b68 and closed this card. Area: a4-K.

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

Metadata

Metadata

Assignees

Labels

area:aipriority:p2

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions