'use strict' const { test } = require('node:test') const assert = require('node:assert/strict') const { VERSION, checkpointHash, runDiagnosticContinuation, sha256, } = require('./wiki_leaf_diagnostic_continuation.cjs') function fixture() { const source = 'alpha\n'.repeat(20) + 'TARGET-WINDOW\n' + 'omega\n'.repeat(20) const targetText = 'TARGET-WINDOW\n' const start = source.indexOf(targetText) const end = start + Array.from(targetText).length const windows = [ { key: checkpointHash(['wiki-full-read-v1', 'alpha\n'.repeat(20)]), start_rune: 0, end_rune: start, text_sha256: sha256('alpha\n'.repeat(20)), status: 'checkpointed', result: { entities: [] } }, { key: checkpointHash(['wiki-full-read-v1', targetText]), start_rune: start, end_rune: end, text_sha256: sha256(targetText), status: 'missing' }, { key: checkpointHash(['wiki-full-read-v1', 'omega\n'.repeat(20)]), start_rune: end, end_rune: Array.from(source).length, text_sha256: sha256('omega\n'.repeat(20)), status: 'checkpointed', result: { concepts: [] } }, ] const manifest = { version: VERSION, max_model_calls: 1, scope: { tenant_id: 10059, knowledge_base_id: 'kb', knowledge_id: 'doc', model_id: 'model', checkpoint_fingerprint: 'a'.repeat(64), source_sha256: sha256(source), parent_attempt: 6, parent_pending_op_id: 4145, diagnostic_pending_op_id: 9001, reviewed_release: 'public-reviewed', reviewed_api_image: 'sha256:reviewed', target_window: { key: windows[1].key, start_rune: start, end_rune: end, text_sha256: windows[1].text_sha256 }, }, windows, } return { source, manifest } } test('reuses completed siblings and captures exactly one identity failure', () => { const { source, manifest } = fixture() let calls = 0 let capture const result = runDiagnosticContinuation(manifest, source, (text, request) => { calls++ assert.equal(text, 'TARGET-WINDOW\n') assert.equal(request.max_model_calls, 1) return { status: 'identity_failure', response: '{bad candidate}' } }, artifact => { capture = artifact }) assert.equal(calls, 1) assert.equal(result.receipt.status, 'captured') assert.equal(capture.response_sha256, sha256('{bad candidate}')) assert.equal(capture.response, undefined) assert.deepEqual(result.receipt.completed_sibling_keys, [manifest.windows[0].key, manifest.windows[2].key]) assert.deepEqual(result.windows, manifest.windows) }) test('saves only a validated target result and never runs a second call', () => { const { source, manifest } = fixture() let calls = 0 const result = runDiagnosticContinuation(manifest, source, () => { calls++ return { status: 'leaf_succeeded', result: { entities: [{ name: 'Target', slug: 'entity/target' }] } } }) assert.equal(calls, 1) assert.equal(result.receipt.status, 'leaf_succeeded') assert.equal(result.receipt.target_checkpoint_saved, true) assert.equal(result.windows.filter(window => window.status === 'checkpointed').length, 3) assert.deepEqual(result.windows[1].result.entities[0], { name: 'Target', slug: 'entity/target' }) }) test('rejects an already checkpointed target and source/scope drift before model call', () => { const { source, manifest } = fixture() let calls = 0 const completedTarget = structuredClone(manifest) completedTarget.windows[1] = { ...completedTarget.windows[1], status: 'checkpointed', result: {} } assert.throws(() => runDiagnosticContinuation(completedTarget, source, () => { calls++ }), /exactly one missing target/) assert.equal(calls, 0) assert.throws(() => runDiagnosticContinuation(manifest, `${source}drift`, () => { calls++ }), /source SHA-256/) assert.equal(calls, 0) const changedScope = structuredClone(manifest) changedScope.scope.target_window.end_rune++ assert.throws(() => runDiagnosticContinuation(changedScope, source, () => { calls++ }), /target window does not match/) assert.equal(calls, 0) }) test('rejects a second missing leaf, a wrong key, and a budget larger than one', () => { const { source, manifest } = fixture() const secondMissing = structuredClone(manifest) secondMissing.windows[2] = { ...secondMissing.windows[2], status: 'missing' } assert.throws(() => runDiagnosticContinuation(secondMissing, source, () => ({})), /exactly one missing target/) const wrongKey = structuredClone(manifest) wrongKey.windows[1].key = 'b'.repeat(64) wrongKey.scope.target_window.key = wrongKey.windows[1].key assert.throws(() => runDiagnosticContinuation(wrongKey, source, () => ({})), /target key does not match/) const overBudget = structuredClone(manifest) overBudget.max_model_calls = 2 assert.throws(() => runDiagnosticContinuation(overBudget, source, () => ({})), /exactly one model call/) })