@earendil-works/pi-agent-core 0.85.0 → 0.86.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -6
- package/dist/agent-loop.d.ts.map +1 -1
- package/dist/agent-loop.js +64 -22
- package/dist/agent-loop.js.map +1 -1
- package/dist/agent.d.ts +5 -3
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +15 -6
- package/dist/agent.js.map +1 -1
- package/dist/harness/config.d.ts +1 -1
- package/dist/harness/config.d.ts.map +1 -1
- package/dist/harness/config.js +10 -2
- package/dist/harness/config.js.map +1 -1
- package/dist/harness/env/nodejs.d.ts +2 -1
- package/dist/harness/env/nodejs.d.ts.map +1 -1
- package/dist/harness/env/nodejs.js +98 -27
- package/dist/harness/env/nodejs.js.map +1 -1
- package/dist/harness/execution/tools.d.ts +1 -1
- package/dist/harness/execution/tools.d.ts.map +1 -1
- package/dist/harness/execution/tools.js +0 -2
- package/dist/harness/execution/tools.js.map +1 -1
- package/dist/harness/messages.d.ts.map +1 -1
- package/dist/harness/messages.js +1 -0
- package/dist/harness/messages.js.map +1 -1
- package/dist/harness/pico3/bash.d.ts +10 -0
- package/dist/harness/pico3/bash.d.ts.map +1 -0
- package/dist/harness/pico3/bash.js +25 -0
- package/dist/harness/pico3/bash.js.map +1 -0
- package/dist/harness/pico3/bounded.d.ts +21 -0
- package/dist/harness/pico3/bounded.d.ts.map +1 -0
- package/dist/harness/pico3/bounded.js +98 -0
- package/dist/harness/pico3/bounded.js.map +1 -0
- package/dist/harness/pico3/chord.d.ts +41 -0
- package/dist/harness/pico3/chord.d.ts.map +1 -0
- package/dist/harness/pico3/chord.js +130 -0
- package/dist/harness/pico3/chord.js.map +1 -0
- package/dist/harness/pico3/context.d.ts +16 -0
- package/dist/harness/pico3/context.d.ts.map +1 -0
- package/dist/harness/pico3/context.js +88 -0
- package/dist/harness/pico3/context.js.map +1 -0
- package/dist/harness/pico3/harness.d.ts +197 -0
- package/dist/harness/pico3/harness.d.ts.map +1 -0
- package/dist/harness/pico3/harness.js +654 -0
- package/dist/harness/pico3/harness.js.map +1 -0
- package/dist/harness/pico3/hooks.d.ts +10 -0
- package/dist/harness/pico3/hooks.d.ts.map +1 -0
- package/dist/harness/pico3/hooks.js +33 -0
- package/dist/harness/pico3/hooks.js.map +1 -0
- package/dist/harness/pico3/index.d.ts +19 -0
- package/dist/harness/pico3/index.d.ts.map +1 -0
- package/dist/harness/pico3/index.js +15 -0
- package/dist/harness/pico3/index.js.map +1 -0
- package/dist/harness/pico3/jsonl.d.ts +58 -0
- package/dist/harness/pico3/jsonl.d.ts.map +1 -0
- package/dist/harness/pico3/jsonl.js +325 -0
- package/dist/harness/pico3/jsonl.js.map +1 -0
- package/dist/harness/pico3/kinds/collapse.d.ts +49 -0
- package/dist/harness/pico3/kinds/collapse.d.ts.map +1 -0
- package/dist/harness/pico3/kinds/collapse.js +191 -0
- package/dist/harness/pico3/kinds/collapse.js.map +1 -0
- package/dist/harness/pico3/kinds/entries.d.ts +78 -0
- package/dist/harness/pico3/kinds/entries.d.ts.map +1 -0
- package/dist/harness/pico3/kinds/entries.js +13 -0
- package/dist/harness/pico3/kinds/entries.js.map +1 -0
- package/dist/harness/pico3/kinds/frames.d.ts +9 -0
- package/dist/harness/pico3/kinds/frames.d.ts.map +1 -0
- package/dist/harness/pico3/kinds/frames.js +75 -0
- package/dist/harness/pico3/kinds/frames.js.map +1 -0
- package/dist/harness/pico3/kinds/generation.d.ts +94 -0
- package/dist/harness/pico3/kinds/generation.d.ts.map +1 -0
- package/dist/harness/pico3/kinds/generation.js +504 -0
- package/dist/harness/pico3/kinds/generation.js.map +1 -0
- package/dist/harness/pico3/kinds/job.d.ts +44 -0
- package/dist/harness/pico3/kinds/job.d.ts.map +1 -0
- package/dist/harness/pico3/kinds/job.js +143 -0
- package/dist/harness/pico3/kinds/job.js.map +1 -0
- package/dist/harness/pico3/kinds/plugin.d.ts +15 -0
- package/dist/harness/pico3/kinds/plugin.d.ts.map +1 -0
- package/dist/harness/pico3/kinds/plugin.js +35 -0
- package/dist/harness/pico3/kinds/plugin.js.map +1 -0
- package/dist/harness/pico3/kinds/post-tools.d.ts +22 -0
- package/dist/harness/pico3/kinds/post-tools.d.ts.map +1 -0
- package/dist/harness/pico3/kinds/post-tools.js +145 -0
- package/dist/harness/pico3/kinds/post-tools.js.map +1 -0
- package/dist/harness/pico3/kinds/task-api.d.ts +3 -0
- package/dist/harness/pico3/kinds/task-api.d.ts.map +1 -0
- package/dist/harness/pico3/kinds/task-api.js +45 -0
- package/dist/harness/pico3/kinds/task-api.js.map +1 -0
- package/dist/harness/pico3/kinds/tool.d.ts +43 -0
- package/dist/harness/pico3/kinds/tool.d.ts.map +1 -0
- package/dist/harness/pico3/kinds/tool.js +373 -0
- package/dist/harness/pico3/kinds/tool.js.map +1 -0
- package/dist/harness/pico3/membrane.d.ts +23 -0
- package/dist/harness/pico3/membrane.d.ts.map +1 -0
- package/dist/harness/pico3/membrane.js +141 -0
- package/dist/harness/pico3/membrane.js.map +1 -0
- package/dist/harness/pico3/memory.d.ts +53 -0
- package/dist/harness/pico3/memory.d.ts.map +1 -0
- package/dist/harness/pico3/memory.js +264 -0
- package/dist/harness/pico3/memory.js.map +1 -0
- package/dist/harness/pico3/scheduler.d.ts +46 -0
- package/dist/harness/pico3/scheduler.d.ts.map +1 -0
- package/dist/harness/pico3/scheduler.js +438 -0
- package/dist/harness/pico3/scheduler.js.map +1 -0
- package/dist/harness/pico3/session.d.ts +263 -0
- package/dist/harness/pico3/session.d.ts.map +1 -0
- package/dist/harness/pico3/session.js +1347 -0
- package/dist/harness/pico3/session.js.map +1 -0
- package/dist/harness/pico3/system.d.ts +126 -0
- package/dist/harness/pico3/system.d.ts.map +1 -0
- package/dist/harness/pico3/system.js +244 -0
- package/dist/harness/pico3/system.js.map +1 -0
- package/dist/harness/pico3/types.d.ts +973 -0
- package/dist/harness/pico3/types.d.ts.map +1 -0
- package/dist/harness/pico3/types.js +96 -0
- package/dist/harness/pico3/types.js.map +1 -0
- package/dist/harness/pico3/view.d.ts +34 -0
- package/dist/harness/pico3/view.d.ts.map +1 -0
- package/dist/harness/pico3/view.js +417 -0
- package/dist/harness/pico3/view.js.map +1 -0
- package/dist/harness/runtime/drive/boundary.d.ts.map +1 -1
- package/dist/harness/runtime/drive/boundary.js +6 -3
- package/dist/harness/runtime/drive/boundary.js.map +1 -1
- package/dist/harness/runtime/drive/response.d.ts.map +1 -1
- package/dist/harness/runtime/drive/response.js +4 -4
- package/dist/harness/runtime/drive/response.js.map +1 -1
- package/dist/harness/runtime/drive/retry.d.ts +2 -2
- package/dist/harness/runtime/drive/retry.d.ts.map +1 -1
- package/dist/harness/runtime/drive/retry.js +3 -6
- package/dist/harness/runtime/drive/retry.js.map +1 -1
- package/dist/harness/runtime/drive/structural.d.ts.map +1 -1
- package/dist/harness/runtime/drive/structural.js +5 -5
- package/dist/harness/runtime/drive/structural.js.map +1 -1
- package/dist/harness/runtime/drive/tool-placement.d.ts.map +1 -1
- package/dist/harness/runtime/drive/tool-placement.js +2 -27
- package/dist/harness/runtime/drive/tool-placement.js.map +1 -1
- package/dist/harness/runtime/drive/tools.d.ts.map +1 -1
- package/dist/harness/runtime/drive/tools.js.map +1 -1
- package/dist/harness/session/fork-policy.d.ts +20 -12
- package/dist/harness/session/fork-policy.d.ts.map +1 -1
- package/dist/harness/session/fork-policy.js +45 -12
- package/dist/harness/session/fork-policy.js.map +1 -1
- package/dist/harness/session/fork.d.ts +0 -2
- package/dist/harness/session/fork.d.ts.map +1 -1
- package/dist/harness/session/fork.js +20 -68
- package/dist/harness/session/fork.js.map +1 -1
- package/dist/harness/session/in-memory-storage-state.d.ts +5 -5
- package/dist/harness/session/in-memory-storage-state.d.ts.map +1 -1
- package/dist/harness/session/in-memory-storage-state.js +99 -39
- package/dist/harness/session/in-memory-storage-state.js.map +1 -1
- package/dist/harness/session/index.d.ts +1 -1
- package/dist/harness/session/index.d.ts.map +1 -1
- package/dist/harness/session/index.js +1 -1
- package/dist/harness/session/index.js.map +1 -1
- package/dist/harness/session/jsonl/fork.d.ts +38 -0
- package/dist/harness/session/jsonl/fork.d.ts.map +1 -0
- package/dist/harness/session/jsonl/fork.js +264 -0
- package/dist/harness/session/jsonl/fork.js.map +1 -0
- package/dist/harness/session/jsonl/io.d.ts +14 -0
- package/dist/harness/session/jsonl/io.d.ts.map +1 -0
- package/dist/harness/session/jsonl/io.js +86 -0
- package/dist/harness/session/jsonl/io.js.map +1 -0
- package/dist/harness/session/jsonl/legacy-v3.d.ts +33 -8
- package/dist/harness/session/jsonl/legacy-v3.d.ts.map +1 -1
- package/dist/harness/session/jsonl/legacy-v3.js +285 -206
- package/dist/harness/session/jsonl/legacy-v3.js.map +1 -1
- package/dist/harness/session/jsonl/repo.d.ts +2 -1
- package/dist/harness/session/jsonl/repo.d.ts.map +1 -1
- package/dist/harness/session/jsonl/repo.js +36 -20
- package/dist/harness/session/jsonl/repo.js.map +1 -1
- package/dist/harness/session/jsonl/storage.d.ts +4 -5
- package/dist/harness/session/jsonl/storage.d.ts.map +1 -1
- package/dist/harness/session/jsonl/storage.js +39 -113
- package/dist/harness/session/jsonl/storage.js.map +1 -1
- package/dist/harness/session/memory.d.ts +3 -5
- package/dist/harness/session/memory.d.ts.map +1 -1
- package/dist/harness/session/memory.js +9 -14
- package/dist/harness/session/memory.js.map +1 -1
- package/dist/harness/session/testing/conformance/session-repo.d.ts +2 -0
- package/dist/harness/session/testing/conformance/session-repo.d.ts.map +1 -1
- package/dist/harness/session/testing/conformance/session-repo.js +220 -1
- package/dist/harness/session/testing/conformance/session-repo.js.map +1 -1
- package/dist/harness/session/testing/index.d.ts +1 -1
- package/dist/harness/session/testing/index.d.ts.map +1 -1
- package/dist/harness/session/testing/index.js +1 -1
- package/dist/harness/session/testing/index.js.map +1 -1
- package/dist/harness/session/types.d.ts +1 -0
- package/dist/harness/session/types.d.ts.map +1 -1
- package/dist/harness/session/types.js.map +1 -1
- package/dist/harness/types.d.ts +14 -0
- package/dist/harness/types.d.ts.map +1 -1
- package/dist/harness/types.js.map +1 -1
- package/dist/proxy.d.ts +2 -2
- package/dist/proxy.d.ts.map +1 -1
- package/dist/proxy.js.map +1 -1
- package/dist/types.d.ts +30 -14
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +10 -6
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"harness.js","sourceRoot":"","sources":["../../../src/harness/pico3/harness.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC;AAEhE,OAAO,EAAE,iBAAiB,EAAyB,MAAM,YAAY,CAAC;AACtE,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAC9D,OAAO,EAAE,OAAO,IAAI,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAC/D,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AACnD,OAAO,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AACrC,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAC3C,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAClD,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AACvC,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,OAAO,EAAe,MAAM,cAAc,CAAC;AAChE,OAAO,EAAsB,cAAc,EAAE,MAAM,aAAa,CAAC;AACjE,OAAO,EAaN,SAAS,GAuBT,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,WAAW,EAAc,MAAM,WAAW,CAAC;AA4DpD,MAAM,MAAM,GAAY,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC;AAE3C,MAAM,OAAO,OAAO;IACF,OAAO,CAAU;IACjB,SAAS,CAAY;IACrB,KAAK,CAAc;IACnB,KAAK,C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AAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,CAAC;QACrG,IAAI,IAAI,GAAG,IAAI,CAAC,MAAM,GAAG,GAAG,CAAC;QAC7B,KAAK,MAAM,CAAC,IAAI,IAAI,EAAE,CAAC;YACtB,IAAI,IAAI,KAAK,SAAS,IAAI,CAAC,CAAC,EAAE,GAAG,IAAI,EAAE,CAAC;gBACvC,IAAI,GAAG,IAAI,CAAC;gBACZ,MAAM;YACP,CAAC;YACD,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACb,CAAC;QACD,IAAI,IAAI;YAAE,MAAM;QAChB,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAE,CAAC,EAAE,CAAC;IACpC,CAAC;IACD,OAAO,GAAG,CAAC,OAAO,EAAE,CAAC;AAAA,CACrB;AAED,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,CAAC;AAGvC,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAC1D,oGAAkG;AAClG,MAAM,CAAC,MAAM,KAAK,GAAG,EAAE,UAAU,EAAE,IAAI,EAAE,SAAS,EAAE,QAAQ,EAAE,GAAG,EAAE,MAAM,EAAW,CAAC;AACrF,MAAM,CAAC,MAAM,OAAO,GAAG,cAAc,CAAC","sourcesContent":["import type { Context } from \"@earendil-works/chord\";\nimport { withAbortSignal } from \"@earendil-works/chord/context\";\nimport type { TSchema } from \"typebox\";\nimport { createHookRunners, type HookRegistration } from \"./hooks.ts\";\nimport { chooseThrough, collapse } from \"./kinds/collapse.ts\";\nimport { entries as builtinEntries } from \"./kinds/entries.ts\";\nimport { generation } from \"./kinds/generation.ts\";\nimport { job } from \"./kinds/job.ts\";\nimport { plugin } from \"./kinds/plugin.ts\";\nimport { postTools } from \"./kinds/post-tools.ts\";\nimport { tool } from \"./kinds/tool.ts\";\nimport { Scheduler } from \"./scheduler.ts\";\nimport { isCoreKind, Session, type TxImpl } from \"./session.ts\";\nimport { type SystemSection, systemSections } from \"./system.ts\";\nimport {\n\ttype AnyKind,\n\ttype AnyToolDeclaration,\n\ttype ConfigFacade,\n\ttype ConfigOfKinds,\n\ttype ContextView,\n\ttype Conversation,\n\ttype ConversationSpec,\n\ttype DisjointConfig,\n\ttype DocRef,\n\ttype Entry,\n\ttype EntryKind,\n\ttype EntryScan,\n\tForbidden,\n\ttype HooksOf,\n\ttype HostTx,\n\ttype Id,\n\ttype Input,\n\ttype Invoker,\n\ttype JsonObject,\n\ttype JsonValue,\n\ttype Models,\n\ttype Namespace,\n\ttype NamespaceDefaults,\n\ttype NamespaceRegistration,\n\ttype NewEntry,\n\ttype PluginHandler,\n\ttype ProcessHost,\n\ttype RewindableState,\n\ttype Runtime,\n\ttype SendInput,\n\ttype StickyState,\n\ttype Storage,\n\ttype Task,\n\ttype ToolDeclaration,\n\ttype UserInput,\n} from \"./types.ts\";\nimport { ViewManager, type Watch } from \"./view.ts\";\n\n// ---------------------------------------------------------------------------\n// Harness\n// ---------------------------------------------------------------------------\n\nexport interface HarnessOptions<Ks extends readonly AnyKind[] = []> {\n\tmodels: Models;\n\ttools?: AnyToolDeclaration[];\n\t/** Ordinary kinds authored with `defineTask`. Their `config` merges onto `c.config`; keys must be disjoint. */\n\ttaskKinds?: Ks;\n\tsections?: SystemSection[];\n\tplugins?: { [name: string]: PluginHandler };\n\tprocessHost?: ProcessHost;\n\t/** Clock for durable kernel timestamps and retry scheduling. */\n\tnow?: () => number;\n\troot?: { rewindable?: Partial<RewindableState>; sticky?: Partial<StickyState> };\n\t/** Errors from listeners, hooks, watches, and the scheduler. Never delivered as commit failures. */\n\tonReport?: (error: unknown) => void;\n}\n\nexport type BuiltinKinds = readonly [\n\ttypeof generation,\n\ttypeof tool,\n\ttypeof postTools,\n\ttypeof collapse,\n\ttypeof job,\n\ttypeof plugin,\n];\nexport type ConfigFor<Ks extends readonly AnyKind[]> = ConfigOfKinds<[...BuiltinKinds, ...Ks]>;\n\nexport interface ConversationHandle<Cfg extends object = ConfigFor<[]>> {\n\treadonly id: Id;\n\treadonly config: ConfigFacade<Cfg>;\n\tsend(input: SendInput, ctx: Context): Promise<InputHandle>;\n\twrite(entry: NewEntry, ctx: Context): Promise<Id>;\n\tcommit<T>(fn: (tx: HostTx, ctx: Context) => T | Promise<T>, ctx: Context): Promise<T>;\n\trewindable(ctx: Context): Promise<RewindableState>;\n\tsticky(ctx: Context): Promise<StickyState>;\n\tcontext(ctx: Context): Promise<ContextView>;\n\tfork(at: Id | \"start\", spec: Omit<ConversationSpec, \"parent\">, ctx: Context): Promise<ConversationHandle<Cfg>>;\n\tcollapse(instructions: string | undefined, ctx: Context): Promise<Id>;\n\treset(handoff: string | undefined, ctx: Context): Promise<void>;\n\tabort(ctx: Context): Promise<void>;\n\twaitForIdle(ctx: Context): Promise<void>;\n\thooks<T extends JsonObject, K extends AnyKind>(\n\t\tnamespace: Namespace<T>,\n\t\tkind: K,\n\t\thandlers: Partial<HooksOf<K>>,\n\t\topts?: { subtree?: boolean },\n\t): () => void;\n\twatch(ctx: Context): Promise<Watch>;\n}\nexport interface InputHandle {\n\treadonly id: Id;\n\tresult(ctx: Context): Promise<Input | undefined>;\n\twait(ctx: Context): Promise<Input>;\n\tabort(ctx: Context): Promise<\"aborted\" | \"already_placed\" | \"not_found\">;\n}\n\nconst KERNEL: Invoker = { type: \"kernel\" };\n\nexport class Harness<Ks extends readonly AnyKind[] = []> {\n\tprivate readonly session: Session;\n\tprivate readonly scheduler: Scheduler;\n\tprivate readonly views: ViewManager;\n\tprivate readonly kinds: Map<string, AnyKind>;\n\tprivate readonly namespaces = new Map<string, NamespaceRegistration>();\n\tprivate readonly tools = new Map<string, AnyToolDeclaration>();\n\tprivate readonly sectionMap = new Map<string, SystemSection>();\n\tprivate readonly entryKinds = new Map<string, EntryKind>();\n\tprivate readonly revisions = { sections: 0, tools: 0 };\n\tprivate readonly plugins: Map<string, PluginHandler>;\n\tprivate readonly hookRegistrations: HookRegistration[] = [];\n\tprivate readonly conversationListeners = new Set<(conversation: ConversationHandle<ConfigFor<Ks>>) => void>();\n\tprivate readonly onReport: (error: unknown) => void;\n\tprivate readonly now: () => number;\n\tprivate readonly ctx: Context;\n\tprivate readonly options: HarnessOptions<Ks>;\n\tprivate resumed = false;\n\tprivate suspended = false;\n\n\tstatic async open<Ks extends readonly AnyKind[] = []>(\n\t\tstorage: Storage,\n\t\toptions: HarnessOptions<Ks> &\n\t\t\t(DisjointConfig<[...BuiltinKinds, ...Ks]> extends true\n\t\t\t\t? unknown\n\t\t\t\t: { readonly __error: \"config keys collide across kinds\" }),\n\t\tctx: Context,\n\t): Promise<Harness<Ks>> {\n\t\tconst h = new Harness<Ks>(storage, options, ctx);\n\t\tawait h.init();\n\t\treturn h;\n\t}\n\n\tprivate constructor(storage: Storage, options: HarnessOptions<Ks>, ctx: Context) {\n\t\tthis.ctx = ctx;\n\t\tthis.options = options;\n\t\tthis.now = options.now ?? Date.now;\n\t\tthis.onReport = (error) => {\n\t\t\ttry {\n\t\t\t\toptions.onReport?.(error);\n\t\t\t} catch {}\n\t\t};\n\t\t// Fixed core, installed internally. User kinds may not replace or shadow a built-in.\n\t\tconst builtins: AnyKind[] = [generation, tool, postTools, collapse, job, plugin];\n\t\tthis.kinds = new Map(builtins.map((k) => [k.name, k]));\n\t\tfor (const k of options.taskKinds ?? []) {\n\t\t\tif (k.name.startsWith(\"pi.\"))\n\t\t\t\tthrow new Error(`task kind \"${k.name}\": names beginning with \"pi.\" are reserved`);\n\t\t\tif (this.kinds.has(k.name)) throw new Error(`task kind \"${k.name}\" registered twice`);\n\t\t\tthis.kinds.set(k.name, k);\n\t\t}\n\t\tfor (const t of options.tools ?? []) this.registerTool(t, \"open\");\n\t\tfor (const s of Object.values(systemSections)) this.sectionMap.set(s.key, s as SystemSection);\n\t\tfor (const s of options.sections ?? []) this.registerSection(s, \"open\");\n\t\tfor (const e of Object.values(builtinEntries)) this.entryKinds.set(e.kind, e as EntryKind);\n\t\tthis.plugins = new Map(Object.entries(options.plugins ?? {}));\n\t\tthis.session = new Session(storage, this.kinds, this.namespaces, this.now); // throws if the Storage already has an owner; validates config disjointness\n\t\tthis.session.onReport = this.onReport;\n\t\tthis.views = new ViewManager(this.session, this.onReport);\n\t\tthis.session.lineListeners.add((result) => this.views.update(result));\n\t\tthis.session.listeners.add((result) => {\n\t\t\tthis.views.deliver();\n\t\t\tfor (const conversation of result.changes.conversations) this.notifyConversation(conversation);\n\t\t});\n\t\tconst self = this;\n\t\tconst hooksFor = createHookRunners(\n\t\t\t() => this.hookRegistrations,\n\t\t\t(c) => this.session.index.ancestors(c),\n\t\t\tthis.onReport,\n\t\t);\n\t\tthis.scheduler = new Scheduler({\n\t\t\tsession: this.session,\n\t\t\tkinds: this.kinds,\n\t\t\tonReport: this.onReport,\n\t\t\tctx,\n\t\t\truntime(task, invoker, _ictx): Runtime {\n\t\t\t\treturn {\n\t\t\t\t\ttaskId: task.id,\n\t\t\t\t\tconversationId: task.conversationId,\n\t\t\t\t\tkind: invoker.kind,\n\t\t\t\t\tcommit: (fn, c) => {\n\t\t\t\t\t\tconst owns = self.session.liveTasks.get(task.id)?.owns ?? [];\n\t\t\t\t\t\tconst docs = owns.flatMap((id): DocRef[] => [\n\t\t\t\t\t\t\t{ doc: \"rewindable\", conversationId: id },\n\t\t\t\t\t\t\t{ doc: \"sticky\", conversationId: id },\n\t\t\t\t\t\t]);\n\t\t\t\t\t\treturn self.session\n\t\t\t\t\t\t\t.commit(invoker, (tx, lineCtx) => fn(tx, self.session.liveTasks.get(task.id)!, lineCtx), c, {\n\t\t\t\t\t\t\t\tdocs,\n\t\t\t\t\t\t\t})\n\t\t\t\t\t\t\t.then((r) => r.value);\n\t\t\t\t\t},\n\t\t\t\t\thooks: hooksFor(invoker.kind, { taskId: task.id, conversationId: task.conversationId }),\n\t\t\t\t\tmodels: options.models,\n\t\t\t\t\ttools: self.tools,\n\t\t\t\t\tregistries: {\n\t\t\t\t\t\tsections: {\n\t\t\t\t\t\t\tmap: self.sectionMap,\n\t\t\t\t\t\t\tget revision() {\n\t\t\t\t\t\t\t\treturn self.revisions.sections;\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\ttools: {\n\t\t\t\t\t\t\tmap: self.tools,\n\t\t\t\t\t\t\tget revision() {\n\t\t\t\t\t\t\t\treturn self.revisions.tools;\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t\tkinds: self.kinds,\n\t\t\t\t\tprocessHost: options.processHost,\n\t\t\t\t\tplugins: self.plugins,\n\t\t\t\t\tnow: self.now,\n\t\t\t\t\tsleep: (untilMs, c) =>\n\t\t\t\t\t\tnew Promise((resolve, reject) => {\n\t\t\t\t\t\t\tconst ms = Math.max(0, untilMs - Date.now());\n\t\t\t\t\t\t\tconst t = setTimeout(() => {\n\t\t\t\t\t\t\t\tc.abortSignal?.removeEventListener(\"abort\", onAbort);\n\t\t\t\t\t\t\t\tresolve();\n\t\t\t\t\t\t\t}, ms);\n\t\t\t\t\t\t\tconst onAbort = () => {\n\t\t\t\t\t\t\t\tclearTimeout(t);\n\t\t\t\t\t\t\t\treject(c.abortSignal?.reason ?? new Error(\"aborted\"));\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\tc.abortSignal?.addEventListener(\"abort\", onAbort, { once: true });\n\t\t\t\t\t\t}),\n\t\t\t\t\twaitForInput: (id, c) => self.scheduler.waitForInput(id, c),\n\t\t\t\t\twaitForTask: (id, c) => self.scheduler.waitForTask(id, c),\n\t\t\t\t\tabortTask: async (id, c) => {\n\t\t\t\t\t\tawait self.session.onLine(async (lineCtx) => {\n\t\t\t\t\t\t\tself.assertInvocation(invoker);\n\t\t\t\t\t\t\tconst target = self.session.liveTasks.get(id) ?? (await self.session.storage.task(id, lineCtx));\n\t\t\t\t\t\t\tif (target === undefined) throw new Error(`task ${id} not found`);\n\t\t\t\t\t\t\tself.assertTaskConversationScope(task.id, target.conversationId);\n\t\t\t\t\t\t}, c);\n\t\t\t\t\t\treturn self.scheduler.abortTask(id, c);\n\t\t\t\t\t},\n\t\t\t\t\tabortConversation: async (id, c) => {\n\t\t\t\t\t\tawait self.session.onLine(() => {\n\t\t\t\t\t\t\tself.assertInvocation(invoker);\n\t\t\t\t\t\t\tself.assertOwnedConversation(task.id, id);\n\t\t\t\t\t\t}, c);\n\t\t\t\t\t\tconst h = await self.conversation(id, c);\n\t\t\t\t\t\tif (h) await h.abort(c);\n\t\t\t\t\t},\n\t\t\t\t\tcreateOwnedConversation: (spec, c) =>\n\t\t\t\t\t\tself.session\n\t\t\t\t\t\t\t.commit(\n\t\t\t\t\t\t\t\tKERNEL,\n\t\t\t\t\t\t\t\t(tx) => {\n\t\t\t\t\t\t\t\t\tself.assertInvocation(invoker);\n\t\t\t\t\t\t\t\t\treturn tx.createOwnedConversation(task.id, task.conversationId, spec);\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\tc,\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t.then((result) => result.value),\n\t\t\t\t\tsendOwned: (id, input, c) =>\n\t\t\t\t\t\tself.session\n\t\t\t\t\t\t\t.commit(\n\t\t\t\t\t\t\t\tKERNEL,\n\t\t\t\t\t\t\t\t(tx) => {\n\t\t\t\t\t\t\t\t\tself.assertInvocation(invoker);\n\t\t\t\t\t\t\t\t\tself.assertOwnedConversation(task.id, id);\n\t\t\t\t\t\t\t\t\treturn tx.send(id, input);\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\tc,\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tdocs: [\n\t\t\t\t\t\t\t\t\t\t{ doc: \"rewindable\", conversationId: id },\n\t\t\t\t\t\t\t\t\t\t{ doc: \"sticky\", conversationId: id },\n\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t.then((r) => r.value),\n\t\t\t\t\tcontext: (c, at, cx) => self.session.commit(invoker, (tx) => tx.context(c, at), cx).then((r) => r.value),\n\t\t\t\t\tnewestEntry: (c, opts, cx) =>\n\t\t\t\t\t\tself.session.commit(invoker, (tx) => tx.newestEntry(c, opts), cx).then((r) => r.value),\n\t\t\t\t\trewindable: (c, cx) =>\n\t\t\t\t\t\tself.session\n\t\t\t\t\t\t\t.commit(invoker, (tx) => tx.snapshot({ doc: \"rewindable\", conversationId: c }), cx, {\n\t\t\t\t\t\t\t\tdocs: [{ doc: \"rewindable\", conversationId: c }],\n\t\t\t\t\t\t\t})\n\t\t\t\t\t\t\t.then((r) => r.value),\n\t\t\t\t\tsticky: (c, cx) =>\n\t\t\t\t\t\tself.session\n\t\t\t\t\t\t\t.commit(invoker, (tx) => tx.snapshot({ doc: \"sticky\", conversationId: c }), cx, {\n\t\t\t\t\t\t\t\tdocs: [{ doc: \"sticky\", conversationId: c }],\n\t\t\t\t\t\t\t})\n\t\t\t\t\t\t\t.then((r) => r.value),\n\t\t\t\t\trewindableAsOf: (c, at, cx) =>\n\t\t\t\t\t\tself.session.commit(invoker, (tx) => tx.rewindableAsOf(c, at), cx).then((r) => r.value),\n\t\t\t\t};\n\t\t\t},\n\t\t});\n\t}\n\n\tprivate notifyConversation(conversation: Conversation): void {\n\t\tif (this.conversationListeners.size === 0) return;\n\t\tconst handle = this.handle(conversation);\n\t\tfor (const listener of [...this.conversationListeners]) {\n\t\t\ttry {\n\t\t\t\tlistener(handle);\n\t\t\t} catch (error) {\n\t\t\t\tthis.onReport(error);\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate assertInvocation(invoker: Extract<Invoker, { type: \"task\" }>): void {\n\t\tif (!invoker.token.alive) throw new Forbidden(\"operation from a finished invocation\");\n\t\tconst live = this.session.liveTasks.get(invoker.id);\n\t\tif (live === undefined) throw new Forbidden(\"operation from a task that is not live\");\n\t\tif (invoker.mode === \"run\" && live.abort === true) throw new Forbidden(\"operation from a marked run invocation\");\n\t}\n\n\tprivate assertTaskConversationScope(taskId: Id, conversationId: Id): void {\n\t\tconst task = this.session.liveTasks.get(taskId);\n\t\tif (task?.conversationId === conversationId) return;\n\t\tif (task?.owns.some((root) => this.session.index.subtree(root).has(conversationId))) return;\n\t\tthrow new Forbidden(`conversation ${conversationId} is outside task ${taskId}'s subtree`);\n\t}\n\n\tprivate assertOwnedConversation(taskId: Id, conversationId: Id): void {\n\t\tconst task = this.session.liveTasks.get(taskId);\n\t\tif (task === undefined || !task.owns.some((root) => this.session.index.subtree(root).has(conversationId))) {\n\t\t\tthrow new Forbidden(`conversation ${conversationId} is not owned by task ${taskId}`);\n\t\t}\n\t}\n\n\tprivate async init() {\n\t\tconst { session } = this;\n\t\tconst convs = await session.read((s, ctx) => s.conversations(ctx), this.ctx);\n\t\tfor (const c of convs) session.conversationRecords.set(c.id, c);\n\t\tconst live = await session.read((s, ctx) => s.scanTasks({ status: [\"pending\", \"running\"] }, ctx), this.ctx);\n\t\tfor (const task of live) session.liveTasks.set(task.id, task);\n\t\t// Owner tasks that are terminal but still own existing conversations: needed for ancestry.\n\t\tconst owners = new Set(convs.map((c) => c.owner).filter((o): o is Id => o !== undefined));\n\t\tfor (const id of owners)\n\t\t\tif (!session.liveTasks.has(id)) {\n\t\t\t\tconst t = await session.read((s, ctx) => s.task(id, ctx), this.ctx);\n\t\t\t\tif (t) session.ownerTaskCache.set(id, t);\n\t\t\t}\n\t\tif (convs.length === 0) {\n\t\t\tawait session.commit(\n\t\t\t\tKERNEL,\n\t\t\t\t(tx) =>\n\t\t\t\t\ttx.createConversation({ rewindable: this.options.root?.rewindable, sticky: this.options.root?.sticky }),\n\t\t\t\tthis.ctx,\n\t\t\t);\n\t\t}\n\t}\n\n\tresume() {\n\t\tif (this.suspended) throw new Error(\"cannot resume a suspended harness; reopen storage with a new harness\");\n\t\tif (this.resumed) return;\n\t\tthis.resumed = true;\n\t\tvoid this.reconcileOrphans()\n\t\t\t.then(() => {\n\t\t\t\tif (!this.suspended) this.scheduler.resume();\n\t\t\t})\n\t\t\t.catch((error) => this.onReport(error));\n\t}\n\tprivate async reconcileOrphans(): Promise<void> {\n\t\tconst orphaned = [...this.session.liveTasks.values()].filter((task) => !this.kinds.has(task.kind));\n\t\tif (orphaned.length === 0) return;\n\t\tconst docs = [...new Set(orphaned.map((task) => task.conversationId))].map(\n\t\t\t(conversationId): DocRef => ({ doc: \"sticky\", conversationId }),\n\t\t);\n\t\tawait this.session.commit(\n\t\t\tKERNEL,\n\t\t\t(tx) => {\n\t\t\t\tfor (const task of orphaned) tx.setTask({ ...task, status: \"terminal\", outcome: { status: \"orphaned\" } });\n\t\t\t},\n\t\t\tthis.ctx,\n\t\t\t{ docs },\n\t\t);\n\t}\n\tquiescent(): boolean {\n\t\treturn this.scheduler.quiescent();\n\t}\n\thold(): () => void {\n\t\tif (!this.scheduler.quiescent()) throw new Error(\"cannot hold a non-quiescent harness; suspend it instead\");\n\t\treturn this.scheduler.hold();\n\t}\n\t/** Cancel and join in-process invocations, clear transient waits, then close without terminalizing tasks. */\n\tasync suspend(ctx: Context): Promise<void> {\n\t\tif (this.suspended) return;\n\t\tthis.suspended = true;\n\t\tawait this.scheduler.joinAll();\n\t\tconst tools = [...this.session.liveTasks.values()].filter((task) => task.kind === \"pi.tool\");\n\t\tif (tools.length > 0) {\n\t\t\tconst docs = [...new Set(tools.map((task) => task.conversationId))].map(\n\t\t\t\t(conversationId): DocRef => ({ doc: \"sticky\", conversationId }),\n\t\t\t);\n\t\t\tawait this.session.commit(\n\t\t\t\tKERNEL,\n\t\t\t\t(tx) => {\n\t\t\t\t\tfor (const task of tools) {\n\t\t\t\t\t\tconst index = (task.input as { index: number }).index;\n\t\t\t\t\t\tconst slot = tx.sticky(task.conversationId).turn.tools[index];\n\t\t\t\t\t\tif (slot?.waitingOn !== undefined) delete slot.waitingOn;\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t\tctx,\n\t\t\t\t{ docs },\n\t\t\t);\n\t\t}\n\t\tthis.views.close();\n\t\tawait this.session.close(ctx);\n\t}\n\n\t// --- registries --------------------------------------------------------------\n\n\tregisterTaskKind<K extends AnyKind>(kind: K): () => void {\n\t\tif (kind.name.startsWith(\"pi.\"))\n\t\t\tthrow new Error(`task kind \"${kind.name}\": names beginning with \"pi.\" are reserved`);\n\t\tif (this.kinds.has(kind.name)) throw new Error(`task kind \"${kind.name}\" already registered`);\n\t\tthis.session.defaults.register(kind);\n\t\tthis.kinds.set(kind.name, kind);\n\t\tthis.scheduler.kick();\n\t\treturn () => {\n\t\t\tif (this.kinds.get(kind.name) !== kind) return;\n\t\t\tthis.kinds.delete(kind.name);\n\t\t\tthis.session.defaults.unregister(kind);\n\t\t};\n\t}\n\n\tnamespace<T extends JsonObject>(\n\t\tid: string,\n\t\tdefaults: NamespaceDefaults<T>,\n\t\topts: { view?: (slice: Readonly<T>) => JsonValue } = {},\n\t): Namespace<T> {\n\t\tif (!/^[a-z][a-z0-9_.-]*$/i.test(id) || id.startsWith(\"pi.\")) throw new Error(`invalid namespace \"${id}\"`);\n\t\tif (this.namespaces.has(id)) throw new Error(`namespace \"${id}\" already registered`);\n\t\tconst stored = JSON.parse(JSON.stringify(defaults)) as {\n\t\t\trewindable?: JsonObject;\n\t\t\tsticky?: JsonObject;\n\t\t\tsession?: JsonObject;\n\t\t};\n\t\tconst routes = new Map<string, \"rewindable\" | \"sticky\" | \"session\">();\n\t\tfor (const doc of [\"rewindable\", \"sticky\", \"session\"] as const) {\n\t\t\tfor (const key of Object.keys(stored[doc] ?? {})) {\n\t\t\t\tif (routes.has(key))\n\t\t\t\t\tthrow new Error(`namespace \"${id}\" key \"${key}\" is declared in more than one document`);\n\t\t\t\troutes.set(key, doc);\n\t\t\t}\n\t\t}\n\t\tlet token!: Namespace<T>;\n\t\ttoken = {\n\t\t\tid,\n\t\t\tunregister: () => {\n\t\t\t\tif (this.namespaces.get(id)?.token !== token) return;\n\t\t\t\tthis.namespaces.delete(id);\n\t\t\t\tfor (let index = this.hookRegistrations.length - 1; index >= 0; index--) {\n\t\t\t\t\tif ((this.hookRegistrations[index]!.namespace as object) === token)\n\t\t\t\t\t\tthis.hookRegistrations.splice(index, 1);\n\t\t\t\t}\n\t\t\t},\n\t\t};\n\t\tconst registration: NamespaceRegistration = {\n\t\t\ttoken,\n\t\t\tdefaults: {\n\t\t\t\trewindable: stored.rewindable ?? {},\n\t\t\t\tsticky: stored.sticky ?? {},\n\t\t\t\tsession: stored.session ?? {},\n\t\t\t},\n\t\t\troutes,\n\t\t\tproject: opts.view === undefined ? undefined : (slice) => opts.view!(slice as Readonly<T>),\n\t\t};\n\t\tthis.namespaces.set(id, registration);\n\t\treturn token;\n\t}\n\n\t/** Register a tool. A duplicate name rejects. Unregister removes only this exact declaration; idempotent. */\n\tregisterTool(tool: AnyToolDeclaration, at: \"open\" | \"runtime\" = \"runtime\"): () => void {\n\t\tif (this.tools.has(tool.name)) throw new Error(`tool \"${tool.name}\" already registered`);\n\t\tthis.tools.set(tool.name, tool);\n\t\tthis.revisions.tools++;\n\t\tvoid at;\n\t\treturn () => {\n\t\t\tif (this.tools.get(tool.name) === tool) {\n\t\t\t\tthis.tools.delete(tool.name);\n\t\t\t\tthis.revisions.tools++;\n\t\t\t}\n\t\t};\n\t}\n\tregisterSection(section: SystemSection, at: \"open\" | \"runtime\" = \"runtime\"): () => void {\n\t\tif (this.sectionMap.has(section.key)) throw new Error(`section \"${section.key}\" already registered`);\n\t\tthis.sectionMap.set(section.key, section);\n\t\tthis.revisions.sections++;\n\t\tvoid at;\n\t\treturn () => {\n\t\t\tif (this.sectionMap.get(section.key) === section) {\n\t\t\t\tthis.sectionMap.delete(section.key);\n\t\t\t\tthis.revisions.sections++;\n\t\t\t}\n\t\t};\n\t}\n\tregisterEntryKind(kind: EntryKind): () => void {\n\t\tif (kind.kind.startsWith(\"pi.\"))\n\t\t\tthrow new Error(`entry kind \"${kind.kind}\": names beginning with \"pi.\" are reserved`);\n\t\tif (this.entryKinds.has(kind.kind)) throw new Error(`entry kind \"${kind.kind}\" already registered`);\n\t\tthis.entryKinds.set(kind.kind, kind);\n\t\treturn () => {\n\t\t\tif (this.entryKinds.get(kind.kind) === kind) this.entryKinds.delete(kind.kind);\n\t\t};\n\t}\n\t/** Register namespace-bound handlers for one kind's hook points, harness-wide. Both tokens must be current. */\n\thooks<T extends JsonObject, K extends AnyKind>(\n\t\tnamespace: Namespace<T>,\n\t\tkind: K,\n\t\thandlers: Partial<HooksOf<K>>,\n\t\t_opts?: Readonly<Record<string, never>>,\n\t): () => void {\n\t\treturn this.addHooks({ namespace: this.checkNamespace(namespace), kind: this.checkKind(kind), handlers });\n\t}\n\tprivate checkKind(kind: AnyKind): AnyKind {\n\t\tif (this.kinds.get(kind.name) !== kind) throw new Error(`kind \"${kind.name}\" is not the registered token`);\n\t\treturn kind;\n\t}\n\tprivate checkNamespace<T extends JsonObject>(namespace: Namespace<T>): Namespace<JsonObject> {\n\t\tif (this.namespaces.get(namespace.id)?.token !== namespace)\n\t\t\tthrow new Forbidden(`namespace \"${namespace.id}\" is stale`);\n\t\treturn namespace as unknown as Namespace<JsonObject>;\n\t}\n\tprivate addHooks(reg: HookRegistration): () => void {\n\t\tthis.hookRegistrations.push(reg);\n\t\tlet done = false;\n\t\treturn () => {\n\t\t\tif (done) return;\n\t\t\tdone = true;\n\t\t\tconst index = this.hookRegistrations.indexOf(reg);\n\t\t\tif (index >= 0) this.hookRegistrations.splice(index, 1);\n\t\t};\n\t}\n\n\t// --- conversations -------------------------------------------------------------\n\n\tasync root(ctx: Context): Promise<ConversationHandle<ConfigFor<Ks>>> {\n\t\treturn this.conversation(1, ctx).then((c) => c!);\n\t}\n\tonConversation(listener: (conversation: ConversationHandle<ConfigFor<Ks>>) => void): () => void {\n\t\tthis.conversationListeners.add(listener);\n\t\tfor (const conversation of this.session.conversationRecords.values()) {\n\t\t\ttry {\n\t\t\t\tlistener(this.handle(conversation));\n\t\t\t} catch (error) {\n\t\t\t\tthis.onReport(error);\n\t\t\t}\n\t\t}\n\t\treturn () => this.conversationListeners.delete(listener);\n\t}\n\tasync conversation(id: Id, ctx: Context): Promise<ConversationHandle<ConfigFor<Ks>> | undefined> {\n\t\tconst c = await this.session.read((s, lineCtx) => s.conversation(id, lineCtx), ctx);\n\t\treturn c === undefined ? undefined : this.handle(c);\n\t}\n\tasync createConversation(\n\t\tspec: ConversationSpec & { input?: UserInput },\n\t\tctx: Context,\n\t): Promise<ConversationHandle<ConfigFor<Ks>>> {\n\t\tconst { input, ...rest } = spec;\n\t\tconst id = await this.session\n\t\t\t.commit(\n\t\t\t\tKERNEL,\n\t\t\t\tasync (tx) => {\n\t\t\t\t\tconst id = tx.createConversation(rest);\n\t\t\t\t\tif (input !== undefined) await tx.send(id, { content: input });\n\t\t\t\t\treturn id;\n\t\t\t\t},\n\t\t\t\tctx,\n\t\t\t)\n\t\t\t.then((r) => r.value);\n\t\treturn (await this.conversation(id, ctx))!;\n\t}\n\tentries(scan: EntryScan, ctx: Context): Promise<Entry[]> {\n\t\treturn this.session.read((s, lineCtx) => s.scanEntries(scan, lineCtx), ctx);\n\t}\n\tgetTask(id: Id, ctx: Context): Promise<Task | undefined> {\n\t\treturn this.session.read((s, lineCtx) => s.task(id, lineCtx), ctx);\n\t}\n\tasync abortInput(id: Id, ctx: Context, conversationId?: Id): Promise<\"aborted\" | \"already_placed\" | \"not_found\"> {\n\t\tif (conversationId !== undefined) {\n\t\t\tconst input = await this.session.read((storage, lineCtx) => storage.input(id, lineCtx), ctx);\n\t\t\tif (input !== undefined && input.conversationId !== conversationId)\n\t\t\t\tthrow new Forbidden(`input ${id} is outside conversation ${conversationId}`);\n\t\t}\n\t\treturn this.inputHandle(id).abort(ctx);\n\t}\n\tabortTask(id: Id, ctx: Context): Promise<\"marked\" | \"terminal\"> {\n\t\treturn this.scheduler.abortTask(id, ctx);\n\t}\n\t/** Durably mark a task for abort without signalling its invocation; the scheduler aborts it when it next drains. */\n\tmarkTask(id: Id, ctx: Context): Promise<\"marked\" | \"terminal\"> {\n\t\treturn this.session\n\t\t\t.commit(\n\t\t\t\tKERNEL,\n\t\t\t\tasync (tx) => {\n\t\t\t\t\tconst t = await tx.task(id);\n\t\t\t\t\tif (t === undefined) throw new Error(`task ${id} not found`);\n\t\t\t\t\tif (t.status === \"terminal\") return \"terminal\" as const;\n\t\t\t\t\ttx.markTask(id);\n\t\t\t\t\treturn \"marked\" as const;\n\t\t\t\t},\n\t\t\t\tctx,\n\t\t\t)\n\t\t\t.then((r) => r.value);\n\t}\n\twaitForIdle(ctx: Context): Promise<void> {\n\t\treturn this.scheduler.waitForIdle(undefined, ctx);\n\t}\n\twaitForTask(id: Id, ctx: Context): Promise<Task> {\n\t\treturn this.scheduler.waitForTask(id, ctx);\n\t}\n\n\t/** Signals every invocation, waits for them, closes storage. Writes nothing. */\n\tasync close(ctx: Context) {\n\t\tawait this.suspend(ctx);\n\t}\n\n\tprivate handle(c: Conversation): ConversationHandle<ConfigFor<Ks>> {\n\t\tconst self = this;\n\t\tconst docs: DocRef[] = [\n\t\t\t{ doc: \"rewindable\", conversationId: c.id },\n\t\t\t{ doc: \"sticky\", conversationId: c.id },\n\t\t];\n\t\tconst hostInvoker: Invoker = { type: \"host\", conversationId: c.id };\n\t\tconst kernelInvoker: Invoker = { type: \"kernel\", conversationId: c.id };\n\t\tconst host = <T>(fn: (tx: HostTx, ctx: Context) => T | Promise<T>, ctx: Context) =>\n\t\t\tself.session.commit(hostInvoker, (tx, lineCtx) => fn(tx, lineCtx), ctx, { docs }).then((r) => r.value);\n\t\tconst kernel = <T>(fn: (tx: TxImpl, ctx: Context) => T | Promise<T>, ctx: Context) =>\n\t\t\tself.session.commit(kernelInvoker, fn, ctx, { docs }).then((r) => r.value);\n\t\tconst config: ConfigFacade<ConfigFor<Ks>> = {\n\t\t\tget: (ctx) =>\n\t\t\t\thost((tx) => {\n\t\t\t\t\tconst cfg = tx.config(c.id);\n\t\t\t\t\tconst out: { [k: string]: JsonValue | undefined } = {};\n\t\t\t\t\tfor (const key of self.session.defaults.route.keys()) out[key] = cfg.get(key);\n\t\t\t\t\treturn out as ConfigFor<Ks>;\n\t\t\t\t}, ctx),\n\t\t\tset: (patch, ctx) =>\n\t\t\t\thost((tx) => {\n\t\t\t\t\tconst cfg = tx.config(c.id);\n\t\t\t\t\tfor (const [key, value] of Object.entries(patch)) {\n\t\t\t\t\t\tif (value === undefined) throw new Error(`config.set(${key}): use config.reset()`);\n\t\t\t\t\t\tcfg.set(key, value as JsonValue);\n\t\t\t\t\t}\n\t\t\t\t}, ctx),\n\t\t\treset: (keys, ctx) =>\n\t\t\t\thost((tx) => {\n\t\t\t\t\tconst cfg = tx.config(c.id);\n\t\t\t\t\tfor (const key of keys) cfg.reset(String(key));\n\t\t\t\t}, ctx),\n\t\t};\n\t\treturn {\n\t\t\tid: c.id,\n\t\t\tconfig,\n\t\t\tasync send(input, ctx) {\n\t\t\t\tconst id = await kernel((tx) => tx.send(c.id, input), ctx);\n\t\t\t\treturn self.inputHandle(id);\n\t\t\t},\n\t\t\twrite: (entry, ctx) => host((tx) => tx.write(c.id, entry), ctx),\n\t\t\tcommit: host,\n\t\t\trewindable: (ctx) => host((tx) => tx.snapshot({ doc: \"rewindable\", conversationId: c.id }), ctx),\n\t\t\tsticky: (ctx) => host((tx) => tx.snapshot({ doc: \"sticky\", conversationId: c.id }), ctx),\n\t\t\tcontext: (ctx) => host((tx) => tx.context(c.id), ctx),\n\t\t\tasync fork(at, spec, ctx) {\n\t\t\t\tconst id = await self.session.fork(c.id, at, spec, ctx);\n\t\t\t\treturn (await self.conversation(id, ctx))!;\n\t\t\t},\n\t\t\tcollapse: (instructions, ctx) =>\n\t\t\t\tkernel(async (tx) => {\n\t\t\t\t\tconst { entries } = await tx.context(c.id);\n\t\t\t\t\tconst state = tx.rewindable(c.id);\n\t\t\t\t\tconst through = chooseThrough(entries, state.keepRecent);\n\t\t\t\t\tif (through === undefined) throw new Error(\"nothing to collapse\");\n\t\t\t\t\treturn tx.createTask({\n\t\t\t\t\t\tkind: \"pi.collapse\",\n\t\t\t\t\t\tconversationId: c.id,\n\t\t\t\t\t\tbackground: true,\n\t\t\t\t\t\tinput: { reason: \"manual\", through, ...(instructions === undefined ? {} : { instructions }) },\n\t\t\t\t\t});\n\t\t\t\t}, ctx),\n\t\t\treset: async (handoff, ctx) => {\n\t\t\t\tawait kernel(\n\t\t\t\t\t(tx) =>\n\t\t\t\t\t\ttx.write(\n\t\t\t\t\t\t\tc.id,\n\t\t\t\t\t\t\thandoff === undefined\n\t\t\t\t\t\t\t\t? { kind: \"pi.reset\", head: \"self\" }\n\t\t\t\t\t\t\t\t: {\n\t\t\t\t\t\t\t\t\t\tkind: \"pi.handoff\",\n\t\t\t\t\t\t\t\t\t\thead: \"self\",\n\t\t\t\t\t\t\t\t\t\tmodel: [{ role: \"user\", content: handoff, timestamp: self.now() }],\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t),\n\t\t\t\t\tctx,\n\t\t\t\t);\n\t\t\t},\n\t\t\tasync abort(ctx) {\n\t\t\t\tconst owned: Id[] = [];\n\t\t\t\tconst marked = await kernel(async (tx) => {\n\t\t\t\t\tconst s = tx.sticky(c.id);\n\t\t\t\t\tconst withdrawn = s.inbox.filter((q) => q.mode !== \"write\").map((q) => q.id);\n\t\t\t\t\tfor (let i = s.inbox.length - 1; i >= 0; i--) if (s.inbox[i]!.mode !== \"write\") s.inbox.splice(i, 1);\n\t\t\t\t\tawait tx.resolveInputs(withdrawn, { status: \"unanswered\", reason: \"aborted\" });\n\t\t\t\t\tfor (const input of withdrawn) tx.emit({ type: \"input.aborted\", input });\n\t\t\t\t\tconst ids: Id[] = [];\n\t\t\t\t\tfor (const t of [...self.session.liveTasks.values()]) {\n\t\t\t\t\t\tif (t.conversationId !== c.id || t.background) continue;\n\t\t\t\t\t\tfor (const o of t.owns) owned.push(o);\n\t\t\t\t\t\tif (t.abort !== true) tx.markTask(t.id);\n\t\t\t\t\t\tids.push(t.id);\n\t\t\t\t\t}\n\t\t\t\t\treturn ids;\n\t\t\t\t}, ctx);\n\t\t\t\tfor (const id of marked) await self.scheduler.abortTask(id, ctx);\n\t\t\t\tawait self.scheduler.waitForIdle(c.id, ctx);\n\t\t\t\tawait Promise.all(owned.map((o) => self.scheduler.waitForIdle(o, ctx)));\n\t\t\t},\n\t\t\twaitForIdle: (ctx) => self.scheduler.waitForIdle(c.id, ctx),\n\t\t\thooks: (namespace, kind, handlers, opts) =>\n\t\t\t\tself.addHooks({\n\t\t\t\t\tnamespace: self.checkNamespace(namespace),\n\t\t\t\t\tkind: self.checkKind(kind),\n\t\t\t\t\thandlers,\n\t\t\t\t\tconversationId: c.id,\n\t\t\t\t\tsubtree: opts?.subtree,\n\t\t\t\t}),\n\t\t\twatch: (ctx) => self.watch(c, ctx),\n\t\t};\n\t}\n\n\t/** Capture and subscribe in one line operation. */\n\tprivate async watch(conversation: Conversation, ctx: Context): Promise<Watch> {\n\t\treturn this.session\n\t\t\t.commit(\n\t\t\t\t{ type: \"host\", conversationId: conversation.id },\n\t\t\t\tasync (tx) => {\n\t\t\t\t\tconst entries = await captureActiveTranscript((scan) => tx.scanEntries(scan), conversation.id);\n\t\t\t\t\treturn this.views.watch(conversation, entries);\n\t\t\t\t},\n\t\t\t\tctx,\n\t\t\t\t{\n\t\t\t\t\tdocs: [\n\t\t\t\t\t\t{ doc: \"rewindable\", conversationId: conversation.id },\n\t\t\t\t\t\t{ doc: \"sticky\", conversationId: conversation.id },\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t)\n\t\t\t.then((result) => result.value);\n\t}\n\n\tprivate inputHandle(id: Id): InputHandle {\n\t\treturn {\n\t\t\tid,\n\t\t\tresult: (ctx) => this.session.read((s, lineCtx) => s.input(id, lineCtx), ctx),\n\t\t\twait: (ctx) => this.scheduler.waitForInput(id, ctx),\n\t\t\tabort: (ctx) =>\n\t\t\t\tthis.session.commit(KERNEL, (tx) => tx.withdrawInput(id), ctx, { docs: [] }).then((r) => r.value),\n\t\t};\n\t}\n}\n\n/**\n * §9: H = newest fork-visible entry with a head. Active transcript = H plus every fork-visible entry\n * with id ≥ H.head, chronological, nothing dropped inside the range; the whole transcript if no head.\n */\nexport async function captureActiveTranscript(\n\tscan: (s: EntryScan) => Promise<Entry[]>,\n\tconversationId: Id,\n): Promise<Entry[]> {\n\tconst [h] = await scan({ conversationId, withHead: true, limit: 1 });\n\tconst from = h?.head;\n\tconst out: Entry[] = [];\n\tlet before: Id | undefined;\n\tfor (;;) {\n\t\tconst page = await scan({ conversationId, ...(before === undefined ? {} : { before }), limit: 256 });\n\t\tlet done = page.length < 256;\n\t\tfor (const e of page) {\n\t\t\tif (from !== undefined && e.id < from) {\n\t\t\t\tdone = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tout.push(e);\n\t\t}\n\t\tif (done) break;\n\t\tbefore = page[page.length - 1]!.id;\n\t}\n\treturn out.reverse();\n}\n\nexport { withAbortSignal, isCoreKind };\nexport type { ConversationView, Envelope, ViewEvent } from \"./types.ts\";\nexport type { Watch } from \"./view.ts\";\nexport { applyEnvelope, WATCH_CAPACITY } from \"./view.ts\";\n/** The built-in kinds, as typed witnesses for `api.task`, `waitForTask`, and `hooks(kind, …)`. */\nexport const kinds = { generation, tool, postTools, collapse, job, plugin } as const;\nexport const entries = builtinEntries;\nexport type { ToolDeclaration, TSchema };\n"]}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { AnyKind, HookRunner, Id, Namespace } from "./types.ts";
|
|
2
|
+
export interface HookRegistration {
|
|
3
|
+
namespace: Namespace;
|
|
4
|
+
kind: AnyKind;
|
|
5
|
+
handlers: object;
|
|
6
|
+
conversationId?: Id;
|
|
7
|
+
subtree?: boolean;
|
|
8
|
+
}
|
|
9
|
+
export declare function createHookRunners(registrations: () => readonly HookRegistration[], ancestors: (conversationId: Id) => readonly Id[], onReport: (error: unknown) => void): <H extends object>(kind: AnyKind, info: Omit<import("./types.ts").HookApi, "kind">) => HookRunner<H>;
|
|
10
|
+
//# sourceMappingURL=hooks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../../../src/harness/pico3/hooks.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAY,UAAU,EAAE,EAAE,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAE/E,MAAM,WAAW,gBAAgB;IAChC,SAAS,EAAE,SAAS,CAAC;IACrB,IAAI,EAAE,OAAO,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;IACjB,cAAc,CAAC,EAAE,EAAE,CAAC;IACpB,OAAO,CAAC,EAAE,OAAO,CAAC;CAClB;AAED,wBAAgB,iBAAiB,CAChC,aAAa,EAAE,MAAM,SAAS,gBAAgB,EAAE,EAChD,SAAS,EAAE,CAAC,cAAc,EAAE,EAAE,KAAK,SAAS,EAAE,EAAE,EAChD,QAAQ,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,IAE1B,CAAC,mGA+BT","sourcesContent":["import type { AnyKind, HookInfo, HookRunner, Id, Namespace } from \"./types.ts\";\n\nexport interface HookRegistration {\n\tnamespace: Namespace;\n\tkind: AnyKind;\n\thandlers: object;\n\tconversationId?: Id;\n\tsubtree?: boolean;\n}\n\nexport function createHookRunners(\n\tregistrations: () => readonly HookRegistration[],\n\tancestors: (conversationId: Id) => readonly Id[],\n\tonReport: (error: unknown) => void,\n) {\n\treturn <H extends object>(kind: AnyKind, info: Omit<HookInfo, \"kind\">): HookRunner<H> => {\n\t\tconst handlers = () =>\n\t\t\tregistrations()\n\t\t\t\t.filter((registration) => registration.kind === kind)\n\t\t\t\t.filter(\n\t\t\t\t\t(registration) =>\n\t\t\t\t\t\tregistration.conversationId === undefined ||\n\t\t\t\t\t\tregistration.conversationId === info.conversationId ||\n\t\t\t\t\t\t(registration.subtree === true &&\n\t\t\t\t\t\t\tancestors(info.conversationId).includes(registration.conversationId)),\n\t\t\t\t)\n\t\t\t\t.map((registration) => ({\n\t\t\t\t\thandlers: registration.handlers as Partial<H>,\n\t\t\t\t\tnamespace: registration.namespace,\n\t\t\t\t\tapi: { ...info, kind: kind.name },\n\t\t\t\t}));\n\t\treturn {\n\t\t\thandlers,\n\t\t\tasync each(ctx, fn, onValue) {\n\t\t\t\tfor (const binding of handlers()) {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst v = await fn(binding.handlers, binding.api);\n\t\t\t\t\t\tif (v !== undefined && onValue?.(v as never) === true) return;\n\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\tif (ctx.abortSignal?.aborted) throw error;\n\t\t\t\t\t\tonReport(error);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t},\n\t\t};\n\t};\n}\n"]}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export function createHookRunners(registrations, ancestors, onReport) {
|
|
2
|
+
return (kind, info) => {
|
|
3
|
+
const handlers = () => registrations()
|
|
4
|
+
.filter((registration) => registration.kind === kind)
|
|
5
|
+
.filter((registration) => registration.conversationId === undefined ||
|
|
6
|
+
registration.conversationId === info.conversationId ||
|
|
7
|
+
(registration.subtree === true &&
|
|
8
|
+
ancestors(info.conversationId).includes(registration.conversationId)))
|
|
9
|
+
.map((registration) => ({
|
|
10
|
+
handlers: registration.handlers,
|
|
11
|
+
namespace: registration.namespace,
|
|
12
|
+
api: { ...info, kind: kind.name },
|
|
13
|
+
}));
|
|
14
|
+
return {
|
|
15
|
+
handlers,
|
|
16
|
+
async each(ctx, fn, onValue) {
|
|
17
|
+
for (const binding of handlers()) {
|
|
18
|
+
try {
|
|
19
|
+
const v = await fn(binding.handlers, binding.api);
|
|
20
|
+
if (v !== undefined && onValue?.(v) === true)
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
if (ctx.abortSignal?.aborted)
|
|
25
|
+
throw error;
|
|
26
|
+
onReport(error);
|
|
27
|
+
}
|
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AC,CAAC;wBAClD,IAAI,CAAC,KAAK,SAAS,IAAI,OAAO,EAAE,CAAC,CAAU,CAAC,KAAK,IAAI;4BAAE,OAAO;oBAC/D,CAAC;oBAAC,OAAO,KAAK,EAAE,CAAC;wBAChB,IAAI,GAAG,CAAC,WAAW,EAAE,OAAO;4BAAE,MAAM,KAAK,CAAC;wBAC1C,QAAQ,CAAC,KAAK,CAAC,CAAC;oBACjB,CAAC;gBACF,CAAC;YAAA,CACD;SACD,CAAC;IAAA,CACF,CAAC;AAAA,CACF","sourcesContent":["import type { AnyKind, HookInfo, HookRunner, Id, Namespace } from \"./types.ts\";\n\nexport interface HookRegistration {\n\tnamespace: Namespace;\n\tkind: AnyKind;\n\thandlers: object;\n\tconversationId?: Id;\n\tsubtree?: boolean;\n}\n\nexport function createHookRunners(\n\tregistrations: () => readonly HookRegistration[],\n\tancestors: (conversationId: Id) => readonly Id[],\n\tonReport: (error: unknown) => void,\n) {\n\treturn <H extends object>(kind: AnyKind, info: Omit<HookInfo, \"kind\">): HookRunner<H> => {\n\t\tconst handlers = () =>\n\t\t\tregistrations()\n\t\t\t\t.filter((registration) => registration.kind === kind)\n\t\t\t\t.filter(\n\t\t\t\t\t(registration) =>\n\t\t\t\t\t\tregistration.conversationId === undefined ||\n\t\t\t\t\t\tregistration.conversationId === info.conversationId ||\n\t\t\t\t\t\t(registration.subtree === true &&\n\t\t\t\t\t\t\tancestors(info.conversationId).includes(registration.conversationId)),\n\t\t\t\t)\n\t\t\t\t.map((registration) => ({\n\t\t\t\t\thandlers: registration.handlers as Partial<H>,\n\t\t\t\t\tnamespace: registration.namespace,\n\t\t\t\t\tapi: { ...info, kind: kind.name },\n\t\t\t\t}));\n\t\treturn {\n\t\t\thandlers,\n\t\t\tasync each(ctx, fn, onValue) {\n\t\t\t\tfor (const binding of handlers()) {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst v = await fn(binding.handlers, binding.api);\n\t\t\t\t\t\tif (v !== undefined && onValue?.(v as never) === true) return;\n\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\tif (ctx.abortSignal?.aborted) throw error;\n\t\t\t\t\t\tonReport(error);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t},\n\t\t};\n\t};\n}\n"]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Experimental Pico3 kernel API.
|
|
3
|
+
*
|
|
4
|
+
* This subpath is intentionally separate from the package root while the kernel
|
|
5
|
+
* and its Chord integration are being validated.
|
|
6
|
+
*/
|
|
7
|
+
export { bashTool } from "./bash.ts";
|
|
8
|
+
export { Bounded } from "./bounded.ts";
|
|
9
|
+
export type { ChordViewBridge, ChordViewBridgeOptions, PublishedConversationView, } from "./chord.ts";
|
|
10
|
+
export { attachChordView, createPicoConversationService, PicoConversationService, PicoHarnessService, } from "./chord.ts";
|
|
11
|
+
export type { BuiltinKinds, ConfigFor, ConversationHandle, HarnessOptions, InputHandle, Watch, } from "./harness.ts";
|
|
12
|
+
export { applyEnvelope, captureActiveTranscript, entries, Harness, isCoreKind, kinds, WATCH_CAPACITY, withAbortSignal, } from "./harness.ts";
|
|
13
|
+
export { JsonlStorage } from "./jsonl.ts";
|
|
14
|
+
export { MemoryStorage } from "./memory.ts";
|
|
15
|
+
export type { SystemSection } from "./system.ts";
|
|
16
|
+
export { defineSystemSection, systemSections } from "./system.ts";
|
|
17
|
+
export type { AbortClosure, AnyKind, AnyToolDeclaration, AssistantMessage, BeforeToolApi, Checkpoint, CheckpointAt, Closure, Completion, ConfigFacade, ConfigOf, ConfigOfKinds, ConfigShape, ContextEdit, ContextView, Conversation, ConversationSpec, ConversationView, DeferredHandle, DisjointConfig, DocRef, Entry, EntryInput, EntryKind, EntryRef, EntryScan, Envelope, GenerationStatus, HookApi, HookBinding, HookInfo, HookResult, HookRunner, HooksOf, HostTx, Id, Input, InputOf, JsonObject, JsonPrimitive, JsonValue, Kind, KindConfig, KindTypes, Message, Model, ModelRef, Models, Namespace, NamespaceDefaults, NewEntry, Outcome, OwnedConversation, OwnedConversationSpec, PhaseHandler, PluginHandler, ProcessHost, ProcessSpec, ProcessStatus, QueuedInput, RequestMessage, RequestOptions, RetryPolicy, RewindableState, Runtime, SendInput, Seq, SessionState, SlotOf, Step, StickyState, Storage, Stored, StoredMessage, SystemMessage, Task, TaskOf, TaskRef, TaskScan, TaskTx, ThinkingLevel, ToolApi, ToolCall, ToolControl, ToolDeclaration, ToolDiagnostic, ToolResult, ToolResultMessage, ToolSlot, TurnState, TurnView, UserInput, UserMessage, ViewEvent, Write, } from "./types.ts";
|
|
18
|
+
export { Closed, CollapseInProgress, ConversationBusy, defineEntry, defineTask, Faulted, Forbidden, GenerationInProgress, memoOnce, ReadAfterWrite, TaskContractFault, toStored, } from "./types.ts";
|
|
19
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/harness/pico3/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AACrC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,YAAY,EACX,eAAe,EACf,sBAAsB,EACtB,yBAAyB,GACzB,MAAM,YAAY,CAAC;AACpB,OAAO,EACN,eAAe,EACf,6BAA6B,EAC7B,uBAAuB,EACvB,kBAAkB,GAClB,MAAM,YAAY,CAAC;AACpB,YAAY,EACX,YAAY,EACZ,SAAS,EACT,kBAAkB,EAClB,cAAc,EACd,WAAW,EACX,KAAK,GACL,MAAM,cAAc,CAAC;AACtB,OAAO,EACN,aAAa,EACb,uBAAuB,EACvB,OAAO,EACP,OAAO,EACP,UAAU,EACV,KAAK,EACL,cAAc,EACd,eAAe,GACf,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAC1C,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC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,OAAO,EACN,MAAM,EACN,kBAAkB,EAClB,gBAAgB,EAChB,WAAW,EACX,UAAU,EACV,OAAO,EACP,SAAS,EACT,oBAAoB,EACpB,QAAQ,EACR,cAAc,EACd,iBAAiB,EACjB,QAAQ,GACR,MAAM,YAAY,CAAC","sourcesContent":["/**\n * Experimental Pico3 kernel API.\n *\n * This subpath is intentionally separate from the package root while the kernel\n * and its Chord integration are being validated.\n */\n\nexport { bashTool } from \"./bash.ts\";\nexport { Bounded } from \"./bounded.ts\";\nexport type {\n\tChordViewBridge,\n\tChordViewBridgeOptions,\n\tPublishedConversationView,\n} from \"./chord.ts\";\nexport {\n\tattachChordView,\n\tcreatePicoConversationService,\n\tPicoConversationService,\n\tPicoHarnessService,\n} from \"./chord.ts\";\nexport type {\n\tBuiltinKinds,\n\tConfigFor,\n\tConversationHandle,\n\tHarnessOptions,\n\tInputHandle,\n\tWatch,\n} from \"./harness.ts\";\nexport {\n\tapplyEnvelope,\n\tcaptureActiveTranscript,\n\tentries,\n\tHarness,\n\tisCoreKind,\n\tkinds,\n\tWATCH_CAPACITY,\n\twithAbortSignal,\n} from \"./harness.ts\";\nexport { JsonlStorage } from \"./jsonl.ts\";\nexport { MemoryStorage } from \"./memory.ts\";\nexport type { SystemSection } from \"./system.ts\";\nexport { defineSystemSection, systemSections } from \"./system.ts\";\nexport type {\n\tAbortClosure,\n\tAnyKind,\n\tAnyToolDeclaration,\n\tAssistantMessage,\n\tBeforeToolApi,\n\tCheckpoint,\n\tCheckpointAt,\n\tClosure,\n\tCompletion,\n\tConfigFacade,\n\tConfigOf,\n\tConfigOfKinds,\n\tConfigShape,\n\tContextEdit,\n\tContextView,\n\tConversation,\n\tConversationSpec,\n\tConversationView,\n\tDeferredHandle,\n\tDisjointConfig,\n\tDocRef,\n\tEntry,\n\tEntryInput,\n\tEntryKind,\n\tEntryRef,\n\tEntryScan,\n\tEnvelope,\n\tGenerationStatus,\n\tHookApi,\n\tHookBinding,\n\tHookInfo,\n\tHookResult,\n\tHookRunner,\n\tHooksOf,\n\tHostTx,\n\tId,\n\tInput,\n\tInputOf,\n\tJsonObject,\n\tJsonPrimitive,\n\tJsonValue,\n\tKind,\n\tKindConfig,\n\tKindTypes,\n\tMessage,\n\tModel,\n\tModelRef,\n\tModels,\n\tNamespace,\n\tNamespaceDefaults,\n\tNewEntry,\n\tOutcome,\n\tOwnedConversation,\n\tOwnedConversationSpec,\n\tPhaseHandler,\n\tPluginHandler,\n\tProcessHost,\n\tProcessSpec,\n\tProcessStatus,\n\tQueuedInput,\n\tRequestMessage,\n\tRequestOptions,\n\tRetryPolicy,\n\tRewindableState,\n\tRuntime,\n\tSendInput,\n\tSeq,\n\tSessionState,\n\tSlotOf,\n\tStep,\n\tStickyState,\n\tStorage,\n\tStored,\n\tStoredMessage,\n\tSystemMessage,\n\tTask,\n\tTaskOf,\n\tTaskRef,\n\tTaskScan,\n\tTaskTx,\n\tThinkingLevel,\n\tToolApi,\n\tToolCall,\n\tToolControl,\n\tToolDeclaration,\n\tToolDiagnostic,\n\tToolResult,\n\tToolResultMessage,\n\tToolSlot,\n\tTurnState,\n\tTurnView,\n\tUserInput,\n\tUserMessage,\n\tViewEvent,\n\tWrite,\n} from \"./types.ts\";\nexport {\n\tClosed,\n\tCollapseInProgress,\n\tConversationBusy,\n\tdefineEntry,\n\tdefineTask,\n\tFaulted,\n\tForbidden,\n\tGenerationInProgress,\n\tmemoOnce,\n\tReadAfterWrite,\n\tTaskContractFault,\n\ttoStored,\n} from \"./types.ts\";\n"]}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Experimental Pico3 kernel API.
|
|
3
|
+
*
|
|
4
|
+
* This subpath is intentionally separate from the package root while the kernel
|
|
5
|
+
* and its Chord integration are being validated.
|
|
6
|
+
*/
|
|
7
|
+
export { bashTool } from "./bash.js";
|
|
8
|
+
export { Bounded } from "./bounded.js";
|
|
9
|
+
export { attachChordView, createPicoConversationService, PicoConversationService, PicoHarnessService, } from "./chord.js";
|
|
10
|
+
export { applyEnvelope, captureActiveTranscript, entries, Harness, isCoreKind, kinds, WATCH_CAPACITY, withAbortSignal, } from "./harness.js";
|
|
11
|
+
export { JsonlStorage } from "./jsonl.js";
|
|
12
|
+
export { MemoryStorage } from "./memory.js";
|
|
13
|
+
export { defineSystemSection, systemSections } from "./system.js";
|
|
14
|
+
export { Closed, CollapseInProgress, ConversationBusy, defineEntry, defineTask, Faulted, Forbidden, GenerationInProgress, memoOnce, ReadAfterWrite, TaskContractFault, toStored, } from "./types.js";
|
|
15
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/harness/pico3/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AACrC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAMvC,OAAO,EACN,eAAe,EACf,6BAA6B,EAC7B,uBAAuB,EACvB,kBAAkB,GAClB,MAAM,YAAY,CAAC;AASpB,OAAO,EACN,aAAa,EACb,uBAAuB,EACvB,OAAO,EACP,OAAO,EACP,UAAU,EACV,KAAK,EACL,cAAc,EACd,eAAe,GACf,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAC1C,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5C,OAAO,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAkGlE,OAAO,EACN,MAAM,EACN,kBAAkB,EAClB,gBAAgB,EAChB,WAAW,EACX,UAAU,EACV,OAAO,EACP,SAAS,EACT,oBAAoB,EACpB,QAAQ,EACR,cAAc,EACd,iBAAiB,EACjB,QAAQ,GACR,MAAM,YAAY,CAAC","sourcesContent":["/**\n * Experimental Pico3 kernel API.\n *\n * This subpath is intentionally separate from the package root while the kernel\n * and its Chord integration are being validated.\n */\n\nexport { bashTool } from \"./bash.ts\";\nexport { Bounded } from \"./bounded.ts\";\nexport type {\n\tChordViewBridge,\n\tChordViewBridgeOptions,\n\tPublishedConversationView,\n} from \"./chord.ts\";\nexport {\n\tattachChordView,\n\tcreatePicoConversationService,\n\tPicoConversationService,\n\tPicoHarnessService,\n} from \"./chord.ts\";\nexport type {\n\tBuiltinKinds,\n\tConfigFor,\n\tConversationHandle,\n\tHarnessOptions,\n\tInputHandle,\n\tWatch,\n} from \"./harness.ts\";\nexport {\n\tapplyEnvelope,\n\tcaptureActiveTranscript,\n\tentries,\n\tHarness,\n\tisCoreKind,\n\tkinds,\n\tWATCH_CAPACITY,\n\twithAbortSignal,\n} from \"./harness.ts\";\nexport { JsonlStorage } from \"./jsonl.ts\";\nexport { MemoryStorage } from \"./memory.ts\";\nexport type { SystemSection } from \"./system.ts\";\nexport { defineSystemSection, systemSections } from \"./system.ts\";\nexport type {\n\tAbortClosure,\n\tAnyKind,\n\tAnyToolDeclaration,\n\tAssistantMessage,\n\tBeforeToolApi,\n\tCheckpoint,\n\tCheckpointAt,\n\tClosure,\n\tCompletion,\n\tConfigFacade,\n\tConfigOf,\n\tConfigOfKinds,\n\tConfigShape,\n\tContextEdit,\n\tContextView,\n\tConversation,\n\tConversationSpec,\n\tConversationView,\n\tDeferredHandle,\n\tDisjointConfig,\n\tDocRef,\n\tEntry,\n\tEntryInput,\n\tEntryKind,\n\tEntryRef,\n\tEntryScan,\n\tEnvelope,\n\tGenerationStatus,\n\tHookApi,\n\tHookBinding,\n\tHookInfo,\n\tHookResult,\n\tHookRunner,\n\tHooksOf,\n\tHostTx,\n\tId,\n\tInput,\n\tInputOf,\n\tJsonObject,\n\tJsonPrimitive,\n\tJsonValue,\n\tKind,\n\tKindConfig,\n\tKindTypes,\n\tMessage,\n\tModel,\n\tModelRef,\n\tModels,\n\tNamespace,\n\tNamespaceDefaults,\n\tNewEntry,\n\tOutcome,\n\tOwnedConversation,\n\tOwnedConversationSpec,\n\tPhaseHandler,\n\tPluginHandler,\n\tProcessHost,\n\tProcessSpec,\n\tProcessStatus,\n\tQueuedInput,\n\tRequestMessage,\n\tRequestOptions,\n\tRetryPolicy,\n\tRewindableState,\n\tRuntime,\n\tSendInput,\n\tSeq,\n\tSessionState,\n\tSlotOf,\n\tStep,\n\tStickyState,\n\tStorage,\n\tStored,\n\tStoredMessage,\n\tSystemMessage,\n\tTask,\n\tTaskOf,\n\tTaskRef,\n\tTaskScan,\n\tTaskTx,\n\tThinkingLevel,\n\tToolApi,\n\tToolCall,\n\tToolControl,\n\tToolDeclaration,\n\tToolDiagnostic,\n\tToolResult,\n\tToolResultMessage,\n\tToolSlot,\n\tTurnState,\n\tTurnView,\n\tUserInput,\n\tUserMessage,\n\tViewEvent,\n\tWrite,\n} from \"./types.ts\";\nexport {\n\tClosed,\n\tCollapseInProgress,\n\tConversationBusy,\n\tdefineEntry,\n\tdefineTask,\n\tFaulted,\n\tForbidden,\n\tGenerationInProgress,\n\tmemoOnce,\n\tReadAfterWrite,\n\tTaskContractFault,\n\ttoStored,\n} from \"./types.ts\";\n"]}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { Context } from "@earendil-works/chord";
|
|
2
|
+
import { MemoryStorage } from "./memory.ts";
|
|
3
|
+
import type { DocRef, Seq, Storage, Write } from "./types.ts";
|
|
4
|
+
/**
|
|
5
|
+
* Files in a session directory:
|
|
6
|
+
* main.jsonl conversations, entries, inputs, rewindable/session doc ops, each task's
|
|
7
|
+
* CREATE and TERMINAL records, and one marker per commit. Append-only, never rewritten.
|
|
8
|
+
* sticky-<conversation>.jsonl sticky doc ops; rewritten from its last base by `truncate` (on the Session line)
|
|
9
|
+
* task-<id>.jsonl a live task's intermediate patches (running, checkpoints); unlinked after its
|
|
10
|
+
* terminal record is published in main
|
|
11
|
+
*
|
|
12
|
+
* Publication (§10): one commit `Seq`; sidecar records are appended first, then exactly one main
|
|
13
|
+
* record, last, listing the sidecar refs it expects — the publication point. Replay applies a sidecar
|
|
14
|
+
* record only when main has the marker for that seq naming that file; unconfirmed sidecar tails are
|
|
15
|
+
* ignored. Every record carries the committed-ID high-water. Bytes after the last newline of any file
|
|
16
|
+
* are a torn write and are truncated before the file is opened for append. There is no compaction.
|
|
17
|
+
*
|
|
18
|
+
* With `fsync: false`, recovery covers process termination while the OS and filesystem remain alive.
|
|
19
|
+
* It does not promise that an acknowledged commit survives power loss, kernel/host failure,
|
|
20
|
+
* storage-cache loss, or a filesystem that loses/reorders completed writes. An acknowledged tail may
|
|
21
|
+
* roll back, or a surviving main marker may reference a missing sidecar and make open fail rather
|
|
22
|
+
* than expose partial state. A lost pre-effect checkpoint can cause an external effect to be attempted
|
|
23
|
+
* again; external idempotency remains required.
|
|
24
|
+
*
|
|
25
|
+
* With `fsync: true`, each sidecar is flushed before the main publication marker is flushed. This
|
|
26
|
+
* strengthens file-data durability and ordering across machine failure. It is not a complete database
|
|
27
|
+
* guarantee: newly created files, renames, and unlinks are not followed by a parent-directory fsync,
|
|
28
|
+
* and storage hardware/filesystems may provide weaker guarantees.
|
|
29
|
+
*
|
|
30
|
+
* One process owns a directory at a time; a second process is unsupported.
|
|
31
|
+
*/
|
|
32
|
+
export declare class JsonlStorage extends MemoryStorage implements Storage {
|
|
33
|
+
private mainFd;
|
|
34
|
+
private readonly sidecars;
|
|
35
|
+
private readonly taskSidecars;
|
|
36
|
+
private readonly dir;
|
|
37
|
+
readonly fsync: boolean;
|
|
38
|
+
private closedOnce;
|
|
39
|
+
private constructor();
|
|
40
|
+
static open(dir: string, opts?: {
|
|
41
|
+
fsync?: boolean;
|
|
42
|
+
}): Promise<JsonlStorage>;
|
|
43
|
+
/** For tests and tooling: sizes of every file in the directory. */
|
|
44
|
+
sizes(): {
|
|
45
|
+
[file: string]: number;
|
|
46
|
+
};
|
|
47
|
+
private replay;
|
|
48
|
+
commit(writes: readonly Write[], ctx: Context): Promise<Seq>;
|
|
49
|
+
private nextIdAfter;
|
|
50
|
+
private append;
|
|
51
|
+
private sidecar;
|
|
52
|
+
private taskSidecar;
|
|
53
|
+
private retireTaskSidecar;
|
|
54
|
+
/** Rewrite the sticky sidecar from its last base: temp file, optional fsync, rename. Called on the Session line. */
|
|
55
|
+
truncate(ref: DocRef, _ctx?: Context): Promise<void>;
|
|
56
|
+
close(_ctx?: Context): Promise<void>;
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=jsonl.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jsonl.d.ts","sourceRoot":"","sources":["../../../src/harness/pico3/jsonl.ts"],"names":[],"mappings":"AAeA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAGrD,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,KAAK,EAAE,MAAM,EAAM,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAUlE;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,qBAAa,YAAa,SAAQ,aAAc,YAAW,OAAO;IACjE,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAyB;IAClD,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAyB;IACtD,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAS;IAC7B,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC;IACxB,OAAO,CAAC,UAAU,CAAS;IAE3B,OAAO,eAKN;IAED,OAAa,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,GAAE;QAAE,KAAK,CAAC,EAAE,OAAO,CAAA;KAAO,GAAG,OAAO,CAAC,YAAY,CAAC,CAWpF;IAED,mEAAmE;IACnE,KAAK,IAAI;QAAE,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAIlC;YAEa,MAAM;IAsGL,MAAM,CAAC,MAAM,EAAE,SAAS,KAAK,EAAE,EAAE,GAAG,EAAE,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,CAoC1E;IACD,OAAO,CAAC,WAAW;IAkBnB,OAAO,CAAC,MAAM;IAKd,OAAO,CAAC,OAAO;IAQf,OAAO,CAAC,WAAW;IAQnB,OAAO,CAAC,iBAAiB;IASzB,oHAAoH;IACrG,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CA8BlE;IAEc,KAAK,CAAC,IAAI,CAAC,EAAE,OAAO,iBAOlC;CACD","sourcesContent":["import {\n\tcloseSync,\n\texistsSync,\n\tfsyncSync,\n\tftruncateSync,\n\tmkdirSync,\n\topenSync,\n\treaddirSync,\n\treadFileSync,\n\trenameSync,\n\tstatSync,\n\tunlinkSync,\n\twriteSync,\n} from \"node:fs\";\nimport { join } from \"node:path\";\nimport type { Context } from \"@earendil-works/chord\";\nimport { BACKGROUND_CONTEXT } from \"@earendil-works/chord/context\";\nimport { isBase, type Op } from \"@earendil-works/chord/delta\";\nimport { MemoryStorage } from \"./memory.ts\";\nimport type { DocRef, Id, Seq, Storage, Write } from \"./types.ts\";\n\n/** One line of any file. `refs` (main only) lists the sidecar files this commit also wrote. */\ninterface Record_ {\n\tseq: Seq;\n\tmaxId: Id;\n\twrites: Write[];\n\trefs?: string[];\n}\n\n/**\n * Files in a session directory:\n * main.jsonl conversations, entries, inputs, rewindable/session doc ops, each task's\n * CREATE and TERMINAL records, and one marker per commit. Append-only, never rewritten.\n * sticky-<conversation>.jsonl sticky doc ops; rewritten from its last base by `truncate` (on the Session line)\n * task-<id>.jsonl a live task's intermediate patches (running, checkpoints); unlinked after its\n * terminal record is published in main\n *\n * Publication (§10): one commit `Seq`; sidecar records are appended first, then exactly one main\n * record, last, listing the sidecar refs it expects — the publication point. Replay applies a sidecar\n * record only when main has the marker for that seq naming that file; unconfirmed sidecar tails are\n * ignored. Every record carries the committed-ID high-water. Bytes after the last newline of any file\n * are a torn write and are truncated before the file is opened for append. There is no compaction.\n *\n * With `fsync: false`, recovery covers process termination while the OS and filesystem remain alive.\n * It does not promise that an acknowledged commit survives power loss, kernel/host failure,\n * storage-cache loss, or a filesystem that loses/reorders completed writes. An acknowledged tail may\n * roll back, or a surviving main marker may reference a missing sidecar and make open fail rather\n * than expose partial state. A lost pre-effect checkpoint can cause an external effect to be attempted\n * again; external idempotency remains required.\n *\n * With `fsync: true`, each sidecar is flushed before the main publication marker is flushed. This\n * strengthens file-data durability and ordering across machine failure. It is not a complete database\n * guarantee: newly created files, renames, and unlinks are not followed by a parent-directory fsync,\n * and storage hardware/filesystems may provide weaker guarantees.\n *\n * One process owns a directory at a time; a second process is unsupported.\n */\nexport class JsonlStorage extends MemoryStorage implements Storage {\n\tprivate mainFd: number;\n\tprivate readonly sidecars = new Map<Id, number>(); // sticky docs, by conversation\n\tprivate readonly taskSidecars = new Map<Id, number>();\n\tprivate readonly dir: string;\n\treadonly fsync: boolean;\n\tprivate closedOnce = false;\n\n\tprivate constructor(dir: string, fsync: boolean) {\n\t\tsuper();\n\t\tthis.dir = dir;\n\t\tthis.fsync = fsync;\n\t\tthis.mainFd = openSync(join(dir, \"main.jsonl\"), \"a\");\n\t}\n\n\tstatic async open(dir: string, opts: { fsync?: boolean } = {}): Promise<JsonlStorage> {\n\t\tmkdirSync(dir, { recursive: true });\n\t\tfor (const f of readdirSync(dir)) if (f.endsWith(\".jsonl\")) truncateTornTail(join(dir, f));\n\t\tconst storage = new JsonlStorage(dir, opts.fsync ?? true);\n\t\ttry {\n\t\t\tawait storage.replay();\n\t\t\treturn storage;\n\t\t} catch (error) {\n\t\t\tawait storage.close();\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\t/** For tests and tooling: sizes of every file in the directory. */\n\tsizes(): { [file: string]: number } {\n\t\tconst out: { [file: string]: number } = {};\n\t\tfor (const f of readdirSync(this.dir)) out[f] = statSync(join(this.dir, f)).size;\n\t\treturn out;\n\t}\n\n\tprivate async replay() {\n\t\tconst read = (file: string): { record: Record_; end: number }[] => {\n\t\t\tif (!existsSync(file)) return [];\n\t\t\tconst bytes = readFileSync(file);\n\t\t\tconst out: { record: Record_; end: number }[] = [];\n\t\t\tlet start = 0;\n\t\t\tlet last = 0;\n\t\t\tfor (let end = bytes.indexOf(0x0a, start); end >= 0; end = bytes.indexOf(0x0a, start)) {\n\t\t\t\tconst line = bytes.subarray(start, end).toString(\"utf8\");\n\t\t\t\tstart = end + 1;\n\t\t\t\tif (line === \"\") continue;\n\t\t\t\tlet record: Record_;\n\t\t\t\ttry {\n\t\t\t\t\trecord = JSON.parse(line) as Record_;\n\t\t\t\t} catch (error) {\n\t\t\t\t\tthrow new Error(`${file}: malformed record: ${String(error)}`);\n\t\t\t\t}\n\t\t\t\tif (typeof record.seq !== \"number\" || typeof record.maxId !== \"number\" || !Array.isArray(record.writes))\n\t\t\t\t\tthrow new Error(`${file}: record lacks seq/maxId/writes`);\n\t\t\t\tif (record.seq <= last) throw new Error(`${file}: sequence not increasing at ${record.seq}`);\n\t\t\t\tlast = record.seq;\n\t\t\t\tout.push({ record, end: start });\n\t\t\t}\n\t\t\treturn out;\n\t\t};\n\t\tlet maxId = 0;\n\t\t// Main is the commit log. Confirmed = (seq → refs) for every marker.\n\t\tconst main = read(join(this.dir, \"main.jsonl\")).map(({ record }) => record);\n\t\tconst confirmed = new Map<Seq, Set<string>>();\n\t\tfor (const record of main) confirmed.set(record.seq, new Set(record.refs ?? []));\n\t\t// Merge every file's records by seq so batches apply in commit order.\n\t\tconst sidecarFiles = readdirSync(this.dir).filter(\n\t\t\t(file) => file.startsWith(\"sticky-\") || file.startsWith(\"task-\"),\n\t\t);\n\t\tconst bySeq = new Map<Seq, Write[]>();\n\t\tconst found = new Map<Seq, Set<string>>();\n\t\tconst retainedStickyBase = new Map<string, Seq>();\n\t\tconst retiredTasks = new Map<Id, Seq>();\n\t\tfor (const record of main) {\n\t\t\tfor (const write of record.writes) {\n\t\t\t\tif (write.type === \"task.patch\" && write.patch.status === \"terminal\")\n\t\t\t\t\tretiredTasks.set(write.patch.id, record.seq);\n\t\t\t}\n\t\t\tbySeq.set(record.seq, [...record.writes]);\n\t\t\tmaxId = Math.max(maxId, record.maxId);\n\t\t}\n\t\tfor (const file of sidecarFiles) {\n\t\t\tconst path = join(this.dir, file);\n\t\t\tconst records = read(path);\n\t\t\tconst first = records[0]?.record;\n\t\t\tif (\n\t\t\t\tfile.startsWith(\"sticky-\") &&\n\t\t\t\tfirst !== undefined &&\n\t\t\t\tisBase(first.writes.flatMap((write) => (write.type === \"doc\" ? write.ops : [])))\n\t\t\t) {\n\t\t\t\tretainedStickyBase.set(file, first.seq);\n\t\t\t}\n\t\t\tlet confirmedEnd = 0;\n\t\t\tlet sawUnconfirmed = false;\n\t\t\tfor (const { record, end } of records) {\n\t\t\t\tif (!confirmed.get(record.seq)?.has(file)) {\n\t\t\t\t\tsawUnconfirmed = true;\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tif (sawUnconfirmed)\n\t\t\t\t\tthrow new Error(`${path}: confirmed record follows an unconfirmed tail at ${record.seq}`);\n\t\t\t\tconfirmedEnd = end;\n\t\t\t\tconst files = found.get(record.seq) ?? new Set<string>();\n\t\t\t\tfiles.add(file);\n\t\t\t\tfound.set(record.seq, files);\n\t\t\t\tbySeq.get(record.seq)!.push(...record.writes);\n\t\t\t\tmaxId = Math.max(maxId, record.maxId);\n\t\t\t}\n\t\t\tif (sawUnconfirmed) truncateTo(path, confirmedEnd);\n\t\t}\n\t\tfor (const record of main) {\n\t\t\tfor (const file of record.refs ?? []) {\n\t\t\t\tif (found.get(record.seq)?.has(file)) continue;\n\t\t\t\tconst retainedFrom = retainedStickyBase.get(file);\n\t\t\t\tif (retainedFrom !== undefined && record.seq < retainedFrom) continue;\n\t\t\t\tif (file.startsWith(\"task-\")) {\n\t\t\t\t\tconst id = Number(file.slice(5, -6));\n\t\t\t\t\tconst retiredAt = retiredTasks.get(id);\n\t\t\t\t\tif (retiredAt !== undefined && record.seq < retiredAt) continue;\n\t\t\t\t}\n\t\t\t\tthrow new Error(`${join(this.dir, file)}: missing record for published sequence ${record.seq}`);\n\t\t\t}\n\t\t}\n\t\tfor (const seq of [...bySeq.keys()].sort((a, b) => a - b)) {\n\t\t\tthis.seq = seq - 1;\n\t\t\tawait super.commit(bySeq.get(seq)!, BACKGROUND_CONTEXT);\n\t\t}\n\t\tthis.setNextId(maxId + 1);\n\t\t// Task sidecars belong to live tasks only; a leftover for a terminal (or unknown) task cannot resurrect anything.\n\t\tfor (const f of sidecarFiles) {\n\t\t\tif (!f.startsWith(\"task-\")) continue;\n\t\t\tconst id = Number(f.slice(5, -6));\n\t\t\tconst t = await super.task(id);\n\t\t\tif (t === undefined || t.status === \"terminal\") unlinkSync(join(this.dir, f));\n\t\t}\n\t}\n\n\toverride async commit(writes: readonly Write[], ctx: Context): Promise<Seq> {\n\t\tif (this.closedOnce) throw new Error(\"storage closed\");\n\t\tconst seq = this.seq + 1;\n\t\tconst apply = this.stage(writes, seq); // validates the whole batch first; throws before any file is touched\n\t\tconst maxId = this.nextIdAfter(writes);\n\t\tconst main: Write[] = [];\n\t\tconst sticky = new Map<Id, Write[]>();\n\t\tconst tasks = new Map<Id, Write[]>();\n\t\tconst retired: Id[] = [];\n\t\tfor (const w of writes) {\n\t\t\tif (w.type === \"doc\" && w.ref.doc === \"sticky\")\n\t\t\t\t(sticky.get(w.ref.conversationId) ?? sticky.set(w.ref.conversationId, []).get(w.ref.conversationId)!).push(\n\t\t\t\t\tw,\n\t\t\t\t);\n\t\t\telse if (w.type === \"task.patch\" && w.patch.status !== \"terminal\")\n\t\t\t\t(tasks.get(w.patch.id) ?? tasks.set(w.patch.id, []).get(w.patch.id)!).push(w);\n\t\t\telse {\n\t\t\t\tmain.push(w);\n\t\t\t\tif (w.type === \"task.patch\" && w.patch.status === \"terminal\") retired.push(w.patch.id);\n\t\t\t}\n\t\t}\n\t\tconst refs: string[] = [];\n\t\tfor (const [id, ws] of tasks) {\n\t\t\tthis.append(this.taskSidecar(id), { seq, maxId, writes: ws });\n\t\t\trefs.push(`task-${id}.jsonl`);\n\t\t}\n\t\tfor (const [c, ws] of sticky) {\n\t\t\tthis.append(this.sidecar(c), { seq, maxId, writes: ws });\n\t\t\trefs.push(`sticky-${c}.jsonl`);\n\t\t}\n\t\tthis.append(this.mainFd, { seq, maxId, writes: main, ...(refs.length ? { refs } : {}) }); // the publication point\n\t\tapply();\n\t\tthis.seq = seq;\n\t\tfor (const id of retired) this.retireTaskSidecar(id); // after the terminal record is durably published\n\t\tvoid ctx;\n\t\treturn seq;\n\t}\n\tprivate nextIdAfter(writes: readonly Write[]): Id {\n\t\tlet max = this.nextIdValue - 1;\n\t\tfor (const w of writes) {\n\t\t\tconst id =\n\t\t\t\tw.type === \"conversation\"\n\t\t\t\t\t? w.conversation.id\n\t\t\t\t\t: w.type === \"entry\"\n\t\t\t\t\t\t? w.entry.id\n\t\t\t\t\t\t: w.type === \"task\"\n\t\t\t\t\t\t\t? w.task.id\n\t\t\t\t\t\t\t: w.type === \"input\"\n\t\t\t\t\t\t\t\t? w.input.id\n\t\t\t\t\t\t\t\t: 0;\n\t\t\tmax = Math.max(max, id);\n\t\t}\n\t\treturn max;\n\t}\n\n\tprivate append(fd: number, record: Record_) {\n\t\twriteSync(fd, `${JSON.stringify(record)}\\n`);\n\t\tif (this.fsync) fsyncSync(fd);\n\t}\n\n\tprivate sidecar(conversationId: Id): number {\n\t\tlet fd = this.sidecars.get(conversationId);\n\t\tif (fd === undefined) {\n\t\t\tfd = openSync(join(this.dir, `sticky-${conversationId}.jsonl`), \"a\");\n\t\t\tthis.sidecars.set(conversationId, fd);\n\t\t}\n\t\treturn fd;\n\t}\n\tprivate taskSidecar(id: Id): number {\n\t\tlet fd = this.taskSidecars.get(id);\n\t\tif (fd === undefined) {\n\t\t\tfd = openSync(join(this.dir, `task-${id}.jsonl`), \"a\");\n\t\t\tthis.taskSidecars.set(id, fd);\n\t\t}\n\t\treturn fd;\n\t}\n\tprivate retireTaskSidecar(id: Id) {\n\t\tconst fd = this.taskSidecars.get(id);\n\t\tif (fd !== undefined) closeSync(fd);\n\t\tthis.taskSidecars.delete(id);\n\t\ttry {\n\t\t\tunlinkSync(join(this.dir, `task-${id}.jsonl`));\n\t\t} catch {}\n\t}\n\n\t/** Rewrite the sticky sidecar from its last base: temp file, optional fsync, rename. Called on the Session line. */\n\toverride async truncate(ref: DocRef, _ctx?: Context): Promise<void> {\n\t\tawait super.truncate(ref);\n\t\tif (ref.doc !== \"sticky\") return;\n\t\tconst file = join(this.dir, `sticky-${ref.conversationId}.jsonl`);\n\t\tif (!existsSync(file)) return;\n\t\tconst lines = readFileSync(file, \"utf8\").split(\"\\n\").filter(Boolean);\n\t\tlet start = 0;\n\t\tfor (let i = lines.length - 1; i >= 0; i--)\n\t\t\tif (\n\t\t\t\tisBase((JSON.parse(lines[i]!) as Record_).writes.flatMap((w) => (w.type === \"doc\" ? w.ops : [])) as Op[])\n\t\t\t) {\n\t\t\t\tstart = i;\n\t\t\t\tbreak;\n\t\t\t}\n\t\tif (start === 0) return;\n\t\tconst tmp = `${file}.tmp`;\n\t\tconst fd = openSync(tmp, \"w\");\n\t\twriteSync(\n\t\t\tfd,\n\t\t\tlines\n\t\t\t\t.slice(start)\n\t\t\t\t.map((l) => `${l}\\n`)\n\t\t\t\t.join(\"\"),\n\t\t);\n\t\tif (this.fsync) fsyncSync(fd);\n\t\tcloseSync(fd);\n\t\tconst old = this.sidecars.get(ref.conversationId);\n\t\tif (old !== undefined) closeSync(old);\n\t\trenameSync(tmp, file);\n\t\tthis.sidecars.set(ref.conversationId, openSync(file, \"a\"));\n\t}\n\n\toverride async close(_ctx?: Context) {\n\t\tif (this.closedOnce) return;\n\t\tthis.closedOnce = true;\n\t\tawait super.close();\n\t\tcloseSync(this.mainFd);\n\t\tfor (const fd of this.sidecars.values()) closeSync(fd);\n\t\tfor (const fd of this.taskSidecars.values()) closeSync(fd);\n\t}\n}\n\nfunction truncateTo(file: string, length: number): void {\n\tconst fd = openSync(file, \"r+\");\n\ttry {\n\t\tftruncateSync(fd, length);\n\t} finally {\n\t\tcloseSync(fd);\n\t}\n}\n\n/** Bytes after the last newline are a torn write: cut them so a later append cannot extend a corrupt line. */\nfunction truncateTornTail(file: string) {\n\tconst size = statSync(file).size;\n\tif (size === 0) return;\n\tconst buf = readFileSync(file);\n\tconst end = buf.lastIndexOf(0x0a);\n\tif (end + 1 !== buf.length) truncateTo(file, end + 1);\n}\n"]}
|