From 08030428f48670edeae2df5407477ee556ab28f9 Mon Sep 17 00:00:00 2001 From: mohammed naji Date: Wed, 9 Sep 2026 01:02:09 +0400 Subject: [PATCH 1/3] fix(retrieve): expand authenticated owners within available budgets --- src/runtime/query-evidence-dependencies.ts | 28 +++- src/runtime/retrieve.ts | 135 ++++++++++++++++-- ...etrieve-small-owner-representation.test.ts | 118 +++++++++++++++ 3 files changed, 261 insertions(+), 20 deletions(-) diff --git a/src/runtime/query-evidence-dependencies.ts b/src/runtime/query-evidence-dependencies.ts index f5111e9d..382ff2bf 100644 --- a/src/runtime/query-evidence-dependencies.ts +++ b/src/runtime/query-evidence-dependencies.ts @@ -449,11 +449,12 @@ function numberPhysicalSourceRows(sourceText: string, startLine: number): { } /** - * Authenticates and serializes a complete, exact, small JS/TS function or - * method owner from the source snapshot already retained by retrieval. + * Authenticates and serializes a complete, exact JS/TS function or method + * owner from the source snapshot already retained by retrieval. */ -export function completeSmallOwnerSourceEvidence( +function authenticatedCompleteOwnerSourceEvidence( input: CompleteSmallOwnerSourceInput, + limits?: { maxLines: number; maxCharacters: number }, ): CompleteSmallOwnerSourceEvidence | null { try { if ( @@ -462,7 +463,7 @@ export function completeSmallOwnerSourceEvidence( || !Number.isInteger(input.ownerRange.end) || input.ownerRange.start < 1 || input.ownerRange.end < input.ownerRange.start - || input.ownerRange.end - input.ownerRange.start + 1 > COMPLETE_SMALL_OWNER_MAX_LINES + || (limits && input.ownerRange.end - input.ownerRange.start + 1 > limits.maxLines) ) { return null } @@ -505,7 +506,7 @@ export function completeSmallOwnerSourceEvidence( !source || source.startLine !== input.ownerRange.start || source.endLine !== input.ownerRange.end - || source.text.length > COMPLETE_SMALL_OWNER_MAX_CHARACTERS + || (limits && source.text.length > limits.maxCharacters) ) { return null } @@ -522,6 +523,23 @@ export function completeSmallOwnerSourceEvidence( } } +/** Authenticates and serializes an exact owner for budget-aware allocation. */ +export function completeOwnerSourceEvidence( + input: CompleteSmallOwnerSourceInput, +): CompleteSmallOwnerSourceEvidence | null { + return authenticatedCompleteOwnerSourceEvidence(input) +} + +/** Preserves the established bounded small-owner representation contract. */ +export function completeSmallOwnerSourceEvidence( + input: CompleteSmallOwnerSourceInput, +): CompleteSmallOwnerSourceEvidence | null { + return authenticatedCompleteOwnerSourceEvidence(input, { + maxLines: COMPLETE_SMALL_OWNER_MAX_LINES, + maxCharacters: COMPLETE_SMALL_OWNER_MAX_CHARACTERS, + }) +} + function normalizedSource(value: string): string { return value.replace(/\s+/g, ' ').trim() } diff --git a/src/runtime/retrieve.ts b/src/runtime/retrieve.ts index de39938a..f3e8937a 100644 --- a/src/runtime/retrieve.ts +++ b/src/runtime/retrieve.ts @@ -87,6 +87,7 @@ import { } from './retrieve/pipeline.js' import { communitiesFromGraph, estimateQueryTokens } from './serve.js' import { + completeOwnerSourceEvidence, completeSmallOwnerSourceEvidence, completeQueryEvidenceLiteralStatement, ownerLocalDeclarationEvidence, @@ -540,10 +541,16 @@ function truncateSnippetToTokenBudget( } } -function applyRetrieveSnippetBudgetToNodes( +function applyRetrieveSnippetBudgetToNodes( nodes: readonly TNode[], options: RetrieveSnippetOptions = {}, eligibleNodeIndexes?: ReadonlySet, + maximumMatchedNodeTokens = Number.POSITIVE_INFINITY, ): { nodes: Array usedTokens: number @@ -580,7 +587,7 @@ function applyRetrieveSnippetBudgetToNodes total + snippetTokenCount(node.snippet), 0, ) + let serializedMatchedNodeTokensUsed = tokenCountForMatchedNodes(shapedNodes) + const promotedNodes = shapedNodes.map((node, index) => { + const completeOwner = completeOwnerMatchedNodes.get(node) + if (!completeOwner?.allocationOnly) { + return node + } + const snippetEligible = eligibleNodeIndexes === undefined + ? index < topNWithSnippet + : eligibleNodeIndexes.has(index) + if (!snippetEligible) { + return node + } + + const currentSnippetTokens = snippetTokenCount(node.snippet) + const fullSnippetTokens = snippetTokenCount(completeOwner.fullSnippet) + const nextSnippetTokens = serializedSnippetTokensUsed - currentSnippetTokens + fullSnippetTokens + const currentEntryTokens = estimateRetrieveEntryTokens( + node.label, + node.source_file, + node.line_number, + node.snippet ?? null, + ) + const fullEntryTokens = estimateRetrieveEntryTokens( + node.label, + node.source_file, + node.line_number, + completeOwner.fullSnippet, + ) + const nextMatchedNodeTokens = serializedMatchedNodeTokensUsed - currentEntryTokens + fullEntryTokens + if ( + nextSnippetTokens > snippetBudget + || nextMatchedNodeTokens > maximumMatchedNodeTokens + ) { + return node + } + + serializedSnippetTokensUsed = nextSnippetTokens + serializedMatchedNodeTokensUsed = nextMatchedNodeTokens + const priorTruncation = 'snippet_truncated' in nodes[index]! + && (nodes[index] as { snippet_truncated?: boolean }).snippet_truncated === true + return attachCompleteOwnerState({ + ...node, + snippet: completeOwner.fullSnippet, + snippet_line_number: completeOwner.fullLineNumber, + snippet_scope: 'symbol' as const, + snippet_truncated: priorTruncation, + representation_type: 'detail' as const, + representation_reason: completeOwner.representationReason, + }, completeOwner) + }) return { - nodes: shapedNodes, + nodes: promotedNodes, usedTokens: serializedSnippetTokensUsed, remainingTokens: Math.max(0, snippetBudget - serializedSnippetTokensUsed), } @@ -715,8 +776,16 @@ export function withRetrieveSnippetBudget( result: RetrieveResult, options: RetrieveSnippetOptions = {}, ): RetrieveResult { - const shapedNodes = applyRetrieveSnippetBudgetToNodes(result.matched_nodes, options) const baseNodeTokenCount = tokenCountForMatchedNodes(result.matched_nodes) + const maximumMatchedNodeTokens = result.task_contract + ? baseNodeTokenCount + Math.max(0, result.task_contract.budget - result.token_count) + : Number.POSITIVE_INFINITY + const shapedNodes = applyRetrieveSnippetBudgetToNodes( + result.matched_nodes, + options, + undefined, + maximumMatchedNodeTokens, + ) const shapedNodeTokenCount = tokenCountForMatchedNodes(shapedNodes.nodes) const retrievalPlan = reconcileRetrievalPlanQueryEvidence( result.retrieval_plan, @@ -792,6 +861,7 @@ interface QueryEvidenceSnippetOptions { nodeKind?: string externalCall?: boolean authenticatedOwner?: boolean + maximumCompleteOwnerTokens?: number sourceLocation?: string | null fileNodeLike?: boolean derived?: boolean @@ -804,11 +874,14 @@ interface CompleteOwnerSnippetState { fallbackSnippet: string fallbackLineNumber: number fallbackScope: QueryEvidenceSnippet['scope'] + representationReason: string + allocationOnly: boolean } const completeOwnerQuerySnippets = new WeakMap() const completeOwnerMatchedNodes = new WeakMap() const COMPLETE_OWNER_REPRESENTATION_REASON = 'complete small owner source' +const ALLOCATED_COMPLETE_OWNER_REPRESENTATION_REASON = 'complete owner source within snippet allocation' function attachCompleteOwnerState( target: T, @@ -826,7 +899,10 @@ function copyCompleteOwnerState(source: object, target: T): T function withoutCompleteOwnerRepresentationClaim(source: T): T { const output = { ...source } as T & { representation_reason?: string } - if (output.representation_reason === COMPLETE_OWNER_REPRESENTATION_REASON) { + if ( + output.representation_reason === COMPLETE_OWNER_REPRESENTATION_REASON + || output.representation_reason === ALLOCATED_COMPLETE_OWNER_REPRESENTATION_REASON + ) { delete output.representation_reason } return output @@ -1894,7 +1970,7 @@ export function readQueryEvidenceSnippet( lineNumber: completed?.lineNumber ?? baseline[0]!.source.index, scope, } - const completeOwner = ownerRange && options.authenticatedOwner === true + const completeSmallOwner = ownerRange && options.authenticatedOwner === true ? completeSmallOwnerSourceEvidence({ sourceFilePath: sourceFile, sourceLines: lines, @@ -1904,11 +1980,29 @@ export function readQueryEvidenceSnippet( ...(options.externalCall !== undefined ? { externalCall: options.externalCall } : {}), }) : null + const completeOwner = completeSmallOwner ?? (ownerRange && options.authenticatedOwner === true + ? completeOwnerSourceEvidence({ + sourceFilePath: sourceFile, + sourceLines: lines, + ownerRange, + label: options.label, + ...(options.nodeKind ? { nodeKind: options.nodeKind } : {}), + ...(options.externalCall !== undefined ? { externalCall: options.externalCall } : {}), + }) + : null) if (!completeOwner) { return fallback } - const result: QueryEvidenceSnippet = { + const allocationOnly = completeSmallOwner === null + if ( + allocationOnly + && options.maximumCompleteOwnerTokens !== undefined + && snippetTokenCount(completeOwner.snippet) > options.maximumCompleteOwnerTokens + ) { + return fallback + } + const result: QueryEvidenceSnippet = allocationOnly ? fallback : { snippet: completeOwner.snippet, lineNumber: completeOwner.lineNumber, scope: 'symbol', @@ -1919,6 +2013,10 @@ export function readQueryEvidenceSnippet( fallbackSnippet: fallback.snippet, fallbackLineNumber: fallback.lineNumber, fallbackScope: fallback.scope, + representationReason: allocationOnly + ? ALLOCATED_COMPLETE_OWNER_REPRESENTATION_REASON + : COMPLETE_OWNER_REPRESENTATION_REASON, + allocationOnly, }) return result } catch { @@ -5379,6 +5477,7 @@ function buildRetrieveResultFromOrderedCandidates( nodeKind: node.nodeKind, externalCall: node.externalCall, authenticatedOwner: true, + maximumCompleteOwnerTokens: taskContract.budget, sourceLocation: node.sourceLocation, fileNodeLike: node.fileNodeLike, derived: node.lineNumberDerived && lineRangeFromSourceLocation(node.sourceLocation) === null, @@ -5489,13 +5588,16 @@ function buildRetrieveResultFromOrderedCandidates( if (!state) { return node } + if (state.allocationOnly) { + return attachCompleteOwnerState(node, state) + } return attachCompleteOwnerState({ ...node, snippet: state.fullSnippet, snippet_line_number: state.fullLineNumber, snippet_scope: 'symbol' as const, representation_type: 'detail' as const, - representation_reason: COMPLETE_OWNER_REPRESENTATION_REASON, + representation_reason: state.representationReason, }, state) }) const packedNodeTokens = tokenCountForMatchedNodes(packedNodes) @@ -6716,14 +6818,17 @@ export function compactRetrieveResult(result: RetrieveResult, options: RetrieveS compactPack.relationships, ) : undefined + const compactPackNodeTokenCount = compactPack.nodes.reduce( + (total, node) => total + estimateRetrieveEntryTokens(node.label, node.source_file, node.line_number, node.snippet ?? null), + 0, + ) + const maximumMatchedNodeTokens = compactPackNodeTokenCount + + Math.max(0, compactPack.task_contract.budget - compactPack.token_count) const shapedNodes = applyRetrieveSnippetBudgetToNodes( compactPack.nodes, options, preferredSnippetNodeIndexes, - ) - const compactPackNodeTokenCount = compactPack.nodes.reduce( - (total, node) => total + estimateRetrieveEntryTokens(node.label, node.source_file, node.line_number, node.snippet ?? null), - 0, + maximumMatchedNodeTokens, ) const shapedNodeTokenCount = shapedNodes.nodes.reduce( (total, node) => total + estimateRetrieveEntryTokens(node.label, node.source_file, node.line_number, node.snippet ?? null), diff --git a/tests/unit/retrieve-small-owner-representation.test.ts b/tests/unit/retrieve-small-owner-representation.test.ts index f4f2bb08..3ac910af 100644 --- a/tests/unit/retrieve-small-owner-representation.test.ts +++ b/tests/unit/retrieve-small-owner-representation.test.ts @@ -8,6 +8,8 @@ import { afterAll, beforeAll, describe, expect, it } from 'vitest' import { generateGraph } from '../../src/infrastructure/generate.js' import { compactRetrieveResultForStdio, + contextPackFromRetrieveResult, + readQueryEvidenceSnippet, retrieveContext, withRetrieveSnippetBudget, type RetrieveOptions, @@ -432,6 +434,122 @@ describe('complete small-owner source representation', () => { })).toBeNull() }) + it('promotes an authenticated 26-line owner only when its exact representation fits allocation', () => { + const ownerSource = [ + 'export function adaptiveOwner(input: string) {', + ' const normalized = input.trim();', + " recordCheckpoint('adaptive-owner-start');", + " if (normalized === '') {", + " return { status: 'empty' };", + ' }', + ' let attempt = 0;', + ' while (attempt < 3) {', + ' attempt += 1;', + ' try {', + ' const loaded = loadCandidate(normalized);', + ' if (!loaded.allowed) {', + " continue;", + ' }', + ' const transformed = transformCandidate(loaded);', + ' if (transformed.requiresReview) {', + ' queueReview(transformed);', + " return { status: 'queued' };", + ' }', + ' persistCandidate(transformed);', + " return { status: 'stored' };", + ' } catch (error) {', + " recordCheckpoint('adaptive-owner-finish');", + ' }', + ' }', + '}', + ].join('\n') + const source = `${ownerSource}\nexport function adaptiveOwnerLexicalDecoy() { return 'adaptive-owner-start adaptive-owner-finish'; }\n` + const fixture = generatedFixture(source) + const retrieved = retrieveContext(fixture.graph, { + question: 'Show adaptiveOwner start and finish checkpoints with the intervening persistence control flow', + budget: 4_000, + retrievalLevel: 5, + }) + const expected = numberedLines(ownerSource) + const owner = matchedByLabel(retrieved, 'adaptiveOwner()') + const exactCost = estimateQueryTokens(expected) + const baselineSnippetCost = retrieved.matched_nodes.reduce( + (total, node) => total + estimateQueryTokens(node.snippet ?? ''), + 0, + ) + const allocationBudget = baselineSnippetCost - estimateQueryTokens(owner.snippet!) + exactCost + + expect(owner.snippet).not.toBe(expected) + expect(owner.snippet).toContain('adaptive-owner-start') + expect(owner.snippet).toContain('adaptive-owner-finish') + expect(owner.snippet).not.toContain('persistCandidate(transformed)') + + const allocated = withRetrieveSnippetBudget(retrieved, { + snippetBudget: allocationBudget, + topNWithSnippet: retrieved.matched_nodes.length, + }) + const allocatedOwner = matchedByLabel(allocated, 'adaptiveOwner()') + expect(allocatedOwner.snippet).toBe(expected) + expect(allocatedOwner.snippet_truncated).toBe(false) + expect(allocatedOwner.representation_reason).toBe('complete owner source within snippet allocation') + expect(allocated.snippet_budget_tokens_used).toBe(allocationBudget) + expect(allocated.matched_nodes.map((node) => node.node_id)).toEqual( + retrieved.matched_nodes.map((node) => node.node_id), + ) + + const projected = contextPackFromRetrieveResult(allocated) + const projectedOwner = projected.nodes.find((node) => node.label === 'adaptiveOwner()') + expect(projectedOwner?.snippet).toBe(expected) + expect(projected.token_count).toBeLessThanOrEqual(projected.task_contract.budget) + + const compact = compactRetrieveResultForStdio(retrieved, { + snippetBudget: allocationBudget, + topNWithSnippet: retrieved.matched_nodes.length, + }) + expect(matchedByLabel(compact, 'adaptiveOwner()').snippet).toBe(expected) + + const insufficient = withRetrieveSnippetBudget(retrieved, { + snippetBudget: allocationBudget - 1, + topNWithSnippet: retrieved.matched_nodes.length, + }) + const insufficientOwner = matchedByLabel(insufficient, 'adaptiveOwner()') + expect(insufficientOwner.snippet).not.toBe(expected) + expect(insufficientOwner.snippet).not.toBeNull() + expect(insufficientOwner.snippet_truncated).toBe(true) + expect(insufficientOwner.representation_reason).not.toBe('complete owner source within snippet allocation') + expect(insufficient.snippet_budget_tokens_used).toBeLessThanOrEqual(allocationBudget - 1) + + retrieved.task_contract = { + ...retrieved.task_contract!, + budget: retrieved.token_count, + } + const noTotalHeadroom = withRetrieveSnippetBudget(retrieved, { + snippetBudget: allocationBudget, + topNWithSnippet: retrieved.matched_nodes.length, + }) + expect(matchedByLabel(noTotalHeadroom, 'adaptiveOwner()').snippet).not.toBe(expected) + expect(noTotalHeadroom.token_count).toBeLessThanOrEqual(retrieved.task_contract.budget) + + const externalFallback = readQueryEvidenceSnippet(fixture.sourceFile, 1, { + question: 'Show adaptiveOwner start and finish checkpoints', + label: 'adaptiveOwner()', + nodeKind: 'function', + externalCall: true, + authenticatedOwner: true, + sourceLocation: '1-26', + }) + expect(externalFallback?.snippet).not.toBe(expected) + + const unauthenticatedFallback = readQueryEvidenceSnippet(fixture.sourceFile, 1, { + question: 'Show adaptiveOwner start and finish checkpoints', + label: 'adaptiveOwner()', + nodeKind: 'function', + authenticatedOwner: false, + sourceLocation: '1-26', + }) + expect(unauthenticatedFallback?.snippet).not.toBe(expected) + }, 120_000) + it('admits exactly 2000 original owner characters and rejects 2001', () => { const owner = (characterCount: number) => { const prefix = 'export function characterBoundary() {\n /*' From 2bac5f8f1e293ebea6fdfcdcbe81913a3e25dd13 Mon Sep 17 00:00:00 2001 From: mohammed naji Date: Wed, 9 Sep 2026 01:20:54 +0400 Subject: [PATCH 2/3] test: align membership mutation anchor with owner allocation --- scripts/lib/semantic-independence-selftest.mjs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/scripts/lib/semantic-independence-selftest.mjs b/scripts/lib/semantic-independence-selftest.mjs index 687870c0..cd45861f 100644 --- a/scripts/lib/semantic-independence-selftest.mjs +++ b/scripts/lib/semantic-independence-selftest.mjs @@ -99,13 +99,16 @@ const MEMBERSHIP_ANCHOR = ` const matchedNodes = packedNodes.map((node) => { if (!state) { return node } + if (state.allocationOnly) { + return attachCompleteOwnerState(node, state) + } return attachCompleteOwnerState({ ...node, snippet: state.fullSnippet, snippet_line_number: state.fullLineNumber, snippet_scope: 'symbol' as const, representation_type: 'detail' as const, - representation_reason: COMPLETE_OWNER_REPRESENTATION_REASON, + representation_reason: state.representationReason, }, state) }) ` From 5f91c188f36c21f74b90f8e103651b26a3ef792e Mon Sep 17 00:00:00 2001 From: mohammed naji Date: Wed, 9 Sep 2026 01:55:21 +0400 Subject: [PATCH 3/3] fix(retrieve): preserve owner recovery across null snippet shaping --- src/runtime/retrieve.ts | 16 +++- ...etrieve-small-owner-representation.test.ts | 96 +++++++++++++++++-- 2 files changed, 98 insertions(+), 14 deletions(-) diff --git a/src/runtime/retrieve.ts b/src/runtime/retrieve.ts index f3e8937a..674ffeb9 100644 --- a/src/runtime/retrieve.ts +++ b/src/runtime/retrieve.ts @@ -567,7 +567,7 @@ function applyRetrieveSnippetBudgetToNodes ( typeof compactNode.node_id === 'string' && compactNode.node_id === node.node_id )) - if (sourceNode) { + const sourceState = sourceNode + ? completeOwnerMatchedNodes.get(sourceNode) + : undefined + if ( + sourceNode + && ( + (typeof compactNode.snippet === 'string' && compactNode.snippet.length > 0) + || sourceState?.allocationOnly === true + ) + ) { copyCompleteOwnerState(sourceNode, compactNode) } } diff --git a/tests/unit/retrieve-small-owner-representation.test.ts b/tests/unit/retrieve-small-owner-representation.test.ts index 3ac910af..cf67a141 100644 --- a/tests/unit/retrieve-small-owner-representation.test.ts +++ b/tests/unit/retrieve-small-owner-representation.test.ts @@ -465,6 +465,9 @@ describe('complete small-owner source representation', () => { ].join('\n') const source = `${ownerSource}\nexport function adaptiveOwnerLexicalDecoy() { return 'adaptive-owner-start adaptive-owner-finish'; }\n` const fixture = generatedFixture(source) + const ownerSourceLocation = fixture.graph.nodeEntries() + .find(([, attributes]) => attributes.label === 'adaptiveOwner()')?.[1].source_location + expect(ownerSourceLocation).toBe('L1-L26') const retrieved = retrieveContext(fixture.graph, { question: 'Show adaptiveOwner start and finish checkpoints with the intervening persistence control flow', budget: 4_000, @@ -519,35 +522,92 @@ describe('complete small-owner source representation', () => { expect(insufficientOwner.representation_reason).not.toBe('complete owner source within snippet allocation') expect(insufficient.snippet_budget_tokens_used).toBeLessThanOrEqual(allocationBudget - 1) - retrieved.task_contract = { + const totalConstrained = { + ...retrieved, + task_contract: { ...retrieved.task_contract!, budget: retrieved.token_count, + }, } - const noTotalHeadroom = withRetrieveSnippetBudget(retrieved, { + const noTotalHeadroom = withRetrieveSnippetBudget(totalConstrained, { snippetBudget: allocationBudget, topNWithSnippet: retrieved.matched_nodes.length, }) expect(matchedByLabel(noTotalHeadroom, 'adaptiveOwner()').snippet).not.toBe(expected) - expect(noTotalHeadroom.token_count).toBeLessThanOrEqual(retrieved.task_contract.budget) + expect(noTotalHeadroom.token_count).toBeLessThanOrEqual(totalConstrained.task_contract.budget) + + const directOwnerSource = ownerSource.replace(' attempt += 1;\n', '') + const directFixture = generatedFixture(`${directOwnerSource}\n`) + const directExpected = numberedLines(directOwnerSource) + const directOwnerSourceLocation = directFixture.graph.nodeEntries() + .find(([, attributes]) => attributes.label === 'adaptiveOwner()')?.[1].source_location + expect(directOwnerSourceLocation).toBe('L1-L25') + + const authenticatedComplete = readQueryEvidenceSnippet(directFixture.sourceFile, 1, { + question: 'Show adaptiveOwner start and finish checkpoints', + label: 'adaptiveOwner()', + nodeKind: 'function', + authenticatedOwner: true, + sourceLocation: String(directOwnerSourceLocation), + }) + expect(authenticatedComplete?.snippet).toBe(directExpected) - const externalFallback = readQueryEvidenceSnippet(fixture.sourceFile, 1, { + const externalFallback = readQueryEvidenceSnippet(directFixture.sourceFile, 1, { question: 'Show adaptiveOwner start and finish checkpoints', label: 'adaptiveOwner()', nodeKind: 'function', externalCall: true, authenticatedOwner: true, - sourceLocation: '1-26', + sourceLocation: String(directOwnerSourceLocation), }) - expect(externalFallback?.snippet).not.toBe(expected) + expect(externalFallback?.snippet).not.toBe(directExpected) + expect(externalFallback?.snippet).toContain('adaptive-owner-start') + expect(externalFallback?.snippet).toContain('adaptive-owner-finish') - const unauthenticatedFallback = readQueryEvidenceSnippet(fixture.sourceFile, 1, { + const unauthenticatedFallback = readQueryEvidenceSnippet(directFixture.sourceFile, 1, { question: 'Show adaptiveOwner start and finish checkpoints', label: 'adaptiveOwner()', nodeKind: 'function', authenticatedOwner: false, - sourceLocation: '1-26', + sourceLocation: String(directOwnerSourceLocation), + }) + expect(unauthenticatedFallback?.snippet).not.toBe(directExpected) + expect(unauthenticatedFallback?.snippet).toContain('adaptive-owner-start') + expect(unauthenticatedFallback?.snippet).toContain('adaptive-owner-finish') + + const omitted = withRetrieveSnippetBudget(retrieved, { + snippetBudget: allocationBudget, + topNWithSnippet: 0, }) - expect(unauthenticatedFallback?.snippet).not.toBe(expected) + const omittedOwner = matchedByLabel(omitted, 'adaptiveOwner()') + expect(omittedOwner.snippet).toBeNull() + expect(omittedOwner.snippet_truncated).toBe(true) + expect(omittedOwner.representation_reason).not.toBe('complete owner source within snippet allocation') + expect(omitted.matched_nodes.map((node) => node.node_id)).toEqual( + retrieved.matched_nodes.map((node) => node.node_id), + ) + + const projectedOmitted = contextPackFromRetrieveResult(omitted) + expect(projectedOmitted.nodes.find((node) => node.label === 'adaptiveOwner()')?.snippet).toBeNull() + + const restored = withRetrieveSnippetBudget(omitted, { + snippetBudget: allocationBudget, + topNWithSnippet: omitted.matched_nodes.length, + }) + expect(matchedByLabel(restored, 'adaptiveOwner()').snippet).toBe(expected) + expect(restored.snippet_budget_tokens_used).toBeLessThanOrEqual(allocationBudget) + expect(restored.token_count).toBeLessThanOrEqual(restored.task_contract!.budget) + expect(restored.matched_nodes.map((node) => node.node_id)).toEqual( + omitted.matched_nodes.map((node) => node.node_id), + ) + + const compactRestored = compactRetrieveResultForStdio(omitted, { + snippetBudget: allocationBudget, + topNWithSnippet: omitted.matched_nodes.length, + }) + expect(matchedByLabel(compactRestored, 'adaptiveOwner()').snippet).toBe(expected) + expect(compactRestored.snippet_budget_tokens_used).toBeLessThanOrEqual(allocationBudget) + expect(compactRestored.token_count).toBeLessThanOrEqual(omitted.task_contract!.budget) }, 120_000) it('admits exactly 2000 original owner characters and rejects 2001', () => { @@ -760,6 +820,9 @@ describe('complete small-owner source representation', () => { retrievalLevel: 5, retrievalStrategy: 'slice-v1', }) + const full = fresh() + const fullSnippet = ownerSnippet(full)! + const exactCost = estimateQueryTokens(fullSnippet) for (const options of [ { snippetBudget: 0, topNWithSnippet: 12 }, { snippetBudget: 2_400, topNWithSnippet: 0 }, @@ -768,6 +831,21 @@ describe('complete small-owner source representation', () => { expect(ownerSnippet(shaped)).toBeNull() expect(matchedByLabel(shaped, 'parseDecimalRatio()').snippet_truncated).toBe(true) expect(shaped.snippet_budget_tokens_used).toBe(0) + + const restored = withRetrieveSnippetBudget(shaped, { + snippetBudget: exactCost, + topNWithSnippet: 12, + }) + expect(ownerSnippet(restored)).toBe(fullSnippet) + expect(restored.matched_nodes.map((node) => node.node_id)).toEqual( + shaped.matched_nodes.map((node) => node.node_id), + ) + + const compactNull = compactRetrieveResultForStdio(shaped, { + snippetBudget: exactCost, + topNWithSnippet: 12, + }) + expect(ownerSnippet(compactNull)).toBeNull() } }, 120_000)