@archmax-ai/harness 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +219 -0
- package/dist/agent.d.ts +174 -0
- package/dist/agent.js +140 -0
- package/dist/agent.js.map +1 -0
- package/dist/assembly/compose.d.ts +170 -0
- package/dist/assembly/compose.js +391 -0
- package/dist/assembly/compose.js.map +1 -0
- package/dist/assembly/delegation-registry.d.ts +5 -0
- package/dist/assembly/delegation-registry.js +118 -0
- package/dist/assembly/delegation-registry.js.map +1 -0
- package/dist/assembly/index.d.ts +192 -0
- package/dist/assembly/index.js +265 -0
- package/dist/assembly/index.js.map +1 -0
- package/dist/assembly/plain.d.ts +7 -0
- package/dist/assembly/plain.js +41 -0
- package/dist/assembly/plain.js.map +1 -0
- package/dist/authoring-skill/archmax-harness/README.md +76 -0
- package/dist/authoring-skill/archmax-harness/SKILL.md +241 -0
- package/dist/authoring-skill/archmax-harness/references/backend-integration.md +626 -0
- package/dist/authoring-skill/archmax-harness/references/cases.md +41 -0
- package/dist/authoring-skill/archmax-harness/references/diagnostics.md +51 -0
- package/dist/authoring-skill/archmax-harness/references/hook-and-test-scripts.md +1070 -0
- package/dist/authoring-skill/archmax-harness/references/hooks-and-scripts.md +82 -0
- package/dist/authoring-skill/archmax-harness/references/wiring.md +101 -0
- package/dist/authoring-skill/archmax-harness/references/workflow-schema.md +1631 -0
- package/dist/authoring-skill/archmax-harness/references/workflow-yaml.md +241 -0
- package/dist/behaviour/support.d.ts +232 -0
- package/dist/behaviour/support.js +339 -0
- package/dist/behaviour/support.js.map +1 -0
- package/dist/cli/banner.d.ts +11 -0
- package/dist/cli/banner.js +29 -0
- package/dist/cli/banner.js.map +1 -0
- package/dist/cli/bootstrap.d.ts +33 -0
- package/dist/cli/bootstrap.js +153 -0
- package/dist/cli/bootstrap.js.map +1 -0
- package/dist/cli/command.d.ts +39 -0
- package/dist/cli/command.js +97 -0
- package/dist/cli/command.js.map +1 -0
- package/dist/cli/state-flow.d.ts +46 -0
- package/dist/cli/state-flow.js +268 -0
- package/dist/cli/state-flow.js.map +1 -0
- package/dist/cli/style.d.ts +41 -0
- package/dist/cli/style.js +64 -0
- package/dist/cli/style.js.map +1 -0
- package/dist/cli/turn.d.ts +41 -0
- package/dist/cli/turn.js +161 -0
- package/dist/cli/turn.js.map +1 -0
- package/dist/cli.d.ts +10 -0
- package/dist/cli.js +426 -0
- package/dist/cli.js.map +1 -0
- package/dist/core/code-description.d.ts +22 -0
- package/dist/core/code-description.js +38 -0
- package/dist/core/code-description.js.map +1 -0
- package/dist/core/deepagents.d.ts +183 -0
- package/dist/core/deepagents.js +128 -0
- package/dist/core/deepagents.js.map +1 -0
- package/dist/core/events.d.ts +350 -0
- package/dist/core/events.js +205 -0
- package/dist/core/events.js.map +1 -0
- package/dist/core/frontmatter.d.ts +15 -0
- package/dist/core/frontmatter.js +32 -0
- package/dist/core/frontmatter.js.map +1 -0
- package/dist/core/listing-redact.d.ts +37 -0
- package/dist/core/listing-redact.js +88 -0
- package/dist/core/listing-redact.js.map +1 -0
- package/dist/core/match.d.ts +26 -0
- package/dist/core/match.js +75 -0
- package/dist/core/match.js.map +1 -0
- package/dist/core/message-readers.d.ts +84 -0
- package/dist/core/message-readers.js +159 -0
- package/dist/core/message-readers.js.map +1 -0
- package/dist/core/messages.d.ts +49 -0
- package/dist/core/messages.js +85 -0
- package/dist/core/messages.js.map +1 -0
- package/dist/core/mount-prompt.d.ts +28 -0
- package/dist/core/mount-prompt.js +55 -0
- package/dist/core/mount-prompt.js.map +1 -0
- package/dist/core/mount-redact.d.ts +28 -0
- package/dist/core/mount-redact.js +32 -0
- package/dist/core/mount-redact.js.map +1 -0
- package/dist/core/mounts.d.ts +88 -0
- package/dist/core/mounts.js +147 -0
- package/dist/core/mounts.js.map +1 -0
- package/dist/core/path-mapping.d.ts +52 -0
- package/dist/core/path-mapping.js +113 -0
- package/dist/core/path-mapping.js.map +1 -0
- package/dist/core/platform-prompt.generated.d.ts +2 -0
- package/dist/core/platform-prompt.generated.js +4 -0
- package/dist/core/platform-prompt.generated.js.map +1 -0
- package/dist/core/prompt.d.ts +42 -0
- package/dist/core/prompt.js +75 -0
- package/dist/core/prompt.js.map +1 -0
- package/dist/core/session-id.d.ts +38 -0
- package/dist/core/session-id.js +61 -0
- package/dist/core/session-id.js.map +1 -0
- package/dist/core/session-store.d.ts +85 -0
- package/dist/core/session-store.js +149 -0
- package/dist/core/session-store.js.map +1 -0
- package/dist/core/session-zone.d.ts +54 -0
- package/dist/core/session-zone.js +82 -0
- package/dist/core/session-zone.js.map +1 -0
- package/dist/core/skill-prompt.d.ts +31 -0
- package/dist/core/skill-prompt.js +54 -0
- package/dist/core/skill-prompt.js.map +1 -0
- package/dist/core/skill-redact.d.ts +15 -0
- package/dist/core/skill-redact.js +27 -0
- package/dist/core/skill-redact.js.map +1 -0
- package/dist/core/skills.d.ts +172 -0
- package/dist/core/skills.js +214 -0
- package/dist/core/skills.js.map +1 -0
- package/dist/core/tool-mocks.d.ts +23 -0
- package/dist/core/tool-mocks.js +39 -0
- package/dist/core/tool-mocks.js.map +1 -0
- package/dist/core/tool-telemetry.d.ts +16 -0
- package/dist/core/tool-telemetry.js +40 -0
- package/dist/core/tool-telemetry.js.map +1 -0
- package/dist/core/usage.d.ts +114 -0
- package/dist/core/usage.js +226 -0
- package/dist/core/usage.js.map +1 -0
- package/dist/core/workspace-context.d.ts +136 -0
- package/dist/core/workspace-context.js +178 -0
- package/dist/core/workspace-context.js.map +1 -0
- package/dist/core/workspace-prompt.d.ts +7 -0
- package/dist/core/workspace-prompt.js +102 -0
- package/dist/core/workspace-prompt.js.map +1 -0
- package/dist/core/workspace-router.d.ts +61 -0
- package/dist/core/workspace-router.js +145 -0
- package/dist/core/workspace-router.js.map +1 -0
- package/dist/core/workspace.d.ts +85 -0
- package/dist/core/workspace.js +193 -0
- package/dist/core/workspace.js.map +1 -0
- package/dist/core/zones.d.ts +181 -0
- package/dist/core/zones.js +202 -0
- package/dist/core/zones.js.map +1 -0
- package/dist/env.d.ts +99 -0
- package/dist/env.js +271 -0
- package/dist/env.js.map +1 -0
- package/dist/index.d.ts +265 -0
- package/dist/index.js +197 -0
- package/dist/index.js.map +1 -0
- package/dist/kernel/kernel.d.ts +192 -0
- package/dist/kernel/kernel.js +751 -0
- package/dist/kernel/kernel.js.map +1 -0
- package/dist/lifecycle/hook-shape.d.ts +44 -0
- package/dist/lifecycle/hook-shape.js +66 -0
- package/dist/lifecycle/hook-shape.js.map +1 -0
- package/dist/lifecycle/runner.d.ts +177 -0
- package/dist/lifecycle/runner.js +372 -0
- package/dist/lifecycle/runner.js.map +1 -0
- package/dist/lifecycle/transcript.d.ts +27 -0
- package/dist/lifecycle/transcript.js +43 -0
- package/dist/lifecycle/transcript.js.map +1 -0
- package/dist/machine/allow.d.ts +65 -0
- package/dist/machine/allow.js +107 -0
- package/dist/machine/allow.js.map +1 -0
- package/dist/machine/delegation.d.ts +9 -0
- package/dist/machine/delegation.js +10 -0
- package/dist/machine/delegation.js.map +1 -0
- package/dist/machine/diagnostic.d.ts +14 -0
- package/dist/machine/diagnostic.js +2 -0
- package/dist/machine/diagnostic.js.map +1 -0
- package/dist/machine/lint-spec.d.ts +4 -0
- package/dist/machine/lint-spec.js +266 -0
- package/dist/machine/lint-spec.js.map +1 -0
- package/dist/machine/load-spec.d.ts +90 -0
- package/dist/machine/load-spec.js +120 -0
- package/dist/machine/load-spec.js.map +1 -0
- package/dist/machine/machine.d.ts +273 -0
- package/dist/machine/machine.js +551 -0
- package/dist/machine/machine.js.map +1 -0
- package/dist/machine/mount-grants.d.ts +25 -0
- package/dist/machine/mount-grants.js +24 -0
- package/dist/machine/mount-grants.js.map +1 -0
- package/dist/machine/slug.d.ts +14 -0
- package/dist/machine/slug.js +17 -0
- package/dist/machine/slug.js.map +1 -0
- package/dist/machine/spec-schema.d.ts +662 -0
- package/dist/machine/spec-schema.js +717 -0
- package/dist/machine/spec-schema.js.map +1 -0
- package/dist/machine/tool-names.d.ts +98 -0
- package/dist/machine/tool-names.js +144 -0
- package/dist/machine/tool-names.js.map +1 -0
- package/dist/machine/triggers.d.ts +114 -0
- package/dist/machine/triggers.js +165 -0
- package/dist/machine/triggers.js.map +1 -0
- package/dist/machine/types.d.ts +186 -0
- package/dist/machine/types.js +12 -0
- package/dist/machine/types.js.map +1 -0
- package/dist/machine/validate-spec.d.ts +42 -0
- package/dist/machine/validate-spec.js +29 -0
- package/dist/machine/validate-spec.js.map +1 -0
- package/dist/machine/variables.d.ts +196 -0
- package/dist/machine/variables.js +415 -0
- package/dist/machine/variables.js.map +1 -0
- package/dist/public/cli.d.ts +15 -0
- package/dist/public/cli.js +14 -0
- package/dist/public/cli.js.map +1 -0
- package/dist/public/messages.d.ts +33 -0
- package/dist/public/messages.js +33 -0
- package/dist/public/messages.js.map +1 -0
- package/dist/public/sandbox.d.ts +99 -0
- package/dist/public/sandbox.js +32 -0
- package/dist/public/sandbox.js.map +1 -0
- package/dist/public/spec.d.ts +147 -0
- package/dist/public/spec.js +149 -0
- package/dist/public/spec.js.map +1 -0
- package/dist/public/testing.d.ts +71 -0
- package/dist/public/testing.js +39 -0
- package/dist/public/testing.js.map +1 -0
- package/dist/rubrics/rubrics.d.ts +135 -0
- package/dist/rubrics/rubrics.js +145 -0
- package/dist/rubrics/rubrics.js.map +1 -0
- package/dist/runtime/contract.d.ts +73 -0
- package/dist/runtime/contract.js +96 -0
- package/dist/runtime/contract.js.map +1 -0
- package/dist/sandbox/assets/parts/core.js +22 -0
- package/dist/sandbox/assets/parts/lifecycle-hook.js +47 -0
- package/dist/sandbox/bridge.d.ts +16 -0
- package/dist/sandbox/bridge.js +95 -0
- package/dist/sandbox/bridge.js.map +1 -0
- package/dist/sandbox/executor.d.ts +46 -0
- package/dist/sandbox/executor.js +119 -0
- package/dist/sandbox/executor.js.map +1 -0
- package/dist/sandbox/imports.d.ts +33 -0
- package/dist/sandbox/imports.js +67 -0
- package/dist/sandbox/imports.js.map +1 -0
- package/dist/sandbox/prelude.d.ts +12 -0
- package/dist/sandbox/prelude.js +51 -0
- package/dist/sandbox/prelude.js.map +1 -0
- package/dist/sandbox/ptc-gateway.d.ts +104 -0
- package/dist/sandbox/ptc-gateway.js +167 -0
- package/dist/sandbox/ptc-gateway.js.map +1 -0
- package/dist/sandbox/runtime.d.ts +59 -0
- package/dist/sandbox/runtime.js +51 -0
- package/dist/sandbox/runtime.js.map +1 -0
- package/dist/sandbox/tools.d.ts +34 -0
- package/dist/sandbox/tools.js +147 -0
- package/dist/sandbox/tools.js.map +1 -0
- package/dist/sessions/operations.d.ts +79 -0
- package/dist/sessions/operations.js +121 -0
- package/dist/sessions/operations.js.map +1 -0
- package/dist/sessions/resolve.d.ts +97 -0
- package/dist/sessions/resolve.js +145 -0
- package/dist/sessions/resolve.js.map +1 -0
- package/dist/sessions/resume.d.ts +242 -0
- package/dist/sessions/resume.js +288 -0
- package/dist/sessions/resume.js.map +1 -0
- package/dist/sessions/scope.d.ts +99 -0
- package/dist/sessions/scope.js +145 -0
- package/dist/sessions/scope.js.map +1 -0
- package/dist/sessions/summary.d.ts +65 -0
- package/dist/sessions/summary.js +56 -0
- package/dist/sessions/summary.js.map +1 -0
- package/dist/testing/assertions.d.ts +30 -0
- package/dist/testing/assertions.js +231 -0
- package/dist/testing/assertions.js.map +1 -0
- package/dist/testing/case-document.d.ts +141 -0
- package/dist/testing/case-document.js +51 -0
- package/dist/testing/case-document.js.map +1 -0
- package/dist/testing/case-schema.d.ts +29 -0
- package/dist/testing/case-schema.js +519 -0
- package/dist/testing/case-schema.js.map +1 -0
- package/dist/testing/discovery.d.ts +10 -0
- package/dist/testing/discovery.js +24 -0
- package/dist/testing/discovery.js.map +1 -0
- package/dist/testing/driver.d.ts +78 -0
- package/dist/testing/driver.js +117 -0
- package/dist/testing/driver.js.map +1 -0
- package/dist/testing/grade.d.ts +116 -0
- package/dist/testing/grade.js +261 -0
- package/dist/testing/grade.js.map +1 -0
- package/dist/testing/interpreter.d.ts +55 -0
- package/dist/testing/interpreter.js +103 -0
- package/dist/testing/interpreter.js.map +1 -0
- package/dist/testing/mock-middleware.d.ts +5 -0
- package/dist/testing/mock-middleware.js +33 -0
- package/dist/testing/mock-middleware.js.map +1 -0
- package/dist/testing/runner.d.ts +205 -0
- package/dist/testing/runner.js +266 -0
- package/dist/testing/runner.js.map +1 -0
- package/dist/testing/target.d.ts +27 -0
- package/dist/testing/target.js +21 -0
- package/dist/testing/target.js.map +1 -0
- package/dist/validate/validate.d.ts +41 -0
- package/dist/validate/validate.js +669 -0
- package/dist/validate/validate.js.map +1 -0
- package/dist/workflow/agent-tools.d.ts +10 -0
- package/dist/workflow/agent-tools.js +10 -0
- package/dist/workflow/agent-tools.js.map +1 -0
- package/dist/workflow/checkpointer.d.ts +52 -0
- package/dist/workflow/checkpointer.js +200 -0
- package/dist/workflow/checkpointer.js.map +1 -0
- package/dist/workflow/control-tools.d.ts +174 -0
- package/dist/workflow/control-tools.js +468 -0
- package/dist/workflow/control-tools.js.map +1 -0
- package/dist/workflow/governance.d.ts +104 -0
- package/dist/workflow/governance.js +546 -0
- package/dist/workflow/governance.js.map +1 -0
- package/dist/workflow/middleware.d.ts +85 -0
- package/dist/workflow/middleware.js +283 -0
- package/dist/workflow/middleware.js.map +1 -0
- package/dist/workflow/on-error.d.ts +25 -0
- package/dist/workflow/on-error.js +58 -0
- package/dist/workflow/on-error.js.map +1 -0
- package/dist/workflow/parks.d.ts +46 -0
- package/dist/workflow/parks.js +513 -0
- package/dist/workflow/parks.js.map +1 -0
- package/dist/workflow/paths.d.ts +67 -0
- package/dist/workflow/paths.js +108 -0
- package/dist/workflow/paths.js.map +1 -0
- package/dist/workflow/prompt-cache.d.ts +70 -0
- package/dist/workflow/prompt-cache.js +103 -0
- package/dist/workflow/prompt-cache.js.map +1 -0
- package/dist/workflow/prompt-pruning.d.ts +20 -0
- package/dist/workflow/prompt-pruning.js +83 -0
- package/dist/workflow/prompt-pruning.js.map +1 -0
- package/dist/workflow/render-prompt.d.ts +45 -0
- package/dist/workflow/render-prompt.js +140 -0
- package/dist/workflow/render-prompt.js.map +1 -0
- package/dist/workflow/runtime-config.d.ts +7 -0
- package/dist/workflow/runtime-config.js +9 -0
- package/dist/workflow/runtime-config.js.map +1 -0
- package/dist/workflow/session-artifacts.d.ts +90 -0
- package/dist/workflow/session-artifacts.js +134 -0
- package/dist/workflow/session-artifacts.js.map +1 -0
- package/dist/workflow/session-view.d.ts +31 -0
- package/dist/workflow/session-view.js +94 -0
- package/dist/workflow/session-view.js.map +1 -0
- package/dist/workflow/snapshot.d.ts +14 -0
- package/dist/workflow/snapshot.js +97 -0
- package/dist/workflow/snapshot.js.map +1 -0
- package/dist/workflow/state.d.ts +347 -0
- package/dist/workflow/state.js +322 -0
- package/dist/workflow/state.js.map +1 -0
- package/dist/workflow/sub-workflow.d.ts +174 -0
- package/dist/workflow/sub-workflow.js +392 -0
- package/dist/workflow/sub-workflow.js.map +1 -0
- package/dist/workflow/tool-service.d.ts +48 -0
- package/dist/workflow/tool-service.js +291 -0
- package/dist/workflow/tool-service.js.map +1 -0
- package/dist/workflow/turn-boundary.d.ts +39 -0
- package/dist/workflow/turn-boundary.js +128 -0
- package/dist/workflow/turn-boundary.js.map +1 -0
- package/dist/workflow/turn-runner.d.ts +40 -0
- package/dist/workflow/turn-runner.js +122 -0
- package/dist/workflow/turn-runner.js.map +1 -0
- package/dist/workflow/workflow-tools.d.ts +46 -0
- package/dist/workflow/workflow-tools.js +90 -0
- package/dist/workflow/workflow-tools.js.map +1 -0
- package/package.json +119 -0
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { formatReplResult, ReplSession } from "@langchain/quickjs";
|
|
2
|
+
const DEFAULT_SUBAGENT_CONCURRENCY = 32;
|
|
3
|
+
/** Fallback per-eval timeout when a caller omits one (mirrors the executor default). */
|
|
4
|
+
const DEFAULT_TIMEOUT_MS = 5_000;
|
|
5
|
+
/**
|
|
6
|
+
* The bundled default: `@langchain/quickjs`'s `ReplSession` behind the
|
|
7
|
+
* interface. Sessions are keyed by namespace and reused across evals (the
|
|
8
|
+
* runtime namespaces per session + session kind), and `formatReplResult`
|
|
9
|
+
* renders the outcome so the executor stays engine-agnostic.
|
|
10
|
+
*/
|
|
11
|
+
export function createQuickJsSandboxRuntime() {
|
|
12
|
+
return {
|
|
13
|
+
session(namespace, options) {
|
|
14
|
+
const bridge = options.subagentDispatch != null
|
|
15
|
+
? {
|
|
16
|
+
dispatch: options.subagentDispatch,
|
|
17
|
+
maxConcurrency: options.subagentMaxConcurrency ?? DEFAULT_SUBAGENT_CONCURRENCY,
|
|
18
|
+
}
|
|
19
|
+
: undefined;
|
|
20
|
+
const session = ReplSession.getOrCreate(namespace, {
|
|
21
|
+
tools: options.tools ?? [],
|
|
22
|
+
memoryLimitBytes: options.quotas?.memoryLimitBytes,
|
|
23
|
+
maxPtcCalls: options.quotas?.maxPtcCalls,
|
|
24
|
+
maxResultChars: options.quotas?.maxResultChars,
|
|
25
|
+
captureConsole: options.captureConsole ?? true,
|
|
26
|
+
sessionId: options.sessionId,
|
|
27
|
+
subagentBridge: bridge,
|
|
28
|
+
});
|
|
29
|
+
// Refresh the bridge dispatch each acquisition so a reused session picks up
|
|
30
|
+
// the current request's `task()` wiring.
|
|
31
|
+
if (bridge)
|
|
32
|
+
session.updateBridgeDispatch(bridge.dispatch);
|
|
33
|
+
return {
|
|
34
|
+
async eval(code, timeoutMs) {
|
|
35
|
+
const result = await session.eval(code, timeoutMs ?? DEFAULT_TIMEOUT_MS);
|
|
36
|
+
return {
|
|
37
|
+
ok: result.ok,
|
|
38
|
+
value: result.value,
|
|
39
|
+
logs: result.logs,
|
|
40
|
+
error: result.error,
|
|
41
|
+
formatted: formatReplResult(result),
|
|
42
|
+
};
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
},
|
|
46
|
+
dispose(namespace) {
|
|
47
|
+
ReplSession.deleteSession(namespace);
|
|
48
|
+
},
|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AAO,CAAC,IAAI,CAAC,IAAI,EAAE,SAAS,IAAI,kBAAkB,CAAC,CAAC;oBACzE,OAAO;wBACL,EAAE,EAAE,MAAM,CAAC,EAAE;wBACb,KAAK,EAAE,MAAM,CAAC,KAAK;wBACnB,IAAI,EAAE,MAAM,CAAC,IAAI;wBACjB,KAAK,EAAE,MAAM,CAAC,KAAK;wBACnB,SAAS,EAAE,gBAAgB,CAAC,MAAM,CAAC;qBACpC,CAAC;gBACJ,CAAC;aACF,CAAC;QACJ,CAAC;QAED,OAAO,CAAC,SAAiB;YACvB,WAAW,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC;QACvC,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { StructuredTool } from "@langchain/core/tools";
|
|
2
|
+
import type { AgentMiddleware } from "langchain";
|
|
3
|
+
import type { PtcToolGateway } from "./ptc-gateway.js";
|
|
4
|
+
import { type ScriptExecutor } from "./executor.js";
|
|
5
|
+
/**
|
|
6
|
+
* The two sandbox tools — `archmax_eval` (inline code) and `archmax_run` (an
|
|
7
|
+
* authored script file) — plus the middleware that scopes their PTC tools,
|
|
8
|
+
* snapshots the session's variables for `archmax_run`, and tears down REPL
|
|
9
|
+
* sessions per scope. Both evaluate in the *same* REPL session for a run scope,
|
|
10
|
+
* so a helper defined by one is in scope for the other.
|
|
11
|
+
*/
|
|
12
|
+
export interface Interpreter {
|
|
13
|
+
middleware: AgentMiddleware;
|
|
14
|
+
tools: StructuredTool[];
|
|
15
|
+
executor: ScriptExecutor;
|
|
16
|
+
/**
|
|
17
|
+
* Release the resources held for a session: the cached PTC tool sets and the
|
|
18
|
+
* agent REPL sessions of the session's own run scope **and of every sub-run
|
|
19
|
+
* scope nested beneath it**. Idempotent; safe after aborted runs.
|
|
20
|
+
*/
|
|
21
|
+
dispose(sessionId: string): void;
|
|
22
|
+
}
|
|
23
|
+
/** Pick PTC tools from the agent's full tool set. Empty `names` = all except {@link PTC_EXCLUDED_TOOLS}. */
|
|
24
|
+
export declare function resolvePtcTools(allTools: unknown[], names: string[]): StructuredTool[];
|
|
25
|
+
export declare function createInterpreter(opts: {
|
|
26
|
+
executor: ScriptExecutor;
|
|
27
|
+
ptcNames: string[];
|
|
28
|
+
/**
|
|
29
|
+
* Governs and instruments the `tools.*` calls scripts make. Optional so a
|
|
30
|
+
* test can assemble a bare interpreter; the runtime always supplies one, and
|
|
31
|
+
* without it a script's tool calls run ungoverned.
|
|
32
|
+
*/
|
|
33
|
+
ptcGateway?: PtcToolGateway;
|
|
34
|
+
}): Interpreter;
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { createMiddleware, tool } from "langchain";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { asModelCallResult, } from "../core/deepagents.js";
|
|
4
|
+
import { EVAL_TOOL, RUN_TOOL } from "../machine/tool-names.js";
|
|
5
|
+
import { PTC_EXCLUDED_TOOLS } from "../kernel/kernel.js";
|
|
6
|
+
import { AGENT_SESSION } from "./executor.js";
|
|
7
|
+
import { DEFAULT_SESSION_ID, releaseScopes, sessionScopeFrom } from "../sessions/scope.js";
|
|
8
|
+
import { readVariables } from "../workflow/state.js";
|
|
9
|
+
/** Pick PTC tools from the agent's full tool set. Empty `names` = all except {@link PTC_EXCLUDED_TOOLS}. */
|
|
10
|
+
export function resolvePtcTools(allTools, names) {
|
|
11
|
+
const tools = allTools;
|
|
12
|
+
if (names.length === 0) {
|
|
13
|
+
return tools.filter((t) => !PTC_EXCLUDED_TOOLS.has(t.name));
|
|
14
|
+
}
|
|
15
|
+
const byName = new Map(tools.map((t) => [t.name, t]));
|
|
16
|
+
// Governance-owned exclusions hold even for an explicit allow-list: scripts
|
|
17
|
+
// may never call the runtime's controls (both sandbox entry points among them)
|
|
18
|
+
// through PTC.
|
|
19
|
+
return names
|
|
20
|
+
.map((n) => byName.get(n))
|
|
21
|
+
.filter((t) => t !== undefined && !PTC_EXCLUDED_TOOLS.has(t.name));
|
|
22
|
+
}
|
|
23
|
+
/** The session's variables as the flat `name → value` map a script reads. */
|
|
24
|
+
function flatVariables(state) {
|
|
25
|
+
return Object.fromEntries(Object.entries(readVariables(state)).map(([name, entry]) => [name, entry.value]));
|
|
26
|
+
}
|
|
27
|
+
export function createInterpreter(opts) {
|
|
28
|
+
const { executor, ptcNames, ptcGateway } = opts;
|
|
29
|
+
// Keyed by **run scope**, not session: two sub-runs of one session execute
|
|
30
|
+
// concurrently and must not share a REPL or each other's PTC tool set. At the
|
|
31
|
+
// top level the scope key is the session id.
|
|
32
|
+
const ptcToolsByScope = new Map();
|
|
33
|
+
// The variables in force for the `archmax_run` call being serviced, captured at
|
|
34
|
+
// the tool call so the script reads the store the call was made against.
|
|
35
|
+
const variablesByScope = new Map();
|
|
36
|
+
/** The PTC surface a call in this scope may use, wrapped by the gateway. */
|
|
37
|
+
const scopeTools = (scope) => ptcToolsByScope.get(scope) ?? [];
|
|
38
|
+
const evalCode = tool(async (input, runtimeConfig) => {
|
|
39
|
+
const scope = sessionScopeFrom(runtimeConfig, DEFAULT_SESSION_ID).sessionId;
|
|
40
|
+
const outcome = await executor.runCode(input.code, {
|
|
41
|
+
sessionId: scope,
|
|
42
|
+
sessionNamespace: AGENT_SESSION,
|
|
43
|
+
tools: scopeTools(scope),
|
|
44
|
+
prelude: true,
|
|
45
|
+
});
|
|
46
|
+
return outcome.formatted;
|
|
47
|
+
}, {
|
|
48
|
+
name: EVAL_TOOL,
|
|
49
|
+
description: "Evaluate JavaScript in a sandboxed REPL with persistent state across calls, " +
|
|
50
|
+
"console capture, and `tools.*` for tool calls. " +
|
|
51
|
+
"Prefer it over many small tool calls when work is loops, filtering, or arithmetic. " +
|
|
52
|
+
"Every `tools.*` call is governed by the workflow graph exactly as a direct call is.",
|
|
53
|
+
schema: z.object({
|
|
54
|
+
code: z
|
|
55
|
+
.string()
|
|
56
|
+
.describe("JavaScript to evaluate. The last expression is returned."),
|
|
57
|
+
}),
|
|
58
|
+
});
|
|
59
|
+
const runScript = tool(async (input, runtimeConfig) => {
|
|
60
|
+
const scope = sessionScopeFrom(runtimeConfig, DEFAULT_SESSION_ID).sessionId;
|
|
61
|
+
const outcome = await executor.runFile(input.file_path, {
|
|
62
|
+
sessionId: scope,
|
|
63
|
+
sessionNamespace: AGENT_SESSION,
|
|
64
|
+
// The script's `args`: what the model passed, plus the session's
|
|
65
|
+
// variables by name — a script reads the run's input without the model
|
|
66
|
+
// having to retype it.
|
|
67
|
+
args: { ...(input.args ?? {}), variables: variablesByScope.get(scope) ?? {} },
|
|
68
|
+
tools: scopeTools(scope),
|
|
69
|
+
});
|
|
70
|
+
return outcome.formatted;
|
|
71
|
+
}, {
|
|
72
|
+
name: RUN_TOOL,
|
|
73
|
+
description: "Run an authored workspace JavaScript file in the same sandboxed REPL as " +
|
|
74
|
+
`\`${EVAL_TOOL}\`, with \`tools.*\` for tool calls. The script reads its input as the ` +
|
|
75
|
+
"global `args`: the arguments you pass plus `args.variables`, the session's variables " +
|
|
76
|
+
"by name. Which files may run is enforced by the workflow graph.",
|
|
77
|
+
schema: z.object({
|
|
78
|
+
file_path: z
|
|
79
|
+
.string()
|
|
80
|
+
.describe("Workspace-relative path, e.g. skills/orders/scripts/check.js."),
|
|
81
|
+
args: z
|
|
82
|
+
.record(z.string(), z.unknown())
|
|
83
|
+
.optional()
|
|
84
|
+
.describe("Exposed to the script as global `args`, beside `args.variables`."),
|
|
85
|
+
}),
|
|
86
|
+
});
|
|
87
|
+
function releaseScope(scope) {
|
|
88
|
+
ptcToolsByScope.delete(scope);
|
|
89
|
+
variablesByScope.delete(scope);
|
|
90
|
+
ptcGateway?.release(scope);
|
|
91
|
+
executor.dispose(scope, AGENT_SESSION);
|
|
92
|
+
}
|
|
93
|
+
function dispose(sessionId) {
|
|
94
|
+
// A session's scopes are its own plus every sub-run's, so disposal sweeps by
|
|
95
|
+
// prefix. The executor is told about each one: a REPL leaked per dispatch
|
|
96
|
+
// would outlive the run that opened it.
|
|
97
|
+
const released = releaseScopes(ptcToolsByScope, sessionId);
|
|
98
|
+
releaseScopes(variablesByScope, sessionId);
|
|
99
|
+
if (!released.includes(sessionId))
|
|
100
|
+
released.push(sessionId);
|
|
101
|
+
for (const scope of released)
|
|
102
|
+
releaseScope(scope);
|
|
103
|
+
}
|
|
104
|
+
const middleware = createMiddleware({
|
|
105
|
+
name: "ScriptInterpreter",
|
|
106
|
+
// A middleware's hooks see the agent state parsed through *its own* schema,
|
|
107
|
+
// so without declaring `variables` here the snapshot below would read an
|
|
108
|
+
// empty store. Declared read-only in shape: the workflow middleware owns
|
|
109
|
+
// the channel and its reducer (the first declaration of a key wins).
|
|
110
|
+
stateSchema: z.object({
|
|
111
|
+
variables: z
|
|
112
|
+
.record(z.string(), z.object({ value: z.unknown(), locked: z.boolean() }).passthrough())
|
|
113
|
+
.optional(),
|
|
114
|
+
}),
|
|
115
|
+
wrapModelCall: async (request, handler) => {
|
|
116
|
+
const scope = sessionScopeFrom(request.runtime, DEFAULT_SESSION_ID).sessionId;
|
|
117
|
+
// Selection stays pure (which tools a script may see); the gateway adds
|
|
118
|
+
// enforcement (what each call may do), which it decides per call against
|
|
119
|
+
// the scope's live context rather than at this capture point.
|
|
120
|
+
const selected = resolvePtcTools(request.tools ?? [], ptcNames);
|
|
121
|
+
ptcToolsByScope.set(scope, ptcGateway ? ptcGateway.wrap(selected, { origin: "script", sessionId: scope }) : selected);
|
|
122
|
+
// Narrowed like the workflow middleware: whichever is innermost is named
|
|
123
|
+
// when the model handler returns something outside the contract.
|
|
124
|
+
return asModelCallResult(await handler(request), "the script interpreter");
|
|
125
|
+
},
|
|
126
|
+
wrapToolCall: async (request, handler) => {
|
|
127
|
+
if (request.toolCall.name === RUN_TOOL) {
|
|
128
|
+
const scope = sessionScopeFrom(request.runtime, DEFAULT_SESSION_ID).sessionId;
|
|
129
|
+
variablesByScope.set(scope, flatVariables(request.state));
|
|
130
|
+
}
|
|
131
|
+
return handler(request);
|
|
132
|
+
},
|
|
133
|
+
afterAgent: async (state, runtime) => {
|
|
134
|
+
// Released at the scope this agent ran in, not the session: a sub-run
|
|
135
|
+
// finishing must not tear down its parent's REPL, which is still mid-run.
|
|
136
|
+
releaseScope(sessionScopeFrom(runtime, DEFAULT_SESSION_ID).sessionId);
|
|
137
|
+
return state;
|
|
138
|
+
},
|
|
139
|
+
});
|
|
140
|
+
return {
|
|
141
|
+
middleware,
|
|
142
|
+
tools: [evalCode, runScript],
|
|
143
|
+
executor,
|
|
144
|
+
dispose,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
//# sourceMappingURL=tools.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tools.js","sourceRoot":"","sources":["../../src/sandbox/tools.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEnD,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EACL,iBAAiB,GAKlB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AAC/D,OAAO,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAEzD,OAAO,EAAE,aAAa,EAAuB,MAAM,eAAe,CAAC;AACnE,OAAO,EAAE,kBAAkB,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAC3F,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAqBrD,4GAA4G;AAC5G,MAAM,UAAU,eAAe,CAAC,QAAmB,EAAE,KAAe;IAClE,MAAM,KAAK,GAAG,QAA4B,CAAC;IAC3C,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACvB,OAAO,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,kBAAkB,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;I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,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE;YACnC,sEAAsE;YACtE,0EAA0E;YAC1E,YAAY,CAAC,gBAAgB,CAAC,OAAO,EAAE,kBAAkB,CAAC,CAAC,SAAS,CAAC,CAAC;YACtE,OAAO,KAAK,CAAC;QACf,CAAC;KACF,CAAC,CAAC;IAEH,OAAO;QACL,UAAU;QACV,KAAK,EAAE,CAAC,QAAQ,EAAE,SAAS,CAAgC;QAC3D,QAAQ;QACR,OAAO;KACR,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The session operations an assembled agent carries — `sessions.list/get/
|
|
3
|
+
* delete/seed` over the configured session store, and the artifact emitter —
|
|
4
|
+
* governed or not. Assembly wires them; nothing here knows how the agent was composed.
|
|
5
|
+
*/
|
|
6
|
+
import type { BaseCheckpointSaver } from "@langchain/langgraph-checkpoint";
|
|
7
|
+
import { type CompiledAgentGraph } from "../core/deepagents.js";
|
|
8
|
+
import type { WorkflowEventEmitter } from "../core/events.js";
|
|
9
|
+
import { type SessionStore } from "../core/session-store.js";
|
|
10
|
+
import { type UsageTracker } from "../core/usage.js";
|
|
11
|
+
import { Workspace } from "../core/workspace.js";
|
|
12
|
+
import { type MountPrefixes } from "../core/zones.js";
|
|
13
|
+
import type { WorkflowMachine } from "../machine/machine.js";
|
|
14
|
+
import type { MachineSpec } from "../machine/types.js";
|
|
15
|
+
import type { ResolvedRuntimeContract } from "../runtime/contract.js";
|
|
16
|
+
import { describeMachineTopology, type Trajectory } from "../workflow/session-artifacts.js";
|
|
17
|
+
import { type TrailStep } from "../workflow/state.js";
|
|
18
|
+
import { type SessionSummary } from "./summary.js";
|
|
19
|
+
export { describeMachineTopology };
|
|
20
|
+
/**
|
|
21
|
+
* Session handles over the configured session store — the supported
|
|
22
|
+
* alternative to manipulating a store's `<sessionId>/…` folders directly.
|
|
23
|
+
*/
|
|
24
|
+
export interface SessionOperations {
|
|
25
|
+
/**
|
|
26
|
+
* Enumerate durable sessions, projected from each session's latest checkpoint.
|
|
27
|
+
* Empty when the checkpointer or store cannot enumerate sessions (an ephemeral
|
|
28
|
+
* `MemorySaver`, a backend store without listing capability).
|
|
29
|
+
*/
|
|
30
|
+
list(): Promise<SessionSummary[]>;
|
|
31
|
+
/** A single session's summary, or `null` when no such session exists. */
|
|
32
|
+
get(sessionId: string): Promise<SessionSummary | null>;
|
|
33
|
+
/**
|
|
34
|
+
* How many messages the session's transcript holds, from its latest checkpoint;
|
|
35
|
+
* `0` for a session that has not run. A **cursor**: read it before a turn and
|
|
36
|
+
* hand it to `messagesSince(messages, cursor)` afterwards to slice exactly what
|
|
37
|
+
* that turn appended — seeds, notes and all — without counting them yourself.
|
|
38
|
+
*/
|
|
39
|
+
messageCount(sessionId: string): Promise<number>;
|
|
40
|
+
/**
|
|
41
|
+
* Remove all of a session's state — checkpoints, artifacts, scratchpad — through
|
|
42
|
+
* the session store, and evict it from the live checkpointer. Returns whether
|
|
43
|
+
* anything was removed. Throws `SessionStoreCapabilityError` when the store
|
|
44
|
+
* cannot delete.
|
|
45
|
+
*/
|
|
46
|
+
delete(sessionId: string): Promise<boolean>;
|
|
47
|
+
/**
|
|
48
|
+
* Seed input files into a session's workspace through the store. Keys are
|
|
49
|
+
* workspace-relative paths classified against the assembly's mounts: only
|
|
50
|
+
* agent-visible session paths (root-level files like `trigger.json`, plus
|
|
51
|
+
* `scratchpad/…`) are seedable, and one bad path seeds nothing. Strings are
|
|
52
|
+
* written verbatim, other values as pretty-printed JSON; existing files are overwritten.
|
|
53
|
+
*/
|
|
54
|
+
seed(sessionId: string, files: Record<string, unknown>): Promise<void>;
|
|
55
|
+
}
|
|
56
|
+
/** Persist a session's graph snapshot, trajectory, trail, variables and metadata. */
|
|
57
|
+
export type EmitRunArtifacts = (sessionId: string, trajectory: Trajectory, opts?: {
|
|
58
|
+
trail?: TrailStep[];
|
|
59
|
+
}) => Promise<string | null>;
|
|
60
|
+
export declare function createSessionOperations(deps: {
|
|
61
|
+
checkpointer: BaseCheckpointSaver;
|
|
62
|
+
sessionStore: SessionStore;
|
|
63
|
+
/** The agent's workspace — its root, outside a bound session, is the raw store root. */
|
|
64
|
+
workspace: Workspace;
|
|
65
|
+
mountPrefixes: MountPrefixes;
|
|
66
|
+
dispose: (sessionId: string) => void;
|
|
67
|
+
}): SessionOperations;
|
|
68
|
+
export declare function createArtifactEmitter(deps: {
|
|
69
|
+
graph: CompiledAgentGraph;
|
|
70
|
+
/** The governing machine, or `null` for a plain assembly. */
|
|
71
|
+
machine: WorkflowMachine | null;
|
|
72
|
+
workspace: Workspace;
|
|
73
|
+
workflowName: string;
|
|
74
|
+
runtimeContract: ResolvedRuntimeContract;
|
|
75
|
+
usage: UsageTracker;
|
|
76
|
+
emit: WorkflowEventEmitter;
|
|
77
|
+
}): EmitRunArtifacts;
|
|
78
|
+
/** Resolve a persisted machine spec by its `specHash`, or `null`. */
|
|
79
|
+
export declare function createSpecSnapshotReader(workspace: Workspace): (hash: string) => Promise<MachineSpec | null>;
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { asDecisionGraph } from "../core/deepagents.js";
|
|
2
|
+
import { SessionStoreIdError, sessionIdRejection } from "../core/session-store.js";
|
|
3
|
+
import { hasUsage } from "../core/usage.js";
|
|
4
|
+
import { canonicalizeRelPath, Workspace } from "../core/workspace.js";
|
|
5
|
+
import { classifyWorkspacePath } from "../core/zones.js";
|
|
6
|
+
import { PACKAGE_VERSION } from "../env.js";
|
|
7
|
+
import { BackendCheckpointSaver } from "../workflow/checkpointer.js";
|
|
8
|
+
import { describeMachineTopology, serializeGraph, serializeMachineGraph, writeRunArtifacts, } from "../workflow/session-artifacts.js";
|
|
9
|
+
import { readSpecSnapshot } from "../workflow/snapshot.js";
|
|
10
|
+
import { readAuditTrail, readRunUsage, readVariables } from "../workflow/state.js";
|
|
11
|
+
import { listSessions, summarizeCheckpointedSession } from "./summary.js";
|
|
12
|
+
export { describeMachineTopology };
|
|
13
|
+
export function createSessionOperations(deps) {
|
|
14
|
+
const { checkpointer, sessionStore, workspace, mountPrefixes, dispose } = deps;
|
|
15
|
+
return {
|
|
16
|
+
list: () => checkpointer instanceof BackendCheckpointSaver && sessionStore.capabilities.list
|
|
17
|
+
? listSessions(workspace, checkpointer)
|
|
18
|
+
: Promise.resolve([]),
|
|
19
|
+
// One session's projection via `getTuple` alone, so it works for any
|
|
20
|
+
// checkpointer — it must: `null` means "this session has not run yet", and
|
|
21
|
+
// resolution would then treat a live session's next turn as its first.
|
|
22
|
+
get: (sessionId) => summarizeCheckpointedSession(checkpointer, sessionId),
|
|
23
|
+
messageCount: async (sessionId) => {
|
|
24
|
+
const tuple = await checkpointer.getTuple({ configurable: { thread_id: sessionId } });
|
|
25
|
+
const messages = tuple?.checkpoint.channel_values?.messages;
|
|
26
|
+
return Array.isArray(messages) ? messages.length : 0;
|
|
27
|
+
},
|
|
28
|
+
delete: async (sessionId) => {
|
|
29
|
+
// Evict live checkpointer state first so a deleted session cannot be resumed
|
|
30
|
+
// from memory, then remove the persisted namespace through the store.
|
|
31
|
+
await checkpointer.deleteThread(sessionId);
|
|
32
|
+
const removed = await sessionStore.deleteSession(sessionId);
|
|
33
|
+
dispose(sessionId);
|
|
34
|
+
return removed;
|
|
35
|
+
},
|
|
36
|
+
seed: async (sessionId, files) => {
|
|
37
|
+
const idRejection = sessionIdRejection(sessionId, mountPrefixes);
|
|
38
|
+
if (idRejection)
|
|
39
|
+
throw new SessionStoreIdError(sessionId, idRejection);
|
|
40
|
+
const id = sessionId.replace(/^\/+/, "");
|
|
41
|
+
// Validate every path before writing any.
|
|
42
|
+
const entries = Object.entries(files).map(([path, value]) => {
|
|
43
|
+
const canonical = canonicalizeRelPath(path);
|
|
44
|
+
const zone = classifyWorkspacePath(path, mountPrefixes);
|
|
45
|
+
if (zone !== "run" && zone !== "run-open") {
|
|
46
|
+
throw new Error(`Cannot seed ${JSON.stringify(path)}: it resolves to the '${zone}' zone. ` +
|
|
47
|
+
`Seedable paths are agent-visible session files (e.g. 'trigger.json', 'scratchpad/…').`);
|
|
48
|
+
}
|
|
49
|
+
const contents = typeof value === "string" ? value : `${JSON.stringify(value, null, 2)}\n`;
|
|
50
|
+
return [canonical.path, contents];
|
|
51
|
+
});
|
|
52
|
+
// Address the store as `<sessionId>/…` explicitly, like the checkpointer.
|
|
53
|
+
const store = new Workspace(sessionStore.backend);
|
|
54
|
+
for (const [path, contents] of entries) {
|
|
55
|
+
const key = `${id}/${path}`;
|
|
56
|
+
try {
|
|
57
|
+
await store.writeText(key, contents);
|
|
58
|
+
}
|
|
59
|
+
catch (err) {
|
|
60
|
+
// A create-only backend refuses to overwrite; replace the whole content
|
|
61
|
+
// through `edit` instead (seeding is idempotent).
|
|
62
|
+
const existing = await store.readText(key);
|
|
63
|
+
if (existing == null)
|
|
64
|
+
throw err;
|
|
65
|
+
if (existing !== contents)
|
|
66
|
+
await store.editText(key, existing, contents);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
export function createArtifactEmitter(deps) {
|
|
73
|
+
const { graph, machine, workspace, workflowName, runtimeContract, usage, emit } = deps;
|
|
74
|
+
return async (sessionId, trajectory, opts) => {
|
|
75
|
+
// One checkpoint read serves the trail, the usage and the variables.
|
|
76
|
+
// `undefined` means unreadable — distinct from a checkpoint that holds
|
|
77
|
+
// nothing: an empty trail.json must mean "no transitions", never "unreadable".
|
|
78
|
+
async function readCheckpointValues() {
|
|
79
|
+
try {
|
|
80
|
+
const snapshot = await asDecisionGraph(graph).getState({ configurable: { thread_id: sessionId } });
|
|
81
|
+
return snapshot?.values;
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
// A governed session's graph is the machine's own topology; a plain agent
|
|
88
|
+
// has only its compiled graph.
|
|
89
|
+
const [serialized, values] = await Promise.all([
|
|
90
|
+
machine ? serializeMachineGraph(machine) : serializeGraph(graph),
|
|
91
|
+
machine ? readCheckpointValues() : undefined,
|
|
92
|
+
]);
|
|
93
|
+
const trail = machine
|
|
94
|
+
? (opts?.trail ?? (values !== undefined ? readAuditTrail(values) : undefined))
|
|
95
|
+
: undefined;
|
|
96
|
+
// Usage comes from the checkpoint when there is one (a session resumed in
|
|
97
|
+
// another process would otherwise report only this process's share); the
|
|
98
|
+
// in-memory tracker is the fallback.
|
|
99
|
+
const checkpointed = values !== undefined ? readRunUsage(values) : undefined;
|
|
100
|
+
const sessionUsage = checkpointed && hasUsage(checkpointed) ? checkpointed : usage.totals(sessionId);
|
|
101
|
+
// An empty store is still recorded: "took no seeded input" is a fact.
|
|
102
|
+
const variables = machine && values !== undefined ? readVariables(values) : undefined;
|
|
103
|
+
return writeRunArtifacts(workspace, workflowName, sessionId, {
|
|
104
|
+
graph: serialized,
|
|
105
|
+
trajectory,
|
|
106
|
+
metadata: {
|
|
107
|
+
runtimeContract,
|
|
108
|
+
packageVersion: PACKAGE_VERSION,
|
|
109
|
+
...(machine ? { specHash: machine.specHash } : {}),
|
|
110
|
+
...(hasUsage(sessionUsage) ? { usage: sessionUsage } : {}),
|
|
111
|
+
},
|
|
112
|
+
...(trail ? { trail } : {}),
|
|
113
|
+
...(variables !== undefined ? { variables } : {}),
|
|
114
|
+
}, emit);
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
/** Resolve a persisted machine spec by its `specHash`, or `null`. */
|
|
118
|
+
export function createSpecSnapshotReader(workspace) {
|
|
119
|
+
return (hash) => readSpecSnapshot(workspace, hash);
|
|
120
|
+
}
|
|
121
|
+
//# sourceMappingURL=operations.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"operations.js","sourceRoot":"","sources":["../../src/sessions/operations.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,eAAe,EAA2B,MAAM,uBAAuB,CAAC;AAEjF,OAAO,EAAE,mBAAmB,EAAE,kBAAkB,EA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S,CAAC,CAAC,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;QACtF,OAAO,iBAAiB,CACtB,SAAS,EACT,YAAY,EACZ,SAAS,EACT;YACE,KAAK,EAAE,UAAU;YACjB,UAAU;YACV,QAAQ,EAAE;gBACR,eAAe;gBACf,cAAc,EAAE,eAAe;gBAC/B,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;gBAClD,GAAG,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,YAAY,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;aAC3D;YACD,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;YAC3B,GAAG,CAAC,SAAS,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;SAClD,EACD,IAAI,CACL,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC;AAED,qEAAqE;AACrE,MAAM,UAAU,wBAAwB,CAAC,SAAoB;IAC3D,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,gBAAgB,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;AACrD,CAAC"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session resolution: given a firing, decide how it lands on its session —
|
|
3
|
+
* without invoking the graph or any model.
|
|
4
|
+
*
|
|
5
|
+
* The rule: **a session whose turn has finished takes its next turn on the same
|
|
6
|
+
* session, continuing from the state it is in; a session whose turn is still
|
|
7
|
+
* open resumes from the state where it parked.** A session is extended, never
|
|
8
|
+
* copied, so there is one id and nothing to map it to. A mid-turn session fails
|
|
9
|
+
* closed: a competing turn would break the checkpointer's single-writer guarantee.
|
|
10
|
+
*/
|
|
11
|
+
import type { WorkflowMachine } from "../machine/machine.js";
|
|
12
|
+
import { type SessionPath } from "../machine/triggers.js";
|
|
13
|
+
import type { SessionSummary } from "./summary.js";
|
|
14
|
+
/**
|
|
15
|
+
* How a firing lands on its session: it opens a `turn`, it `resume`s the state
|
|
16
|
+
* the session parked in, or — a session parked at a human state — it is a `reply`,
|
|
17
|
+
* answered without moving the run.
|
|
18
|
+
*/
|
|
19
|
+
export type SessionDisposition = "turn" | "resume" | "reply";
|
|
20
|
+
export interface ResolvedSession {
|
|
21
|
+
/** The session to invoke (`turn`) or deliver to (`resume`) — the only id there is. */
|
|
22
|
+
sessionId: string;
|
|
23
|
+
disposition: SessionDisposition;
|
|
24
|
+
/**
|
|
25
|
+
* `turn` only: where the turn begins — the retained position of a session that
|
|
26
|
+
* already has one, else the entry state the firing's trigger declares.
|
|
27
|
+
*/
|
|
28
|
+
startState?: string;
|
|
29
|
+
/**
|
|
30
|
+
* `resume` and `reply` only: the state the session is parked at — the one a
|
|
31
|
+
* delivery resumes, or the human state a message is answered at.
|
|
32
|
+
*/
|
|
33
|
+
state?: string;
|
|
34
|
+
/** @deprecated Use {@link ResolvedSession.state}; kept for one release. */
|
|
35
|
+
node?: string;
|
|
36
|
+
/** Whether the id was minted for this session because the firing declared none. */
|
|
37
|
+
native: boolean;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Thrown when a firing maps to a session whose turn is still in progress: a
|
|
41
|
+
* session has a single writer, so the firing must wait. A human-parked session
|
|
42
|
+
* is *not* this case — it resolves to `reply`.
|
|
43
|
+
*/
|
|
44
|
+
export declare class SessionNotResumableError extends Error {
|
|
45
|
+
readonly sessionId: string;
|
|
46
|
+
readonly status: string | undefined;
|
|
47
|
+
constructor(sessionId: string, status: string | undefined);
|
|
48
|
+
}
|
|
49
|
+
/** Thrown when a firing names a trigger no state of the workflow declares. */
|
|
50
|
+
export declare class UnknownSessionTriggerError extends Error {
|
|
51
|
+
readonly trigger: string;
|
|
52
|
+
constructor(trigger: string);
|
|
53
|
+
}
|
|
54
|
+
/** What the caller supplies for one firing. */
|
|
55
|
+
export interface ResolveSessionInput {
|
|
56
|
+
trigger?: {
|
|
57
|
+
id: string;
|
|
58
|
+
};
|
|
59
|
+
/** The variables the firing carries — the same map an invoke or delivery seeds. */
|
|
60
|
+
variables?: Record<string, unknown>;
|
|
61
|
+
/** An explicit id, taking precedence over every path. */
|
|
62
|
+
sessionId?: string;
|
|
63
|
+
/**
|
|
64
|
+
* Where to read the id from for this firing — the same dotted path a
|
|
65
|
+
* `triggers` entry declares (`triggers.-1.conversationId`), overriding what
|
|
66
|
+
* the workflow says.
|
|
67
|
+
*/
|
|
68
|
+
sessionPath?: string;
|
|
69
|
+
}
|
|
70
|
+
/** The lookups resolution needs from the runtime, kept injectable for testing. */
|
|
71
|
+
export interface SessionLookup {
|
|
72
|
+
/** The session's state, projected from its own checkpoint — or `null` when the id names no session yet. */
|
|
73
|
+
findSession(sessionId: string): Promise<SessionSummary | null>;
|
|
74
|
+
/** Mint an id for a firing that declares no session of its own. */
|
|
75
|
+
mintSessionId(): string;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Derive a firing's session id: the explicit one, else the invoking trigger's
|
|
79
|
+
* declared path resolved over the firing's variables, else a minted id (the
|
|
80
|
+
* firing is its own session). Every turn belongs to exactly one session.
|
|
81
|
+
*/
|
|
82
|
+
export declare function deriveSessionId(path: SessionPath | undefined, input: ResolveSessionInput, nativeId: string): {
|
|
83
|
+
sessionId: string;
|
|
84
|
+
native: boolean;
|
|
85
|
+
unresolvedPath?: SessionPath;
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* Resolve and classify a firing. Never invokes the graph: the caller decides
|
|
89
|
+
* whether to start the run or deliver into it.
|
|
90
|
+
*/
|
|
91
|
+
export declare function resolveSession(machine: WorkflowMachine, lookup: SessionLookup, input: ResolveSessionInput, onWarning?: (message: string) => void): Promise<ResolvedSession>;
|
|
92
|
+
/** Thrown when a caller-supplied session path is not a usable dotted path. */
|
|
93
|
+
export declare class InvalidSessionPathError extends Error {
|
|
94
|
+
readonly path: string;
|
|
95
|
+
readonly reason: string;
|
|
96
|
+
constructor(path: string, reason: string);
|
|
97
|
+
}
|