melchizedek-agents 1.6.0 → 1.8.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.
Files changed (44) hide show
  1. package/CHANGELOG.md +113 -0
  2. package/DOCUMENTATION.md +16 -7
  3. package/config/agents/syndicate.schema.json +63 -0
  4. package/config/agents/syndicateSchema.yaml +50 -0
  5. package/dist/lib/a2a/executor.js +21 -0
  6. package/dist/lib/compile.d.ts +4 -3
  7. package/dist/lib/compile.js +8 -3
  8. package/dist/lib/doctor.d.ts +10 -0
  9. package/dist/lib/doctor.js +33 -4
  10. package/dist/lib/index.d.ts +9 -0
  11. package/dist/lib/index.js +7 -0
  12. package/dist/lib/loadSyndicate.d.ts +13 -0
  13. package/dist/lib/loadSyndicate.js +14 -5
  14. package/dist/lib/observability/supabaseSpanExporter.js +2 -1
  15. package/dist/lib/observability/tracer.d.ts +2 -2
  16. package/dist/lib/observability/tracer.js +2 -2
  17. package/dist/lib/project.d.ts +116 -0
  18. package/dist/lib/project.js +274 -0
  19. package/dist/lib/runtime/native/checkpoint.d.ts +2 -1
  20. package/dist/lib/runtime/native/checkpoint.js +6 -2
  21. package/dist/lib/runtime/native/interrupts.js +9 -2
  22. package/dist/lib/runtime/nativeTurn.d.ts +2 -1
  23. package/dist/lib/runtime/nativeTurn.js +5 -1
  24. package/dist/lib/runtime/syndicateTurn.d.ts +11 -1
  25. package/dist/lib/runtime/syndicateTurn.js +383 -5
  26. package/dist/lib/sandbox/sandbox.d.ts +121 -0
  27. package/dist/lib/sandbox/sandbox.js +276 -0
  28. package/dist/lib/sandbox/worker.d.ts +29 -0
  29. package/dist/lib/sandbox/worker.js +280 -0
  30. package/dist/lib/script/config.d.ts +35 -0
  31. package/dist/lib/script/config.js +17 -0
  32. package/dist/lib/script/journal.d.ts +137 -0
  33. package/dist/lib/script/journal.js +193 -0
  34. package/dist/lib/script/run.d.ts +138 -0
  35. package/dist/lib/script/run.js +364 -0
  36. package/dist/lib/syndicateSchema.d.ts +20 -0
  37. package/dist/lib/syndicateSchema.js +98 -6
  38. package/dist/lib/toolRegistry.js +4 -0
  39. package/dist/lib/tools/runCodeTool.d.ts +50 -0
  40. package/dist/lib/tools/runCodeTool.js +59 -0
  41. package/dist/scripts/a2a_server.js +3 -3
  42. package/dist/scripts/doctor.js +2 -1
  43. package/dist/scripts/mcp_server.js +2 -1
  44. package/package.json +6 -1
package/CHANGELOG.md CHANGED
@@ -6,6 +6,119 @@ the starter pack and the templates), not the repo's full history.
6
6
 
7
7
  ## Unreleased
8
8
 
9
+ ## 1.8.0 — 2026-10-18
10
+
11
+ A minor release that gives a repository a manifest (ADR 0138). A
12
+ `melchizedek.json` at the root names the agents folder, the exact engine
13
+ version the project is written for, and how each syndicate ships; the
14
+ bins find it on their own, `melchizedek-doctor` checks the pin, and the
15
+ desktop app opens a repository through it. Nothing changes for a
16
+ deployment that sets `MELCHIZEDEK_AGENTS_DIR`.
17
+
18
+ ### Added
19
+
20
+ - **The project manifest, `melchizedek.json` (ADR 0138).** A JSON file at
21
+ a repository's root naming its agents folder (`agents`), the engine
22
+ version it is written for (`engine`, an exact pin), an `ignore` list,
23
+ a `tests` folder, the env variable *names* it needs, and per-file
24
+ shipping records (`syndicates.<file>.deliverable`,
25
+ `publish: { kind: "registry", id }`). New subpath
26
+ `melchizedek-agents/project` and barrel exports: `findProject`,
27
+ `loadProject`, `parseProjectManifest`, `resolveAgentsDir`,
28
+ `listProjectSyndicates`, `checkProject`, `projectManifestSchema`.
29
+ - **The agents directory reads the manifest.** Every loader and bin
30
+ resolves it as: explicit option, `MELCHIZEDEK_AGENTS_DIR`, the nearest
31
+ manifest's `agents` (not above the repository root), then
32
+ `<cwd>/config/agents`. A deployment that sets the variable is
33
+ unchanged.
34
+ - **`melchizedek-doctor` reports the project**: the engine pin against
35
+ the running engine, a missing agents or tests folder, `ignore`
36
+ patterns that match nothing, unset env names, and `syndicates` keys
37
+ with no file. A pin mismatch or a missing folder fails `--check`.
38
+
39
+ ## 1.7.0 — 2026-10-17
40
+
41
+ A minor release that lets a syndicate hold code (ADR 0137). A `script:`
42
+ block is the fourth orchestration method: one JavaScript module, run in
43
+ a QuickJS sandbox on a worker thread, calls the syndicate's agents, its
44
+ allowed tools and the person as functions, so routing, per-call models
45
+ and human-in-the-loop change with a YAML deploy instead of an engine
46
+ release. Agents on any provider get `run_code`, the same sandbox as a
47
+ registry tool. Both are off until the operator sets
48
+ `MELCHIZEDEK_CODE_SANDBOX=on`; nothing changes for a deployment that
49
+ does not.
50
+
51
+ ### Added
52
+
53
+ - **`script:` orchestration (ADR 0137 Part B).** `script: { source,
54
+ models?, tools? }` at the root; cannot be combined with `dispatch` or
55
+ `workflow`. The default export is called with `{ message, transcript,
56
+ variables, agent, tool, askUser, parallel, now, random, log }` and its
57
+ return is the answer; guards run on it once. `agent(name, input, {
58
+ model?, reasoning?, temperature?, conversation? })` runs a declared
59
+ agent (parsed JSON when it has an output schema, else text; a failed run
60
+ throws into the script); `conversation: true` answers in the
61
+ conversation as a dispatch route does. `askUser(id, question, {
62
+ payload?, schema? })` pauses the turn `input-required` with the same
63
+ `result.input` an `ask_user` node writes; the next message resumes the
64
+ run by replaying its journal (one content-less event per call, stored in
65
+ the conversation only when the run pauses) on the source the pause pinned, so a redeploy never breaks
66
+ a waiting conversation. A resume whose calls no longer match fails
67
+ `SCRIPT_DIVERGED`; a run past 1,000 calls fails `SCRIPT_CALL_LIMIT`.
68
+ The schema refuses `require_approval`, the `ask_user` tool, local skill
69
+ scripts and authorization_code grants on a script's agents (a pause
70
+ inside a called agent cannot be replayed yet), `context:` and root
71
+ `max_concurrency`; `mode: task` is allowed (a pause reached inside a
72
+ called agent anyway fails `PAUSE_INSIDE_AGENT`). `{{token}}`
73
+ interpolation skips `script.source`; the script reads the bound
74
+ `variables`. `script.require_approval` gates `tool()` calls with the
75
+ standard approval request (`result.approval`). Every `script.models`
76
+ pairing is checked before the run (`SCRIPT_MODEL_INVALID`). Under
77
+ `runDurableTurn` each call's journal event is a checkpoint boundary, so
78
+ a stopped worker resumes mid-script. `agent()` calls report through
79
+ `events.onAgentEvent`; spans `script.invoke` and `script.call`
80
+ (exported to `adk_telemetry`, no arguments or results) trace a run, and
81
+ each agent call's turn span carries `script.run` and `script.call.seq`.
82
+ A `yaml_reference` to a script syndicate runs its whole script as a
83
+ subagent, route or node, its pauses reaching the turn; `agent()` may
84
+ call nested workflow, dispatch and script syndicates.
85
+ `startEngineSpan` takes an optional parent context.
86
+ `TurnStage` gains `'script'`; `SyndicateTurnResult.failedStage` may be
87
+ `'script'`.
88
+ - **`run_code` (ADR 0137 Part A).** A registry tool: `{ code, input? }`,
89
+ an ES module whose default export is called with `input`, returning
90
+ `{ result, logs, error }`, each bounded at 20,000 characters. No
91
+ network, files, host, clock or randomness. An agent's
92
+ `run_code: { attach: true }` puts each run on the reply:
93
+ `SyndicateTurnResult.codeRuns` and, on the A2A server, a `code_runs`
94
+ artifact of `code_run` data parts. `DrainedRun` gains `codeRuns`.
95
+ - **The sandbox.** QuickJS (`quickjs-emscripten-core` and
96
+ `@jitl/quickjs-wasmfile-release-sync`, both pinned at 0.32.0) on a
97
+ worker thread per run: 64 MB memory, 1 MB stack, 5 s of compute (waits
98
+ on the host not counted), a watchdog and the turn's signal terminate
99
+ it; `MELCHIZEDEK_CODE_SANDBOX_WORKERS` (default 16) caps concurrent
100
+ runs. A syndicate with a script or a `run_code` agent is refused with
101
+ `CodeSandboxOffError` when a turn starts while the switch is off.
102
+ - **Package surface.** New subpaths `melchizedek-agents/script`
103
+ (`runScript`, `ScriptFatal`, `SCRIPT_CALL_LIMIT`, and the journal reader
104
+ `openRun` / `scriptPauseIn` with the `SCRIPT_*` marker keys and the
105
+ `JournalEntry`, `OpenRun`, `ScriptCallRecord`, `ScriptPauseRecord`,
106
+ `ScriptPinRecord` types) and
107
+ `melchizedek-agents/sandbox` (`runInSandbox`, `codeSandboxEnabled`,
108
+ `refuseWithoutSandbox`, `CodeSandboxOffError`, `SandboxHalt`,
109
+ `DEFAULT_SANDBOX_LIMITS`). The barrel adds those plus
110
+ `isScriptSyndicate`, `openRun`, `runCodeTool`, `RUN_CODE` and the types
111
+ `ScriptConfig`, `ScriptHost`, `ScriptCallSite`, `JournalEntry`, `OpenRun`,
112
+ `ScriptPauseRecord`, `ScriptPinRecord`, `ScriptRunInput`, `ScriptRunOutcome`,
113
+ `SandboxLimits`, `SandboxResult`, `SandboxRunOptions`, `CodeRun`,
114
+ `RunCodeOutput`.
115
+
116
+ ### For melch-app
117
+
118
+ - An Electron build must list `quickjs-emscripten-core`,
119
+ `@jitl/quickjs-wasmfile-release-sync` and `melchizedek-agents/dist/lib/sandbox`
120
+ in `asarUnpack`: the wasm file and the worker script load from disk.
121
+
9
122
  ## 1.6.0 — 2026-10-15
10
123
 
11
124
  A minor release for applications that show a turn as it runs and account
package/DOCUMENTATION.md CHANGED
@@ -35,6 +35,8 @@ lib/tools/mcpToolFactory.ts MCP client: remote tools → the engine's own Tools
35
35
  scripts/demo_mcp_server.ts demo MCP server (library catalog, SSE)
36
36
  lib/runtime/native/… the native loop: steps, delegation, compaction, interrupts
37
37
  lib/workflow/… the workflow scheduler for `workflow:` syndicates
38
+ lib/script/… the runner for `script:` syndicates: journal, replay, pause (ADR 0137)
39
+ lib/sandbox/… the QuickJS code sandbox on a worker thread (run_code, script:)
38
40
  lib/session/… Supabase-backed session service
39
41
  lib/memory/… pgvector long-term memory service
40
42
  lib/observability/… OpenTelemetry run tracing
@@ -63,7 +65,7 @@ agent. The native runtime is the only runtime
63
65
  ([ADR 0107](./wiki/decisions/0107-release-1-0-0-removes-adk.md)): it owns the
64
66
  loop, the model contract and its adapters, the tools, the sessions and
65
67
  the workflow scheduler, and runs a single-agent, delegating,
66
- plan-dispatch or `workflow:` syndicate, `context:` compaction
68
+ plan-dispatch, `workflow:` or `script:` syndicate, `context:` compaction
67
69
  ([ADR 0078](./wiki/decisions/0078-native-compaction-ports-adk-compactor.md)),
68
70
  `mode: task`, and pause and resume included
69
71
  ([ADR 0095](./wiki/decisions/0095-native-workflow-turn-drains-through-the-adk-reader.md)).
@@ -130,11 +132,13 @@ Field reference:
130
132
  | `memory_retention_days` | root | Days a fact in this syndicate's namespace is kept; the server deletes older facts when it loads the syndicate and daily after. Requires `memory_namespace` (never applied to the shared default namespace). |
131
133
  | `dispatch` | root | Switches the syndicate from DELEGATE to PLAN-DISPATCH routing (§6). `default_route` (required) names the fail-static subagent; `route_key` / `reason_key` name the router's JSON properties (defaults `route` / `reason`). Honoured by every surface (the CLI, the server, the worker, evals). |
132
134
  | `workflow` | root | Switches the syndicate to a WORKFLOW (§6): a graph whose nodes are its agents plus declared `join`, `map`, `tool` and `ask_user` nodes, and whose `edges` say what runs after what and on which route. The orchestrator is a node like any other; nothing delegates. Cannot be combined with `dispatch`. Contract: `lib/workflow.ts`; worked example: `examples/pipeline.yaml`. |
135
+ | `script` | root | Switches the syndicate to a SCRIPT ([ADR 0137](./wiki/decisions/0137-script-orchestration-and-sandboxed-code.md)): `source` is one JavaScript module, run in the engine's QuickJS sandbox, whose default export gets `{ message, transcript, variables, agent, tool, askUser, parallel, now, random, log }` and returns the answer. `models` lists the models `agent(name, input, { model })` may move a call to; `tools` the registry tools `tool(name, args)` may run. `askUser` pauses the turn; the next message replays the run's journal on the source it pinned. Needs `MELCHIZEDEK_CODE_SANDBOX=on`; cannot be combined with `dispatch` or `workflow`; the agents may not pause inside themselves (no `require_approval`, `ask_user` tool, local skill scripts or authorization_code grants). `{{token}}` does not touch `source`. |
133
136
  | `retries` | root | Self-correction, on by default ([ADR 0034](./wiki/decisions/0034-self-correction.md)): `model_errors` (default 2) retries a model reply marked malformed (`MALFORMED_FUNCTION_CALL`) instead of failing the turn; `tool_errors` (default 3) answers a tool that threw with structured reflection guidance and caps its retries. `0` turns either off. Every retry is a model call under `max_steps`. |
134
137
  | `guards` | root | Optional list of post-answer guard NAMES (`lib/guards/index.ts`). Each runs after the answering turn and before the reply publishes, receiving the final text plus every tool-result text of that turn, and rewrites in place rather than re-asking the model; its notes land in the `[STATUS]` stream. Guards declared by a syndicate reached through `yaml_reference:` count too — the server resolves the union via `collectGuards()`. Resolved by name, never by module path, so adding one is a deliberate act in code; **the published registry ships one, `science`** (citation checks for `research.yaml`); `registerGuard()` adds your own from code, and an unregistered name is warned about and skipped. |
135
138
  | `name` / `model` / `instruction` | agent | The agent triple. Any Gemini id, `claude-*`, or `ollama/*` for open-weight local models (see §5). |
136
139
  | `description` | subagent | **The delegation API.** The orchestrator reads this when deciding to hand off — write it like a function signature ("Use this subagent to…, pass it…"). |
137
140
  | `tools` | agent | Names resolved by the tool registry (§3). Long-term memory agents add `preload_memory` / `load_memory`. |
141
+ | `run_code` | agent | With `run_code` in `tools`, the agent runs JavaScript in the engine's sandbox on any provider (`{ code, input? }` → `{ result, logs, error }`; no network, host, clock or randomness; needs `MELCHIZEDEK_CODE_SANDBOX=on`, [ADR 0137](./wiki/decisions/0137-script-orchestration-and-sandboxed-code.md)). `run_code: { attach: true }` puts each run's code and result on the reply as a `code_run` data part (`result.codeRuns`). |
138
142
  | `reasoning` | agent | How hard the agent reasons, on any provider: `none`, `low`, `medium`, `high`, `xhigh`, `max`, or `{ budget_tokens: <int> }`. The compiler sends each provider the field it reads: a thinking level on Gemini 3, a thinking budget on Claude 4.6 and earlier (2,048 / 8,192 / 16,384 tokens for low / medium / high), adaptive thinking with that effort on later Claude models ([ADR 0049](./wiki/decisions/0049-claude-requests-by-model-generation.md)), an effort word on GPT, Grok, Kimi, Ollama and the gateway ([ADR 0047](./wiki/decisions/0047-provider-neutral-reasoning-key.md)). `xhigh` and `max` reach the models that take them (both on Claude from Opus 4.7 and Sonnet 5, `max` on Kimi K3, `xhigh` on GPT-5.2 and later and grok-4.7); elsewhere the model's highest setting is sent and the span carries `llm.reasoning.weakened` (`npm run doctor -- --matrix` lists each path, [ADR 0117](./wiki/decisions/0117-tool-choice-and-effort-above-high-in-v2.md)). Unset, each adapter keeps its own default. |
139
143
  | `sampling` | agent | `{ temperature, top_p, max_output_tokens, stop }`, the fields the engine sends to every provider; each adapter sends the ones its API takes ([ADR 0115](./wiki/decisions/0115-yaml-schema-v2-provider-neutral-keys.md)). |
140
144
  | `output` | agent | `{ schema, mime }`. `mime` is `application/json` (JSON mode, with or without a schema) or `text/plain` (the default). `schema` is the structured-JSON contract: the agent ends its turn on one JSON object matching it. An orchestrator may hold one beside its subagents: it delegates first, then answers in the schema itself. The schema travels in the provider's own structured-output field beside the tools on Claude from Opus 4.8, Sonnet 5 and Haiku 5.5, on OpenAI, and on Gemini 2+ through Vertex AI, and as a `set_model_response` tool elsewhere (the capability matrix's `structured_output_with_tools`, [ADR 0109](./wiki/decisions/0109-structured-output-beside-tools.md)). Worked example: `examples/structured_critic.yaml`. A leaf that holds the schema (`critic.yaml`) and plan-dispatch remain the choices when the answer should be a specialist's. |
@@ -1606,22 +1610,27 @@ subclass), and register it behind a model-id prefix.
1606
1610
  subagent. `scripts/demo_mcp_server.ts` is a complete server to copy —
1607
1611
  tool definitions, SSE wiring, and persistent state in ~250 lines.
1608
1612
 
1609
- **Teach your coding agent the framework**: `skills/` holds six Agent
1613
+ **Teach your coding agent the framework**: `skills/` holds eleven Agent
1610
1614
  Skills (the open SKILL.md standard — a directory per skill, `name` and
1611
1615
  `description` frontmatter) that give Claude Code, Codex, Cursor,
1612
1616
  OpenCode or Gemini CLI the catalog of syndicates and the procedures in
1613
1617
  this document: `melchizedek` (find and run, delegate a task),
1614
1618
  `melchizedek-author`, `melchizedek-serve`, `melchizedek-memory`,
1615
- `melchizedek-models`, `melchizedek-scribe`. `npx melchizedek-skills
1619
+ `melchizedek-models`, `melchizedek-scribe`, and five onboarding skills
1620
+ that route a person by the credentials they have — `melchizedek-onboard`
1621
+ (the triage) and `melchizedek-onboard-local`, `-keys`, `-cloud` and
1622
+ `-serve` (the authentication levels each owns). `npx melchizedek-skills
1616
1623
  install` copies them into `.claude/skills/` and `.agents/skills/` of the
1617
1624
  current project (`--for claude,codex,cursor,opencode,gemini,agents,all`,
1618
1625
  `--global`, `--dir <path>`, `--only <names>`, `--force`, `--dry-run`;
1619
1626
  `npm run skills:install` in a clone). The installer (`lib/skills.ts`)
1620
1627
  copies only from the package's own `skills/` directory, follows no
1621
- symlinks, and never overwrites a differing file without `--force`. Each
1622
- SKILL.md body was written by the Scribe syndicate
1623
- (`config/agents/examples/scribe.yaml`) from a brief of facts and reviewed
1624
- by a person; `skills/README.md` records the procedure.
1628
+ symlinks, and never overwrites a differing file without `--force`. The
1629
+ onboarding skills are short procedures written by hand that print their
1630
+ facts from `melchizedek-setup --level <id>`; every other SKILL.md body was
1631
+ written by the Scribe syndicate (`config/agents/examples/scribe.yaml`)
1632
+ from a brief of facts and reviewed by a person; `skills/README.md`
1633
+ records the procedure.
1625
1634
 
1626
1635
  ## 8. Security notes
1627
1636
 
@@ -943,6 +943,17 @@
943
943
  "type": "string",
944
944
  "const": "gemini"
945
945
  },
946
+ "run_code": {
947
+ "description": "How this agent's run_code tool behaves (ADR 0137). Needs run_code in `tools`.",
948
+ "type": "object",
949
+ "properties": {
950
+ "attach": {
951
+ "description": "Put each run's code and result on the reply as a code_run data part. Default false.",
952
+ "type": "boolean"
953
+ }
954
+ },
955
+ "additionalProperties": false
956
+ },
946
957
  "context": {
947
958
  "description": "Summarize a long conversation instead of overflowing the window. The orchestrator of a delegate syndicate only.",
948
959
  "type": "object",
@@ -1967,6 +1978,17 @@
1967
1978
  "type": "string",
1968
1979
  "const": "gemini"
1969
1980
  },
1981
+ "run_code": {
1982
+ "description": "How this agent's run_code tool behaves (ADR 0137). Needs run_code in `tools`.",
1983
+ "type": "object",
1984
+ "properties": {
1985
+ "attach": {
1986
+ "description": "Put each run's code and result on the reply as a code_run data part. Default false.",
1987
+ "type": "boolean"
1988
+ }
1989
+ },
1990
+ "additionalProperties": false
1991
+ },
1970
1992
  "context": {
1971
1993
  "description": "Summarize a long conversation instead of overflowing the window. The orchestrator of a delegate syndicate only.",
1972
1994
  "type": "object",
@@ -2302,6 +2324,47 @@
2302
2324
  "additionalProperties": false,
2303
2325
  "description": "Opts into a WORKFLOW: the syndicate as a graph (lib/workflow.ts). Cannot be combined with dispatch."
2304
2326
  },
2327
+ "script": {
2328
+ "type": "object",
2329
+ "properties": {
2330
+ "source": {
2331
+ "type": "string",
2332
+ "minLength": 1,
2333
+ "maxLength": 65536,
2334
+ "description": "One JavaScript module whose default export is an async function of { message, variables, agent, tool, askUser, parallel, now, random, log }: the orchestration. Its return value is the answer. {{token}} interpolation does not touch it; it reads `variables`."
2335
+ },
2336
+ "models": {
2337
+ "description": "The models agent(name, input, { model }) may move a call to; each agent is compiled and checked on every one at load.",
2338
+ "maxItems": 16,
2339
+ "type": "array",
2340
+ "items": {
2341
+ "type": "string",
2342
+ "minLength": 1
2343
+ }
2344
+ },
2345
+ "tools": {
2346
+ "description": "The registry tools tool(name, args) may run; the only path from the script to the network or the host.",
2347
+ "type": "array",
2348
+ "items": {
2349
+ "type": "string",
2350
+ "minLength": 1
2351
+ }
2352
+ },
2353
+ "require_approval": {
2354
+ "description": "Tools from script.tools whose every call waits for a person to approve the exact call (ADR 0028): the turn ends input-required; approved, the pinned arguments run, refused, the script gets { error }.",
2355
+ "type": "array",
2356
+ "items": {
2357
+ "type": "string",
2358
+ "minLength": 1
2359
+ }
2360
+ }
2361
+ },
2362
+ "required": [
2363
+ "source"
2364
+ ],
2365
+ "additionalProperties": false,
2366
+ "description": "Opts into SCRIPT orchestration (lib/script/): the syndicate's logic as code run in the engine's sandbox. Needs MELCHIZEDEK_CODE_SANDBOX=on. Cannot be combined with dispatch or workflow."
2367
+ },
2305
2368
  "retries": {
2306
2369
  "description": "Self-correction on model and tool errors: the engine's reflect-and-retry. On by default.",
2307
2370
  "type": "object",
@@ -190,6 +190,48 @@ dispatch:
190
190
  # Confirm: { ask_user: "Publish? Reply yes, or say what to change." }
191
191
  # max_concurrency: 4
192
192
 
193
+ # [optional] Switches this syndicate to a SCRIPT (ADR 0137): one
194
+ # JavaScript module, run in the engine's QuickJS sandbox, is the
195
+ # orchestration. It calls the agents below, the tools it lists and the
196
+ # person as functions, so routing, per-call models and pauses live here
197
+ # and change with a deploy. Needs MELCHIZEDEK_CODE_SANDBOX=on. Cannot be
198
+ # combined with `dispatch` or `workflow`. Contract: lib/script/.
199
+ #
200
+ # - The default export is called with { message, transcript, variables,
201
+ # agent, tool, askUser, parallel, now, random, log }; its return (text,
202
+ # or JSON) is the answer, and `guards` run on it once.
203
+ # - agent(name, input, { model?, reasoning?, temperature?,
204
+ # conversation? }) runs a declared agent seeing only `input` (null:
205
+ # the message); it returns parsed JSON when the agent has an output
206
+ # schema, else its text, and throws when the run fails. With
207
+ # `conversation: true` it answers in the conversation, as a dispatch
208
+ # route does. `model` must be in script.models.
209
+ # - tool(name, args) runs a registry tool listed in script.tools; one
210
+ # also in script.require_approval waits for a person to approve the
211
+ # exact call (refused, the script gets { error }).
212
+ # - askUser(id, question, { payload?, schema? }) pauses the turn
213
+ # input-required; the next message resumes it, returning
214
+ # { reply, input }. The run replays its journal from the top, so a
215
+ # script must make the same calls on every run: use now() and
216
+ # random(), never Date or Math.random (they throw), and never
217
+ # Promise.race.
218
+ # - require_approval, the ask_user tool, skills.scripts: local and
219
+ # authorization_code grants are refused on a script's agents in this
220
+ # version (a pause inside a called agent cannot be replayed).
221
+ # - {{token}} interpolation does not touch `source`; read `variables`.
222
+ # - A yaml_reference to a script syndicate runs its whole script as a
223
+ # subagent, a route or a node, its pauses reaching the turn.
224
+ # script:
225
+ # models: ["gemini-3.8-flash", "claude-opus-5-5"]
226
+ # tools: ["web_extract", "publish"]
227
+ # require_approval: ["publish"]
228
+ # source: |
229
+ # export default async ({ message, agent, askUser, log }) => {
230
+ # const triage = await agent("OrchestratorName", message);
231
+ # log(`Routed to ${triage.route}`);
232
+ # return agent(triage.route, null, { conversation: true });
233
+ # };
234
+
193
235
  # ── Orchestrator ────────────────────────────────────────────
194
236
  # The turn's root agent: the engine's agent loop runs it. Its keys are
195
237
  # spelled after ADK's LlmAgentConfig.
@@ -293,6 +335,14 @@ orchestrator:
293
335
  # Google's server-side sandbox; nothing runs on this host. Gemini only.
294
336
  # code_execution: "gemini"
295
337
 
338
+ # [optional] With `run_code` in tools, the agent runs JavaScript in the
339
+ # engine's QuickJS sandbox on any provider and rests its answer on the
340
+ # result (no network, no host, no clock; needs MELCHIZEDEK_CODE_SANDBOX=on,
341
+ # ADR 0137). `attach: true` puts each run's code and result on the reply
342
+ # as a code_run data part. Default off.
343
+ # run_code:
344
+ # attach: true
345
+
296
346
  # [optional] Summarize a long conversation instead of overflowing the
297
347
  # window: past `compact_after_tokens` (the last request's prompt), earlier
298
348
  # turns become one summary and the last `keep_recent_events` stay
@@ -375,6 +375,24 @@ export function approvalAnswer(rawParts, pending) {
375
375
  }
376
376
  return undefined;
377
377
  }
378
+ /** The `code_runs` artifact: one `code_run` data part per run an attaching agent made (ADR 0137 item 3). */
379
+ function publishCodeRuns(eventBus, taskId, contextId, runs) {
380
+ eventBus.publish(AgentEvent.artifactUpdate({
381
+ taskId,
382
+ contextId,
383
+ artifact: {
384
+ artifactId: 'code_runs',
385
+ name: 'code_runs',
386
+ description: 'The code the answer rests on, and what it returned',
387
+ parts: runs.map((run) => ({ content: { $case: 'data', value: run }, metadata: undefined, filename: '', mediaType: 'application/json' })),
388
+ metadata: undefined,
389
+ extensions: [],
390
+ },
391
+ append: false,
392
+ lastChunk: true,
393
+ metadata: undefined,
394
+ }));
395
+ }
378
396
  function publishFinal(eventBus, taskId, contextId, state, text) {
379
397
  eventBus.publish(AgentEvent.statusUpdate({
380
398
  taskId,
@@ -687,6 +705,9 @@ export class SyndicateExecutor {
687
705
  return;
688
706
  }
689
707
  log(`✓ Task ${short} complete — ${result.text.length.toLocaleString()} chars, ${spent}`);
708
+ // run_code's attach (ADR 0137): the code an answer rests on, and its result, as data parts beside it.
709
+ if (result.codeRuns?.length)
710
+ publishCodeRuns(eventBus, taskId, contextId, result.codeRuns);
690
711
  publishFinal(eventBus, taskId, contextId, 'completed', result.text || undefined);
691
712
  await report(ctx, 'completed', undefined, u);
692
713
  // Long-term memory: there is no "session end" on a server, so ingest
@@ -115,9 +115,10 @@ export type SpecTool = {
115
115
  dispatch: DispatchSpec;
116
116
  };
117
117
  /**
118
- * A nested dispatch (plan-dispatch) syndicate (ADR 0120): it runs as it
119
- * runs at the top, as a turn of its own on its own conversation (its
120
- * classifier picks a route, the route answers), so what it compiles to is
118
+ * A nested dispatch (plan-dispatch) syndicate (ADR 0120), or a nested
119
+ * script syndicate (ADR 0137 item 15): it runs as it runs at the top, as a
120
+ * turn of its own on its own conversation (its classifier picks a route and
121
+ * the route answers, or its script runs), so what it compiles to is
121
122
  * the file and the options its turn loads its routes with. The turn runner
122
123
  * runs it (lib/runtime/syndicateTurn.ts, TurnControl.nestedDispatch), as a
123
124
  * delegated call, a dispatch route or a workflow node.
@@ -37,6 +37,7 @@
37
37
  import { relative } from 'node:path';
38
38
  import { isDispatchSyndicate } from './dispatch.js';
39
39
  import { isWorkflowSyndicate, nodeKind } from './workflowConfig.js';
40
+ import { isScriptSyndicate } from './script/config.js';
40
41
  import { loadSyndicate, nestedLoader } from './loadSyndicate.js';
41
42
  import { REASONING_OLDER_SPELLING } from './syndicateSchema.js';
42
43
  import { reasoningConfig } from './models/reasoning.js';
@@ -134,7 +135,7 @@ function gateTools(tools, names, agentName) {
134
135
  }
135
136
  /** True when any agent of a syndicate may pause for approval: a gated tool, or skill scripts. */
136
137
  export function declaresApprovals(config) {
137
- return agentGates(config.orchestrator) || (config.subagents ?? []).some((s) => agentGates(s));
138
+ return agentGates(config.orchestrator) || (config.subagents ?? []).some((s) => agentGates(s)) || !!config.script?.require_approval?.length;
138
139
  }
139
140
  /**
140
141
  * An agent's `examples:` as their InstructionTool (lib/tools/examples.ts):
@@ -398,8 +399,9 @@ export async function compileEntrySpec(subCfg, opts = {}, delegated = false) {
398
399
  if (isWorkflowSyndicate(nested)) {
399
400
  return { kind: 'workflow', workflow: await compileWorkflowSpec(nested, nestedOpts, subCfg.name, subCfg.description, subCfg.yaml_reference, delegated) };
400
401
  }
401
- // A nested dispatch syndicate classifies and routes as at the top (ADR 0120), wherever it runs.
402
- if (isDispatchSyndicate(nested))
402
+ // A nested dispatch syndicate classifies and routes as at the top (ADR 0120), wherever it runs; a nested script
403
+ // syndicate runs its whole script the same way, as a turn of its own (ADR 0137 item 15).
404
+ if (isDispatchSyndicate(nested) || isScriptSyndicate(nested))
403
405
  return { kind: 'dispatch', dispatch: compileDispatchEntry(subCfg, nested, nestedOpts) };
404
406
  return { kind: 'agent', spec: await compileSpec(nested, nestedOpts, subCfg.name, subCfg.description) };
405
407
  }
@@ -437,6 +439,9 @@ export async function compileSubagentSpec(subCfg, opts = {}) {
437
439
  if (isDispatchSyndicate(nested)) {
438
440
  throw new Error(`${subCfg.yaml_reference}: '${subCfg.name}' is a dispatch syndicate, which runs as its own turn (its classifier, then a route), not as one agent; compile the entry with compileEntrySpec (ADR 0120).`);
439
441
  }
442
+ if (isScriptSyndicate(nested)) {
443
+ throw new Error(`${subCfg.yaml_reference}: '${subCfg.name}' is a script syndicate, which runs as its own turn (its script), not as one agent; compile the entry with compileEntrySpec (ADR 0137).`);
444
+ }
440
445
  return compileSpec(nested, nestedOpts, subCfg.name, subCfg.description);
441
446
  }
442
447
  // YAML v2 (ADR 0115): the engine form, for a config built in code (a loaded one already is).
@@ -27,6 +27,7 @@ import type { Platform } from './models/endpoints.ts';
27
27
  import type { CapabilityGap, CapabilityReport } from './models/capabilities.ts';
28
28
  import type { ProviderId } from './models/providerMap.ts';
29
29
  import type { RuntimeName, RuntimeSource } from './runtime/runtimeFlag.ts';
30
+ import { type ProjectCheck } from './project.ts';
30
31
  /**
31
32
  * What a file costs to run: `keyless` (all Ollama), a single provider's id
32
33
  * when every agent shares one (`gemini`, `anthropic`, `openai`, `xai` — one
@@ -169,7 +170,16 @@ export interface DoctorResult {
169
170
  serving?: DoctorServing;
170
171
  /** A stored Sign in with ChatGPT credential (ADR 0126), when one is present or its variables are set. */
171
172
  chatgpt?: DoctorChatGpt;
173
+ /** The project manifest (ADR 0138) found from the working directory, when there is one. */
174
+ project?: DoctorProject;
172
175
  }
176
+ /** A `melchizedek.json` at or above the working directory: its checks, or why it does not load. */
177
+ export type DoctorProject = ProjectCheck | {
178
+ root: string;
179
+ error: string;
180
+ problems: string[];
181
+ warnings: string[];
182
+ };
173
183
  /**
174
184
  * A Sign in with ChatGPT credential (ADR 0126): where it is, whether it is
175
185
  * signed in and carries OpenAI ids here, and what stops it. Never a token.
@@ -42,6 +42,7 @@ import { OAUTH_HOSTS_ENV } from './tools/oauthHosts.js';
42
42
  import { CREDENTIAL_HOSTS_ENV, credentialHosts } from './tools/credentialHosts.js';
43
43
  import { syndicateCredentialUses } from './tools/credentialUses.js';
44
44
  import { credentialHostEnvProblems, unboundCredentialWarning } from './a2a/oauthSetup.js';
45
+ import { checkProject, findProject, isIgnored, ProjectManifestError, resolveAgentsDir } from './project.js';
45
46
  /** The `gateway.id` the doctor reports when `MODEL_GATEWAY` names no known gateway; the value itself is never kept. */
46
47
  const UNRECOGNISED_GATEWAY = 'unrecognised';
47
48
  function resolvable(module) {
@@ -457,10 +458,11 @@ export function flagMemoryNamespaces(syndicates) {
457
458
  }
458
459
  }
459
460
  export function runDoctor(options = {}) {
460
- const agentsDir = path.resolve(options.agentsDir ??
461
- process.env.MELCHIZEDEK_AGENTS_DIR ??
462
- path.join(process.cwd(), 'config', 'agents'));
463
- const syndicates = listSyndicateFiles(agentsDir).map((f) => diagnoseSyndicate(f, agentsDir, options.scripts));
461
+ const agentsDir = resolveAgentsDir(options.agentsDir);
462
+ const { project, report: projectReport } = projectFromCwd();
463
+ // The manifest's ignore list applies when the doctor reads the project's own folder.
464
+ const listed = listSyndicateFiles(agentsDir).filter((f) => !project || agentsDir !== project.agentsDir || !isIgnored(project, path.relative(project.root, path.join(agentsDir, f))));
465
+ const syndicates = listed.map((f) => diagnoseSyndicate(f, agentsDir, options.scripts));
464
466
  // Which single variable unlocks what. A syndicate counts toward a
465
467
  // variable only when that variable is ALL it is missing.
466
468
  const byEnv = new Map();
@@ -541,8 +543,22 @@ export function runDoctor(options = {}) {
541
543
  providers: providerPaths(),
542
544
  ...(serving ? { serving } : {}),
543
545
  ...(chatgpt ? { chatgpt } : {}),
546
+ ...(projectReport ? { project: projectReport } : {}),
544
547
  };
545
548
  }
549
+ /** The project at or above the working directory, checked; a broken manifest becomes a problem, not a throw. */
550
+ function projectFromCwd() {
551
+ try {
552
+ const project = findProject();
553
+ return project ? { project, report: checkProject(project) } : {};
554
+ }
555
+ catch (e) {
556
+ if (!(e instanceof ProjectManifestError))
557
+ throw e;
558
+ const problem = `melchizedek.json: ${e.issues.join('; ')}`;
559
+ return { report: { root: path.dirname(e.file), error: problem, problems: [problem], warnings: [] } };
560
+ }
561
+ }
546
562
  /**
547
563
  * The Sign in with ChatGPT line (ADR 0126), when a credential file is present
548
564
  * or one of its variables is set. `served`: a serving variable is set, so
@@ -613,6 +629,8 @@ export function checkProblems(result) {
613
629
  problems.push(`credentials: ${p}`);
614
630
  for (const p of result.chatgpt?.problems ?? [])
615
631
  problems.push(`chatgpt: ${p}`);
632
+ for (const p of result.project?.problems ?? [])
633
+ problems.push(`project: ${p}`);
616
634
  for (const s of result.syndicates) {
617
635
  for (const g of s.grants ?? [])
618
636
  for (const p of g.hostProblems ?? [])
@@ -635,6 +653,17 @@ export function renderDoctor(result, opts = {}) {
635
653
  : `${c.green}${text}${c.reset}`;
636
654
  lines.push(`${c.bold}melchizedek doctor${c.reset} ${c.dim}· ${result.agentsDir}${c.reset}`);
637
655
  lines.push('');
656
+ // The project manifest (ADR 0138): the engine pin, and what in it is stale.
657
+ if (result.project) {
658
+ const p = result.project;
659
+ const pin = 'engine' in p ? `${p.engine.matches ? c.green + '✓' : c.red + '✗'}${c.reset} engine ${p.engine.pinned}${p.engine.matches ? '' : `${c.red}, ${p.engine.running} running${c.reset}`} · ${p.syndicates.length} syndicate(s) ` : '';
660
+ lines.push(`project ${pin}${c.dim}${p.root}${c.reset}`);
661
+ for (const x of p.problems)
662
+ if (!x.startsWith('engine:'))
663
+ lines.push(` ${c.red}✗ ${x}${c.reset}`);
664
+ for (const x of p.warnings)
665
+ lines.push(` ${c.yellow}! ${x}${c.reset}`);
666
+ }
638
667
  // The runtime a turn would run on, and where that came from (ADR 0107).
639
668
  if (result.runtime) {
640
669
  const rt = result.runtime;
@@ -28,6 +28,14 @@ export type { AgentBranch, DrainedRun, MessagePart, TurnUsage, RouteDecision, Sy
28
28
  export type { CompileOptions } from './compile.ts';
29
29
  export { isWorkflowSyndicate, describeInput } from './workflow.ts';
30
30
  export type { WorkflowConfig, WorkflowNodeYaml, EdgeElement, PendingInput } from './workflow.ts';
31
+ export { isScriptSyndicate } from './script/config.ts';
32
+ export type { ScriptConfig } from './script/config.ts';
33
+ export { openRun, runScript, ScriptFatal, SCRIPT_CALL_LIMIT } from './script/run.ts';
34
+ export type { JournalEntry, OpenRun, ScriptCallSite, ScriptHost, ScriptPauseRecord, ScriptPinRecord, ScriptRunInput, ScriptRunOutcome } from './script/run.ts';
35
+ export { CODE_SANDBOX_ENV, CodeSandboxOffError, DEFAULT_SANDBOX_LIMITS, SandboxHalt, codeSandboxEnabled, runInSandbox } from './sandbox/sandbox.ts';
36
+ export type { SandboxLimits, SandboxResult, SandboxRunOptions } from './sandbox/sandbox.ts';
37
+ export { RUN_CODE, runCodeTool } from './tools/runCodeTool.ts';
38
+ export type { CodeRun, RunCodeOutput } from './tools/runCodeTool.ts';
31
39
  export { createA2AApp, compileAgentCard, currentRequestContext } from './a2a/app.ts';
32
40
  export type { A2AApp, A2AAppOptions, RequestIdentity } from './a2a/app.ts';
33
41
  export type { A2AContext } from './a2a/executor.ts';
@@ -59,3 +67,4 @@ export { PROVIDERS, providerForModel, providerKeyPresent, providerStatuses, logP
59
67
  export { loadEnv } from './loadEnv.ts';
60
68
  export { setCredentialSource, credentialValue } from './models/credentialSource.ts';
61
69
  export type { CredentialSource } from './models/credentialSource.ts';
70
+ export { PROJECT_MANIFEST, projectManifestSchema, ProjectManifestError, findProject, findProjectRoot, loadProject, parseProjectManifest, resolveAgentsDir, listProjectSyndicates, checkProject, type Project, type ProjectManifest, type ProjectCheck, } from './project.ts';
package/dist/lib/index.js CHANGED
@@ -23,6 +23,11 @@ export { DEFAULT_RUNTIME, describeRuntime, RuntimeRemovedError, UnsupportedOnRun
23
23
  export { InProcessSessionService } from './runtime/sessions.js';
24
24
  export { approvalResponsePart, pendingApproval, APPROVAL_REQUEST } from './runtime/approvals.js';
25
25
  export { isWorkflowSyndicate, describeInput } from './workflow.js';
26
+ // SCRIPT orchestration and the code sandbox (ADR 0137).
27
+ export { isScriptSyndicate } from './script/config.js';
28
+ export { openRun, runScript, ScriptFatal, SCRIPT_CALL_LIMIT } from './script/run.js';
29
+ export { CODE_SANDBOX_ENV, CodeSandboxOffError, DEFAULT_SANDBOX_LIMITS, SandboxHalt, codeSandboxEnabled, runInSandbox } from './sandbox/sandbox.js';
30
+ export { RUN_CODE, runCodeTool } from './tools/runCodeTool.js';
26
31
  // ── Serve it over A2A, or call a remote A2A agent ───────────────────────────
27
32
  export { createA2AApp, compileAgentCard, currentRequestContext } from './a2a/app.js';
28
33
  export { callerTokens, firstOf, hashCallerToken, jwtIdentity, parseCallers, scopeSegment, sharedSecret, trustedHeader, } from './a2a/identity.js';
@@ -50,3 +55,5 @@ describeCapabilities, capabilitySummary, planTransport, gatewayConfig, gatewayPr
50
55
  export { loadEnv } from './loadEnv.js';
51
56
  // Where credentials are read, by environment variable name (ADR 0133).
52
57
  export { setCredentialSource, credentialValue } from './models/credentialSource.js';
58
+ // Project manifest, melchizedek.json (ADR 0138).
59
+ export { PROJECT_MANIFEST, projectManifestSchema, ProjectManifestError, findProject, findProjectRoot, loadProject, parseProjectManifest, resolveAgentsDir, listProjectSyndicates, checkProject, } from './project.js';
@@ -11,6 +11,8 @@ export type { SkillsConfig } from './tools/skillToolset.ts';
11
11
  import type { WorkflowConfig } from './workflowConfig.ts';
12
12
  import type { ReasoningSetting } from './models/contract.ts';
13
13
  export type { WorkflowConfig } from './workflowConfig.ts';
14
+ export type { ScriptConfig } from './script/config.ts';
15
+ import type { ScriptConfig } from './script/config.ts';
14
16
  export interface ThinkingConfig {
15
17
  /** Token budget for internal reasoning. 0 = off, -1 = dynamic. */
16
18
  thinkingBudget?: number;
@@ -175,6 +177,10 @@ export interface AgentYamlConfig {
175
177
  openapi?: OpenApiConfig[];
176
178
  /** `gemini`: the model's Python runs in Gemini's server-side sandbox (ADR 0033). Gemini models only. */
177
179
  code_execution?: 'gemini';
180
+ /** How this agent's `run_code` tool behaves (ADR 0137): `attach` puts each run's code and result on the reply. Needs run_code in `tools`. */
181
+ run_code?: {
182
+ attach?: boolean;
183
+ };
178
184
  /** Compact a long conversation into a summary past a token threshold (ADR 0033). Delegate orchestrator only. */
179
185
  context?: ContextConfig;
180
186
  /** `task`: the agent works until it calls finish_task; its arguments are the node's output. Workflow nodes only. */
@@ -239,6 +245,13 @@ export interface SyndicateYamlConfig {
239
245
  * Contract: lib/workflow.ts.
240
246
  */
241
247
  workflow?: WorkflowConfig;
248
+ /**
249
+ * Opts this syndicate into SCRIPT orchestration (ADR 0137): one JavaScript
250
+ * module, run in the engine's sandbox, calls the agents, the allowed tools
251
+ * and a person as functions. Cannot be combined with `dispatch` or
252
+ * `workflow`. Needs MELCHIZEDEK_CODE_SANDBOX=on. Contract: lib/script/.
253
+ */
254
+ script?: ScriptConfig;
242
255
  /** Optional default variable definitions for {{token}} interpolation. */
243
256
  variables?: VariableMap;
244
257
  /** Defines the persistence and semantic memory layer for the syndicate. */