Lineage
Lineage records how a session came to exist. It is a closed union with three states:
root no parent spawn fresh child, no copied conversation └── parent session created by a command or tool fork copied conversation through one anchor └── parent session later turns diverge independently
Creation methods
| Call | Lineage | History |
|---|---|---|
ctx.ai.sessions.create() | root | Empty |
session.spawn({ title }) | spawn | Empty child inheriting model and working directory; title is required and becomes the lineage label |
session.fork() | fork | Copies the parent through its current end |
turn.fork() | fork | Copies the parent through that exact completed turn |
agentHandle.spawn() | root | Empty session configured by the declared agent |
agentHandle.spawn() uses the workspace session catalog. It does not create a
child of the current session. Use ctx.session.spawn() when parent-child
lineage matters.
Background work from the current turn
This slash command forks the current conversation, runs work, then creates a report fork anchored after the completed work turn. The summary returns to the original conversation as a notification.
import { function defineMod(definition: ModDefinition): ModThe entry point of the SDK. A mod's module default-exports one `defineMod` call and nothing else. The returned value is data. It registers nothing, starts nothing, and touches no host state. The host folds it twice. At build time it runs the module in a throwaway sandbox and reads the value to derive the serializable manifest. At runtime it folds the same value again to wire your handlers into their lanes: ext-host for commands, tools, effects, and hooks; the sandboxed renderer for views. That double fold is why the module must be pure. It is re-executed to produce the manifest, so anything at the top level, a file read, a network call, a timer, a `console` line, runs during a build with no host attached. Put behavior inside handlers: `run`, `activate`, `handle`.defineMod, function slashCommand<const TId extends string, TResult>(slashCommandValue: SlashCommandInput<TId, TResult>): SlashCommand<TId, TResult>Declare a command the user types inside a chat session. The `id` is the name typed after the slash, `description` is the line shown in the typeahead, and `argumentHint` is the placeholder for whatever the user types after the name. `argument` declares whether that text is `'required'`, `'optional'`, or `'none'`. The host trims it, answers a missing required argument with the usage line, and hands the handler `{ ctx, argument }`, where `ctx` carries the AI session the command was typed into. Use this instead of `command` when the behavior belongs to a conversation and needs the session. Use `command` when it belongs to the workbench and runs from the palette. Use `tool` when the model, not the user, should decide to run it. Slash commands go in the same top-level `commands` array as palette commands.slashCommand } from '@modular/sdk'; const const task: SlashCommand<"task", string>task = function slashCommand<"task", string>(slashCommandValue: SlashCommandInput<"task", string>): SlashCommand<"task", string>Declare a command the user types inside a chat session. The `id` is the name typed after the slash, `description` is the line shown in the typeahead, and `argumentHint` is the placeholder for whatever the user types after the name. `argument` declares whether that text is `'required'`, `'optional'`, or `'none'`. The host trims it, answers a missing required argument with the usage line, and hands the handler `{ ctx, argument }`, where `ctx` carries the AI session the command was typed into. Use this instead of `command` when the behavior belongs to a conversation and needs the session. Use `command` when it belongs to the workbench and runs from the palette. Use `tool` when the model, not the user, should decide to run it. Slash commands go in the same top-level `commands` array as palette commands.slashCommand({ id: "task"id: 'task', description: stringdescription: 'Fork this conversation and run background work.', argumentHint?: string | undefinedargumentHint: '<prompt>', argument: "required"argument: 'required', run: (call: SlashCommandCall<string>) => Awaitable<string>run: async ({ SlashCommandCall<string>.argument: stringargument: prompt: stringprompt, ctx: SlashCommandContextctx }) => { const const work: AiSessionHandlework = await ctx: SlashCommandContextctx.session: AiSessionHandleThe chat session the command was typed into.session.AiSessionHandle.fork(config?: AiSessionForkConfig): Promise<AiSessionHandle>fork(); const const completedWork: AiTurnHandlecompletedWork = await const work: AiSessionHandlework.AiSessionHandle.send(message: AiPromptMessage, options?: AiSendOptions): Promise<AiTurnHandle>send({ AiPromptMessage.type: "prompt"type: 'prompt', AiPromptMessage.text: stringtext: prompt: stringprompt }); const const report: AiSessionHandlereport = await const completedWork: AiTurnHandlecompletedWork.AiTurnHandle.fork(config?: AiSessionForkConfig): Promise<AiSessionHandle>fork(); const const summary: AiRunResultsummary = await const report: AiSessionHandlereport.AiSessionHandle.run(prompt: string, options?: AiSendOptions): Promise<AiRunResult>run( 'Summarize the completed work, including files, commands, and results.' ); await ctx: SlashCommandContextctx.session: AiSessionHandleThe chat session the command was typed into.session.AiSessionHandle.notify(notification: AiNotification): Promise<void>Deliver a system notification at its declared attention level. Fire-and-forget.notify({ AiNotification.attention: AiAttentionattention: 'respond', AiNotification.title: stringtitle: `Task finished: ${prompt: stringprompt}`, AiNotification.body: stringbody: `${const summary: AiRunResultsummary.AiRunResult.text: stringtext}\n\nSession ${const work: AiSessionHandlework.AiSessionHandle.id: string & $brand<"SessionId">id}`, }); return `Started task ${const work: AiSessionHandlework.AiSessionHandle.id: string & $brand<"SessionId">id}.`; }, }); export default function defineMod(definition: ModDefinition): ModThe entry point of the SDK. A mod's module default-exports one `defineMod` call and nothing else. The returned value is data. It registers nothing, starts nothing, and touches no host state. The host folds it twice. At build time it runs the module in a throwaway sandbox and reads the value to derive the serializable manifest. At runtime it folds the same value again to wire your handlers into their lanes: ext-host for commands, tools, effects, and hooks; the sandboxed renderer for views. That double fold is why the module must be pure. It is re-executed to produce the manifest, so anything at the top level, a file read, a network call, a timer, a `console` line, runs during a build with no host attached. Put behavior inside handlers: `run`, `activate`, `handle`.defineMod({ metadata: ModMetadatametadata: { ModMetadata.displayName: stringdisplayName: 'Background Tasks', ModMetadata.description: stringdescription: 'Fork conversations into background work', }, commands?: readonly ModCommand<string>[] | undefinedcommands: [const task: SlashCommand<"task", string>task], });
The distinction between work.fork() and completedWork.fork() matters. The
turn handle is the exact anchor after the work finished. Forking the session
again only means "copy through the current end," which can change if another
turn enters the session first.
Conversation-relative commands such as /task require an existing
conversation with a completed turn. A fresh session has no conversation state
to branch from.
Read lineage
AiSessionLineage is discriminated by
kind:
| Kind | Important fields |
|---|---|
root | No parent fields |
spawn | parentSessionId, parentChatUri, depth, spawnedAt, trigger |
fork | parentSessionId, parentTurnOrdinal, parentRunnerId, depth, forkedAt |
Use session.parent() and session.children() for handles. Use
session.lineage when the creation record itself decides behavior.
import type { AiSessionHandle } from '@modular/sdk'; export function function describeOrigin(session: AiSessionHandle): stringdescribeOrigin(session: AiSessionHandlesession: AiSessionHandle): string { switch (session: AiSessionHandlesession.AiSessionHandle.lineage: AiSessionLineagelineage.kind: "fork" | "root" | "spawn"kind) { case 'root': return 'Root conversation'; case 'spawn': return `Spawned at depth ${session: AiSessionHandlesession.AiSessionHandle.lineage: { readonly kind: 'spawn'; readonly spawnedAt: string; readonly depth: number; readonly parentSessionId: SessionId; readonly parentChatUri: string; readonly trigger: AiSpawnTrigger; readonly label: string; }lineage.depth: numberdepth}`; case 'fork': return `Forked after parent turn ${session: AiSessionHandlesession.AiSessionHandle.lineage: { readonly kind: 'fork'; readonly depth: number; readonly forkedAt: string; readonly parentTurnOrdinal: number; readonly parentRunnerId: AiRunnerId; readonly parentSessionId: SessionId; readonly parentUserMessageId?: string; }lineage.parentTurnOrdinal: numberparentTurnOrdinal}`; } }
Do not flatten lineage into parentId?: string. That loses whether history was
copied, the exact fork anchor, and the command or tool that created a spawn.