@earendil-works/pi-durable 0.99.0 → 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.
- package/README.md +435 -46
- package/dist/entries.d.ts +20 -2
- package/dist/entries.d.ts.map +1 -1
- package/dist/entries.js +14 -1
- package/dist/entries.js.map +1 -1
- package/dist/env/index.d.ts +13 -38
- package/dist/env/index.d.ts.map +1 -1
- package/dist/env/index.js +2 -0
- package/dist/env/index.js.map +1 -1
- package/dist/env/node.d.ts.map +1 -1
- package/dist/env/node.js +51 -58
- package/dist/env/node.js.map +1 -1
- package/dist/harness/compaction.d.ts +56 -0
- package/dist/harness/compaction.d.ts.map +1 -0
- package/dist/harness/compaction.js +385 -0
- package/dist/harness/compaction.js.map +1 -0
- package/dist/harness/config.d.ts +10 -1
- package/dist/harness/config.d.ts.map +1 -1
- package/dist/harness/config.js +6 -0
- package/dist/harness/config.js.map +1 -1
- package/dist/harness/context.d.ts +8 -0
- package/dist/harness/context.d.ts.map +1 -1
- package/dist/harness/context.js +32 -21
- package/dist/harness/context.js.map +1 -1
- package/dist/harness/events.d.ts +166 -0
- package/dist/harness/events.d.ts.map +1 -0
- package/dist/harness/events.js +319 -0
- package/dist/harness/events.js.map +1 -0
- package/dist/harness/generation.d.ts +37 -6
- package/dist/harness/generation.d.ts.map +1 -1
- package/dist/harness/generation.js +350 -72
- package/dist/harness/generation.js.map +1 -1
- package/dist/harness/harness.d.ts.map +1 -1
- package/dist/harness/harness.js +124 -3
- package/dist/harness/harness.js.map +1 -1
- package/dist/harness/inbox.d.ts +57 -0
- package/dist/harness/inbox.d.ts.map +1 -0
- package/dist/harness/inbox.js +99 -0
- package/dist/harness/inbox.js.map +1 -0
- package/dist/harness/json.d.ts +10 -0
- package/dist/harness/json.d.ts.map +1 -0
- package/dist/harness/json.js +33 -0
- package/dist/harness/json.js.map +1 -0
- package/dist/harness/live.d.ts +66 -7
- package/dist/harness/live.d.ts.map +1 -1
- package/dist/harness/live.js +71 -7
- package/dist/harness/live.js.map +1 -1
- package/dist/harness/output.d.ts +62 -0
- package/dist/harness/output.d.ts.map +1 -0
- package/dist/harness/output.js +261 -0
- package/dist/harness/output.js.map +1 -0
- package/dist/harness/prompt.d.ts +10 -5
- package/dist/harness/prompt.d.ts.map +1 -1
- package/dist/harness/prompt.js +66 -13
- package/dist/harness/prompt.js.map +1 -1
- package/dist/harness/registry.d.ts.map +1 -1
- package/dist/harness/registry.js +11 -2
- package/dist/harness/registry.js.map +1 -1
- package/dist/harness/scheduler.d.ts +37 -12
- package/dist/harness/scheduler.d.ts.map +1 -1
- package/dist/harness/scheduler.js +632 -72
- package/dist/harness/scheduler.js.map +1 -1
- package/dist/harness/submissions.d.ts +12 -7
- package/dist/harness/submissions.d.ts.map +1 -1
- package/dist/harness/submissions.js +72 -33
- package/dist/harness/submissions.js.map +1 -1
- package/dist/harness/tool.d.ts +36 -0
- package/dist/harness/tool.d.ts.map +1 -0
- package/dist/harness/tool.js +412 -0
- package/dist/harness/tool.js.map +1 -0
- package/dist/harness/types.d.ts +185 -11
- package/dist/harness/types.d.ts.map +1 -1
- package/dist/harness/types.js.map +1 -1
- package/dist/harness/usage.d.ts +18 -0
- package/dist/harness/usage.d.ts.map +1 -0
- package/dist/harness/usage.js +57 -0
- package/dist/harness/usage.js.map +1 -0
- package/dist/harness/view.d.ts +43 -0
- package/dist/harness/view.d.ts.map +1 -0
- package/dist/harness/view.js +194 -0
- package/dist/harness/view.js.map +1 -0
- package/dist/index.d.ts +10 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -1
- package/dist/index.js.map +1 -1
- package/dist/session/observation.d.ts +18 -11
- package/dist/session/observation.d.ts.map +1 -1
- package/dist/session/observation.js +24 -13
- package/dist/session/observation.js.map +1 -1
- package/dist/session/session.d.ts +10 -1
- package/dist/session/session.d.ts.map +1 -1
- package/dist/session/session.js +18 -4
- package/dist/session/session.js.map +1 -1
- package/dist/session/transaction.d.ts +10 -1
- package/dist/session/transaction.d.ts.map +1 -1
- package/dist/session/transaction.js +83 -27
- package/dist/session/transaction.js.map +1 -1
- package/dist/storage/jsonl/storage.d.ts +22 -2
- package/dist/storage/jsonl/storage.d.ts.map +1 -1
- package/dist/storage/jsonl/storage.js +1 -1
- package/dist/storage/jsonl/storage.js.map +1 -1
- package/dist/storage/memory.js +1 -1
- package/dist/storage/memory.js.map +1 -1
- package/dist/storage/sqlite/migrations.js +1 -1
- package/dist/storage/sqlite/migrations.js.map +1 -1
- package/dist/testing/storage-benchmark.d.ts.map +1 -1
- package/dist/testing/storage-benchmark.js +0 -1
- package/dist/testing/storage-benchmark.js.map +1 -1
- package/dist/testing/storage-conformance.d.ts.map +1 -1
- package/dist/testing/storage-conformance.js +34 -2
- package/dist/testing/storage-conformance.js.map +1 -1
- package/dist/tools/bash.d.ts +28 -0
- package/dist/tools/bash.d.ts.map +1 -0
- package/dist/tools/bash.js +69 -0
- package/dist/tools/bash.js.map +1 -0
- package/dist/tools/edit-diff.d.ts +89 -0
- package/dist/tools/edit-diff.d.ts.map +1 -0
- package/dist/tools/edit-diff.js +386 -0
- package/dist/tools/edit-diff.js.map +1 -0
- package/dist/tools/edit.d.ts +18 -0
- package/dist/tools/edit.d.ts.map +1 -0
- package/dist/tools/edit.js +116 -0
- package/dist/tools/edit.js.map +1 -0
- package/dist/tools/env.d.ts +5 -0
- package/dist/tools/env.d.ts.map +1 -0
- package/dist/tools/env.js +7 -0
- package/dist/tools/env.js.map +1 -0
- package/dist/tools/file-mutation-queue.d.ts +5 -0
- package/dist/tools/file-mutation-queue.d.ts.map +1 -0
- package/dist/tools/file-mutation-queue.js +46 -0
- package/dist/tools/file-mutation-queue.js.map +1 -0
- package/dist/tools/image.d.ts +2 -0
- package/dist/tools/image.d.ts.map +1 -0
- package/dist/tools/image.js +92 -0
- package/dist/tools/image.js.map +1 -0
- package/dist/tools/index.d.ts +5 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +5 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/path-utils.d.ts +5 -0
- package/dist/tools/path-utils.d.ts.map +1 -0
- package/dist/tools/path-utils.js +26 -0
- package/dist/tools/path-utils.js.map +1 -0
- package/dist/tools/read.d.ts +17 -0
- package/dist/tools/read.d.ts.map +1 -0
- package/dist/tools/read.js +100 -0
- package/dist/tools/read.js.map +1 -0
- package/dist/tools/write.d.ts +10 -0
- package/dist/tools/write.d.ts.map +1 -0
- package/dist/tools/write.js +30 -0
- package/dist/tools/write.js.map +1 -0
- package/dist/{env/utils/truncate.d.ts → truncate.d.ts} +1 -17
- package/dist/truncate.d.ts.map +1 -0
- package/dist/truncate.js +146 -0
- package/dist/truncate.js.map +1 -0
- package/dist/types.d.ts +86 -19
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/docs/pico-v5-handoff.md +366 -86
- package/docs/pico-v5.md +1373 -283
- package/package.json +11 -3
- package/dist/env/utils/adaptive-publisher.d.ts +0 -24
- package/dist/env/utils/adaptive-publisher.d.ts.map +0 -1
- package/dist/env/utils/adaptive-publisher.js +0 -79
- package/dist/env/utils/adaptive-publisher.js.map +0 -1
- package/dist/env/utils/output-capture.d.ts +0 -31
- package/dist/env/utils/output-capture.d.ts.map +0 -1
- package/dist/env/utils/output-capture.js +0 -238
- package/dist/env/utils/output-capture.js.map +0 -1
- package/dist/env/utils/truncate.d.ts.map +0 -1
- package/dist/env/utils/truncate.js +0 -294
- package/dist/env/utils/truncate.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"submissions.js","sourceRoot":"","sources":["../../src/harness/submissions.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAC1C,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAGhD,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEp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iBAAiB,CAAC,CAAC;QACpF,OAAO,MAAM,CAAC;IACf,CAAC;CACD;AAED,SAAS,SAAS,CAAC,MAAwB;IAC1C,OAAO,MAAM,CAAC,MAAM,KAAK,MAAM,IAAI,MAAM,CAAC,MAAM,KAAK,YAAY,CAAC;AACnE,CAAC","sourcesContent":["import type { Context } from \"@earendil-works/chord\";\nimport { UserEntry } from \"../entries.ts\";\nimport { ConversationBusy } from \"../errors.ts\";\nimport type { SessionImpl } from \"../session/session.ts\";\nimport type { CommitPublication, ConversationId, Storage, SubmissionId, SubmissionRecord } from \"../types.ts\";\nimport { GenerationTask } from \"./generation.ts\";\nimport { LiveDoc } from \"./live.ts\";\nimport type { SettledSubmissionRecord, Submission, SubmissionDraft } from \"./types.ts\";\nimport { closedError, Waiters } from \"./util.ts\";\n\ntype AbortResult = \"aborted\" | \"already_placed\" | \"settled\";\n\n/** Admission, waits, and withdrawal of the durable submissions of one Harness. */\nexport class Submissions {\n\treadonly #session: SessionImpl;\n\treadonly #storage: Storage;\n\treadonly #now: () => number;\n\t/** Enable task scheduling; submitting or waiting asks for progress. */\n\treadonly #resume: () => void;\n\treadonly #waiters = new Waiters<SubmissionId, SettledSubmissionRecord>();\n\t#closed = false;\n\n\tconstructor(session: SessionImpl, storage: Storage, now: () => number, resume: () => void) {\n\t\tthis.#session = session;\n\t\tthis.#storage = storage;\n\t\tthis.#now = now;\n\t\tthis.#resume = resume;\n\t\tsession.subscribeCommits((publication) => this.#observe(publication));\n\t\tsession.subscribeClose(() => {\n\t\t\tthis.#closed = true;\n\t\t\tthis.#waiters.rejectAll(closedError());\n\t\t});\n\t}\n\n\t/**\n\t * Admit a submission in one commit. A known request ID returns its existing submission without writing. Until the\n\t * inbox exists, a busy conversation rejects with `ConversationBusy` and writes nothing. Idle input places a user\n\t * entry and starts a generation run; an idle write appends its entry and settles `done`.\n\t */\n\tasync submit(conversationId: ConversationId, draft: SubmissionDraft, context: Context): Promise<Submission> {\n\t\tthis.#resume();\n\t\tconst id = await this.#session.commitWith(async (tx) => {\n\t\t\tif (draft.requestId !== undefined) {\n\t\t\t\tconst existing = await tx.submissionByRequest(conversationId, draft.requestId);\n\t\t\t\tif (existing !== undefined) {\n\t\t\t\t\tif (existing.type !== draft.type) {\n\t\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\t`Request ${draft.requestId} already identifies a submission of type ${existing.type}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\treturn existing.id;\n\t\t\t\t}\n\t\t\t}\n\t\t\tconst live = await tx.doc(LiveDoc, conversationId);\n\t\t\tif (live.run !== undefined) throw new ConversationBusy(conversationId);\n\t\t\tconst requestId = draft.requestId === undefined ? {} : { requestId: draft.requestId };\n\t\t\tif (draft.type === \"write\") {\n\t\t\t\tconst entry = await tx.appendEntry(conversationId, draft.entry);\n\t\t\t\tconst write = { conversationId, ...requestId, type: \"write\", status: \"done\", entry: entry.id } as const;\n\t\t\t\treturn (await tx.createSubmission(write)).id;\n\t\t\t}\n\t\t\tconst message = { role: \"user\", content: draft.content, timestamp: this.#now() } as const;\n\t\t\tconst entry = await tx.appendEntry(UserEntry, conversationId, { model: [message] });\n\t\t\tconst input = { conversationId, ...requestId, type: \"input\", status: \"placed\", entry: entry.id } as const;\n\t\t\tconst { id } = await tx.createSubmission(input);\n\t\t\tlive.run = { taskId: await tx.createTask(GenerationTask, {}, { conversationId }), inputs: [id] };\n\t\t\treturn id;\n\t\t}, context);\n\t\treturn new SubmissionHandle(id, this);\n\t}\n\n\t/** Handle for an existing submission, or `undefined`. */\n\tasync get(id: SubmissionId, context: Context): Promise<Submission | undefined> {\n\t\tconst record = await this.#session.readOnLine(() => this.#storage.submission(id, context));\n\t\treturn record === undefined ? undefined : new SubmissionHandle(record.id, this);\n\t}\n\n\tasync status(id: SubmissionId, context: Context): Promise<SubmissionRecord> {\n\t\tconst record = await this.#session.readOnLine(() => this.#storage.submission(id, context));\n\t\tif (record === undefined) throw new Error(`Submission ${id} does not exist`);\n\t\treturn record;\n\t}\n\n\tasync wait(id: SubmissionId, context: Context): Promise<SettledSubmissionRecord> {\n\t\tthis.#resume();\n\t\t// Check and register on the line so no settling publication falls between them.\n\t\tconst found = await this.#session.readOnLine(async () => {\n\t\t\tconst record = await this.#storage.submission(id, context);\n\t\t\tif (record === undefined) throw new Error(`Submission ${id} does not exist`);\n\t\t\tif (isSettled(record)) return { promise: Promise.resolve(record) };\n\t\t\t// Close rejects registered waiters synchronously and may begin during the read.\n\t\t\tif (this.#closed) throw closedError();\n\t\t\treturn { promise: this.#waiters.add(id, context) };\n\t\t});\n\t\treturn found.promise;\n\t}\n\n\t/** Withdraw a queued submission; placed inputs and settled submissions are reported, not changed. */\n\tabort(id: SubmissionId, context: Context, conversationId?: ConversationId): Promise<AbortResult | \"not_found\"> {\n\t\treturn this.#session.commitWith(async (tx) => {\n\t\t\tconst record = await tx.submission(id);\n\t\t\tif (record === undefined || (conversationId !== undefined && record.conversationId !== conversationId)) {\n\t\t\t\treturn \"not_found\";\n\t\t\t}\n\t\t\tif (record.status === \"queued\") {\n\t\t\t\ttx.settleSubmission(id, { status: \"unanswered\", reason: \"aborted\" });\n\t\t\t\treturn \"aborted\";\n\t\t\t}\n\t\t\treturn record.status === \"placed\" ? \"already_placed\" : \"settled\";\n\t\t}, context);\n\t}\n\n\t#observe(publication: CommitPublication): void {\n\t\tfor (const change of publication.changes) {\n\t\t\tif (change.type !== \"submission\" || !isSettled(change.value)) continue;\n\t\t\tthis.#waiters.resolve(change.value.id, change.value);\n\t\t}\n\t}\n}\n\nclass SubmissionHandle implements Submission {\n\treadonly id: SubmissionId;\n\treadonly #submissions: Submissions;\n\n\tconstructor(id: SubmissionId, submissions: Submissions) {\n\t\tthis.id = id;\n\t\tthis.#submissions = submissions;\n\t}\n\n\tstatus(context: Context): Promise<SubmissionRecord> {\n\t\treturn this.#submissions.status(this.id, context);\n\t}\n\n\twait(context: Context): Promise<SettledSubmissionRecord> {\n\t\treturn this.#submissions.wait(this.id, context);\n\t}\n\n\tasync abort(context: Context): Promise<AbortResult> {\n\t\tconst result = await this.#submissions.abort(this.id, context);\n\t\tif (result === \"not_found\") throw new Error(`Submission ${this.id} does not exist`);\n\t\treturn result;\n\t}\n}\n\nfunction isSettled(record: SubmissionRecord): record is SettledSubmissionRecord {\n\treturn record.status === \"done\" || record.status === \"unanswered\";\n}\n"]}
|
|
1
|
+
{"version":3,"file":"submissions.js","sourceRoot":"","sources":["../../src/harness/submissions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,QAAQ,EAA2B,MAAM,uBAAuB,CAAC;AACxF,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAC1C,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAWhD,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAE,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAChG,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAIjD,kFAAkF;AAClF,MAAM,OAAO,WAAW;IACd,QAAQ,CAAc;IACtB,QAAQ,CAAU;IAClB,IAAI,CAAe;IAC5B,uEAAuE;IAC9D,OAAO,CAAa;IACpB,QAAQ,GAAG,IAAI,OAAO,EAAyC,CAAC;IACzE,OAAO,GAAG,KAAK,CAAC;IAEhB,YAAY,OAAoB,EAAE,OAAgB,EAAE,GAAiB,EAAE,MAAkB;QACxF,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;QACxB,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,GAAG,CAAC;QAChB,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;QACtB,OAAO,CAAC,gBAAgB,CAAC,CAAC,WAAW,EAAE,EAAE,CAAC,IAAI,CAAC,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AAC,SAAS,EAAE,cAAc,EAAE,EAAE,KAAK,EAAE,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;IACpF,MAAM,KAAK,GAAG,EAAE,cAAc,EAAE,GAAG,SAAS,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,KAAK,CAAC,EAAE,EAAW,CAAC;IAC1G,MAAM,EAAE,EAAE,EAAE,GAAG,MAAM,EAAE,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAC;IAChD,MAAM,QAAQ,CAAC,EAAE,EAAE,cAAc,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;IAC/C,OAAO,EAAE,CAAC;AACX,CAAC","sourcesContent":["import { type Context, copyJson, type JsonRepresentation } from \"@earendil-works/chord\";\nimport { UserEntry } from \"../entries.ts\";\nimport { ConversationBusy } from \"../errors.ts\";\nimport type { SessionImpl } from \"../session/session.ts\";\nimport type { Transaction } from \"../session/transaction.ts\";\nimport type {\n\tCommitPublication,\n\tConversationId,\n\tJsonObject,\n\tStorage,\n\tSubmissionId,\n\tSubmissionRecord,\n} from \"../types.ts\";\nimport { startRun } from \"./generation.ts\";\nimport { applyBoundary, InboxDoc, isStale, prepareBoundary, removeInboxItem } from \"./inbox.ts\";\nimport { LiveDoc } from \"./live.ts\";\nimport type { SettledSubmissionRecord, Submission, SubmissionDraft, UserInput } from \"./types.ts\";\nimport { closedError, Waiters } from \"./util.ts\";\n\ntype AbortResult = \"aborted\" | \"already_placed\" | \"settled\";\n\n/** Admission, waits, and withdrawal of the durable submissions of one Harness. */\nexport class Submissions {\n\treadonly #session: SessionImpl;\n\treadonly #storage: Storage;\n\treadonly #now: () => number;\n\t/** Enable task scheduling; submitting or waiting asks for progress. */\n\treadonly #resume: () => void;\n\treadonly #waiters = new Waiters<SubmissionId, SettledSubmissionRecord>();\n\t#closed = false;\n\n\tconstructor(session: SessionImpl, storage: Storage, now: () => number, resume: () => void) {\n\t\tthis.#session = session;\n\t\tthis.#storage = storage;\n\t\tthis.#now = now;\n\t\tthis.#resume = resume;\n\t\tsession.subscribeCommits((publication) => this.#observe(publication));\n\t\tsession.subscribeClose(() => {\n\t\t\tthis.#closed = true;\n\t\t\tthis.#waiters.rejectAll(closedError());\n\t\t});\n\t}\n\n\t/** Admit a submission in one commit; see `admitSubmission()`. */\n\tasync submit(conversationId: ConversationId, draft: SubmissionDraft, context: Context): Promise<Submission> {\n\t\tthis.#resume();\n\t\tconst id = await this.#session.commitWith(\n\t\t\t(tx) => admitSubmission(tx, conversationId, draft, this.#now()),\n\t\t\tcontext,\n\t\t);\n\t\treturn new SubmissionHandle(id, this);\n\t}\n\n\t/** Handle for an existing submission, or `undefined`. */\n\tasync get(id: SubmissionId, context: Context): Promise<Submission | undefined> {\n\t\tconst record = await this.#session.readOnLine(() => this.#storage.submission(id, context));\n\t\treturn record === undefined ? undefined : new SubmissionHandle(record.id, this);\n\t}\n\n\tasync status(id: SubmissionId, context: Context): Promise<SubmissionRecord> {\n\t\tconst record = await this.#session.readOnLine(() => this.#storage.submission(id, context));\n\t\tif (record === undefined) throw new Error(`Submission ${id} does not exist`);\n\t\treturn record;\n\t}\n\n\tasync wait(id: SubmissionId, context: Context): Promise<SettledSubmissionRecord> {\n\t\tthis.#resume();\n\t\t// Check and register on the line so no settling publication falls between them.\n\t\tconst found = await this.#session.readOnLine(async () => {\n\t\t\tconst record = await this.#storage.submission(id, context);\n\t\t\tif (record === undefined) throw new Error(`Submission ${id} does not exist`);\n\t\t\tif (isSettled(record)) return { promise: Promise.resolve(record) };\n\t\t\t// Close rejects registered waiters synchronously and may begin during the read.\n\t\t\tif (this.#closed) throw closedError();\n\t\t\treturn { promise: this.#waiters.add(id, context) };\n\t\t});\n\t\treturn found.promise;\n\t}\n\n\t/** Withdraw a queued submission and remove its inbox item; placed inputs and settled submissions are reported. */\n\tabort(id: SubmissionId, context: Context, conversationId?: ConversationId): Promise<AbortResult | \"not_found\"> {\n\t\treturn this.#session.commitWith(async (tx) => {\n\t\t\tconst record = await tx.submission(id);\n\t\t\tif (record === undefined || (conversationId !== undefined && record.conversationId !== conversationId)) {\n\t\t\t\treturn \"not_found\";\n\t\t\t}\n\t\t\tif (record.status === \"queued\") {\n\t\t\t\ttx.settleSubmission(id, { status: \"unanswered\", reason: \"aborted\" });\n\t\t\t\tawait removeInboxItem(tx, record.conversationId, id);\n\t\t\t\treturn \"aborted\";\n\t\t\t}\n\t\t\treturn record.status === \"placed\" ? \"already_placed\" : \"settled\";\n\t\t}, context);\n\t}\n\n\t#observe(publication: CommitPublication): void {\n\t\tfor (const change of publication.changes) {\n\t\t\tif (change.type !== \"submission\" || !isSettled(change.value)) continue;\n\t\t\tthis.#waiters.resolve(change.value.id, change.value);\n\t\t}\n\t}\n}\n\nclass SubmissionHandle implements Submission {\n\treadonly id: SubmissionId;\n\treadonly #submissions: Submissions;\n\n\tconstructor(id: SubmissionId, submissions: Submissions) {\n\t\tthis.id = id;\n\t\tthis.#submissions = submissions;\n\t}\n\n\tstatus(context: Context): Promise<SubmissionRecord> {\n\t\treturn this.#submissions.status(this.id, context);\n\t}\n\n\twait(context: Context): Promise<SettledSubmissionRecord> {\n\t\treturn this.#submissions.wait(this.id, context);\n\t}\n\n\tasync abort(context: Context): Promise<AbortResult> {\n\t\tconst result = await this.#submissions.abort(this.id, context);\n\t\tif (result === \"not_found\") throw new Error(`Submission ${this.id} does not exist`);\n\t\treturn result;\n\t}\n}\n\nfunction isSettled(record: SubmissionRecord): record is SettledSubmissionRecord {\n\treturn record.status === \"done\" || record.status === \"unanswered\";\n}\n\n/**\n * Admit a submission inside a commit (spec §6); `Conversation.submit()` and conversation-owned compactions share it. A\n * known request ID returns its existing submission without writing. A busy conversation queues it in `pi.inbox`, or\n * rejects `whenBusy: \"reject\"` input with `ConversationBusy`. An idle conversation with queued items queues it behind\n * them and runs a final boundary. Otherwise idle input places a user entry and starts a run, and an idle write appends\n * its entry and settles `done`, or `stale` when its head reaches before the active range.\n */\nexport async function admitSubmission(\n\ttx: Transaction,\n\tconversationId: ConversationId,\n\tdraft: SubmissionDraft,\n\tnow: number,\n): Promise<SubmissionId> {\n\tif (draft.requestId !== undefined) {\n\t\tconst existing = await tx.submissionByRequest(conversationId, draft.requestId);\n\t\tif (existing !== undefined) {\n\t\t\tif (existing.type !== draft.type) {\n\t\t\t\tthrow new Error(`Request ${draft.requestId} already identifies a submission of type ${existing.type}`);\n\t\t\t}\n\t\t\treturn existing.id;\n\t\t}\n\t}\n\tconst live = await tx.doc(LiveDoc, conversationId);\n\tconst busy = live.run !== undefined;\n\tif (busy && draft.type === \"input\" && draft.whenBusy === \"reject\") throw new ConversationBusy(conversationId);\n\tconst requestId = draft.requestId === undefined ? {} : { requestId: draft.requestId };\n\t// A boundary reads the table, so it is prepared before the first table write; a busy one needs none.\n\tconst boundary = busy ? undefined : await prepareBoundary(tx, conversationId);\n\tif (boundary === undefined || boundary.inbox.items.length > 0) {\n\t\tconst { id } = await tx.createSubmission({\n\t\t\tconversationId,\n\t\t\t...requestId,\n\t\t\ttype: draft.type,\n\t\t\tstatus: \"queued\",\n\t\t});\n\t\t// Hosts may leave optional fields `undefined`; drafts take strict JSON.\n\t\tconst value = copyJson(draft.type === \"write\" ? draft.entry : draft.content, {\n\t\t\tomitUndefinedProperties: true,\n\t\t});\n\t\tconst items = (boundary?.inbox ?? (await tx.doc(InboxDoc, conversationId))).items;\n\t\tif (draft.type === \"write\") items.push({ id, mode: \"write\", entry: value as JsonObject });\n\t\telse {\n\t\t\tconst mode = draft.whenBusy === \"steer\" ? \"steer\" : \"followUp\";\n\t\t\titems.push({ id, mode, content: value as JsonRepresentation<UserInput> });\n\t\t}\n\t\tif (boundary === undefined) return id;\n\t\tconst { users } = await applyBoundary(tx, boundary, \"final\", now);\n\t\tif (users.length > 0) await startRun(tx, conversationId, live, users);\n\t\treturn id;\n\t}\n\tif (draft.type === \"write\") {\n\t\tif (isStale(boundary, draft.entry)) {\n\t\t\tconst stale = { status: \"unanswered\", reason: \"stale\" } as const;\n\t\t\treturn (await tx.createSubmission({ conversationId, ...requestId, type: \"write\", ...stale })).id;\n\t\t}\n\t\tconst entry = await tx.appendEntry(conversationId, draft.entry);\n\t\tconst write = { conversationId, ...requestId, type: \"write\", status: \"done\", entry: entry.id } as const;\n\t\treturn (await tx.createSubmission(write)).id;\n\t}\n\tconst message = { role: \"user\", content: draft.content, timestamp: now } as const;\n\tconst entry = await tx.appendEntry(UserEntry, conversationId, { model: [message] });\n\tconst input = { conversationId, ...requestId, type: \"input\", status: \"placed\", entry: entry.id } as const;\n\tconst { id } = await tx.createSubmission(input);\n\tawait startRun(tx, conversationId, live, [id]);\n\treturn id;\n}\n"]}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { ToolCall } from "@earendil-works/pi-ai";
|
|
2
|
+
import type { ConversationId, EntryId, JsonObject, Task, Tx, TypedEntry } from "../types.ts";
|
|
3
|
+
import type { ToolControl, ToolDiagnostic, ToolExecutionResult, ToolHooks } from "./types.ts";
|
|
4
|
+
export type ToolTaskInput = {
|
|
5
|
+
assistant: EntryId;
|
|
6
|
+
callId: string;
|
|
7
|
+
};
|
|
8
|
+
export type ToolTaskCheckpoint = {
|
|
9
|
+
phase: "call";
|
|
10
|
+
}
|
|
11
|
+
/** Durable intent: the final arguments and the replay policy recorded before execution. */
|
|
12
|
+
| {
|
|
13
|
+
phase: "execute";
|
|
14
|
+
arguments: JsonObject;
|
|
15
|
+
replay: "safe" | "unsafe";
|
|
16
|
+
};
|
|
17
|
+
export type ToolTaskResult = {
|
|
18
|
+
entryId: EntryId;
|
|
19
|
+
control?: ToolControl;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Built-in tool task: resolves the called tool from its phase snapshot, validates, runs `beforeTool`, records intent,
|
|
23
|
+
* executes, runs `afterTool`, and appends the result, all in one `call` handler so nothing separates resolution from
|
|
24
|
+
* settlement. `execute` is reached only by recovery and applies the replay rule.
|
|
25
|
+
*/
|
|
26
|
+
export declare const ToolTask: Task<ToolTaskInput, ToolTaskCheckpoint, ToolTaskResult, ToolHooks>;
|
|
27
|
+
/** An error result the Harness writes itself: no content and one `error` diagnostic with `code`. */
|
|
28
|
+
export declare function harnessError(code: string, message: string): ToolExecutionResult;
|
|
29
|
+
/**
|
|
30
|
+
* Append a `pi.tool-result` entry. The content ends with the rendered diagnostics, so the stored message is exactly
|
|
31
|
+
* what the model sees; `data` keeps the structured list. A result's usage is added to `pi.usage` in the same commit.
|
|
32
|
+
*/
|
|
33
|
+
export declare function appendToolResult(tx: Tx, conversationId: ConversationId, call: ToolCall, result: ToolExecutionResult, timestamp: number): Promise<TypedEntry<{
|
|
34
|
+
diagnostics: ToolDiagnostic[];
|
|
35
|
+
}>>;
|
|
36
|
+
//# sourceMappingURL=tool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool.d.ts","sourceRoot":"","sources":["../../src/harness/tool.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAA6B,QAAQ,EAAqB,MAAM,uBAAuB,CAAC;AAKpG,OAAO,KAAK,EACX,cAAc,EACd,OAAO,EACP,UAAU,EACV,IAAI,EAIJ,EAAE,EACF,UAAU,EACV,MAAM,aAAa,CAAC;AAIrB,OAAO,KAAK,EACX,WAAW,EACX,cAAc,EAEd,mBAAmB,EACnB,SAAS,EAET,MAAM,YAAY,CAAC;AAGpB,MAAM,MAAM,aAAa,GAAG;IAAE,SAAS,EAAE,OAAO,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAEnE,MAAM,MAAM,kBAAkB,GAC3B;IAAE,KAAK,EAAE,MAAM,CAAA;CAAE;AACnB,2FAA2F;GACzF;IAAE,KAAK,EAAE,SAAS,CAAC;IAAC,SAAS,EAAE,UAAU,CAAC;IAAC,MAAM,EAAE,MAAM,GAAG,QAAQ,CAAA;CAAE,CAAC;AAE1E,MAAM,MAAM,cAAc,GAAG;IAAE,OAAO,EAAE,OAAO,CAAC;IAAC,OAAO,CAAC,EAAE,WAAW,CAAA;CAAE,CAAC;AAKzE;;;;GAIG;AACH,eAAO,MAAM,QAAQ,oEAoEnB,CAAC;AAmSH,oGAAoG;AACpG,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,mBAAmB,CAE/E;AAmBD;;;GAGG;AACH,wBAAsB,gBAAgB,CACrC,EAAE,EAAE,EAAE,EACN,cAAc,EAAE,cAAc,EAC9B,IAAI,EAAE,QAAQ,EACd,MAAM,EAAE,mBAAmB,EAC3B,SAAS,EAAE,MAAM,GACf,OAAO,CAAC,UAAU,CAAC;IAAE,WAAW,EAAE,cAAc,EAAE,CAAA;CAAE,CAAC,CAAC,CAgBxD"}
|
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
import { copyJson } from "@earendil-works/chord";
|
|
2
|
+
import { awaitWithContext } from "@earendil-works/chord/context";
|
|
3
|
+
import { overlap } from "@earendil-works/chord/delta";
|
|
4
|
+
import { validateToolArguments } from "@earendil-works/pi-ai/utils/validation";
|
|
5
|
+
import { AssistantEntry, ToolResultEntry } from "../entries.js";
|
|
6
|
+
import { defineTask } from "../tasks.js";
|
|
7
|
+
import { DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES, utf8ByteLength } from "../truncate.js";
|
|
8
|
+
import { assignJson } from "./json.js";
|
|
9
|
+
import { clearProgress, finishSlot, LiveDoc, toolSlot } from "./live.js";
|
|
10
|
+
import { boundOutput, OutputBuffer, Progress } from "./output.js";
|
|
11
|
+
import { recordUsage } from "./usage.js";
|
|
12
|
+
/**
|
|
13
|
+
* Built-in tool task: resolves the called tool from its phase snapshot, validates, runs `beforeTool`, records intent,
|
|
14
|
+
* executes, runs `afterTool`, and appends the result, all in one `call` handler so nothing separates resolution from
|
|
15
|
+
* settlement. `execute` is reached only by recovery and applies the replay rule.
|
|
16
|
+
*/
|
|
17
|
+
export const ToolTask = defineTask({
|
|
18
|
+
name: "pi.tool",
|
|
19
|
+
version: 1,
|
|
20
|
+
initial: () => ({ phase: "call" }),
|
|
21
|
+
phases: {
|
|
22
|
+
call: async (task, runtime, context) => {
|
|
23
|
+
const call = await readCall(runtime, task.input, context);
|
|
24
|
+
const tool = runtime.registry.tool(call.name);
|
|
25
|
+
if (tool === undefined) {
|
|
26
|
+
const error = harnessError("tool_unavailable", `Tool ${call.name} is not available`);
|
|
27
|
+
return settle(runtime, call, COMPLETED, () => error, context);
|
|
28
|
+
}
|
|
29
|
+
const prepared = prepare(tool, call.arguments);
|
|
30
|
+
const checked = "error" in prepared ? prepared : validate(tool, call, prepared.args);
|
|
31
|
+
if ("error" in checked)
|
|
32
|
+
return settle(runtime, call, COMPLETED, () => invalid(checked.error), context);
|
|
33
|
+
let args = checked.args;
|
|
34
|
+
let block;
|
|
35
|
+
await runtime.hooks.each("beforeTool", async (hook) => {
|
|
36
|
+
if (block !== undefined)
|
|
37
|
+
return;
|
|
38
|
+
try {
|
|
39
|
+
const decision = await hook({ ...call, arguments: args }, runtime, context);
|
|
40
|
+
if (decision?.block !== undefined)
|
|
41
|
+
block = decision.block;
|
|
42
|
+
else if (decision?.arguments !== undefined)
|
|
43
|
+
args = decision.arguments;
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
if (runtime.signal.aborted)
|
|
47
|
+
throw error;
|
|
48
|
+
block = errorText(error);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
if (block !== undefined) {
|
|
52
|
+
const blocked = harnessError("blocked", `Tool call blocked: ${block}`);
|
|
53
|
+
return settle(runtime, call, COMPLETED, () => blocked, context);
|
|
54
|
+
}
|
|
55
|
+
const validated = validate(tool, call, args);
|
|
56
|
+
if ("error" in validated)
|
|
57
|
+
return settle(runtime, call, COMPLETED, () => invalid(validated.error), context);
|
|
58
|
+
const final = validated.args;
|
|
59
|
+
await runtime.commit(async (tx) => {
|
|
60
|
+
const slot = toolSlot(await tx.doc(LiveDoc, runtime.conversationId), runtime.taskId);
|
|
61
|
+
if (slot !== undefined)
|
|
62
|
+
slot.status = "running";
|
|
63
|
+
const intent = { phase: "execute", arguments: final, replay: tool.replay ?? "unsafe" };
|
|
64
|
+
return { status: "running", checkpoint: intent };
|
|
65
|
+
}, context);
|
|
66
|
+
await run(runtime, call, tool, final, context);
|
|
67
|
+
},
|
|
68
|
+
/** Recovery after intent: rerun only when the stored and the current policy both say `safe`. */
|
|
69
|
+
execute: async (task, runtime, context) => {
|
|
70
|
+
const { arguments: args, replay } = task.state.checkpoint;
|
|
71
|
+
const call = await readCall(runtime, task.input, context);
|
|
72
|
+
const tool = runtime.registry.tool(call.name);
|
|
73
|
+
if (replay === "safe" && tool?.replay === "safe") {
|
|
74
|
+
// The rerun reports from scratch; clear what the interrupted attempt published.
|
|
75
|
+
await runtime.commit(async (tx) => {
|
|
76
|
+
const slot = toolSlot(await tx.doc(LiveDoc, runtime.conversationId), runtime.taskId);
|
|
77
|
+
if (slot !== undefined)
|
|
78
|
+
clearProgress(slot);
|
|
79
|
+
return undefined;
|
|
80
|
+
}, context);
|
|
81
|
+
return run(runtime, call, tool, args, context);
|
|
82
|
+
}
|
|
83
|
+
const message = `Tool ${call.name} was interrupted and may have partially run`;
|
|
84
|
+
// `failed` records cancellation intent, so the call's owned conversations, left unsupervised, are aborted.
|
|
85
|
+
const ending = { status: "failed", message };
|
|
86
|
+
await settle(runtime, call, ending, (slot) => fromSlot(slot, "interrupted", message), context);
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
abort: async (task, runtime, context) => {
|
|
90
|
+
const call = await readCall(runtime, task.input, context);
|
|
91
|
+
const message = `Tool ${call.name} was aborted`;
|
|
92
|
+
await settle(runtime, call, { status: "aborted" }, (slot) => fromSlot(slot, "aborted", message), context);
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
/** The tool call `callId` of the assistant entry. */
|
|
96
|
+
async function readCall(runtime, input, context) {
|
|
97
|
+
const entry = await runtime.entry(AssistantEntry, input.assistant, context);
|
|
98
|
+
const message = entry?.model?.[0];
|
|
99
|
+
const call = message?.role === "assistant"
|
|
100
|
+
? message.content.find((content) => content.type === "toolCall" && content.id === input.callId)
|
|
101
|
+
: undefined;
|
|
102
|
+
if (call === undefined)
|
|
103
|
+
throw new Error(`Entry ${input.assistant} has no tool call ${input.callId}`);
|
|
104
|
+
return call;
|
|
105
|
+
}
|
|
106
|
+
/** The call's arguments as repaired by the tool; a throwing repair makes them invalid. */
|
|
107
|
+
function prepare(tool, args) {
|
|
108
|
+
if (tool.prepareArguments === undefined)
|
|
109
|
+
return { args };
|
|
110
|
+
try {
|
|
111
|
+
return { args: tool.prepareArguments(args) };
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
return { error: errorText(error) };
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/** Arguments validated and coerced against the implementation's schema. */
|
|
118
|
+
function validate(tool, call, args) {
|
|
119
|
+
try {
|
|
120
|
+
return { args: validateToolArguments(tool, { ...call, arguments: args }) };
|
|
121
|
+
}
|
|
122
|
+
catch (error) {
|
|
123
|
+
return { error: errorText(error) };
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
function invalid(message) {
|
|
127
|
+
return harnessError("invalid_arguments", message);
|
|
128
|
+
}
|
|
129
|
+
/** Execute with the resolved implementation, then settle its result. */
|
|
130
|
+
async function run(runtime, call, tool, args, context) {
|
|
131
|
+
const limits = {
|
|
132
|
+
maxBytes: tool.outputLimits?.maxBytes ?? DEFAULT_MAX_BYTES,
|
|
133
|
+
maxLines: tool.outputLimits?.maxLines ?? DEFAULT_MAX_LINES,
|
|
134
|
+
retain: tool.outputLimits?.retain ?? "head",
|
|
135
|
+
};
|
|
136
|
+
const reported = { output: new OutputBuffer(limits), limits, diagnostics: [], details: undefined };
|
|
137
|
+
const progress = publishProgress(runtime, reported, context);
|
|
138
|
+
let ended = false;
|
|
139
|
+
const assertLive = () => {
|
|
140
|
+
if (ended)
|
|
141
|
+
throw new Error(`Tool call ${call.id} has settled`);
|
|
142
|
+
};
|
|
143
|
+
const api = {
|
|
144
|
+
taskId: runtime.taskId,
|
|
145
|
+
conversationId: runtime.conversationId,
|
|
146
|
+
callId: call.id,
|
|
147
|
+
env: runtime.env,
|
|
148
|
+
output: (chunk) => {
|
|
149
|
+
assertLive();
|
|
150
|
+
if (reported.output.push(chunk))
|
|
151
|
+
progress.mark();
|
|
152
|
+
},
|
|
153
|
+
diagnostic: (diagnostic) => {
|
|
154
|
+
assertLive();
|
|
155
|
+
reported.diagnostics.push(copyJson(diagnostic, { omitUndefinedProperties: true }));
|
|
156
|
+
progress.mark();
|
|
157
|
+
},
|
|
158
|
+
details: async (value, detailsContext) => {
|
|
159
|
+
assertLive();
|
|
160
|
+
detailsContext.abortSignal?.throwIfAborted();
|
|
161
|
+
reported.details = copyJson(value, { omitUndefinedProperties: true });
|
|
162
|
+
const committed = progress.markAndWait();
|
|
163
|
+
// Cancelling the wait leaves the update in place; the commit's own outcome stays observed.
|
|
164
|
+
committed.catch(() => { });
|
|
165
|
+
return awaitWithContext(committed, detailsContext);
|
|
166
|
+
},
|
|
167
|
+
commit: async (change, commitContext) => {
|
|
168
|
+
let result;
|
|
169
|
+
await runtime.commit(async (tx) => {
|
|
170
|
+
result = await change(tx);
|
|
171
|
+
return undefined;
|
|
172
|
+
}, commitContext);
|
|
173
|
+
return result;
|
|
174
|
+
},
|
|
175
|
+
memo: runtime.memo,
|
|
176
|
+
createTask: async (task, input, options, taskContext) => {
|
|
177
|
+
let id;
|
|
178
|
+
await runtime.commit(async (tx) => {
|
|
179
|
+
id = await tx.createTask(task, input, options);
|
|
180
|
+
return undefined;
|
|
181
|
+
}, taskContext);
|
|
182
|
+
return id;
|
|
183
|
+
},
|
|
184
|
+
getTask: runtime.getTask,
|
|
185
|
+
waitForTask: runtime.waitForTask,
|
|
186
|
+
conversation: runtime.conversation,
|
|
187
|
+
snapshot: runtime.snapshot,
|
|
188
|
+
snapshotAsOf: runtime.snapshotAsOf,
|
|
189
|
+
watchDoc: runtime.watchDoc,
|
|
190
|
+
};
|
|
191
|
+
let result;
|
|
192
|
+
let ending = COMPLETED;
|
|
193
|
+
try {
|
|
194
|
+
result = await tool.execute(args, api, context);
|
|
195
|
+
}
|
|
196
|
+
catch (error) {
|
|
197
|
+
if (runtime.signal.aborted) {
|
|
198
|
+
ended = true;
|
|
199
|
+
for (const waiter of await progress.stop())
|
|
200
|
+
waiter.reject(error);
|
|
201
|
+
throw error;
|
|
202
|
+
}
|
|
203
|
+
result = { isError: true, diagnostics: [toolDiagnostic("tool_error", errorText(error))] };
|
|
204
|
+
// A throw ends the task `failed`, which cancels what the call owned; it no longer supervises it. The error text
|
|
205
|
+
// is already in the result entry.
|
|
206
|
+
ending = { status: "failed", message: `Tool ${call.name} threw` };
|
|
207
|
+
}
|
|
208
|
+
ended = true;
|
|
209
|
+
reported.output.end();
|
|
210
|
+
// Details still waiting for a progress commit settle with the terminal commit, the final flush.
|
|
211
|
+
const pending = await progress.stop();
|
|
212
|
+
try {
|
|
213
|
+
const settled = await finalResult(runtime, call, result, reported, context);
|
|
214
|
+
await settle(runtime, call, ending, () => settled, context);
|
|
215
|
+
}
|
|
216
|
+
catch (error) {
|
|
217
|
+
for (const waiter of pending)
|
|
218
|
+
waiter.reject(error);
|
|
219
|
+
throw error;
|
|
220
|
+
}
|
|
221
|
+
for (const waiter of pending)
|
|
222
|
+
waiter.resolve();
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Throttled commits of what the tool reported into its `pi.live.tools` slot, each writing only what changed since the
|
|
226
|
+
* last one.
|
|
227
|
+
*/
|
|
228
|
+
function publishProgress(runtime, reported, context) {
|
|
229
|
+
let written = { text: "", details: undefined, diagnostics: 0 };
|
|
230
|
+
return new Progress(async () => {
|
|
231
|
+
// Capture everything synchronously: the tool keeps reporting while the commit is in flight.
|
|
232
|
+
const snapshot = reported.output.snapshot();
|
|
233
|
+
const current = { text: snapshot.text, details: reported.details, diagnostics: reported.diagnostics.length };
|
|
234
|
+
const added = reported.diagnostics.slice(written.diagnostics, current.diagnostics);
|
|
235
|
+
const detailsChanged = current.details !== written.details;
|
|
236
|
+
// What the commit writes, as Chord diffs the string: an append, a trim plus an append of what follows the shared
|
|
237
|
+
// part, or the whole window when its bounded overlap search finds nothing.
|
|
238
|
+
let bytes = 0;
|
|
239
|
+
if (snapshot.text !== written.text) {
|
|
240
|
+
const shared = snapshot.text.startsWith(written.text)
|
|
241
|
+
? written.text.length
|
|
242
|
+
: overlap(written.text, snapshot.text, 65_536);
|
|
243
|
+
bytes += utf8ByteLength(snapshot.text.slice(shared));
|
|
244
|
+
}
|
|
245
|
+
if (detailsChanged)
|
|
246
|
+
bytes += utf8ByteLength(JSON.stringify(current.details ?? null));
|
|
247
|
+
if (added.length > 0)
|
|
248
|
+
bytes += utf8ByteLength(JSON.stringify(added));
|
|
249
|
+
await runtime.commit(async (tx) => {
|
|
250
|
+
const slot = toolSlot(await tx.doc(LiveDoc, runtime.conversationId), runtime.taskId);
|
|
251
|
+
if (slot === undefined)
|
|
252
|
+
return undefined;
|
|
253
|
+
// REMINDER: assign `output` as one string field. Chord then diffs it into an append, or a trim plus an
|
|
254
|
+
// append for a sliding tail; replacing the slot object would record the whole window on every commit.
|
|
255
|
+
if ((slot.output ?? "") !== snapshot.text)
|
|
256
|
+
slot.output = snapshot.text;
|
|
257
|
+
if (snapshot.droppedBytes > 0)
|
|
258
|
+
slot.droppedBytes = snapshot.droppedBytes;
|
|
259
|
+
if (snapshot.droppedLines > 0)
|
|
260
|
+
slot.droppedLines = snapshot.droppedLines;
|
|
261
|
+
// Diff details leaf by leaf and append new diagnostics, so each commit writes only what changed.
|
|
262
|
+
if (detailsChanged && current.details !== undefined) {
|
|
263
|
+
assignJson(slot, "details", current.details);
|
|
264
|
+
}
|
|
265
|
+
if (added.length > 0) {
|
|
266
|
+
if (slot.diagnostics === undefined)
|
|
267
|
+
slot.diagnostics = [];
|
|
268
|
+
for (const diagnostic of added)
|
|
269
|
+
slot.diagnostics.push(diagnostic);
|
|
270
|
+
}
|
|
271
|
+
return undefined;
|
|
272
|
+
}, context);
|
|
273
|
+
written = current;
|
|
274
|
+
return bytes;
|
|
275
|
+
}, (error) => {
|
|
276
|
+
// Rejections after an abort mark or close are expected; the committed state stays consistent.
|
|
277
|
+
if (!runtime.signal.aborted)
|
|
278
|
+
runtime.report(error);
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* The settled result: the tool's result with the retained output and last details as fallbacks, its diagnostics after
|
|
283
|
+
* those reported through the api, `afterTool` applied, and explicit text bounded, with the Harness's truncation
|
|
284
|
+
* diagnostic last.
|
|
285
|
+
*/
|
|
286
|
+
async function finalResult(runtime, call, result, reported, context) {
|
|
287
|
+
const harness = [];
|
|
288
|
+
const retained = result.content === undefined ? reported.output.snapshot() : undefined;
|
|
289
|
+
const content = retained === undefined ? result.content : retained.text === "" ? [] : [{ type: "text", text: retained.text }];
|
|
290
|
+
let final = {
|
|
291
|
+
...result,
|
|
292
|
+
content,
|
|
293
|
+
details: result.details === undefined ? reported.details : result.details,
|
|
294
|
+
diagnostics: [...reported.diagnostics, ...(result.diagnostics ?? [])],
|
|
295
|
+
};
|
|
296
|
+
await runtime.hooks.each("afterTool", async (hook) => {
|
|
297
|
+
final = (await hook(call, final, runtime, context)) ?? final;
|
|
298
|
+
});
|
|
299
|
+
// The retained output's truncation applies only while afterTool kept that content.
|
|
300
|
+
if (retained !== undefined && retained.droppedBytes > 0 && final.content === content) {
|
|
301
|
+
harness.push(truncated(retained, reported.limits.retain));
|
|
302
|
+
}
|
|
303
|
+
const bounded = boundContent(final.content ?? [], reported.limits);
|
|
304
|
+
if (bounded.droppedBytes > 0)
|
|
305
|
+
harness.push(truncated(bounded, reported.limits.retain));
|
|
306
|
+
return { ...final, content: bounded.content, diagnostics: [...(final.diagnostics ?? []), ...harness] };
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Commit the tool's terminal state: append its result entry, mark its slot done, and complete or end aborted with the
|
|
310
|
+
* entry ID. `build` receives the slot so interruption and abort can report the durable partial output.
|
|
311
|
+
*/
|
|
312
|
+
async function settle(runtime, call, ending, build, context) {
|
|
313
|
+
await runtime.commit(async (tx) => {
|
|
314
|
+
const slot = toolSlot(await tx.doc(LiveDoc, runtime.conversationId), runtime.taskId);
|
|
315
|
+
const result = build(slot);
|
|
316
|
+
const entry = await appendToolResult(tx, runtime.conversationId, call, result, runtime.now());
|
|
317
|
+
if (slot !== undefined)
|
|
318
|
+
finishSlot(slot, entry.id);
|
|
319
|
+
const entryId = entry.id;
|
|
320
|
+
if (ending.status === "aborted")
|
|
321
|
+
return { status: "terminal", outcome: { status: "aborted", result: { entryId } } };
|
|
322
|
+
if (ending.status === "failed") {
|
|
323
|
+
const error = { message: ending.message };
|
|
324
|
+
return { status: "terminal", outcome: { status: "failed", error, result: { entryId } } };
|
|
325
|
+
}
|
|
326
|
+
// Tools build control objects freely; drop keys set to undefined so the task result is strict JSON.
|
|
327
|
+
const control = result.control === undefined
|
|
328
|
+
? {}
|
|
329
|
+
: { control: copyJson(result.control, { omitUndefinedProperties: true }) };
|
|
330
|
+
return { status: "terminal", outcome: { status: "completed", result: { entryId, ...control } } };
|
|
331
|
+
}, context);
|
|
332
|
+
}
|
|
333
|
+
const COMPLETED = { status: "completed" };
|
|
334
|
+
/** An error result from the slot's durable partial output, details, and diagnostics. */
|
|
335
|
+
function fromSlot(slot, code, message) {
|
|
336
|
+
const diagnostics = [...(slot?.diagnostics ?? [])];
|
|
337
|
+
const droppedBytes = slot?.droppedBytes ?? 0;
|
|
338
|
+
if (droppedBytes > 0)
|
|
339
|
+
diagnostics.push(truncated({ droppedBytes, droppedLines: slot?.droppedLines ?? 0 }));
|
|
340
|
+
diagnostics.push(toolDiagnostic(code, message));
|
|
341
|
+
return {
|
|
342
|
+
content: slot?.output === undefined || slot.output === "" ? [] : [{ type: "text", text: slot.output }],
|
|
343
|
+
isError: true,
|
|
344
|
+
...(slot?.details === undefined ? {} : { details: slot.details }),
|
|
345
|
+
diagnostics,
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
/** An error result the Harness writes itself: no content and one `error` diagnostic with `code`. */
|
|
349
|
+
export function harnessError(code, message) {
|
|
350
|
+
return { content: [], isError: true, diagnostics: [toolDiagnostic(code, message)] };
|
|
351
|
+
}
|
|
352
|
+
function toolDiagnostic(code, message) {
|
|
353
|
+
return { severity: "error", code, message };
|
|
354
|
+
}
|
|
355
|
+
/** The Harness's truncation diagnostic; `retain` is unknown when rebuilt from a slot after recovery. */
|
|
356
|
+
function truncated(dropped, retain) {
|
|
357
|
+
const kept = retain === undefined ? "" : ` to its ${retain === "head" ? "beginning" : "end"}`;
|
|
358
|
+
return {
|
|
359
|
+
severity: "warn",
|
|
360
|
+
code: "truncated",
|
|
361
|
+
message: `Output truncated${kept}: ${dropped.droppedLines} lines, ${dropped.droppedBytes} bytes dropped`,
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* Append a `pi.tool-result` entry. The content ends with the rendered diagnostics, so the stored message is exactly
|
|
366
|
+
* what the model sees; `data` keeps the structured list. A result's usage is added to `pi.usage` in the same commit.
|
|
367
|
+
*/
|
|
368
|
+
export async function appendToolResult(tx, conversationId, call, result, timestamp) {
|
|
369
|
+
const diagnostics = [...(result.diagnostics ?? [])];
|
|
370
|
+
const content = [...(result.content ?? [])];
|
|
371
|
+
if (diagnostics.length > 0)
|
|
372
|
+
content.push({ type: "text", text: renderDiagnostics(diagnostics) });
|
|
373
|
+
const message = {
|
|
374
|
+
role: "toolResult",
|
|
375
|
+
toolCallId: call.id,
|
|
376
|
+
toolName: call.name,
|
|
377
|
+
content,
|
|
378
|
+
...(result.details === undefined ? {} : { details: result.details }),
|
|
379
|
+
...(result.usage === undefined ? {} : { usage: result.usage }),
|
|
380
|
+
isError: result.isError ?? false,
|
|
381
|
+
timestamp,
|
|
382
|
+
};
|
|
383
|
+
if (result.usage !== undefined)
|
|
384
|
+
await recordUsage(tx, conversationId, "tools", call.name, result.usage);
|
|
385
|
+
return tx.appendEntry(ToolResultEntry, conversationId, { model: [message], data: { diagnostics } });
|
|
386
|
+
}
|
|
387
|
+
function renderDiagnostics(diagnostics) {
|
|
388
|
+
return `<harness>\n${diagnostics.map((diagnostic) => `[${diagnostic.severity}] ${diagnostic.message}`).join("\n")}\n</harness>`;
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* Bound the text of result content. When the joined text exceeds the limits, the text items are replaced by one bounded
|
|
392
|
+
* item at the position of the first (head) or last (tail) text item; other content is kept.
|
|
393
|
+
*/
|
|
394
|
+
function boundContent(content, limits) {
|
|
395
|
+
const texts = content.filter((item) => item.type === "text");
|
|
396
|
+
const bounded = boundOutput(texts.map((item) => item.text).join(""), limits);
|
|
397
|
+
if (bounded.droppedBytes === 0)
|
|
398
|
+
return { content, droppedBytes: 0, droppedLines: 0 };
|
|
399
|
+
const keep = limits.retain === "head" ? texts[0] : texts.at(-1);
|
|
400
|
+
const result = [];
|
|
401
|
+
for (const item of content) {
|
|
402
|
+
if (item.type !== "text")
|
|
403
|
+
result.push(item);
|
|
404
|
+
else if (item === keep)
|
|
405
|
+
result.push({ ...item, text: bounded.text });
|
|
406
|
+
}
|
|
407
|
+
return { content: result, droppedBytes: bounded.droppedBytes, droppedLines: bounded.droppedLines };
|
|
408
|
+
}
|
|
409
|
+
function errorText(error) {
|
|
410
|
+
return error instanceof Error ? error.message : String(error);
|
|
411
|
+
}
|
|
412
|
+
//# sourceMappingURL=tool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool.js","sourceRoot":"","sources":["../../src/harness/tool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,QAAQ,EAAkB,MAAM,uBAAuB,CAAC;AAC/E,OAAO,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAC;AACjE,OAAO,EAAE,OAAO,EAAE,MAAM,6BAA6B,CAAC;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AAO,CAAC,YAAY,EAAE,YAAY,EAAE,OAAO,CAAC,YAAY,EAAE,CAAC;AACpG,CAAC;AAED,SAAS,SAAS,CAAC,KAAc;IAChC,OAAO,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAC/D,CAAC","sourcesContent":["import { type Context, copyJson, type JsonValue } from \"@earendil-works/chord\";\nimport { awaitWithContext } from \"@earendil-works/chord/context\";\nimport { overlap } from \"@earendil-works/chord/delta\";\nimport type { ImageContent, TextContent, ToolCall, ToolResultMessage } from \"@earendil-works/pi-ai\";\nimport { validateToolArguments } from \"@earendil-works/pi-ai/utils/validation\";\nimport { AssistantEntry, ToolResultEntry } from \"../entries.ts\";\nimport { defineTask } from \"../tasks.ts\";\nimport { DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES, utf8ByteLength } from \"../truncate.ts\";\nimport type {\n\tConversationId,\n\tEntryId,\n\tJsonObject,\n\tTask,\n\tTaskId,\n\tTaskOptions,\n\tTaskRuntime,\n\tTx,\n\tTypedEntry,\n} from \"../types.ts\";\nimport { assignJson } from \"./json.ts\";\nimport { clearProgress, finishSlot, LiveDoc, type ToolSlot, toolSlot } from \"./live.ts\";\nimport { boundOutput, OutputBuffer, type OutputLimits, Progress } from \"./output.ts\";\nimport type {\n\tToolControl,\n\tToolDiagnostic,\n\tToolExecutionApi,\n\tToolExecutionResult,\n\tToolHooks,\n\tToolRegistration,\n} from \"./types.ts\";\nimport { recordUsage } from \"./usage.ts\";\n\nexport type ToolTaskInput = { assistant: EntryId; callId: string };\n\nexport type ToolTaskCheckpoint =\n\t| { phase: \"call\" }\n\t/** Durable intent: the final arguments and the replay policy recorded before execution. */\n\t| { phase: \"execute\"; arguments: JsonObject; replay: \"safe\" | \"unsafe\" };\n\nexport type ToolTaskResult = { entryId: EntryId; control?: ToolControl };\n\ntype Runtime = TaskRuntime<ToolTaskInput, ToolTaskCheckpoint, ToolTaskResult, ToolHooks>;\ntype Content = (TextContent | ImageContent)[];\n\n/**\n * Built-in tool task: resolves the called tool from its phase snapshot, validates, runs `beforeTool`, records intent,\n * executes, runs `afterTool`, and appends the result, all in one `call` handler so nothing separates resolution from\n * settlement. `execute` is reached only by recovery and applies the replay rule.\n */\nexport const ToolTask = defineTask<ToolTaskInput, ToolTaskCheckpoint, ToolTaskResult, ToolHooks>({\n\tname: \"pi.tool\",\n\tversion: 1,\n\tinitial: () => ({ phase: \"call\" }),\n\tphases: {\n\t\tcall: async (task, runtime, context) => {\n\t\t\tconst call = await readCall(runtime, task.input, context);\n\t\t\tconst tool = runtime.registry.tool(call.name);\n\t\t\tif (tool === undefined) {\n\t\t\t\tconst error = harnessError(\"tool_unavailable\", `Tool ${call.name} is not available`);\n\t\t\t\treturn settle(runtime, call, COMPLETED, () => error, context);\n\t\t\t}\n\t\t\tconst prepared = prepare(tool, call.arguments as JsonObject);\n\t\t\tconst checked = \"error\" in prepared ? prepared : validate(tool, call, prepared.args);\n\t\t\tif (\"error\" in checked) return settle(runtime, call, COMPLETED, () => invalid(checked.error), context);\n\t\t\tlet args = checked.args;\n\t\t\tlet block: string | undefined;\n\t\t\tawait runtime.hooks.each(\"beforeTool\", async (hook) => {\n\t\t\t\tif (block !== undefined) return;\n\t\t\t\ttry {\n\t\t\t\t\tconst decision = await hook({ ...call, arguments: args }, runtime, context);\n\t\t\t\t\tif (decision?.block !== undefined) block = decision.block;\n\t\t\t\t\telse if (decision?.arguments !== undefined) args = decision.arguments;\n\t\t\t\t} catch (error) {\n\t\t\t\t\tif (runtime.signal.aborted) throw error;\n\t\t\t\t\tblock = errorText(error);\n\t\t\t\t}\n\t\t\t});\n\t\t\tif (block !== undefined) {\n\t\t\t\tconst blocked = harnessError(\"blocked\", `Tool call blocked: ${block}`);\n\t\t\t\treturn settle(runtime, call, COMPLETED, () => blocked, context);\n\t\t\t}\n\t\t\tconst validated = validate(tool, call, args);\n\t\t\tif (\"error\" in validated) return settle(runtime, call, COMPLETED, () => invalid(validated.error), context);\n\t\t\tconst final = validated.args;\n\t\t\tawait runtime.commit(async (tx) => {\n\t\t\t\tconst slot = toolSlot(await tx.doc(LiveDoc, runtime.conversationId), runtime.taskId);\n\t\t\t\tif (slot !== undefined) slot.status = \"running\";\n\t\t\t\tconst intent = { phase: \"execute\", arguments: final, replay: tool.replay ?? \"unsafe\" } as const;\n\t\t\t\treturn { status: \"running\", checkpoint: intent };\n\t\t\t}, context);\n\t\t\tawait run(runtime, call, tool, final, context);\n\t\t},\n\t\t/** Recovery after intent: rerun only when the stored and the current policy both say `safe`. */\n\t\texecute: async (task, runtime, context) => {\n\t\t\tconst { arguments: args, replay } = task.state.checkpoint;\n\t\t\tconst call = await readCall(runtime, task.input, context);\n\t\t\tconst tool = runtime.registry.tool(call.name);\n\t\t\tif (replay === \"safe\" && tool?.replay === \"safe\") {\n\t\t\t\t// The rerun reports from scratch; clear what the interrupted attempt published.\n\t\t\t\tawait runtime.commit(async (tx) => {\n\t\t\t\t\tconst slot = toolSlot(await tx.doc(LiveDoc, runtime.conversationId), runtime.taskId);\n\t\t\t\t\tif (slot !== undefined) clearProgress(slot);\n\t\t\t\t\treturn undefined;\n\t\t\t\t}, context);\n\t\t\t\treturn run(runtime, call, tool, args, context);\n\t\t\t}\n\t\t\tconst message = `Tool ${call.name} was interrupted and may have partially run`;\n\t\t\t// `failed` records cancellation intent, so the call's owned conversations, left unsupervised, are aborted.\n\t\t\tconst ending = { status: \"failed\", message } as const;\n\t\t\tawait settle(runtime, call, ending, (slot) => fromSlot(slot, \"interrupted\", message), context);\n\t\t},\n\t},\n\tabort: async (task, runtime, context) => {\n\t\tconst call = await readCall(runtime, task.input, context);\n\t\tconst message = `Tool ${call.name} was aborted`;\n\t\tawait settle(runtime, call, { status: \"aborted\" }, (slot) => fromSlot(slot, \"aborted\", message), context);\n\t},\n});\n\n/** The tool call `callId` of the assistant entry. */\nasync function readCall(runtime: Runtime, input: ToolTaskInput, context: Context): Promise<ToolCall> {\n\tconst entry = await runtime.entry(AssistantEntry, input.assistant, context);\n\tconst message = entry?.model?.[0];\n\tconst call =\n\t\tmessage?.role === \"assistant\"\n\t\t\t? message.content.find(\n\t\t\t\t\t(content): content is ToolCall => content.type === \"toolCall\" && content.id === input.callId,\n\t\t\t\t)\n\t\t\t: undefined;\n\tif (call === undefined) throw new Error(`Entry ${input.assistant} has no tool call ${input.callId}`);\n\treturn call;\n}\n\n/** Arguments, or why they are invalid. */\ntype Checked = { readonly args: JsonObject } | { readonly error: string };\n\n/** The call's arguments as repaired by the tool; a throwing repair makes them invalid. */\nfunction prepare(tool: ToolRegistration, args: JsonObject): Checked {\n\tif (tool.prepareArguments === undefined) return { args };\n\ttry {\n\t\treturn { args: tool.prepareArguments(args) as JsonObject };\n\t} catch (error) {\n\t\treturn { error: errorText(error) };\n\t}\n}\n\n/** Arguments validated and coerced against the implementation's schema. */\nfunction validate(tool: ToolRegistration, call: ToolCall, args: JsonObject): Checked {\n\ttry {\n\t\treturn { args: validateToolArguments(tool, { ...call, arguments: args }) as JsonObject };\n\t} catch (error) {\n\t\treturn { error: errorText(error) };\n\t}\n}\n\nfunction invalid(message: string): ToolExecutionResult {\n\treturn harnessError(\"invalid_arguments\", message);\n}\n\n/** What a running tool reported through its api: output, the last details, and diagnostics. */\ntype Reported = {\n\treadonly output: OutputBuffer;\n\treadonly limits: OutputLimits;\n\treadonly diagnostics: ToolDiagnostic[];\n\tdetails: JsonValue | undefined;\n};\n\n/** Execute with the resolved implementation, then settle its result. */\nasync function run(\n\truntime: Runtime,\n\tcall: ToolCall,\n\ttool: ToolRegistration,\n\targs: JsonObject,\n\tcontext: Context,\n): Promise<void> {\n\tconst limits: OutputLimits = {\n\t\tmaxBytes: tool.outputLimits?.maxBytes ?? DEFAULT_MAX_BYTES,\n\t\tmaxLines: tool.outputLimits?.maxLines ?? DEFAULT_MAX_LINES,\n\t\tretain: tool.outputLimits?.retain ?? \"head\",\n\t};\n\tconst reported: Reported = { output: new OutputBuffer(limits), limits, diagnostics: [], details: undefined };\n\tconst progress = publishProgress(runtime, reported, context);\n\tlet ended = false;\n\tconst assertLive = (): void => {\n\t\tif (ended) throw new Error(`Tool call ${call.id} has settled`);\n\t};\n\tconst api: ToolExecutionApi = {\n\t\ttaskId: runtime.taskId,\n\t\tconversationId: runtime.conversationId,\n\t\tcallId: call.id,\n\t\tenv: runtime.env,\n\t\toutput: (chunk) => {\n\t\t\tassertLive();\n\t\t\tif (reported.output.push(chunk)) progress.mark();\n\t\t},\n\t\tdiagnostic: (diagnostic) => {\n\t\t\tassertLive();\n\t\t\treported.diagnostics.push(copyJson(diagnostic, { omitUndefinedProperties: true }) as ToolDiagnostic);\n\t\t\tprogress.mark();\n\t\t},\n\t\tdetails: async (value, detailsContext) => {\n\t\t\tassertLive();\n\t\t\tdetailsContext.abortSignal?.throwIfAborted();\n\t\t\treported.details = copyJson(value, { omitUndefinedProperties: true });\n\t\t\tconst committed = progress.markAndWait();\n\t\t\t// Cancelling the wait leaves the update in place; the commit's own outcome stays observed.\n\t\t\tcommitted.catch(() => {});\n\t\t\treturn awaitWithContext(committed, detailsContext);\n\t\t},\n\t\tcommit: async (change, commitContext) => {\n\t\t\tlet result: Awaited<ReturnType<typeof change>> | undefined;\n\t\t\tawait runtime.commit(async (tx) => {\n\t\t\t\tresult = await change(tx);\n\t\t\t\treturn undefined;\n\t\t\t}, commitContext);\n\t\t\treturn result as Awaited<ReturnType<typeof change>>;\n\t\t},\n\t\tmemo: runtime.memo,\n\t\tcreateTask: async <I, S extends { phase: string }, R, H extends object>(\n\t\t\ttask: Task<I, S, R, H>,\n\t\t\tinput: I,\n\t\t\toptions: Omit<TaskOptions, \"conversationId\">,\n\t\t\ttaskContext: Context,\n\t\t): Promise<TaskId<R>> => {\n\t\t\tlet id: TaskId<R> | undefined;\n\t\t\tawait runtime.commit(async (tx) => {\n\t\t\t\tid = await tx.createTask(task, input, options);\n\t\t\t\treturn undefined;\n\t\t\t}, taskContext);\n\t\t\treturn id!;\n\t\t},\n\t\tgetTask: runtime.getTask,\n\t\twaitForTask: runtime.waitForTask,\n\t\tconversation: runtime.conversation,\n\t\tsnapshot: runtime.snapshot,\n\t\tsnapshotAsOf: runtime.snapshotAsOf,\n\t\twatchDoc: runtime.watchDoc,\n\t};\n\n\tlet result: ToolExecutionResult;\n\tlet ending = COMPLETED;\n\ttry {\n\t\tresult = await tool.execute(args, api, context);\n\t} catch (error) {\n\t\tif (runtime.signal.aborted) {\n\t\t\tended = true;\n\t\t\tfor (const waiter of await progress.stop()) waiter.reject(error);\n\t\t\tthrow error;\n\t\t}\n\t\tresult = { isError: true, diagnostics: [toolDiagnostic(\"tool_error\", errorText(error))] };\n\t\t// A throw ends the task `failed`, which cancels what the call owned; it no longer supervises it. The error text\n\t\t// is already in the result entry.\n\t\tending = { status: \"failed\", message: `Tool ${call.name} threw` };\n\t}\n\tended = true;\n\treported.output.end();\n\t// Details still waiting for a progress commit settle with the terminal commit, the final flush.\n\tconst pending = await progress.stop();\n\ttry {\n\t\tconst settled = await finalResult(runtime, call, result, reported, context);\n\t\tawait settle(runtime, call, ending, () => settled, context);\n\t} catch (error) {\n\t\tfor (const waiter of pending) waiter.reject(error);\n\t\tthrow error;\n\t}\n\tfor (const waiter of pending) waiter.resolve();\n}\n\n/**\n * Throttled commits of what the tool reported into its `pi.live.tools` slot, each writing only what changed since the\n * last one.\n */\nfunction publishProgress(runtime: Runtime, reported: Reported, context: Context): Progress {\n\tlet written = { text: \"\", details: undefined as JsonValue | undefined, diagnostics: 0 };\n\treturn new Progress(\n\t\tasync () => {\n\t\t\t// Capture everything synchronously: the tool keeps reporting while the commit is in flight.\n\t\t\tconst snapshot = reported.output.snapshot();\n\t\t\tconst current = { text: snapshot.text, details: reported.details, diagnostics: reported.diagnostics.length };\n\t\t\tconst added = reported.diagnostics.slice(written.diagnostics, current.diagnostics);\n\t\t\tconst detailsChanged = current.details !== written.details;\n\t\t\t// What the commit writes, as Chord diffs the string: an append, a trim plus an append of what follows the shared\n\t\t\t// part, or the whole window when its bounded overlap search finds nothing.\n\t\t\tlet bytes = 0;\n\t\t\tif (snapshot.text !== written.text) {\n\t\t\t\tconst shared = snapshot.text.startsWith(written.text)\n\t\t\t\t\t? written.text.length\n\t\t\t\t\t: overlap(written.text, snapshot.text, 65_536);\n\t\t\t\tbytes += utf8ByteLength(snapshot.text.slice(shared));\n\t\t\t}\n\t\t\tif (detailsChanged) bytes += utf8ByteLength(JSON.stringify(current.details ?? null));\n\t\t\tif (added.length > 0) bytes += utf8ByteLength(JSON.stringify(added));\n\t\t\tawait runtime.commit(async (tx) => {\n\t\t\t\tconst slot = toolSlot(await tx.doc(LiveDoc, runtime.conversationId), runtime.taskId);\n\t\t\t\tif (slot === undefined) return undefined;\n\t\t\t\t// REMINDER: assign `output` as one string field. Chord then diffs it into an append, or a trim plus an\n\t\t\t\t// append for a sliding tail; replacing the slot object would record the whole window on every commit.\n\t\t\t\tif ((slot.output ?? \"\") !== snapshot.text) slot.output = snapshot.text;\n\t\t\t\tif (snapshot.droppedBytes > 0) slot.droppedBytes = snapshot.droppedBytes;\n\t\t\t\tif (snapshot.droppedLines > 0) slot.droppedLines = snapshot.droppedLines;\n\t\t\t\t// Diff details leaf by leaf and append new diagnostics, so each commit writes only what changed.\n\t\t\t\tif (detailsChanged && current.details !== undefined) {\n\t\t\t\t\tassignJson(slot as unknown as Record<string, JsonValue>, \"details\", current.details);\n\t\t\t\t}\n\t\t\t\tif (added.length > 0) {\n\t\t\t\t\tif (slot.diagnostics === undefined) slot.diagnostics = [];\n\t\t\t\t\tfor (const diagnostic of added) slot.diagnostics.push(diagnostic);\n\t\t\t\t}\n\t\t\t\treturn undefined;\n\t\t\t}, context);\n\t\t\twritten = current;\n\t\t\treturn bytes;\n\t\t},\n\t\t(error) => {\n\t\t\t// Rejections after an abort mark or close are expected; the committed state stays consistent.\n\t\t\tif (!runtime.signal.aborted) runtime.report(error);\n\t\t},\n\t);\n}\n\n/**\n * The settled result: the tool's result with the retained output and last details as fallbacks, its diagnostics after\n * those reported through the api, `afterTool` applied, and explicit text bounded, with the Harness's truncation\n * diagnostic last.\n */\nasync function finalResult(\n\truntime: Runtime,\n\tcall: ToolCall,\n\tresult: ToolExecutionResult,\n\treported: Reported,\n\tcontext: Context,\n): Promise<ToolExecutionResult> {\n\tconst harness: ToolDiagnostic[] = [];\n\tconst retained = result.content === undefined ? reported.output.snapshot() : undefined;\n\tconst content: Content =\n\t\tretained === undefined ? result.content! : retained.text === \"\" ? [] : [{ type: \"text\", text: retained.text }];\n\tlet final: ToolExecutionResult = {\n\t\t...result,\n\t\tcontent,\n\t\tdetails: result.details === undefined ? reported.details : result.details,\n\t\tdiagnostics: [...reported.diagnostics, ...(result.diagnostics ?? [])],\n\t};\n\tawait runtime.hooks.each(\"afterTool\", async (hook) => {\n\t\tfinal = (await hook(call, final, runtime, context)) ?? final;\n\t});\n\t// The retained output's truncation applies only while afterTool kept that content.\n\tif (retained !== undefined && retained.droppedBytes > 0 && final.content === content) {\n\t\tharness.push(truncated(retained, reported.limits.retain));\n\t}\n\tconst bounded = boundContent(final.content ?? [], reported.limits);\n\tif (bounded.droppedBytes > 0) harness.push(truncated(bounded, reported.limits.retain));\n\treturn { ...final, content: bounded.content, diagnostics: [...(final.diagnostics ?? []), ...harness] };\n}\n\n/**\n * Commit the tool's terminal state: append its result entry, mark its slot done, and complete or end aborted with the\n * entry ID. `build` receives the slot so interruption and abort can report the durable partial output.\n */\nasync function settle(\n\truntime: Runtime,\n\tcall: ToolCall,\n\tending: Ending,\n\tbuild: (slot: Readonly<ToolSlot> | undefined) => ToolExecutionResult,\n\tcontext: Context,\n): Promise<void> {\n\tawait runtime.commit(async (tx) => {\n\t\tconst slot = toolSlot(await tx.doc(LiveDoc, runtime.conversationId), runtime.taskId);\n\t\tconst result = build(slot);\n\t\tconst entry = await appendToolResult(tx, runtime.conversationId, call, result, runtime.now());\n\t\tif (slot !== undefined) finishSlot(slot, entry.id);\n\t\tconst entryId = entry.id;\n\t\tif (ending.status === \"aborted\")\n\t\t\treturn { status: \"terminal\", outcome: { status: \"aborted\", result: { entryId } } };\n\t\tif (ending.status === \"failed\") {\n\t\t\tconst error = { message: ending.message };\n\t\t\treturn { status: \"terminal\", outcome: { status: \"failed\", error, result: { entryId } } };\n\t\t}\n\t\t// Tools build control objects freely; drop keys set to undefined so the task result is strict JSON.\n\t\tconst control =\n\t\t\tresult.control === undefined\n\t\t\t\t? {}\n\t\t\t\t: { control: copyJson(result.control as JsonValue, { omitUndefinedProperties: true }) as ToolControl };\n\t\treturn { status: \"terminal\", outcome: { status: \"completed\", result: { entryId, ...control } } };\n\t}, context);\n}\n\n/**\n * How a tool task ends; the result entry is appended either way. `failed` (execution threw or was interrupted)\n * records cancellation intent for the conversations the call owns; a result with `isError` still completes.\n */\ntype Ending = { readonly status: \"completed\" | \"aborted\" } | { readonly status: \"failed\"; readonly message: string };\n\nconst COMPLETED: Ending = { status: \"completed\" };\n\n/** An error result from the slot's durable partial output, details, and diagnostics. */\nfunction fromSlot(slot: Readonly<ToolSlot> | undefined, code: string, message: string): ToolExecutionResult {\n\tconst diagnostics = [...(slot?.diagnostics ?? [])];\n\tconst droppedBytes = slot?.droppedBytes ?? 0;\n\tif (droppedBytes > 0) diagnostics.push(truncated({ droppedBytes, droppedLines: slot?.droppedLines ?? 0 }));\n\tdiagnostics.push(toolDiagnostic(code, message));\n\treturn {\n\t\tcontent: slot?.output === undefined || slot.output === \"\" ? [] : [{ type: \"text\", text: slot.output }],\n\t\tisError: true,\n\t\t...(slot?.details === undefined ? {} : { details: slot.details }),\n\t\tdiagnostics,\n\t};\n}\n\n/** An error result the Harness writes itself: no content and one `error` diagnostic with `code`. */\nexport function harnessError(code: string, message: string): ToolExecutionResult {\n\treturn { content: [], isError: true, diagnostics: [toolDiagnostic(code, message)] };\n}\n\nfunction toolDiagnostic(code: string, message: string): ToolDiagnostic {\n\treturn { severity: \"error\", code, message };\n}\n\n/** The Harness's truncation diagnostic; `retain` is unknown when rebuilt from a slot after recovery. */\nfunction truncated(\n\tdropped: { readonly droppedLines: number; readonly droppedBytes: number },\n\tretain?: \"head\" | \"tail\",\n): ToolDiagnostic {\n\tconst kept = retain === undefined ? \"\" : ` to its ${retain === \"head\" ? \"beginning\" : \"end\"}`;\n\treturn {\n\t\tseverity: \"warn\",\n\t\tcode: \"truncated\",\n\t\tmessage: `Output truncated${kept}: ${dropped.droppedLines} lines, ${dropped.droppedBytes} bytes dropped`,\n\t};\n}\n\n/**\n * Append a `pi.tool-result` entry. The content ends with the rendered diagnostics, so the stored message is exactly\n * what the model sees; `data` keeps the structured list. A result's usage is added to `pi.usage` in the same commit.\n */\nexport async function appendToolResult(\n\ttx: Tx,\n\tconversationId: ConversationId,\n\tcall: ToolCall,\n\tresult: ToolExecutionResult,\n\ttimestamp: number,\n): Promise<TypedEntry<{ diagnostics: ToolDiagnostic[] }>> {\n\tconst diagnostics = [...(result.diagnostics ?? [])];\n\tconst content: Content = [...(result.content ?? [])];\n\tif (diagnostics.length > 0) content.push({ type: \"text\", text: renderDiagnostics(diagnostics) });\n\tconst message = {\n\t\trole: \"toolResult\",\n\t\ttoolCallId: call.id,\n\t\ttoolName: call.name,\n\t\tcontent,\n\t\t...(result.details === undefined ? {} : { details: result.details }),\n\t\t...(result.usage === undefined ? {} : { usage: result.usage }),\n\t\tisError: result.isError ?? false,\n\t\ttimestamp,\n\t} as ToolResultMessage;\n\tif (result.usage !== undefined) await recordUsage(tx, conversationId, \"tools\", call.name, result.usage);\n\treturn tx.appendEntry(ToolResultEntry, conversationId, { model: [message], data: { diagnostics } });\n}\n\nfunction renderDiagnostics(diagnostics: readonly ToolDiagnostic[]): string {\n\treturn `<harness>\\n${diagnostics.map((diagnostic) => `[${diagnostic.severity}] ${diagnostic.message}`).join(\"\\n\")}\\n</harness>`;\n}\n\n/**\n * Bound the text of result content. When the joined text exceeds the limits, the text items are replaced by one bounded\n * item at the position of the first (head) or last (tail) text item; other content is kept.\n */\nfunction boundContent(\n\tcontent: Content,\n\tlimits: OutputLimits,\n): { content: Content; droppedBytes: number; droppedLines: number } {\n\tconst texts = content.filter((item): item is TextContent => item.type === \"text\");\n\tconst bounded = boundOutput(texts.map((item) => item.text).join(\"\"), limits);\n\tif (bounded.droppedBytes === 0) return { content, droppedBytes: 0, droppedLines: 0 };\n\tconst keep = limits.retain === \"head\" ? texts[0] : texts.at(-1);\n\tconst result: Content = [];\n\tfor (const item of content) {\n\t\tif (item.type !== \"text\") result.push(item);\n\t\telse if (item === keep) result.push({ ...item, text: bounded.text });\n\t}\n\treturn { content: result, droppedBytes: bounded.droppedBytes, droppedLines: bounded.droppedLines };\n}\n\nfunction errorText(error: unknown): string {\n\treturn error instanceof Error ? error.message : String(error);\n}\n"]}
|