@botbotgo/agent-harness 0.0.267 → 0.0.269
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 +8 -0
- package/README.zh.md +7 -0
- 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 +38 -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 +48 -46
- package/dist/protocol/ag-ui/http.js +9 -9
- package/dist/runtime/adapter/invocation-result.js +2 -2
- 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/helpers.js +2 -2
- 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/dist/workspace/compile.js +6 -5
- package/dist/workspace/object-loader.d.ts +4 -1
- package/dist/workspace/object-loader.js +13 -5
- package/dist/workspace/support/workspace-ref-utils.d.ts +5 -0
- package/dist/workspace/support/workspace-ref-utils.js +9 -0
- package/dist/workspace/tool-hydration.d.ts +2 -1
- package/dist/workspace/tool-hydration.js +4 -4
- package/package.json +1 -1
|
@@ -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
|
}
|
|
@@ -121,8 +121,8 @@ function createMemoryRecord(candidate, options) {
|
|
|
121
121
|
sourceRefs: sourceRef ? [sourceRef] : [],
|
|
122
122
|
tags: candidate.tags ?? [],
|
|
123
123
|
provenance: {
|
|
124
|
-
|
|
125
|
-
|
|
124
|
+
sessionId: options.threadId,
|
|
125
|
+
requestId: options.runId,
|
|
126
126
|
agentId: options.agentId,
|
|
127
127
|
workspaceId: options.workspaceId,
|
|
128
128
|
userId: options.userId,
|
|
@@ -149,8 +149,8 @@ function refreshRecord(existing, incoming, recordedAt) {
|
|
|
149
149
|
provenance: {
|
|
150
150
|
...existing.provenance,
|
|
151
151
|
refreshedFrom: incoming.provenance,
|
|
152
|
-
|
|
153
|
-
|
|
152
|
+
lastRefreshRequestId: incoming.provenance.requestId,
|
|
153
|
+
lastRefreshSessionId: incoming.provenance.sessionId,
|
|
154
154
|
},
|
|
155
155
|
revision: existing.revision + 1,
|
|
156
156
|
};
|
|
@@ -168,8 +168,8 @@ function mergeRecord(existing, incoming, recordedAt) {
|
|
|
168
168
|
provenance: {
|
|
169
169
|
...existing.provenance,
|
|
170
170
|
mergedFrom: incoming.provenance,
|
|
171
|
-
|
|
172
|
-
|
|
171
|
+
lastMergeRequestId: incoming.provenance.requestId,
|
|
172
|
+
lastMergeSessionId: incoming.provenance.sessionId,
|
|
173
173
|
},
|
|
174
174
|
revision: existing.revision + 1,
|
|
175
175
|
};
|
|
@@ -40,12 +40,13 @@ function formatApprovalSection(approvals) {
|
|
|
40
40
|
return lines;
|
|
41
41
|
}
|
|
42
42
|
function renderRunSummaryMarkdown(input) {
|
|
43
|
+
const sessionId = input.thread.sessionId ?? input.thread.threadId ?? "";
|
|
43
44
|
const userMessages = input.messages.filter((message) => message.role === "user").slice(-3);
|
|
44
45
|
const assistantMessages = input.messages.filter((message) => message.role === "assistant").slice(-3);
|
|
45
46
|
return [
|
|
46
47
|
"# Run Memory Summary",
|
|
47
48
|
"",
|
|
48
|
-
`- thread_id: ${
|
|
49
|
+
`- thread_id: ${sessionId}`,
|
|
49
50
|
`- run_id: ${input.runId}`,
|
|
50
51
|
`- agent_id: ${input.agentId}`,
|
|
51
52
|
`- status: ${input.thread.status}`,
|
|
@@ -58,12 +59,13 @@ function renderRunSummaryMarkdown(input) {
|
|
|
58
59
|
].join("\n");
|
|
59
60
|
}
|
|
60
61
|
function renderThreadDigestMarkdown(input) {
|
|
62
|
+
const sessionId = input.thread.sessionId ?? input.thread.threadId ?? "";
|
|
61
63
|
const latestUser = input.messages.filter((message) => message.role === "user").at(-1);
|
|
62
64
|
const latestAssistant = input.messages.filter((message) => message.role === "assistant").at(-1);
|
|
63
65
|
return [
|
|
64
66
|
"# Durable Thread Digest",
|
|
65
67
|
"",
|
|
66
|
-
`- thread_id: ${
|
|
68
|
+
`- thread_id: ${sessionId}`,
|
|
67
69
|
`- latest_run_id: ${input.runId}`,
|
|
68
70
|
`- status: ${input.thread.status}`,
|
|
69
71
|
`- last_memory_trigger: ${input.trigger}`,
|
|
@@ -125,7 +127,7 @@ export class RuntimeMemorySync {
|
|
|
125
127
|
}
|
|
126
128
|
const trigger = event.eventType === "approval.resolved" ? "approval.resolved" : "run.completed";
|
|
127
129
|
const task = this.syncChain
|
|
128
|
-
.then(() => this.syncRun(event.threadId, event.runId, trigger, event.timestamp))
|
|
130
|
+
.then(() => this.syncRun(event.sessionId ?? event.threadId ?? "", event.requestId ?? event.runId ?? "", trigger, event.timestamp))
|
|
129
131
|
.catch(() => {
|
|
130
132
|
// Fail open: runtime memory sync must not break the hot path.
|
|
131
133
|
});
|
|
@@ -148,7 +150,7 @@ export class RuntimeMemorySync {
|
|
|
148
150
|
if (!thread || !run) {
|
|
149
151
|
return;
|
|
150
152
|
}
|
|
151
|
-
const messages = allMessages.filter((message) => message.runId === runId);
|
|
153
|
+
const messages = allMessages.filter((message) => (message.requestId ?? message.runId) === runId);
|
|
152
154
|
if (messages.length === 0) {
|
|
153
155
|
return;
|
|
154
156
|
}
|
|
@@ -7,13 +7,15 @@ function excerpt(message) {
|
|
|
7
7
|
return normalized.length > 240 ? `${normalized.slice(0, 237)}...` : normalized;
|
|
8
8
|
}
|
|
9
9
|
function renderThreadSnapshotMarkdown(thread, messages, approvals) {
|
|
10
|
+
const sessionId = thread.sessionId ?? thread.threadId ?? "";
|
|
11
|
+
const latestRequestId = thread.latestRequestId ?? thread.latestRunId ?? "";
|
|
10
12
|
const userMessages = messages.filter((message) => message.role === "user");
|
|
11
13
|
const assistantMessages = messages.filter((message) => message.role === "assistant");
|
|
12
14
|
return [
|
|
13
15
|
"# Thread Snapshot",
|
|
14
16
|
"",
|
|
15
|
-
`- thread_id: ${
|
|
16
|
-
`- latest_run_id: ${
|
|
17
|
+
`- thread_id: ${sessionId}`,
|
|
18
|
+
`- latest_run_id: ${latestRequestId}`,
|
|
17
19
|
`- agent_id: ${thread.agentId}`,
|
|
18
20
|
`- status: ${thread.status}`,
|
|
19
21
|
`- updated_at: ${thread.updatedAt}`,
|
|
@@ -37,7 +39,7 @@ function formatOpenApprovalsSection(approvals) {
|
|
|
37
39
|
lines.push(`### ${approval.approvalId}`);
|
|
38
40
|
lines.push(`- pending_action_id: ${approval.pendingActionId}`);
|
|
39
41
|
lines.push(`- tool: ${approval.toolName}`);
|
|
40
|
-
lines.push(`- run_id: ${approval.runId}`);
|
|
42
|
+
lines.push(`- run_id: ${approval.requestId ?? approval.runId}`);
|
|
41
43
|
lines.push(`- requested_at: ${approval.requestedAt}`);
|
|
42
44
|
lines.push(`- allowed: ${approval.allowedDecisions.join(", ")}`);
|
|
43
45
|
lines.push("");
|
|
@@ -74,7 +76,7 @@ export class ThreadMemorySync {
|
|
|
74
76
|
return;
|
|
75
77
|
}
|
|
76
78
|
const task = this.syncChain
|
|
77
|
-
.then(() => this.syncThread(event.threadId))
|
|
79
|
+
.then(() => this.syncThread(event.sessionId ?? event.threadId ?? ""))
|
|
78
80
|
.catch(() => {
|
|
79
81
|
// Fail open: background memory digestion must not break the hot path.
|
|
80
82
|
});
|
|
@@ -96,7 +98,7 @@ export class ThreadMemorySync {
|
|
|
96
98
|
this.persistence.listThreadMessages(threadId, 24),
|
|
97
99
|
this.persistence.listApprovals(),
|
|
98
100
|
]);
|
|
99
|
-
const pendingApprovals = approvals.filter((approval) => approval.threadId === threadId && approval.status === "pending");
|
|
101
|
+
const pendingApprovals = approvals.filter((approval) => (approval.sessionId ?? approval.threadId) === threadId && approval.status === "pending");
|
|
100
102
|
if (!this.store) {
|
|
101
103
|
return;
|
|
102
104
|
}
|
|
@@ -95,8 +95,8 @@ export declare class AgentHarnessRuntime {
|
|
|
95
95
|
getThread(threadId: string): Promise<ThreadRecord | null>;
|
|
96
96
|
listApprovals(filter?: {
|
|
97
97
|
status?: ApprovalRecord["status"];
|
|
98
|
-
|
|
99
|
-
|
|
98
|
+
sessionId?: string;
|
|
99
|
+
requestId?: string;
|
|
100
100
|
}): Promise<ApprovalRecord[]>;
|
|
101
101
|
getApproval(approvalId: string): Promise<ApprovalRecord | null>;
|
|
102
102
|
listArtifacts(threadId: string, runId: string): Promise<ArtifactListing>;
|