UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

78 lines • 3.34 kB
import { ANTHROPIC_SUBSCRIPTION_PROVIDER_ID } from "./account-management.js"; import { CLAUDE_SDK_OAUTH_API_ID } from "./api-id.js"; import { firstDivergedPath, snapshotProjection, } from "./session-commit-divergence.js"; import { sessionSyncDigest } from "./session-sync.js"; /** * Only the payload the model produced is fingerprinted. Everything the stream * pipeline stamps around it (thinking timing, content-block indices, partial * JSON) can legitimately differ between the last `message_update` and `message_end` * without any extension rewriting the answer; hashing such fields marked plain * turns `assistant_rewritten` and forced a full re-send on the next turn * (senpi#691, oh-my-openagent#7925). An unknown block shape stays fail-closed. */ function semanticContentBlock(block) { switch (block.type) { case "text": return { type: block.type, text: block.text }; case "thinking": return { type: block.type, thinking: block.thinking, thinkingSignature: block.thinkingSignature }; case "toolCall": return { type: block.type, id: block.id, name: block.name, arguments: block.arguments }; default: return block; } } function semanticProjection(message) { return { role: message.role, api: message.api, provider: message.provider, model: message.model, content: message.content.map(semanticContentBlock), }; } export function assistantContentHash(message) { return sessionSyncDigest(semanticProjection(message)); } export function isResidentAssistant(message, modelId) { return (message.api === CLAUDE_SDK_OAUTH_API_ID && message.provider === ANTHROPIC_SUBSCRIPTION_PROVIDER_ID && message.model === modelId); } export function isTerminalFailure(message) { return message.stopReason === "error" || message.stopReason === "aborted"; } /** * Divergence is decided by comparing what the provider streamed against what the * session ledger committed, never by in-flight staging: a result-only turn fills * its content at the terminal message with no preceding delta, so a staged-hash * comparison reports false divergence on a perfectly valid SDK response shape. */ export class AssistantCommitBoundary { constructor() { this.providerFinalByKey = new Map(); } captureProviderFinal(key, message) { const projection = semanticProjection(message); this.providerFinalByKey.set(key, { hash: sessionSyncDigest(projection), projection: snapshotProjection(projection), }); } commit(key, message, modelId) { const providerFinal = this.providerFinalByKey.get(key); this.providerFinalByKey.delete(key); if (!isResidentAssistant(message, modelId)) return { outcome: "not-resident" }; if (providerFinal === undefined) return { outcome: "clean" }; const committed = semanticProjection(message); if (providerFinal.hash === sessionSyncDigest(committed)) return { outcome: "clean" }; return { outcome: "rewritten", divergedPath: firstDivergedPath(providerFinal.projection, committed) }; } forget(key) { this.providerFinalByKey.delete(key); } } //# sourceMappingURL=session-commit-boundary.js.map