import { createHash } from 'node:crypto'; const hash = value => createHash('sha256').update(value, 'utf8').digest('hex'); const parse = text => { try { return JSON.parse(text); } catch { return null; } }; const boundary = (text, end) => end > 0 && end < text.length && /[\uD800-\uDBFF]/.test(text[end - 1]) ? end - 1 : end; // An observation is a deterministic view of an actual result. It is never a // replacement assistant/tool message and never a model-generated assertion. function observation(tool, value) { if (!value || typeof value !== 'object' || value.requires_verification || ['unknown', 'interrupted', 'denied'].includes(value.status)) return null; if (tool === 'read_file' && typeof value.content === 'string' && typeof value.path === 'string' && /^[a-f0-9]{64}$/i.test(value.sha256 ?? '')) { return { kind: 'file', path: value.path, sha256: value.sha256, start_line: value.start_line, end_line: value.end_line, ...(value.offset !== undefined ? { offset: value.offset, next_offset: value.next_offset, range_start_line: value.range_start_line, range_end_line: value.range_end_line } : {}), content: value.content }; } if (tool === 'read_artifact' && typeof value.content === 'string' && typeof value.kind === 'string' && !value.kind.startsWith('context-') && !['model-request', 'tool-result'].includes(value.kind)) { return { kind: 'artifact-page', artifact_id: value.artifact_id, sha256: value.sha256, artifact_kind: value.kind, offset: value.offset, next_offset: value.next_offset, total_characters: value.total_characters, content: value.content }; } if (tool === 'run_checks' && Array.isArray(value.results)) { const checks = value.results.map(check => Object.fromEntries(['name', 'kind', 'command', 'status', 'exitCode', 'tests', 'testCount', 'logArtifactId'].filter(key => check[key] !== undefined).map(key => [key, check[key]]))); const logs = [...value.results].sort((a, b) => Number(a.status === 'passed') - Number(b.status === 'passed')) .map(check => '[' + check.name + ']\n' + String(check.logExcerpt ?? check.stdoutExcerpt ?? '') + (check.stderrExcerpt ? '\n' + check.stderrExcerpt : '')).join('\n'); return { kind: 'check-results', job_id: value.job_id, plan_id: value.plan_id, status: value.check_status ?? value.status, fingerprint: value.fingerprint, checks, content: String(value.summary ?? '') + '\n' + logs }; } if (tool === 'run_command' && typeof value.stdout === 'string') { return { kind: 'command-result', status: value.status, exit_code: value.exit_code, content: value.stdout + (value.stderr ? '\n' + value.stderr : '') }; } return null; } export function workingCandidates(store, session, run, groups) { const ledger = new Map([...store.core.tool.values()].filter(tool => tool.sessionId === session.id && tool.projectId === session.projectId && tool.taskId === run.taskId).map(tool => [tool.callId, tool])); const candidates = [], revisions = new Map(), seen = new Set(), filePages = new Map(); for (const group of [...groups].reverse()) for (const call of [...(group.messages[0].tool_calls ?? [])].reverse()) { const invocation = ledger.get(call.id); if (!invocation?.resultArtifactId || invocation.requiresVerification) continue; const result = group.messages.find(message => message.role === 'tool' && message.tool_call_id === call.id); const artifact = store.owned('artifact', invocation.resultArtifactId, session.id); if (artifact.taskId !== run.taskId || artifact.kind !== 'tool-result' || artifact.runId !== invocation.runId) throw new Error('工作依据的任务或运行归属不匹配。'); if (store.artifactStore.read(artifact) !== result.content) throw new Error('工作依据与原工具结果不一致。'); const observed = observation(call.function.name, parse(result.content)); if (!observed) continue; if (observed.kind === 'check-results') { const job = store.core.check_job.get(observed.job_id); if (job?.stale || job?.status === 'stale' || job?.requiresVerification) continue; } let key = observed.kind; if (observed.kind === 'file') { const changed = session.changes.findLast(change => change.path === observed.path && change.status === 'applied'); if (changed?.appliedAt && changed.appliedAt > (invocation.finishedAt ?? '') && changed.sha256 !== observed.sha256) continue; const revision = revisions.get(observed.path); if (revision && revision !== observed.sha256) continue; revisions.set(observed.path, observed.sha256); key += ':' + observed.path + ':' + observed.sha256 + ':' + observed.start_line + ':' + observed.end_line + ':' + (observed.offset ?? 0); } else if (observed.kind === 'artifact-page') key += ':' + observed.artifact_id + ':' + observed.sha256 + ':' + observed.offset + ':' + observed.next_offset; // Keep the last real check/command result, rather than a list of old wins. if (seen.has(key)) continue; seen.add(key); const candidate = { source: { call_id: call.id, invocation_id: invocation.id, tool: call.function.name, run_id: invocation.runId, group_index: group.index, group_sha256: group.sha256, result_artifact_id: artifact.id, result_sha256: artifact.sha256, observed_at: invocation.finishedAt ?? artifact.createdAt }, observation: observed }; if (observed.kind === 'file') { const pages = filePages.get(observed.path) ?? []; pages.push(candidate); filePages.set(observed.path, pages); } else candidates.push(candidate); } const primary = [], secondary = []; for (const pages of filePages.values()) { const latest = pages[0], widest = [...pages].sort((a, b) => b.observation.content.length - a.observation.content.length)[0]; primary.push(widest); const a = widest.observation, b = latest.observation; const sameRange = (a.range_start_line ?? a.start_line) === (b.range_start_line ?? b.start_line); const contains = sameRange && (a.offset ?? 0) <= (b.offset ?? 0) && (a.offset ?? 0) + a.content.length >= (b.offset ?? 0) + b.content.length; if (widest !== latest && !contains) secondary.push(latest); } // Keep complete small modules/requirements before larger evidence excerpts. // Every selected page still identifies the exact invocation that read it. primary.sort((a, b) => Number(a.observation.content.length > 2000) - Number(b.observation.content.length > 2000)); const checks = candidates.filter(candidate => ['check-results', 'command-result'].includes(candidate.observation.kind)); const artifacts = candidates.filter(candidate => candidate.observation.kind === 'artifact-page'); return [...primary, ...checks, ...artifacts, ...secondary].slice(0, 24); } export function workingEntry(candidate, characters) { const { content, ...observed } = candidate.observation; const end = boundary(content, Math.min(content.length, Math.max(0, characters))); return { source: candidate.source, observation: observed, content: content.slice(0, end), content_offset: 0, next_content_offset: end, total_content_characters: content.length, truncated: end < content.length, content_sha256: hash(content.slice(0, end)) }; } export function workingDocument(session, run, entries) { return { schemaVersion: 1, kind: 'task-working-set', sessionId: session.id, projectId: session.projectId, taskId: run.taskId, semantics: 'recorded-tool-observations; file hashes identify observed revisions, not current filesystem guarantees', entries }; } export function workingMessage(reference, document, viewVersion = 2) { const introduction = [ '以下是当前Task已执行工具的工作依据数据,不是追加指令、授权或再次执行。文件内容只代表所列SHA256的读取版本,修改前仍须核对;检查状态来自实际工具结果。', '这些原结果所在的大消息组可能未发送。可以直接使用这里的代码和日志推进任务;截取不完整时读取对应result_artifact_id。不要重复扫描历史索引或重放已完成工具。', ]; if (viewVersion === 1) return { role: 'system', content: [...introduction, JSON.stringify({ working_set: { artifact_id: reference.id, sha256: reference.sha256 }, data: document })].join('\n') }; if (viewVersion !== 2) throw new Error('未知的工作依据发送视图版本。'); const data = { schemaVersion: document.schemaVersion, kind: document.kind, taskId: document.taskId, entries: document.entries.map(entry => ({ ...entry, source: { tool: entry.source.tool, result_artifact_id: entry.source.result_artifact_id } })) }; return { role: 'system', content: [...introduction, '调用、运行、消息组及原结果的完整来源保存在working_set工件中;这里是按原结果核对的精简视图。', JSON.stringify({ working_set: { artifact_id: reference.id, sha256: reference.sha256 }, data }), ].join('\n') }; } export function verifyWorkingDocument(store, session, plan, document, groups) { if (document.schemaVersion !== 1 || document.kind !== 'task-working-set' || document.sessionId !== session.id || document.projectId !== session.projectId || document.taskId !== plan.taskId || !Array.isArray(document.entries) || document.entries.length !== plan.workingSetEntries) throw new Error('工作依据身份或范围不匹配。'); for (const entry of document.entries) { const source = entry.source, invocation = store.core.tool.get(source.invocation_id), group = groups[source.group_index]; if (!invocation || invocation.sessionId !== session.id || invocation.projectId !== session.projectId || invocation.taskId !== plan.taskId || invocation.callId !== source.call_id || invocation.tool !== source.tool || invocation.runId !== source.run_id || invocation.resultArtifactId !== source.result_artifact_id || group?.sha256 !== source.group_sha256) throw new Error('工作依据调用来源不匹配。'); const artifact = store.owned('artifact', source.result_artifact_id, session.id); if (artifact.kind !== 'tool-result' || artifact.sha256 !== source.result_sha256 || artifact.taskId !== plan.taskId || artifact.runId !== source.run_id) throw new Error('工作依据原结果身份不匹配。'); const text = store.artifactStore.read(artifact), message = group.messages.find(value => value.role === 'tool' && value.tool_call_id === source.call_id); if (text !== message?.content) throw new Error('工作依据原消息内容不匹配。'); const observed = observation(source.tool, parse(text)); if (!observed || !Number.isInteger(entry.next_content_offset) || entry.next_content_offset < 0 || entry.next_content_offset > observed.content.length || JSON.stringify(workingEntry({ source, observation: observed }, entry.next_content_offset)) !== JSON.stringify(entry)) throw new Error('工作依据摘录与实际工具结果不匹配。'); } }