diff --git a/apps/server/src/cli.ts b/apps/server/src/cli.ts index 5517fec2..7234aa68 100644 --- a/apps/server/src/cli.ts +++ b/apps/server/src/cli.ts @@ -144,7 +144,7 @@ export const serverAt = (home: string, isDefault: boolean) => const info = yield* Codework.call((client) => client("server.info", {})).pipe(Effect.option); if (Option.isNone(info) || info.value.pid !== pid) return { _tag: "Unreachable", url, pid } satisfies CliServer; const { version } = info.value; - const usable = yield* Codework.call((client) => client("session.list", {})).pipe(Effect.isSuccess); + const usable = yield* Codework.call((client) => client("thread.list", {})).pipe(Effect.isSuccess); return { _tag: usable ? "Running" : "Incompatible", url, pid, version } satisfies CliServer; }); diff --git a/apps/server/src/codework.ts b/apps/server/src/codework.ts index ac05bd31..6c4f5312 100644 --- a/apps/server/src/codework.ts +++ b/apps/server/src/codework.ts @@ -9,7 +9,10 @@ import path from "node:path"; import { CodeworkUnavailable } from "@codeworksh/contract"; import { Servers } from "./servers.ts"; -export type SessionInfo = Mirror.SessionInfo; +export type ThreadInfo = Mirror.ThreadInfo; + +/** What an untitled thread is called until its first prompt names it. */ +export const UNTITLED = Mirror.UNTITLED; /** How a running server advertises itself: `/service.json`, written by `codework serve`. */ const Registration = Schema.fromJsonString( diff --git a/apps/server/src/handlers.ts b/apps/server/src/handlers.ts index 5bb996a1..f6ada460 100644 --- a/apps/server/src/handlers.ts +++ b/apps/server/src/handlers.ts @@ -17,12 +17,14 @@ import { Servers } from "./servers.ts"; import { Workspaces } from "./workspaces.ts"; /** - * Sessions grouped by project, both newest first. A session whose project row is missing gets a - * group of its own folder, so it still shows rather than vanishing. + * Sessions grouped by project, both newest first, each shown as its main thread. A session whose + * project row is missing gets a group of its own folder, so it still shows rather than vanishing. */ -const byProject = (sessions: ReadonlyArray): Project[] => { +const byProject = (threads: ReadonlyArray): Project[] => { const groups = new Map(); - for (const session of [...sessions].sort((a, b) => b.updatedAt - a.updatedAt)) { + const mains = threads.filter((thread) => thread.origin.kind === "main"); + for (const thread of [...mains].sort((a, b) => b.updatedAt - a.updatedAt)) { + const session = thread.session; const folder = session.hostDir ?? session.directory; const key = session.project?.id ?? folder; const group = groups.get(key) ?? { @@ -34,9 +36,9 @@ const byProject = (sessions: ReadonlyArray): Project[] => }; group.sessions.push({ id: session.id, - title: session.title, - updated: session.updatedAt, - ...(session.model === undefined ? {} : { model: session.model }), + title: thread.title ?? Codework.UNTITLED, + updated: thread.updatedAt, + ...(thread.model === undefined ? {} : { model: thread.model }), }); groups.set(key, group); } @@ -67,7 +69,7 @@ export const layer = Contract.Api.toLayer( ...desk, "projects.list": Effect.fn("projects.list")(function* () { - return byProject(yield* call((client) => client("session.list", {}))); + return byProject(yield* call((client) => client("thread.list", {}))); }), // The catalog's own errors are kept apart from an unreachable server, so their detail shows. diff --git a/apps/web/src/widgets/session/backend.ts b/apps/web/src/widgets/session/backend.ts index 2d88d4f3..222bdc14 100644 --- a/apps/web/src/widgets/session/backend.ts +++ b/apps/web/src/widgets/session/backend.ts @@ -4,7 +4,8 @@ import { HistoryEntry, type Part, QueuedPrompt, - type SessionInfo, + type ThreadInfo, + UNTITLED, } from "@codeworksh/widget/codework"; import { Deferred, Effect, Fiber, Option, Schedule, Schema, Stream } from "effect"; @@ -29,13 +30,14 @@ import { type Window, } from "./schema.ts"; -const toInfo = (info: SessionInfo): Info => ({ - title: info.title, +// A session shows as its main thread until threads get a view of their own. +const toInfo = (info: ThreadInfo): Info => ({ + title: info.title ?? UNTITLED, updated: info.updatedAt, - ...(info.hostDir === undefined ? {} : { hostDir: info.hostDir }), - ...(info.sandbox === undefined ? {} : { sandbox: info.sandbox.driver }), + ...(info.session.hostDir === undefined ? {} : { hostDir: info.session.hostDir }), + ...(info.session.sandbox === undefined ? {} : { sandbox: info.session.sandbox.driver }), ...(info.thinkingLevel === undefined ? {} : { thinkingLevel: info.thinkingLevel }), - ...(info.project === undefined ? {} : { project: info.project }), + ...(info.session.project === undefined ? {} : { project: info.session.project }), ...(info.model === undefined ? {} : { model: info.model }), }); @@ -180,9 +182,9 @@ export const session = defineBackend({ const refresh = Effect.fn("Session.refresh")(function* (id: string, cursor: number | undefined) { // In this order: a prompt promoted between the queue and history reads is then in both, never in neither. const { info, queue, history } = yield* Effect.all({ - info: codework("session.info", { sessionId: id }), - queue: codework("session.queue", { sessionId: id }), - history: codework("session.history", { sessionId: id }), + info: codework("thread.info", { sessionId: id }), + queue: codework("thread.queue", { sessionId: id }), + history: codework("thread.history", { sessionId: id }), }); const before = yield* load(id); const next = toInfo(info); @@ -410,7 +412,7 @@ export const session = defineBackend({ ); const prompt = ({ sessionId, messageId, text, delivery }: Prompt) => - codework("session.prompt", { + codework("thread.prompt", { sessionId, text, id: messageId, @@ -437,7 +439,9 @@ export const session = defineBackend({ ...(thinkingLevel === undefined ? {} : { thinkingLevel }), }, }).pipe( - Effect.flatMap((info) => prompt({ sessionId: info.id, messageId, text }).pipe(Effect.as(info.id))), + Effect.flatMap((info) => + prompt({ sessionId: info.session.id, messageId, text }).pipe(Effect.as(info.session.id)), + ), Effect.map((sessionId): Created => ({ _tag: "Created", sessionId })), Effect.catch((error) => Effect.succeed({ _tag: "Failed", message: describe(error) })), Effect.flatMap((created) => encodeCreated(created).pipe(Effect.orDie)), @@ -446,7 +450,7 @@ export const session = defineBackend({ // The session's own log moves with the change, so the refresh that follows publishes the new info. yield* handle(topics.configure, Configure, ({ sessionId, model, thinkingLevel }) => - codework("session.configure", { + codework("thread.configure", { sessionId, runtime: { ...(model === undefined ? {} : { model }), @@ -462,7 +466,7 @@ export const session = defineBackend({ // A cancel that lost to promotion answers `Sent`: the prompt is in the conversation, and the // refresh that follows the log moving shows it there. yield* handle(topics.cancel, Cancel, ({ sessionId, messageId }) => - codework("session.cancel", { sessionId, messageId }).pipe( + codework("thread.cancel", { sessionId, messageId }).pipe( Effect.map(({ cancelled }): Cancelled => (cancelled ? { _tag: "Cancelled" } : { _tag: "Sent" })), Effect.catch((error) => Effect.succeed({ _tag: "Failed", message: describe(error) })), Effect.flatMap((reply) => encodeCancelled(reply).pipe(Effect.orDie)), @@ -470,7 +474,7 @@ export const session = defineBackend({ ); yield* handle(topics.change, Change, ({ sessionId, messageId, delivery, text }) => - codework("session.update", { + codework("thread.update", { sessionId, messageId, ...(delivery === undefined ? {} : { delivery }), diff --git a/apps/widget/src/codework.ts b/apps/widget/src/codework.ts index 6588eef7..38c80883 100644 --- a/apps/widget/src/codework.ts +++ b/apps/widget/src/codework.ts @@ -44,7 +44,7 @@ export type ToolCall = typeof ToolCall.Type; export const Part = Schema.Union([TextContent, ImageContent, ThinkingContent, ToolCall]); export type Part = typeof Part.Type; -/** One message on a session's active branch, oldest first. */ +/** One message in a thread's line, inherited ones first, oldest first. */ export const HistoryEntry = Schema.Struct({ id: Schema.String, seq: Schema.Int, @@ -65,18 +65,33 @@ export type ThinkingLevel = typeof ThinkingLevel.Type; export const SessionInfo = Schema.Struct({ id: Schema.String, - title: Schema.String, directory: Schema.String, hostDir: Schema.optionalKey(Schema.String), /** The sandbox the session runs in; absent for the host itself. */ sandbox: Schema.optionalKey(Schema.Struct({ driver: Schema.String })), project: Schema.optionalKey(Schema.Struct({ id: Schema.String, name: Schema.String })), updatedAt: Schema.Finite, + /** Where unaddressed prompts go. */ + mainThreadId: Schema.String, +}); +export type SessionInfo = typeof SessionInfo.Type; + +/** One linear transcript of a session, with the session embedded. */ +export const ThreadInfo = Schema.Struct({ + id: Schema.String, + origin: Schema.Struct({ kind: Schema.Literals(["main", "fork", "subagent"]) }), + /** Absent until its first prompt. */ + title: Schema.optionalKey(Schema.String), + updatedAt: Schema.Finite, model: Schema.optionalKey(Schema.Struct({ provider: Schema.String, id: Schema.String })), - /** The thinking level chosen for this session; absent when settings decide. */ + /** The thinking level chosen for this thread; absent when settings decide. */ thinkingLevel: Schema.optionalKey(ThinkingLevel), + session: SessionInfo, }); -export type SessionInfo = typeof SessionInfo.Type; +export type ThreadInfo = typeof ThreadInfo.Type; + +/** What a client calls a thread its first prompt has not named yet. */ +export const UNTITLED = "Session"; /** A harness event as the server sends it; `data` stays encoded, decoded per `type` by its reader. */ export const EventEnvelope = Schema.Struct({ @@ -116,9 +131,9 @@ export class SessionNotFoundError extends Schema.TaggedError()("PromptConflictError", { - sessionId: Schema.String, + threadId: Schema.String, messageId: Schema.String, }) {} @@ -194,6 +209,7 @@ export type RuntimeConfig = typeof RuntimeConfig.Type; */ export const CreateError = Schema.Union([ SessionNotFoundError, + Schema.TaggedStruct("ThreadNotFoundError", { threadId: Schema.String }), Schema.TaggedStruct("SessionLinkedSpaceNotFoundError", { sessionId: Schema.String }), Schema.TaggedStruct("RelinkError", { sessionId: Schema.String, reason: Schema.String }), Schema.TaggedStruct("SandboxNotFoundError", { id: Schema.String }), @@ -210,6 +226,16 @@ export const CreateError = Schema.Union([ ]); export type CreateError = typeof CreateError.Type; +/** A thread of a session; the session's main thread when `threadId` is absent. */ +const ThreadRef = { sessionId: Schema.String, threadId: Schema.optionalKey(Schema.String) }; + +/** The server's "no such thread" (the harness `ThreadNotFoundError`), by its wire tag. */ +export class ThreadNotFoundError extends Schema.TaggedError()("ThreadNotFoundError", { + threadId: Schema.String, +}) {} + +const ThreadError = Schema.Union([SessionNotFoundError, ThreadNotFoundError]); + export const Api = RpcGroup.make( Rpc.make("server.info", { payload: {}, @@ -229,7 +255,8 @@ export const Api = RpcGroup.make( /** Names the project if this creates it; an existing project keeps its name. Trimmed, non-empty. */ project: Schema.optionalKey(Schema.Struct({ name: Schema.String })), }, - success: SessionInfo, + /** The session's main thread. */ + success: ThreadInfo, error: CreateError, }), /** The models a session anchored at `hostDir` can use; without it, only the user's settings apply. */ @@ -238,33 +265,34 @@ export const Api = RpcGroup.make( success: ModelList, error: Schema.Union([SettingsError, ModelCatalogError]), }), - Rpc.make("session.list", { - payload: {}, - success: Schema.Array(SessionInfo), + /** Threads newest first, of one session or all. */ + Rpc.make("thread.list", { + payload: { sessionId: Schema.optionalKey(Schema.String) }, + success: Schema.Array(ThreadInfo), }), - /** Sets the session's model and thinking level over its settings; they apply from its next turn. */ - Rpc.make("session.configure", { - payload: { sessionId: Schema.String, runtime: RuntimeConfig }, - success: SessionInfo, - error: SessionNotFoundError, + /** Sets the thread's model and thinking level over its settings; they apply from its next request. */ + Rpc.make("thread.configure", { + payload: { ...ThreadRef, runtime: RuntimeConfig }, + success: ThreadInfo, + error: ThreadError, }), - Rpc.make("session.info", { - payload: { sessionId: Schema.String }, - success: SessionInfo, - error: SessionNotFoundError, + Rpc.make("thread.info", { + payload: ThreadRef, + success: ThreadInfo, + error: ThreadError, }), - Rpc.make("session.history", { - payload: { sessionId: Schema.String }, + Rpc.make("thread.history", { + payload: ThreadRef, success: Schema.Struct({ entries: Schema.Array(HistoryEntry) }), - error: SessionNotFoundError, + error: ThreadError, }), /** Prompts admitted but not yet promoted, in admission order; reread whenever the log moves. */ - Rpc.make("session.queue", { - payload: { sessionId: Schema.String }, + Rpc.make("thread.queue", { + payload: ThreadRef, success: Schema.Struct({ entries: Schema.Array(QueuedPrompt) }), - error: SessionNotFoundError, + error: ThreadError, }), - /** Durable events after `after` (exclusive), then `log.synced`; with `follow`, each new one as it commits. */ + /** Durable events of every thread after `after` (exclusive), then `log.synced`; with `follow`, each new one as it commits. */ Rpc.make("session.log", { payload: { sessionId: Schema.String, @@ -276,33 +304,34 @@ export const Api = RpcGroup.make( stream: true, }), /** `id` names the user message the prompt becomes; it is the prompt's correlation id. */ - Rpc.make("session.prompt", { + Rpc.make("thread.prompt", { payload: { - sessionId: Schema.String, + ...ThreadRef, text: Schema.String, delivery: Schema.optionalKey(Delivery), id: Schema.optionalKey(Schema.String), }, - success: Schema.Void, - error: Schema.Union([SessionNotFoundError, PromptConflictError]), + success: Schema.Struct({ threadId: Schema.String }), + error: Schema.Union([ThreadError, PromptConflictError]), }), /** Withdraws a queued prompt; `cancelled` is false when it was already promoted or is unknown. */ - Rpc.make("session.cancel", { - payload: { sessionId: Schema.String, messageId: Schema.String }, + Rpc.make("thread.cancel", { + payload: { ...ThreadRef, messageId: Schema.String }, success: Schema.Struct({ cancelled: Schema.Boolean }), - error: SessionNotFoundError, + error: ThreadError, }), - /** Moves a queued prompt to the other lane or rewrites it in place; `delivery: "steer"` also wakes the session. */ - Rpc.make("session.update", { + /** Moves a queued prompt to the other lane or rewrites it in place; `delivery: "steer"` also wakes the thread. */ + Rpc.make("thread.update", { payload: { - sessionId: Schema.String, + ...ThreadRef, messageId: Schema.String, delivery: Schema.optionalKey(Delivery), text: Schema.optionalKey(Schema.String), }, success: Schema.Struct({ updated: Schema.Boolean }), - error: SessionNotFoundError, + error: ThreadError, }), + /** Stops every thread of the session. */ Rpc.make("session.interrupt", { payload: { sessionId: Schema.String }, success: Schema.Struct({ interrupted: Schema.Boolean }), diff --git a/packages/aikit/test/e2e/__artifacts__/thinking.levels.anthropic.json b/packages/aikit/test/e2e/__artifacts__/thinking.levels.anthropic.json index 2abf9716..aa7beb02 100644 --- a/packages/aikit/test/e2e/__artifacts__/thinking.levels.anthropic.json +++ b/packages/aikit/test/e2e/__artifacts__/thinking.levels.anthropic.json @@ -183,6 +183,37 @@ "providerThinkingLevel": "max" } }, + "claude-haiku-5-5": { + "off": { + "thinkingLevel": "off", + "providerThinkingLevel": null, + "thinking": false + }, + "minimal": { + "thinkingLevel": "minimal", + "providerThinkingLevel": "low" + }, + "low": { + "thinkingLevel": "low", + "providerThinkingLevel": "low" + }, + "medium": { + "thinkingLevel": "medium", + "providerThinkingLevel": "medium" + }, + "high": { + "thinkingLevel": "high", + "providerThinkingLevel": "high" + }, + "xhigh": { + "thinkingLevel": "xhigh", + "providerThinkingLevel": "xhigh" + }, + "max": { + "thinkingLevel": "max", + "providerThinkingLevel": "max" + } + }, "claude-haiku-4-5-20251001": { "off": { "thinkingLevel": "off", diff --git a/packages/codework/src/acp/feed.ts b/packages/codework/src/acp/feed.ts index 95600830..708552ea 100644 --- a/packages/codework/src/acp/feed.ts +++ b/packages/codework/src/acp/feed.ts @@ -1,5 +1,5 @@ import type * as Acp from "@agentclientprotocol/sdk"; -import { EventList, type EventSchema, type SessionStore } from "@codeworksh/harness/effect"; +import { EventList, type EventSchema, type ThreadStore } from "@codeworksh/harness/effect"; import { Option, Schema } from "effect"; const isTextDelta = Schema.is(EventList.LLMTextDelta); @@ -92,7 +92,7 @@ export const noticeUpdate = (notice: string): Acp.SessionUpdate => ({ }); /** The `session/update`s that replay one stored entry during `session/load`. */ -export const replay = ({ entry, parts }: SessionStore.HydratedEntry): ReadonlyArray => { +export const replay = ({ entry, parts }: ThreadStore.HydratedEntry): ReadonlyArray => { if (entry.type !== "user" && entry.type !== "assistant") return []; return parts.flatMap((row): ReadonlyArray => { const part = decodePart(row.data); diff --git a/packages/codework/src/acp/handlers.ts b/packages/codework/src/acp/handlers.ts index 4be72cd0..a7f74ec4 100644 --- a/packages/codework/src/acp/handlers.ts +++ b/packages/codework/src/acp/handlers.ts @@ -7,6 +7,7 @@ import { PromptSchema, Session, SessionStore, + ThreadStore, } from "@codeworksh/harness/effect"; import { DateTime, Effect, Fiber, type Layer, Option, Schema, Stream } from "effect"; import pkg from "../../package.json" with { type: "json" }; @@ -82,6 +83,7 @@ type Services = Layer.Success>; export const make = Effect.gen(function* () { const control = yield* Control.Service; const sessions = yield* SessionStore.Service; + const threads = yield* ThreadStore.Service; // Handlers run on the agent's fibers, outside this layer; they carry the harness with them. const context = yield* Effect.context(); @@ -113,7 +115,7 @@ export const make = Effect.gen(function* () { run( Effect.gen(function* () { const handle = yield* Session.create({ directory: cwd, hostDir: cwd }); - const current = yield* Session.configuration(handle.id); + const current = yield* Session.configuration(handle.main.id); return { sessionId: handle.id, configOptions: yield* Config.options(current, cwd) }; }), ), @@ -121,11 +123,12 @@ export const make = Effect.gen(function* () { loadSession: ({ sessionId: id }) => run( Effect.gen(function* () { - yield* Session.attach({ sessionId: sessionId(id) }); + // An ACP session is a CodeWork session's main thread. + const main = yield* Session.attach({ sessionId: sessionId(id) }); const row = yield* found(id); const hostDir = Option.getOrUndefined(row.hostDir); - const current = yield* Session.configuration(row.id); - for (const entry of yield* sessions.path(sessionId(id))) { + const current = yield* Session.configuration(main.id); + for (const entry of yield* main.history()) { for (const update of Feed.replay(entry)) { yield* client.sessionUpdate({ sessionId: id, update }); } @@ -140,6 +143,11 @@ export const make = Effect.gen(function* () { const rows = (yield* sessions.list()) .filter((row) => cwd == null || Option.getOrUndefined(row.hostDir) === cwd) .sort((a, b) => DateTime.Order(b.updatedAt ?? b.createdAt, a.updatedAt ?? a.createdAt)); + const titles = new Map(); + for (const thread of yield* threads.list()) { + if (thread.originKind === "main" && Option.isSome(thread.title)) + titles.set(thread.sessionId, thread.title.value); + } const offset = cursor == null ? 0 : Number(cursor); if (!Number.isSafeInteger(offset) || offset < 0) { return yield* Agent.RequestError.invalidParams(`Invalid cursor: ${cursor}`); @@ -149,7 +157,7 @@ export const make = Effect.gen(function* () { sessions: rows.slice(offset, next).map((row) => ({ sessionId: row.id, cwd: Option.getOrElse(row.hostDir, () => row.directory), - title: row.title, + title: titles.get(row.id) ?? null, updatedAt: DateTime.formatIso(row.updatedAt ?? row.createdAt), })), ...(next < rows.length ? { nextCursor: String(next) } : {}), @@ -177,7 +185,8 @@ export const make = Effect.gen(function* () { } else { return yield* Agent.RequestError.invalidParams(`Unknown config option: ${request.configId}`); } - return { configOptions: yield* Config.options(yield* Session.configuration(row.id), hostDir) }; + const main = yield* Session.attach({ sessionId: row.id }); + return { configOptions: yield* Config.options(yield* Session.configuration(main.id), hostDir) }; }), ), @@ -185,10 +194,11 @@ export const make = Effect.gen(function* () { run( Effect.gen(function* () { const parts = yield* promptParts(prompt); - const handle = Option.getOrUndefined(yield* Session.get(sessionId(id))); - if (handle === undefined) { + const session = Option.getOrUndefined(yield* Session.get(sessionId(id))); + if (session === undefined) { return yield* new SessionStore.SessionNotFoundError({ sessionId: sessionId(id) }); } + const handle = session.main; if (running.has(id)) { return yield* Agent.RequestError.invalidRequest("A prompt is already running for this session"); } @@ -221,9 +231,9 @@ export const make = Effect.gen(function* () { yield* handle.run({ parts }); const end = yield* Fiber.join(settled); - // The harness titles a session from its first prompt; tell the editor. + // The harness titles a thread from its first prompt; tell the editor. const { title } = yield* handle.info; - if (title !== before) { + if (title !== undefined && title !== before) { yield* client .sessionUpdate({ sessionId: id, update: { sessionUpdate: "session_info_update", title } }) .pipe(Effect.ignore); @@ -235,7 +245,7 @@ export const make = Effect.gen(function* () { }).pipe(Effect.scoped), ), - cancel: ({ sessionId: id }) => control.interrupt(sessionId(id), { reason: "user" }).pipe(Effect.asVoid), + cancel: ({ sessionId: id }) => control.interruptSession(sessionId(id), { reason: "user" }).pipe(Effect.asVoid), }; }; }); diff --git a/packages/codework/src/cli/cmd/cmd.ts b/packages/codework/src/cli/cmd/cmd.ts index ee228820..6add7f14 100644 --- a/packages/codework/src/cli/cmd/cmd.ts +++ b/packages/codework/src/cli/cmd/cmd.ts @@ -130,6 +130,10 @@ export const Cmd = Spec.make("codework", { Flag.withDescription("Continue an existing session"), Flag.optional, ), + thread: Flag.String("thread").pipe( + Flag.withDescription("Thread of --session to continue: a thread ID, or main (default)"), + Flag.optional, + ), cwd: Flag.String("cwd").pipe( Flag.withAlias("C"), Flag.withDescription("Working directory for a new session"), @@ -172,6 +176,10 @@ export const Cmd = Spec.make("codework", { command: 'codework run --session --provider openai --model gpt-5.6-luna "Now fix them"', description: "Continue a session with explicit model bindings", }, + { + command: 'codework run --session --thread "Try OAuth instead"', + description: "Continue one of the session's threads", + }, ], }), Spec.make("tui", { diff --git a/packages/codework/src/cli/cmd/handlers/plugin/session.ts b/packages/codework/src/cli/cmd/handlers/plugin/session.ts index 19d8a9cc..a6cafd3b 100644 --- a/packages/codework/src/cli/cmd/handlers/plugin/session.ts +++ b/packages/codework/src/cli/cmd/handlers/plugin/session.ts @@ -32,7 +32,7 @@ export const linkedDirectory = Effect.fn("CLI.plugin.linkedDirectory")(function* // Only the session row is read, so this needs the database and the event log the store is // built on -- no sandboxes, no control plane, no plugin machinery. Effect.provide( - SessionStore.layer.pipe( + SessionStore.storeLayer.pipe( Layer.provide(Event.layer), Layer.provide(Database.layer(yield* Database.path(paths.data))), ), diff --git a/packages/codework/src/cli/cmd/handlers/run.ts b/packages/codework/src/cli/cmd/handlers/run.ts index 8632bdbd..78afdd83 100644 --- a/packages/codework/src/cli/cmd/handlers/run.ts +++ b/packages/codework/src/cli/cmd/handlers/run.ts @@ -80,6 +80,7 @@ export default Runtime.handler( Effect.fn("CLI.run", { level: "Info" })(function* ({ prompt, session, + thread, cwd, sandboxDriver, sandboxProviderId, @@ -102,6 +103,16 @@ export default Runtime.handler( "--cwd, --sandbox-driver, --sandbox-id, and --sandbox-provider-id can only be used when creating a new session", }); } + if (Option.isSome(thread) && Option.isNone(session)) { + return yield* new InvalidInputError({ message: "--thread continues a thread of --session" }); + } + if ( + Option.isSome(thread) && + thread.value !== Session.ThreadSchema.MAIN && + !Schema.is(Session.ThreadSchema.ID)(thread.value) + ) { + return yield* new InvalidInputError({ message: "--thread takes a thread ID (thr_…) or main" }); + } if (Option.isSome(provider) !== Option.isSome(model)) { return yield* new InvalidInputError({ message: "--provider and --model must be provided together" }); } @@ -140,10 +151,19 @@ export default Runtime.handler( : { model: { provider: provider.value, id: model.value } }), ...(Option.isNone(thinking) ? {} : { thinkingLevel: thinking.value }), }; + // `main` names the session's main thread; anything else is a thread ID. + const threadId = + Option.isNone(thread) || thread.value === Session.ThreadSchema.MAIN + ? undefined + : Session.ThreadSchema.ID.make(thread.value); const remote = Effect.gen(function* () { const rpc = yield* Client.make; const info = Option.isSome(session) - ? yield* rpc["session.configure"]({ sessionId: Session.SessionSchema.ID.make(session.value), runtime }) + ? yield* rpc["thread.configure"]({ + sessionId: Session.SessionSchema.ID.make(session.value), + ...(threadId === undefined ? {} : { threadId }), + runtime, + }) : yield* rpc["session.create"]({ runtime, ...(Option.isNone(cwd) ? {} : { directory: cwd.value }), @@ -154,32 +174,40 @@ export default Runtime.handler( const columns = terminalColumns(); yield* writeError( header({ - sessionId: info.id, - sandbox: info.sandbox?.driver ?? "local", - directory: info.directory, + sessionId: info.session.id, + sandbox: info.session.sandbox?.driver ?? "local", + directory: info.placement?.directory ?? info.session.directory, columns, }), ); - if (info.sandbox !== undefined) yield* writeError(`sandbox-id: ${info.sandbox.id}\n`); - yield* Client.run(rpc, { sessionId: info.id, text: prompt }, render(state)).pipe( + if (info.session.sandbox !== undefined) yield* writeError(`sandbox-id: ${info.session.sandbox.id}\n`); + yield* Client.run(rpc, { sessionId: info.session.id, threadId: info.id, text: prompt }, render(state)).pipe( Effect.onInterrupt(() => - rpc["session.interrupt"]({ sessionId: info.id }).pipe(Effect.timeout("5 seconds"), Effect.ignore), + rpc["thread.interrupt"]({ sessionId: info.session.id, threadId: info.id }).pipe( + Effect.timeout("5 seconds"), + Effect.ignore, + ), ), ); yield* finish(state, columns); }); const program = Effect.gen(function* () { - let handle: Session.Handle; + let handle: Session.ThreadHandle; if (Option.isSome(session)) { - handle = yield* Session.attach({ sessionId: Session.SessionSchema.ID.make(session.value), ...runtime }); + handle = yield* Session.attach({ + sessionId: Session.SessionSchema.ID.make(session.value), + ...(threadId === undefined ? {} : { threadId }), + ...runtime, + }); } else { const selected = yield* selectSandbox(selection); - handle = yield* Session.create({ + const created = yield* Session.create({ ...runtime, ...(selected === undefined ? {} : { sandbox: selected }), ...(Option.isNone(cwd) ? {} : { directory: cwd.value }), hostDir, }); + handle = created.main; } const ended = yield* Queue.unbounded(); const renderState = yield* Ref.make(initialRenderState); @@ -191,9 +219,9 @@ export default Runtime.handler( const columns = terminalColumns(); yield* writeError( header({ - sessionId: handle.id, - sandbox: info.sandbox?.driver ?? "local", - directory: info.directory, + sessionId: handle.sessionId, + sandbox: info.session.sandbox?.driver ?? "local", + directory: info.placement?.directory ?? info.session.directory, columns, }), ); @@ -206,8 +234,7 @@ export default Runtime.handler( ); yield* handle.wait(); if (Exit.isFailure(execution)) return yield* Effect.failCause(execution.cause); - const path = yield* handle.path(); - const leaf = path.at(-1); + const leaf = (yield* handle.history()).at(-1); if (leaf !== undefined) yield* awaitMessage(ended, leaf.entry.id); yield* Fiber.interrupt(printer); yield* finish(renderState, columns); diff --git a/packages/codework/src/server/client.ts b/packages/codework/src/server/client.ts index 01a01254..64a473fa 100644 --- a/packages/codework/src/server/client.ts +++ b/packages/codework/src/server/client.ts @@ -24,7 +24,7 @@ export class ExecutionError extends Schema.TaggedError()("Client /** * Runs one prompt against a server and renders its output. * - * Sessions are shared, so the stream carries other clients' work too and every + * Threads are shared, so the stream carries other clients' work too and every * terminal event has to be attributed before it is believed. A terminal that * arrives before our own prompt is promoted belongs to somebody else's drain: * it is remembered rather than raised, and forgotten the moment our prompt is @@ -34,7 +34,7 @@ export class ExecutionError extends Schema.TaggedError()("Client */ export const run = Effect.fn("Client.run")(function* ( rpc: Client, - input: { sessionId: Session.SessionSchema.ID; text: string }, + input: { sessionId: Session.SessionSchema.ID; threadId: Session.ThreadSchema.ID; text: string }, render: (event: EventSchema.Payload) => Effect.Effect, ) { const id = Session.SessionMessageSchema.ID.create(); @@ -65,7 +65,7 @@ export const run = Effect.fn("Client.run")(function* ( Stream.mapEffect( Effect.fnUntraced(function* (envelope) { if (envelope.type === Envelope.Connected.type) { - yield* rpc["session.prompt"]({ ...input, id, delivery: "followUp" }); + yield* rpc["thread.prompt"]({ ...input, id, delivery: "followUp" }); return; } const event = yield* Envelope.decode(envelope).pipe( @@ -73,8 +73,8 @@ export const run = Effect.fn("Client.run")(function* ( ); if ( event === undefined || - !Predicate.hasProperty(event.data, "sessionId") || - event.data.sessionId !== input.sessionId + !Predicate.hasProperty(event.data, "threadId") || + event.data.threadId !== input.threadId ) return; if (Schema.is(EventList.PromptAdmitted)(event) && event.data.messageId === id) admitted = true; @@ -113,8 +113,8 @@ export const run = Effect.fn("Client.run")(function* ( // finish; only a prompt that never materialized ends the run early. let abandoned = false; const reconcile = Deferred.await(stalled).pipe( - Effect.andThen(rpc["session.wait"]({ sessionId: input.sessionId })), - Effect.andThen(rpc["session.message"]({ sessionId: input.sessionId, messageId: id })), + Effect.andThen(rpc["thread.wait"]({ sessionId: input.sessionId, threadId: input.threadId })), + Effect.andThen(rpc["thread.message"]({ sessionId: input.sessionId, threadId: input.threadId, messageId: id })), Effect.andThen(({ found }) => found ? Effect.never diff --git a/packages/codework/src/server/contract.ts b/packages/codework/src/server/contract.ts index 2ebdc28a..25f0360b 100644 --- a/packages/codework/src/server/contract.ts +++ b/packages/codework/src/server/contract.ts @@ -12,6 +12,8 @@ import { Session, SessionStore, Settings, + ThreadSchema, + ThreadStore, } from "@codeworksh/harness/effect"; import { Schema } from "effect"; import { Rpc, RpcGroup } from "effect/rpc"; @@ -35,30 +37,65 @@ export const SandboxInfo = Schema.Struct({ }); export type SandboxInfo = typeof SandboxInfo.Type; +/** The container: where its threads run by default, and which host project it reads. */ export const SessionInfo = Schema.Struct({ id: Session.SessionSchema.ID, - title: Schema.String, directory: Session.AbsolutePath, /** The host directory this session is anchored to. Absent when it has none. */ hostDir: optional(Session.AbsolutePath), sandbox: optional(SandboxInfo), /** The project the session's space belongs to. */ project: optional(Schema.Struct({ id: Schema.String, name: Schema.String })), + /** Last activity in any of its threads, epoch milliseconds. */ + updatedAt: Schema.Finite, + /** Where unaddressed prompts go. */ + mainThreadId: ThreadSchema.ID, +}); +export type SessionInfo = typeof SessionInfo.Type; + +export const ThreadOrigin = Schema.Union([ + Schema.Struct({ kind: Schema.Literal("main") }), + Schema.Struct({ kind: Schema.Literal("fork"), threadId: Schema.String, entryId: Schema.String }), + Schema.Struct({ + kind: Schema.Literal("subagent"), + threadId: Schema.String, + callId: Schema.String, + entryId: optional(Schema.String), + }), +]); + +/** One linear transcript of a session, with the session embedded. */ +export const ThreadInfo = Schema.Struct({ + id: ThreadSchema.ID, + origin: ThreadOrigin, + /** Absent until its first own prompt; clients show a fork's source title until then. */ + title: optional(Schema.String), /** Last activity, epoch milliseconds. */ updatedAt: Schema.Finite, - /** The model chosen for this session; absent when settings decide. */ + /** The model chosen for this thread; absent when settings decide. */ model: optional(Schema.Struct({ provider: Schema.String, id: Schema.String })), - /** The thinking level chosen for this session; absent when settings decide. */ + /** The thinking level chosen for this thread; absent when settings decide. */ thinkingLevel: optional(Schema.Enum(Model.ThinkingLevelEnum)), + /** Where it runs instead of the session's space and directory. */ + placement: optional(Schema.Struct({ spaceId: Schema.String, directory: Session.AbsolutePath })), /** Its unfinished run was cut off by restarts too often to resume on its own; the next prompt runs it. */ parked: optional(Schema.Boolean), + session: SessionInfo, }); -export type SessionInfo = typeof SessionInfo.Type; +export type ThreadInfo = typeof ThreadInfo.Type; -/** One message on a session's active branch, oldest first. */ +/** A thread of a session: `threadId`, or the session's `main` thread when absent. */ +const ThreadRef = { + sessionId: Session.SessionSchema.ID, + threadId: optional(ThreadSchema.ID), +}; + +/** One message in a thread's line (inherited ones first), oldest first. */ export const HistoryEntry = Schema.Struct({ - /** The entry id: what live events and `session.message` refer to. */ + /** The entry id: what live events and `thread.message` refer to. */ id: Schema.String, + /** The thread that wrote it: this one, or a source it was forked from. */ + threadId: Schema.String, /** Its place in the session's durable log; live events continue from it. */ seq: Schema.Int, role: Schema.Literals(["user", "assistant"]), @@ -92,9 +129,10 @@ export const LogSynced = Schema.Struct({ }); export type LogSynced = typeof LogSynced.Type; +/** A thread's model and thinking choices: a given field replaces the stored one, `null` clears it. */ export const RuntimeConfig = Schema.Struct({ - model: optional(Schema.Struct({ provider: Schema.String, id: Schema.String })), - thinkingLevel: optional(Schema.Enum(Model.ThinkingLevelEnum)), + model: optional(Schema.NullOr(Schema.Struct({ provider: Schema.String, id: Schema.String }))), + thinkingLevel: optional(Schema.NullOr(Schema.Enum(Model.ThinkingLevelEnum))), }); export type RuntimeConfig = typeof RuntimeConfig.Type; @@ -165,10 +203,13 @@ export type OAuthInfo = typeof OAuthInfo.Type; const SessionErrors = Schema.Union([ SessionStore.SessionNotFoundError, + ThreadStore.ThreadNotFoundError, SessionStore.SessionLinkedSpaceNotFoundError, SessionStore.RelinkError, ]); +const ThreadErrors = Schema.Union([SessionStore.SessionNotFoundError, ThreadStore.ThreadNotFoundError]); + // Every lifecycle error the sandbox control plane can raise; narrowing per // endpoint would misreport what `withMount` and `stop` actually fail with. const SandboxErrors = Schema.Union([ @@ -218,8 +259,10 @@ export const Api = RpcGroup.make( success: Schema.Void, error: OAuthError, }), + /** A session and its `main` thread; the main thread's info, with the session embedded. */ Rpc.make("session.create", { payload: { + /** The main thread's title; absent = its first prompt names it. */ title: optional(Schema.String), directory: optional(Schema.String), /** @@ -241,7 +284,7 @@ export const Api = RpcGroup.make( */ project: optional(Schema.Struct({ name: ProjectName })), }, - success: SessionInfo, + success: ThreadInfo, error: Schema.Union([SessionErrors, SandboxErrors, LocationErrors]), }), Rpc.make("project.rename", { @@ -271,32 +314,51 @@ export const Api = RpcGroup.make( }), error: Schema.Union([Settings.SettingsError, Runner.ModelCatalogError]), }), - Rpc.make("session.list", { - payload: {}, - success: Schema.Array(SessionInfo), - }), /** - * The conversation on a session's active branch: user and assistant messages, oldest first. - * Other branches and the harness's own entries (compactions, summaries) are left out. + * Point an existing session at a host directory, or clear it with `null`. + * + * Not `session.relink`: that moves where the *work* happens, this changes which host directory + * the session is *anchored to*. Conflating them would recreate exactly the `cwd`/`hostDir` + * confusion the split exists to remove, which is why they sound alike and stay apart. */ - Rpc.make("session.history", { - payload: { sessionId: Session.SessionSchema.ID }, - success: Schema.Struct({ entries: Schema.Array(HistoryEntry) }), + Rpc.make("session.link", { + payload: { + sessionId: Session.SessionSchema.ID, + /** Absent clears the anchor, leaving the session with no host directory. */ + hostDir: optional(Session.AbsolutePath), + }, + success: SessionInfo, error: SessionStore.SessionNotFoundError, }), /** - * Prompts admitted but not yet promoted, in admission order. Admission and promotion are - * durable events, so a client following `session.log` rereads this whenever the log moves. + * Moves where the work happens. Without `threadId`, the session's default space and directory, + * which every thread without its own placement follows; with it, that thread's placement only. */ - Rpc.make("session.queue", { + Rpc.make("session.relink", { + payload: { + sessionId: Session.SessionSchema.ID, + threadId: optional(ThreadSchema.ID), + sandbox: optional(SandboxRef), + directory: optional(Schema.String), + }, + success: SessionInfo, + error: Schema.Union([SessionErrors, SandboxErrors, LocationErrors]), + }), + /** Stops every thread, then removes the session with its log. `deleted` is false when it did not exist. */ + Rpc.make("session.delete", { payload: { sessionId: Session.SessionSchema.ID }, - success: Schema.Struct({ entries: Schema.Array(QueuedPrompt) }), - error: SessionStore.SessionNotFoundError, + success: Schema.Struct({ deleted: Schema.Boolean }), + }), + /** Interrupts every running thread of the session. */ + Rpc.make("session.interrupt", { + payload: { sessionId: Session.SessionSchema.ID }, + success: Schema.Struct({ interrupted: Schema.Boolean }), }), /** * A session's durable events after `after` (exclusive; omitted reads from the start), then a * `log.synced` marker at the head read when the call began. With `follow`, each event then - * streams as it commits; without it the stream ends at the marker. + * streams as it commits; without it the stream ends at the marker. Every thread of the session + * writes this one log; events name their `threadId`. * Ephemeral events (text and tool progress deltas) are never stored, so they are not here; * `event.subscribe` carries them live. To resume, pass the higher of the last event's * `durable.seq` and the last marker's `seq`: the marker can sit past the last event sent. @@ -307,92 +369,102 @@ export const Api = RpcGroup.make( error: Schema.Union([SessionStore.SessionNotFoundError, EncodingError]), stream: true, }), - Rpc.make("session.configure", { - payload: { sessionId: Session.SessionSchema.ID, runtime: RuntimeConfig }, - success: SessionInfo, - error: SessionStore.SessionNotFoundError, + /** Threads newest first, each with its session embedded: of one session, or all. */ + Rpc.make("thread.list", { + payload: { sessionId: optional(Session.SessionSchema.ID) }, + success: Schema.Array(ThreadInfo), }), - Rpc.make("session.info", { - payload: { sessionId: Session.SessionSchema.ID }, - success: SessionInfo, - error: SessionStore.SessionNotFoundError, + Rpc.make("thread.info", { + payload: ThreadRef, + success: ThreadInfo, + error: ThreadErrors, }), /** - * Point an existing session at a host directory, or clear it with `null`. - * - * Not `session.relink`: that moves where the *work* happens, this changes which host directory - * the session is *anchored to*. Conflating them would recreate exactly the `cwd`/`hostDir` - * confusion the split exists to remove, which is why they sound alike and stay apart. + * The conversation in a thread's line: user and assistant messages, inherited ones first, oldest + * first. The harness's own entries (compactions, resets, notices) are left out. */ - Rpc.make("session.link", { - payload: { - sessionId: Session.SessionSchema.ID, - /** Absent clears the anchor, leaving the session with no host directory. */ - hostDir: optional(Session.AbsolutePath), - }, - success: SessionInfo, - error: SessionStore.SessionNotFoundError, + Rpc.make("thread.history", { + payload: ThreadRef, + success: Schema.Struct({ entries: Schema.Array(HistoryEntry) }), + error: ThreadErrors, }), - Rpc.make("session.relink", { - payload: { - sessionId: Session.SessionSchema.ID, - sandbox: optional(SandboxRef), - directory: optional(Schema.String), - }, - success: SessionInfo, - error: Schema.Union([SessionErrors, SandboxErrors, LocationErrors]), + /** + * Prompts admitted to the thread but not yet promoted, in admission order. Admission and + * promotion are durable events, so a client following `session.log` rereads this whenever the + * log moves. + */ + Rpc.make("thread.queue", { + payload: ThreadRef, + success: Schema.Struct({ entries: Schema.Array(QueuedPrompt) }), + error: ThreadErrors, + }), + Rpc.make("thread.configure", { + payload: { ...ThreadRef, runtime: RuntimeConfig }, + success: ThreadInfo, + error: ThreadErrors, + }), + /** + * A new thread whose history continues from this one's line through `entryId`, which must be + * committed. `before` stops at the entry's predecessor and returns its text to edit again. + */ + Rpc.make("thread.fork", { + payload: { ...ThreadRef, entryId: Schema.String, mode: optional(Schema.Literals(["at", "before"])) }, + success: Schema.Struct({ thread: ThreadInfo, text: optional(Schema.String) }), + error: Schema.Union([ThreadErrors, ThreadStore.ForkError]), }), - Rpc.make("session.prompt", { + Rpc.make("thread.prompt", { payload: { - sessionId: Session.SessionSchema.ID, + ...ThreadRef, text: Schema.String, delivery: optional(PromptSchema.Delivery), id: optional(Session.SessionMessageSchema.ID), }, - success: Schema.Void, - error: Schema.Union([SessionStore.SessionNotFoundError, Control.PromptConflictError]), + success: Schema.Struct({ threadId: ThreadSchema.ID }), + error: Schema.Union([ThreadErrors, Control.PromptConflictError]), }), /** * Withdraws a queued prompt before it joins the conversation. `cancelled` is false when it had * already been promoted (it then stays in the conversation) or no pending prompt has that id. */ - Rpc.make("session.cancel", { - payload: { sessionId: Session.SessionSchema.ID, messageId: Session.SessionMessageSchema.ID }, + Rpc.make("thread.cancel", { + payload: { ...ThreadRef, messageId: Session.SessionMessageSchema.ID }, success: Schema.Struct({ cancelled: Schema.Boolean }), - error: SessionStore.SessionNotFoundError, + error: ThreadErrors, }), /** * Moves a queued prompt to the other lane or rewrites it, keeping its place in the queue. - * `delivery: "steer"` also wakes the session, so a prompt left waiting after a stop runs now. + * `delivery: "steer"` also wakes the thread, so a prompt left waiting after a stop runs now. * `updated` is false when it had already been promoted or cancelled, or no pending prompt has that id. */ - Rpc.make("session.update", { + Rpc.make("thread.update", { payload: { - sessionId: Session.SessionSchema.ID, + ...ThreadRef, messageId: Session.SessionMessageSchema.ID, delivery: optional(PromptSchema.Delivery), text: optional(Schema.String), }, success: Schema.Struct({ updated: Schema.Boolean }), - error: SessionStore.SessionNotFoundError, + error: ThreadErrors, }), - Rpc.make("session.interrupt", { - payload: { sessionId: Session.SessionSchema.ID }, + Rpc.make("thread.interrupt", { + payload: ThreadRef, success: Schema.Struct({ interrupted: Schema.Boolean }), + error: ThreadErrors, }), /** - * Whether a prompt materialized into the conversation. An admitted prompt - * that is still queued has no entry yet, so this is the projection a client - * reconciles against when the volatile stream left it unsure + * Whether a prompt materialized into the thread's conversation. An admitted prompt that is still + * queued has no entry yet, so this is the projection a client reconciles against when the + * volatile stream left it unsure. */ - Rpc.make("session.message", { - payload: { sessionId: Session.SessionSchema.ID, messageId: Session.SessionMessageSchema.ID }, + Rpc.make("thread.message", { + payload: { ...ThreadRef, messageId: Session.SessionMessageSchema.ID }, success: Schema.Struct({ found: Schema.Boolean }), + error: ThreadErrors, }), - Rpc.make("session.wait", { - payload: { sessionId: Session.SessionSchema.ID }, + Rpc.make("thread.wait", { + payload: ThreadRef, success: Schema.Void, - error: SessionStore.SessionNotFoundError, + error: ThreadErrors, }), /** * Re-import every configured plugin, for the whole server. diff --git a/packages/codework/src/server/handlers.ts b/packages/codework/src/server/handlers.ts index ea114490..d885c655 100644 --- a/packages/codework/src/server/handlers.ts +++ b/packages/codework/src/server/handlers.ts @@ -2,6 +2,7 @@ import { Model } from "@codeworksh/aikit"; import { Control, Event, + EventList, EventRegistry, EventSchema, ModelCatalog, @@ -12,6 +13,8 @@ import { SessionStore, Settings, State, + ThreadSchema, + ThreadStore, } from "@codeworksh/harness/effect"; import { DateTime, Effect, Option, Stream } from "effect"; import { @@ -21,6 +24,7 @@ import { type RuntimeConfig, type SandboxInfo, type SessionInfo, + type ThreadInfo, } from "./contract.ts"; import { type EncodingError, Envelope, type EventEnvelope } from "./envelope.ts"; import { EventFeed } from "./feed.ts"; @@ -43,29 +47,34 @@ const toSandboxInfo = (info: Sandbox.Info): SandboxInfo => ({ const toSessionInfo = (info: Session.Info): SessionInfo => ({ id: info.id, - title: info.title, directory: info.directory, ...(info.hostDir === undefined ? {} : { hostDir: info.hostDir }), ...(info.sandbox === undefined ? {} : { sandbox: toSandboxInfo(info.sandbox) }), ...(info.project === undefined ? {} : { project: info.project }), updatedAt: info.updatedAt, + mainThreadId: info.mainThreadId, +}); + +const toThreadInfo = (info: Session.ThreadInfo): ThreadInfo => ({ + id: info.id, + origin: info.origin, + ...(info.title === undefined ? {} : { title: info.title }), + updatedAt: info.updatedAt, ...(info.model === undefined ? {} : { model: info.model }), ...(info.thinkingLevel === undefined ? {} : { thinkingLevel: info.thinkingLevel }), + ...(info.placement === undefined ? {} : { placement: info.placement }), ...(info.parked ? { parked: true } : {}), + session: toSessionInfo(info.session), }); -// Wire configs name only the keys they set, so `undefined` never reaches the -// harness inputs under exactOptionalPropertyTypes. +// Wire configs name only the keys they set, so `undefined` never reaches the harness under +// exactOptionalPropertyTypes. Creating takes choices only; `null` clears nothing there. const runtimeInput = (runtime: RuntimeConfig | undefined): Session.RuntimeInput => ({ - ...(runtime?.model === undefined ? {} : { model: runtime.model }), - ...(runtime?.thinkingLevel === undefined ? {} : { thinkingLevel: runtime.thinkingLevel }), + ...(runtime?.model == null ? {} : { model: runtime.model }), + ...(runtime?.thinkingLevel == null ? {} : { thinkingLevel: runtime.thinkingLevel }), }); -const sessionInfo = Effect.fn("Server.sessionInfo")(function* (sessionId: Session.SessionSchema.ID) { - const found = yield* Session.get(sessionId); - if (Option.isNone(found)) return yield* new SessionStore.SessionNotFoundError({ sessionId }); - return toSessionInfo(yield* found.value.info); -}); +const threadInfo = (threadId: ThreadSchema.ID) => Session.threadInfo(threadId).pipe(Effect.map(toThreadInfo)); // RpcServer's request span has no level, so it is Debug and drops every Info span under it. // Provisioning a sandbox is worth exporting, so it starts a trace of its own. @@ -93,6 +102,9 @@ export const layer = Contract.Api.toLayer( const control = yield* Control.Service; const feed = yield* EventFeed.Service; const sessions = yield* SessionStore.Service; + const threads = yield* ThreadStore.Service; + const resolve = (ref: Control.ThreadRef) => + control.resolve(ref).pipe(Effect.map((thread) => ThreadSchema.ID.make(thread.id))); const state = yield* State.Service; const oauth = yield* OAuth.Service; const identity = yield* Identity.Service; @@ -119,7 +131,7 @@ export const layer = Contract.Api.toLayer( ...(project === undefined ? {} : { project }), ...runtimeInput(runtime), }).pipe(Effect.onError(() => releaseOnFailure(selection))); - return toSessionInfo(yield* handle.info); + return toThreadInfo(yield* handle.main.info); }), "model.list": Effect.fnUntraced(function* ({ hostDir }) { const filter = hostDir === undefined ? {} : { hostDir }; @@ -162,27 +174,87 @@ export const layer = Contract.Api.toLayer( "plugin.reload": Effect.fnUntraced(function* () { return yield* state.reload; }), - "session.list": Effect.fnUntraced(function* () { - const rows = yield* sessions.list(); - return yield* Effect.forEach(rows, (row) => sessionInfo(row.id).pipe(Effect.orDie)); + "session.link": Effect.fnUntraced(function* ({ sessionId, hostDir }) { + const handle = yield* Session.link({ sessionId, hostDir: hostDir ?? null }); + return toSessionInfo(yield* handle.info); }), - "session.configure": Effect.fnUntraced(function* ({ sessionId, runtime }) { - const handle = yield* Session.attach({ sessionId, ...runtimeInput(runtime) }); + "session.relink": Effect.fnUntraced(function* ({ sessionId, threadId, sandbox, directory }) { + const selection = + sandbox === undefined ? undefined : yield* exported("Server.session.relink", Sandbox.resolve(sandbox)); + const selected = selection?.info; + const handle = yield* Session.relink({ + sessionId, + ...(threadId === undefined ? {} : { threadId }), + ...(selected === undefined ? {} : { sandbox: selected }), + ...(directory === undefined ? {} : { directory }), + }).pipe(Effect.onError(() => releaseOnFailure(selection))); return toSessionInfo(yield* handle.info); }), - "session.info": Effect.fnUntraced(function* ({ sessionId }) { - return yield* sessionInfo(sessionId); + "session.delete": Effect.fnUntraced(function* ({ sessionId }) { + return { deleted: yield* Session.remove(sessionId) }; + }), + "session.interrupt": Effect.fnUntraced(function* ({ sessionId }) { + // Ack only: the wire answers "was there work to stop", and the caller + // uses `thread.wait` when it needs cleanup to have finished. + return { interrupted: yield* control.interruptSession(sessionId, { reason: "user" }) }; }), - "session.history": Effect.fnUntraced(function* ({ sessionId }) { - if (Option.isNone(yield* sessions.get(sessionId))) { - return yield* new SessionStore.SessionNotFoundError({ sessionId }); + "session.log": ({ sessionId, after, follow }) => + Stream.unwrap( + Effect.gen(function* () { + if (Option.isNone(yield* sessions.get(sessionId))) { + return yield* new SessionStore.SessionNotFoundError({ sessionId }); + } + return events + .log({ + aggregateId: sessionId, + follow: follow ?? false, + ...(after === undefined ? {} : { after }), + }) + .pipe(Stream.mapEffect(toLogItem(registry.get))); + }), + ), + "thread.list": Effect.fnUntraced(function* ({ sessionId }) { + const rows = yield* threads.list(sessionId); + return yield* Effect.forEach(rows, (row) => threadInfo(ThreadSchema.ID.make(row.id)).pipe(Effect.orDie)); + }), + "thread.info": Effect.fnUntraced(function* (ref) { + return yield* threadInfo(yield* resolve(ref)); + }), + "thread.configure": Effect.fnUntraced(function* ({ runtime, ...ref }) { + const threadId = yield* resolve(ref); + const change = { + ...(runtime.model === undefined ? {} : { model: runtime.model }), + ...(runtime.thinkingLevel === undefined ? {} : { thinkingLevel: runtime.thinkingLevel }), + }; + if (Object.keys(change).length > 0) { + yield* events.publish(EventList.ThreadConfigured, { + sessionId: ref.sessionId, + threadId, + timestamp: yield* DateTime.now, + change, + }); } - const path = yield* sessions.path(sessionId); - const entries = path.flatMap(({ entry, parts }): ReadonlyArray => + return yield* threadInfo(threadId); + }), + "thread.fork": Effect.fnUntraced(function* ({ entryId, mode, ...ref }) { + const forked = yield* threads.fork({ + threadId: yield* resolve(ref), + entryId, + ...(mode === undefined ? {} : { mode }), + }); + return { + thread: yield* threadInfo(ThreadSchema.ID.make(forked.thread.id)), + ...(forked.text === undefined ? {} : { text: forked.text }), + }; + }), + "thread.history": Effect.fnUntraced(function* (ref) { + const line = yield* threads.line(yield* resolve(ref)); + const entries = line.flatMap(({ entry, parts }): ReadonlyArray => entry.type === "user" || entry.type === "assistant" ? [ { id: entry.id, + threadId: entry.threadId, seq: entry.seq, role: entry.type, state: entry.state, @@ -196,11 +268,8 @@ export const layer = Contract.Api.toLayer( ); return { entries }; }), - "session.queue": Effect.fnUntraced(function* ({ sessionId }) { - if (Option.isNone(yield* sessions.get(sessionId))) { - return yield* new SessionStore.SessionNotFoundError({ sessionId }); - } - const pending = yield* control.pending(sessionId); + "thread.queue": Effect.fnUntraced(function* (ref) { + const pending = yield* control.pending(yield* resolve(ref)); return { entries: pending.map((input) => ({ id: input.id, @@ -210,73 +279,38 @@ export const layer = Contract.Api.toLayer( })), }; }), - "session.log": ({ sessionId, after, follow }) => - Stream.unwrap( - Effect.gen(function* () { - if (Option.isNone(yield* sessions.get(sessionId))) { - return yield* new SessionStore.SessionNotFoundError({ sessionId }); - } - return events - .log({ - aggregateId: sessionId, - follow: follow ?? false, - ...(after === undefined ? {} : { after }), - }) - .pipe(Stream.mapEffect(toLogItem(registry.get))); - }), - ), - "session.link": Effect.fnUntraced(function* ({ sessionId, hostDir }) { - const handle = yield* Session.link({ sessionId, hostDir: hostDir ?? null }); - return toSessionInfo(yield* handle.info); - }), - "session.relink": Effect.fnUntraced(function* ({ sessionId, sandbox, directory }) { - const selection = - sandbox === undefined ? undefined : yield* exported("Server.session.relink", Sandbox.resolve(sandbox)); - const selected = selection?.info; - const handle = yield* Session.relink({ - sessionId, - ...(selected === undefined ? {} : { sandbox: selected }), - ...(directory === undefined ? {} : { directory }), - }).pipe(Effect.onError(() => releaseOnFailure(selection))); - return toSessionInfo(yield* handle.info); - }), - "session.prompt": Effect.fnUntraced(function* ({ sessionId, text, delivery, id }) { - yield* control.prompt({ - sessionId, + "thread.prompt": Effect.fnUntraced(function* ({ text, delivery, id, ...ref }) { + const admitted = yield* control.prompt({ + ...ref, prompt: PromptSchema.Prompt.fromText(text), ...(delivery === undefined ? {} : { delivery }), ...(id === undefined ? {} : { id }), }); + return { threadId: admitted.threadId }; }), - "session.interrupt": Effect.fnUntraced(function* ({ sessionId }) { + "thread.interrupt": Effect.fnUntraced(function* (ref) { // Ack only: the wire answers "was there work to stop", and the caller - // uses `session.wait` when it needs cleanup to have finished. - return { interrupted: yield* control.interrupt(sessionId, { reason: "user" }) }; + // uses `thread.wait` when it needs cleanup to have finished. + return { interrupted: yield* control.interrupt(yield* resolve(ref), { reason: "user" }) }; }), - "session.cancel": Effect.fnUntraced(function* ({ sessionId, messageId }) { - if (Option.isNone(yield* sessions.get(sessionId))) { - return yield* new SessionStore.SessionNotFoundError({ sessionId }); - } - return { cancelled: yield* control.cancel(sessionId, messageId) }; + "thread.cancel": Effect.fnUntraced(function* ({ messageId, ...ref }) { + return { cancelled: yield* control.cancel(yield* resolve(ref), messageId) }; }), - "session.update": Effect.fnUntraced(function* ({ sessionId, messageId, delivery, text }) { - if (Option.isNone(yield* sessions.get(sessionId))) { - return yield* new SessionStore.SessionNotFoundError({ sessionId }); - } - const updated = yield* control.update(sessionId, messageId, { + "thread.update": Effect.fnUntraced(function* ({ messageId, delivery, text, ...ref }) { + const updated = yield* control.update(yield* resolve(ref), messageId, { ...(delivery === undefined ? {} : { delivery }), ...(text === undefined ? {} : { prompt: PromptSchema.Prompt.fromText(text) }), }); return { updated }; }), - "session.message": Effect.fnUntraced(function* ({ sessionId, messageId }) { - const found = yield* sessions.entry(messageId); - // Scoped to the session the caller named: an entry id is global, and a - // client should not learn about conversations it did not ask for. - return { found: Option.isSome(found) && found.value.entry.sessionId === sessionId }; + "thread.message": Effect.fnUntraced(function* ({ messageId, ...ref }) { + // Scoped to the thread's line: an entry id is global, and a client should not learn + // about conversations it did not ask for. An inherited prompt is in the thread's. + const line = yield* threads.line(yield* resolve(ref)); + return { found: line.some(({ entry }) => entry.id === messageId) }; }), - "session.wait": Effect.fnUntraced(function* ({ sessionId }) { - yield* control.wait(sessionId); + "thread.wait": Effect.fnUntraced(function* (ref) { + yield* control.wait(yield* resolve(ref)); }), "sandbox.drivers": Effect.fnUntraced(function* () { const drivers = yield* Sandbox.drivers(); diff --git a/packages/codework/test/__artifacts__/server.fork.json b/packages/codework/test/__artifacts__/server.fork.json new file mode 100644 index 00000000..7b8ff245 --- /dev/null +++ b/packages/codework/test/__artifacts__/server.fork.json @@ -0,0 +1,106 @@ +{ + "replies": [ + "response 1", + "response 2" + ], + "fork": { + "at": { + "origin": { + "kind": "fork", + "threadId": "
", + "entryId": "
" + }, + "session": "" + }, + "before": { + "origin": { + "kind": "fork", + "threadId": "
", + "entryId": "
" + }, + "text": "branch" + } + }, + "listed": [ + { + "id": "", + "kind": "fork", + "title": "branch" + }, + { + "id": "", + "kind": "fork" + }, + { + "id": "
", + "kind": "main", + "title": "hello" + } + ], + "histories": { + "main": [ + { + "thread": "
", + "role": "user", + "state": "committed", + "text": "hello" + }, + { + "thread": "
", + "role": "assistant", + "state": "committed", + "text": "response 1" + } + ], + "at": [ + { + "thread": "
", + "role": "user", + "state": "committed", + "text": "hello" + }, + { + "thread": "
", + "role": "assistant", + "state": "committed", + "text": "response 1" + }, + { + "thread": "", + "role": "user", + "state": "committed", + "text": "branch" + }, + { + "thread": "", + "role": "assistant", + "state": "committed", + "text": "response 2" + } + ], + "before": [ + { + "thread": "
", + "role": "user", + "state": "committed", + "text": "hello" + }, + { + "thread": "
", + "role": "assistant", + "state": "committed", + "text": "response 1" + } + ] + }, + "deleted": { + "deleted": true + }, + "afterDelete": { + "threads": 0, + "info": "SessionNotFoundError", + "again": { + "deleted": false + } + } +} diff --git a/packages/codework/test/cli.sandbox.live.test.ts b/packages/codework/test/cli.sandbox.live.test.ts index 6ed20079..baf86748 100644 --- a/packages/codework/test/cli.sandbox.live.test.ts +++ b/packages/codework/test/cli.sandbox.live.test.ts @@ -69,11 +69,11 @@ describe.each(DRIVERS)("created $driver sandbox through the CLI", (live) => { const db = new DatabaseSync(database); try { const sandboxes = db - .prepare("SELECT driver, kind, ownership, status, provider_status, last_error FROM sandbox_instance") + .prepare("SELECT driver, kind, ownership, status, provider_status, last_error FROM sandbox") .all() .map((row) => ({ ...row })); const bash = db - .prepare("SELECT status FROM session_entry_part WHERE type = 'toolCall' AND tool_name = 'bash'") + .prepare("SELECT status FROM part WHERE type = 'toolCall' AND tool_name = 'bash'") .all() .map((row) => String(row["status"])); expect(bash).toContain("completed"); diff --git a/packages/codework/test/cli.search.live.test.ts b/packages/codework/test/cli.search.live.test.ts index e123bd9d..d84f7595 100644 --- a/packages/codework/test/cli.search.live.test.ts +++ b/packages/codework/test/cli.search.live.test.ts @@ -97,7 +97,7 @@ const run = (live: (typeof LIVE)[number], settings: object | undefined) => { const db = new DatabaseSync(database); try { const rows = db - .prepare("SELECT tool_name, status FROM session_entry_part WHERE type = 'toolCall'") + .prepare("SELECT tool_name, status FROM part WHERE type = 'toolCall'") .all() .map((row) => ({ tool_name: String(row["tool_name"]), status: String(row["status"]) })); return { diff --git a/packages/codework/test/feed.test.ts b/packages/codework/test/feed.test.ts index 7dbe3b88..41870fd5 100644 --- a/packages/codework/test/feed.test.ts +++ b/packages/codework/test/feed.test.ts @@ -22,7 +22,11 @@ const settled = (): EventSchema.Payload => ({ id: EventSchema.ID.create(), type: EventList.ExecutionSucceeded.type, - data: { sessionId: Session.SessionSchema.ID.create(), timestamp: DateTime.makeUnsafe(0) }, + data: { + sessionId: Session.SessionSchema.ID.create(), + threadId: Session.ThreadSchema.ID.create(), + timestamp: DateTime.makeUnsafe(0), + }, }) as EventSchema.Payload; describe("EventFeed", () => { diff --git a/packages/codework/test/plugin.cmd.test.ts b/packages/codework/test/plugin.cmd.test.ts index 9f6a0648..d3fb5ee9 100644 --- a/packages/codework/test/plugin.cmd.test.ts +++ b/packages/codework/test/plugin.cmd.test.ts @@ -121,9 +121,9 @@ const exchanges = async ( for (const hostDir of hostDirs) { const session = sessions.get(hostDir) ?? (yield* Session.create({ hostDir })); sessions.set(hostDir, session); - yield* session.prompt({ text: "go", delivery: "followUp" }); - yield* session.resume(); - yield* session.wait(); + yield* session.main.prompt({ text: "go", delivery: "followUp" }); + yield* session.main.resume(); + yield* session.main.wait(); } }).pipe( Effect.provide( diff --git a/packages/codework/test/sea.test.ts b/packages/codework/test/sea.test.ts index 3381b831..4b56c055 100644 --- a/packages/codework/test/sea.test.ts +++ b/packages/codework/test/sea.test.ts @@ -145,7 +145,7 @@ describe.skipIf(!existsSync(binary))("the standalone binary", () => { ); const db = new DatabaseSync(database); const completed = db - .prepare("SELECT tool_name FROM session_entry_part WHERE type = 'toolCall' AND status = 'completed'") + .prepare("SELECT tool_name FROM part WHERE type = 'toolCall' AND status = 'completed'") .all() .map((row) => String(row["tool_name"])); db.close(); @@ -220,7 +220,7 @@ describe.skipIf(!existsSync(binary))("the standalone binary", () => { ); const db = new DatabaseSync(database); const completed = db - .prepare("SELECT tool_name FROM session_entry_part WHERE type = 'toolCall' AND status = 'completed'") + .prepare("SELECT tool_name FROM part WHERE type = 'toolCall' AND status = 'completed'") .all() .map((row) => String(row["tool_name"])); db.close(); diff --git a/packages/codework/test/server.test.ts b/packages/codework/test/server.test.ts index 3fb2a721..f6e5f460 100644 --- a/packages/codework/test/server.test.ts +++ b/packages/codework/test/server.test.ts @@ -76,17 +76,27 @@ describe("server", () => { expect(drivers.map(({ name }) => name)).toEqual(expect.arrayContaining(["memory", "sqldb"])); }).pipe(Effect.scoped, Effect.provide(layer()), Effect.runPromise)); - it("session.create + session.info + session.list round-trip", () => + it("session.create + thread.info + thread.list round-trip", () => Effect.gen(function* () { const rpc = yield* RpcTest.makeClient(Contract.Api); const created = yield* rpc["session.create"]({}); - expect(created.id.startsWith("ses")).toBe(true); + expect(created.session.id.startsWith("ses")).toBe(true); + expect(created.id.startsWith("thr")).toBe(true); + expect(created.session.mainThreadId).toBe(created.id); + expect(created.origin).toEqual({ kind: "main" }); + expect(created.title).toBeUndefined(); - const info = yield* rpc["session.info"]({ sessionId: created.id }); + const info = yield* rpc["thread.info"]({ sessionId: created.session.id }); expect(info.id).toBe(created.id); + expect((yield* rpc["thread.info"]({ sessionId: created.session.id, threadId: created.id })).id).toBe( + created.id, + ); - const listed = yield* rpc["session.list"]({}); + const listed = yield* rpc["thread.list"]({}); expect(listed.some(({ id }) => id === created.id)).toBe(true); + expect((yield* rpc["thread.list"]({ sessionId: created.session.id })).map(({ id }) => id)).toEqual([ + created.id, + ]); }).pipe(Effect.scoped, Effect.provide(layer()), Effect.runPromise)); it("uses the harness hostCwd for a local session when the client omits directory", () => { @@ -95,7 +105,7 @@ describe("server", () => { return Effect.gen(function* () { const rpc = yield* RpcTest.makeClient(Contract.Api); const created = yield* rpc["session.create"]({}); - expect(created.directory).toBe(hostCwd); + expect(created.session.directory).toBe(hostCwd); }).pipe(Effect.scoped, Effect.provide(layer({ hostCwd })), Effect.runPromise); }); @@ -108,13 +118,13 @@ describe("server", () => { // Passed through as given. The client is the owner of this machine, so naming a host // path is no more privilege than running `codework` in it. const placed = yield* rpc["session.create"]({ hostDir: Session.AbsolutePath.make(root) }); - expect(placed.hostDir).toBe(root); - expect((yield* rpc["session.info"]({ sessionId: placed.id })).hostDir).toBe(root); + expect(placed.session.hostDir).toBe(root); + expect((yield* rpc["thread.info"]({ sessionId: placed.session.id })).session.hostDir).toBe(root); // And nothing fills it in: a session the client did not place has no host project, // rather than quietly adopting the server's own startup directory. const unplaced = yield* rpc["session.create"]({}); - expect(unplaced.hostDir).toBeUndefined(); + expect(unplaced.session.hostDir).toBeUndefined(); }).pipe(Effect.scoped, Effect.provide(layer()), Effect.runPromise); }); @@ -125,18 +135,18 @@ describe("server", () => { return Effect.gen(function* () { const rpc = yield* RpcTest.makeClient(Contract.Api); const created = yield* rpc["session.create"]({}); - expect(created.hostDir).toBeUndefined(); + expect(created.session.hostDir).toBeUndefined(); const linked = yield* rpc["session.link"]({ - sessionId: created.id, + sessionId: created.session.id, hostDir: Session.AbsolutePath.make(root), }); expect(linked.hostDir).toBe(root); // Stored, not held in the call: a later reader sees it too. - expect((yield* rpc["session.info"]({ sessionId: created.id })).hostDir).toBe(root); + expect((yield* rpc["thread.info"]({ sessionId: created.session.id })).session.hostDir).toBe(root); // Omitting the field clears the anchor, leaving the session with no host directory. - const cleared = yield* rpc["session.link"]({ sessionId: created.id }); + const cleared = yield* rpc["session.link"]({ sessionId: created.session.id }); expect(cleared.hostDir).toBeUndefined(); }).pipe(Effect.scoped, Effect.provide(layer()), Effect.runPromise); }); @@ -150,15 +160,15 @@ describe("server", () => { return Effect.gen(function* () { const rpc = yield* RpcTest.makeClient(Contract.Api); const created = yield* rpc["session.create"]({}); - const failure = yield* linkedDirectory(created.id, Option.some(home)).pipe(Effect.flip); + const failure = yield* linkedDirectory(created.session.id, Option.some(home)).pipe(Effect.flip); expect(failure).toMatchObject({ _tag: "SessionNotLinkedError", reason: "session-not-linked" }); // Linked, the same lookup answers with the directory. yield* rpc["session.link"]({ - sessionId: created.id, + sessionId: created.session.id, hostDir: Session.AbsolutePath.make(realpathSync(home)), }); - expect(yield* linkedDirectory(created.id, Option.some(home))).toBe(realpathSync(home)); + expect(yield* linkedDirectory(created.session.id, Option.some(home))).toBe(realpathSync(home)); }).pipe( Effect.scoped, // A file database, because the lookup opens its own connection the way the CLI does. @@ -183,7 +193,7 @@ describe("server", () => { * it was fixed: `my-project` came back as `/my-project`. */ yield* rpc["session.link"]({ - sessionId: created.id, + sessionId: created.session.id, // Deliberately bypass the client type to prove the wire decoder rejects malformed input. hostDir: "my-project" as Session.AbsolutePath, }); @@ -211,7 +221,7 @@ describe("server", () => { return Effect.gen(function* () { const rpc = yield* RpcTest.makeClient(Contract.Api); const created = yield* rpc["session.create"]({}); - yield* rpc["session.link"]({ sessionId: created.id, hostDir: Session.AbsolutePath.make(project) }); + yield* rpc["session.link"]({ sessionId: created.session.id, hostDir: Session.AbsolutePath.make(project) }); /* * `--session` exists for a server or a UI acting on a session's behalf, so the shell's @@ -224,7 +234,17 @@ describe("server", () => { yield* Effect.promise(() => exec( process.execPath, - ["--conditions=development", cli, "plugin", "add", "./tool", "--session", created.id, "--home", home], + [ + "--conditions=development", + cli, + "plugin", + "add", + "./tool", + "--session", + created.session.id, + "--home", + home, + ], { cwd: elsewhere, }, @@ -250,13 +270,21 @@ describe("server", () => { expect(reloaded.failure).toBeUndefined(); }).pipe(Effect.scoped, Effect.provide(layer()), Effect.runPromise)); - it("session.info fails with SessionNotFoundError for a bogus id", () => + it("thread.info fails with SessionNotFoundError for a bogus session and ThreadNotFoundError for a bogus thread", () => Effect.gen(function* () { const rpc = yield* RpcTest.makeClient(Contract.Api); - const failure = yield* rpc["session.info"]({ + const failure = yield* rpc["thread.info"]({ sessionId: Session.SessionSchema.ID.ascending("ses_nope"), }).pipe(Effect.flip); expect(failure._tag).toBe("SessionNotFoundError"); + + const other = yield* rpc["session.create"]({}); + const created = yield* rpc["session.create"]({}); + // A real thread of another session is still not this session's. + const foreign = yield* rpc["thread.info"]({ sessionId: created.session.id, threadId: other.id }).pipe( + Effect.flip, + ); + expect(foreign._tag).toBe("ThreadNotFoundError"); }).pipe(Effect.scoped, Effect.provide(layer()), Effect.runPromise)); it("session.create and session.relink report invalid directories", () => { @@ -287,7 +315,7 @@ describe("server", () => { const session = yield* rpc["session.create"]({ directory: root }); const relinkError = yield* rpc["session.relink"]({ - sessionId: session.id, + sessionId: session.session.id, directory: "missing", sandbox: { id: sandbox.id }, }).pipe(Effect.flip); @@ -309,13 +337,13 @@ describe("server", () => { ); const session = yield* rpc["session.create"]({}); - yield* rpc["session.prompt"]({ sessionId: session.id, text: "hello" }); + yield* rpc["thread.prompt"]({ sessionId: session.session.id, text: "hello" }); const envelope = yield* Fiber.join(admitted).pipe(Effect.timeout("10 seconds")); expect(Option.isSome(envelope)).toBe(true); const decoded = yield* Envelope.decode(Option.getOrThrow(envelope)); expect(decoded.type).toBe(EventList.PromptAdmitted.type); - expect((decoded.data as { readonly sessionId?: string }).sessionId).toBe(session.id); + expect(decoded.data).toMatchObject({ sessionId: session.session.id, threadId: session.id }); }).pipe(Effect.scoped, Effect.provide(layer()), Effect.runPromise)); it("skips plugin events while keeping the subscription alive", () => @@ -329,6 +357,7 @@ describe("server", () => { }); yield* events.publish(EventList.ExecutionSucceeded, { sessionId: Session.SessionSchema.ID.create(), + threadId: Session.ThreadSchema.ID.create(), timestamp: yield* DateTime.now, }); expect((yield* Queue.take(queue)).type).toBe(EventList.ExecutionSucceeded.type); @@ -364,9 +393,9 @@ describe("server", () => { const sandbox = yield* rpc["sandbox.create"]({ driver: "memory" }); const first = yield* rpc["session.create"]({ sandbox: { id: sandbox.id } }); const second = yield* rpc["session.create"]({ sandbox: { id: sandbox.id } }); - expect(first.sandbox?.id).toBe(sandbox.id); - expect(second.sandbox?.id).toBe(sandbox.id); - const relinked = yield* rpc["session.relink"]({ sessionId: first.id, sandbox: { id: sandbox.id } }); + expect(first.session.sandbox?.id).toBe(sandbox.id); + expect(second.session.sandbox?.id).toBe(sandbox.id); + const relinked = yield* rpc["session.relink"]({ sessionId: first.session.id, sandbox: { id: sandbox.id } }); expect(relinked.sandbox?.id).toBe(sandbox.id); yield* rpc["session.relink"]({ sessionId: Session.SessionSchema.ID.create(), @@ -404,7 +433,7 @@ describe("server", () => { return Effect.gen(function* () { const rpc = yield* RpcTest.makeClient(Contract.Api); const projectOf = (sessionId: Session.SessionSchema.ID) => - rpc["session.info"]({ sessionId }).pipe(Effect.map((info) => info.project)); + rpc["thread.info"]({ sessionId }).pipe(Effect.map((info) => info.session.project)); const nameOf = (sessionId: Session.SessionSchema.ID) => projectOf(sessionId).pipe(Effect.map((project) => project?.name)); @@ -433,8 +462,8 @@ describe("server", () => { const shared = dir("shared"); const first = yield* rpc["session.create"]({ directory: shared, project: { name: "A" } }); const second = yield* rpc["session.create"]({ directory: shared, project: { name: "B" } }); - const firstProject = yield* projectOf(first.id); - const secondProject = yield* projectOf(second.id); + const firstProject = yield* projectOf(first.session.id); + const secondProject = yield* projectOf(second.session.id); expect(secondProject?.id).toBe(firstProject?.id); // 5. Rename shows through every session of the project. @@ -443,7 +472,7 @@ describe("server", () => { expect(missing).toMatchObject({ _tag: "ProjectNotFoundError", projectId: "prj_missing" }); // 6. Invalid names are refused at the edge and create nothing. - const before = (yield* rpc["session.list"]({})).length; + const before = (yield* rpc["thread.list"]({})).length; const outcome = (exit: Exit.Exit) => Exit.isFailure(exit) && String(exit.cause).includes("Schema validation failed") ? "rejected" : String(exit); const invalid: Record = {}; @@ -459,13 +488,13 @@ describe("server", () => { rename: outcome(rename), }; } - const after = (yield* rpc["session.list"]({})).length; + const after = (yield* rpc["thread.list"]({})).length; expect(after).toBe(before); const artifact = { - memory: { named: yield* nameOf(memoryNamed.id), unnamed: yield* nameOf(memoryUnnamed.id) }, - folder: { named: yield* nameOf(folderNamed.id), unnamed: yield* nameOf(folderUnnamed.id) }, - repo: { named: yield* nameOf(repoNamed.id), unnamed: yield* nameOf(repoUnnamed.id) }, + memory: { named: yield* nameOf(memoryNamed.session.id), unnamed: yield* nameOf(memoryUnnamed.session.id) }, + folder: { named: yield* nameOf(folderNamed.session.id), unnamed: yield* nameOf(folderUnnamed.session.id) }, + repo: { named: yield* nameOf(repoNamed.session.id), unnamed: yield* nameOf(repoUnnamed.session.id) }, existing: { first: firstProject?.name, second: secondProject?.name, @@ -473,8 +502,8 @@ describe("server", () => { }, rename: { result: { sameId: renamed.id === firstProject?.id, name: renamed.name }, - first: yield* nameOf(first.id), - second: yield* nameOf(second.id), + first: yield* nameOf(first.session.id), + second: yield* nameOf(second.session.id), missing: { _tag: missing._tag, projectId: missing.projectId }, }, invalid, @@ -591,12 +620,12 @@ describe("server", () => { ); }); - it("session.message reports an unknown prompt as absent", () => + it("thread.message reports an unknown prompt as absent", () => Effect.gen(function* () { const rpc = yield* RpcTest.makeClient(Contract.Api); const session = yield* rpc["session.create"]({}); - const found = yield* rpc["session.message"]({ - sessionId: session.id, + const found = yield* rpc["thread.message"]({ + sessionId: session.session.id, messageId: Session.SessionMessageSchema.ID.create(), }); expect(found.found).toBe(false); @@ -782,18 +811,18 @@ describe("WebSocket client", () => { const session = yield* rpc["session.create"]({}); const linked = yield* Effect.promise(() => - execCli(["session", "link", session.id, project, "--server", url], project), + execCli(["session", "link", session.session.id, project, "--server", url], project), ); expect(linked.status).toBe(0); - expect(linked.stdout).toContain(`Linked ${session.id} to ${project}`); - expect((yield* rpc["session.info"]({ sessionId: session.id })).hostDir).toBe(project); + expect(linked.stdout).toContain(`Linked ${session.session.id} to ${project}`); + expect((yield* rpc["thread.info"]({ sessionId: session.session.id })).session.hostDir).toBe(project); const unlinked = yield* Effect.promise(() => - execCli(["session", "link", session.id, "--unlink", "--server", url], project), + execCli(["session", "link", session.session.id, "--unlink", "--server", url], project), ); expect(unlinked.status).toBe(0); - expect(unlinked.stdout).toContain(`Unlinked ${session.id}`); - expect((yield* rpc["session.info"]({ sessionId: session.id })).hostDir).toBeUndefined(); + expect(unlinked.stdout).toContain(`Unlinked ${session.session.id}`); + expect((yield* rpc["thread.info"]({ sessionId: session.session.id })).session.hostDir).toBeUndefined(); }).pipe(Effect.scoped, Effect.provide(websocket()), Effect.timeout("20 seconds"), Effect.runPromise); }); @@ -834,28 +863,121 @@ describe("WebSocket client", () => { Effect.sync(() => { if (Schema.is(EventList.LLMTextDelta)(event)) text.push(event.data.delta); }); - yield* Client.run(rpc, { sessionId: session.id, text: "hello" }, render); - yield* Client.run(rpc, { sessionId: session.id, text: "again" }, render); + const ref = { sessionId: session.session.id, threadId: session.id }; + yield* Client.run(rpc, { ...ref, text: "hello" }, render); + yield* Client.run(rpc, { ...ref, text: "again" }, render); expect(text).toEqual(["response 1", "response 2"]); }).pipe(Effect.scoped, Effect.provide(websocket()), Effect.timeout("10 seconds"), Effect.runPromise)); - it("session.message finds a prompt that ran, scoped to its own session", () => + it("thread.message finds a prompt that ran, scoped to its own thread", () => Effect.gen(function* () { const rpc = yield* connectedClient; const session = yield* rpc["session.create"]({ runtime: { model: { provider: "openai", id: "gpt-6-luna" } } }); const messageId = Session.SessionMessageSchema.ID.create(); - yield* rpc["session.prompt"]({ sessionId: session.id, text: "hello", id: messageId }); - yield* rpc["session.wait"]({ sessionId: session.id }); + yield* rpc["thread.prompt"]({ sessionId: session.session.id, text: "hello", id: messageId }); + yield* rpc["thread.wait"]({ sessionId: session.session.id }); // This is the reconcile a client falls back on when the stream let it down. - expect((yield* rpc["session.message"]({ sessionId: session.id, messageId })).found).toBe(true); + expect((yield* rpc["thread.message"]({ sessionId: session.session.id, messageId })).found).toBe(true); // Entry ids are global; the answer is not. Another session never sees it. const other = yield* rpc["session.create"]({}); - expect((yield* rpc["session.message"]({ sessionId: other.id, messageId })).found).toBe(false); + expect((yield* rpc["thread.message"]({ sessionId: other.session.id, messageId })).found).toBe(false); }).pipe(Effect.scoped, Effect.provide(websocket()), Effect.timeout("10 seconds"), Effect.runPromise)); + it("forks a thread, runs both, and deletes the session with every thread", () => + Effect.gen(function* () { + const rpc = yield* connectedClient; + const main = yield* rpc["session.create"]({ runtime: { model: { provider: "openai", id: "gpt-6-luna" } } }); + const sessionId = main.session.id; + const text = (event: EventSchema.Payload) => + Schema.is(EventList.LLMTextDelta)(event) ? [event.data.delta] : []; + const replies: string[] = []; + const render = (event: EventSchema.Payload) => Effect.sync(() => replies.push(...text(event))); + + yield* Client.run(rpc, { sessionId, threadId: main.id, text: "hello" }, render); + const assistant = (yield* rpc["thread.history"]({ sessionId })).entries.at(-1); + if (assistant === undefined) return yield* Effect.die("main thread has no exchange"); + + const at = yield* rpc["thread.fork"]({ sessionId, entryId: assistant.id }); + yield* Client.run(rpc, { sessionId, threadId: at.thread.id, text: "branch" }, render); + const branched = (yield* rpc["thread.history"]({ sessionId, threadId: at.thread.id })).entries.at(-2); + if (branched === undefined) return yield* Effect.die("fork has no exchange"); + // Editing the fork's own prompt again: a fork of a fork, through the inherited line. + const before = yield* rpc["thread.fork"]({ + sessionId, + threadId: at.thread.id, + entryId: branched.id, + mode: "before", + }); + + const labels = new Map([ + [sessionId, ""], + [main.id, "
"], + [at.thread.id, ""], + [before.thread.id, ""], + [assistant.id, "
"], + [branched.id, ""], + ]); + const label = (id: string) => labels.get(id) ?? id; + const origin = (info: typeof main) => + info.origin.kind === "fork" + ? { kind: info.origin.kind, threadId: label(info.origin.threadId), entryId: label(info.origin.entryId) } + : info.origin; + const history = (threadId: Session.ThreadSchema.ID) => + rpc["thread.history"]({ sessionId, threadId }).pipe( + Effect.map(({ entries }) => + entries.map((entry) => ({ + thread: label(entry.threadId), + role: entry.role, + state: entry.state, + text: entry.parts.flatMap((part) => (part.type === "text" ? [part.text] : [])).join(""), + })), + ), + ); + const listed = yield* rpc["thread.list"]({ sessionId }); + const histories = { + main: yield* history(main.id), + at: yield* history(at.thread.id), + before: yield* history(before.thread.id), + }; + + const deleted = yield* rpc["session.delete"]({ sessionId }); + const afterDelete = { + threads: (yield* rpc["thread.list"]({ sessionId })).length, + info: (yield* rpc["thread.info"]({ sessionId }).pipe(Effect.flip))._tag, + again: yield* rpc["session.delete"]({ sessionId }), + }; + + const artifact = { + replies, + fork: { + at: { origin: origin(at.thread), text: at.text, session: label(at.thread.session.id) }, + before: { origin: origin(before.thread), text: before.text }, + }, + listed: listed + .map(({ id, origin, title }) => ({ id: label(id), kind: origin.kind, title })) + .sort((a, b) => a.id.localeCompare(b.id)), + histories, + deleted, + afterDelete, + }; + expect(replies).toEqual(["response 1", "response 2"]); + expect(at.thread.origin).toEqual({ kind: "fork", threadId: main.id, entryId: assistant.id }); + expect(before.text).toBe("branch"); + expect(histories.at.map(({ thread }) => thread)).toEqual(["
", "
", "", ""]); + expect(histories.main).toHaveLength(2); + expect(histories.before).toEqual(histories.main); + expect(deleted).toEqual({ deleted: true }); + expect(afterDelete).toEqual({ threads: 0, info: "SessionNotFoundError", again: { deleted: false } }); + return artifact; + }) + .pipe(Effect.scoped, Effect.provide(websocket()), Effect.timeout("10 seconds"), Effect.runPromise) + .then((artifact) => + expect(`${JSON.stringify(artifact, null, "\t")}\n`).toMatchFileSnapshot("./__artifacts__/server.fork.json"), + )); + it("does not adopt a failure that belongs to another client's prompt", () => Effect.gen(function* () { const firstEntered = yield* Deferred.make(); @@ -873,8 +995,8 @@ describe("WebSocket client", () => { expect((yield* Queue.take(queue)).type).toBe(Envelope.Connected.type); // Someone else's prompt owns the drain, and it is going to fail. - yield* rpc["session.prompt"]({ - sessionId: session.id, + yield* rpc["thread.prompt"]({ + sessionId: session.session.id, text: "theirs", id: Session.SessionMessageSchema.ID.create(), }); @@ -882,9 +1004,11 @@ describe("WebSocket client", () => { // Ours is admitted while that drain is still running, so the failure // lands after our admission but before our promotion. - const ours = yield* Client.run(rpc, { sessionId: session.id, text: "ours" }, () => Effect.void).pipe( - Effect.forkChild, - ); + const ours = yield* Client.run( + rpc, + { sessionId: session.session.id, threadId: session.id, text: "ours" }, + () => Effect.void, + ).pipe(Effect.forkChild); let admissions = 0; while (admissions < 2) { if ((yield* Queue.take(queue)).type === EventList.PromptAdmitted.type) admissions += 1; @@ -918,9 +1042,11 @@ describe("WebSocket client", () => { const session = yield* rpc["session.create"]({ runtime: { model: { provider: "missing-test-provider", id: "missing-model" } }, }); - const error = yield* Client.run(rpc, { sessionId: session.id, text: "hello" }, () => Effect.void).pipe( - Effect.flip, - ); + const error = yield* Client.run( + rpc, + { sessionId: session.session.id, threadId: session.id, text: "hello" }, + () => Effect.void, + ).pipe(Effect.flip); expect(error._tag).toBe("Client.ExecutionError"); expect(error.message).toContain("missing-test-provider"); // The category survives the wire, so a client can branch on it. @@ -972,13 +1098,15 @@ describe("WebSocket client", () => { const session = yield* rpc["session.create"]({ runtime: { model: { provider: "openai", id: "gpt-6-luna" } }, }); - const running = yield* Client.run(rpc, { sessionId: session.id, text: "hello" }, () => Effect.void).pipe( - Effect.forkChild, - ); + const running = yield* Client.run( + rpc, + { sessionId: session.session.id, threadId: session.id, text: "hello" }, + () => Effect.void, + ).pipe(Effect.forkChild); yield* Deferred.await(entered); yield* Fiber.interrupt(running); yield* Deferred.succeed(release, undefined); - yield* rpc["session.wait"]({ sessionId: session.id }); + yield* rpc["thread.wait"]({ sessionId: session.session.id }); expect(completed).toBe(true); }); yield* program.pipe( @@ -1005,20 +1133,21 @@ describe("WebSocket client", () => { const session = yield* rpc["session.create"]({ runtime: { model: { provider: "openai", id: "gpt-6-luna" } }, }); - const running = yield* Client.run(rpc, { sessionId: session.id, text: "hello" }, () => Effect.void).pipe( - Effect.flip, - Effect.forkChild, - ); + const running = yield* Client.run( + rpc, + { sessionId: session.session.id, threadId: session.id, text: "hello" }, + () => Effect.void, + ).pipe(Effect.flip, Effect.forkChild); yield* Deferred.await(entered); - expect((yield* rpc["session.interrupt"]({ sessionId: session.id })).interrupted).toBe(true); + expect((yield* rpc["session.interrupt"]({ sessionId: session.session.id })).interrupted).toBe(true); const error = yield* Fiber.join(running); expect(error._tag).toBe("Client.ExecutionError"); // Not Effect's internal "All fibers interrupted without error". expect(error.message).toBe("Session interrupted"); // A user-requested stop, not the shutdown path. if (error._tag === "Client.ExecutionError") expect(error.type).toBe("aborted"); - yield* rpc["session.wait"]({ sessionId: session.id }); - expect((yield* rpc["session.interrupt"]({ sessionId: session.id })).interrupted).toBe(false); + yield* rpc["thread.wait"]({ sessionId: session.session.id }); + expect((yield* rpc["session.interrupt"]({ sessionId: session.session.id })).interrupted).toBe(false); }); yield* program.pipe( Effect.scoped, @@ -1038,15 +1167,15 @@ describe("WebSocket client", () => { const session = yield* rpc["session.create"]({ runtime: { model: { provider: "openai", id: "gpt-6-luna" } }, }); - yield* rpc["session.prompt"]({ sessionId: session.id, text: "hello" }); + yield* rpc["thread.prompt"]({ sessionId: session.session.id, text: "hello" }); yield* Deferred.await(entered); expect( - (yield* rpc["session.interrupt"]({ sessionId: session.id }).pipe(Effect.timeout("1 second"))) + (yield* rpc["session.interrupt"]({ sessionId: session.session.id }).pipe(Effect.timeout("1 second"))) .interrupted, ).toBe(true); expect(cleaned).toBe(false); yield* Deferred.succeed(release, undefined); - yield* rpc["session.wait"]({ sessionId: session.id }); + yield* rpc["thread.wait"]({ sessionId: session.session.id }); expect(cleaned).toBe(true); }); yield* program.pipe( diff --git a/packages/harness/src/context/assemble.ts b/packages/harness/src/context/assemble.ts index b8cefb5a..ac29c868 100644 --- a/packages/harness/src/context/assemble.ts +++ b/packages/harness/src/context/assemble.ts @@ -2,12 +2,12 @@ import { Message } from "@codeworksh/aikit"; import { DateTime, Effect, Schema } from "effect"; import { optional } from "../schema.ts"; import { SessionSchema } from "../session/schema.ts"; -import type { Session } from "../session/session.ts"; +import type { Thread } from "../thread/thread.ts"; import { decodeMessage, decodeSyntheticMessage, type ContextMessage } from "./codec.ts"; import { ContextDecodeError } from "./errors.ts"; export interface Snapshot { - readonly sessionId: string; + readonly threadId: string; readonly leafEntryId: string | null; readonly messages: ReadonlyArray; readonly lastAssistant?: { @@ -25,13 +25,8 @@ const SyntheticData = Schema.fromJsonString( }), ); -const BranchSummaryData = Schema.fromJsonString( - Schema.Struct({ - fromEntryId: Schema.String, - summary: Schema.String, - details: optional(Schema.Unknown), - }), -); +// `handoff`: what the thread carries over the reset, if anything. +const ResetData = Schema.fromJsonString(Schema.Struct({ handoff: optional(Schema.String) })); const CustomData = Schema.fromJsonString( Schema.Struct({ @@ -42,26 +37,23 @@ const CustomData = Schema.fromJsonString( const decodeSyntheticData = Schema.decodeUnknownEffect(SyntheticData); const decodeCompactionData = Schema.decodeUnknownEffect(SessionSchema.CompactionData); -const decodeBranchSummaryData = Schema.decodeUnknownEffect(BranchSummaryData); +const decodeResetData = Schema.decodeUnknownEffect(ResetData); const decodeCustomData = Schema.decodeUnknownEffect(CustomData); -const decodeFailure = (entry: Session.HydratedEntry) => (error: { readonly message: string }) => +const decodeFailure = (entry: Thread.HydratedEntry) => (error: { readonly message: string }) => new ContextDecodeError({ entryId: entry.entry.id, type: entry.entry.type, reason: error.message, }); -// TODO(sanchitrk): -// comeback to this when we've decided on compaction, branch summary, etc. - const compactionText = (summary: string) => `The conversation history before this point was compacted into the following summary:\n\n\n${summary}\n`; -const branchSummaryText = (summary: string) => - `The following is a summary of a branch that this conversation came back from:\n\n\n${summary}\n`; +const resetText = (handoff: string) => + `The conversation was started over. Carried over from before:\n\n\n${handoff}\n`; -const wrapperMessage = (entry: Session.HydratedEntry, text: string): Message.UserMessage => +const wrapperMessage = (entry: Thread.HydratedEntry, text: string): Message.UserMessage => Message.createUserMessage({ messageId: entry.entry.id, role: "user", @@ -69,35 +61,29 @@ const wrapperMessage = (entry: Session.HydratedEntry, text: string): Message.Use parts: [{ type: "text", text }], }); -// assembles entries which builds the LLM context. -// a entry is hybrid data type; with entry types not relevant to the LLMs -// yet stored into the timeline. -// -// NOTE(sanchitrk): -// remove unnecessary cases. comeback to this when in use. -export const assemblePath = Effect.fn("Context.assemblePath")(function* ( - sessionId: string, - path: ReadonlyArray, +/** + * Builds a thread's LLM context from its line (own entries, then inherited ones, in seq order). + * Only committed entries count, and the context starts at the newest `reset` or `compaction`. + * Entries that are not messages (system, custom) stay in the transcript, out of the context. + */ +export const assembleLine = Effect.fn("Context.assembleLine")(function* ( + threadId: string, + line: ReadonlyArray, ): Effect.fn.Return { - const leafEntryId = path.at(-1)?.entry.id ?? null; - const committedPath = path.filter((hydrated) => hydrated.entry.state === "committed"); + const leafEntryId = line.at(-1)?.entry.id ?? null; + const committedPath = line.filter((hydrated) => hydrated.entry.state === "committed"); let lastAssistant: Snapshot["lastAssistant"]; - let latestCompaction: { readonly index: number; readonly data: SessionSchema.CompactionData } | undefined; + let latestStart: + | { readonly index: number; readonly compaction: SessionSchema.CompactionData } + | { readonly index: number; readonly handoff: string | undefined } + | undefined; const messagesByEntry = new Map(); const compactions = new Map(); - const branchSummaries = new Map(); + const handoffs = new Map(); for (const [index, hydrated] of committedPath.entries()) { const { entry } = hydrated; - if (entry.sessionId !== sessionId) { - return yield* new ContextDecodeError({ - entryId: entry.id, - type: entry.type, - reason: `entry belongs to session ${entry.sessionId}, not ${sessionId}`, - }); - } - switch (entry.type) { case "user": { messagesByEntry.set(entry.id, yield* decodeMessage(hydrated)); @@ -131,14 +117,17 @@ export const assemblePath = Effect.fn("Context.assemblePath")(function* ( case "compaction": { const data = yield* decodeCompactionData(entry.data).pipe(Effect.mapError(decodeFailure(hydrated))); compactions.set(entry.id, data); - latestCompaction = { index, data }; + latestStart = { index, compaction: data }; break; } - case "branchSummary": { - const data = yield* decodeBranchSummaryData(entry.data).pipe(Effect.mapError(decodeFailure(hydrated))); - branchSummaries.set(entry.id, data.summary); + case "reset": { + const data = yield* decodeResetData(entry.data).pipe(Effect.mapError(decodeFailure(hydrated))); + if (data.handoff !== undefined) handoffs.set(entry.id, data.handoff); + latestStart = { index, handoff: data.handoff }; break; } + case "system": + break; case "custom": { yield* decodeCustomData(entry.data).pipe(Effect.mapError(decodeFailure(hydrated))); break; @@ -147,9 +136,11 @@ export const assemblePath = Effect.fn("Context.assemblePath")(function* ( } let effectivePath = committedPath; - if (latestCompaction !== undefined) { - const { index, data } = latestCompaction; - let kept: ReadonlyArray = []; + if (latestStart !== undefined && !("compaction" in latestStart)) { + effectivePath = committedPath.slice(latestStart.index); + } else if (latestStart !== undefined) { + const { index, compaction: data } = latestStart; + let kept: ReadonlyArray = []; if (data.firstKeptEntryId !== null) { const firstKeptIndex = committedPath.findIndex((item) => item.entry.id === data.firstKeptEntryId); if (firstKeptIndex < 0 || firstKeptIndex >= index) { @@ -178,14 +169,14 @@ export const assemblePath = Effect.fn("Context.assemblePath")(function* ( messages.push(wrapperMessage(hydrated, compactionText(compaction.summary))); continue; } - const branchSummary = branchSummaries.get(entry.id); - if (branchSummary !== undefined) { - messages.push(wrapperMessage(hydrated, branchSummaryText(branchSummary))); + const handoff = handoffs.get(entry.id); + if (handoff !== undefined) { + messages.push(wrapperMessage(hydrated, resetText(handoff))); } } return { - sessionId, + threadId, leafEntryId, messages, ...(lastAssistant === undefined ? {} : { lastAssistant }), diff --git a/packages/harness/src/context/codec.ts b/packages/harness/src/context/codec.ts index 34df7ac1..bd513e41 100644 --- a/packages/harness/src/context/codec.ts +++ b/packages/harness/src/context/codec.ts @@ -6,13 +6,13 @@ import { Message } from "@codeworksh/aikit"; import { DateTime, Effect, Option, Schema } from "effect"; import { validateAikitMessage, validateAikitUserMessage } from "../schema.ts"; import { SessionSchema } from "../session/schema.ts"; -import type { Session } from "../session/session.ts"; +import type { Thread } from "../thread/thread.ts"; import { ContextDecodeError, ContextEncodeError } from "./errors.ts"; export interface EncodedMessage { readonly type: "user" | "assistant"; readonly data: string; - readonly parts: ReadonlyArray; + readonly parts: ReadonlyArray; } type AikitPart = Message.AssistantMessage["parts"][number] | Message.UserMessage["parts"][number]; @@ -26,7 +26,7 @@ const encodeJsonObject = Schema.encodeEffect(SessionSchema.JsonObject); export const encodePart = Effect.fn("Context.encodePart")(function* ( part: AikitPart, -): Effect.fn.Return { +): Effect.fn.Return { const data = yield* encodeJsonObject(part).pipe( Effect.mapError( (cause) => @@ -124,7 +124,7 @@ export const encodeMessage = Effect.fn("Context.encodeMessage")(function* ( }); const decodeParts = Effect.fn("Context.decodeParts")(function* ( - hydrated: Session.HydratedEntry, + hydrated: Thread.HydratedEntry, ): Effect.fn.Return, ContextDecodeError> { const { entry } = hydrated; const sorted = [...hydrated.parts].sort((left, right) => left.partIndex - right.partIndex); @@ -183,7 +183,7 @@ const decodeParts = Effect.fn("Context.decodeParts")(function* ( }); export const decodeSyntheticMessage = Effect.fn("Context.decodeSyntheticMessage")(function* ( - hydrated: Session.HydratedEntry, + hydrated: Thread.HydratedEntry, ): Effect.fn.Return { const { entry } = hydrated; const parts = yield* decodeParts(hydrated); @@ -200,7 +200,7 @@ export const decodeSyntheticMessage = Effect.fn("Context.decodeSyntheticMessage" }); export const decodeMessage = Effect.fn("Context.decodeMessage")(function* ( - hydrated: Session.HydratedEntry, + hydrated: Thread.HydratedEntry, ): Effect.fn.Return { const { entry } = hydrated; if (entry.type !== "user" && entry.type !== "assistant") { diff --git a/packages/harness/src/context/context.ts b/packages/harness/src/context/context.ts index f86bd6c3..791541f1 100644 --- a/packages/harness/src/context/context.ts +++ b/packages/harness/src/context/context.ts @@ -1,13 +1,13 @@ import { Context, Effect, Layer, Option } from "effect"; -import { SessionSchema } from "../session/schema.ts"; -import { Session } from "../session/session.ts"; -import { assemblePath, type Snapshot } from "./assemble.ts"; +import type { ThreadSchema } from "../thread/schema.ts"; +import { Thread } from "../thread/thread.ts"; +import { assembleLine, type Snapshot } from "./assemble.ts"; import type { ContextDecodeError } from "./errors.ts"; -export type ContextReadError = Session.SessionNotFoundError | ContextDecodeError; +export type ContextReadError = Thread.ThreadNotFoundError | ContextDecodeError; export interface Interface { - readonly assemble: (sessionId: SessionSchema.ID) => Effect.Effect; + readonly assemble: (threadId: ThreadSchema.ID) => Effect.Effect; } export class Service extends Context.Service()("@codeworksh/harness/context/context/Service") {} @@ -15,11 +15,11 @@ export class Service extends Context.Service()("@codeworksh/ export const layer = Layer.effect( Service, Effect.gen(function* () { - const sessions = yield* Session.Service; - const assemble = Effect.fn("Context.assemble")(function* (sessionId: SessionSchema.ID) { - const found = yield* sessions.get(sessionId); - if (Option.isNone(found)) return yield* new Session.SessionNotFoundError({ sessionId }); - return yield* assemblePath(sessionId, yield* sessions.path(sessionId)); + const threads = yield* Thread.Service; + const assemble = Effect.fn("Context.assemble")(function* (threadId: ThreadSchema.ID) { + const found = yield* threads.get(threadId); + if (Option.isNone(found)) return yield* new Thread.ThreadNotFoundError({ threadId }); + return yield* assembleLine(threadId, yield* threads.line(threadId)); }); return Service.of({ assemble }); }), diff --git a/packages/harness/src/control.ts b/packages/harness/src/control.ts index b2b70beb..4c180919 100644 --- a/packages/harness/src/control.ts +++ b/packages/harness/src/control.ts @@ -1,6 +1,6 @@ /* * @file The control plane: the commands that start, join, and stop work on a - * session. + * thread. * * Session storage records what happened. Control decides whether, when, and for * how long a session runs. Keeping the two apart is what lets `Session.Service` @@ -15,14 +15,17 @@ */ import { Context, Effect, Layer, Option, Schema } from "effect"; +import { Inbox } from "./inbox/inbox.ts"; +import type { Admitted } from "./inbox/schema.ts"; import { RunnerExecution } from "./runner/execution.ts"; import type { Runner } from "./runner/run.ts"; -import { SessionInput } from "./session/input/input.ts"; -import type { Admitted } from "./session/input/schema.ts"; import { SessionMessageSchema } from "./session/message/schema.ts"; import type { Delivery, Prompt } from "./session/prompt/schema.ts"; import { SessionSchema } from "./session/schema.ts"; import { Session } from "./session/session.ts"; +import type { ThreadRow } from "./db/schema.sql.ts"; +import { ThreadSchema } from "./thread/schema.ts"; +import { Thread } from "./thread/thread.ts"; /** * The prompt could not be admitted under the id it was given: either that id @@ -30,12 +33,17 @@ import { Session } from "./session/session.ts"; * rather than a defect — a client that retried with a reused id can act on it. */ export class PromptConflictError extends Schema.TaggedError()("PromptConflictError", { - sessionId: Schema.String, + threadId: Schema.String, messageId: Schema.String, }) {} -export interface PromptInput { +/** A thread of a session: `threadId`, or the session's `main` thread when absent. */ +export interface ThreadRef { readonly sessionId: SessionSchema.ID; + readonly threadId?: ThreadSchema.ID | undefined; +} + +export interface PromptInput extends ThreadRef { readonly prompt: Prompt; /** Supply to make the call idempotent across retries; minted when omitted. */ readonly id?: SessionMessageSchema.ID; @@ -43,15 +51,19 @@ export interface PromptInput { readonly delivery?: Delivery; } +export type ResolveError = Session.SessionNotFoundError | Thread.ThreadNotFoundError; + /** - * Times a run may be cut off by a dying process before the session is parked instead of resumed. + * Times a run may be cut off by a dying process before the thread is parked instead of resumed. * A committed turn starts the count over. */ export const RESTART_LIMIT = 5; export interface Interface { + /** The thread a reference names. A thread of another session is not found. */ + readonly resolve: (ref: ThreadRef) => Effect.Effect; /** - * Records a prompt in the session's durable inbox, then wakes its drain. + * Records a prompt in the thread's durable inbox, then wakes its drain. * * Returns once the prompt is stored, not once it is answered, and not once it * is in the conversation: an admitted prompt enters the transcript at @@ -59,15 +71,13 @@ export interface Interface { * two the inbox is the only durable record, which is what makes a crash in * that window recoverable rather than a user message nothing ever answered. */ - readonly prompt: (input: PromptInput) => Effect.Effect; - /** Records a prompt, wakes execution, and waits until the session is idle. */ - readonly run: (input: PromptInput) => Effect.Effect; - /** Waits until the session is idle without starting work. */ - readonly wait: (sessionId: SessionSchema.ID) => Effect.Effect; - /** Starts a drain while the session is idle, or joins the one already running. */ - readonly resume: ( - sessionId: SessionSchema.ID, - ) => Effect.Effect; + readonly prompt: (input: PromptInput) => Effect.Effect; + /** Records a prompt, wakes execution, and waits until the thread is idle. */ + readonly run: (input: PromptInput) => Effect.Effect; + /** Waits until the thread is idle without starting work. */ + readonly wait: (threadId: ThreadSchema.ID) => Effect.Effect; + /** Starts a drain while the thread is idle, or joins the one already running. */ + readonly resume: (threadId: ThreadSchema.ID) => Effect.Effect; /** * Stops work owned by this process. Idle is a no-op. Resolves once the * interruption is accepted and returns whether there was work to accept it; @@ -75,31 +85,38 @@ export interface Interface { * following work another caller admits in the meantime. */ readonly interrupt: ( + threadId: ThreadSchema.ID, + options?: { readonly reason?: SessionSchema.InterruptReason; readonly awaitSettlement?: boolean }, + ) => Effect.Effect; + /** Interrupts every running thread of the session; whether any was running. */ + readonly interruptSession: ( sessionId: SessionSchema.ID, options?: { readonly reason?: SessionSchema.InterruptReason; readonly awaitSettlement?: boolean }, ) => Effect.Effect; - /** Sessions this process is currently draining. */ - readonly active: Effect.Effect>; + /** Stops the session's threads, then removes it with its log. False when there was none. */ + readonly deleteSession: (sessionId: SessionSchema.ID) => Effect.Effect; + /** Threads this process is currently draining. */ + readonly active: Effect.Effect>; /** * Resumes the runs the last process left unfinished, as the server starts. One a crash cut off - * counts a restart, one a shutdown closed does not; at {@link RESTART_LIMIT} the session is parked + * counts a restart, one a shutdown closed does not; at {@link RESTART_LIMIT} the thread is parked * and waits for its next prompt. Resolves once every resumed run has settled. */ readonly resumeUnfinished: Effect.Effect; /** Prompts admitted but not yet in the conversation, steers and follow-ups, in admission order. */ - readonly pending: (sessionId: SessionSchema.ID) => Effect.Effect>; + readonly pending: (threadId: ThreadSchema.ID) => Effect.Effect>; /** * Moves a pending prompt to the other lane or rewrites it, keeping its place; false when it was - * already promoted, cancelled, or is unknown. Asking for the steer lane also wakes the session, so + * already promoted, cancelled, or is unknown. Asking for the steer lane also wakes the thread, so * a prompt left waiting by a stop or a failure runs now. */ readonly update: ( - sessionId: SessionSchema.ID, + threadId: ThreadSchema.ID, id: SessionMessageSchema.ID, - patch: SessionInput.UpdatePatch, + patch: Inbox.UpdatePatch, ) => Effect.Effect; /** Withdraws a pending prompt; false when it was already promoted, cancelled, or is unknown. */ - readonly cancel: (sessionId: SessionSchema.ID, id: SessionMessageSchema.ID) => Effect.Effect; + readonly cancel: (threadId: ThreadSchema.ID, id: SessionMessageSchema.ID) => Effect.Effect; } export class Service extends Context.Service()("@codeworksh/harness/control/Service") {} @@ -108,13 +125,31 @@ export const layer = Layer.effect( Service, Effect.gen(function* () { const sessions = yield* Session.Service; - const inputs = yield* SessionInput.make; + const threads = yield* Thread.Service; + const inputs = yield* Inbox.make; const execution = yield* RunnerExecution.Service; - const isLifecycleConflict = Schema.is(SessionInput.LifecycleConflict); + const isLifecycleConflict = Schema.is(Inbox.LifecycleConflict); // Marked before the wake, so a prompt accepted here survives a crash before its run starts. - const wake = (sessionId: SessionSchema.ID) => - sessions.markRunning(sessionId).pipe(Effect.andThen(execution.wake(sessionId))); + const wake = (threadId: ThreadSchema.ID) => + threads.markRunning(threadId).pipe(Effect.andThen(execution.wake(threadId))); + + const resolve = Effect.fn("Control.resolve")(function* (ref: ThreadRef) { + if (ref.threadId === undefined) { + const main = yield* threads.main(ref.sessionId); + if (Option.isSome(main)) return main.value; + return yield* new Session.SessionNotFoundError({ sessionId: ref.sessionId }); + } + const thread = yield* threads.get(ref.threadId); + if (Option.isNone(thread) || thread.value.sessionId !== ref.sessionId) { + return yield* new Thread.ThreadNotFoundError({ threadId: ref.threadId }); + } + return thread.value; + }); + + const requireThread = Effect.fn("Control.requireThread")(function* (threadId: ThreadSchema.ID) { + if (Option.isNone(yield* threads.get(threadId))) return yield* new Thread.ThreadNotFoundError({ threadId }); + }); /** * Uninterruptible end to end: a client that disconnects mid-call must not @@ -123,77 +158,100 @@ export const layer = Layer.effect( * duplicating. */ const admit = Effect.fn("Control.admit")(function* (input: PromptInput) { - const session = yield* sessions.get(input.sessionId); - if (Option.isNone(session)) { - return yield* new Session.SessionNotFoundError({ sessionId: input.sessionId }); - } - + const thread = yield* resolve(input); + const threadId = ThreadSchema.ID.make(thread.id); const messageId = input.id ?? SessionMessageSchema.ID.create(); const delivery = input.delivery ?? "steer"; const admitted = yield* inputs - .admit({ id: messageId, sessionId: input.sessionId, prompt: input.prompt, delivery }) + .admit({ id: messageId, sessionId: input.sessionId, threadId, prompt: input.prompt, delivery }) .pipe( // The id already left the inbox for the conversation. That is a // conflict the caller can see, not a broken invariant. Effect.catchDefect((defect) => - isLifecycleConflict(defect) - ? new PromptConflictError({ sessionId: input.sessionId, messageId }) - : Effect.die(defect), + isLifecycleConflict(defect) ? new PromptConflictError({ threadId, messageId }) : Effect.die(defect), ), ); // A retry that reuses an id but changes the prompt is not the same // request. The stored row wins, and the caller is told. - if (!SessionInput.equivalent(admitted, { sessionId: input.sessionId, prompt: input.prompt, delivery })) { - return yield* new PromptConflictError({ sessionId: input.sessionId, messageId }); + if (!Inbox.equivalent(admitted, { threadId, prompt: input.prompt, delivery })) { + return yield* new PromptConflictError({ threadId, messageId }); } return admitted; }); const prompt = Effect.fn("Control.prompt")((input: PromptInput) => - Effect.uninterruptible(admit(input).pipe(Effect.tap((admitted) => wake(admitted.sessionId)))), + Effect.uninterruptible(admit(input).pipe(Effect.tap((admitted) => wake(admitted.threadId)))), ); const run = Effect.fn("Control.run")((input: PromptInput) => Effect.uninterruptibleMask((restore) => Effect.gen(function* () { const admitted = yield* admit(input); - yield* wake(admitted.sessionId); - yield* restore(execution.awaitIdle(admitted.sessionId)); + yield* wake(admitted.threadId); + yield* restore(execution.awaitIdle(admitted.threadId)); return admitted; }), ), ); - const wait = Effect.fn("Control.wait")(function* (sessionId: SessionSchema.ID) { - const session = yield* sessions.get(sessionId); - if (Option.isNone(session)) return yield* new Session.SessionNotFoundError({ sessionId }); - yield* execution.awaitIdle(sessionId); + const wait = Effect.fn("Control.wait")(function* (threadId: ThreadSchema.ID) { + yield* requireThread(threadId); + yield* execution.awaitIdle(threadId); }); - const resume = Effect.fn("Control.resume")(function* (sessionId: SessionSchema.ID) { - const session = yield* sessions.get(sessionId); - if (Option.isNone(session)) return yield* new Session.SessionNotFoundError({ sessionId }); - yield* sessions.markRunning(sessionId); - yield* execution.resume(sessionId); + const resume = Effect.fn("Control.resume")(function* (threadId: ThreadSchema.ID) { + yield* requireThread(threadId); + yield* threads.markRunning(threadId); + yield* execution.resume(threadId); }); const interrupt = Effect.fn("Control.interrupt")( ( - sessionId: SessionSchema.ID, + threadId: ThreadSchema.ID, options?: { readonly reason?: SessionSchema.InterruptReason; readonly awaitSettlement?: boolean }, - ) => Effect.uninterruptible(execution.interrupt(sessionId, options?.reason ?? "user", options)), + ) => Effect.uninterruptible(execution.interrupt(threadId, options?.reason ?? "user", options)), ); + const interruptSession = Effect.fn("Control.interruptSession")(function* ( + sessionId: SessionSchema.ID, + options?: { readonly reason?: SessionSchema.InterruptReason; readonly awaitSettlement?: boolean }, + ) { + const running = yield* execution.active; + const owned = (yield* threads.list(sessionId)).filter((thread) => + running.has(ThreadSchema.ID.make(thread.id)), + ); + const interrupted = yield* Effect.forEach( + owned, + (thread) => interrupt(ThreadSchema.ID.make(thread.id), options), + { concurrency: "unbounded" }, + ); + return interrupted.some(Boolean); + }); + + const deleteSession = Effect.fn("Control.deleteSession")(function* (sessionId: SessionSchema.ID) { + const ids = (yield* threads.list(sessionId)).map((thread) => ThreadSchema.ID.make(thread.id)); + yield* interruptSession(sessionId, { reason: "user", awaitSettlement: true }); + const deleted = yield* sessions.delete(sessionId); + // A prompt that woke a thread after the stop above runs on rows that are now gone; stop it too. + const running = yield* execution.active; + yield* Effect.forEach( + ids.filter((id) => running.has(id)), + (id) => interrupt(id, { reason: "user", awaitSettlement: true }), + { concurrency: "unbounded", discard: true }, + ); + return deleted; + }); + // Uninterruptible like `prompt`: a change to the steer lane that commits must also wake. const update = Effect.fn("Control.update")( - (sessionId: SessionSchema.ID, id: SessionMessageSchema.ID, patch: SessionInput.UpdatePatch) => + (threadId: ThreadSchema.ID, id: SessionMessageSchema.ID, patch: Inbox.UpdatePatch) => Effect.uninterruptible( Effect.gen(function* () { - const updated = yield* inputs.update(sessionId, id, patch); + const updated = yield* inputs.update(threadId, id, patch); if (Option.isNone(updated)) return false; - if (patch.delivery === "steer") yield* wake(sessionId); + if (patch.delivery === "steer") yield* wake(threadId); return true; }), ), @@ -201,19 +259,20 @@ export const layer = Layer.effect( const resumeUnfinished = Effect.gen(function* () { const running = yield* execution.active; - const unfinished = (yield* sessions.unfinished).filter((row) => !running.has(row.id)); + const unfinished = (yield* threads.unfinished).filter((row) => !running.has(ThreadSchema.ID.make(row.id))); yield* Effect.forEach( unfinished, Effect.fnUntraced(function* (row) { - if ((yield* execution.active).has(row.id)) return; + const threadId = ThreadSchema.ID.make(row.id); + if ((yield* execution.active).has(threadId)) return; // Claimed again here, not from the list: a stop since then cleared it, and must stand. // Counted before resuming, so a crash inside the resumed run is counted too. - const restarts = yield* sessions.claimRestart(row.id); + const restarts = yield* threads.claimRestart(threadId); if (Option.isNone(restarts) || restarts.value >= RESTART_LIMIT) return; - yield* execution.recover(row.id).pipe( + yield* execution.recover(threadId).pipe( Effect.tapCause((cause) => - Effect.logWarning("Resuming an unfinished session failed", cause).pipe( - Effect.annotateLogs({ sessionId: row.id }), + Effect.logWarning("Resuming an unfinished thread failed", cause).pipe( + Effect.annotateLogs({ threadId }), ), ), Effect.ignore, @@ -224,11 +283,14 @@ export const layer = Layer.effect( }).pipe(Effect.withSpan("Control.resumeUnfinished")); return Service.of({ + resolve, prompt, run, wait, resume, interrupt, + interruptSession, + deleteSession, active: execution.active, resumeUnfinished, pending: inputs.pending, diff --git a/packages/harness/src/db/migrations.ts b/packages/harness/src/db/migrations.ts index dd73f3ac..e520d0bc 100644 --- a/packages/harness/src/db/migrations.ts +++ b/packages/harness/src/db/migrations.ts @@ -9,284 +9,249 @@ import { SqlClient } from "effect/sql"; // it. Only the numeric prefix orders migrations, and it must be strictly // greater than every id already applied — the migrator runs by high-water // mark, so an id dated before an applied one is silently skipped. +// +// The thread model replaced every earlier migration: databases created before it are discarded, +// not upgraded, so this one assumes an empty database. export const migrations = { - "202607070001_init": Effect.gen(function* () { + "202610290001_init": Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; - // Tracks head of the inbox event sequence for a given aggregate. + // Head of one aggregate's log (aggregate = session), and who holds it. yield* sql` - CREATE TABLE event_sequence ( + CREATE TABLE sequence ( aggregate_id TEXT PRIMARY KEY, - seq INTEGER NOT NULL, - owner_id TEXT + seq INTEGER NOT NULL, + owner_id TEXT ) `; - // A durable inbox event store. An event is then picked and processed. - // Stores countigous sequence of events for a given aggregate. + // The durable log. Seq is contiguous per aggregate, from 1. yield* sql` CREATE TABLE event ( - id TEXT PRIMARY KEY, - aggregate_id TEXT NOT NULL - REFERENCES event_sequence(aggregate_id) ON DELETE CASCADE, - seq INTEGER NOT NULL, - type TEXT NOT NULL, - data TEXT NOT NULL + id TEXT PRIMARY KEY, + aggregate_id TEXT NOT NULL REFERENCES sequence(aggregate_id) ON DELETE CASCADE, + seq INTEGER NOT NULL, + type TEXT NOT NULL, + data TEXT NOT NULL ) `; + yield* sql`CREATE UNIQUE INDEX event_seq_idx ON event (aggregate_id, seq)`; + yield* sql`CREATE INDEX event_type_idx ON event (aggregate_id, type, seq)`; - yield* sql`CREATE UNIQUE INDEX event_aggregate_id_seq_idx ON event (aggregate_id, seq)`; - yield* sql`CREATE INDEX event_aggregate_id_type_seq_idx ON event (aggregate_id, type, seq)`; - - // A durable filesystem namespace. Created before space because it - // references it. Reference counts are deliberately - // absent: they live in control-plane memory, since a persisted count cannot - // tell whether the process that took it is still running. - // - // The host is never a row. `local` is a reserved id that exists at runtime - // and never reaches a column; NULL is its only storage form, so nothing has - // to be seeded or repaired for a session to exist. + // A provisioned filesystem namespace. The host is never a row: NULL is its storage form. yield* sql` - CREATE TABLE sandbox_instance ( - id TEXT PRIMARY KEY CHECK (id <> 'local'), - driver TEXT NOT NULL, - kind TEXT NOT NULL CHECK (kind IN ('local', 'virtual', 'remote')), + CREATE TABLE sandbox ( + id TEXT PRIMARY KEY CHECK (id <> 'local'), + driver TEXT NOT NULL, + kind TEXT NOT NULL, provider_resource_id TEXT, - runtime_config TEXT, - ownership TEXT NOT NULL CHECK (ownership IN ('managed', 'external')), - status TEXT NOT NULL, - provider_status TEXT, - state_observed_at INTEGER NOT NULL, - metadata TEXT, - last_error TEXT, - last_mounted_at INTEGER, - last_unmounted_at INTEGER, - last_used_at INTEGER, - removed_at INTEGER, - created_at INTEGER NOT NULL, - updated_at INTEGER NOT NULL + runtime_config TEXT, + ownership TEXT NOT NULL, + status TEXT NOT NULL, + provider_status TEXT, + state_observed_at INTEGER NOT NULL, + metadata TEXT, + last_error TEXT, + last_mounted_at INTEGER, + last_unmounted_at INTEGER, + last_used_at INTEGER, + removed_at INTEGER, + created_at INTEGER NOT NULL, + updated_at INTEGER NOT NULL ) `; - - // One live instance per driver resource: two application identities for - // one namespace would alias, which is the thing instance IDs exist to - // prevent. Tombstones are excluded so a removed row never blocks a - // genuinely new resource that reuses the locator. + // One live row per driver resource; tombstones excluded. yield* sql` - CREATE UNIQUE INDEX sandbox_instance_resource_idx - ON sandbox_instance (driver, provider_resource_id) - WHERE provider_resource_id IS NOT NULL AND status != 'removed' + CREATE UNIQUE INDEX sandbox_resource_idx ON sandbox (driver, provider_resource_id) + WHERE provider_resource_id IS NOT NULL AND status <> 'removed' `; - // A logical codebase, env-independent. Archived rather than deleted once - // it has no active space anywhere, so session history stays reachable. + // A logical codebase, env-independent. Archived, never deleted. yield* sql` CREATE TABLE project ( - id TEXT PRIMARY KEY, - name TEXT NOT NULL, - status TEXT NOT NULL CHECK (status IN ('active', 'archived')), + id TEXT PRIMARY KEY, + name TEXT NOT NULL, + status TEXT NOT NULL, created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL ) `; - // One directory in one env. `id` is Hash.fast([env|'local', location]) — - // no project component, so re-pointing a space to another project keeps - // every session key intact. `env` NULL = host (same convention as - // sandbox_instance_id everywhere else); the FK is skipped on NULL. - // RESTRICT on both FKs: a space with sessions is archived, never deleted, - // and a destroyed sandbox is tombstoned, not removed. + // One directory in one env. id = hash(env | 'local', location). yield* sql` CREATE TABLE space ( - id TEXT PRIMARY KEY, - project_id TEXT NOT NULL REFERENCES project(id) ON UPDATE CASCADE ON DELETE RESTRICT, - location TEXT NOT NULL, - kind TEXT NOT NULL CHECK (kind IN ('primary', 'linked', 'copy', 'plain')), - env TEXT - REFERENCES sandbox_instance(id) ON UPDATE CASCADE ON DELETE RESTRICT - CHECK (env IS NULL OR env <> 'local'), - status TEXT NOT NULL CHECK (status IN ('active', 'archived')), + id TEXT PRIMARY KEY, + project_id TEXT NOT NULL REFERENCES project(id) ON DELETE RESTRICT, + location TEXT NOT NULL, + kind TEXT NOT NULL, + env TEXT REFERENCES sandbox(id) ON DELETE RESTRICT CHECK (env IS NULL OR env <> 'local'), + status TEXT NOT NULL, created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL ) `; - - // One place, one row. SQLite treats NULLs as distinct in a unique index, - // so (NULL, '/repo') would insert twice; coalescing to the reserved id - // makes the index read as what it means. - yield* sql` - CREATE UNIQUE INDEX space_location_idx - ON space (COALESCE(env, 'local'), location) - `; - - // Exactly one primary per (project, env). Partial: the other kinds are - // unbounded. Status is deliberately not part of the predicate — an - // archived primary must be demoted before another can be promoted. + yield* sql`CREATE UNIQUE INDEX space_location_idx ON space (coalesce(env, 'local'), location)`; yield* sql` - CREATE UNIQUE INDEX space_primary_idx - ON space (project_id, COALESCE(env, 'local')) WHERE kind = 'primary' + CREATE UNIQUE INDEX space_primary_idx ON space (project_id, coalesce(env, 'local')) WHERE kind = 'primary' `; - yield* sql`CREATE INDEX space_project_idx ON space (project_id)`; - // A session belongs to a space; project and env are derived through it, - // never denormalised here. `directory` is the ABSOLUTE realpath of the - // session cwd, always equal to or under space.location (which is immutable). - // RESTRICT: sessions are history, a referenced space cannot go away. yield* sql` CREATE TABLE session ( - id TEXT PRIMARY KEY, - space_id TEXT NOT NULL REFERENCES space(id) ON UPDATE CASCADE ON DELETE RESTRICT, - parent_id TEXT REFERENCES session(id) ON UPDATE CASCADE ON DELETE SET NULL, - slug TEXT NOT NULL, - directory TEXT NOT NULL, - title TEXT NOT NULL, - tag TEXT, - metadata TEXT, - cost REAL NOT NULL DEFAULT 0, - tokens_input INTEGER NOT NULL DEFAULT 0, - tokens_output INTEGER NOT NULL DEFAULT 0, - tokens_cache_read INTEGER NOT NULL DEFAULT 0, - tokens_cache_write INTEGER NOT NULL DEFAULT 0, + id TEXT PRIMARY KEY, + space_id TEXT NOT NULL REFERENCES space(id) ON DELETE RESTRICT, + slug TEXT NOT NULL, + directory TEXT NOT NULL, + host_dir TEXT, + tag TEXT, + metadata TEXT, created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL ) `; - - yield* sql`CREATE INDEX session_space_idx ON session (space_id)`; yield* sql`CREATE UNIQUE INDEX session_slug_idx ON session (slug)`; + yield* sql`CREATE INDEX session_space_idx ON session (space_id)`; yield* sql`CREATE INDEX session_tag_idx ON session (tag)`; - // The tree edge is a composite FK (session_id, parent_id) so a parent can - // never live in another session — cross-session edges would mess up the context - // NULL parent_id (roots) skips the FK per SQLite. - // In short: FK (session_id, parent_id); makes sure that a child entry via parent_id must belong - // to exact same session_id. yield* sql` - CREATE TABLE session_entry ( - id TEXT PRIMARY KEY, - session_id TEXT NOT NULL REFERENCES session(id) ON UPDATE CASCADE ON DELETE CASCADE, - parent_id TEXT, - seq INTEGER NOT NULL, - type TEXT NOT NULL, - state TEXT NOT NULL DEFAULT 'committed' - CHECK (state IN ('draft', 'committed', 'aborted')), - data TEXT NOT NULL, - label TEXT, - metadata TEXT, - created_at INTEGER NOT NULL, - updated_at INTEGER NOT NULL, - FOREIGN KEY (session_id, parent_id) REFERENCES session_entry(session_id, id) + CREATE TABLE thread ( + id TEXT PRIMARY KEY, + session_id TEXT NOT NULL REFERENCES session(id) ON DELETE CASCADE, + origin_kind TEXT NOT NULL, + origin_thread_id TEXT, + origin_entry_id TEXT, + origin_call_id TEXT, + title TEXT, + execution TEXT, + restarts INTEGER NOT NULL DEFAULT 0, + cost REAL NOT NULL DEFAULT 0, + tokens_input INTEGER NOT NULL DEFAULT 0, + tokens_output INTEGER NOT NULL DEFAULT 0, + tokens_cache_read INTEGER NOT NULL DEFAULT 0, + tokens_cache_write INTEGER NOT NULL DEFAULT 0, + created_seq INTEGER NOT NULL, + created_at INTEGER NOT NULL, + updated_at INTEGER NOT NULL, + UNIQUE (session_id, id), + -- the source thread lives in the same session + FOREIGN KEY (session_id, origin_thread_id) REFERENCES thread(session_id, id), + -- the fork point lives in the source thread + FOREIGN KEY (origin_thread_id, origin_entry_id) REFERENCES entry(thread_id, id), + -- main is the only root: the context walk ends there, thread_main_idx counts it + CHECK ((origin_kind = 'main') = (origin_thread_id IS NULL)), + -- a fork always has a cut; without it the context walk silently returns nothing + CHECK (origin_kind <> 'fork' OR origin_entry_id IS NOT NULL), + -- exactly the subagents carry a call + CHECK ((origin_kind = 'subagent') = (origin_call_id IS NOT NULL)) ) `; + yield* sql`CREATE UNIQUE INDEX thread_main_idx ON thread (session_id) WHERE origin_kind = 'main'`; + yield* sql`CREATE INDEX thread_origin_idx ON thread (origin_thread_id) WHERE origin_thread_id IS NOT NULL`; + // Call ids are unique per entry, not globally: one subagent per call of a thread. + yield* sql` + CREATE UNIQUE INDEX thread_call_idx ON thread (origin_thread_id, origin_call_id) + WHERE origin_call_id IS NOT NULL + `; + yield* sql`CREATE INDEX thread_running_idx ON thread (session_id) WHERE execution = 'running'`; - // Parent key for the composite FKs (SQLite requires a UNIQUE covering - // the referenced columns). - yield* sql`CREATE UNIQUE INDEX session_entry_session_id_idx ON session_entry (session_id, id)`; + yield* sql` + CREATE TABLE entry ( + id TEXT PRIMARY KEY, + session_id TEXT NOT NULL, + thread_id TEXT NOT NULL, + seq INTEGER NOT NULL, + committed_seq INTEGER, + type TEXT NOT NULL, + state TEXT NOT NULL, + data TEXT NOT NULL, + label TEXT, + metadata TEXT, + created_at INTEGER NOT NULL, + updated_at INTEGER NOT NULL, + UNIQUE (thread_id, id), + FOREIGN KEY (session_id, thread_id) REFERENCES thread(session_id, id) ON DELETE CASCADE, + -- the fork cut reads committed_seq: set exactly when the entry stops changing + CHECK ((state = 'draft') = (committed_seq IS NULL)), + CHECK (committed_seq IS NULL OR committed_seq >= seq) + ) + `; + yield* sql`CREATE INDEX entry_seq_idx ON entry (session_id, seq)`; + // One entry per thread per event: seq alone orders a thread, and a cut at a seq is exact. + yield* sql`CREATE UNIQUE INDEX entry_thread_idx ON entry (thread_id, seq)`; + yield* sql`CREATE INDEX entry_type_idx ON entry (thread_id, type, seq)`; + yield* sql`CREATE INDEX entry_draft_idx ON entry (thread_id) WHERE state = 'draft'`; + yield* sql`CREATE INDEX entry_label_idx ON entry (thread_id, seq) WHERE label IS NOT NULL`; yield* sql` - CREATE TABLE session_entry_part ( - id TEXT PRIMARY KEY, - entry_id TEXT NOT NULL, - session_id TEXT NOT NULL REFERENCES session(id) ON UPDATE CASCADE ON DELETE CASCADE, + CREATE TABLE part ( + id TEXT PRIMARY KEY, + entry_id TEXT NOT NULL, + thread_id TEXT NOT NULL, part_index INTEGER NOT NULL, - type TEXT NOT NULL, - status TEXT, - call_id TEXT, - tool_name TEXT, - data TEXT NOT NULL, + type TEXT NOT NULL, + status TEXT, + call_id TEXT, + tool_name TEXT, + data TEXT NOT NULL, created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL, - CHECK (type != 'toolCall' OR (status IS NOT NULL AND call_id IS NOT NULL AND tool_name IS NOT NULL)), - CHECK (type = 'toolCall' OR (status IS NULL AND call_id IS NULL AND tool_name IS NULL)), - FOREIGN KEY (session_id, entry_id) REFERENCES session_entry(session_id, id) ON UPDATE CASCADE ON DELETE CASCADE + FOREIGN KEY (thread_id, entry_id) REFERENCES entry(thread_id, id) ON DELETE CASCADE ) `; - - yield* sql`ALTER TABLE session ADD COLUMN leaf_entry_id TEXT REFERENCES session_entry(id)`; - - yield* sql`CREATE UNIQUE INDEX session_entry_session_seq_idx ON session_entry (session_id, seq)`; - yield* sql`CREATE INDEX session_entry_parent_idx ON session_entry (session_id, parent_id)`; - yield* sql`CREATE INDEX session_entry_type_idx ON session_entry (session_id, type, seq)`; - yield* sql`CREATE INDEX session_entry_state_idx ON session_entry (session_id, state, seq)`; - yield* sql`CREATE INDEX session_entry_label_idx ON session_entry (session_id, seq) WHERE label IS NOT NULL`; - - yield* sql`CREATE UNIQUE INDEX session_entry_part_entry_idx ON session_entry_part (entry_id, part_index)`; - yield* sql`CREATE INDEX session_entry_part_call_idx ON session_entry_part (session_id, call_id) WHERE call_id IS NOT NULL`; - yield* sql`CREATE UNIQUE INDEX session_entry_part_call_uidx ON session_entry_part (entry_id, call_id) WHERE call_id IS NOT NULL`; - yield* sql`CREATE INDEX session_entry_part_unsettled_idx ON session_entry_part (session_id, status) WHERE status IN ('pending', 'running')`; - yield* sql`CREATE INDEX session_entry_part_session_idx ON session_entry_part (session_id, entry_id, part_index)`; + yield* sql`CREATE UNIQUE INDEX part_index_idx ON part (entry_id, part_index)`; + yield* sql`CREATE UNIQUE INDEX part_call_idx ON part (entry_id, call_id) WHERE call_id IS NOT NULL`; + yield* sql`CREATE INDEX part_call_lookup_idx ON part (thread_id, call_id) WHERE call_id IS NOT NULL`; + yield* sql` + CREATE INDEX part_unsettled_idx ON part (thread_id, status) WHERE status IN ('pending', 'running') + `; yield* sql` - CREATE TABLE session_input ( - id TEXT PRIMARY KEY, - session_id TEXT NOT NULL REFERENCES session(id) ON UPDATE CASCADE ON DELETE CASCADE, - prompt TEXT NOT NULL, - delivery TEXT NOT NULL, - admitted_seq INTEGER NOT NULL, - promoted_seq INTEGER, - created_at INTEGER NOT NULL + CREATE TABLE inbox ( + id TEXT PRIMARY KEY, + session_id TEXT NOT NULL, + thread_id TEXT NOT NULL, + kind TEXT NOT NULL, + data TEXT NOT NULL, + delivery TEXT NOT NULL, + admitted_seq INTEGER NOT NULL, + promoted_seq INTEGER, + cancelled_seq INTEGER, + created_at INTEGER NOT NULL, + FOREIGN KEY (session_id, thread_id) REFERENCES thread(session_id, id) ON DELETE CASCADE ) `; + yield* sql`CREATE UNIQUE INDEX inbox_admitted_idx ON inbox (session_id, admitted_seq)`; + yield* sql`CREATE UNIQUE INDEX inbox_promoted_idx ON inbox (session_id, promoted_seq)`; + // Pending lanes per thread, drained in admission order. + yield* sql`CREATE INDEX inbox_pending_idx ON inbox (thread_id, promoted_seq, delivery, admitted_seq)`; - // Pending lanes are selected by session and delivery, then drained in - // admission order. SQLite indexes NULL values, so promoted_seq IS NULL - // uses this index for pending-input queries. yield* sql` - CREATE INDEX session_input_pending_idx - ON session_input (session_id, promoted_seq, delivery, admitted_seq) + CREATE TABLE document ( + id TEXT PRIMARY KEY, + session_id TEXT NOT NULL REFERENCES session(id) ON DELETE CASCADE, + thread_id TEXT, + kind TEXT NOT NULL, + history TEXT, + fork TEXT, + created_seq INTEGER NOT NULL, + retired_seq INTEGER, + FOREIGN KEY (session_id, thread_id) REFERENCES thread(session_id, id) ON DELETE CASCADE + ) `; + // One live document per kind and scope. yield* sql` - CREATE UNIQUE INDEX session_input_admitted_seq_idx - ON session_input (session_id, admitted_seq) + CREATE UNIQUE INDEX document_live_idx ON document (session_id, coalesce(thread_id, ''), kind) + WHERE retired_seq IS NULL `; + yield* sql` - CREATE UNIQUE INDEX session_input_promoted_seq_idx - ON session_input (session_id, promoted_seq) + CREATE TABLE revision ( + document_id TEXT NOT NULL REFERENCES document(id) ON DELETE CASCADE, + seq INTEGER NOT NULL, + version INTEGER NOT NULL, + value TEXT NOT NULL, + PRIMARY KEY (document_id, seq) + ) `; }), - - "202609190001_session_host_dir": Effect.gen(function* () { - const sql = yield* SqlClient.SqlClient; - - // The host directory anchor this session belongs to. - // Distinct from `directory`: that is a path inside the session's - // space, possibly on another machine, whereas this one is always on the machine the - // harness runs on. - // - // Nullable, with no default. A session may legitimately have no host project -- one - // created over RPC by a client that did not name one -- and NULL is that fact. It is - // never filled in from the process's own directory: the project layer is simply not - // read. A session can be given one later without being re-created. - yield* sql`ALTER TABLE session ADD COLUMN host_dir TEXT`; - }), - - "202609290001_session_config": Effect.gen(function* () { - const sql = yield* SqlClient.SqlClient; - - // The model and thinking level chosen for this session, as a JSON object - // (`SessionSchema.Config`). Projected from `session.config.changed`, which - // merges into it. NULL: nothing chosen, the session follows its settings. - yield* sql`ALTER TABLE session ADD COLUMN config TEXT`; - }), - - "202610230001_session_input_cancel": Effect.gen(function* () { - const sql = yield* SqlClient.SqlClient; - - // The sequence of the `session.prompt.cancelled` event that withdrew a pending input. - // The row is kept, not deleted: a promotion already in flight then finds it cancelled and - // loses, rather than reading a missing row as a replay and appending the prompt anyway. - yield* sql`ALTER TABLE session_input ADD COLUMN cancelled_seq INTEGER`; - }), - - "202610240001_session_execution": Effect.gen(function* () { - const sql = yield* SqlClient.SqlClient; - - // The run a session has unfinished (`SessionSchema.Execution`), written as it starts and - // ends, so a server starting finds what the last process left without its cooperation. - yield* sql`ALTER TABLE session ADD COLUMN execution TEXT`; - // Times that run was cut off by a process dying. A committed turn is progress and resets it; - // at the limit the session is parked rather than resumed again. - yield* sql`ALTER TABLE session ADD COLUMN restarts INTEGER NOT NULL DEFAULT 0`; - }), }; diff --git a/packages/harness/src/db/schema.sql.ts b/packages/harness/src/db/schema.sql.ts index 2632403a..da63974a 100644 --- a/packages/harness/src/db/schema.sql.ts +++ b/packages/harness/src/db/schema.sql.ts @@ -5,7 +5,9 @@ import { ProjectSchema } from "../project/schema.ts"; import { SandboxInstance } from "../sandbox/instance.ts"; import { AbsolutePath, NonNegativeInt } from "../schema.ts"; import { Prompt } from "../session/prompt/schema.ts"; +import { DocumentSchema } from "../document/schema.ts"; import { SessionSchema } from "../session/schema.ts"; +import { ThreadSchema } from "../thread/schema.ts"; import { SpaceSchema } from "../space/schema.ts"; // Column names derive from field names via the client's camelToSnake @@ -21,21 +23,13 @@ const Metadata = Schema.Record(Schema.String, Schema.String); // Session entry discriminant. Frozen once shipped — values live in persisted // rows, so a rename is a data migration. Additions are code-only (the column is unconstrained TEXT). -export const entryTypes = [ - "user", - "assistant", - "synthetic", - "compaction", - "branchSummary", - "custom", - "configChange", -] as const; +export const entryTypes = ["user", "assistant", "synthetic", "system", "compaction", "reset", "custom"] as const; export type EntryType = (typeof entryTypes)[number]; export const messageEntryTypes = ["user", "assistant", "synthetic"] as const; export type MessageEntryType = (typeof messageEntryTypes)[number]; -// Conversation visibility for one tree node. Non-assistant entries are born +// Conversation visibility for one entry. Non-assistant entries are born // committed; an assistant alone moves draft -> committed | aborted. export const entryStates = ["draft", "committed", "aborted"] as const; export type EntryState = (typeof entryStates)[number]; @@ -51,13 +45,16 @@ export type PartType = (typeof partTypes)[number]; export const toolStatuses = ["pending", "running", "completed", "error", "skipped", "aborted"] as const; export type ToolStatus = (typeof toolStatuses)[number]; +export const inboxKinds = ["prompt"] as const; +export type InboxKind = (typeof inboxKinds)[number]; + export const inputDeliveries = ["steer", "followUp"] as const; export type InputDelivery = (typeof inputDeliveries)[number]; const EventData = Schema.Record(Schema.String, Schema.Unknown); -// Head of the event sequence -export class EventSequenceRow extends Model.Class("EventSequenceRow")({ +// Head of one aggregate's log (aggregate = session), and who holds it. +export class SequenceRow extends Model.Class("SequenceRow")({ aggregateId: Schema.String, seq: Schema.Natural, ownerId: Model.FieldOption(Schema.String), @@ -124,40 +121,46 @@ export class SessionRow extends Model.Class("SessionRow")({ id: SessionSchema.IDFromDb, // Project and env are derived through the space, never denormalised here. spaceId: Schema.String, - parentId: Model.FieldOption(SessionSchema.IDFromDb), slug: Schema.String, - // Absolute realpath of the session cwd; always equal to or under `space.location`. + // Absolute realpath of the sandbox cwd, the default of every thread; at or under `space.location`. directory: AbsolutePath, - // The host directory anchor this session belongs to. + // Host anchor for settings and plugins. Absent = no project layer. hostDir: Model.FieldOption(AbsolutePath), - // The model and thinking level chosen over settings; see `SessionSchema.Config`. - config: Model.FieldOption(Model.JsonFromString(SessionSchema.Config)), - title: Schema.String, tag: Model.FieldOption(Schema.String), metadata: Model.FieldOption(Model.JsonFromString(Metadata)), - // Durable leaf cursor: read anchor (path walks leaf→root) and append anchor - // (new entries attach here). Moved inside every append/branch transaction. - leafEntryId: Model.FieldOption(Schema.String), - // Session-lifetime billed usage, mirroring aikit Usage. Bumped in the - // assistant-append transaction; a cache — assistant envelopes are truth. + ...Timestamps, +}) {} + +// One linear transcript of a session. `origin*` is the flattened `ThreadSchema.Origin`. +export class ThreadRow extends Model.Class("ThreadRow")({ + id: ThreadSchema.IDFromDb, + sessionId: SessionSchema.IDFromDb, + originKind: ThreadSchema.OriginKind, + originThreadId: Model.FieldOption(ThreadSchema.IDFromDb), + originEntryId: Model.FieldOption(Schema.String), + originCallId: Model.FieldOption(Schema.String), + // Absent until the thread's first own prompt names it. + title: Model.FieldOption(Schema.String), + execution: Model.FieldOption(ThreadSchema.Execution), + restarts: Model.GeneratedByDb(Schema.Int), + // Billed usage of own entries only, never the inherited prefix. A cache: assistant envelopes are truth. cost: Model.GeneratedByDb(Schema.Finite), tokensInput: Model.GeneratedByDb(Schema.Int), tokensOutput: Model.GeneratedByDb(Schema.Int), tokensCacheRead: Model.GeneratedByDb(Schema.Int), tokensCacheWrite: Model.GeneratedByDb(Schema.Int), - // The run left unfinished, and how often a dying process cut it off; see `SessionSchema.Execution`. - execution: Model.FieldOption(SessionSchema.Execution), - restarts: Model.GeneratedByDb(Schema.Int), + createdSeq: NonNegativeInt, ...Timestamps, }) {} -// Durable input-queue record. admittedSeq is arrival order; promotedSeq is -// nullable until Control delivers the input to the Loop, then records delivery -// order. Neither sequence is related to session_entry.seq. -export class SessionInputRow extends Model.Class("SessionInputRow")({ +// Work addressed to one thread, from admission to delivery. `kind` + `data` is the flattened item; +// pending = neither `promotedSeq` nor `cancelledSeq`. +export class InboxRow extends Model.Class("InboxRow")({ id: Schema.String, sessionId: SessionSchema.IDFromDb, - prompt: Model.JsonFromString(Prompt), // normalized Prompt, stored as JSON + threadId: ThreadSchema.IDFromDb, + kind: Schema.Literals(inboxKinds), + data: Model.JsonFromString(Prompt), delivery: Schema.Literals(inputDeliveries), admittedSeq: Schema.Int, promotedSeq: Model.FieldOption(Schema.Int), @@ -165,41 +168,59 @@ export class SessionInputRow extends Model.Class("SessionInputR createdAt: Model.DateTimeInsertFromNumber, }) {} -// One timeline/tree node. Identity, tree position, and type are immutable. -// A draft assistant's `data` and `state` are filled in place by lifecycle -// projectors; other payloads are immutable. For message types -// (user/assistant/synthetic), `data` holds the aikit message envelope -// (everything except `parts`); parts live in SessionEntryPartRow. -export class SessionEntryRow extends Model.Class("SessionEntryRow")({ +// One transcript record in one thread. Immutable except a draft assistant, whose `data` and +// `state` are filled in place until it is committed or aborted. For message types +// (user/assistant/synthetic), `data` holds the aikit envelope without `parts`. +export class EntryRow extends Model.Class("EntryRow")({ id: Schema.String, sessionId: SessionSchema.IDFromDb, - parentId: Model.FieldOption(Schema.String), // tree edge; NULL = root - // Position in the session's durable log: the sequence of the event that - // produced this entry. Sparse — events that append nothing (a prompt - // admission, say) leave gaps. Forked entries carry their source positions, - // which is why the fork seeds the new aggregate above them. + threadId: ThreadSchema.IDFromDb, + // The creating event. Sparse: events that append nothing leave gaps. seq: NonNegativeInt, + // The event that committed or aborted it; equals `seq` for entries never drafted. Absent while draft. + committedSeq: Model.FieldOption(NonNegativeInt), type: Schema.Literals(entryTypes), state: Schema.Literals(entryStates), - data: Schema.String, // JSON: full payload, or message envelope - label: Model.FieldOption(Schema.String), // annotation: bookmark text - metadata: Model.FieldOption(Model.JsonFromString(Metadata)), // annotation: engine scratch + data: Schema.String, + label: Model.FieldOption(Schema.String), + metadata: Model.FieldOption(Model.JsonFromString(Metadata)), ...Timestamps, }) {} -// One aikit message part. `data` is the verbatim aikit part JSON and is -// authoritative; `status`/`callId`/`toolName` are promoted copies for indexed -// queries, written together with `data`. The only mutation is toolCall -// settlement (data + status), bounded by turn.end. -export class SessionEntryPartRow extends Model.Class("SessionEntryPartRow")({ +// One aikit message part. `data` is the verbatim aikit part JSON and is authoritative; +// `status`/`callId`/`toolName` are promoted copies for indexed queries. The only mutation is +// toolCall settlement (data + status), bounded by the turn's commit. +export class EntryPartRow extends Model.Class("EntryPartRow")({ id: Schema.String, entryId: Schema.String, - sessionId: SessionSchema.IDFromDb, // denormalized for session-scoped queries - partIndex: Schema.Int, // dense 0..n-1 within the entry; the loop's addressing unit + // Not the session: restart recovery finds a thread's unsettled calls by index. + threadId: ThreadSchema.IDFromDb, + partIndex: Schema.Int, type: Schema.Literals(partTypes), - status: Model.FieldOption(Schema.Literals(toolStatuses)), // toolCall only - callId: Model.FieldOption(Schema.String), // toolCall only - toolName: Model.FieldOption(Schema.String), // toolCall only - data: Schema.String, // verbatim aikit part JSON + status: Model.FieldOption(Schema.Literals(toolStatuses)), + callId: Model.FieldOption(Schema.String), + toolName: Model.FieldOption(Schema.String), + data: Schema.String, ...Timestamps, }) {} + +// JSON state owned by a session (no thread) or a thread. Every change is a revision. +export class DocumentRow extends Model.Class("DocumentRow")({ + id: DocumentSchema.ID, + sessionId: SessionSchema.IDFromDb, + threadId: Model.FieldOption(ThreadSchema.IDFromDb), + kind: Schema.String, + history: Model.FieldOption(DocumentSchema.History), + fork: Model.FieldOption(DocumentSchema.Fork), + createdSeq: NonNegativeInt, + // Set when retired; a new document of the same kind may then be created. + retiredSeq: Model.FieldOption(NonNegativeInt), +}) {} + +// A document's full value at the seq of the event that wrote it. Older versions migrate on read. +export class DocumentRevisionRow extends Model.Class("DocumentRevisionRow")({ + documentId: DocumentSchema.ID, + seq: NonNegativeInt, + version: Schema.Int, + value: Model.JsonFromString(Schema.Record(Schema.String, Schema.Json)), +}) {} diff --git a/packages/harness/src/document/document.ts b/packages/harness/src/document/document.ts new file mode 100644 index 00000000..b932421f --- /dev/null +++ b/packages/harness/src/document/document.ts @@ -0,0 +1,170 @@ +import { Context, Effect, Layer, Option, Schema } from "effect"; +import { SqlClient, SqlSchema } from "effect/sql"; +import { uuidv7 } from "uuidv7"; +import { DocumentRevisionRow, DocumentRow } from "../db/schema.sql.ts"; +import type { SessionSchema } from "../session/schema.ts"; +import type { ThreadSchema } from "../thread/schema.ts"; +import { DocumentSchema } from "./schema.ts"; + +type Value = DocumentRevisionRow["value"]; + +export interface Revision { + readonly seq: number; + readonly version: number; + readonly value: Value; +} + +export interface CreateInput { + readonly sessionId: SessionSchema.ID; + /** Absent = session scope. */ + readonly threadId?: ThreadSchema.ID; + readonly definition: DocumentSchema.Definition; + readonly value: Value; + /** The event that creates it; also its first revision's. */ + readonly seq: number; +} + +export interface Interface { + /** Projector-only: a document and its first revision, at one seq. */ + readonly create: (input: CreateInput) => Effect.Effect; + /** The live document of a kind owned by a thread. */ + readonly find: (threadId: string, kind: string) => Effect.Effect>; + /** A thread's live documents. */ + readonly ofThread: (threadId: string) => Effect.Effect>; + readonly current: (documentId: DocumentSchema.ID) => Effect.Effect>; + /** The value in effect at `seq`: the newest revision at or before it. */ + readonly asOf: (documentId: DocumentSchema.ID, seq: number) => Effect.Effect>; + /** Projector-only: a new full value at the event's seq. A `latest` document drops older revisions. */ + readonly write: (documentId: DocumentSchema.ID, revision: Revision) => Effect.Effect; + /** A thread's `agent` value now; `{}` when it has none. */ + readonly agent: (threadId: string) => Effect.Effect; + /** A thread's `agent` value as of a seq. */ + readonly agentAsOf: (threadId: string, seq: number) => Effect.Effect; + /** + * Projector-only: apply a change to a thread's `agent` and write the result at `seq`. Read and + * written in the event's transaction, so a concurrent change is never lost. + */ + readonly configure: (threadId: string, change: DocumentSchema.AgentChange, seq: number) => Effect.Effect; +} + +export class Service extends Context.Service()("@codeworksh/harness/document/document/Service") {} + +const decodeAgent = Schema.decodeUnknownEffect(DocumentSchema.Agent); +const encodeAgent = Schema.encodeEffect(DocumentSchema.Agent); + +export const layer = Layer.effect( + Service, + Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + + const insertDocument = SqlSchema.void({ + Request: DocumentRow.insert, + execute: (row) => sql`INSERT INTO document ${sql.insert(row)}`, + }); + const insertRevision = SqlSchema.void({ + Request: DocumentRevisionRow.insert, + execute: (row) => sql`INSERT INTO revision ${sql.insert(row)}`, + }); + const findLive = SqlSchema.findOneOption({ + Request: Schema.Struct({ threadId: Schema.String, kind: Schema.String }), + Result: DocumentRow, + execute: ({ threadId, kind }) => sql` + SELECT * FROM document WHERE thread_id = ${threadId} AND kind = ${kind} AND retired_seq IS NULL + `, + }); + const findById = SqlSchema.findOneOption({ + Request: Schema.String, + Result: DocumentRow, + execute: (id) => sql`SELECT * FROM document WHERE id = ${id}`, + }); + const selectOfThread = SqlSchema.findAll({ + Request: Schema.String, + Result: DocumentRow, + execute: (threadId) => sql` + SELECT * FROM document WHERE thread_id = ${threadId} AND retired_seq IS NULL ORDER BY created_seq + `, + }); + const selectRevision = SqlSchema.findOneOption({ + Request: Schema.Struct({ documentId: Schema.String, asOf: Schema.Int }), + Result: DocumentRevisionRow, + execute: ({ documentId, asOf }) => sql` + SELECT * FROM revision WHERE document_id = ${documentId} AND seq <= ${asOf} ORDER BY seq DESC LIMIT 1 + `, + }); + + const toRevision = (row: DocumentRevisionRow): Revision => ({ + seq: row.seq, + version: row.version, + value: row.value, + }); + + const write = Effect.fn("Document.write")(function* (documentId: DocumentSchema.ID, revision: Revision) { + yield* insertRevision({ documentId, ...revision }); + const document = yield* findById(documentId); + if (Option.isSome(document) && Option.getOrUndefined(document.value.history) === "latest") { + yield* sql`DELETE FROM revision WHERE document_id = ${documentId} AND seq < ${revision.seq}`; + } + }, Effect.orDie); + + const create = Effect.fn("Document.create")(function* (input: CreateInput) { + const { definition } = input; + const row = DocumentRow.insert.make({ + id: DocumentSchema.ID.make(uuidv7()), + sessionId: input.sessionId, + threadId: Option.fromUndefinedOr(input.threadId), + kind: definition.kind, + history: Option.fromUndefinedOr(definition.history), + fork: Option.fromUndefinedOr(definition.fork), + createdSeq: input.seq, + retiredSeq: Option.none(), + }); + yield* insertDocument(row); + yield* write(row.id, { seq: input.seq, version: definition.version, value: input.value }); + return new DocumentRow(row); + }, Effect.orDie); + + const find = Effect.fn("Document.find")(function* (threadId: string, kind: string) { + return yield* findLive({ threadId, kind }); + }, Effect.orDie); + + const ofThread = Effect.fn("Document.ofThread")(function* (threadId: string) { + return yield* selectOfThread(threadId); + }, Effect.orDie); + + const asOf = Effect.fn("Document.asOf")(function* (documentId: DocumentSchema.ID, seq: number) { + return Option.map(yield* selectRevision({ documentId, asOf: seq }), toRevision); + }, Effect.orDie); + + const current = (documentId: DocumentSchema.ID) => asOf(documentId, Number.MAX_SAFE_INTEGER); + + const readAgent = Effect.fnUntraced(function* (threadId: string, seq: number) { + const document = yield* find(threadId, DocumentSchema.agent.kind); + if (Option.isNone(document)) return {}; + const revision = yield* asOf(document.value.id, seq); + if (Option.isNone(revision)) return {}; + return yield* decodeAgent(revision.value.value).pipe(Effect.orDie); + }); + + const agent = Effect.fn("Document.agent")((threadId: string) => readAgent(threadId, Number.MAX_SAFE_INTEGER)); + const agentAsOf = Effect.fn("Document.agentAsOf")(readAgent); + + const configure = Effect.fn("Document.configure")(function* ( + threadId: string, + change: DocumentSchema.AgentChange, + seq: number, + ) { + const document = yield* find(threadId, DocumentSchema.agent.kind); + if (Option.isNone(document)) return yield* Effect.die(`thread ${threadId} has no agent document`); + const next = DocumentSchema.applyAgentChange(yield* readAgent(threadId, Number.MAX_SAFE_INTEGER), change); + yield* write(document.value.id, { + seq, + version: DocumentSchema.agent.version, + value: yield* encodeAgent(next).pipe(Effect.orDie), + }); + }); + + return Service.of({ create, find, ofThread, current, asOf, write, agent, agentAsOf, configure }); + }), +); + +export * as Document from "./document.ts"; diff --git a/packages/harness/src/document/schema.ts b/packages/harness/src/document/schema.ts new file mode 100644 index 00000000..90725581 --- /dev/null +++ b/packages/harness/src/document/schema.ts @@ -0,0 +1,82 @@ +import { Model } from "@codeworksh/aikit"; +import { Schema } from "effect"; +import { AbsolutePath, optional } from "../schema.ts"; + +export const ID = Schema.String.pipe(Schema.brand("Document.ID")); +export type ID = typeof ID.Type; + +/** latest = only the newest value matters; rewindable = readable as of any seq. */ +export const History = Schema.Literals(["latest", "rewindable"]); +export type History = typeof History.Type; + +/** What a fork's new thread gets: the value at the cut, the value now, or nothing. */ +export const Fork = Schema.Literals(["asOf", "current", "initial"]); +export type Fork = typeof Fork.Type; + +/** + * A document kind, declared once in code. Storage keeps scope and policy on the row, so a document + * survives when its definition is gone. + */ +export interface Definition { + readonly kind: string; + readonly version: number; + readonly scope: "session" | "thread"; + /** Thread scope only. */ + readonly history?: History; + /** Thread scope only; `asOf` needs `rewindable`. */ + readonly fork?: Fork; +} + +export const ThinkingLevel = Schema.Literals(Object.values(Model.ThinkingLevelEnum)); + +export const ModelRef = Schema.Struct({ provider: Schema.String, id: Schema.String }); + +/** Physical boundary of a thread: which space, and the cwd inside it. Set together, never one alone. */ +export const Placement = Schema.Struct({ spaceId: Schema.String, directory: AbsolutePath }); +export type Placement = typeof Placement.Type; + +export const ModelTools = Schema.Struct({ + add: optional(Schema.Array(Schema.String)), + remove: optional(Schema.Array(Schema.String)), +}); + +/** Choices made for one thread; an unset field follows settings. */ +export const Agent = Schema.Struct({ + model: optional(ModelRef), + thinkingLevel: optional(ThinkingLevel), + /** Deferred tools offered to the model (`add`) or withheld (`remove`). */ + modelTools: optional(ModelTools), + /** Physical boundary override; absent = the session's. */ + placement: optional(Placement), +}); +export type Agent = typeof Agent.Type; + +/** A change to {@link Agent}: a given field replaces the stored one, `null` clears it, absent leaves it. */ +export const AgentChange = Schema.Struct({ + model: optional(Schema.NullOr(ModelRef)), + thinkingLevel: optional(Schema.NullOr(ThinkingLevel)), + modelTools: optional(Schema.NullOr(ModelTools)), + placement: optional(Schema.NullOr(Placement)), +}); +export type AgentChange = typeof AgentChange.Type; + +export const applyAgentChange = (current: Agent, change: AgentChange): Agent => { + const next: Record = { ...current }; + for (const [key, value] of Object.entries(change)) { + if (value === undefined) continue; + if (value === null) delete next[key]; + else next[key] = value; + } + return next as Agent; +}; + +/** Built in: created with every thread, replaces a session-wide configuration. */ +export const agent: Definition = { + kind: "agent", + version: 1, + scope: "thread", + history: "rewindable", + fork: "asOf", +}; + +export * as DocumentSchema from "./schema.ts"; diff --git a/packages/harness/src/effect.ts b/packages/harness/src/effect.ts index 39a85f37..7066f83a 100644 --- a/packages/harness/src/effect.ts +++ b/packages/harness/src/effect.ts @@ -11,6 +11,9 @@ export { DateTimeUtcFromMillis, optional } from "./schema.ts"; export { SandboxError } from "./sandbox/errors.ts"; export { PromptSchema } from "./session/prompt/schema.ts"; export { Session as SessionStore } from "./session/session.ts"; +export { Thread as ThreadStore } from "./thread/thread.ts"; +export { ThreadSchema } from "./thread/schema.ts"; +export { DocumentSchema } from "./document/schema.ts"; export { Runner } from "./runner/run.ts"; export { Location } from "./location/location.ts"; export { Project } from "./project/project.ts"; diff --git a/packages/harness/src/effect/session.ts b/packages/harness/src/effect/session.ts index 68019552..b6b3fcdd 100644 --- a/packages/harness/src/effect/session.ts +++ b/packages/harness/src/effect/session.ts @@ -14,8 +14,13 @@ import { SessionMessageSchema } from "../session/message/schema.ts"; import type { Delivery } from "../session/prompt/schema.ts"; import { PromptSchema } from "../session/prompt/schema.ts"; import * as SessionRuntime from "../session/runtime.ts"; +import type { ThreadRow } from "../db/schema.sql.ts"; +import { Document } from "../document/document.ts"; +import type { DocumentSchema } from "../document/schema.ts"; import { SessionSchema } from "../session/schema.ts"; import { Session as SessionStore } from "../session/session.ts"; +import { ThreadSchema } from "../thread/schema.ts"; +import { Thread } from "../thread/thread.ts"; import { State } from "../state/state.ts"; import { rooted } from "../util/path.ts"; import type { Info as SandboxInfo } from "./sandbox.ts"; @@ -43,6 +48,7 @@ export interface RuntimeInput { } export interface CreateInput extends RuntimeInput { + /** The main thread's title; absent = its first prompt names it. */ readonly title?: string; readonly sandbox?: SandboxInfo; readonly directory?: string; @@ -63,10 +69,14 @@ export interface CreateInput extends RuntimeInput { export interface AttachInput extends RuntimeInput { readonly sessionId: SessionSchema.ID; + /** Absent = the session's `main` thread. */ + readonly threadId?: ThreadSchema.ID; } export interface RelinkInput { readonly sessionId: SessionSchema.ID; + /** Absent moves the session's default; present sets that thread's placement. */ + readonly threadId?: ThreadSchema.ID; readonly sandbox?: SandboxInfo; readonly directory?: string; } @@ -87,46 +97,87 @@ type PromptResult = Effect.Effect, Effect.Error; - readonly info: Effect.Effect; + readonly info: Effect.Effect; readonly prompt: (input: PromptInput) => PromptResult; readonly run: (input: PromptInput) => PromptResult; readonly wait: () => ReturnType; readonly resume: () => ReturnType; /** Stops active work and waits for its cleanup; idle is a no-op returning false. */ readonly interrupt: () => ReturnType; - /** Live session notifications. Use Event.log for durable replay. */ + /** Records model and thinking choices in the thread's `agent` document. */ + readonly configure: (input: RuntimeInput) => Effect.Effect; + /** A new thread cut at a committed entry of this one's line; `text` refills the editor for `before`. */ + readonly fork: ( + input: ForkInput, + ) => Effect.Effect< + { readonly thread: ThreadHandle; readonly text?: string }, + Thread.ThreadNotFoundError | Thread.ForkError + >; + /** Live notifications of this thread. Use Event.log for durable replay. */ readonly events: () => Stream.Stream; - readonly path: () => Effect.Effect>; + /** The thread's line: inherited entries through the fork point, then its own. */ + readonly history: () => Effect.Effect>; +} + +export interface Handle { + readonly id: SessionSchema.ID; + readonly info: Effect.Effect; + /** The session's `main` thread, where unaddressed prompts go. */ + readonly main: ThreadHandle; + /** One of the session's threads; `main` names the main one. */ + readonly thread: ( + id: ThreadSchema.ID | typeof ThreadSchema.MAIN, + ) => Effect.Effect; + readonly threads: Effect.Effect>; + /** Stops every running thread and waits for their cleanup; false when none was running. */ + readonly interrupt: () => Effect.Effect; } -/** What stays in this process. The model and thinking level are the session's {@link chosen} config. */ +/** What stays in this process. The model and thinking level are the thread's {@link chosen} `agent`. */ const runtimeBindings = (input: RuntimeInput): SessionRuntime.Bindings => ({ ...input.model?.options, ...(input.tools?.execution === undefined ? {} : { toolExecution: input.tools.execution }), @@ -134,18 +185,22 @@ const runtimeBindings = (input: RuntimeInput): SessionRuntime.Bindings => ({ ...(input.systemPrompt?.append === undefined ? {} : { promptSystemAppend: input.systemPrompt.append }), }); -/** The keys of the session's durable config this input names. */ -const chosen = (input: RuntimeInput): SessionSchema.Config => ({ +/** The `agent` fields this input names. */ +const chosen = (input: RuntimeInput): DocumentSchema.Agent => ({ ...(input.model === undefined ? {} : { model: { provider: input.model.provider, id: input.model.id } }), ...(input.thinkingLevel === undefined ? {} : { thinkingLevel: input.thinkingLevel }), }); -/** Records a config choice; the projector merges it into the session row in the same commit. */ -const choose = Effect.fn("Session.choose")(function* (sessionId: SessionSchema.ID, input: RuntimeInput) { - const config = chosen(input); - if (Object.keys(config).length === 0) return; +/** Records a choice as one `agent` revision; nothing when the input names none. */ +const choose = Effect.fn("Session.choose")(function* ( + sessionId: SessionSchema.ID, + threadId: ThreadSchema.ID, + input: RuntimeInput, +) { + const change = chosen(input); + if (Object.keys(change).length === 0) return; const events = yield* Event.Service; - yield* events.publish(EventList.ConfigChanged, { sessionId, timestamp: yield* DateTime.now, ...config }); + yield* events.publish(EventList.ThreadConfigured, { sessionId, threadId, timestamp: yield* DateTime.now, change }); }); const promptInput = Effect.fnUntraced(function* (input: PromptInput) { @@ -164,63 +219,150 @@ const promptInput = Effect.fnUntraced(function* (input: PromptInput) { }; }); -const makeHandle = Effect.fn("Session.makeHandle")(function* (id: SessionSchema.ID) { +export const info = Effect.fn("Session.info")(function* (id: SessionSchema.ID) { const sessions = yield* SessionStore.Service; - const control = yield* Control.Service; - const events = yield* Event.Service; + const threads = yield* Thread.Service; const sandboxes = yield* SandboxController.Controller; + const found = yield* sessions.get(id); + const main = yield* threads.main(id); + if (Option.isNone(found) || Option.isNone(main)) + return yield* new SessionStore.SessionNotFoundError({ sessionId: id }); + const row = found.value; + // The env is the space's. A missing space or a destroyed env both read as + // "no sandbox": the handle stays readable; running it is the mount's call, + // which refuses a removed instance. + const space = Option.getOrUndefined(yield* sessions.space(id)); + const sandbox = + space === undefined || space.env === SandboxInstanceSchema.ID.local + ? undefined + : Option.getOrUndefined(yield* sandboxes.get(space.env)); + const hostDir = Option.getOrUndefined(row.hostDir); + const project = Option.getOrUndefined(yield* sessions.project(id)); + return { + id, + directory: row.directory, + ...(hostDir === undefined ? {} : { hostDir }), + hasHostLink: hostDir !== undefined, + ...(sandbox === undefined ? {} : { sandbox }), + ...(project === undefined ? {} : { project: { id: project.id, name: project.name } }), + updatedAt: DateTime.toEpochMillis(row.updatedAt), + mainThreadId: ThreadSchema.ID.make(main.value.id), + } satisfies Info; +}); - const info = Effect.gen(function* () { - const found = yield* sessions.get(id); - if (Option.isNone(found)) return yield* new SessionStore.SessionNotFoundError({ sessionId: id }); - const row = found.value; - // The env is the space's. A missing space or a destroyed env both read as - // "no sandbox": the handle stays readable (title, directory); running it - // is the mount's call, which refuses a removed instance. - const space = Option.getOrUndefined(yield* sessions.space(id)); - const sandbox = - space === undefined || space.env === SandboxInstanceSchema.ID.local - ? undefined - : Option.getOrUndefined(yield* sandboxes.get(space.env)); - const hostDir = Option.getOrUndefined(row.hostDir); - const project = Option.getOrUndefined(yield* sessions.project(id)); - const config = Option.getOrUndefined(row.config); - const model = config?.model; - const thinkingLevel = config?.thinkingLevel; - const parked = - Option.isSome(row.execution) && row.restarts >= Control.RESTART_LIMIT && !(yield* control.active).has(id); - return { - id, - title: row.title, - directory: row.directory, - ...(hostDir === undefined ? {} : { hostDir }), - hasHostLink: hostDir !== undefined, - ...(sandbox === undefined ? {} : { sandbox }), - ...(project === undefined ? {} : { project: { id: project.id, name: project.name } }), - updatedAt: DateTime.toEpochMillis(row.updatedAt), - ...(model === undefined ? {} : { model }), - ...(thinkingLevel === undefined ? {} : { thinkingLevel }), - parked, - }; - }).pipe(Effect.withSpan("Session.info")); +const originOf = (row: ThreadRow): ThreadSchema.Origin => { + const threadId = Option.getOrUndefined(row.originThreadId); + const entryId = Option.getOrUndefined(row.originEntryId); + const callId = Option.getOrUndefined(row.originCallId); + if (row.originKind === "fork" && threadId !== undefined && entryId !== undefined) + return { kind: "fork", threadId, entryId }; + if (row.originKind === "subagent" && threadId !== undefined && callId !== undefined) + return { kind: "subagent", threadId, callId, ...(entryId === undefined ? {} : { entryId }) }; + return { kind: "main" }; +}; +export const threadInfo = Effect.fn("Session.threadInfo")(function* (id: ThreadSchema.ID) { + const threads = yield* Thread.Service; + const documents = yield* Document.Service; + const control = yield* Control.Service; + const found = yield* threads.get(id); + if (Option.isNone(found)) return yield* new Thread.ThreadNotFoundError({ threadId: id }); + const row = found.value; + const agent = yield* documents.agent(id); + const title = Option.getOrUndefined(row.title); + const parked = + Option.isSome(row.execution) && row.restarts >= Control.RESTART_LIMIT && !(yield* control.active).has(id); return { id, + sessionId: SessionSchema.ID.make(row.sessionId), + origin: originOf(row), + ...(title === undefined ? {} : { title }), + updatedAt: DateTime.toEpochMillis(row.updatedAt), + ...(agent.model === undefined ? {} : { model: agent.model }), + ...(agent.thinkingLevel === undefined ? {} : { thinkingLevel: agent.thinkingLevel }), + ...(agent.placement === undefined ? {} : { placement: agent.placement }), + parked, + session: yield* info(SessionSchema.ID.make(row.sessionId)), + } satisfies ThreadInfo; +}); + +const makeThread = Effect.fn("Session.makeThread")(function* (sessionId: SessionSchema.ID, id: ThreadSchema.ID) { + const context = yield* Effect.context< + | SessionStore.Service + | Thread.Service + | Document.Service + | Control.Service + | Event.Service + | SandboxController.Controller + >(); + const threads = yield* Thread.Service; + const control = yield* Control.Service; + const events = yield* Event.Service; + const handle: ThreadHandle = { + id, + sessionId, active: control.active.pipe(Effect.map((active) => active.has(id))), - info, + info: threadInfo(id).pipe(Effect.provideContext(context)), prompt: (input) => - promptInput(input).pipe(Effect.flatMap((prompt) => control.prompt({ sessionId: id, ...prompt }))), - run: (input) => promptInput(input).pipe(Effect.flatMap((prompt) => control.run({ sessionId: id, ...prompt }))), + promptInput(input).pipe(Effect.flatMap((prompt) => control.prompt({ sessionId, threadId: id, ...prompt }))), + run: (input) => + promptInput(input).pipe(Effect.flatMap((prompt) => control.run({ sessionId, threadId: id, ...prompt }))), wait: () => control.wait(id), resume: () => control.resume(id), // The embedder API keeps its blocking contract: a caller that returns from // `interrupt()` can assume the drain released its mount. interrupt: () => control.interrupt(id, { awaitSettlement: true }), + configure: (input) => choose(sessionId, id, input).pipe(Effect.provideContext(context)), + fork: (input) => + threads.fork({ threadId: id, ...input }).pipe( + Effect.flatMap((forked) => + makeThread(sessionId, ThreadSchema.ID.make(forked.thread.id)).pipe( + Effect.provideContext(context), + Effect.map((thread) => ({ thread, ...(forked.text === undefined ? {} : { text: forked.text }) })), + ), + ), + ), events: () => events .subscribe() - .pipe(Stream.filter((event) => (event.data as { readonly sessionId?: unknown }).sessionId === id)), - path: () => sessions.path(id), + .pipe(Stream.filter((event) => (event.data as { readonly threadId?: unknown }).threadId === id)), + history: () => threads.line(id), + }; + return handle; +}); + +const makeHandle = Effect.fn("Session.makeHandle")(function* (id: SessionSchema.ID) { + const context = yield* Effect.context< + | SessionStore.Service + | Thread.Service + | Document.Service + | Control.Service + | Event.Service + | SandboxController.Controller + >(); + const threads = yield* Thread.Service; + const control = yield* Control.Service; + const main = yield* threads.main(id); + if (Option.isNone(main)) return yield* new SessionStore.SessionNotFoundError({ sessionId: id }); + const mainHandle = yield* makeThread(id, ThreadSchema.ID.make(main.value.id)); + return { + id, + info: info(id).pipe(Effect.provideContext(context)), + main: mainHandle, + thread: (threadId) => + Effect.gen(function* () { + if (threadId === ThreadSchema.MAIN) return mainHandle; + const found = yield* threads.get(threadId); + if (Option.isNone(found) || found.value.sessionId !== id) { + return yield* new Thread.ThreadNotFoundError({ threadId }); + } + return yield* makeThread(id, threadId); + }).pipe(Effect.provideContext(context)), + threads: threads.list(id).pipe( + Effect.flatMap((rows) => Effect.forEach(rows, (row) => makeThread(id, ThreadSchema.ID.make(row.id)))), + Effect.provideContext(context), + ), + interrupt: () => control.interruptSession(id, { awaitSettlement: true }), } satisfies Handle; }); @@ -244,12 +386,12 @@ export const create = Effect.fn("Session.create")(function* (input: CreateInput id, spaceId: location.space.id, directory: location.directory, - title: input.title ?? SessionSchema.DEFAULT_TITLE, + ...(input.title === undefined ? {} : { title: input.title }), ...(input.hostDir === undefined ? {} : { hostDir: declaredHostDir(input.hostDir) }), + agent: chosen(input), }); yield* runtime.set(id, runtimeBindings(input)); - yield* choose(id, input); - return yield* makeHandle(id); + return yield* makeHandle(id).pipe(Effect.orDie); }); /** @@ -294,6 +436,9 @@ export const link = Effect.fn("Session.link")(function* (input: { * resolved into its space exactly like `create`; the session keeps its * position under the new root when that absolute path exists there, else it * lands on the resolved directory. The transcript is untouched. + * + * With `threadId`, only that thread moves: its `agent.placement` is set, and the session's + * default, which every other thread follows, stays. */ export const relink = Effect.fn("Session.relink")(function* (input: RelinkInput) { const sessions = yield* SessionStore.Service; @@ -304,17 +449,29 @@ export const relink = Effect.fn("Session.relink")(function* (input: RelinkInput) Effect.gen(function* () { const location = yield* Location.Service.use(Effect.succeed).pipe(Effect.provide(Location.layerMounted())); const session = yield* sessions.get(input.sessionId); - const current = yield* sessions.space(input.sessionId); - const rebased = - Option.isSome(session) && Option.isSome(current) - ? SessionStore.rebaseDirectory(current.value.location, location.space.location, session.value.directory) - : undefined; + const own = yield* sessions.space(input.sessionId); + const current = + input.threadId === undefined + ? Option.flatMap(session, (row) => Option.map(own, (space) => ({ space, directory: row.directory }))) + : yield* sessions.placement(input.threadId); + const rebased = Option.isSome(current) + ? SessionStore.rebaseDirectory( + current.value.space.location, + location.space.location, + current.value.directory, + ) + : undefined; const fs = yield* SandboxIO.FileSystem; const directory = rebased !== undefined && (yield* fs.exists(rebased).pipe(Effect.orElseSucceed(() => false))) ? rebased : location.directory; - yield* sessions.relink({ sessionId: input.sessionId, spaceId: location.space.id, directory }); + yield* sessions.relink({ + sessionId: input.sessionId, + ...(input.threadId === undefined ? {} : { threadId: input.threadId }), + spaceId: location.space.id, + directory, + }); return yield* makeHandle(input.sessionId); }), input.directory === undefined ? undefined : { cwd: input.directory }, @@ -325,39 +482,52 @@ export const get = Effect.fn("Session.get")(function* (sessionId: SessionSchema. const sessions = yield* SessionStore.Service; const found = yield* sessions.get(sessionId); if (Option.isNone(found)) return Option.none(); - return Option.some(yield* makeHandle(sessionId)); + return Option.some(yield* makeHandle(sessionId).pipe(Effect.orDie)); }); +/** + * A thread of an existing session (its `main` when `threadId` is absent), with this process's + * bindings merged in and any model or thinking choice recorded in its `agent` document. + */ export const attach = Effect.fn("Session.attach")(function* (input: AttachInput) { const found = yield* get(input.sessionId); if (Option.isNone(found)) { return yield* new SessionStore.SessionNotFoundError({ sessionId: input.sessionId }); } + const thread = yield* found.value.thread(input.threadId ?? ThreadSchema.MAIN); const runtime = yield* SessionRuntime.Service; /* * Merge, not replace. `runtimeBindings` emits only the keys this call names, so a * bare `attach({ sessionId })` produces `{}` -- and a replace would silently drop the - * tool execution mode, prompt inputs, and model a previous attach established. Bindings are now - * the only config layer, so there is nothing behind them to restore what a wipe took. + * tool execution mode, prompt inputs, and model a previous attach established. */ yield* runtime.update(input.sessionId, runtimeBindings(input)); - yield* choose(input.sessionId, input); - return found.value; + yield* choose(input.sessionId, thread.id, input); + return thread; +}); + +/** Removes a session after stopping its threads. False when it did not exist. */ +export const remove = Effect.fn("Session.delete")(function* (sessionId: SessionSchema.ID) { + const control = yield* Control.Service; + const runtime = yield* SessionRuntime.Service; + const deleted = yield* control.deleteSession(sessionId); + yield* runtime.remove(sessionId); + return deleted; }); /** - * The model and thinking level the session's next turn runs with. A choice made through - * {@link create} or {@link attach} is saved with the session and outlives the process; without - * one, the session follows its settings. + * The model and thinking level a thread's next turn runs with. A choice made through + * {@link create}, {@link attach} or `configure` is saved with the thread and outlives the + * process; without one, the thread follows its settings. */ -export const configuration = Effect.fn("Session.configuration")(function* (sessionId: SessionSchema.ID) { - const sessions = yield* SessionStore.Service; - if (Option.isNone(yield* sessions.get(sessionId))) { - return yield* new SessionStore.SessionNotFoundError({ sessionId }); +export const configuration = Effect.fn("Session.configuration")(function* (threadId: ThreadSchema.ID) { + const threads = yield* Thread.Service; + if (Option.isNone(yield* threads.get(threadId))) { + return yield* new Thread.ThreadNotFoundError({ threadId }); } - return yield* State.Service.use((state) => state.configuration(sessionId)); + return yield* State.Service.use((state) => state.configuration(threadId)); }); -export { AbsolutePath, SessionMessageSchema, SessionSchema }; +export { AbsolutePath, SessionMessageSchema, SessionSchema, ThreadSchema }; export * as Session from "./session.ts"; diff --git a/packages/harness/src/event/event.ts b/packages/harness/src/event/event.ts index 5677495f..808f9b80 100644 --- a/packages/harness/src/event/event.ts +++ b/packages/harness/src/event/event.ts @@ -59,7 +59,7 @@ export type Manifest = { export type ReadAggregateInput = { readonly aggregateId: string; - /** Exclusive lower bound. Omitted reads from the first stored sequence (0). */ + /** Exclusive lower bound. Omitted reads from the first stored sequence (1). */ readonly after?: number; readonly limit: number; readonly manifest: Manifest; @@ -72,7 +72,7 @@ export type ReadAggregateResult = { /** * Marks completion of the initial journal read through the captured sequence. - * Emitted once per `log` stream; omitted `seq` means the head was -1. + * Emitted once per `log` stream; omitted `seq` means the log is empty. * This is a replay boundary, not an acknowledgment of consumer processing. */ export type Synced = { @@ -88,7 +88,7 @@ export const isSynced = (item: LogItem): item is Synced => item.type === "log.sy export type LogInput = { readonly aggregateId: string; - /** Exclusive lower bound. Omitted reads from the first stored sequence (0). */ + /** Exclusive lower bound. Omitted reads from the first stored sequence (1). */ readonly after?: number; /** Keep the stream open past the marker, appending events as they commit. */ readonly follow?: boolean; @@ -141,7 +141,7 @@ export interface Subscribe { } export interface Interface { - /** Current sequence head, or -1 if absent. A version position, not a row count. */ + /** Current sequence head, or 0 if absent: every log starts at 1. A version position, not a row count. */ readonly latestSequence: (aggregateId: string) => Effect.Effect; /** * Reads one page of stored events in sequence order, filtered and decoded by @@ -278,14 +278,14 @@ export const layer = Layer.effect( const findLatestSequence = SqlSchema.findOneOption({ Request: Schema.String, Result: Schema.Struct({ seq: Schema.Int }), - execute: (aggregateId) => sql`SELECT seq FROM event_sequence WHERE aggregate_id = ${aggregateId}`, + execute: (aggregateId) => sql`SELECT seq FROM sequence WHERE aggregate_id = ${aggregateId}`, }); - // -1 for an aggregate that has never been written, so that the first - // sequence a publisher assigns (`latest + 1`) is 0. + // 0 for an aggregate that has never been written, so that the first + // sequence a publisher assigns (`latest + 1`) is 1. const latestSequence = Effect.fn("Event.latestSequence")(function* (aggregateId: string) { return yield* findLatestSequence(aggregateId).pipe( - Effect.map(Option.match({ onNone: () => -1, onSome: (row) => row.seq })), + Effect.map(Option.match({ onNone: () => 0, onSome: (row) => row.seq })), Effect.orDie, ); }); @@ -342,7 +342,7 @@ export const layer = Layer.effect( const readAggregate = Effect.fn("Event.readAggregate")(function* (input: ReadAggregateInput) { const rows = yield* selectAggregateEvents({ aggregateId: input.aggregateId, - after: input.after ?? -1, + after: input.after ?? 0, through: UNBOUNDED, types: Array.from(input.manifest.definitions.keys()), limit: input.limit + 1, @@ -443,8 +443,8 @@ export const layer = Layer.effect( Request: Schema.String, Result: Schema.Struct({ seq: Schema.Int }), execute: (aggregateId) => sql` - INSERT INTO event_sequence (aggregate_id, seq) VALUES (${aggregateId}, 0) - ON CONFLICT(aggregate_id) DO UPDATE SET seq = event_sequence.seq + 1 + INSERT INTO sequence (aggregate_id, seq) VALUES (${aggregateId}, 1) + ON CONFLICT(aggregate_id) DO UPDATE SET seq = sequence.seq + 1 RETURNING seq `, }); @@ -550,8 +550,8 @@ export const layer = Layer.effect( const advanceSequence = SqlSchema.void({ Request: Schema.Struct({ aggregateId: Schema.String, seq: Schema.Int }), execute: ({ aggregateId, seq }) => sql` - INSERT INTO event_sequence (aggregate_id, seq) VALUES (${aggregateId}, ${seq}) - ON CONFLICT(aggregate_id) DO UPDATE SET seq = MAX(event_sequence.seq, excluded.seq) + INSERT INTO sequence (aggregate_id, seq) VALUES (${aggregateId}, ${seq}) + ON CONFLICT(aggregate_id) DO UPDATE SET seq = MAX(sequence.seq, excluded.seq) `, }); @@ -623,7 +623,7 @@ export const layer = Layer.effect( const definitions = input.definitions ?? EventManifest.Manifest.definitions; // The cursor outlives a single catch-up: each wake resumes from the // last sequence actually emitted, not from where the pass began. - const cursor = yield* Ref.make(input.after ?? -1); + const cursor = yield* Ref.make(input.after ?? 0); const catchUp = (through: number): Stream.Stream => Stream.unwrap( Ref.get(cursor).pipe( @@ -661,7 +661,7 @@ export const layer = Layer.effect( const marker: Synced = { type: "log.synced", aggregateId: input.aggregateId, - ...(head >= 0 ? { seq: head } : {}), + ...(head > 0 ? { seq: head } : {}), }; const replay = catchUp(head).pipe(Stream.concat(Stream.make(marker))); if (!wakes) return replay; diff --git a/packages/harness/src/event/list.ts b/packages/harness/src/event/list.ts index 46607344..a048a796 100644 --- a/packages/harness/src/event/list.ts +++ b/packages/harness/src/event/list.ts @@ -4,12 +4,20 @@ import { SessionFailure } from "../session/failure.ts"; import { SessionMessageSchema } from "../session/message/schema.ts"; import { PromptSchema } from "../session/prompt/schema.ts"; import { SessionSchema } from "../session/schema.ts"; +import { DocumentSchema } from "../document/schema.ts"; +import { ThreadSchema } from "../thread/schema.ts"; +import { AbsolutePath } from "../schema.ts"; import { EventSchema } from "./schema.ts"; -const baseOptions = { +const sessionOptions = { timestamp: DateTimeUtcFromMillis, sessionId: SessionSchema.ID, }; +// The aggregate stays the session: every thread of it writes one log, one seq space. +const baseOptions = { + ...sessionOptions, + threadId: ThreadSchema.ID, +}; const durableOptions = { durable: { aggregate: "sessionId", @@ -62,12 +70,47 @@ export const PromptUpdated = EventSchema.define({ }); export type PromptUpdated = typeof PromptUpdated.Type; -export const ConfigChanged = EventSchema.define({ - type: "session.config.changed", +/** A session and its `main` thread, whose `agent` document starts at `agent`. */ +export const SessionCreated = EventSchema.define({ + type: "session.created", ...durableOptions, - schema: { ...baseOptions, ...SessionSchema.Config.fields }, + schema: { + ...baseOptions, + spaceId: Schema.String, + slug: Schema.String, + directory: AbsolutePath, + hostDir: optional(AbsolutePath), + tag: optional(Schema.String), + metadata: optional(Schema.Record(Schema.String, Schema.String)), + title: optional(Schema.String), + agent: DocumentSchema.Agent, + }, }); -export type ConfigChanged = typeof ConfigChanged.Type; +export type SessionCreated = typeof SessionCreated.Type; + +/** + * A new thread whose history continues from `originThreadId` through `entryId`. `sourceThreadId` is + * the thread the fork was asked of; the origin is the thread that owns the entry. + */ +export const ThreadForked = EventSchema.define({ + type: "thread.forked", + ...durableOptions, + schema: { + ...baseOptions, + sourceThreadId: ThreadSchema.ID, + originThreadId: ThreadSchema.ID, + entryId: Schema.String, + }, +}); +export type ThreadForked = typeof ThreadForked.Type; + +/** A change to a thread's `agent` document; one revision at this event's seq. */ +export const ThreadConfigured = EventSchema.define({ + type: "thread.configured", + ...durableOptions, + schema: { ...baseOptions, change: DocumentSchema.AgentChange }, +}); +export type ThreadConfigured = typeof ThreadConfigured.Type; const LLMFields = { ...baseOptions, @@ -231,27 +274,6 @@ export const ToolSettled = EventSchema.define({ }); export type ToolSettled = typeof ToolSettled.Type; -/** - * First event in a forked session's log. The aggregate is the *new* session, so - * this lands at `baseSeq + 1`: the fork seeds the new aggregate's sequence to - * the fork point, reserving [0..baseSeq] for the copied entries. Recording it - * makes the seeded range auditable from the log itself, not only from - * `session.parentId`. - */ -export const SessionForked = EventSchema.define({ - type: "session.next.forked", - ...durableOptions, - schema: { - ...baseOptions, - sourceSessionId: SessionSchema.ID, - /** Entry in the source session the copy stops at (its leaf, for a clone). */ - sourceEntryId: Schema.String, - /** Highest sequence carried over; the new log starts above it. */ - baseSeq: NonNegativeInt, - }, -}); -export type SessionForked = typeof SessionForked.Type; - /** * Lifecycle of a process-owned busy period. One `started` and exactly one * terminal per period, covering every drain the period coalesced. The terminals @@ -296,11 +318,12 @@ export const PluginUpdated = EventSchema.define({ file: optional(Schema.String), error: optional(Schema.String), /** - * The session whose turn triggered the event, when one did -- `failed` from a - * session's own `follow` names it. Absent on `reload` and boot, which no session owns: + * The thread whose turn triggered the event, when one did -- `failed` from a + * turn's own `follow` names it. Absent on `reload` and boot, which no thread owns: * the pool is process-wide, so this is the trigger, never an attribution of ownership. */ sessionId: optional(SessionSchema.ID), + threadId: optional(ThreadSchema.ID), }, }); export type PluginUpdated = typeof PluginUpdated.Type; @@ -318,8 +341,9 @@ export const Definitions = EventSchema.inventory( Prompted, PromptCancelled, PromptUpdated, - ConfigChanged, - SessionForked, + SessionCreated, + ThreadForked, + ThreadConfigured, TurnStarted, TurnEnded, TurnAborted, @@ -352,8 +376,9 @@ export const DurableDefinitions = EventSchema.inventory( Prompted, PromptCancelled, PromptUpdated, - ConfigChanged, - SessionForked, + SessionCreated, + ThreadForked, + ThreadConfigured, TurnEnded, LLMStarted, LLMEnded, diff --git a/packages/harness/src/session/input/input.ts b/packages/harness/src/inbox/inbox.ts similarity index 68% rename from packages/harness/src/session/input/input.ts rename to packages/harness/src/inbox/inbox.ts index ceccdddf..ba95065e 100644 --- a/packages/harness/src/session/input/input.ts +++ b/packages/harness/src/inbox/inbox.ts @@ -1,13 +1,14 @@ import { DateTime, Effect, Option, Schema } from "effect"; import { VariantSchema } from "effect/schema"; import { SqlClient, SqlSchema } from "effect/sql"; -import { SessionInputRow } from "../../db/schema.sql.ts"; -import { Event } from "../../event/event.ts"; -import { EventList } from "../../event/list.ts"; -import { SessionMessageSchema } from "../message/schema.ts"; -import { Delivery, Prompt } from "../prompt/schema.ts"; -import { SessionSchema } from "../schema.ts"; -import { DateTimeUtcFromMillis } from "../../schema.ts"; +import { InboxRow } from "../db/schema.sql.ts"; +import { Event } from "../event/event.ts"; +import { EventList } from "../event/list.ts"; +import { SessionMessageSchema } from "../session/message/schema.ts"; +import { Delivery, Prompt } from "../session/prompt/schema.ts"; +import { SessionSchema } from "../session/schema.ts"; +import { ThreadSchema } from "../thread/schema.ts"; +import { DateTimeUtcFromMillis } from "../schema.ts"; import { Admitted } from "./schema.ts"; /** @@ -18,18 +19,19 @@ import { Admitted } from "./schema.ts"; * raising it rolls the event back and leaves the store untouched. `admit` and * `promote*` then recover by reading the row that won. */ -export class LifecycleConflict extends Schema.TaggedError()("SessionInput.LifecycleConflict", { +export class LifecycleConflict extends Schema.TaggedError()("Inbox.LifecycleConflict", { id: SessionMessageSchema.ID, }) {} const isLifecycleConflict = Schema.is(LifecycleConflict); -const fromRow = (row: SessionInputRow): Admitted => +const fromRow = (row: InboxRow): Admitted => Admitted.make({ admittedSeq: row.admittedSeq, id: SessionMessageSchema.ID.make(row.id), sessionId: SessionSchema.ID.make(row.sessionId), - prompt: row.prompt, + threadId: ThreadSchema.ID.make(row.threadId), + prompt: row.data, delivery: row.delivery, timeCreated: row.createdAt, ...(Option.isNone(row.promotedSeq) ? {} : { promotedSeq: row.promotedSeq.value }), @@ -44,19 +46,19 @@ const fromRow = (row: SessionInputRow): Admitted => export const equivalent = ( input: Admitted, expected: { - readonly sessionId: SessionSchema.ID; + readonly threadId: ThreadSchema.ID; readonly prompt: Prompt; readonly delivery: Delivery; }, ) => input.delivery === expected.delivery && - input.sessionId === expected.sessionId && + input.threadId === expected.threadId && Prompt.equivalence(input.prompt, expected.prompt); const matchesProjection = ( input: Admitted, expected: { - readonly sessionId: SessionSchema.ID; + readonly threadId: ThreadSchema.ID; readonly prompt: Prompt; readonly delivery: Delivery; readonly timeCreated: DateTime.Utc; @@ -68,6 +70,7 @@ const matchesProjection = ( export interface AdmitInput { readonly id: SessionMessageSchema.ID; readonly sessionId: SessionSchema.ID; + readonly threadId: ThreadSchema.ID; readonly prompt: Prompt; readonly delivery: Delivery; } @@ -79,13 +82,13 @@ export interface ProjectAdmittedInput extends AdmitInput { export interface ProjectCancelledInput { readonly id: SessionMessageSchema.ID; - readonly sessionId: SessionSchema.ID; + readonly threadId: ThreadSchema.ID; readonly cancelledSeq: number; } export interface ProjectUpdatedInput { readonly id: SessionMessageSchema.ID; - readonly sessionId: SessionSchema.ID; + readonly threadId: ThreadSchema.ID; readonly prompt?: Prompt | undefined; readonly delivery?: Delivery | undefined; } @@ -118,20 +121,20 @@ export interface Interface { * order. The stored input after the change, or none when it was promoted, cancelled, or unknown. */ readonly update: ( - sessionId: SessionSchema.ID, + threadId: ThreadSchema.ID, id: SessionMessageSchema.ID, patch: UpdatePatch, ) => Effect.Effect>; /** - * Withdraws a pending input of the session. False when there is none by that id, or it was + * Withdraws a pending input of the thread. False when there is none by that id, or it was * promoted or cancelled first: a cancel that loses to promotion leaves the prompt in the * conversation. */ - readonly cancel: (sessionId: SessionSchema.ID, id: SessionMessageSchema.ID) => Effect.Effect; + readonly cancel: (threadId: ThreadSchema.ID, id: SessionMessageSchema.ID) => Effect.Effect; /** Inputs not promoted yet, both lanes, in admission order. */ - readonly pending: (sessionId: SessionSchema.ID) => Effect.Effect>; + readonly pending: (threadId: ThreadSchema.ID) => Effect.Effect>; /** Whether the lane holds an input that has not been promoted yet. */ - readonly hasPending: (sessionId: SessionSchema.ID, delivery: Delivery) => Effect.Effect; + readonly hasPending: (threadId: ThreadSchema.ID, delivery: Delivery) => Effect.Effect; /** * Promotes every pending steer admitted at or before `cutoff`, in admission * order. The cutoff keeps a steer that lands mid-promotion out of a request @@ -142,13 +145,13 @@ export interface Interface { * counting a promotion someone else committed would have two workers answer * one prompt. */ - readonly promoteSteers: (sessionId: SessionSchema.ID, cutoff: number) => Effect.Effect; + readonly promoteSteers: (threadId: ThreadSchema.ID, cutoff: number) => Effect.Effect; /** * Promotes the single oldest pending follow-up, if any. One per run, unlike * steers, which is what makes the follow-up lane strictly turn-at-a-time. * False when there was none, or when another caller promoted it first. */ - readonly promoteFollowUp: (sessionId: SessionSchema.ID) => Effect.Effect; + readonly promoteFollowUp: (threadId: ThreadSchema.ID) => Effect.Effect; } export const make = Effect.gen(function* () { @@ -157,17 +160,17 @@ export const make = Effect.gen(function* () { const findRow = SqlSchema.findOneOption({ Request: Schema.String, - Result: SessionInputRow, - execute: (id) => sql`SELECT * FROM session_input WHERE id = ${id}`, + Result: InboxRow, + execute: (id) => sql`SELECT * FROM inbox WHERE id = ${id}`, }); // RETURNING distinguishes "inserted" from "already there" without a // preceding read, so the conflict is detected by the write itself. const insertRow = SqlSchema.findAll({ - Request: SessionInputRow.insert, + Request: InboxRow.insert, Result: Schema.Struct({ id: Schema.String }), execute: (row) => sql` - INSERT INTO session_input ${sql.insert(row)} + INSERT INTO inbox ${sql.insert(row)} ON CONFLICT(id) DO NOTHING RETURNING id `, @@ -176,15 +179,15 @@ export const make = Effect.gen(function* () { const updateRow = SqlSchema.findAll({ Request: Schema.Struct({ id: Schema.String, - sessionId: Schema.String, - prompt: Schema.NullOr(Schema.fromJsonString(Prompt)), + threadId: Schema.String, + data: Schema.NullOr(Schema.fromJsonString(Prompt)), delivery: Schema.NullOr(Delivery), }), Result: Schema.Struct({ id: Schema.String }), // A null field is one the update left alone. - execute: ({ id, sessionId, prompt, delivery }) => sql` - UPDATE session_input SET prompt = COALESCE(${prompt}, prompt), delivery = COALESCE(${delivery}, delivery) - WHERE id = ${id} AND session_id = ${sessionId} AND promoted_seq IS NULL AND cancelled_seq IS NULL + execute: ({ id, threadId, data, delivery }) => sql` + UPDATE inbox SET data = COALESCE(${data}, data), delivery = COALESCE(${delivery}, delivery) + WHERE id = ${id} AND thread_id = ${threadId} AND promoted_seq IS NULL AND cancelled_seq IS NULL RETURNING id `, }); @@ -193,18 +196,18 @@ export const make = Effect.gen(function* () { const reviveRow = SqlSchema.findAll({ Request: Schema.Struct({ id: Schema.String, - sessionId: Schema.String, - prompt: Schema.fromJsonString(Prompt), + threadId: Schema.String, + data: Schema.fromJsonString(Prompt), delivery: Delivery, admittedSeq: Schema.Int, createdAt: DateTimeUtcFromMillis, }), Result: Schema.Struct({ id: Schema.String }), - execute: ({ id, sessionId, prompt, delivery, admittedSeq, createdAt }) => sql` - UPDATE session_input - SET prompt = ${prompt}, delivery = ${delivery}, admitted_seq = ${admittedSeq}, created_at = ${createdAt}, + execute: ({ id, threadId, data, delivery, admittedSeq, createdAt }) => sql` + UPDATE inbox + SET data = ${data}, delivery = ${delivery}, admitted_seq = ${admittedSeq}, created_at = ${createdAt}, cancelled_seq = NULL - WHERE id = ${id} AND session_id = ${sessionId} AND promoted_seq IS NULL AND cancelled_seq IS NOT NULL + WHERE id = ${id} AND thread_id = ${threadId} AND promoted_seq IS NULL AND cancelled_seq IS NOT NULL RETURNING id `, }); @@ -214,62 +217,62 @@ export const make = Effect.gen(function* () { const promoteRow = SqlSchema.findAll({ Request: Schema.Struct({ id: Schema.String, - sessionId: Schema.String, + threadId: Schema.String, promotedSeq: Schema.Int, }), - Result: SessionInputRow, - execute: ({ id, sessionId, promotedSeq }) => sql` - UPDATE session_input SET promoted_seq = ${promotedSeq} - WHERE id = ${id} AND session_id = ${sessionId} AND promoted_seq IS NULL AND cancelled_seq IS NULL + Result: InboxRow, + execute: ({ id, threadId, promotedSeq }) => sql` + UPDATE inbox SET promoted_seq = ${promotedSeq} + WHERE id = ${id} AND thread_id = ${threadId} AND promoted_seq IS NULL AND cancelled_seq IS NULL RETURNING * `, }); // The same compare-and-set as promotion: whichever of the two commits first wins the row. const cancelRow = SqlSchema.findAll({ - Request: Schema.Struct({ id: Schema.String, sessionId: Schema.String, cancelledSeq: Schema.Int }), + Request: Schema.Struct({ id: Schema.String, threadId: Schema.String, cancelledSeq: Schema.Int }), Result: Schema.Struct({ id: Schema.String }), - execute: ({ id, sessionId, cancelledSeq }) => sql` - UPDATE session_input SET cancelled_seq = ${cancelledSeq} - WHERE id = ${id} AND session_id = ${sessionId} AND promoted_seq IS NULL AND cancelled_seq IS NULL + execute: ({ id, threadId, cancelledSeq }) => sql` + UPDATE inbox SET cancelled_seq = ${cancelledSeq} + WHERE id = ${id} AND thread_id = ${threadId} AND promoted_seq IS NULL AND cancelled_seq IS NULL RETURNING id `, }); // An input id becomes the entry id once the Loop materializes it, so an - // existing entry means this input has already graduated out of the queue. + // existing entry means this input has already graduated out of the inbox. const selectEntry = SqlSchema.findAll({ Request: Schema.String, Result: Schema.Struct({ id: Schema.String }), - execute: (id) => sql`SELECT id FROM session_entry WHERE id = ${id}`, + execute: (id) => sql`SELECT id FROM entry WHERE id = ${id}`, }); const selectPendingOne = SqlSchema.findAll({ - Request: Schema.Struct({ sessionId: Schema.String, delivery: Delivery }), + Request: Schema.Struct({ threadId: Schema.String, delivery: Delivery }), Result: Schema.Struct({ id: Schema.String }), - execute: ({ sessionId, delivery }) => sql` - SELECT id FROM session_input - WHERE session_id = ${sessionId} AND promoted_seq IS NULL AND cancelled_seq IS NULL AND delivery = ${delivery} + execute: ({ threadId, delivery }) => sql` + SELECT id FROM inbox + WHERE thread_id = ${threadId} AND promoted_seq IS NULL AND cancelled_seq IS NULL AND delivery = ${delivery} LIMIT 1 `, }); const selectPending = SqlSchema.findAll({ Request: Schema.String, - Result: SessionInputRow, - execute: (sessionId) => sql` - SELECT * FROM session_input - WHERE session_id = ${sessionId} AND promoted_seq IS NULL AND cancelled_seq IS NULL + Result: InboxRow, + execute: (threadId) => sql` + SELECT * FROM inbox + WHERE thread_id = ${threadId} AND promoted_seq IS NULL AND cancelled_seq IS NULL ORDER BY admitted_seq `, }); const selectPendingSteers = SqlSchema.findAll({ - Request: Schema.Struct({ sessionId: Schema.String, cutoff: Schema.Int }), - Result: SessionInputRow, - execute: ({ sessionId, cutoff }) => sql` - SELECT * FROM session_input - WHERE session_id = ${sessionId} + Request: Schema.Struct({ threadId: Schema.String, cutoff: Schema.Int }), + Result: InboxRow, + execute: ({ threadId, cutoff }) => sql` + SELECT * FROM inbox + WHERE thread_id = ${threadId} AND promoted_seq IS NULL AND cancelled_seq IS NULL AND delivery = 'steer' @@ -280,20 +283,20 @@ export const make = Effect.gen(function* () { const selectPendingFollowUp = SqlSchema.findAll({ Request: Schema.String, - Result: SessionInputRow, - execute: (sessionId) => sql` - SELECT * FROM session_input - WHERE session_id = ${sessionId} AND promoted_seq IS NULL AND cancelled_seq IS NULL AND delivery = 'followUp' + Result: InboxRow, + execute: (threadId) => sql` + SELECT * FROM inbox + WHERE thread_id = ${threadId} AND promoted_seq IS NULL AND cancelled_seq IS NULL AND delivery = 'followUp' ORDER BY admitted_seq LIMIT 1 `, }); - const find = Effect.fn("SessionInput.find")(function* (id: SessionMessageSchema.ID) { + const find = Effect.fn("Inbox.find")(function* (id: SessionMessageSchema.ID) { return yield* findRow(id).pipe(Effect.map(Option.map(fromRow))); }, Effect.orDie); - const admit = Effect.fn("SessionInput.admit")(function* (input: AdmitInput) { + const admit = Effect.fn("Inbox.admit")(function* (input: AdmitInput) { // Read before publishing, never inside it: the commit transaction must open // with a write, and a stored row makes re-admission a no-op anyway. const existing = yield* find(input.id); @@ -304,6 +307,7 @@ export const make = Effect.gen(function* () { .publish(EventList.PromptAdmitted, { messageId: input.id, sessionId: input.sessionId, + threadId: input.threadId, timestamp, prompt: input.prompt, delivery: input.delivery, @@ -317,6 +321,7 @@ export const make = Effect.gen(function* () { admittedSeq: event.durable.seq, id: input.id, sessionId: input.sessionId, + threadId: input.threadId, prompt: input.prompt, delivery: input.delivery, timeCreated: timestamp, @@ -338,7 +343,7 @@ export const make = Effect.gen(function* () { ); }); - const projectAdmitted = Effect.fn("SessionInput.projectAdmitted")(function* (input: ProjectAdmittedInput) { + const projectAdmitted = Effect.fn("Inbox.projectAdmitted")(function* (input: ProjectAdmittedInput) { // Safe to read first here: the commit transaction already opened with the // event's sequence bump, so this cannot upgrade a read lock. const entry = yield* selectEntry(input.id); @@ -346,7 +351,9 @@ export const make = Effect.gen(function* () { const stored = yield* insertRow({ id: input.id, sessionId: input.sessionId, - prompt: input.prompt, + threadId: input.threadId, + kind: "prompt", + data: input.prompt, delivery: input.delivery, admittedSeq: input.admittedSeq, promotedSeq: Option.none(), @@ -356,8 +363,8 @@ export const make = Effect.gen(function* () { if (stored.length > 0) return; const revived = yield* reviveRow({ id: input.id, - sessionId: input.sessionId, - prompt: input.prompt, + threadId: input.threadId, + data: input.prompt, delivery: input.delivery, admittedSeq: input.admittedSeq, createdAt: input.timeCreated, @@ -365,10 +372,10 @@ export const make = Effect.gen(function* () { if (revived.length === 0) return yield* Effect.die(new LifecycleConflict({ id: input.id })); }, Effect.orDie); - const projectPrompted = Effect.fn("SessionInput.projectPrompted")(function* (input: ProjectPromptedInput) { + const projectPrompted = Effect.fn("Inbox.projectPrompted")(function* (input: ProjectPromptedInput) { const updated = yield* promoteRow({ id: input.id, - sessionId: input.sessionId, + threadId: input.threadId, promotedSeq: input.promotedSeq, }); const promoted = updated[0]; @@ -396,7 +403,9 @@ export const make = Effect.gen(function* () { yield* insertRow({ id: input.id, sessionId: input.sessionId, - prompt: input.prompt, + threadId: input.threadId, + kind: "prompt", + data: input.prompt, delivery: input.delivery, admittedSeq: input.promotedSeq, promotedSeq: Option.some(input.promotedSeq), @@ -405,7 +414,7 @@ export const make = Effect.gen(function* () { }); }, Effect.orDie); - const projectCancelled = Effect.fn("SessionInput.projectCancelled")(function* (input: ProjectCancelledInput) { + const projectCancelled = Effect.fn("Inbox.projectCancelled")(function* (input: ProjectCancelledInput) { const updated = yield* cancelRow(input); if (updated.length > 0) return; // Nothing moved: this exact cancel being replayed, or the row was promoted or cancelled first. @@ -414,11 +423,11 @@ export const make = Effect.gen(function* () { return yield* Effect.die(new LifecycleConflict({ id: input.id })); }, Effect.orDie); - const projectUpdated = Effect.fn("SessionInput.projectUpdated")(function* (input: ProjectUpdatedInput) { + const projectUpdated = Effect.fn("Inbox.projectUpdated")(function* (input: ProjectUpdatedInput) { const updated = yield* updateRow({ id: input.id, - sessionId: input.sessionId, - prompt: input.prompt ?? null, + threadId: input.threadId, + data: input.prompt ?? null, delivery: input.delivery ?? null, }); if (updated.length > 0) return; @@ -426,20 +435,24 @@ export const make = Effect.gen(function* () { return yield* Effect.die(new LifecycleConflict({ id: input.id })); }, Effect.orDie); - const update = Effect.fn("SessionInput.update")(function* ( - sessionId: SessionSchema.ID, + const pendingOf = (threadId: ThreadSchema.ID, id: SessionMessageSchema.ID) => + find(id).pipe( + Effect.map( + Option.filter( + (stored) => + stored.threadId === threadId && stored.promotedSeq === undefined && stored.cancelledSeq === undefined, + ), + ), + ); + + const update = Effect.fn("Inbox.update")(function* ( + threadId: ThreadSchema.ID, id: SessionMessageSchema.ID, patch: UpdatePatch, ) { // Read before publishing, as `admit` does: the commit must open with a write. - const stored = yield* find(id); - if ( - Option.isNone(stored) || - stored.value.sessionId !== sessionId || - stored.value.promotedSeq !== undefined || - stored.value.cancelledSeq !== undefined - ) - return Option.none(); + const stored = yield* pendingOf(threadId, id); + if (Option.isNone(stored)) return Option.none(); const prompt = patch.prompt === undefined || Prompt.equivalence(patch.prompt, stored.value.prompt) ? undefined : patch.prompt; const delivery = @@ -448,7 +461,8 @@ export const make = Effect.gen(function* () { const timestamp = yield* DateTime.now; return yield* events .publish(EventList.PromptUpdated, { - sessionId, + sessionId: stored.value.sessionId, + threadId, messageId: id, timestamp, ...(prompt === undefined ? {} : { prompt }), @@ -464,45 +478,35 @@ export const make = Effect.gen(function* () { ); }); - const cancel = Effect.fn("SessionInput.cancel")(function* ( - sessionId: SessionSchema.ID, - id: SessionMessageSchema.ID, - ) { + const cancel = Effect.fn("Inbox.cancel")(function* (threadId: ThreadSchema.ID, id: SessionMessageSchema.ID) { // Read before publishing, as `admit` does: the commit must open with a write. - const stored = yield* find(id); - if ( - Option.isNone(stored) || - stored.value.sessionId !== sessionId || - stored.value.promotedSeq !== undefined || - stored.value.cancelledSeq !== undefined - ) - return false; + const stored = yield* pendingOf(threadId, id); + if (Option.isNone(stored)) return false; const timestamp = yield* DateTime.now; - return yield* events.publish(EventList.PromptCancelled, { sessionId, messageId: id, timestamp }).pipe( - Effect.as(true), - // Promoted (or cancelled) between the read and the commit: the projector conflicted and - // the event rolled back. - Effect.catchDefect((defect) => (isLifecycleConflict(defect) ? Effect.succeed(false) : Effect.die(defect))), - ); + return yield* events + .publish(EventList.PromptCancelled, { sessionId: stored.value.sessionId, threadId, messageId: id, timestamp }) + .pipe( + Effect.as(true), + // Promoted (or cancelled) between the read and the commit: the projector conflicted and + // the event rolled back. + Effect.catchDefect((defect) => (isLifecycleConflict(defect) ? Effect.succeed(false) : Effect.die(defect))), + ); }); // Publishing `Prompted` is what promotes: the projector stamps promoted_seq // with that event's own sequence, so delivery order lives in the same space // as the rest of the session's history. - // TODO(sanchitrk): revisit - const publishPrompted = Effect.fn("SessionInput.publishPrompted")(function* ( - sessionId: SessionSchema.ID, - rows: ReadonlyArray, - ) { + const publishPrompted = Effect.fn("Inbox.publishPrompted")(function* (rows: ReadonlyArray) { let promoted = 0; for (const row of rows) { const id = SessionMessageSchema.ID.make(row.id); const won = yield* events .publish(EventList.Prompted, { - sessionId, + sessionId: SessionSchema.ID.make(row.sessionId), + threadId: ThreadSchema.ID.make(row.threadId), timestamp: row.createdAt, messageId: id, - prompt: row.prompt, + prompt: row.data, delivery: row.delivery, }) .pipe( @@ -521,7 +525,7 @@ export const make = Effect.gen(function* () { stored.value.cancelledSeq !== undefined || stored.value.admittedSeq !== row.admittedSeq || stored.value.delivery !== row.delivery || - !Prompt.equivalence(stored.value.prompt, row.prompt)) + !Prompt.equivalence(stored.value.prompt, row.data)) ? Effect.succeed(false) : Effect.die(defect), ), @@ -534,27 +538,24 @@ export const make = Effect.gen(function* () { return promoted; }); - const pending = Effect.fn("SessionInput.pending")(function* (sessionId: SessionSchema.ID) { - return (yield* selectPending(sessionId)).map(fromRow); + const pending = Effect.fn("Inbox.pending")(function* (threadId: ThreadSchema.ID) { + return (yield* selectPending(threadId)).map(fromRow); }, Effect.orDie); - const hasPending = Effect.fn("SessionInput.hasPending")(function* (sessionId: SessionSchema.ID, delivery: Delivery) { - const rows = yield* selectPendingOne({ sessionId, delivery }); + const hasPending = Effect.fn("Inbox.hasPending")(function* (threadId: ThreadSchema.ID, delivery: Delivery) { + const rows = yield* selectPendingOne({ threadId, delivery }); return rows.length > 0; }, Effect.orDie); - const promoteSteers = Effect.fn("SessionInput.promoteSteers")(function* ( - sessionId: SessionSchema.ID, - cutoff: number, - ) { - const rows = yield* selectPendingSteers({ sessionId, cutoff }).pipe(Effect.orDie); - return yield* publishPrompted(sessionId, rows); + const promoteSteers = Effect.fn("Inbox.promoteSteers")(function* (threadId: ThreadSchema.ID, cutoff: number) { + const rows = yield* selectPendingSteers({ threadId, cutoff }).pipe(Effect.orDie); + return yield* publishPrompted(rows); }); - const promoteFollowUp = Effect.fn("SessionInput.promoteFollowUp")(function* (sessionId: SessionSchema.ID) { - const rows = yield* selectPendingFollowUp(sessionId).pipe(Effect.orDie); + const promoteFollowUp = Effect.fn("Inbox.promoteFollowUp")(function* (threadId: ThreadSchema.ID) { + const rows = yield* selectPendingFollowUp(threadId).pipe(Effect.orDie); if (rows.length === 0) return false; - return (yield* publishPrompted(sessionId, rows)) > 0; + return (yield* publishPrompted(rows)) > 0; }); return { @@ -573,4 +574,4 @@ export const make = Effect.gen(function* () { } satisfies Interface; }); -export * as SessionInput from "./input.ts"; +export * as Inbox from "./inbox.ts"; diff --git a/packages/harness/src/session/input/schema.ts b/packages/harness/src/inbox/schema.ts similarity index 58% rename from packages/harness/src/session/input/schema.ts rename to packages/harness/src/inbox/schema.ts index 3111b81c..ccee2b62 100644 --- a/packages/harness/src/session/input/schema.ts +++ b/packages/harness/src/inbox/schema.ts @@ -1,17 +1,19 @@ import { Schema } from "effect"; -import { DateTimeUtcFromMillis, NonNegativeInt, optional } from "../../schema.ts"; -import { Delivery, Prompt } from "../prompt/schema.ts"; -import { SessionSchema } from "../schema.ts"; -import { SessionMessageSchema } from "../message/schema.ts"; +import { DateTimeUtcFromMillis, NonNegativeInt, optional } from "../schema.ts"; +import { Delivery, Prompt } from "../session/prompt/schema.ts"; +import { SessionSchema } from "../session/schema.ts"; +import { SessionMessageSchema } from "../session/message/schema.ts"; +import { ThreadSchema } from "../thread/schema.ts"; export const Admitted = Schema.Struct({ admittedSeq: NonNegativeInt, id: SessionMessageSchema.ID, sessionId: SessionSchema.ID, + threadId: ThreadSchema.ID, prompt: Prompt, delivery: Delivery, timeCreated: DateTimeUtcFromMillis, promotedSeq: NonNegativeInt.pipe(optional), cancelledSeq: NonNegativeInt.pipe(optional), -}).annotate({ identifier: "SessionInput.Admitted" }); +}).annotate({ identifier: "Inbox.Admitted" }); export interface Admitted extends Schema.Schema.Type {} diff --git a/packages/harness/src/runner/event.ts b/packages/harness/src/runner/event.ts index 04c2d1a7..7c8568fa 100644 --- a/packages/harness/src/runner/event.ts +++ b/packages/harness/src/runner/event.ts @@ -8,7 +8,7 @@ * * The LLM boundary sequences the stream and hands each event over; this * module decides what the event means. It writes nothing itself -- durable - * lifecycle events reach `session_entry` through the projectors in + * lifecycle events reach `entry` through the projectors in * `session/projector.ts`. * * There are no fragment buffers here, which is the main structural difference @@ -26,6 +26,7 @@ import { Event } from "../event/event.ts"; import { EventList } from "../event/list.ts"; import { SessionMessageSchema } from "../session/message/schema.ts"; import type { SessionSchema } from "../session/schema.ts"; +import type { ThreadSchema } from "../thread/schema.ts"; import { Runner } from "./run.ts"; /** How the response settled. The loop branches on this, never on `result()`. */ @@ -50,7 +51,10 @@ export interface Publisher { readonly terminal: Effect.Effect; } -export const make = Effect.fn("LLMEventPublisher.make")(function* (input: { readonly sessionId: SessionSchema.ID }) { +export const make = Effect.fn("LLMEventPublisher.make")(function* (input: { + readonly sessionId: SessionSchema.ID; + readonly threadId: ThreadSchema.ID; +}) { const events = yield* Event.Service; let messageId: SessionMessageSchema.ID | undefined; @@ -58,7 +62,7 @@ export const make = Effect.fn("LLMEventPublisher.make")(function* (input: { read let settled: Terminal | undefined; const clone = (message: Message.AssistantMessage): Message.AssistantMessage => structuredClone(message); - const fault = (reason: string) => new Runner.LLMStreamError({ sessionId: input.sessionId, reason }); + const fault = (reason: string) => new Runner.LLMStreamError({ threadId: input.threadId, reason }); /** * Every event names the message it belongs to. The first one to arrive fixes @@ -90,7 +94,7 @@ export const make = Effect.fn("LLMEventPublisher.make")(function* (input: { read return yield* fault(`received "${event.type}" after the response had already terminated`); } const timestamp = yield* DateTime.now; - const base = { sessionId: input.sessionId, timestamp }; + const base = { sessionId: input.sessionId, threadId: input.threadId, timestamp }; switch (event.type) { case "start": { diff --git a/packages/harness/src/runner/execute.ts b/packages/harness/src/runner/execute.ts index b3e7856b..f8e72d41 100644 --- a/packages/harness/src/runner/execute.ts +++ b/packages/harness/src/runner/execute.ts @@ -16,14 +16,18 @@ import { Runner } from "./run.ts"; // session import { SessionFailure } from "../session/failure.ts"; -import type { ID as SessionId } from "../session/schema.ts"; import { SessionSchema } from "../session/schema.ts"; import { Session } from "../session/session.ts"; +import { ThreadSchema } from "../thread/schema.ts"; +import { Thread } from "../thread/thread.ts"; + +type ThreadId = ThreadSchema.ID; export const layer = Layer.effect( RunnerExecution.Service, Effect.gen(function* () { - const store = yield* Session.Service; + const sessions = yield* Session.Service; + const threads = yield* Thread.Service; const sandbox = yield* SandboxController.Controller; const sql = yield* SqlClient.SqlClient; // Captured here, not requested inside `drain`: `RunCoordinator.make` @@ -33,78 +37,109 @@ export const layer = Layer.effect( const events = yield* Event.Service; // A lifecycle publish that fails is reported, never thrown back into the // coordinator: losing the observation must not change how the drain ends. - const reportLifecycle = (sessionId: SessionId, publish: Effect.Effect) => + const reportLifecycle = (threadId: ThreadId, publish: Effect.Effect) => publish.pipe( Effect.tapCause((cause) => Cause.hasInterruptsOnly(cause) ? Effect.void : Effect.logError("Failed to publish session execution lifecycle", cause).pipe( - Effect.annotateLogs({ sessionId }), + Effect.annotateLogs({ threadId }), ), ), Effect.ignore, ); - // Sessions being recovered: their run neither marks the session nor runs once it is unmarked. - const recovering = new Set(); + // The session a thread belongs to, for the lifecycle events its log carries. Threads never + // move between sessions, so one lookup per thread is enough. + const owners = new Map(); + const sessionOf = (threadId: ThreadId) => + Effect.gen(function* () { + const known = owners.get(threadId); + if (known !== undefined) return Option.some(known); + const thread = yield* threads.get(threadId); + if (Option.isNone(thread)) return Option.none(); + const sessionId = SessionSchema.ID.make(thread.value.sessionId); + owners.set(threadId, sessionId); + return Option.some(sessionId); + }); + const publishLifecycle = ( + threadId: ThreadId, + publish: (base: { + readonly sessionId: SessionSchema.ID; + readonly threadId: ThreadId; + readonly timestamp: DateTime.Utc; + }) => Effect.Effect, + ) => + Effect.gen(function* () { + const sessionId = yield* sessionOf(threadId); + if (Option.isNone(sessionId)) return; + yield* publish({ sessionId: sessionId.value, threadId, timestamp: yield* DateTime.now }); + }); + + // Threads being recovered: their run neither marks the thread nor runs once it is unmarked. + const recovering = new Set(); // The log position at each user stop: prompts admitted after it are new work, not parked by it. - const stoppedAt = new Map(); + const stoppedAt = new Map(); - const coordinator = yield* RunCoordinator.make({ + const coordinator = yield* RunCoordinator.make({ // The run is recorded unfinished before it starts, so a process dying anywhere in it - // leaves the session for the next server start to resume. A recovered one already is. - started: (sessionId) => + // leaves the thread for the next server start to resume. A recovered one already is. + started: (threadId) => reportLifecycle( - sessionId, - (recovering.has(sessionId) ? Effect.void : store.markRunning(sessionId)).pipe( - Effect.andThen(DateTime.now), - Effect.andThen((timestamp) => events.publish(EventList.ExecutionStarted, { sessionId, timestamp })), + threadId, + (recovering.has(threadId) ? Effect.void : threads.markRunning(threadId)).pipe( + Effect.andThen( + publishLifecycle(threadId, (base) => events.publish(EventList.ExecutionStarted, base)), + ), ), ), // One terminal per busy period, covering every drain it coalesced. - settled: (sessionId, exit, reason) => + settled: (threadId, exit, reason) => reportLifecycle( - sessionId, + threadId, Effect.gen(function* () { // A shutdown leaves the run to resume on the next start; a user stop or any other end // finishes it. const interrupted = Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause); - const stop = stoppedAt.get(sessionId); - stoppedAt.delete(sessionId); - yield* store.endExecution( - sessionId, + const stop = stoppedAt.get(threadId); + stoppedAt.delete(threadId); + yield* threads.endExecution( + threadId, !interrupted ? "finished" : reason === "user" ? "stopped" : "closed", stop, ); - const timestamp = yield* DateTime.now; - if (Exit.isSuccess(exit)) - return yield* events.publish(EventList.ExecutionSucceeded, { sessionId, timestamp }); + const sessionId = yield* sessionOf(threadId); + if (Option.isNone(sessionId)) return; + const base = { sessionId: sessionId.value, threadId, timestamp: yield* DateTime.now }; + if (Exit.isSuccess(exit)) return yield* events.publish(EventList.ExecutionSucceeded, base); if (Cause.hasInterruptsOnly(exit.cause)) // Nobody named a reason, so the process took the decision itself. return yield* events.publish(EventList.ExecutionInterrupted, { - sessionId, - timestamp, + ...base, reason: reason ?? "shutdown", }); return yield* events.publish(EventList.ExecutionFailed, { - sessionId, - timestamp, + ...base, error: SessionFailure.fromCause(Cause.squash(exit.cause)), }); }), ), - drain: Effect.fnUntraced(function* (sessionId: SessionId, force) { - const session = yield* store.get(sessionId); - if (Option.isNone(session)) return yield* new Session.SessionNotFoundError({ sessionId }); - const recovered = recovering.delete(sessionId); - if (recovered && Option.isNone(session.value.execution)) return; - const space = yield* store.space(sessionId); - // Unreachable while the FK holds; typed so a UI can offer relink - // as the repair instead of surfacing a defect. - if (Option.isNone(space)) return yield* new Session.SessionLinkedSpaceNotFoundError({ sessionId }); - // The session's env is derived through its space; its cwd is its own (D-SESSION). - const instanceId = space.value.env; - const mount = sandbox.mount(instanceId, { cwd: session.value.directory }); + drain: Effect.fnUntraced(function* (threadId: ThreadId, force) { + const thread = yield* threads.get(threadId); + // Deleted with its session after something woke it: nothing is left to run. + if (Option.isNone(thread)) return; + const sessionId = thread.value.sessionId; + const recovered = recovering.delete(threadId); + if (recovered && Option.isNone(thread.value.execution)) return; + // The thread's placement, else the session's space and directory. A placement names its + // space in a document no FK guards, so a space gone since is typed: `relink --thread` + // is the repair. + const placement = yield* sessions.placement(threadId); + if (Option.isNone(placement)) { + return yield* new Session.SessionLinkedSpaceNotFoundError({ sessionId, threadId }); + } + const instanceId = placement.value.space.env; + const mount = sandbox.mount(instanceId, { cwd: placement.value.directory }); const scopedRun = Effect.gen(function* () { const mountContext = yield* Layer.build(mount); @@ -112,7 +147,7 @@ export const layer = Layer.effect( const fs = Context.get(mountContext, SandboxIO.FileSystem); if (!(yield* fs.exists(current.cwd))) { return yield* new Runner.SandboxDirectoryNotFoundError({ - sessionId, + threadId, sandboxInstanceId: instanceId, directory: current.cwd, }); @@ -127,7 +162,7 @@ export const layer = Layer.effect( ["Location.DirectoryNotFoundError", "Location.NotDirectoryError"], (error) => new Runner.SandboxDirectoryNotFoundError({ - sessionId, + threadId, sandboxInstanceId: error.sandboxInstanceId, directory: error.directory, }), @@ -135,7 +170,7 @@ export const layer = Layer.effect( ); return yield* runner - .run({ sessionId, force, recovered }) + .run({ threadId, force, recovered }) .pipe(Effect.provideContext(Context.merge(locationContext, mountContext))); }); @@ -143,32 +178,33 @@ export const layer = Layer.effect( Effect.scoped, Effect.tapCause((cause) => Cause.hasDies(cause) - ? Effect.logError("defect", cause).pipe(Effect.annotateLogs({ sessionId })) + ? Effect.logError("defect", cause).pipe(Effect.annotateLogs({ threadId })) : Effect.void, ), ); }), }); - const interrupt: RunnerExecution.Interface["interrupt"] = (sessionId, reason, options) => + const interrupt: RunnerExecution.Interface["interrupt"] = (threadId, reason, options) => Effect.gen(function* () { // The first stop of a run sets its position; a repeat while it settles keeps that one. - const recorded = reason === "user" && !stoppedAt.has(sessionId); - if (recorded) stoppedAt.set(sessionId, yield* events.latestSequence(sessionId)); - const accepted = yield* coordinator.interrupt(sessionId, reason, options); + const sessionId = reason === "user" && !stoppedAt.has(threadId) ? yield* sessionOf(threadId) : undefined; + const recorded = sessionId !== undefined && Option.isSome(sessionId); + if (recorded) stoppedAt.set(threadId, yield* events.latestSequence(sessionId.value)); + const accepted = yield* coordinator.interrupt(threadId, reason, options); // Nothing was running, so no settlement will read the position this call set. - if (recorded && !accepted) stoppedAt.delete(sessionId); + if (recorded && !accepted) stoppedAt.delete(threadId); return accepted; }); - const recover = (sessionId: SessionId) => + const recover = (threadId: ThreadId) => Effect.suspend(() => { - recovering.add(sessionId); - return coordinator.run(sessionId); + recovering.add(threadId); + return coordinator.run(threadId); }).pipe( Effect.ensuring( Effect.sync(() => { - recovering.delete(sessionId); + recovering.delete(threadId); }), ), ); diff --git a/packages/harness/src/runner/execution.ts b/packages/harness/src/runner/execution.ts index 27664611..21114270 100644 --- a/packages/harness/src/runner/execution.ts +++ b/packages/harness/src/runner/execution.ts @@ -4,38 +4,39 @@ */ import { Context, Effect, Layer } from "effect"; -import { SessionSchema } from "../session/schema.ts"; +import type { SessionSchema } from "../session/schema.ts"; +import type { ThreadSchema } from "../thread/schema.ts"; import { Runner } from "./run.ts"; export interface Interface { /** Snapshots active execution owned by this process. */ - readonly active: Effect.Effect>; + readonly active: Effect.Effect>; /** Starts execution while idle or joins the active execution. */ - readonly resume: (sessionId: SessionSchema.ID) => Effect.Effect; + readonly resume: (threadId: ThreadSchema.ID) => Effect.Effect; /** * Resumes a run the last process left unfinished. Like `resume`, except that it marks nothing, - * and runs nothing when the session is no longer marked as it begins: a stop that landed since - * recovery chose the session stands. + * and runs nothing when the thread is no longer marked as it begins: a stop that landed since + * recovery chose the thread stands. */ - readonly recover: (sessionId: SessionSchema.ID) => Effect.Effect; + readonly recover: (threadId: ThreadSchema.ID) => Effect.Effect; /** Registers newly recorded work. Repeated wakeups may coalesce. */ - readonly wake: (sessionId: SessionSchema.ID) => Effect.Effect; + readonly wake: (threadId: ThreadSchema.ID) => Effect.Effect; /** * Accepts interruption of active work. Idle interruption returns false. * `awaitSettlement` waits for the interrupted execution's cleanup only. */ readonly interrupt: ( - sessionId: SessionSchema.ID, + threadId: ThreadSchema.ID, reason?: SessionSchema.InterruptReason, options?: { readonly awaitSettlement?: boolean }, ) => Effect.Effect; - /** Waits until this process owns no execution for the session. */ - readonly awaitIdle: (sessionId: SessionSchema.ID) => Effect.Effect; + /** Waits until this process owns no execution for the thread. */ + readonly awaitIdle: (threadId: ThreadSchema.ID) => Effect.Effect; } export class Service extends Context.Service()("@codeworksh/harness/runner/execution/Service") {} -// Low-level compatibility layer for callers that only need durable session. +// Low-level compatibility layer for callers that only need durable threads. export const noopLayer = Layer.succeed( Service, Service.of({ diff --git a/packages/harness/src/runner/hooks.ts b/packages/harness/src/runner/hooks.ts index 72644b3c..4a6168bb 100644 --- a/packages/harness/src/runner/hooks.ts +++ b/packages/harness/src/runner/hooks.ts @@ -8,7 +8,7 @@ */ import { Context, Effect, Layer } from "effect"; -import type { Admitted } from "../session/input/schema.ts"; +import type { Admitted } from "../inbox/schema.ts"; import type { State } from "../state/state.ts"; import type { LLM } from "./llm.ts"; @@ -27,7 +27,7 @@ export interface Completed { } /** One provider request, as the loop built it. */ -export type Request = Omit; +export type Request = Omit; export interface Attempt { /** 0 for the first attempt of a turn. */ diff --git a/packages/harness/src/runner/llm.ts b/packages/harness/src/runner/llm.ts index 8edc8b31..682bea4d 100644 --- a/packages/harness/src/runner/llm.ts +++ b/packages/harness/src/runner/llm.ts @@ -13,7 +13,7 @@ import { getGitHubCopilotApiKey, JsonGitHubCopilotAuthStorage } from "@codeworks import { getOpenAICodexApiKey, JsonOpenAICodexAuthStorage } from "@codeworksh/aikit/oauth/openai/codex"; import { Cause, Data, Duration, Effect, Exit, Fiber, Schema, Scope, Stream } from "effect"; import { ModelCatalog } from "../model/catalog.ts"; -import type { SessionSchema } from "../session/schema.ts"; +import type { ThreadSchema } from "../thread/schema.ts"; import { merge } from "../settings/merge.ts"; import { resolveOverrides, resolveRequest } from "../settings/resolve.ts"; import { type Block, type DeclaredModel, keyName } from "../settings/schema.ts"; @@ -24,7 +24,8 @@ import { LLMEventPublisher } from "./event.ts"; import { Runner } from "./run.ts"; export interface Input { - readonly sessionId: SessionSchema.ID; + /** Also the provider session and prompt-cache key: fresh per thread, never inherited. */ + readonly threadId: ThreadSchema.ID; readonly context: Message.Context; readonly provider: string; readonly model: string; @@ -67,7 +68,7 @@ export const runtimeOptions = (input: Input, model: Model.Info, signal: AbortSig ...options, ...(activeTools === undefined ? {} : { activeTools: [...activeTools] }), ...(input.thinkingLevel === undefined || input.thinkingLevel === "off" ? {} : { reasoning: input.thinkingLevel }), - sessionId: input.sessionId, + sessionId: input.threadId, signal, }; }; @@ -305,7 +306,7 @@ export const make = (openStream: Open): Request => { "gen_ai.operation.name": "chat", "gen_ai.provider.name": input.provider, "gen_ai.request.model": input.model, - "gen_ai.conversation.id": input.sessionId, + "gen_ai.conversation.id": input.threadId, "gen_ai.request.stream": true, }); const start = yield* Effect.clockWith((clock) => clock.currentTimeMillis); diff --git a/packages/harness/src/runner/loop.ts b/packages/harness/src/runner/loop.ts index f90132b0..b495a4ca 100644 --- a/packages/harness/src/runner/loop.ts +++ b/packages/harness/src/runner/loop.ts @@ -1,5 +1,5 @@ /* - * @file Durable session drain. + * @file Durable thread drain. * * The loop reads queues and projections, but every durable mutation is an * event. LLMStarted owns the speculative draft; TurnEnded is the only commit. @@ -11,10 +11,10 @@ import { ContextCodec } from "../context/codec.ts"; import { Context } from "../context/context.ts"; import { Event } from "../event/event.ts"; import { EventList } from "../event/list.ts"; -import { SessionInput } from "../session/input/input.ts"; +import { Inbox } from "../inbox/inbox.ts"; import { SessionMessageSchema } from "../session/message/schema.ts"; -import type { SessionSchema } from "../session/schema.ts"; -import { Session } from "../session/session.ts"; +import type { ThreadSchema } from "../thread/schema.ts"; +import { Thread } from "../thread/thread.ts"; import { State } from "../state/state.ts"; import { Telemetry } from "../telemetry/telemetry.ts"; import { errorMessage } from "../util/error.ts"; @@ -89,13 +89,13 @@ export const layer = (options: Options = {}) => const context = yield* Context.Service; const events = yield* Event.Service; const hooks = yield* Hooks.Service; - const inputs = yield* SessionInput.make; - const sessions = yield* Session.Service; + const inputs = yield* Inbox.make; + const threads = yield* Thread.Service; const state = yield* State.Service; const requestLLM = options.request ?? LLM.run; const failureStub = Effect.fn("Loop.failureStub")(function* ( - draft: Session.HydratedEntry, + draft: Thread.HydratedEntry, reason: "aborted" | "error", message: string, ) { @@ -112,7 +112,7 @@ export const layer = (options: Options = {}) => }); const publishFailure = Effect.fn("Loop.publishFailure")(function* ( - draft: Session.HydratedEntry, + draft: Thread.HydratedEntry, cause: EventList.TurnAbortCause, ) { const reason = cause._tag === "interrupted" ? "aborted" : "error"; @@ -120,17 +120,17 @@ export const layer = (options: Options = {}) => yield* events.publish(EventList.LLMFailed, { timestamp: yield* DateTime.now, sessionId: draft.entry.sessionId, + threadId: draft.entry.threadId, messageId: SessionMessageSchema.ID.from(draft.entry.id), reason, message: yield* failureStub(draft, reason, message), }); }); - const healDanglingDraft = Effect.fn("Loop.healDanglingDraft")(function* (sessionId: SessionSchema.ID) { - const path = yield* sessions.path(sessionId); - const draft = path.find((entry) => entry.entry.type === "assistant" && entry.entry.state === "draft"); - if (draft === undefined) return; - yield* publishFailure(draft, { _tag: "interrupted" }); + const healDanglingDraft = Effect.fn("Loop.healDanglingDraft")(function* (threadId: ThreadSchema.ID) { + const draft = yield* threads.draft(threadId); + if (Option.isNone(draft)) return; + yield* publishFailure(draft.value, { _tag: "interrupted" }); }); const settleTools = Effect.fn("Loop.settleTools", { level: "Info" })(function* ( @@ -152,6 +152,7 @@ export const layer = (options: Options = {}) => yield* events.publish(EventList.ToolSettled, { timestamp: yield* DateTime.now, sessionId: snapshot.sessionId, + threadId: snapshot.threadId, messageId, callID: call.callID, part, @@ -161,6 +162,7 @@ export const layer = (options: Options = {}) => yield* events.publish(EventList.TurnEnded, { timestamp: yield* DateTime.now, sessionId: snapshot.sessionId, + threadId: snapshot.threadId, messageId, }); markCommitted(); @@ -198,6 +200,7 @@ export const layer = (options: Options = {}) => yield* events.publish(EventList.ToolStarted, { timestamp: yield* DateTime.now, sessionId: snapshot.sessionId, + threadId: snapshot.threadId, messageId, callID: call.callID, name: call.name, @@ -207,6 +210,7 @@ export const layer = (options: Options = {}) => const execute = snapshot.tools .handle(call, { sessionId: snapshot.sessionId, + threadId: snapshot.threadId, messageId, modelInput: snapshot.resolvedModel.input, onProgress: (progress) => @@ -214,6 +218,7 @@ export const layer = (options: Options = {}) => .publish(EventList.ToolProgress, { timestamp: DateTime.makeUnsafe(progress.toolCall.time.end), sessionId: snapshot.sessionId, + threadId: snapshot.threadId, messageId, callID: call.callID, partial: progress.partial, @@ -270,17 +275,17 @@ export const layer = (options: Options = {}) => snapshot: State.Snapshot, continuing: boolean, ) { - const sessionId = snapshot.sessionId; + const { sessionId, threadId } = snapshot; if (promotion !== undefined) { const cutoff = yield* events.latestSequence(sessionId); - const followUp = promotion === "followUp" && (yield* inputs.promoteFollowUp(sessionId)); - const steers = yield* inputs.promoteSteers(sessionId, cutoff); + const followUp = promotion === "followUp" && (yield* inputs.promoteFollowUp(threadId)); + const steers = yield* inputs.promoteSteers(threadId, cutoff); // The pending input this turn was started for was cancelled before promotion: with no // earlier work to continue there is nothing to answer, so no provider request. if (!continuing && !followUp && steers === 0) return undefined; } - const assembled = yield* context.assemble(sessionId); + const assembled = yield* context.assemble(threadId); const leaf = assembled.lastAssistant?.entryId === assembled.leafEntryId ? assembled.lastAssistant.message : undefined; if (leaf !== undefined && (leaf.stopReason === "stop" || leaf.stopReason === "length")) { @@ -297,14 +302,14 @@ export const layer = (options: Options = {}) => const settleDraft = (cause: EventList.TurnAbortCause) => Effect.gen(function* () { if (publisher?.startedMessageId === undefined) return; - const stored = yield* sessions.entry(publisher.startedMessageId); + const stored = yield* threads.entry(publisher.startedMessageId); if (Option.isSome(stored) && stored.value.entry.state === "draft") { yield* publishFailure(stored.value, cause); } }); const attempt = Effect.gen(function* () { - publisher = yield* LLMEventPublisher.make({ sessionId }).pipe( + publisher = yield* LLMEventPublisher.make({ sessionId, threadId }).pipe( Effect.provideService(Event.Service, events), ); const prepared = yield* hooks.prepareRequest( @@ -323,7 +328,7 @@ export const layer = (options: Options = {}) => }, { retry: retries, ...(failed === undefined ? {} : { failed }) }, ); - const terminal = yield* requestLLM({ sessionId, ...prepared, publisher }); + const terminal = yield* requestLLM({ threadId, ...prepared, publisher }); if (terminal.outcome === "failed") { if (terminal.reason === "aborted") return yield* Effect.interrupt; @@ -357,6 +362,7 @@ export const layer = (options: Options = {}) => yield* events.publish(EventList.RetryScheduled, { timestamp: yield* DateTime.now, sessionId, + threadId, attempt: retries, maxRetries: snapshot.retry.maxRetries, delayMs, @@ -369,7 +375,7 @@ export const layer = (options: Options = {}) => ); const turnWindow = Effect.gen(function* () { - yield* events.publish(EventList.TurnStarted, { timestamp: yield* DateTime.now, sessionId }); + yield* events.publish(EventList.TurnStarted, { timestamp: yield* DateTime.now, sessionId, threadId }); const terminal = yield* request().pipe( Effect.onExit((exit) => retries === 0 @@ -378,6 +384,7 @@ export const layer = (options: Options = {}) => yield* events.publish(EventList.RetryFinished, { timestamp: yield* DateTime.now, sessionId, + threadId, attempt: retries, success: Exit.isSuccess(exit), ...(Exit.isSuccess(exit) @@ -412,6 +419,7 @@ export const layer = (options: Options = {}) => yield* events.publish(EventList.TurnAborted, { timestamp: yield* DateTime.now, sessionId, + threadId, cause: turnCause, }); }); @@ -429,19 +437,19 @@ export const layer = (options: Options = {}) => }); const run = Effect.fn("Loop.run", { level: "Info" })(function* (input: { - readonly sessionId: SessionSchema.ID; + readonly threadId: ThreadSchema.ID; readonly force: boolean; readonly recovered?: boolean; }) { - yield* healDanglingDraft(input.sessionId); + yield* healDanglingDraft(input.threadId); - const hasSteer = yield* inputs.hasPending(input.sessionId, "steer"); - const hasFollowUp = hasSteer ? false : yield* inputs.hasPending(input.sessionId, "followUp"); + const hasSteer = yield* inputs.hasPending(input.threadId, "steer"); + const hasFollowUp = hasSteer ? false : yield* inputs.hasPending(input.threadId, "followUp"); if (!input.force && !hasSteer && !hasFollowUp) return; yield* Effect.annotateCurrentSpan({ "gen_ai.operation.name": "invoke_agent", "gen_ai.agent.name": "codework", - "gen_ai.conversation.id": input.sessionId, + "gen_ai.conversation.id": input.threadId, }); // A recovered or explicitly resumed run first answers the reply it stopped in (cut off, or ended @@ -451,7 +459,7 @@ export const layer = (options: Options = {}) => (input.recovered === true || input.force) && !hasSteer && hasFollowUp && - (yield* context.assemble(input.sessionId).pipe( + (yield* context.assemble(input.threadId).pipe( Effect.map((assembled) => { const leaf = assembled.lastAssistant?.entryId === assembled.leafEntryId @@ -465,14 +473,14 @@ export const layer = (options: Options = {}) => // A cut-off run still promotes steers that land while it loads: they join the reply being answered. let promotion: Promotion = hasSteer || cutOff ? "steer" : hasFollowUp ? "followUp" : undefined; /** Steers when there are any, else the oldest follow-up: what a run promotes first. */ - const peekPending = (sessionId: SessionSchema.ID) => - Effect.map(inputs.pending(sessionId), (queued) => { + const peekPending = (threadId: ThreadSchema.ID) => + Effect.map(inputs.pending(threadId), (queued) => { const steers = queued.filter((item) => item.delivery === "steer"); return steers.length > 0 ? steers : queued.slice(0, 1); }); const drain = Effect.gen(function* () { - let snapshot = yield* state.snapshot(input.sessionId); + let snapshot = yield* state.snapshot(input.threadId); let continuing = input.force; // Tool results and steers continue first; a follow-up only once both are exhausted. while (true) { @@ -481,7 +489,7 @@ export const layer = (options: Options = {}) => const completed: Hooks.Completed | undefined = result === undefined ? undefined - : { ...result, snapshot, pending: yield* peekPending(input.sessionId) }; + : { ...result, snapshot, pending: yield* peekPending(input.threadId) }; const decision = completed === undefined ? undefined : yield* hooks.finishTurn(completed); // Queued steers and follow-ups stay for the next run. if (decision === "end") return; @@ -489,14 +497,14 @@ export const layer = (options: Options = {}) => // A forced continuation after a finished reply sends nothing: the leaf guard in // `runTurnAttempt` ends the turn unless something was appended after it. continuing = toolUse || decision === "continue"; - const steer = yield* inputs.hasPending(input.sessionId, "steer"); + const steer = yield* inputs.hasPending(input.threadId, "steer"); // Only tool results outrank a follow-up. A forced continuation rides along with it: the // follow-up is the natural next request, and the continuation only its fallback. - const followUp = !toolUse && !steer && (yield* inputs.hasPending(input.sessionId, "followUp")); + const followUp = !toolUse && !steer && (yield* inputs.hasPending(input.threadId, "followUp")); if (!continuing && !steer && !followUp) return; promotion = followUp ? "followUp" : "steer"; - const next = yield* state.snapshot(input.sessionId); + const next = yield* state.snapshot(input.threadId); snapshot = completed === undefined ? next : yield* hooks.prepareNextTurn(completed, next); } }); diff --git a/packages/harness/src/runner/run.ts b/packages/harness/src/runner/run.ts index 666faa9e..67877973 100644 --- a/packages/harness/src/runner/run.ts +++ b/packages/harness/src/runner/run.ts @@ -12,14 +12,15 @@ import { SandboxInstance } from "../sandbox/instance.ts"; import { SandboxIO } from "../sandbox/io.ts"; import { optional } from "../schema.ts"; import type { Settings } from "../settings/settings.ts"; -import type { ID as SessionId } from "../session/schema.ts"; import type { SessionLinkedSpaceNotFoundError, SessionNotFoundError } from "../session/session.ts"; +import type { ThreadSchema } from "../thread/schema.ts"; +import type { ThreadNotFoundError } from "../thread/thread.ts"; import type { State } from "../state/state.ts"; export class SandboxDirectoryNotFoundError extends Schema.TaggedError()( "Runner.SandboxDirectoryNotFoundError", { - sessionId: Schema.String, + threadId: Schema.String, sandboxInstanceId: SandboxInstance.ID, directory: Schema.String, }, @@ -160,7 +161,7 @@ export class ModelNotFoundError extends Schema.TaggedError() } export class LLMStreamError extends Schema.TaggedError()("Runner.LLMStreamError", { - sessionId: Schema.String, + threadId: Schema.String, reason: Schema.String, }) { override get message(): string { @@ -176,6 +177,7 @@ export type RunError = | ContextDecodeError | ContextEncodeError | SessionNotFoundError + | ThreadNotFoundError | SessionLinkedSpaceNotFoundError | Settings.SettingsError | State.SnapshotError @@ -186,7 +188,7 @@ export type RunError = export interface Interface { /** Drains eligible durable work. Explicit runs perform one provider attempt even when no work is eligible. */ readonly run: (input: { - readonly sessionId: SessionId; + readonly threadId: ThreadSchema.ID; readonly force: boolean; /** Resuming a run a previous process left unfinished: its cut-off reply is answered first. */ readonly recovered?: boolean; diff --git a/packages/harness/src/sandbox/control.ts b/packages/harness/src/sandbox/control.ts index 39280f31..0c721d4e 100644 --- a/packages/harness/src/sandbox/control.ts +++ b/packages/harness/src/sandbox/control.ts @@ -229,7 +229,7 @@ export const make = Effect.fn("Sandbox.Controller.make")(function* (options: Opt if (!lease.mounted) return; const now = yield* Effect.clockWith((clock) => clock.currentTimeMillis); yield* sql` - UPDATE sandbox_instance + UPDATE sandbox SET last_unmounted_at = ${now}, last_used_at = ${now}, updated_at = ${now} WHERE id = ${id} `.pipe(Effect.orDie); @@ -340,7 +340,7 @@ export const make = Effect.fn("Sandbox.Controller.make")(function* (options: Opt ? undefined : yield* encodePersistedErrorJson(input.lastError).pipe(Effect.orDie); yield* sql` - UPDATE sandbox_instance + UPDATE sandbox SET status = ${input.status}, provider_status = ${input.providerStatus ?? null}, @@ -415,7 +415,7 @@ export const make = Effect.fn("Sandbox.Controller.make")(function* (options: Opt message: "sandbox creation was interrupted before provisioning completed", }).pipe(Effect.orDie); yield* sql` - UPDATE sandbox_instance + UPDATE sandbox SET status = 'faulted', last_error = ${creationInterrupted}, @@ -488,7 +488,7 @@ export const make = Effect.fn("Sandbox.Controller.make")(function* (options: Opt Object.keys(metadata).length === 0 ? null : yield* encodeMetadataJson(metadata).pipe(Effect.orDie); const now = yield* Effect.clockWith((clock) => clock.currentTimeMillis); yield* sql` - UPDATE sandbox_instance + UPDATE sandbox SET provider_resource_id = ${provisioned.providerResourceId ?? null}, runtime_config = ${runtimeConfig}, @@ -693,7 +693,7 @@ export const make = Effect.fn("Sandbox.Controller.make")(function* (options: Opt if (id !== SandboxInstance.ID.local) { const now = yield* Effect.clockWith((clock) => clock.currentTimeMillis); yield* sql` - UPDATE sandbox_instance + UPDATE sandbox SET status = 'online', last_error = NULL, @@ -917,7 +917,7 @@ export const make = Effect.fn("Sandbox.Controller.make")(function* (options: Opt .withTransaction( Effect.gen(function* () { yield* sql` - UPDATE sandbox_instance + UPDATE sandbox SET status = 'removed', removed_at = ${now}, diff --git a/packages/harness/src/sandbox/store.ts b/packages/harness/src/sandbox/store.ts index e31e82a6..7bb6e4b3 100644 --- a/packages/harness/src/sandbox/store.ts +++ b/packages/harness/src/sandbox/store.ts @@ -4,7 +4,7 @@ import { SandboxInstanceRow } from "../db/schema.sql.ts"; import { SandboxInstance } from "./instance.ts"; /** - * Row-level access to `sandbox_instance`. Deliberately dumb: it persists and + * Row-level access to `sandbox`. Deliberately dumb: it persists and * reads instance rows and knows nothing about drivers, provisioning, reference * counts, or lifecycle rules. `Sandbox.Controller` is added on top of this and * owns all of that. @@ -52,13 +52,13 @@ export const make = Effect.gen(function* () { const findRow = SqlSchema.findOneOption({ Request: SandboxInstance.ID, Result: SandboxInstanceRow, - execute: (id) => sql`SELECT * FROM sandbox_instance WHERE id = ${id}`, + execute: (id) => sql`SELECT * FROM sandbox WHERE id = ${id}`, }); const listRows = SqlSchema.findAll({ Request: Schema.Void, Result: SandboxInstanceRow, - execute: () => sql`SELECT * FROM sandbox_instance ORDER BY created_at`, + execute: () => sql`SELECT * FROM sandbox ORDER BY created_at`, }); // ON CONFLICT(id), not OR IGNORE: the ignore must cover exactly the @@ -67,7 +67,7 @@ export const make = Effect.gen(function* () { // existing namespace — and surface it as an inexplicable missing row. const insertRow = SqlSchema.void({ Request: SandboxInstanceRow.insert, - execute: (row) => sql`INSERT INTO sandbox_instance ${sql.insert(row)} ON CONFLICT(id) DO NOTHING`, + execute: (row) => sql`INSERT INTO sandbox ${sql.insert(row)} ON CONFLICT(id) DO NOTHING`, }); const find = (id: SandboxInstance.ID) => findRow(id).pipe(Effect.orDie); @@ -113,7 +113,7 @@ export const make = Effect.gen(function* () { // Re-reading afterwards would answer a different question, since another // writer may have moved the row in between. const moved = yield* sql` - UPDATE sandbox_instance + UPDATE sandbox SET status = ${input.to}, updated_at = ${now} WHERE id = ${input.id} AND ${sql.in("status", input.from)} RETURNING id diff --git a/packages/harness/src/session/live.ts b/packages/harness/src/session/live.ts index 9eb70744..ec906e3e 100644 --- a/packages/harness/src/session/live.ts +++ b/packages/harness/src/session/live.ts @@ -1,5 +1,5 @@ /* - * @file The session service with its projections registered. + * @file The session, thread and document services with their projections registered. * * Provide this, not `Session.layer` alone. `Control.prompt` records a prompt by * publishing an event, and the row only appears because a projector reacted to @@ -11,6 +11,6 @@ import { Layer } from "effect"; import { SessionProjector } from "./projector.ts"; import { Session } from "./session.ts"; -export const layer = Layer.provideMerge(SessionProjector.layer, Session.layer); +export const layer = Layer.provideMerge(SessionProjector.layer, Session.storeLayer); export * as SessionLive from "./live.ts"; diff --git a/packages/harness/src/session/projector.ts b/packages/harness/src/session/projector.ts index 4c0e44b1..4b091e84 100644 --- a/packages/harness/src/session/projector.ts +++ b/packages/harness/src/session/projector.ts @@ -5,7 +5,7 @@ * must happen exactly once and before anything publishes -- `Layer` gives both, * because a layer is built once per graph, at startup. * - * Registering from `SessionInput.make` instead would run once per consumer, and + * Registering from `Inbox.make` instead would run once per consumer, and * a projector registered twice runs twice on one event: the second insert hits * ON CONFLICT, raises LifecycleConflict inside the commit, and rolls the event * back. Every admission would fail, blaming a conflict that does not exist. @@ -32,20 +32,29 @@ import { DateTime, Effect, Layer } from "effect"; import { ContextCodec } from "../context/codec.ts"; import { Event } from "../event/event.ts"; import { EventList } from "../event/list.ts"; -import { SessionInput } from "./input/input.ts"; +import { Document } from "../document/document.ts"; +import { Inbox } from "../inbox/inbox.ts"; +import { ThreadSchema } from "../thread/schema.ts"; +import { Thread } from "../thread/thread.ts"; import { PromptSchema } from "./prompt/schema.ts"; import type { SessionMessageSchema } from "./message/schema.ts"; -import { SessionSchema } from "./schema.ts"; import { Session } from "./session.ts"; export const layer = Layer.effectDiscard( Effect.gen(function* () { const events = yield* Event.Service; - const input = yield* SessionInput.make; + const input = yield* Inbox.make; const sessions = yield* Session.Service; + const threads = yield* Thread.Service; + const documents = yield* Document.Service; + + /** The sequence is assigned by the commit itself, so a durable event without one never went through it. */ + const seqOf = (event: { readonly type: string; readonly durable?: { readonly seq: number } }) => + event.durable === undefined + ? Effect.die(`${event.type} is missing its aggregate sequence`) + : Effect.succeed(event.durable.seq); const encodeAssistant = Effect.fn("SessionProjector.encodeAssistant")(function* (input: { - readonly sessionId: SessionSchema.ID; readonly messageId: SessionMessageSchema.ID; readonly message: Message.AssistantMessage; }) { @@ -57,16 +66,25 @@ export const layer = Layer.effectDiscard( return yield* ContextCodec.encodeMessage(input.message).pipe(Effect.orDie); }); + yield* events.project(EventList.SessionCreated, (event) => + seqOf(event).pipe(Effect.flatMap((seq) => sessions.projectCreated(event.data, seq))), + ); + + yield* events.project(EventList.ThreadForked, (event) => + seqOf(event).pipe(Effect.flatMap((seq) => threads.projectForked(event.data, seq))), + ); + + yield* events.project(EventList.ThreadConfigured, (event) => + seqOf(event).pipe(Effect.flatMap((seq) => documents.configure(event.data.threadId, event.data.change, seq))), + ); + yield* events.project(EventList.PromptAdmitted, (event) => Effect.gen(function* () { - // The sequence is assigned by the commit itself, so a durable event - // arriving without one means it never went through the transaction. - if (event.durable === undefined) - return yield* Effect.die("PromptAdmitted is missing its aggregate sequence"); yield* input.projectAdmitted({ - admittedSeq: event.durable.seq, + admittedSeq: yield* seqOf(event), id: event.data.messageId, sessionId: event.data.sessionId, + threadId: event.data.threadId, prompt: event.data.prompt, delivery: event.data.delivery, timeCreated: event.data.timestamp, @@ -76,12 +94,10 @@ export const layer = Layer.effectDiscard( yield* events.project(EventList.PromptCancelled, (event) => Effect.gen(function* () { - if (event.durable === undefined) - return yield* Effect.die("PromptCancelled is missing its aggregate sequence"); yield* input.projectCancelled({ - cancelledSeq: event.durable.seq, + cancelledSeq: yield* seqOf(event), id: event.data.messageId, - sessionId: event.data.sessionId, + threadId: event.data.threadId, }); }), ); @@ -89,7 +105,7 @@ export const layer = Layer.effectDiscard( yield* events.project(EventList.PromptUpdated, (event) => input.projectUpdated({ id: event.data.messageId, - sessionId: event.data.sessionId, + threadId: event.data.threadId, ...(event.data.prompt === undefined ? {} : { prompt: event.data.prompt }), ...(event.data.delivery === undefined ? {} : { delivery: event.data.delivery }), }), @@ -99,11 +115,12 @@ export const layer = Layer.effectDiscard( // what makes `promoteSteers` / `promoteFollowUp` take effect at all. yield* events.project(EventList.Prompted, (event) => Effect.gen(function* () { - if (event.durable === undefined) return yield* Effect.die("Prompted is missing its aggregate sequence"); + const seq = yield* seqOf(event); yield* input.projectPrompted({ - promotedSeq: event.durable.seq, + promotedSeq: seq, id: event.data.messageId, sessionId: event.data.sessionId, + threadId: event.data.threadId, prompt: event.data.prompt, delivery: event.data.delivery, timeCreated: event.data.timestamp, @@ -111,9 +128,6 @@ export const layer = Layer.effectDiscard( // Promotion is what puts a prompt into the conversation, so the append // belongs in this commit: "promoted but absent from history" is then // unrepresentable rather than a window someone has to reconcile. - // Materialize the canonical conversation message now. Context later - // rehydrates this envelope + parts; aikit performs only the target-model - // conversion when Loop calls the provider. const encoded = yield* ContextCodec.encodeMessage( Message.createUserMessage({ messageId: event.data.messageId, @@ -122,11 +136,11 @@ export const layer = Layer.effectDiscard( parts: [...event.data.prompt.parts], }), ).pipe(Effect.orDie); - yield* sessions + yield* threads .append({ id: event.data.messageId, - sessionId: event.data.sessionId, - seq: event.durable.seq, + threadId: event.data.threadId, + seq, state: "committed", ...encoded, // Rides the in-memory payload; the event row never carried it, so @@ -134,29 +148,19 @@ export const layer = Layer.effectDiscard( ...(event.metadata === undefined ? {} : { metadata: event.metadata }), }) .pipe(Effect.orDie); - const title = SessionSchema.titleFrom(PromptSchema.Prompt.text(event.data.prompt)); - if (title !== undefined) yield* sessions.retitle(event.data.sessionId, title); + const title = ThreadSchema.titleFrom(PromptSchema.Prompt.text(event.data.prompt)); + if (title !== undefined) yield* threads.retitle(event.data.threadId, title); }), ); - yield* events.project(EventList.ConfigChanged, (event) => { - const { sessionId, timestamp: _timestamp, ...config } = event.data; - return sessions.configure(sessionId, config); - }); - yield* events.project(EventList.LLMStarted, (event) => Effect.gen(function* () { - if (event.durable === undefined) return yield* Effect.die("LLMStarted is missing its aggregate sequence"); - const encoded = yield* encodeAssistant({ - sessionId: event.data.sessionId, - messageId: event.data.messageId, - message: event.data.message, - }); - yield* sessions + const encoded = yield* encodeAssistant(event.data); + yield* threads .append({ id: event.data.messageId, - sessionId: event.data.sessionId, - seq: event.durable.seq, + threadId: event.data.threadId, + seq: yield* seqOf(event), state: "draft", ...encoded, ...(event.metadata === undefined ? {} : { metadata: event.metadata }), @@ -167,14 +171,10 @@ export const layer = Layer.effectDiscard( yield* events.project(EventList.LLMEnded, (event) => Effect.gen(function* () { - const encoded = yield* encodeAssistant({ - sessionId: event.data.sessionId, - messageId: event.data.messageId, - message: event.data.message, - }); - yield* sessions.replaceAssistant({ + const encoded = yield* encodeAssistant(event.data); + yield* threads.replaceAssistant({ id: event.data.messageId, - sessionId: event.data.sessionId, + threadId: event.data.threadId, data: encoded.data, parts: encoded.parts, }); @@ -183,14 +183,11 @@ export const layer = Layer.effectDiscard( yield* events.project(EventList.LLMFailed, (event) => Effect.gen(function* () { - const encoded = yield* encodeAssistant({ - sessionId: event.data.sessionId, - messageId: event.data.messageId, - message: event.data.message, - }); - yield* sessions.abortAssistant({ + const encoded = yield* encodeAssistant(event.data); + yield* threads.abort({ id: event.data.messageId, - sessionId: event.data.sessionId, + threadId: event.data.threadId, + seq: yield* seqOf(event), data: encoded.data, }); }), @@ -199,7 +196,7 @@ export const layer = Layer.effectDiscard( yield* events.project(EventList.ToolSettled, (event) => Effect.gen(function* () { const encoded = yield* ContextCodec.encodePart(event.data.part).pipe(Effect.orDie); - yield* sessions.settleToolCall({ + yield* threads.settleToolCall({ entryId: event.data.messageId, callId: event.data.callID, status: event.data.part.status, @@ -209,13 +206,14 @@ export const layer = Layer.effectDiscard( ); yield* events.project(EventList.TurnEnded, (event) => - sessions - .setEntryState({ + Effect.gen(function* () { + yield* threads.commit({ id: event.data.messageId, - sessionId: event.data.sessionId, - state: "committed", - }) - .pipe(Effect.andThen(sessions.resetRestarts(event.data.sessionId))), + threadId: event.data.threadId, + seq: yield* seqOf(event), + }); + yield* threads.resetRestarts(event.data.threadId); + }), ); }), ); diff --git a/packages/harness/src/session/schema.ts b/packages/harness/src/session/schema.ts index 5dec780f..d12bd439 100644 --- a/packages/harness/src/session/schema.ts +++ b/packages/harness/src/session/schema.ts @@ -1,6 +1,5 @@ -import { Model } from "@codeworksh/aikit"; import { Schema } from "effect"; -import { NonNegativeCost, NonNegativeInt, optional } from "../schema.ts"; +import { NonNegativeCost, NonNegativeInt } from "../schema.ts"; // Session identity lives in `@codeworksh/plugin`: it is on every plugin context and every // tool call, so the brand a plugin holds has to be the one the harness mints. @@ -8,38 +7,6 @@ export { SessionID as ID } from "@codeworksh/plugin/ids"; export const IDFromDb = Schema.String.pipe(Schema.brand("Session.ID")); -/** The title of a session created without one; its first prompt replaces it. */ -export const DEFAULT_TITLE = "Session"; -export const TITLE_MAX_LENGTH = 48; - -/** - * A title from a prompt: its first non-empty line, whitespace collapsed, cut on a word boundary - * to {@link TITLE_MAX_LENGTH} characters including the ellipsis. `undefined` for a blank prompt. - */ -export const titleFrom = (text: string): string | undefined => { - const line = text - .split("\n") - .map((one) => one.replace(/\s+/g, " ").trim()) - .find((one) => one.length > 0); - if (line === undefined || line.length <= TITLE_MAX_LENGTH) return line; - const cut = line.slice(0, TITLE_MAX_LENGTH - 1); - const space = cut.lastIndexOf(" "); - return `${(space > 0 ? cut.slice(0, space) : cut).trimEnd()}…`; -}; - -export const ThinkingLevel = Schema.Literals(Object.values(Model.ThinkingLevelEnum)); - -/** - * The model and thinking level chosen for a session, over its settings. Every key is optional: - * an absent one means the session follows its settings. `session.config.changed` carries only the - * keys it changes, and the `session.config` column holds them merged. - */ -export const Config = Schema.Struct({ - model: optional(Schema.Struct({ provider: Schema.String, id: Schema.String })), - thinkingLevel: optional(ThinkingLevel), -}); -export type Config = typeof Config.Type; - /** * Why an execution stopped. Supplied by whoever asked for the interruption -- * a stop the user asked for and a stop the process imposed on shutdown are @@ -48,14 +15,6 @@ export type Config = typeof Config.Type; export const InterruptReason = Schema.Literals(["user", "shutdown"]); export type InterruptReason = typeof InterruptReason.Type; -/** - * A run left unfinished: `running` while one is in flight, and still after the process died during - * it; `closed` once a shutdown stopped it. Either resumes when the server starts again; a run that - * ended any other way leaves none. - */ -export const Execution = Schema.Literals(["running", "closed"]); -export type Execution = typeof Execution.Type; - // Shared persistence-boundary codecs. Append validates before writing; fork // decodes again because legacy/imported rows may predate that validation. export const JsonObject = Schema.fromJsonString(Schema.Record(Schema.String, Schema.Unknown)); diff --git a/packages/harness/src/session/session.ts b/packages/harness/src/session/session.ts index 191ba26b..e7b07d70 100644 --- a/packages/harness/src/session/session.ts +++ b/packages/harness/src/session/session.ts @@ -1,7 +1,5 @@ -import { Context, DateTime, Effect, Layer, Option, Result, Schema } from "effect"; +import { Context, DateTime, Effect, Layer, Option, Schema } from "effect"; import { SqlClient, SqlSchema } from "effect/sql"; -import { uuidv7 } from "uuidv7"; -import { Database } from "../db/db.ts"; import { type EntryState, entryStates, @@ -12,19 +10,22 @@ import { type PartType, partTypes, ProjectRow, - SessionEntryPartRow, - SessionEntryRow, SessionRow, SpaceRow, + ThreadRow, type ToolStatus, toolStatuses, } from "../db/schema.sql.ts"; +import { Document } from "../document/document.ts"; +import { DocumentSchema } from "../document/schema.ts"; import { Event } from "../event/event.ts"; import { EventList } from "../event/list.ts"; import { ProjectSchema } from "../project/schema.ts"; import { AbsolutePath } from "../schema.ts"; import { SpaceSchema } from "../space/schema.ts"; import { Space } from "../space/space.ts"; +import { ThreadSchema } from "../thread/schema.ts"; +import { Thread } from "../thread/thread.ts"; import { posix } from "../util/posix.ts"; import { Slug } from "../util/slug.ts"; import { SessionSchema } from "./schema.ts"; @@ -34,8 +35,6 @@ export { entryTypes, messageEntryTypes, partTypes, - SessionEntryPartRow, - SessionEntryRow, SessionRow, toolStatuses, type EntryState, @@ -49,30 +48,14 @@ export class SessionNotFoundError extends Schema.TaggedError()( "SessionLinkedSpaceNotFoundError", - { sessionId: Schema.String }, + { sessionId: Schema.String, threadId: Schema.optional(Schema.String) }, ) {} -export class EntryNotFoundError extends Schema.TaggedError()("EntryNotFoundError", { - sessionId: Schema.String, - entryId: Schema.String, -}) {} - -export class ToolCallNotFoundError extends Schema.TaggedError()("ToolCallNotFoundError", { - entryId: Schema.String, - callId: Schema.String, -}) {} - -// Structural rejection — the data-structure layer's "index out of bounds". -export class InvalidEntryDataError extends Schema.TaggedError()("InvalidEntryDataError", { - entryId: Schema.String, - type: Schema.String, - reason: Schema.String, -}) {} - // Relink rejections: the target is not a live space, the session would cross // projects, or an explicit directory would break the containment invariant. export class RelinkError extends Schema.TaggedError()("RelinkError", { @@ -87,16 +70,14 @@ export class RelinkError extends Schema.TaggedError()("RelinkError" }) {} export type RelinkReason = RelinkError["reason"]; -// I6: a session's directory is its space's location or sits underneath it. +// A directory is its space's location or sits underneath it. const directoryWithin = (location: AbsolutePath, directory: AbsolutePath) => directory === location || directory.startsWith(`${location}/`); /** - * Re-root a session directory onto another space of the same project: the - * position relative to the old root is kept, so `dir = /old/app/pkg/x` under - * `from = /old/app` lands on `/new/app/pkg/x` under `to = /new/app`. A - * directory not under the old root — a broken invariant — lands on the new - * root instead. + * Re-root a directory onto another space of the same project: the position relative to the old + * root is kept, so `dir = /old/app/pkg/x` under `from = /old/app` lands on `/new/app/pkg/x` under + * `to = /new/app`. A directory not under the old root — a broken invariant — lands on the new root. */ export const rebaseDirectory = (from: AbsolutePath, to: AbsolutePath, directory: AbsolutePath): AbsolutePath => { if (!directoryWithin(from, directory)) return to; @@ -122,9 +103,9 @@ export class SessionNotLinkedError extends Schema.TaggedError>; + /** The main thread's first `agent` value: the choices given to create. */ + readonly agent?: DocumentSchema.Agent; } -/** Sessions of every space of a project, or of one space. */ -export type ListInput = { readonly projectId: ProjectSchema.ID } | { readonly spaceId: SpaceSchema.ID }; - -export interface AppendPart { - readonly id?: string; // uuidv7; generated when omitted - readonly type: PartType; - readonly status?: ToolStatus; // toolCall parts only - readonly callId?: string; // toolCall parts only - readonly toolName?: string; // toolCall parts only - readonly data: string; // verbatim aikit part JSON +export interface Created { + readonly session: SessionRow; + readonly thread: ThreadRow; } -export interface AppendEntry { - readonly id: string; // uuidv7; == aikit messageId for message types - readonly sessionId: SessionSchema.ID; - /** - * Position in the session's durable log — the sequence of the event that - * produced this entry. Must exceed every existing entry's seq, which the - * event log guarantees since sequences only ever advance. - */ - readonly seq: number; - readonly type: EntryType; - /** Defaults to committed. Only LLMStarted inserts a draft. */ - readonly state?: EntryState; - readonly data: string; // JSON: full payload, or message envelope - readonly parts?: ReadonlyArray; // order = partIndex; message types only - readonly parentId?: string; // explicit branch appends only; default = current leaf - /** - * Publish-time context from the event that produced this entry. The log does - * not store event metadata, so if it is to survive at all it survives here. - */ - readonly metadata?: Readonly>; -} - -export interface SettleToolCall { - readonly entryId: string; // owning assistant entry - readonly callId: string; // from the loop's tool.execution.end - readonly status: Exclude; - readonly data: string; // settled aikit part JSON, verbatim -} - -export interface ReplaceAssistant { - readonly id: string; - readonly sessionId: SessionSchema.ID; - readonly data: string; - readonly parts: ReadonlyArray; -} - -export interface SetEntryState { - readonly id: string; - readonly sessionId: SessionSchema.ID; - readonly state: "committed"; -} - -export interface AbortAssistant { - readonly id: string; - readonly sessionId: SessionSchema.ID; - /** Encoded assistant stub. Parts are always deleted by the transition. */ - readonly data: string; -} - -// Fork copies ONE path (root → fork point) into a new session — never sibling -// or abandoned branches. -// clone = fork at the current leaf. -export interface ForkInput { - readonly sessionId: SessionSchema.ID; // source session - readonly entryId?: string; // fork point; default = current leaf (clone) - readonly mode?: "at" | "before"; // default "at"; "before" valid only for user entries - readonly id?: SessionSchema.ID; // new session id; generated when omitted - readonly title?: string; // default: source title - readonly tag?: string; // default: source tag -} +/** Sessions of every space of a project, or of one space. */ +export type ListInput = { readonly projectId: ProjectSchema.ID } | { readonly spaceId: SpaceSchema.ID }; -// The env move: point a session at another space of the same project (remote -// env died, repo cloned locally, worktree consolidated). Transcript entries -// are keyed by session id and are untouched. +// The env move: point a session, or one thread, at another space of the same project (remote env +// died, repo cloned locally, worktree consolidated). Entries are untouched either way. export interface RelinkInput { readonly sessionId: SessionSchema.ID; + /** Absent moves the session's default space and directory; present sets that thread's `placement`. */ + readonly threadId?: ThreadSchema.ID; /** Target space; must be active and belong to the session's project. */ readonly spaceId: SpaceSchema.ID; /** @@ -225,128 +145,58 @@ export interface RelinkInput { readonly directory?: AbsolutePath; } -// Entry row + its part rows, zipped. Message decode happens above this layer. -export interface HydratedEntry { - readonly entry: SessionEntryRow; - readonly parts: ReadonlyArray; +/** Where a thread runs: its `agent.placement`, else the session's space and directory. */ +export interface Placement { + readonly space: SpaceSchema.Info; + readonly directory: AbsolutePath; } export interface Interface { - readonly create: (input: CreateSession) => Effect.Effect; + /** A session with its `main` thread and that thread's `agent` document, in one event. */ + readonly create: (input: CreateSession) => Effect.Effect; + /** Projector of `session.created`. */ + readonly projectCreated: (data: EventList.SessionCreated["data"], seq: number) => Effect.Effect; /** * Point an existing session at a host directory, or clear it with `null`. * - * Separate from `relink`, which moves a session between spaces: this changes nothing about - * where the work happens, only which host project's settings and plugins the session reads. - * The two are independent -- a session can move machines and keep its host project, or stay - * put and be given one it never had. + * Separate from `relink`, which moves where the work happens: this changes only which host + * project's settings and plugins the session reads. */ readonly link: (input: { sessionId: SessionSchema.ID; hostDir: AbsolutePath | null; }) => Effect.Effect; - /** - * Replace a session's {@link SessionSchema.DEFAULT_TITLE}. A session with any other title keeps it, - * so a title someone chose is never overwritten. Returns whether the title changed. - */ - readonly retitle: (sessionId: SessionSchema.ID, title: string) => Effect.Effect; - /** - * Merge chosen keys into a session's {@link SessionSchema.Config}; keys not named keep their - * value. The projection of `session.config.changed`, so it runs inside that event's commit. - */ - readonly configure: (sessionId: SessionSchema.ID, config: SessionSchema.Config) => Effect.Effect; - /** Records that the session has a run to do: one starting, or one a prompt just woke. */ - readonly markRunning: (sessionId: SessionSchema.ID) => Effect.Effect; - /** - * Records how a run ended. `closed` (a shutdown) leaves it to resume. `stopped` (the user) and - * `finished` clear it and the restart count, except that a finished run with prompts still - * pending stays marked: they are accepted work a successor, or the next server start, runs. - */ - readonly endExecution: ( - sessionId: SessionSchema.ID, - end: "finished" | "stopped" | "closed", - /** For a stop: the log position it was asked at. A prompt admitted after it keeps the mark. */ - stoppedAt?: number, - ) => Effect.Effect; - /** - * Claims a session's unfinished run for recovery: marks it running again, counting a restart when - * a dying process left it running (a shutdown's closed run is not counted). The new count, or none - * when nothing is unfinished any more, say because the user stopped it since. - */ - readonly claimRestart: (sessionId: SessionSchema.ID) => Effect.Effect>; - /** A committed turn is progress: the restart count starts over. Runs inside `TurnEnded`'s commit. */ - readonly resetRestarts: (sessionId: SessionSchema.ID) => Effect.Effect; - /** Sessions with a run left unfinished, oldest first. */ - readonly unfinished: Effect.Effect>; - readonly get: (sessionId: SessionSchema.ID) => Effect.Effect>; - /** The space a session attaches to — its env and absolute location. None when the session is unknown. */ - readonly space: (sessionId: SessionSchema.ID) => Effect.Effect>; + readonly get: (sessionId: string) => Effect.Effect>; + /** The session's own space — its env and absolute location. None when the session is unknown. */ + readonly space: (sessionId: string) => Effect.Effect>; + /** Where a thread runs. None when the thread is unknown or its space is gone. */ + readonly placement: (threadId: ThreadSchema.ID) => Effect.Effect>; /** The project a session belongs to, through its space. None when the session is unknown. */ - readonly project: (sessionId: SessionSchema.ID) => Effect.Effect>; + readonly project: (sessionId: string) => Effect.Effect>; /** Sessions newest first, across the whole database when no scope is given. */ readonly list: (input?: ListInput) => Effect.Effect; - readonly entry: (entryId: string) => Effect.Effect>; - /** Active root→leaf path with parts attached. */ - readonly path: (sessionId: SessionSchema.ID) => Effect.Effect; - /** All branches, newest-first, paged by entry seq. */ - readonly timeline: (input: { - sessionId: SessionSchema.ID; - cursorSeq?: number; - limit?: number; - }) => Effect.Effect; - /** Unsettled toolCall parts (crash recovery / live status). */ - readonly unsettled: (sessionId: SessionSchema.ID) => Effect.Effect; - readonly append: ( - input: AppendEntry, - ) => Effect.Effect; - /** Projector-only: replace the payload of an existing draft assistant in place. */ - readonly replaceAssistant: (input: ReplaceAssistant) => Effect.Effect; - /** Projector-only: settle one pending/running tool part on a draft assistant. */ - readonly settleToolCall: (input: SettleToolCall) => Effect.Effect; - /** Projector-only: commit a draft after every tool part is terminal. */ - readonly setEntryState: (input: SetEntryState) => Effect.Effect; - /** Projector-only: turn a draft into a partless aborted tombstone. */ - readonly abortAssistant: (input: AbortAssistant) => Effect.Effect; - /** Copy root→fork-point into a new session (`parentId` = source). Clone = fork at leaf. */ - readonly fork: ( - input: ForkInput, - ) => Effect.Effect; /** - * Move the session to another space of the same project. Without an explicit - * `directory` the cwd keeps its position relative to the old root; either way - * the result satisfies I6 (directory at or under the space's location). + * Move the session (or one thread) to another space of the same project. Without an explicit + * `directory` the cwd keeps its position relative to the old root; either way the result is at + * or under the space's location. */ - readonly relink: (input: RelinkInput) => Effect.Effect; - /** Move the leaf cursor; the next append forks a sibling branch. */ - readonly branch: (input: { - sessionId: SessionSchema.ID; - entryId: string; - }) => Effect.Effect; - /** Set or clear (null) the bookmark label on an entry. */ - readonly setLabel: (input: { - sessionId: SessionSchema.ID; - entryId: string; - label: string | null; - }) => Effect.Effect; + readonly relink: ( + input: RelinkInput, + ) => Effect.Effect; + /** Removes the session with every thread, entry, document and its event log. False when unknown. */ + readonly delete: (sessionId: SessionSchema.ID) => Effect.Effect; } export class Service extends Context.Service()("@codeworksh/harness/session/session/Service") {} -const messageTypes: ReadonlySet = new Set(messageEntryTypes); - -// Structural decode of the persisted envelope's usage; failures surface as -// InvalidEntryDataError, never a defect — callers (Context Manager, UI) can act. -const decodeEnvelopeUsage = Schema.decodeUnknownEffect(SessionSchema.AssistantEnvelopeUsage); -const decodeMessageEnvelopeIdentity = Schema.decodeUnknownEffect(SessionSchema.MessageEnvelopeIdentity); -const decodeCompactionData = Schema.decodeUnknownEffect(SessionSchema.CompactionData); -const decodeJsonObject = Schema.decodeUnknownEffect(SessionSchema.JsonObject); -const encodeJsonObject = Schema.encodeEffect(SessionSchema.JsonObject); - export const layer = Layer.effect( Service, Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; const events = yield* Event.Service; + const threads = yield* Thread.Service; + const documents = yield* Document.Service; + const findSession = SqlSchema.findOneOption({ Request: Schema.String, Result: SessionRow, @@ -406,153 +256,60 @@ export const layer = Layer.effect( execute: (row) => sql`INSERT INTO session ${sql.insert(row)}`, }); - const findEntry = SqlSchema.findOneOption({ - Request: Schema.String, - Result: SessionEntryRow, - execute: (id) => sql`SELECT * FROM session_entry WHERE id = ${id}`, - }); - - // seq is supplied by the caller, not computed here: it is the sequence of - // the event that produced the entry, so the durable log and the tree share - // one position space. - // - // The WHERE NOT EXISTS keeps positions strictly advancing. `max(seq) + 1` - // used to make that structural; sourcing seq from outside would otherwise - // downgrade it to an unchecked contract, and a stale position fails - // silently — `selectPath` orders by seq, so a child would sort above its - // own parent. Inserting zero rows is the signal, checked by the callers. - const insertEntry = SqlSchema.findAll({ - Request: SessionEntryRow.insert, - Result: Schema.Struct({ id: Schema.String }), - execute: (row) => sql` - INSERT INTO session_entry (id, session_id, parent_id, seq, type, state, data, label, metadata, created_at, updated_at) - SELECT - ${row.id}, ${row.sessionId}, ${row.parentId ?? null}, ${row.seq}, - ${row.type}, ${row.state}, ${row.data}, ${row.label ?? null}, ${row.metadata ?? null}, - ${row.createdAt}, ${row.updatedAt} - WHERE NOT EXISTS ( - SELECT 1 FROM session_entry WHERE session_id = ${row.sessionId} AND seq >= ${row.seq} - ) - RETURNING id - `, - }); - - // Single-row insert: toolCall and non-tool parts have different encoded - // key sets (FieldOption omits absent keys), so multi-row sql.insert would - // mix shapes. Parts per message are few; sequential inserts inside the - // append transaction are fine. - const insertPart = SqlSchema.void({ - Request: SessionEntryPartRow.insert, - execute: (row) => sql`INSERT INTO session_entry_part ${sql.insert(row)}`, - }); - - // Root→leaf in one query: a parent always exists before its child, so a - // parent's seq is always smaller and ORDER BY seq returns root-first. - const selectPath = SqlSchema.findAll({ - Request: Schema.String, - Result: SessionEntryRow, - execute: (leafEntryId) => sql` - WITH RECURSIVE path AS ( - SELECT * FROM session_entry WHERE id = ${leafEntryId} - UNION ALL - SELECT e.* FROM session_entry e - JOIN path p ON e.id = p.parent_id AND e.session_id = p.session_id - ) - SELECT * FROM path ORDER BY seq - `, - }); - - // Recovery/import fallback when session.leaf_entry_id is NULL (§4.1). - const selectLatestEntry = SqlSchema.findOneOption({ - Request: Schema.String, - Result: SessionEntryRow, - execute: (sessionId) => sql` - SELECT * FROM session_entry WHERE session_id = ${sessionId} ORDER BY seq DESC LIMIT 1 - `, - }); - - const selectTimelinePage = SqlSchema.findAll({ - Request: Schema.Struct({ - sessionId: Schema.String, - cursorSeq: Schema.NullOr(Schema.Int), - limit: Schema.Int, - }), - Result: SessionEntryRow, - execute: (r) => sql` - SELECT * FROM session_entry - WHERE session_id = ${r.sessionId} AND (${r.cursorSeq} IS NULL OR seq < ${r.cursorSeq}) - ORDER BY seq DESC LIMIT ${r.limit} - `, - }); - - const selectPartsForEntries = SqlSchema.findAll({ - Request: Schema.Array(Schema.String), - Result: SessionEntryPartRow, - execute: (entryIds) => sql` - SELECT * FROM session_entry_part - WHERE ${sql.in("entry_id", entryIds)} - ORDER BY entry_id, part_index - `, - }); - - const selectUnsettled = SqlSchema.findAll({ - Request: Schema.String, - Result: SessionEntryPartRow, - execute: (sessionId) => sql` - SELECT * FROM session_entry_part - WHERE session_id = ${sessionId} AND status IN ('pending', 'running') - ORDER BY entry_id, part_index - `, - }); - const epochNow = Effect.map(DateTime.now, DateTime.toEpochMillis); - // Zip rule (§7.2): group parts by entry id, attach while walking entries. - // entry.type says whether parts are expected; a mismatch is corruption, - // not an edge case (§10.4). - const hydrate = ( - entries: ReadonlyArray, - parts: ReadonlyArray, - ): HydratedEntry[] => { - const byEntry = new Map(); - for (const part of parts) { - const bucket = byEntry.get(part.entryId); - if (bucket) bucket.push(part); - else byEntry.set(part.entryId, [part]); - } - return entries.map((entry) => ({ entry, parts: byEntry.get(entry.id) ?? [] })); - }; - - const partsFor = Effect.fnUntraced(function* (entries: ReadonlyArray) { - const ids = entries.filter((entry) => messageTypes.has(entry.type)).map((entry) => entry.id); - if (ids.length === 0) return []; - return yield* selectPartsForEntries(ids).pipe(Effect.orDie); - }); + const projectCreated = Effect.fn("Session.projectCreated")(function* ( + data: EventList.SessionCreated["data"], + seq: number, + ) { + const row = yield* SessionRow.insert.makeEffect({ + id: data.sessionId, + spaceId: data.spaceId, + slug: data.slug, + directory: data.directory, + hostDir: Option.fromUndefinedOr(data.hostDir), + tag: Option.fromUndefinedOr(data.tag), + metadata: Option.fromUndefinedOr(data.metadata), + }); + yield* insertSession(row); + yield* threads.insert({ + id: data.threadId, + sessionId: data.sessionId, + origin: { kind: "main" }, + ...(data.title === undefined ? {} : { title: data.title }), + seq, + }); + yield* documents.create({ + sessionId: data.sessionId, + threadId: data.threadId, + definition: DocumentSchema.agent, + value: yield* Schema.encodeEffect(DocumentSchema.Agent)(data.agent), + seq, + }); + }, Effect.orDie); const create = Effect.fn("Session.create")(function* (input: CreateSession) { const id = input.id ?? SessionSchema.ID.create(); - const row = yield* SessionRow.insert - .makeEffect({ - id, - spaceId: input.spaceId, - parentId: Option.fromUndefinedOr(input.parentId), - slug: yield* Slug.create(id), - directory: input.directory, - hostDir: Option.fromUndefinedOr(input.hostDir), - config: Option.none(), - execution: Option.none(), - title: input.title, - tag: Option.fromUndefinedOr(input.tag), - metadata: Option.fromUndefinedOr(input.metadata as Record | undefined), - leafEntryId: Option.none(), - }) - .pipe(Effect.orDie); - yield* insertSession(row).pipe(Effect.orDie); - const created = yield* findSession(id).pipe(Effect.orDie); - if (Option.isNone(created)) { + const threadId = input.mainThreadId ?? ThreadSchema.ID.create(); + yield* events.publish(EventList.SessionCreated, { + timestamp: yield* DateTime.now, + sessionId: id, + threadId, + spaceId: input.spaceId, + slug: yield* Slug.create(id), + directory: input.directory, + ...(input.hostDir === undefined ? {} : { hostDir: input.hostDir }), + ...(input.tag === undefined ? {} : { tag: input.tag }), + ...(input.metadata === undefined ? {} : { metadata: { ...input.metadata } }), + ...(input.title === undefined ? {} : { title: input.title }), + agent: input.agent ?? {}, + }); + const session = yield* findSession(id).pipe(Effect.orDie); + const thread = yield* threads.get(threadId); + if (Option.isNone(session) || Option.isNone(thread)) { return yield* Effect.die(new Error("session insert did not persist")); } - return created.value; + return { session: session.value, thread: thread.value }; }); const get = Effect.fn("Session.get")(function* (sessionId: string) { @@ -563,6 +320,20 @@ export const layer = Layer.effect( return Option.map(yield* findSpace(sessionId).pipe(Effect.orDie), Space.fromRow); }); + const placement = Effect.fn("Session.placement")(function* (threadId: ThreadSchema.ID) { + const thread = yield* threads.get(threadId); + if (Option.isNone(thread)) return Option.none(); + const chosen = (yield* documents.agent(threadId)).placement; + if (chosen !== undefined) { + const target = yield* findSpaceById(chosen.spaceId).pipe(Effect.orDie); + return Option.map(target, (row) => ({ space: Space.fromRow(row), directory: chosen.directory })); + } + const session = yield* findSession(thread.value.sessionId).pipe(Effect.orDie); + const own = yield* space(thread.value.sessionId); + if (Option.isNone(session) || Option.isNone(own)) return Option.none(); + return Option.some({ space: own.value, directory: session.value.directory }); + }); + // A bare lookup has no checkout to ask, so the info carries no `vcs` (as with `Project.get`). const project = Effect.fn("Session.project")(function* (sessionId: string) { return Option.map( @@ -581,618 +352,6 @@ export const layer = Layer.effect( return yield* rows.pipe(Effect.orDie); }); - const entry = Effect.fn("Session.entry")(function* (entryId: string) { - const found = yield* findEntry(entryId).pipe(Effect.orDie); - if (Option.isNone(found)) return Option.none(); - const parts = yield* partsFor([found.value]); - return Option.some(hydrate([found.value], parts)[0]!); - }); - - const resolveLeaf = Effect.fnUntraced(function* (session: SessionRow) { - if (Option.isSome(session.leafEntryId)) return session.leafEntryId; - const latest = yield* selectLatestEntry(session.id).pipe(Effect.orDie); - return Option.map(latest, (row) => row.id); - }); - - const path = Effect.fn("Session.path")(function* (sessionId: string) { - const session = yield* findSession(sessionId).pipe(Effect.orDie); - if (Option.isNone(session)) return []; - const leaf = yield* resolveLeaf(session.value); - if (Option.isNone(leaf)) return []; - const entries = yield* selectPath(leaf.value).pipe(Effect.orDie); - const parts = yield* partsFor(entries); - return hydrate(entries, parts); - }); - - const timeline = Effect.fn("Session.timeline")(function* (input: { - sessionId: string; - cursorSeq?: number; - limit?: number; - }) { - const entries = yield* selectTimelinePage({ - sessionId: input.sessionId, - cursorSeq: input.cursorSeq ?? null, - limit: input.limit ?? 50, - }).pipe(Effect.orDie); - const parts = yield* partsFor(entries); - return hydrate(entries, parts); - }); - - const unsettled = Effect.fn("Session.unsettled")(function* (sessionId: string) { - return yield* selectUnsettled(sessionId).pipe(Effect.orDie); - }); - - type AppendTxResult = - | { readonly _tag: "sessionNotFound" } - | { readonly _tag: "parentNotFound" } - | { readonly _tag: "invalidData"; readonly reason: string } - | { readonly _tag: "inserted"; readonly entry: SessionEntryRow }; - - const append = Effect.fn("Session.append")(function* (input: AppendEntry) { - const parts = input.parts ?? []; - const state = input.state ?? "committed"; - if (state === "aborted" || (input.type !== "assistant" && state !== "committed")) { - return yield* new InvalidEntryDataError({ - entryId: input.id, - type: input.type, - reason: - state === "aborted" - ? "an aborted assistant must be produced by abortAssistant" - : `entry type "${input.type}" must be committed`, - }); - } - if (parts.length > 0 && !messageTypes.has(input.type)) { - return yield* new InvalidEntryDataError({ - entryId: input.id, - type: input.type, - reason: `entry type "${input.type}" must not carry parts`, - }); - } - - // Part data is documented as verbatim aikit JSON and every reader parses - // it as such, so it is validated here for the same reason the envelope - // is: an unparseable part is only discovered by whoever reads it back, - // long after the write that could have rejected it. - for (const [index, part] of parts.entries()) { - yield* decodeJsonObject(part.data).pipe( - Effect.mapError( - (error) => - new InvalidEntryDataError({ - entryId: input.id, - type: input.type, - reason: `part ${index} ("${part.type}") is not a JSON object: ${error.message}`, - }), - ), - ); - } - - const envelopeIdentity = messageTypes.has(input.type) - ? yield* decodeMessageEnvelopeIdentity(input.data).pipe( - Effect.mapError( - (error) => - new InvalidEntryDataError({ entryId: input.id, type: input.type, reason: error.message }), - ), - ) - : undefined; - if (envelopeIdentity !== undefined && envelopeIdentity.messageId !== input.id) { - return yield* new InvalidEntryDataError({ - entryId: input.id, - type: input.type, - reason: `messageId "${envelopeIdentity.messageId}" does not match entry id`, - }); - } - - const compaction = - input.type === "compaction" - ? yield* decodeCompactionData(input.data).pipe( - Effect.mapError( - (error) => - new InvalidEntryDataError({ entryId: input.id, type: input.type, reason: error.message }), - ), - ) - : undefined; - - // The envelope is authoritative; aggregate deltas are never accepted - // independently of the data persisted by this transaction. - const usage = - input.type === "assistant" - ? yield* decodeEnvelopeUsage(input.data).pipe( - Effect.map((envelope) => envelope.usage), - Effect.mapError( - (error) => - new InvalidEntryDataError({ entryId: input.id, type: input.type, reason: error.message }), - ), - ) - : undefined; - - const result: AppendTxResult = yield* sql - .withTransaction( - Effect.gen(function* () { - const session = yield* findSession(input.sessionId); - if (Option.isNone(session)) return { _tag: "sessionNotFound" } as const; - const currentLeaf = yield* resolveLeaf(session.value); - - // Append anchor: explicit branch target - // (validated same-session; the composite FK is the schema backstop), - // else the current leaf resolved with the same fallback reads use (§4.1). - let parentId: Option.Option; - if (input.parentId !== undefined) { - const rows = yield* sql` - SELECT id FROM session_entry - WHERE id = ${input.parentId} AND session_id = ${input.sessionId} - `; - if (rows.length === 0) return { _tag: "parentNotFound" } as const; - parentId = Option.some(input.parentId); - } else { - parentId = currentLeaf; - } - - if (compaction !== undefined && compaction.firstKeptEntryId !== null) { - if (Option.isNone(parentId)) { - return { - _tag: "invalidData", - reason: `firstKeptEntryId "${compaction.firstKeptEntryId}" is not on the compaction path`, - } as const; - } - const compactionPath = yield* selectPath(parentId.value); - if (!compactionPath.some((entry) => entry.id === compaction.firstKeptEntryId)) { - return { - _tag: "invalidData", - reason: `firstKeptEntryId "${compaction.firstKeptEntryId}" is not on the compaction path`, - } as const; - } - } - - const entryRow = yield* SessionEntryRow.insert.makeEffect({ - id: input.id, - sessionId: input.sessionId, - parentId, - seq: input.seq, - type: input.type, - state, - data: input.data, - label: Option.none(), - metadata: Option.fromUndefinedOr(input.metadata), - }); - const inserted = yield* insertEntry(entryRow); - // Keep the aggregate head above what was just written. An entry - // that arrived without an event still occupies its position, and - // the next event must land above it or hit the guard below - // forever. - yield* events.advance(input.sessionId, input.seq); - if (inserted.length === 0) { - return { - _tag: "invalidData", - reason: `seq ${input.seq} does not advance past the session's latest entry`, - } as const; - } - - for (const [partIndex, part] of parts.entries()) { - const partRow = yield* SessionEntryPartRow.insert.makeEffect({ - id: part.id ?? uuidv7(), - entryId: input.id, - sessionId: input.sessionId, - partIndex, - type: part.type, - status: Option.fromUndefinedOr(part.status), - callId: Option.fromUndefinedOr(part.callId), - toolName: Option.fromUndefinedOr(part.toolName), - data: part.data, - }); - yield* insertPart(partRow); - } - - // Advance the cursor and account for the envelope currently stored. - // Draft assistants are adjusted by replace/abort when their terminal - // envelope arrives. - const now = yield* epochNow; - yield* sql` - UPDATE session SET - leaf_entry_id = ${input.id}, - updated_at = ${now}, - cost = cost + ${usage?.cost.total ?? 0}, - tokens_input = tokens_input + ${usage?.input ?? 0}, - tokens_output = tokens_output + ${usage?.output ?? 0}, - tokens_cache_read = tokens_cache_read + ${usage?.cacheRead ?? 0}, - tokens_cache_write = tokens_cache_write + ${usage?.cacheWrite ?? 0} - WHERE id = ${input.sessionId} - `; - - const entry = yield* findEntry(input.id); - if (Option.isNone(entry)) { - return yield* Effect.die(new Error("session append: inserted entry did not persist")); - } - return { _tag: "inserted", entry: entry.value } as const; - }), - ) - .pipe(Effect.orDie); - - switch (result._tag) { - case "sessionNotFound": - return yield* new SessionNotFoundError({ sessionId: input.sessionId }); - case "parentNotFound": - return yield* new EntryNotFoundError({ sessionId: input.sessionId, entryId: input.parentId ?? "" }); - case "invalidData": - return yield* new InvalidEntryDataError({ - entryId: input.id, - type: input.type, - reason: result.reason, - }); - case "inserted": - return result.entry; - } - }); - - const replaceAssistant = Effect.fn("Session.replaceAssistant")(function* (input: ReplaceAssistant) { - const nextEnvelope = yield* decodeEnvelopeUsage(input.data); - yield* sql.withTransaction( - Effect.gen(function* () { - const current = yield* findEntry(input.id); - if ( - Option.isNone(current) || - current.value.sessionId !== input.sessionId || - current.value.type !== "assistant" || - current.value.state !== "draft" - ) { - return yield* Effect.die(`replaceAssistant requires draft assistant ${input.id}`); - } - const previousEnvelope = yield* decodeEnvelopeUsage(current.value.data); - const now = yield* epochNow; - yield* sql`DELETE FROM session_entry_part WHERE entry_id = ${input.id}`; - for (const [partIndex, part] of input.parts.entries()) { - const partRow = yield* SessionEntryPartRow.insert.makeEffect({ - id: part.id ?? uuidv7(), - entryId: input.id, - sessionId: input.sessionId, - partIndex, - type: part.type, - status: Option.fromUndefinedOr(part.status), - callId: Option.fromUndefinedOr(part.callId), - toolName: Option.fromUndefinedOr(part.toolName), - data: part.data, - }); - yield* insertPart(partRow); - } - yield* sql` - UPDATE session_entry SET data = ${input.data}, updated_at = ${now} - WHERE id = ${input.id} AND session_id = ${input.sessionId} AND state = 'draft' - `; - yield* sql` - UPDATE session SET - updated_at = ${now}, - cost = cost + ${nextEnvelope.usage.cost.total - previousEnvelope.usage.cost.total}, - tokens_input = tokens_input + ${nextEnvelope.usage.input - previousEnvelope.usage.input}, - tokens_output = tokens_output + ${nextEnvelope.usage.output - previousEnvelope.usage.output}, - tokens_cache_read = tokens_cache_read + ${nextEnvelope.usage.cacheRead - previousEnvelope.usage.cacheRead}, - tokens_cache_write = tokens_cache_write + ${nextEnvelope.usage.cacheWrite - previousEnvelope.usage.cacheWrite} - WHERE id = ${input.sessionId} - `; - }), - ); - }, Effect.orDie); - - const settleToolCall = Effect.fn("Session.settleToolCall")(function* (input: SettleToolCall) { - const data = yield* decodeJsonObject(input.data).pipe(Effect.orDie); - if (data["type"] !== "toolCall" || data["callID"] !== input.callId || data["status"] !== input.status) { - return yield* Effect.die(`settled tool payload does not match call ${input.callId}`); - } - const now = yield* epochNow; - const rows = yield* sql` - UPDATE session_entry_part - SET data = ${input.data}, status = ${input.status}, updated_at = ${now} - WHERE entry_id = ${input.entryId} - AND call_id = ${input.callId} - AND type = 'toolCall' - AND status IN ('pending', 'running') - AND EXISTS ( - SELECT 1 FROM session_entry - WHERE id = ${input.entryId} AND state = 'draft' - ) - RETURNING id - `; - if (rows.length !== 1) { - return yield* Effect.die(`settleToolCall requires one unsettled call ${input.entryId}/${input.callId}`); - } - }, Effect.orDie); - - const setEntryState = Effect.fn("Session.setEntryState")(function* (input: SetEntryState) { - const now = yield* epochNow; - const rows = yield* sql` - UPDATE session_entry SET state = ${input.state}, updated_at = ${now} - WHERE id = ${input.id} - AND session_id = ${input.sessionId} - AND type = 'assistant' - AND state = 'draft' - AND NOT EXISTS ( - SELECT 1 FROM session_entry_part - WHERE entry_id = ${input.id} AND status IN ('pending', 'running') - ) - RETURNING id - `; - if (rows.length !== 1) return yield* Effect.die(`setEntryState requires a fully-settled draft ${input.id}`); - }, Effect.orDie); - - const abortAssistant = Effect.fn("Session.abortAssistant")(function* (input: AbortAssistant) { - const nextEnvelope = yield* decodeEnvelopeUsage(input.data); - yield* sql.withTransaction( - Effect.gen(function* () { - const current = yield* findEntry(input.id); - if ( - Option.isNone(current) || - current.value.sessionId !== input.sessionId || - current.value.type !== "assistant" || - current.value.state !== "draft" - ) { - return yield* Effect.die(`abortAssistant requires draft assistant ${input.id}`); - } - const previousEnvelope = yield* decodeEnvelopeUsage(current.value.data); - const now = yield* epochNow; - yield* sql`DELETE FROM session_entry_part WHERE entry_id = ${input.id}`; - yield* sql` - UPDATE session_entry - SET state = 'aborted', data = ${input.data}, updated_at = ${now} - WHERE id = ${input.id} AND session_id = ${input.sessionId} AND state = 'draft' - `; - yield* sql` - UPDATE session SET - updated_at = ${now}, - cost = cost + ${nextEnvelope.usage.cost.total - previousEnvelope.usage.cost.total}, - tokens_input = tokens_input + ${nextEnvelope.usage.input - previousEnvelope.usage.input}, - tokens_output = tokens_output + ${nextEnvelope.usage.output - previousEnvelope.usage.output}, - tokens_cache_read = tokens_cache_read + ${nextEnvelope.usage.cacheRead - previousEnvelope.usage.cacheRead}, - tokens_cache_write = tokens_cache_write + ${nextEnvelope.usage.cacheWrite - previousEnvelope.usage.cacheWrite} - WHERE id = ${input.sessionId} - `; - }), - ); - }, Effect.orDie); - - type ForkTxResult = - | { readonly _tag: "sessionNotFound" } - | { readonly _tag: "entryNotFound"; readonly entryId: string } - | { readonly _tag: "invalidMode"; readonly entryId: string; readonly type: string } - | { readonly _tag: "invalidData"; readonly entryId: string; readonly type: string; readonly reason: string } - | { readonly _tag: "forked"; readonly session: SessionRow }; - - // Copied entries get NEW ids (ours are globally unique), - // so every entry-id reference on the path is remapped: - // parent edges, message envelopes' messageId, and compaction's - // `firstKeptEntryId` (when non-null, always on the copied path and mappable). - // branchSummary.fromEntryId is NOT remapped — it points at an abandoned - // leaf that is deliberately not copied; - const rewriteForkedData = Effect.fnUntraced(function* ( - entry: SessionEntryRow, - newId: string, - idMap: ReadonlyMap, - ) { - if (!messageTypes.has(entry.type) && entry.type !== "compaction") return entry.data; - const invalidData = (reason: string) => - new InvalidEntryDataError({ entryId: entry.id, type: entry.type, reason }); - const payload: Record = { - ...(yield* decodeJsonObject(entry.data).pipe(Effect.mapError((error) => invalidData(error.message)))), - }; - if (messageTypes.has(entry.type)) { - const identity = yield* decodeMessageEnvelopeIdentity(entry.data).pipe( - Effect.mapError((error) => invalidData(error.message)), - ); - if (identity.messageId !== entry.id) { - return yield* invalidData(`messageId "${identity.messageId}" does not match entry id`); - } - payload["messageId"] = newId; - } - if (entry.type === "compaction") { - const compaction = yield* decodeCompactionData(entry.data).pipe( - Effect.mapError((error) => invalidData(error.message)), - ); - if (compaction.firstKeptEntryId !== null) { - const mapped = idMap.get(compaction.firstKeptEntryId); - if (mapped === undefined) { - return yield* invalidData( - `firstKeptEntryId "${compaction.firstKeptEntryId}" is not on the copied path`, - ); - } - payload["firstKeptEntryId"] = mapped; - } - } - return yield* encodeJsonObject(payload).pipe(Effect.mapError((error) => invalidData(error.message))); - }); - - const fork = Effect.fn("Session.fork")(function* (input: ForkInput) { - const mode = input.mode ?? "at"; - const newSessionId = input.id ?? SessionSchema.ID.create(); - - const result: ForkTxResult = yield* sql - .withTransaction( - Effect.gen(function* () { - const source = yield* findSession(input.sessionId); - if (Option.isNone(source)) return { _tag: "sessionNotFound" } as const; - - // Resolve the fork-point entry: explicit (validated in-session) - // or the current leaf (clone). None = empty source. - let targetEntry: Option.Option; - if (input.entryId !== undefined) { - const found = yield* findEntry(input.entryId); - if (Option.isNone(found) || found.value.sessionId !== input.sessionId) { - return { _tag: "entryNotFound", entryId: input.entryId } as const; - } - targetEntry = found; - } else { - const leaf = yield* resolveLeaf(source.value); - targetEntry = Option.isNone(leaf) ? Option.none() : yield* findEntry(leaf.value); - } - - // mode "before": land just above a user prompt so the caller can - // re-edit and resubmit it in the fork. Parent may be none (root prompt) — that yields an empty fork. - let targetId: Option.Option; - if (Option.isSome(targetEntry) && mode === "before") { - if (targetEntry.value.type !== "user") { - return { - _tag: "invalidMode", - entryId: targetEntry.value.id, - type: targetEntry.value.type, - } as const; - } - targetId = targetEntry.value.parentId; - } else { - targetId = Option.map(targetEntry, (entry) => entry.id); - } - - const pathEntries = Option.isSome(targetId) ? yield* selectPath(targetId.value) : []; - const idMap = new Map(pathEntries.map((entry) => [entry.id, uuidv7()])); - - // Prepare the complete copy before the first write. Returning a tagged - // validation error after an insert would otherwise commit a partial fork. - const prepared = yield* Effect.result( - Effect.forEach(pathEntries, (entry) => { - const id = idMap.get(entry.id)!; - return rewriteForkedData(entry, id, idMap).pipe( - Effect.map((data) => ({ source: entry, id, data })), - ); - }), - ); - if (Result.isFailure(prepared)) { - return { - _tag: "invalidData", - entryId: prepared.failure.entryId, - type: prepared.failure.type, - reason: prepared.failure.reason, - } as const; - } - const preparedEntries = prepared.success; - - // New session first (prepared entries FK it); leaf set after the copy. - // Aggregates start at 0 — spend stays recorded in the source. - const sessionRow = yield* SessionRow.insert.makeEffect({ - id: newSessionId, - // Same space and directory as the source, so the same env and cwd. - spaceId: source.value.spaceId, - parentId: Option.some(source.value.id), // fork lineage - slug: yield* Slug.create(newSessionId), - directory: source.value.directory, - // A fork continues the same work, so it reads the same project. - hostDir: source.value.hostDir, - // And runs the same model, until the fork chooses another. - config: source.value.config, - // Nothing runs in a fork yet, whatever the source was doing. - execution: Option.none(), - title: input.title ?? source.value.title, - tag: input.tag === undefined ? source.value.tag : Option.some(input.tag), - metadata: source.value.metadata, - leafEntryId: Option.none(), - }); - yield* insertSession(sessionRow); - - // Positions are carried over verbatim, not renumbered: they are log - // positions, and preserving them keeps the copy ordered exactly as - // the source. The new aggregate is then seeded above the highest of - // them so post-fork events never land underneath. - for (const prepared of preparedEntries) { - const entryRow = yield* SessionEntryRow.insert.makeEffect({ - id: prepared.id, - sessionId: newSessionId, - parentId: Option.map(prepared.source.parentId, (parent) => idMap.get(parent)!), - seq: prepared.source.seq, - type: prepared.source.type, - state: prepared.source.state, - data: prepared.data, - label: prepared.source.label, // annotations ride the copy (§10.12) - metadata: prepared.source.metadata, - }); - // Path order means source positions already ascend, so a - // rejection here would mean the source tree violated its own - // ordering invariant rather than anything about this copy. - const copied = yield* insertEntry(entryRow); - if (copied.length === 0) { - return yield* Effect.die( - `fork: copied entry ${prepared.id} at seq ${prepared.source.seq} does not advance`, - ); - } - } - - // Parts: new ids, re-keyed entry/session, everything else verbatim — - // including pending toolCalls (the fork inherits crash recovery). - const messageEntryIds = pathEntries - .filter((entry) => messageTypes.has(entry.type)) - .map((entry) => entry.id); - if (messageEntryIds.length > 0) { - const parts = yield* selectPartsForEntries(messageEntryIds); - for (const part of parts) { - const partRow = yield* SessionEntryPartRow.insert.makeEffect({ - id: uuidv7(), - entryId: idMap.get(part.entryId)!, - sessionId: newSessionId, - partIndex: part.partIndex, - type: part.type, - status: part.status, - callId: part.callId, - toolName: part.toolName, - data: part.data, - }); - yield* insertPart(partRow); - } - } - - if (Option.isSome(targetId)) { - const now = yield* epochNow; - yield* sql` - UPDATE session SET leaf_entry_id = ${idMap.get(targetId.value)!}, updated_at = ${now} - WHERE id = ${newSessionId} - `; - } - - // The copied entries hold positions from the source's log, so the - // new aggregate starts above the highest of them. Without this its - // first event would land at 0, underneath the copy, and - // `parent.seq < child.seq` — which `selectPath` orders by — would - // invert. An empty fork copies nothing and starts at 0 normally. - if (preparedEntries.length > 0 && Option.isSome(targetId)) { - const baseSeq = preparedEntries.reduce( - (highest, prepared) => Math.max(highest, prepared.source.seq), - 0, - ); - yield* events.advance(newSessionId, baseSeq); - // Lands at baseSeq + 1, so the seeded range is recorded by the - // log itself rather than inferable only from `parentId`. - yield* events.publish(EventList.SessionForked, { - sessionId: newSessionId, - sourceSessionId: input.sessionId, - sourceEntryId: targetId.value, - baseSeq, - timestamp: yield* DateTime.now, - }); - } - - const created = yield* findSession(newSessionId); - if (Option.isNone(created)) { - return yield* Effect.die(new Error("session fork: created session did not persist")); - } - return { _tag: "forked", session: created.value } as const; - }), - ) - .pipe(Effect.orDie); - - switch (result._tag) { - case "sessionNotFound": - return yield* new SessionNotFoundError({ sessionId: input.sessionId }); - case "entryNotFound": - return yield* new EntryNotFoundError({ sessionId: input.sessionId, entryId: result.entryId }); - case "invalidMode": - return yield* new InvalidEntryDataError({ - entryId: result.entryId, - type: result.type, - reason: `fork mode "before" requires a user entry`, - }); - case "invalidData": - return yield* new InvalidEntryDataError({ - entryId: result.entryId, - type: result.type, - reason: result.reason, - }); - case "forked": - return result.session; - } - }); - const link = Effect.fn("Session.link")(function* (input: { sessionId: SessionSchema.ID; hostDir: AbsolutePath | null; @@ -1209,189 +368,75 @@ export const layer = Layer.effect( return updated.value; }); - const retitle = Effect.fn("Session.retitle")(function* (sessionId: SessionSchema.ID, title: string) { - const changed = yield* sql<{ readonly id: string }>` - UPDATE session SET title = ${title} - WHERE id = ${sessionId} AND title = ${SessionSchema.DEFAULT_TITLE} - RETURNING id - `.pipe(Effect.orDie); - return changed.length > 0; - }); - - const encodeConfig = Schema.encodeEffect(Schema.fromJsonString(SessionSchema.Config)); - const configure = Effect.fn("Session.configure")(function* ( - sessionId: SessionSchema.ID, - config: SessionSchema.Config, - ) { - const patch = yield* encodeConfig(config); - yield* sql` - UPDATE session SET config = json_patch(COALESCE(config, '{}'), ${patch}) - WHERE id = ${sessionId} - `; - }, Effect.orDie); - - const markRunning = Effect.fn("Session.markRunning")(function* (sessionId: SessionSchema.ID) { - yield* sql`UPDATE session SET execution = 'running' WHERE id = ${sessionId}`; - }, Effect.orDie); - - const endExecution = Effect.fn("Session.endExecution")(function* ( - sessionId: SessionSchema.ID, - end: "finished" | "stopped" | "closed", - stoppedAt?: number, - ) { - if (end === "closed") { - yield* sql`UPDATE session SET execution = 'closed' WHERE id = ${sessionId}`; - return; - } - // Pending prompts that keep the mark: any for a finished run; for a stop, only those sent - // after it, since what was queued before it stays parked by the user's choice. - const after = end === "stopped" ? (stoppedAt ?? Number.MAX_SAFE_INTEGER) : -1; - yield* sql` - UPDATE session SET execution = NULL, restarts = 0 - WHERE id = ${sessionId} AND NOT EXISTS ( - SELECT 1 FROM session_input - WHERE session_id = ${sessionId} AND promoted_seq IS NULL AND cancelled_seq IS NULL - AND admitted_seq > ${after} - ) - `; - }, Effect.orDie); - - const claimRestart = Effect.fn("Session.claimRestart")(function* (sessionId: SessionSchema.ID) { - const rows = yield* sql<{ restarts: number }>` - UPDATE session - SET restarts = restarts + (CASE WHEN execution = 'running' THEN 1 ELSE 0 END), execution = 'running' - WHERE id = ${sessionId} AND execution IS NOT NULL - RETURNING restarts - `; - return Option.fromUndefinedOr(rows[0]?.restarts); - }, Effect.orDie); - - const resetRestarts = Effect.fn("Session.resetRestarts")(function* (sessionId: SessionSchema.ID) { - yield* sql`UPDATE session SET restarts = 0 WHERE id = ${sessionId} AND restarts > 0`; - }, Effect.orDie); - - const findUnfinished = SqlSchema.findAll({ - Request: Schema.Void, - Result: SessionRow, - execute: () => sql`SELECT * FROM session WHERE execution IS NOT NULL ORDER BY updated_at`, - }); - const unfinished = findUnfinished(undefined).pipe(Effect.orDie, Effect.withSpan("Session.unfinished")); - const relink = Effect.fn("Session.relink")(function* (input: RelinkInput) { const reject = (reason: RelinkReason) => new RelinkError({ sessionId: input.sessionId, spaceId: input.spaceId, reason }); const session = yield* findSession(input.sessionId).pipe(Effect.orDie); if (Option.isNone(session)) return yield* new SessionNotFoundError({ sessionId: input.sessionId }); + if (input.threadId !== undefined) { + const thread = yield* threads.get(input.threadId); + if (Option.isNone(thread) || thread.value.sessionId !== input.sessionId) { + return yield* new Thread.ThreadNotFoundError({ threadId: input.threadId }); + } + } const target = yield* findSpaceById(input.spaceId).pipe(Effect.orDie); if (Option.isNone(target)) return yield* reject("space_not_found"); if (target.value.status !== "active") return yield* reject("space_is_archived"); - // Same-project guard. A missing current space means the FK invariant is - // already broken; there is nothing to compare, so the move is allowed. - const current = yield* findSpace(input.sessionId).pipe(Effect.orDie); - if (Option.isSome(current) && current.value.projectId !== target.value.projectId) { + // Same-project guard: the session's own space names the project every placement stays in. + const own = yield* findSpace(input.sessionId).pipe(Effect.orDie); + if (Option.isSome(own) && own.value.projectId !== target.value.projectId) { return yield* reject("project_scope_mismatch"); } + const current = + input.threadId === undefined + ? Option.map(own, (row) => ({ location: row.location, directory: session.value.directory })) + : Option.map(yield* placement(input.threadId), (one) => ({ + location: one.space.location, + directory: one.directory, + })); const directory = input.directory ?? (Option.isSome(current) - ? rebaseDirectory(current.value.location, target.value.location, session.value.directory) + ? rebaseDirectory(current.value.location, target.value.location, current.value.directory) : target.value.location); if (!directoryWithin(target.value.location, directory)) return yield* reject("directory_outside_space"); - const now = yield* epochNow; - yield* sql` - UPDATE session SET space_id = ${input.spaceId}, directory = ${directory}, updated_at = ${now} - WHERE id = ${input.sessionId} - `.pipe(Effect.orDie); + if (input.threadId === undefined) { + const now = yield* epochNow; + yield* sql` + UPDATE session SET space_id = ${input.spaceId}, directory = ${directory}, updated_at = ${now} + WHERE id = ${input.sessionId} + `.pipe(Effect.orDie); + } else { + yield* events.publish(EventList.ThreadConfigured, { + timestamp: yield* DateTime.now, + sessionId: input.sessionId, + threadId: input.threadId, + change: { placement: { spaceId: input.spaceId, directory } }, + }); + } const updated = yield* findSession(input.sessionId).pipe(Effect.orDie); if (Option.isNone(updated)) return yield* Effect.die(new Error("session relink did not persist")); return updated.value; }); - const branch = Effect.fn("Session.branch")(function* (input: { sessionId: string; entryId: string }) { - const moved = yield* sql + // The log has no foreign key to the session, so both go in one transaction. + const remove = Effect.fn("Session.delete")(function* (sessionId: SessionSchema.ID) { + return yield* sql .withTransaction( Effect.gen(function* () { - const rows = yield* sql` - SELECT id FROM session_entry WHERE id = ${input.entryId} AND session_id = ${input.sessionId} - `; - if (rows.length === 0) return false; - const now = yield* epochNow; - yield* sql` - UPDATE session SET leaf_entry_id = ${input.entryId}, updated_at = ${now} - WHERE id = ${input.sessionId} - `; - return true; + const deleted = yield* sql`DELETE FROM session WHERE id = ${sessionId} RETURNING id`; + yield* sql`DELETE FROM sequence WHERE aggregate_id = ${sessionId}`; + return deleted.length > 0; }), ) .pipe(Effect.orDie); - - if (!moved) { - return yield* new EntryNotFoundError({ sessionId: input.sessionId, entryId: input.entryId }); - } }); - const setLabel = Effect.fn("Session.setLabel")(function* (input: { - sessionId: string; - entryId: string; - label: string | null; - }) { - const labeled = yield* sql - .withTransaction( - Effect.gen(function* () { - const rows = yield* sql` - SELECT id FROM session_entry WHERE id = ${input.entryId} AND session_id = ${input.sessionId} - `; - if (rows.length === 0) return false; - const now = yield* epochNow; - yield* sql` - UPDATE session_entry SET label = ${input.label}, updated_at = ${now} - WHERE id = ${input.entryId} - `; - return true; - }), - ) - .pipe(Effect.orDie); - - if (!labeled) { - return yield* new EntryNotFoundError({ sessionId: input.sessionId, entryId: input.entryId }); - } - }); - - return Service.of({ - create, - link, - retitle, - configure, - markRunning, - endExecution, - claimRestart, - resetRestarts, - unfinished, - get, - space, - project, - list, - entry, - path, - timeline, - unsettled, - append, - replaceAssistant, - settleToolCall, - setEntryState, - abortAssistant, - fork, - relink, - branch, - setLabel, - }); + return Service.of({ create, projectCreated, link, get, space, placement, project, list, relink, delete: remove }); }), ); -// Sessions foreign-key to sandbox_instance, so the host row has to exist before -// a session can record which namespace it runs in. -// Plain database: the host has no row and the namespace foreign key is skipped -// on NULL, so a session needs no sandbox setup to exist. -export const defaultLayer = layer.pipe(Layer.provide(Database.defaultLayer)); +/** The session store with the thread and document stores it is built on. */ +export const storeLayer = layer.pipe(Layer.provideMerge(Thread.layer), Layer.provideMerge(Document.layer)); export * as Session from "./session.ts"; diff --git a/packages/harness/src/state/state.ts b/packages/harness/src/state/state.ts index 7e988730..4ada18e0 100644 --- a/packages/harness/src/state/state.ts +++ b/packages/harness/src/state/state.ts @@ -25,9 +25,12 @@ import type { Plugin } from "../plugin/plugin.ts"; import { LLM } from "../runner/llm.ts"; import type { Runner } from "../runner/run.ts"; import { SandboxIO } from "../sandbox/io.ts"; +import { Document } from "../document/document.ts"; import { SessionRuntime } from "../session/runtime.ts"; import type { ID as SessionId } from "../session/schema.ts"; import { Session as SessionStore } from "../session/session.ts"; +import type { ThreadSchema } from "../thread/schema.ts"; +import { Thread } from "../thread/thread.ts"; import { Global } from "../global.ts"; import { merge } from "../settings/merge.ts"; import { compose, resolveOptions } from "../settings/resolve.ts"; @@ -53,7 +56,7 @@ export type ToolExecutionMode = "sequential" | "parallel"; * * - `signal` -- `LLM.run` binds it to the scope that aborts the transport on * interruption. A caller value would silently break cancellation. - * - `sessionId` -- Loop owns it. + * - `sessionId` -- Loop owns it: the thread's id. * - `reasoning` -- this *is* the thinking level. Exposing it alongside * `thinkingLevel` would be two spellings of one value, free to drift. * - `modelId` -- duplicates {@link Options.model}. @@ -87,7 +90,7 @@ export interface Options extends RequestOptions { * fails before any provider request goes out. */ export class SnapshotError extends Schema.TaggedError()("State.SnapshotError", { - sessionId: Schema.String, + threadId: Schema.String, reason: Schema.String, cause: Schema.Defect(), }) { @@ -109,6 +112,7 @@ export class SnapshotError extends Schema.TaggedError()("State.Sn */ export interface Snapshot { readonly sessionId: SessionId; + readonly threadId: ThreadSchema.ID; readonly sandbox: SandboxIO.Identity; readonly location: Location.Info; readonly systemPrompt: string; @@ -160,22 +164,22 @@ export interface Interface { */ readonly reload: Effect.Effect; /** - * The model and thinking level a session's next turn runs with: its runtime bindings over - * its chosen `SessionSchema.Config` over the caller's options over settings, resolved the - * way {@link snapshot} resolves them. The level is the one the model will actually run at. + * The model and thinking level a thread's next turn runs with: its session's runtime bindings + * over its `agent` document over the caller's options over settings, resolved the way + * {@link snapshot} resolves them. The level is the one the model will actually run at. */ readonly configuration: ( - sessionId: SessionId, + threadId: ThreadSchema.ID, ) => Effect.Effect< Configuration, Settings.SettingsError | Runner.ModelCatalogError | Runner.ModelNotFoundError | Runner.ProviderError >; /** - * Capture runtime state for one turn. Called inside a session drain, + * Capture runtime state for one request. Called inside a thread drain, * where the mount it reads is already open. */ readonly snapshot: ( - sessionId: SessionId, + threadId: ThreadSchema.ID, ) => Effect.Effect< Snapshot, | SnapshotError @@ -246,6 +250,8 @@ export const layer = ( Effect.gen(function* () { const runtime = yield* SessionRuntime.Service; const sessions = yield* SessionStore.Service; + const threads = yield* Thread.Service; + const documents = yield* Document.Service; const settings = yield* Settings.Service; const eventRegistry = yield* EventRegistry.Service; const eventService = yield* Event.Service; @@ -324,24 +330,32 @@ export const layer = ( for (const [id, origin] of (yield* Ref.get(pool)).origins) { yield* updated(origin, id, "loaded"); } - const publishFailed = (cause: { readonly message: string }, sessionId?: SessionId) => + const publishFailed = ( + cause: { readonly message: string }, + owner?: { readonly sessionId: SessionId; readonly threadId: ThreadSchema.ID }, + ) => eventService.publish(EventList.PluginUpdated, { status: "failed", error: cause.message, ...("reference" in cause && typeof cause.reference === "string" ? { reference: cause.reference } : {}), - ...(sessionId === undefined ? {} : { sessionId }), + ...owner, }); - /** Settings and composed configuration for a session's next turn. */ - const configure = Effect.fnUntraced(function* (sessionId: SessionId) { - // Read per turn, not captured at creation, so `Session.link` takes effect at + /** Settings and composed configuration for a thread's next request. */ + const configure = Effect.fnUntraced(function* (threadId: ThreadSchema.ID) { + const thread = yield* threads.get(threadId); + // A thread is only drained or configured once it exists; its session is gone with it. + if (Option.isNone(thread)) return yield* Effect.die(`thread ${threadId} not found`); + const sessionId: SessionId = thread.value.sessionId; + // Read per request, not captured at creation, so `Session.link` takes effect at // the next one. A session with none discovers no project layer: there is no // fallback to the process's directory, which would hand it a stranger's project // (it is a long-running server; one process serves sessions in many projects, or // in none). const session = Option.getOrUndefined(yield* sessions.get(sessionId)); const hostDir = session === undefined ? undefined : Option.getOrUndefined(session.hostDir); - // The chosen config survives restarts; runtime bindings, set in this process, sit over it. - const chosen = session === undefined ? {} : Option.getOrElse(session.config, () => ({})); + // The `agent` document survives restarts and is read once per request; runtime bindings, + // set in this process, sit over it. + const chosen = yield* documents.agent(threadId); const sessionOptions = { ...(chosen.model === undefined ? {} : { provider: chosen.model.provider, model: chosen.model.id }), ...(chosen.thinkingLevel === undefined ? {} : { thinkingLevel: chosen.thinkingLevel }), @@ -360,7 +374,12 @@ export const layer = ( // The chosen level stays as written. What runs, is stamped on the message and is shown is // the nearest level this model supports, re-fitted whenever the model changes. const thinkingLevel = Model.clampThinkingLevel(resolvedModel, composed.thinkingLevel); - return { hostDir, loadedSettings, configured: { ...composed, resolvedModel, thinkingLevel } }; + return { + sessionId, + hostDir, + loadedSettings, + configured: { ...composed, resolvedModel, thinkingLevel }, + }; }); return Service.of({ reload: Effect.gen(function* () { @@ -386,16 +405,17 @@ export const layer = ( const [first] = failures.keys(); return first === undefined ? { plugins } : { plugins, failure: first }; }), - configuration: Effect.fn("State.configuration")(function* (sessionId: SessionId) { - const { configured } = yield* configure(sessionId); + configuration: Effect.fn("State.configuration")(function* (threadId: ThreadSchema.ID) { + const { configured } = yield* configure(threadId); return { provider: configured.provider, model: configured.model, thinkingLevel: configured.thinkingLevel, } satisfies Configuration; }), - snapshot: Effect.fn("State.snapshot")(function* (sessionId: SessionId) { - const { hostDir, loadedSettings, configured } = yield* configure(sessionId); + snapshot: Effect.fn("State.snapshot")(function* (threadId: ThreadSchema.ID) { + const { sessionId, hostDir, loadedSettings, configured } = yield* configure(threadId); + const owner = { sessionId, threadId }; const { promptCustom, promptSystemAppend, @@ -412,7 +432,7 @@ export const layer = ( const refs = references(loadedSettings); const failed = (cause: { readonly message: string }) => - new SnapshotError({ sessionId, reason: cause.message, cause }); + new SnapshotError({ threadId, reason: cause.message, cause }); // Follow the store: load an entry whose bytes are already here, and move to a // newer generation of one that is. Never a fetch -- `plugin install` stays the @@ -424,7 +444,7 @@ export const layer = ( const target = yield* poolOf(root); const current = yield* Ref.get(target); const moved = yield* follow(refs, current, loadedSettings, root).pipe( - Effect.tapError((cause) => publishFailed(cause, sessionId)), + Effect.tapError((cause) => publishFailed(cause, owner)), ); if (Option.isNone(moved)) return current; return yield* activate(target, moved.value); @@ -453,12 +473,13 @@ export const layer = ( reference, error, ...(where === undefined ? {} : { file: where.file }), - sessionId, + ...owner, }); } const contributions = yield* setup(chosen.selection, { sessionId, + threadId, sandbox, paths: global, location, @@ -469,10 +490,11 @@ export const layer = ( ...(promptCustom === undefined ? {} : { promptCustom: resolver(promptCustom) }), ...(promptSystemAppend === undefined ? {} : { promptSystemAppend: resolver(promptSystemAppend) }), }, - }).pipe(Effect.mapError((cause) => new SnapshotError({ sessionId, reason: cause.message, cause }))); + }).pipe(Effect.mapError((cause) => new SnapshotError({ threadId, reason: cause.message, cause }))); return { sessionId, + threadId, sandbox, location, ...contributions, diff --git a/packages/harness/src/thread/schema.ts b/packages/harness/src/thread/schema.ts new file mode 100644 index 00000000..c2b9c461 --- /dev/null +++ b/packages/harness/src/thread/schema.ts @@ -0,0 +1,53 @@ +import { Schema } from "effect"; + +// Thread identity lives in `@codeworksh/plugin`, next to the session's: it is on every plugin +// context and every tool call. +import type { ThreadID } from "@codeworksh/plugin/ids"; +export { ThreadID as ID } from "@codeworksh/plugin/ids"; + +export const IDFromDb = Schema.String.pipe(Schema.brand("Thread.ID")); + +/** How a thread came to exist; stored flattened as the `origin_*` columns. */ +export const OriginKind = Schema.Literals(["main", "fork", "subagent"]); +export type OriginKind = typeof OriginKind.Type; + +export type Origin = + /** The session's default thread. Exactly one per session. */ + | { readonly kind: "main" } + /** History continues from `threadId` through `entryId`. */ + | { readonly kind: "fork"; readonly threadId: ThreadID; readonly entryId: string } + /** + * Spawned by tool call `callId` in `threadId`, which waits on it. `entryId` set = inherits that + * thread's history through it; absent = starts empty. + */ + | { readonly kind: "subagent"; readonly threadId: ThreadID; readonly callId: string; readonly entryId?: string }; + +/** + * A run left unfinished: `running` while one is in flight, and still after the process died during + * it; `closed` once a shutdown stopped it. Either resumes when the server starts again; a run that + * ended any other way leaves none. + */ +export const Execution = Schema.Literals(["running", "closed"]); +export type Execution = typeof Execution.Type; + +/** `main` names a session's main thread wherever a thread id is accepted from a person. */ +export const MAIN = "main"; + +export const TITLE_MAX_LENGTH = 48; + +/** + * A title from a prompt: its first non-empty line, whitespace collapsed, cut on a word boundary + * to {@link TITLE_MAX_LENGTH} characters including the ellipsis. `undefined` for a blank prompt. + */ +export const titleFrom = (text: string): string | undefined => { + const line = text + .split("\n") + .map((one) => one.replace(/\s+/g, " ").trim()) + .find((one) => one.length > 0); + if (line === undefined || line.length <= TITLE_MAX_LENGTH) return line; + const cut = line.slice(0, TITLE_MAX_LENGTH - 1); + const space = cut.lastIndexOf(" "); + return `${(space > 0 ? cut.slice(0, space) : cut).trimEnd()}…`; +}; + +export * as ThreadSchema from "./schema.ts"; diff --git a/packages/harness/src/thread/thread.ts b/packages/harness/src/thread/thread.ts new file mode 100644 index 00000000..71336ea3 --- /dev/null +++ b/packages/harness/src/thread/thread.ts @@ -0,0 +1,864 @@ +import { Context, DateTime, Effect, Layer, Option, Schema } from "effect"; +import { SqlClient, SqlSchema } from "effect/sql"; +import { uuidv7 } from "uuidv7"; +import { + EntryPartRow, + EntryRow, + type EntryState, + type EntryType, + messageEntryTypes, + type PartType, + ThreadRow, + type ToolStatus, +} from "../db/schema.sql.ts"; +import { Document } from "../document/document.ts"; +import { DocumentSchema } from "../document/schema.ts"; +import { Event } from "../event/event.ts"; +import { EventList } from "../event/list.ts"; +import { SessionSchema } from "../session/schema.ts"; +import { ThreadSchema } from "./schema.ts"; + +export class ThreadNotFoundError extends Schema.TaggedError()("ThreadNotFoundError", { + threadId: Schema.String, +}) { + override get message(): string { + return `thread ${this.threadId} not found`; + } +} + +export class EntryNotFoundError extends Schema.TaggedError()("EntryNotFoundError", { + threadId: Schema.String, + entryId: Schema.String, +}) {} + +// Structural rejection — the data-structure layer's "index out of bounds". +export class InvalidEntryDataError extends Schema.TaggedError()("InvalidEntryDataError", { + entryId: Schema.String, + type: Schema.String, + reason: Schema.String, +}) {} + +/** A fork point the thread's line cannot be cut at. */ +export class ForkError extends Schema.TaggedError()("ForkError", { + threadId: Schema.String, + entryId: Schema.String, + reason: Schema.Literals([ + // not in the thread's line + "entry_not_found", + // a draft is still changing, an aborted entry is not part of the context + "entry_not_committed", + // `before` refills the editor, so it needs a user entry + "not_a_user_entry", + // `before` the first entry of main: an empty start is a new session or a reset + "nothing_before_entry", + ]), +}) { + override get message(): string { + return `cannot fork ${this.threadId} at ${this.entryId}: ${this.reason.replaceAll("_", " ")}`; + } +} + +export interface AppendPart { + readonly id?: string; // uuidv7; generated when omitted + readonly type: PartType; + readonly status?: ToolStatus; // toolCall parts only + readonly callId?: string; // toolCall parts only + readonly toolName?: string; // toolCall parts only + readonly data: string; // verbatim aikit part JSON +} + +export interface AppendEntry { + readonly id: string; // uuidv7; == aikit messageId for message types + readonly threadId: ThreadSchema.ID; + /** The sequence of the event that produced this entry. Must exceed every own entry's seq. */ + readonly seq: number; + readonly type: EntryType; + /** Defaults to committed. Only LLMStarted inserts a draft. */ + readonly state?: EntryState; + readonly data: string; // JSON: full payload, or message envelope + readonly parts?: ReadonlyArray; // order = partIndex; message types only + /** + * Publish-time context from the event that produced this entry. The log does + * not store event metadata, so if it is to survive at all it survives here. + */ + readonly metadata?: Readonly>; +} + +export interface SettleToolCall { + readonly entryId: string; // owning assistant entry + readonly callId: string; + readonly status: Exclude; + readonly data: string; // settled aikit part JSON, verbatim +} + +export interface ReplaceAssistant { + readonly id: string; + readonly threadId: ThreadSchema.ID; + readonly data: string; + readonly parts: ReadonlyArray; +} + +/** Ends a draft assistant: committed once every tool part is settled, or aborted (parts dropped). */ +export interface EndAssistant { + readonly id: string; + readonly threadId: ThreadSchema.ID; + /** The event that ends it; becomes its `committedSeq`. */ + readonly seq: number; +} + +export interface ForkInput { + readonly threadId: ThreadSchema.ID; + readonly entryId: string; + /** `at` keeps the entry; `before` stops at its predecessor (user entries only). Default `at`. */ + readonly mode?: "at" | "before"; + readonly id?: ThreadSchema.ID; +} + +export interface Forked { + readonly thread: ThreadRow; + /** For `before`: the text of the user entry left out, to refill the editor with. */ + readonly text?: string; +} + +/** What a projector inserts for a new thread; usage and execution start empty. */ +export interface InsertThread { + readonly id: ThreadSchema.ID; + readonly sessionId: SessionSchema.ID; + readonly origin: ThreadSchema.Origin; + readonly title?: string; + readonly seq: number; +} + +// Entry row + its part rows, zipped. Message decode happens above this layer. +export interface HydratedEntry { + readonly entry: EntryRow; + readonly parts: ReadonlyArray; +} + +export interface Interface { + readonly get: (threadId: string) => Effect.Effect>; + /** The session's main thread; none when the session is unknown. */ + readonly main: (sessionId: string) => Effect.Effect>; + /** Threads newest first: of one session, or of the whole database. */ + readonly list: (sessionId?: SessionSchema.ID) => Effect.Effect>; + readonly entry: (entryId: string) => Effect.Effect>; + /** + * The thread's line: its own entries, then its source's through the fork entry, recursively, in + * seq order. Every state is included; context assembly drops what is not committed. + */ + readonly line: (threadId: ThreadSchema.ID) => Effect.Effect>; + /** The thread's open draft, if a run left one. */ + readonly draft: (threadId: ThreadSchema.ID) => Effect.Effect>; + /** Unsettled toolCall parts of the thread's own entries (crash recovery / live status). */ + readonly unsettled: (threadId: ThreadSchema.ID) => Effect.Effect>; + /** Projector-only: a new thread row. */ + readonly insert: (input: InsertThread) => Effect.Effect; + readonly append: ( + input: AppendEntry, + ) => Effect.Effect; + /** Projector-only: replace the payload of an existing draft assistant in place. */ + readonly replaceAssistant: (input: ReplaceAssistant) => Effect.Effect; + /** Projector-only: settle one pending/running tool part on a draft assistant. */ + readonly settleToolCall: (input: SettleToolCall) => Effect.Effect; + /** Projector-only: commit a draft after every tool part is terminal. */ + readonly commit: (input: EndAssistant) => Effect.Effect; + /** Projector-only: turn a draft into a partless aborted tombstone. */ + readonly abort: (input: EndAssistant & { readonly data: string }) => Effect.Effect; + /** Cut a new thread off `threadId`'s line at a committed entry. */ + readonly fork: (input: ForkInput) => Effect.Effect; + /** Projector of `thread.forked`: the thread and its copied documents, at the event's seq. */ + readonly projectForked: (input: EventList.ThreadForked["data"], seq: number) => Effect.Effect; + /** Names an untitled thread. Returns whether the title changed. */ + readonly retitle: (threadId: ThreadSchema.ID, title: string) => Effect.Effect; + /** Records that the thread has a run to do: one starting, or one a prompt just woke. */ + readonly markRunning: (threadId: ThreadSchema.ID) => Effect.Effect; + /** + * Records how a run ended. `closed` (a shutdown) leaves it to resume. `stopped` (the user) and + * `finished` clear it and the restart count, except that a finished run with prompts still + * pending stays marked: they are accepted work a successor, or the next server start, runs. + */ + readonly endExecution: ( + threadId: ThreadSchema.ID, + end: "finished" | "stopped" | "closed", + /** For a stop: the log position it was asked at. A prompt admitted after it keeps the mark. */ + stoppedAt?: number, + ) => Effect.Effect; + /** + * Claims a thread's unfinished run for recovery: marks it running again, counting a restart when + * a dying process left it running (a shutdown's closed run is not counted). The new count, or none + * when nothing is unfinished any more, say because the user stopped it since. + */ + readonly claimRestart: (threadId: ThreadSchema.ID) => Effect.Effect>; + /** A committed turn is progress: the restart count starts over. Runs inside `TurnEnded`'s commit. */ + readonly resetRestarts: (threadId: ThreadSchema.ID) => Effect.Effect; + /** Root threads with a run left unfinished, oldest first. */ + readonly unfinished: Effect.Effect>; + /** Set or clear (null) the bookmark label on an entry of the thread. */ + readonly setLabel: (input: { + threadId: ThreadSchema.ID; + entryId: string; + label: string | null; + }) => Effect.Effect; +} + +export class Service extends Context.Service()("@codeworksh/harness/thread/thread/Service") {} + +const messageTypes: ReadonlySet = new Set(messageEntryTypes); + +// Structural decode of the persisted envelope's usage; failures surface as +// InvalidEntryDataError, never a defect — callers (Context Manager, UI) can act. +const decodeEnvelopeUsage = Schema.decodeUnknownEffect(SessionSchema.AssistantEnvelopeUsage); +const decodeMessageEnvelopeIdentity = Schema.decodeUnknownEffect(SessionSchema.MessageEnvelopeIdentity); +const decodeCompactionData = Schema.decodeUnknownEffect(SessionSchema.CompactionData); +const decodeJsonObject = Schema.decodeUnknownEffect(SessionSchema.JsonObject); +const decodeUserText = Schema.decodeUnknownOption( + Schema.fromJsonString(Schema.Struct({ type: Schema.Literal("text"), text: Schema.String })), +); + +export const layer = Layer.effect( + Service, + Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + const events = yield* Event.Service; + const documents = yield* Document.Service; + + const findThread = SqlSchema.findOneOption({ + Request: Schema.String, + Result: ThreadRow, + execute: (id) => sql`SELECT * FROM thread WHERE id = ${id}`, + }); + + const findMain = SqlSchema.findOneOption({ + Request: Schema.String, + Result: ThreadRow, + execute: (sessionId) => sql`SELECT * FROM thread WHERE session_id = ${sessionId} AND origin_kind = 'main'`, + }); + + const selectBySession = SqlSchema.findAll({ + Request: Schema.String, + Result: ThreadRow, + execute: (sessionId) => sql`SELECT * FROM thread WHERE session_id = ${sessionId} ORDER BY updated_at DESC`, + }); + + const selectAll = SqlSchema.findAll({ + Request: Schema.Void, + Result: ThreadRow, + execute: () => sql`SELECT * FROM thread ORDER BY updated_at DESC`, + }); + + const insertThread = SqlSchema.void({ + Request: ThreadRow.insert, + execute: (row) => sql`INSERT INTO thread ${sql.insert(row)}`, + }); + + const findEntry = SqlSchema.findOneOption({ + Request: Schema.String, + Result: EntryRow, + execute: (id) => sql`SELECT * FROM entry WHERE id = ${id}`, + }); + + // The WHERE NOT EXISTS keeps a thread's positions strictly advancing: seq comes from the + // event that produced the entry, and a stale one would sort the thread out of order. + // Inserting zero rows is the signal, checked by the callers. + const insertEntry = SqlSchema.findAll({ + Request: EntryRow.insert, + Result: Schema.Struct({ id: Schema.String }), + execute: (row) => sql` + INSERT INTO entry (id, session_id, thread_id, seq, committed_seq, type, state, data, label, metadata, created_at, updated_at) + SELECT + ${row.id}, ${row.sessionId}, ${row.threadId}, ${row.seq}, ${row.committedSeq ?? null}, + ${row.type}, ${row.state}, ${row.data}, ${row.label ?? null}, ${row.metadata ?? null}, + ${row.createdAt}, ${row.updatedAt} + WHERE NOT EXISTS ( + SELECT 1 FROM entry WHERE thread_id = ${row.threadId} AND seq >= ${row.seq} + ) + RETURNING id + `, + }); + + // Single-row insert: toolCall and non-tool parts have different encoded + // key sets (FieldOption omits absent keys), so multi-row sql.insert would + // mix shapes. + const insertPart = SqlSchema.void({ + Request: EntryPartRow.insert, + execute: (row) => sql`INSERT INTO part ${sql.insert(row)}`, + }); + + // Seq order is enough: everything a thread inherits was written before the thread existed, + // and a thread has one entry per seq. + const selectLine = SqlSchema.findAll({ + Request: Schema.String, + Result: EntryRow, + execute: (threadId) => sql` + WITH RECURSIVE line(thread_id, upto) AS ( + SELECT id, NULL FROM thread WHERE id = ${threadId} + UNION ALL + SELECT t.origin_thread_id, e.committed_seq + FROM line l + JOIN thread t ON t.id = l.thread_id + JOIN entry e ON e.id = t.origin_entry_id + ) + SELECT e.* + FROM line l + JOIN entry e ON e.thread_id = l.thread_id AND (l.upto IS NULL OR e.seq <= l.upto) + ORDER BY e.seq + `, + }); + + const selectDraft = SqlSchema.findOneOption({ + Request: Schema.String, + Result: EntryRow, + execute: (threadId) => sql`SELECT * FROM entry WHERE thread_id = ${threadId} AND state = 'draft'`, + }); + + const selectPartsForEntries = SqlSchema.findAll({ + Request: Schema.Array(Schema.String), + Result: EntryPartRow, + execute: (entryIds) => sql` + SELECT * FROM part + WHERE ${sql.in("entry_id", entryIds)} + ORDER BY entry_id, part_index + `, + }); + + const selectUnsettled = SqlSchema.findAll({ + Request: Schema.String, + Result: EntryPartRow, + execute: (threadId) => sql` + SELECT * FROM part + WHERE thread_id = ${threadId} AND status IN ('pending', 'running') + ORDER BY entry_id, part_index + `, + }); + + const epochNow = Effect.map(DateTime.now, DateTime.toEpochMillis); + + // entry.type says whether parts are expected; a mismatch is corruption, not an edge case. + const hydrate = (entries: ReadonlyArray, parts: ReadonlyArray): HydratedEntry[] => { + const byEntry = new Map(); + for (const part of parts) { + const bucket = byEntry.get(part.entryId); + if (bucket) bucket.push(part); + else byEntry.set(part.entryId, [part]); + } + return entries.map((entry) => ({ entry, parts: byEntry.get(entry.id) ?? [] })); + }; + + const partsFor = Effect.fnUntraced(function* (entries: ReadonlyArray) { + const ids = entries.filter((entry) => messageTypes.has(entry.type)).map((entry) => entry.id); + if (ids.length === 0) return []; + return yield* selectPartsForEntries(ids).pipe(Effect.orDie); + }); + + const get = Effect.fn("Thread.get")(function* (threadId: string) { + return yield* findThread(threadId).pipe(Effect.orDie); + }); + + const main = Effect.fn("Thread.main")(function* (sessionId: string) { + return yield* findMain(sessionId).pipe(Effect.orDie); + }); + + const list = Effect.fn("Thread.list")(function* (sessionId?: SessionSchema.ID) { + return yield* (sessionId === undefined ? selectAll() : selectBySession(sessionId)).pipe(Effect.orDie); + }); + + const entry = Effect.fn("Thread.entry")(function* (entryId: string) { + const found = yield* findEntry(entryId).pipe(Effect.orDie); + if (Option.isNone(found)) return Option.none(); + const parts = yield* partsFor([found.value]); + return Option.some(hydrate([found.value], parts)[0]!); + }); + + const line = Effect.fn("Thread.line")(function* (threadId: ThreadSchema.ID) { + const entries = yield* selectLine(threadId).pipe(Effect.orDie); + return hydrate(entries, yield* partsFor(entries)); + }); + + const draft = Effect.fn("Thread.draft")(function* (threadId: ThreadSchema.ID) { + const found = yield* selectDraft(threadId).pipe(Effect.orDie); + if (Option.isNone(found)) return Option.none(); + return Option.some(hydrate([found.value], yield* partsFor([found.value]))[0]!); + }); + + const unsettled = Effect.fn("Thread.unsettled")(function* (threadId: ThreadSchema.ID) { + return yield* selectUnsettled(threadId).pipe(Effect.orDie); + }); + + const insert = Effect.fn("Thread.insert")(function* (input: InsertThread) { + const origin = input.origin; + const row = yield* ThreadRow.insert.makeEffect({ + id: input.id, + sessionId: input.sessionId, + originKind: origin.kind, + originThreadId: origin.kind === "main" ? Option.none() : Option.some(origin.threadId), + originEntryId: origin.kind === "main" ? Option.none() : Option.fromUndefinedOr(origin.entryId), + originCallId: origin.kind === "subagent" ? Option.some(origin.callId) : Option.none(), + title: Option.fromUndefinedOr(input.title), + execution: Option.none(), + createdSeq: input.seq, + }); + yield* insertThread(row); + }, Effect.orDie); + + const writeParts = Effect.fnUntraced(function* ( + entryId: string, + threadId: ThreadSchema.ID, + parts: ReadonlyArray, + ) { + for (const [partIndex, part] of parts.entries()) { + const partRow = yield* EntryPartRow.insert.makeEffect({ + id: part.id ?? uuidv7(), + entryId, + threadId, + partIndex, + type: part.type, + status: Option.fromUndefinedOr(part.status), + callId: Option.fromUndefinedOr(part.callId), + toolName: Option.fromUndefinedOr(part.toolName), + data: part.data, + }); + yield* insertPart(partRow); + } + }); + + // Usage lives on the thread and moves with the envelope stored; the session's last activity + // moves with it. + const touch = Effect.fnUntraced(function* ( + thread: ThreadRow, + delta?: { cost: number; input: number; output: number; cacheRead: number; cacheWrite: number }, + ) { + const now = yield* epochNow; + yield* sql` + UPDATE thread SET + updated_at = ${now}, + cost = cost + ${delta?.cost ?? 0}, + tokens_input = tokens_input + ${delta?.input ?? 0}, + tokens_output = tokens_output + ${delta?.output ?? 0}, + tokens_cache_read = tokens_cache_read + ${delta?.cacheRead ?? 0}, + tokens_cache_write = tokens_cache_write + ${delta?.cacheWrite ?? 0} + WHERE id = ${thread.id} + `; + yield* sql`UPDATE session SET updated_at = ${now} WHERE id = ${thread.sessionId}`; + }); + + type AppendTxResult = + | { readonly _tag: "threadNotFound" } + | { readonly _tag: "invalidData"; readonly reason: string } + | { readonly _tag: "inserted"; readonly entry: EntryRow }; + + const append = Effect.fn("Thread.append")(function* (input: AppendEntry) { + const parts = input.parts ?? []; + const state = input.state ?? "committed"; + const invalid = (reason: string) => new InvalidEntryDataError({ entryId: input.id, type: input.type, reason }); + if (state === "aborted" || (input.type !== "assistant" && state !== "committed")) { + return yield* invalid( + state === "aborted" + ? "an aborted assistant must be produced by abort" + : `entry type "${input.type}" must be committed`, + ); + } + if (parts.length > 0 && !messageTypes.has(input.type)) { + return yield* invalid(`entry type "${input.type}" must not carry parts`); + } + + // Part data is documented as verbatim aikit JSON and every reader parses + // it as such, so it is validated at the write that could reject it. + for (const [index, part] of parts.entries()) { + yield* decodeJsonObject(part.data).pipe( + Effect.mapError((error) => + invalid(`part ${index} ("${part.type}") is not a JSON object: ${error.message}`), + ), + ); + } + + const envelopeIdentity = messageTypes.has(input.type) + ? yield* decodeMessageEnvelopeIdentity(input.data).pipe(Effect.mapError((error) => invalid(error.message))) + : undefined; + if (envelopeIdentity !== undefined && envelopeIdentity.messageId !== input.id) { + return yield* invalid(`messageId "${envelopeIdentity.messageId}" does not match entry id`); + } + + const compaction = + input.type === "compaction" + ? yield* decodeCompactionData(input.data).pipe(Effect.mapError((error) => invalid(error.message))) + : undefined; + + // The envelope is authoritative; aggregate deltas are never accepted + // independently of the data persisted by this transaction. + const usage = + input.type === "assistant" + ? yield* decodeEnvelopeUsage(input.data).pipe( + Effect.map((envelope) => envelope.usage), + Effect.mapError((error) => invalid(error.message)), + ) + : undefined; + + const result: AppendTxResult = yield* sql + .withTransaction( + Effect.gen(function* () { + const thread = yield* findThread(input.threadId); + if (Option.isNone(thread)) return { _tag: "threadNotFound" } as const; + + if (compaction !== undefined && compaction.firstKeptEntryId !== null) { + const entries = yield* selectLine(input.threadId); + if (!entries.some((one) => one.id === compaction.firstKeptEntryId)) { + return { + _tag: "invalidData", + reason: `firstKeptEntryId "${compaction.firstKeptEntryId}" is not in the thread's line`, + } as const; + } + } + + const entryRow = yield* EntryRow.insert.makeEffect({ + id: input.id, + sessionId: thread.value.sessionId, + threadId: input.threadId, + seq: input.seq, + committedSeq: state === "committed" ? Option.some(input.seq) : Option.none(), + type: input.type, + state, + data: input.data, + label: Option.none(), + metadata: Option.fromUndefinedOr(input.metadata), + }); + const inserted = yield* insertEntry(entryRow); + if (inserted.length === 0) { + return { + _tag: "invalidData", + reason: `seq ${input.seq} does not advance past the thread's latest entry`, + } as const; + } + yield* writeParts(input.id, input.threadId, parts); + yield* touch( + thread.value, + usage === undefined + ? undefined + : { + cost: usage.cost.total, + input: usage.input, + output: usage.output, + cacheRead: usage.cacheRead, + cacheWrite: usage.cacheWrite, + }, + ); + + const stored = yield* findEntry(input.id); + if (Option.isNone(stored)) { + return yield* Effect.die(new Error("thread append: inserted entry did not persist")); + } + return { _tag: "inserted", entry: stored.value } as const; + }), + ) + .pipe(Effect.orDie); + + switch (result._tag) { + case "threadNotFound": + return yield* new ThreadNotFoundError({ threadId: input.threadId }); + case "invalidData": + return yield* invalid(result.reason); + case "inserted": + return result.entry; + } + }); + + const usageDelta = Effect.fnUntraced(function* (previous: string, next: string) { + const before = (yield* decodeEnvelopeUsage(previous)).usage; + const after = (yield* decodeEnvelopeUsage(next)).usage; + return { + cost: after.cost.total - before.cost.total, + input: after.input - before.input, + output: after.output - before.output, + cacheRead: after.cacheRead - before.cacheRead, + cacheWrite: after.cacheWrite - before.cacheWrite, + }; + }); + + const requireDraft = Effect.fnUntraced(function* (id: string, threadId: ThreadSchema.ID, operation: string) { + const current = yield* findEntry(id); + if ( + Option.isNone(current) || + current.value.threadId !== threadId || + current.value.type !== "assistant" || + current.value.state !== "draft" + ) { + return yield* Effect.die(`${operation} requires draft assistant ${id}`); + } + const thread = yield* findThread(threadId); + if (Option.isNone(thread)) return yield* Effect.die(`${operation}: thread ${threadId} is gone`); + return { entry: current.value, thread: thread.value }; + }); + + const replaceAssistant = Effect.fn("Thread.replaceAssistant")(function* (input: ReplaceAssistant) { + yield* sql.withTransaction( + Effect.gen(function* () { + const { entry, thread } = yield* requireDraft(input.id, input.threadId, "replaceAssistant"); + const delta = yield* usageDelta(entry.data, input.data); + const now = yield* epochNow; + yield* sql`DELETE FROM part WHERE entry_id = ${input.id}`; + yield* writeParts(input.id, input.threadId, input.parts); + yield* sql` + UPDATE entry SET data = ${input.data}, updated_at = ${now} + WHERE id = ${input.id} AND state = 'draft' + `; + yield* touch(thread, delta); + }), + ); + }, Effect.orDie); + + const settleToolCall = Effect.fn("Thread.settleToolCall")(function* (input: SettleToolCall) { + const data = yield* decodeJsonObject(input.data).pipe(Effect.orDie); + if (data["type"] !== "toolCall" || data["callID"] !== input.callId || data["status"] !== input.status) { + return yield* Effect.die(`settled tool payload does not match call ${input.callId}`); + } + const now = yield* epochNow; + const rows = yield* sql` + UPDATE part + SET data = ${input.data}, status = ${input.status}, updated_at = ${now} + WHERE entry_id = ${input.entryId} + AND call_id = ${input.callId} + AND type = 'toolCall' + AND status IN ('pending', 'running') + AND EXISTS (SELECT 1 FROM entry WHERE id = ${input.entryId} AND state = 'draft') + RETURNING id + `; + if (rows.length !== 1) { + return yield* Effect.die(`settleToolCall requires one unsettled call ${input.entryId}/${input.callId}`); + } + }, Effect.orDie); + + const commit = Effect.fn("Thread.commit")(function* (input: EndAssistant) { + const now = yield* epochNow; + const rows = yield* sql` + UPDATE entry SET state = 'committed', committed_seq = ${input.seq}, updated_at = ${now} + WHERE id = ${input.id} + AND thread_id = ${input.threadId} + AND type = 'assistant' + AND state = 'draft' + AND NOT EXISTS ( + SELECT 1 FROM part WHERE entry_id = ${input.id} AND status IN ('pending', 'running') + ) + RETURNING id + `; + if (rows.length !== 1) return yield* Effect.die(`commit requires a fully-settled draft ${input.id}`); + yield* sql`UPDATE thread SET updated_at = ${now} WHERE id = ${input.threadId}`; + }, Effect.orDie); + + const abort = Effect.fn("Thread.abort")(function* (input: EndAssistant & { readonly data: string }) { + yield* sql.withTransaction( + Effect.gen(function* () { + const { entry, thread } = yield* requireDraft(input.id, input.threadId, "abort"); + const delta = yield* usageDelta(entry.data, input.data); + const now = yield* epochNow; + yield* sql`DELETE FROM part WHERE entry_id = ${input.id}`; + yield* sql` + UPDATE entry + SET state = 'aborted', committed_seq = ${input.seq}, data = ${input.data}, updated_at = ${now} + WHERE id = ${input.id} AND state = 'draft' + `; + yield* touch(thread, delta); + }), + ); + }, Effect.orDie); + + /** + * Resolves a fork request to the stored origin: the committed entry it is cut at, owned by the + * thread that wrote it, which is not always the thread asked for. + */ + const forkPoint = Effect.fnUntraced(function* (input: ForkInput) { + const reject = (reason: ForkError["reason"]) => + Effect.fail(new ForkError({ threadId: input.threadId, entryId: input.entryId, reason })); + const entries = yield* selectLine(input.threadId).pipe(Effect.orDie); + const index = entries.findIndex((one) => one.id === input.entryId); + const target = entries[index]; + if (target === undefined) return yield* reject("entry_not_found"); + if (target.state !== "committed") return yield* reject("entry_not_committed"); + if (input.mode !== "before") return { point: target, text: undefined }; + if (target.type !== "user") return yield* reject("not_a_user_entry"); + const before = entries.slice(0, index).findLast((one) => one.state === "committed"); + if (before === undefined) return yield* reject("nothing_before_entry"); + const parts = yield* partsFor([target]); + const text = parts + .flatMap((part) => Option.toArray(decodeUserText(part.data))) + .map((part) => part.text) + .join("\n\n"); + return { point: before, text }; + }); + + const fork = Effect.fn("Thread.fork")(function* (input: ForkInput) { + const source = yield* findThread(input.threadId).pipe(Effect.orDie); + if (Option.isNone(source)) return yield* new ThreadNotFoundError({ threadId: input.threadId }); + const { point, text } = yield* forkPoint(input); + const id = input.id ?? ThreadSchema.ID.create(); + yield* events.publish(EventList.ThreadForked, { + timestamp: yield* DateTime.now, + sessionId: SessionSchema.ID.make(source.value.sessionId), + threadId: id, + sourceThreadId: input.threadId, + originThreadId: ThreadSchema.ID.make(point.threadId), + entryId: point.id, + }); + const created = yield* findThread(id).pipe(Effect.orDie); + if (Option.isNone(created)) return yield* Effect.die(new Error("thread fork did not persist")); + return { thread: created.value, ...(text === undefined ? {} : { text }) } satisfies Forked; + }); + + const projectForked = Effect.fn("Thread.projectForked")(function* ( + input: EventList.ThreadForked["data"], + seq: number, + ) { + // Checked again inside the commit: the session may have been deleted since the request was read. + const point = yield* findEntry(input.entryId).pipe(Effect.orDie); + if ( + Option.isNone(point) || + point.value.threadId !== input.originThreadId || + point.value.state !== "committed" || + Option.isNone(point.value.committedSeq) + ) { + return yield* Effect.die(`fork point ${input.entryId} is not a committed entry of ${input.originThreadId}`); + } + const cut = point.value.committedSeq.value; + yield* insert({ + id: input.threadId, + sessionId: input.sessionId, + origin: { kind: "fork", threadId: input.originThreadId, entryId: input.entryId }, + seq, + }); + // The documents in effect at the cut are the owner's: every thread between the source and + // the owner was created after it. + for (const document of yield* documents.ofThread(input.sourceThreadId)) { + const policy = Option.getOrUndefined(document.fork); + if (policy === undefined || policy === "initial") continue; + const owned = + policy === "asOf" ? yield* documents.find(input.originThreadId, document.kind) : Option.some(document); + if (Option.isNone(owned)) continue; + const value = + policy === "asOf" + ? yield* documents.asOf(owned.value.id, cut) + : yield* documents.current(owned.value.id); + if (Option.isNone(value)) continue; + yield* documents.create({ + sessionId: input.sessionId, + threadId: input.threadId, + definition: { + kind: document.kind, + version: value.value.version, + scope: "thread", + ...Option.match(document.history, { onNone: () => ({}), onSome: (history) => ({ history }) }), + fork: policy, + }, + value: value.value.value, + seq, + }); + } + // Every thread has an agent document, whatever its source held. + if (Option.isNone(yield* documents.find(input.threadId, DocumentSchema.agent.kind))) { + yield* documents.create({ + sessionId: input.sessionId, + threadId: input.threadId, + definition: DocumentSchema.agent, + value: {}, + seq, + }); + } + }); + + const retitle = Effect.fn("Thread.retitle")(function* (threadId: ThreadSchema.ID, title: string) { + const changed = yield* sql<{ readonly id: string }>` + UPDATE thread SET title = ${title} WHERE id = ${threadId} AND title IS NULL RETURNING id + `.pipe(Effect.orDie); + return changed.length > 0; + }); + + const markRunning = Effect.fn("Thread.markRunning")(function* (threadId: ThreadSchema.ID) { + yield* sql`UPDATE thread SET execution = 'running' WHERE id = ${threadId}`; + }, Effect.orDie); + + const endExecution = Effect.fn("Thread.endExecution")(function* ( + threadId: ThreadSchema.ID, + end: "finished" | "stopped" | "closed", + stoppedAt?: number, + ) { + if (end === "closed") { + yield* sql`UPDATE thread SET execution = 'closed' WHERE id = ${threadId}`; + return; + } + // Pending prompts that keep the mark: any for a finished run; for a stop, only those sent + // after it, since what was queued before it stays parked by the user's choice. + const after = end === "stopped" ? (stoppedAt ?? Number.MAX_SAFE_INTEGER) : -1; + yield* sql` + UPDATE thread SET execution = NULL, restarts = 0 + WHERE id = ${threadId} AND NOT EXISTS ( + SELECT 1 FROM inbox + WHERE thread_id = ${threadId} AND promoted_seq IS NULL AND cancelled_seq IS NULL + AND admitted_seq > ${after} + ) + `; + }, Effect.orDie); + + const claimRestart = Effect.fn("Thread.claimRestart")(function* (threadId: ThreadSchema.ID) { + const rows = yield* sql<{ restarts: number }>` + UPDATE thread + SET restarts = restarts + (CASE WHEN execution = 'running' THEN 1 ELSE 0 END), execution = 'running' + WHERE id = ${threadId} AND execution IS NOT NULL + RETURNING restarts + `; + return Option.fromUndefinedOr(rows[0]?.restarts); + }, Effect.orDie); + + const resetRestarts = Effect.fn("Thread.resetRestarts")(function* (threadId: ThreadSchema.ID) { + yield* sql`UPDATE thread SET restarts = 0 WHERE id = ${threadId} AND restarts > 0`; + }, Effect.orDie); + + const findUnfinished = SqlSchema.findAll({ + Request: Schema.Void, + Result: ThreadRow, + execute: () => sql` + SELECT * FROM thread WHERE execution IS NOT NULL AND origin_kind <> 'subagent' ORDER BY updated_at + `, + }); + const unfinished = findUnfinished(undefined).pipe(Effect.orDie, Effect.withSpan("Thread.unfinished")); + + const setLabel = Effect.fn("Thread.setLabel")(function* (input: { + threadId: ThreadSchema.ID; + entryId: string; + label: string | null; + }) { + const now = yield* epochNow; + const rows = yield* sql` + UPDATE entry SET label = ${input.label}, updated_at = ${now} + WHERE id = ${input.entryId} AND thread_id = ${input.threadId} + RETURNING id + `.pipe(Effect.orDie); + if (rows.length === 0) { + return yield* new EntryNotFoundError({ threadId: input.threadId, entryId: input.entryId }); + } + }); + + return Service.of({ + get, + main, + list, + entry, + line, + draft, + unsettled, + insert, + append, + replaceAssistant, + settleToolCall, + commit, + abort, + fork, + projectForked, + retitle, + markRunning, + endExecution, + claimRestart, + resetRestarts, + unfinished, + setLabel, + }); + }), +); + +export * as Thread from "./thread.ts"; diff --git a/packages/harness/src/tool/executor.ts b/packages/harness/src/tool/executor.ts index 489e3186..a191811e 100644 --- a/packages/harness/src/tool/executor.ts +++ b/packages/harness/src/tool/executor.ts @@ -57,6 +57,7 @@ export interface ProgressEvent { */ export interface HandleOptions { readonly sessionId?: ToolBefore["sessionId"]; + readonly threadId?: ToolBefore["threadId"]; readonly messageId?: ToolBefore["messageId"]; readonly modelInput?: ToolCallContext["modelInput"]; /** Sliding-queue capacity for best-effort progress. Bounded default (64); tunable. */ @@ -344,13 +345,15 @@ export const make = (tools: ReadonlyArray): E }; const hasHooks = hooks.beforeToolCall !== undefined || hooks.afterToolCall !== undefined; - if (hasHooks && (options?.sessionId === undefined || options.messageId === undefined)) { - return yield* Effect.die(new Error("Hooked tools require sessionId and messageId")); + const owner = + options?.sessionId !== undefined && options.threadId !== undefined && options.messageId !== undefined + ? { sessionId: options.sessionId, threadId: options.threadId, messageId: options.messageId } + : undefined; + if (hasHooks && owner === undefined) { + return yield* Effect.die(new Error("Hooked tools require sessionId, threadId and messageId")); } const hookCall: ToolBefore | undefined = - options?.sessionId !== undefined && options.messageId !== undefined - ? { ...ctx, sessionId: options.sessionId, messageId: options.messageId, params: decoded.success } - : undefined; + owner === undefined ? undefined : { ...ctx, ...owner, params: decoded.success }; if (hooks.beforeToolCall && hookCall) { const before = yield* invoke(() => hooks.beforeToolCall!(hookCall)).pipe(Effect.exit); if (Exit.isFailure(before)) { diff --git a/packages/harness/test/__artifacts__/runner.retry.json b/packages/harness/test/__artifacts__/runner.retry.json index 4e923302..22367eea 100644 --- a/packages/harness/test/__artifacts__/runner.retry.json +++ b/packages/harness/test/__artifacts__/runner.retry.json @@ -1,5 +1,8 @@ { "events": [ + { + "type": "session.created" + }, { "type": "session.turn.started" }, diff --git a/packages/harness/test/__artifacts__/thread.concurrent.json b/packages/harness/test/__artifacts__/thread.concurrent.json new file mode 100644 index 00000000..c4e64d5f --- /dev/null +++ b/packages/harness/test/__artifacts__/thread.concurrent.json @@ -0,0 +1,13 @@ +{ + "bothActive": true, + "main": [ + "user committed", + "assistant committed", + "user committed", + "assistant committed" + ], + "fork": [ + "user committed", + "assistant committed" + ] +} diff --git a/packages/harness/test/__artifacts__/thread.constraints.json b/packages/harness/test/__artifacts__/thread.constraints.json new file mode 100644 index 00000000..6fc67088 --- /dev/null +++ b/packages/harness/test/__artifacts__/thread.constraints.json @@ -0,0 +1,8 @@ +{ + "secondMain": "constraint failed: thread.session_id", + "twoEntriesOneEvent": "constraint failed: entry.thread_id, entry.seq", + "twoRevisionsOneEvent": "constraint failed: revision.document_id, revision.seq", + "draftWithCommit": "constraint failed: (state = 'draft') = (committed_seq IS NULL)", + "forkWithoutCut": "constraint failed: origin_kind <> 'fork' OR origin_entry_id IS NOT NULL", + "staleSeq": "seq 2 does not advance past the thread's latest entry" +} diff --git a/packages/harness/test/__artifacts__/thread.fork.json b/packages/harness/test/__artifacts__/thread.fork.json new file mode 100644 index 00000000..e4df1332 --- /dev/null +++ b/packages/harness/test/__artifacts__/thread.fork.json @@ -0,0 +1,93 @@ +{ + "reasons": [ + "nothing_before_entry", + "not_a_user_entry", + "entry_not_found", + "entry_not_committed", + "entry_not_committed" + ], + "events": [ + "1 session.created.1 T1", + "2 session.prompt.admitted.1 T1", + "3 session.prompt.promoted.1 T1", + "4 session.llm.started.1 T1", + "5 session.llm.ended.1 T1", + "6 session.turn.ended.1 T1", + "7 session.prompt.admitted.1 T1", + "8 session.prompt.promoted.1 T1", + "9 session.llm.started.1 T1", + "10 session.llm.ended.1 T1", + "11 session.turn.ended.1 T1", + "12 thread.forked.1 T2", + "13 thread.forked.1 T3", + "14 thread.forked.1 T4", + "15 session.prompt.admitted.1 T1", + "16 session.prompt.promoted.1 T1", + "17 session.llm.started.1 T1", + "18 session.llm.failed.1 T1" + ], + "threads": [ + { + "thread": "T1", + "origin": "main", + "createdSeq": 1, + "title": "first" + }, + { + "thread": "T2", + "origin": "fork T1 at E4", + "createdSeq": 12, + "title": null + }, + { + "thread": "T3", + "origin": "fork T1 at E1", + "createdSeq": 13, + "title": null + }, + { + "thread": "T4", + "origin": "fork T1 at E2", + "createdSeq": 14, + "title": null + } + ], + "entries": [ + "E1 T1 user seq=3 committed=3 committed", + "E2 T1 assistant seq=4 committed=6 committed", + "E3 T1 user seq=8 committed=8 committed", + "E4 T1 assistant seq=9 committed=11 committed", + "E5 T1 user seq=16 committed=16 committed", + "E6 T1 assistant seq=17 committed=18 aborted" + ], + "documents": [ + { + "thread": "T1", + "kind": "agent", + "revisions": [ + "1 {}" + ] + }, + { + "thread": "T2", + "kind": "agent", + "revisions": [ + "12 {}" + ] + }, + { + "thread": "T3", + "kind": "agent", + "revisions": [ + "13 {}" + ] + }, + { + "thread": "T4", + "kind": "agent", + "revisions": [ + "14 {}" + ] + } + ] +} diff --git a/packages/harness/test/__artifacts__/thread.recovery.json b/packages/harness/test/__artifacts__/thread.recovery.json new file mode 100644 index 00000000..7633fce6 --- /dev/null +++ b/packages/harness/test/__artifacts__/thread.recovery.json @@ -0,0 +1,55 @@ +{ + "events": [ + "1 session.created.1 T1", + "2 session.prompt.admitted.1 T1", + "3 session.prompt.promoted.1 T1", + "4 session.llm.started.1 T1", + "5 session.llm.ended.1 T1", + "6 session.turn.ended.1 T1", + "7 thread.forked.1 T2", + "8 session.prompt.admitted.1 T2", + "9 session.prompt.promoted.1 T2", + "10 session.llm.started.1 T2", + "11 session.llm.failed.1 T2", + "12 session.llm.started.1 T2", + "13 session.llm.ended.1 T2", + "14 session.turn.ended.1 T2" + ], + "threads": [ + { + "thread": "T1", + "origin": "main", + "createdSeq": 1, + "title": "base" + }, + { + "thread": "T2", + "origin": "fork T1 at E1", + "createdSeq": 7, + "title": "cut off" + } + ], + "entries": [ + "E1 T1 user seq=3 committed=3 committed", + "E2 T1 assistant seq=4 committed=6 committed", + "E3 T2 user seq=9 committed=9 committed", + "E4 T2 assistant seq=10 committed=11 aborted", + "E5 T2 assistant seq=12 committed=14 committed" + ], + "documents": [ + { + "thread": "T1", + "kind": "agent", + "revisions": [ + "1 {}" + ] + }, + { + "thread": "T2", + "kind": "agent", + "revisions": [ + "7 {}" + ] + } + ] +} diff --git a/packages/harness/test/__artifacts__/thread.reset.json b/packages/harness/test/__artifacts__/thread.reset.json new file mode 100644 index 00000000..5b5a328e --- /dev/null +++ b/packages/harness/test/__artifacts__/thread.reset.json @@ -0,0 +1,46 @@ +{ + "context": { + "roles": [ + "user", + "user" + ], + "texts": "The conversation was started over. Carried over from before:\n\n\nkeep the login work\n | after the reset" + }, + "events": [ + "1 session.created.1 T1", + "2 session.prompt.admitted.1 T1", + "3 session.prompt.promoted.1 T1", + "4 session.llm.started.1 T1", + "5 session.llm.ended.1 T1", + "6 session.turn.ended.1 T1", + "8 session.prompt.admitted.1 T1", + "9 session.prompt.promoted.1 T1", + "10 session.llm.started.1 T1", + "11 session.llm.ended.1 T1", + "12 session.turn.ended.1 T1" + ], + "threads": [ + { + "thread": "T1", + "origin": "main", + "createdSeq": 1, + "title": "before the reset" + } + ], + "entries": [ + "E1 T1 user seq=3 committed=3 committed", + "E2 T1 assistant seq=4 committed=6 committed", + "E3 T1 reset seq=7 committed=7 committed", + "E4 T1 user seq=9 committed=9 committed", + "E5 T1 assistant seq=10 committed=12 committed" + ], + "documents": [ + { + "thread": "T1", + "kind": "agent", + "revisions": [ + "1 {}" + ] + } + ] +} diff --git a/packages/harness/test/__artifacts__/thread.walkthrough.json b/packages/harness/test/__artifacts__/thread.walkthrough.json new file mode 100644 index 00000000..dc02c7c5 --- /dev/null +++ b/packages/harness/test/__artifacts__/thread.walkthrough.json @@ -0,0 +1,90 @@ +{ + "events": [ + "1 session.created.1 T1", + "2 thread.configured.1 T1", + "3 session.prompt.admitted.1 T1", + "4 session.prompt.promoted.1 T1", + "5 session.llm.started.1 T1", + "6 session.llm.ended.1 T1", + "7 thread.configured.1 T1", + "8 session.tool.settled.1 T1", + "9 session.turn.ended.1 T1", + "10 session.llm.started.1 T1", + "11 session.llm.ended.1 T1", + "12 session.turn.ended.1 T1", + "13 session.prompt.admitted.1 T1", + "14 session.prompt.promoted.1 T1", + "15 session.llm.started.1 T1", + "16 session.llm.ended.1 T1", + "17 session.turn.ended.1 T1", + "18 thread.forked.1 T2", + "19 session.prompt.admitted.1 T2", + "20 session.prompt.promoted.1 T2", + "21 session.llm.started.1 T2", + "22 session.llm.ended.1 T2", + "23 session.turn.ended.1 T2", + "24 thread.configured.1 T2", + "25 thread.forked.1 T3" + ], + "threads": [ + { + "thread": "T1", + "origin": "main", + "createdSeq": 1, + "title": "Add login" + }, + { + "thread": "T2", + "origin": "fork T1 at E3", + "createdSeq": 18, + "title": "Use OAuth instead" + }, + { + "thread": "T3", + "origin": "fork T1 at E2", + "createdSeq": 25, + "title": null + } + ], + "entries": [ + "E1 T1 user seq=4 committed=4 committed", + "E2 T1 assistant seq=5 committed=9 committed", + "E3 T1 assistant seq=10 committed=12 committed", + "E4 T1 user seq=14 committed=14 committed", + "E5 T1 assistant seq=15 committed=17 committed", + "E6 T2 user seq=20 committed=20 committed", + "E7 T2 assistant seq=21 committed=23 committed" + ], + "documents": [ + { + "thread": "T1", + "kind": "agent", + "revisions": [ + "1 {}", + "2 {\"model\":{\"provider\":\"openai\",\"id\":\"gpt-5.4\"}}", + "7 {\"model\":{\"provider\":\"openai\",\"id\":\"gpt-5.4\"},\"modelTools\":{\"add\":[\"web_fetch\"]}}" + ] + }, + { + "thread": "T2", + "kind": "agent", + "revisions": [ + "18 {\"model\":{\"provider\":\"openai\",\"id\":\"gpt-5.4\"},\"modelTools\":{\"add\":[\"web_fetch\"]}}", + "24 {\"model\":{\"provider\":\"anthropic\",\"id\":\"claude-sonnet-4-5\"},\"modelTools\":{\"add\":[\"web_fetch\"]}}" + ] + }, + { + "thread": "T3", + "kind": "agent", + "revisions": [ + "25 {\"model\":{\"provider\":\"openai\",\"id\":\"gpt-5.4\"},\"modelTools\":{\"add\":[\"web_fetch\"]}}" + ] + } + ], + "t2Context": [ + "user", + "assistant", + "assistant", + "user" + ] +} diff --git a/packages/harness/test/context.test.ts b/packages/harness/test/context.test.ts index 939813cf..028c7d88 100644 --- a/packages/harness/test/context.test.ts +++ b/packages/harness/test/context.test.ts @@ -11,6 +11,7 @@ import { seedSpace } from "./fixtures/space.ts"; import { SessionLive } from "../src/session/live.ts"; import { SessionMessageSchema } from "../src/session/message/schema.ts"; import { Session } from "../src/session/session.ts"; +import { Thread } from "../src/thread/thread.ts"; import { testEffect } from "./utils/effect.ts"; const sessionLayer = SessionLive.layer.pipe( @@ -60,33 +61,29 @@ const setup = Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; const { spaceId, location } = yield* seedSpace(); const sessions = yield* Session.Service; - const created = yield* sessions.create({ + const threads = yield* Thread.Service; + const events = yield* Event.Service; + const { session, thread } = yield* sessions.create({ spaceId, directory: location, title: "Context test", }); - let seq = 0; - const appendMessage = Effect.fnUntraced(function* (message: ContextCodec.ContextMessage) { - const encoded = yield* ContextCodec.encodeMessage(message); - seq += 1; - yield* sessions.append({ - id: message.messageId, - sessionId: created.id, - seq, - ...encoded, - }); + // Each entry takes a seq of its own, as the event that would have written it. + const append = Effect.fnUntraced(function* (input: Omit) { + const seq = (yield* events.latestSequence(session.id)) + 1; + yield* events.advance(session.id, seq); + yield* threads.append({ ...input, threadId: thread.id, seq }); }); - const append = Effect.fnUntraced(function* (input: Omit) { - seq += 1; - yield* sessions.append({ ...input, sessionId: created.id, seq }); + const appendMessage = Effect.fnUntraced(function* (message: ContextCodec.ContextMessage) { + yield* append({ id: message.messageId, ...(yield* ContextCodec.encodeMessage(message)) }); }); - return { append, appendMessage, context: yield* Context.Service, created, sessions, sql }; + return { append, appendMessage, context: yield* Context.Service, session, thread, threads, sql }; }); describe("context codec", () => { it("round-trips complete messages and promotes tool-call columns", () => Effect.gen(function* () { - const { appendMessage, created, sessions } = yield* setup; + const { appendMessage, thread, threads } = yield* setup; const toolCall: Message.ToolCallPendingPart = { type: "toolCall", callID: "call_weather", @@ -109,56 +106,53 @@ describe("context codec", () => { }); yield* appendMessage(original); - const stored = Option.getOrThrow(yield* sessions.entry(original.messageId)); + const stored = Option.getOrThrow(yield* threads.entry(original.messageId)); expect(yield* ContextCodec.decodeMessage(stored)).toEqual(original); - expect((yield* sessions.path(created.id)).at(-1)?.entry.id).toBe(original.messageId); + expect((yield* threads.line(thread.id)).at(-1)?.entry.id).toBe(original.messageId); })); }); describe("context assembly", () => { - it("omits draft and aborted assistants while retaining the raw tree leaf", () => + it("omits draft and aborted assistants while retaining the leaf", () => Effect.gen(function* () { - const { appendMessage, context, created } = yield* setup; + const { appendMessage, context, session, thread } = yield* setup; const events = yield* Event.Service; const assistantId = SessionMessageSchema.ID.make("msg_hidden_assistant"); const message = assistant(assistantId, "provider-a", "model-a"); yield* appendMessage(user("msg_visible_user", "hello")); yield* events.publish(EventList.LLMStarted, { - sessionId: created.id, + sessionId: session.id, + threadId: thread.id, messageId: assistantId, timestamp: yield* DateTime.now, message, }); - const during = yield* context.assemble(created.id); + const during = yield* context.assemble(thread.id); expect(during.leafEntryId).toBe(assistantId); expect(during.messages.map((item) => item.messageId)).toEqual(["msg_visible_user"]); expect(during.lastAssistant).toBeUndefined(); yield* events.publish(EventList.LLMFailed, { - sessionId: created.id, + sessionId: session.id, + threadId: thread.id, messageId: assistantId, timestamp: yield* DateTime.now, reason: "aborted", message: { ...message, stopReason: "aborted", errorMessage: "interrupted", parts: [] }, }); - const after = yield* context.assemble(created.id); + const after = yield* context.assemble(thread.id); expect(after.leafEntryId).toBe(assistantId); expect(after.messages.map((item) => item.messageId)).toEqual(["msg_visible_user"]); expect(after.lastAssistant).toBeUndefined(); })); - it("applies latest compaction over the full path", () => + it("applies latest compaction over the full line", () => Effect.gen(function* () { - const { append, appendMessage, context, created, sessions } = yield* setup; + const { append, appendMessage, context, thread, threads } = yield* setup; yield* appendMessage(user("u1", "old prompt")); yield* appendMessage(assistant("a1", "provider-1", "model-1")); yield* appendMessage(user("u2", "kept prompt")); - yield* append({ - id: "branch1", - type: "branchSummary", - data: JSON.stringify({ fromEntryId: "u1", summary: "The abandoned branch changed a test." }), - }); yield* append({ id: "synthetic1", type: "synthetic", @@ -172,28 +166,18 @@ describe("context assembly", () => { }); yield* append({ id: "custom1", type: "custom", data: JSON.stringify({ customType: "checkpoint" }) }); yield* appendMessage(assistant("a2", "provider-2", "model-2", [{ type: "text", text: "new answer" }])); - const snapshot = yield* context.assemble(created.id); + const snapshot = yield* context.assemble(thread.id); expect(snapshot.leafEntryId).toBe("a2"); expect(snapshot.lastAssistant).toMatchObject({ entryId: "a2", message: { messageId: "a2" } }); - expect(snapshot.messages.map((message) => message.messageId)).toEqual([ - "compact1", - "u2", - "branch1", - "synthetic1", - "a2", - ]); + expect(snapshot.messages.map((message) => message.messageId)).toEqual(["compact1", "u2", "synthetic1", "a2"]); expect(snapshot.messages[0]?.parts[0]).toEqual({ type: "text", text: "The conversation history before this point was compacted into the following summary:\n\n\nEarlier work was summarized.\n", }); - expect(snapshot.messages[2]?.parts[0]).toEqual({ - type: "text", - text: "The following is a summary of a branch that this conversation came back from:\n\n\nThe abandoned branch changed a test.\n", - }); - expect(snapshot.messages[3]?.parts[0]).toEqual({ type: "text", text: "file contents" }); + expect(snapshot.messages[2]?.parts[0]).toEqual({ type: "text", text: "file contents" }); - const path = yield* sessions.path(created.id); - const compacted = path.find((item) => item.entry.id === "compact1"); + const line = yield* threads.line(thread.id); + const compacted = line.find((item) => item.entry.id === "compact1"); expect(snapshot.messages[0]?.time.created).toBe( compacted === undefined ? undefined : DateTime.toEpochMillis(compacted.entry.createdAt), ); diff --git a/packages/harness/test/control.prompt.test.ts b/packages/harness/test/control.prompt.test.ts index ab8592c6..104ba6d3 100644 --- a/packages/harness/test/control.prompt.test.ts +++ b/packages/harness/test/control.prompt.test.ts @@ -7,12 +7,15 @@ import { ContextCodec } from "../src/context/codec.ts"; import { Database } from "../src/db/db.ts"; import { Event } from "../src/event/event.ts"; import { EventList } from "../src/event/list.ts"; +import { Inbox } from "../src/inbox/inbox.ts"; import { RunnerExecution } from "../src/runner/execution.ts"; -import { SessionInput } from "../src/session/input/input.ts"; import { SessionLive } from "../src/session/live.ts"; import { SessionMessageSchema } from "../src/session/message/schema.ts"; import { PromptSchema } from "../src/session/prompt/schema.ts"; +import { SessionSchema } from "../src/session/schema.ts"; import { Session } from "../src/session/session.ts"; +import { ThreadSchema } from "../src/thread/schema.ts"; +import { Thread } from "../src/thread/thread.ts"; import { seedSpace } from "./fixtures/space.ts"; import { testEffect } from "./utils/effect.ts"; @@ -36,18 +39,19 @@ const text = "Fix the failing tests"; const setup = Effect.gen(function* () { const { spaceId, location } = yield* seedSpace(); const sessions = yield* Session.Service; - const session = yield* sessions.create({ - spaceId, - directory: location, - title: "T", - tag: "test", - }); - return { sessions, control: yield* Control.Service, sessionId: session.id }; + const { session, thread } = yield* sessions.create({ spaceId, directory: location, title: "T", tag: "test" }); + return { + sessions, + threads: yield* Thread.Service, + control: yield* Control.Service, + sessionId: SessionSchema.ID.make(session.id), + threadId: ThreadSchema.ID.make(thread.id), + }; }); const admittedCount = Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; - return (yield* sql`SELECT id FROM session_input`).length; + return (yield* sql`SELECT id FROM inbox`).length; }); const eventCount = (type: string) => @@ -61,7 +65,7 @@ const prompt = (text: string) => PromptSchema.Prompt.fromText(text); describe("Control.prompt", () => { it("returns the original record when the id is retried", () => Effect.gen(function* () { - const { sessions, control, sessionId } = yield* setup; + const { threads, control, sessionId, threadId } = yield* setup; const input = { sessionId, id: messageId, prompt: prompt(text) }; const first = yield* control.prompt(input); @@ -69,7 +73,7 @@ describe("Control.prompt", () => { expect(retried).toEqual(first); expect(yield* admittedCount).toBe(1); - expect(yield* sessions.path(sessionId)).toEqual([]); + expect(yield* threads.line(threadId)).toEqual([]); })); it("rejects reuse of one id with a different prompt", () => @@ -103,14 +107,14 @@ describe("Control.prompt", () => { it("promotes once under concurrent promotion attempts", () => Effect.gen(function* () { - const { sessions, control, sessionId } = yield* setup; - const inputs = yield* SessionInput.make; + const { threads, control, sessionId, threadId } = yield* setup; + const inputs = yield* Inbox.make; const admitted = yield* control.prompt({ sessionId, id: messageId, prompt: prompt("Promote once") }); const counts = yield* Effect.all( [ - inputs.promoteSteers(sessionId, Number.MAX_SAFE_INTEGER), - inputs.promoteSteers(sessionId, Number.MAX_SAFE_INTEGER), + inputs.promoteSteers(threadId, Number.MAX_SAFE_INTEGER), + inputs.promoteSteers(threadId, Number.MAX_SAFE_INTEGER), ], { concurrency: "unbounded" }, ); @@ -124,10 +128,10 @@ describe("Control.prompt", () => { // Promotion is what puts the prompt in the conversation, and the entry // carries the promoting event's position. - const path = yield* sessions.path(sessionId); - expect(path.map((h) => h.entry.id)).toEqual([messageId]); - expect(path[0]!.entry.seq).toBe(promoted.promotedSeq); - expect((yield* ContextCodec.decodeMessage(path[0]!)).parts[0]).toMatchObject({ + const line = yield* threads.line(threadId); + expect(line.map((h) => h.entry.id)).toEqual([messageId]); + expect(line[0]!.entry.seq).toBe(promoted.promotedSeq); + expect((yield* ContextCodec.decodeMessage(line[0]!)).parts[0]).toMatchObject({ type: "text", text: "Promote once", }); @@ -135,108 +139,163 @@ describe("Control.prompt", () => { it("reports the follow-up to whichever caller won it", () => Effect.gen(function* () { - const { control, sessionId } = yield* setup; - const inputs = yield* SessionInput.make; + const { control, sessionId, threadId } = yield* setup; + const inputs = yield* Inbox.make; yield* control.prompt({ sessionId, prompt: prompt("Later"), delivery: "followUp" }); - const won = yield* Effect.all([inputs.promoteFollowUp(sessionId), inputs.promoteFollowUp(sessionId)], { + const won = yield* Effect.all([inputs.promoteFollowUp(threadId), inputs.promoteFollowUp(threadId)], { concurrency: "unbounded", }); expect(won.filter(Boolean).length).toBe(1); expect(yield* eventCount("session.prompt.promoted.1")).toBe(1); - expect(yield* inputs.hasPending(sessionId, "followUp")).toBe(false); + expect(yield* inputs.hasPending(threadId, "followUp")).toBe(false); })); it("promotes steers only through the captured cutoff", () => Effect.gen(function* () { - const { sessions, control, sessionId } = yield* setup; - const inputs = yield* SessionInput.make; + const { threads, control, sessionId, threadId } = yield* setup; + const inputs = yield* Inbox.make; const first = yield* control.prompt({ sessionId, prompt: prompt("Before cutoff") }); const cutoff = first.admittedSeq; const second = yield* control.prompt({ sessionId, prompt: prompt("After cutoff") }); - yield* inputs.promoteSteers(sessionId, cutoff); + yield* inputs.promoteSteers(threadId, cutoff); expect(some(yield* inputs.find(first.id)).promotedSeq).toBeDefined(); expect(some(yield* inputs.find(second.id)).promotedSeq).toBeUndefined(); - expect((yield* sessions.path(sessionId)).map((h) => h.entry.id)).toEqual([first.id]); + expect((yield* threads.line(threadId)).map((h) => h.entry.id)).toEqual([first.id]); + })); +}); + +describe("Control threads", () => { + it("addresses main when no thread is named, and keeps each thread's queue its own", () => + Effect.gen(function* () { + const { threads, control, sessionId, threadId } = yield* setup; + const inputs = yield* Inbox.make; + const first = yield* control.prompt({ sessionId, prompt: prompt("On main") }); + expect(first.threadId).toBe(threadId); + yield* inputs.promoteSteers(threadId, Number.MAX_SAFE_INTEGER); + + const forked = yield* threads.fork({ threadId, entryId: first.id }); + const fork = ThreadSchema.ID.make(forked.thread.id); + const onFork = yield* control.prompt({ sessionId, threadId: fork, prompt: prompt("On the fork") }); + const onMain = yield* control.prompt({ sessionId, prompt: prompt("Main again") }); + + expect((yield* control.pending(fork)).map((input) => input.id)).toEqual([onFork.id]); + expect((yield* control.pending(threadId)).map((input) => input.id)).toEqual([onMain.id]); + expect(yield* control.cancel(threadId, onFork.id)).toBe(false); + expect(yield* control.update(threadId, onFork.id, { delivery: "followUp" })).toBe(false); + })); + + it("refuses a thread of another session, and an unknown session", () => + Effect.gen(function* () { + const { sessions, control, sessionId } = yield* setup; + const { spaceId, location } = yield* seedSpace(); + const { thread: other } = yield* sessions.create({ spaceId, directory: location }); + + const foreign = yield* control + .prompt({ sessionId, threadId: ThreadSchema.ID.make(other.id), prompt: prompt(text) }) + .pipe(Effect.flip); + expect(foreign._tag).toBe("ThreadNotFoundError"); + const unknown = yield* control + .prompt({ sessionId: SessionSchema.ID.make("ses_missing"), prompt: prompt(text) }) + .pipe(Effect.flip); + expect(unknown._tag).toBe("SessionNotFoundError"); + expect(yield* admittedCount).toBe(0); + })); + + it("deletes a session with its pending prompts; an idle session has nothing to interrupt", () => + Effect.gen(function* () { + const { sessions, control, sessionId } = yield* setup; + yield* control.prompt({ sessionId, prompt: prompt(text) }); + + expect(yield* control.interruptSession(sessionId)).toBe(false); + expect(yield* control.deleteSession(sessionId)).toBe(true); + expect(Option.isNone(yield* sessions.get(sessionId))).toBe(true); + expect(yield* admittedCount).toBe(0); + expect(yield* control.deleteSession(sessionId)).toBe(false); })); }); describe("Control.cancel", () => { it("lists pending prompts of both lanes in admission order, without promoted or cancelled ones", () => Effect.gen(function* () { - const { control, sessionId } = yield* setup; - const inputs = yield* SessionInput.make; + const { control, sessionId, threadId } = yield* setup; + const inputs = yield* Inbox.make; const first = yield* control.prompt({ sessionId, prompt: prompt("First steer") }); const later = yield* control.prompt({ sessionId, prompt: prompt("Later"), delivery: "followUp" }); const second = yield* control.prompt({ sessionId, prompt: prompt("Second steer") }); const dropped = yield* control.prompt({ sessionId, prompt: prompt("Dropped") }); - expect((yield* control.pending(sessionId)).map((input) => [input.id, input.delivery])).toEqual([ + expect((yield* control.pending(threadId)).map((input) => [input.id, input.delivery])).toEqual([ [first.id, "steer"], [later.id, "followUp"], [second.id, "steer"], [dropped.id, "steer"], ]); - yield* control.cancel(sessionId, dropped.id); - yield* inputs.promoteSteers(sessionId, Number.MAX_SAFE_INTEGER); - expect((yield* control.pending(sessionId)).map((input) => input.id)).toEqual([later.id]); + yield* control.cancel(threadId, dropped.id); + yield* inputs.promoteSteers(threadId, Number.MAX_SAFE_INTEGER); + expect((yield* control.pending(threadId)).map((input) => input.id)).toEqual([later.id]); })); it("cancels a pending prompt once and refuses a second cancel", () => Effect.gen(function* () { - const { sessions, control, sessionId } = yield* setup; - const inputs = yield* SessionInput.make; + const { threads, control, sessionId, threadId } = yield* setup; + const inputs = yield* Inbox.make; const admitted = yield* control.prompt({ sessionId, id: messageId, prompt: prompt(text) }); - expect(yield* control.cancel(sessionId, admitted.id)).toBe(true); - expect(yield* control.cancel(sessionId, admitted.id)).toBe(false); + expect(yield* control.cancel(threadId, admitted.id)).toBe(true); + expect(yield* control.cancel(threadId, admitted.id)).toBe(false); expect(yield* eventCount("session.prompt.cancelled.1")).toBe(1); - expect(yield* control.pending(sessionId)).toEqual([]); - expect(yield* inputs.hasPending(sessionId, "steer")).toBe(false); + expect(yield* control.pending(threadId)).toEqual([]); + expect(yield* inputs.hasPending(threadId, "steer")).toBe(false); expect(some(yield* inputs.find(admitted.id)).cancelledSeq).toBeDefined(); // Nothing is left to promote, and nothing reaches the conversation. - expect(yield* inputs.promoteSteers(sessionId, Number.MAX_SAFE_INTEGER)).toBe(0); - expect(yield* sessions.path(sessionId)).toEqual([]); + expect(yield* inputs.promoteSteers(threadId, Number.MAX_SAFE_INTEGER)).toBe(0); + expect(yield* threads.line(threadId)).toEqual([]); })); it("refuses to cancel another session's prompt, or an unknown id", () => Effect.gen(function* () { - const { sessions, control, sessionId } = yield* setup; + const { sessions, control, sessionId, threadId } = yield* setup; const { spaceId, location } = yield* seedSpace(); - const other = yield* sessions.create({ spaceId, directory: location, title: "Other", tag: "test" }); + const { thread: other } = yield* sessions.create({ + spaceId, + directory: location, + title: "Other", + tag: "test", + }); const admitted = yield* control.prompt({ sessionId, prompt: prompt(text) }); - expect(yield* control.cancel(other.id, admitted.id)).toBe(false); - expect(yield* control.cancel(sessionId, SessionMessageSchema.ID.make("msg_unknown"))).toBe(false); + expect(yield* control.cancel(ThreadSchema.ID.make(other.id), admitted.id)).toBe(false); + expect(yield* control.cancel(threadId, SessionMessageSchema.ID.make("msg_unknown"))).toBe(false); expect(yield* eventCount("session.prompt.cancelled.1")).toBe(0); - expect((yield* control.pending(sessionId)).map((input) => input.id)).toEqual([admitted.id]); + expect((yield* control.pending(threadId)).map((input) => input.id)).toEqual([admitted.id]); })); it("refuses to cancel a promoted prompt, which stays in the conversation", () => Effect.gen(function* () { - const { sessions, control, sessionId } = yield* setup; - const inputs = yield* SessionInput.make; + const { threads, control, sessionId, threadId } = yield* setup; + const inputs = yield* Inbox.make; const admitted = yield* control.prompt({ sessionId, prompt: prompt(text) }); - yield* inputs.promoteSteers(sessionId, Number.MAX_SAFE_INTEGER); + yield* inputs.promoteSteers(threadId, Number.MAX_SAFE_INTEGER); - expect(yield* control.cancel(sessionId, admitted.id)).toBe(false); + expect(yield* control.cancel(threadId, admitted.id)).toBe(false); expect(yield* eventCount("session.prompt.cancelled.1")).toBe(0); - expect((yield* sessions.path(sessionId)).map((h) => h.entry.id)).toEqual([admitted.id]); + expect((yield* threads.line(threadId)).map((h) => h.entry.id)).toEqual([admitted.id]); })); it("admits a cancelled id again, as a fresh pending prompt", () => Effect.gen(function* () { - const { sessions, control, sessionId } = yield* setup; - const inputs = yield* SessionInput.make; + const { threads, control, sessionId, threadId } = yield* setup; + const inputs = yield* Inbox.make; const first = yield* control.prompt({ sessionId, id: messageId, prompt: prompt(text) }); - yield* control.cancel(sessionId, messageId); + yield* control.cancel(threadId, messageId); const retried = yield* control.prompt({ sessionId, @@ -247,7 +306,7 @@ describe("Control.cancel", () => { expect(retried.cancelledSeq).toBeUndefined(); expect(retried.admittedSeq).toBeGreaterThan(first.admittedSeq); - expect(yield* control.pending(sessionId)).toMatchObject([{ id: messageId, delivery: "followUp" }]); + expect(yield* control.pending(threadId)).toMatchObject([{ id: messageId, delivery: "followUp" }]); expect( yield* control.prompt({ sessionId, @@ -257,24 +316,25 @@ describe("Control.cancel", () => { }), ).toEqual(retried); - expect(yield* inputs.promoteFollowUp(sessionId)).toBe(true); - const path = yield* sessions.path(sessionId); - expect(path.map((h) => h.entry.id)).toEqual([messageId]); - expect((yield* ContextCodec.decodeMessage(path[0]!)).parts[0]).toMatchObject({ text: "Fix it another way" }); + expect(yield* inputs.promoteFollowUp(threadId)).toBe(true); + const line = yield* threads.line(threadId); + expect(line.map((h) => h.entry.id)).toEqual([messageId]); + expect((yield* ContextCodec.decodeMessage(line[0]!)).parts[0]).toMatchObject({ text: "Fix it another way" }); })); // The promotion the runner had already read for a row that is then cancelled: it must lose, // not read the cancelled row as a replay onto an empty projection and append it anyway. it("rolls back a promotion that commits after the cancel", () => Effect.gen(function* () { - const { sessions, control, sessionId } = yield* setup; + const { threads, control, sessionId, threadId } = yield* setup; const events = yield* Event.Service; const admitted = yield* control.prompt({ sessionId, prompt: prompt(text) }); - yield* control.cancel(sessionId, admitted.id); + yield* control.cancel(threadId, admitted.id); const late = yield* events .publish(EventList.Prompted, { sessionId, + threadId, timestamp: admitted.timeCreated, messageId: admitted.id, prompt: admitted.prompt, @@ -284,39 +344,39 @@ describe("Control.cancel", () => { expect(Exit.isFailure(late)).toBe(true); expect(yield* eventCount("session.prompt.promoted.1")).toBe(0); - expect(yield* sessions.path(sessionId)).toEqual([]); + expect(yield* threads.line(threadId)).toEqual([]); })); it("lets exactly one of a cancel and a promotion win the same prompt", () => Effect.gen(function* () { - const { sessions, control, sessionId } = yield* setup; - const inputs = yield* SessionInput.make; + const { threads, control, sessionId, threadId } = yield* setup; + const inputs = yield* Inbox.make; const admitted = yield* control.prompt({ sessionId, prompt: prompt(text) }); const [cancelled, promoted] = yield* Effect.all( - [control.cancel(sessionId, admitted.id), inputs.promoteSteers(sessionId, Number.MAX_SAFE_INTEGER)], + [control.cancel(threadId, admitted.id), inputs.promoteSteers(threadId, Number.MAX_SAFE_INTEGER)], { concurrency: "unbounded" }, ); expect(cancelled !== (promoted === 1)).toBe(true); - expect((yield* sessions.path(sessionId)).map((h) => h.entry.id)).toEqual(cancelled ? [] : [admitted.id]); + expect((yield* threads.line(threadId)).map((h) => h.entry.id)).toEqual(cancelled ? [] : [admitted.id]); expect(yield* eventCount("session.prompt.cancelled.1")).toBe(cancelled ? 1 : 0); expect(yield* eventCount("session.prompt.promoted.1")).toBe(cancelled ? 0 : 1); })); it("does not turn an unrelated projector defect into a lost cancel", () => Effect.gen(function* () { - const { control, sessionId } = yield* setup; + const { control, sessionId, threadId } = yield* setup; const events = yield* Event.Service; const admitted = yield* control.prompt({ sessionId, prompt: prompt(text) }); const defect = new Error("projector failed"); yield* events.project(EventList.PromptCancelled, () => Effect.die(defect)); - const exit = yield* control.cancel(sessionId, admitted.id).pipe(Effect.exit); + const exit = yield* control.cancel(threadId, admitted.id).pipe(Effect.exit); expect(Exit.isFailure(exit) && Cause.squash(exit.cause)).toBe(defect); expect(yield* eventCount("session.prompt.cancelled.1")).toBe(0); - expect((yield* control.pending(sessionId)).map((input) => input.id)).toEqual([admitted.id]); + expect((yield* control.pending(threadId)).map((input) => input.id)).toEqual([admitted.id]); })); }); @@ -330,7 +390,7 @@ const watched = Control.layer.pipe( active: Effect.succeed(new Set()), resume: () => Effect.void, recover: () => Effect.void, - wake: (sessionId) => Effect.sync(() => wakes.push(sessionId)), + wake: (threadId) => Effect.sync(() => wakes.push(threadId)), interrupt: () => Effect.succeed(false), awaitIdle: () => Effect.void, }), @@ -345,21 +405,21 @@ const { effect: itWatched } = testEffect(watched); describe("Control.update", () => { itWatched("moves a pending prompt between lanes in place, waking only for the steer lane", () => Effect.gen(function* () { - const { control, sessionId } = yield* setup; + const { control, sessionId, threadId } = yield* setup; const later = yield* control.prompt({ sessionId, prompt: prompt("Later"), delivery: "followUp" }); const steer = yield* control.prompt({ sessionId, prompt: prompt("Now") }); wakes.length = 0; - expect(yield* control.update(sessionId, later.id, { delivery: "steer" })).toBe(true); - expect((yield* control.pending(sessionId)).map((input) => [input.id, input.delivery])).toEqual([ + expect(yield* control.update(threadId, later.id, { delivery: "steer" })).toBe(true); + expect((yield* control.pending(threadId)).map((input) => [input.id, input.delivery])).toEqual([ [later.id, "steer"], [steer.id, "steer"], ]); - expect(wakes).toEqual([sessionId]); + expect(wakes).toEqual([threadId]); wakes.length = 0; - expect(yield* control.update(sessionId, steer.id, { delivery: "followUp" })).toBe(true); - expect((yield* control.pending(sessionId)).map((input) => [input.id, input.delivery])).toEqual([ + expect(yield* control.update(threadId, steer.id, { delivery: "followUp" })).toBe(true); + expect((yield* control.pending(threadId)).map((input) => [input.id, input.delivery])).toEqual([ [later.id, "steer"], [steer.id, "followUp"], ]); @@ -371,67 +431,72 @@ describe("Control.update", () => { // "Send now" on a steer left waiting by a stop: nothing to change, but the session must wake. itWatched("wakes for a steer already in the steer lane without recording a change", () => Effect.gen(function* () { - const { control, sessionId } = yield* setup; + const { control, sessionId, threadId } = yield* setup; const steer = yield* control.prompt({ sessionId, prompt: prompt("Parked") }); wakes.length = 0; - expect(yield* control.update(sessionId, steer.id, { delivery: "steer" })).toBe(true); + expect(yield* control.update(threadId, steer.id, { delivery: "steer" })).toBe(true); - expect(wakes).toEqual([sessionId]); + expect(wakes).toEqual([threadId]); expect(yield* eventCount("session.prompt.updated.1")).toBe(0); }), ); it("rewrites a pending prompt in place, and the promotion carries the new text", () => Effect.gen(function* () { - const { sessions, control, sessionId } = yield* setup; - const inputs = yield* SessionInput.make; + const { threads, control, sessionId, threadId } = yield* setup; + const inputs = yield* Inbox.make; const first = yield* control.prompt({ sessionId, prompt: prompt("Fix the tests") }); const second = yield* control.prompt({ sessionId, prompt: prompt("Then lint") }); - expect(yield* control.update(sessionId, first.id, { prompt: prompt("Fix the flaky test only") })).toBe(true); - expect((yield* control.pending(sessionId)).map((input) => input.id)).toEqual([first.id, second.id]); + expect(yield* control.update(threadId, first.id, { prompt: prompt("Fix the flaky test only") })).toBe(true); + expect((yield* control.pending(threadId)).map((input) => input.id)).toEqual([first.id, second.id]); - yield* inputs.promoteSteers(sessionId, Number.MAX_SAFE_INTEGER); - const path = yield* sessions.path(sessionId); - expect(path.map((h) => h.entry.id)).toEqual([first.id, second.id]); - expect((yield* ContextCodec.decodeMessage(path[0]!)).parts[0]).toMatchObject({ + yield* inputs.promoteSteers(threadId, Number.MAX_SAFE_INTEGER); + const line = yield* threads.line(threadId); + expect(line.map((h) => h.entry.id)).toEqual([first.id, second.id]); + expect((yield* ContextCodec.decodeMessage(line[0]!)).parts[0]).toMatchObject({ text: "Fix the flaky test only", }); })); it("refuses to update a promoted, cancelled, unknown or another session's prompt", () => Effect.gen(function* () { - const { sessions, control, sessionId } = yield* setup; - const inputs = yield* SessionInput.make; + const { sessions, control, sessionId, threadId } = yield* setup; + const inputs = yield* Inbox.make; const { spaceId, location } = yield* seedSpace(); - const other = yield* sessions.create({ spaceId, directory: location, title: "Other", tag: "test" }); + const { thread: other } = yield* sessions.create({ + spaceId, + directory: location, + title: "Other", + tag: "test", + }); const promoted = yield* control.prompt({ sessionId, prompt: prompt("Promoted") }); - yield* inputs.promoteSteers(sessionId, Number.MAX_SAFE_INTEGER); + yield* inputs.promoteSteers(threadId, Number.MAX_SAFE_INTEGER); const cancelled = yield* control.prompt({ sessionId, prompt: prompt("Cancelled") }); - yield* control.cancel(sessionId, cancelled.id); + yield* control.cancel(threadId, cancelled.id); const pending = yield* control.prompt({ sessionId, prompt: prompt("Pending"), delivery: "followUp" }); const refused = { delivery: "steer" as const, prompt: prompt("Changed") }; - expect(yield* control.update(sessionId, promoted.id, refused)).toBe(false); - expect(yield* control.update(sessionId, cancelled.id, refused)).toBe(false); - expect(yield* control.update(sessionId, SessionMessageSchema.ID.make("msg_unknown"), refused)).toBe(false); - expect(yield* control.update(other.id, pending.id, refused)).toBe(false); + expect(yield* control.update(threadId, promoted.id, refused)).toBe(false); + expect(yield* control.update(threadId, cancelled.id, refused)).toBe(false); + expect(yield* control.update(threadId, SessionMessageSchema.ID.make("msg_unknown"), refused)).toBe(false); + expect(yield* control.update(ThreadSchema.ID.make(other.id), pending.id, refused)).toBe(false); expect(yield* eventCount("session.prompt.updated.1")).toBe(0); - expect(yield* control.pending(sessionId)).toMatchObject([{ id: pending.id, delivery: "followUp" }]); + expect(yield* control.pending(threadId)).toMatchObject([{ id: pending.id, delivery: "followUp" }]); })); it("settles an update racing a promotion in one of its valid orders", () => Effect.gen(function* () { - const { sessions, control, sessionId } = yield* setup; - const inputs = yield* SessionInput.make; + const { threads, control, sessionId, threadId } = yield* setup; + const inputs = yield* Inbox.make; const admitted = yield* control.prompt({ sessionId, prompt: prompt("Original") }); const [updated, promoted] = yield* Effect.all( [ - control.update(sessionId, admitted.id, { prompt: prompt("Rewritten") }), - inputs.promoteSteers(sessionId, Number.MAX_SAFE_INTEGER), + control.update(threadId, admitted.id, { prompt: prompt("Rewritten") }), + inputs.promoteSteers(threadId, Number.MAX_SAFE_INTEGER), ], { concurrency: "unbounded" }, ); @@ -439,13 +504,13 @@ describe("Control.update", () => { // Three orders are valid: the rewrite commits first and is promoted; the promotion commits // first and the rewrite is refused; or the promotion selected the prompt before the rewrite // committed, then steps back and leaves the rewritten prompt pending. - const path = yield* sessions.path(sessionId); + const line = yield* threads.line(threadId); if (promoted === 0) { expect(updated).toBe(true); - expect(path).toEqual([]); - expect(PromptSchema.Prompt.text((yield* control.pending(sessionId))[0]!.prompt)).toBe("Rewritten"); + expect(line).toEqual([]); + expect(PromptSchema.Prompt.text((yield* control.pending(threadId))[0]!.prompt)).toBe("Rewritten"); } else { - expect((yield* ContextCodec.decodeMessage(path[0]!)).parts[0]).toMatchObject({ + expect((yield* ContextCodec.decodeMessage(line[0]!)).parts[0]).toMatchObject({ text: updated ? "Rewritten" : "Original", }); } @@ -456,29 +521,29 @@ describe("Control.update", () => { describe("Control queue order", () => { it("keeps the others in order when a middle prompt is cancelled", () => Effect.gen(function* () { - const { control, sessionId } = yield* setup; + const { control, sessionId, threadId } = yield* setup; const first = yield* control.prompt({ sessionId, prompt: prompt("First") }); const middle = yield* control.prompt({ sessionId, prompt: prompt("Middle"), delivery: "followUp" }); const last = yield* control.prompt({ sessionId, prompt: prompt("Last") }); - yield* control.cancel(sessionId, middle.id); + yield* control.cancel(threadId, middle.id); - expect((yield* control.pending(sessionId)).map((input) => input.id)).toEqual([first.id, last.id]); + expect((yield* control.pending(threadId)).map((input) => input.id)).toEqual([first.id, last.id]); })); it("does not turn an unrelated projector defect into a lost update", () => Effect.gen(function* () { - const { control, sessionId } = yield* setup; + const { control, sessionId, threadId } = yield* setup; const events = yield* Event.Service; const admitted = yield* control.prompt({ sessionId, prompt: prompt(text), delivery: "followUp" }); const defect = new Error("projector failed"); yield* events.project(EventList.PromptUpdated, () => Effect.die(defect)); - const exit = yield* control.update(sessionId, admitted.id, { delivery: "steer" }).pipe(Effect.exit); + const exit = yield* control.update(threadId, admitted.id, { delivery: "steer" }).pipe(Effect.exit); expect(Exit.isFailure(exit) && Cause.squash(exit.cause)).toBe(defect); expect(yield* eventCount("session.prompt.updated.1")).toBe(0); - expect(yield* control.pending(sessionId)).toMatchObject([{ id: admitted.id, delivery: "followUp" }]); + expect(yield* control.pending(threadId)).toMatchObject([{ id: admitted.id, delivery: "followUp" }]); })); }); @@ -487,8 +552,8 @@ describe("Control queue races", () => { // promotion must step back, not fail the drain, and the next one carries the new content. it("steps a promotion back when its prompt was updated after selection", () => Effect.gen(function* () { - const { sessions, control, sessionId } = yield* setup; - const inputs = yield* SessionInput.make; + const { threads, control, sessionId, threadId } = yield* setup; + const inputs = yield* Inbox.make; const events = yield* Event.Service; const first = yield* control.prompt({ sessionId, prompt: prompt("First") }); const second = yield* control.prompt({ sessionId, prompt: prompt("Second") }); @@ -497,27 +562,27 @@ describe("Control queue races", () => { !rewritten && Schema.is(EventList.Prompted)(event) && event.data.messageId === first.id ? Effect.gen(function* () { rewritten = true; - yield* control.update(sessionId, second.id, { prompt: prompt("Second, rewritten") }); + yield* control.update(threadId, second.id, { prompt: prompt("Second, rewritten") }); }) : Effect.void, ); - expect(yield* inputs.promoteSteers(sessionId, Number.MAX_SAFE_INTEGER)).toBe(1); + expect(yield* inputs.promoteSteers(threadId, Number.MAX_SAFE_INTEGER)).toBe(1); expect(rewritten).toBe(true); - expect(yield* control.pending(sessionId)).toMatchObject([{ id: second.id }]); + expect(yield* control.pending(threadId)).toMatchObject([{ id: second.id }]); - expect(yield* inputs.promoteSteers(sessionId, Number.MAX_SAFE_INTEGER)).toBe(1); - const path = yield* sessions.path(sessionId); - expect(path.map((h) => h.entry.id)).toEqual([first.id, second.id]); - expect((yield* ContextCodec.decodeMessage(path[1]!)).parts[0]).toMatchObject({ text: "Second, rewritten" }); + expect(yield* inputs.promoteSteers(threadId, Number.MAX_SAFE_INTEGER)).toBe(1); + const line = yield* threads.line(threadId); + expect(line.map((h) => h.entry.id)).toEqual([first.id, second.id]); + expect((yield* ContextCodec.decodeMessage(line[1]!)).parts[0]).toMatchObject({ text: "Second, rewritten" }); })); // The prompt was cancelled and sent again, unchanged, after the promotion selected it: the stale // promotion steps back and the next one promotes the new admission. it("steps a promotion back when its prompt was cancelled and re-admitted after selection", () => Effect.gen(function* () { - const { sessions, control, sessionId } = yield* setup; - const inputs = yield* SessionInput.make; + const { threads, control, sessionId, threadId } = yield* setup; + const inputs = yield* Inbox.make; const events = yield* Event.Service; const first = yield* control.prompt({ sessionId, prompt: prompt("First") }); const second = { sessionId, id: SessionMessageSchema.ID.make("msg_second"), prompt: prompt("Second") }; @@ -527,7 +592,7 @@ describe("Control queue races", () => { !resent && Schema.is(EventList.Prompted)(event) && event.data.messageId === first.id ? Effect.gen(function* () { resent = true; - yield* control.cancel(sessionId, second.id); + yield* control.cancel(threadId, second.id); // Sent again later, as a person would: the new admission's time differs. yield* TestClock.adjust("1 second"); yield* control.prompt(second); @@ -535,103 +600,104 @@ describe("Control queue races", () => { : Effect.void, ); - expect(yield* inputs.promoteSteers(sessionId, Number.MAX_SAFE_INTEGER)).toBe(1); + expect(yield* inputs.promoteSteers(threadId, Number.MAX_SAFE_INTEGER)).toBe(1); expect(resent).toBe(true); - const [pending] = yield* control.pending(sessionId); + const [pending] = yield* control.pending(threadId); expect(pending?.id).toBe(second.id); expect(pending!.admittedSeq).toBeGreaterThan(before.admittedSeq); - expect(yield* inputs.promoteSteers(sessionId, Number.MAX_SAFE_INTEGER)).toBe(1); - expect((yield* sessions.path(sessionId)).map((h) => h.entry.id)).toEqual([first.id, second.id]); + expect(yield* inputs.promoteSteers(threadId, Number.MAX_SAFE_INTEGER)).toBe(1); + expect((yield* threads.line(threadId)).map((h) => h.entry.id)).toEqual([first.id, second.id]); })); // Two updates of different fields, the second computed before the first committed: each event // carries only its own field, so the first one's change survives. it("applies only the field an update changes", () => Effect.gen(function* () { - const { control, sessionId } = yield* setup; + const { control, sessionId, threadId } = yield* setup; const events = yield* Event.Service; const admitted = yield* control.prompt({ sessionId, prompt: prompt("Original") }); - yield* control.update(sessionId, admitted.id, { prompt: prompt("Rewritten") }); + yield* control.update(threadId, admitted.id, { prompt: prompt("Rewritten") }); yield* events.publish(EventList.PromptUpdated, { sessionId, + threadId, messageId: admitted.id, timestamp: admitted.timeCreated, delivery: "followUp", }); - const [pending] = yield* control.pending(sessionId); + const [pending] = yield* control.pending(threadId); expect(pending?.delivery).toBe("followUp"); expect(PromptSchema.Prompt.text(pending!.prompt)).toBe("Rewritten"); })); it("does not report a re-admission as accepted when its projector fails", () => Effect.gen(function* () { - const { control, sessionId } = yield* setup; + const { control, sessionId, threadId } = yield* setup; const events = yield* Event.Service; const input = { sessionId, id: messageId, prompt: prompt(text) }; yield* control.prompt(input); - yield* control.cancel(sessionId, messageId); + yield* control.cancel(threadId, messageId); yield* events.project(EventList.PromptAdmitted, () => Effect.die(new Error("projector failed"))); const exit = yield* control.prompt(input).pipe(Effect.exit); expect(Exit.isFailure(exit)).toBe(true); - expect(yield* control.pending(sessionId)).toEqual([]); + expect(yield* control.pending(threadId)).toEqual([]); })); }); describe("Control unfinished runs", () => { - const executionOf = (sessionId: string) => + const executionOf = (threadId: string) => Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; - const [row] = yield* sql<{ execution: string | null }>`SELECT execution FROM session WHERE id = ${sessionId}`; + const [row] = yield* sql<{ execution: string | null }>`SELECT execution FROM thread WHERE id = ${threadId}`; return row?.execution; }); // Marked before the wake: a crash between accepting the prompt and its run starting must not lose it. - it("marks a session running as soon as a prompt is accepted", () => + it("marks a thread running as soon as a prompt is accepted", () => Effect.gen(function* () { - const { control, sessionId } = yield* setup; - expect(yield* executionOf(sessionId)).toBeNull(); + const { control, sessionId, threadId } = yield* setup; + expect(yield* executionOf(threadId)).toBeNull(); yield* control.prompt({ sessionId, prompt: prompt(text) }); - expect(yield* executionOf(sessionId)).toBe("running"); + expect(yield* executionOf(threadId)).toBe("running"); })); it("keeps a finished run marked while accepted prompts are still pending", () => Effect.gen(function* () { - const { sessions, control, sessionId } = yield* setup; + const { threads, control, sessionId, threadId } = yield* setup; const admitted = yield* control.prompt({ sessionId, prompt: prompt(text) }); - yield* sessions.endExecution(sessionId, "finished"); - expect(yield* executionOf(sessionId)).toBe("running"); + yield* threads.endExecution(threadId, "finished"); + expect(yield* executionOf(threadId)).toBe("running"); // Cancelling leaves the mark alone; the run that next ends with nothing pending clears it. - yield* control.cancel(sessionId, admitted.id); - expect(yield* executionOf(sessionId)).toBe("running"); - yield* sessions.endExecution(sessionId, "finished"); - expect(yield* executionOf(sessionId)).toBeNull(); + yield* control.cancel(threadId, admitted.id); + expect(yield* executionOf(threadId)).toBe("running"); + yield* threads.endExecution(threadId, "finished"); + expect(yield* executionOf(threadId)).toBeNull(); })); it("keeps a closed run, and clears a stopped one unless a prompt came after the stop", () => Effect.gen(function* () { - const { sessions, control, sessionId } = yield* setup; + const { threads, control, sessionId, threadId } = yield* setup; const events = yield* Event.Service; yield* control.prompt({ sessionId, prompt: prompt("Parked by the stop") }); - yield* sessions.endExecution(sessionId, "closed"); - expect(yield* executionOf(sessionId)).toBe("closed"); + yield* threads.endExecution(threadId, "closed"); + expect(yield* executionOf(threadId)).toBe("closed"); // Sent while the stop was being cleaned up: its wake survives, so the mark must too. const stop = yield* events.latestSequence(sessionId); yield* control.prompt({ sessionId, prompt: prompt("Sent after the stop") }); - yield* sessions.endExecution(sessionId, "stopped", stop); - expect(yield* executionOf(sessionId)).toBe("running"); + yield* threads.endExecution(threadId, "stopped", stop); + expect(yield* executionOf(threadId)).toBe("running"); - yield* sessions.endExecution(sessionId, "stopped", yield* events.latestSequence(sessionId)); - expect(yield* executionOf(sessionId)).toBeNull(); + yield* threads.endExecution(threadId, "stopped", yield* events.latestSequence(sessionId)); + expect(yield* executionOf(threadId)).toBeNull(); })); }); diff --git a/packages/harness/test/db/db.test.ts b/packages/harness/test/db/db.test.ts index 3008e658..f058ecae 100644 --- a/packages/harness/test/db/db.test.ts +++ b/packages/harness/test/db/db.test.ts @@ -49,7 +49,7 @@ const queries = (sql: SqlClient.SqlClient) => ({ }), }); -// Spaces are foreign-keyed to sandbox_instance, so a namespace has to exist +// Spaces are foreign-keyed to sandbox, so a namespace has to exist // before anything can claim to live in it. Registering here keeps these tests // about the schema rather than about the Controller. const instanceId = (id: string) => SandboxInstance.ID.make(id); @@ -81,6 +81,38 @@ const registerInstances = (...ids: ReadonlyArray) => }); describe("Database", () => { + describe("migrations", () => { + memoryIt( + "applies the one threads migration and creates its tables", + Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + const applied = yield* sql<{ migrationId: number; name: string }>` + SELECT migration_id, name FROM effect_sql_migrations ORDER BY migration_id + `; + expect(applied.map((row) => `${row.migrationId}_${row.name}`)).toEqual(["202610290001_init"]); + const tables = yield* sql<{ name: string }>` + SELECT name FROM sqlite_master + WHERE type = 'table' AND name NOT LIKE 'sqlite_%' AND name <> 'effect_sql_migrations' + ORDER BY name + `; + expect(tables.map((row) => row.name)).toEqual([ + "document", + "entry", + "event", + "inbox", + "part", + "project", + "revision", + "sandbox", + "sequence", + "session", + "space", + "thread", + ]); + }), + ); + }); + describe("models", () => { it( "enforces foreign keys, one row per place, and one primary per env", diff --git a/packages/harness/test/event.log.test.ts b/packages/harness/test/event.log.test.ts index b88e6213..92581048 100644 --- a/packages/harness/test/event.log.test.ts +++ b/packages/harness/test/event.log.test.ts @@ -6,12 +6,14 @@ import { EventList } from "../src/event/list.ts"; import { EventSchema } from "../src/event/schema.ts"; import { SessionMessageSchema } from "../src/session/message/schema.ts"; import { SessionSchema } from "../src/session/schema.ts"; +import { ThreadSchema } from "../src/thread/schema.ts"; import { testEffect } from "./utils/effect.ts"; const layer = Layer.provideMerge(Event.layer, Database.layer(":memory:")); const { effect: it, live: liveIt } = testEffect(layer); const A = SessionSchema.ID.make("session_a"); const B = SessionSchema.ID.make("session_b"); +const threadId = ThreadSchema.ID.make("thread_a"); const messageId = (name: string) => SessionMessageSchema.ID.make(name); @@ -20,7 +22,7 @@ const turns = (events: Event.Interface, sessionId: SessionSchema.ID, names: Read const timestamp = yield* DateTime.now; yield* Effect.forEach( names, - (name) => events.publish(EventList.TurnEnded, { timestamp, sessionId, messageId: messageId(name) }), + (name) => events.publish(EventList.TurnEnded, { timestamp, sessionId, threadId, messageId: messageId(name) }), { discard: true }, ); }); @@ -67,7 +69,7 @@ describe("Event.log", () => { expect(items[0]).toEqual({ type: "log.synced", aggregateId: A }); expect(names(items)).toEqual(expected); expect(items.flatMap((item) => (Event.isSynced(item) ? [] : [item.durable?.seq]))).toEqual( - expected.map((_, index) => index), + expected.map((_, index) => index + 1), ); }), ); @@ -123,7 +125,7 @@ describe("Event.log", () => { yield* Fiber.join(interruption); const exit = yield* Fiber.await(publisher); expect(Exit.isFailure(exit) && Cause.hasInterrupts(exit.cause)).toBe(true); - expect(yield* events.latestSequence(A)).toBe(0); + expect(yield* events.latestSequence(A)).toBe(1); expect(names(Array.from(yield* events.log({ aggregateId: A }).pipe(Stream.runCollect)))).toEqual([ "cancelled", ]); @@ -139,7 +141,7 @@ describe("Event.log", () => { const items = Array.from(yield* events.log({ aggregateId: A }).pipe(Stream.runCollect)); expect(names(items)).toEqual(["a0", "a1"]); - expect(items.at(-1)).toEqual({ type: "log.synced", aggregateId: A, seq: 1 }); + expect(items.at(-1)).toEqual({ type: "log.synced", aggregateId: A, seq: 2 }); })); liveIt("appends commits after the marker and never live-only events", () => @@ -155,6 +157,7 @@ describe("Event.log", () => { yield* events.publish(EventList.LLMTextDelta, { timestamp, sessionId: A, + threadId, messageId: messageId("live"), partIndex: 0, delta: "token", @@ -165,7 +168,7 @@ describe("Event.log", () => { const items = Array.from(yield* Fiber.join(fiber)); expect(names(items)).toEqual(["replayed", "tailed"]); - expect(items[1]).toEqual({ type: "log.synced", aggregateId: A, seq: 0 }); + expect(items[1]).toEqual({ type: "log.synced", aggregateId: A, seq: 1 }); }), ); @@ -307,7 +310,7 @@ describe("Event.log", () => { readonly durable?: { readonly aggregateId: string; readonly seq: number }; }; expect(first.durable?.aggregateId).toBe(topic); - expect(first.durable?.seq).toBe(0); + expect(first.durable?.seq).toBe(1); })); it("pages custom definitions across many pages and resumes from a cursor", () => @@ -335,12 +338,12 @@ describe("Event.log", () => { // Sequences are contiguous across the page boundaries, so nothing was // skipped or re-read where one page hands over to the next. const seqs = all.flatMap((item) => (Event.isSynced(item) ? [] : [item.durable?.seq])); - expect(seqs).toEqual(Array.from({ length: total }, (_, index) => index)); + expect(seqs).toEqual(Array.from({ length: total }, (_, index) => index + 1)); // And a cursor resumes mid-stream rather than replaying from the start. const resumed = Array.from( yield* events - .log({ aggregateId: topic, after: 199, definitions: foreignDefinitions }) + .log({ aggregateId: topic, after: 200, definitions: foreignDefinitions }) .pipe(Stream.runCollect), ); const tail = resumed.flatMap((item) => diff --git a/packages/harness/test/event.subscribe.test.ts b/packages/harness/test/event.subscribe.test.ts index f2f381d7..c1e80142 100644 --- a/packages/harness/test/event.subscribe.test.ts +++ b/packages/harness/test/event.subscribe.test.ts @@ -5,16 +5,19 @@ import { Event } from "../src/event/event.ts"; import { EventList } from "../src/event/list.ts"; import { SessionMessageSchema } from "../src/session/message/schema.ts"; import { SessionSchema } from "../src/session/schema.ts"; +import { ThreadSchema } from "../src/thread/schema.ts"; import { testEffect } from "./utils/effect.ts"; const { live: it } = testEffect(Layer.provideMerge(Event.layer, Database.layer(":memory:"))); const sessionId = SessionSchema.ID.make("session_a"); +const threadId = ThreadSchema.ID.make("thread_a"); const publish = (events: Event.Interface, index: number) => Effect.gen(function* () { const timestamp = yield* DateTime.now; return yield* events.publish(EventList.TurnEnded, { timestamp, sessionId, + threadId, messageId: SessionMessageSchema.ID.make(`message_${index}`), }); }); @@ -34,6 +37,7 @@ describe("Event.subscribe", () => { const ephemeral = yield* events.publish(EventList.LLMTextDelta, { timestamp, sessionId, + threadId, messageId: SessionMessageSchema.ID.make("message_live"), partIndex: 0, delta: "hello", diff --git a/packages/harness/test/event.test.ts b/packages/harness/test/event.test.ts index 28e01fab..0ca46f16 100644 --- a/packages/harness/test/event.test.ts +++ b/packages/harness/test/event.test.ts @@ -38,7 +38,7 @@ const rowsFor = (sql: SqlClient.SqlClient, aggId: string) => sql`SELECT * FROM event WHERE aggregate_id = ${aggId} ORDER BY seq`; describe("Event store", () => { - it("assigns sequences from 0, independently per aggregate", () => + it("assigns sequences from 1, independently per aggregate", () => Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; const events = yield* Event.Service; @@ -47,13 +47,13 @@ describe("Event store", () => { const second = yield* events.publish(Msg, { aggId: A, text: "two" }); const other = yield* events.publish(Msg, { aggId: B, text: "elsewhere" }); - expect(first.durable?.seq).toBe(0); - expect(second.durable?.seq).toBe(1); - expect(other.durable?.seq).toBe(0); - expect((yield* rowsFor(sql, A)).map((r) => r.seq)).toEqual([0, 1]); - expect(yield* events.latestSequence(A)).toBe(1); - expect(yield* events.latestSequence(B)).toBe(0); - expect(yield* events.latestSequence("agg_never_written")).toBe(-1); + expect(first.durable?.seq).toBe(1); + expect(second.durable?.seq).toBe(2); + expect(other.durable?.seq).toBe(1); + expect((yield* rowsFor(sql, A)).map((r) => r.seq)).toEqual([1, 2]); + expect(yield* events.latestSequence(A)).toBe(2); + expect(yield* events.latestSequence(B)).toBe(1); + expect(yield* events.latestSequence("agg_never_written")).toBe(0); })); // An id is unique across the whole store, not per aggregate: reusing one @@ -70,7 +70,7 @@ describe("Event store", () => { expect(exit._tag).toBe("Failure"); expect((yield* rowsFor(sql, B)).length).toBe(0); // The rejected publish consumed no sequence at the other aggregate. - expect(yield* events.latestSequence(B)).toBe(-1); + expect(yield* events.latestSequence(B)).toBe(0); })); // The single most important property of the store: a projector that rejects @@ -97,9 +97,9 @@ describe("Event store", () => { expect((yield* sql`SELECT value FROM event_probe`).map((r) => r.value)).toEqual(["kept"]); expect((yield* rowsFor(sql, A)).length).toBe(1); // The sequence rolled back too, so the number is handed out again. - expect(yield* events.latestSequence(A)).toBe(0); + expect(yield* events.latestSequence(A)).toBe(1); reject = false; - expect((yield* events.publish(Msg, { aggId: A, text: "next" })).durable?.seq).toBe(1); + expect((yield* events.publish(Msg, { aggId: A, text: "next" })).durable?.seq).toBe(2); })); it("hands the committed sequence to projectors", () => @@ -110,7 +110,7 @@ describe("Event store", () => { yield* events.publish(Msg, { aggId: A, text: "one" }); yield* events.publish(Msg, { aggId: A, text: "two" }); - expect(seen).toEqual([0, 1]); + expect(seen).toEqual([1, 2]); })); describe("advance", () => { @@ -140,8 +140,8 @@ describe("Event store", () => { yield* events.advance(A, 0); - expect(yield* events.latestSequence(A)).toBe(1); - expect((yield* events.publish(Msg, { aggId: A, text: "three" })).durable?.seq).toBe(2); + expect(yield* events.latestSequence(A)).toBe(2); + expect((yield* events.publish(Msg, { aggId: A, text: "three" })).durable?.seq).toBe(3); })); }); diff --git a/packages/harness/test/fixtures/bash.spec.ts b/packages/harness/test/fixtures/bash.spec.ts index cdbc97a7..92977935 100644 --- a/packages/harness/test/fixtures/bash.spec.ts +++ b/packages/harness/test/fixtures/bash.spec.ts @@ -37,13 +37,13 @@ export const bashPluginSpec = (options: { driver: options.driver.registered.name, providerResourceId: yield* Effect.promise(options.resourceId), }); - const session = yield* Session.create({ + const { main: session } = yield* Session.create({ sandbox, directory: "/tmp", model: { provider: "openai", id: "gpt-5.6-luna" }, }); yield* session.run(`Use bash exactly once to execute this command: ${command}\nReport its output verbatim.`); - const path = yield* session.path(); + const path = yield* session.history(); const messages = yield* Effect.forEach(path, ContextCodec.decodeMessage); expect(path.every((entry) => entry.entry.state === "committed")).toBe(true); const calls = messages.flatMap((message) => message.parts.filter((part) => part.type === "toolCall")); diff --git a/packages/harness/test/fixtures/instruction.spec.ts b/packages/harness/test/fixtures/instruction.spec.ts index b1f7888a..6ff5cb66 100644 --- a/packages/harness/test/fixtures/instruction.spec.ts +++ b/packages/harness/test/fixtures/instruction.spec.ts @@ -155,16 +155,16 @@ export const scenario = (target: Target) => () => directory: path.posix.join(repo, "pkg"), hostDir: host.anchor, }); - yield* main.run("hello"); + yield* main.main.run("hello"); const nested = yield* Session.create({ ...on, directory: path.posix.join(worktree, "pkg"), hostDir: host.anchor, }); - yield* nested.run("hello"); + yield* nested.main.run("hello"); // Settings follow the host directory link; files never do. const linked = yield* Session.link({ sessionId: main.id, hostDir: host.off }); - yield* linked.run("again"); + yield* linked.main.run("again"); return base; }), ); @@ -222,16 +222,16 @@ export const relinkScenario = (drivers: ReadonlyArray) => () => directory: path.posix.join(onDaytona.repo, "pkg"), hostDir: host.anchor, }); - yield* session.run("on daytona"); + yield* session.main.run("on daytona"); const toVercel = yield* Session.relink({ sessionId: session.id, sandbox: vercel, directory: onVercel.repo, }); expect((yield* toVercel.info).directory).toBe(path.posix.join(onVercel.repo, "pkg")); - yield* toVercel.run("on vercel"); + yield* toVercel.main.run("on vercel"); const toHost = yield* Session.relink({ sessionId: session.id, directory: onHost.repo }); - yield* toHost.run("on host"); + yield* toHost.main.run("on host"); return [ [onDaytona.base, ""], [onVercel.base, ""], @@ -303,7 +303,7 @@ export const repositoryScenario = (drivers: ReadonlyArray) => () directory: path.posix.join(first.root, directories[0]), hostDir: host.anchor, }); - yield* moving.run("api"); + yield* moving.main.run("api"); for (const target of rest) { const moved = yield* Session.relink({ sessionId: moving.id, @@ -311,7 +311,7 @@ export const repositoryScenario = (drivers: ReadonlyArray) => () directory: target.root, }); expect((yield* moved.info).directory.endsWith(`/${directories[0]}`)).toBe(true); - yield* moved.run("api"); + yield* moved.main.run("api"); } for (const directory of directories.slice(1)) for (const target of targets) { @@ -320,7 +320,7 @@ export const repositoryScenario = (drivers: ReadonlyArray) => () directory: path.posix.join(target.root, directory), hostDir: host.anchor, }); - yield* session.run(directory); + yield* session.main.run(directory); } return targets.map((target) => target.root); }), diff --git a/packages/harness/test/fixtures/remote.spec.ts b/packages/harness/test/fixtures/remote.spec.ts index 2346a7b3..231d92d4 100644 --- a/packages/harness/test/fixtures/remote.spec.ts +++ b/packages/harness/test/fixtures/remote.spec.ts @@ -12,6 +12,7 @@ import { SandboxIO } from "../../src/sandbox/io.ts"; import { Sandbox } from "../../src/sandbox/sandbox.ts"; import { type ISandboxExe, Shell } from "../../src/sandbox/shell/shell.ts"; import { SandboxStore } from "../../src/sandbox/store.ts"; +import { SessionLive } from "../../src/session/live.ts"; import { Session } from "../../src/session/session.ts"; import { Hash } from "../../src/util/hash.ts"; @@ -166,7 +167,7 @@ export const remoteSandboxSpec = (options: RemoteSandboxSpecOptions) => { ), ); const database = Database.layer(":memory:"); - // Spaces are foreign-keyed to sandbox_instance, so the remote + // Spaces are foreign-keyed to sandbox, so the remote // namespace has to be registered before anything claims to live // in it — and before Location resolves the repo into a space. const registered = Layer.effectDiscard( @@ -182,7 +183,7 @@ export const remoteSandboxSpec = (options: RemoteSandboxSpecOptions) => { const location = Location.layerMounted().pipe(Layer.provide(sandbox), Layer.provide(registered)); // provideMerge, not provide: `registered` and the block below // share the one in-memory database. - const application = Layer.merge(location, Session.layer).pipe( + const application = Layer.merge(location, SessionLive.layer).pipe( Layer.provideMerge(Event.layer), Layer.provideMerge(database), ); @@ -195,7 +196,7 @@ export const remoteSandboxSpec = (options: RemoteSandboxSpecOptions) => { directory: resolved.directory, title: `${options.kind} integration`, }); - const space = Option.getOrThrow(yield* sessions.space(created.id)); + const space = Option.getOrThrow(yield* sessions.space(created.session.id)); return { space: { location: resolved.space.location, diff --git a/packages/harness/test/fixtures/runner.cycle.spec.ts b/packages/harness/test/fixtures/runner.cycle.spec.ts index 762afd5a..4d8e53f6 100644 --- a/packages/harness/test/fixtures/runner.cycle.spec.ts +++ b/packages/harness/test/fixtures/runner.cycle.spec.ts @@ -25,8 +25,9 @@ import { SandboxDriverRegistry } from "../../src/sandbox/registry.ts"; import * as VercelSandboxDriver from "../../src/sandboxes/vercel/index.ts"; import { SessionLive } from "../../src/session/live.ts"; import { SessionRuntime } from "../../src/session/runtime.ts"; -import type { SessionSchema } from "../../src/session/schema.ts"; import { Session } from "../../src/session/session.ts"; +import type { ThreadSchema } from "../../src/thread/schema.ts"; +import { Thread } from "../../src/thread/thread.ts"; import { builtins } from "../../src/plugin/builtin.ts"; import { State } from "../../src/state/state.ts"; import { pooled } from "./pool.ts"; @@ -74,7 +75,7 @@ const inputStateFor = (sql: SqlClient.SqlClient) => SELECT COUNT(*) AS count, SUM(CASE WHEN promoted_seq IS NULL THEN 1 ELSE 0 END) AS pending - FROM session_input + FROM inbox WHERE session_id = ${sessionId} `, }); @@ -91,9 +92,9 @@ const thinkingOf = (message: Message.UserMessage | Message.AssistantMessage): st .join("\n") .trim(); -const waitUntilIdle = (control: Control.Interface, sessionId: SessionSchema.ID) => +const waitUntilIdle = (control: Control.Interface, threadId: ThreadSchema.ID) => Effect.gen(function* () { - while ((yield* control.active).has(sessionId)) yield* Effect.sleep("10 millis"); + while ((yield* control.active).has(threadId)) yield* Effect.sleep("10 millis"); }).pipe(Effect.timeout("10 seconds")); const historyPrompts = [ @@ -140,6 +141,7 @@ export const runnerCycleSpec = (resourceId: () => Promise) => const context = yield* Context.Service; const events = yield* Event.Service; const sessions = yield* Session.Service; + const threads = yield* Thread.Service; const sandboxes = yield* SandboxController.Controller; const sandboxInstanceId = (yield* sandboxes.register({ @@ -149,7 +151,7 @@ export const runnerCycleSpec = (resourceId: () => Promise) => })).id; const { spaceId, location } = yield* seedSpace({ location: "/tmp", env: sandboxInstanceId }); - const session = yield* sessions.create({ + const { session, thread } = yield* sessions.create({ spaceId, directory: location, title: "Live runner cycle", @@ -186,13 +188,13 @@ export const runnerCycleSpec = (resourceId: () => Promise) => yield* Effect.addFinalizer(() => removeTerminalListener); const readLastAssistant = Effect.fnUntraced(function* () { - const path = yield* sessions.path(session.id); + const path = yield* threads.line(thread.id); const last = path.at(-1); if (last === undefined) - return yield* Effect.die("session path is empty after an LLM terminal event"); + return yield* Effect.die("thread line is empty after an LLM terminal event"); const message = yield* ContextCodec.decodeMessage(last); if (message.role !== "assistant") { - return yield* Effect.die(`last session entry is ${message.role}, not assistant`); + return yield* Effect.die(`last thread entry is ${message.role}, not assistant`); } return message; }); @@ -203,7 +205,7 @@ export const runnerCycleSpec = (resourceId: () => Promise) => prompt: { parts: [{ type: "text", text: prompt }] }, }); const terminal = yield* Queue.take(terminals).pipe(Effect.timeout("75 seconds")); - yield* waitUntilIdle(control, session.id); + yield* waitUntilIdle(control, thread.id); expect(terminal).toBe("ended"); return yield* readLastAssistant(); }); @@ -214,7 +216,7 @@ export const runnerCycleSpec = (resourceId: () => Promise) => const thinking = thinkingOf(assistant); if (thinking.length > 0) persistedThinking.push(thinking); expect(textOf(assistant).length).toBeGreaterThan(0); - const path = yield* sessions.path(session.id); + const path = yield* threads.line(thread.id); expect(path.map((item) => item.entry.type)).toEqual( Array.from({ length: (index + 1) * 2 }, (_, position) => position % 2 === 0 ? "user" : "assistant", @@ -254,12 +256,12 @@ export const runnerCycleSpec = (resourceId: () => Promise) => prompt: { parts: [{ type: "text", text: storyPrompt }] }, }); const firstTextDelta = yield* Queue.take(textDeltas).pipe(Effect.timeout("75 seconds")); - yield* control.interrupt(session.id); + yield* control.interrupt(thread.id); const interruptedTerminal = yield* Queue.take(terminals).pipe(Effect.timeout("10 seconds")); expect(interruptedTerminal).toBe("failed"); expect(Array.from(yield* control.active)).toEqual([]); - const interruptedPath = yield* sessions.path(session.id); + const interruptedPath = yield* threads.line(thread.id); expect(interruptedPath.map((item) => item.entry.type)).toEqual( Array.from({ length: 12 }, (_, position) => (position % 2 === 0 ? "user" : "assistant")), ); @@ -298,11 +300,11 @@ export const runnerCycleSpec = (resourceId: () => Promise) => expect(requests.at(-1)?.effort).toBe("none"); expect(requests.at(-1)?.headers).toMatchObject({ "x-settings-revision": "off" }); - const finalPath = yield* sessions.path(session.id); + const finalPath = yield* threads.line(thread.id); expect(finalPath.map((item) => item.entry.type)).toEqual( Array.from({ length: 14 }, (_, position) => (position % 2 === 0 ? "user" : "assistant")), ); - const snapshot = yield* context.assemble(session.id); + const snapshot = yield* context.assemble(thread.id); expect(snapshot.messages).toHaveLength(13); expect(snapshot.messages.some((message) => message.messageId === aborted.messageId)).toBe(false); expect(snapshot.messages[12]).toMatchObject({ role: "assistant", stopReason: "stop" }); @@ -310,7 +312,7 @@ export const runnerCycleSpec = (resourceId: () => Promise) => const inputState = yield* inputStateFor(sql)(session.id); expect(inputState).toEqual({ count: 7, pending: 0 }); const finalEventTypes = yield* eventTypesFor(sql)(session.id); - expect(finalEventTypes).toHaveLength(34); + expect(finalEventTypes).toHaveLength(35); expect(finalEventTypes.at(-1)?.type).toBe("session.turn.ended.1"); yield* Effect.logInfo("live recovery response", { turn: 7, diff --git a/packages/harness/test/fixtures/skill.spec.ts b/packages/harness/test/fixtures/skill.spec.ts index 8d349f33..ad2c0b0e 100644 --- a/packages/harness/test/fixtures/skill.spec.ts +++ b/packages/harness/test/fixtures/skill.spec.ts @@ -185,16 +185,16 @@ export const scenario = (target: Target) => () => directory: path.posix.join(repo, "pkg"), hostDir: host.anchor, }); - yield* main.run("hello"); + yield* main.main.run("hello"); const nested = yield* Session.create({ ...on, directory: path.posix.join(worktree, "pkg"), hostDir: host.anchor, }); - yield* nested.run("hello"); + yield* nested.main.run("hello"); // Settings follow the host directory link; skill files never do. const linked = yield* Session.link({ sessionId: main.id, hostDir: host.off }); - yield* linked.run("again"); + yield* linked.main.run("again"); return base; }), ); @@ -257,16 +257,16 @@ export const relinkScenario = (drivers: ReadonlyArray) => () => directory: path.posix.join(onDaytona.repo, "pkg"), hostDir: host.anchor, }); - yield* session.run("on daytona"); + yield* session.main.run("on daytona"); const toVercel = yield* Session.relink({ sessionId: session.id, sandbox: vercel, directory: onVercel.repo, }); expect((yield* toVercel.info).directory).toBe(path.posix.join(onVercel.repo, "pkg")); - yield* toVercel.run("on vercel"); + yield* toVercel.main.run("on vercel"); const toHost = yield* Session.relink({ sessionId: session.id, directory: onHost.repo }); - yield* toHost.run("on host"); + yield* toHost.main.run("on host"); return [ [onDaytona.base, ""], [onVercel.base, ""], diff --git a/packages/harness/test/plugin.examples.test.ts b/packages/harness/test/plugin.examples.test.ts index 405b78ac..99c7bf78 100644 --- a/packages/harness/test/plugin.examples.test.ts +++ b/packages/harness/test/plugin.examples.test.ts @@ -25,9 +25,9 @@ const example = (name: string) => fileURLToPath(new URL(`../../../extras/${name} const run = (root: string, plugins: ReadonlyArray, llm = immediateOpen()) => { const contexts: Message.Context[] = []; return Effect.gen(function* () { - const session = yield* Session.create({ directory: root }); + const { main: session } = yield* Session.create({ directory: root }); yield* session.run("hello"); - return { contexts, path: yield* session.path() } as const; + return { contexts, path: yield* session.history() } as const; }).pipe( Effect.provide( Harness.layer({ diff --git a/packages/harness/test/plugin.external.test.ts b/packages/harness/test/plugin.external.test.ts index 475ce2f9..c3865f87 100644 --- a/packages/harness/test/plugin.external.test.ts +++ b/packages/harness/test/plugin.external.test.ts @@ -42,10 +42,10 @@ const exchange = (input: { return Effect.gen(function* () { // The session is linked to the project under test through `hostDir`, which is where its // settings come from. Its local sandbox works in `root` (`cwd`), which configures nothing. - const session = yield* Session.create({ directory: input.root, hostDir: input.hostDir ?? input.root }); + const { main: session } = yield* Session.create({ directory: input.root, hostDir: input.hostDir ?? input.root }); yield* session.run(input.prompt ?? "hello"); // Read inside the scope: the memory database closes with the layer. - const path = yield* session.path(); + const path = yield* session.history(); return { contexts, prompts, path } as const; }).pipe( Effect.provide( @@ -193,17 +193,17 @@ describe("third-party plugins", () => { Effect.gen(function* () { const events = yield* Event.Service; yield* events.listen((event) => Effect.sync(() => void seen.push(event.type))); - const session = yield* Session.create({ directory: root }); + const { main: session } = yield* Session.create({ directory: root }); yield* session.run("hello"); // The row lands on the session aggregate; the kernel manifest skips it, // the plugin's own definitions decode it. Read inside the scope: the // memory database closes with the layer. const rows = Array.from( yield* events - .log({ aggregateId: session.id, definitions: EventSchema.durable([Marker]) }) + .log({ aggregateId: session.sessionId, definitions: EventSchema.durable([Marker]) }) .pipe(Stream.runCollect), ); - return { rows, sessionId: session.id }; + return { rows, sessionId: session.sessionId }; }).pipe( Effect.provide( Harness.layer({ @@ -230,7 +230,7 @@ describe("third-party plugins", () => { const contexts: Message.Context[] = []; const failure = await Effect.runPromise( Effect.gen(function* () { - const session = yield* Session.create({ directory: root }); + const { main: session } = yield* Session.create({ directory: root }); yield* session.prompt("hello"); return yield* session.resume().pipe(Effect.flip); }).pipe( @@ -259,7 +259,7 @@ describe("third-party plugins", () => { withSettings(async ({ root }) => { const failure = await Effect.runPromise( Effect.gen(function* () { - const session = yield* Session.create({ directory: root }); + const { main: session } = yield* Session.create({ directory: root }); yield* session.prompt("hello"); return yield* session.resume().pipe(Effect.flip); }).pipe( @@ -304,7 +304,7 @@ describe("third-party plugins", () => { const contexts: Message.Context[] = []; const exit = await Effect.runPromise( Effect.gen(function* () { - const session = yield* Session.create({ directory: root }); + const { main: session } = yield* Session.create({ directory: root }); yield* session.prompt("hello"); const running = yield* session.resume().pipe(Effect.forkChild); // Give the drain time to reach setup and block there, then pull the plug. diff --git a/packages/harness/test/plugin.foreign.test.ts b/packages/harness/test/plugin.foreign.test.ts index 33218746..17805ebd 100644 --- a/packages/harness/test/plugin.foreign.test.ts +++ b/packages/harness/test/plugin.foreign.test.ts @@ -53,9 +53,9 @@ describe("plugins built against another Effect instance", () => { const contexts: Message.Context[] = []; const { path } = await Effect.runPromise( Effect.gen(function* () { - const session = yield* Session.create({ directory: root }); + const { main: session } = yield* Session.create({ directory: root }); yield* session.run("hello"); - return { path: yield* session.path() } as const; + return { path: yield* session.history() } as const; }).pipe( Effect.provide( Harness.layer({ diff --git a/packages/harness/test/plugin.git.live.test.ts b/packages/harness/test/plugin.git.live.test.ts index f3450b12..664510e3 100644 --- a/packages/harness/test/plugin.git.live.test.ts +++ b/packages/harness/test/plugin.git.live.test.ts @@ -71,9 +71,9 @@ const exchange = (workspace: Workspace, open?: LLM.Open) => { const contexts: Message.Context[] = []; const turn = open ?? toolTurn(); return Effect.gen(function* () { - const session = yield* Session.create({ directory: workspace.root, hostDir: workspace.root }); + const { main: session } = yield* Session.create({ directory: workspace.root, hostDir: workspace.root }); yield* session.run("hello"); - return { contexts, path: yield* session.path() } as const; + return { contexts, path: yield* session.history() } as const; }).pipe( Effect.provide( Harness.layer({ diff --git a/packages/harness/test/plugin.hooks.test.ts b/packages/harness/test/plugin.hooks.test.ts index 2940c28f..6f4fae4e 100644 --- a/packages/harness/test/plugin.hooks.test.ts +++ b/packages/harness/test/plugin.hooks.test.ts @@ -7,10 +7,15 @@ import * as Tool from "../src/tool/tool.ts"; import type { ToolAddOptions, ToolAfter } from "../src/plugin/tool/schema.ts"; import { SessionSchema } from "../src/session/schema.ts"; import { SessionMessageSchema } from "../src/session/message/schema.ts"; +import { ThreadSchema } from "../src/thread/schema.ts"; import { pendingCall } from "./tools.fixture.ts"; import { it } from "./utils/effect.ts"; -const options = { sessionId: SessionSchema.ID.create(), messageId: SessionMessageSchema.ID.create() }; +const options = { + sessionId: SessionSchema.ID.create(), + threadId: ThreadSchema.ID.create(), + messageId: SessionMessageSchema.ID.create(), +}; const call = pendingCall("echo", { value: "hello" }); const tool = (handler: () => Effect.Effect = () => Effect.succeed("hello")) => Tool.register( diff --git a/packages/harness/test/plugin.host.test.ts b/packages/harness/test/plugin.host.test.ts index a176dc1e..10a8b1a5 100644 --- a/packages/harness/test/plugin.host.test.ts +++ b/packages/harness/test/plugin.host.test.ts @@ -62,7 +62,7 @@ describe("plugin domains and exchange host", () => { const open = immediateOpen(); await Effect.runPromise( Effect.gen(function* () { - const session = yield* Session.create({ + const { main: session } = yield* Session.create({ directory: root, systemPrompt: { custom: async (ctx) => `custom:${ctx.plugin.tools.list().length}`, @@ -133,7 +133,7 @@ describe("plugin domains and exchange host", () => { }; const failure = await Effect.runPromise( Effect.gen(function* () { - const session = yield* Session.create({ directory: root }); + const { main: session } = yield* Session.create({ directory: root }); yield* session.run("hello"); }).pipe( Effect.provide( @@ -159,13 +159,13 @@ describe("plugin domains and exchange host", () => { let setups = 0; await Effect.runPromise( Effect.gen(function* () { - const session = yield* Session.create({ + const { main: session } = yield* Session.create({ directory: root, model: { provider: "openai", id: "no-such-model" }, }); yield* session.run("hello"); expect(setups).toBe(0); - expect(yield* session.path()).toEqual([]); + expect(yield* session.history()).toEqual([]); }).pipe( Effect.provide( Harness.layer({ @@ -204,7 +204,7 @@ describe("plugin domains and exchange host", () => { let requested = false; await Effect.runPromise( Effect.gen(function* () { - const session = yield* Session.create({ directory: root }); + const { main: session } = yield* Session.create({ directory: root }); yield* session.run("hello"); expect(later).toBe(false); expect(requested).toBe(false); @@ -251,7 +251,7 @@ describe("plugin domains and exchange host", () => { const entered = yield* Deferred.make(); const release = yield* Deferred.make(); yield* Effect.gen(function* () { - const session = yield* Session.create({ directory: root }); + const { main: session } = yield* Session.create({ directory: root }); const fiber = yield* session.run("hello").pipe(Effect.forkChild); yield* Deferred.await(entered); yield* session.interrupt(); @@ -310,7 +310,7 @@ describe("plugin pipelines in the kernel loop", () => { }), ); yield* Effect.gen(function* () { - const session = yield* Session.create({ directory: root, tools: { execution: mode } }); + const { main: session } = yield* Session.create({ directory: root, tools: { execution: mode } }); const run = yield* session.run("hello").pipe(Effect.forkChild); yield* Deferred.await(firstAfter); if (mode === "parallel") yield* Deferred.await(secondAfter); @@ -325,7 +325,7 @@ describe("plugin pipelines in the kernel loop", () => { `${id}:done`, ]); if (mode === "sequential") expect(trace.indexOf("a:done")).toBeLessThan(trace.indexOf("b:before")); - const path = yield* session.path(); + const path = yield* session.history(); expect(path).toHaveLength(3); }).pipe( Effect.provide( diff --git a/packages/harness/test/plugin.instruction.test.ts b/packages/harness/test/plugin.instruction.test.ts index ee159bb2..096e5b42 100644 --- a/packages/harness/test/plugin.instruction.test.ts +++ b/packages/harness/test/plugin.instruction.test.ts @@ -37,7 +37,7 @@ const prompt = async (input: { root: string; directory: string; hostCwd: string; }); await Effect.runPromise( Effect.gen(function* () { - const session = yield* Session.create({ directory: input.directory }); + const { main: session } = yield* Session.create({ directory: input.directory }); yield* session.run("hello"); }).pipe( Effect.provide( diff --git a/packages/harness/test/plugin.passes.test.ts b/packages/harness/test/plugin.passes.test.ts index 05b69a80..a32ba1e8 100644 --- a/packages/harness/test/plugin.passes.test.ts +++ b/packages/harness/test/plugin.passes.test.ts @@ -86,7 +86,7 @@ describe("a running session", () => { const open = immediateOpen(); await Effect.runPromise( Effect.gen(function* () { - const session = yield* Session.create({ directory: project, hostDir: project }); + const { main: session } = yield* Session.create({ directory: project, hostDir: project }); // `prompt` admits, `resume` drains, `wait` blocks until the exchange is done -- // which this test needs, so the edit lands strictly between two of them. yield* session.prompt({ text: "one", delivery: "followUp" }); @@ -261,7 +261,7 @@ describe("a running session and the store", () => { const open = immediateOpen(); await Effect.runPromise( Effect.gen(function* () { - const session = yield* Session.create({ directory: project, hostDir: project }); + const { main: session } = yield* Session.create({ directory: project, hostDir: project }); yield* session.prompt({ text: "one", delivery: "followUp" }); yield* session.resume(); yield* session.wait(); @@ -338,7 +338,7 @@ describe("boot", () => { const open = immediateOpen(); await Effect.runPromise( Effect.gen(function* () { - const session = yield* Session.create({ directory: project, hostDir: project }); + const { main: session } = yield* Session.create({ directory: project, hostDir: project }); yield* session.prompt({ text: "go", delivery: "followUp" }); yield* session.resume(); yield* session.wait(); @@ -396,7 +396,7 @@ describe("boot", () => { const open = immediateOpen(); await Effect.runPromise( Effect.gen(function* () { - const session = yield* Session.create({ directory: plain, hostDir: plain }); + const { main: session } = yield* Session.create({ directory: plain, hostDir: plain }); yield* session.prompt({ text: "go", delivery: "followUp" }); yield* session.resume(); yield* session.wait(); @@ -450,7 +450,7 @@ describe("reload", () => { const open = immediateOpen(); return Effect.runPromise( Effect.gen(function* () { - const created = yield* Session.create({ directory: input.project, hostDir: input.project }); + const { main: created } = yield* Session.create({ directory: input.project, hostDir: input.project }); const state = yield* State.Service; const run = () => created @@ -693,10 +693,10 @@ describe("linking a session to a host directory", () => { Effect.gen(function* () { // No host directory: this session has no project layer, which is a normal // state rather than a missing value. - const created = yield* Session.create({ directory: project }); + const { main: created } = yield* Session.create({ directory: project }); // The key is absent rather than undefined, which is exactly why callers get to // ask the question by name instead of knowing that. - expect((yield* created.info).hasHostLink).toBe(false); + expect((yield* created.info).session.hasHostLink).toBe(false); const run = () => created @@ -704,8 +704,8 @@ describe("linking a session to a host directory", () => { .pipe(Effect.andThen(created.resume()), Effect.andThen(created.wait())); yield* run(); - yield* Session.link({ sessionId: created.id, hostDir: project }); - expect(yield* created.info).toMatchObject({ hostDir: project, hasHostLink: true }); + yield* Session.link({ sessionId: created.sessionId, hostDir: project }); + expect((yield* created.info).session).toMatchObject({ hostDir: project, hasHostLink: true }); // Settings are re-read every exchange, so the project layer simply starts // being read -- and its plugin is local, so its bytes were always here. diff --git a/packages/harness/test/plugin.prompt.test.ts b/packages/harness/test/plugin.prompt.test.ts index 64128d08..887c68c7 100644 --- a/packages/harness/test/plugin.prompt.test.ts +++ b/packages/harness/test/plugin.prompt.test.ts @@ -28,9 +28,9 @@ describe("codework.prompt.default", () => { const open = immediateOpen(); return Effect.runPromise( Effect.gen(function* () { - const handle = yield* Session.create({ directory: root, ...session }); + const { main: handle } = yield* Session.create({ directory: root, ...session }); yield* handle.run("hello"); - return yield* handle.path(); + return yield* handle.history(); }).pipe( Effect.provide( Harness.layer({ diff --git a/packages/harness/test/plugin.skill.test.ts b/packages/harness/test/plugin.skill.test.ts index e8b756d4..5400d35b 100644 --- a/packages/harness/test/plugin.skill.test.ts +++ b/packages/harness/test/plugin.skill.test.ts @@ -49,7 +49,7 @@ const prompt = async (input: { }); await Effect.runPromise( Effect.gen(function* () { - const session = yield* Session.create({ directory: input.directory }); + const { main: session } = yield* Session.create({ directory: input.directory }); yield* session.run("hello"); }).pipe( Effect.provide( @@ -376,7 +376,7 @@ describe("codework.prompt.skill", () => { const requests: Array<{ systemPrompt: string; messages: unknown }> = []; await Effect.runPromise( Effect.gen(function* () { - const session = yield* Session.create({ directory: dir }); + const { main: session } = yield* Session.create({ directory: dir }); yield* session.run("extract report.pdf"); }).pipe( Effect.provide( diff --git a/packages/harness/test/project.test.ts b/packages/harness/test/project.test.ts index 24aa516a..019f3d98 100644 --- a/packages/harness/test/project.test.ts +++ b/packages/harness/test/project.test.ts @@ -77,8 +77,8 @@ const seedSession = (id: string, space: string, directory: string) => Effect.flatMap( SqlClient.SqlClient, (sql) => sql` - INSERT INTO session (id, space_id, slug, directory, title, created_at, updated_at) - VALUES (${id}, ${space}, ${id}, ${directory}, ${id}, 0, 0) + INSERT INTO session (id, space_id, slug, directory, created_at, updated_at) + VALUES (${id}, ${space}, ${id}, ${directory}, 0, 0) `, ); diff --git a/packages/harness/test/runner.coordinator.test.ts b/packages/harness/test/runner.coordinator.test.ts index fb7659c7..47dda1be 100644 --- a/packages/harness/test/runner.coordinator.test.ts +++ b/packages/harness/test/runner.coordinator.test.ts @@ -22,11 +22,11 @@ describe("RunCoordinator", () => { const coordinator = yield* RunCoordinator.make({ drain: () => Deferred.succeed(entered, undefined).pipe(Effect.andThen(Effect.never)), }); - const joined = yield* coordinator.run("session").pipe(Effect.forkChild); + const joined = yield* coordinator.run("thread").pipe(Effect.forkChild); yield* Deferred.await(entered); yield* coordinator - .interrupt("session") - .pipe(Effect.andThen(coordinator.awaitIdle("session")), Effect.uninterruptible); + .interrupt("thread") + .pipe(Effect.andThen(coordinator.awaitIdle("thread")), Effect.uninterruptible); expect(Array.from(yield* coordinator.active)).toEqual([]); const exit = yield* Fiber.await(joined); expect(Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause)).toBe(true); @@ -49,9 +49,9 @@ describe("RunCoordinator", () => { }), }); - const first = yield* Effect.forkChild(coordinator.run("session")); + const first = yield* Effect.forkChild(coordinator.run("thread")); yield* Deferred.await(started); - const second = yield* Effect.forkChild(coordinator.run("session")); + const second = yield* Effect.forkChild(coordinator.run("thread")); yield* settle; // The second caller attached to the drain already in flight. @@ -80,8 +80,8 @@ describe("RunCoordinator", () => { ), }); - yield* coordinator.wake("session"); - const idle = yield* Effect.forkChild(coordinator.awaitIdle("session")); + yield* coordinator.wake("thread"); + const idle = yield* Effect.forkChild(coordinator.awaitIdle("thread")); yield* Deferred.await(started); expect(forces).toEqual([false]); expect(idle.pollUnsafe()).toBeUndefined(); @@ -109,10 +109,10 @@ describe("RunCoordinator", () => { }), }); - const running = yield* Effect.forkChild(coordinator.run("session")); + const running = yield* Effect.forkChild(coordinator.run("thread")); yield* Deferred.await(firstStarted); - yield* coordinator.wake("session"); - const idle = yield* Effect.forkChild(coordinator.awaitIdle("session")); + yield* coordinator.wake("thread"); + const idle = yield* Effect.forkChild(coordinator.awaitIdle("thread")); yield* Deferred.succeed(firstGate, undefined); yield* Deferred.await(secondStarted); expect(running.pollUnsafe()).toBeUndefined(); @@ -143,11 +143,11 @@ describe("RunCoordinator", () => { }), }); - const fiber = yield* Effect.forkChild(coordinator.run("session")); + const fiber = yield* Effect.forkChild(coordinator.run("thread")); yield* Deferred.await(started); - yield* coordinator.wake("session"); - yield* coordinator.wake("session"); - yield* coordinator.wake("session"); + yield* coordinator.wake("thread"); + yield* coordinator.wake("thread"); + yield* coordinator.wake("thread"); yield* Deferred.succeed(gate, undefined); yield* Fiber.join(fiber); yield* settle; @@ -176,11 +176,11 @@ describe("RunCoordinator", () => { settled: (_key, exit) => Effect.sync(() => void exits.push(exit)), }); - yield* coordinator.wake("session"); + yield* coordinator.wake("thread"); yield* Deferred.await(started); - yield* coordinator.wake("session"); + yield* coordinator.wake("thread"); yield* Deferred.succeed(gate, undefined); - yield* coordinator.awaitIdle("session"); + yield* coordinator.awaitIdle("thread"); expect(drains).toBe(2); expect(starts).toBe(1); @@ -240,12 +240,12 @@ describe("RunCoordinator", () => { }), }); - const fiber = yield* Effect.forkChild(coordinator.run("session")); + const fiber = yield* Effect.forkChild(coordinator.run("thread")); yield* Deferred.await(started); - yield* coordinator.wake("session"); // queue a follow-up... - expect(yield* coordinator.interrupt("session")).toBe(true); // ...which the interrupt discards + yield* coordinator.wake("thread"); // queue a follow-up... + expect(yield* coordinator.interrupt("thread")).toBe(true); // ...which the interrupt discards yield* Fiber.join(fiber).pipe(Effect.exit); - yield* coordinator.awaitIdle("session"); + yield* coordinator.awaitIdle("thread"); // Interruption is a stop, not a restart: no successor runs. expect(calls).toBe(1); @@ -268,9 +268,9 @@ describe("RunCoordinator", () => { ), }); - const first = yield* Effect.forkChild(coordinator.run("session")); + const first = yield* Effect.forkChild(coordinator.run("thread")); yield* Deferred.await(started); - const second = yield* Effect.forkChild(coordinator.run("session")); + const second = yield* Effect.forkChild(coordinator.run("thread")); yield* settle; yield* Deferred.succeed(gate, undefined); @@ -298,9 +298,9 @@ describe("RunCoordinator", () => { ), }); - yield* coordinator.wake("session"); + yield* coordinator.wake("thread"); yield* Deferred.await(started); - const idle = yield* Effect.forkChild(coordinator.awaitIdle("session")); + const idle = yield* Effect.forkChild(coordinator.awaitIdle("thread")); yield* Deferred.succeed(gate, undefined); yield* Fiber.join(idle); @@ -326,12 +326,12 @@ describe("RunCoordinator", () => { }), }); - const running = yield* Effect.forkChild(coordinator.run("session")); + const running = yield* Effect.forkChild(coordinator.run("thread")); yield* Deferred.await(started); // Ask to stop, then immediately ask to run again. - expect(yield* coordinator.interrupt("session")).toBe(true); - const resumed = yield* Effect.forkChild(coordinator.run("session")); + expect(yield* coordinator.interrupt("thread")).toBe(true); + const resumed = yield* Effect.forkChild(coordinator.run("thread")); yield* Fiber.join(running).pipe(Effect.exit); yield* Fiber.join(resumed); @@ -360,9 +360,9 @@ describe("RunCoordinator", () => { }), }); - const first = yield* Effect.forkChild(coordinator.run("session")); + const first = yield* Effect.forkChild(coordinator.run("thread")); yield* Deferred.await(started); - const second = yield* Effect.forkChild(coordinator.run("session")); + const second = yield* Effect.forkChild(coordinator.run("thread")); yield* settle; yield* Fiber.interrupt(second); @@ -403,9 +403,9 @@ describe("RunCoordinator", () => { const coordinator = yield* RunCoordinator.make({ drain: () => Deferred.succeed(started, undefined).pipe(Effect.andThen(Effect.never)), }); - yield* coordinator.wake("session"); + yield* coordinator.wake("thread"); yield* Deferred.await(started); - expect(Array.from(yield* coordinator.active)).toEqual(["session"]); + expect(Array.from(yield* coordinator.active)).toEqual(["thread"]); return coordinator; }), ); @@ -443,14 +443,14 @@ describe("RunCoordinator", () => { }), }); - yield* coordinator.wake("session"); + yield* coordinator.wake("thread"); yield* Deferred.await(firstStarted); - expect(yield* coordinator.interrupt("session")).toBe(true); + expect(yield* coordinator.interrupt("thread")).toBe(true); yield* Deferred.await(cleanupStarted); - yield* coordinator.wake("session"); // admitted mid-cleanup + yield* coordinator.wake("thread"); // admitted mid-cleanup yield* Deferred.succeed(cleanupGate, undefined); yield* Deferred.await(secondStarted); - yield* coordinator.awaitIdle("session"); + yield* coordinator.awaitIdle("thread"); expect(calls).toBe(2); }), @@ -474,15 +474,15 @@ describe("RunCoordinator", () => { settled: (_key, _exit, reason) => Effect.sync(() => void reasons.push(reason)), }); - yield* coordinator.wake("session"); + yield* coordinator.wake("thread"); yield* Deferred.await(started); - expect(yield* coordinator.interrupt("session", "user")).toBe(true); - expect(yield* coordinator.interrupt("session", "user")).toBe(false); + expect(yield* coordinator.interrupt("thread", "user")).toBe(true); + expect(yield* coordinator.interrupt("thread", "user")).toBe(false); expect(reasons).toEqual([]); - expect(Array.from(yield* coordinator.active)).toEqual(["session"]); + expect(Array.from(yield* coordinator.active)).toEqual(["thread"]); yield* Deferred.await(cleanupStarted); - const idle = yield* Effect.forkChild(coordinator.awaitIdle("session")); + const idle = yield* Effect.forkChild(coordinator.awaitIdle("thread")); yield* settle; expect(idle.pollUnsafe()).toBeUndefined(); yield* Deferred.succeed(cleanupGate, undefined); @@ -504,17 +504,17 @@ describe("RunCoordinator", () => { settled: () => Deferred.succeed(settling, undefined).pipe(Effect.andThen(Deferred.await(release))), }); - yield* coordinator.wake("session"); + yield* coordinator.wake("thread"); yield* Deferred.await(settling); - yield* coordinator.wake("session"); + yield* coordinator.wake("thread"); const interrupted = yield* coordinator - .interrupt("session", undefined, { awaitSettlement: true }) + .interrupt("thread", undefined, { awaitSettlement: true }) .pipe(Effect.forkChild); yield* settle; expect(interrupted.pollUnsafe()).toBeUndefined(); yield* Deferred.succeed(release, undefined); expect(yield* Fiber.join(interrupted)).toBe(false); - yield* coordinator.awaitIdle("session"); + yield* coordinator.awaitIdle("thread"); expect(drains).toBe(1); expect(Array.from(yield* coordinator.active)).toEqual([]); @@ -543,10 +543,10 @@ describe("RunCoordinator", () => { }), }); - const resumed = yield* Effect.forkChild(coordinator.run("session")); + const resumed = yield* Effect.forkChild(coordinator.run("thread")); yield* Deferred.await(started); - yield* coordinator.wake("session"); - const idle = yield* Effect.forkChild(coordinator.awaitIdle("session")); + yield* coordinator.wake("thread"); + const idle = yield* Effect.forkChild(coordinator.awaitIdle("thread")); yield* Deferred.succeed(gate, undefined); // The failure reaches the caller, and the queued work still runs — once. @@ -580,16 +580,16 @@ describe("RunCoordinator", () => { }), }); - const fiber = yield* Effect.forkChild(coordinator.run("session")); + const fiber = yield* Effect.forkChild(coordinator.run("thread")); yield* Deferred.await(firstStarted); - yield* coordinator.wake("session"); + yield* coordinator.wake("thread"); yield* Deferred.succeed(firstGate, undefined); yield* Deferred.await(secondStarted); - yield* coordinator.wake("session"); // arrives during the follow-up + yield* coordinator.wake("thread"); // arrives during the follow-up yield* Deferred.succeed(secondGate, undefined); yield* Fiber.join(fiber); - yield* coordinator.awaitIdle("session"); + yield* coordinator.awaitIdle("thread"); expect(calls).toBe(3); }), @@ -615,9 +615,9 @@ describe("RunCoordinator", () => { }); wake = coordinator.wake; - yield* coordinator.wake("session"); + yield* coordinator.wake("thread"); yield* Deferred.await(completed); - yield* coordinator.awaitIdle("session"); + yield* coordinator.awaitIdle("thread"); expect(calls).toBe(limit); }), @@ -650,23 +650,21 @@ describe("RunCoordinator", () => { }), }); - yield* coordinator.wake("session"); + yield* coordinator.wake("thread"); yield* Deferred.await(started); - const settled = yield* Effect.forkChild( - coordinator.interrupt("session", undefined, { awaitSettlement: true }), - ); + const settled = yield* Effect.forkChild(coordinator.interrupt("thread", undefined, { awaitSettlement: true })); // Work admitted while the interrupted execution is still cleaning up. The // wake has to land after the interrupt was accepted, since an earlier one // is deliberately claimed by the interrupted intent. yield* Deferred.await(cleanupStarted); - yield* coordinator.wake("session"); + yield* coordinator.wake("thread"); expect(settled.pollUnsafe()).toBeUndefined(); yield* Deferred.succeed(cleanupGate, undefined); // Resolves on the interrupted execution's own settlement; the successor is // still parked on its gate, so `awaitIdle` would still be blocked here. expect(yield* Fiber.join(settled)).toBe(true); - const idle = yield* Effect.forkChild(coordinator.awaitIdle("session")); + const idle = yield* Effect.forkChild(coordinator.awaitIdle("thread")); expect(idle.pollUnsafe()).toBeUndefined(); yield* Deferred.succeed(successorGate, undefined); diff --git a/packages/harness/test/runner.llm.live.test.ts b/packages/harness/test/runner.llm.live.test.ts index 2c2c56d1..9a8bcc24 100644 --- a/packages/harness/test/runner.llm.live.test.ts +++ b/packages/harness/test/runner.llm.live.test.ts @@ -12,6 +12,7 @@ import { LLM } from "../src/runner/llm.ts"; import { seedSpace } from "./fixtures/space.ts"; import { SessionLive } from "../src/session/live.ts"; import { Session } from "../src/session/session.ts"; +import { Thread } from "../src/thread/thread.ts"; import { testEffect } from "./utils/effect.ts"; const layer = SessionLive.layer.pipe(Layer.provideMerge(Event.layer), Layer.provideMerge(Database.layer(":memory:"))); @@ -21,13 +22,13 @@ const setup = Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; const { spaceId, location } = yield* seedSpace(); const sessions = yield* Session.Service; - const session = yield* sessions.create({ + const { session, thread } = yield* sessions.create({ spaceId, directory: location, title: "Provider live test", tag: "test", }); - return { sql, sessions, sessionId: session.id }; + return { sql, threads: yield* Thread.Service, sessionId: session.id, threadId: thread.id }; }); const context = (text: string): Message.Context => ({ @@ -55,7 +56,7 @@ describe.each(LIVE)("runner LLM — live $name", (live) => { * persisted exactly when thinking streamed, and that holds on every attempt. */ const attempt = Effect.gen(function* () { - const { sql, sessions, sessionId } = yield* setup; + const { sql, threads, sessionId, threadId } = yield* setup; const thinkingDeltas = yield* Ref.make(0); // Attempts run one after another and each removes its listener when its run ends, so // every delta counted here is this session's. @@ -64,9 +65,9 @@ describe.each(LIVE)("runner LLM — live $name", (live) => { ? Ref.update(thinkingDeltas, (count) => count + 1) : Effect.void, ); - const publisher = yield* LLMEventPublisher.make({ sessionId }); + const publisher = yield* LLMEventPublisher.make({ sessionId, threadId }); const terminal = yield* LLM.run({ - sessionId, + threadId, context: context( "Solve carefully, showing no work: how many positive integers below 1000 are divisible by 3 or 5 but not by 15? Reply with the number and one short verification sentence.", ), @@ -81,7 +82,7 @@ describe.each(LIVE)("runner LLM — live $name", (live) => { }).pipe(Effect.ensuring(removeListener)); expect(terminal.outcome).toBe("ended"); - const path = yield* sessions.path(sessionId); + const path = yield* threads.line(threadId); expect(path.map((item) => item.entry.type)).toEqual(["assistant"]); const stored = yield* ContextCodec.decodeMessage(path[0]!); if (stored.role !== "assistant") @@ -103,7 +104,11 @@ describe.each(LIVE)("runner LLM — live $name", (live) => { expect(JSON.parse(path[0]!.entry.data)).toMatchObject({ stopReason: "stop" }); expect(path[0]!.parts.some((part) => part.type === "thinking")).toBe(true); const durable = yield* sql`SELECT type FROM event WHERE aggregate_id = ${sessionId} ORDER BY seq`; - expect(durable.map((row) => row.type)).toEqual(["session.llm.started.1", "session.llm.ended.1"]); + expect(durable.map((row) => row.type)).toEqual([ + "session.created.1", + "session.llm.started.1", + "session.llm.ended.1", + ]); }), { timeout: 540_000 }, ); @@ -111,7 +116,7 @@ describe.each(LIVE)("runner LLM — live $name", (live) => { liveIt( "bridges Effect interruption to a partless aborted tombstone", Effect.gen(function* () { - const { sessions, sessionId } = yield* setup; + const { threads, sessionId, threadId } = yield* setup; const events = yield* Event.Service; const streaming = yield* Deferred.make<"thinking" | "text">(); yield* events.listen((event) => @@ -122,9 +127,9 @@ describe.each(LIVE)("runner LLM — live $name", (live) => { : Effect.void, ); - const publisher = yield* LLMEventPublisher.make({ sessionId }); + const publisher = yield* LLMEventPublisher.make({ sessionId, threadId }); const running = yield* LLM.run({ - sessionId, + threadId, context: context("List 200 distinct first names, one per line."), provider: live.provider, model: live.id, @@ -138,7 +143,7 @@ describe.each(LIVE)("runner LLM — live $name", (live) => { const exit = yield* Fiber.await(running); expect(Exit.isFailure(exit) && Cause.hasInterrupts(exit.cause)).toBe(true); - const path = yield* sessions.path(sessionId); + const path = yield* threads.line(threadId); expect(path.map((item) => item.entry.type)).toEqual(["assistant"]); expect(path[0]!.entry.state).toBe("aborted"); const stored = yield* ContextCodec.decodeMessage(path[0]!); diff --git a/packages/harness/test/runner.llm.test.ts b/packages/harness/test/runner.llm.test.ts index 6b06312e..6c22ec84 100644 --- a/packages/harness/test/runner.llm.test.ts +++ b/packages/harness/test/runner.llm.test.ts @@ -18,13 +18,16 @@ const { effect: it } = testEffect(layer); const setup = Effect.gen(function* () { const { spaceId, location } = yield* seedSpace(); const sessions = yield* Session.Service; - const session = yield* sessions.create({ + const { session, thread } = yield* sessions.create({ spaceId, directory: location, title: "Provider test", tag: "test", }); - return { sessionId: session.id, publisher: yield* LLMEventPublisher.make({ sessionId: session.id }) }; + return { + threadId: thread.id, + publisher: yield* LLMEventPublisher.make({ sessionId: session.id, threadId: thread.id }), + }; }); const context: Message.Context = { @@ -41,7 +44,7 @@ describe("runner LLM", () => { it( "maps an async iterator failure to a typed ProviderError", Effect.gen(function* () { - const { sessionId, publisher } = yield* setup; + const { threadId, publisher } = yield* setup; const request = LLM.make(() => Effect.succeed({ [Symbol.asyncIterator]() { @@ -52,7 +55,7 @@ describe("runner LLM", () => { }), ); const failure = yield* request({ - sessionId, + threadId, context, provider: "openai", model: "gpt-4o-mini", diff --git a/packages/harness/test/runner.loop.test.ts b/packages/harness/test/runner.loop.test.ts index 3b867695..c10554f6 100644 --- a/packages/harness/test/runner.loop.test.ts +++ b/packages/harness/test/runner.loop.test.ts @@ -12,7 +12,7 @@ import { Context } from "../src/context/context.ts"; import { Control } from "../src/control.ts"; import { Session as SessionApi } from "../src/effect/session.ts"; import { Database } from "../src/db/db.ts"; -import { SessionInputRow } from "../src/db/schema.sql.ts"; +import { InboxRow } from "../src/db/schema.sql.ts"; import { Event } from "../src/event/event.ts"; import { EventList } from "../src/event/list.ts"; import { EventRegistry } from "../src/event/registry.ts"; @@ -20,18 +20,19 @@ import type { EventSchema } from "../src/event/schema.ts"; import { RunnerExecute } from "../src/runner/execute.ts"; import { RunnerExecution } from "../src/runner/execution.ts"; import { Hooks } from "../src/runner/hooks.ts"; +import { Inbox } from "../src/inbox/inbox.ts"; import { LLM } from "../src/runner/llm.ts"; import { Loop } from "../src/runner/loop.ts"; import { SandboxController } from "../src/sandbox/control.ts"; import { SandboxDriverRegistry } from "../src/sandbox/registry.ts"; -import { SessionInput } from "../src/session/input/input.ts"; import { SessionMessageSchema } from "../src/session/message/schema.ts"; import { PromptSchema } from "../src/session/prompt/schema.ts"; import { SessionProjector } from "../src/session/projector.ts"; -import { SessionSchema } from "../src/session/schema.ts"; import { Session } from "../src/session/session.ts"; import { SessionRuntime } from "../src/session/runtime.ts"; import { State } from "../src/state/state.ts"; +import type { ThreadSchema } from "../src/thread/schema.ts"; +import { Thread } from "../src/thread/thread.ts"; import { pooled } from "./fixtures/pool.ts"; import * as Tool from "../src/tool/tool.ts"; import { assistant, immediateOpen, toolTurn } from "./fixtures/llm.ts"; @@ -85,7 +86,7 @@ const runtime = ( Layer.provideMerge(sandbox), Layer.provideMerge(Context.layer), Layer.provideMerge(SessionProjector.layer), - Layer.provideMerge(Session.layer), + Layer.provideMerge(Session.storeLayer), Layer.provideMerge(Event.layer), Layer.provideMerge(EventRegistry.layer()), Layer.provideMerge(database), @@ -104,39 +105,45 @@ const hooksOf = (overrides: Partial): Hooks.Interface => ({ const inputsFor = (sql: SqlClient.SqlClient) => SqlSchema.findAll({ Request: Schema.String, - Result: SessionInputRow, - execute: (sessionId) => sql` - SELECT * FROM session_input WHERE session_id = ${sessionId} ORDER BY admitted_seq + Result: InboxRow, + execute: (threadId) => sql` + SELECT * FROM inbox WHERE thread_id = ${threadId} ORDER BY admitted_seq `, }); -const delivered = Effect.fnUntraced(function* (sessionId: string) { +const delivered = Effect.fnUntraced(function* (threadId: string) { const sql = yield* SqlClient.SqlClient; - const rows = yield* inputsFor(sql)(sessionId); + const rows = yield* inputsFor(sql)(threadId); return rows.map((row) => ({ id: row.id, promotedSeq: Option.getOrNull(row.promotedSeq) })); }); const seedSession = Effect.fnUntraced(function* () { const sessions = yield* Session.Service; const { spaceId, location } = yield* seedSpace({ location: process.cwd(), projectId: "p" }); - const session = yield* sessions.create({ + const { session, thread } = yield* sessions.create({ spaceId, directory: location, title: "runner loop", }); - return session.id; + return { sessionId: session.id, threadId: thread.id }; +}); + +const sessionOf = Effect.fnUntraced(function* (threadId: ThreadSchema.ID) { + const threads = yield* Thread.Service; + return Option.getOrThrow(yield* threads.get(threadId)).sessionId; }); const admit = Effect.fnUntraced(function* (input: { readonly id: string; - readonly sessionId: SessionSchema.ID; + readonly threadId: ThreadSchema.ID; readonly delivery: "steer" | "followUp"; readonly text?: string; }) { - const inputs = yield* SessionInput.make; + const inputs = yield* Inbox.make; return yield* inputs.admit({ id: SessionMessageSchema.ID.make(input.id), - sessionId: input.sessionId, + sessionId: yield* sessionOf(input.threadId), + threadId: input.threadId, prompt: { parts: [{ type: "text", text: input.text ?? input.id }] }, delivery: input.delivery, }); @@ -151,17 +158,17 @@ describe("runner loop — aikit input/output", () => { Effect.gen(function* () { contexts.length = 0; const execution = yield* RunnerExecution.Service; - const sessions = yield* Session.Service; - const sessionId = yield* seedSession(); + const threads = yield* Thread.Service; + const { threadId } = yield* seedSession(); - yield* admit({ id: "msg_follow", sessionId, delivery: "followUp" }); - yield* admit({ id: "msg_steer", sessionId, delivery: "steer" }); - yield* execution.resume(sessionId); + yield* admit({ id: "msg_follow", threadId, delivery: "followUp" }); + yield* admit({ id: "msg_steer", threadId, delivery: "steer" }); + yield* execution.resume(threadId); - const rows = yield* delivered(sessionId); + const rows = yield* delivered(threadId); const bySeq = [...rows].sort((left, right) => (left.promotedSeq ?? 0) - (right.promotedSeq ?? 0)); expect(bySeq.map((row) => row.id)).toEqual(["msg_steer", "msg_follow"]); - expect((yield* sessions.path(sessionId)).map((item) => item.entry.type)).toEqual([ + expect((yield* threads.line(threadId)).map((item) => item.entry.type)).toEqual([ "user", "assistant", "user", @@ -181,15 +188,15 @@ describe("runner loop — aikit input/output", () => { "does not promote an input twice and the leaf guard skips a settled reply", Effect.gen(function* () { const execution = yield* RunnerExecution.Service; - const sessions = yield* Session.Service; - const sessionId = yield* seedSession(); - yield* admit({ id: "msg_once", sessionId, delivery: "steer" }); + const threads = yield* Thread.Service; + const { threadId } = yield* seedSession(); + yield* admit({ id: "msg_once", threadId, delivery: "steer" }); - yield* execution.resume(sessionId); - yield* execution.resume(sessionId); + yield* execution.resume(threadId); + yield* execution.resume(threadId); - expect(yield* delivered(sessionId)).toEqual([{ id: "msg_once", promotedSeq: 1 }]); - expect((yield* sessions.path(sessionId)).map((item) => item.entry.type)).toEqual(["user", "assistant"]); + expect(yield* delivered(threadId)).toEqual([{ id: "msg_once", promotedSeq: 3 }]); + expect((yield* threads.line(threadId)).map((item) => item.entry.type)).toEqual(["user", "assistant"]); }), ); @@ -197,17 +204,17 @@ describe("runner loop — aikit input/output", () => { "promotes all current steers as one request, then follow-ups one per request", Effect.gen(function* () { const execution = yield* RunnerExecution.Service; - const sessions = yield* Session.Service; - const sessionId = yield* seedSession(); - yield* admit({ id: "msg_s1", sessionId, delivery: "steer" }); - yield* admit({ id: "msg_f1", sessionId, delivery: "followUp" }); - yield* admit({ id: "msg_s2", sessionId, delivery: "steer" }); - yield* admit({ id: "msg_f2", sessionId, delivery: "followUp" }); - yield* admit({ id: "msg_s3", sessionId, delivery: "steer" }); + const threads = yield* Thread.Service; + const { threadId } = yield* seedSession(); + yield* admit({ id: "msg_s1", threadId, delivery: "steer" }); + yield* admit({ id: "msg_f1", threadId, delivery: "followUp" }); + yield* admit({ id: "msg_s2", threadId, delivery: "steer" }); + yield* admit({ id: "msg_f2", threadId, delivery: "followUp" }); + yield* admit({ id: "msg_s3", threadId, delivery: "steer" }); - yield* execution.resume(sessionId); + yield* execution.resume(threadId); - const path = yield* sessions.path(sessionId); + const path = yield* threads.line(threadId); expect(path.map((item) => item.entry.type)).toEqual([ "user", "user", @@ -239,20 +246,20 @@ describe("runner loop — aikit input/output", () => { location: `/definitely-missing-${crypto.randomUUID()}`, projectId: "p", }); - const session = yield* sessions.create({ + const { session, thread } = yield* sessions.create({ spaceId, directory: location, title: "missing sandbox cwd", }); - const exit = yield* execution.resume(session.id).pipe(Effect.exit); + const exit = yield* execution.resume(thread.id).pipe(Effect.exit); expect(Exit.isFailure(exit)).toBe(true); if (Exit.isFailure(exit)) { const failure = Cause.findErrorOption(exit.cause); expect(Option.isSome(failure) && failure.value._tag).toBe("Runner.SandboxDirectoryNotFoundError"); } const durable = yield* sql`SELECT type FROM event WHERE aggregate_id = ${session.id}`; - expect(durable).toEqual([]); + expect(durable).toEqual([{ type: "session.created.1" }]); }), ); }); @@ -324,12 +331,12 @@ describe("runner loop — tool continuation and lifecycle gate", () => { snapshots = 0; responseIndex = 0; const execution = yield* RunnerExecution.Service; - const sessions = yield* Session.Service; + const threads = yield* Thread.Service; const sql = yield* SqlClient.SqlClient; - const sessionId = yield* seedSession(); - yield* admit({ id: "msg_tool", sessionId, delivery: "steer" }); + const { sessionId, threadId } = yield* seedSession(); + yield* admit({ id: "msg_tool", threadId, delivery: "steer" }); - yield* execution.resume(sessionId); + yield* execution.resume(threadId); expect(contexts).toHaveLength(2); expect(snapshots).toBe(2); @@ -338,7 +345,7 @@ describe("runner loop — tool continuation and lifecycle gate", () => { second.role === "assistant" ? second.parts.find((part) => part.type === "toolCall") : undefined; expect(firstTool).toMatchObject({ callID: "call_echo", status: "completed" }); - const path = yield* sessions.path(sessionId); + const path = yield* threads.line(threadId); expect(path.map((item) => [item.entry.type, item.entry.state])).toEqual([ ["user", "committed"], ["assistant", "committed"], @@ -382,11 +389,11 @@ describe("runner loop — crash healing", () => { snapshots = 0; const events = yield* Event.Service; const execution = yield* RunnerExecution.Service; - const sessions = yield* Session.Service; - const sessionId = yield* seedSession(); + const threads = yield* Thread.Service; + const { sessionId, threadId } = yield* seedSession(); const input: LLM.Input = { resolvedModel: yield* LLM.resolve({ provider: "openai", model: "gpt-4o-mini" }), - sessionId, + threadId, context: { messages: [] }, provider: "openai", model: "gpt-6-luna", @@ -395,14 +402,15 @@ describe("runner loop — crash healing", () => { yield* events.publish(EventList.LLMStarted, { timestamp: DateTime.makeUnsafe(1), sessionId, + threadId, messageId: SessionMessageSchema.ID.from(draft.messageId), message: draft, }); - yield* execution.wake(sessionId); - while ((yield* execution.active).has(sessionId)) yield* Effect.yieldNow; + yield* execution.wake(threadId); + while ((yield* execution.active).has(threadId)) yield* Effect.yieldNow; - const path = yield* sessions.path(sessionId); + const path = yield* threads.line(threadId); expect(path).toHaveLength(1); expect(path[0]!.entry.state).toBe("aborted"); expect(path[0]!.parts).toEqual([]); @@ -460,10 +468,10 @@ describe("runner loop — tool interruption", () => { toolGate = yield* Deferred.make(); const execution = yield* RunnerExecution.Service; const events = yield* Event.Service; - const sessions = yield* Session.Service; + const threads = yield* Thread.Service; const sql = yield* SqlClient.SqlClient; - const sessionId = yield* seedSession(); - yield* admit({ id: "msg_interrupt_tools", sessionId, delivery: "steer" }); + const { sessionId, threadId } = yield* seedSession(); + yield* admit({ id: "msg_interrupt_tools", threadId, delivery: "steer" }); const bothStarted = yield* Deferred.make(); let started = 0; @@ -476,7 +484,7 @@ describe("runner loop — tool interruption", () => { : Effect.void, ); - const running = yield* execution.resume(sessionId).pipe(Effect.forkChild); + const running = yield* execution.resume(threadId).pipe(Effect.forkChild); yield* Deferred.await(bothStarted); const lifecycle: EventSchema.Payload[] = []; yield* events.listen((event) => @@ -484,7 +492,7 @@ describe("runner loop — tool interruption", () => { if (event.type === EventList.ExecutionInterrupted.type) lifecycle.push(event); }), ); - yield* execution.interrupt(sessionId, "user", { awaitSettlement: true }); + yield* execution.interrupt(threadId, "user", { awaitSettlement: true }); const exit = yield* Fiber.await(running); expect(Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause)).toBe(true); // The reason is the caller's, not something inferred from the cause. @@ -493,7 +501,7 @@ describe("runner loop — tool interruption", () => { if (interrupted !== undefined && Schema.is(EventList.ExecutionInterrupted)(interrupted)) expect(interrupted.data.reason).toBe("user"); - const path = yield* sessions.path(sessionId); + const path = yield* threads.line(threadId); expect(path[1]!.entry.state).toBe("committed"); expect(path[1]!.parts.map((part) => Option.getOrThrow(part.status))).toEqual(["aborted", "aborted"]); for (const part of path[1]!.parts) { @@ -541,9 +549,9 @@ describe("runner loop — provider interruption", () => { Effect.gen(function* () { const execution = yield* RunnerExecution.Service; const events = yield* Event.Service; - const sessions = yield* Session.Service; - const sessionId = yield* seedSession(); - yield* admit({ id: "msg_interrupted", sessionId, delivery: "steer" }); + const threads = yield* Thread.Service; + const { threadId } = yield* seedSession(); + yield* admit({ id: "msg_interrupted", threadId, delivery: "steer" }); const streaming = yield* Deferred.make(); yield* events.listen((event) => @@ -551,14 +559,14 @@ describe("runner loop — provider interruption", () => { ? Deferred.succeed(streaming, undefined).pipe(Effect.asVoid) : Effect.void, ); - const waiting = yield* execution.resume(sessionId).pipe(Effect.forkChild); + const waiting = yield* execution.resume(threadId).pipe(Effect.forkChild); yield* Deferred.await(streaming); - yield* execution.interrupt(sessionId); + yield* execution.interrupt(threadId); const exit = yield* Fiber.await(waiting); expect(Exit.isFailure(exit) && Cause.hasInterrupts(exit.cause)).toBe(true); - const path = yield* sessions.path(sessionId); + const path = yield* threads.line(threadId); expect(path.map((item) => item.entry.type)).toEqual(["user", "assistant"]); expect(path[1]!.entry.state).toBe("aborted"); expect(JSON.parse(path[1]!.entry.data)).toMatchObject({ stopReason: "aborted" }); @@ -598,11 +606,11 @@ describe("runner loop — provider failure", () => { "aborts a provider-failed assistant before failing the turn", Effect.gen(function* () { const execution = yield* RunnerExecution.Service; - const sessions = yield* Session.Service; - const sessionId = yield* seedSession(); - yield* admit({ id: "msg_failed", sessionId, delivery: "steer" }); + const threads = yield* Thread.Service; + const { threadId } = yield* seedSession(); + yield* admit({ id: "msg_failed", threadId, delivery: "steer" }); - const exit = yield* execution.resume(sessionId).pipe(Effect.exit); + const exit = yield* execution.resume(threadId).pipe(Effect.exit); expect(Exit.isFailure(exit)).toBe(true); if (Exit.isFailure(exit)) { const failure = Cause.findErrorOption(exit.cause); @@ -615,7 +623,7 @@ describe("runner loop — provider failure", () => { } } - const path = yield* sessions.path(sessionId); + const path = yield* threads.line(threadId); expect(path.map((item) => item.entry.type)).toEqual(["user", "assistant"]); expect(path[1]!.entry.state).toBe("aborted"); expect(path[1]!.parts).toEqual([]); @@ -638,10 +646,10 @@ describe("runner loop — provider failure", () => { if (event.type.startsWith("session.execution.")) lifecycle.push(event); }), ); - const sessionId = yield* seedSession(); - yield* admit({ id: "msg_failed_lifecycle", sessionId, delivery: "steer" }); + const { threadId } = yield* seedSession(); + yield* admit({ id: "msg_failed_lifecycle", threadId, delivery: "steer" }); - yield* execution.resume(sessionId).pipe(Effect.exit); + yield* execution.resume(threadId).pipe(Effect.exit); expect(lifecycle.map((event) => event.type)).toEqual([ EventList.ExecutionStarted.type, @@ -722,9 +730,9 @@ describe("runner loop — retry", () => { return seen; }); - const pathOf = Effect.fnUntraced(function* (sessionId: SessionSchema.ID) { - const sessions = yield* Session.Service; - return (yield* sessions.path(sessionId)).map((item) => ({ + const pathOf = Effect.fnUntraced(function* (threadId: ThreadSchema.ID) { + const threads = yield* Thread.Service; + return (yield* threads.line(threadId)).map((item) => ({ id: item.entry.id, type: item.entry.type, state: item.entry.state, @@ -748,12 +756,12 @@ describe("runner loop — retry", () => { Effect.gen(function* () { const execution = yield* RunnerExecution.Service; const seen = yield* journal(); - const sessionId = yield* seedSession(); - yield* admit({ id: "msg_retry", sessionId, delivery: "steer" }); + const { threadId } = yield* seedSession(); + yield* admit({ id: "msg_retry", threadId, delivery: "steer" }); - yield* execution.resume(sessionId); + yield* execution.resume(threadId); - const path = yield* pathOf(sessionId); + const path = yield* pathOf(threadId); expect(path.map((entry) => entry.state)).toEqual(["committed", "aborted", "aborted", "committed"]); // Every attempt is sent the original context: the failed drafts and their partial text never leak in. expect(contexts).toHaveLength(3); @@ -778,8 +786,8 @@ describe("runner loop — retry", () => { const execution = yield* RunnerExecution.Service; const events = yield* Event.Service; const seen = yield* journal(); - const sessionId = yield* seedSession(); - yield* admit({ id: "msg_retry_interrupt", sessionId, delivery: "steer" }); + const { threadId } = yield* seedSession(); + yield* admit({ id: "msg_retry_interrupt", threadId, delivery: "steer" }); const scheduled = yield* Deferred.make(); yield* events.listen((event) => @@ -787,13 +795,13 @@ describe("runner loop — retry", () => { ? Deferred.succeed(scheduled, undefined).pipe(Effect.asVoid) : Effect.void, ); - const waiting = yield* execution.resume(sessionId).pipe(Effect.forkChild); + const waiting = yield* execution.resume(threadId).pipe(Effect.forkChild); yield* Deferred.await(scheduled); - yield* execution.interrupt(sessionId); + yield* execution.interrupt(threadId); const exit = yield* Fiber.await(waiting); expect(Exit.isFailure(exit) && Cause.hasInterrupts(exit.cause)).toBe(true); - expect((yield* pathOf(sessionId)).map((entry) => entry.state)).toEqual(["committed", "aborted"]); + expect((yield* pathOf(threadId)).map((entry) => entry.state)).toEqual(["committed", "aborted"]); expect(seen.slice(-2)).toEqual([ { type: EventList.RetryFinished.type, attempt: 1, success: false, message: "interrupted" }, { type: EventList.TurnAborted.type, cause: { _tag: "interrupted" } }, @@ -818,13 +826,13 @@ describe("runner loop — retry", () => { Effect.gen(function* () { const execution = yield* RunnerExecution.Service; const seen = yield* journal(); - const sessionId = yield* seedSession(); - yield* admit({ id: "msg_no_retry", sessionId, delivery: "steer" }); + const { threadId } = yield* seedSession(); + yield* admit({ id: "msg_no_retry", threadId, delivery: "steer" }); - const exit = yield* execution.resume(sessionId).pipe(Effect.exit); + const exit = yield* execution.resume(threadId).pipe(Effect.exit); expect(Exit.isFailure(exit)).toBe(true); expect(seen.some((event) => String(event.type).startsWith("session.retry."))).toBe(false); - expect((yield* pathOf(sessionId)).map((entry) => entry.state)).toEqual(["committed", "aborted"]); + expect((yield* pathOf(threadId)).map((entry) => entry.state)).toEqual(["committed", "aborted"]); }), ); } @@ -899,21 +907,22 @@ const texts = (context: Message.Context) => ); /** A user prompt as a client sends one: admitted, then the session woken. */ -const send = (sessionId: SessionSchema.ID, id: string, delivery: "steer" | "followUp" = "steer") => +const send = (threadId: ThreadSchema.ID, id: string, delivery: "steer" | "followUp" = "steer") => Effect.gen(function* () { const control = yield* Control.Service; yield* control.prompt({ - sessionId, + sessionId: yield* sessionOf(threadId), + threadId, id: SessionMessageSchema.ID.make(id), prompt: PromptSchema.Prompt.fromText(id), delivery, }); }); -const settle = (sessionId: SessionSchema.ID) => +const settle = (threadId: ThreadSchema.ID) => Effect.gen(function* () { const execution = yield* RunnerExecution.Service; - yield* execution.awaitIdle(sessionId); + yield* execution.awaitIdle(threadId); }); /** The busy periods' outcomes, in order. */ @@ -945,12 +954,12 @@ describe("runner loop — queued input", () => { ); /** Cancels `id` the first time a turn loads settings: inside the window between the pending check and promotion. */ - const cancelBeforePromotion = Effect.fnUntraced(function* (sessionId: SessionSchema.ID, id: string) { + const cancelBeforePromotion = Effect.fnUntraced(function* (threadId: ThreadSchema.ID, id: string) { const services = yield* Effect.context(); beforeSettings.run = Effect.gen(function* () { beforeSettings.run = Effect.void; - const inputs = yield* SessionInput.make; - yield* inputs.cancel(sessionId, SessionMessageSchema.ID.make(id)); + const inputs = yield* Inbox.make; + yield* inputs.cancel(threadId, SessionMessageSchema.ID.make(id)); }).pipe(Effect.provideContext(services)); }); @@ -958,24 +967,24 @@ describe("runner loop — queued input", () => { "asks the model nothing when the only pending prompt is cancelled before promotion", Effect.gen(function* () { yield* reset; - const sessions = yield* Session.Service; - const inputs = yield* SessionInput.make; + const threads = yield* Thread.Service; + const inputs = yield* Inbox.make; const seen = yield* outcomes; - const sessionId = yield* seedSession(); - yield* admit({ id: "msg_withdrawn", sessionId, delivery: "steer" }); - yield* cancelBeforePromotion(sessionId, "msg_withdrawn"); + const { threadId } = yield* seedSession(); + yield* admit({ id: "msg_withdrawn", threadId, delivery: "steer" }); + yield* cancelBeforePromotion(threadId, "msg_withdrawn"); // A wake, as a prompt sends: a forced resume would answer regardless. const execution = yield* RunnerExecution.Service; - yield* execution.wake(sessionId); - yield* settle(sessionId); + yield* execution.wake(threadId); + yield* settle(threadId); expect(seen).toEqual(["succeeded"]); expect( Option.getOrThrow(yield* inputs.find(SessionMessageSchema.ID.make("msg_withdrawn"))).cancelledSeq, ).toBeDefined(); expect(contexts).toEqual([]); - expect(yield* sessions.path(sessionId)).toEqual([]); + expect(yield* threads.line(threadId)).toEqual([]); }), ); @@ -984,16 +993,16 @@ describe("runner loop — queued input", () => { Effect.gen(function* () { yield* reset; const seen = yield* outcomes; - const sessionId = yield* seedSession(); - yield* send(sessionId, "A"); - yield* settle(sessionId); + const { threadId } = yield* seedSession(); + yield* send(threadId, "A"); + yield* settle(threadId); - yield* admit({ id: "B", sessionId, delivery: "followUp" }); - yield* admit({ id: "S", sessionId, delivery: "steer" }); - yield* cancelBeforePromotion(sessionId, "S"); + yield* admit({ id: "B", threadId, delivery: "followUp" }); + yield* admit({ id: "S", threadId, delivery: "steer" }); + yield* cancelBeforePromotion(threadId, "S"); const execution = yield* RunnerExecution.Service; - yield* execution.wake(sessionId); - yield* settle(sessionId); + yield* execution.wake(threadId); + yield* settle(threadId); expect(seen).toEqual(["succeeded", "succeeded"]); expect(contexts.map(texts)).toEqual([["A"], ["A", "B"]]); @@ -1006,17 +1015,17 @@ describe("runner loop — queued input", () => { Effect.gen(function* () { yield* reset; const seen = yield* outcomes; - const sessionId = yield* seedSession(); - const services = yield* Effect.context(); + const { threadId } = yield* seedSession(); + const services = yield* Effect.context(); script.push("fail"); - during.run = Effect.all([send(sessionId, "F1", "followUp"), send(sessionId, "S1")]).pipe( + during.run = Effect.all([send(threadId, "F1", "followUp"), send(threadId, "S1")]).pipe( Effect.asVoid, Effect.orDie, Effect.provideContext(services), ); - yield* send(sessionId, "A"); - yield* settle(sessionId); + yield* send(threadId, "A"); + yield* settle(threadId); expect(seen).toEqual(["failed", "succeeded"]); expect(contexts.map(texts)).toEqual([["A"], ["A", "S1"], ["A", "S1", "F1"]]); @@ -1032,7 +1041,7 @@ describe("runner loop — queued input", () => { const events = yield* Event.Service; const execution = yield* RunnerExecution.Service; const seen = yield* outcomes; - const sessionId = yield* seedSession(); + const { threadId } = yield* seedSession(); script.push("hang"); const streaming = yield* Deferred.make(); yield* events.listen((event) => @@ -1041,22 +1050,22 @@ describe("runner loop — queued input", () => { : Effect.void, ); - yield* send(sessionId, "A"); + yield* send(threadId, "A"); yield* Deferred.await(streaming); - yield* send(sessionId, "S1"); - yield* send(sessionId, "F1", "followUp"); - yield* execution.interrupt(sessionId, "user", { awaitSettlement: true }); - yield* settle(sessionId); + yield* send(threadId, "S1"); + yield* send(threadId, "F1", "followUp"); + yield* execution.interrupt(threadId, "user", { awaitSettlement: true }); + yield* settle(threadId); expect(seen).toEqual(["interrupted"]); - expect((yield* delivered(sessionId)).map((row) => [row.id, row.promotedSeq !== null])).toEqual([ + expect((yield* delivered(threadId)).map((row) => [row.id, row.promotedSeq !== null])).toEqual([ ["A", true], ["S1", false], ["F1", false], ]); - yield* send(sessionId, "N"); - yield* settle(sessionId); + yield* send(threadId, "N"); + yield* settle(threadId); expect(seen).toEqual(["interrupted", "succeeded"]); expect(contexts.map(texts).slice(1)).toEqual([ @@ -1072,24 +1081,24 @@ describe("runner loop — resume after restart", () => { const { contexts, script } = provider; const { effect: it } = testEffect(runtime({ open: provider.open })); - const stateOf = (sessionId: SessionSchema.ID) => + const stateOf = (threadId: ThreadSchema.ID) => Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; const [row] = yield* sql<{ execution: string | null; restarts: number }>` - SELECT execution, restarts FROM session WHERE id = ${sessionId} + SELECT execution, restarts FROM thread WHERE id = ${threadId} `; return row; }); - /** The session's state each time a run starts, as it starts. */ - const atEachStart = (sessionId: SessionSchema.ID) => + /** The thread's state each time a run starts, as it starts. */ + const atEachStart = (threadId: ThreadSchema.ID) => Effect.gen(function* () { const events = yield* Event.Service; const sql = yield* SqlClient.SqlClient; const seen: Array<{ execution: string | null; restarts: number } | undefined> = []; yield* events.listen((event) => - Schema.is(EventList.ExecutionStarted)(event) && event.data.sessionId === sessionId - ? stateOf(sessionId).pipe( + Schema.is(EventList.ExecutionStarted)(event) && event.data.threadId === threadId + ? stateOf(threadId).pipe( Effect.map((state) => { seen.push(state); }), @@ -1117,15 +1126,15 @@ describe("runner loop — resume after restart", () => { * What a process that died mid-reply leaves: the prompt in the conversation, the reply it was * streaming still a draft, and the run still marked running. */ - const crashedMidReply = Effect.fnUntraced(function* (sessionId: SessionSchema.ID, id: string) { + const crashedMidReply = Effect.fnUntraced(function* (threadId: ThreadSchema.ID, id: string) { const events = yield* Event.Service; - const sessions = yield* Session.Service; - const inputs = yield* SessionInput.make; - yield* admit({ id, sessionId, delivery: "steer" }); - yield* inputs.promoteSteers(sessionId, Number.MAX_SAFE_INTEGER); + const threads = yield* Thread.Service; + const inputs = yield* Inbox.make; + yield* admit({ id, threadId, delivery: "steer" }); + yield* inputs.promoteSteers(threadId, Number.MAX_SAFE_INTEGER); const input: LLM.Input = { resolvedModel: yield* LLM.resolve({ provider: "openai", model: "gpt-4o-mini" }), - sessionId, + threadId, context: { messages: [] }, provider: "openai", model: "gpt-6-luna", @@ -1133,11 +1142,12 @@ describe("runner loop — resume after restart", () => { const draft = assistant(input, 99, { parts: [{ type: "text", text: "half a reply" }] }); yield* events.publish(EventList.LLMStarted, { timestamp: DateTime.makeUnsafe(1), - sessionId, + sessionId: yield* sessionOf(threadId), + threadId, messageId: SessionMessageSchema.ID.from(draft.messageId), message: draft, }); - yield* sessions.markRunning(sessionId); + yield* threads.markRunning(threadId); }); it( @@ -1147,22 +1157,22 @@ describe("runner loop — resume after restart", () => { const control = yield* Control.Service; const execution = yield* RunnerExecution.Service; const seen = yield* outcomes; - const sessionId = yield* seedSession(); + const { threadId } = yield* seedSession(); const streaming = yield* hanging; script.push("hang"); - yield* send(sessionId, "A"); + yield* send(threadId, "A"); yield* streaming; - yield* send(sessionId, "S1"); - yield* execution.interrupt(sessionId, "user", { awaitSettlement: true }); - yield* settle(sessionId); + yield* send(threadId, "S1"); + yield* execution.interrupt(threadId, "user", { awaitSettlement: true }); + yield* settle(threadId); // The stop is the user's: what was queued behind it stays queued, unmarked. - expect(yield* stateOf(sessionId)).toEqual({ execution: null, restarts: 0 }); + expect(yield* stateOf(threadId)).toEqual({ execution: null, restarts: 0 }); yield* control.resumeUnfinished; expect(seen).toEqual(["interrupted"]); expect(contexts.map(texts)).toEqual([["A"]]); - expect((yield* delivered(sessionId)).map((row) => [row.id, row.promotedSeq !== null])).toEqual([ + expect((yield* delivered(threadId)).map((row) => [row.id, row.promotedSeq !== null])).toEqual([ ["A", true], ["S1", false], ]); @@ -1177,18 +1187,18 @@ describe("runner loop — resume after restart", () => { const control = yield* Control.Service; const execution = yield* RunnerExecution.Service; const seen = yield* outcomes; - const sessionId = yield* seedSession(); + const { threadId } = yield* seedSession(); const streaming = yield* hanging; script.push("hang"); - yield* send(sessionId, "A"); + yield* send(threadId, "A"); yield* streaming; - yield* send(sessionId, "S1"); - yield* send(sessionId, "F1", "followUp"); - yield* execution.interrupt(sessionId, "shutdown", { awaitSettlement: true }); - yield* settle(sessionId); + yield* send(threadId, "S1"); + yield* send(threadId, "F1", "followUp"); + yield* execution.interrupt(threadId, "shutdown", { awaitSettlement: true }); + yield* settle(threadId); - expect(yield* stateOf(sessionId)).toEqual({ execution: "closed", restarts: 0 }); - const atStart = yield* atEachStart(sessionId); + expect(yield* stateOf(threadId)).toEqual({ execution: "closed", restarts: 0 }); + const atStart = yield* atEachStart(threadId); yield* control.resumeUnfinished; expect(atStart).toEqual([{ execution: "running", restarts: 0 }]); @@ -1197,7 +1207,7 @@ describe("runner loop — resume after restart", () => { ["A", "S1"], ["A", "S1", "F1"], ]); - expect(yield* stateOf(sessionId)).toEqual({ execution: null, restarts: 0 }); + expect(yield* stateOf(threadId)).toEqual({ execution: null, restarts: 0 }); }), ); @@ -1208,14 +1218,14 @@ describe("runner loop — resume after restart", () => { yield* provider.reset; const control = yield* Control.Service; const execution = yield* RunnerExecution.Service; - const sessionId = yield* seedSession(); + const { threadId } = yield* seedSession(); const streaming = yield* hanging; script.push("hang"); - yield* send(sessionId, "A"); + yield* send(threadId, "A"); yield* streaming; - yield* send(sessionId, "B", "followUp"); - yield* execution.interrupt(sessionId, "shutdown", { awaitSettlement: true }); - yield* settle(sessionId); + yield* send(threadId, "B", "followUp"); + yield* execution.interrupt(threadId, "shutdown", { awaitSettlement: true }); + yield* settle(threadId); yield* control.resumeUnfinished; @@ -1228,22 +1238,22 @@ describe("runner loop — resume after restart", () => { Effect.gen(function* () { yield* provider.reset; const control = yield* Control.Service; - const sessions = yield* Session.Service; - const sessionId = yield* seedSession(); - yield* crashedMidReply(sessionId, "A"); - const atStart = yield* atEachStart(sessionId); + const threads = yield* Thread.Service; + const { threadId } = yield* seedSession(); + yield* crashedMidReply(threadId, "A"); + const atStart = yield* atEachStart(threadId); yield* control.resumeUnfinished; expect(atStart).toEqual([{ execution: "running", restarts: 1 }]); expect(contexts.map(texts)).toEqual([["A"]]); - const path = yield* sessions.path(sessionId); + const path = yield* threads.line(threadId); expect(path.map((entry) => [entry.entry.type, entry.entry.state])).toEqual([ ["user", "committed"], ["assistant", "aborted"], ["assistant", "committed"], ]); - expect(yield* stateOf(sessionId)).toEqual({ execution: null, restarts: 0 }); + expect(yield* stateOf(threadId)).toEqual({ execution: null, restarts: 0 }); }), ); @@ -1253,16 +1263,16 @@ describe("runner loop — resume after restart", () => { "runs nothing when a session recovery chose was stopped before its run began", Effect.gen(function* () { yield* provider.reset; - const sessions = yield* Session.Service; + const threads = yield* Thread.Service; const execution = yield* RunnerExecution.Service; - const sessionId = yield* seedSession(); - yield* crashedMidReply(sessionId, "A"); - yield* sessions.endExecution(sessionId, "stopped"); + const { threadId } = yield* seedSession(); + yield* crashedMidReply(threadId, "A"); + yield* threads.endExecution(threadId, "stopped"); - yield* execution.recover(sessionId); + yield* execution.recover(threadId); expect(contexts).toEqual([]); - expect(yield* stateOf(sessionId)).toEqual({ execution: null, restarts: 0 }); + expect(yield* stateOf(threadId)).toEqual({ execution: null, restarts: 0 }); }), ); @@ -1272,22 +1282,24 @@ describe("runner loop — resume after restart", () => { yield* provider.reset; const control = yield* Control.Service; const sql = yield* SqlClient.SqlClient; - const sessionId = yield* seedSession(); - yield* crashedMidReply(sessionId, "A"); - yield* sql`UPDATE session SET restarts = ${Control.RESTART_LIMIT - 1} WHERE id = ${sessionId}`; + const { sessionId, threadId } = yield* seedSession(); + yield* crashedMidReply(threadId, "A"); + yield* sql`UPDATE thread SET restarts = ${Control.RESTART_LIMIT - 1} WHERE id = ${threadId}`; yield* control.resumeUnfinished; expect(contexts).toEqual([]); - expect(yield* stateOf(sessionId)).toEqual({ execution: "running", restarts: Control.RESTART_LIMIT }); - expect((yield* (yield* SessionApi.get(sessionId).pipe(Effect.map(Option.getOrThrow))).info).parked).toBe(true); + expect(yield* stateOf(threadId)).toEqual({ execution: "running", restarts: Control.RESTART_LIMIT }); + expect((yield* (yield* SessionApi.get(sessionId).pipe(Effect.map(Option.getOrThrow))).main.info).parked).toBe( + true, + ); - yield* send(sessionId, "B"); - yield* settle(sessionId); + yield* send(threadId, "B"); + yield* settle(threadId); expect(contexts.map(texts)).toEqual([["A", "B"]]); - expect(yield* stateOf(sessionId)).toEqual({ execution: null, restarts: 0 }); - expect((yield* (yield* SessionApi.get(sessionId).pipe(Effect.map(Option.getOrThrow))).info).parked).toBe( + expect(yield* stateOf(threadId)).toEqual({ execution: null, restarts: 0 }); + expect((yield* (yield* SessionApi.get(sessionId).pipe(Effect.map(Option.getOrThrow))).main.info).parked).toBe( false, ); }), @@ -1300,18 +1312,18 @@ describe("runner loop — resume after restart", () => { yield* provider.reset; const sql = yield* SqlClient.SqlClient; const execution = yield* RunnerExecution.Service; - const sessionId = yield* seedSession(); - yield* sql`UPDATE session SET restarts = 3 WHERE id = ${sessionId}`; + const { threadId } = yield* seedSession(); + yield* sql`UPDATE thread SET restarts = 3 WHERE id = ${threadId}`; const streaming = yield* hanging; - const services = yield* Effect.context(); + const services = yield* Effect.context(); script.push("answer", "hang"); - provider.during.run = send(sessionId, "S").pipe(Effect.orDie, Effect.provideContext(services)); + provider.during.run = send(threadId, "S").pipe(Effect.orDie, Effect.provideContext(services)); - yield* send(sessionId, "A"); + yield* send(threadId, "A"); yield* streaming; - expect(yield* stateOf(sessionId)).toEqual({ execution: "running", restarts: 0 }); - yield* execution.interrupt(sessionId, "user", { awaitSettlement: true }); + expect(yield* stateOf(threadId)).toEqual({ execution: "running", restarts: 0 }); + yield* execution.interrupt(threadId, "user", { awaitSettlement: true }); }), ); }); @@ -1330,7 +1342,7 @@ describe("runner loop — cancel and unfinished replies", () => { const execution = yield* RunnerExecution.Service; const events = yield* Event.Service; const sql = yield* SqlClient.SqlClient; - const sessionId = yield* seedSession(); + const { threadId } = yield* seedSession(); const streaming = yield* Deferred.make(); yield* events.listen((event) => Schema.is(EventList.LLMTextDelta)(event) && event.data.delta === "partial" @@ -1338,15 +1350,15 @@ describe("runner loop — cancel and unfinished replies", () => { : Effect.void, ); provider.script.push("hang"); - yield* send(sessionId, "A"); + yield* send(threadId, "A"); yield* Deferred.await(streaming); - yield* send(sessionId, "B", "followUp"); - yield* execution.interrupt(sessionId, "shutdown", { awaitSettlement: true }); - yield* settle(sessionId); + yield* send(threadId, "B", "followUp"); + yield* execution.interrupt(threadId, "shutdown", { awaitSettlement: true }); + yield* settle(threadId); - yield* control.cancel(sessionId, SessionMessageSchema.ID.make("B")); + yield* control.cancel(threadId, SessionMessageSchema.ID.make("B")); - const [row] = yield* sql<{ execution: string | null }>`SELECT execution FROM session WHERE id = ${sessionId}`; + const [row] = yield* sql<{ execution: string | null }>`SELECT execution FROM thread WHERE id = ${threadId}`; expect(row?.execution).toBe("closed"); }), ); @@ -1441,10 +1453,10 @@ describe("runner loop — lifecycle hooks", () => { "snapshots before each turn, so a tool declared mid-chain reaches the next request", Effect.gen(function* () { const execution = yield* RunnerExecution.Service; - const sessionId = yield* seedSession(); - yield* admit({ id: "msg_chain", sessionId, delivery: "steer" }); + const { threadId } = yield* seedSession(); + yield* admit({ id: "msg_chain", threadId, delivery: "steer" }); - yield* execution.resume(sessionId); + yield* execution.resume(threadId); expect(snapshots).toBe(2); expect(contexts.map(toolNames)).toEqual([["echo"], ["echo", "echo_late"]]); @@ -1477,12 +1489,12 @@ describe("runner loop — lifecycle hooks", () => { "shows the queued steers, else the oldest follow-up, without promoting them", Effect.gen(function* () { const execution = yield* RunnerExecution.Service; - const sessionId = yield* seedSession(); - yield* admit({ id: "msg_first", sessionId, delivery: "steer" }); - yield* admit({ id: "msg_later", sessionId, delivery: "followUp" }); - yield* admit({ id: "msg_next", sessionId, delivery: "followUp" }); + const { threadId } = yield* seedSession(); + yield* admit({ id: "msg_first", threadId, delivery: "steer" }); + yield* admit({ id: "msg_later", threadId, delivery: "followUp" }); + yield* admit({ id: "msg_next", threadId, delivery: "followUp" }); - yield* execution.resume(sessionId); + yield* execution.resume(threadId); // Turn 1 (toolUse), turn 2 (stop), turn 3 (first follow-up), turn 4 (second follow-up). expect(seen).toEqual([["followUp:msg_later"], ["followUp:msg_later"], ["followUp:msg_next"], []]); @@ -1508,31 +1520,31 @@ describe("runner loop — lifecycle hooks", () => { "stops the run after the committed turn and leaves queued input for the next run", Effect.gen(function* () { const execution = yield* RunnerExecution.Service; - const sessionId = yield* seedSession(); - yield* admit({ id: "msg_first", sessionId, delivery: "steer" }); - yield* admit({ id: "msg_queued", sessionId, delivery: "followUp" }); + const { threadId } = yield* seedSession(); + yield* admit({ id: "msg_first", threadId, delivery: "steer" }); + yield* admit({ id: "msg_queued", threadId, delivery: "followUp" }); - yield* execution.resume(sessionId); + yield* execution.resume(threadId); expect(contexts).toHaveLength(1); expect(prepared).toBe(0); - const sessions = yield* Session.Service; - const path = yield* sessions.path(sessionId); + const threads = yield* Thread.Service; + const path = yield* threads.line(threadId); expect(path.map((item) => [item.entry.type, item.entry.state])).toEqual([ ["user", "committed"], ["assistant", "committed"], ]); const tool = path[1]!.parts.find((part) => part.type === "toolCall"); expect(tool).toBeDefined(); - const rows = yield* delivered(sessionId); + const rows = yield* delivered(threadId); expect(rows.find((row) => row.id === "msg_queued")?.promotedSeq).toBeNull(); // The next run answers the tool results first, then picks the queued follow-up up. - yield* execution.resume(sessionId); + yield* execution.resume(threadId); expect(contexts).toHaveLength(3); expect(contexts[1]!.messages).toHaveLength(2); expect(contexts[2]!.messages).toHaveLength(4); - const next = yield* delivered(sessionId); + const next = yield* delivered(threadId); expect(next.find((row) => row.id === "msg_queued")?.promotedSeq).not.toBeNull(); }), ); @@ -1554,13 +1566,13 @@ describe("runner loop — lifecycle hooks", () => { "joins the reply being answered, ahead of the queued follow-up", Effect.gen(function* () { const execution = yield* RunnerExecution.Service; - const sessionId = yield* seedSession(); - yield* admit({ id: "msg_first", sessionId, delivery: "steer" }); - yield* admit({ id: "msg_queued", sessionId, delivery: "followUp" }); - yield* execution.resume(sessionId); + const { threadId } = yield* seedSession(); + yield* admit({ id: "msg_first", threadId, delivery: "steer" }); + yield* admit({ id: "msg_queued", threadId, delivery: "followUp" }); + yield* execution.resume(threadId); const services = yield* Effect.context>>(); - gate.run = admit({ id: "msg_late", sessionId, delivery: "steer" }).pipe( + gate.run = admit({ id: "msg_late", threadId, delivery: "steer" }).pipe( Effect.andThen( Effect.sync(() => { gate.run = Effect.void; @@ -1568,7 +1580,7 @@ describe("runner loop — lifecycle hooks", () => { ), Effect.provideContext(services), ); - yield* execution.resume(sessionId); + yield* execution.resume(threadId); expect(contexts.map(texts)).toEqual([ ["msg_first"], @@ -1598,10 +1610,10 @@ describe("runner loop — lifecycle hooks", () => { contexts.length = 0; completed.length = 0; const execution = yield* RunnerExecution.Service; - const sessionId = yield* seedSession(); - yield* admit({ id: "msg_done", sessionId, delivery: "steer" }); + const { threadId } = yield* seedSession(); + yield* admit({ id: "msg_done", threadId, delivery: "steer" }); - yield* execution.resume(sessionId); + yield* execution.resume(threadId); // The decision was honoured (another turn was scheduled) and the leaf guard sent nothing. expect(completed).toEqual(["stop"]); @@ -1628,10 +1640,10 @@ describe("runner loop — lifecycle hooks", () => { "is met by the tool-result request and adds none of its own", Effect.gen(function* () { const execution = yield* RunnerExecution.Service; - const sessionId = yield* seedSession(); - yield* admit({ id: "msg_tool", sessionId, delivery: "steer" }); + const { threadId } = yield* seedSession(); + yield* admit({ id: "msg_tool", threadId, delivery: "steer" }); - yield* execution.resume(sessionId); + yield* execution.resume(threadId); expect(completed).toEqual(["toolUse", "stop"]); expect(contexts).toHaveLength(2); @@ -1656,10 +1668,10 @@ describe("runner loop — lifecycle hooks", () => { "its snapshot reaches the next request, with a steer queued while it ran", Effect.gen(function* () { const execution = yield* RunnerExecution.Service; - const sessionId = yield* seedSession(); - yield* admit({ id: "msg_first", sessionId, delivery: "steer" }); + const { threadId } = yield* seedSession(); + yield* admit({ id: "msg_first", threadId, delivery: "steer" }); const services = yield* Effect.context>>(); - during.run = admit({ id: "msg_during", sessionId, delivery: "steer" }).pipe( + during.run = admit({ id: "msg_during", threadId, delivery: "steer" }).pipe( Effect.andThen( Effect.sync(() => { during.run = Effect.void; @@ -1668,7 +1680,7 @@ describe("runner loop — lifecycle hooks", () => { Effect.provideContext(services), ); - yield* execution.resume(sessionId); + yield* execution.resume(threadId); expect(contexts.map((context) => context.systemPrompt === "replaced")).toEqual([false, true]); expect(contexts.map(texts)).toEqual([["msg_first"], ["msg_first", "msg_during"]]); @@ -1698,10 +1710,10 @@ describe("runner loop — lifecycle hooks", () => { "sees promoted input, and a steer queued while it runs waits for the next turn", Effect.gen(function* () { const execution = yield* RunnerExecution.Service; - const sessionId = yield* seedSession(); - yield* admit({ id: "msg_first", sessionId, delivery: "steer" }); + const { threadId } = yield* seedSession(); + yield* admit({ id: "msg_first", threadId, delivery: "steer" }); const services = yield* Effect.context>>(); - during.run = admit({ id: "msg_during", sessionId, delivery: "steer" }).pipe( + during.run = admit({ id: "msg_during", threadId, delivery: "steer" }).pipe( Effect.andThen( Effect.sync(() => { during.run = Effect.void; @@ -1710,7 +1722,7 @@ describe("runner loop — lifecycle hooks", () => { Effect.provideContext(services), ); - yield* execution.resume(sessionId); + yield* execution.resume(threadId); expect(seen).toEqual([["msg_first"], ["msg_first", "msg_during"]]); expect(contexts.map(texts)).toEqual(seen); @@ -1740,10 +1752,10 @@ describe("runner loop — lifecycle hooks", () => { "sees the retry number and failure, and its replacement model reaches the request", Effect.gen(function* () { const execution = yield* RunnerExecution.Service; - const sessionId = yield* seedSession(); - yield* admit({ id: "msg_route", sessionId, delivery: "steer" }); + const { threadId } = yield* seedSession(); + yield* admit({ id: "msg_route", threadId, delivery: "steer" }); - yield* execution.resume(sessionId); + yield* execution.resume(threadId); expect(attempts).toMatchObject([ { retry: 0 }, @@ -1809,10 +1821,10 @@ describe("runner loop — tool exposure", () => { "declares only direct and model tools, in the request and the prompt, and refuses the rest", Effect.gen(function* () { const execution = yield* RunnerExecution.Service; - const sessionId = yield* seedSession(); - yield* admit({ id: "msg_exposure", sessionId, delivery: "steer" }); + const { threadId } = yield* seedSession(); + yield* admit({ id: "msg_exposure", threadId, delivery: "steer" }); - yield* execution.resume(sessionId); + yield* execution.resume(threadId); expect(contexts).toHaveLength(2); const section = (name: string) => diff --git a/packages/harness/test/runner.publisher.test.ts b/packages/harness/test/runner.publisher.test.ts index c3d428d7..efe955af 100644 --- a/packages/harness/test/runner.publisher.test.ts +++ b/packages/harness/test/runner.publisher.test.ts @@ -7,6 +7,7 @@ import { LLMEventPublisher } from "../src/runner/event.ts"; import { seedSpace } from "./fixtures/space.ts"; import { SessionLive } from "../src/session/live.ts"; import { Session } from "../src/session/session.ts"; +import { Thread } from "../src/thread/thread.ts"; import { testEffect } from "./utils/effect.ts"; /** @@ -20,13 +21,17 @@ const { effect: it } = testEffect(layer); const setup = Effect.gen(function* () { const { spaceId, location } = yield* seedSpace(); const sessions = yield* Session.Service; - const session = yield* sessions.create({ + const { session, thread } = yield* sessions.create({ spaceId, directory: location, title: "T", tag: "test", }); - return { sessions, sessionId: session.id, publisher: yield* LLMEventPublisher.make({ sessionId: session.id }) }; + return { + threads: yield* Thread.Service, + threadId: thread.id, + publisher: yield* LLMEventPublisher.make({ sessionId: session.id, threadId: thread.id }), + }; }); const messageId = "0193f0a0-0000-7000-8000-000000000001"; @@ -55,7 +60,7 @@ const assistant = (overrides: Partial = {}): Message.A describe("LLMEventPublisher", () => { it("rejects a second terminal, so one response cannot become two entries", () => Effect.gen(function* () { - const { sessions, sessionId, publisher } = yield* setup; + const { threads, threadId, publisher } = yield* setup; const message = assistant(); yield* publisher.publish({ type: "start", partial: message }); yield* publisher.publish({ type: "done", reason: "stop", message }); @@ -64,7 +69,7 @@ describe("LLMEventPublisher", () => { expect(failure._tag).toBe("Runner.LLMStreamError"); expect(failure.reason).toContain("after the response had already terminated"); - expect((yield* sessions.path(sessionId)).length).toBe(1); + expect((yield* threads.line(threadId)).length).toBe(1); })); it("rejects a message id that changes mid-response", () => @@ -85,7 +90,7 @@ describe("LLMEventPublisher", () => { it("reports a stream that ended without terminating", () => Effect.gen(function* () { - const { sessions, sessionId, publisher } = yield* setup; + const { threads, threadId, publisher } = yield* setup; const message = assistant(); yield* publisher.publish({ type: "start", partial: message }); yield* publisher.publish({ type: "text.end", partIndex: 0, content: "hello", partial: message }); @@ -94,7 +99,7 @@ describe("LLMEventPublisher", () => { expect(failure._tag).toBe("Runner.LLMStreamError"); expect(failure.reason).toContain("without a terminal event"); - const path = yield* sessions.path(sessionId); + const path = yield* threads.line(threadId); expect(path).toHaveLength(1); expect(path[0]!.entry.state).toBe("draft"); })); diff --git a/packages/harness/test/runner.retry.live.test.ts b/packages/harness/test/runner.retry.live.test.ts index fec1964f..f3e8a780 100644 --- a/packages/harness/test/runner.retry.live.test.ts +++ b/packages/harness/test/runner.retry.live.test.ts @@ -151,8 +151,8 @@ describe.each(cases)("loop retry against live $name", (live) => { }), ); const handle = yield* Session.create({ directory: root }); - yield* handle.run("Reply with the single word: retried"); - return { journal, fragments, path: yield* handle.path() }; + yield* handle.main.run("Reply with the single word: retried"); + return { journal, fragments, path: yield* handle.main.history() }; }).pipe( Effect.provide( Harness.layer({ diff --git a/packages/harness/test/runner.turns.live.test.ts b/packages/harness/test/runner.turns.live.test.ts index 0ba2f799..336c9317 100644 --- a/packages/harness/test/runner.turns.live.test.ts +++ b/packages/harness/test/runner.turns.live.test.ts @@ -96,11 +96,11 @@ describe.each(LIVE)("turn snapshots against live $name", (live) => { const handle = yield* Session.create({ directory: root }); const runtime = yield* SessionRuntime.Service; yield* runtime.update(handle.id, { onPayload }); - yield* handle.run( + yield* handle.main.run( "Call the first tool and follow its instruction. Then reply with exactly the two tokens " + "the tools returned, separated by one space, and nothing else.", ); - const messages = yield* Effect.forEach(yield* handle.path(), ContextCodec.decodeMessage); + const messages = yield* Effect.forEach(yield* handle.main.history(), ContextCodec.decodeMessage); const last = messages.findLast((message) => message.parts.some((part) => part.type === "text")); return { calls: messages diff --git a/packages/harness/test/sandbox.control.test.ts b/packages/harness/test/sandbox.control.test.ts index 739e3ff3..39fc2f2f 100644 --- a/packages/harness/test/sandbox.control.test.ts +++ b/packages/harness/test/sandbox.control.test.ts @@ -346,7 +346,7 @@ describe("Sandbox.Controller", () => { ownership: "managed", status: "provisioning", }); - yield* sql`UPDATE sandbox_instance SET state_observed_at = -1000 WHERE id = ${instanceId}`; + yield* sql`UPDATE sandbox SET state_observed_at = -1000 WHERE id = ${instanceId}`; const restarted = yield* SandboxController.make({ hostCwd: "/", provisioningTimeoutMs: 1 }); const swept = Option.getOrThrow(yield* restarted.get(instanceId)); diff --git a/packages/harness/test/sandbox.instance.test.ts b/packages/harness/test/sandbox.instance.test.ts index 2738f281..96123ed9 100644 --- a/packages/harness/test/sandbox.instance.test.ts +++ b/packages/harness/test/sandbox.instance.test.ts @@ -28,7 +28,7 @@ describe("SandboxInstance", () => { Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; const insert = sql` - INSERT INTO sandbox_instance + INSERT INTO sandbox (id, driver, kind, ownership, status, state_observed_at, created_at, updated_at) VALUES ('local', 'local', 'local', 'external', 'online', 0, 0, 0) `; @@ -69,7 +69,7 @@ describe("SandboxInstance", () => { VALUES ('s', 'p', '/workspace', 'plain', 'sbx_a', 'active', 0, 0) `; - const deletion = sql`DELETE FROM sandbox_instance WHERE id = 'sbx_a'`; + const deletion = sql`DELETE FROM sandbox WHERE id = 'sbx_a'`; expect(Option.isSome(yield* Effect.option(deletion))).toBe(false); }), ); @@ -119,7 +119,7 @@ describe("SandboxInstance", () => { }); const alias = sql` - INSERT INTO sandbox_instance + INSERT INTO sandbox (id, driver, kind, provider_resource_id, ownership, status, state_observed_at, created_at, updated_at) VALUES ('sbx_b', 'sqldb', 'virtual', '/var/fs.db', 'managed', 'online', 0, 0, 0) `; diff --git a/packages/harness/test/sandbox.remote.driver.e2e.test.ts b/packages/harness/test/sandbox.remote.driver.e2e.test.ts index 23654b58..989f52da 100644 --- a/packages/harness/test/sandbox.remote.driver.e2e.test.ts +++ b/packages/harness/test/sandbox.remote.driver.e2e.test.ts @@ -112,7 +112,7 @@ const lifecycle = async (input: { expect(Option.getOrThrow(yield* controller.get(created.id)).status).toBe("removed"); const rows = yield* sql<{ runtimeConfig: string }>` - SELECT runtime_config AS runtimeConfig FROM sandbox_instance WHERE id = ${created.id} + SELECT runtime_config AS runtimeConfig FROM sandbox WHERE id = ${created.id} `; expect(rows[0]!.runtimeConfig).not.toContain(input.secret); expect(input.remote.registered.capabilities.cancels).toBe(input.expectedCancels); diff --git a/packages/harness/test/sdk.test.ts b/packages/harness/test/sdk.test.ts index 0a5ae07f..5d6b44f2 100644 --- a/packages/harness/test/sdk.test.ts +++ b/packages/harness/test/sdk.test.ts @@ -44,7 +44,7 @@ describe("Harness Effect SDK", () => { model: { provider: "openai", id: "gpt-5.6-luna" }, thinkingLevel: "max", }); - yield* created.run("first"); + yield* created.main.run("first"); // Bare re-attach in the same runtime: merges nothing, clears nothing. const attached = yield* Session.attach({ sessionId: created.id }); @@ -79,14 +79,14 @@ describe("Harness Effect SDK", () => { model: { provider: "openai", id: "gpt-5.6-luna" }, thinkingLevel: "max", }); - yield* session.run("first"); + yield* session.main.run("first"); return session.id; }).pipe(Effect.provide(runtime()), Effect.scoped); yield* Effect.gen(function* () { const session = yield* Session.attach({ sessionId }); yield* session.run("second"); - expect((yield* session.path()).map(({ entry }) => entry.type)).toEqual([ + expect((yield* session.history()).map(({ entry }) => entry.type)).toEqual([ "user", "assistant", "user", @@ -105,7 +105,7 @@ describe("Harness Effect SDK", () => { yield* Effect.gen(function* () { const session = yield* Session.attach({ sessionId }); - expect(yield* Session.configuration(sessionId)).toEqual({ + expect(yield* Session.configuration(session.id)).toEqual({ provider: "openai", model: "gpt-5.4", thinkingLevel: "low", diff --git a/packages/harness/test/session.fork.seq.test.ts b/packages/harness/test/session.fork.seq.test.ts deleted file mode 100644 index 53c49ade..00000000 --- a/packages/harness/test/session.fork.seq.test.ts +++ /dev/null @@ -1,146 +0,0 @@ -import { Effect, Layer, Option } from "effect"; -import { SqlClient } from "effect/sql"; -import { describe, expect } from "vite-plus/test"; -import { Control } from "../src/control.ts"; -import { Database } from "../src/db/db.ts"; -import { Event } from "../src/event/event.ts"; -import { RunnerExecution } from "../src/runner/execution.ts"; -import { seedSpace } from "./fixtures/space.ts"; -import { SessionInput } from "../src/session/input/input.ts"; -import { SessionLive } from "../src/session/live.ts"; -import { PromptSchema } from "../src/session/prompt/schema.ts"; -import { SessionSchema } from "../src/session/schema.ts"; -import { Session } from "../src/session/session.ts"; -import { testEffect } from "./utils/effect.ts"; - -const some = (option: Option.Option): A => Option.getOrThrow(option); - -const layer = Session.layer.pipe(Layer.provideMerge(Event.layer), Layer.provideMerge(Database.layer(":memory:"))); -const { effect: it } = testEffect(layer); - -const entry = (sessionId: SessionSchema.ID, id: string, seq: number): Session.AppendEntry => ({ - id, - sessionId, - seq, - type: "user", - data: JSON.stringify({ messageId: id, role: "user", time: { created: 1 } }), - parts: [{ type: "text", data: JSON.stringify({ type: "text", text: id }) }], -}); - -const seed = Effect.gen(function* () { - const { spaceId, location } = yield* seedSpace(); - const session = yield* Session.Service; - return yield* session.create({ - spaceId, - directory: location, - title: "T", - tag: "test", - }); -}); - -describe("fork sequence seeding", () => { - it("seeds the new aggregate above the copied entries and records the fork", () => - Effect.gen(function* () { - const sql = yield* SqlClient.SqlClient; - const session = yield* Session.Service; - const events = yield* Event.Service; - const source = yield* seed; - - // Sparse positions, as a real log produces. - yield* session.append(entry(source.id, "e1", 1)); - yield* session.append(entry(source.id, "e2", 4)); - yield* session.append(entry(source.id, "e3", 9)); - - // Appends advance the source's own head, so it already sits at 9. - const sourceHead = yield* events.latestSequence(source.id); - expect(sourceHead).toBe(9); - - const fork = yield* session.fork({ sessionId: source.id }); - - // The seeded position is the highest copied entry, not 0. - expect(yield* events.latestSequence(fork.id)).toBe(10); - - const forked = yield* sql`SELECT * FROM event WHERE aggregate_id = ${fork.id}`; - expect(forked.length).toBe(1); - expect(forked[0]!.type).toBe("session.next.forked.1"); - expect(forked[0]!.seq).toBe(10); - expect(JSON.parse(forked[0]!.data as string).baseSeq).toBe(9); - - // The fork wrote into its own aggregate only. - expect(yield* events.latestSequence(source.id)).toBe(sourceHead); - expect((yield* sql`SELECT id FROM event WHERE aggregate_id = ${source.id}`).length).toBe(0); - })); - - // Positions arrive from outside now, so the tree can no longer derive the - // guarantee that they advance -- it has to enforce it. Left unchecked, a - // stale position fails silently: `selectPath` orders by seq, so the child - // would sort above its own parent. - it("append rejects a position that does not advance", () => - Effect.gen(function* () { - const session = yield* Session.Service; - const source = yield* seed; - yield* session.append(entry(source.id, "e1", 5)); - - const stale = yield* session.append(entry(source.id, "e2", 2)).pipe(Effect.flip); - expect(stale._tag).toBe("InvalidEntryDataError"); - - // Equal is not advancing either, and it must fail the same way rather - // than surfacing as a raw unique-index violation. - const same = yield* session.append(entry(source.id, "e3", 5)).pipe(Effect.flip); - expect(same._tag).toBe("InvalidEntryDataError"); - - // Nothing was written; the path is untouched. - const path = yield* session.path(source.id); - expect(path.map((h) => h.entry.id)).toEqual(["e1"]); - - // And a position that does advance still works. - yield* session.append(entry(source.id, "e4", 6)); - expect((yield* session.path(source.id)).map((h) => h.entry.seq)).toEqual([5, 6]); - })); -}); - -/** - * `append` takes a position from outside, so anything that writes without - * publishing leaves the aggregate head pointing below state that already - * exists. The next event then lands on an occupied position, the advancing - * guard rejects it, and — because that guard runs inside a projector — the - * event rolls back. A prompt in that state is never deliverable. - */ -describe("append keeps the aggregate head in sync", () => { - const wired = Control.layer.pipe( - // Nothing here drains, so the wake has nowhere to go and no runner is worth - // standing up for it. - Layer.provide(RunnerExecution.noopLayer), - Layer.provideMerge(SessionLive.layer), - Layer.provideMerge(Event.layer), - Layer.provideMerge(Database.layer(":memory:")), - ); - const { effect: itWired } = testEffect(wired); - - itWired("a prompt after a direct append still promotes", () => - Effect.gen(function* () { - const sessions = yield* Session.Service; - const control = yield* Control.Service; - const events = yield* Event.Service; - const inputs = yield* SessionInput.make; - const source = yield* seed; - - yield* sessions.append(entry(source.id, "msg_direct", 1)); - // The head learned about it, so the next event cannot collide. - expect(yield* events.latestSequence(source.id)).toBe(1); - - const admitted = yield* control.prompt({ - sessionId: source.id, - prompt: PromptSchema.Prompt.fromText("after"), - }); - expect(admitted.admittedSeq).toBeGreaterThan(1); - - expect(yield* inputs.promoteSteers(source.id, yield* events.latestSequence(source.id))).toBe(1); - expect(some(yield* inputs.find(admitted.id)).promotedSeq).toBeDefined(); - - const path = yield* sessions.path(source.id); - expect(path.map((h) => h.entry.id)).toEqual(["msg_direct", admitted.id]); - expect(path[1]!.entry.seq).toBeGreaterThan(path[0]!.entry.seq); - }), - ); -}); diff --git a/packages/harness/test/session.input.flow.test.ts b/packages/harness/test/session.input.flow.test.ts index 24ae3d88..4f583ff3 100644 --- a/packages/harness/test/session.input.flow.test.ts +++ b/packages/harness/test/session.input.flow.test.ts @@ -4,12 +4,14 @@ import { describe, expect } from "vite-plus/test"; import { Database } from "../src/db/db.ts"; import { Event } from "../src/event/event.ts"; import { EventList } from "../src/event/list.ts"; -import { SessionInput } from "../src/session/input/input.ts"; +import { Inbox } from "../src/inbox/inbox.ts"; import { SessionMessageSchema } from "../src/session/message/schema.ts"; import { SessionProjector } from "../src/session/projector.ts"; import { PromptSchema } from "../src/session/prompt/schema.ts"; import { SessionSchema } from "../src/session/schema.ts"; import { Session } from "../src/session/session.ts"; +import { ThreadSchema } from "../src/thread/schema.ts"; +import { Thread } from "../src/thread/thread.ts"; import { seedSpace } from "./fixtures/space.ts"; import { testEffect } from "./utils/effect.ts"; @@ -17,7 +19,7 @@ import { testEffect } from "./utils/effect.ts"; // bug in the code under test, not a case to handle. const some = (option: Option.Option): A => Option.getOrThrow(option); -const base = Session.layer.pipe(Layer.provideMerge(Event.layer), Layer.provideMerge(Database.layer(":memory:"))); +const base = Session.storeLayer.pipe(Layer.provideMerge(Event.layer), Layer.provideMerge(Database.layer(":memory:"))); // Provided twice on purpose: a layer is built once per graph, so the projectors // register once. Registering them per consumer instead would make every // admission conflict with itself. @@ -26,65 +28,67 @@ const wired = Layer.provideMerge(Layer.merge(SessionProjector.layer, SessionProj const { effect: it } = testEffect(wired); const sessionId = SessionSchema.ID.make("ses_a"); +const threadId = ThreadSchema.ID.make("thr_a"); const prompt = PromptSchema.Prompt.fromText("fix the bug"); const seedSessions = Effect.gen(function* () { - const sql = yield* SqlClient.SqlClient; + const sessions = yield* Session.Service; const { spaceId, location } = yield* seedSpace(); - yield* sql` - INSERT INTO session (id, space_id, slug, directory, title, tag, created_at, updated_at) - VALUES (${sessionId}, ${spaceId}, ${sessionId}, ${location}, 'T', 'test', 0, 0) - `; + yield* sessions.create({ id: sessionId, mainThreadId: threadId, spaceId, directory: location }); }); -const admit = (input: SessionInput.Interface, text: string) => +const admit = (input: Inbox.Interface, text: string) => input.admit({ id: SessionMessageSchema.ID.create(), sessionId, + threadId, prompt: PromptSchema.Prompt.fromText(text), delivery: "steer", }); const setup = Effect.gen(function* () { yield* seedSessions; - return yield* SessionInput.make; + return yield* Inbox.make; }); -describe("SessionInput admission", () => { +describe("Inbox admission", () => { // An id that already became an entry has left the inbox for good. - it("refuses an id that already graduated into session_entry", () => + it("refuses an id that already graduated into an entry", () => Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; const input = yield* setup; const id = SessionMessageSchema.ID.create(); yield* sql` - INSERT INTO session_entry (id, session_id, parent_id, seq, type, data, created_at, updated_at) - VALUES (${id}, ${sessionId}, NULL, 0, 'user', '{}', 0, 0) + INSERT INTO entry (id, session_id, thread_id, seq, committed_seq, type, state, data, created_at, updated_at) + VALUES (${id}, ${sessionId}, ${threadId}, 1, 1, 'user', 'committed', '{}', 0, 0) `; - const exit = yield* input.admit({ id, sessionId, prompt, delivery: "steer" }).pipe(Effect.exit); + const exit = yield* input.admit({ id, sessionId, threadId, prompt, delivery: "steer" }).pipe(Effect.exit); expect(exit._tag).toBe("Failure"); - // The projector rejected it inside the commit, so no event survives. - expect((yield* sql`SELECT * FROM event`).length).toBe(0); - expect((yield* sql`SELECT * FROM session_input`).length).toBe(0); + // The projector rejected it inside the commit, so no event survives past `session.created`. + expect((yield* sql<{ type: string }>`SELECT type FROM event`).map((row) => row.type)).toEqual([ + "session.created.1", + ]); + expect((yield* sql`SELECT * FROM inbox`).length).toBe(0); })); }); -describe("SessionInput projections", () => { +describe("Inbox projections", () => { // The projection is where publish-time context survives, because the log // itself never carried it. it("carries the promoting event's metadata onto the entry", () => Effect.gen(function* () { const input = yield* setup; const events = yield* Event.Service; - const sessions = yield* Session.Service; + const threads = yield* Thread.Service; const admitted = yield* admit(input, "one"); yield* events.publish( EventList.Prompted, { sessionId, + threadId, timestamp: admitted.timeCreated, messageId: admitted.id, prompt: admitted.prompt, @@ -93,8 +97,8 @@ describe("SessionInput projections", () => { { metadata: { requestId: "req_7" } }, ); - const path = yield* sessions.path(sessionId); - expect(path.map((h) => h.entry.id)).toEqual([admitted.id]); - expect(some(path[0]!.entry.metadata)).toEqual({ requestId: "req_7" }); + const line = yield* threads.line(threadId); + expect(line.map((h) => h.entry.id)).toEqual([admitted.id]); + expect(some(line[0]!.entry.metadata)).toEqual({ requestId: "req_7" }); })); }); diff --git a/packages/harness/test/session.llm.projection.test.ts b/packages/harness/test/session.llm.projection.test.ts index 56300d24..1027a5e9 100644 --- a/packages/harness/test/session.llm.projection.test.ts +++ b/packages/harness/test/session.llm.projection.test.ts @@ -13,7 +13,7 @@ import { testEffect } from "./utils/effect.ts"; /** * The output half on its own: publish the terminal aikit events and assert what - * reaches `session_entry`. No loop, no publisher, no provider — the whole point + * reaches `entry`. No loop, no publisher, no provider — the whole point * of splitting the phases is that this side is provable without them. */ const layer = SessionLive.layer.pipe(Layer.provideMerge(Event.layer), Layer.provideMerge(Database.layer(":memory:"))); @@ -22,13 +22,13 @@ const { effect: it } = testEffect(layer); const setup = Effect.gen(function* () { const { spaceId, location } = yield* seedSpace(); const sessions = yield* Session.Service; - const session = yield* sessions.create({ + const { session, thread } = yield* sessions.create({ spaceId, directory: location, title: "T", tag: "test", }); - return { sessions, events: yield* Event.Service, sessionId: session.id }; + return { events: yield* Event.Service, sessionId: session.id, threadId: thread.id }; }); const messageId = SessionMessageSchema.ID.create(); @@ -72,24 +72,26 @@ const usageTotals = Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; // The client is configured with `transformResultNames: snakeToCamel`, so the // columns come back camelCased even from a raw query. - const rows = yield* sql`SELECT cost, tokens_input, tokens_output, tokens_cache_read FROM session`; + const rows = yield* sql`SELECT cost, tokens_input, tokens_output, tokens_cache_read FROM thread`; return rows[0] as { cost: number; tokensInput: number; tokensOutput: number; tokensCacheRead: number }; }); describe("LLM terminal projection", () => { it("charges usage once, from the terminal message", () => Effect.gen(function* () { - const { events, sessionId } = yield* setup; + const { events, sessionId, threadId } = yield* setup; expect((yield* usageTotals).tokensInput).toBe(0); yield* events.publish(EventList.LLMStarted, { sessionId, + threadId, messageId, timestamp: DateTime.makeUnsafe(0), message: draft(), }); yield* events.publish(EventList.LLMEnded, { sessionId, + threadId, messageId, timestamp: DateTime.makeUnsafe(0), reason: "stop", diff --git a/packages/harness/test/session.relink.e2e.test.ts b/packages/harness/test/session.relink.e2e.test.ts index 8ee6cb0a..5f6b55f0 100644 --- a/packages/harness/test/session.relink.e2e.test.ts +++ b/packages/harness/test/session.relink.e2e.test.ts @@ -12,7 +12,9 @@ import { SandboxController } from "../src/sandbox/control.ts"; import { SandboxIO } from "../src/sandbox/io.ts"; import * as DaytonaDriver from "../src/sandboxes/daytona/index.ts"; import * as VercelDriver from "../src/sandboxes/vercel/index.ts"; +import { Event } from "../src/event/event.ts"; import { Session as SessionStore } from "../src/session/session.ts"; +import { Thread } from "../src/thread/thread.ts"; import { Hash } from "../src/util/hash.ts"; import { remoteSuite } from "./fixtures/live.ts"; import { hasLiveOidc } from "./fixtures/vercel.ts"; @@ -40,6 +42,7 @@ suite("Session.relink across remote envs (vercel → daytona)", () => { Effect.gen(function* () { const sandboxes = yield* SandboxController.Controller; const store = yield* SessionStore.Service; + const threads = yield* Thread.Service; const clone = (sandboxId: Parameters[0]) => sandboxes.withMount( @@ -76,10 +79,13 @@ suite("Session.relink across remote envs (vercel → daytona)", () => { // The session starts on the Vercel clone; one transcript entry is // enough to prove the log survives the move. const session = yield* Session.create({ sandbox: vercel, directory: repoVercel }); - yield* store.append({ + const events = yield* Event.Service; + const seq = (yield* events.latestSequence(session.id)) + 1; + yield* events.advance(session.id, seq); + yield* threads.append({ id: "e-before", - sessionId: session.id, - seq: 1, + threadId: session.main.id, + seq, type: "user", data: JSON.stringify({ messageId: "e-before", role: "user", time: { created: 1 } }), parts: [{ type: "text", data: JSON.stringify({ type: "text", text: "before the move" }) }], @@ -101,7 +107,7 @@ suite("Session.relink across remote envs (vercel → daytona)", () => { expect(after.env).toBe(daytona.id); expect(after.projectId).toBe(projectId); expect(info.directory).toBe(after.location); - expect((yield* moved.path()).map(({ entry }) => entry.id)).toEqual(["e-before"]); + expect((yield* moved.main.history()).map(({ entry }) => entry.id)).toEqual(["e-before"]); // The source env going away afterwards archives its space but does // not touch the moved session. diff --git a/packages/harness/test/session.test.ts b/packages/harness/test/session.test.ts index 22a22255..c2a4318b 100644 --- a/packages/harness/test/session.test.ts +++ b/packages/harness/test/session.test.ts @@ -1,50 +1,44 @@ import { Effect, Exit, Layer, Option } from "effect"; -import { beforeEach, describe, expect } from "vite-plus/test"; +import { SqlClient } from "effect/sql"; +import { describe, expect } from "vite-plus/test"; import { Database } from "../src/db/db.ts"; +import { Document } from "../src/document/document.ts"; import { Event } from "../src/event/event.ts"; import { AbsolutePath } from "../src/schema.ts"; +import { SessionLive } from "../src/session/live.ts"; import { SessionSchema } from "../src/session/schema.ts"; import { Session } from "../src/session/session.ts"; +import { ThreadSchema } from "../src/thread/schema.ts"; +import { Thread } from "../src/thread/thread.ts"; import { seedSpace } from "./fixtures/space.ts"; import { testEffect } from "./utils/effect.ts"; // Fresh in-memory database per test: the layer is rebuilt for every it.effect. -const layer = Session.layer.pipe(Layer.provideMerge(Event.layer), Layer.provideMerge(Database.layer(":memory:"))); +const layer = SessionLive.layer.pipe(Layer.provideMerge(Event.layer), Layer.provideMerge(Database.layer(":memory:"))); const it = testEffect(layer); -// In production an entry's seq is the sequence of the event that produced it. -// These tests exercise the tree directly, with no event log behind them, so a -// per-session counter stands in. Dense here; sparse in production. -const seqCounters = new Map(); -const nextSeq = (sessionId: string) => { - const next = (seqCounters.get(sessionId) ?? 0) + 1; - seqCounters.set(sessionId, next); - return next; -}; -// A fork copies entry positions verbatim and seeds the new aggregate above -// them, so the stand-in counter has to continue from the source's position. -const inheritSeq = (sourceId: string, forkId: string) => { - seqCounters.set(forkId, seqCounters.get(sourceId) ?? 0); -}; -beforeEach(() => seqCounters.clear()); +const createSession = (location?: string) => + Effect.gen(function* () { + const { spaceId, location: directory } = yield* seedSpace(location === undefined ? {} : { location }); + const sessions = yield* Session.Service; + const { session, thread } = yield* sessions.create({ spaceId, directory, title: "Test session", tag: "test" }); + return { sessionId: SessionSchema.ID.make(session.id), threadId: ThreadSchema.ID.make(thread.id) }; + }); -const createSession = () => +// An entry's seq is the sequence of the event that produced it. These tests append directly, so +// each reserves its own position on the session's log, as an event would. +const nextSeq = (sessionId: SessionSchema.ID) => Effect.gen(function* () { - // session.space_id references space(id) - const { spaceId, location } = yield* seedSpace(); - const session = yield* Session.Service; - return yield* session.create({ - spaceId, - directory: location, - title: "Test session", - tag: "test", - }); + const events = yield* Event.Service; + const seq = (yield* events.latestSequence(sessionId)) + 1; + yield* events.advance(sessionId, seq); + return seq; }); -const userEntry = (sessionId: SessionSchema.ID, id: string, text: string): Session.AppendEntry => ({ +const userEntry = (threadId: ThreadSchema.ID, seq: number, id: string, text: string): Thread.AppendEntry => ({ id, - sessionId, - seq: nextSeq(sessionId), + threadId, + seq, type: "user", data: JSON.stringify({ messageId: id, role: "user", time: { created: 1 } }), parts: [{ type: "text", data: JSON.stringify({ type: "text", text }) }], @@ -64,134 +58,93 @@ const usage = ( return { ...tokens, totalTokens: tokens.input + tokens.output + tokens.cacheRead + tokens.cacheWrite, - cost: { - input: 0, - output: 0, - cacheRead: 0, - cacheWrite: 0, - total: input.costTotal ?? 0, - }, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: input.costTotal ?? 0 }, }; }; const assistantEntry = ( - sessionId: SessionSchema.ID, + threadId: ThreadSchema.ID, + seq: number, id: string, - options?: { usage?: SessionSchema.Usage; toolCall?: { callId: string; toolName: string } }, -): Session.AppendEntry => ({ + options?: { usage?: SessionSchema.Usage }, +): Thread.AppendEntry => ({ id, - sessionId, - seq: nextSeq(sessionId), + threadId, + seq, type: "assistant", - state: options?.toolCall === undefined ? "committed" : "draft", data: JSON.stringify({ messageId: id, role: "assistant", stopReason: "stop", usage: options?.usage ?? usage(), }), - parts: [ - { type: "text", data: JSON.stringify({ type: "text", text: "ok" }) }, - ...(options?.toolCall - ? [ - { - type: "toolCall" as const, - status: "pending" as const, - callId: options.toolCall.callId, - toolName: options.toolCall.toolName, - data: JSON.stringify({ - type: "toolCall", - callID: options.toolCall.callId, - name: options.toolCall.toolName, - status: "pending", - }), - }, - ] - : []), - ], + parts: [{ type: "text", data: JSON.stringify({ type: "text", text: "ok" }) }], }); describe("session", () => { - it.effect("assistant appends bump the session usage aggregates", () => + it.effect("assistant appends bump the thread usage aggregates", () => Effect.gen(function* () { - const session = yield* Session.Service; - const created = yield* createSession(); + const threads = yield* Thread.Service; + const { sessionId, threadId } = yield* createSession(); - yield* session.append(userEntry(created.id, "e1", "hi")); - yield* session.append( - assistantEntry(created.id, "e2", { + yield* threads.append(userEntry(threadId, yield* nextSeq(sessionId), "e1", "hi")); + yield* threads.append( + assistantEntry(threadId, yield* nextSeq(sessionId), "e2", { usage: usage({ input: 100, output: 20, cacheRead: 40, cacheWrite: 10, costTotal: 0.5 }), }), ); - yield* session.append(userEntry(created.id, "e3", "again")); - yield* session.append( - assistantEntry(created.id, "e4", { + yield* threads.append(userEntry(threadId, yield* nextSeq(sessionId), "e3", "again")); + yield* threads.append( + assistantEntry(threadId, yield* nextSeq(sessionId), "e4", { usage: usage({ input: 150, output: 30, cacheRead: 90, cacheWrite: 0, costTotal: 0.25 }), }), ); - const row = Option.getOrElse(yield* session.get(created.id), () => created); + const row = Option.getOrThrow(yield* threads.get(threadId)); expect(row.tokensInput).toBe(250); expect(row.tokensOutput).toBe(50); expect(row.tokensCacheRead).toBe(130); expect(row.tokensCacheWrite).toBe(10); expect(row.cost).toBeCloseTo(0.75); + expect((yield* threads.line(threadId)).map((h) => h.entry.id)).toEqual(["e1", "e2", "e3", "e4"]); }), ); - it.effect("path walks root→leaf across a branch point", () => + it.effect("append rejects an unknown thread, a seq that does not advance, and a mismatched envelope", () => Effect.gen(function* () { - const session = yield* Session.Service; - const created = yield* createSession(); - - yield* session.append(userEntry(created.id, "e1", "start")); - yield* session.append(assistantEntry(created.id, "e2")); - yield* session.append(userEntry(created.id, "e3", "approach A")); - yield* session.append(assistantEntry(created.id, "e4")); + const threads = yield* Thread.Service; + const { sessionId, threadId } = yield* createSession(); + const seq = yield* nextSeq(sessionId); + yield* threads.append(userEntry(threadId, seq, "e1", "first")); - // abandon A: move the leaf back to e2 and take approach B - yield* session.branch({ sessionId: created.id, entryId: "e2" }); - yield* session.append(userEntry(created.id, "e5", "approach B")); - - const path = yield* session.path(created.id); - expect(path.map((h) => h.entry.id)).toEqual(["e1", "e2", "e5"]); + const unknown = yield* threads + .append(userEntry(ThreadSchema.ID.make("thr_missing"), seq + 1, "e2", "nowhere")) + .pipe(Effect.flip); + expect(unknown._tag).toBe("ThreadNotFoundError"); - // abandoned branch stays queryable on the full timeline - const timeline = yield* session.timeline({ sessionId: created.id }); - expect(timeline.map((h) => h.entry.id)).toEqual(["e5", "e4", "e3", "e2", "e1"]); + const stale = yield* threads.append(userEntry(threadId, seq, "e3", "same seq")).pipe(Effect.flip); + expect(stale._tag).toBe("InvalidEntryDataError"); - // e5 is a sibling of e3 (both children of e2) - const e5 = timeline.find((h) => h.entry.id === "e5")!; - expect(Option.getOrElse(e5.entry.parentId, () => "")).toBe("e2"); - }), - ); + const mismatched = yield* threads + .append({ ...userEntry(threadId, yield* nextSeq(sessionId), "e4", "x"), id: "e4-other" }) + .pipe(Effect.flip); + expect(mismatched._tag).toBe("InvalidEntryDataError"); - it.effect("append validates an explicit parentId against the session", () => - Effect.gen(function* () { - const session = yield* Session.Service; - const a = yield* createSession(); - const b = yield* createSession(); - yield* session.append(userEntry(a.id, "ea1", "in session a")); - yield* session.append(userEntry(b.id, "eb1", "in session b")); - - // cross-session parent is rejected typed - const result = yield* session - .append({ ...userEntry(b.id, "eb2", "bad parent"), parentId: "ea1" }) + const draftUser = yield* threads + .append({ ...userEntry(threadId, yield* nextSeq(sessionId), "e5", "x"), state: "draft" }) .pipe(Effect.flip); - expect(result._tag).toBe("EntryNotFoundError"); + expect(draftUser._tag).toBe("InvalidEntryDataError"); - // same-session explicit parent works (sibling branch append) - const sibling = yield* session.append({ ...userEntry(b.id, "eb3", "good parent"), parentId: "eb1" }); - expect(Option.getOrElse(sibling.parentId, () => "")).toBe("eb1"); + expect((yield* threads.line(threadId)).map((h) => h.entry.id)).toEqual(["e1"]); }), ); it.effect("duplicate call ids within an entry are rejected by the schema", () => Effect.gen(function* () { - const session = yield* Session.Service; - const created = yield* createSession(); + const threads = yield* Thread.Service; + const { sessionId, threadId } = yield* createSession(); - const toolPart = (callId: string): Session.AppendPart => ({ + const toolPart = (callId: string): Thread.AppendPart => ({ type: "toolCall", status: "pending", callId, @@ -199,241 +152,134 @@ describe("session", () => { data: JSON.stringify({ type: "toolCall", callID: callId, name: "read", status: "pending" }), }); - const exit = yield* session + const exit = yield* threads .append({ - ...assistantEntry(created.id, "e1"), + ...assistantEntry(threadId, yield* nextSeq(sessionId), "e1"), + state: "draft", parts: [toolPart("call_dup"), toolPart("call_dup")], }) .pipe(Effect.exit); expect(Exit.isFailure(exit)).toBe(true); // the failed transaction rolled back whole — no partial entry - expect(Option.isNone(yield* session.entry("e1"))).toBe(true); + expect(Option.isNone(yield* threads.entry("e1"))).toBe(true); }), ); - it.effect("append validates compaction shape and boundary against its actual parent path", () => + it.effect("append validates compaction shape and boundary against the thread's line", () => Effect.gen(function* () { - const session = yield* Session.Service; - const source = yield* createSession(); - const other = yield* createSession(); - yield* session.append(userEntry(source.id, "e1", "root")); - yield* session.append(assistantEntry(source.id, "e2")); - yield* session.branch({ sessionId: source.id, entryId: "e1" }); - yield* session.append(userEntry(source.id, "e3", "active sibling")); - yield* session.append(userEntry(other.id, "other-e1", "foreign")); - - const invalidCompactions: ReadonlyArray = [ - { - id: "c-missing-boundary", - sessionId: source.id, - seq: nextSeq(source.id), - type: "compaction", - data: JSON.stringify({ summary: "missing", tokensBefore: 10 }), - }, - { - id: "c-negative-tokens", - sessionId: source.id, - seq: nextSeq(source.id), - type: "compaction", - data: JSON.stringify({ summary: "negative", firstKeptEntryId: null, tokensBefore: -1 }), - }, - { - id: "c-abandoned-boundary", - sessionId: source.id, - seq: nextSeq(source.id), - type: "compaction", - data: JSON.stringify({ summary: "abandoned", firstKeptEntryId: "e2", tokensBefore: 10 }), - }, - { - id: "c-foreign-boundary", - sessionId: source.id, - seq: nextSeq(source.id), + const threads = yield* Thread.Service; + const source = yield* createSession("/source"); + const other = yield* createSession("/other"); + yield* threads.append(userEntry(source.threadId, yield* nextSeq(source.sessionId), "e1", "root")); + yield* threads.append(assistantEntry(source.threadId, yield* nextSeq(source.sessionId), "e2")); + yield* threads.append(userEntry(other.threadId, yield* nextSeq(other.sessionId), "other-e1", "foreign")); + + const compaction = (id: string, data: object) => + Effect.map(nextSeq(source.sessionId), (seq): Thread.AppendEntry => ({ + id, + threadId: source.threadId, + seq, type: "compaction", - data: JSON.stringify({ summary: "foreign", firstKeptEntryId: "other-e1", tokensBefore: 10 }), - }, + data: JSON.stringify(data), + })); + const invalid = [ + yield* compaction("c-missing-boundary", { summary: "missing", tokensBefore: 10 }), + yield* compaction("c-negative-tokens", { summary: "negative", firstKeptEntryId: null, tokensBefore: -1 }), + yield* compaction("c-foreign-boundary", { + summary: "foreign", + firstKeptEntryId: "other-e1", + tokensBefore: 10, + }), ]; - - for (const input of invalidCompactions) { - const error = yield* session.append(input).pipe(Effect.flip); + for (const input of invalid) { + const error = yield* threads.append(input).pipe(Effect.flip); expect(error._tag).toBe("InvalidEntryDataError"); - expect(Option.isNone(yield* session.entry(input.id))).toBe(true); + expect(Option.isNone(yield* threads.entry(input.id))).toBe(true); } - const afterInvalid = Option.getOrElse(yield* session.get(source.id), () => source); - expect(Option.getOrElse(afterInvalid.leafEntryId, () => "")).toBe("e3"); - expect(yield* session.timeline({ sessionId: source.id })).toHaveLength(3); - - // The explicit parent, not the current leaf, defines the compaction path. - const anchored = yield* session.append({ - id: "c-anchored", - sessionId: source.id, - seq: nextSeq(source.id), - parentId: "e2", - type: "compaction", - data: JSON.stringify({ summary: "root work", firstKeptEntryId: "e1", tokensBefore: 10 }), - }); - expect(Option.getOrElse(anchored.parentId, () => "")).toBe("e2"); + expect(yield* threads.line(source.threadId)).toHaveLength(2); - const wrongAnchor = yield* session - .append({ - id: "c-wrong-anchor", - sessionId: source.id, - seq: nextSeq(source.id), - parentId: "e2", - type: "compaction", - data: JSON.stringify({ summary: "wrong path", firstKeptEntryId: "e3", tokensBefore: 10 }), - }) - .pipe(Effect.flip); - expect(wrongAnchor._tag).toBe("InvalidEntryDataError"); - expect(Option.isNone(yield* session.entry("c-wrong-anchor"))).toBe(true); - const afterWrongAnchor = Option.getOrElse(yield* session.get(source.id), () => source); - expect(Option.getOrElse(afterWrongAnchor.leafEntryId, () => "")).toBe("c-anchored"); - - const summaryOnly = yield* session.append({ - id: "c-summary-only", - sessionId: source.id, - seq: nextSeq(source.id), - type: "compaction", - data: JSON.stringify({ summary: "all prior work", firstKeptEntryId: null, tokensBefore: 10 }), - }); + const anchored = yield* threads.append( + yield* compaction("c-anchored", { summary: "root work", firstKeptEntryId: "e1", tokensBefore: 10 }), + ); + expect(JSON.parse(anchored.data).firstKeptEntryId).toBe("e1"); + + const summaryOnly = yield* threads.append( + yield* compaction("c-summary-only", { + summary: "all prior work", + firstKeptEntryId: null, + tokensBefore: 10, + }), + ); expect(JSON.parse(summaryOnly.data).firstKeptEntryId).toBeNull(); }), ); - it.effect("clone (fork at leaf) copies the path with fresh identity", () => + it.effect("labels an entry, clears it, and rejects an entry of another thread", () => Effect.gen(function* () { - const session = yield* Session.Service; - const source = yield* createSession(); - yield* session.append(userEntry(source.id, "e1", "hello")); - yield* session.append(assistantEntry(source.id, "e2", { toolCall: { callId: "call_1", toolName: "read" } })); - yield* session.setLabel({ sessionId: source.id, entryId: "e1", label: "start" }); - - const fork = yield* session.fork({ sessionId: source.id }); - - // lineage + fresh session state - expect(Option.getOrElse(fork.parentId, () => "")).toBe(source.id); - expect(fork.cost).toBe(0); - expect(fork.title).toBe(source.title); - - const forkPath = yield* session.path(fork.id); - const sourcePath = yield* session.path(source.id); - expect(forkPath.map((h) => h.entry.type)).toEqual(sourcePath.map((h) => h.entry.type)); - // all new entry ids, dense seq, envelope messageId rewritten to the new id - for (const [i, hydrated] of forkPath.entries()) { - expect(hydrated.entry.id).not.toBe(sourcePath[i]!.entry.id); - expect(hydrated.entry.seq).toBe(i + 1); - expect(JSON.parse(hydrated.entry.data).messageId).toBe(hydrated.entry.id); - } - // parts copied verbatim with preserved partIndex; label rode along - const forkAssistant = forkPath[1]!; - expect(forkAssistant.parts.map((p) => p.partIndex)).toEqual([0, 1]); - expect(Option.getOrElse(forkAssistant.parts[1]!.callId, () => "")).toBe("call_1"); - expect(Option.getOrElse(forkPath[0]!.entry.label, () => "")).toBe("start"); - // pending toolCall copied as pending → fork inherits recovery - expect(yield* session.unsettled(fork.id)).toHaveLength(1); - - // source untouched - const sourceAfter = Option.getOrElse(yield* session.get(source.id), () => source); - expect(Option.getOrElse(sourceAfter.leafEntryId, () => "")).toBe("e2"); - expect((yield* session.timeline({ sessionId: source.id })).length).toBe(2); - }), - ); + const threads = yield* Thread.Service; + const { sessionId, threadId } = yield* createSession("/labels"); + const other = yield* createSession("/elsewhere"); + yield* threads.append(userEntry(threadId, yield* nextSeq(sessionId), "e1", "mark me")); - it.effect("fork copies only the active path, not abandoned branches", () => - Effect.gen(function* () { - const session = yield* Session.Service; - const source = yield* createSession(); - yield* session.append(userEntry(source.id, "e1", "start")); - yield* session.append(assistantEntry(source.id, "e2")); - yield* session.append(userEntry(source.id, "e3", "approach A")); - yield* session.branch({ sessionId: source.id, entryId: "e2" }); - yield* session.append(userEntry(source.id, "e5", "approach B")); - - const fork = yield* session.fork({ sessionId: source.id }); - - // source has 4 entries; fork has only the active path e1→e2→e5 - const forkTimeline = yield* session.timeline({ sessionId: fork.id }); - expect(forkTimeline).toHaveLength(3); - const forkPath = yield* session.path(fork.id); - expect(forkPath.map((h) => JSON.parse(h.entry.data).messageId ?? h.entry.id)).toEqual( - forkPath.map((h) => h.entry.id), - ); - // fork diverges independently of the source - inheritSeq(source.id, fork.id); - yield* session.append(userEntry(fork.id, "f6", "fork continues")); - expect((yield* session.timeline({ sessionId: source.id })).length).toBe(4); - expect((yield* session.timeline({ sessionId: fork.id })).length).toBe(4); + yield* threads.setLabel({ threadId, entryId: "e1", label: "start" }); + const labelled = Option.getOrThrow(yield* threads.entry("e1")); + expect(Option.getOrUndefined(labelled.entry.label)).toBe("start"); + + yield* threads.setLabel({ threadId, entryId: "e1", label: null }); + const cleared = Option.getOrThrow(yield* threads.entry("e1")); + expect(Option.isNone(cleared.entry.label)).toBe(true); + + const missing = yield* threads + .setLabel({ threadId: other.threadId, entryId: "e1", label: "x" }) + .pipe(Effect.flip); + expect(missing._tag).toBe("EntryNotFoundError"); }), ); - it.effect("fork remaps compaction firstKeptEntryId into the new session", () => + it.effect("links a session to a host directory and clears it", () => Effect.gen(function* () { - const session = yield* Session.Service; - const source = yield* createSession(); - yield* session.append(userEntry(source.id, "e1", "old")); - yield* session.append(assistantEntry(source.id, "e2")); - yield* session.append({ - id: "c3", - sessionId: source.id, - seq: nextSeq(source.id), - type: "compaction", - data: JSON.stringify({ summary: "earlier work", firstKeptEntryId: "e2", tokensBefore: 1000 }), - }); - yield* session.append(userEntry(source.id, "e4", "after compaction")); - yield* session.append({ - id: "c5", - sessionId: source.id, - seq: nextSeq(source.id), - type: "compaction", - data: JSON.stringify({ summary: "all prior work", firstKeptEntryId: null, tokensBefore: 500 }), - }); + const sessions = yield* Session.Service; + const { sessionId } = yield* createSession(); + + const linked = yield* sessions.link({ sessionId, hostDir: AbsolutePath.make("/host/app") }); + expect(Option.getOrUndefined(linked.hostDir)).toBe("/host/app"); - const fork = yield* session.fork({ sessionId: source.id }); - const forkPath = yield* session.path(fork.id); - - const forkE2 = forkPath[1]!.entry; // copy of e2 - const forkCompaction = forkPath[2]!.entry; - const forkSummaryOnly = forkPath[4]!.entry; - expect(forkCompaction.type).toBe("compaction"); - const payload = JSON.parse(forkCompaction.data); - // window pointer follows the copy — not the old session's id - expect(payload.firstKeptEntryId).toBe(forkE2.id); - expect(payload.firstKeptEntryId).not.toBe("e2"); - expect(payload.summary).toBe("earlier work"); - expect(JSON.parse(forkSummaryOnly.data).firstKeptEntryId).toBeNull(); + const cleared = yield* sessions.link({ sessionId, hostDir: null }); + expect(Option.isNone(cleared.hostDir)).toBe(true); + + const unknown = yield* sessions + .link({ sessionId: SessionSchema.ID.make("ses_missing"), hostDir: null }) + .pipe(Effect.flip); + expect(unknown._tag).toBe("SessionNotFoundError"); }), ); - it.effect("forkBefore a user entry lands at its parent; validates entry type", () => + it.effect("delete removes the session with its threads, entries and log", () => Effect.gen(function* () { - const session = yield* Session.Service; - const source = yield* createSession(); - yield* session.append(userEntry(source.id, "e1", "keep me")); - yield* session.append(assistantEntry(source.id, "e2")); - yield* session.append(userEntry(source.id, "e3", "redo this prompt")); - - // before e3 → fork contains e1, e2; leaf = copy of e2 - const fork = yield* session.fork({ - sessionId: source.id, - entryId: "e3", - mode: "before", - }); - const forkPath = yield* session.path(fork.id); - expect(forkPath).toHaveLength(2); - expect(forkPath.map((h) => h.entry.type)).toEqual(["user", "assistant"]); - - // before a non-user entry is a typed structural rejection - const invalid = yield* session.fork({ sessionId: source.id, entryId: "e2", mode: "before" }).pipe(Effect.flip); - expect(invalid._tag).toBe("InvalidEntryDataError"); - - // before the root user entry → empty fork - const empty = yield* session.fork({ - sessionId: source.id, - entryId: "e1", - mode: "before", - }); - expect(Option.isNone(empty.leafEntryId)).toBe(true); - expect(yield* session.path(empty.id)).toHaveLength(0); + const sql = yield* SqlClient.SqlClient; + const sessions = yield* Session.Service; + const threads = yield* Thread.Service; + const events = yield* Event.Service; + const { sessionId, threadId } = yield* createSession("/doomed"); + const kept = yield* createSession("/kept"); + yield* threads.append(userEntry(threadId, yield* nextSeq(sessionId), "e1", "bye")); + + expect(yield* sessions.delete(sessionId)).toBe(true); + + const [left] = yield* sql>` + SELECT + (SELECT count(*) FROM session WHERE id = ${sessionId}) AS session, + (SELECT count(*) FROM thread WHERE session_id = ${sessionId}) AS thread, + (SELECT count(*) FROM entry WHERE session_id = ${sessionId}) AS entry, + (SELECT count(*) FROM document WHERE session_id = ${sessionId}) AS document, + (SELECT count(*) FROM event WHERE aggregate_id = ${sessionId}) AS event, + (SELECT count(*) FROM sequence WHERE aggregate_id = ${sessionId}) AS sequence + `; + expect(left).toEqual({ session: 0, thread: 0, entry: 0, document: 0, event: 0, sequence: 0 }); + expect(yield* events.latestSequence(sessionId)).toBe(0); + expect(Option.isSome(yield* sessions.get(kept.sessionId))).toBe(true); + expect(yield* sessions.delete(sessionId)).toBe(false); }), ); @@ -448,24 +294,19 @@ describe("session", () => { it.effect("keeps the session's position under the new root", () => Effect.gen(function* () { const { from, to } = yield* seedPair(); - const session = yield* Session.Service; - const created = yield* session.create({ + const sessions = yield* Session.Service; + const { session: created } = yield* sessions.create({ spaceId: from.spaceId, directory: AbsolutePath.make("/old/app/packages/x"), - title: "t", }); - const moved = yield* session.relink({ sessionId: created.id, spaceId: to.spaceId }); + const moved = yield* sessions.relink({ sessionId: created.id, spaceId: to.spaceId }); expect(moved.spaceId).toBe(to.spaceId); expect(moved.directory).toBe("/new/app/packages/x"); // Sitting at the old root lands on the new root. - const atRoot = yield* session.create({ - spaceId: from.spaceId, - directory: from.location, - title: "t", - }); - const movedRoot = yield* session.relink({ sessionId: atRoot.id, spaceId: to.spaceId }); + const { session: atRoot } = yield* sessions.create({ spaceId: from.spaceId, directory: from.location }); + const movedRoot = yield* sessions.relink({ sessionId: atRoot.id, spaceId: to.spaceId }); expect(movedRoot.directory).toBe("/new/app"); }), ); @@ -473,21 +314,20 @@ describe("session", () => { it.effect("an explicit directory under the target wins; outside it is rejected", () => Effect.gen(function* () { const { from, to } = yield* seedPair(); - const session = yield* Session.Service; - const created = yield* session.create({ + const sessions = yield* Session.Service; + const { session: created } = yield* sessions.create({ spaceId: from.spaceId, directory: AbsolutePath.make("/old/app/pkg"), - title: "t", }); - const explicit = yield* session.relink({ + const explicit = yield* sessions.relink({ sessionId: created.id, spaceId: to.spaceId, directory: AbsolutePath.make("/new/app/other"), }); expect(explicit.directory).toBe("/new/app/other"); - const rejected = yield* session + const rejected = yield* sessions .relink({ sessionId: created.id, spaceId: to.spaceId, directory: AbsolutePath.make("/nope") }) .pipe(Effect.flip); expect(rejected).toMatchObject({ _tag: "RelinkError", reason: "directory_outside_space" }); @@ -498,17 +338,44 @@ describe("session", () => { Effect.gen(function* () { const { from } = yield* seedPair(); const other = yield* seedSpace({ location: "/other", projectId: "q", kind: "primary" }); - const session = yield* Session.Service; - const created = yield* session.create({ + const sessions = yield* Session.Service; + const { session: created } = yield* sessions.create({ spaceId: from.spaceId, directory: from.location }); + + const error = yield* sessions.relink({ sessionId: created.id, spaceId: other.spaceId }).pipe(Effect.flip); + expect(error).toMatchObject({ _tag: "RelinkError", reason: "project_scope_mismatch" }); + const row = Option.getOrThrow(yield* sessions.get(created.id)); + expect(row.spaceId).toBe(from.spaceId); + }), + ); + + it.effect("with a thread, places that thread and leaves the session where it was", () => + Effect.gen(function* () { + const { from, to } = yield* seedPair(); + const sessions = yield* Session.Service; + const documents = yield* Document.Service; + const { session: created, thread } = yield* sessions.create({ spaceId: from.spaceId, - directory: from.location, - title: "t", + directory: AbsolutePath.make("/old/app/packages/x"), }); + const threadId = ThreadSchema.ID.make(thread.id); - const error = yield* session.relink({ sessionId: created.id, spaceId: other.spaceId }).pipe(Effect.flip); - expect(error).toMatchObject({ _tag: "RelinkError", reason: "project_scope_mismatch" }); - const row = Option.getOrThrow(yield* session.get(created.id)); + const row = yield* sessions.relink({ sessionId: created.id, threadId, spaceId: to.spaceId }); expect(row.spaceId).toBe(from.spaceId); + expect(row.directory).toBe("/old/app/packages/x"); + expect((yield* documents.agent(threadId)).placement).toEqual({ + spaceId: to.spaceId, + directory: "/new/app/packages/x", + }); + const placed = Option.getOrThrow(yield* sessions.placement(threadId)); + expect(placed.space.id).toBe(to.spaceId); + expect(placed.directory).toBe("/new/app/packages/x"); + + // A thread of another session is not this session's to place. + const { thread: foreign } = yield* sessions.create({ spaceId: from.spaceId, directory: from.location }); + const error = yield* sessions + .relink({ sessionId: created.id, threadId: ThreadSchema.ID.make(foreign.id), spaceId: to.spaceId }) + .pipe(Effect.flip); + expect(error._tag).toBe("ThreadNotFoundError"); }), ); }); diff --git a/packages/harness/test/settings.hostdir.test.ts b/packages/harness/test/settings.hostdir.test.ts index d9d554e4..8f535693 100644 --- a/packages/harness/test/settings.hostdir.test.ts +++ b/packages/harness/test/settings.hostdir.test.ts @@ -100,12 +100,12 @@ describe("a session's host directory decides its settings", () => { await run( dirs, Effect.gen(function* () { - const session = yield* Session.create({}); + const { main: session } = yield* Session.create({}); yield* session.run("one"); // Not "low" (the process's own directory) and not "max" (its parent project). // Absent skips the walk; it does not pick a plausible starting point. expect(lastLevel()).toBe("off"); - expect((yield* session.info).hostDir).toBeUndefined(); + expect((yield* session.info).session.hostDir).toBeUndefined(); }), ); })); @@ -119,7 +119,7 @@ describe("a session's host directory decides its settings", () => { // `directory` names a place inside the session's space. Here it collides with a // real host project — the case that makes the bug silent, because the walk // succeeds and returns a stranger's settings. - const session = yield* Session.create({ directory: dirs.beta }); + const { main: session } = yield* Session.create({ directory: dirs.beta }); yield* session.run("one"); expect(lastLevel()).toBe("off"); }), @@ -132,17 +132,17 @@ describe("a session's host directory decides its settings", () => { await run( dirs, Effect.gen(function* () { - const session = yield* Session.create({}); + const { main: session } = yield* Session.create({}); yield* session.run("one"); expect(lastLevel()).toBe("off"); - yield* Session.link({ sessionId: session.id, hostDir: dirs.beta }); + yield* Session.link({ sessionId: session.sessionId, hostDir: dirs.beta }); yield* session.run("two"); expect(lastLevel()).toBe("medium"); - expect((yield* session.info).hostDir).toBe(dirs.beta); + expect((yield* session.info).session.hostDir).toBe(dirs.beta); // And back off again: clearing returns the session to the user layer. - yield* Session.link({ sessionId: session.id, hostDir: null }); + yield* Session.link({ sessionId: session.sessionId, hostDir: null }); yield* session.run("three"); expect(lastLevel()).toBe("off"); }), @@ -157,8 +157,8 @@ describe("a session's host directory decides its settings", () => { await run( dirs, Effect.gen(function* () { - const session = yield* Session.create({ hostDir: dirs.beta }); - sessionId = session.id; + const { main: session } = yield* Session.create({ hostDir: dirs.beta }); + sessionId = session.sessionId; }), { database }, ); @@ -168,7 +168,7 @@ describe("a session's host directory decides its settings", () => { dirs, Effect.gen(function* () { const session = yield* Session.attach({ sessionId: Session.SessionSchema.ID.make(sessionId) }); - expect((yield* session.info).hostDir).toBe(dirs.beta); + expect((yield* session.info).session.hostDir).toBe(dirs.beta); yield* session.run("one"); expect(lastLevel()).toBe("medium"); }), diff --git a/packages/harness/test/settings.live.test.ts b/packages/harness/test/settings.live.test.ts index f7d815c0..54538f8d 100644 --- a/packages/harness/test/settings.live.test.ts +++ b/packages/harness/test/settings.live.test.ts @@ -75,18 +75,18 @@ describe.each(LIVE)("settings against live $name", (live) => { await write(A); await Effect.runPromise( Effect.gen(function* () { - const handle = yield* Session.create({ + const { main: handle } = yield* Session.create({ directory: root, // Selection and matching controls come from the host file. }); const runtime = yield* SessionRuntime.Service; - yield* runtime.update(handle.id, { onPayload }); + yield* runtime.update(handle.sessionId, { onPayload }); yield* handle.run("Reply with the single word: one"); yield* Effect.promise(() => write(B)); yield* handle.run("Reply with the single word: two"); yield* Effect.promise(() => write(A)); yield* handle.run("Reply with the single word: three"); - for (const hydrated of yield* handle.path()) { + for (const hydrated of yield* handle.history()) { if (hydrated.entry.type !== "assistant") continue; expect(hydrated.entry.state).toBe("committed"); replies.push( diff --git a/packages/harness/test/settings.llm.test.ts b/packages/harness/test/settings.llm.test.ts index 895f6996..508ded07 100644 --- a/packages/harness/test/settings.llm.test.ts +++ b/packages/harness/test/settings.llm.test.ts @@ -6,7 +6,7 @@ import { llm, Model, Message } from "@codeworksh/aikit"; import { Effect } from "effect"; import { describe, expect, it } from "vite-plus/test"; import { LLM } from "../src/runner/llm.ts"; -import { SessionSchema } from "../src/session/schema.ts"; +import { ThreadSchema } from "../src/thread/schema.ts"; import { compose } from "../src/settings/resolve.ts"; import { merge } from "../src/settings/merge.ts"; import { defaults } from "../src/settings/schema.ts"; @@ -83,7 +83,7 @@ describe("settings at the LLM boundary", () => { const events = await Effect.runPromise( LLM.openWith({ authFile })( { - sessionId: SessionSchema.ID.create(), + threadId: ThreadSchema.ID.create(), provider: "openai-codex", model: model.id, resolvedModel: model, @@ -160,7 +160,7 @@ describe("settings at the LLM boundary", () => { const events = await Effect.runPromise( LLM.openWith({ authFile })( { - sessionId: SessionSchema.ID.create(), + threadId: ThreadSchema.ID.create(), provider: "github-copilot", model: model.id, resolvedModel: model, @@ -192,7 +192,7 @@ describe("settings at the LLM boundary", () => { const fromEnv = await Effect.runPromise( LLM.openWith({ authFile })( { - sessionId: SessionSchema.ID.create(), + threadId: ThreadSchema.ID.create(), provider: "github-copilot", model: model.id, resolvedModel: model, @@ -229,7 +229,7 @@ describe("settings at the LLM boundary", () => { expect(model).toBeDefined(); if (!model) return; const input: LLM.Input = { - sessionId: SessionSchema.ID.create(), + threadId: ThreadSchema.ID.create(), provider: "lmstudio", model: model.id, resolvedModel: model, @@ -307,7 +307,7 @@ describe("settings at the LLM boundary", () => { const events = await Effect.runPromise( LLM.open( { - sessionId: SessionSchema.ID.create(), + threadId: ThreadSchema.ID.create(), provider: selection.provider, model: selection.model, resolvedModel: await Effect.runPromise( diff --git a/packages/harness/test/settings.loop.test.ts b/packages/harness/test/settings.loop.test.ts index d40b80cf..c4b35dbc 100644 --- a/packages/harness/test/settings.loop.test.ts +++ b/packages/harness/test/settings.loop.test.ts @@ -85,7 +85,7 @@ describe("settings at turn boundaries", () => { return terminal(input, index, index === 1); }); yield* Effect.gen(function* () { - const handle = yield* Session.create({ directory: root }); + const { main: handle } = yield* Session.create({ directory: root }); yield* handle.prompt("first"); yield* Deferred.await(requestEntered); yield* Effect.promise(() => file(custom, "medium")); @@ -106,7 +106,7 @@ describe("settings at turn boundaries", () => { expect(inputs.at(-1)?.thinkingLevel).toBe("off"); // A virtual sandbox uses the same host settings, without any virtual settings file. const sandbox = yield* Sandbox.create({ driver: "memory" }); - const virtual = yield* Session.create({ sandbox }); + const { main: virtual } = yield* Session.create({ sandbox }); yield* virtual.run("virtual"); expect(inputs.at(-1)?.thinkingLevel).toBe("off"); }).pipe( @@ -192,7 +192,7 @@ describe("settings at turn boundaries", () => { return stream; }); yield* Effect.gen(function* () { - const handle = yield* Session.create({ directory: root }); + const { main: handle } = yield* Session.create({ directory: root }); yield* handle.run("parallel, including continuation"); yield* handle.run("fresh sequential"); expect(starts).toEqual([2, 2, 2]); @@ -250,7 +250,7 @@ describe("settings at turn boundaries", () => { return stream; }); yield* Effect.gen(function* () { - const handle = yield* Session.create({ directory: root }); + const { main: handle } = yield* Session.create({ directory: root }); const events = yield* Event.Service; yield* events.listen((event) => event.type === "session.llm.started" @@ -289,7 +289,7 @@ describe("settings at turn boundaries", () => { const open: LLM.Open = (input) => Effect.sync(() => (inputs.push(input), terminal(input, inputs.length))); await Effect.runPromise( Effect.gen(function* () { - const handle = yield* Session.create({ directory: root }); + const { main: handle } = yield* Session.create({ directory: root }); const events = yield* Event.Service; yield* events.listen((event) => Effect.sync(() => { @@ -357,11 +357,11 @@ describe("settings at turn boundaries", () => { return terminal(input, inputs.length, inputs.length === 1); }); yield* Effect.gen(function* () { - const handle = yield* Session.create({ directory: root }); + const { main: handle } = yield* Session.create({ directory: root }); yield* handle.prompt("first"); yield* Deferred.await(toolEntered); // A binding set during a tool batch must not disturb the turn already running. - yield* Session.attach({ sessionId: handle.id, thinkingLevel: "max" }); + yield* Session.attach({ sessionId: handle.sessionId, thinkingLevel: "max" }); yield* Deferred.succeed(toolRelease, undefined); yield* handle.wait(); expect(inputs.map((input) => input.thinkingLevel)).toEqual(["low", "max"]); @@ -403,12 +403,12 @@ describe("settings at turn boundaries", () => { await Effect.runPromise( Effect.gen(function* () { const sessionId = yield* Effect.gen(function* () { - const handle = yield* Session.create({ directory: root }); + const { main: handle } = yield* Session.create({ directory: root }); // A chosen thinking level is saved with the session; the file is read afresh. - yield* Session.attach({ sessionId: handle.id, thinkingLevel: "max" }); + yield* Session.attach({ sessionId: handle.sessionId, thinkingLevel: "max" }); yield* handle.run("first"); expect(inputs.at(-1)?.thinkingLevel).toBe("max"); - return handle.id; + return handle.sessionId; }).pipe(Effect.provide(runtime()), Effect.scoped); yield* Effect.promise(() => file(custom, "medium")); @@ -439,16 +439,16 @@ describe("settings at turn boundaries", () => { await Effect.runPromise( Effect.gen(function* () { yield* Effect.gen(function* () { - const handle = yield* Session.create({ directory: root }); + const { main: handle } = yield* Session.create({ directory: root }); yield* handle.run("first"); // Lookup fails before promotion; the admitted prompt remains queued. - expect(yield* handle.path()).toEqual([]); + expect(yield* handle.history()).toEqual([]); expect(inputs).toHaveLength(0); yield* Effect.promise(() => select("openai", "gpt-5.6-luna")); yield* handle.resume(); expect(inputs.at(-1)?.model).toBe("gpt-5.6-luna"); - const answered = yield* handle.path(); + const answered = yield* handle.history(); expect(answered.filter(({ entry }) => entry.type === "user")).toHaveLength(1); expect(answered.at(-1)?.entry.type).toBe("assistant"); expect(answered.at(-1)?.entry.state).toBe("committed"); @@ -484,7 +484,7 @@ describe("settings at turn boundaries", () => { return terminal(input, inputs.length); }); yield* Effect.gen(function* () { - const handle = yield* Session.create({ directory: root }); + const { main: handle } = yield* Session.create({ directory: root }); yield* handle.run("with A"); expect(inputs.at(-1)?.thinkingLevel).toBe("low"); @@ -543,12 +543,12 @@ describe("settings at turn boundaries", () => { ]; await Effect.runPromise( Effect.gen(function* () { - const handle = yield* Session.create({ directory: root }); + const { main: handle } = yield* Session.create({ directory: root }); const rows: Array> = []; for (const [index, item] of cases.entries()) { yield* Effect.promise(() => select(item)); yield* handle.run(`turn ${index}`); - const persisted = (yield* handle.path()).at(-1)?.entry; + const persisted = (yield* handle.history()).at(-1)?.entry; const envelope: { readonly thinkingLevel?: string } = JSON.parse(persisted?.data ?? "{}"); rows.push({ requested: item.thinkingLevel ?? defaults.model.thinkingLevel, @@ -562,7 +562,7 @@ describe("settings at turn boundaries", () => { }); } // A session's own choice is clamped the same way, and re-fitted on a model change. - yield* Session.attach({ sessionId: handle.id, thinkingLevel: "max" }); + yield* Session.attach({ sessionId: handle.sessionId, thinkingLevel: "max" }); yield* Effect.promise(() => select({ provider: "openai", id: "gpt-4o-mini" })); const chosenOff = (yield* Session.configuration(handle.id)).thinkingLevel; yield* Effect.promise(() => select({ provider: "openai", id: "gpt-5.6-luna" })); diff --git a/packages/harness/test/space.test.ts b/packages/harness/test/space.test.ts index 2c4276fd..0bf30fce 100644 --- a/packages/harness/test/space.test.ts +++ b/packages/harness/test/space.test.ts @@ -41,12 +41,12 @@ const seedProject = (id: string) => sql`INSERT INTO project (id, name, status, created_at, updated_at) VALUES (${id}, ${id}, 'active', 0, 0)`, ); -// The host is NULL; anything else must exist in sandbox_instance first. +// The host is NULL; anything else must exist in sandbox first. const seedEnv = (id: SandboxInstance.ID) => Effect.flatMap( SqlClient.SqlClient, (sql) => sql` - INSERT INTO sandbox_instance (id, driver, kind, ownership, status, state_observed_at, created_at, updated_at) + INSERT INTO sandbox (id, driver, kind, ownership, status, state_observed_at, created_at, updated_at) VALUES (${id}, 'memory', 'virtual', 'managed', 'online', 0, 0, 0) `, ); diff --git a/packages/harness/test/thread.test.ts b/packages/harness/test/thread.test.ts new file mode 100644 index 00000000..5c3aa5e5 --- /dev/null +++ b/packages/harness/test/thread.test.ts @@ -0,0 +1,604 @@ +import "./utils/env.ts"; +import { createAssistantMessageEventStream, Message } from "@codeworksh/aikit"; +import { Cause, DateTime, Deferred, Effect, Exit, Layer, Option, Schema } from "effect"; +import { SqlClient } from "effect/sql"; +import { describe, expect } from "vite-plus/test"; +import { Context } from "../src/context/context.ts"; +import { Control } from "../src/control.ts"; +import { Database } from "../src/db/db.ts"; +import { Document } from "../src/document/document.ts"; +import { Event } from "../src/event/event.ts"; +import { EventList } from "../src/event/list.ts"; +import { EventRegistry } from "../src/event/registry.ts"; +import { Global } from "../src/global.ts"; +import { builtins } from "../src/plugin/builtin.ts"; +import type { Plugin } from "../src/plugin/plugin.ts"; +import { RunnerExecute } from "../src/runner/execute.ts"; +import { RunnerExecution } from "../src/runner/execution.ts"; +import { Hooks } from "../src/runner/hooks.ts"; +import { LLM } from "../src/runner/llm.ts"; +import { Loop } from "../src/runner/loop.ts"; +import { SandboxController } from "../src/sandbox/control.ts"; +import { SandboxDriverRegistry } from "../src/sandbox/registry.ts"; +import { SessionMessageSchema } from "../src/session/message/schema.ts"; +import { PromptSchema } from "../src/session/prompt/schema.ts"; +import { SessionProjector } from "../src/session/projector.ts"; +import { SessionRuntime } from "../src/session/runtime.ts"; +import { SessionSchema } from "../src/session/schema.ts"; +import { Session } from "../src/session/session.ts"; +import { Settings } from "../src/settings/settings.ts"; +import { State } from "../src/state/state.ts"; +import { ThreadSchema } from "../src/thread/schema.ts"; +import { Thread } from "../src/thread/thread.ts"; +import * as Tool from "../src/tool/tool.ts"; +import { assistant } from "./fixtures/llm.ts"; +import { pooled } from "./fixtures/pool.ts"; +import { seedSpace } from "./fixtures/space.ts"; +import { testEffect } from "./utils/effect.ts"; + +/** What the model is asked to do next, per thread: reply, call `find`, or wait on a gate. */ +type Step = + | { readonly _tag: "reply"; readonly text: string } + | { readonly _tag: "find" } + | { readonly _tag: "gate"; readonly gate: Deferred.Deferred; readonly opened: Deferred.Deferred }; + +const script = new Map>(); +const contexts: Array<{ readonly threadId: string; readonly roles: ReadonlyArray; readonly texts: string }> = + []; +let responses = 0; + +const textOf = (message: Message.Context["messages"][number]) => + "parts" in message ? message.parts.flatMap((part) => (part.type === "text" ? [part.text] : [])).join(" ") : ""; + +const open: LLM.Open = (input, signal) => + Effect.gen(function* () { + const step = script.get(input.threadId)?.shift() ?? { _tag: "reply", text: "ok" }; + contexts.push({ + threadId: input.threadId, + roles: input.context.messages.map((message) => message.role), + texts: input.context.messages.map(textOf).join(" | "), + }); + responses += 1; + const message = + step._tag === "find" + ? assistant(input, responses, { + messageId: `assistant_${responses}`, + stopReason: "toolUse", + parts: [ + { + type: "toolCall", + callID: "call_find", + name: "find_tool", + arguments: { name: "web_fetch" }, + status: "pending", + time: { start: 1, end: 1 }, + }, + ], + }) + : assistant(input, responses, { + messageId: `assistant_${responses}`, + parts: [{ type: "text", text: step._tag === "reply" ? step.text : "released" }], + }); + const stream = createAssistantMessageEventStream(); + stream.push({ type: "start", partial: message }); + const done = Effect.sync(() => + stream.push({ type: "done", reason: step._tag === "find" ? "toolUse" : "stop", message }), + ); + if (step._tag !== "gate") yield* done; + else { + // Held after `start`, so the turn's draft is open while the gate is shut; a stop aborts it. + yield* Effect.forkDetach(Deferred.await(step.gate).pipe(Effect.andThen(done))); + const abort = () => + stream.push({ type: "error", reason: "aborted", error: { ...message, stopReason: "aborted", parts: [] } }); + signal.addEventListener("abort", abort, { once: true }); + } + if (step._tag === "gate") yield* Deferred.succeed(step.opened, undefined); + return stream; + }); + +// Stands in for `tool_search`: it changes the calling thread's `agent` inside its turn. +let configureFromTool: ((name: string) => Effect.Effect) | undefined; +const findTool: Plugin = { + id: "test.tool.find", + kind: "tool", + setup: (ctx) => { + const sessionId = ctx.sessionId; + const threadId = ctx.threadId; + ctx.plugin.tools.add( + Tool.register( + Tool.make({ + name: "find_tool", + description: "offers a deferred tool", + parameters: Schema.Struct({ name: Schema.String }), + success: Schema.String, + handler: ({ name }) => (configureFromTool?.(name) ?? Effect.void).pipe(Effect.as(`found ${name}`)), + }), + ), + ); + active = { sessionId, threadId }; + }, +}; +let active: { readonly sessionId: SessionSchema.ID; readonly threadId: ThreadSchema.ID } | undefined; + +const layer = () => { + const database = Database.layer(":memory:"); + const sandbox = SandboxController.layer({ hostCwd: "/" }).pipe( + Layer.provideMerge(SandboxDriverRegistry.layer()), + Layer.provideMerge(database), + ); + const seeded = pooled([...builtins, findTool]); + return Control.layer.pipe( + Layer.provideMerge( + RunnerExecute.layer.pipe( + Layer.provide(Loop.layer({ request: LLM.make(open) }).pipe(Layer.provide(Hooks.layer))), + ), + ), + Layer.provideMerge(State.layer({}, seeded.ref, seeded.references, seeded.follow, seeded.rebuild)), + Layer.provideMerge(SessionRuntime.layer), + Layer.provideMerge(Layer.succeed(Settings.Service, { load: () => Effect.succeed(Settings.defaults) })), + Layer.provideMerge(sandbox), + Layer.provideMerge(Context.layer), + Layer.provideMerge(SessionProjector.layer), + Layer.provideMerge(Session.storeLayer), + Layer.provideMerge(Event.layer), + Layer.provideMerge(EventRegistry.layer()), + Layer.provideMerge(database), + Layer.provideMerge(Layer.succeed(Global.Service, Global.make({ home: "/unused/home" }))), + ); +}; + +const { effect: it } = testEffect(layer()); + +const reset = Effect.sync(() => { + script.clear(); + contexts.length = 0; + responses = 0; + configureFromTool = undefined; + active = undefined; +}); + +const createSession = Effect.gen(function* () { + const sessions = yield* Session.Service; + const { spaceId, location } = yield* seedSpace({ location: process.cwd(), projectId: "p" }); + const { session, thread } = yield* sessions.create({ spaceId, directory: location }); + return { sessionId: SessionSchema.ID.make(session.id), main: ThreadSchema.ID.make(thread.id) }; +}); + +const configure = ( + sessionId: SessionSchema.ID, + threadId: ThreadSchema.ID, + change: EventList.ThreadConfigured["data"]["change"], +) => + Effect.gen(function* () { + const events = yield* Event.Service; + yield* events.publish(EventList.ThreadConfigured, { + timestamp: yield* DateTime.now, + sessionId, + threadId, + change, + }); + }); + +const run = (sessionId: SessionSchema.ID, threadId: ThreadSchema.ID, text: string) => + Effect.gen(function* () { + const control = yield* Control.Service; + return yield* control.run({ + sessionId, + threadId, + prompt: PromptSchema.Prompt.fromText(text), + delivery: "followUp", + }); + }); + +/** Ids in the order first seen, so artifacts read `T1`, `E3` rather than uuids. */ +const aliases = () => { + const names = new Map(); + const counters = new Map(); + return (prefix: string, id: string) => { + const known = names.get(id); + if (known !== undefined) return known; + const next = (counters.get(prefix) ?? 0) + 1; + counters.set(prefix, next); + const name = `${prefix}${next}`; + names.set(id, name); + return name; + }; +}; + +const snapshot = Effect.fnUntraced(function* (sessionId: SessionSchema.ID) { + const sql = yield* SqlClient.SqlClient; + const name = aliases(); + const threads = yield* sql<{ + id: string; + originKind: string; + originThreadId: string | null; + originEntryId: string | null; + createdSeq: number; + title: string | null; + }>`SELECT * FROM thread WHERE session_id = ${sessionId} ORDER BY created_seq`; + for (const thread of threads) name("T", thread.id); + const entries = yield* sql<{ + id: string; + threadId: string; + seq: number; + committedSeq: number | null; + type: string; + state: string; + }>`SELECT * FROM entry WHERE session_id = ${sessionId} ORDER BY seq`; + for (const entry of entries) name("E", entry.id); + const documents = yield* sql<{ id: string; threadId: string; kind: string; createdSeq: number }>` + SELECT * FROM document WHERE session_id = ${sessionId} ORDER BY created_seq + `; + const log = yield* sql<{ seq: number; type: string; data: string }>` + SELECT seq, type, data FROM event WHERE aggregate_id = ${sessionId} ORDER BY seq + `; + return { + events: log.map((event) => { + const data = JSON.parse(event.data) as { readonly threadId?: string }; + return `${event.seq} ${event.type}${data.threadId === undefined ? "" : ` ${name("T", data.threadId)}`}`; + }), + threads: threads.map((thread) => ({ + thread: name("T", thread.id), + origin: + thread.originKind === "main" + ? "main" + : `${thread.originKind} ${name("T", thread.originThreadId!)} at ${name("E", thread.originEntryId!)}`, + createdSeq: thread.createdSeq, + title: thread.title, + })), + entries: entries.map( + (entry) => + `${name("E", entry.id)} ${name("T", entry.threadId)} ${entry.type} seq=${entry.seq} committed=${entry.committedSeq} ${entry.state}`, + ), + documents: yield* Effect.forEach(documents, (document) => + Effect.gen(function* () { + const revisions = yield* sql<{ seq: number; value: string }>` + SELECT seq, value FROM revision WHERE document_id = ${document.id} ORDER BY seq + `; + return { + thread: name("T", document.threadId), + kind: document.kind, + revisions: revisions.map((revision) => `${revision.seq} ${revision.value}`), + }; + }), + ), + }; +}); + +const failureOf = (exit: Exit.Exit) => + Exit.isFailure(exit) ? Option.getOrUndefined(Exit.findErrorOption(exit)) : undefined; + +const artifact = (name: string, value: unknown) => + Effect.promise(() => + expect(`${JSON.stringify(value, null, "\t")}\n`).toMatchFileSnapshot(`./__artifacts__/${name}`), + ); + +describe("threads", () => { + it( + "walkthrough: a fork cuts at the committed seq and copies agent as of the cut", + Effect.gen(function* () { + yield* reset; + const threads = yield* Thread.Service; + const documents = yield* Document.Service; + const { sessionId, main } = yield* createSession; + + yield* configure(sessionId, main, { model: { provider: "openai", id: "gpt-5.4" } }); + const events = yield* Event.Service; + configureFromTool = (name) => + Effect.gen(function* () { + const current = yield* documents.agent(active!.threadId); + yield* events.publish(EventList.ThreadConfigured, { + timestamp: yield* DateTime.now, + sessionId: active!.sessionId, + threadId: active!.threadId, + change: { modelTools: { add: [...(current.modelTools?.add ?? []), name] } }, + }); + }); + script.set(main, [ + { _tag: "find" }, + { _tag: "reply", text: "login added" }, + { _tag: "reply", text: "tests added" }, + ]); + yield* run(sessionId, main, "Add login"); + yield* run(sessionId, main, "Now add tests"); + + const line = yield* threads.line(main); + expect(line.map(({ entry }) => entry.type)).toEqual(["user", "assistant", "assistant", "user", "assistant"]); + const [, e2, e3] = line; + const forked = yield* threads.fork({ threadId: main, entryId: e3!.entry.id }); + const t2 = ThreadSchema.ID.make(forked.thread.id); + + // The fork's agent is the source's as of E3's commit: gpt, with the tool found during E2. + expect(yield* documents.agent(t2)).toEqual({ + model: { provider: "openai", id: "gpt-5.4" }, + modelTools: { add: ["web_fetch"] }, + }); + // What E2's request was built with: gpt, no tool yet. + expect(yield* documents.agentAsOf(main, e2!.entry.seq)).toEqual({ + model: { provider: "openai", id: "gpt-5.4" }, + }); + + script.set(t2, [{ _tag: "reply", text: "oauth it is" }]); + yield* run(sessionId, t2, "Use OAuth instead"); + yield* configure(sessionId, t2, { model: { provider: "anthropic", id: "claude-sonnet-4-5" } }); + expect((yield* documents.agent(t2)).model?.provider).toBe("anthropic"); + expect((yield* documents.agent(main)).model?.provider).toBe("openai"); + + // Forking at E2 cuts at its commit, after the tool's change. + const atE2 = yield* threads.fork({ threadId: main, entryId: e2!.entry.id }); + expect((yield* documents.agent(ThreadSchema.ID.make(atE2.thread.id))).modelTools).toEqual({ + add: ["web_fetch"], + }); + + // T2 sees T1 through E3, then its own prompt; T1's later entries are not its history. + const t2Context = contexts.filter((one) => one.threadId === t2).at(-1)!; + expect(t2Context.texts.includes("Now add tests")).toBe(false); + yield* artifact("thread.walkthrough.json", { + ...(yield* snapshot(sessionId)), + t2Context: t2Context.roles, + }); + }), + ); + + it( + "fork rules: refuses drafts and aborted entries, re-points inherited origins, and before()", + Effect.gen(function* () { + yield* reset; + const threads = yield* Thread.Service; + const control = yield* Control.Service; + const { sessionId, main } = yield* createSession; + yield* run(sessionId, main, "first"); + yield* run(sessionId, main, "second"); + const [e1, e2, e3, e4] = yield* threads.line(main); + + // An inherited entry re-points the origin to the thread that wrote it. + const t2 = ThreadSchema.ID.make((yield* threads.fork({ threadId: main, entryId: e4!.entry.id })).thread.id); + const fromInherited = yield* threads.fork({ threadId: t2, entryId: e1!.entry.id }); + expect(fromInherited.thread.originThreadId).toEqual(Option.some(main)); + + // before(): stops at the predecessor and hands back the prompt to edit. + const before = yield* threads.fork({ threadId: main, entryId: e3!.entry.id, mode: "before" }); + expect(before.thread.originEntryId).toEqual(Option.some(e2!.entry.id)); + expect(before.text).toBe("second"); + const firstOfMain = yield* threads + .fork({ threadId: main, entryId: e1!.entry.id, mode: "before" }) + .pipe(Effect.exit); + const notUser = yield* threads + .fork({ threadId: main, entryId: e4!.entry.id, mode: "before" }) + .pipe(Effect.exit); + const elsewhere = yield* threads.fork({ threadId: main, entryId: "missing" }).pipe(Effect.exit); + + // A draft is still changing: no fork of in-flight state. + const gate = yield* Deferred.make(); + const opened = yield* Deferred.make(); + script.set(main, [{ _tag: "gate", gate, opened }]); + yield* control.prompt({ sessionId, threadId: main, prompt: PromptSchema.Prompt.fromText("third") }); + yield* Deferred.await(opened); + const draft = yield* threads.draft(main).pipe( + Effect.tap(() => Effect.yieldNow), + Effect.repeat({ until: Option.isSome }), + Effect.map(Option.getOrThrow), + ); + const atDraft = yield* threads.fork({ threadId: main, entryId: draft.entry.id }).pipe(Effect.exit); + + // An interrupted draft is aborted: not part of the context, so not a fork point either. + yield* control.interrupt(main, { awaitSettlement: true }); + const aborted = yield* threads.entry(draft.entry.id).pipe(Effect.map(Option.getOrThrow)); + const atAborted = yield* threads.fork({ threadId: main, entryId: draft.entry.id }).pipe(Effect.exit); + + const reasons = [firstOfMain, notUser, elsewhere, atDraft, atAborted].map( + (exit) => (failureOf(exit) as Thread.ForkError | undefined)?.reason, + ); + expect(reasons).toEqual([ + "nothing_before_entry", + "not_a_user_entry", + "entry_not_found", + "entry_not_committed", + "entry_not_committed", + ]); + expect(aborted.entry.state).toBe("aborted"); + yield* artifact("thread.fork.json", { reasons, ...(yield* snapshot(sessionId)) }); + }), + ); + + it( + "two threads of one session run at the same time", + Effect.gen(function* () { + yield* reset; + const threads = yield* Thread.Service; + const control = yield* Control.Service; + const execution = yield* RunnerExecution.Service; + const { sessionId, main } = yield* createSession; + yield* run(sessionId, main, "base"); + const [first] = yield* threads.line(main); + const t2 = ThreadSchema.ID.make((yield* threads.fork({ threadId: main, entryId: first!.entry.id })).thread.id); + + const gates = yield* Effect.forEach([main, t2], (threadId, index) => + Effect.gen(function* () { + const gate = yield* Deferred.make(); + const opened = yield* Deferred.make(); + script.set(threadId, [{ _tag: "gate", gate, opened }]); + yield* control.prompt({ sessionId, threadId, prompt: PromptSchema.Prompt.fromText(`go ${index + 1}`) }); + return { gate, opened }; + }), + ); + // Both requests are open at once: neither thread waits on the other. + yield* Effect.forEach(gates, ({ opened }) => Deferred.await(opened)); + const both = yield* execution.active; + expect(both.has(main) && both.has(t2)).toBe(true); + yield* Effect.forEach(gates, ({ gate }) => Deferred.succeed(gate, undefined)); + yield* Effect.forEach([main, t2], (threadId) => control.wait(threadId)); + + const own = (threadId: ThreadSchema.ID) => + threads + .line(threadId) + .pipe( + Effect.map((line) => + line + .filter(({ entry }) => entry.threadId === threadId) + .map(({ entry }) => `${entry.type} ${entry.state}`), + ), + ); + const result = { bothActive: true, main: yield* own(main), fork: yield* own(t2) }; + expect(result.main).toEqual([ + "user committed", + "assistant committed", + "user committed", + "assistant committed", + ]); + expect(result.fork).toEqual(["user committed", "assistant committed"]); + // Seqs interleave between the two runs, so the artifact holds per-thread facts only. + yield* artifact("thread.concurrent.json", result); + }), + ); + + it( + "a reset starts the context over, carrying its handoff", + Effect.gen(function* () { + yield* reset; + const threads = yield* Thread.Service; + const events = yield* Event.Service; + const { sessionId, main } = yield* createSession; + yield* run(sessionId, main, "before the reset"); + // No reset event exists yet; the entry is written at a seq of its own, as an event would. + const seq = (yield* events.latestSequence(sessionId)) + 1; + yield* events.advance(sessionId, seq); + yield* threads.append({ + id: "reset_1", + threadId: main, + seq, + type: "reset", + data: JSON.stringify({ handoff: "keep the login work" }), + }); + yield* run(sessionId, main, "after the reset"); + const last = contexts.at(-1)!; + expect(last.roles).toEqual(["user", "user"]); + expect(last.texts.includes("before the reset")).toBe(false); + expect(last.texts.includes("keep the login work")).toBe(true); + yield* artifact("thread.reset.json", { + context: { roles: last.roles, texts: last.texts }, + ...(yield* snapshot(sessionId)), + }); + }), + ); + + it( + "recovery fails a thread's open draft as interrupted and answers again", + Effect.gen(function* () { + yield* reset; + const threads = yield* Thread.Service; + const control = yield* Control.Service; + const events = yield* Event.Service; + const { sessionId, main } = yield* createSession; + yield* run(sessionId, main, "base"); + const [first] = yield* threads.line(main); + const t2 = ThreadSchema.ID.make((yield* threads.fork({ threadId: main, entryId: first!.entry.id })).thread.id); + + // What a process dying mid-turn leaves: a promoted prompt, an open draft, the run still marked. + const timestamp = yield* DateTime.now; + const prompt = PromptSchema.Prompt.fromText("cut off"); + const messageId = SessionMessageSchema.ID.make("prompt_cut"); + const base = { timestamp, sessionId, threadId: t2, messageId, prompt, delivery: "followUp" as const }; + yield* events.publish(EventList.PromptAdmitted, base); + yield* events.publish(EventList.Prompted, base); + yield* events.publish(EventList.LLMStarted, { + timestamp, + sessionId, + threadId: t2, + messageId: SessionMessageSchema.ID.make("dangling"), + message: Message.createAssistantMessage({ + messageId: "dangling", + role: "assistant", + protocol: "openai", + provider: { id: "test", name: "test", source: "custom", env: [] }, + model: "test", + thinkingLevel: "off", + usage: { + input: 0, + output: 0, + cacheRead: 0, + cacheWrite: 0, + totalTokens: 0, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, + }, + stopReason: "stop", + time: { created: 0, completed: 0 }, + parts: [{ type: "text", text: "half a rep" }], + }), + }); + yield* threads.markRunning(t2); + + script.set(t2, [{ _tag: "reply", text: "answered again" }]); + yield* control.resumeUnfinished; + + const own = (yield* threads.line(t2)) + .filter(({ entry }) => entry.threadId === t2) + .map(({ entry }) => `${entry.type} ${entry.state}`); + expect(own).toEqual(["user committed", "assistant aborted", "assistant committed"]); + const thread = Option.getOrThrow(yield* threads.get(t2)); + expect(Option.getOrUndefined(thread.execution)).toBeUndefined(); + expect(thread.restarts).toBe(0); + // Only the thread that was cut off ran again, from its committed entries: the inherited + // prompt and its own, without the aborted draft. + expect(contexts.filter((one) => one.threadId === main)).toHaveLength(1); + expect(contexts.at(-1)!.roles).toEqual(["user", "user"]); + yield* artifact("thread.recovery.json", yield* snapshot(sessionId)); + }), + ); + + it( + "constraints refuse a second main, two entries per thread per event, two revisions per document per event", + Effect.gen(function* () { + yield* reset; + const sql = yield* SqlClient.SqlClient; + const { sessionId, main } = yield* createSession; + yield* run(sessionId, main, "hello"); + const entry = (yield* sql<{ + seq: number; + sessionId: string; + }>`SELECT * FROM entry WHERE thread_id = ${main}`)[0]!; + const document = (yield* sql<{ id: string }>`SELECT id FROM document WHERE thread_id = ${main}`)[0]!; + const refused = (statement: Effect.Effect) => + statement.pipe( + Effect.exit, + Effect.map((exit) => + Exit.isFailure(exit) + ? (/constraint failed[^\n]*/.exec(Cause.pretty(exit.cause))?.[0] ?? Cause.pretty(exit.cause)) + : "accepted", + ), + ); + const results = { + secondMain: yield* refused(sql` + INSERT INTO thread (id, session_id, origin_kind, created_seq, created_at, updated_at) + VALUES ('thr_second', ${sessionId}, 'main', 99, 0, 0) + `), + twoEntriesOneEvent: yield* refused(sql` + INSERT INTO entry (id, session_id, thread_id, seq, committed_seq, type, state, data, created_at, updated_at) + VALUES ('entry_twin', ${sessionId}, ${main}, ${entry.seq}, ${entry.seq}, 'custom', 'committed', '{}', 0, 0) + `), + twoRevisionsOneEvent: yield* refused(sql` + INSERT INTO revision (document_id, seq, version, value) VALUES (${document.id}, 1, 1, '{}') + `), + draftWithCommit: yield* refused(sql` + INSERT INTO entry (id, session_id, thread_id, seq, committed_seq, type, state, data, created_at, updated_at) + VALUES ('entry_draft', ${sessionId}, ${main}, 500, 500, 'assistant', 'draft', '{}', 0, 0) + `), + forkWithoutCut: yield* refused(sql` + INSERT INTO thread (id, session_id, origin_kind, origin_thread_id, created_seq, created_at, updated_at) + VALUES ('thr_nocut', ${sessionId}, 'fork', ${main}, 99, 0, 0) + `), + }; + expect(Object.values(results).every((result) => result.includes("constraint failed"))).toBe(true); + // Below the index: a seq under the thread's latest is refused before the insert. + const threads = yield* Thread.Service; + const stale = yield* threads + .append({ id: "entry_stale", threadId: main, seq: entry.seq - 1, type: "custom", data: "{}" }) + .pipe(Effect.flip); + expect(stale._tag).toBe("InvalidEntryDataError"); + yield* artifact("thread.constraints.json", { + ...results, + staleSeq: stale._tag === "InvalidEntryDataError" ? stale.reason : stale._tag, + }); + }), + ); +}); diff --git a/packages/harness/test/tools.edit.test.ts b/packages/harness/test/tools.edit.test.ts index a244ab8a..5ad48bb6 100644 --- a/packages/harness/test/tools.edit.test.ts +++ b/packages/harness/test/tools.edit.test.ts @@ -241,9 +241,12 @@ const cases: ReadonlyArray<{ readonly name: string; readonly before: string | nu const editAll = (root: string, custom: string) => Effect.gen(function* () { - const session = yield* Session.create({ directory: root, model: { provider: "openai", id: "gpt-5.6-luna" } }); + const { main: session } = yield* Session.create({ + directory: root, + model: { provider: "openai", id: "gpt-5.6-luna" }, + }); yield* session.run("Edit the files."); - const path = yield* session.path(); + const path = yield* session.history(); expect(path.every((entry) => entry.entry.state === "committed")).toBe(true); const messages = yield* Effect.forEach(path, ContextCodec.decodeMessage); return messages.flatMap((message) => message.parts.filter((part) => part.type === "toolCall")); diff --git a/packages/harness/test/tools.empty.live.test.ts b/packages/harness/test/tools.empty.live.test.ts index eb19068f..e00a0a5c 100644 --- a/packages/harness/test/tools.empty.live.test.ts +++ b/packages/harness/test/tools.empty.live.test.ts @@ -50,9 +50,12 @@ describe.each(direct)("parameterless tool against live $name", (live) => { await fs.mkdir(work, { recursive: true }); const { calls, reply } = await Effect.gen(function* () { - const session = yield* Session.create({ directory: work, model: { provider: live.provider, id: live.id } }); + const { main: session } = yield* Session.create({ + directory: work, + model: { provider: live.provider, id: live.id }, + }); yield* session.run("Call the finite tool, then reply with exactly the token it returned and nothing else."); - const path = yield* session.path(); + const path = yield* session.history(); const messages = yield* Effect.forEach(path, ContextCodec.decodeMessage); const parts = messages.flatMap((message) => message.parts); const last = messages.findLast((message) => message.parts.some((part) => part.type === "text")); diff --git a/packages/harness/test/tools.read.test.ts b/packages/harness/test/tools.read.test.ts index 0417ca6d..79090e26 100644 --- a/packages/harness/test/tools.read.test.ts +++ b/packages/harness/test/tools.read.test.ts @@ -135,9 +135,12 @@ const cases: ReadonlyArray<{ /** Every case as one turn of parallel `read` calls through plugin setup, the local mount, and durable settlement. */ const readAll = (root: string, custom: string) => Effect.gen(function* () { - const session = yield* Session.create({ directory: root, model: { provider: "openai", id: "gpt-5.6-luna" } }); + const { main: session } = yield* Session.create({ + directory: root, + model: { provider: "openai", id: "gpt-5.6-luna" }, + }); yield* session.run("Read the files."); - const path = yield* session.path(); + const path = yield* session.history(); expect(path.every((entry) => entry.entry.state === "committed")).toBe(true); const messages = yield* Effect.forEach(path, ContextCodec.decodeMessage); return messages.flatMap((message) => message.parts.filter((part) => part.type === "toolCall")); diff --git a/packages/harness/test/tools.read.vision.test.ts b/packages/harness/test/tools.read.vision.test.ts index 7f2d26bd..4b5ad112 100644 --- a/packages/harness/test/tools.read.vision.test.ts +++ b/packages/harness/test/tools.read.vision.test.ts @@ -102,7 +102,7 @@ describe("read on a text-only model, then a model switch", () => { await Effect.runPromise( Effect.gen(function* () { yield* Effect.promise(() => select(textOnly)); - const handle = yield* Session.create({ directory: root }); + const { main: handle } = yield* Session.create({ directory: root }); yield* handle.run("read the image"); yield* Effect.promise(() => select(vision)); yield* handle.run("what do you see now?"); diff --git a/packages/harness/test/tools.search.live.test.ts b/packages/harness/test/tools.search.live.test.ts index 93213de9..57b92833 100644 --- a/packages/harness/test/tools.search.live.test.ts +++ b/packages/harness/test/tools.search.live.test.ts @@ -53,9 +53,12 @@ describe.each(direct)("search tools against live $name", (live) => { } const { calls, reply } = await Effect.gen(function* () { - const session = yield* Session.create({ directory: work, model: { provider: live.provider, id: live.id } }); + const { main: session } = yield* Session.create({ + directory: work, + model: { provider: live.provider, id: live.id }, + }); yield* session.run(PROMPT); - const path = yield* session.path(); + const path = yield* session.history(); const messages = yield* Effect.forEach(path, ContextCodec.decodeMessage); const parts = messages.flatMap((message) => message.parts); const last = messages.findLast((message) => message.parts.some((part) => part.type === "text")); diff --git a/packages/harness/test/tools.write.test.ts b/packages/harness/test/tools.write.test.ts index ef95c368..92c7894d 100644 --- a/packages/harness/test/tools.write.test.ts +++ b/packages/harness/test/tools.write.test.ts @@ -40,9 +40,12 @@ const cases: ReadonlyArray<{ const writeAll = (root: string, custom: string) => Effect.gen(function* () { - const session = yield* Session.create({ directory: root, model: { provider: "openai", id: "gpt-5.6-luna" } }); + const { main: session } = yield* Session.create({ + directory: root, + model: { provider: "openai", id: "gpt-5.6-luna" }, + }); yield* session.run("Write the files."); - const path = yield* session.path(); + const path = yield* session.history(); expect(path.every((entry) => entry.entry.state === "committed")).toBe(true); const messages = yield* Effect.forEach(path, ContextCodec.decodeMessage); return messages.flatMap((message) => message.parts.filter((part) => part.type === "toolCall")); diff --git a/packages/plugin/src/ids.ts b/packages/plugin/src/ids.ts index 315b8d75..6d0b4f99 100644 --- a/packages/plugin/src/ids.ts +++ b/packages/plugin/src/ids.ts @@ -5,7 +5,7 @@ import { withStatics } from "./schema.ts"; /** * The identifiers that cross the plugin boundary. * - * A plugin reads a session id off its context and a message id off every tool call, so both + * A plugin reads a session and thread id off its context and a message id off every tool call, so both * brands have to be the ones the harness itself mints -- a structurally identical brand declared * twice is two types, and a plugin would have to cast to hand either back. They live here, in the * package both sides depend on, rather than being re-declared on each side. @@ -32,6 +32,18 @@ export const SessionID = Schema.String.check(Schema.isStartingWith("ses")).pipe( ); export type SessionID = typeof SessionID.Type; +/** + * Thread identity: one linear transcript inside a session. Every session has a `main` thread; + * forks add more. Also the provider session and prompt-cache key, so it is fresh per thread. + */ +export const ThreadID = Schema.String.check(Schema.isStartingWith("thr")).pipe( + Schema.brand("Thread.ID"), + withStatics((schema) => ({ + create: () => schema.make("thr_" + uuidv7()), + })), +); +export type ThreadID = typeof ThreadID.Type; + /** * A conversation message id. * diff --git a/packages/plugin/src/plugin/context.ts b/packages/plugin/src/plugin/context.ts index 9e0e12da..522617e7 100644 --- a/packages/plugin/src/plugin/context.ts +++ b/packages/plugin/src/plugin/context.ts @@ -1,7 +1,7 @@ import type { Model } from "@codeworksh/aikit"; import type { Effect } from "effect"; import type { Data, Definition, Payload, PublishOptions } from "../event.ts"; -import type { SessionID } from "../ids.ts"; +import type { SessionID, ThreadID } from "../ids.ts"; import type { Info as LocationInfo } from "../location.ts"; import type { SandboxIO } from "../sandbox/io.ts"; import type { Info as SettingsInfo } from "../settings.ts"; @@ -44,6 +44,8 @@ export interface Paths { /** One object, shared by every plugin in one turn. */ export interface SharedPluginContext { readonly sessionId: SessionID; + /** The thread whose turn this is. `sandbox` and `location` are its placement, else the session's. */ + readonly threadId: ThreadID; readonly sandbox: SandboxIO.Identity; readonly paths: Paths; readonly location: LocationInfo; diff --git a/packages/plugin/src/plugin/tool.ts b/packages/plugin/src/plugin/tool.ts index 1b061835..5aa3b6b1 100644 --- a/packages/plugin/src/plugin/tool.ts +++ b/packages/plugin/src/plugin/tool.ts @@ -1,11 +1,12 @@ import { type Effect, Schema } from "effect"; import { AikitToolCallTerminalPart } from "../event.ts"; -import { MessageID, SessionID } from "../ids.ts"; +import { MessageID, SessionID, ThreadID } from "../ids.ts"; import { type AnyToolDef, type ModelContent, type RegisteredTool, ToolCallContext } from "../tool.ts"; export const ToolBefore = Schema.Struct({ ...ToolCallContext.fields, sessionId: SessionID, + threadId: ThreadID, messageId: MessageID, params: Schema.Unknown, });