refactor(schema): rename V2 session events and normalize payloads (#35217)

This commit is contained in:
Kit Langton 2026-07-03 14:25:59 -04:00 committed by GitHub
commit 394e0b9045
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
63 changed files with 1888 additions and 1989 deletions

View file

@ -4,7 +4,7 @@ import { Schema } from "effect"
import { optional } from "./schema.js"
import { ascending } from "./identifier.js"
import { Location } from "./location.js"
import { statics } from "./schema.js"
import { DateTimeUtcFromMillis, statics } from "./schema.js"
export const ID = Schema.String.check(Schema.isStartsWith("evt_")).pipe(
Schema.brand("Event.ID"),
@ -60,6 +60,7 @@ export type Data<D extends Definition> = Schema.Schema.Type<D["data"]>
type PayloadBase<D extends Definition> = {
readonly id: ID
readonly type: D["type"]
readonly created: typeof DateTimeUtcFromMillis.Type
readonly data: Data<D>
readonly location?: Location.Ref
readonly metadata?: Record<string, unknown>
@ -85,6 +86,7 @@ export function durable<
const data = Schema.Struct(input.schema)
return Schema.Struct({
id: ID,
created: DateTimeUtcFromMillis,
metadata: optional(Schema.Record(Schema.String, Schema.Unknown)),
type: Schema.Literal(input.type),
durable: DurableEnvelope,
@ -109,6 +111,7 @@ export function ephemeral<
const data = Schema.Struct(input.schema)
return Schema.Struct({
id: ID,
created: DateTimeUtcFromMillis,
metadata: optional(Schema.Record(Schema.String, Schema.Unknown)),
type: Schema.Literal(input.type),
location: optional(Location.Ref),

View file

@ -6,7 +6,7 @@ import { Event } from "./event.js"
import { ProviderMetadata, ToolContent } from "./llm.js"
import { Delivery } from "./session-delivery.js"
import { Model } from "./model.js"
import { DateTimeUtcFromMillis, NonNegativeInt, RelativePath } from "./schema.js"
import { NonNegativeInt, RelativePath } from "./schema.js"
import { FileAttachment, Prompt } from "./prompt.js"
import { SessionID } from "./session-id.js"
import { Location } from "./location.js"
@ -20,17 +20,16 @@ export const Source = Schema.Struct({
end: NonNegativeInt,
text: Schema.String,
}).annotate({
identifier: "session.next.event.source",
identifier: "session.event.source",
})
export interface Source extends Schema.Schema.Type<typeof Source> {}
const Base = {
timestamp: DateTimeUtcFromMillis,
sessionID: SessionID,
}
const PromptFields = {
...Base,
messageID: SessionMessage.ID,
inputID: SessionMessage.ID,
prompt: Prompt,
delivery: Delivery,
}
@ -44,48 +43,46 @@ const options = {
const stepSettlementOptions = {
durable: {
aggregate: "sessionID",
version: 2,
version: 1,
},
} as const
export const UnknownError = SessionMessage.UnknownError
export type UnknownError = SessionMessage.UnknownError
export const AgentSwitched = Event.durable({
type: "session.next.agent.switched",
export const AgentSelected = Event.durable({
type: "agent.selected",
...options,
schema: {
...Base,
messageID: SessionMessage.ID,
agent: Schema.String,
},
})
export type AgentSwitched = typeof AgentSwitched.Type
export type AgentSelected = typeof AgentSelected.Type
export const ModelSwitched = Event.durable({
type: "session.next.model.switched",
export const ModelSelected = Event.durable({
type: "model.selected",
...options,
schema: {
...Base,
messageID: SessionMessage.ID,
model: Model.Ref,
},
})
export type ModelSwitched = typeof ModelSwitched.Type
export type ModelSelected = typeof ModelSelected.Type
export const Moved = Event.durable({
type: "session.next.moved",
type: "session.moved",
...options,
schema: {
...Base,
location: Location.Ref,
subdirectory: RelativePath.pipe(optional),
subpath: RelativePath.pipe(optional),
},
})
export type Moved = typeof Moved.Type
export const Renamed = Event.durable({
type: "session.next.renamed",
type: "renamed",
...options,
schema: {
...Base,
@ -95,32 +92,35 @@ export const Renamed = Event.durable({
export type Renamed = typeof Renamed.Type
export const Forked = Event.durable({
type: "session.next.forked",
type: "forked",
...options,
schema: {
...Base,
parentID: SessionID,
messageID: SessionMessage.ID.pipe(optional),
from: SessionMessage.ID.pipe(optional),
},
})
export type Forked = typeof Forked.Type
export const Prompted = Event.durable({
type: "session.next.prompted",
export const PromptPromoted = Event.durable({
type: "prompt.promoted",
...options,
schema: PromptFields,
schema: {
sessionID: SessionID,
inputID: SessionMessage.ID,
},
})
export type Prompted = typeof Prompted.Type
export type PromptPromoted = typeof PromptPromoted.Type
export const PromptAdmitted = Event.durable({
type: "session.next.prompt.admitted",
type: "prompt.admitted",
...options,
schema: PromptFields,
})
export type PromptAdmitted = typeof PromptAdmitted.Type
export const ExecutionSettled = Event.ephemeral({
type: "session.next.execution.settled",
type: "execution.settled",
schema: {
...Base,
outcome: Schema.Literals(["success", "failure", "interrupted"]),
@ -130,22 +130,20 @@ export const ExecutionSettled = Event.ephemeral({
export type ExecutionSettled = typeof ExecutionSettled.Type
export const ContextUpdated = Event.durable({
type: "session.next.context.updated",
type: "session.context.updated",
...options,
schema: {
...Base,
messageID: SessionMessage.ID,
text: Schema.String,
},
})
export type ContextUpdated = typeof ContextUpdated.Type
export const Synthetic = Event.durable({
type: "session.next.synthetic",
type: "synthetic",
...options,
schema: {
...Base,
messageID: SessionMessage.ID,
text: Schema.String,
description: Schema.String.pipe(optional),
metadata: Schema.Record(Schema.String, Schema.Unknown).pipe(optional),
@ -155,11 +153,10 @@ export type Synthetic = typeof Synthetic.Type
export namespace Skill {
export const Activated = Event.durable({
type: "session.next.skill.activated",
type: "skill.activated",
...options,
schema: {
...Base,
messageID: SessionMessage.ID,
name: Schema.String,
text: Schema.String,
},
@ -169,11 +166,10 @@ export namespace Skill {
export namespace Shell {
export const Started = Event.durable({
type: "session.next.shell.started",
type: "shell.started",
...options,
schema: {
...Base,
messageID: SessionMessage.ID,
callID: Schema.String,
command: Schema.String,
},
@ -181,7 +177,7 @@ export namespace Shell {
export type Started = typeof Started.Type
export const Ended = Event.durable({
type: "session.next.shell.ended",
type: "shell.ended",
...options,
schema: {
...Base,
@ -194,7 +190,7 @@ export namespace Shell {
export namespace Step {
export const Started = Event.durable({
type: "session.next.step.started",
type: "step.started",
...options,
schema: {
...Base,
@ -207,7 +203,7 @@ export namespace Step {
export type Started = typeof Started.Type
export const Ended = Event.durable({
type: "session.next.step.ended",
type: "step.ended",
...stepSettlementOptions,
schema: {
...Base,
@ -230,7 +226,7 @@ export namespace Step {
export type Ended = typeof Ended.Type
export const Failed = Event.durable({
type: "session.next.step.failed",
type: "step.failed",
...stepSettlementOptions,
schema: {
...Base,
@ -243,7 +239,7 @@ export namespace Step {
export namespace Text {
export const Started = Event.durable({
type: "session.next.text.started",
type: "text.started",
...options,
schema: {
...Base,
@ -255,7 +251,7 @@ export namespace Text {
// Stream fragments are live-only; Text.Ended is the replayable full-value boundary.
export const Delta = Event.ephemeral({
type: "session.next.text.delta",
type: "text.delta",
schema: {
...Base,
assistantMessageID: SessionMessage.ID,
@ -266,7 +262,7 @@ export namespace Text {
export type Delta = typeof Delta.Type
export const Ended = Event.durable({
type: "session.next.text.ended",
type: "text.ended",
...options,
schema: {
...Base,
@ -280,7 +276,7 @@ export namespace Text {
export namespace Reasoning {
export const Started = Event.durable({
type: "session.next.reasoning.started",
type: "reasoning.started",
...options,
schema: {
...Base,
@ -293,7 +289,7 @@ export namespace Reasoning {
// Stream fragments are live-only; Reasoning.Ended is the replayable full-value boundary.
export const Delta = Event.ephemeral({
type: "session.next.reasoning.delta",
type: "reasoning.delta",
schema: {
...Base,
assistantMessageID: SessionMessage.ID,
@ -304,7 +300,7 @@ export namespace Reasoning {
export type Delta = typeof Delta.Type
export const Ended = Event.durable({
type: "session.next.reasoning.ended",
type: "reasoning.ended",
...options,
schema: {
...Base,
@ -326,7 +322,7 @@ export namespace Tool {
export namespace Input {
export const Started = Event.durable({
type: "session.next.tool.input.started",
type: "tool.input.started",
...options,
schema: {
...ToolBase,
@ -337,7 +333,7 @@ export namespace Tool {
// Stream fragments are live-only; Input.Ended is the replayable raw-input boundary.
export const Delta = Event.ephemeral({
type: "session.next.tool.input.delta",
type: "tool.input.delta",
schema: {
...ToolBase,
delta: Schema.String,
@ -346,7 +342,7 @@ export namespace Tool {
export type Delta = typeof Delta.Type
export const Ended = Event.durable({
type: "session.next.tool.input.ended",
type: "tool.input.ended",
...options,
schema: {
...ToolBase,
@ -357,7 +353,7 @@ export namespace Tool {
}
export const Called = Event.durable({
type: "session.next.tool.called",
type: "tool.called",
...options,
schema: {
...ToolBase,
@ -376,7 +372,7 @@ export namespace Tool {
* transitions or at a bounded cadence, not persist every stdout/stderr chunk.
*/
export const Progress = Event.durable({
type: "session.next.tool.progress",
type: "tool.progress",
...options,
schema: {
...ToolBase,
@ -387,7 +383,7 @@ export namespace Tool {
export type Progress = typeof Progress.Type
export const Success = Event.durable({
type: "session.next.tool.success",
type: "tool.success",
...options,
schema: {
...ToolBase,
@ -404,7 +400,7 @@ export namespace Tool {
export type Success = typeof Success.Type
export const Failed = Event.durable({
type: "session.next.tool.failed",
type: "tool.failed",
...options,
schema: {
...ToolBase,
@ -427,12 +423,12 @@ export const RetryError = Schema.Struct({
responseBody: Schema.String.pipe(optional),
metadata: Schema.Record(Schema.String, Schema.String).pipe(optional),
}).annotate({
identifier: "session.next.retry_error",
identifier: "session.retry.error",
})
export interface RetryError extends Schema.Schema.Type<typeof RetryError> {}
export const Retried = Event.durable({
type: "session.next.retried",
type: "retried",
...options,
schema: {
...Base,
@ -444,32 +440,29 @@ export type Retried = typeof Retried.Type
export namespace Compaction {
export const Started = Event.durable({
type: "session.next.compaction.started",
type: "compaction.started",
...options,
schema: {
...Base,
messageID: SessionMessage.ID,
reason: Schema.Union([Schema.Literal("auto"), Schema.Literal("manual")]),
},
})
export type Started = typeof Started.Type
export const Delta = Event.ephemeral({
type: "session.next.compaction.delta",
type: "compaction.delta",
schema: {
...Base,
messageID: SessionMessage.ID,
text: Schema.String,
},
})
export type Delta = typeof Delta.Type
export const Ended = Event.durable({
type: "session.next.compaction.ended",
type: "compaction.ended",
...options,
schema: {
...Base,
messageID: SessionMessage.ID,
reason: Started.data.fields.reason,
text: Schema.String,
recent: Schema.String,
@ -480,25 +473,25 @@ export namespace Compaction {
export namespace RevertEvent {
export const Staged = Event.durable({
type: "session.next.revert.staged",
type: "revert.staged",
...options,
schema: { ...Base, revert: Revert.State },
})
export const Cleared = Event.durable({ type: "session.next.revert.cleared", ...options, schema: Base })
export const Cleared = Event.durable({ type: "revert.cleared", ...options, schema: Base })
export const Committed = Event.durable({
type: "session.next.revert.committed",
type: "revert.committed",
...options,
schema: { ...Base, messageID: SessionMessage.ID },
})
}
export const Definitions = Event.inventory(
AgentSwitched,
ModelSwitched,
AgentSelected,
ModelSelected,
Moved,
Renamed,
Forked,
Prompted,
PromptPromoted,
PromptAdmitted,
ExecutionSettled,
ContextUpdated,

View file

@ -8,10 +8,14 @@ import { FileAttachment, Prompt } from "./prompt.js"
import { DateTimeUtcFromMillis, RelativePath, statics } from "./schema.js"
import { SessionID } from "./session-id.js"
import { ascending } from "./identifier.js"
import { Event } from "./event.js"
export const ID = Schema.String.check(Schema.isStartsWith("msg_")).pipe(
Schema.brand("Session.Message.ID"),
statics((schema) => ({ create: () => schema.make("msg_" + ascending()) })),
statics((schema) => ({
create: () => schema.make("msg_" + ascending()),
fromEvent: (eventID: Event.ID) => schema.make(eventID.replace(/^evt_/, "msg_")),
})),
)
export type ID = typeof ID.Type
@ -27,19 +31,19 @@ const Base = {
time: Schema.Struct({ created: DateTimeUtcFromMillis }),
}
export interface AgentSwitched extends Schema.Schema.Type<typeof AgentSwitched> {}
export const AgentSwitched = Schema.Struct({
export interface AgentSelected extends Schema.Schema.Type<typeof AgentSelected> {}
export const AgentSelected = Schema.Struct({
...Base,
type: Schema.Literal("agent-switched"),
agent: Schema.String,
}).annotate({ identifier: "Session.Message.AgentSwitched" })
}).annotate({ identifier: "Session.Message.AgentSelected" })
export interface ModelSwitched extends Schema.Schema.Type<typeof ModelSwitched> {}
export const ModelSwitched = Schema.Struct({
export interface ModelSelected extends Schema.Schema.Type<typeof ModelSelected> {}
export const ModelSelected = Schema.Struct({
...Base,
type: Schema.Literal("model-switched"),
model: Model.Ref,
}).annotate({ identifier: "Session.Message.ModelSwitched" })
}).annotate({ identifier: "Session.Message.ModelSelected" })
export interface User extends Schema.Schema.Type<typeof User> {}
export const User = Schema.Struct({
@ -207,8 +211,8 @@ export const Compaction = Schema.Struct({
}).annotate({ identifier: "Session.Message.Compaction" })
export const Message = Schema.Union([
AgentSwitched,
ModelSwitched,
AgentSelected,
ModelSelected,
User,
Synthetic,
System,
@ -219,5 +223,5 @@ export const Message = Schema.Union([
])
.pipe(Schema.toTaggedUnion("type"))
.annotate({ identifier: "Session.Message" })
export type Message = AgentSwitched | ModelSwitched | User | Synthetic | System | Skill | Shell | Assistant | Compaction
export type Message = AgentSelected | ModelSelected | User | Synthetic | System | Skill | Shell | Assistant | Compaction
export type Type = Message["type"]

View file

@ -52,7 +52,7 @@ describe("public event manifest", () => {
expect(Session.Event.Definitions).toBe(SessionEvent.Definitions)
expect(Workspace.Event).toBe(WorkspaceEvent)
expect(Workspace.Event.Definitions).toBe(WorkspaceEvent.Definitions)
expect(EventManifest.Latest.get("session.next.step.ended")).toBe(SessionEvent.Step.Ended)
expect(EventManifest.Latest.get("step.ended")).toBe(SessionEvent.Step.Ended)
expect(EventManifest.Latest.get("todo.updated")).toBe(SessionTodo.Event.Updated)
expect(EventManifest.Latest.get("agent.updated")).toBe(Agent.Event.Updated)
expect(EventManifest.Latest.get("project.updated")).toBe(Project.Event.Updated)
@ -71,8 +71,8 @@ describe("public event manifest", () => {
SessionV1.Event.Diff,
SessionV1.Event.Error,
])
expect(EventManifest.Durable.has("session.next.step.ended.1")).toBe(false)
expect(EventManifest.Durable.get("session.next.step.ended.2")).toBe(SessionEvent.Step.Ended)
expect(EventManifest.Durable.get("step.ended.1")).toBe(SessionEvent.Step.Ended)
expect(EventManifest.Durable.has("step.ended.2")).toBe(false)
})
test("derives durable definitions from explicit definition durability", () => {
@ -85,37 +85,37 @@ describe("public event manifest", () => {
"message.removed.1",
"message.part.updated.1",
"message.part.removed.1",
"session.next.agent.switched.1",
"session.next.model.switched.1",
"session.next.moved.1",
"session.next.renamed.1",
"session.next.forked.1",
"session.next.prompted.1",
"session.next.prompt.admitted.1",
"session.next.context.updated.1",
"session.next.synthetic.1",
"session.next.skill.activated.1",
"session.next.shell.started.1",
"session.next.shell.ended.1",
"session.next.step.started.1",
"session.next.step.ended.2",
"session.next.step.failed.2",
"session.next.text.started.1",
"session.next.text.ended.1",
"session.next.tool.input.started.1",
"session.next.tool.input.ended.1",
"session.next.tool.called.1",
"session.next.tool.progress.1",
"session.next.tool.success.1",
"session.next.tool.failed.1",
"session.next.reasoning.started.1",
"session.next.reasoning.ended.1",
"session.next.retried.1",
"session.next.compaction.started.1",
"session.next.compaction.ended.1",
"session.next.revert.staged.1",
"session.next.revert.cleared.1",
"session.next.revert.committed.1",
"agent.selected.1",
"model.selected.1",
"session.moved.1",
"renamed.1",
"forked.1",
"prompt.promoted.1",
"prompt.admitted.1",
"session.context.updated.1",
"synthetic.1",
"skill.activated.1",
"shell.started.1",
"shell.ended.1",
"step.started.1",
"step.ended.1",
"step.failed.1",
"text.started.1",
"text.ended.1",
"tool.input.started.1",
"tool.input.ended.1",
"tool.called.1",
"tool.progress.1",
"tool.success.1",
"tool.failed.1",
"reasoning.started.1",
"reasoning.ended.1",
"retried.1",
"compaction.started.1",
"compaction.ended.1",
"revert.staged.1",
"revert.cleared.1",
"revert.committed.1",
].toSorted(),
)
expect(SessionEvent.DurableDefinitions).toEqual(