实现状态:已落地。
0014_message_part_sequence/0015_message_part_sequence_backfill_and_guard已提供稳定顺序,contracts 已声明MessageSemantics/TimelinePart,V4 hydration 已消费 timeline part。 下文 Phase 1–5 保留为实施轨迹,不再表示待实现工作。
聊天 transcript 的核心模型仍然是:
session -> message -> part
这条主线不能再被 session_entry、renderer store、text metadata、单独 timeline 表
切开。timeline 是聊天内容流里的一个结构化片段,因此应该是 part 的一种:
message.parts[] includes { type: "timeline", ... }
但 timeline 不能是任意 JSON。它必须由 contracts 和 protocol schema 声明,
运行时写入、snapshot 恢复、UI 渲染都只消费这个强结构。
| 原则 | 说明 |
|---|---|
| timeline 是 part | compact、fork、goal verification、model change 都写成 part.type="timeline"。 |
| JSON 不是兜底袋子 | timeline 字段仍在 part.data JSON 里,但由 TypeScript 类型和 Zod protocol schema 约束。 |
| role 和产品语义分离 | message.role 只表示 provider role;真实用户、model-only reminder、command、timeline 由显式字段声明。 |
| 顺序显式 | message 和 part 的渲染顺序不能依赖 id 字典序。 |
| 老数据可读 | 新版本必须能从 legacy compaction part、fork text metadata、goal session_entry、renderer-only model_change 语义中恢复。 |
现有 message / part 表保留,核心数据仍存 data JSON。
本次只对排序和查询稳定性做必要加法。
alter table message add column sequence integer;
create index if not exists message_session_sequence_idx
on message(session_id, sequence, time_created, id);
语义:
sequence 是 session 内 message 的稳定渲染顺序。max(sequence) + 1 分配。time_created, rowid。sequence is null, sequence, time_created, rowid。alter table part add column sequence integer;
create index if not exists part_message_sequence_idx
on part(message_id, sequence, time_created, id);
create index if not exists part_session_sequence_idx
on part(session_id, message_id, sequence);
语义:
sequence 是 message 内 part 的稳定顺序。max(sequence) + 1 分配。message_id, time_created, rowid/id。sequence is null, sequence, time_created, id。UserMessageInfo 和 AssistantMessageInfo 增加可选 semantics 字段。该字段存入
message.data,不是额外表;因为它描述的是 message 自身,不需要跨 message 查询。
interface MessageSemantics {
origin: "real_user" | "agent_runtime" | "system" | "migration";
kind:
| "user_prompt"
| "slash_command"
| "system_reminder"
| "background_notification"
| "subagent_notification"
| "todo_reminder"
| "rewind_notice"
| "fork_notice"
| "timeline_event"
| "compact_summary"
| "assistant_response";
source?: string;
commandName?: string;
uiVisibility: "visible" | "hidden" | "debug";
providerVisibility: "visible" | "hidden";
transcriptVisibility: "visible" | "hidden";
}
兼容期内继续保留旧字段:
syntheticsourcevisibilitysource / visibility / runtimeMessage新写入必须同时写 semantics;旧字段只用于 legacy fallback 和降级兼容。
核心映射:
| 数据 | origin | kind | uiVisibility | providerVisibility |
|---|---|---|---|---|
| 用户普通输入 | real_user | user_prompt | visible | visible |
/goal xxx 可见命令 |
real_user | slash_command | visible | visible |
/compact 操作 |
real_user | slash_command | hidden | hidden |
| goal continuation | agent_runtime | system_reminder | hidden | visible |
| background task notification | agent_runtime | background_notification | hidden | visible |
| subagent notification | agent_runtime | subagent_notification | hidden | visible |
| todo reminder | agent_runtime | todo_reminder | hidden | visible |
| timeline carrier | system | timeline_event | visible | hidden |
| compact summary context | agent_runtime | compact_summary | hidden | visible |
| assistant 正文 | agent_runtime | assistant_response | visible | visible |
新增 MessagePart 类型:
type TimelinePart =
| ContextCompactionTimelinePart
| GoalVerificationTimelinePart
| SessionForkTimelinePart
| ModelChangeTimelinePart;
interface TimelinePartBase {
id: PartId;
sessionID: SessionId;
messageID: MessageId;
type: "timeline";
timelineType:
| "context_compaction"
| "goal_verification"
| "session_fork"
| "model_change";
display: "separator" | "worklog";
status?: string;
anchorMessageId?: MessageId;
anchorTurnId?: TurnId;
time?: {
start?: number;
end?: number;
};
}
interface ContextCompactionTimelinePart extends TimelinePartBase {
timelineType: "context_compaction";
operationId: string;
trigger: "manual" | "auto" | "reactive" | "partial" | "session_memory";
phase?: "standalone_turn" | "pre_request" | "mid_turn" | "reactive";
compactReason?: string;
boundaryId?: string;
summaryMessageId?: MessageId;
preCompactTokenCount?: number;
postCompactTokenCount?: number;
truePostCompactTokenCount?: number;
attempt?: number;
maxAttempts?: number;
reason?: string;
}
interface GoalVerificationTimelinePart extends TimelinePartBase {
timelineType: "goal_verification";
targetId: string;
verificationId: string;
goalIteration?: number;
verification?: {
passed: boolean;
reason: string;
nextAction?: string | null;
};
}
interface SessionForkTimelinePart extends TimelinePartBase {
timelineType: "session_fork";
parentSessionId: SessionId;
targetMessageId: MessageId;
targetCheckpointId?: string;
restoredFileCount?: number;
}
interface ModelChangeTimelinePart extends TimelinePartBase {
timelineType: "model_change";
fromModel?: {
providerId: string;
modelId: string;
variant?: string;
label?: string;
};
toModel: {
providerId: string;
modelId: string;
variant?: string;
label: string;
};
}
所有 timeline 都必须挂在一个 message 上。
| 场景 | message | part |
|---|---|---|
| manual compact | synthetic assistant carrier | timeline/context_compaction |
| auto compact | 当前或 synthetic assistant carrier,按现有 worklog 语义 | timeline/context_compaction |
| fork notice | fork child 中 synthetic user 或 assistant carrier,兼容期保留旧 fork notice | timeline/session_fork |
| goal verifier | synthetic assistant carrier,锚定被验证的 assistant | timeline/goal_verification |
| model change | 下一次 accepted user input 之前插入 synthetic assistant carrier | timeline/model_change |
更新规则:
part.id 是身份,part.sequence 是位置,更新时不能改变 sequence。operationIdtargetId + goalIteration,没有 iteration 时 verificationIdparentSessionId + targetMessageId + targetCheckpointIdtask/session + createdAt + requestId新版本读 session 时做 legacy canonicalization:
synthetic/source/visibility/part.metadata/runtimeMessage。timeline/context_compaction。timeline/session_fork。session_entry 继续识别,并投影为 timeline/goal_verification。legacy fallback 只在读取侧存在;新写入必须写 timeline part。
如果要求用户升级后再用旧版本打开也不崩,需要一个兼容窗口:
compaction part。session_entry。part.type="timeline" 时可能无法完整展示新 timeline;这是降级显示损失,不应影响新版本读取。如果必须保证旧版本完全不遇到 unknown part type,则需要先发布一个“旧版本容忍 unknown part”
的过渡版本,再开始写 timeline part。否则只能保证新版本向下读取旧数据,不能保证无限制二进制降级。
第一阶段不改 UI 渲染行为。目标是让 agent/CLI 写出来的数据本身已经完整、 有序、可恢复,UI 后续只需要消费稳定 transcript。
本阶段包含:
timeline part 与 message semantics schema。本阶段不包含:
packages/ui 的 timeline projection。session_entry。完成第一阶段后,UI 仍可继续按旧路径渲染;但新 snapshot 已经携带规范的
part.type="timeline",为第二阶段 UI 切换消费源做好准备。
新 snapshot 构造:
message 按 sequence 读取。part 按 message_id, sequence 读取。mapMessagePart 识别 type="timeline" 并直接映射到 protocol。legacy compaction/session_entry/text metadata fallback 先保留,直到 UI 完成切换并验证。
| 层 | 文件 |
|---|---|
| migration | apps/zcode-cli/packages/adapters/src/storage/session-store/migrations.ts |
| rows/repositories | apps/zcode-cli/packages/adapters/src/storage/session-store/rows.ts、repositories/messages.ts |
| contracts | apps/zcode-cli/packages/contracts/src/interfaces/session-store.port.ts |
| runtime writes | apps/zcode-cli/packages/core/src/runtime/methods/message-persistence.ts、compact-persistence.ts、session-fork.ts、events.ts、model set path |
| protocol | packages/shared/src/zcode-protocol/index.ts、apps/zcode-cli/packages/bootstrap/src/zcode-protocol/message-mapper.ts |
| shared read compatibility | packages/shared/src/zcode-session-visible-content.ts、packages/shared/src/zcodePersistedMessageMerge.ts |
| V4 transcript hydration | apps/zcode-cli/packages/bootstrap/src/zcode-protocol-v4/transcript-hydration.ts、product-projection.ts |
| UI projection | packages/ui/src/v4/conversationProjectionStore.ts、packages/ui/src/v4/sessionDataLayer.ts |
message.sequence 和 part.sequence migration。MessageSemantics 和 TimelinePart 类型。part.type="timeline" schema。timeline/context_compaction,兼容期继续写旧 compaction part。timeline/session_fork,兼容期继续写旧 fork notice metadata。timeline/goal_verification,兼容期继续写 session_entry。timeline/model_change;如果用户只是切换后不发送,不污染 transcript。message.semantics,兼容期继续保留旧
synthetic/source/visibility 字段。session_entry。必须覆盖:
rowid 顺序。part.type="timeline"。display,避免 UI 自行推断。