Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175/** * SessionEntry — one row of the append-only session JSONL (ARCHITECTURE §10). * * Every entry has a stable `id`, `parentId` (for branching — same JSONL is the lineage * tree), `timestamp` (ms epoch), and a `type`-discriminated `details` payload. * * Lineage = the same JSONL (ARCH §12): compaction partitions it into addressable spans * (`compaction:0`, …), nothing is ever deleted, `recall` walks the full file. * * Entry types: * - message user/assistant turn with content blocks * - tool_result paired to its call by tool_call_id; carries isError + exit codes * - compaction summary + messageRange + token accounting + compactor name + index * - checkpoint optional: mode transitions / metadata snapshots */import { Type, type Static } from '@sinclair/typebox';import { MessageSchema } from '@/types/message';import { ToolResultContentSchema } from '@/types/tool-result';import { RenderDescriptorSchema } from '@/contracts/render-descriptor';import { PlanArtifactSchema } from '@/contracts/plan-artifact';import { SkillActivationSchema } from '@/contracts/skill';
export { LegacyCheckpointDetailsSchema, PlanCheckpointDetailsSchema, SkillActivationCheckpointDetailsSchema, ForkCheckpointDetailsSchema, CheckpointDetailsSchema };
/** Shared envelope fields for every entry. */const EntryBase = { id: Type.String(), parentId: Type.Optional(Type.String()), timestamp: Type.Integer({ description: 'ms since epoch' }),};
export const MessageEntrySchema = Type.Object({ ...EntryBase, type: Type.Literal('message'), details: Type.Object({ message: MessageSchema, }),});
export const ToolResultEntrySchema = Type.Object({ ...EntryBase, type: Type.Literal('tool_result'), details: Type.Object({ tool_call_id: Type.String(), content: ToolResultContentSchema, is_error: Type.Boolean(), // Structured result INFO (saga 26haas). `exit_code` existed pre-26haas but was never // populated; now `appendToolResult` forwards it from the ToolResult (the compactor's // `normalizeToolResult` already reads it as the `bash` block's `exitCode`). `duration_ms` // is new — it was previously trapped inside the `command` render descriptor, so the // client couldn't render it without the server's pre-render. Both additive/optional. exit_code: Type.Optional(Type.Integer()), duration_ms: Type.Optional(Type.Integer()), // Phase B (saga sfu22d.4): the tool's `onResult` render descriptor, persisted so a // resumed `session.replay` reconstructs the rich tool body (code/command/path_list) // identically to live. Additive/optional — pre-Phase-B sessions simply omit it and // the TUI falls back to the plain `↳ text` footer. Never carries mutatedFiles/ // checkpointIntent (those are loop-local/wire-only — `ToolResultEventSchema` is an // explicit field-pick of content/is_error/render only). render: Type.Optional(RenderDescriptorSchema), }),});
export const CompactionEntrySchema = Type.Object({ ...EntryBase, type: Type.Literal('compaction'), details: Type.Object({ summary: Type.String(), messageRange: Type.Tuple([Type.String(), Type.String()]), tokensBefore: Type.Integer(), tokensAfter: Type.Integer(), compactor: Type.String(), index: Type.Integer(), }),});
/** A pre-d4ks8c.4 checkpoint: only an optional human `reason` + loose `metadata` bag. * No artifactKind (the discriminator is `undefined`/absent here). `additionalProperties:false` * means a row carrying an `artifactKind` or any artifact payload CANNOT satisfy this arm — it * must match the matching kind arm below. */const LegacyCheckpointDetailsSchema = Type.Object( { artifactKind: Type.Optional(Type.Undefined()), reason: Type.Optional(Type.String()), metadata: Type.Optional(Type.Record(Type.String(), Type.Unknown())), toolResultEntryId: Type.Optional(Type.String()), }, { additionalProperties: false },);
/** Plan artifact arm: `artifactKind === 'plan'` + a REQUIRED `plan` payload. No * `skillActivation` may be present (`additionalProperties:false`) → a skill payload under * a plan kind, or both payloads, is rejected. Backward-compatible: existing plan rows still * validate (they carry `artifactKind:'plan'` + `plan` + optional `reason`/`metadata`). */const PlanCheckpointDetailsSchema = Type.Object( { artifactKind: Type.Literal('plan'), plan: PlanArtifactSchema, reason: Type.Optional(Type.String()), metadata: Type.Optional(Type.Record(Type.String(), Type.Unknown())), toolResultEntryId: Type.Optional(Type.String()), }, { additionalProperties: false },);
/** Skill-activation arm: `artifactKind === 'skill-activation'` + a REQUIRED `skillActivation` * payload. No `plan` may be present → a plan payload under a skill kind, or both payloads, * is rejected; a skill kind with no `skillActivation` is rejected (the field is required). */const SkillActivationCheckpointDetailsSchema = Type.Object( { artifactKind: Type.Literal('skill-activation'), skillActivation: SkillActivationSchema, toolResultEntryId: Type.Optional(Type.String()), reason: Type.Optional(Type.String()), metadata: Type.Optional(Type.Record(Type.String(), Type.Unknown())), }, { additionalProperties: false },);
/** Fork arm (saga 3aliod): `artifactKind === 'fork'` + REQUIRED fork provenance. A fork * marker is the FIRST row of a forked session's JSONL: it records where the session was * split from (source session id + source entry id) and carries the fork's own id. The * marker roots the new chain (no parentId — it is a new file's root); the materialized * live-window copies chain off it. The source session is never mutated. No `plan`/ * `skillActivation` may be present (`additionalProperties:false`). */const ForkCheckpointDetailsSchema = Type.Object( { artifactKind: Type.Literal('fork'), fork: Type.Object( { /** The fork's id (unique per fork operation). */ forkId: Type.String({ minLength: 1 }), /** Session id of the conversation this fork was split from. */ sourceSessionId: Type.String({ minLength: 1 }), /** Entry id in the SOURCE session the fork was split at. */ sourceEntryId: Type.String({ minLength: 1 }), }, { additionalProperties: false }, ), toolResultEntryId: Type.Optional(Type.String()), reason: Type.Optional(Type.String()), metadata: Type.Optional(Type.Record(Type.String(), Type.Unknown())), }, { additionalProperties: false },);
/** The discriminated `details` envelope: ONE of legacy / plan / skill-activation / fork. * Because each arm is `additionalProperties:false` and pins the discriminator, mismatched * kind/payload combos are rejected at the schema level (saga d4ks8c.9 hardening). */const CheckpointDetailsSchema = Type.Union([ LegacyCheckpointDetailsSchema, PlanCheckpointDetailsSchema, SkillActivationCheckpointDetailsSchema, ForkCheckpointDetailsSchema,]);
export const CheckpointEntrySchema = Type.Object({ ...EntryBase, type: Type.Literal('checkpoint'), details: CheckpointDetailsSchema,});
export const SessionEntrySchema = Type.Union([ MessageEntrySchema, ToolResultEntrySchema, CompactionEntrySchema, CheckpointEntrySchema,]);
export type MessageEntry = Static<typeof MessageEntrySchema>;export type ToolResultEntry = Static<typeof ToolResultEntrySchema>;export type CompactionEntry = Static<typeof CompactionEntrySchema>;export type CheckpointEntry = Static<typeof CheckpointEntrySchema>;export type SessionEntry = Static<typeof SessionEntrySchema>;