@mondaydotcomorg/atp-server 0.17.14
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 +489 -0
- package/dist/aggregator/index.d.ts +59 -0
- package/dist/aggregator/index.d.ts.map +1 -0
- package/dist/aggregator/index.js +171 -0
- package/dist/aggregator/index.js.map +1 -0
- package/dist/callback/index.d.ts +98 -0
- package/dist/callback/index.d.ts.map +1 -0
- package/dist/callback/index.js +136 -0
- package/dist/callback/index.js.map +1 -0
- package/dist/client-sessions.d.ts +82 -0
- package/dist/client-sessions.d.ts.map +1 -0
- package/dist/client-sessions.js +174 -0
- package/dist/client-sessions.js.map +1 -0
- package/dist/controllers/definitions.controller.d.ts +4 -0
- package/dist/controllers/definitions.controller.d.ts.map +1 -0
- package/dist/controllers/definitions.controller.js +11 -0
- package/dist/controllers/definitions.controller.js.map +1 -0
- package/dist/controllers/execute.controller.d.ts +18 -0
- package/dist/controllers/execute.controller.d.ts.map +1 -0
- package/dist/controllers/execute.controller.js +122 -0
- package/dist/controllers/execute.controller.js.map +1 -0
- package/dist/controllers/info.controller.d.ts +3 -0
- package/dist/controllers/info.controller.d.ts.map +1 -0
- package/dist/controllers/info.controller.js +13 -0
- package/dist/controllers/info.controller.js.map +1 -0
- package/dist/controllers/resume.controller.d.ts +11 -0
- package/dist/controllers/resume.controller.d.ts.map +1 -0
- package/dist/controllers/resume.controller.js +61 -0
- package/dist/controllers/resume.controller.js.map +1 -0
- package/dist/controllers/search.controller.d.ts +4 -0
- package/dist/controllers/search.controller.d.ts.map +1 -0
- package/dist/controllers/search.controller.js +7 -0
- package/dist/controllers/search.controller.js.map +1 -0
- package/dist/controllers/stream.controller.d.ts +19 -0
- package/dist/controllers/stream.controller.d.ts.map +1 -0
- package/dist/controllers/stream.controller.js +141 -0
- package/dist/controllers/stream.controller.js.map +1 -0
- package/dist/core/config.d.ts +161 -0
- package/dist/core/config.d.ts.map +1 -0
- package/dist/core/config.js +7 -0
- package/dist/core/config.js.map +1 -0
- package/dist/core/http.d.ts +4 -0
- package/dist/core/http.d.ts.map +1 -0
- package/dist/core/http.js +17 -0
- package/dist/core/http.js.map +1 -0
- package/dist/create-server.d.ts +120 -0
- package/dist/create-server.d.ts.map +1 -0
- package/dist/create-server.js +423 -0
- package/dist/create-server.js.map +1 -0
- package/dist/execution-state/index.d.ts +95 -0
- package/dist/execution-state/index.d.ts.map +1 -0
- package/dist/execution-state/index.js +128 -0
- package/dist/execution-state/index.js.map +1 -0
- package/dist/executor/ast-provenance-bridge.d.ts +12 -0
- package/dist/executor/ast-provenance-bridge.d.ts.map +1 -0
- package/dist/executor/ast-provenance-bridge.js +66 -0
- package/dist/executor/ast-provenance-bridge.js.map +1 -0
- package/dist/executor/ast-tracking-runtime.d.ts +7 -0
- package/dist/executor/ast-tracking-runtime.d.ts.map +1 -0
- package/dist/executor/ast-tracking-runtime.js +559 -0
- package/dist/executor/ast-tracking-runtime.js.map +1 -0
- package/dist/executor/bootstrap-generated.d.ts +32 -0
- package/dist/executor/bootstrap-generated.d.ts.map +1 -0
- package/dist/executor/bootstrap-generated.js +90 -0
- package/dist/executor/bootstrap-generated.js.map +1 -0
- package/dist/executor/compiler-config.d.ts +32 -0
- package/dist/executor/compiler-config.d.ts.map +1 -0
- package/dist/executor/compiler-config.js +99 -0
- package/dist/executor/compiler-config.js.map +1 -0
- package/dist/executor/constants.d.ts +4 -0
- package/dist/executor/constants.d.ts.map +1 -0
- package/dist/executor/constants.js +4 -0
- package/dist/executor/constants.js.map +1 -0
- package/dist/executor/error-handler.d.ts +9 -0
- package/dist/executor/error-handler.d.ts.map +1 -0
- package/dist/executor/error-handler.js +95 -0
- package/dist/executor/error-handler.js.map +1 -0
- package/dist/executor/execution-error-handler.d.ts +7 -0
- package/dist/executor/execution-error-handler.d.ts.map +1 -0
- package/dist/executor/execution-error-handler.js +136 -0
- package/dist/executor/execution-error-handler.js.map +1 -0
- package/dist/executor/executor.d.ts +20 -0
- package/dist/executor/executor.d.ts.map +1 -0
- package/dist/executor/executor.js +452 -0
- package/dist/executor/executor.js.map +1 -0
- package/dist/executor/index.d.ts +4 -0
- package/dist/executor/index.d.ts.map +1 -0
- package/dist/executor/index.js +3 -0
- package/dist/executor/index.js.map +1 -0
- package/dist/executor/resume-handler.d.ts +9 -0
- package/dist/executor/resume-handler.d.ts.map +1 -0
- package/dist/executor/resume-handler.js +22 -0
- package/dist/executor/resume-handler.js.map +1 -0
- package/dist/executor/sandbox-builder.d.ts +29 -0
- package/dist/executor/sandbox-builder.d.ts.map +1 -0
- package/dist/executor/sandbox-builder.js +538 -0
- package/dist/executor/sandbox-builder.js.map +1 -0
- package/dist/executor/sandbox-injector.d.ts +7 -0
- package/dist/executor/sandbox-injector.d.ts.map +1 -0
- package/dist/executor/sandbox-injector.js +293 -0
- package/dist/executor/sandbox-injector.js.map +1 -0
- package/dist/executor/types.d.ts +21 -0
- package/dist/executor/types.d.ts.map +1 -0
- package/dist/executor/types.js +2 -0
- package/dist/executor/types.js.map +1 -0
- package/dist/explorer/index.d.ts +69 -0
- package/dist/explorer/index.d.ts.map +1 -0
- package/dist/explorer/index.js +228 -0
- package/dist/explorer/index.js.map +1 -0
- package/dist/handlers/definitions.handler.d.ts +3 -0
- package/dist/handlers/definitions.handler.d.ts.map +1 -0
- package/dist/handlers/definitions.handler.js +11 -0
- package/dist/handlers/definitions.handler.js.map +1 -0
- package/dist/handlers/execute.handler.d.ts +7 -0
- package/dist/handlers/execute.handler.d.ts.map +1 -0
- package/dist/handlers/execute.handler.js +225 -0
- package/dist/handlers/execute.handler.js.map +1 -0
- package/dist/handlers/explorer.handler.d.ts +4 -0
- package/dist/handlers/explorer.handler.d.ts.map +1 -0
- package/dist/handlers/explorer.handler.js +10 -0
- package/dist/handlers/explorer.handler.js.map +1 -0
- package/dist/handlers/init.handler.d.ts +5 -0
- package/dist/handlers/init.handler.d.ts.map +1 -0
- package/dist/handlers/init.handler.js +41 -0
- package/dist/handlers/init.handler.js.map +1 -0
- package/dist/handlers/resume.handler.d.ts +6 -0
- package/dist/handlers/resume.handler.d.ts.map +1 -0
- package/dist/handlers/resume.handler.js +256 -0
- package/dist/handlers/resume.handler.js.map +1 -0
- package/dist/handlers/search.handler.d.ts +5 -0
- package/dist/handlers/search.handler.d.ts.map +1 -0
- package/dist/handlers/search.handler.js +11 -0
- package/dist/handlers/search.handler.js.map +1 -0
- package/dist/http/request-handler.d.ts +15 -0
- package/dist/http/request-handler.d.ts.map +1 -0
- package/dist/http/request-handler.js +94 -0
- package/dist/http/request-handler.js.map +1 -0
- package/dist/http/router.d.ts +4 -0
- package/dist/http/router.d.ts.map +1 -0
- package/dist/http/router.js +32 -0
- package/dist/http/router.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/instrumentation/index.d.ts +5 -0
- package/dist/instrumentation/index.d.ts.map +1 -0
- package/dist/instrumentation/index.js +5 -0
- package/dist/instrumentation/index.js.map +1 -0
- package/dist/instrumentation/serializer.d.ts +61 -0
- package/dist/instrumentation/serializer.d.ts.map +1 -0
- package/dist/instrumentation/serializer.js +334 -0
- package/dist/instrumentation/serializer.js.map +1 -0
- package/dist/instrumentation/state-manager.d.ts +61 -0
- package/dist/instrumentation/state-manager.d.ts.map +1 -0
- package/dist/instrumentation/state-manager.js +205 -0
- package/dist/instrumentation/state-manager.js.map +1 -0
- package/dist/instrumentation/transformer.d.ts +9 -0
- package/dist/instrumentation/transformer.d.ts.map +1 -0
- package/dist/instrumentation/transformer.js +70 -0
- package/dist/instrumentation/transformer.js.map +1 -0
- package/dist/instrumentation/types.d.ts +59 -0
- package/dist/instrumentation/types.d.ts.map +1 -0
- package/dist/instrumentation/types.js +5 -0
- package/dist/instrumentation/types.js.map +1 -0
- package/dist/middleware/audit.d.ts +18 -0
- package/dist/middleware/audit.d.ts.map +1 -0
- package/dist/middleware/audit.js +76 -0
- package/dist/middleware/audit.js.map +1 -0
- package/dist/openapi/index.d.ts +133 -0
- package/dist/openapi/index.d.ts.map +1 -0
- package/dist/openapi/index.js +235 -0
- package/dist/openapi/index.js.map +1 -0
- package/dist/openapi-loader.d.ts +87 -0
- package/dist/openapi-loader.d.ts.map +1 -0
- package/dist/openapi-loader.js +491 -0
- package/dist/openapi-loader.js.map +1 -0
- package/dist/routes/index.d.ts +21 -0
- package/dist/routes/index.d.ts.map +1 -0
- package/dist/routes/index.js +47 -0
- package/dist/routes/index.js.map +1 -0
- package/dist/search/index.d.ts +48 -0
- package/dist/search/index.d.ts.map +1 -0
- package/dist/search/index.js +156 -0
- package/dist/search/index.js.map +1 -0
- package/dist/security/index.d.ts +2 -0
- package/dist/security/index.d.ts.map +1 -0
- package/dist/security/index.js +2 -0
- package/dist/security/index.js.map +1 -0
- package/dist/shutdown.d.ts +19 -0
- package/dist/shutdown.d.ts.map +1 -0
- package/dist/shutdown.js +87 -0
- package/dist/shutdown.js.map +1 -0
- package/dist/utils/banner.d.ts +12 -0
- package/dist/utils/banner.d.ts.map +1 -0
- package/dist/utils/banner.js +18 -0
- package/dist/utils/banner.js.map +1 -0
- package/dist/utils/context.d.ts +16 -0
- package/dist/utils/context.d.ts.map +1 -0
- package/dist/utils/context.js +44 -0
- package/dist/utils/context.js.map +1 -0
- package/dist/utils/error.d.ts +8 -0
- package/dist/utils/error.d.ts.map +1 -0
- package/dist/utils/error.js +17 -0
- package/dist/utils/error.js.map +1 -0
- package/dist/utils/hint-based-instrumentation.d.ts +14 -0
- package/dist/utils/hint-based-instrumentation.d.ts.map +1 -0
- package/dist/utils/hint-based-instrumentation.js +84 -0
- package/dist/utils/hint-based-instrumentation.js.map +1 -0
- package/dist/utils/index.d.ts +8 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +8 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/info.d.ts +20 -0
- package/dist/utils/info.d.ts.map +1 -0
- package/dist/utils/info.js +15 -0
- package/dist/utils/info.js.map +1 -0
- package/dist/utils/provenance-reattachment.d.ts +32 -0
- package/dist/utils/provenance-reattachment.d.ts.map +1 -0
- package/dist/utils/provenance-reattachment.js +115 -0
- package/dist/utils/provenance-reattachment.js.map +1 -0
- package/dist/utils/request.d.ts +21 -0
- package/dist/utils/request.d.ts.map +1 -0
- package/dist/utils/request.js +44 -0
- package/dist/utils/request.js.map +1 -0
- package/dist/utils/response.d.ts +30 -0
- package/dist/utils/response.d.ts.map +1 -0
- package/dist/utils/response.js +53 -0
- package/dist/utils/response.js.map +1 -0
- package/dist/utils/runtime-types.d.ts +6 -0
- package/dist/utils/runtime-types.d.ts.map +1 -0
- package/dist/utils/runtime-types.js +14 -0
- package/dist/utils/runtime-types.js.map +1 -0
- package/dist/utils/schema.d.ts +9 -0
- package/dist/utils/schema.d.ts.map +1 -0
- package/dist/utils/schema.js +13 -0
- package/dist/utils/schema.js.map +1 -0
- package/dist/utils/token-emitter.d.ts +21 -0
- package/dist/utils/token-emitter.d.ts.map +1 -0
- package/dist/utils/token-emitter.js +129 -0
- package/dist/utils/token-emitter.js.map +1 -0
- package/dist/validator/index.d.ts +36 -0
- package/dist/validator/index.d.ts.map +1 -0
- package/dist/validator/index.js +224 -0
- package/dist/validator/index.js.map +1 -0
- package/package.json +68 -0
- package/src/aggregator/index.ts +207 -0
- package/src/callback/index.ts +191 -0
- package/src/client-sessions.ts +234 -0
- package/src/controllers/definitions.controller.ts +19 -0
- package/src/controllers/execute.controller.ts +166 -0
- package/src/controllers/info.controller.ts +14 -0
- package/src/controllers/resume.controller.ts +92 -0
- package/src/controllers/search.controller.ts +16 -0
- package/src/controllers/stream.controller.ts +190 -0
- package/src/core/config.ts +180 -0
- package/src/core/http.ts +21 -0
- package/src/create-server.ts +536 -0
- package/src/execution-state/index.ts +204 -0
- package/src/executor/ast-provenance-bridge.ts +80 -0
- package/src/executor/ast-tracking-runtime.ts +558 -0
- package/src/executor/bootstrap-generated.ts +90 -0
- package/src/executor/compiler-config.ts +146 -0
- package/src/executor/constants.ts +5 -0
- package/src/executor/error-handler.ts +118 -0
- package/src/executor/execution-error-handler.ts +178 -0
- package/src/executor/executor.ts +631 -0
- package/src/executor/index.ts +3 -0
- package/src/executor/resume-handler.ts +39 -0
- package/src/executor/sandbox-builder.ts +684 -0
- package/src/executor/sandbox-injector.ts +345 -0
- package/src/executor/types.ts +22 -0
- package/src/explorer/index.ts +297 -0
- package/src/handlers/definitions.handler.ts +13 -0
- package/src/handlers/execute.handler.ts +286 -0
- package/src/handlers/explorer.handler.ts +18 -0
- package/src/handlers/init.handler.ts +53 -0
- package/src/handlers/resume.handler.ts +316 -0
- package/src/handlers/search.handler.ts +32 -0
- package/src/http/request-handler.ts +117 -0
- package/src/http/router.ts +29 -0
- package/src/index.ts +60 -0
- package/src/instrumentation/index.ts +4 -0
- package/src/instrumentation/serializer.ts +421 -0
- package/src/instrumentation/state-manager.ts +237 -0
- package/src/instrumentation/transformer.ts +84 -0
- package/src/instrumentation/types.ts +76 -0
- package/src/middleware/audit.ts +101 -0
- package/src/openapi/index.ts +378 -0
- package/src/openapi-loader.ts +744 -0
- package/src/routes/index.ts +93 -0
- package/src/search/index.ts +216 -0
- package/src/security/index.ts +1 -0
- package/src/shutdown.ts +108 -0
- package/src/utils/banner.ts +25 -0
- package/src/utils/context.ts +58 -0
- package/src/utils/error.ts +25 -0
- package/src/utils/hint-based-instrumentation.ts +99 -0
- package/src/utils/index.ts +15 -0
- package/src/utils/info.ts +31 -0
- package/src/utils/provenance-reattachment.ts +144 -0
- package/src/utils/request.ts +53 -0
- package/src/utils/response.ts +69 -0
- package/src/utils/runtime-types.ts +14 -0
- package/src/utils/schema.ts +18 -0
- package/src/utils/token-emitter.ts +182 -0
- package/src/validator/index.ts +253 -0
|
@@ -0,0 +1,631 @@
|
|
|
1
|
+
import ivm from 'isolated-vm';
|
|
2
|
+
import type {
|
|
3
|
+
ExecutionConfig,
|
|
4
|
+
ExecutionResult,
|
|
5
|
+
APIGroupConfig,
|
|
6
|
+
ClientToolDefinition,
|
|
7
|
+
} from '@mondaydotcomorg/atp-protocol';
|
|
8
|
+
import { ExecutionStatus, ProvenanceMode } from '@mondaydotcomorg/atp-protocol';
|
|
9
|
+
import {
|
|
10
|
+
log,
|
|
11
|
+
setPauseForClient,
|
|
12
|
+
setReplayMode,
|
|
13
|
+
setProgressCallback,
|
|
14
|
+
initializeVectorStore,
|
|
15
|
+
runInExecutionContext,
|
|
16
|
+
setVectorStoreExecutionId,
|
|
17
|
+
clearVectorStoreExecutionId,
|
|
18
|
+
initializeApproval,
|
|
19
|
+
} from '@mondaydotcomorg/atp-runtime';
|
|
20
|
+
import { nanoid } from 'nanoid';
|
|
21
|
+
import type { CallbackRecord } from '../execution-state/index.js';
|
|
22
|
+
import type { ClientSessionManager } from '../client-sessions.js';
|
|
23
|
+
import { BOOTSTRAP_CODE } from './bootstrap-generated.js';
|
|
24
|
+
import type { RuntimeContext, ExecutorConfig } from './types.js';
|
|
25
|
+
import { SandboxBuilder } from './sandbox-builder.js';
|
|
26
|
+
import { StateManager, CodeInstrumentor } from '../instrumentation/index.js';
|
|
27
|
+
import { ATP_COMPILER_ENABLED } from './constants.js';
|
|
28
|
+
import { transformCodeWithCompiler, getCompilerRuntime } from './compiler-config.js';
|
|
29
|
+
import { setupResumeExecution } from './resume-handler.js';
|
|
30
|
+
import {
|
|
31
|
+
injectTimerPolyfills,
|
|
32
|
+
injectSandbox,
|
|
33
|
+
setupAPINamespace,
|
|
34
|
+
setupRuntimeNamespace,
|
|
35
|
+
} from './sandbox-injector.js';
|
|
36
|
+
import { handleExecutionError } from './execution-error-handler.js';
|
|
37
|
+
import {
|
|
38
|
+
SecurityPolicyEngine,
|
|
39
|
+
instrumentCode as astInstrumentCode,
|
|
40
|
+
createTrackingRuntime,
|
|
41
|
+
setProvenanceExecutionId,
|
|
42
|
+
clearProvenanceExecutionId,
|
|
43
|
+
cleanupProvenanceForExecution,
|
|
44
|
+
captureProvenanceState,
|
|
45
|
+
restoreProvenanceState,
|
|
46
|
+
captureProvenanceSnapshot,
|
|
47
|
+
registerProvenanceMetadata,
|
|
48
|
+
} from '@mondaydotcomorg/atp-provenance';
|
|
49
|
+
import {
|
|
50
|
+
registerIsolateContext,
|
|
51
|
+
unregisterIsolateContext,
|
|
52
|
+
createASTProvenanceChecker,
|
|
53
|
+
} from './ast-provenance-bridge.js';
|
|
54
|
+
import { instrumentLiteralsFromHints } from '../utils/hint-based-instrumentation.js';
|
|
55
|
+
import { getHintMap, reattachProvenanceFromHints } from '../utils/provenance-reattachment.js';
|
|
56
|
+
export class SandboxExecutor {
|
|
57
|
+
private config: ExecutorConfig;
|
|
58
|
+
private sandboxBuilder: SandboxBuilder;
|
|
59
|
+
private approvalHandler?: (request: any) => Promise<any>;
|
|
60
|
+
private sessionManager?: ClientSessionManager;
|
|
61
|
+
|
|
62
|
+
constructor(
|
|
63
|
+
config: ExecutorConfig,
|
|
64
|
+
apiGroups: APIGroupConfig[] = [],
|
|
65
|
+
approvalHandler?: (request: any) => Promise<any>,
|
|
66
|
+
sessionManager?: ClientSessionManager
|
|
67
|
+
) {
|
|
68
|
+
this.config = config;
|
|
69
|
+
this.sandboxBuilder = new SandboxBuilder(apiGroups);
|
|
70
|
+
this.approvalHandler = approvalHandler;
|
|
71
|
+
this.sessionManager = sessionManager;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async execute(
|
|
75
|
+
code: string,
|
|
76
|
+
config: ExecutionConfig,
|
|
77
|
+
clientId?: string,
|
|
78
|
+
resumeData?: {
|
|
79
|
+
callbackHistory: CallbackRecord[];
|
|
80
|
+
newCallbackResult: unknown;
|
|
81
|
+
executionId?: string;
|
|
82
|
+
}
|
|
83
|
+
): Promise<ExecutionResult> {
|
|
84
|
+
const executionId = resumeData?.executionId || nanoid();
|
|
85
|
+
|
|
86
|
+
return runInExecutionContext(executionId, async () => {
|
|
87
|
+
return await this.executeInContext(
|
|
88
|
+
executionId,
|
|
89
|
+
clientId || 'anonymous',
|
|
90
|
+
code,
|
|
91
|
+
config,
|
|
92
|
+
resumeData
|
|
93
|
+
);
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
private async executeInContext(
|
|
98
|
+
executionId: string,
|
|
99
|
+
clientId: string,
|
|
100
|
+
code: string,
|
|
101
|
+
config: ExecutionConfig,
|
|
102
|
+
resumeData?: {
|
|
103
|
+
callbackHistory: CallbackRecord[];
|
|
104
|
+
newCallbackResult: unknown;
|
|
105
|
+
}
|
|
106
|
+
): Promise<ExecutionResult & { transformedCode?: string }> {
|
|
107
|
+
const context: RuntimeContext = {
|
|
108
|
+
llmCallCount: 0,
|
|
109
|
+
approvalCallCount: 0,
|
|
110
|
+
logs: [],
|
|
111
|
+
startTime: Date.now(),
|
|
112
|
+
maxLLMCalls: config.maxLLMCalls,
|
|
113
|
+
executionId,
|
|
114
|
+
clientId,
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
setVectorStoreExecutionId(executionId);
|
|
118
|
+
initializeVectorStore(executionId);
|
|
119
|
+
|
|
120
|
+
const executionLogger = log.child({ executionId, clientId });
|
|
121
|
+
|
|
122
|
+
if (config.provenanceMode && config.provenanceMode !== ProvenanceMode.NONE) {
|
|
123
|
+
setProvenanceExecutionId(executionId);
|
|
124
|
+
executionLogger.debug('Provenance execution tracking initialized', { executionId });
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (this.approvalHandler) {
|
|
128
|
+
initializeApproval(async (request: { message: any }) => {
|
|
129
|
+
executionLogger.debug('Approval requested', { message: request.message });
|
|
130
|
+
const response = await this.approvalHandler!(request);
|
|
131
|
+
return response;
|
|
132
|
+
});
|
|
133
|
+
executionLogger.debug('Approval handler initialized');
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
let stateManager: StateManager | null = null;
|
|
137
|
+
if (this.config.cacheProvider) {
|
|
138
|
+
stateManager = new StateManager(
|
|
139
|
+
executionId,
|
|
140
|
+
clientId,
|
|
141
|
+
this.config.cacheProvider,
|
|
142
|
+
executionLogger
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
if (resumeData) {
|
|
146
|
+
const loaded = await stateManager.loadForResume(executionId);
|
|
147
|
+
if (loaded) {
|
|
148
|
+
executionLogger.info('State loaded for resume', {
|
|
149
|
+
executionId,
|
|
150
|
+
statementsRestored: stateManager.getStats().statementsExecuted,
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (config.progressCallback) {
|
|
157
|
+
setProgressCallback(config.progressCallback);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const callbackHistory: CallbackRecord[] = [];
|
|
161
|
+
|
|
162
|
+
if (resumeData) {
|
|
163
|
+
setupResumeExecution(resumeData, callbackHistory, executionLogger);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (
|
|
167
|
+
config.clientServices &&
|
|
168
|
+
(config.clientServices.hasLLM ||
|
|
169
|
+
config.clientServices.hasApproval ||
|
|
170
|
+
config.clientServices.hasEmbedding ||
|
|
171
|
+
config.clientServices.hasTools)
|
|
172
|
+
) {
|
|
173
|
+
setPauseForClient(true);
|
|
174
|
+
executionLogger.debug('Client services detected, pause mode enabled', {
|
|
175
|
+
hasLLM: config.clientServices.hasLLM,
|
|
176
|
+
hasApproval: config.clientServices.hasApproval,
|
|
177
|
+
hasEmbedding: config.clientServices.hasEmbedding,
|
|
178
|
+
hasTools: config.clientServices.hasTools,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const isolate = new ivm.Isolate({
|
|
183
|
+
memoryLimit: Math.floor(config.maxMemory / (1024 * 1024)),
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
const memoryBefore = process.memoryUsage().heapUsed;
|
|
187
|
+
|
|
188
|
+
let pauseError: unknown = null;
|
|
189
|
+
const onPauseError = (error: unknown) => {
|
|
190
|
+
pauseError = error;
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
let codeToExecute = code;
|
|
194
|
+
let alreadyTransformed = false;
|
|
195
|
+
|
|
196
|
+
try {
|
|
197
|
+
const ivmContext = await isolate.createContext();
|
|
198
|
+
const jail = ivmContext.global;
|
|
199
|
+
|
|
200
|
+
await jail.set('global', jail.derefInto());
|
|
201
|
+
|
|
202
|
+
await injectTimerPolyfills(ivmContext);
|
|
203
|
+
|
|
204
|
+
let result: unknown = null;
|
|
205
|
+
|
|
206
|
+
const provenanceMode = config.provenanceMode || ProvenanceMode.NONE;
|
|
207
|
+
let policyEngine: SecurityPolicyEngine | undefined;
|
|
208
|
+
let astTracker: ReturnType<typeof createTrackingRuntime> | undefined;
|
|
209
|
+
|
|
210
|
+
if (
|
|
211
|
+
provenanceMode !== ProvenanceMode.NONE &&
|
|
212
|
+
config.securityPolicies &&
|
|
213
|
+
config.securityPolicies.length > 0
|
|
214
|
+
) {
|
|
215
|
+
policyEngine = new SecurityPolicyEngine(config.securityPolicies, executionLogger);
|
|
216
|
+
|
|
217
|
+
if (this.approvalHandler) {
|
|
218
|
+
policyEngine.setApprovalCallback(
|
|
219
|
+
async (message: string, context: Record<string, unknown>) => {
|
|
220
|
+
executionLogger.debug('Policy engine requesting approval', { message, context });
|
|
221
|
+
const response = await this.approvalHandler!({ message, context });
|
|
222
|
+
return response.approved === true;
|
|
223
|
+
}
|
|
224
|
+
);
|
|
225
|
+
executionLogger.debug('Approval callback connected to policy engine');
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
executionLogger.info('Security policy engine initialized', {
|
|
229
|
+
provenanceMode,
|
|
230
|
+
policies: config.securityPolicies.map((p) => p.name),
|
|
231
|
+
hasApprovalCallback: !!this.approvalHandler,
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if (provenanceMode === ProvenanceMode.AST) {
|
|
236
|
+
registerIsolateContext(executionId, ivmContext);
|
|
237
|
+
executionLogger.info('Registered isolate context for AST provenance bridge', {
|
|
238
|
+
executionId,
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
if (policyEngine) {
|
|
242
|
+
const astChecker = createASTProvenanceChecker(executionId);
|
|
243
|
+
policyEngine.setGetProvenance(astChecker);
|
|
244
|
+
executionLogger.info('Set AST provenance checker for policy engine', { executionId });
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (provenanceMode === ProvenanceMode.AST) {
|
|
249
|
+
astTracker = createTrackingRuntime();
|
|
250
|
+
executionLogger.debug('AST provenance tracker initialized');
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
let clientTools: ClientToolDefinition[] = [];
|
|
254
|
+
if (this.sessionManager && clientId && clientId !== 'anonymous') {
|
|
255
|
+
try {
|
|
256
|
+
const session = await this.sessionManager.getSession(clientId);
|
|
257
|
+
if (session?.tools && session.tools.length > 0) {
|
|
258
|
+
clientTools = session.tools;
|
|
259
|
+
}
|
|
260
|
+
} catch (error) {}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const sandbox = this.sandboxBuilder.createSandbox(
|
|
264
|
+
context,
|
|
265
|
+
config,
|
|
266
|
+
executionLogger,
|
|
267
|
+
executionId,
|
|
268
|
+
policyEngine,
|
|
269
|
+
clientTools
|
|
270
|
+
);
|
|
271
|
+
|
|
272
|
+
if (astTracker) {
|
|
273
|
+
Object.assign(sandbox, astTracker.runtime);
|
|
274
|
+
executionLogger.debug('AST tracking runtime injected into sandbox', {
|
|
275
|
+
runtimeFunctions: Object.keys(astTracker.runtime),
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (stateManager) {
|
|
280
|
+
sandbox.__state = {
|
|
281
|
+
capture: async (statementId: number, getVars: () => Record<string, unknown>) => {
|
|
282
|
+
return await stateManager.capture(statementId, getVars);
|
|
283
|
+
},
|
|
284
|
+
call: async (statementId: number, fn: () => unknown) => {
|
|
285
|
+
return await stateManager.call(statementId, fn);
|
|
286
|
+
},
|
|
287
|
+
branch: (statementId: number, condition: boolean) => {
|
|
288
|
+
return stateManager.branch(statementId, condition);
|
|
289
|
+
},
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
if (ATP_COMPILER_ENABLED) {
|
|
294
|
+
sandbox.__runtime = getCompilerRuntime();
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
let hintMetadata: Map<string, any> | undefined;
|
|
298
|
+
if (provenanceMode === ProvenanceMode.AST) {
|
|
299
|
+
hintMetadata = getHintMap(executionId);
|
|
300
|
+
|
|
301
|
+
if (hintMetadata && hintMetadata.size > 0) {
|
|
302
|
+
for (const [digest, metadata] of hintMetadata.entries()) {
|
|
303
|
+
executionLogger.info('Registering hint by digest', {
|
|
304
|
+
digest: digest.substring(0, 20),
|
|
305
|
+
hasSource: !!metadata?.source,
|
|
306
|
+
sourceType: metadata?.source?.type,
|
|
307
|
+
metadataKeys: metadata ? Object.keys(metadata) : [],
|
|
308
|
+
});
|
|
309
|
+
registerProvenanceMetadata(digest, metadata, executionId);
|
|
310
|
+
}
|
|
311
|
+
executionLogger.info('Registered hint metadata in host registry', {
|
|
312
|
+
hintCount: hintMetadata.size,
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
await injectSandbox(
|
|
318
|
+
ivmContext,
|
|
319
|
+
jail,
|
|
320
|
+
sandbox,
|
|
321
|
+
executionLogger,
|
|
322
|
+
onPauseError,
|
|
323
|
+
executionId,
|
|
324
|
+
provenanceMode,
|
|
325
|
+
hintMetadata
|
|
326
|
+
);
|
|
327
|
+
|
|
328
|
+
await ivmContext.eval(BOOTSTRAP_CODE);
|
|
329
|
+
|
|
330
|
+
await setupAPINamespace(ivmContext, sandbox, provenanceMode);
|
|
331
|
+
|
|
332
|
+
if (ATP_COMPILER_ENABLED) {
|
|
333
|
+
await setupRuntimeNamespace(ivmContext, sandbox);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
let useCompiler = false;
|
|
337
|
+
let astInstrumented = false;
|
|
338
|
+
|
|
339
|
+
const isResume = resumeData !== undefined;
|
|
340
|
+
const isAlreadyWrapped = code.trim().startsWith('(async function');
|
|
341
|
+
alreadyTransformed = isAlreadyWrapped;
|
|
342
|
+
|
|
343
|
+
if (isAlreadyWrapped && provenanceMode === ProvenanceMode.AST) {
|
|
344
|
+
astInstrumented = true;
|
|
345
|
+
executionLogger.info('Code already AST-instrumented (from previous execution)', {
|
|
346
|
+
codeLength: code.length,
|
|
347
|
+
isResume,
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
executionLogger.info('Instrumentation decision', {
|
|
352
|
+
provenanceMode,
|
|
353
|
+
useCompiler,
|
|
354
|
+
alreadyTransformed,
|
|
355
|
+
isResume,
|
|
356
|
+
resumeDataPresent: !!resumeData,
|
|
357
|
+
astCondition: provenanceMode === ProvenanceMode.AST && !useCompiler && !alreadyTransformed,
|
|
358
|
+
codeLength: code.length,
|
|
359
|
+
codePreview: code.substring(0, 100),
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
if (provenanceMode === ProvenanceMode.AST && !useCompiler && !alreadyTransformed) {
|
|
363
|
+
try {
|
|
364
|
+
const instrumentResult = astInstrumentCode(code);
|
|
365
|
+
codeToExecute = instrumentResult.code;
|
|
366
|
+
astInstrumented = true;
|
|
367
|
+
executionLogger.info('Code instrumented for provenance tracking (AST mode)', {
|
|
368
|
+
trackingCalls: instrumentResult.metadata.trackingCalls,
|
|
369
|
+
instrumentedCodeStart: codeToExecute.substring(0, 150),
|
|
370
|
+
instrumentedCodeEnd: codeToExecute.substring(codeToExecute.length - 150),
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
if (hintMetadata && hintMetadata.size > 0) {
|
|
374
|
+
const hintInstrumented = instrumentLiteralsFromHints(codeToExecute, hintMetadata);
|
|
375
|
+
if (hintInstrumented.taintedCount > 0) {
|
|
376
|
+
codeToExecute = hintInstrumented.code;
|
|
377
|
+
executionLogger.info('Applied hint instrumentation to AST code', {
|
|
378
|
+
taintedCount: hintInstrumented.taintedCount,
|
|
379
|
+
hintsAvailable: hintMetadata.size,
|
|
380
|
+
finalCodeStart: codeToExecute.substring(0, 200),
|
|
381
|
+
finalCodeEnd: codeToExecute.substring(codeToExecute.length - 200),
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
} catch (error) {
|
|
386
|
+
executionLogger.warn(
|
|
387
|
+
'Failed to instrument code for provenance, executing without tracking',
|
|
388
|
+
{
|
|
389
|
+
error: error instanceof Error ? error.message : String(error),
|
|
390
|
+
codeLength: code.length,
|
|
391
|
+
codeStart: code.substring(0, 100),
|
|
392
|
+
codeEnd: code.substring(code.length - 100),
|
|
393
|
+
}
|
|
394
|
+
);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
if (
|
|
399
|
+
ATP_COMPILER_ENABLED &&
|
|
400
|
+
this.config.cacheProvider &&
|
|
401
|
+
!astInstrumented &&
|
|
402
|
+
!alreadyTransformed
|
|
403
|
+
) {
|
|
404
|
+
const compilerResult = await transformCodeWithCompiler(
|
|
405
|
+
code,
|
|
406
|
+
executionId,
|
|
407
|
+
this.config.cacheProvider,
|
|
408
|
+
executionLogger
|
|
409
|
+
);
|
|
410
|
+
codeToExecute = compilerResult.code;
|
|
411
|
+
useCompiler = compilerResult.useCompiler;
|
|
412
|
+
} else if (alreadyTransformed) {
|
|
413
|
+
codeToExecute = code;
|
|
414
|
+
useCompiler = true;
|
|
415
|
+
executionLogger.debug('Using already-transformed code on resume');
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
if (!useCompiler && !astInstrumented && stateManager) {
|
|
419
|
+
try {
|
|
420
|
+
const instrumentor = new CodeInstrumentor();
|
|
421
|
+
const instrumented = instrumentor.instrument(code);
|
|
422
|
+
codeToExecute = instrumented.code;
|
|
423
|
+
executionLogger.debug('Code instrumented for state capture', {
|
|
424
|
+
statements: instrumented.metadata.statements.length,
|
|
425
|
+
variables: instrumented.metadata.variables.size,
|
|
426
|
+
functions: instrumented.metadata.functions.length,
|
|
427
|
+
});
|
|
428
|
+
} catch (error) {
|
|
429
|
+
executionLogger.warn('Failed to instrument code, executing without state capture', {
|
|
430
|
+
error,
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
const wrappedCode = astInstrumented
|
|
436
|
+
? `${codeToExecute}()`
|
|
437
|
+
: `
|
|
438
|
+
(async function() {
|
|
439
|
+
${codeToExecute}
|
|
440
|
+
})();
|
|
441
|
+
`;
|
|
442
|
+
|
|
443
|
+
executionLogger.debug('Final wrapped code', {
|
|
444
|
+
astInstrumented,
|
|
445
|
+
codeLength: wrappedCode.length,
|
|
446
|
+
codeStart: wrappedCode.substring(0, 200),
|
|
447
|
+
codeEnd: wrappedCode.substring(wrappedCode.length - 100),
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
const script = await isolate.compileScript(wrappedCode);
|
|
451
|
+
result = await script.run(ivmContext, { timeout: config.timeout, promise: true, copy: true });
|
|
452
|
+
|
|
453
|
+
if (pauseError) {
|
|
454
|
+
throw pauseError;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
const memoryAfter = process.memoryUsage().heapUsed;
|
|
458
|
+
const memoryUsed = Math.max(0, memoryAfter - memoryBefore);
|
|
459
|
+
|
|
460
|
+
if (stateManager) {
|
|
461
|
+
await stateManager.persist();
|
|
462
|
+
executionLogger.info('Final state persisted', {
|
|
463
|
+
executionId,
|
|
464
|
+
statements: stateManager.getStats().statementsExecuted,
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
if (provenanceMode === ProvenanceMode.AST && ivmContext) {
|
|
469
|
+
try {
|
|
470
|
+
const trackTest = await ivmContext.eval(
|
|
471
|
+
`
|
|
472
|
+
(function() {
|
|
473
|
+
if (typeof globalThis.__track !== 'function') {
|
|
474
|
+
return { error: '__track not found', type: typeof globalThis.__track };
|
|
475
|
+
}
|
|
476
|
+
try {
|
|
477
|
+
const testObj = { test: 'value' };
|
|
478
|
+
const tracked = globalThis.__track(testObj, { type: 'tool', tool: 'test' }, []);
|
|
479
|
+
const metadata = globalThis.__get_all_metadata ? globalThis.__get_all_metadata() : [];
|
|
480
|
+
return { success: true, metadataCount: metadata.length, tracked: !!tracked };
|
|
481
|
+
} catch (e) {
|
|
482
|
+
return { error: String(e) };
|
|
483
|
+
}
|
|
484
|
+
})()
|
|
485
|
+
`,
|
|
486
|
+
{ copy: true }
|
|
487
|
+
);
|
|
488
|
+
executionLogger.info('Track function test', trackTest);
|
|
489
|
+
|
|
490
|
+
const metadataArray = await ivmContext.eval(
|
|
491
|
+
'globalThis.__get_all_metadata ? globalThis.__get_all_metadata() : []',
|
|
492
|
+
{ copy: true }
|
|
493
|
+
);
|
|
494
|
+
executionLogger.info('Extracted AST metadata from isolate', {
|
|
495
|
+
entries: Array.isArray(metadataArray) ? metadataArray.length : 0,
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
if (Array.isArray(metadataArray) && metadataArray.length > 0) {
|
|
499
|
+
for (const [id, metadata] of metadataArray) {
|
|
500
|
+
executionLogger.info('Registering metadata', {
|
|
501
|
+
id,
|
|
502
|
+
idType: typeof id,
|
|
503
|
+
hasMetadata: !!metadata,
|
|
504
|
+
metadataKeys: metadata ? Object.keys(metadata) : [],
|
|
505
|
+
});
|
|
506
|
+
registerProvenanceMetadata(id, metadata, executionId);
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
executionLogger.info('Linked AST metadata to host registry', {
|
|
510
|
+
count: metadataArray.length,
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
} catch (error) {
|
|
514
|
+
executionLogger.warn('Failed to extract AST metadata from isolate', {
|
|
515
|
+
error: error instanceof Error ? error.message : String(error),
|
|
516
|
+
stack: error instanceof Error ? error.stack : undefined,
|
|
517
|
+
});
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
ivmContext.release();
|
|
522
|
+
isolate.dispose();
|
|
523
|
+
|
|
524
|
+
const stats: any = {
|
|
525
|
+
duration: Date.now() - context.startTime,
|
|
526
|
+
memoryUsed,
|
|
527
|
+
llmCallsCount: context.llmCallCount,
|
|
528
|
+
approvalCallsCount: context.approvalCallCount,
|
|
529
|
+
};
|
|
530
|
+
|
|
531
|
+
if (stateManager) {
|
|
532
|
+
const captureStats = stateManager.getStats();
|
|
533
|
+
stats.statementsExecuted = captureStats.statementsExecuted;
|
|
534
|
+
stats.statementsCached = captureStats.statementsCached;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
let provenanceSnapshot: unknown;
|
|
538
|
+
if (provenanceMode !== ProvenanceMode.NONE) {
|
|
539
|
+
try {
|
|
540
|
+
provenanceSnapshot = captureProvenanceSnapshot(executionId);
|
|
541
|
+
executionLogger.debug('Provenance snapshot captured', {
|
|
542
|
+
registrySize: (provenanceSnapshot as any)?.registry?.length || 0,
|
|
543
|
+
primitivesSize: (provenanceSnapshot as any)?.primitives?.length || 0,
|
|
544
|
+
});
|
|
545
|
+
} catch (error) {
|
|
546
|
+
executionLogger.warn('Failed to capture provenance snapshot', { error });
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
return {
|
|
551
|
+
executionId,
|
|
552
|
+
status: ExecutionStatus.COMPLETED,
|
|
553
|
+
result,
|
|
554
|
+
stats,
|
|
555
|
+
transformedCode: codeToExecute !== code || alreadyTransformed ? codeToExecute : undefined,
|
|
556
|
+
provenanceSnapshot,
|
|
557
|
+
};
|
|
558
|
+
} catch (error) {
|
|
559
|
+
if (stateManager) {
|
|
560
|
+
try {
|
|
561
|
+
await stateManager.persist();
|
|
562
|
+
executionLogger.debug('State persisted after error for potential resume');
|
|
563
|
+
} catch (persistError) {
|
|
564
|
+
executionLogger.error('Failed to persist state after error', { persistError });
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
return handleExecutionError(
|
|
569
|
+
error,
|
|
570
|
+
pauseError,
|
|
571
|
+
context,
|
|
572
|
+
executionId,
|
|
573
|
+
callbackHistory,
|
|
574
|
+
memoryBefore,
|
|
575
|
+
executionLogger,
|
|
576
|
+
isolate,
|
|
577
|
+
codeToExecute !== code || alreadyTransformed ? codeToExecute : undefined
|
|
578
|
+
);
|
|
579
|
+
} finally {
|
|
580
|
+
this.cleanup(executionId, config.provenanceMode);
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
private cleanProvenanceIds(value: unknown): unknown {
|
|
585
|
+
if (value === null || value === undefined) {
|
|
586
|
+
return value;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
if (typeof value !== 'object') {
|
|
590
|
+
return value;
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
if (Array.isArray(value)) {
|
|
594
|
+
return value.map((item) => this.cleanProvenanceIds(item));
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
const cleaned: Record<string, unknown> = {};
|
|
598
|
+
const allKeys = Object.getOwnPropertyNames(value);
|
|
599
|
+
for (const key of allKeys) {
|
|
600
|
+
if (key !== '__prov_id__') {
|
|
601
|
+
cleaned[key] = this.cleanProvenanceIds((value as Record<string, unknown>)[key]);
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
return cleaned;
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
private cleanup(executionId?: string, provenanceMode?: string): void {
|
|
608
|
+
try {
|
|
609
|
+
setPauseForClient(false);
|
|
610
|
+
} catch (e) {}
|
|
611
|
+
try {
|
|
612
|
+
setReplayMode(undefined);
|
|
613
|
+
} catch (e) {}
|
|
614
|
+
|
|
615
|
+
if (executionId && provenanceMode === ProvenanceMode.AST) {
|
|
616
|
+
try {
|
|
617
|
+
unregisterIsolateContext(executionId);
|
|
618
|
+
} catch (e) {}
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
if (executionId && provenanceMode && provenanceMode !== ProvenanceMode.NONE) {
|
|
622
|
+
try {
|
|
623
|
+
cleanupProvenanceForExecution(executionId);
|
|
624
|
+
clearProvenanceExecutionId();
|
|
625
|
+
} catch (e) {}
|
|
626
|
+
}
|
|
627
|
+
setProgressCallback(null);
|
|
628
|
+
|
|
629
|
+
clearVectorStoreExecutionId();
|
|
630
|
+
}
|
|
631
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { Logger } from '@mondaydotcomorg/atp-runtime';
|
|
2
|
+
import { setReplayMode } from '@mondaydotcomorg/atp-runtime';
|
|
3
|
+
import type { CallbackRecord } from '../execution-state/index.js';
|
|
4
|
+
|
|
5
|
+
export interface ResumeData {
|
|
6
|
+
callbackHistory: CallbackRecord[];
|
|
7
|
+
newCallbackResult: unknown;
|
|
8
|
+
executionId?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function setupResumeExecution(
|
|
12
|
+
resumeData: ResumeData,
|
|
13
|
+
callbackHistory: CallbackRecord[],
|
|
14
|
+
executionLogger: Logger
|
|
15
|
+
): void {
|
|
16
|
+
executionLogger.info('Resuming execution with callback history', {
|
|
17
|
+
historyLength: resumeData.callbackHistory.length,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const replayMap = new Map<number, unknown>();
|
|
21
|
+
|
|
22
|
+
for (const record of resumeData.callbackHistory) {
|
|
23
|
+
if (record.result !== undefined) {
|
|
24
|
+
replayMap.set(record.sequenceNumber, record.result);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const lastCallback = resumeData.callbackHistory[resumeData.callbackHistory.length - 1];
|
|
29
|
+
if (lastCallback) {
|
|
30
|
+
replayMap.set(lastCallback.sequenceNumber, resumeData.newCallbackResult);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
setReplayMode(replayMap);
|
|
34
|
+
callbackHistory.push(...resumeData.callbackHistory);
|
|
35
|
+
|
|
36
|
+
executionLogger.debug('Replay map configured', {
|
|
37
|
+
replayEntries: replayMap.size,
|
|
38
|
+
});
|
|
39
|
+
}
|