fix(snapshot): harden message id retention
This commit is contained in:
@@ -2,5 +2,5 @@
|
||||
# side as of the last confirmed-consistent state. Both languages carry equal authority;
|
||||
# after editing either side, bring the other along and re-record with:
|
||||
# pnpm run verify-translation-pairing --write packages/support/acp-snapshot/README.md
|
||||
README.md: 9d142dc964e60f9508b6c137525eb916cdbd969f
|
||||
README.zh.md: 2e88e7c5bb8acb0cd99a35b5fe0fbfc15d6101e3
|
||||
README.md: 0b935ef60c33fd24660d8ecf2497f5506157c724
|
||||
README.zh.md: 91be3c97bcb67ce10c61513113f683e741bc762f
|
||||
|
||||
@@ -8,8 +8,8 @@ Four layers, importable separately:
|
||||
|
||||
- **`launchAcpTestAgent` (launcher)** — boots a source agent under tsx or a built `lib` agent under plain Node from a supplied cwd, connects the SDK client over a raw-byte stdout tee, collects session updates and stderr, surfaces asynchronous spawn failures through startup, fails closed on unhandled permission requests, and owns graceful or signalled shutdown. Shutdown waits for process exit, inherited stdio closure, and ACP parser exhaustion before resolving or propagating a child error, so captures are complete and callers can remove owned paths after either outcome. When Windows accepts forced termination but publishes its exit marker asynchronously, shutdown gives that marker a bounded grace before treating fallback refusal as a second failure. Snapshot and ordinary e2e suites share this process boundary; a test supplies only agent paths, cwd, environment overrides, and any permission policy.
|
||||
- **`runScenario` (harness)** — drives ACP JSON-RPC stdio from a deterministic `input.json` script through the launcher, tees raw stdout for the expected-output and purity checks, and harvests every persisted raw JSONL session log (parent and subagent children, primary-first) after graceful stdin EOF. `AgentUnderTest` supplies absolute `binScript`, optional `libBinScript`, `configPath`, and `tsconfigPath` paths because the subprocess cwd is outside the repo; `workspaceParent` may move the generated child cwd from the platform temp directory when that grant is itself under test. Startup failures preserve captured agent stderr in the rejected diagnostic.
|
||||
- **Normalizers** — pure functions turning captured surfaces into stable text or portable fixtures: `normalizeStdout` (JSON-RPC ids → first-seen sequence; UUIDs and every native/JavaScript filesystem spelling of the generated cwd → tokens, longest-first; cwd-rooted separators selected as canonical `/` or host-native; doubles as the stdout-purity check), `normalizeSessionLog` (times zeroed, `seq` kept, the same cwd-path policy), `tokenizeSessionFixtureCwd` (the generated workspace and its filesystem aliases → one canonical `{{cwd}}`, including an already-tokenized macOS `/private` alias; authored temp paths unchanged), `scrubSystemPrompts` (prompt text → `{{system}}`), `scrubToolSchemas` (schema bulk → `{{tools}}`), `scrubRequestHeaders` (all header bulk → `{{system}}`/`{{tools}}`/`{{messagePrefix}}` outside each pin, structure kept — [pinned-header Agent Note](../../../.agents/notes/archived/testing/2026-07-06-pin-request-header-content-in-one-scenario.md)), and `stabilizeFixtureMessageIds` (committed UUIDs carried into unchanged, unambiguous messages across any recorder's fixture-ready parent/child logs).
|
||||
- **`defineAcpSnapshotSuite` (factory)** — registers the whole describe/it tree for a scenario table: per-scenario expected-output and re-persisted-log comparisons, record/refresh fixture write-back, rejection of structured `UNKNOWN_TOOL` results, a tokenized pin per header class composed with independently shared `system-prompt.expected.md` and `tool-schemas.expected.json` sidecars, and a live uniformity guard. Its fixture guards reject orphan scenario dirs, missing files, multiple pins for one class, duplicate sidecar content, noncanonical macOS-prefixed cwd tokens, unscrubbed JSONL headers, and malformed pinning headers. Before record or refresh writes fixtures, an unchanged complete message retains its committed UUID when its identity-free value resolves to exactly one fresh ID and one existing ID across the scenario's parent/child logs; new, changed, and ambiguous messages keep fresh UUIDs. Refresh evaluates fresh leaves with the harvested run's ids, cwd, and every cwd alias, then reuses normalized-equivalent leaves only when the complete logical-record layout aligns and volatile string replacements form a bijection; ambiguous logs keep fresh strings, and fresh semantic values remain authoritative. It also expands packed timing envelopes before aligning event times, so switching between packed and unpacked layouts cannot shift later records. A newly inserted `session/title` receives its preceding event's time so feature-driven insertions do not churn the remainder of a fixture. Each scenario directory's `session.jsonl` plus contiguous `session.<n>.jsonl` siblings are the ordered primary/child inventory; the scenario table does not duplicate their count. Must be called at vitest collection time.
|
||||
- **Normalizers** — pure functions turning captured surfaces into stable text or portable fixtures: `normalizeStdout` (JSON-RPC ids → first-seen sequence; UUIDs and every native/JavaScript filesystem spelling of the generated cwd → tokens, longest-first; cwd-rooted separators selected as canonical `/` or host-native; doubles as the stdout-purity check), `normalizeSessionLog` (times zeroed, `seq` kept, the same cwd-path policy), `tokenizeSessionFixtureCwd` (the generated workspace and its filesystem aliases → one canonical `{{cwd}}`, including an already-tokenized macOS `/private` alias; authored temp paths unchanged), `scrubSystemPrompts` (prompt text → `{{system}}`), `scrubToolSchemas` (schema bulk → `{{tools}}`), `scrubRequestHeaders` (all header bulk → `{{system}}`/`{{tools}}`/`{{messagePrefix}}` outside each pin, structure kept — [pinned-header Agent Note](../../../.agents/notes/archived/testing/2026-07-06-pin-request-header-content-in-one-scenario.md)), and `stabilizeFixtureMessageIds` (committed UUIDs carried into unchanged, mutually unique messages by structurally rewriting only complete surface and durable-inbox message ID fields across any recorder's fixture-ready parent/child logs).
|
||||
- **`defineAcpSnapshotSuite` (factory)** — registers the whole describe/it tree for a scenario table: per-scenario expected-output and re-persisted-log comparisons, record/refresh fixture write-back, rejection of structured `UNKNOWN_TOOL` results, a tokenized pin per header class composed with independently shared `system-prompt.expected.md` and `tool-schemas.expected.json` sidecars, and a live uniformity guard. Its fixture guards reject orphan scenario dirs, missing files, multiple pins for one class, duplicate sidecar content, noncanonical macOS-prefixed cwd tokens, unscrubbed JSONL headers, and malformed pinning headers. Before record or refresh writes fixtures, an unchanged complete message retains its committed UUID only when both its ID and identity-free fingerprint are unique across the scenario's fixture-ready parent/child logs; the session package's authoritative surface-type predicate selects surface carriers, correlated `agent/inbox/spliced` copies join the same mapping, and only validated `id` fields in those carriers are rewritten. New, changed, malformed, and graph-ambiguous messages keep fresh UUIDs. Refresh evaluates fresh leaves with the harvested run's ids, cwd, and every cwd alias, then reuses normalized-equivalent leaves only when the complete logical-record layout aligns and volatile string replacements form a bijection; complete message IDs in surface or inbox carriers are excluded because the later structural pass owns them, ambiguous logs keep fresh strings, and fresh semantic values remain authoritative. It also expands packed timing envelopes before aligning event times, so switching between packed and unpacked layouts cannot shift later records. A newly inserted `session/title` receives its preceding event's time so feature-driven insertions do not churn the remainder of a fixture. Each scenario directory's `session.jsonl` plus contiguous `session.<n>.jsonl` siblings are the ordered primary/child inventory; the scenario table does not duplicate their count. Must be called at vitest collection time.
|
||||
|
||||
Committed session fixtures use canonical packed rows. An in-flight branch that merges this contract runs the [temporary repository migrator](../../../scripts/migrate-packed-session-fixtures.ts) with `pnpm run migrate:packed-session-fixtures`; its [removal proposal](../../../.agents/notes/proposed/process/2026-07-26-remove-packed-session-fixture-migrator.md) owns deletion after affected branches converge.
|
||||
|
||||
|
||||
@@ -8,8 +8,8 @@ ACP 快照套件工具包:无密钥快照层(`pnpm run test:snapshot`,见[
|
||||
|
||||
- **`launchAcpTestAgent`(启动器)**:从指定 cwd 在 tsx 下启动源 agent,或在普通 Node 下启动已构建 `lib` agent;通过原始字节 stdout tee 连接 SDK 客户端,收集会话更新和 stderr,在启动过程中公开异步 spawn 失败,对未处理权限请求快速失败,并负责优雅或带信号关闭。关闭会等待进程退出、继承 stdio 关闭和 ACP parser 耗尽,然后才解析或传播子级错误,使捕获内容完整,且调用方可在任一结果后移除自有路径。当 Windows 接受强制终止但异步发布退出标记时,关闭会给该标记有界宽限,然后才将回退拒绝视为第二次失败。快照和普通 e2e 套件共享该进程边界;测试只需提供 agent 路径、cwd、环境覆盖和任何权限策略。
|
||||
- **`runScenario`(harness)**:通过启动器从确定性 `input.json` 脚本驱动 ACP JSON-RPC stdio,将原始 stdout tee 给预期输出和纯度检查,并在优雅 stdin EOF 后收集每个持久化原始 JSONL 会话日志(父级和 subagent 子级,主级优先)。`AgentUnderTest` 提供绝对 `binScript`、可选 `libBinScript`、`configPath` 和 `tsconfigPath` 路径,因为子进程 cwd 位于仓库外。当生成子级 cwd 自身位于待测授权中时,`workspaceParent` 可以将它从平台临时目录移出。启动失败会在拒绝诊断中保留已捕获 agent stderr。
|
||||
- **规范化器**:将已捕获接口转换为稳定文本或可移植 fixture 的纯函数:`normalizeStdout`(JSON-RPC id → 首次出现序列;UUID 以及生成 cwd 的每个原生/JavaScript 文件系统写法 → token,按最长优先;根据 cwd 的分隔符选择规范 `/` 或宿主原生形式;同时作为 stdout 纯度检查)、`normalizeSessionLog`(时间归零、保留 `seq`、使用同一 cwd 路径策略)、`tokenizeSessionFixtureCwd`(生成的 workspace 及其文件系统别名,包括已 token 化的 macOS `/private` 别名 → 单一规范 `{{cwd}}`;手工编写的临时路径保持不变)、`scrubSystemPrompts`(提示词文本 → `{{system}}`)、`scrubToolSchemas`(schema bulk → `{{tools}}`)、`scrubRequestHeaders`(每个 pin 之外的所有 header bulk → `{{system}}`/`{{tools}}`/`{{messagePrefix}}`,保留结构;见[header 固定 Agent Note](../../../.agents/notes/archived/testing/2026-07-06-pin-request-header-content-in-one-scenario.md))和 `stabilizeFixtureMessageIds`(针对任意录制器已准备写入 fixture 的父级/子级日志,将已提交 UUID 带入未变化且无歧义的消息)。
|
||||
- **`defineAcpSnapshotSuite`(工厂)**:为场景表注册完整 describe/it 树:每场景预期输出与重新持久化日志比较、录制/刷新 fixture 回写、拒绝结构化 `UNKNOWN_TOOL` 结果、每个 header 类别一个 token 化 pin(由可独立共享的 `system-prompt.expected.md` 和 `tool-schemas.expected.json` sidecar 组合而成),以及实时一致性保护。其 fixture 保护会拒绝遗留场景目录、缺失文件、一个类别包含多个 pin、重复的 sidecar 内容、带非规范 macOS 前缀的 cwd token、未擦除的 JSONL header,以及格式错误的 pin header。在录制或刷新写入 fixture 前,如果一条未变化的完整消息去除身份后的值在场景的父级/子级日志中恰好对应一个本次生成的 ID 和一个现有 ID,它就会保留已提交的 UUID;新增、发生变化和有歧义的消息则保留本次生成的 UUID。刷新会使用收集所得本次运行的 id、cwd 及全部 cwd 别名评估本次生成的叶值;只有完整逻辑记录布局对齐且易变字符串替换形成双射时,才会复用规范化后等价的叶值;有歧义的日志保留本次生成的字符串,而本次生成的语义值仍为权威数据。它还会在对齐事件时间前展开打包时序 envelope,因此切换打包/非打包布局无法移动后续记录。新插入的 `session/title` 使用前一个事件的时间,因此功能驱动的插入不会扰动 fixture 余下部分。每个场景目录的 `session.jsonl` 和连续 `session.<n>.jsonl` 同级文件是有序主级/子级清单;场景表不重复其数量。必须在 vitest 收集时调用。
|
||||
- **规范化器**:将已捕获接口转换为稳定文本或可移植 fixture 的纯函数:`normalizeStdout`(JSON-RPC id → 首次出现序列;UUID 以及生成 cwd 的每个原生/JavaScript 文件系统写法 → token,按最长优先;根据 cwd 的分隔符选择规范 `/` 或宿主原生形式;同时作为 stdout 纯度检查)、`normalizeSessionLog`(时间归零、保留 `seq`、使用同一 cwd 路径策略)、`tokenizeSessionFixtureCwd`(生成的 workspace 及其文件系统别名,包括已 token 化的 macOS `/private` 别名 → 单一规范 `{{cwd}}`;手工编写的临时路径保持不变)、`scrubSystemPrompts`(提示词文本 → `{{system}}`)、`scrubToolSchemas`(schema bulk → `{{tools}}`)、`scrubRequestHeaders`(每个 pin 之外的所有 header bulk → `{{system}}`/`{{tools}}`/`{{messagePrefix}}`,保留结构;见[header 固定 Agent Note](../../../.agents/notes/archived/testing/2026-07-06-pin-request-header-content-in-one-scenario.md))和 `stabilizeFixtureMessageIds`(针对任意录制器已准备写入 fixture 的父级/子级日志,通过结构化方式仅改写 surface 和持久 inbox 中完整消息的 ID 字段,将已提交 UUID 带入未变化且双向唯一匹配的消息)。
|
||||
- **`defineAcpSnapshotSuite`(工厂)**:为场景表注册完整 describe/it 树:每场景预期输出与重新持久化日志比较、录制/刷新 fixture 回写、拒绝结构化 `UNKNOWN_TOOL` 结果、每个 header 类别一个 token 化 pin(由可独立共享的 `system-prompt.expected.md` 和 `tool-schemas.expected.json` sidecar 组合而成),以及实时一致性保护。其 fixture 保护会拒绝遗留场景目录、缺失文件、一个类别包含多个 pin、重复的 sidecar 内容、带非规范 macOS 前缀的 cwd token、未擦除的 JSONL header,以及格式错误的 pin header。在录制或刷新写入 fixture 前,仅当一条未变化完整消息的 ID 及其去除身份后的指纹在场景可写入 fixture 的父级/子级日志中均唯一时,该消息才会保留已提交的 UUID;会话包的权威 surface 类型谓词负责选择 surface 载体,与其关联的 `agent/inbox/spliced` 副本也纳入同一映射,且仅改写这些载体中通过验证的 `id` 字段。新增、发生变化、格式错误以及图关系存在歧义的消息保留本次生成的 UUID。刷新会使用收集所得本次运行的 id、cwd 及全部 cwd 别名评估本次生成的叶值;只有完整逻辑记录布局对齐且易变字符串替换形成双射时,才会复用归一化后等价的叶值;surface 或 inbox 载体中的完整消息 ID 不参与此路径,因为后续结构化处理负责这些 ID;有歧义的日志保留本次生成的字符串,而本次生成的语义值仍为权威数据。它还会在对齐事件时间前展开打包时序 envelope,因此切换打包/非打包布局无法移动后续记录。新插入的 `session/title` 使用前一个事件的时间,因此功能驱动的插入不会扰动 fixture 余下部分。每个场景目录的 `session.jsonl` 和连续 `session.<n>.jsonl` 同级文件是有序主级/子级清单;场景表不重复其数量。必须在 vitest 收集时调用。
|
||||
|
||||
签入仓库的会话 fixture 使用规范打包行。合并此契约的在途分支通过 `pnpm run migrate:packed-session-fixtures` 运行[临时仓库迁移器](../../../scripts/migrate-packed-session-fixtures.ts);待受影响分支收敛后,由其[移除提案](../../../.agents/notes/proposed/process/2026-07-26-remove-packed-session-fixture-migrator.md)负责删除该迁移器。
|
||||
|
||||
|
||||
@@ -31,10 +31,12 @@
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@deepseek-ai/dsh-invariants": "^0.0.1",
|
||||
"@deepseek-ai/dsh-session": "^0.0.1",
|
||||
"cordis": "^4.0.0-rc.7"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@deepseek-ai/dsh-invariants": "workspace:^",
|
||||
"@deepseek-ai/dsh-session": "workspace:^",
|
||||
"cordis": "^4.0.0-rc.7"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
import { readFile, readdir, rm, writeFile } from 'node:fs/promises'
|
||||
import { existsSync } from 'node:fs'
|
||||
import { join } from 'node:path'
|
||||
import { isSurfaceEligibleType } from '@deepseek-ai/dsh-session/surface'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { type AgentUnderTest, type HarvestedLog, type InputScript, runScenario } from './harness.ts'
|
||||
import {
|
||||
@@ -512,11 +513,11 @@ export function headerChangeCount(rawLog: string): number {
|
||||
.length
|
||||
}
|
||||
|
||||
/** A literal string replacement used to carry an existing fixture value into fresh write-back. */
|
||||
/** A literal replacement from a fresh replay-run volatile to its existing fixture value. */
|
||||
export interface FixtureReplacement {
|
||||
/** The fresh run's value to replace. */
|
||||
/** The fresh replay run's volatile value. */
|
||||
from: string
|
||||
/** The existing fixture value to keep. */
|
||||
/** The existing fixture value retained during write-back. */
|
||||
to: string
|
||||
}
|
||||
|
||||
@@ -526,24 +527,51 @@ function parseJsonlRecords(text: string): Record<string, unknown>[] {
|
||||
.map(line => JSON.parse(line) as Record<string, unknown>)
|
||||
}
|
||||
|
||||
/** Narrow one parsed value to the complete identified-message shape retained by fixtures. */
|
||||
function completeMessage(value: unknown): Record<string, unknown> | undefined {
|
||||
if (
|
||||
!isRecord(value)
|
||||
|| typeof value.id !== 'string'
|
||||
|| !UUID_RE.test(value.id)
|
||||
|| typeof value.role !== 'string'
|
||||
|| !Array.isArray(value.content)
|
||||
|| !isRecord(value.source)
|
||||
) return undefined
|
||||
return value
|
||||
}
|
||||
|
||||
/** Return the complete identified message carried by one surface event. */
|
||||
function eventMessage(record: Record<string, unknown>): Record<string, unknown> | undefined {
|
||||
function surfaceEventMessage(record: Record<string, unknown>): Record<string, unknown> | undefined {
|
||||
const type = record.type
|
||||
if (typeof type !== 'string' || !isSurfaceEligibleType(type)) return undefined
|
||||
const data = record.data
|
||||
if (!isRecord(data)) return undefined
|
||||
const message = record.type === 'user/message'
|
||||
? data
|
||||
: record.type === 'assistant/message' || record.type === 'tool/result' || record.type === 'steering/message'
|
||||
? data.message
|
||||
: undefined
|
||||
if (
|
||||
!isRecord(message)
|
||||
|| typeof message.id !== 'string'
|
||||
|| !UUID_RE.test(message.id)
|
||||
|| typeof message.role !== 'string'
|
||||
|| !Array.isArray(message.content)
|
||||
|| !isRecord(message.source)
|
||||
) return undefined
|
||||
return message
|
||||
let message: unknown
|
||||
switch (type) {
|
||||
case 'user/message':
|
||||
message = data
|
||||
break
|
||||
case 'assistant/message':
|
||||
case 'tool/result':
|
||||
message = data.message
|
||||
break
|
||||
/* v8 ignore next -- the authoritative predicate must fail loud when a new surface shape lands. */
|
||||
default: throw new Error(`acp-snapshot: unsupported surface event type "${type}"`)
|
||||
}
|
||||
return completeMessage(message)
|
||||
}
|
||||
|
||||
/** Return complete message identities structurally owned by one durable record. */
|
||||
function recordMessages(record: Record<string, unknown>): Record<string, unknown>[] {
|
||||
const surfaceMessage = surfaceEventMessage(record)
|
||||
if (surfaceMessage !== undefined) return [surfaceMessage]
|
||||
if (record.type !== 'agent/inbox/spliced' || !isRecord(record.data) || !Array.isArray(record.data.inserted)) {
|
||||
return []
|
||||
}
|
||||
return record.data.inserted.flatMap((value) => {
|
||||
const message = completeMessage(value)
|
||||
return message === undefined ? [] : [message]
|
||||
})
|
||||
}
|
||||
|
||||
/** Serialize parsed JSON by value rather than insertion order. */
|
||||
@@ -555,42 +583,48 @@ function canonicalJson(value: unknown): string {
|
||||
return JSON.stringify(value)
|
||||
}
|
||||
|
||||
/** Index each unambiguous identity-free message value by its sole message id. */
|
||||
function uniqueMessageIds(logs: readonly string[]): Map<string, string | undefined> {
|
||||
const fingerprintsById = new Map<string, string | undefined>()
|
||||
/** Index identity-free message values whose ID and fingerprint are mutually unique. */
|
||||
function uniqueMessageIds(logs: readonly string[]): Map<string, string> {
|
||||
const fingerprintsById = new Map<string, Set<string>>()
|
||||
const idsByFingerprint = new Map<string, Set<string>>()
|
||||
for (const log of logs) {
|
||||
for (const record of parseJsonlRecords(log)) {
|
||||
const message = eventMessage(record)
|
||||
if (message === undefined) continue
|
||||
const { id, ...withoutId } = message
|
||||
const messageId = id as string
|
||||
const fingerprint = canonicalJson(withoutId)
|
||||
if (!fingerprintsById.has(messageId)) fingerprintsById.set(messageId, fingerprint)
|
||||
else if (fingerprintsById.get(messageId) !== fingerprint) fingerprintsById.set(messageId, undefined)
|
||||
for (const message of recordMessages(record)) {
|
||||
const { id, ...withoutId } = message
|
||||
const messageId = id as string
|
||||
const fingerprint = canonicalJson(withoutId)
|
||||
const fingerprints = fingerprintsById.get(messageId)
|
||||
if (fingerprints === undefined) fingerprintsById.set(messageId, new Set([fingerprint]))
|
||||
else fingerprints.add(fingerprint)
|
||||
const ids = idsByFingerprint.get(fingerprint)
|
||||
if (ids === undefined) idsByFingerprint.set(fingerprint, new Set([messageId]))
|
||||
else ids.add(messageId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const idsByFingerprint = new Map<string, string | undefined>()
|
||||
for (const [id, fingerprint] of fingerprintsById) {
|
||||
if (fingerprint === undefined) continue
|
||||
if (!idsByFingerprint.has(fingerprint)) idsByFingerprint.set(fingerprint, id)
|
||||
else idsByFingerprint.set(fingerprint, undefined)
|
||||
const unique = new Map<string, string>()
|
||||
for (const [id, fingerprints] of fingerprintsById) {
|
||||
if (fingerprints.size !== 1) continue
|
||||
const fingerprint = fingerprints.values().next().value as string
|
||||
if (idsByFingerprint.get(fingerprint)?.size !== 1) continue
|
||||
unique.set(fingerprint, id)
|
||||
}
|
||||
return idsByFingerprint
|
||||
return unique
|
||||
}
|
||||
|
||||
/**
|
||||
* Match unchanged complete messages across a scenario's fresh and existing logs.
|
||||
* New, changed, repeated, or otherwise ambiguous messages keep their fresh ids.
|
||||
* New, changed, duplicate-content, or otherwise ambiguous messages keep their fresh ids.
|
||||
*/
|
||||
function fixtureMessageIdReplacements(logs: readonly string[], fixtures: readonly string[]): FixtureReplacement[] {
|
||||
function fixtureMessageIdReplacements(logs: readonly string[], fixtures: readonly string[]): Map<string, string> {
|
||||
const freshIds = uniqueMessageIds(logs)
|
||||
const existingIds = uniqueMessageIds(fixtures)
|
||||
const replacements: FixtureReplacement[] = []
|
||||
const replacements = new Map<string, string>()
|
||||
for (const [fingerprint, fresh] of freshIds) {
|
||||
const existing = existingIds.get(fingerprint)
|
||||
if (fresh === undefined || existing === undefined || fresh === existing) continue
|
||||
replacements.push({ from: fresh, to: existing })
|
||||
if (existing === undefined || fresh === existing) continue
|
||||
replacements.set(fresh, existing)
|
||||
}
|
||||
return replacements
|
||||
}
|
||||
@@ -602,6 +636,22 @@ function applyFixtureReplacements(content: string, replacements: readonly Fixtur
|
||||
return stable
|
||||
}
|
||||
|
||||
/** Rewrite only validated durable-message ID fields, leaving every other occurrence untouched. */
|
||||
function applyFixtureMessageIds(content: string, replacements: ReadonlyMap<string, string>): string {
|
||||
return content.split('\n').map((line) => {
|
||||
if (line.trim().length === 0) return line
|
||||
const record = JSON.parse(line) as Record<string, unknown>
|
||||
let changed = false
|
||||
for (const message of recordMessages(record)) {
|
||||
const replacement = replacements.get(message.id as string)
|
||||
if (replacement === undefined) continue
|
||||
message.id = replacement
|
||||
changed = true
|
||||
}
|
||||
return changed ? JSON.stringify(record) : line
|
||||
}).join('\n')
|
||||
}
|
||||
|
||||
/**
|
||||
* Carry committed UUIDs into unchanged, unambiguous messages in fresh session fixtures.
|
||||
*
|
||||
@@ -611,7 +661,7 @@ function applyFixtureReplacements(content: string, replacements: readonly Fixtur
|
||||
*/
|
||||
export function stabilizeFixtureMessageIds(logs: readonly string[], fixtures: readonly string[]): string[] {
|
||||
const replacements = fixtureMessageIdReplacements(logs, fixtures)
|
||||
return logs.map(log => applyFixtureReplacements(log, replacements))
|
||||
return logs.map(log => applyFixtureMessageIds(log, replacements))
|
||||
}
|
||||
|
||||
/** One packed row's member times, or `undefined` for an ordinary record. */
|
||||
@@ -659,15 +709,15 @@ export function unknownToolCallIds(rawLog: string): string[] {
|
||||
}
|
||||
|
||||
/**
|
||||
* Build refresh write-back replacements: scenario-wide unchanged message ids,
|
||||
* plus per-log session ids, cwd values, and spill paths.
|
||||
* Build refresh write-back replacements for per-log session ids, cwd values,
|
||||
* and spill paths. Durable message ids have a later structural owner.
|
||||
*
|
||||
* @param logs The freshly harvested logs, in fixture order.
|
||||
* @param fixtures The existing fixture contents, in matching order.
|
||||
* @returns Literal replacements from fresh values to the fixture's existing values.
|
||||
*/
|
||||
export function refreshFixtureReplacements(logs: HarvestedLog[], fixtures: string[]): FixtureReplacement[] {
|
||||
const replacements = fixtureMessageIdReplacements(logs.map(log => log.content), fixtures)
|
||||
const replacements: FixtureReplacement[] = []
|
||||
for (let i = 0; i < logs.length; i++) {
|
||||
const fresh = parseJsonlRecords((logs[i] as HarvestedLog).content)[0]
|
||||
const existing = parseJsonlRecords(fixtures[i] ?? '')[0]
|
||||
@@ -818,6 +868,7 @@ function collectNormalizedStringMappings(
|
||||
existing: unknown,
|
||||
normalizedFresh: unknown,
|
||||
normalizedExisting: unknown,
|
||||
excludedStrings: ReadonlySet<string>,
|
||||
forward: Map<string, string>,
|
||||
reverse: Map<string, string>,
|
||||
): boolean {
|
||||
@@ -837,6 +888,7 @@ function collectNormalizedStringMappings(
|
||||
existing[index],
|
||||
normalizedFresh[index],
|
||||
normalizedExisting[index],
|
||||
excludedStrings,
|
||||
forward,
|
||||
reverse,
|
||||
))
|
||||
@@ -856,6 +908,7 @@ function collectNormalizedStringMappings(
|
||||
existing[key],
|
||||
normalizedFresh[key],
|
||||
normalizedExisting[key],
|
||||
excludedStrings,
|
||||
forward,
|
||||
reverse,
|
||||
))
|
||||
@@ -866,6 +919,8 @@ function collectNormalizedStringMappings(
|
||||
|| typeof normalizedFresh !== 'string'
|
||||
|| normalizedFresh !== normalizedExisting
|
||||
|| fresh === existing
|
||||
|| excludedStrings.has(fresh)
|
||||
|| excludedStrings.has(existing)
|
||||
) return true
|
||||
const freshKey = JSON.stringify([normalizedFresh, fresh])
|
||||
const existingKey = JSON.stringify([normalizedFresh, existing])
|
||||
@@ -891,6 +946,10 @@ function normalizedStringMappings(
|
||||
freshContext: NormalizeContext,
|
||||
existingContext: NormalizeContext,
|
||||
): Map<string, string> | undefined {
|
||||
const excludedStrings = new Set<string>()
|
||||
for (const record of [...freshRecords, ...existingRecords]) {
|
||||
for (const message of recordMessages(record)) excludedStrings.add(message.id as string)
|
||||
}
|
||||
const forward = new Map<string, string>()
|
||||
const reverse = new Map<string, string>()
|
||||
let existingIndex = 0
|
||||
@@ -912,6 +971,7 @@ function normalizedStringMappings(
|
||||
existingRecord,
|
||||
normalizedRefreshRecord(freshRecords[recordIndex] as Record<string, unknown>, freshContext),
|
||||
normalizedRefreshRecord(existingRecord, existingContext),
|
||||
excludedStrings,
|
||||
forward,
|
||||
reverse,
|
||||
)) return undefined
|
||||
@@ -924,11 +984,13 @@ function normalizedStringMappings(
|
||||
/**
|
||||
* Rewrite a fresh replay-produced log so repeated refreshes do not churn
|
||||
* volatile fixture fields. Meaningful event payloads come from `fresh`; the
|
||||
* existing fixture lends normalized-equivalent values, including ids, paths,
|
||||
* existing fixture lends normalized-equivalent values, including non-message ids, paths,
|
||||
* creation/event times, spill locators, and hook durations, only when the
|
||||
* complete record layout aligns and volatile strings form a consistent
|
||||
* bijection. Ambiguous layouts or mappings keep fresh strings. Packed timing
|
||||
* envelopes expand for alignment, so packing does not shift later records;
|
||||
* bijection. Complete durable-message ids are excluded because the later
|
||||
* fixture-ready structural pass owns them. Ambiguous layouts or mappings
|
||||
* keep fresh strings. Packed timing envelopes expand for alignment, so
|
||||
* packing does not shift later records;
|
||||
* fresh semantic values and fragment arrays remain authoritative.
|
||||
*
|
||||
* @param fresh The newly harvested session JSONL.
|
||||
@@ -1158,17 +1220,15 @@ export function defineAcpSnapshotSuite(options: SnapshotSuiteOptions): void {
|
||||
const refreshReplacements = REFRESHING
|
||||
? refreshFixtureReplacements(result.sessionLogs, existingFixtures)
|
||||
: []
|
||||
const outputFixtures = REFRESHING
|
||||
const freshFixtures = REFRESHING
|
||||
? result.sessionLogs.map((log, index) => scrub(portableFixture(stabilizeRefreshLog(
|
||||
log.content,
|
||||
existingFixtures[index] as string,
|
||||
refreshReplacements,
|
||||
ctx,
|
||||
))))
|
||||
: stabilizeFixtureMessageIds(
|
||||
result.sessionLogs.map(log => scrub(portableFixture(log.content))),
|
||||
existingFixtures,
|
||||
)
|
||||
: result.sessionLogs.map(log => scrub(portableFixture(log.content)))
|
||||
const outputFixtures = stabilizeFixtureMessageIds(freshFixtures, existingFixtures)
|
||||
await Promise.all(outputFixtures.map((fixture, index) =>
|
||||
writeFile(join(dir, outputFixtureFiles[index] as string), fixture)))
|
||||
if (RECORDING) {
|
||||
|
||||
@@ -7,6 +7,7 @@ import { afterAll, describe, expect, it } from 'vitest'
|
||||
import {
|
||||
defineAcpSnapshotSuite,
|
||||
stabilizeFixtureMessageIds,
|
||||
tokenizeSessionFixtureCwd,
|
||||
type HarvestedLog,
|
||||
type Scenario,
|
||||
} from '../src/index.ts'
|
||||
@@ -679,6 +680,109 @@ describe('stabilizeFixtureMessageIds', () => {
|
||||
}
|
||||
})
|
||||
|
||||
it('rewrites only complete messages carried by surface events or durable inbox splices', () => {
|
||||
const ids = {
|
||||
freshUser: '11111111-1111-4111-8111-111111111111',
|
||||
oldUser: '22222222-2222-4222-8222-222222222222',
|
||||
freshAssistant: '33333333-3333-4333-8333-333333333333',
|
||||
oldAssistant: '44444444-4444-4444-8444-444444444444',
|
||||
freshTool: '55555555-5555-4555-8555-555555555555',
|
||||
oldTool: '66666666-6666-4666-8666-666666666666',
|
||||
oldMalformed: '77777777-7777-4777-8777-777777777777',
|
||||
} as const
|
||||
const message = (id: string, role: string, text: string): Record<string, unknown> => ({
|
||||
id,
|
||||
role,
|
||||
content: [{ type: 'text', text }],
|
||||
source: { kind: role === 'user' ? 'user' : 'model' },
|
||||
})
|
||||
const log = (userId: string, assistantId: string, toolId: string, malformedId: string): string => [
|
||||
JSON.stringify({ type: 'session', id: 'same', cwd: '{{cwd}}' }),
|
||||
JSON.stringify({
|
||||
type: 'agent/inbox/spliced',
|
||||
data: {
|
||||
inserted: [
|
||||
message(userId, 'user', 'user'),
|
||||
{ ...message(userId, 'user', 'malformed inbox'), source: null },
|
||||
],
|
||||
},
|
||||
}),
|
||||
JSON.stringify({ type: 'user/message', data: message(userId, 'user', 'user') }),
|
||||
JSON.stringify({ type: 'assistant/message', data: { message: message(assistantId, 'assistant', 'assistant') } }),
|
||||
JSON.stringify({ type: 'tool/result', data: { message: message(toolId, 'tool', 'tool') } }),
|
||||
JSON.stringify({ type: 'turn/start', data: { id: userId } }),
|
||||
JSON.stringify({ type: 'steering/message', data: message(userId, 'user', 'obsolete') }),
|
||||
JSON.stringify({ type: 'user/message', data: { ...message(userId, 'user', 'malformed'), source: null } }),
|
||||
JSON.stringify({ type: 'user/message', data: message(malformedId, 'user', 'non-UUID') }),
|
||||
JSON.stringify({ type: 'assistant/message', data: null }),
|
||||
JSON.stringify({ type: 42, data: message(userId, 'user', 'non-string type') }),
|
||||
'',
|
||||
].join('\n')
|
||||
|
||||
const stable = stabilizeFixtureMessageIds(
|
||||
[log(ids.freshUser, ids.freshAssistant, ids.freshTool, 'not-a-uuid')],
|
||||
[log(ids.oldUser, ids.oldAssistant, ids.oldTool, ids.oldMalformed)],
|
||||
)[0] as string
|
||||
const records = stable.trim().split('\n').map(line => JSON.parse(line) as Record<string, unknown>)
|
||||
|
||||
const inserted = ((records[1]?.data as { inserted: Array<{ id: string }> }).inserted)
|
||||
expect(inserted[0]?.id).toBe(ids.oldUser)
|
||||
expect(inserted[1]?.id).toBe(ids.freshUser)
|
||||
expect((records[2]?.data as { id: string }).id).toBe(ids.oldUser)
|
||||
expect((records[3]?.data as { message: { id: string } }).message.id).toBe(ids.oldAssistant)
|
||||
expect((records[4]?.data as { message: { id: string } }).message.id).toBe(ids.oldTool)
|
||||
expect((records[5]?.data as { id: string }).id).toBe(ids.freshUser)
|
||||
expect((records[6]?.data as { id: string }).id).toBe(ids.freshUser)
|
||||
expect((records[7]?.data as { id: string }).id).toBe(ids.freshUser)
|
||||
expect((records[8]?.data as { id: string }).id).toBe('not-a-uuid')
|
||||
})
|
||||
|
||||
it('matches cwd-bearing messages only after the fresh log reaches fixture-ready form', () => {
|
||||
const freshId = '11111111-1111-4111-8111-111111111111'
|
||||
const existingId = '22222222-2222-4222-8222-222222222222'
|
||||
const freshCwd = '/tmp/acp-snapshot-fresh-cwd'
|
||||
const message = (id: string, path: string): Record<string, unknown> => ({
|
||||
type: 'user/message',
|
||||
data: {
|
||||
id,
|
||||
role: 'user',
|
||||
content: [{ type: 'text', text: `read ${path}/input.txt` }],
|
||||
source: { kind: 'user' },
|
||||
},
|
||||
})
|
||||
const fresh = tokenizeSessionFixtureCwd([
|
||||
JSON.stringify({ type: 'session', id: 'fresh', cwd: freshCwd }),
|
||||
JSON.stringify(message(freshId, freshCwd)),
|
||||
'',
|
||||
].join('\n'))
|
||||
const existing = [
|
||||
JSON.stringify({ type: 'session', id: 'old', cwd: '{{cwd}}' }),
|
||||
JSON.stringify(message(existingId, '{{cwd}}')),
|
||||
'',
|
||||
].join('\n')
|
||||
|
||||
expect(stabilizeFixtureMessageIds([fresh], [existing])[0]).toContain(`"id":"${existingId}"`)
|
||||
})
|
||||
|
||||
it('rejects a fingerprint connected to an id that also identifies different content', () => {
|
||||
const freshId = '11111111-1111-4111-8111-111111111111'
|
||||
const conflictingId = '22222222-2222-4222-8222-222222222222'
|
||||
const competingId = '33333333-3333-4333-8333-333333333333'
|
||||
const message = (id: string, text: string): string => JSON.stringify({
|
||||
type: 'user/message',
|
||||
data: { id, role: 'user', content: [{ type: 'text', text }], source: { kind: 'user' } },
|
||||
})
|
||||
const fresh = `${message(freshId, 'shared')}\n`
|
||||
const existing = [
|
||||
message(conflictingId, 'shared'),
|
||||
message(conflictingId, 'different'),
|
||||
message(competingId, 'shared'),
|
||||
'',
|
||||
].join('\n')
|
||||
|
||||
expect(stabilizeFixtureMessageIds([fresh], [existing])).toEqual([fresh])
|
||||
})
|
||||
|
||||
it('leaves fresh fixtures unchanged when no committed counterpart exists', () => {
|
||||
const fresh = '{"type":"session","id":"new"}\n'
|
||||
expect(stabilizeFixtureMessageIds([fresh], [''])).toEqual([fresh])
|
||||
@@ -726,76 +830,28 @@ describe('refreshFixtureReplacements', () => {
|
||||
])
|
||||
})
|
||||
|
||||
it('maps one inherited message id across parent and child logs', () => {
|
||||
it('leaves complete message ids out of the literal refresh replacement list', () => {
|
||||
const freshMessageId = '11111111-1111-4111-8111-111111111111'
|
||||
const existingMessageId = '22222222-2222-4222-8222-222222222222'
|
||||
const content = [{ type: 'text', text: 'inherited' }]
|
||||
const log = (sessionId: string, messageId: string): string => [
|
||||
JSON.stringify({ type: 'session', id: sessionId, cwd: '/same' }),
|
||||
JSON.stringify({
|
||||
type: 'user/message',
|
||||
data: { role: 'user', content, source: { kind: 'user' }, id: messageId },
|
||||
data: {
|
||||
id: messageId,
|
||||
role: 'user',
|
||||
content: [{ type: 'text', text: 'same' }],
|
||||
source: { kind: 'user' },
|
||||
},
|
||||
}),
|
||||
'',
|
||||
].join('\n')
|
||||
const harvested = (content: string): HarvestedLog => ({ id: 'diagnostic', createdAt: 1, content })
|
||||
|
||||
const replacements = refreshFixtureReplacements(
|
||||
[harvested(log('fresh-parent', freshMessageId)), harvested(log('fresh-child', freshMessageId))],
|
||||
[log('old-parent', existingMessageId), log('old-child', existingMessageId)],
|
||||
[{ id: 'diagnostic', createdAt: 1, content: log('fresh', freshMessageId) }],
|
||||
[log('old', existingMessageId)],
|
||||
)
|
||||
|
||||
expect(replacements.filter(replacement => replacement.from === freshMessageId)).toEqual([
|
||||
{ from: freshMessageId, to: existingMessageId },
|
||||
])
|
||||
})
|
||||
|
||||
it('keeps fresh ids for new, changed, and ambiguous messages', () => {
|
||||
const ids = {
|
||||
new: '11111111-1111-4111-8111-111111111111',
|
||||
changed: '22222222-2222-4222-8222-222222222222',
|
||||
ambiguousA: '33333333-3333-4333-8333-333333333333',
|
||||
ambiguousB: '44444444-4444-4444-8444-444444444444',
|
||||
oldChanged: '55555555-5555-4555-8555-555555555555',
|
||||
oldAmbiguous: '66666666-6666-4666-8666-666666666666',
|
||||
stable: '77777777-7777-4777-8777-777777777777',
|
||||
} as const
|
||||
const message = (id: string, text: string): Record<string, unknown> => ({
|
||||
type: 'user/message',
|
||||
data: { role: 'user', content: [{ type: 'text', text }], source: { kind: 'user' }, id },
|
||||
})
|
||||
const log = (messages: Record<string, unknown>[]): string => [
|
||||
JSON.stringify({ type: 'session', id: 'same', cwd: '/same' }),
|
||||
...messages.map(record => JSON.stringify(record)),
|
||||
'',
|
||||
].join('\n')
|
||||
const fresh = log([
|
||||
message(ids.new, 'new'),
|
||||
message(ids.changed, 'changed'),
|
||||
message(ids.changed, 'changed again'),
|
||||
message(ids.ambiguousA, 'duplicate'),
|
||||
message(ids.ambiguousB, 'duplicate'),
|
||||
message(ids.stable, 'stable'),
|
||||
])
|
||||
const existing = log([
|
||||
message(ids.oldChanged, 'before'),
|
||||
message(ids.oldAmbiguous, 'duplicate'),
|
||||
message(ids.stable, 'stable'),
|
||||
])
|
||||
|
||||
const replacements = refreshFixtureReplacements(
|
||||
[{ id: 'diagnostic', createdAt: 1, content: fresh }],
|
||||
[existing],
|
||||
)
|
||||
|
||||
const replacedIds = replacements.map(replacement => replacement.from)
|
||||
for (const id of [
|
||||
ids.new,
|
||||
ids.changed,
|
||||
ids.ambiguousA,
|
||||
ids.ambiguousB,
|
||||
ids.stable,
|
||||
]) expect(replacedIds).not.toContain(id)
|
||||
expect(replacements).toEqual([{ from: 'fresh', to: 'old' }])
|
||||
})
|
||||
})
|
||||
|
||||
@@ -936,13 +992,38 @@ describe('stabilizeRefreshLog', () => {
|
||||
[{ id: 'diagnostic', createdAt: 1, content: fresh }],
|
||||
[existing],
|
||||
)
|
||||
const output = stabilize(fresh, existing, replacements).trim().split('\n')
|
||||
const refreshed = stabilize(fresh, existing, replacements)
|
||||
const intermediate = refreshed.trim().split('\n')
|
||||
.map(line => JSON.parse(line) as Record<string, unknown>)
|
||||
expect((intermediate[1]?.data as { id: string }).id).toBe(freshUserId)
|
||||
expect(((intermediate[3]?.data as { message: { id: string } }).message).id).toBe(freshAssistantId)
|
||||
|
||||
const output = (stabilizeFixtureMessageIds([refreshed], [existing])[0] as string).trim().split('\n')
|
||||
.map(line => JSON.parse(line) as Record<string, unknown>)
|
||||
|
||||
expect((output[1]?.data as { id: string }).id).toBe(existingUserId)
|
||||
expect(((output[3]?.data as { message: { id: string } }).message).id).toBe(existingAssistantId)
|
||||
})
|
||||
|
||||
it('leaves an aligned complete message id to the fixture-ready structural pass', () => {
|
||||
const freshId = '11111111-1111-4111-8111-111111111111'
|
||||
const existingId = '22222222-2222-4222-8222-222222222222'
|
||||
const log = (id: string): string => [
|
||||
JSON.stringify({ type: 'session', id: 'same', createdAt: 1, cwd: '/same' }),
|
||||
JSON.stringify({
|
||||
type: 'user/message',
|
||||
data: { id, role: 'user', content: [{ type: 'text', text: 'same' }], source: { kind: 'user' } },
|
||||
}),
|
||||
'',
|
||||
].join('\n')
|
||||
const fresh = log(freshId)
|
||||
const existing = log(existingId)
|
||||
const refreshed = stabilize(fresh, existing)
|
||||
|
||||
expect(refreshed).toContain(`"id":"${freshId}"`)
|
||||
expect(stabilizeFixtureMessageIds([refreshed], [existing])[0]).toContain(`"id":"${existingId}"`)
|
||||
})
|
||||
|
||||
it('keeps volatile fixture fields while preserving fresh meaningful payloads', () => {
|
||||
const fresh = [
|
||||
'{"type":"session","id":"new-child","createdAt":200,"cwd":"/new","parentSession":"new-parent","seedLength":1}',
|
||||
|
||||
@@ -13,6 +13,9 @@
|
||||
},
|
||||
{
|
||||
"path": "../../support/invariants"
|
||||
},
|
||||
{
|
||||
"path": "../../core/session"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user