From 81e275fd0ea4cf1e9ae5281be54a69aa86f26493 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 26 Nov 2026 13:35:21 +0000 Subject: [PATCH 1/4] fix(cli): os knowledge rebuild falls back to the store's index state when a poll lands on another node Claude-Session: https://claude.ai/code/session_01Utp6Cb4BAjgQ43tqy1DvVJ Co-authored-by: Claude --- .../src/commands/knowledge/knowledge.test.ts | 133 +++++++++++++++--- .../cli/src/commands/knowledge/rebuild.ts | 99 +++++++++++-- packages/cli/src/commands/knowledge/status.ts | 17 ++- 3 files changed, 209 insertions(+), 40 deletions(-) diff --git a/packages/cli/src/commands/knowledge/knowledge.test.ts b/packages/cli/src/commands/knowledge/knowledge.test.ts index 409bbf3e84d..8d0bb9404d0 100644 --- a/packages/cli/src/commands/knowledge/knowledge.test.ts +++ b/packages/cli/src/commands/knowledge/knowledge.test.ts @@ -12,6 +12,12 @@ * the route it calls, the payload it sends, that it follows a started build * to its end, and that a build which did not finish exits 1. * + * Behind a load balancer a progress read can land on a node that holds no + * record of the build (the record lives in memory on the node running it): + * `nodeWithoutRecord` answers such a poll. The command then reads the + * store's index state, the same on every node, and never exits 1 for the + * poll alone. + * * The oclif `Config` is loaded at module scope, as in * `commands/datasource/envelope-unwrap.test.ts` (whose header says why). */ @@ -94,31 +100,39 @@ const running = (discovered: number) => ({ trigger: 'operator', status: 'running', startedAt: STARTED_AT, progress: { pages: 1, discovered, indexed: discovered, skipped: 0, failed: 0 }, }); -const statusOf = (build: unknown) => ok({ +const CURRENT = { state: 'current', due: false, detail: 'current.' }; +const NEVER_BUILT = { state: 'nothing', due: true, detail: 'never built.' }; +const statusOf = (build: unknown, index: unknown = CURRENT) => ok({ ok: true, message: '1 source(s)', - details: { sources: [{ id: 'kb', label: 'KB', adapter: 'pgvector', kind: 'object', index: { state: 'current', due: false, detail: 'current.' }, build }] }, + details: { sources: [{ id: 'kb', label: 'KB', adapter: 'pgvector', kind: 'object', index, build }] }, }); +/** A poll answered by a node that is not running this build: no record there. */ +const nodeWithoutRecord = (index: unknown) => statusOf(null, index); +const done = { + ...running(250), + status: 'done', + finishedAt: '2026-11-26T05:01:00.000Z', + progress: { pages: 3, discovered: 250, indexed: 240, skipped: 10, failed: 0 }, + result: { ok: true, discovered: 250, indexed: 240, skipped: 10, purged: 2, failed: 0 }, +}; const target = ['--url', SERVER, '--token', 'tok']; describe('os knowledge rebuild', () => { it('starts the rebuild through the settings action door and follows it to its end', async () => { const run = await drive(KnowledgeRebuild, ['kb', ...target], [ + statusOf(null), ok({ ok: true, message: "Rebuild of source 'kb' started.", details: { source: 'kb', build: running(0) } }), statusOf(running(100)), - statusOf({ - ...running(250), - status: 'done', - finishedAt: '2026-11-26T05:01:00.000Z', - progress: { pages: 3, discovered: 250, indexed: 240, skipped: 10, failed: 0 }, - result: { ok: true, discovered: 250, indexed: 240, skipped: 10, purged: 2, failed: 0 }, - }), + statusOf(done), ]); expect(run.failure).toBeUndefined(); - expect(run.requests[0]).toEqual({ url: `${SERVER}/api/settings/knowledge/rebuild`, body: { source: 'kb' }, auth: 'Bearer tok' }); - expect(run.requests.slice(1).map((r) => [r.url, r.body])).toEqual([ + // The index state is read once before the rebuild: the baseline a poll on another node is judged against. + expect(run.requests[0]).toEqual({ url: `${SERVER}/api/settings/knowledge/index_status`, body: { source: 'kb' }, auth: 'Bearer tok' }); + expect(run.requests[1]).toEqual({ url: `${SERVER}/api/settings/knowledge/rebuild`, body: { source: 'kb' }, auth: 'Bearer tok' }); + expect(run.requests.slice(2).map((r) => [r.url, r.body])).toEqual([ [`${SERVER}/api/settings/knowledge/index_status`, { source: 'kb' }], [`${SERVER}/api/settings/knowledge/index_status`, { source: 'kb' }], ]); @@ -130,6 +144,7 @@ describe('os knowledge rebuild', () => { it('exits 1 when the build did not finish', async () => { const run = await drive(KnowledgeRebuild, ['kb', ...target], [ + statusOf(null), ok({ ok: true, details: { source: 'kb', build: running(0) } }), statusOf({ ...running(10), status: 'failed', finishedAt: STARTED_AT, message: 'the adapter upsert failed: quota' }), ]); @@ -137,18 +152,93 @@ describe('os knowledge rebuild', () => { }); it('exits 1 with the server refusal text, and asks nothing more', async () => { - const run = await drive(KnowledgeRebuild, ['kb', ...target], [refused('A build of source \'kb\' is already running on this node.')]); - expect(run.failure).toMatchObject({ message: "A build of source 'kb' is already running on this node.", exit: 1 }); - expect(run.requests).toHaveLength(1); - }); - - it('says so when the progress is answered by a node that is not running this build', async () => { const run = await drive(KnowledgeRebuild, ['kb', ...target], [ - ok({ ok: true, details: { source: 'kb', build: running(0) } }), statusOf(null), + refused('A build of source \'kb\' is already running on this node.'), ]); - expect(run.failure?.exit).toBe(1); - expect(run.failure?.message).toContain('not running this build'); + expect(run.failure).toMatchObject({ message: "A build of source 'kb' is already running on this node.", exit: 1 }); + expect(run.requests).toHaveLength(2); + }); + + describe('a poll that lands on a node without the build record (behind a load balancer)', () => { + it('keeps following while the build is still running, and does not exit 1', async () => { + const run = await drive(KnowledgeRebuild, ['kb', ...target], [ + nodeWithoutRecord(NEVER_BUILT), + ok({ ok: true, details: { source: 'kb', build: running(0) } }), + nodeWithoutRecord(NEVER_BUILT), + statusOf(running(100), NEVER_BUILT), + nodeWithoutRecord(NEVER_BUILT), + statusOf(done), + ]); + expect(run.failure).toBeUndefined(); + expect(run.requests).toHaveLength(6); + const text = run.logs.join('\n'); + expect(text).toContain("answered by a node that is not running this build; the store's index state: nothing"); + expect(text).toContain('done (operator, ended 2026-11-26T05:01:00.000Z)'); + }); + + it('exits 0 once the store reports the index current after the build succeeded', async () => { + const run = await drive(KnowledgeRebuild, ['kb', ...target], [ + nodeWithoutRecord(NEVER_BUILT), + ok({ ok: true, details: { source: 'kb', build: running(0) } }), + nodeWithoutRecord(NEVER_BUILT), + nodeWithoutRecord(CURRENT), + ]); + expect(run.failure).toBeUndefined(); + expect(run.requests).toHaveLength(4); + expect(run.logs.join('\n')).toContain('The index is current'); + }); + + it('reads a record of an older build on the answering node as no record of this one', async () => { + const older = { ...done, startedAt: '2026-11-25T00:00:00.000Z' }; + const run = await drive(KnowledgeRebuild, ['kb', ...target], [ + nodeWithoutRecord(NEVER_BUILT), + ok({ ok: true, details: { source: 'kb', build: running(0) } }), + statusOf(older, NEVER_BUILT), + nodeWithoutRecord(CURRENT), + ]); + expect(run.failure).toBeUndefined(); + expect(run.logs.join('\n')).not.toContain('ended 2026-11-25'); + }); + + it('does not read an index that was already current as done; waits for the building node', async () => { + const run = await drive(KnowledgeRebuild, ['kb', ...target], [ + nodeWithoutRecord(CURRENT), + ok({ ok: true, details: { source: 'kb', build: running(0) } }), + nodeWithoutRecord(CURRENT), + nodeWithoutRecord(CURRENT), + statusOf({ ...done, status: 'failed', message: 'the adapter upsert failed: quota' }), + ]); + // Only the recorded failure, read on the node that ran the build, exits 1. + expect(run.failure).toMatchObject({ message: 'the adapter upsert failed: quota', exit: 1 }); + expect(run.requests).toHaveLength(5); + expect(run.logs.join('\n')).toContain('it was current, or unread, before this rebuild'); + }); + + it('exits 1 after --timeout seconds of polls that reach no record, and says the build may still run', async () => { + const run = await drive(KnowledgeRebuild, ['kb', '--interval', '2', '--timeout', '4', ...target], [ + nodeWithoutRecord(NEVER_BUILT), + ok({ ok: true, details: { source: 'kb', build: running(0) } }), + nodeWithoutRecord(NEVER_BUILT), + nodeWithoutRecord(NEVER_BUILT), + ]); + expect(run.failure?.exit).toBe(1); + expect(run.failure?.message).toContain('Stopped following after 4s'); + expect(run.failure?.message).toContain('may still be running'); + expect(run.requests).toHaveLength(4); + }); + + it('starts the timeout again each time a read reaches the build record', async () => { + const run = await drive(KnowledgeRebuild, ['kb', '--interval', '2', '--timeout', '4', ...target], [ + nodeWithoutRecord(NEVER_BUILT), + ok({ ok: true, details: { source: 'kb', build: running(0) } }), + nodeWithoutRecord(NEVER_BUILT), + statusOf(running(50), NEVER_BUILT), + nodeWithoutRecord(NEVER_BUILT), + statusOf(done), + ]); + expect(run.failure).toBeUndefined(); + }); }); it('--no-wait returns once the build has started, and says its outcome is not reported', async () => { @@ -180,7 +270,8 @@ describe('os knowledge status', () => { const text = run.logs.join('\n'); expect(text).toContain('kb (KB; adapter pgvector, object source)'); expect(text).toContain('index: nothing — a build is due — never built.'); - expect(text).toContain('last build on this node: none'); + expect(text).toContain('last build on the node that answered: none recorded there'); + expect(text).toContain('a build run on another node'); }); it('exits 1 with the server refusal text', async () => { diff --git a/packages/cli/src/commands/knowledge/rebuild.ts b/packages/cli/src/commands/knowledge/rebuild.ts index 4d3e99def2b..b353e7bfa5b 100644 --- a/packages/cli/src/commands/knowledge/rebuild.ts +++ b/packages/cli/src/commands/knowledge/rebuild.ts @@ -17,12 +17,30 @@ import { * Calls the `knowledge` settings namespace's `rebuild` action, which admits an * administrator only and starts the build in the background on the node that * answers; then, unless `--no-wait`, reads `index_status` every `--interval` - * seconds and prints the build's progress until it ends. Exits 0 when the - * build finished, 1 when it did not run or did not finish. With `--no-wait` + * seconds and prints the build's progress until it ends. With `--no-wait` * it exits 0 once the build has started and reports no outcome: the build * may still end `not-run` (another node holds the source's lock) or * `failed`, which only `os knowledge status` then shows. * + * Behind a load balancer. The build record lives in memory on the node that + * runs the build, so a progress read can land on a node that holds no record + * of this build. Such a poll is not a failure: the command falls back to the + * index state, which the store reports and which is the same on every node. + * The index state was read once before the rebuild started; when it was not + * `current` then (never built, a new fingerprint, a changed recipe), + * `current` now means a complete build has recorded itself (the adapter + * records it only at a build's end), and the command exits 0, saying the + * counts were not read. When the index was already `current`, or its state + * was not read, it cannot tell a running build from a finished one, and the + * command keeps polling until a read reaches the building node. + * + * Exits 0 when the build finished (its record says `done`, or the index + * turned `current`), 1 when its record says it did not run or did not + * finish, when the server refused, and when `--timeout` seconds of polls + * (counted as polls times `--interval`) passed without reading this build's + * record or seeing the index turn `current`: the command says it stopped + * following, and the build may still be running. + * * A build sends the content fields of every row the source's `where` matches, * except those the index already holds unchanged, to the embedding provider, * which bills for them. @@ -50,11 +68,30 @@ export default class KnowledgeRebuild extends Command { allowNo: true, }), interval: Flags.integer({ description: 'Seconds between two progress reads', default: 2, min: 1 }), + timeout: Flags.integer({ + description: + 'Seconds of polls (polls times --interval) to keep following while no read reaches this build\'s record and the index has not turned current; then exit 1 saying the build may still be running', + default: 600, + min: 1, + }), }; async run(): Promise { const { args, flags } = await this.parse(KnowledgeRebuild); const target = resolveKnowledgeTarget(flags); + + // The index state before the build: the only baseline against which the + // store's state can say a build completed (due before, current after). + let indexBefore: KnowledgeSourceView['index']; + if (flags.wait) { + const before = await runKnowledgeAction(target, 'index_status', { source: args.source }); + if (!before.ok) { + this.error(before.message, { exit: 1 }); + return; + } + indexBefore = firstSource(before.details)?.index; + } + const started = await runKnowledgeAction(target, 'rebuild', { source: args.source }); if (!started.ok) { this.error(started.message, { exit: 1 }); @@ -69,8 +106,10 @@ export default class KnowledgeRebuild extends Command { return; } const startedAt = startedBuild?.startedAt; + const indexWasCurrent = indexBefore?.state === undefined || indexBefore.state === 'current'; let lastLine = ''; + let unseenSeconds = 0; for (;;) { await new Promise((resolve) => setTimeout(resolve, flags.interval * 1000)); const read = await runKnowledgeAction(target, 'index_status', { source: args.source }); @@ -78,29 +117,59 @@ export default class KnowledgeRebuild extends Command { this.error(`The build's progress could not be read: ${read.message}`, { exit: 1 }); return; } - const entry = (Array.isArray(read.details.sources) ? (read.details.sources as KnowledgeSourceView[]) : [])[0]; + const entry = firstSource(read.details); const build = entry?.build ?? undefined; - if (!build || (startedAt !== undefined && build.startedAt !== startedAt)) { - this.error( - 'The progress was answered by a server node that is not running this build (behind a load balancer, ' + - 'another node may have taken the request). The build continues on the node that started it; read it there ' + - 'with os knowledge status.', - { exit: 1 }, + if (build && (startedAt === undefined || build.startedAt === startedAt)) { + // The node that answered is the one running this build: its record decides. + unseenSeconds = 0; + const line = describeBuild(build); + if (line !== lastLine) { + this.log(line); + lastLine = line; + } + if (build.status === 'running') continue; + if (build.status === 'done') { + if (build.message) this.log(build.message); + return; + } + this.error(build.message ?? `The build ended ${build.status ?? 'without a status'}.`, { exit: 1 }); + return; + } + + // Another node answered (behind a load balancer): no record of this + // build there. Never a failure on its own; the store's index state is. + const index = entry?.index; + if (!indexWasCurrent && index?.state === 'current') { + this.log( + 'The index is current: a complete build has recorded itself in the store. The node that answered holds no ' + + 'record of this build, so its counts are not shown (os knowledge status shows them when it reaches the node that ran it).', ); return; } - const line = describeBuild(build); + const line = + `answered by a node that is not running this build; the store's index state: ${index?.state ?? 'unknown'}` + + (!indexWasCurrent + ? ' (waiting for it to turn current)' + : ' (it was current, or unread, before this rebuild, so it cannot show when this build ends; waiting for a read that reaches the node running it)'); if (line !== lastLine) { this.log(line); lastLine = line; } - if (build.status === 'running') continue; - if (build.status === 'done') { - if (build.message) this.log(build.message); + unseenSeconds += flags.interval; + if (unseenSeconds >= flags.timeout) { + this.error( + `Stopped following after ${flags.timeout}s of progress reads that reached no record of this build and did not ` + + 'see the index turn current (behind a load balancer, the reads may keep landing on other nodes). The build may ' + + 'still be running on the node that started it: read its outcome with os knowledge status.', + { exit: 1 }, + ); return; } - this.error(build.message ?? `The build ended ${build.status ?? 'without a status'}.`, { exit: 1 }); - return; } } } + +/** The one source of an `index_status` answer for a single source. */ +function firstSource(details: Record): KnowledgeSourceView | undefined { + return (Array.isArray(details.sources) ? (details.sources as KnowledgeSourceView[]) : [])[0]; +} diff --git a/packages/cli/src/commands/knowledge/status.ts b/packages/cli/src/commands/knowledge/status.ts index ae853ab4f01..16af12b2633 100644 --- a/packages/cli/src/commands/knowledge/status.ts +++ b/packages/cli/src/commands/knowledge/status.ts @@ -14,9 +14,13 @@ import { * node ran: its progress while it runs, its result once it ended. * * Calls the `knowledge` settings namespace's `index_status` action, which - * admits an administrator only. A build record lives on the node that ran - * the build: behind a load balancer, the node that answers may not be the one - * building; the index state it reads is the store's own. + * admits an administrator only. A build record lives in memory on the node + * that ran the build: behind a load balancer, the node that answers may not + * be the one building, and a restarted node remembers none. The answer + * carries no node identity, so the command says which node a record is from + * only as "the node that answered", and when that node holds none it says + * that a build run on another node (or before a restart) is not shown here. + * The index state it prints is the store's own, the same on every node. */ export default class KnowledgeStatus extends Command { static override description = @@ -53,7 +57,12 @@ export default class KnowledgeStatus extends Command { this.log(`${source.id} (${source.label ?? source.id}; adapter ${source.adapter ?? '?'}, ${source.kind ?? '?'} source)`); const index = source.index; this.log(` index: ${index?.state ?? 'unknown'}${index?.due ? ' — a build is due' : ''}${index?.detail ? ` — ${index.detail}` : ''}`); - this.log(` last build on this node: ${source.build ? describeBuild(source.build) : 'none'}`); + this.log( + source.build + ? ` last build on the node that answered: ${describeBuild(source.build)}` + : ' last build on the node that answered: none recorded there (a build run on another node, or before ' + + 'this node restarted, is not shown; the index state above is the store\'s, the same on every node)', + ); const note = source.build?.message; if (note) this.log(` ${note}`); } From 91e9a57551df14ae7ba4be6d17e6696fc2c90338 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 26 Nov 2026 13:46:13 +0000 Subject: [PATCH 2/4] docs(cli): os knowledge rebuild and status behind a load balancer; changeset Claude-Session: https://claude.ai/code/session_01Utp6Cb4BAjgQ43tqy1DvVJ Co-authored-by: Claude --- .../37466-knowledge-cli-load-balancer.md | 10 +++++ content/docs/deployment/cli.mdx | 40 ++++++++++++++----- 2 files changed, 39 insertions(+), 11 deletions(-) create mode 100644 .changeset/37466-knowledge-cli-load-balancer.md diff --git a/.changeset/37466-knowledge-cli-load-balancer.md b/.changeset/37466-knowledge-cli-load-balancer.md new file mode 100644 index 00000000000..8bdea2aa2cc --- /dev/null +++ b/.changeset/37466-knowledge-cli-load-balancer.md @@ -0,0 +1,10 @@ +--- +'@objectstack/cli': patch +--- + +fix(cli): `os knowledge rebuild` and `os knowledge status` give the right answer behind a load balancer + +Clause-②: no +Metadata-protocol: none + +A build's record lives in memory on the node running the build. `os knowledge rebuild` used to exit 1 as soon as a progress read landed on another node, although the build continued and could succeed. It now falls back to the index state the store reports, the same on every node: the index turning `current` after it was not means the build completed (exit 0, without the counts). It reports failure only on the build's own recorded failure, or after `--timeout` seconds (new flag, default 600) of reads that reached no record of the build, saying the build may still be running. `os knowledge rebuild` now also reads the index state once before it starts the build. `os knowledge status` says when the node that answered holds no build record. diff --git a/content/docs/deployment/cli.mdx b/content/docs/deployment/cli.mdx index 4837a98a6eb..dd7f5a39ac1 100644 --- a/content/docs/deployment/cli.mdx +++ b/content/docs/deployment/cli.mdx @@ -4014,7 +4014,7 @@ nothing about the sources. | Command | Description | |---------|-------------| -| `os knowledge status` | Each source's index state (built, never built, due a rebuild) and the last build the answering node ran | +| `os knowledge status` | Each source's index state (built, never built, due a rebuild) and the last build the answering node ran, or that it holds none | | `os knowledge rebuild` | Start one complete build of a source on the server and follow its progress | #### `os knowledge status` @@ -4027,10 +4027,12 @@ os knowledge status support_articles # one source Prints, per source, its adapter and kind, its index state against what the server expects (and whether a build is due), and the last build the node that answered ran: its progress while it runs, its counts once it ended. A build's -record lives on the node that ran it, so behind a load balancer the node that -answers may not be the one building; the index state is read from the store and -is the same on every node. Exits `1` with the server's refusal text when the -action is refused. +record lives in memory on the node that ran it, so behind a load balancer the +node that answers may not be the one building, and a restarted node remembers +none. When the answering node holds no record, the command says so: a build +run on another node, or before a restart, is not shown. The index state is read +from the store and is the same on every node. Exits `1` with the server's +refusal text when the action is refused. #### `os knowledge rebuild` @@ -4042,12 +4044,27 @@ os knowledge rebuild support_articles --no-wait # start, and return at once **A build costs money:** every row the source's `where` matches is read, and the content fields of each one the index does not already hold unchanged are sent to the embedding provider, which bills for them. The build runs on the server in -the background; the command reads `index_status` every `--interval` seconds and -prints the build's progress until it ends. It exits `0` when the build finished, -and `1` when the server refused the rebuild (an unknown or `file` source, a -build already running on that node), when the build did not run or did not -finish, or when the progress was answered by another node than the one -building (the build continues there). `--no-wait` exits `0` once the build has +the background; the command reads the index state once before it starts the +build, then reads `index_status` every `--interval` seconds and prints the +build's progress until it ends. + +Behind a load balancer a progress read can land on a node that is not running +the build and holds no record of it. That read is not a failure: the command +falls back to the index state, which the store reports and which is the same on +every node. When the index was not `current` before the rebuild (never built, a +new fingerprint, a changed recipe), it turning `current` means a complete build +recorded itself, and the command exits `0` without the build's counts. When the +index was already `current`, its state cannot show when the build ends, and the +command keeps polling until a read reaches the node running the build. + +It exits `0` when the build finished (its record says done, or the index turned +`current`), and `1` when the server refused the rebuild (an unknown or `file` +source, a build already running on that node), when the build's record says it +did not run or did not finish, or when `--timeout` seconds of reads (counted as +reads times `--interval`) reached no record of the build and did not see the +index turn `current`; it then says the build may still be running and points +at `os knowledge status`. A read that reaches the build's record starts the +timeout again. `--no-wait` exits `0` once the build has started and reports no outcome: the build can still end without running (another node holds the source's lock) or fail, which only `os knowledge status` then shows. @@ -4058,6 +4075,7 @@ then shows. | `-t, --token ` | both | `OS_TOKEN` | Bearer token of an administrator | | `--no-wait` | `rebuild` | — | Return once the build has started; its outcome is not reported | | `--interval ` | `rebuild` | — | Seconds between two progress reads (default `2`) | +| `--timeout ` | `rebuild` | — | Seconds of reads that reach no record of the build before it stops following and exits `1` (default `600`) | ### Skills From c67ccd1f4a2146930b9b8e5aa8cccfdde82c102a Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 26 Nov 2026 14:31:08 +0000 Subject: [PATCH 3/4] fix(cli): os knowledge rebuild reads the index as done only after a due baseline on a shared store Claude-Session: https://claude.ai/code/session_01Utp6Cb4BAjgQ43tqy1DvVJ Co-authored-by: Claude --- content/docs/deployment/cli.mdx | 17 ++++--- .../src/commands/knowledge/knowledge.test.ts | 43 +++++++++++++++--- .../cli/src/commands/knowledge/rebuild.ts | 44 +++++++++++++------ packages/cli/src/commands/knowledge/status.ts | 6 ++- 4 files changed, 84 insertions(+), 26 deletions(-) diff --git a/content/docs/deployment/cli.mdx b/content/docs/deployment/cli.mdx index dd7f5a39ac1..3dfaea83c28 100644 --- a/content/docs/deployment/cli.mdx +++ b/content/docs/deployment/cli.mdx @@ -4031,7 +4031,9 @@ record lives in memory on the node that ran it, so behind a load balancer the node that answers may not be the one building, and a restarted node remembers none. When the answering node holds no record, the command says so: a build run on another node, or before a restart, is not shown. The index state is read -from the store and is the same on every node. Exits `1` with the server's +from the store, and is the same on every node when the adapter's store is +shared between nodes (`pgvector`); the `memory` adapter keeps its index in each +process, so there each node answers for its own. Exits `1` with the server's refusal text when the action is refused. #### `os knowledge rebuild` @@ -4051,11 +4053,14 @@ build's progress until it ends. Behind a load balancer a progress read can land on a node that is not running the build and holds no record of it. That read is not a failure: the command falls back to the index state, which the store reports and which is the same on -every node. When the index was not `current` before the rebuild (never built, a -new fingerprint, a changed recipe), it turning `current` means a complete build -recorded itself, and the command exits `0` without the build's counts. When the -index was already `current`, its state cannot show when the build ends, and the -command keeps polling until a read reaches the node running the build. +every node when the adapter's store is shared between nodes. When a build was +due before the rebuild (never built, a new fingerprint, a changed recipe), the +index turning `current` means a complete build recorded itself, and the command +exits `0` without the build's counts. When no build was due (the index already +`current`, or unreadable), the state cannot show when the build ends, and the +command keeps polling until a read reaches the node running the build. On the +`memory` adapter, whose index lives in each process, the index state is never +read as the build's end: only the building node's record answers. It exits `0` when the build finished (its record says done, or the index turned `current`), and `1` when the server refused the rebuild (an unknown or `file` diff --git a/packages/cli/src/commands/knowledge/knowledge.test.ts b/packages/cli/src/commands/knowledge/knowledge.test.ts index 8d0bb9404d0..29e0c276091 100644 --- a/packages/cli/src/commands/knowledge/knowledge.test.ts +++ b/packages/cli/src/commands/knowledge/knowledge.test.ts @@ -15,8 +15,9 @@ * Behind a load balancer a progress read can land on a node that holds no * record of the build (the record lives in memory on the node running it): * `nodeWithoutRecord` answers such a poll. The command then reads the - * store's index state, the same on every node, and never exits 1 for the - * poll alone. + * store's index state (the same on every node for a shared store; never + * read as the build's end on the per-process `memory` adapter), and never + * exits 1 for the poll alone. * * The oclif `Config` is loaded at module scope, as in * `commands/datasource/envelope-unwrap.test.ts` (whose header says why). @@ -102,10 +103,11 @@ const running = (discovered: number) => ({ }); const CURRENT = { state: 'current', due: false, detail: 'current.' }; const NEVER_BUILT = { state: 'nothing', due: true, detail: 'never built.' }; -const statusOf = (build: unknown, index: unknown = CURRENT) => ok({ +const UNREADABLE = { state: 'unreadable', due: false, detail: 'reading the adapter failed: connection reset.' }; +const statusOf = (build: unknown, index: unknown = CURRENT, adapter = 'pgvector') => ok({ ok: true, message: '1 source(s)', - details: { sources: [{ id: 'kb', label: 'KB', adapter: 'pgvector', kind: 'object', index, build }] }, + details: { sources: [{ id: 'kb', label: 'KB', adapter, kind: 'object', index, build }] }, }); /** A poll answered by a node that is not running this build: no record there. */ const nodeWithoutRecord = (index: unknown) => statusOf(null, index); @@ -212,7 +214,37 @@ describe('os knowledge rebuild', () => { // Only the recorded failure, read on the node that ran the build, exits 1. expect(run.failure).toMatchObject({ message: 'the adapter upsert failed: quota', exit: 1 }); expect(run.requests).toHaveLength(5); - expect(run.logs.join('\n')).toContain('it was current, or unread, before this rebuild'); + expect(run.logs.join('\n')).toContain('a build was not due before this rebuild'); + }); + + it('does not read an unreadable baseline as a due build: a current index elsewhere is no exit 0', async () => { + // A transient adapter error on the baseline read answers `unreadable` + // (due: false). The index then reads `current` on another node because a + // same-fingerprint rebuild never leaves it; that is not this build's end. + const run = await drive(KnowledgeRebuild, ['kb', '--interval', '2', '--timeout', '4', ...target], [ + nodeWithoutRecord(UNREADABLE), + ok({ ok: true, details: { source: 'kb', build: running(0) } }), + nodeWithoutRecord(CURRENT), + nodeWithoutRecord(CURRENT), + ]); + expect(run.failure?.exit).toBe(1); + expect(run.failure?.message).toContain('Stopped following after 4s'); + expect(run.logs.join('\n')).not.toContain('The index is current'); + }); + + it('never reads the per-process memory adapter\'s index as this build\'s end; waits for the building node', async () => { + const run = await drive(KnowledgeRebuild, ['kb', ...target], [ + statusOf(null, NEVER_BUILT, 'memory'), + ok({ ok: true, details: { source: 'kb', build: running(0) } }), + statusOf(null, CURRENT, 'memory'), + statusOf(done, CURRENT, 'memory'), + ]); + expect(run.failure).toBeUndefined(); + expect(run.requests).toHaveLength(4); + const text = run.logs.join('\n'); + expect(text).not.toContain('The index is current'); + expect(text).toContain('the memory adapter keeps its index per process'); + expect(text).toContain('done (operator, ended 2026-11-26T05:01:00.000Z)'); }); it('exits 1 after --timeout seconds of polls that reach no record, and says the build may still run', async () => { @@ -272,6 +304,7 @@ describe('os knowledge status', () => { expect(text).toContain('index: nothing — a build is due — never built.'); expect(text).toContain('last build on the node that answered: none recorded there'); expect(text).toContain('a build run on another node'); + expect(text).toContain('not on the memory adapter'); }); it('exits 1 with the server refusal text', async () => { diff --git a/packages/cli/src/commands/knowledge/rebuild.ts b/packages/cli/src/commands/knowledge/rebuild.ts index b353e7bfa5b..fd49d32e601 100644 --- a/packages/cli/src/commands/knowledge/rebuild.ts +++ b/packages/cli/src/commands/knowledge/rebuild.ts @@ -25,14 +25,18 @@ import { * Behind a load balancer. The build record lives in memory on the node that * runs the build, so a progress read can land on a node that holds no record * of this build. Such a poll is not a failure: the command falls back to the - * index state, which the store reports and which is the same on every node. - * The index state was read once before the rebuild started; when it was not - * `current` then (never built, a new fingerprint, a changed recipe), + * index state, which the store reports and which is the same on every node + * when the adapter's store is shared between nodes. + * The index state was read once before the rebuild started; when a build was + * due then (`due: true`: never built, a new fingerprint, a changed recipe), * `current` now means a complete build has recorded itself (the adapter * records it only at a build's end), and the command exits 0, saying the - * counts were not read. When the index was already `current`, or its state - * was not read, it cannot tell a running build from a finished one, and the - * command keeps polling until a read reaches the building node. + * counts were not read. When no build was due (the index `current`, + * `unreadable`, or a state that never turns `current`), or the state was not + * read, it cannot tell a running build from a finished one, and the command + * keeps polling until a read reaches the building node. The same holds on + * the `memory` adapter, whose index lives in each process and is not shared + * between nodes: there the index state is never read as this build's end. * * Exits 0 when the build finished (its record says `done`, or the index * turned `current`), 1 when its record says it did not run or did not @@ -81,15 +85,16 @@ export default class KnowledgeRebuild extends Command { const target = resolveKnowledgeTarget(flags); // The index state before the build: the only baseline against which the - // store's state can say a build completed (due before, current after). - let indexBefore: KnowledgeSourceView['index']; + // store's state can say a build completed (due before, current after), + // and the adapter, which says whether that state is shared between nodes. + let sourceBefore: KnowledgeSourceView | undefined; if (flags.wait) { const before = await runKnowledgeAction(target, 'index_status', { source: args.source }); if (!before.ok) { this.error(before.message, { exit: 1 }); return; } - indexBefore = firstSource(before.details)?.index; + sourceBefore = firstSource(before.details); } const started = await runKnowledgeAction(target, 'rebuild', { source: args.source }); @@ -106,7 +111,15 @@ export default class KnowledgeRebuild extends Command { return; } const startedAt = startedBuild?.startedAt; - const indexWasCurrent = indexBefore?.state === undefined || indexBefore.state === 'current'; + // The index state can say this build completed only when a build was due + // before it (never built, a new fingerprint, a changed recipe, no recipe + // hash on an adapter that records one: all turn `current` at a build's + // end) and when every node reads one shared store. Any other baseline + // (`current`, `unreadable`, unread, a state that never turns `current`) + // and the `memory` adapter, whose index is per process, leave the + // building node's record as the only answer. + const perProcessIndex = sourceBefore?.adapter === MEMORY_ADAPTER; + const indexCanTell = sourceBefore?.index?.due === true && !perProcessIndex; let lastLine = ''; let unseenSeconds = 0; @@ -139,7 +152,7 @@ export default class KnowledgeRebuild extends Command { // Another node answered (behind a load balancer): no record of this // build there. Never a failure on its own; the store's index state is. const index = entry?.index; - if (!indexWasCurrent && index?.state === 'current') { + if (indexCanTell && index?.state === 'current') { this.log( 'The index is current: a complete build has recorded itself in the store. The node that answered holds no ' + 'record of this build, so its counts are not shown (os knowledge status shows them when it reaches the node that ran it).', @@ -148,9 +161,11 @@ export default class KnowledgeRebuild extends Command { } const line = `answered by a node that is not running this build; the store's index state: ${index?.state ?? 'unknown'}` + - (!indexWasCurrent + (indexCanTell ? ' (waiting for it to turn current)' - : ' (it was current, or unread, before this rebuild, so it cannot show when this build ends; waiting for a read that reaches the node running it)'); + : perProcessIndex + ? " (the memory adapter keeps its index per process, so another node's state says nothing of this build; waiting for a read that reaches the node running it)" + : ' (a build was not due before this rebuild, or the state was not read, so it cannot show when this build ends; waiting for a read that reaches the node running it)'); if (line !== lastLine) { this.log(line); lastLine = line; @@ -169,6 +184,9 @@ export default class KnowledgeRebuild extends Command { } } +/** The adapter whose index lives in one process's memory: its state is not shared between nodes. */ +const MEMORY_ADAPTER = 'memory'; + /** The one source of an `index_status` answer for a single source. */ function firstSource(details: Record): KnowledgeSourceView | undefined { return (Array.isArray(details.sources) ? (details.sources as KnowledgeSourceView[]) : [])[0]; diff --git a/packages/cli/src/commands/knowledge/status.ts b/packages/cli/src/commands/knowledge/status.ts index 16af12b2633..52bbbc25936 100644 --- a/packages/cli/src/commands/knowledge/status.ts +++ b/packages/cli/src/commands/knowledge/status.ts @@ -20,7 +20,8 @@ import { * carries no node identity, so the command says which node a record is from * only as "the node that answered", and when that node holds none it says * that a build run on another node (or before a restart) is not shown here. - * The index state it prints is the store's own, the same on every node. + * The index state it prints is the store's own: the same on every node when + * the adapter's store is shared, and per node on the `memory` adapter. */ export default class KnowledgeStatus extends Command { static override description = @@ -61,7 +62,8 @@ export default class KnowledgeStatus extends Command { source.build ? ` last build on the node that answered: ${describeBuild(source.build)}` : ' last build on the node that answered: none recorded there (a build run on another node, or before ' + - 'this node restarted, is not shown; the index state above is the store\'s, the same on every node)', + 'this node restarted, is not shown; the index state above is the store\'s, the same on every node only when ' + + 'the adapter\'s store is shared between nodes, not on the memory adapter)', ); const note = source.build?.message; if (note) this.log(` ${note}`); From 3b351697d10880a91dd4a6bafce636e6c68b116f Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 26 Nov 2026 14:38:12 +0000 Subject: [PATCH 4/4] chore(changeset): os knowledge rebuild --timeout is a widening Claude-Session: https://claude.ai/code/session_01Utp6Cb4BAjgQ43tqy1DvVJ Co-authored-by: Claude --- .changeset/37466-knowledge-cli-load-balancer.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.changeset/37466-knowledge-cli-load-balancer.md b/.changeset/37466-knowledge-cli-load-balancer.md index 8bdea2aa2cc..23ea37c5d5a 100644 --- a/.changeset/37466-knowledge-cli-load-balancer.md +++ b/.changeset/37466-knowledge-cli-load-balancer.md @@ -4,7 +4,7 @@ fix(cli): `os knowledge rebuild` and `os knowledge status` give the right answer behind a load balancer -Clause-②: no +Clause-②: yes (widening) Metadata-protocol: none -A build's record lives in memory on the node running the build. `os knowledge rebuild` used to exit 1 as soon as a progress read landed on another node, although the build continued and could succeed. It now falls back to the index state the store reports, the same on every node: the index turning `current` after it was not means the build completed (exit 0, without the counts). It reports failure only on the build's own recorded failure, or after `--timeout` seconds (new flag, default 600) of reads that reached no record of the build, saying the build may still be running. `os knowledge rebuild` now also reads the index state once before it starts the build. `os knowledge status` says when the node that answered holds no build record. +A build's record lives in memory on the node running the build. `os knowledge rebuild` used to exit 1 as soon as a progress read landed on another node, although the build continued and could succeed. It now falls back to the index state the store reports: when a build was due before the rebuild and the adapter keeps a store shared between nodes (not `memory`, whose index is per process), the index turning `current` means the build completed (exit 0, without the counts). It reports failure only on the build's own recorded failure, or after `--timeout` seconds (new flag, default 600) of reads that reached no record of the build, saying the build may still be running. `os knowledge rebuild` now also reads the index state once before it starts the build. `os knowledge status` says when the node that answered holds no build record.