mirror of
https://github.com/stablyai/orca.git
synced 2026-09-24 08:02:33 +00:00
19 lines
13 KiB
JSON
19 lines
13 KiB
JSON
{
|
|
"path": "src/main/claude/claude-transcript-branch-proof.ts",
|
|
"sha256": "3224594befe1edae514724caf5eff6a4cda85212e2985be4920b6164b088b82c",
|
|
"changes": [
|
|
{
|
|
"before": "import { readFile } from 'node:fs/promises'",
|
|
"after": "import { createReadStream } from 'node:fs'\nimport { splitTranscriptStreamLines } from '../native-chat/transcript-stream-lines'"
|
|
},
|
|
{
|
|
"before": "export function proveClaudeTranscriptBranchFromJsonl(input: {\n contents: string\n providerSessionId: string\n previousLeafUuid: string | null\n intentionalRewindUuid?: string\n}): ClaudeTranscriptBranchProof {\n const nodes = new Map<string, TranscriptNode>()\n let leafUuid: string | null = null\n let leafMarkerLineIndex = -1\n const lines = input.contents.split('\\n')\n for (const [index, line] of lines.entries()) {\n if (!line.trim()) {\n continue\n }\n let record: unknown\n try {\n record = JSON.parse(line)\n } catch {\n if (index === lines.length - 1 && !input.contents.endsWith('\\n')) {\n throw new ClaudeTranscriptTailIncompleteError()\n }\n throw transcriptError('malformed JSONL')\n }\n if (typeof record !== 'object' || record === null || Array.isArray(record)) {\n throw transcriptError('non-object record')\n }\n const row = record as Record<string, unknown>\n if (row.type === 'last-prompt') {\n const markerSessionId = nonEmptyString(row.sessionId)\n const markerLeaf = nonEmptyString(row.leafUuid)\n if (markerSessionId !== input.providerSessionId || !markerLeaf) {\n throw transcriptError('invalid last-prompt marker')\n }\n leafUuid = markerLeaf\n leafMarkerLineIndex = index\n }\n const uuid = nonEmptyString(row.uuid)\n if (!uuid) {\n continue\n }\n const parentUuid = row.parentUuid === null ? null : nonEmptyString(row.parentUuid)\n if (row.parentUuid !== null && !parentUuid) {\n throw transcriptError(`record ${uuid} has no parent identity`)\n }\n const sessionId = nonEmptyString(row.sessionId)\n const existing = nodes.get(uuid)\n const disallowedLeaf =\n row.isSidechain === true ||\n row.parent_tool_use_id != null ||\n row.type === 'result' ||\n row.type === 'stream_event' ||\n (row.type === 'system' && row.subtype === 'init')\n if (\n existing &&\n (existing.parentUuid !== parentUuid ||\n existing.sessionId !== sessionId ||\n existing.disallowedLeaf !== disallowedLeaf)\n ) {\n throw transcriptError(`record ${uuid} has conflicting ancestry`)\n }\n nodes.set(uuid, {\n parentUuid,\n sessionId,\n lineIndex: existing?.lineIndex ?? index,\n disallowedLeaf\n })\n }\n if (!leafUuid) {\n throw transcriptError('missing last-prompt marker')\n }\n const leaf = nodes.get(leafUuid)\n if (!leaf || leaf.sessionId !== input.providerSessionId || leaf.disallowedLeaf) {\n throw transcriptError('marker leaf is missing from the session graph')\n }\n if (leaf.lineIndex > leafMarkerLineIndex) {\n throw transcriptError('marker precedes its leaf record')\n }\n const previousLeafUuid = input.previousLeafUuid\n if (input.intentionalRewindUuid !== undefined) {\n if (leafUuid !== input.intentionalRewindUuid || !input.previousLeafUuid) {\n throw transcriptError('rewind target does not match the observed leaf')\n }\n proveMainLineAncestry(nodes, input.previousLeafUuid, input.providerSessionId)\n proveAppendOrder(nodes)\n let ancestor = nodes.get(input.previousLeafUuid)?.parentUuid ?? null\n for (let depth = 0; ancestor !== null && depth < MAX_CLAUDE_TRANSCRIPT_ANCESTRY; depth += 1) {\n if (ancestor === leafUuid) {\n return { leafUuid, relation: 'intentional-rewind' }\n }\n ancestor = nodes.get(ancestor)?.parentUuid ?? null\n }\n throw transcriptError('rewind target is not an ancestor of the previous cursor')\n }\n if (!previousLeafUuid) {\n proveMainLineAncestry(nodes, leafUuid, input.providerSessionId)\n // A branch proof is based on an append-only snapshot. A child that appears\n // before its claimed parent is not a post-snapshot descendant observation;\n // accepting that graph would turn reordered/torn rows into durable ancestry.\n proveAppendOrder(nodes)\n return { leafUuid, relation: 'initial' }\n }\n const previous = nodes.get(previousLeafUuid)\n if (!previous) {\n throw new ClaudeTranscriptPreviousCursorMissingError()\n }\n if (previous.sessionId !== input.providerSessionId || previous.disallowedLeaf) {\n throw transcriptError('previous cursor is not on the main transcript')\n }\n // The latest marker can be equal to, or descend from, a sampled cursor. In\n // either case prove the sampled cursor's own ancestry before accepting it;\n // otherwise a cursor that descended through a parent-tool-use sidechain\n // could be persisted and resumed as if it were on the main transcript.\n proveMainLineAncestry(nodes, previousLeafUuid, input.providerSessionId)\n if (leafUuid === previousLeafUuid) {\n proveAppendOrder(nodes)\n return { leafUuid, relation: 'same' }\n }\n const visited = new Set<string>()\n let cursor: string | null = leafUuid\n for (let depth = 0; cursor !== null && depth < MAX_CLAUDE_TRANSCRIPT_ANCESTRY; depth += 1) {\n if (visited.has(cursor)) {\n throw transcriptError('cycle in parentUuid ancestry')\n }\n visited.add(cursor)\n const node = nodes.get(cursor)\n if (!node || node.sessionId !== input.providerSessionId) {\n throw transcriptError(`missing ancestor ${cursor}`)\n }\n if (node.disallowedLeaf) {\n throw transcriptError(`ancestor ${cursor} is not on the main transcript`)\n }\n cursor = node.parentUuid\n if (cursor === previousLeafUuid) {\n proveAppendOrder(nodes)\n return { leafUuid, relation: 'descendant' }\n }\n }\n if (cursor !== null) {\n throw transcriptError('ancestry exceeds the bounded proof limit')\n }\n throw transcriptError('latest marker is on a sibling branch')\n}\n",
|
|
"after": "type BranchProofInput = {\n providerSessionId: string\n previousLeafUuid: string | null\n intentionalRewindUuid?: string\n}\n\nfunction createBranchProof(input: BranchProofInput) {\n const nodes = new Map<string, TranscriptNode>()\n let leafUuid: string | null = null\n let leafMarkerLineIndex = -1\n return { add, finish }\n\n function add(line: string, index: number, terminated: boolean): void {\n if (!line.trim()) {\n return\n }\n let record: unknown\n try {\n record = JSON.parse(line)\n } catch {\n if (!terminated) {\n throw new ClaudeTranscriptTailIncompleteError()\n }\n throw transcriptError('malformed JSONL')\n }\n if (typeof record !== 'object' || record === null || Array.isArray(record)) {\n throw transcriptError('non-object record')\n }\n const row = record as Record<string, unknown>\n if (row.type === 'last-prompt') {\n const markerSessionId = nonEmptyString(row.sessionId)\n const markerLeaf = nonEmptyString(row.leafUuid)\n if (markerSessionId !== input.providerSessionId || !markerLeaf) {\n throw transcriptError('invalid last-prompt marker')\n }\n leafUuid = markerLeaf\n leafMarkerLineIndex = index\n }\n const uuid = nonEmptyString(row.uuid)\n if (!uuid) {\n return\n }\n const parentUuid = row.parentUuid === null ? null : nonEmptyString(row.parentUuid)\n if (row.parentUuid !== null && !parentUuid) {\n throw transcriptError(`record ${uuid} has no parent identity`)\n }\n const sessionId = nonEmptyString(row.sessionId)\n const existing = nodes.get(uuid)\n const disallowedLeaf =\n row.isSidechain === true ||\n row.parent_tool_use_id != null ||\n row.type === 'result' ||\n row.type === 'stream_event' ||\n (row.type === 'system' && row.subtype === 'init')\n if (\n existing &&\n (existing.parentUuid !== parentUuid ||\n existing.sessionId !== sessionId ||\n existing.disallowedLeaf !== disallowedLeaf)\n ) {\n throw transcriptError(`record ${uuid} has conflicting ancestry`)\n }\n nodes.set(uuid, {\n parentUuid,\n sessionId,\n lineIndex: existing?.lineIndex ?? index,\n disallowedLeaf\n })\n }\n\n function finish(): ClaudeTranscriptBranchProof {\n if (!leafUuid) {\n throw transcriptError('missing last-prompt marker')\n }\n const leaf = nodes.get(leafUuid)\n if (!leaf || leaf.sessionId !== input.providerSessionId || leaf.disallowedLeaf) {\n throw transcriptError('marker leaf is missing from the session graph')\n }\n if (leaf.lineIndex > leafMarkerLineIndex) {\n throw transcriptError('marker precedes its leaf record')\n }\n const previousLeafUuid = input.previousLeafUuid\n if (input.intentionalRewindUuid !== undefined) {\n if (leafUuid !== input.intentionalRewindUuid || !input.previousLeafUuid) {\n throw transcriptError('rewind target does not match the observed leaf')\n }\n proveMainLineAncestry(nodes, input.previousLeafUuid, input.providerSessionId)\n proveAppendOrder(nodes)\n let ancestor = nodes.get(input.previousLeafUuid)?.parentUuid ?? null\n for (let depth = 0; ancestor !== null && depth < MAX_CLAUDE_TRANSCRIPT_ANCESTRY; depth += 1) {\n if (ancestor === leafUuid) {\n return { leafUuid, relation: 'intentional-rewind' }\n }\n ancestor = nodes.get(ancestor)?.parentUuid ?? null\n }\n throw transcriptError('rewind target is not an ancestor of the previous cursor')\n }\n if (!previousLeafUuid) {\n proveMainLineAncestry(nodes, leafUuid, input.providerSessionId)\n // A branch proof is based on an append-only snapshot. A child that appears\n // before its claimed parent is not a post-snapshot descendant observation;\n // accepting that graph would turn reordered/torn rows into durable ancestry.\n proveAppendOrder(nodes)\n return { leafUuid, relation: 'initial' }\n }\n const previous = nodes.get(previousLeafUuid)\n if (!previous) {\n throw new ClaudeTranscriptPreviousCursorMissingError()\n }\n if (previous.sessionId !== input.providerSessionId || previous.disallowedLeaf) {\n throw transcriptError('previous cursor is not on the main transcript')\n }\n // The latest marker can be equal to, or descend from, a sampled cursor. In\n // either case prove the sampled cursor's own ancestry before accepting it;\n // otherwise a cursor that descended through a parent-tool-use sidechain\n // could be persisted and resumed as if it were on the main transcript.\n proveMainLineAncestry(nodes, previousLeafUuid, input.providerSessionId)\n if (leafUuid === previousLeafUuid) {\n proveAppendOrder(nodes)\n return { leafUuid, relation: 'same' }\n }\n const visited = new Set<string>()\n let cursor: string | null = leafUuid\n for (let depth = 0; cursor !== null && depth < MAX_CLAUDE_TRANSCRIPT_ANCESTRY; depth += 1) {\n if (visited.has(cursor)) {\n throw transcriptError('cycle in parentUuid ancestry')\n }\n visited.add(cursor)\n const node = nodes.get(cursor)\n if (!node || node.sessionId !== input.providerSessionId) {\n throw transcriptError(`missing ancestor ${cursor}`)\n }\n if (node.disallowedLeaf) {\n throw transcriptError(`ancestor ${cursor} is not on the main transcript`)\n }\n cursor = node.parentUuid\n if (cursor === previousLeafUuid) {\n proveAppendOrder(nodes)\n return { leafUuid, relation: 'descendant' }\n }\n }\n if (cursor !== null) {\n throw transcriptError('ancestry exceeds the bounded proof limit')\n }\n throw transcriptError('latest marker is on a sibling branch')\n }\n}\n\nexport function proveClaudeTranscriptBranchFromJsonl(\n input: BranchProofInput & { contents: string }\n): ClaudeTranscriptBranchProof {\n const proof = createBranchProof(input)\n const lines = input.contents.split('\\n')\n for (const [index, line] of lines.entries()) {\n proof.add(line, index, index < lines.length - 1)\n }\n return proof.finish()\n}\n"
|
|
},
|
|
{
|
|
"before": " return proveClaudeTranscriptBranchFromJsonl({\n contents: await readFile(input.transcriptPath, 'utf8'),\n providerSessionId: input.providerSessionId,\n previousLeafUuid: input.previousLeafUuid,\n intentionalRewindUuid: input.intentionalRewindUuid\n })",
|
|
"after": " const proof = createBranchProof(input)\n let index = 0\n for await (const record of splitTranscriptStreamLines(createReadStream(input.transcriptPath))) {\n proof.add(record.line, index++, record.terminated)\n }\n return proof.finish()"
|
|
}
|
|
]
|
|
}
|