@earendil-works/pi-durable 0.99.1 → 0.99.2

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.
Files changed (172) hide show
  1. package/README.md +435 -46
  2. package/dist/entries.d.ts +20 -2
  3. package/dist/entries.d.ts.map +1 -1
  4. package/dist/entries.js +14 -1
  5. package/dist/entries.js.map +1 -1
  6. package/dist/env/index.d.ts +13 -38
  7. package/dist/env/index.d.ts.map +1 -1
  8. package/dist/env/index.js +2 -0
  9. package/dist/env/index.js.map +1 -1
  10. package/dist/env/node.d.ts.map +1 -1
  11. package/dist/env/node.js +51 -58
  12. package/dist/env/node.js.map +1 -1
  13. package/dist/harness/compaction.d.ts +56 -0
  14. package/dist/harness/compaction.d.ts.map +1 -0
  15. package/dist/harness/compaction.js +385 -0
  16. package/dist/harness/compaction.js.map +1 -0
  17. package/dist/harness/config.d.ts +10 -1
  18. package/dist/harness/config.d.ts.map +1 -1
  19. package/dist/harness/config.js +6 -0
  20. package/dist/harness/config.js.map +1 -1
  21. package/dist/harness/context.d.ts +8 -0
  22. package/dist/harness/context.d.ts.map +1 -1
  23. package/dist/harness/context.js +32 -21
  24. package/dist/harness/context.js.map +1 -1
  25. package/dist/harness/events.d.ts +166 -0
  26. package/dist/harness/events.d.ts.map +1 -0
  27. package/dist/harness/events.js +319 -0
  28. package/dist/harness/events.js.map +1 -0
  29. package/dist/harness/generation.d.ts +37 -6
  30. package/dist/harness/generation.d.ts.map +1 -1
  31. package/dist/harness/generation.js +350 -72
  32. package/dist/harness/generation.js.map +1 -1
  33. package/dist/harness/harness.d.ts.map +1 -1
  34. package/dist/harness/harness.js +124 -3
  35. package/dist/harness/harness.js.map +1 -1
  36. package/dist/harness/inbox.d.ts +57 -0
  37. package/dist/harness/inbox.d.ts.map +1 -0
  38. package/dist/harness/inbox.js +99 -0
  39. package/dist/harness/inbox.js.map +1 -0
  40. package/dist/harness/json.d.ts +10 -0
  41. package/dist/harness/json.d.ts.map +1 -0
  42. package/dist/harness/json.js +33 -0
  43. package/dist/harness/json.js.map +1 -0
  44. package/dist/harness/live.d.ts +66 -7
  45. package/dist/harness/live.d.ts.map +1 -1
  46. package/dist/harness/live.js +71 -7
  47. package/dist/harness/live.js.map +1 -1
  48. package/dist/harness/output.d.ts +62 -0
  49. package/dist/harness/output.d.ts.map +1 -0
  50. package/dist/harness/output.js +261 -0
  51. package/dist/harness/output.js.map +1 -0
  52. package/dist/harness/prompt.d.ts +10 -5
  53. package/dist/harness/prompt.d.ts.map +1 -1
  54. package/dist/harness/prompt.js +66 -13
  55. package/dist/harness/prompt.js.map +1 -1
  56. package/dist/harness/registry.d.ts.map +1 -1
  57. package/dist/harness/registry.js +11 -2
  58. package/dist/harness/registry.js.map +1 -1
  59. package/dist/harness/scheduler.d.ts +37 -12
  60. package/dist/harness/scheduler.d.ts.map +1 -1
  61. package/dist/harness/scheduler.js +632 -72
  62. package/dist/harness/scheduler.js.map +1 -1
  63. package/dist/harness/submissions.d.ts +12 -7
  64. package/dist/harness/submissions.d.ts.map +1 -1
  65. package/dist/harness/submissions.js +72 -33
  66. package/dist/harness/submissions.js.map +1 -1
  67. package/dist/harness/tool.d.ts +36 -0
  68. package/dist/harness/tool.d.ts.map +1 -0
  69. package/dist/harness/tool.js +412 -0
  70. package/dist/harness/tool.js.map +1 -0
  71. package/dist/harness/types.d.ts +185 -11
  72. package/dist/harness/types.d.ts.map +1 -1
  73. package/dist/harness/types.js.map +1 -1
  74. package/dist/harness/usage.d.ts +18 -0
  75. package/dist/harness/usage.d.ts.map +1 -0
  76. package/dist/harness/usage.js +57 -0
  77. package/dist/harness/usage.js.map +1 -0
  78. package/dist/harness/view.d.ts +43 -0
  79. package/dist/harness/view.d.ts.map +1 -0
  80. package/dist/harness/view.js +194 -0
  81. package/dist/harness/view.js.map +1 -0
  82. package/dist/index.d.ts +10 -4
  83. package/dist/index.d.ts.map +1 -1
  84. package/dist/index.js +6 -1
  85. package/dist/index.js.map +1 -1
  86. package/dist/session/observation.d.ts +18 -11
  87. package/dist/session/observation.d.ts.map +1 -1
  88. package/dist/session/observation.js +24 -13
  89. package/dist/session/observation.js.map +1 -1
  90. package/dist/session/session.d.ts +10 -1
  91. package/dist/session/session.d.ts.map +1 -1
  92. package/dist/session/session.js +18 -4
  93. package/dist/session/session.js.map +1 -1
  94. package/dist/session/transaction.d.ts +10 -1
  95. package/dist/session/transaction.d.ts.map +1 -1
  96. package/dist/session/transaction.js +83 -27
  97. package/dist/session/transaction.js.map +1 -1
  98. package/dist/storage/jsonl/storage.d.ts +22 -2
  99. package/dist/storage/jsonl/storage.d.ts.map +1 -1
  100. package/dist/storage/jsonl/storage.js +1 -1
  101. package/dist/storage/jsonl/storage.js.map +1 -1
  102. package/dist/storage/memory.js +1 -1
  103. package/dist/storage/memory.js.map +1 -1
  104. package/dist/storage/sqlite/migrations.js +1 -1
  105. package/dist/storage/sqlite/migrations.js.map +1 -1
  106. package/dist/testing/storage-benchmark.d.ts.map +1 -1
  107. package/dist/testing/storage-benchmark.js +0 -1
  108. package/dist/testing/storage-benchmark.js.map +1 -1
  109. package/dist/testing/storage-conformance.d.ts.map +1 -1
  110. package/dist/testing/storage-conformance.js +34 -2
  111. package/dist/testing/storage-conformance.js.map +1 -1
  112. package/dist/tools/bash.d.ts +28 -0
  113. package/dist/tools/bash.d.ts.map +1 -0
  114. package/dist/tools/bash.js +69 -0
  115. package/dist/tools/bash.js.map +1 -0
  116. package/dist/tools/edit-diff.d.ts +89 -0
  117. package/dist/tools/edit-diff.d.ts.map +1 -0
  118. package/dist/tools/edit-diff.js +386 -0
  119. package/dist/tools/edit-diff.js.map +1 -0
  120. package/dist/tools/edit.d.ts +18 -0
  121. package/dist/tools/edit.d.ts.map +1 -0
  122. package/dist/tools/edit.js +116 -0
  123. package/dist/tools/edit.js.map +1 -0
  124. package/dist/tools/env.d.ts +5 -0
  125. package/dist/tools/env.d.ts.map +1 -0
  126. package/dist/tools/env.js +7 -0
  127. package/dist/tools/env.js.map +1 -0
  128. package/dist/tools/file-mutation-queue.d.ts +5 -0
  129. package/dist/tools/file-mutation-queue.d.ts.map +1 -0
  130. package/dist/tools/file-mutation-queue.js +46 -0
  131. package/dist/tools/file-mutation-queue.js.map +1 -0
  132. package/dist/tools/image.d.ts +2 -0
  133. package/dist/tools/image.d.ts.map +1 -0
  134. package/dist/tools/image.js +92 -0
  135. package/dist/tools/image.js.map +1 -0
  136. package/dist/tools/index.d.ts +5 -0
  137. package/dist/tools/index.d.ts.map +1 -0
  138. package/dist/tools/index.js +5 -0
  139. package/dist/tools/index.js.map +1 -0
  140. package/dist/tools/path-utils.d.ts +5 -0
  141. package/dist/tools/path-utils.d.ts.map +1 -0
  142. package/dist/tools/path-utils.js +26 -0
  143. package/dist/tools/path-utils.js.map +1 -0
  144. package/dist/tools/read.d.ts +17 -0
  145. package/dist/tools/read.d.ts.map +1 -0
  146. package/dist/tools/read.js +100 -0
  147. package/dist/tools/read.js.map +1 -0
  148. package/dist/tools/write.d.ts +10 -0
  149. package/dist/tools/write.d.ts.map +1 -0
  150. package/dist/tools/write.js +30 -0
  151. package/dist/tools/write.js.map +1 -0
  152. package/dist/{env/utils/truncate.d.ts → truncate.d.ts} +1 -17
  153. package/dist/truncate.d.ts.map +1 -0
  154. package/dist/truncate.js +146 -0
  155. package/dist/truncate.js.map +1 -0
  156. package/dist/types.d.ts +86 -19
  157. package/dist/types.d.ts.map +1 -1
  158. package/dist/types.js.map +1 -1
  159. package/docs/pico-v5-handoff.md +366 -86
  160. package/docs/pico-v5.md +1373 -283
  161. package/package.json +11 -3
  162. package/dist/env/utils/adaptive-publisher.d.ts +0 -24
  163. package/dist/env/utils/adaptive-publisher.d.ts.map +0 -1
  164. package/dist/env/utils/adaptive-publisher.js +0 -79
  165. package/dist/env/utils/adaptive-publisher.js.map +0 -1
  166. package/dist/env/utils/output-capture.d.ts +0 -31
  167. package/dist/env/utils/output-capture.d.ts.map +0 -1
  168. package/dist/env/utils/output-capture.js +0 -238
  169. package/dist/env/utils/output-capture.js.map +0 -1
  170. package/dist/env/utils/truncate.d.ts.map +0 -1
  171. package/dist/env/utils/truncate.js +0 -294
  172. package/dist/env/utils/truncate.js.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"registry.js","sourceRoot":"","sources":["../../src/harness/registry.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAiBpC,MAAM,WAAW,GAAG,oBAAoB,CAAC;AAEzC,iGAAiG;AACjG,MAAM,CAAC,MAAM,aAAa,GAAuB,CAAC,cAAc,CAAC,CAAC;AAElE,MAAM,CAAC,MAAM,iBAAiB,GAAG,IAAI,CAAC;AAEtC,sIAAsI;AACtI,KAAK,UAAU,YAAY,CAAC,EAAM,EAAE,YAAgC,EAAE,QAA0B;IAC/F,MAAM,EAAE,CAAC,GAAG,CAAC,OAAO,EAAE,YAAY,CAAC,EAAE,CAAC,CAAC;IACvC,IAAI,YAAY,CAAC,MAAM,KAAK,SAAS;QAAE,OAAO;IAC9C,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,kBAAkB,EAAE,YAAY,CAAC,EAAE,CAAC,CAAC,CAAC,WAAW,GAAG,CAAC,GAAG,QAAQ,CAAC,SAAS,EAAE,CAAC,CAAC;AAC7F,CAAC;AAyCD,uFAAuF;AACvF,MAAM,aAAa;IACT,OAAO,CAAkC;IAClD,YAAY,CAAgC;IAE5C,YAAY,OAAwC;QACnD,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACxB,CAAC;IAED,KAAK;QACJ,OAAO,IAAI,CAAC,SAAS,EAAE,CAAC,KAAK,CAAC;IAC/B,CAAC;IAED,IAAI,CAAC,IAAY;QAChB,OAAO,IAAI,CAAC,SAAS,EAAE,CAAC,WAAW,CAAC,GAAG,CAAC,IAAI,CAAC,CA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WAAW,CAAC,IAAI,CAAC,GAAG,CAAC;QAAE,MAAM,IAAI,SAAS,CAAC,eAAe,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,eAAe,WAAW,EAAE,CAAC,CAAC;AACjH,CAAC;AAED,SAAS,cAAc,CAAC,MAAe;IACtC,IACC,CAAC,OAAO,MAAM,KAAK,QAAQ,IAAI,OAAO,MAAM,KAAK,UAAU,CAAC;QAC5D,MAAM,KAAK,IAAI;QACf,OAAQ,MAA6B,CAAC,IAAI,KAAK,UAAU,EACxD,CAAC;QACD,MAA+B,CAAC,IAAI,CAAC,SAAS,EAAE,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC;QAClE,MAAM,IAAI,SAAS,CAAC,8CAA8C,CAAC,CAAC;IACrE,CAAC;AACF,CAAC","sourcesContent":["import type { ConversationRecord, Tx } from \"../types.ts\";\nimport { ConversationConfig } from \"./config.ts\";\nimport { GenerationTask } from \"./generation.ts\";\nimport { LiveDoc } from \"./live.ts\";\nimport type {\n\tAnyTask,\n\tConversationSetup,\n\tHookRegistration,\n\tHookScope,\n\tHooksOf,\n\tPromptSection,\n\tPromptSectionWrapper,\n\tRegistration,\n\tRegistry,\n\tRegistryFailure,\n\tRegistrySnapshot,\n\tToolRegistration,\n\tToolWrapper,\n} from \"./types.ts\";\n\nconst SECTION_KEY = /^[a-z][a-z0-9_-]*$/;\n\n/** Built-in task definitions every registry starts with; they cannot be disposed or replaced. */\nexport const BUILTIN_TASKS: readonly AnyTask[] = [GenerationTask];\n\nexport const BUILTIN_SETUP_KEY = \"pi\";\n\n/** Built-in documents: an empty `pi.live`, and for a new conversation the default configuration with every registered tool active. */\nasync function builtinSetup(tx: Tx, conversation: ConversationRecord, registry: RegistrySnapshot): Promise<void> {\n\tawait tx.doc(LiveDoc, conversation.id);\n\tif (conversation.parent !== undefined) return;\n\t(await tx.doc(ConversationConfig, conversation.id)).activeTools = [...registry.toolNames()];\n}\n\ntype Slot<Tool extends ToolRegistration> =\n\t| { readonly kind: \"tool\"; readonly tool: Tool }\n\t| { readonly kind: \"toolWrap\"; readonly name: string; readonly wrapper: ToolWrapper<Tool> }\n\t| { readonly kind: \"hook\"; readonly taskName: string; readonly hook: StoredHook }\n\t| { readonly kind: \"task\"; readonly task: AnyTask }\n\t| { readonly kind: \"setup\"; readonly key: string; readonly setup: ConversationSetup }\n\t| { readonly kind: \"section\"; readonly section: PromptSection<Tool> }\n\t| { readonly kind: \"sectionWrap\"; readonly key: string; readonly wrapper: PromptSectionWrapper<Tool> };\n\n/** Hook handlers erased across task definitions; lookup by task token restores their type. */\ntype StoredHook = {\n\treadonly handlers: unknown;\n\treadonly scope?: HookScope;\n};\n\n/** One registration and its lifecycle. */\ntype RegistryRecord<Tool extends ToolRegistration> = {\n\treadonly slot: Slot<Tool>;\n\t/** Uniqueness and position key; absent for keyless hooks, which always append. */\n\treadonly key: string | undefined;\n\t/** Human-readable identity for duplicate errors. */\n\treadonly label: string;\n\tposition: number;\n\tstatus: \"staged\" | \"published\" | \"disposed\" | \"rejected\";\n};\n\ntype Batch<Tool extends ToolRegistration> = {\n\t/** Records to publish; a record disposed while staged is removed. */\n\treadonly added: RegistryRecord<Tool>[];\n\treadonly disposed: Set<RegistryRecord<Tool>>;\n};\n\ntype Composition<Tool extends ToolRegistration> = {\n\treadonly tools: readonly Tool[];\n\treadonly toolsByName: ReadonlyMap<string, Tool>;\n\treadonly sections: readonly PromptSection<Tool>[];\n\treadonly failures: readonly RegistryFailure[];\n};\n\n/** Immutable published registry state; wrappers are composed lazily once per state. */\nclass RegistryState<Tool extends ToolRegistration> implements RegistrySnapshot<Tool> {\n\treadonly records: readonly RegistryRecord<Tool>[];\n\t#composition: Composition<Tool> | undefined;\n\n\tconstructor(records: readonly RegistryRecord<Tool>[]) {\n\t\tthis.records = records;\n\t}\n\n\ttools(): readonly Tool[] {\n\t\treturn this.#composed().tools;\n\t}\n\n\ttool(name: string): Tool | undefined {\n\t\treturn this.#composed().toolsByName.get(name);\n\t}\n\n\ttoolNames(): readonly string[] {\n\t\tconst names: string[] = [];\n\t\tfor (const record of this.records) if (record.slot.kind === \"tool\") names.push(record.slot.tool.name);\n\t\treturn names;\n\t}\n\n\ttask(name: string): AnyTask | undefined {\n\t\tfor (const record of this.records) {\n\t\t\tif (record.slot.kind === \"task\" && record.slot.task.definition.name === name) return record.slot.task;\n\t\t}\n\t\treturn undefined;\n\t}\n\n\tconversationSetups(): readonly { readonly key: string; readonly setup: ConversationSetup }[] {\n\t\tconst setups: { key: string; setup: ConversationSetup }[] = [];\n\t\tfor (const record of this.records) if (record.slot.kind === \"setup\") setups.push(record.slot);\n\t\treturn setups;\n\t}\n\n\ttasks(): AnyTask[] {\n\t\tconst tasks: AnyTask[] = [];\n\t\tfor (const record of this.records) if (record.slot.kind === \"task\") tasks.push(record.slot.task);\n\t\treturn tasks;\n\t}\n\n\thooks<K extends AnyTask>(task: K): readonly HookRegistration<K>[] {\n\t\t// Registration was typed by a token with this name; matching by name lets hooks survive a task reload.\n\t\tconst hooks: StoredHook[] = [];\n\t\tfor (const record of this.records) {\n\t\t\tif (record.slot.kind === \"hook\" && record.slot.taskName === task.definition.name) hooks.push(record.slot.hook);\n\t\t}\n\t\treturn hooks as readonly HookRegistration<K>[];\n\t}\n\n\tsections(): readonly PromptSection<Tool>[] {\n\t\treturn this.#composed().sections;\n\t}\n\n\tfailures(): readonly RegistryFailure[] {\n\t\treturn this.#composed().failures;\n\t}\n\n\t#composed(): Composition<Tool> {\n\t\tif (this.#composition !== undefined) return this.#composition;\n\t\tconst failures: RegistryFailure[] = [];\n\t\tconst toolWraps = new Map<string, ToolWrapper<Tool>[]>();\n\t\tconst sectionWraps = new Map<string, PromptSectionWrapper<Tool>[]>();\n\t\tfor (const record of this.records) {\n\t\t\tconst slot = record.slot;\n\t\t\tif (slot.kind === \"toolWrap\") appendTo(toolWraps, slot.name, slot.wrapper);\n\t\t\tif (slot.kind === \"sectionWrap\") appendTo(sectionWraps, slot.key, slot.wrapper);\n\t\t}\n\t\tconst tools: Tool[] = [];\n\t\tconst toolsByName = new Map<string, Tool>();\n\t\tconst sections: PromptSection<Tool>[] = [];\n\t\tfor (const record of this.records) {\n\t\t\tconst slot = record.slot;\n\t\t\tif (slot.kind === \"tool\") {\n\t\t\t\tconst name = slot.tool.name;\n\t\t\t\ttry {\n\t\t\t\t\tlet tool = slot.tool;\n\t\t\t\t\tfor (const wrap of toolWraps.get(name) ?? []) {\n\t\t\t\t\t\ttool = wrap(tool);\n\t\t\t\t\t\tif (tool.name !== name) throw new Error(`Tool wrapper renamed ${name} to ${tool.name}`);\n\t\t\t\t\t}\n\t\t\t\t\ttools.push(tool);\n\t\t\t\t\ttoolsByName.set(name, tool);\n\t\t\t\t} catch (error) {\n\t\t\t\t\tfailures.push({ kind: \"tool\", name, error });\n\t\t\t\t}\n\t\t\t} else if (slot.kind === \"section\") {\n\t\t\t\tconst key = slot.section.key;\n\t\t\t\ttry {\n\t\t\t\t\tlet section = slot.section;\n\t\t\t\t\tfor (const wrap of sectionWraps.get(key) ?? []) {\n\t\t\t\t\t\tsection = wrap(section);\n\t\t\t\t\t\tif (section.key !== key) throw new Error(`Section wrapper renamed ${key} to ${section.key}`);\n\t\t\t\t\t}\n\t\t\t\t\tsections.push(section);\n\t\t\t\t} catch (error) {\n\t\t\t\t\tfailures.push({ kind: \"section\", name: key, error });\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tthis.#composition = { tools, toolsByName, sections, failures };\n\t\treturn this.#composition;\n\t}\n}\n\nclass RegistryImpl<Tool extends ToolRegistration> implements Registry<Tool> {\n\t#current = new RegistryState<Tool>([]);\n\t#batch: Batch<Tool> | undefined;\n\t/** First position of every key ever published; re-registered keys keep it. */\n\treadonly #positions = new Map<string, number>();\n\treadonly #listeners = new Set<() => void>();\n\t#nextPosition = 0;\n\n\treadonly tools: Registry<Tool>[\"tools\"];\n\treadonly hooks: Registry<Tool>[\"hooks\"];\n\treadonly tasks: Registry<Tool>[\"tasks\"];\n\treadonly conversations: Registry<Tool>[\"conversations\"];\n\treadonly systemPrompt: Registry<Tool>[\"systemPrompt\"];\n\n\tconstructor() {\n\t\tthis.tools = {\n\t\t\tadd: (tool) => this.#register({ kind: \"tool\", tool }, `tool\\0${tool.name}`, `Tool ${tool.name}`),\n\t\t\twrap: (name, key, wrapper) =>\n\t\t\t\tthis.#register(\n\t\t\t\t\t{ kind: \"toolWrap\", name, wrapper },\n\t\t\t\t\t`toolWrap\\0${name}\\0${key}`,\n\t\t\t\t\t`Tool wrapper ${key} for ${name}`,\n\t\t\t\t),\n\t\t\tlist: () => this.#current.tools(),\n\t\t};\n\t\tthis.hooks = {\n\t\t\tadd: <K extends AnyTask>(\n\t\t\t\ttask: K,\n\t\t\t\thandlers: Partial<HooksOf<K>>,\n\t\t\t\toptions?: { readonly scope?: HookScope; readonly key?: string },\n\t\t\t) => {\n\t\t\t\tconst taskName = task.definition.name;\n\t\t\t\tconst hook: StoredHook = options?.scope === undefined ? { handlers } : { handlers, scope: options.scope };\n\t\t\t\tconst key = options?.key;\n\t\t\t\treturn this.#register(\n\t\t\t\t\t{ kind: \"hook\", taskName, hook },\n\t\t\t\t\tkey === undefined ? undefined : `hook\\0${taskName}\\0${key}`,\n\t\t\t\t\t`Hook ${key} for ${taskName}`,\n\t\t\t\t);\n\t\t\t},\n\t\t};\n\t\tthis.tasks = {\n\t\t\tadd: (task) => {\n\t\t\t\tconst name = task.definition.name;\n\t\t\t\treturn this.#register({ kind: \"task\", task }, `task\\0${name}`, `Task ${name}`);\n\t\t\t},\n\t\t\tlist: () => this.#current.tasks(),\n\t\t};\n\t\tthis.conversations = {\n\t\t\tsetup: (key, setup) => this.#register({ kind: \"setup\", key, setup }, `setup\\0${key}`, `Setup ${key}`),\n\t\t};\n\t\tthis.systemPrompt = {\n\t\t\tsection: (key, render, options) => {\n\t\t\t\trequireSectionKey(key);\n\t\t\t\tconst section: PromptSection<Tool> =\n\t\t\t\t\toptions?.tag === undefined ? { key, render } : { key, render, tag: options.tag };\n\t\t\t\treturn this.#register({ kind: \"section\", section }, `section\\0${key}`, `Section ${key}`);\n\t\t\t},\n\t\t\twrap: (key, wrapperKey, wrapper) => {\n\t\t\t\trequireSectionKey(key);\n\t\t\t\treturn this.#register(\n\t\t\t\t\t{ kind: \"sectionWrap\", key, wrapper },\n\t\t\t\t\t`sectionWrap\\0${key}\\0${wrapperKey}`,\n\t\t\t\t\t`Section wrapper ${wrapperKey} for ${key}`,\n\t\t\t\t);\n\t\t\t},\n\t\t\tsections: () => this.#current.sections(),\n\t\t};\n\t}\n\n\tsnapshot(): RegistrySnapshot<Tool> {\n\t\treturn this.#current;\n\t}\n\n\tsubscribe(listener: () => void): () => void {\n\t\tthis.#listeners.add(listener);\n\t\treturn () => this.#listeners.delete(listener);\n\t}\n\n\tbatch(register: () => void): Registration {\n\t\tif (this.#batch !== undefined) throw new Error(\"Registry batches cannot be nested\");\n\t\tconst batch: Batch<Tool> = { added: [], disposed: new Set() };\n\t\tthis.#batch = batch;\n\t\tlet added: RegistryRecord<Tool>[];\n\t\ttry {\n\t\t\trejectThenable(register());\n\t\t\tadded = [...batch.added];\n\t\t\tthis.#batch = undefined;\n\t\t\tthis.#publish(batch);\n\t\t} catch (error) {\n\t\t\tthis.#batch = undefined;\n\t\t\tfor (const record of batch.added) record.status = \"rejected\";\n\t\t\tthrow error;\n\t\t}\n\t\treturn {\n\t\t\tdispose: () => {\n\t\t\t\tif (this.#batch !== undefined) {\n\t\t\t\t\tfor (const record of added) this.#dispose(record);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tthis.batch(() => {\n\t\t\t\t\tfor (const record of added) this.#dispose(record);\n\t\t\t\t});\n\t\t\t},\n\t\t};\n\t}\n\n\t#register(slot: Slot<Tool>, key: string | undefined, label: string): Registration {\n\t\tconst record: RegistryRecord<Tool> = { slot, key, label, position: -1, status: \"staged\" };\n\t\tconst batch = this.#batch;\n\t\tif (batch !== undefined) {\n\t\t\tbatch.added.push(record);\n\t\t} else {\n\t\t\ttry {\n\t\t\t\tthis.#publish({ added: [record], disposed: new Set() });\n\t\t\t} catch (error) {\n\t\t\t\trecord.status = \"rejected\";\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t}\n\t\treturn { dispose: () => this.#dispose(record) };\n\t}\n\n\t#dispose(record: RegistryRecord<Tool>): void {\n\t\tconst batch = this.#batch;\n\t\tif (record.status === \"staged\") {\n\t\t\tconst index = batch?.added.indexOf(record) ?? -1;\n\t\t\tif (index < 0) return;\n\t\t\tbatch!.added.splice(index, 1);\n\t\t\trecord.status = \"disposed\";\n\t\t} else if (record.status === \"published\") {\n\t\t\tif (batch !== undefined) batch.disposed.add(record);\n\t\t\telse this.#publish({ added: [], disposed: new Set([record]) });\n\t\t}\n\t}\n\n\t/** Validate the final staged state, then publish it synchronously. */\n\t#publish(batch: Batch<Tool>): void {\n\t\tif (batch.added.length === 0 && batch.disposed.size === 0) return;\n\t\tconst records = [...this.#current.records.filter((record) => !batch.disposed.has(record)), ...batch.added];\n\t\tconst keys = new Set<string>();\n\t\tfor (const record of records) {\n\t\t\tif (record.key === undefined) continue;\n\t\t\tif (keys.has(record.key)) throw new Error(`${record.label} is already registered`);\n\t\t\tkeys.add(record.key);\n\t\t}\n\t\tfor (const record of batch.added) {\n\t\t\tif (record.key === undefined) {\n\t\t\t\trecord.position = this.#nextPosition++;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tlet position = this.#positions.get(record.key);\n\t\t\tif (position === undefined) {\n\t\t\t\tposition = this.#nextPosition++;\n\t\t\t\tthis.#positions.set(record.key, position);\n\t\t\t}\n\t\t\trecord.position = position;\n\t\t}\n\t\trecords.sort((left, right) => left.position - right.position);\n\t\tthis.#current = new RegistryState(records);\n\t\tfor (const record of batch.added) record.status = \"published\";\n\t\tfor (const record of batch.disposed) record.status = \"disposed\";\n\t\tfor (const listener of [...this.#listeners]) listener();\n\t}\n}\n\n/** Create an application-owned registry holding only the built-ins. */\nexport function createRegistry<Tool extends ToolRegistration = ToolRegistration>(): Registry<Tool> {\n\tconst registry = new RegistryImpl<Tool>();\n\t// Their registrations are dropped, so nothing can dispose them.\n\tfor (const task of BUILTIN_TASKS) registry.tasks.add(task);\n\tregistry.conversations.setup(BUILTIN_SETUP_KEY, builtinSetup);\n\treturn registry;\n}\n\nfunction appendTo<T>(map: Map<string, T[]>, key: string, value: T): void {\n\tconst list = map.get(key);\n\tif (list === undefined) map.set(key, [value]);\n\telse list.push(value);\n}\n\nfunction requireSectionKey(key: string): void {\n\tif (!SECTION_KEY.test(key)) throw new TypeError(`Section key ${JSON.stringify(key)} must match ${SECTION_KEY}`);\n}\n\nfunction rejectThenable(result: unknown): void {\n\tif (\n\t\t(typeof result === \"object\" || typeof result === \"function\") &&\n\t\tresult !== null &&\n\t\ttypeof (result as { then?: unknown }).then === \"function\"\n\t) {\n\t\t(result as PromiseLike<unknown>).then(undefined, () => undefined);\n\t\tthrow new TypeError(\"Registry batch callbacks must be synchronous\");\n\t}\n}\n"]}
1
+ {"version":3,"file":"registry.js","sourceRoot":"","sources":["../../src/harness/registry.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAgBrC,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEtC,MAAM,WAAW,GAAG,oBAAoB,CAAC;AAEzC,iGAAiG;AACjG,MAAM,CAAC,MAAM,aAAa,GAAuB,CAAC,cAAc,EAAE,QAAQ,EAAE,cAAc,CAAC,CAAC;AAE5F,MAAM,CAAC,MAAM,iBAAiB,GAAG,IAAI,CAAC;AAEtC;;;GAG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GAAG,EAAE,CAAC,SAAS,CAAC,CAAC;QAClE,MAAM,IAAI,SAAS,CAAC,8CAA8C,CAAC,CAAC;IACrE,CAAC;AACF,CAAC","sourcesContent":["import type { ConversationRecord, Tx } from \"../types.ts\";\nimport { CompactionTask } from \"./compaction.ts\";\nimport { ConversationConfig } from \"./config.ts\";\nimport { GenerationTask } from \"./generation.ts\";\nimport { InboxDoc } from \"./inbox.ts\";\nimport { LiveDoc } from \"./live.ts\";\nimport { ToolTask } from \"./tool.ts\";\nimport type {\n\tAnyTask,\n\tConversationSetup,\n\tHookRegistration,\n\tHookScope,\n\tHooksOf,\n\tPromptSection,\n\tPromptSectionWrapper,\n\tRegistration,\n\tRegistry,\n\tRegistryFailure,\n\tRegistrySnapshot,\n\tToolRegistration,\n\tToolWrapper,\n} from \"./types.ts\";\nimport { UsageDoc } from \"./usage.ts\";\n\nconst SECTION_KEY = /^[a-z][a-z0-9_-]*$/;\n\n/** Built-in task definitions every registry starts with; they cannot be disposed or replaced. */\nexport const BUILTIN_TASKS: readonly AnyTask[] = [GenerationTask, ToolTask, CompactionTask];\n\nexport const BUILTIN_SETUP_KEY = \"pi\";\n\n/**\n * Built-in documents: empty `pi.live`, `pi.inbox`, and `pi.usage`, and for a new conversation the default\n * configuration with every registered tool active.\n */\nasync function builtinSetup(tx: Tx, conversation: ConversationRecord, registry: RegistrySnapshot): Promise<void> {\n\tawait tx.doc(LiveDoc, conversation.id);\n\tawait tx.doc(InboxDoc, conversation.id);\n\tawait tx.doc(UsageDoc, conversation.id);\n\tif (conversation.parent !== undefined) return;\n\t(await tx.doc(ConversationConfig, conversation.id)).activeTools = [...registry.toolNames()];\n}\n\ntype Slot<Tool extends ToolRegistration> =\n\t| { readonly kind: \"tool\"; readonly tool: Tool }\n\t| { readonly kind: \"toolWrap\"; readonly name: string; readonly wrapper: ToolWrapper<Tool> }\n\t| { readonly kind: \"hook\"; readonly taskName: string; readonly hook: StoredHook }\n\t| { readonly kind: \"task\"; readonly task: AnyTask }\n\t| { readonly kind: \"setup\"; readonly key: string; readonly setup: ConversationSetup }\n\t| { readonly kind: \"section\"; readonly section: PromptSection<Tool> }\n\t| { readonly kind: \"sectionWrap\"; readonly key: string; readonly wrapper: PromptSectionWrapper<Tool> };\n\n/** Hook handlers erased across task definitions; lookup by task token restores their type. */\ntype StoredHook = {\n\treadonly handlers: unknown;\n\treadonly scope?: HookScope;\n};\n\n/** One registration and its lifecycle. */\ntype RegistryRecord<Tool extends ToolRegistration> = {\n\treadonly slot: Slot<Tool>;\n\t/** Uniqueness and position key; absent for keyless hooks, which always append. */\n\treadonly key: string | undefined;\n\t/** Human-readable identity for duplicate errors. */\n\treadonly label: string;\n\tposition: number;\n\tstatus: \"staged\" | \"published\" | \"disposed\" | \"rejected\";\n};\n\ntype Batch<Tool extends ToolRegistration> = {\n\t/** Records to publish; a record disposed while staged is removed. */\n\treadonly added: RegistryRecord<Tool>[];\n\treadonly disposed: Set<RegistryRecord<Tool>>;\n};\n\ntype Composition<Tool extends ToolRegistration> = {\n\treadonly tools: readonly Tool[];\n\treadonly toolsByName: ReadonlyMap<string, Tool>;\n\treadonly sections: readonly PromptSection<Tool>[];\n\treadonly failures: readonly RegistryFailure[];\n};\n\n/** Immutable published registry state; wrappers are composed lazily once per state. */\nclass RegistryState<Tool extends ToolRegistration> implements RegistrySnapshot<Tool> {\n\treadonly records: readonly RegistryRecord<Tool>[];\n\t#composition: Composition<Tool> | undefined;\n\n\tconstructor(records: readonly RegistryRecord<Tool>[]) {\n\t\tthis.records = records;\n\t}\n\n\ttools(): readonly Tool[] {\n\t\treturn this.#composed().tools;\n\t}\n\n\ttool(name: string): Tool | undefined {\n\t\treturn this.#composed().toolsByName.get(name);\n\t}\n\n\ttoolNames(): readonly string[] {\n\t\tconst names: string[] = [];\n\t\tfor (const record of this.records) if (record.slot.kind === \"tool\") names.push(record.slot.tool.name);\n\t\treturn names;\n\t}\n\n\ttask(name: string): AnyTask | undefined {\n\t\tfor (const record of this.records) {\n\t\t\tif (record.slot.kind === \"task\" && record.slot.task.definition.name === name) return record.slot.task;\n\t\t}\n\t\treturn undefined;\n\t}\n\n\tconversationSetups(): readonly { readonly key: string; readonly setup: ConversationSetup }[] {\n\t\tconst setups: { key: string; setup: ConversationSetup }[] = [];\n\t\tfor (const record of this.records) if (record.slot.kind === \"setup\") setups.push(record.slot);\n\t\treturn setups;\n\t}\n\n\ttasks(): AnyTask[] {\n\t\tconst tasks: AnyTask[] = [];\n\t\tfor (const record of this.records) if (record.slot.kind === \"task\") tasks.push(record.slot.task);\n\t\treturn tasks;\n\t}\n\n\thooks<K extends AnyTask>(task: K): readonly HookRegistration<K>[] {\n\t\t// Registration was typed by a token with this name; matching by name lets hooks survive a task reload.\n\t\tconst hooks: StoredHook[] = [];\n\t\tfor (const record of this.records) {\n\t\t\tif (record.slot.kind === \"hook\" && record.slot.taskName === task.definition.name) hooks.push(record.slot.hook);\n\t\t}\n\t\treturn hooks as readonly HookRegistration<K>[];\n\t}\n\n\tsections(): readonly PromptSection<Tool>[] {\n\t\treturn this.#composed().sections;\n\t}\n\n\tfailures(): readonly RegistryFailure[] {\n\t\treturn this.#composed().failures;\n\t}\n\n\t#composed(): Composition<Tool> {\n\t\tif (this.#composition !== undefined) return this.#composition;\n\t\tconst failures: RegistryFailure[] = [];\n\t\tconst toolWraps = new Map<string, ToolWrapper<Tool>[]>();\n\t\tconst sectionWraps = new Map<string, PromptSectionWrapper<Tool>[]>();\n\t\tfor (const record of this.records) {\n\t\t\tconst slot = record.slot;\n\t\t\tif (slot.kind === \"toolWrap\") appendTo(toolWraps, slot.name, slot.wrapper);\n\t\t\tif (slot.kind === \"sectionWrap\") appendTo(sectionWraps, slot.key, slot.wrapper);\n\t\t}\n\t\tconst tools: Tool[] = [];\n\t\tconst toolsByName = new Map<string, Tool>();\n\t\tconst sections: PromptSection<Tool>[] = [];\n\t\tfor (const record of this.records) {\n\t\t\tconst slot = record.slot;\n\t\t\tif (slot.kind === \"tool\") {\n\t\t\t\tconst name = slot.tool.name;\n\t\t\t\ttry {\n\t\t\t\t\tlet tool = slot.tool;\n\t\t\t\t\tfor (const wrap of toolWraps.get(name) ?? []) {\n\t\t\t\t\t\ttool = wrap(tool);\n\t\t\t\t\t\tif (tool.name !== name) throw new Error(`Tool wrapper renamed ${name} to ${tool.name}`);\n\t\t\t\t\t}\n\t\t\t\t\ttools.push(tool);\n\t\t\t\t\ttoolsByName.set(name, tool);\n\t\t\t\t} catch (error) {\n\t\t\t\t\tfailures.push({ kind: \"tool\", name, error });\n\t\t\t\t}\n\t\t\t} else if (slot.kind === \"section\") {\n\t\t\t\tconst key = slot.section.key;\n\t\t\t\ttry {\n\t\t\t\t\tlet section = slot.section;\n\t\t\t\t\tfor (const wrap of sectionWraps.get(key) ?? []) {\n\t\t\t\t\t\tsection = wrap(section);\n\t\t\t\t\t\tif (section.key !== key) throw new Error(`Section wrapper renamed ${key} to ${section.key}`);\n\t\t\t\t\t}\n\t\t\t\t\tsections.push(section);\n\t\t\t\t} catch (error) {\n\t\t\t\t\tfailures.push({ kind: \"section\", name: key, error });\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tthis.#composition = { tools, toolsByName, sections, failures };\n\t\treturn this.#composition;\n\t}\n}\n\nclass RegistryImpl<Tool extends ToolRegistration> implements Registry<Tool> {\n\t#current = new RegistryState<Tool>([]);\n\t#batch: Batch<Tool> | undefined;\n\t/** First position of every key ever published; re-registered keys keep it. */\n\treadonly #positions = new Map<string, number>();\n\treadonly #listeners = new Set<() => void>();\n\t#nextPosition = 0;\n\n\treadonly tools: Registry<Tool>[\"tools\"];\n\treadonly hooks: Registry<Tool>[\"hooks\"];\n\treadonly tasks: Registry<Tool>[\"tasks\"];\n\treadonly conversations: Registry<Tool>[\"conversations\"];\n\treadonly systemPrompt: Registry<Tool>[\"systemPrompt\"];\n\n\tconstructor() {\n\t\tthis.tools = {\n\t\t\tadd: (tool) => this.#register({ kind: \"tool\", tool }, `tool\\0${tool.name}`, `Tool ${tool.name}`),\n\t\t\twrap: (name, key, wrapper) =>\n\t\t\t\tthis.#register(\n\t\t\t\t\t{ kind: \"toolWrap\", name, wrapper },\n\t\t\t\t\t`toolWrap\\0${name}\\0${key}`,\n\t\t\t\t\t`Tool wrapper ${key} for ${name}`,\n\t\t\t\t),\n\t\t\tlist: () => this.#current.tools(),\n\t\t};\n\t\tthis.hooks = {\n\t\t\tadd: <K extends AnyTask>(\n\t\t\t\ttask: K,\n\t\t\t\thandlers: Partial<HooksOf<K>>,\n\t\t\t\toptions?: { readonly scope?: HookScope; readonly key?: string },\n\t\t\t) => {\n\t\t\t\tconst taskName = task.definition.name;\n\t\t\t\tconst hook: StoredHook = options?.scope === undefined ? { handlers } : { handlers, scope: options.scope };\n\t\t\t\tconst key = options?.key;\n\t\t\t\treturn this.#register(\n\t\t\t\t\t{ kind: \"hook\", taskName, hook },\n\t\t\t\t\tkey === undefined ? undefined : `hook\\0${taskName}\\0${key}`,\n\t\t\t\t\t`Hook ${key} for ${taskName}`,\n\t\t\t\t);\n\t\t\t},\n\t\t};\n\t\tthis.tasks = {\n\t\t\tadd: (task) => {\n\t\t\t\tconst name = task.definition.name;\n\t\t\t\treturn this.#register({ kind: \"task\", task }, `task\\0${name}`, `Task ${name}`);\n\t\t\t},\n\t\t\tlist: () => this.#current.tasks(),\n\t\t};\n\t\tthis.conversations = {\n\t\t\tsetup: (key, setup) => this.#register({ kind: \"setup\", key, setup }, `setup\\0${key}`, `Setup ${key}`),\n\t\t};\n\t\tthis.systemPrompt = {\n\t\t\tsection: (key, render, options) => {\n\t\t\t\trequireSectionKey(key);\n\t\t\t\tconst section: PromptSection<Tool> =\n\t\t\t\t\toptions?.tag === undefined ? { key, render } : { key, render, tag: options.tag };\n\t\t\t\treturn this.#register({ kind: \"section\", section }, `section\\0${key}`, `Section ${key}`);\n\t\t\t},\n\t\t\twrap: (key, wrapperKey, wrapper) => {\n\t\t\t\trequireSectionKey(key);\n\t\t\t\treturn this.#register(\n\t\t\t\t\t{ kind: \"sectionWrap\", key, wrapper },\n\t\t\t\t\t`sectionWrap\\0${key}\\0${wrapperKey}`,\n\t\t\t\t\t`Section wrapper ${wrapperKey} for ${key}`,\n\t\t\t\t);\n\t\t\t},\n\t\t\tsections: () => this.#current.sections(),\n\t\t};\n\t}\n\n\tsnapshot(): RegistrySnapshot<Tool> {\n\t\treturn this.#current;\n\t}\n\n\tsubscribe(listener: () => void): () => void {\n\t\tthis.#listeners.add(listener);\n\t\treturn () => this.#listeners.delete(listener);\n\t}\n\n\tbatch(register: () => void): Registration {\n\t\tif (this.#batch !== undefined) throw new Error(\"Registry batches cannot be nested\");\n\t\tconst batch: Batch<Tool> = { added: [], disposed: new Set() };\n\t\tthis.#batch = batch;\n\t\tlet added: RegistryRecord<Tool>[];\n\t\ttry {\n\t\t\trejectThenable(register());\n\t\t\tadded = [...batch.added];\n\t\t\tthis.#batch = undefined;\n\t\t\tthis.#publish(batch);\n\t\t} catch (error) {\n\t\t\tthis.#batch = undefined;\n\t\t\tfor (const record of batch.added) record.status = \"rejected\";\n\t\t\tthrow error;\n\t\t}\n\t\treturn {\n\t\t\tdispose: () => {\n\t\t\t\tif (this.#batch !== undefined) {\n\t\t\t\t\tfor (const record of added) this.#dispose(record);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tthis.batch(() => {\n\t\t\t\t\tfor (const record of added) this.#dispose(record);\n\t\t\t\t});\n\t\t\t},\n\t\t};\n\t}\n\n\t#register(slot: Slot<Tool>, key: string | undefined, label: string): Registration {\n\t\tconst record: RegistryRecord<Tool> = { slot, key, label, position: -1, status: \"staged\" };\n\t\tconst batch = this.#batch;\n\t\tif (batch !== undefined) {\n\t\t\tbatch.added.push(record);\n\t\t} else {\n\t\t\ttry {\n\t\t\t\tthis.#publish({ added: [record], disposed: new Set() });\n\t\t\t} catch (error) {\n\t\t\t\trecord.status = \"rejected\";\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t}\n\t\treturn { dispose: () => this.#dispose(record) };\n\t}\n\n\t#dispose(record: RegistryRecord<Tool>): void {\n\t\tconst batch = this.#batch;\n\t\tif (record.status === \"staged\") {\n\t\t\tconst index = batch?.added.indexOf(record) ?? -1;\n\t\t\tif (index < 0) return;\n\t\t\tbatch!.added.splice(index, 1);\n\t\t\trecord.status = \"disposed\";\n\t\t} else if (record.status === \"published\") {\n\t\t\tif (batch !== undefined) batch.disposed.add(record);\n\t\t\telse this.#publish({ added: [], disposed: new Set([record]) });\n\t\t}\n\t}\n\n\t/** Validate the final staged state, then publish it synchronously. */\n\t#publish(batch: Batch<Tool>): void {\n\t\tif (batch.added.length === 0 && batch.disposed.size === 0) return;\n\t\tconst records = [...this.#current.records.filter((record) => !batch.disposed.has(record)), ...batch.added];\n\t\tconst keys = new Set<string>();\n\t\tfor (const record of records) {\n\t\t\tif (record.key === undefined) continue;\n\t\t\tif (keys.has(record.key)) throw new Error(`${record.label} is already registered`);\n\t\t\tkeys.add(record.key);\n\t\t}\n\t\tfor (const record of batch.added) {\n\t\t\tif (record.key === undefined) {\n\t\t\t\trecord.position = this.#nextPosition++;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tlet position = this.#positions.get(record.key);\n\t\t\tif (position === undefined) {\n\t\t\t\tposition = this.#nextPosition++;\n\t\t\t\tthis.#positions.set(record.key, position);\n\t\t\t}\n\t\t\trecord.position = position;\n\t\t}\n\t\trecords.sort((left, right) => left.position - right.position);\n\t\tthis.#current = new RegistryState(records);\n\t\tfor (const record of batch.added) record.status = \"published\";\n\t\tfor (const record of batch.disposed) record.status = \"disposed\";\n\t\tfor (const listener of [...this.#listeners]) listener();\n\t}\n}\n\n/** Create an application-owned registry holding only the built-ins. */\nexport function createRegistry<Tool extends ToolRegistration = ToolRegistration>(): Registry<Tool> {\n\tconst registry = new RegistryImpl<Tool>();\n\t// Their registrations are dropped, so nothing can dispose them.\n\tfor (const task of BUILTIN_TASKS) registry.tasks.add(task);\n\tregistry.conversations.setup(BUILTIN_SETUP_KEY, builtinSetup);\n\treturn registry;\n}\n\nfunction appendTo<T>(map: Map<string, T[]>, key: string, value: T): void {\n\tconst list = map.get(key);\n\tif (list === undefined) map.set(key, [value]);\n\telse list.push(value);\n}\n\nfunction requireSectionKey(key: string): void {\n\tif (!SECTION_KEY.test(key)) throw new TypeError(`Section key ${JSON.stringify(key)} must match ${SECTION_KEY}`);\n}\n\nfunction rejectThenable(result: unknown): void {\n\tif (\n\t\t(typeof result === \"object\" || typeof result === \"function\") &&\n\t\tresult !== null &&\n\t\ttypeof (result as { then?: unknown }).then === \"function\"\n\t) {\n\t\t(result as PromiseLike<unknown>).then(undefined, () => undefined);\n\t\tthrow new TypeError(\"Registry batch callbacks must be synchronous\");\n\t}\n}\n"]}
@@ -1,36 +1,51 @@
1
1
  import { type Context, type JsonValue } from "@earendil-works/chord";
2
2
  import type { Models } from "@earendil-works/pi-ai";
3
+ import type { ExecutionEnv } from "../env/index.ts";
3
4
  import type { SessionImpl } from "../session/session.ts";
4
5
  import type { Transaction } from "../session/transaction.ts";
5
6
  import type { ConversationId, Storage, TaskId, TaskOutcome, TaskRecord } from "../types.ts";
6
- import type { HarnessInspection, RegistryReader, RegistrySnapshot, SettledTask, TaskInspection } from "./types.ts";
7
+ import type { ConversationHandle, HarnessInspection, RegistryReader, RegistrySnapshot, SettledTask, TaskInspection } from "./types.ts";
7
8
  type AnyTaskRecord = TaskRecord<JsonValue, JsonValue, JsonValue>;
8
9
  /** Terminal outcomes the scheduler writes without running task code. */
9
10
  export type SchedulerOutcome = Extract<TaskOutcome<JsonValue>, {
10
11
  readonly status: "faulted" | "orphaned";
11
12
  }>;
13
+ /** An invocation a conversation handle is bound to: its signal, and a check that throws once it ended. */
14
+ export type InvocationBinding = {
15
+ readonly signal: AbortSignal;
16
+ check(): void;
17
+ };
12
18
  export type TaskSchedulerOptions = {
13
19
  readonly session: SessionImpl;
14
20
  readonly storage: Storage;
15
21
  readonly registry: RegistryReader;
16
22
  readonly models: Models;
23
+ readonly env: ExecutionEnv | undefined;
17
24
  readonly now: () => number;
18
25
  readonly report: (error: unknown) => void;
19
- /** Harness cleanup staged in the same commit as every terminal outcome the scheduler writes itself. */
26
+ /** Harness cleanup staged in the commit that makes an outcome the scheduler wrote itself terminal. */
20
27
  readonly settleOutcome: (tx: Transaction, record: AnyTaskRecord, outcome: SchedulerOutcome) => Promise<void>;
28
+ /** Withdraw a conversation's queued inputs, for conversation abort and abort cascades. */
29
+ readonly withdrawInputs: (tx: Transaction, conversationId: ConversationId) => Promise<void>;
30
+ /** Invocation-bound handle of an existing conversation, for task runtimes and tools. */
31
+ readonly conversation: (id: ConversationId, binding: InvocationBinding, context: Context) => Promise<ConversationHandle | undefined>;
21
32
  /** Context for scheduler commits and invocations; carries no caller cancellation. */
22
33
  readonly context: Context;
23
34
  };
24
35
  /**
25
36
  * Durable task scheduler of one Harness.
26
37
  *
27
- * `#live` mirrors every committed pending/running task record. The synchronous commit listener updates it on the
28
- * Session line, so code running on the line reads exactly the committed state from it.
38
+ * `#live` mirrors every committed non-terminal task record: pending, running, waiting, and completing. The synchronous
39
+ * commit listener updates it on the Session line, so code running on the line reads exactly the committed state from it.
40
+ *
41
+ * Tasks and conversations form one ownership tree (spec §5.5): a task's parent is its owner task, or its conversation;
42
+ * a conversation's parent is its owner task, if any. Walks up that tree decide cascades, idle scopes, and whether a
43
+ * task's ordinary owned work is live, which holds its outcome as `completing` and delays its abort handler.
29
44
  *
30
45
  * Invariant: every task transition is decided and written by one callback serialized on the Session line. That covers
31
- * reservation, marks, runtime commits, and the synchronous step before each phase, which applies the precedence rules
32
- * and writes a fault or handover. Handlers and joins run off the line. An invocation ends inside the step that decides its end,
33
- * so a runtime commit it queued either lands before that decision or is rejected.
46
+ * reservation, marks, runtime commits, finalization, and the synchronous step before each phase, which applies the
47
+ * precedence rules and writes a fault or handover. Handlers and joins run off the line. An invocation ends inside the
48
+ * step that decides its end, so a runtime commit it queued either lands before that decision or is rejected.
34
49
  */
35
50
  export declare class TaskScheduler {
36
51
  #private;
@@ -42,22 +57,32 @@ export declare class TaskScheduler {
42
57
  /** Wait for every invocation signalled by `#seal()`. Writes nothing. */
43
58
  join(): Promise<void>;
44
59
  /**
45
- * Commit the abort mark, or settle a task that no registered definition can take as `orphaned`, then signal and
46
- * join the run invocation seen on the line. The next drain starts the abort invocation.
60
+ * Commit the abort mark, or settle a task that no registered definition can take as `orphaned` when nothing it owns
61
+ * is live, then join the run invocation seen on the line; the commit listener signalled it. The abort invocation
62
+ * starts once the task's ordinary owned work is gone. A `completing` task is only marked.
47
63
  */
48
64
  abort(id: TaskId, context: Context): Promise<"marked" | "terminal">;
49
65
  waitForTask(id: TaskId, context: Context): Promise<SettledTask<JsonValue>>;
50
- /** Resolve when no live non-background task exists, optionally within one conversation. */
66
+ /**
67
+ * Resolve when ordinary traversal from the conversation, or from every ownerless conversation, reaches no live
68
+ * non-background task.
69
+ */
51
70
  waitForIdle(conversationId: ConversationId | undefined, context: Context): Promise<void>;
71
+ /**
72
+ * `Conversation.abort()`: in one commit, withdraw the queued inputs and mark every live non-background task that
73
+ * ordinary traversal from the conversation reaches; resolves once the scope is idle. With `background`, traversal
74
+ * crosses background boundaries, and the wait also covers every task it reached.
75
+ */
76
+ abortConversation(conversationId: ConversationId, background: boolean, context: Context): Promise<void>;
52
77
  /**
53
78
  * Scheduling state and every live task with its derived state, read on the Session line. Runs no task code: a
54
79
  * pending migration shows as `ready` with `migrates`, and only a migration the scheduler already tried, or one that
55
80
  * cannot exist, shows as failed.
56
81
  */
57
- inspect(snapshot: RegistrySnapshot): {
82
+ inspect(snapshot: RegistrySnapshot): Promise<{
58
83
  scheduling: HarnessInspection["scheduling"];
59
84
  tasks: TaskInspection[];
60
- };
85
+ }>;
61
86
  }
62
87
  export {};
63
88
  //# sourceMappingURL=scheduler.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"scheduler.d.ts","sourceRoot":"","sources":["../../src/harness/scheduler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,OAAO,EAA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