@botbotgo/agent-harness 0.0.290 → 0.0.291
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 +20 -20
- package/README.zh.md +14 -14
- package/dist/acp.d.ts +5 -5
- package/dist/acp.js +3 -3
- package/dist/api.d.ts +20 -21
- package/dist/api.js +38 -50
- package/dist/cli.js +47 -43
- package/dist/config/agents/orchestra.yaml +3 -3
- package/dist/config/knowledge/knowledge-runtime.yaml +4 -4
- package/dist/config/runtime/runtime-memory.yaml +7 -7
- package/dist/config/runtime/workspace.yaml +7 -7
- package/dist/contracts/core.d.ts +1 -1
- package/dist/contracts/runtime.d.ts +35 -40
- package/dist/contracts/workspace.d.ts +2 -2
- package/dist/flow/build-flow-graph.js +20 -33
- package/dist/flow/export-sequence-mermaid.js +4 -4
- package/dist/flow/types.d.ts +2 -2
- package/dist/index.d.ts +4 -4
- package/dist/index.js +1 -1
- package/dist/init-project.js +10 -10
- package/dist/knowledge/module.js +37 -45
- package/dist/mcp.d.ts +9 -9
- package/dist/mcp.js +6 -6
- package/dist/package-version.d.ts +1 -1
- package/dist/package-version.js +1 -1
- package/dist/persistence/file-store.d.ts +69 -69
- package/dist/persistence/file-store.js +224 -221
- package/dist/persistence/sqlite-request-context-store.d.ts +22 -0
- package/dist/persistence/sqlite-request-context-store.js +64 -0
- package/dist/persistence/sqlite-request-queue-store.d.ts +41 -0
- package/dist/persistence/sqlite-request-queue-store.js +120 -0
- package/dist/persistence/sqlite-store.d.ts +72 -72
- package/dist/persistence/sqlite-store.js +361 -361
- package/dist/persistence/types.d.ts +84 -84
- package/dist/protocol/a2a/http.js +79 -74
- package/dist/protocol/ag-ui/http.d.ts +7 -7
- package/dist/protocol/ag-ui/http.js +20 -20
- package/dist/resource/resource-impl.js +1 -1
- package/dist/runtime/adapter/compat/deepagent-compat.d.ts +2 -2
- package/dist/runtime/adapter/flow/invocation-flow.d.ts +6 -5
- package/dist/runtime/adapter/flow/invocation-flow.js +6 -5
- package/dist/runtime/adapter/flow/stream-runtime.d.ts +3 -3
- package/dist/runtime/adapter/flow/stream-runtime.js +5 -4
- package/dist/runtime/adapter/invocation-result.d.ts +6 -5
- package/dist/runtime/adapter/invocation-result.js +5 -4
- package/dist/runtime/adapter/middleware-assembly.js +3 -2
- package/dist/runtime/adapter/tool/tool-hitl.js +1 -1
- package/dist/runtime/adapter/upstream-configurable-keys.d.ts +2 -0
- package/dist/runtime/adapter/upstream-configurable-keys.js +2 -0
- package/dist/runtime/agent-runtime-adapter.d.ts +11 -8
- package/dist/runtime/agent-runtime-adapter.js +36 -32
- package/dist/runtime/harness/events/events.d.ts +8 -8
- package/dist/runtime/harness/events/events.js +25 -19
- package/dist/runtime/harness/events/listener-runtime.d.ts +5 -4
- package/dist/runtime/harness/events/listener-runtime.js +7 -3
- package/dist/runtime/harness/events/runtime-event-operations.d.ts +7 -7
- package/dist/runtime/harness/events/runtime-event-operations.js +5 -5
- package/dist/runtime/harness/events/streaming.d.ts +8 -7
- package/dist/runtime/harness/events/streaming.js +20 -19
- package/dist/runtime/harness/events/timeline.js +6 -6
- package/dist/runtime/harness/index.d.ts +1 -1
- package/dist/runtime/harness/index.js +1 -1
- package/dist/runtime/harness/run/helpers.d.ts +14 -11
- package/dist/runtime/harness/run/helpers.js +10 -7
- package/dist/runtime/harness/run/inspection.d.ts +3 -2
- package/dist/runtime/harness/run/inspection.js +7 -7
- package/dist/runtime/harness/run/operator-overview.d.ts +2 -2
- package/dist/runtime/harness/run/operator-overview.js +18 -17
- package/dist/runtime/harness/run/queue-diagnostics.js +6 -6
- package/dist/runtime/harness/run/recovery.d.ts +15 -15
- package/dist/runtime/harness/run/recovery.js +53 -50
- package/dist/runtime/harness/run/resources.d.ts +2 -2
- package/dist/runtime/harness/run/resources.js +8 -8
- package/dist/runtime/harness/run/resume.d.ts +3 -3
- package/dist/runtime/harness/run/resume.js +4 -4
- package/dist/runtime/harness/run/routing.d.ts +4 -4
- package/dist/runtime/harness/run/routing.js +8 -8
- package/dist/runtime/harness/run/run-lifecycle.d.ts +12 -12
- package/dist/runtime/harness/run/run-lifecycle.js +26 -26
- package/dist/runtime/harness/run/run-operations.d.ts +45 -45
- package/dist/runtime/harness/run/run-operations.js +79 -78
- package/dist/runtime/harness/run/run-queue.d.ts +8 -8
- package/dist/runtime/harness/run/run-queue.js +16 -16
- package/dist/runtime/harness/run/run-slot-acquisition.d.ts +32 -32
- package/dist/runtime/harness/run/run-slot-acquisition.js +41 -41
- package/dist/runtime/harness/run/{thread-records.d.ts → session-records.d.ts} +6 -13
- package/dist/runtime/harness/run/{thread-records.js → session-records.js} +14 -60
- package/dist/runtime/harness/run/start-run.d.ts +36 -36
- package/dist/runtime/harness/run/start-run.js +55 -36
- package/dist/runtime/harness/run/startup-runtime.d.ts +9 -9
- package/dist/runtime/harness/run/startup-runtime.js +22 -20
- package/dist/runtime/harness/run/stream-run.d.ts +18 -18
- package/dist/runtime/harness/run/stream-run.js +52 -52
- package/dist/runtime/harness/runtime-defaults.d.ts +2 -2
- package/dist/runtime/harness/runtime-defaults.js +7 -7
- package/dist/runtime/harness/system/health-monitor.d.ts +3 -3
- package/dist/runtime/harness/system/health-monitor.js +18 -18
- package/dist/runtime/harness/system/mem0-ingestion-sync.d.ts +6 -6
- package/dist/runtime/harness/system/mem0-ingestion-sync.js +36 -27
- package/dist/runtime/harness/system/runtime-memory-candidates.js +2 -2
- package/dist/runtime/harness/system/runtime-memory-manager.d.ts +13 -13
- package/dist/runtime/harness/system/runtime-memory-manager.js +41 -38
- package/dist/runtime/harness/system/runtime-memory-policy.d.ts +1 -1
- package/dist/runtime/harness/system/runtime-memory-policy.js +1 -1
- package/dist/runtime/harness/system/runtime-memory-records.d.ts +4 -2
- package/dist/runtime/harness/system/runtime-memory-records.js +21 -8
- package/dist/runtime/harness/system/runtime-memory-sync.d.ts +6 -6
- package/dist/runtime/harness/system/runtime-memory-sync.js +47 -44
- package/dist/runtime/harness/system/{thread-memory-sync.d.ts → session-memory-sync.d.ts} +7 -7
- package/dist/runtime/harness/system/{thread-memory-sync.js → session-memory-sync.js} +28 -28
- package/dist/runtime/harness.d.ts +33 -45
- package/dist/runtime/harness.js +273 -291
- package/dist/runtime/maintenance/checkpoint-maintenance.js +2 -2
- package/dist/runtime/maintenance/file-checkpoint-saver.d.ts +1 -1
- package/dist/runtime/maintenance/file-checkpoint-saver.js +6 -6
- package/dist/runtime/maintenance/runtime-record-maintenance.d.ts +1 -1
- package/dist/runtime/maintenance/runtime-record-maintenance.js +33 -33
- package/dist/runtime/maintenance/sqlite-checkpoint-saver.d.ts +1 -1
- package/dist/runtime/maintenance/sqlite-checkpoint-saver.js +30 -10
- package/dist/runtime/support/harness-support.d.ts +2 -2
- package/dist/runtime/support/harness-support.js +7 -7
- package/dist/runtime/support/runtime-adapter-options.d.ts +2 -2
- package/dist/runtime/support/runtime-adapter-options.js +3 -3
- package/dist/runtime/support/runtime-factories.d.ts +2 -2
- package/dist/runtime/support/runtime-factories.js +10 -10
- package/dist/workspace/agent-binding-compiler.js +3 -3
- package/dist/workspace/object-loader.js +1 -1
- package/dist/workspace/support/workspace-ref-utils.d.ts +4 -3
- package/dist/workspace/support/workspace-ref-utils.js +5 -4
- package/package.json +1 -1
- package/dist/persistence/sqlite-run-context-store.d.ts +0 -22
- package/dist/persistence/sqlite-run-context-store.js +0 -64
- package/dist/persistence/sqlite-run-queue-store.d.ts +0 -41
- package/dist/persistence/sqlite-run-queue-store.js +0 -120
|
@@ -1,97 +1,97 @@
|
|
|
1
|
-
import type { CancelOptions, CompiledAgentBinding, InternalApprovalRecord, MessageContent,
|
|
1
|
+
import type { CancelOptions, CompiledAgentBinding, InternalApprovalRecord, MessageContent, RequestResult, ResumeOptions, SessionRecord, TranscriptMessage } from "../../../contracts/types.js";
|
|
2
2
|
import type { RecoveryIntent } from "../../../persistence/types.js";
|
|
3
3
|
type ResumeOperationRuntime = {
|
|
4
4
|
getApprovalById: (approvalId: string) => Promise<InternalApprovalRecord | null>;
|
|
5
|
-
getSession: (
|
|
6
|
-
resolveApprovalRecord: (options: ResumeOptions,
|
|
5
|
+
getSession: (sessionId: string) => Promise<SessionRecord | null>;
|
|
6
|
+
resolveApprovalRecord: (options: ResumeOptions, session: SessionRecord) => Promise<InternalApprovalRecord>;
|
|
7
7
|
getBinding: (agentId: string) => CompiledAgentBinding | undefined;
|
|
8
8
|
buildResumePayload: (binding: CompiledAgentBinding, approval: InternalApprovalRecord, options: ResumeOptions) => unknown;
|
|
9
|
-
|
|
9
|
+
getRequestCancellation: (requestId: string) => Promise<{
|
|
10
10
|
requested: boolean;
|
|
11
11
|
reason?: string;
|
|
12
12
|
}>;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
emit: (
|
|
19
|
-
resolveApproval: (
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
invoke: (binding: CompiledAgentBinding, input: MessageContent,
|
|
13
|
+
finalizeCancelledRequest: (sessionId: string, requestId: string, previousState: RequestResult["state"] | null, reason?: string) => Promise<RequestResult>;
|
|
14
|
+
setRequestState: (sessionId: string, requestId: string, state: RequestResult["state"], checkpointRef: string | null) => Promise<void>;
|
|
15
|
+
acquireRequestSlot: (sessionId?: string, requestId?: string, activeState?: RequestResult["state"], priority?: number) => Promise<() => Promise<void>>;
|
|
16
|
+
resolvePersistedRequestPriority: (sessionId: string, requestId: string) => Promise<number>;
|
|
17
|
+
saveRequestRecoveryIntent: (sessionId: string, requestId: string, payload: RecoveryIntent) => Promise<unknown>;
|
|
18
|
+
emit: (sessionId: string, requestId: string, sequence: number, eventType: string, payload: Record<string, unknown>) => Promise<unknown>;
|
|
19
|
+
resolveApproval: (sessionId: string, requestId: string, approvalId: string, resolution: "approved" | "edited" | "rejected" | "expired") => Promise<unknown>;
|
|
20
|
+
listSessionMessages: (sessionId: string) => Promise<TranscriptMessage[]>;
|
|
21
|
+
loadRequestInput: (sessionId: string, requestId: string) => Promise<MessageContent>;
|
|
22
|
+
invoke: (binding: CompiledAgentBinding, input: MessageContent, sessionId: string, requestId: string, resumePayload: unknown, priorHistory: TranscriptMessage[]) => Promise<RequestResult>;
|
|
23
23
|
recordLlmSuccess: (startedAt: number) => void;
|
|
24
24
|
recordLlmFailure: (startedAt: number) => void;
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
previousState:
|
|
25
|
+
clearRequestRecoveryIntent: (sessionId: string, requestId: string) => Promise<void>;
|
|
26
|
+
finalizeContinuedRequest: (binding: CompiledAgentBinding, sessionId: string, requestId: string, input: MessageContent, actual: RequestResult, options: {
|
|
27
|
+
previousState: RequestResult["state"] | null;
|
|
28
28
|
stateSequence: number;
|
|
29
29
|
approvalSequence?: number;
|
|
30
|
-
}) => Promise<
|
|
30
|
+
}) => Promise<RequestResult>;
|
|
31
31
|
};
|
|
32
32
|
type CancelOperationRuntime = {
|
|
33
|
-
|
|
33
|
+
getRequest: (requestId: string) => Promise<{
|
|
34
34
|
sessionId: string;
|
|
35
35
|
requestId: string;
|
|
36
36
|
agentId: string;
|
|
37
|
-
state:
|
|
37
|
+
state: RequestResult["state"];
|
|
38
38
|
} | null>;
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
39
|
+
requestRequestCancel: (requestId: string, reason?: string) => Promise<unknown>;
|
|
40
|
+
dropPendingRequestSlot: (requestId: string) => boolean;
|
|
41
|
+
finalizeCancelledRequest: (sessionId: string, requestId: string, previousState: RequestResult["state"] | null, reason?: string) => Promise<RequestResult>;
|
|
42
|
+
setRequestStateAndEmit: (sessionId: string, requestId: string, sequence: number, state: RequestResult["state"], options: {
|
|
43
43
|
previousState: string | null;
|
|
44
44
|
checkpointRef?: string | null;
|
|
45
45
|
error?: string;
|
|
46
46
|
}) => Promise<unknown>;
|
|
47
47
|
};
|
|
48
|
-
type
|
|
48
|
+
type ExecuteQueuedRequestOperationRuntime = {
|
|
49
49
|
persistence: {
|
|
50
|
-
|
|
51
|
-
state:
|
|
50
|
+
getRequest: (requestId: string) => Promise<{
|
|
51
|
+
state: RequestResult["state"];
|
|
52
52
|
} | null>;
|
|
53
|
-
|
|
53
|
+
clearRequestInput: (sessionId: string, requestId: string) => Promise<void>;
|
|
54
54
|
};
|
|
55
|
-
|
|
55
|
+
getRequestCancellation: (requestId: string) => Promise<{
|
|
56
56
|
requested: boolean;
|
|
57
57
|
reason?: string;
|
|
58
58
|
}>;
|
|
59
|
-
|
|
60
|
-
emit: (
|
|
61
|
-
|
|
59
|
+
finalizeCancelledRequest: (sessionId: string, requestId: string, previousState: RequestResult["state"] | null, reason?: string) => Promise<RequestResult>;
|
|
60
|
+
emit: (sessionId: string, requestId: string, sequence: number, eventType: string, payload: Record<string, unknown>) => Promise<unknown>;
|
|
61
|
+
setRequestStateAndEmit: (sessionId: string, requestId: string, sequence: number, state: RequestResult["state"], options: {
|
|
62
62
|
previousState: string | null;
|
|
63
63
|
checkpointRef?: string | null;
|
|
64
64
|
error?: string;
|
|
65
65
|
}) => Promise<unknown>;
|
|
66
|
-
invokeWithHistory: (binding: CompiledAgentBinding, input: MessageContent,
|
|
66
|
+
invokeWithHistory: (binding: CompiledAgentBinding, input: MessageContent, sessionId: string, requestId: string, resumePayload?: unknown, priorHistory?: TranscriptMessage[], options?: {
|
|
67
67
|
context?: Record<string, unknown>;
|
|
68
68
|
state?: Record<string, unknown>;
|
|
69
69
|
files?: Record<string, unknown>;
|
|
70
|
-
}) => Promise<
|
|
71
|
-
|
|
72
|
-
previousState:
|
|
70
|
+
}) => Promise<RequestResult>;
|
|
71
|
+
finalizeContinuedRequest: (binding: CompiledAgentBinding, sessionId: string, requestId: string, input: MessageContent, actual: RequestResult, options: {
|
|
72
|
+
previousState: RequestResult["state"] | null;
|
|
73
73
|
stateSequence: number;
|
|
74
74
|
approvalSequence?: number;
|
|
75
|
-
}) => Promise<
|
|
76
|
-
emitSyntheticFallback: (
|
|
75
|
+
}) => Promise<RequestResult>;
|
|
76
|
+
emitSyntheticFallback: (sessionId: string, requestId: string, agentId: string, error: unknown) => Promise<void>;
|
|
77
77
|
renderRuntimeFailure: (error: unknown) => string;
|
|
78
78
|
};
|
|
79
|
-
export declare function
|
|
80
|
-
export declare function
|
|
81
|
-
export declare function
|
|
79
|
+
export declare function resumeRequest(runtime: ResumeOperationRuntime, options: ResumeOptions): Promise<RequestResult>;
|
|
80
|
+
export declare function cancelRequestOperation(runtime: CancelOperationRuntime, options: CancelOptions): Promise<RequestResult>;
|
|
81
|
+
export declare function executeQueuedRequestOperation(runtime: ExecuteQueuedRequestOperationRuntime, input: {
|
|
82
82
|
binding: CompiledAgentBinding;
|
|
83
83
|
message: MessageContent;
|
|
84
|
-
|
|
85
|
-
|
|
84
|
+
sessionId: string;
|
|
85
|
+
requestId: string;
|
|
86
86
|
agentId: string;
|
|
87
87
|
options?: {
|
|
88
88
|
context?: Record<string, unknown>;
|
|
89
89
|
state?: Record<string, unknown>;
|
|
90
90
|
files?: Record<string, unknown>;
|
|
91
91
|
priorHistory?: TranscriptMessage[];
|
|
92
|
-
previousState?:
|
|
92
|
+
previousState?: RequestResult["state"];
|
|
93
93
|
stateSequence?: number;
|
|
94
94
|
approvalSequence?: number;
|
|
95
95
|
};
|
|
96
|
-
}): Promise<
|
|
96
|
+
}): Promise<RequestResult>;
|
|
97
97
|
export {};
|
|
@@ -1,86 +1,87 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isTerminalRequestState } from "./helpers.js";
|
|
2
2
|
async function finalizeIfCancellationRequested(input) {
|
|
3
|
-
const cancellation = await input.
|
|
3
|
+
const cancellation = await input.getRequestCancellation(input.requestId);
|
|
4
4
|
if (!cancellation.requested) {
|
|
5
5
|
return null;
|
|
6
6
|
}
|
|
7
|
-
return input.
|
|
7
|
+
return input.finalizeCancelledRequest(input.sessionId, input.requestId, input.previousState, cancellation.reason);
|
|
8
8
|
}
|
|
9
9
|
function normalizeContinuationState(previousState) {
|
|
10
10
|
return previousState === "queued" ? "running" : previousState;
|
|
11
11
|
}
|
|
12
|
-
export async function
|
|
12
|
+
export async function resumeRequest(runtime, options) {
|
|
13
13
|
const approvalById = options.approvalId ? await runtime.getApprovalById(options.approvalId) : null;
|
|
14
|
-
const
|
|
14
|
+
const session = options.sessionId
|
|
15
15
|
? await runtime.getSession(options.sessionId)
|
|
16
16
|
: approvalById
|
|
17
17
|
? await runtime.getSession(approvalById.sessionId)
|
|
18
18
|
: null;
|
|
19
|
-
if (!
|
|
19
|
+
if (!session) {
|
|
20
20
|
throw new Error("resume requires either sessionId or approvalId");
|
|
21
21
|
}
|
|
22
|
-
const approval = approvalById ?? await runtime.resolveApprovalRecord(options,
|
|
23
|
-
const
|
|
24
|
-
const
|
|
25
|
-
const
|
|
22
|
+
const approval = approvalById ?? await runtime.resolveApprovalRecord(options, session);
|
|
23
|
+
const sessionId = approval.sessionId;
|
|
24
|
+
const requestId = approval.requestId;
|
|
25
|
+
const activeAgentId = session.currentAgentId ?? session.entryAgentId;
|
|
26
|
+
const binding = runtime.getBinding(activeAgentId);
|
|
26
27
|
if (!binding) {
|
|
27
|
-
throw new Error(`Unknown agent ${
|
|
28
|
+
throw new Error(`Unknown agent ${activeAgentId}`);
|
|
28
29
|
}
|
|
29
30
|
const resumePayload = runtime.buildResumePayload(binding, approval, options);
|
|
30
31
|
const cancelledBeforeResume = await finalizeIfCancellationRequested({
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
previousState:
|
|
32
|
+
getRequestCancellation: runtime.getRequestCancellation,
|
|
33
|
+
finalizeCancelledRequest: runtime.finalizeCancelledRequest,
|
|
34
|
+
sessionId,
|
|
35
|
+
requestId,
|
|
36
|
+
previousState: session.currentState,
|
|
36
37
|
});
|
|
37
38
|
if (cancelledBeforeResume) {
|
|
38
39
|
return cancelledBeforeResume;
|
|
39
40
|
}
|
|
40
|
-
const checkpointRef = `checkpoints/${
|
|
41
|
-
await runtime.
|
|
42
|
-
const
|
|
41
|
+
const checkpointRef = `checkpoints/${sessionId}/${requestId}/cp-1`;
|
|
42
|
+
await runtime.setRequestState(sessionId, requestId, "resuming", checkpointRef);
|
|
43
|
+
const releaseRequestSlot = await runtime.acquireRequestSlot(sessionId, requestId, "resuming", await runtime.resolvePersistedRequestPriority(sessionId, requestId));
|
|
43
44
|
try {
|
|
44
|
-
await runtime.
|
|
45
|
+
await runtime.saveRequestRecoveryIntent(sessionId, requestId, {
|
|
45
46
|
kind: "approval-decision",
|
|
46
47
|
savedAt: new Date().toISOString(),
|
|
47
48
|
checkpointRef,
|
|
48
49
|
resumePayload,
|
|
49
50
|
attempts: 0,
|
|
50
51
|
});
|
|
51
|
-
await runtime.emit(
|
|
52
|
+
await runtime.emit(sessionId, requestId, 5, "request.resumed", {
|
|
52
53
|
resumeKind: "cross-restart",
|
|
53
54
|
checkpointRef,
|
|
54
55
|
state: "resuming",
|
|
55
56
|
approvalId: approval.approvalId,
|
|
56
57
|
pendingActionId: approval.pendingActionId,
|
|
57
58
|
});
|
|
58
|
-
await runtime.resolveApproval(
|
|
59
|
-
await runtime.emit(
|
|
59
|
+
await runtime.resolveApproval(sessionId, requestId, approval.approvalId, options.decision === "reject" ? "rejected" : options.decision === "edit" ? "edited" : "approved");
|
|
60
|
+
await runtime.emit(sessionId, requestId, 6, "approval.resolved", {
|
|
60
61
|
approvalId: approval.approvalId,
|
|
61
62
|
pendingActionId: approval.pendingActionId,
|
|
62
63
|
decision: options.decision ?? "approve",
|
|
63
64
|
toolName: approval.toolName,
|
|
64
65
|
});
|
|
65
|
-
const history = await runtime.
|
|
66
|
-
const priorHistory = history.filter((message) => message.requestId !==
|
|
67
|
-
const
|
|
66
|
+
const history = await runtime.listSessionMessages(sessionId);
|
|
67
|
+
const priorHistory = history.filter((message) => message.requestId !== requestId);
|
|
68
|
+
const requestInput = await runtime.loadRequestInput(sessionId, requestId);
|
|
68
69
|
const startedAt = Date.now();
|
|
69
70
|
try {
|
|
70
|
-
const actual = await runtime.invoke(binding, "",
|
|
71
|
+
const actual = await runtime.invoke(binding, "", sessionId, requestId, resumePayload, priorHistory);
|
|
71
72
|
runtime.recordLlmSuccess(startedAt);
|
|
72
73
|
const cancelledAfterInvoke = await finalizeIfCancellationRequested({
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
74
|
+
getRequestCancellation: runtime.getRequestCancellation,
|
|
75
|
+
finalizeCancelledRequest: runtime.finalizeCancelledRequest,
|
|
76
|
+
sessionId,
|
|
77
|
+
requestId,
|
|
77
78
|
previousState: "resuming",
|
|
78
79
|
});
|
|
79
80
|
if (cancelledAfterInvoke) {
|
|
80
81
|
return cancelledAfterInvoke;
|
|
81
82
|
}
|
|
82
|
-
await runtime.
|
|
83
|
-
const finalized = await runtime.
|
|
83
|
+
await runtime.clearRequestRecoveryIntent(sessionId, requestId);
|
|
84
|
+
const finalized = await runtime.finalizeContinuedRequest(binding, sessionId, requestId, requestInput, actual, {
|
|
84
85
|
previousState: "resuming",
|
|
85
86
|
stateSequence: 7,
|
|
86
87
|
approvalSequence: 8,
|
|
@@ -97,95 +98,95 @@ export async function resumeRun(runtime, options) {
|
|
|
97
98
|
}
|
|
98
99
|
}
|
|
99
100
|
finally {
|
|
100
|
-
await
|
|
101
|
+
await releaseRequestSlot();
|
|
101
102
|
}
|
|
102
103
|
}
|
|
103
|
-
export async function
|
|
104
|
-
const
|
|
105
|
-
if (!
|
|
106
|
-
throw new Error(`Unknown
|
|
104
|
+
export async function cancelRequestOperation(runtime, options) {
|
|
105
|
+
const request = await runtime.getRequest(options.requestId);
|
|
106
|
+
if (!request) {
|
|
107
|
+
throw new Error(`Unknown request ${options.requestId}`);
|
|
107
108
|
}
|
|
108
|
-
if (
|
|
109
|
+
if (isTerminalRequestState(request.state)) {
|
|
109
110
|
return {
|
|
110
|
-
sessionId:
|
|
111
|
-
requestId:
|
|
112
|
-
agentId:
|
|
113
|
-
state:
|
|
114
|
-
output:
|
|
111
|
+
sessionId: request.sessionId,
|
|
112
|
+
requestId: request.requestId,
|
|
113
|
+
agentId: request.agentId,
|
|
114
|
+
state: request.state,
|
|
115
|
+
output: request.state,
|
|
115
116
|
};
|
|
116
117
|
}
|
|
117
|
-
await runtime.
|
|
118
|
-
if (
|
|
119
|
-
if (
|
|
120
|
-
runtime.
|
|
118
|
+
await runtime.requestRequestCancel(request.requestId, options.reason);
|
|
119
|
+
if (request.state === "queued" || request.state === "waiting_for_approval" || request.state === "claimed") {
|
|
120
|
+
if (request.state === "queued") {
|
|
121
|
+
runtime.dropPendingRequestSlot(request.requestId);
|
|
121
122
|
}
|
|
122
|
-
return runtime.
|
|
123
|
+
return runtime.finalizeCancelledRequest(request.sessionId, request.requestId, request.state, options.reason);
|
|
123
124
|
}
|
|
124
|
-
await runtime.
|
|
125
|
-
previousState:
|
|
125
|
+
await runtime.setRequestStateAndEmit(request.sessionId, request.requestId, 103, "cancelling", {
|
|
126
|
+
previousState: request.state,
|
|
126
127
|
...(options.reason ? { error: options.reason } : {}),
|
|
127
128
|
});
|
|
128
129
|
return {
|
|
129
|
-
sessionId:
|
|
130
|
-
requestId:
|
|
131
|
-
agentId:
|
|
130
|
+
sessionId: request.sessionId,
|
|
131
|
+
requestId: request.requestId,
|
|
132
|
+
agentId: request.agentId,
|
|
132
133
|
state: "cancelling",
|
|
133
134
|
output: options.reason ? `cancelling: ${options.reason}` : "cancelling",
|
|
134
135
|
};
|
|
135
136
|
}
|
|
136
|
-
export async function
|
|
137
|
-
const { binding, message,
|
|
137
|
+
export async function executeQueuedRequestOperation(runtime, input) {
|
|
138
|
+
const { binding, message, sessionId, requestId, agentId } = input;
|
|
138
139
|
const options = input.options ?? {};
|
|
139
140
|
const previousState = options.previousState ?? "running";
|
|
140
|
-
const
|
|
141
|
-
if (
|
|
141
|
+
const currentRequest = await runtime.persistence.getRequest(requestId);
|
|
142
|
+
if (currentRequest?.state === "cancelled") {
|
|
142
143
|
return {
|
|
143
|
-
sessionId:
|
|
144
|
-
requestId:
|
|
144
|
+
sessionId: sessionId,
|
|
145
|
+
requestId: requestId,
|
|
145
146
|
agentId,
|
|
146
147
|
state: "cancelled",
|
|
147
148
|
output: "cancelled",
|
|
148
149
|
};
|
|
149
150
|
}
|
|
150
151
|
const cancelledBeforeInvoke = await finalizeIfCancellationRequested({
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
152
|
+
getRequestCancellation: runtime.getRequestCancellation,
|
|
153
|
+
finalizeCancelledRequest: runtime.finalizeCancelledRequest,
|
|
154
|
+
sessionId,
|
|
155
|
+
requestId,
|
|
155
156
|
previousState,
|
|
156
157
|
});
|
|
157
158
|
if (cancelledBeforeInvoke) {
|
|
158
159
|
return cancelledBeforeInvoke;
|
|
159
160
|
}
|
|
160
161
|
if (previousState === "queued") {
|
|
161
|
-
await runtime.emit(
|
|
162
|
+
await runtime.emit(sessionId, requestId, 101, "request.dequeued", {
|
|
162
163
|
queuePosition: 0,
|
|
163
|
-
|
|
164
|
+
activeRequestCount: 0,
|
|
164
165
|
maxConcurrentRuns: 0,
|
|
165
166
|
recoveredOnStartup: true,
|
|
166
167
|
});
|
|
167
|
-
await runtime.
|
|
168
|
+
await runtime.setRequestStateAndEmit(sessionId, requestId, 102, "running", {
|
|
168
169
|
previousState: "queued",
|
|
169
170
|
});
|
|
170
171
|
}
|
|
171
172
|
const continuationState = normalizeContinuationState(previousState);
|
|
172
173
|
try {
|
|
173
|
-
const actual = await runtime.invokeWithHistory(binding, message,
|
|
174
|
+
const actual = await runtime.invokeWithHistory(binding, message, sessionId, requestId, undefined, options.priorHistory, {
|
|
174
175
|
context: options.context,
|
|
175
176
|
state: options.state,
|
|
176
177
|
files: options.files,
|
|
177
178
|
});
|
|
178
179
|
const cancelledAfterInvoke = await finalizeIfCancellationRequested({
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
180
|
+
getRequestCancellation: runtime.getRequestCancellation,
|
|
181
|
+
finalizeCancelledRequest: runtime.finalizeCancelledRequest,
|
|
182
|
+
sessionId,
|
|
183
|
+
requestId,
|
|
183
184
|
previousState: continuationState,
|
|
184
185
|
});
|
|
185
186
|
if (cancelledAfterInvoke) {
|
|
186
187
|
return cancelledAfterInvoke;
|
|
187
188
|
}
|
|
188
|
-
const finalized = await runtime.
|
|
189
|
+
const finalized = await runtime.finalizeContinuedRequest(binding, sessionId, requestId, message, actual, {
|
|
189
190
|
previousState: continuationState,
|
|
190
191
|
stateSequence: options.stateSequence ?? 103,
|
|
191
192
|
approvalSequence: options.approvalSequence ?? 104,
|
|
@@ -196,20 +197,20 @@ export async function executeQueuedRunOperation(runtime, input) {
|
|
|
196
197
|
};
|
|
197
198
|
}
|
|
198
199
|
catch (error) {
|
|
199
|
-
await runtime.emitSyntheticFallback(
|
|
200
|
-
await runtime.
|
|
200
|
+
await runtime.emitSyntheticFallback(sessionId, requestId, agentId, error);
|
|
201
|
+
await runtime.setRequestStateAndEmit(sessionId, requestId, 104, "failed", {
|
|
201
202
|
previousState: continuationState,
|
|
202
203
|
error: error instanceof Error ? error.message : String(error),
|
|
203
204
|
});
|
|
204
205
|
return {
|
|
205
|
-
sessionId:
|
|
206
|
-
requestId:
|
|
206
|
+
sessionId: sessionId,
|
|
207
|
+
requestId: requestId,
|
|
207
208
|
agentId,
|
|
208
209
|
state: "failed",
|
|
209
210
|
output: runtime.renderRuntimeFailure(error),
|
|
210
211
|
};
|
|
211
212
|
}
|
|
212
213
|
finally {
|
|
213
|
-
await runtime.persistence.
|
|
214
|
+
await runtime.persistence.clearRequestInput(sessionId, requestId);
|
|
214
215
|
}
|
|
215
216
|
}
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
export type
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
export type PendingRequestSlot = {
|
|
2
|
+
sessionId?: string;
|
|
3
|
+
requestId?: string;
|
|
4
4
|
priority: number;
|
|
5
5
|
insertionOrder: number;
|
|
6
6
|
activate: () => void | Promise<void>;
|
|
7
7
|
abort: () => void;
|
|
8
8
|
};
|
|
9
9
|
export type QueuePositionChange = {
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
sessionId: string;
|
|
11
|
+
requestId: string;
|
|
12
12
|
priority: number;
|
|
13
13
|
queuePosition: number;
|
|
14
14
|
};
|
|
15
|
-
export declare function
|
|
16
|
-
export declare function
|
|
17
|
-
export declare function
|
|
15
|
+
export declare function enqueuePendingRequestSlot(pendingRequestSlots: PendingRequestSlot[], entry: Omit<PendingRequestSlot, "insertionOrder">, insertionOrder: number): QueuePositionChange[];
|
|
16
|
+
export declare function shiftNextPendingRequestSlot(pendingRequestSlots: PendingRequestSlot[]): PendingRequestSlot | undefined;
|
|
17
|
+
export declare function dropPendingRequestSlot(pendingRequestSlots: PendingRequestSlot[], requestId: string): boolean;
|
|
@@ -1,43 +1,43 @@
|
|
|
1
|
-
export function
|
|
2
|
-
const previousPositions = new Map(
|
|
3
|
-
.filter((candidate) => Boolean(candidate.
|
|
4
|
-
.map((candidate, index) => [candidate.
|
|
5
|
-
|
|
1
|
+
export function enqueuePendingRequestSlot(pendingRequestSlots, entry, insertionOrder) {
|
|
2
|
+
const previousPositions = new Map(pendingRequestSlots
|
|
3
|
+
.filter((candidate) => Boolean(candidate.sessionId && candidate.requestId))
|
|
4
|
+
.map((candidate, index) => [candidate.requestId, index + 1]));
|
|
5
|
+
pendingRequestSlots.push({
|
|
6
6
|
...entry,
|
|
7
7
|
insertionOrder,
|
|
8
8
|
});
|
|
9
|
-
|
|
9
|
+
pendingRequestSlots.sort((left, right) => {
|
|
10
10
|
if (right.priority !== left.priority) {
|
|
11
11
|
return right.priority - left.priority;
|
|
12
12
|
}
|
|
13
13
|
return left.insertionOrder - right.insertionOrder;
|
|
14
14
|
});
|
|
15
|
-
return
|
|
16
|
-
if (!candidate.
|
|
15
|
+
return pendingRequestSlots.flatMap((candidate, index) => {
|
|
16
|
+
if (!candidate.sessionId || !candidate.requestId) {
|
|
17
17
|
return [];
|
|
18
18
|
}
|
|
19
|
-
const previousPosition = previousPositions.get(candidate.
|
|
19
|
+
const previousPosition = previousPositions.get(candidate.requestId);
|
|
20
20
|
const queuePosition = index + 1;
|
|
21
21
|
if (previousPosition === undefined || previousPosition === queuePosition) {
|
|
22
22
|
return [];
|
|
23
23
|
}
|
|
24
24
|
return [{
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
sessionId: candidate.sessionId,
|
|
26
|
+
requestId: candidate.requestId,
|
|
27
27
|
priority: candidate.priority,
|
|
28
28
|
queuePosition,
|
|
29
29
|
}];
|
|
30
30
|
});
|
|
31
31
|
}
|
|
32
|
-
export function
|
|
33
|
-
return
|
|
32
|
+
export function shiftNextPendingRequestSlot(pendingRequestSlots) {
|
|
33
|
+
return pendingRequestSlots.shift();
|
|
34
34
|
}
|
|
35
|
-
export function
|
|
36
|
-
const index =
|
|
35
|
+
export function dropPendingRequestSlot(pendingRequestSlots, requestId) {
|
|
36
|
+
const index = pendingRequestSlots.findIndex((entry) => entry.requestId === requestId);
|
|
37
37
|
if (index < 0) {
|
|
38
38
|
return false;
|
|
39
39
|
}
|
|
40
|
-
const [entry] =
|
|
40
|
+
const [entry] = pendingRequestSlots.splice(index, 1);
|
|
41
41
|
entry?.abort();
|
|
42
42
|
return true;
|
|
43
43
|
}
|
|
@@ -1,64 +1,64 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import { type
|
|
3
|
-
export type
|
|
1
|
+
import type { RequestResult } from "../../../contracts/types.js";
|
|
2
|
+
import { type PendingRequestSlot } from "./run-queue.js";
|
|
3
|
+
export type RequestSlotConcurrencyConfig = {
|
|
4
4
|
maxConcurrentRuns?: number;
|
|
5
5
|
leaseMs: number;
|
|
6
6
|
heartbeatIntervalMs: number;
|
|
7
7
|
};
|
|
8
|
-
type
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
type RequestClaimPersistence = {
|
|
9
|
+
claimQueuedRequest(payload: {
|
|
10
|
+
sessionId: string;
|
|
11
|
+
requestId: string;
|
|
12
12
|
workerId: string;
|
|
13
13
|
claimedAt: string;
|
|
14
14
|
leaseExpiresAt: string;
|
|
15
15
|
}): Promise<unknown>;
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
renewRequestLease(payload: {
|
|
17
|
+
requestId: string;
|
|
18
18
|
workerId: string;
|
|
19
19
|
heartbeatAt: string;
|
|
20
20
|
leaseExpiresAt: string;
|
|
21
21
|
}): Promise<unknown>;
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
22
|
+
releaseRequestClaim(requestId: string): Promise<unknown>;
|
|
23
|
+
enqueueRequest(payload: {
|
|
24
|
+
sessionId: string;
|
|
25
|
+
requestId: string;
|
|
26
26
|
priority: number;
|
|
27
27
|
}): Promise<unknown>;
|
|
28
|
-
|
|
29
|
-
state:
|
|
28
|
+
getRequest(requestId: string): Promise<{
|
|
29
|
+
state: RequestResult["state"];
|
|
30
30
|
} | null>;
|
|
31
31
|
};
|
|
32
|
-
type
|
|
33
|
-
persistence:
|
|
32
|
+
type RequestSlotRuntime = {
|
|
33
|
+
persistence: RequestClaimPersistence;
|
|
34
34
|
workerId: string;
|
|
35
|
-
concurrencyConfig:
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
35
|
+
concurrencyConfig: RequestSlotConcurrencyConfig;
|
|
36
|
+
pendingRequestSlots: PendingRequestSlot[];
|
|
37
|
+
getActiveRequestSlots: () => number;
|
|
38
|
+
setActiveRequestSlots: (count: number) => void;
|
|
39
|
+
enqueuePendingRequestSlot: (entry: {
|
|
40
|
+
sessionId?: string;
|
|
41
|
+
requestId?: string;
|
|
42
42
|
priority: number;
|
|
43
43
|
activate: () => void | Promise<void>;
|
|
44
44
|
abort: () => void;
|
|
45
45
|
}) => Array<{
|
|
46
|
-
|
|
47
|
-
|
|
46
|
+
sessionId: string;
|
|
47
|
+
requestId: string;
|
|
48
48
|
priority: number;
|
|
49
49
|
queuePosition: number;
|
|
50
50
|
}>;
|
|
51
|
-
emit: (
|
|
52
|
-
|
|
51
|
+
emit: (sessionId: string, requestId: string, sequence: number, eventType: string, payload: Record<string, unknown>) => Promise<unknown>;
|
|
52
|
+
setRequestStateAndEmit: (sessionId: string, requestId: string, sequence: number, state: RequestResult["state"], options: {
|
|
53
53
|
previousState: string | null;
|
|
54
54
|
checkpointRef?: string | null;
|
|
55
55
|
error?: string;
|
|
56
56
|
}) => Promise<unknown>;
|
|
57
57
|
};
|
|
58
|
-
export declare function
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
activeState?:
|
|
58
|
+
export declare function acquireRequestSlot(runtime: RequestSlotRuntime, options?: {
|
|
59
|
+
sessionId?: string;
|
|
60
|
+
requestId?: string;
|
|
61
|
+
activeState?: RequestResult["state"];
|
|
62
62
|
priority?: number;
|
|
63
63
|
}): Promise<() => Promise<void>>;
|
|
64
64
|
export {};
|