From cc49f0389c5432c2f9ee556b6da631a96a673b46 Mon Sep 17 00:00:00 2001 From: Boki Date: Tue, 27 Oct 2026 17:10:22 -0400 Subject: [PATCH] fix(research): accept automatic candidate publication lineage (RP16) --- scripts/research-client.ts | 3 +- scripts/test/research-client.test.ts | 112 ++++++++++++++++++++- test/postgres/ai-research-workflow.test.ts | 44 ++++++++ 3 files changed, 157 insertions(+), 2 deletions(-) diff --git a/scripts/research-client.ts b/scripts/research-client.ts index aff2e9dd..55568b55 100644 --- a/scripts/research-client.ts +++ b/scripts/research-client.ts @@ -367,6 +367,7 @@ export class ResearchClient { () => undefined ); } + /** Null selects automatic catalog lineage; receipt retries retain the original parent. */ publishVersion( strategyId: string, input: StrategyVersionPackage, @@ -387,7 +388,7 @@ export class ResearchClient { expect( saved.strategyId === strategyId && saved.origin === 'candidate' && - saved.parentVersionId === parentVersionId && + (parentVersionId === null || saved.parentVersionId === parentVersionId) && saved.package.artifact.sha256 === pkg.artifact.sha256 && saved.package.dependencyLock.sha256 === pkg.dependencyLock.sha256 ); diff --git a/scripts/test/research-client.test.ts b/scripts/test/research-client.test.ts index c4207f6f..78aa0b1b 100644 --- a/scripts/test/research-client.test.ts +++ b/scripts/test/research-client.test.ts @@ -143,7 +143,7 @@ describe('authorized research client', () => { const saved = { id: detail.strategyVersionId, strategyId: detail.preview.request.strategy.familyId, - ordinal: 2, + ordinal: 1, origin: 'candidate', createdBy: agent, parentVersionId: null, @@ -161,6 +161,116 @@ describe('authorized research client', () => { ); }); + test('automatic publication lineage accepts and retains the server-selected parent', async () => { + const detail = ordinary(); + const pkg = detail.preview.request.strategy.package; + const saved = { + id: detail.strategyVersionId, + strategyId: detail.preview.request.strategy.familyId, + ordinal: 2, + origin: 'candidate', + createdBy: agent, + parentVersionId: 'abcdefab-abcd-4bcd-8bcd-000000000002', + package: pkg, + }; + const publication = client(() => Response.json({ version: saved }, { status: 201 })); + const implicit = await publication.value.publishVersion(saved.strategyId, pkg, requestKey); + const explicitNull = await publication.value.publishVersion( + saved.strategyId, + pkg, + requestKey, + null + ); + expect(implicit).toMatchObject({ id: saved.id, parentVersionId: saved.parentVersionId }); + expect(explicitNull).toEqual(implicit); + expect(publication.calls).toHaveLength(2); + expect(publication.calls[0]!.body).toEqual({ package: pkg, parentVersionId: null }); + expect(publication.calls[1]!.init.body).toBe(publication.calls[0]!.init.body!); + }); + + test('automatic lineage receipt retries retain the original key/body after a newer publication', async () => { + const detail = ordinary(); + const pkg = detail.preview.request.strategy.package; + const second = { + id: detail.strategyVersionId, + strategyId: detail.preview.request.strategy.familyId, + ordinal: 2, + origin: 'candidate', + createdBy: agent, + parentVersionId: 'abcdefab-abcd-4bcd-8bcd-000000000002', + package: pkg, + }; + const third = { + ...second, + id: 'abcdefab-abcd-4bcd-8bcd-000000000003', + ordinal: 3, + parentVersionId: second.id, + }; + const publication = client((_call, index) => { + if (index === 0) { + throw new Error('Response lost after the second version committed'); + } + return Response.json({ version: index === 1 ? third : second }, { status: 201 }); + }); + await expect( + publication.value.publishVersion(second.strategyId, pkg, requestKey) + ).rejects.toMatchObject({ uncertain: true }); + expect(publication.calls).toHaveLength(1); + expect( + await publication.value.publishVersion(second.strategyId, pkg, `${requestKey}:new`) + ).toMatchObject({ id: third.id, parentVersionId: second.id }); + expect( + await publication.value.publishVersion(second.strategyId, pkg, requestKey) + ).toMatchObject({ id: second.id, parentVersionId: second.parentVersionId }); + expect(publication.calls).toHaveLength(3); + expect(publication.calls.every(call => call.init.method === 'POST')).toBeTrue(); + expect(publication.calls[2]!.init.body).toBe(publication.calls[0]!.init.body!); + expect(publication.calls[2]!.body).toEqual({ package: pkg, parentVersionId: null }); + for (const index of [0, 2]) { + expect(new Headers(publication.calls[index]!.init.headers).get('idempotency-key')).toBe( + requestKey + ); + } + }); + + test('publication still rejects wrong explicit lineage, malformed parents, family, origin and hashes', async () => { + const detail = ordinary(); + const pkg = detail.preview.request.strategy.package; + const saved = { + id: detail.strategyVersionId, + strategyId: detail.preview.request.strategy.familyId, + ordinal: 2, + origin: 'candidate', + createdBy: agent, + parentVersionId: 'abcdefab-abcd-4bcd-8bcd-000000000002', + package: pkg, + }; + const explicit = client(() => Response.json({ version: saved }, { status: 201 })); + await expect( + explicit.value.publishVersion(saved.strategyId, pkg, requestKey, saved.id) + ).rejects.toMatchObject({ uncertain: true }); + expect(explicit.calls).toHaveLength(1); + expect(explicit.calls[0]!.body).toEqual({ package: pkg, parentVersionId: saved.id }); + for (const invalid of [ + { ...saved, parentVersionId: 'not-a-version-id' }, + { ...saved, parentVersionId: 2 }, + { ...saved, parentVersionId: undefined }, + { ...saved, strategyId: 'abcdefab-abcd-4bcd-8bcd-000000000003' }, + { ...saved, origin: 'builtin' }, + { ...saved, package: { ...pkg, artifact: { ...pkg.artifact, sha256: 'f'.repeat(64) } } }, + { + ...saved, + package: { ...pkg, dependencyLock: { ...pkg.dependencyLock, sha256: 'f'.repeat(64) } }, + }, + ]) { + const rejected = client(() => Response.json({ version: invalid }, { status: 201 })); + await expect( + rejected.value.publishVersion(saved.strategyId, pkg, requestKey) + ).rejects.toMatchObject({ uncertain: true }); + expect(rejected.calls).toHaveLength(1); + } + }); + test('normalizes uppercase identities before captures and binds cancellation and publication receipts', async () => { const detail = ordinary(); detail.id = 'abcdefab-abcd-4bcd-8bcd-abcdefabcdef'; diff --git a/test/postgres/ai-research-workflow.test.ts b/test/postgres/ai-research-workflow.test.ts index 90acc130..a7284275 100644 --- a/test/postgres/ai-research-workflow.test.ts +++ b/test/postgres/ai-research-workflow.test.ts @@ -253,6 +253,50 @@ async function fixture() { } describe.skipIf(!url)('AI research workflow through authenticated APIs and Rust', () => { + test('candidate revisions retain automatic and explicit lineage across delayed receipt replay', async () => { + const f = await fixture(); + const family = await f.client.createStrategy({ + name: 'Refined candidate', + description: 'Successive revisions and a delayed publication receipt', + }); + const pkg = f.suggestions().candidates[0]!.package; + const first = await f.client.publishVersion(family.id, pkg, 'lineage:first'); + expect(first).toMatchObject({ + ordinal: 1, + parentVersionId: null, + createdBy: f.provisioned.agent.id, + }); + const second = await f.client.publishVersion(family.id, pkg, 'lineage:second'); + expect(second).toMatchObject({ ordinal: 2, parentVersionId: first.id }); + const third = await f.client.publishVersion(family.id, pkg, 'lineage:third', null); + expect(third).toMatchObject({ ordinal: 3, parentVersionId: second.id }); + // An explicit branch parent remains binding even when another version is latest. + const branch = await f.client.publishVersion(family.id, pkg, 'lineage:branch', first.id); + expect(branch).toMatchObject({ ordinal: 4, parentVersionId: first.id }); + const replayed = await f.client.publishVersion(family.id, pkg, 'lineage:second'); + expect(replayed).toEqual(second); + const publications = f.requests.filter( + request => request.path === `/api/v2/strategies/${family.id}/versions` + ); + expect(publications).toHaveLength(5); + expect( + publications.every(request => request.status === 201 && request.method === 'POST') + ).toBeTrue(); + expect(publications[1]).toEqual(publications[4]!); + expect(publications[1]!.body).toEqual({ package: pkg, parentVersionId: null }); + expect(publications[3]!.body).toEqual({ package: pkg, parentVersionId: first.id }); + expect( + ( + await f.pool.query( + 'SELECT count(*)::int AS count FROM strategy_versions WHERE strategy_id=$1', + [family.id] + ) + ).rows[0].count + ).toBe(4); + expect((await f.pool.query('SELECT count(*)::int AS count FROM runs')).rows[0].count).toBe(0); + expect(f.executionErrors).toEqual([]); + }); + test('two generated candidates, train-only selection, costs, exact saved evidence and a retained failure', async () => { const f = await fixture(); const events: ResearchCycleEvent[] = [];