maka-agent 0.2.0-dev.11.20260831 → 0.2.0-dev.12.20260901
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/LICENSE +2 -2
- package/RUNTIME_HOST_PEER_DEPENDENCIES.rust.tsv +70 -0
- package/RUNTIME_HOST_PEER_THIRD_PARTY_NOTICES.txt +1839 -140
- package/dist/cli-core.js +25 -1
- package/dist/mcp-capability-provider.js +1 -1
- package/dist/onboarding-catalog.js +3 -5
- package/dist/pi-transcript.js +21 -17
- package/dist/pi-tui-layout.js +4 -2
- package/dist/pi-tui-mcp-status.js +44 -130
- package/dist/pi-tui-pickers.js +11 -5
- package/dist/pi-tui-runner.js +133 -102
- package/dist/runtime-host-cli.js +150 -4
- package/dist/runtime-host-lifecycle-transaction.js +5 -2
- package/dist/runtime-host-managed-lifecycle-manager.js +1 -0
- package/dist/runtime-host-onboarding.js +12 -16
- package/dist/runtime-host-peer-management-command.js +16 -4
- package/dist/runtime-host-plugin-command.js +101 -0
- package/dist/runtime-host-run-command.js +5 -1
- package/dist/runtime-host-service-management-command.js +2 -1
- package/dist/runtime-host-setup-command.js +1 -0
- package/dist/runtime-host-task-readiness.js +2 -3
- package/dist/runtime-host-tui-command.js +1 -1
- package/dist/runtime-host-tui-context.js +18 -5
- package/dist/runtime-host-update-command.js +13 -5
- package/dist/runtime-host-update-discovery.js +1 -0
- package/dist/tui-clipboard.js +86 -0
- package/dist/tui-copy-catalog.js +246 -0
- package/dist/tui-copy-command.js +103 -0
- package/native/runtime-host-peer/prebuilds/darwin-arm64/maka_runtime_host_peer.node +0 -0
- package/native/runtime-host-peer/prebuilds/linux-arm64/maka_runtime_host_peer.node +0 -0
- package/native/runtime-host-peer/prebuilds/linux-x64/maka_runtime_host_peer.node +0 -0
- package/native/runtime-host-peer/prebuilds/win32-x64/maka_runtime_host_peer.node +0 -0
- package/node_modules/@maka/core/dist/attachments.js +80 -1
- package/node_modules/@maka/core/dist/bounded-response.js +71 -0
- package/node_modules/@maka/core/dist/chat-model-choice.js +8 -33
- package/node_modules/@maka/core/dist/client-capability-grant.js +149 -0
- package/node_modules/@maka/core/dist/connection-readiness.js +3 -31
- package/node_modules/@maka/core/dist/deep-research.js +1 -20
- package/node_modules/@maka/core/dist/events.js +22 -1
- package/node_modules/@maka/core/dist/foreign-session.js +2 -2
- package/node_modules/@maka/core/dist/interaction.js +49 -2
- package/node_modules/@maka/core/dist/llm-connections.js +74 -82
- package/node_modules/@maka/core/dist/model-catalog.js +221 -315
- package/node_modules/@maka/core/dist/model-facts.js +13 -19
- package/node_modules/@maka/core/dist/model-metadata.generated.js +1907 -1872
- package/node_modules/@maka/core/dist/model-metadata.js +184 -199
- package/node_modules/@maka/core/dist/model-thinking.js +1 -1
- package/node_modules/@maka/core/dist/models-dev-projection.js +302 -0
- package/node_modules/@maka/core/dist/models-dev-refresh.js +55 -0
- package/node_modules/@maka/core/dist/onboarding.js +2 -1
- package/node_modules/@maka/core/dist/provider-auth.js +47 -291
- package/node_modules/@maka/core/dist/provider-registry.js +68 -357
- package/node_modules/@maka/core/dist/runtime-event.js +4 -0
- package/node_modules/@maka/core/dist/runtime-policy/connection-catalog-codec.js +34 -18
- package/node_modules/@maka/core/dist/runtime-policy/model-catalog-entry-codec.js +75 -0
- package/node_modules/@maka/core/dist/runtime-policy.js +2 -1
- package/node_modules/@maka/core/dist/session-send-projection.js +2 -1
- package/node_modules/@maka/core/dist/session-start-mode.js +42 -0
- package/node_modules/@maka/core/dist/session.js +119 -5
- package/node_modules/@maka/core/dist/slash-command-catalog.js +1 -0
- package/node_modules/@maka/core/dist/task-submission-readiness.js +2 -1
- package/node_modules/@maka/core/dist/workhub-creation-intent.js +896 -0
- package/node_modules/@maka/core/package.json +6 -1
- package/node_modules/@maka/runtime/dist/agent-run.js +7 -0
- package/node_modules/@maka/runtime/dist/ai-sdk-backend.js +3 -2
- package/node_modules/@maka/runtime/dist/image-file.js +5 -20
- package/node_modules/@maka/runtime/dist/local-web-fetch.js +2 -43
- package/node_modules/@maka/runtime/dist/mcp-tools.js +28 -0
- package/node_modules/@maka/runtime/dist/model-fetcher.js +15 -8
- package/node_modules/@maka/runtime/dist/model-history.js +18 -1
- package/node_modules/@maka/runtime/dist/model-runtime.js +2 -2
- package/node_modules/@maka/runtime/dist/plugin-composition-loader.js +173 -20
- package/node_modules/@maka/runtime/dist/plugin-runtime.js +219 -0
- package/node_modules/@maka/runtime/dist/quiescent-session-snapshot.js +78 -0
- package/node_modules/@maka/runtime/dist/runtime-event-backfill.js +3 -0
- package/node_modules/@maka/runtime/dist/runtime-event-read-model.js +1 -0
- package/node_modules/@maka/runtime/dist/session-event-runtime-mapper.js +8 -4
- package/node_modules/@maka/runtime/dist/telemetry/model-pricing.generated.js +93 -83
- package/node_modules/@maka/runtime/dist/test-connection.js +6 -6
- package/node_modules/@maka/runtime/dist/tool-runtime.js +50 -6
- package/node_modules/@maka/runtime/dist/workers/filesystem-worker.js +39 -16
- package/node_modules/@maka/runtime/package.json +1 -0
- package/node_modules/@maka/runtime-host/dist/adapter/session-projector.js +10 -0
- package/node_modules/@maka/runtime-host/dist/client/catalog-reader.js +19 -5
- package/node_modules/@maka/runtime-host/dist/client/client-capability-channel.js +3 -2
- package/node_modules/@maka/runtime-host/dist/client/connection.js +22 -0
- package/node_modules/@maka/runtime-host/dist/client/host-profile.js +1 -0
- package/node_modules/@maka/runtime-host/dist/client/index.js +1 -0
- package/node_modules/@maka/runtime-host/dist/client/oauth-presentation.js +1 -1
- package/node_modules/@maka/runtime-host/dist/client/peer-client.js +4 -0
- package/node_modules/@maka/runtime-host/dist/client/reconnecting-connection.js +8 -0
- package/node_modules/@maka/runtime-host/dist/operator/index.js +2 -1
- package/node_modules/@maka/runtime-host/dist/operator/managed-deployment.js +2 -0
- package/node_modules/@maka/runtime-host/dist/operator/peer-management-frame.js +2 -0
- package/node_modules/@maka/runtime-host/dist/operator/service-management-frame.js +2 -0
- package/node_modules/@maka/runtime-host/dist/peer-mesh/owner.js +1 -0
- package/node_modules/@maka/runtime-host/dist/protocol/client-capability.js +22 -1
- package/node_modules/@maka/{storage/dist/task-ledger-store-internal.js → runtime-host/dist/protocol/connection-catalog-change.js} +7 -13
- package/node_modules/@maka/runtime-host/dist/protocol/index.js +33 -1
- package/node_modules/@maka/runtime-host/dist/protocol/operations.js +3 -1
- package/node_modules/@maka/runtime-host/dist/protocol/plugin-platform.js +512 -0
- package/node_modules/@maka/runtime-host/dist/protocol/runtime-policy.js +25 -61
- package/node_modules/@maka/runtime-host/dist/protocol/session-catalog.js +1 -1
- package/node_modules/@maka/runtime-host/dist/protocol/turn.js +4 -1
- package/node_modules/@maka/runtime-host/dist/protocol/workhub-coordination.js +92 -4
- package/node_modules/@maka/runtime-host/dist/server/access-authority.js +15 -13
- package/node_modules/@maka/runtime-host/dist/server/access-credential-metadata.js +4 -2
- package/node_modules/@maka/runtime-host/dist/server/access-credential-store.js +119 -47
- package/node_modules/@maka/runtime-host/dist/server/bootstrap-runtime-policy.js +10 -4
- package/node_modules/@maka/runtime-host/dist/server/client-capability-coordinator.js +192 -18
- package/node_modules/@maka/runtime-host/dist/server/client-capability-invocation-broker.js +122 -48
- package/node_modules/@maka/runtime-host/dist/server/client-capability-provider-id.js +31 -0
- package/node_modules/@maka/runtime-host/dist/server/connection-effect-coordinator.js +5 -5
- package/node_modules/@maka/runtime-host/dist/server/connection-session.js +1 -0
- package/node_modules/@maka/runtime-host/dist/server/execution-composition.js +117 -4
- package/node_modules/@maka/runtime-host/dist/server/execution-model-authority.js +2 -2
- package/node_modules/@maka/runtime-host/dist/server/extension-bundle.js +249 -0
- package/node_modules/@maka/runtime-host/dist/server/extension-package-manifest.js +262 -0
- package/node_modules/@maka/runtime-host/dist/server/host-change-feed.js +11 -0
- package/node_modules/@maka/runtime-host/dist/server/index.js +5 -0
- package/node_modules/@maka/runtime-host/dist/server/interaction-coordinator.js +181 -13
- package/node_modules/@maka/runtime-host/dist/server/interaction-projection.js +6 -0
- package/node_modules/@maka/runtime-host/dist/server/message-coordinator.js +88 -22
- package/node_modules/@maka/runtime-host/dist/server/model-metadata-refresh.js +96 -0
- package/node_modules/@maka/runtime-host/dist/server/operation-dispatcher.js +43 -118
- package/node_modules/@maka/runtime-host/dist/server/plugin-composition-patch.js +58 -0
- package/node_modules/@maka/runtime-host/dist/server/plugin-composition-store.js +141 -0
- package/node_modules/@maka/runtime-host/dist/server/plugin-package-loader.js +130 -0
- package/node_modules/@maka/runtime-host/dist/server/plugin-package-store.js +528 -0
- package/node_modules/@maka/runtime-host/dist/server/plugin-platform-coordinator.js +249 -0
- package/node_modules/@maka/runtime-host/dist/server/plugin-platform.js +1130 -0
- package/node_modules/@maka/runtime-host/dist/server/root-admission-owner.js +4 -0
- package/node_modules/@maka/runtime-host/dist/server/root-turn-coordinator.js +24 -5
- package/node_modules/@maka/runtime-host/dist/server/runtime-policy-coordinator.js +29 -4
- package/node_modules/@maka/runtime-host/dist/server/session-catalog-coordinator.js +2 -1
- package/node_modules/@maka/runtime-host/dist/server/session-transcript-pager.js +10 -2
- package/node_modules/@maka/runtime-host/dist/server/web-fetch-tool.js +1 -1
- package/node_modules/@maka/runtime-host/dist/server/workhub-coordination-action-gate.js +276 -3
- package/node_modules/@maka/runtime-host/dist/server/workhub-coordination-coordinator.js +101 -1
- package/node_modules/@maka/runtime-host/dist/transport/peer-native.js +1 -0
- package/node_modules/@maka/runtime-host/dist/webrtc-stun-policy.js +114 -0
- package/node_modules/@maka/runtime-host/package.json +2 -0
- package/node_modules/@maka/storage/dist/agent-run-store.js +4 -0
- package/node_modules/@maka/storage/dist/artifact-store.js +7 -23
- package/node_modules/@maka/storage/dist/conversation-operational-state.js +3 -0
- package/node_modules/@maka/storage/dist/execution-stores.js +3 -0
- package/node_modules/@maka/storage/dist/interaction-store-public.js +1 -1
- package/node_modules/@maka/storage/dist/interaction-store.js +173 -0
- package/node_modules/@maka/storage/dist/production-session-snapshot.js +761 -0
- package/node_modules/@maka/storage/dist/quiescent-session-snapshot.js +1 -1
- package/node_modules/@maka/storage/dist/runtime-policy/connection-catalog-document.js +19 -22
- package/node_modules/@maka/storage/dist/runtime-policy/coordinator.js +16 -36
- package/node_modules/@maka/storage/dist/runtime-policy/onboarding-transaction.js +2 -2
- package/node_modules/@maka/storage/dist/runtime-policy-stores.js +1 -1
- package/node_modules/@maka/storage/dist/session-message-projection.js +1 -1
- package/node_modules/@maka/storage/dist/session-store.js +32 -6
- package/node_modules/@maka/storage/dist/session-todo-store.js +6 -68
- package/node_modules/@maka/storage/dist/sqlite-core-execution-schema.js +19 -0
- package/node_modules/@maka/storage/dist/sqlite-session-metadata-schema.js +10 -1
- package/node_modules/@maka/storage/dist/sqlite-session-metadata-store.js +73 -3
- package/node_modules/@maka/storage/dist/sqlite-workflow-schema.js +2 -10
- package/node_modules/@maka/storage/package.json +2 -0
- package/package.json +1 -1
- package/node_modules/@maka/core/dist/task-ledger.js +0 -669
- package/node_modules/@maka/storage/dist/task-ledger-authority.js +0 -121
- package/node_modules/@maka/storage/dist/task-ledger-store.js +0 -663
|
@@ -1,663 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Licensed to the Apache Software Foundation (ASF) under one
|
|
3
|
-
* or more contributor license agreements. See the NOTICE file
|
|
4
|
-
* distributed with this work for additional information
|
|
5
|
-
* regarding copyright ownership. The ASF licenses this file
|
|
6
|
-
* to you under the Apache License, Version 2.0 (the
|
|
7
|
-
* "License"); you may not use this file except in compliance
|
|
8
|
-
* with the License. You may obtain a copy of the License at
|
|
9
|
-
*
|
|
10
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
-
*
|
|
12
|
-
* Unless required by applicable law or agreed to in writing,
|
|
13
|
-
* software distributed under the License is distributed on an
|
|
14
|
-
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
15
|
-
* KIND, either express or implied. See the License for the
|
|
16
|
-
* specific language governing permissions and limitations
|
|
17
|
-
* under the License.
|
|
18
|
-
*/
|
|
19
|
-
import { resolve } from 'node:path';
|
|
20
|
-
import { createHash, randomUUID } from 'node:crypto';
|
|
21
|
-
import { TASK_LEDGER_MAX_TASKS, TASK_ARCHIVE_AFTER_MS, findTaskByRef, isSafeTaskId, isTaskKey, isTaskOwner, isTerminalTaskStatus, isTaskLedgerEvent, normalizeUpdateTaskInput, normalizeCreateTaskInput, projectTaskLedgerEvents, taskLedgerEventTypeForCreate, taskLedgerEventTypeForUpdate, validateTaskUpdate, classifyTaskResumeTrust, } from '@maka/core/task-ledger';
|
|
22
|
-
import { chainWrite } from './write-queue.js';
|
|
23
|
-
import { assertSafeSessionId } from './session-store.js';
|
|
24
|
-
import { registerTaskLedgerCanonicalReader } from './task-ledger-store-internal.js';
|
|
25
|
-
import { acquireOperationalStateDatabase, } from './operational-state-store.js';
|
|
26
|
-
export function createSqliteTaskLedgerStore(workspaceRoot) {
|
|
27
|
-
return new SqliteTaskLedgerStoreImpl(workspaceRoot);
|
|
28
|
-
}
|
|
29
|
-
class SqliteTaskLedgerStoreImpl {
|
|
30
|
-
#lease;
|
|
31
|
-
writeQueues = new Map();
|
|
32
|
-
listeners = new Set();
|
|
33
|
-
constructor(workspaceRoot) {
|
|
34
|
-
this.#lease = acquireOperationalStateDatabase(resolve(workspaceRoot));
|
|
35
|
-
registerTaskLedgerCanonicalReader(this, {
|
|
36
|
-
list: (sessionId, options) => this.#listCanonical(sessionId, options),
|
|
37
|
-
get: (sessionId, id, options) => this.#getCanonical(sessionId, id, options),
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
ready() {
|
|
41
|
-
return Promise.resolve();
|
|
42
|
-
}
|
|
43
|
-
close() {
|
|
44
|
-
this.#lease.close();
|
|
45
|
-
}
|
|
46
|
-
async list(sessionId, options = {}) {
|
|
47
|
-
assertSafeSessionId(sessionId);
|
|
48
|
-
return this.applyListOptions(await this.readForRender(sessionId), options);
|
|
49
|
-
}
|
|
50
|
-
async get(sessionId, id, options = {}) {
|
|
51
|
-
assertSafeSessionId(sessionId);
|
|
52
|
-
if (!isSafeTaskId(id))
|
|
53
|
-
throw new Error('Task id must be a stable token (alphanumeric plus . _ : -, max 64 chars)');
|
|
54
|
-
const tasks = await this.list(sessionId, options);
|
|
55
|
-
return findTaskByRef(tasks, id);
|
|
56
|
-
}
|
|
57
|
-
async #listCanonical(sessionId, options = {}) {
|
|
58
|
-
assertSafeSessionId(sessionId);
|
|
59
|
-
const { tasks } = await this.readForMutateWithSource(sessionId);
|
|
60
|
-
return this.applyListOptions(tasks, options);
|
|
61
|
-
}
|
|
62
|
-
async #getCanonical(sessionId, id, options = {}) {
|
|
63
|
-
assertSafeSessionId(sessionId);
|
|
64
|
-
if (!isSafeTaskId(id))
|
|
65
|
-
throw new Error('Task id must be a stable token (alphanumeric plus . _ : -, max 64 chars)');
|
|
66
|
-
return findTaskByRef(await this.#listCanonical(sessionId, options), id);
|
|
67
|
-
}
|
|
68
|
-
subscribe(listener) {
|
|
69
|
-
this.listeners.add(listener);
|
|
70
|
-
return () => this.listeners.delete(listener);
|
|
71
|
-
}
|
|
72
|
-
async copyConversationTaskLedger(input) {
|
|
73
|
-
assertSafeSessionId(input.sourceSessionId);
|
|
74
|
-
assertSafeSessionId(input.targetSessionId);
|
|
75
|
-
if (input.sourceSessionId === input.targetSessionId) {
|
|
76
|
-
throw new Error('Task Ledger conversation copy requires distinct Sessions');
|
|
77
|
-
}
|
|
78
|
-
if (input.beforeTs !== undefined &&
|
|
79
|
-
(!Number.isSafeInteger(input.beforeTs) || input.beforeTs < 0)) {
|
|
80
|
-
throw new Error('Task Ledger conversation-copy boundary is invalid');
|
|
81
|
-
}
|
|
82
|
-
const turnIds = new Set(input.turnIds);
|
|
83
|
-
const runIds = new Map(input.runIdMap.map(({ sourceRunId, targetRunId }) => [sourceRunId, targetRunId]));
|
|
84
|
-
const source = await this.readConversationCopyEvents(input.sourceSessionId);
|
|
85
|
-
const selected = [];
|
|
86
|
-
let crossedBoundary = false;
|
|
87
|
-
for (const event of source) {
|
|
88
|
-
const eventTurnId = event.refs?.turnId;
|
|
89
|
-
const retained = eventTurnId !== undefined
|
|
90
|
-
? turnIds.has(eventTurnId)
|
|
91
|
-
: input.beforeTs === undefined || event.ts < input.beforeTs;
|
|
92
|
-
if (!retained) {
|
|
93
|
-
crossedBoundary = true;
|
|
94
|
-
continue;
|
|
95
|
-
}
|
|
96
|
-
if (crossedBoundary) {
|
|
97
|
-
throw new Error('Task Ledger events cross the conversation-copy boundary');
|
|
98
|
-
}
|
|
99
|
-
selected.push(rewriteConversationTaskEvent(event, input.sourceSessionId, input.targetSessionId, runIds, input.linkedChildren ?? 'preserve'));
|
|
100
|
-
}
|
|
101
|
-
if (selected.length === 0)
|
|
102
|
-
return;
|
|
103
|
-
const projection = projectTaskLedgerEvents(selected);
|
|
104
|
-
if (projection.diagnostics.length > 0) {
|
|
105
|
-
throw new Error(`Task Ledger conversation copy is not projectable: ${projection.diagnostics.join('; ')}`);
|
|
106
|
-
}
|
|
107
|
-
await chainWrite(this.writeQueues, input.targetSessionId, async () => {
|
|
108
|
-
this.copyConversationLedger(input.targetSessionId, selected);
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
async purgeConversationTaskLedger(sessionId) {
|
|
112
|
-
assertSafeSessionId(sessionId);
|
|
113
|
-
await chainWrite(this.writeQueues, sessionId, async () => {
|
|
114
|
-
this.#lease.transaction('write', () => {
|
|
115
|
-
this.#lease.database
|
|
116
|
-
.prepare('DELETE FROM workflow_task_ledger_events WHERE session_id = ?')
|
|
117
|
-
.run(sessionId);
|
|
118
|
-
});
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
async create(sessionId, drafts, context = {}) {
|
|
122
|
-
assertSafeSessionId(sessionId);
|
|
123
|
-
if (!Array.isArray(drafts) || drafts.length === 0) {
|
|
124
|
-
throw new Error('Task creation requires at least one task draft');
|
|
125
|
-
}
|
|
126
|
-
// Front-door the per-batch cap before generating ids or normalizing drafts:
|
|
127
|
-
// a single call can never add more than the absolute ledger cap, and rejecting
|
|
128
|
-
// here avoids generating N uuids for a batch the write-queue total check
|
|
129
|
-
// would refuse anyway. The total (existing + new) cap is still enforced
|
|
130
|
-
// inside the serialized mutate callback below.
|
|
131
|
-
if (drafts.length > TASK_LEDGER_MAX_TASKS) {
|
|
132
|
-
throw new Error(`Task creation batch of ${drafts.length} tasks exceeds the ${TASK_LEDGER_MAX_TASKS}-task per-batch cap; split the work into smaller calls.`);
|
|
133
|
-
}
|
|
134
|
-
const normalizedDrafts = drafts.map((draft) => {
|
|
135
|
-
const normalized = normalizeCreateTaskInput(draft);
|
|
136
|
-
if (!normalized.ok)
|
|
137
|
-
throw new Error(normalized.message);
|
|
138
|
-
return normalized.value;
|
|
139
|
-
});
|
|
140
|
-
const created = [];
|
|
141
|
-
// Cap check runs inside the serialized mutate callback (after reading the
|
|
142
|
-
// current ledger) so concurrent creates cannot race past the limit, and a
|
|
143
|
-
// rejected create never touches the file.
|
|
144
|
-
const all = await this.mutate(sessionId, (tasks) => {
|
|
145
|
-
if (tasks.length + normalizedDrafts.length > TASK_LEDGER_MAX_TASKS) {
|
|
146
|
-
throw new Error(`Task ledger is limited to ${TASK_LEDGER_MAX_TASKS} tasks total per session ` +
|
|
147
|
-
`(currently ${tasks.length}, adding ${normalizedDrafts.length}). This is a hard runaway guard on the ` +
|
|
148
|
-
'total count — completed or cancelled tasks still count, so batch related work into fewer, ' +
|
|
149
|
-
'coarser tasks instead.');
|
|
150
|
-
}
|
|
151
|
-
const now = Date.now();
|
|
152
|
-
for (const draft of normalizedDrafts) {
|
|
153
|
-
const parent = draft.parentId ? findTaskByRef(tasks, draft.parentId) : undefined;
|
|
154
|
-
if (draft.parentId && !parent)
|
|
155
|
-
throw new Error(`No such parent task: ${draft.parentId}`);
|
|
156
|
-
if (parent && isTerminalTaskStatus(parent.status)) {
|
|
157
|
-
throw new Error(`Cannot create a child under terminal task ${parent.key}`);
|
|
158
|
-
}
|
|
159
|
-
const task = {
|
|
160
|
-
id: randomUUID(),
|
|
161
|
-
key: nextTaskKey([...tasks, ...created], parent),
|
|
162
|
-
subject: draft.subject,
|
|
163
|
-
status: 'pending',
|
|
164
|
-
createdAt: now,
|
|
165
|
-
updatedAt: now,
|
|
166
|
-
...(parent ? { parentId: parent.id } : {}),
|
|
167
|
-
...(ownerFromContext(context) ? { owner: ownerFromContext(context) } : {}),
|
|
168
|
-
};
|
|
169
|
-
created.push(task);
|
|
170
|
-
}
|
|
171
|
-
return [...tasks, ...created];
|
|
172
|
-
}, (next) => created.map((task) => buildTaskLedgerEvent({
|
|
173
|
-
type: taskLedgerEventTypeForCreate(task),
|
|
174
|
-
sessionId,
|
|
175
|
-
task,
|
|
176
|
-
context,
|
|
177
|
-
})));
|
|
178
|
-
return { created, total: all.length };
|
|
179
|
-
}
|
|
180
|
-
async update(sessionId, id, patch, context = {}) {
|
|
181
|
-
assertSafeSessionId(sessionId);
|
|
182
|
-
const now = Date.now();
|
|
183
|
-
let updated;
|
|
184
|
-
let previous;
|
|
185
|
-
const all = await this.mutate(sessionId, (tasks) => {
|
|
186
|
-
// Locate the target before producing a new list: an unknown id must
|
|
187
|
-
// fail inside the callback without rewriting an identical file.
|
|
188
|
-
const resolved = findTaskByRef(tasks, id);
|
|
189
|
-
const index = resolved ? tasks.findIndex((task) => task.id === resolved.id) : -1;
|
|
190
|
-
const current = index === -1 ? undefined : tasks[index];
|
|
191
|
-
if (!current)
|
|
192
|
-
throw new Error(`No such task: ${id}`);
|
|
193
|
-
previous = current;
|
|
194
|
-
const normalizedPatch = normalizeUpdateTaskInput(patch);
|
|
195
|
-
if (!normalizedPatch.ok)
|
|
196
|
-
throw new Error(normalizedPatch.message);
|
|
197
|
-
const normalized = validateTaskUpdate(current, normalizedPatch.value, {
|
|
198
|
-
explicitReopen: normalizedPatch.value.explicitReopen === true,
|
|
199
|
-
});
|
|
200
|
-
if (!normalized.ok)
|
|
201
|
-
throw new Error(normalized.message);
|
|
202
|
-
const { explicitReopen: _explicitReopen, ...taskPatch } = normalized.value;
|
|
203
|
-
void _explicitReopen;
|
|
204
|
-
updated = {
|
|
205
|
-
...current,
|
|
206
|
-
...(taskPatch.subject !== undefined ? { subject: taskPatch.subject } : {}),
|
|
207
|
-
...(taskPatch.status !== undefined ? { status: taskPatch.status } : {}),
|
|
208
|
-
...(taskPatch.blockedReason !== undefined
|
|
209
|
-
? { blockedReason: taskPatch.blockedReason }
|
|
210
|
-
: {}),
|
|
211
|
-
...(taskPatch.failureReason !== undefined
|
|
212
|
-
? { failureReason: taskPatch.failureReason }
|
|
213
|
-
: {}),
|
|
214
|
-
...(taskPatch.completionEvidence !== undefined
|
|
215
|
-
? { completionEvidence: taskPatch.completionEvidence }
|
|
216
|
-
: {}),
|
|
217
|
-
...(taskPatch.status === 'in_progress' && context.actor === 'main_agent'
|
|
218
|
-
? { owner: ownerFromContext(context) }
|
|
219
|
-
: {}),
|
|
220
|
-
updatedAt: now,
|
|
221
|
-
};
|
|
222
|
-
if (taskPatch.status !== undefined && isTerminalTaskStatus(taskPatch.status)) {
|
|
223
|
-
if (taskPatch.status === 'completed')
|
|
224
|
-
assertDescendantsTerminal(tasks, current.id);
|
|
225
|
-
updated.endedAt = now;
|
|
226
|
-
}
|
|
227
|
-
else if (taskPatch.status === 'pending' || taskPatch.status === 'in_progress') {
|
|
228
|
-
delete updated.endedAt;
|
|
229
|
-
}
|
|
230
|
-
if (taskPatch.status === 'pending')
|
|
231
|
-
delete updated.owner;
|
|
232
|
-
updated = clearStaleTaskEvidence(updated);
|
|
233
|
-
const next = [...tasks];
|
|
234
|
-
next[index] = updated;
|
|
235
|
-
return next;
|
|
236
|
-
}, () => {
|
|
237
|
-
if (!previous || !updated)
|
|
238
|
-
return [];
|
|
239
|
-
return [
|
|
240
|
-
buildTaskLedgerEvent({
|
|
241
|
-
type: taskLedgerEventTypeForUpdate(previous, updated),
|
|
242
|
-
sessionId,
|
|
243
|
-
task: updated,
|
|
244
|
-
previous,
|
|
245
|
-
context,
|
|
246
|
-
}),
|
|
247
|
-
];
|
|
248
|
-
});
|
|
249
|
-
if (!updated)
|
|
250
|
-
throw new Error(`No such task: ${id}`);
|
|
251
|
-
return { updated, total: all.length };
|
|
252
|
-
}
|
|
253
|
-
async claim(sessionId, id, owner, context = {}) {
|
|
254
|
-
assertSafeSessionId(sessionId);
|
|
255
|
-
assertChildTaskOwner(owner);
|
|
256
|
-
let updated;
|
|
257
|
-
let previous;
|
|
258
|
-
const all = await this.mutate(sessionId, (tasks) => {
|
|
259
|
-
const current = findTaskByRef(tasks, id);
|
|
260
|
-
if (!current)
|
|
261
|
-
throw new Error(`No such task: ${id}`);
|
|
262
|
-
if (isTerminalTaskStatus(current.status))
|
|
263
|
-
throw new Error(`Cannot claim terminal task ${current.key}`);
|
|
264
|
-
if (current.status === 'in_progress' &&
|
|
265
|
-
current.owner?.actor === 'child_agent' &&
|
|
266
|
-
current.owner.turnId !== owner.turnId) {
|
|
267
|
-
throw new Error(`Task ${current.key} is already claimed by another child agent`);
|
|
268
|
-
}
|
|
269
|
-
previous = current;
|
|
270
|
-
updated = clearStaleTaskEvidence({
|
|
271
|
-
...current,
|
|
272
|
-
status: 'in_progress',
|
|
273
|
-
owner,
|
|
274
|
-
updatedAt: Date.now(),
|
|
275
|
-
});
|
|
276
|
-
return tasks.map((task) => (task.id === current.id ? updated : task));
|
|
277
|
-
}, () => previous && updated
|
|
278
|
-
? [
|
|
279
|
-
buildTaskLedgerEvent({
|
|
280
|
-
type: taskLedgerEventTypeForUpdate(previous, updated),
|
|
281
|
-
sessionId,
|
|
282
|
-
task: updated,
|
|
283
|
-
previous,
|
|
284
|
-
context,
|
|
285
|
-
}),
|
|
286
|
-
]
|
|
287
|
-
: []);
|
|
288
|
-
if (!updated)
|
|
289
|
-
throw new Error(`No such task: ${id}`);
|
|
290
|
-
return { updated, total: all.length };
|
|
291
|
-
}
|
|
292
|
-
async claimAvailable(sessionId, id, owner, scope, context = {}) {
|
|
293
|
-
assertSafeSessionId(sessionId);
|
|
294
|
-
assertChildTaskOwner(owner);
|
|
295
|
-
if (!isSafeTaskId(scope.parentRunId))
|
|
296
|
-
throw new Error('Available task claim requires a stable parent AgentRun id');
|
|
297
|
-
let updated;
|
|
298
|
-
let previous;
|
|
299
|
-
const all = await this.mutate(sessionId, (tasks) => {
|
|
300
|
-
const current = findTaskByRef(tasks, id);
|
|
301
|
-
if (!current)
|
|
302
|
-
throw new Error(`No such task: ${id}`);
|
|
303
|
-
if (isTerminalTaskStatus(current.status))
|
|
304
|
-
throw new Error(`Cannot claim terminal task ${current.key}`);
|
|
305
|
-
const alreadyClaimed = tasks.find((task) => task.id !== current.id &&
|
|
306
|
-
!isTerminalTaskStatus(task.status) &&
|
|
307
|
-
task.owner?.actor === 'child_agent' &&
|
|
308
|
-
task.owner.turnId === owner.turnId);
|
|
309
|
-
if (alreadyClaimed) {
|
|
310
|
-
throw new Error(`Child agent already owns task ${alreadyClaimed.key}; one shared task may be claimed per child turn`);
|
|
311
|
-
}
|
|
312
|
-
const sameOwner = current.owner?.actor === 'child_agent' && current.owner.turnId === owner.turnId;
|
|
313
|
-
if (!sameOwner &&
|
|
314
|
-
(current.owner?.actor !== 'main_agent' || current.owner.runId !== scope.parentRunId)) {
|
|
315
|
-
throw new Error(`Task ${current.key} is not shared by parent run ${scope.parentRunId}`);
|
|
316
|
-
}
|
|
317
|
-
if (current.status === 'in_progress' && !sameOwner) {
|
|
318
|
-
throw new Error(`Task ${current.key} is already in progress and is not available for self-claim`);
|
|
319
|
-
}
|
|
320
|
-
if (current.owner?.actor === 'child_agent' && !sameOwner) {
|
|
321
|
-
throw new Error(`Task ${current.key} is already claimed by another child agent`);
|
|
322
|
-
}
|
|
323
|
-
previous = current;
|
|
324
|
-
updated =
|
|
325
|
-
sameOwner && current.status === 'in_progress'
|
|
326
|
-
? current
|
|
327
|
-
: clearStaleTaskEvidence({
|
|
328
|
-
...current,
|
|
329
|
-
status: 'in_progress',
|
|
330
|
-
owner,
|
|
331
|
-
updatedAt: Date.now(),
|
|
332
|
-
});
|
|
333
|
-
return updated === current
|
|
334
|
-
? tasks
|
|
335
|
-
: tasks.map((task) => (task.id === current.id ? updated : task));
|
|
336
|
-
}, () => previous && updated && previous !== updated
|
|
337
|
-
? [
|
|
338
|
-
buildTaskLedgerEvent({
|
|
339
|
-
type: taskLedgerEventTypeForUpdate(previous, updated),
|
|
340
|
-
sessionId,
|
|
341
|
-
task: updated,
|
|
342
|
-
previous,
|
|
343
|
-
context,
|
|
344
|
-
}),
|
|
345
|
-
]
|
|
346
|
-
: []);
|
|
347
|
-
if (!updated)
|
|
348
|
-
throw new Error(`No such task: ${id}`);
|
|
349
|
-
return { updated, total: all.length };
|
|
350
|
-
}
|
|
351
|
-
async settleAgentOutcome(sessionId, id, outcome, context = {}) {
|
|
352
|
-
assertSafeSessionId(sessionId);
|
|
353
|
-
assertChildTaskOwner(outcome.owner);
|
|
354
|
-
let updated;
|
|
355
|
-
let previous;
|
|
356
|
-
const all = await this.mutate(sessionId, (tasks) => {
|
|
357
|
-
const current = findTaskByRef(tasks, id);
|
|
358
|
-
if (!current)
|
|
359
|
-
throw new Error(`No such task: ${id}`);
|
|
360
|
-
if (current.owner?.actor === 'child_agent' &&
|
|
361
|
-
current.owner.turnId &&
|
|
362
|
-
current.owner.turnId !== outcome.owner.turnId) {
|
|
363
|
-
throw new Error(`Task ${current.key} is owned by a different child agent`);
|
|
364
|
-
}
|
|
365
|
-
previous = current;
|
|
366
|
-
const now = Date.now();
|
|
367
|
-
updated = { ...current, owner: outcome.owner, updatedAt: now };
|
|
368
|
-
if (!isTerminalTaskStatus(current.status)) {
|
|
369
|
-
if (outcome.status === 'failed') {
|
|
370
|
-
updated.status = 'failed';
|
|
371
|
-
updated.failureReason = normalizeOutcomeReason(outcome.reason, 'Child agent failed');
|
|
372
|
-
updated.endedAt = now;
|
|
373
|
-
}
|
|
374
|
-
else if (outcome.status === 'cancelled') {
|
|
375
|
-
updated.status = 'cancelled';
|
|
376
|
-
updated.endedAt = now;
|
|
377
|
-
}
|
|
378
|
-
else if (outcome.status === 'waiting_for_user') {
|
|
379
|
-
updated.status = 'blocked';
|
|
380
|
-
updated.blockedReason = normalizeOutcomeReason(outcome.reason, 'Child agent is waiting for user input');
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
updated = clearStaleTaskEvidence(updated);
|
|
384
|
-
return tasks.map((task) => (task.id === current.id ? updated : task));
|
|
385
|
-
}, () => previous && updated
|
|
386
|
-
? [
|
|
387
|
-
buildTaskLedgerEvent({
|
|
388
|
-
type: taskLedgerEventTypeForUpdate(previous, updated),
|
|
389
|
-
sessionId,
|
|
390
|
-
task: updated,
|
|
391
|
-
previous,
|
|
392
|
-
context: { ...context, reason: outcome.reason ?? context.reason },
|
|
393
|
-
}),
|
|
394
|
-
]
|
|
395
|
-
: []);
|
|
396
|
-
if (!updated)
|
|
397
|
-
throw new Error(`No such task: ${id}`);
|
|
398
|
-
return { updated, total: all.length };
|
|
399
|
-
}
|
|
400
|
-
async readForRender(sessionId) {
|
|
401
|
-
return (await this.readProjected(sessionId)).tasks;
|
|
402
|
-
}
|
|
403
|
-
async readForMutateWithSource(sessionId) {
|
|
404
|
-
return this.readProjected(sessionId);
|
|
405
|
-
}
|
|
406
|
-
async readProjected(sessionId) {
|
|
407
|
-
const events = await this.readTaskEvents(sessionId);
|
|
408
|
-
const projection = projectTaskLedgerEvents(events);
|
|
409
|
-
if (projection.diagnostics.length > 0) {
|
|
410
|
-
throw new Error(`task event ledger has projection diagnostics: ${projection.diagnostics.join('; ')}`);
|
|
411
|
-
}
|
|
412
|
-
if (projection.tasks.length > TASK_LEDGER_MAX_TASKS) {
|
|
413
|
-
throw new Error(`task event ledger has ${projection.tasks.length} tasks, exceeding the ${TASK_LEDGER_MAX_TASKS}-task cap; refusing to load an unbounded ledger`);
|
|
414
|
-
}
|
|
415
|
-
return { tasks: projection.tasks };
|
|
416
|
-
}
|
|
417
|
-
async readTaskEvents(sessionId) {
|
|
418
|
-
return readSqliteTaskLedgerEvents(this.#lease.database, sessionId);
|
|
419
|
-
}
|
|
420
|
-
async readConversationCopyEvents(sessionId) {
|
|
421
|
-
return this.readTaskEvents(sessionId);
|
|
422
|
-
}
|
|
423
|
-
async mutate(sessionId, fn, eventsForMutation) {
|
|
424
|
-
let next = [];
|
|
425
|
-
await chainWrite(this.writeQueues, sessionId, async () => {
|
|
426
|
-
const currentRead = await this.readForMutateWithSource(sessionId);
|
|
427
|
-
const current = currentRead.tasks;
|
|
428
|
-
next = fn(current);
|
|
429
|
-
const mutationEvents = eventsForMutation(next);
|
|
430
|
-
await this.appendEvents(sessionId, mutationEvents);
|
|
431
|
-
this.emitChanged({
|
|
432
|
-
sessionId,
|
|
433
|
-
taskIds: [...new Set(mutationEvents.map((event) => event.taskId))],
|
|
434
|
-
at: Date.now(),
|
|
435
|
-
});
|
|
436
|
-
});
|
|
437
|
-
return next;
|
|
438
|
-
}
|
|
439
|
-
async appendEvents(sessionId, events) {
|
|
440
|
-
if (events.length === 0)
|
|
441
|
-
return;
|
|
442
|
-
this.#lease.transaction('write', () => {
|
|
443
|
-
for (const event of events)
|
|
444
|
-
insertTaskLedgerEvent(this.#lease.database, sessionId, event);
|
|
445
|
-
});
|
|
446
|
-
}
|
|
447
|
-
copyConversationLedger(sessionId, events) {
|
|
448
|
-
this.#lease.transaction('write', () => {
|
|
449
|
-
const existing = this.#lease.database
|
|
450
|
-
.prepare('SELECT COUNT(*) AS count FROM workflow_task_ledger_events WHERE session_id = ?')
|
|
451
|
-
.get(sessionId);
|
|
452
|
-
if (existing.count !== 0) {
|
|
453
|
-
throw new Error('Task Ledger conversation-copy target already exists');
|
|
454
|
-
}
|
|
455
|
-
for (const event of events)
|
|
456
|
-
insertTaskLedgerEvent(this.#lease.database, sessionId, event);
|
|
457
|
-
});
|
|
458
|
-
}
|
|
459
|
-
applyListOptions(tasks, options) {
|
|
460
|
-
const now = options.now ?? Date.now();
|
|
461
|
-
const filtered = tasks.filter((task) => {
|
|
462
|
-
if (options.status && task.status !== options.status)
|
|
463
|
-
return false;
|
|
464
|
-
if (options.includeTerminal === false && isTerminalTaskStatus(task.status))
|
|
465
|
-
return false;
|
|
466
|
-
if (options.includeArchived === false &&
|
|
467
|
-
isTerminalTaskStatus(task.status) &&
|
|
468
|
-
task.endedAt !== undefined &&
|
|
469
|
-
task.endedAt <= now - TASK_ARCHIVE_AFTER_MS)
|
|
470
|
-
return false;
|
|
471
|
-
return true;
|
|
472
|
-
});
|
|
473
|
-
if (options.classifyResumeTrust !== true)
|
|
474
|
-
return filtered;
|
|
475
|
-
return filtered.map((task) => ({
|
|
476
|
-
...task,
|
|
477
|
-
resumeTrust: task.resumeTrust ?? classifyTaskResumeTrust(task),
|
|
478
|
-
}));
|
|
479
|
-
}
|
|
480
|
-
emitChanged(event) {
|
|
481
|
-
for (const listener of this.listeners) {
|
|
482
|
-
try {
|
|
483
|
-
listener(event);
|
|
484
|
-
}
|
|
485
|
-
catch {
|
|
486
|
-
/* observers cannot perturb the ledger */
|
|
487
|
-
}
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
function readSqliteTaskLedgerEvents(database, sessionId) {
|
|
492
|
-
assertSafeSessionId(sessionId);
|
|
493
|
-
const rows = database
|
|
494
|
-
.prepare(`
|
|
495
|
-
SELECT record_json
|
|
496
|
-
FROM workflow_task_ledger_events
|
|
497
|
-
WHERE session_id = ?
|
|
498
|
-
ORDER BY sequence
|
|
499
|
-
`)
|
|
500
|
-
.all(sessionId);
|
|
501
|
-
return rows.map((row, index) => {
|
|
502
|
-
if (typeof row.record_json !== 'string') {
|
|
503
|
-
throw new Error(`Invalid SQLite task event at sequence ${index}`);
|
|
504
|
-
}
|
|
505
|
-
const parsed = JSON.parse(row.record_json);
|
|
506
|
-
if (!isTaskLedgerEvent(parsed) || parsed.sessionId !== sessionId) {
|
|
507
|
-
throw new Error(`Invalid SQLite task event at sequence ${index}`);
|
|
508
|
-
}
|
|
509
|
-
return parsed;
|
|
510
|
-
});
|
|
511
|
-
}
|
|
512
|
-
function insertTaskLedgerEvent(database, sessionId, event) {
|
|
513
|
-
const row = database
|
|
514
|
-
.prepare(`
|
|
515
|
-
SELECT COALESCE(MAX(sequence), -1) + 1 AS sequence
|
|
516
|
-
FROM workflow_task_ledger_events
|
|
517
|
-
WHERE session_id = ?
|
|
518
|
-
`)
|
|
519
|
-
.get(sessionId);
|
|
520
|
-
if (typeof row.sequence !== 'number' || !Number.isSafeInteger(row.sequence)) {
|
|
521
|
-
throw new Error('Invalid next task event sequence');
|
|
522
|
-
}
|
|
523
|
-
database
|
|
524
|
-
.prepare(`
|
|
525
|
-
INSERT INTO workflow_task_ledger_events(
|
|
526
|
-
session_id, sequence, event_id, record_json
|
|
527
|
-
) VALUES (?, ?, ?, ?)
|
|
528
|
-
`)
|
|
529
|
-
.run(sessionId, row.sequence, event.eventId, JSON.stringify(event));
|
|
530
|
-
}
|
|
531
|
-
function nextTaskKey(tasks, parent) {
|
|
532
|
-
const siblings = tasks.filter((task) => task.parentId === parent?.id);
|
|
533
|
-
const prefix = parent ? `${parent.key}.` : 'T';
|
|
534
|
-
const used = new Set(siblings.map((task) => task.key));
|
|
535
|
-
let index = 1;
|
|
536
|
-
while (used.has(`${prefix}${index}`))
|
|
537
|
-
index += 1;
|
|
538
|
-
const key = `${prefix}${index}`;
|
|
539
|
-
if (!isTaskKey(key))
|
|
540
|
-
throw new Error(`Task hierarchy is too deep to allocate a stable key under ${parent?.key ?? 'root'}`);
|
|
541
|
-
return key;
|
|
542
|
-
}
|
|
543
|
-
function assertChildTaskOwner(owner) {
|
|
544
|
-
if (owner.actor !== 'child_agent' || !owner.agentId || !owner.turnId || !isTaskOwner(owner)) {
|
|
545
|
-
throw new Error('Child task ownership requires stable child_agent agentId and turnId references');
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
|
-
function ownerFromContext(context) {
|
|
549
|
-
if (context.actor !== 'main_agent')
|
|
550
|
-
return undefined;
|
|
551
|
-
return {
|
|
552
|
-
actor: 'main_agent',
|
|
553
|
-
...(context.runId ? { runId: context.runId } : {}),
|
|
554
|
-
...(context.turnId ? { turnId: context.turnId } : {}),
|
|
555
|
-
};
|
|
556
|
-
}
|
|
557
|
-
function assertDescendantsTerminal(tasks, parentId) {
|
|
558
|
-
const pending = [parentId];
|
|
559
|
-
while (pending.length > 0) {
|
|
560
|
-
const current = pending.shift();
|
|
561
|
-
for (const child of tasks.filter((task) => task.parentId === current)) {
|
|
562
|
-
if (!isTerminalTaskStatus(child.status)) {
|
|
563
|
-
throw new Error(`Cannot complete a parent while descendant ${child.key} is ${child.status}`);
|
|
564
|
-
}
|
|
565
|
-
pending.push(child.id);
|
|
566
|
-
}
|
|
567
|
-
}
|
|
568
|
-
}
|
|
569
|
-
function normalizeOutcomeReason(value, fallback) {
|
|
570
|
-
const normalized = (value ?? fallback).normalize('NFC').replace(/\s+/g, ' ').trim();
|
|
571
|
-
return Array.from(normalized).slice(0, 1000).join('');
|
|
572
|
-
}
|
|
573
|
-
function clearStaleTaskEvidence(task) {
|
|
574
|
-
const next = { ...task };
|
|
575
|
-
if (next.status !== 'blocked')
|
|
576
|
-
delete next.blockedReason;
|
|
577
|
-
if (next.status !== 'failed')
|
|
578
|
-
delete next.failureReason;
|
|
579
|
-
if (next.status !== 'completed')
|
|
580
|
-
delete next.completionEvidence;
|
|
581
|
-
return next;
|
|
582
|
-
}
|
|
583
|
-
function rewriteConversationTaskEvent(event, sourceSessionId, targetSessionId, runIds, linkedChildren) {
|
|
584
|
-
const { owner, ...task } = event.task;
|
|
585
|
-
const snapshotChildOwner = linkedChildren === 'snapshot' && owner?.actor === 'child_agent';
|
|
586
|
-
const snapshotChildRun = linkedChildren === 'snapshot' && event.actor === 'child_agent';
|
|
587
|
-
const rewrittenOwner = owner === undefined
|
|
588
|
-
? undefined
|
|
589
|
-
: snapshotChildOwner
|
|
590
|
-
? undefined
|
|
591
|
-
: {
|
|
592
|
-
...owner,
|
|
593
|
-
...(owner.sessionId === sourceSessionId ? { sessionId: targetSessionId } : {}),
|
|
594
|
-
...(owner.runId
|
|
595
|
-
? {
|
|
596
|
-
runId: requiredConversationCopyRunId(runIds, owner.runId),
|
|
597
|
-
}
|
|
598
|
-
: {}),
|
|
599
|
-
};
|
|
600
|
-
const refs = event.refs === undefined
|
|
601
|
-
? undefined
|
|
602
|
-
: (() => {
|
|
603
|
-
const { runId: sourceRunId, ...preserved } = event.refs;
|
|
604
|
-
return {
|
|
605
|
-
...preserved,
|
|
606
|
-
...(sourceRunId && !snapshotChildRun
|
|
607
|
-
? { runId: requiredConversationCopyRunId(runIds, sourceRunId) }
|
|
608
|
-
: {}),
|
|
609
|
-
};
|
|
610
|
-
})();
|
|
611
|
-
return {
|
|
612
|
-
...event,
|
|
613
|
-
eventId: `task-copy-${createHash('sha256')
|
|
614
|
-
.update(JSON.stringify([targetSessionId, event.eventId]))
|
|
615
|
-
.digest('hex')}`,
|
|
616
|
-
sessionId: targetSessionId,
|
|
617
|
-
task: {
|
|
618
|
-
...task,
|
|
619
|
-
...(rewrittenOwner ? { owner: rewrittenOwner } : {}),
|
|
620
|
-
},
|
|
621
|
-
...(refs ? { refs } : {}),
|
|
622
|
-
};
|
|
623
|
-
}
|
|
624
|
-
function requiredConversationCopyRunId(runIds, sourceRunId) {
|
|
625
|
-
const targetRunId = runIds.get(sourceRunId);
|
|
626
|
-
if (!targetRunId) {
|
|
627
|
-
throw new Error(`Conversation copy is missing AgentRun ${sourceRunId}`);
|
|
628
|
-
}
|
|
629
|
-
return targetRunId;
|
|
630
|
-
}
|
|
631
|
-
function buildTaskLedgerEvent(input) {
|
|
632
|
-
return {
|
|
633
|
-
eventId: `task-event-${randomUUID()}`,
|
|
634
|
-
type: input.type,
|
|
635
|
-
ts: Date.now(),
|
|
636
|
-
sessionId: input.sessionId,
|
|
637
|
-
taskId: input.task.id,
|
|
638
|
-
...(input.previous ? { previousStatus: input.previous.status } : {}),
|
|
639
|
-
nextStatus: input.task.status,
|
|
640
|
-
task: input.task,
|
|
641
|
-
...((input.context.reason ?? eventReason(input.task))
|
|
642
|
-
? { reason: input.context.reason ?? eventReason(input.task) }
|
|
643
|
-
: {}),
|
|
644
|
-
...(eventEvidence(input.task) ? { evidence: eventEvidence(input.task) } : {}),
|
|
645
|
-
...(eventRefs(input.context) ? { refs: eventRefs(input.context) } : {}),
|
|
646
|
-
...(input.context.source ? { source: input.context.source } : {}),
|
|
647
|
-
...(input.context.actor ? { actor: input.context.actor } : {}),
|
|
648
|
-
};
|
|
649
|
-
}
|
|
650
|
-
function eventReason(task) {
|
|
651
|
-
return task.blockedReason ?? task.failureReason;
|
|
652
|
-
}
|
|
653
|
-
function eventEvidence(task) {
|
|
654
|
-
return task.completionEvidence;
|
|
655
|
-
}
|
|
656
|
-
function eventRefs(context) {
|
|
657
|
-
const refs = {
|
|
658
|
-
...(context.runId ? { runId: context.runId } : {}),
|
|
659
|
-
...(context.turnId ? { turnId: context.turnId } : {}),
|
|
660
|
-
...(context.toolCallId ? { toolCallId: context.toolCallId } : {}),
|
|
661
|
-
};
|
|
662
|
-
return Object.keys(refs).length === 0 ? undefined : refs;
|
|
663
|
-
}
|