@botbotgo/agent-harness 0.0.268 → 0.0.270
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 +3 -2
- package/README.zh.md +3 -2
- package/dist/acp.js +2 -2
- package/dist/api.d.ts +6 -11
- package/dist/api.js +20 -23
- package/dist/contracts/runtime.d.ts +42 -70
- package/dist/flow/build-flow-graph.js +29 -45
- package/dist/flow/types.d.ts +0 -6
- package/dist/package-version.d.ts +1 -1
- package/dist/package-version.js +1 -1
- package/dist/persistence/file-store.js +24 -17
- package/dist/persistence/sqlite-run-context-store.js +2 -2
- package/dist/persistence/sqlite-store.js +18 -16
- package/dist/protocol/a2a/http.js +69 -46
- package/dist/protocol/ag-ui/http.js +9 -9
- package/dist/runtime/adapter/invocation-result.js +2 -2
- package/dist/runtime/adapter/tool/tool-hitl.d.ts +3 -1
- package/dist/runtime/adapter/tool/tool-hitl.js +75 -6
- package/dist/runtime/harness/events/listener-runtime.d.ts +2 -2
- package/dist/runtime/harness/events/streaming.d.ts +8 -8
- package/dist/runtime/harness/events/streaming.js +10 -10
- package/dist/runtime/harness/events/timeline.js +4 -4
- package/dist/runtime/harness/run/governance.js +33 -4
- package/dist/runtime/harness/run/helpers.js +2 -2
- package/dist/runtime/harness/run/operator-overview.js +6 -0
- package/dist/runtime/harness/run/recovery.js +20 -20
- package/dist/runtime/harness/run/resume.js +3 -3
- package/dist/runtime/harness/run/run-lifecycle.js +5 -5
- package/dist/runtime/harness/run/run-operations.d.ts +2 -2
- package/dist/runtime/harness/run/run-operations.js +21 -21
- package/dist/runtime/harness/run/start-run.d.ts +3 -3
- package/dist/runtime/harness/run/start-run.js +3 -3
- package/dist/runtime/harness/run/startup-runtime.js +1 -1
- package/dist/runtime/harness/run/stream-run.js +37 -27
- package/dist/runtime/harness/run/thread-records.js +12 -33
- package/dist/runtime/harness/system/mem0-ingestion-sync.js +2 -2
- package/dist/runtime/harness/system/runtime-memory-manager.js +4 -4
- package/dist/runtime/harness/system/runtime-memory-records.js +6 -6
- package/dist/runtime/harness/system/runtime-memory-sync.js +6 -4
- package/dist/runtime/harness/system/thread-memory-sync.js +7 -5
- package/dist/runtime/harness.d.ts +2 -2
- package/dist/runtime/harness.js +161 -156
- package/dist/runtime/support/harness-support.js +4 -4
- package/package.json +1 -1
|
@@ -29,6 +29,8 @@ function projectGovernanceDiagnostics(runs) {
|
|
|
29
29
|
const toolPolicies = collectGovernanceToolPolicies(runs);
|
|
30
30
|
const highRiskTools = toolPolicies.filter((tool) => tool.risk === "high").length;
|
|
31
31
|
const approvalRequiredTools = toolPolicies.filter((tool) => tool.requiresApproval).length;
|
|
32
|
+
const autoApprovedTools = toolPolicies.filter((tool) => tool.decisionMode === "auto-approve").length;
|
|
33
|
+
const autoRejectedTools = toolPolicies.filter((tool) => tool.decisionMode === "auto-reject" || tool.decisionMode === "deny-and-continue").length;
|
|
32
34
|
const untrustedMcpTools = toolPolicies.filter((tool) => tool.mcpTrustTier === "untrusted").length;
|
|
33
35
|
const crossTenantTools = toolPolicies.filter((tool) => tool.tenantScope === "cross-tenant").length;
|
|
34
36
|
const writeAccessMcpTools = toolPolicies.filter((tool) => tool.category === "mcp" && tool.mcpAccess === "read-write").length;
|
|
@@ -36,11 +38,15 @@ function projectGovernanceDiagnostics(runs) {
|
|
|
36
38
|
runsWithGovernance: runs.filter((run) => (run.runtimeSnapshot?.governance?.bundles?.length ?? 0) > 0).length,
|
|
37
39
|
highRiskTools,
|
|
38
40
|
approvalRequiredTools,
|
|
41
|
+
autoApprovedTools,
|
|
42
|
+
autoRejectedTools,
|
|
39
43
|
untrustedMcpTools,
|
|
40
44
|
crossTenantTools,
|
|
41
45
|
writeAccessMcpTools,
|
|
42
46
|
summary: `highRisk=${highRiskTools} ` +
|
|
43
47
|
`approvalRequired=${approvalRequiredTools} ` +
|
|
48
|
+
`autoApproved=${autoApprovedTools} ` +
|
|
49
|
+
`autoRejected=${autoRejectedTools} ` +
|
|
44
50
|
`untrusted=${untrustedMcpTools} ` +
|
|
45
51
|
`crossTenant=${crossTenantTools}`,
|
|
46
52
|
};
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { normalizeRunPriority } from "./helpers.js";
|
|
2
2
|
async function failRecovery(context, thread, previousState, error, options = {}) {
|
|
3
|
-
await context.setRunStateAndEmit(thread.
|
|
3
|
+
await context.setRunStateAndEmit(thread.sessionId, thread.latestRequestId, 100, "failed", {
|
|
4
4
|
previousState,
|
|
5
5
|
error,
|
|
6
6
|
});
|
|
7
7
|
if (options.releaseRunClaim) {
|
|
8
|
-
await context.persistence.releaseRunClaim(thread.
|
|
8
|
+
await context.persistence.releaseRunClaim(thread.latestRequestId);
|
|
9
9
|
}
|
|
10
10
|
return true;
|
|
11
11
|
}
|
|
12
12
|
async function resolveRecoveryBinding(context, thread) {
|
|
13
|
-
const runMeta = await context.persistence.getRunMeta(thread.
|
|
13
|
+
const runMeta = await context.persistence.getRunMeta(thread.sessionId, thread.latestRequestId);
|
|
14
14
|
const binding = context.getBinding(runMeta.agentId);
|
|
15
15
|
if (!binding) {
|
|
16
16
|
return null;
|
|
@@ -22,12 +22,12 @@ async function resolveRecoveryBinding(context, thread) {
|
|
|
22
22
|
}
|
|
23
23
|
async function executeRecoveredRun(context, input) {
|
|
24
24
|
const { thread, binding, agentId, request, previousState, emitResumeEvent } = input;
|
|
25
|
-
const releaseRunSlot = await context.acquireRunSlot(thread.
|
|
25
|
+
const releaseRunSlot = await context.acquireRunSlot(thread.sessionId, thread.latestRequestId, "running", normalizeRunPriority(request.priority));
|
|
26
26
|
try {
|
|
27
27
|
if (emitResumeEvent) {
|
|
28
|
-
await context.emit(thread.
|
|
28
|
+
await context.emit(thread.sessionId, thread.latestRequestId, emitResumeEvent.sequence, emitResumeEvent.eventType, emitResumeEvent.payload);
|
|
29
29
|
}
|
|
30
|
-
await context.executeQueuedRun(binding, request.input, thread.
|
|
30
|
+
await context.executeQueuedRun(binding, request.input, thread.sessionId, thread.latestRequestId, agentId, {
|
|
31
31
|
context: request.invocation?.context,
|
|
32
32
|
state: request.invocation?.inputs,
|
|
33
33
|
files: request.invocation?.attachments,
|
|
@@ -42,9 +42,9 @@ async function executeRecoveredRun(context, input) {
|
|
|
42
42
|
return true;
|
|
43
43
|
}
|
|
44
44
|
async function failResumingRecovery(context, thread, checkpointRef, error) {
|
|
45
|
-
await context.persistence.setRunState(thread.
|
|
46
|
-
await context.persistence.clearRecoveryIntent(thread.
|
|
47
|
-
await context.emit(thread.
|
|
45
|
+
await context.persistence.setRunState(thread.sessionId, thread.latestRequestId, "failed", checkpointRef);
|
|
46
|
+
await context.persistence.clearRecoveryIntent(thread.sessionId, thread.latestRequestId);
|
|
47
|
+
await context.emit(thread.sessionId, thread.latestRequestId, 101, "run.state.changed", {
|
|
48
48
|
previousState: "resuming",
|
|
49
49
|
state: "failed",
|
|
50
50
|
checkpointRef,
|
|
@@ -60,7 +60,7 @@ export async function recoverQueuedStartupRun(context, thread) {
|
|
|
60
60
|
if (!resolved) {
|
|
61
61
|
return true;
|
|
62
62
|
}
|
|
63
|
-
const request = await context.persistence.getRunRequest(thread.
|
|
63
|
+
const request = await context.persistence.getRunRequest(thread.sessionId, thread.latestRequestId);
|
|
64
64
|
if (!request) {
|
|
65
65
|
return failRecovery(context, thread, "queued", "missing persisted run request for queued run recovery");
|
|
66
66
|
}
|
|
@@ -89,7 +89,7 @@ export async function recoverRunningStartupRun(context, thread) {
|
|
|
89
89
|
releaseRunClaim: true,
|
|
90
90
|
});
|
|
91
91
|
}
|
|
92
|
-
const request = await context.persistence.getRunRequest(thread.
|
|
92
|
+
const request = await context.persistence.getRunRequest(thread.sessionId, thread.latestRequestId);
|
|
93
93
|
if (!request) {
|
|
94
94
|
return failRecovery(context, thread, "running", "missing persisted run request for stale running run recovery", {
|
|
95
95
|
releaseRunClaim: true,
|
|
@@ -119,31 +119,31 @@ export async function recoverResumingStartupRun(context, thread) {
|
|
|
119
119
|
if (!binding) {
|
|
120
120
|
return true;
|
|
121
121
|
}
|
|
122
|
-
const recoveryIntent = await context.persistence.getRecoveryIntent(thread.
|
|
122
|
+
const recoveryIntent = await context.persistence.getRecoveryIntent(thread.sessionId, thread.latestRequestId);
|
|
123
123
|
if (!recoveryIntent || recoveryIntent.kind !== "approval-decision") {
|
|
124
124
|
return true;
|
|
125
125
|
}
|
|
126
126
|
if (recoveryIntent.attempts >= context.recoveryConfig.maxRecoveryAttempts) {
|
|
127
127
|
return failResumingRecovery(context, thread, recoveryIntent.checkpointRef, "recovery attempts exhausted");
|
|
128
128
|
}
|
|
129
|
-
await context.persistence.saveRecoveryIntent(thread.
|
|
129
|
+
await context.persistence.saveRecoveryIntent(thread.sessionId, thread.latestRequestId, {
|
|
130
130
|
...recoveryIntent,
|
|
131
131
|
attempts: recoveryIntent.attempts + 1,
|
|
132
132
|
});
|
|
133
|
-
await context.emit(thread.
|
|
133
|
+
await context.emit(thread.sessionId, thread.latestRequestId, 100, "run.resumed", {
|
|
134
134
|
resumeKind: "startup-recovery",
|
|
135
135
|
checkpointRef: recoveryIntent.checkpointRef,
|
|
136
136
|
state: "resuming",
|
|
137
137
|
});
|
|
138
|
-
const history = await context.persistence.listThreadMessages(thread.
|
|
139
|
-
const priorHistory = history.filter((message) => message.
|
|
140
|
-
const runInput = await context.loadRunInput(thread.
|
|
138
|
+
const history = await context.persistence.listThreadMessages(thread.sessionId);
|
|
139
|
+
const priorHistory = history.filter((message) => message.requestId !== thread.latestRequestId);
|
|
140
|
+
const runInput = await context.loadRunInput(thread.sessionId, thread.latestRequestId);
|
|
141
141
|
const startedAt = Date.now();
|
|
142
142
|
try {
|
|
143
|
-
const actual = await context.runtimeAdapter.invoke(binding, "", thread.
|
|
143
|
+
const actual = await context.runtimeAdapter.invoke(binding, "", thread.sessionId, thread.latestRequestId, recoveryIntent.resumePayload, priorHistory);
|
|
144
144
|
context.recordLlmSuccess(startedAt);
|
|
145
|
-
await context.persistence.clearRecoveryIntent(thread.
|
|
146
|
-
await context.finalizeContinuedRun(binding, thread.
|
|
145
|
+
await context.persistence.clearRecoveryIntent(thread.sessionId, thread.latestRequestId);
|
|
146
|
+
await context.finalizeContinuedRun(binding, thread.sessionId, thread.latestRequestId, runInput, actual, {
|
|
147
147
|
previousState: "resuming",
|
|
148
148
|
stateSequence: 101,
|
|
149
149
|
approvalSequence: 102,
|
|
@@ -7,13 +7,13 @@ export async function resolveApprovalRecord(persistence, options, thread) {
|
|
|
7
7
|
}
|
|
8
8
|
return approval;
|
|
9
9
|
}
|
|
10
|
-
const
|
|
11
|
-
const approvals = await persistence.getRunApprovals(options.
|
|
10
|
+
const requestId = options.requestId ?? thread.latestRequestId;
|
|
11
|
+
const approvals = await persistence.getRunApprovals(options.sessionId ?? thread.sessionId, requestId);
|
|
12
12
|
const approval = approvals
|
|
13
13
|
.filter((candidate) => candidate.status === "pending")
|
|
14
14
|
.sort((left, right) => right.requestedAt.localeCompare(left.requestedAt))[0];
|
|
15
15
|
if (!approval) {
|
|
16
|
-
throw new Error(`No pending approval for run ${
|
|
16
|
+
throw new Error(`No pending approval for run ${requestId}`);
|
|
17
17
|
}
|
|
18
18
|
return approval;
|
|
19
19
|
}
|
|
@@ -5,7 +5,7 @@ export async function appendAssistantMessage(persistence, threadId, runId, conte
|
|
|
5
5
|
await persistence.appendThreadMessage(threadId, {
|
|
6
6
|
role: "assistant",
|
|
7
7
|
content,
|
|
8
|
-
runId,
|
|
8
|
+
requestId: runId,
|
|
9
9
|
createdAt: new Date().toISOString(),
|
|
10
10
|
});
|
|
11
11
|
}
|
|
@@ -38,8 +38,8 @@ export async function finalizeCancelledRun(runtime, threadId, runId, previousSta
|
|
|
38
38
|
});
|
|
39
39
|
const runMeta = await runtime.persistence.getRunMeta(threadId, runId);
|
|
40
40
|
return {
|
|
41
|
-
threadId,
|
|
42
|
-
runId,
|
|
41
|
+
sessionId: threadId,
|
|
42
|
+
requestId: runId,
|
|
43
43
|
agentId: runMeta.agentId,
|
|
44
44
|
state: "cancelled",
|
|
45
45
|
output: reason ? `cancelled: ${reason}` : "cancelled",
|
|
@@ -58,8 +58,8 @@ export async function finalizeContinuedRun(runtime, binding, threadId, runId, in
|
|
|
58
58
|
}
|
|
59
59
|
return {
|
|
60
60
|
...actual,
|
|
61
|
-
threadId,
|
|
62
|
-
runId,
|
|
61
|
+
sessionId: threadId,
|
|
62
|
+
requestId: runId,
|
|
63
63
|
approvalId: approval?.approvalId ?? actual.approvalId,
|
|
64
64
|
pendingActionId: approval?.pendingActionId ?? actual.pendingActionId,
|
|
65
65
|
};
|
|
@@ -31,8 +31,8 @@ type ResumeOperationRuntime = {
|
|
|
31
31
|
};
|
|
32
32
|
type CancelOperationRuntime = {
|
|
33
33
|
getRun: (runId: string) => Promise<{
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
sessionId: string;
|
|
35
|
+
requestId: string;
|
|
36
36
|
agentId: string;
|
|
37
37
|
state: RunResult["state"];
|
|
38
38
|
} | null>;
|
|
@@ -11,17 +11,17 @@ function normalizeContinuationState(previousState) {
|
|
|
11
11
|
}
|
|
12
12
|
export async function resumeRun(runtime, options) {
|
|
13
13
|
const approvalById = options.approvalId ? await runtime.getApprovalById(options.approvalId) : null;
|
|
14
|
-
const thread = options.
|
|
15
|
-
? await runtime.getSession(options.
|
|
14
|
+
const thread = options.sessionId
|
|
15
|
+
? await runtime.getSession(options.sessionId)
|
|
16
16
|
: approvalById
|
|
17
|
-
? await runtime.getSession(approvalById.
|
|
17
|
+
? await runtime.getSession(approvalById.sessionId)
|
|
18
18
|
: null;
|
|
19
19
|
if (!thread) {
|
|
20
|
-
throw new Error("resume requires either
|
|
20
|
+
throw new Error("resume requires either sessionId or approvalId");
|
|
21
21
|
}
|
|
22
22
|
const approval = approvalById ?? await runtime.resolveApprovalRecord(options, thread);
|
|
23
|
-
const threadId = approval.
|
|
24
|
-
const runId = approval.
|
|
23
|
+
const threadId = approval.sessionId;
|
|
24
|
+
const runId = approval.requestId;
|
|
25
25
|
const binding = runtime.getBinding(thread.agentId);
|
|
26
26
|
if (!binding) {
|
|
27
27
|
throw new Error(`Unknown agent ${thread.agentId}`);
|
|
@@ -63,7 +63,7 @@ export async function resumeRun(runtime, options) {
|
|
|
63
63
|
toolName: approval.toolName,
|
|
64
64
|
});
|
|
65
65
|
const history = await runtime.listThreadMessages(threadId);
|
|
66
|
-
const priorHistory = history.filter((message) => message.
|
|
66
|
+
const priorHistory = history.filter((message) => message.requestId !== runId);
|
|
67
67
|
const runInput = await runtime.loadRunInput(threadId, runId);
|
|
68
68
|
const startedAt = Date.now();
|
|
69
69
|
try {
|
|
@@ -101,33 +101,33 @@ export async function resumeRun(runtime, options) {
|
|
|
101
101
|
}
|
|
102
102
|
}
|
|
103
103
|
export async function cancelRunOperation(runtime, options) {
|
|
104
|
-
const run = await runtime.getRun(options.
|
|
104
|
+
const run = await runtime.getRun(options.requestId);
|
|
105
105
|
if (!run) {
|
|
106
|
-
throw new Error(`Unknown run ${options.
|
|
106
|
+
throw new Error(`Unknown run ${options.requestId}`);
|
|
107
107
|
}
|
|
108
108
|
if (isTerminalRunState(run.state)) {
|
|
109
109
|
return {
|
|
110
|
-
|
|
111
|
-
|
|
110
|
+
sessionId: run.sessionId,
|
|
111
|
+
requestId: run.requestId,
|
|
112
112
|
agentId: run.agentId,
|
|
113
113
|
state: run.state,
|
|
114
114
|
output: run.state,
|
|
115
115
|
};
|
|
116
116
|
}
|
|
117
|
-
await runtime.requestRunCancel(run.
|
|
117
|
+
await runtime.requestRunCancel(run.requestId, options.reason);
|
|
118
118
|
if (run.state === "queued" || run.state === "waiting_for_approval" || run.state === "claimed") {
|
|
119
119
|
if (run.state === "queued") {
|
|
120
|
-
runtime.dropPendingRunSlot(run.
|
|
120
|
+
runtime.dropPendingRunSlot(run.requestId);
|
|
121
121
|
}
|
|
122
|
-
return runtime.finalizeCancelledRun(run.
|
|
122
|
+
return runtime.finalizeCancelledRun(run.sessionId, run.requestId, run.state, options.reason);
|
|
123
123
|
}
|
|
124
|
-
await runtime.setRunStateAndEmit(run.
|
|
124
|
+
await runtime.setRunStateAndEmit(run.sessionId, run.requestId, 103, "cancelling", {
|
|
125
125
|
previousState: run.state,
|
|
126
126
|
...(options.reason ? { error: options.reason } : {}),
|
|
127
127
|
});
|
|
128
128
|
return {
|
|
129
|
-
|
|
130
|
-
|
|
129
|
+
sessionId: run.sessionId,
|
|
130
|
+
requestId: run.requestId,
|
|
131
131
|
agentId: run.agentId,
|
|
132
132
|
state: "cancelling",
|
|
133
133
|
output: options.reason ? `cancelling: ${options.reason}` : "cancelling",
|
|
@@ -140,8 +140,8 @@ export async function executeQueuedRunOperation(runtime, input) {
|
|
|
140
140
|
const currentRun = await runtime.persistence.getRun(runId);
|
|
141
141
|
if (currentRun?.state === "cancelled") {
|
|
142
142
|
return {
|
|
143
|
-
threadId,
|
|
144
|
-
runId,
|
|
143
|
+
sessionId: threadId,
|
|
144
|
+
requestId: runId,
|
|
145
145
|
agentId,
|
|
146
146
|
state: "cancelled",
|
|
147
147
|
output: "cancelled",
|
|
@@ -202,8 +202,8 @@ export async function executeQueuedRunOperation(runtime, input) {
|
|
|
202
202
|
error: error instanceof Error ? error.message : String(error),
|
|
203
203
|
});
|
|
204
204
|
return {
|
|
205
|
-
threadId,
|
|
206
|
-
runId,
|
|
205
|
+
sessionId: threadId,
|
|
206
|
+
requestId: runId,
|
|
207
207
|
agentId,
|
|
208
208
|
state: "failed",
|
|
209
209
|
output: runtime.renderRuntimeFailure(error),
|
|
@@ -15,7 +15,7 @@ type EnsureThreadStartedRuntime = {
|
|
|
15
15
|
userMessage: {
|
|
16
16
|
role: "user";
|
|
17
17
|
content: MessageContent;
|
|
18
|
-
|
|
18
|
+
requestId: string;
|
|
19
19
|
createdAt: string;
|
|
20
20
|
};
|
|
21
21
|
runRequest: PersistedRunRequest;
|
|
@@ -35,7 +35,7 @@ type EnsureThreadStartedRuntime = {
|
|
|
35
35
|
appendThreadMessage: (threadId: string, message: {
|
|
36
36
|
role: "user";
|
|
37
37
|
content: MessageContent;
|
|
38
|
-
|
|
38
|
+
requestId: string;
|
|
39
39
|
createdAt: string;
|
|
40
40
|
}) => Promise<void>;
|
|
41
41
|
createRun: (input: {
|
|
@@ -73,7 +73,7 @@ export declare function ensureThreadStarted(runtime: EnsureThreadStartedRuntime,
|
|
|
73
73
|
isNewThread: boolean;
|
|
74
74
|
}>;
|
|
75
75
|
export declare function prepareRunStart(runtime: PrepareRunStartRuntime, input: {
|
|
76
|
-
options: Pick<RunStartOptions, "input" | "agentId" | "
|
|
76
|
+
options: Pick<RunStartOptions, "input" | "agentId" | "sessionId" | "priority">;
|
|
77
77
|
invocation: InvocationEnvelope;
|
|
78
78
|
runCreatedPayload: (binding: Binding, selectedAgentId: string) => Record<string, unknown>;
|
|
79
79
|
}): Promise<{
|
|
@@ -18,7 +18,7 @@ export async function ensureThreadStarted(runtime, input) {
|
|
|
18
18
|
const userMessage = {
|
|
19
19
|
role: "user",
|
|
20
20
|
content: normalizeMessageContent(message),
|
|
21
|
-
runId,
|
|
21
|
+
requestId: runId,
|
|
22
22
|
createdAt,
|
|
23
23
|
};
|
|
24
24
|
if (typeof runtime.persistence.bootstrapRun === "function") {
|
|
@@ -70,7 +70,7 @@ export async function ensureThreadStarted(runtime, input) {
|
|
|
70
70
|
}
|
|
71
71
|
export async function prepareRunStart(runtime, input) {
|
|
72
72
|
const { options, invocation, runCreatedPayload } = input;
|
|
73
|
-
const selectedAgentId = await runtime.resolveSelectedAgentId(options.input, options.agentId, options.
|
|
73
|
+
const selectedAgentId = await runtime.resolveSelectedAgentId(options.input, options.agentId, options.sessionId);
|
|
74
74
|
const binding = getRequiredWorkspaceBinding(runtime.workspace, selectedAgentId);
|
|
75
75
|
const policyDecision = runtime.policyEngine.evaluate(binding);
|
|
76
76
|
if (!policyDecision.allowed) {
|
|
@@ -83,7 +83,7 @@ export async function prepareRunStart(runtime, input) {
|
|
|
83
83
|
binding,
|
|
84
84
|
message: options.input,
|
|
85
85
|
runRequest,
|
|
86
|
-
existingThreadId: options.
|
|
86
|
+
existingThreadId: options.sessionId,
|
|
87
87
|
});
|
|
88
88
|
return {
|
|
89
89
|
binding,
|
|
@@ -54,7 +54,7 @@ export async function reclaimExpiredClaimedRuns(input, nowIso = new Date().toISO
|
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
export async function isStaleRunningRun(input, thread, nowMs = Date.now()) {
|
|
57
|
-
const control = await input.persistence.getRunControl(thread.
|
|
57
|
+
const control = await input.persistence.getRunControl(thread.latestRequestId);
|
|
58
58
|
const heartbeatAt = control?.heartbeatAt;
|
|
59
59
|
if (!heartbeatAt) {
|
|
60
60
|
return true;
|
|
@@ -101,8 +101,8 @@ export async function* streamHarnessRun(options) {
|
|
|
101
101
|
});
|
|
102
102
|
yield {
|
|
103
103
|
type: "upstream-event",
|
|
104
|
-
|
|
105
|
-
|
|
104
|
+
sessionId: options.threadId,
|
|
105
|
+
requestId: options.runId,
|
|
106
106
|
event: normalizedChunk.event,
|
|
107
107
|
};
|
|
108
108
|
continue;
|
|
@@ -116,8 +116,8 @@ export async function* streamHarnessRun(options) {
|
|
|
116
116
|
});
|
|
117
117
|
yield {
|
|
118
118
|
type: "upstream-event",
|
|
119
|
-
|
|
120
|
-
|
|
119
|
+
sessionId: options.threadId,
|
|
120
|
+
requestId: options.runId,
|
|
121
121
|
surfaceItem,
|
|
122
122
|
event: normalizedChunk.event,
|
|
123
123
|
};
|
|
@@ -137,8 +137,8 @@ export async function* streamHarnessRun(options) {
|
|
|
137
137
|
yield {
|
|
138
138
|
type: "result",
|
|
139
139
|
result: {
|
|
140
|
-
|
|
141
|
-
|
|
140
|
+
sessionId: options.threadId,
|
|
141
|
+
requestId: options.runId,
|
|
142
142
|
agentId: currentAgentId,
|
|
143
143
|
state: "waiting_for_approval",
|
|
144
144
|
output: assistantOutput,
|
|
@@ -164,8 +164,8 @@ export async function* streamHarnessRun(options) {
|
|
|
164
164
|
}
|
|
165
165
|
yield {
|
|
166
166
|
type: "tool-result",
|
|
167
|
-
|
|
168
|
-
|
|
167
|
+
sessionId: options.threadId,
|
|
168
|
+
requestId: options.runId,
|
|
169
169
|
agentId: options.selectedAgentId,
|
|
170
170
|
toolName: normalizedChunk.toolName,
|
|
171
171
|
output: normalizedChunk.output,
|
|
@@ -181,8 +181,8 @@ export async function* streamHarnessRun(options) {
|
|
|
181
181
|
};
|
|
182
182
|
yield {
|
|
183
183
|
type: "content",
|
|
184
|
-
|
|
185
|
-
|
|
184
|
+
sessionId: options.threadId,
|
|
185
|
+
requestId: options.runId,
|
|
186
186
|
agentId: currentAgentId,
|
|
187
187
|
content: normalizedChunk.content,
|
|
188
188
|
};
|
|
@@ -212,8 +212,8 @@ export async function* streamHarnessRun(options) {
|
|
|
212
212
|
yield {
|
|
213
213
|
type: "result",
|
|
214
214
|
result: {
|
|
215
|
-
|
|
216
|
-
|
|
215
|
+
sessionId: options.threadId,
|
|
216
|
+
requestId: options.runId,
|
|
217
217
|
agentId: currentAgentId,
|
|
218
218
|
state: "completed",
|
|
219
219
|
output: assistantOutput,
|
|
@@ -236,16 +236,16 @@ export async function* streamHarnessRun(options) {
|
|
|
236
236
|
};
|
|
237
237
|
yield {
|
|
238
238
|
type: "content",
|
|
239
|
-
|
|
240
|
-
|
|
239
|
+
sessionId: options.threadId,
|
|
240
|
+
requestId: options.runId,
|
|
241
241
|
agentId: options.selectedAgentId,
|
|
242
242
|
content: runtimeFailure,
|
|
243
243
|
};
|
|
244
244
|
yield {
|
|
245
245
|
type: "result",
|
|
246
246
|
result: {
|
|
247
|
-
|
|
248
|
-
|
|
247
|
+
sessionId: options.threadId,
|
|
248
|
+
requestId: options.runId,
|
|
249
249
|
agentId: currentAgentId,
|
|
250
250
|
state: "failed",
|
|
251
251
|
output: runtimeFailure,
|
|
@@ -265,16 +265,16 @@ export async function* streamHarnessRun(options) {
|
|
|
265
265
|
};
|
|
266
266
|
yield {
|
|
267
267
|
type: "content",
|
|
268
|
-
|
|
269
|
-
|
|
268
|
+
sessionId: options.threadId,
|
|
269
|
+
requestId: options.runId,
|
|
270
270
|
agentId: options.selectedAgentId,
|
|
271
271
|
content: runtimeFailure,
|
|
272
272
|
};
|
|
273
273
|
yield {
|
|
274
274
|
type: "result",
|
|
275
275
|
result: {
|
|
276
|
-
|
|
277
|
-
|
|
276
|
+
sessionId: options.threadId,
|
|
277
|
+
requestId: options.runId,
|
|
278
278
|
agentId: options.selectedAgentId,
|
|
279
279
|
state: "failed",
|
|
280
280
|
output: runtimeFailure,
|
|
@@ -306,9 +306,19 @@ export async function* streamHarnessRun(options) {
|
|
|
306
306
|
yield {
|
|
307
307
|
type: "result",
|
|
308
308
|
result: {
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
309
|
+
state: actual.state,
|
|
310
|
+
output: actual.output,
|
|
311
|
+
finalMessageText: actual.finalMessageText,
|
|
312
|
+
outputContent: actual.outputContent,
|
|
313
|
+
contentBlocks: actual.contentBlocks,
|
|
314
|
+
structuredResponse: actual.structuredResponse,
|
|
315
|
+
interruptContent: actual.interruptContent,
|
|
316
|
+
approvalId: actual.approvalId,
|
|
317
|
+
pendingActionId: actual.pendingActionId,
|
|
318
|
+
delegationId: actual.delegationId,
|
|
319
|
+
artifacts: actual.artifacts,
|
|
320
|
+
sessionId: options.threadId,
|
|
321
|
+
requestId: options.runId,
|
|
312
322
|
agentId: currentAgentId,
|
|
313
323
|
metadata: {
|
|
314
324
|
...(actual.metadata ?? {}),
|
|
@@ -336,16 +346,16 @@ export async function* streamHarnessRun(options) {
|
|
|
336
346
|
};
|
|
337
347
|
yield {
|
|
338
348
|
type: "content",
|
|
339
|
-
|
|
340
|
-
|
|
349
|
+
sessionId: options.threadId,
|
|
350
|
+
requestId: options.runId,
|
|
341
351
|
agentId: options.selectedAgentId,
|
|
342
352
|
content: runtimeFailure,
|
|
343
353
|
};
|
|
344
354
|
yield {
|
|
345
355
|
type: "result",
|
|
346
356
|
result: {
|
|
347
|
-
|
|
348
|
-
|
|
357
|
+
sessionId: options.threadId,
|
|
358
|
+
requestId: options.runId,
|
|
349
359
|
agentId: currentAgentId,
|
|
350
360
|
state: "failed",
|
|
351
361
|
output: runtimeFailure,
|
|
@@ -6,11 +6,11 @@ function selectLatestPendingApproval(approvals) {
|
|
|
6
6
|
.sort((left, right) => right.requestedAt.localeCompare(left.requestedAt))[0];
|
|
7
7
|
}
|
|
8
8
|
export async function buildRequestInspectionRecord(persistence, request) {
|
|
9
|
-
const inspection = await persistence.getRunInspection(request.
|
|
10
|
-
const runtimeEvents = await persistence.listRunEvents(request.
|
|
9
|
+
const inspection = await persistence.getRunInspection(request.sessionId, request.requestId);
|
|
10
|
+
const runtimeEvents = await persistence.listRunEvents(request.sessionId, request.requestId);
|
|
11
11
|
return {
|
|
12
|
-
requestId: request.
|
|
13
|
-
sessionId: request.
|
|
12
|
+
requestId: request.requestId,
|
|
13
|
+
sessionId: request.sessionId,
|
|
14
14
|
agentId: request.agentId,
|
|
15
15
|
parentRunId: request.parentRunId,
|
|
16
16
|
executionMode: request.executionMode,
|
|
@@ -28,15 +28,15 @@ export async function buildRequestInspectionRecord(persistence, request) {
|
|
|
28
28
|
runtimeSnapshot: request.runtimeSnapshot,
|
|
29
29
|
traceItems: inspection.traceItems,
|
|
30
30
|
runtimeTimeline: projectRuntimeTimeline(runtimeEvents, {
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
sessionId: request.sessionId,
|
|
32
|
+
requestId: request.requestId,
|
|
33
33
|
}),
|
|
34
34
|
};
|
|
35
35
|
}
|
|
36
36
|
function toRunRecord(request) {
|
|
37
37
|
return {
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
requestId: request.requestId,
|
|
39
|
+
sessionId: request.sessionId,
|
|
40
40
|
agentId: request.agentId,
|
|
41
41
|
parentRunId: request.parentRunId,
|
|
42
42
|
executionMode: request.executionMode,
|
|
@@ -97,32 +97,11 @@ export async function buildThreadInspectionRecord(input, threadId) {
|
|
|
97
97
|
getSession: async (sessionId) => {
|
|
98
98
|
const summary = await input.getSession(sessionId);
|
|
99
99
|
return summary
|
|
100
|
-
? {
|
|
101
|
-
agentId: summary.agentId,
|
|
102
|
-
sessionId: summary.threadId,
|
|
103
|
-
latestRequestId: summary.latestRunId,
|
|
104
|
-
createdAt: summary.createdAt,
|
|
105
|
-
updatedAt: summary.updatedAt,
|
|
106
|
-
status: summary.status,
|
|
107
|
-
currentAgentId: summary.currentAgentId,
|
|
108
|
-
}
|
|
100
|
+
? { ...summary }
|
|
109
101
|
: null;
|
|
110
102
|
},
|
|
111
103
|
}, threadId);
|
|
112
|
-
return session
|
|
113
|
-
? {
|
|
114
|
-
threadId: session.sessionId,
|
|
115
|
-
entryAgentId: session.entryAgentId,
|
|
116
|
-
currentAgentId: session.currentAgentId,
|
|
117
|
-
currentState: session.currentState,
|
|
118
|
-
latestRunId: session.latestRequestId,
|
|
119
|
-
createdAt: session.createdAt,
|
|
120
|
-
updatedAt: session.updatedAt,
|
|
121
|
-
messages: session.messages,
|
|
122
|
-
runs: session.requests.map((request) => toRunRecord(request)),
|
|
123
|
-
pendingDecision: session.pendingDecision,
|
|
124
|
-
}
|
|
125
|
-
: null;
|
|
104
|
+
return session ? { ...session } : null;
|
|
126
105
|
}
|
|
127
106
|
export async function listPublicApprovals(input, filter) {
|
|
128
107
|
const approvals = await input.persistence.listApprovals(filter);
|
|
@@ -137,9 +116,9 @@ export async function deleteThreadRecord(input, threadId) {
|
|
|
137
116
|
if (!thread) {
|
|
138
117
|
return false;
|
|
139
118
|
}
|
|
140
|
-
const activeRun = thread.
|
|
119
|
+
const activeRun = thread.requests.find((run) => !isTerminalRunState(run.state));
|
|
141
120
|
if (activeRun) {
|
|
142
|
-
throw new Error(`Cannot delete thread ${threadId} while run ${activeRun.
|
|
121
|
+
throw new Error(`Cannot delete thread ${threadId} while run ${activeRun.requestId} is ${activeRun.state}`);
|
|
143
122
|
}
|
|
144
123
|
const deleted = await input.deleteThread(threadId);
|
|
145
124
|
if (deleted) {
|
|
@@ -161,7 +161,7 @@ export class Mem0IngestionSync {
|
|
|
161
161
|
}
|
|
162
162
|
const trigger = event.eventType === "approval.resolved" ? "approval.resolved" : "run.completed";
|
|
163
163
|
const task = this.syncChain
|
|
164
|
-
.then(() => this.syncRun(event.threadId, event.runId, trigger))
|
|
164
|
+
.then(() => this.syncRun(event.sessionId ?? event.threadId ?? "", event.requestId ?? event.runId ?? "", trigger))
|
|
165
165
|
.catch(() => {
|
|
166
166
|
// Fail open: memory ingestion must not break run execution.
|
|
167
167
|
});
|
|
@@ -182,7 +182,7 @@ export class Mem0IngestionSync {
|
|
|
182
182
|
}
|
|
183
183
|
async syncRun(threadId, runId, trigger) {
|
|
184
184
|
const allMessages = await this.persistence.listThreadMessages(threadId, this.config.maxMessagesPerRun);
|
|
185
|
-
const runMessages = allMessages.filter((message) => message.runId === runId);
|
|
185
|
+
const runMessages = allMessages.filter((message) => (message.requestId ?? message.runId) === runId);
|
|
186
186
|
const normalized = normalizeMessages(runMessages);
|
|
187
187
|
if (normalized.length === 0) {
|
|
188
188
|
return;
|
|
@@ -93,7 +93,7 @@ export function createBackgroundMemoryCandidates(input) {
|
|
|
93
93
|
const approvals = summarizeApprovals(input.approvals);
|
|
94
94
|
const summarySeed = latestUser ?? latestAssistant ?? `Run ${input.runId}`;
|
|
95
95
|
const baseSummary = summarySeed.length > 120 ? `${summarySeed.slice(0, 117)}...` : summarySeed;
|
|
96
|
-
const sourceRefBase = `runtime://threads/${input.thread.
|
|
96
|
+
const sourceRefBase = `runtime://threads/${input.thread.sessionId}/runs/${input.runId}/background-reflection`;
|
|
97
97
|
return input.scopes.map((scope) => ({
|
|
98
98
|
kind: "episodic",
|
|
99
99
|
scope,
|
|
@@ -101,7 +101,7 @@ export function createBackgroundMemoryCandidates(input) {
|
|
|
101
101
|
sourceRef: `${sourceRefBase}#${scope}`,
|
|
102
102
|
summary: `${baseSummary} (${scope})`,
|
|
103
103
|
content: [
|
|
104
|
-
`Run ${input.runId} completed for thread ${input.thread.
|
|
104
|
+
`Run ${input.runId} completed for thread ${input.thread.sessionId}.`,
|
|
105
105
|
`Trigger: ${input.trigger}.`,
|
|
106
106
|
`Latest user context: ${userContext}`,
|
|
107
107
|
`Latest assistant context: ${assistantContext}`,
|
|
@@ -302,7 +302,7 @@ export class RuntimeMemoryFormationSync {
|
|
|
302
302
|
}
|
|
303
303
|
const trigger = event.eventType === "approval.resolved" ? "approval.resolved" : "run.completed";
|
|
304
304
|
const task = this.syncChain
|
|
305
|
-
.then(() => this.reflectRun(event.threadId, event.runId, trigger, event.timestamp))
|
|
305
|
+
.then(() => this.reflectRun(event.sessionId ?? event.threadId ?? "", event.requestId ?? event.runId ?? "", trigger, event.timestamp))
|
|
306
306
|
.catch(() => {
|
|
307
307
|
// Fail open: reflection should not block runtime progress.
|
|
308
308
|
});
|
|
@@ -325,7 +325,7 @@ export class RuntimeMemoryFormationSync {
|
|
|
325
325
|
if (!thread || !run) {
|
|
326
326
|
return;
|
|
327
327
|
}
|
|
328
|
-
const messages = allMessages.filter((message) => message.runId === runId);
|
|
328
|
+
const messages = allMessages.filter((message) => (message.requestId ?? message.runId) === runId);
|
|
329
329
|
if (messages.length === 0) {
|
|
330
330
|
return;
|
|
331
331
|
}
|