@zergai/ztc 0.26.6-dev.202611240947 → 0.27.0-beta.1
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 +629 -167
- package/RUST_THIRD_PARTY_LICENSES.txt +5 -5
- package/bin/zmux-darwin-arm64 +0 -0
- package/bin/zmux-linux-x64 +0 -0
- package/bin/ztc-audio-darwin-arm64 +0 -0
- package/bin/ztc-capture-darwin-arm64 +0 -0
- package/bin/ztc-computer-darwin-arm64 +0 -0
- package/bin/ztc-llm-darwin-arm64 +0 -0
- package/bin/ztc-tts-darwin-arm64 +0 -0
- package/bin/ztc-tui-darwin-arm64 +0 -0
- package/bin/ztc-tui-linux-x64 +0 -0
- package/bin/zterm-darwin-arm64 +2 -2
- package/dist/App.js +6 -1
- package/dist/agent/agent.d.ts +21 -15
- package/dist/agent/agent.js +389 -163
- package/dist/agent/backends/codex_app_server.d.ts +7 -1
- package/dist/agent/backends/codex_app_server.js +11 -1
- package/dist/agent/backends/gemini.js +22 -8
- package/dist/agent/commands/batch.js +8 -0
- package/dist/agent/commands/config.js +10 -0
- package/dist/agent/commands/environment.js +5 -5
- package/dist/agent/commands/goal.d.ts +1 -0
- package/dist/agent/commands/goal.js +2 -1
- package/dist/agent/commands/index.js +2 -1
- package/dist/agent/commands/manifest.js +4 -0
- package/dist/agent/commands/optimize.js +5 -2
- package/dist/agent/commands/pane_cli.d.ts +29 -0
- package/dist/agent/commands/pane_cli.js +303 -0
- package/dist/agent/commands/settings.js +13 -3
- package/dist/agent/commands/shannon.js +8 -0
- package/dist/agent/commands/update.js +15 -3
- package/dist/agent/commands/zerg.js +31 -1
- package/dist/agent/commands/ztc_pane.js +3 -1
- package/dist/agent/factory.d.ts +4 -0
- package/dist/agent/factory.js +11 -1
- package/dist/agent/loop_policy.d.ts +42 -5
- package/dist/agent/loop_policy.js +206 -3
- package/dist/agent/runtime/command_jobs.d.ts +5 -0
- package/dist/agent/runtime/command_jobs.js +241 -31
- package/dist/agent/runtime/command_wait.d.ts +2 -0
- package/dist/agent/runtime/command_wait.js +1 -0
- package/dist/agent/runtime/done_report.d.ts +18 -0
- package/dist/agent/runtime/done_report.js +130 -0
- package/dist/agent/runtime/execution_context.js +4 -1
- package/dist/agent/runtime/failure_cause.d.ts +2 -0
- package/dist/agent/runtime/failure_cause.js +35 -0
- package/dist/agent/runtime/policy.d.ts +14 -2
- package/dist/agent/runtime/policy.js +104 -12
- package/dist/agent/runtime/quiet_job.d.ts +18 -0
- package/dist/agent/runtime/quiet_job.js +48 -0
- package/dist/agent/runtime/recovery.d.ts +1 -5
- package/dist/agent/runtime/recovery.js +2 -44
- package/dist/agent/runtime/tool_runtime.d.ts +1 -0
- package/dist/agent/runtime/tool_runtime.js +2 -0
- package/dist/agent/subagent.js +6 -1
- package/dist/agent/tools/deferred.js +1 -0
- package/dist/agent/tools/index.d.ts +2 -0
- package/dist/agent/tools/index.js +1 -0
- package/dist/agent/tools/outcome.d.ts +15 -0
- package/dist/agent/tools/outcome.js +107 -0
- package/dist/agent/tools/pane.d.ts +2 -1
- package/dist/agent/tools/pane.js +21 -3
- package/dist/agent/tools/registry.js +2 -0
- package/dist/agent/tools/shell.d.ts +1 -0
- package/dist/agent/tools/shell.js +128 -17
- package/dist/agent/tools/sim_domain_tools.js +4 -2
- package/dist/agent/tools/supervision.js +4 -2
- package/dist/agent/tools/types.d.ts +3 -0
- package/dist/app/command_context.js +5 -9
- package/dist/app/conversation_compaction.d.ts +4 -2
- package/dist/app/conversation_compaction.js +16 -5
- package/dist/app/headless_shell_mode.d.ts +14 -0
- package/dist/app/headless_shell_mode.js +7 -0
- package/dist/app/hosted_resident.d.ts +356 -0
- package/dist/app/hosted_resident.js +677 -0
- package/dist/app/restart.js +11 -2
- package/dist/app/session_manager.d.ts +5 -0
- package/dist/app/session_manager.js +21 -6
- package/dist/app/session_messages.js +6 -6
- package/dist/app/workspace_layout_controller.js +7 -0
- package/dist/batch/worktree.d.ts +10 -0
- package/dist/batch/worktree.js +15 -0
- package/dist/bench/registry.js +4 -2
- package/dist/cli_args.js +1 -1
- package/dist/cli_main.js +80 -10
- package/dist/cli_ratatui.d.ts +75 -0
- package/dist/cli_ratatui.js +485 -84
- package/dist/components/InputArea.js +4 -4
- package/dist/config/execution.d.ts +10 -0
- package/dist/config/execution.js +10 -0
- package/dist/config/hosted_runtime.d.ts +4 -1
- package/dist/config/hosted_runtime.js +4 -1
- package/dist/config/permission_pin.d.ts +34 -2
- package/dist/config/permission_pin.js +52 -2
- package/dist/config/person_command_policy.d.ts +38 -0
- package/dist/config/person_command_policy.js +11 -0
- package/dist/config/profiles.d.ts +21 -0
- package/dist/config/profiles.js +56 -2
- package/dist/config/types.d.ts +4 -1
- package/dist/config.js +34 -19
- package/dist/desktop/daily/codex_exec_adapter.js +7 -2
- package/dist/desktop/daily/desktop_wiring.d.ts +7 -1
- package/dist/desktop/daily/desktop_wiring.js +11 -3
- package/dist/desktop/daily/recipes.js +20 -4
- package/dist/desktop/health.d.ts +24 -0
- package/dist/desktop/health.js +151 -0
- package/dist/desktop/native_dialogs.d.ts +37 -0
- package/dist/desktop/native_dialogs.js +105 -0
- package/dist/diagnostics/session_export.d.ts +1 -0
- package/dist/diagnostics/session_export.js +28 -0
- package/dist/embedded/registry.js +4 -2
- package/dist/emulation/tool_adapters.js +9 -4
- package/dist/emulation/ztc_addendum.js +3 -0
- package/dist/headless.d.ts +11 -4
- package/dist/headless.js +99 -34
- package/dist/headless_approval.d.ts +6 -0
- package/dist/headless_cli.js +1 -1
- package/dist/hooks/runner.js +7 -6
- package/dist/library.d.ts +4 -0
- package/dist/library.js +3 -0
- package/dist/mcp/client.js +3 -3
- package/dist/modes/registry.js +13 -4
- package/dist/onboarding/setup_environment.js +13 -1
- package/dist/optimize/adapters/kernelbench.js +88 -10
- package/dist/optimize/adapters/types.d.ts +5 -0
- package/dist/optimize/executor.js +21 -0
- package/dist/panes/cli_install.d.ts +54 -0
- package/dist/panes/cli_install.js +157 -0
- package/dist/panes/model_catalog.js +3 -0
- package/dist/panes/new_pane_choices.d.ts +6 -2
- package/dist/panes/new_pane_choices.js +24 -12
- package/dist/panes/pane_cli_launch.d.ts +50 -0
- package/dist/panes/pane_cli_launch.js +124 -0
- package/dist/panes/pane_clis.d.ts +171 -0
- package/dist/panes/pane_clis.js +325 -0
- package/dist/panes/person_only_panes.d.ts +45 -0
- package/dist/panes/person_only_panes.js +117 -0
- package/dist/plugin/simulation_living.js +0 -2
- package/dist/plugin/simulation_living_engine.d.ts +1 -0
- package/dist/plugin/simulation_living_engine.js +1 -0
- package/dist/plugin/simulation_living_labels.d.ts +3 -0
- package/dist/plugin/simulation_living_labels.js +2 -0
- package/dist/router_auth.js +12 -1
- package/dist/session.js +26 -1
- package/dist/simulation/art/gemini_image.js +11 -5
- package/dist/simulation/domains/loader.js +25 -3
- package/dist/simulation/living/cold.d.ts +23 -0
- package/dist/simulation/living/cold.js +41 -0
- package/dist/simulation/living/context.d.ts +591 -1283
- package/dist/simulation/living/context.js +378 -244
- package/dist/simulation/living/contradictions.d.ts +14 -0
- package/dist/simulation/living/contradictions.js +282 -0
- package/dist/simulation/living/core/ids.d.ts +53 -0
- package/dist/simulation/living/core/ids.js +39 -0
- package/dist/simulation/living/core/kind.d.ts +20 -0
- package/dist/simulation/living/core/kind.js +15 -0
- package/dist/simulation/living/core/laws.d.ts +179 -0
- package/dist/simulation/living/core/laws.js +184 -0
- package/dist/simulation/living/core/ops.d.ts +257 -0
- package/dist/simulation/living/core/ops.js +34 -0
- package/dist/simulation/living/definer.d.ts +17 -1
- package/dist/simulation/living/definer.js +130 -51
- package/dist/simulation/living/definition.d.ts +174 -65
- package/dist/simulation/living/definition.js +287 -180
- package/dist/simulation/living/director.d.ts +80 -0
- package/dist/simulation/living/director.js +363 -0
- package/dist/simulation/living/frame.d.ts +108 -21
- package/dist/simulation/living/frame.js +111 -66
- package/dist/simulation/living/identifiers.d.ts +0 -1
- package/dist/simulation/living/identifiers.js +0 -3
- package/dist/simulation/living/index.d.ts +18 -9
- package/dist/simulation/living/index.js +18 -9
- package/dist/simulation/living/inference.d.ts +92 -50
- package/dist/simulation/living/inference.js +319 -175
- package/dist/simulation/living/knowledge.d.ts +28 -0
- package/dist/simulation/living/knowledge.js +73 -0
- package/dist/simulation/living/library.json +1177 -0
- package/dist/simulation/living/mechanics.d.ts +31 -34
- package/dist/simulation/living/mechanics.js +222 -138
- package/dist/simulation/living/model-input.d.ts +6 -3
- package/dist/simulation/living/model-input.js +18 -26
- package/dist/simulation/living/open-world.d.ts +74 -94
- package/dist/simulation/living/open-world.js +151 -30
- package/dist/simulation/living/phrases.d.ts +3 -0
- package/dist/simulation/living/phrases.js +37 -0
- package/dist/simulation/living/phrases.json +560 -0
- package/dist/simulation/living/projection.d.ts +316 -2
- package/dist/simulation/living/projection.js +50 -2
- package/dist/simulation/living/prompts/example-spec.json +11 -0
- package/dist/simulation/living/prompts/role-text.json +14 -0
- package/dist/simulation/living/prompts.d.ts +5 -0
- package/dist/simulation/living/prompts.js +275 -0
- package/dist/simulation/living/recall.d.ts +2 -2
- package/dist/simulation/living/reduce.d.ts +51 -14
- package/dist/simulation/living/reduce.js +941 -579
- package/dist/simulation/living/replay.d.ts +5 -4
- package/dist/simulation/living/replay.js +10 -9
- package/dist/simulation/living/runtime.d.ts +135 -25
- package/dist/simulation/living/runtime.js +1641 -583
- package/dist/simulation/living/scale.d.ts +96 -0
- package/dist/simulation/living/scale.js +124 -0
- package/dist/simulation/living/script.d.ts +5 -0
- package/dist/simulation/living/script.js +115 -0
- package/dist/simulation/living/shared-kinds.d.ts +4 -11
- package/dist/simulation/living/shared-kinds.js +11 -81
- package/dist/simulation/living/space.d.ts +14 -0
- package/dist/simulation/living/space.js +132 -0
- package/dist/simulation/living/state.d.ts +100 -0
- package/dist/simulation/living/state.js +393 -0
- package/dist/simulation/living/types.d.ts +829 -512
- package/dist/simulation/living/types.js +162 -187
- package/dist/simulation/living/world-rules.d.ts +100 -0
- package/dist/simulation/living/world-rules.js +267 -0
- package/dist/simulation/plugins/loader.js +11 -0
- package/dist/skills/loader.js +4 -0
- package/dist/tools.d.ts +1 -0
- package/dist/tools.js +1 -0
- package/dist/ui/ratatui/headless_pane_manager.d.ts +4 -1
- package/dist/ui/ratatui/headless_pane_manager.js +13 -9
- package/dist/ui/ratatui/pane_relay.js +29 -0
- package/dist/ui/ratatui/pane_type.d.ts +10 -0
- package/dist/ui/ratatui/pane_type.js +11 -0
- package/dist/ui/ratatui/process.d.ts +30 -0
- package/dist/ui/ratatui/process.js +133 -19
- package/dist/ui/ratatui/protocol.d.ts +124 -2
- package/dist/ui/ratatui/protocol.js +35 -0
- package/dist/ui/ratatui/relay_policy.js +3 -0
- package/dist/ui/ratatui/ztc_launch_trust.d.ts +2 -1
- package/dist/ui/ratatui/ztc_launch_trust.js +8 -1
- package/dist/ui/terminal_image.d.ts +12 -0
- package/dist/ui/terminal_image.js +21 -1
- package/dist/utils/codex_project_trust.d.ts +19 -0
- package/dist/utils/codex_project_trust.js +37 -0
- package/dist/utils/compaction.js +103 -20
- package/dist/utils/git_program_files.d.ts +19 -12
- package/dist/utils/git_program_files.js +411 -83
- package/dist/utils/host_shell.d.ts +10 -1
- package/dist/utils/host_shell.js +14 -1
- package/dist/utils/mid_turn_compaction.d.ts +2 -0
- package/dist/utils/mid_turn_compaction.js +2 -0
- package/dist/utils/models.js +6 -1
- package/dist/utils/sandbox.d.ts +109 -8
- package/dist/utils/sandbox.js +981 -187
- package/dist/utils/shell.d.ts +2 -0
- package/dist/utils/shell.js +10 -4
- package/dist/utils/subprocess_env.d.ts +47 -11
- package/dist/utils/subprocess_env.js +107 -15
- package/dist/utils/system_ps.d.ts +8 -0
- package/dist/utils/system_ps.js +39 -7
- package/dist/utils/unattended_compaction.d.ts +9 -0
- package/dist/utils/unattended_compaction.js +13 -0
- package/dist/utils/ztc_own_state.d.ts +8 -5
- package/dist/utils/ztc_own_state.js +17 -11
- package/dist/web/mirror_client.js +1 -0
- package/dist/web/mirror_stream.d.ts +50 -6
- package/dist/web/mirror_stream.js +160 -15
- package/dist/workflows/registry.js +3 -1
- package/dist/workspace/controller.js +12 -0
- package/dist/workspace_collision.d.ts +3 -0
- package/dist/workspace_collision.js +14 -1
- package/dist/workspace_session_context.js +2 -1
- package/dist/zlm-embed.mjs +3823 -1970
- package/node_modules/@zerg/model-contracts/README.md +16 -4
- package/node_modules/@zerg/model-contracts/index.js +6 -1
- package/npm-shrinkwrap.json +1 -1
- package/package.json +42 -24
- package/packages/model-contracts/README.md +16 -4
- package/packages/model-contracts/index.js +6 -1
|
@@ -0,0 +1,677 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A hosted resident: the ZTC a runtime starts with --headless --mirror whose host stubbed its shell
|
|
3
|
+
* (ZTC_HEADLESS_SHELL_MODE=stub, see stubsMirrorShell), as ZergCloud launches a ZeP service zerg. Its mirror
|
|
4
|
+
* controllers prompt its agent, and the agent keeps its own tools in the zerg's container: that is the product. What
|
|
5
|
+
* the controllers send the app itself is held to the decisions here, default deny, so nothing they type starts a
|
|
6
|
+
* process, writes a file or mints a credential outside the agent. Each decision, and where ZTC applies it:
|
|
7
|
+
* - slash commands: hostedResidentCommandRefusal (cli_ratatui handleSubmit), run with hostedResidentCommandContext
|
|
8
|
+
* (buildCommandContext);
|
|
9
|
+
* - mux commands: hostedResidentMuxDecision (handleMirrorMuxCommand, and spawnZtcPane for ZTC's own callers); a ZTC
|
|
10
|
+
* pane its agent's tools open: hostedResidentAgentZtcPaneDecision, through hostedResidentAgentPaneTransport (the pane
|
|
11
|
+
* tools' transport), as a controller's create_ztc_pane, and no other pane its agent's tools ask for; and no pane at
|
|
12
|
+
* all that one of its ZTC panes asks for through its pane relay: hostedResidentRelayTransport (cli_ratatui's
|
|
13
|
+
* PaneRelayServer, once landing step 3's --m3z-fixes wiring is in);
|
|
14
|
+
* - pane input: hostedResidentPaneInputRefusal (the mirror's pane-input fallback, and write_pane), only into a ZTC pane
|
|
15
|
+
* the resident opened itself, and a bus message for a Claude Code or Codex pane: hostedResidentAgentPaneMessageAllowed
|
|
16
|
+
* (MirrorStreamServer.deliverBusMessage);
|
|
17
|
+
* - mirror routes: hostedResidentRouteRefusal and servesDebugLogs (MirrorStreamServer's request and upgrade handlers),
|
|
18
|
+
* zlicky capture uploads: hostedResidentTakesCapture, and reads of a capture: hostedResidentServesCaptureRead;
|
|
19
|
+
* - what the mirror publishes, and keeps for replay, of a tool's result: hostedResidentPublishedToolResult, and of the
|
|
20
|
+
* agent's evidence ledger: hostedResidentPublishedEvidence (MirrorStreamServer.publish and storeEvent);
|
|
21
|
+
* - a ZTC pane's environment: hostedResidentPaneEnv (childZtcPaneEnv) with the extension roots the launch named,
|
|
22
|
+
* resolved once as the resident starts (hostedResidentExtensionRoots); its arguments: hostedResidentPaneArgs (the
|
|
23
|
+
* pane's spawn); where the pane may start: hostedResidentPaneStartRefusal (cli_main, in the pane, as it starts);
|
|
24
|
+
* which extension files it loads: hostedResidentExtensionFileRefusal (each of its extension loaders, through
|
|
25
|
+
* loadsExtensionFile, and a domain's template loader for each template); and which modules it runs, whoever imports
|
|
26
|
+
* them: hostedResidentModuleRefusal (a resolve hook, holdResidentPaneImports, from cli_main and loadsExtensionFile);
|
|
27
|
+
* - the prompts ZTC itself puts up that its controllers would answer with a host effect, which never open: the
|
|
28
|
+
* workspace-overlap chooser (guardWorkspaceAdmission) and the missing-key chooser (askForSendCredential; the
|
|
29
|
+
* controllers get HOSTED_RESIDENT_NO_MODEL_ROUTE instead).
|
|
30
|
+
* And of its agent's own reach, only one family: the ZergAI platform's zergs. Its agent has none of the platform's zerg_*
|
|
31
|
+
* tools (hostedResidentAgentTools, in the agent factory: createAgentFromConfig's hostedResident), and its ztc_command
|
|
32
|
+
* runs none of the slash commands that reach the same platform API with the same login, nor /debug headlines, which
|
|
33
|
+
* launches a local zerg service as /zerg launch does (hostedResidentAgentCommandRefusal, in cli_ratatui
|
|
34
|
+
* executeCommandForTool).
|
|
35
|
+
*/
|
|
36
|
+
import { realpathSync } from 'node:fs';
|
|
37
|
+
// A namespace import: module.registerHooks is missing on a Node older than 22.15 (or 23.5), and a named import of it
|
|
38
|
+
// would fail to link there.
|
|
39
|
+
import * as nodeModule from 'node:module';
|
|
40
|
+
import { delimiter, isAbsolute, relative, sep } from 'node:path';
|
|
41
|
+
import { fileURLToPath } from 'node:url';
|
|
42
|
+
import { extensionPathsFromEnvironment } from '../extension_discovery.js';
|
|
43
|
+
import { createdPaneType } from '../ui/ratatui/pane_type.js';
|
|
44
|
+
import { runtimeDiag } from '../utils/debug_log.js';
|
|
45
|
+
/**
|
|
46
|
+
* The slash commands a hosted resident runs for its controllers. Each handler only reads this session's state or
|
|
47
|
+
* changes its conversation, and none starts a process, writes outside ZTC's session state, fetches a URL the
|
|
48
|
+
* controller names, prints a token, writes config or an account, restarts ZTC or changes the permission profile:
|
|
49
|
+
* - /clear empties the conversation, its todos and queue (clearMessages, clearTransientState);
|
|
50
|
+
* - /compact summarizes the conversation in place, with the route's own key or locally;
|
|
51
|
+
* - /context prints the context window's use;
|
|
52
|
+
* - /help lists the commands;
|
|
53
|
+
* - /status prints the route, readiness and execution facts, keys masked;
|
|
54
|
+
* - /usage prints retained usage records and Router's own limits for the route's sign-in.
|
|
55
|
+
*/
|
|
56
|
+
export const HOSTED_RESIDENT_CONTROLLER_COMMANDS = new Set([
|
|
57
|
+
'clear', 'compact', 'context', 'help', 'status', 'usage',
|
|
58
|
+
]);
|
|
59
|
+
const ALLOWED_LIST = '/clear, /compact, /context, /help, /status and /usage';
|
|
60
|
+
/**
|
|
61
|
+
* What a hosted resident says when a prompt finds its model route without a key. It never puts up the missing-key
|
|
62
|
+
* chooser, whose answers save a key (or the name of a variable to read one from) to config.json: its controllers would
|
|
63
|
+
* answer it.
|
|
64
|
+
*/
|
|
65
|
+
export const HOSTED_RESIDENT_NO_MODEL_ROUTE = 'This hosted resident has no model route ready. Its host supplies the route '
|
|
66
|
+
+ '(a Router grant, or a provider key in its environment); a key cannot be added from its mirror.';
|
|
67
|
+
/** Why a controller's /<cmd> does not run on a hosted resident, or null when it is one of the six above. */
|
|
68
|
+
export function hostedResidentCommandRefusal(cmd) {
|
|
69
|
+
if (HOSTED_RESIDENT_CONTROLLER_COMMANDS.has(cmd.toLowerCase()))
|
|
70
|
+
return null;
|
|
71
|
+
return `/${cmd} is not available from this mirror. Its controllers run only ${ALLOWED_LIST}; ask the agent for anything else.`;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The command context a hosted resident's commands run with: the one the app builds, less what its controllers'
|
|
75
|
+
* commands could otherwise do on the host. It pins no command pane (/job pins commands that run on the host), opens
|
|
76
|
+
* no environment, and its shell (/shell, ! and /cd) is the stub, which runs nothing.
|
|
77
|
+
*/
|
|
78
|
+
export function hostedResidentCommandContext(context, stubShell) {
|
|
79
|
+
const { commandPanes: _hostCommands, ...resident } = context;
|
|
80
|
+
return { ...resident, environmentAvailable: () => false, shell: stubShell };
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* The agent tools a hosted resident's agent goes without: the ZergAI platform's zerg_* tools. zerg_list, zerg_query and
|
|
84
|
+
* zerg_task_status reach the platform API (configStore.getApiBaseUrl) with ZTC's platform login: under the image's
|
|
85
|
+
* default tier that API is http://localhost:8000, where any tool child can listen, and a hosted resident's host gives it
|
|
86
|
+
* no platform login (its route is a Router grant), while the one an earlier entrypoint derived from that grant went
|
|
87
|
+
* there with zerg_list. zerg_watch reads the Mini-ZDE connection /zerg sets up. A ZeP service zerg's agent works in its
|
|
88
|
+
* workspace and lists, queries and drives no platform zergs, so its tool set leaves out every tool named zerg_*, the
|
|
89
|
+
* platform's four and any a plugin or MCP server would add under that name; the agent keeps every other tool.
|
|
90
|
+
*/
|
|
91
|
+
export const HOSTED_RESIDENT_WITHHELD_TOOL_PREFIX = 'zerg_';
|
|
92
|
+
/** `tools` less the ones a hosted resident's agent goes without (HOSTED_RESIDENT_WITHHELD_TOOL_PREFIX), in order. */
|
|
93
|
+
export function hostedResidentAgentTools(tools) {
|
|
94
|
+
return tools.filter(tool => !tool.definition.name.startsWith(HOSTED_RESIDENT_WITHHELD_TOOL_PREFIX));
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* The slash commands a hosted resident's agent does not run through ztc_command, whatever their arguments: the zerg_*
|
|
98
|
+
* tools' counterparts. /zerg lists, shows, creates, starts, stops and deletes the platform's zergs through the ZergAI
|
|
99
|
+
* platform API (configStore.getApiBaseUrl) with ZTC's platform login (apiRequest), and launches or connects the zerg
|
|
100
|
+
* service the rest of its family drives; /zergs is its list. One more reaches that API: /manifest load, which with no
|
|
101
|
+
* zerg connected lists the platform's pool for one with the same login (zergLoadManifestHandler), so it is refused too
|
|
102
|
+
* (hostedResidentAgentCommandRefusal). So is /debug headlines, whose smoke test switches ZTC to zerg mode and launches a
|
|
103
|
+
* local zerg service with /zerg launch's own code (zergLaunchLocal: `poetry --version`, then `poetry run zerg ...`, each
|
|
104
|
+
* with ZTC's environment). The rest of /manifest reads and writes manifests on the host, the rest of /debug toggles debug
|
|
105
|
+
* output, and like every other command they stay the agent's.
|
|
106
|
+
*/
|
|
107
|
+
export const HOSTED_RESIDENT_WITHHELD_AGENT_COMMANDS = new Set(['zerg', 'zergs']);
|
|
108
|
+
const REACHES_PLATFORM_ZERGS = "it reaches the ZergAI platform's zergs through the platform API with ZTC's platform login";
|
|
109
|
+
const LAUNCHES_LOCAL_ZERG = "it switches ZTC to zerg mode and launches a local zerg service as /zerg launch does, with ZTC's "
|
|
110
|
+
+ 'environment';
|
|
111
|
+
/**
|
|
112
|
+
* Why a hosted resident's agent does not run /`cmd` `args` through ztc_command, or null when it does: refused are /zerg
|
|
113
|
+
* and /zergs (HOSTED_RESIDENT_WITHHELD_AGENT_COMMANDS), /manifest load and /debug headlines, each named in any letter
|
|
114
|
+
* case and with any surrounding space (the app finds a command by its lower-cased name, and refusing more spellings than
|
|
115
|
+
* a handler takes costs nothing). The agent's slash commands are not its controllers', which hostedResidentCommandRefusal
|
|
116
|
+
* holds to six.
|
|
117
|
+
*/
|
|
118
|
+
export function hostedResidentAgentCommandRefusal(cmd, args) {
|
|
119
|
+
const name = cmd.trim().toLowerCase();
|
|
120
|
+
const sub = args[0]?.trim().toLowerCase();
|
|
121
|
+
const [refused, reason] = HOSTED_RESIDENT_WITHHELD_AGENT_COMMANDS.has(name) ? [`/${name}`, REACHES_PLATFORM_ZERGS]
|
|
122
|
+
: name === 'manifest' && sub === 'load' ? ['/manifest load', REACHES_PLATFORM_ZERGS]
|
|
123
|
+
: name === 'debug' && sub === 'headlines' ? ['/debug headlines', LAUNCHES_LOCAL_ZERG] : [null, null];
|
|
124
|
+
if (refused === null)
|
|
125
|
+
return null;
|
|
126
|
+
return `A hosted resident's agent does not run ${refused}: ${reason}, and this zerg's agent works in its own workspace.`;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* The most ZTC panes a hosted resident keeps open besides pane 0, whoever opens them. Each is a whole ZTC process that
|
|
130
|
+
* holds the launch's route credentials: about 140 MB resident apiece while idle, beside the resident's own 180 MB
|
|
131
|
+
* (measured on the real binary, 2026-11-20). A ZergCloud zerg's default machine has 1 GB (shared-cpu-2x), shared with
|
|
132
|
+
* the zerg service, so four panes and the resident (about 740 MB) fit and eight would not. Four still leaves room for
|
|
133
|
+
* the reviewer and fixer panes a pr-review-bot manifest opens. The agent's own shell and command panes stop at eight
|
|
134
|
+
* (pane.ts MAX_TERMINAL_PANES), and a hosted resident started --no-mux, as the runtime image starts one, has no panes.
|
|
135
|
+
*/
|
|
136
|
+
export const HOSTED_RESIDENT_MAX_ZTC_PANES = 4;
|
|
137
|
+
/** Mux commands that only arrange panes. A headless mirror refuses the TUI-only ones itself. */
|
|
138
|
+
const LAYOUT_COMMANDS = new Set([
|
|
139
|
+
'focus_pane', 'set_layout', 'toggle_zoom', 'scroll_pane', 'resize_pane', 'swap_pane', 'cycle_overflow',
|
|
140
|
+
]);
|
|
141
|
+
const OUTSIDE_WORKSPACE = 'A hosted resident opens ZTC panes only inside its workspace.';
|
|
142
|
+
/** A pane id as a controller must send one: a whole number, 0 or more. */
|
|
143
|
+
function isPaneId(value) {
|
|
144
|
+
return Number.isSafeInteger(value) && value >= 0;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* A ZTC pane's launch flags on a hosted resident: the launch's own provider and model, as given. Left to inference,
|
|
148
|
+
* naming only the model can pick another provider, and openai_compatible (no pane provider) would be dropped.
|
|
149
|
+
*/
|
|
150
|
+
function launchFlags(launch) {
|
|
151
|
+
return [...(launch.provider ? ['--provider', launch.provider] : []), ...(launch.model ? ['--model', launch.model] : [])];
|
|
152
|
+
}
|
|
153
|
+
/** Whether `path`, an absolute real path, is `workspace` or a path below it. A relative path is in no place. */
|
|
154
|
+
function insideWorkspace(workspace, path) {
|
|
155
|
+
if (!isAbsolute(path))
|
|
156
|
+
return false;
|
|
157
|
+
// The workspace itself is '' here, inside as any path below it is.
|
|
158
|
+
const below = relative(workspace, path);
|
|
159
|
+
return !isAbsolute(below) && below.split(sep)[0] !== '..';
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* The real directory a pane asked to open in `cwd` (the workspace when it names none) starts in, or null when that is
|
|
163
|
+
* not a directory inside the workspace. The path goes to realDirectory as written, with no '..' folded away first: the
|
|
164
|
+
* kernel reads 'link/..' as the parent of link's target, which may be outside the workspace though the text is not.
|
|
165
|
+
*/
|
|
166
|
+
function realPaneDirectory(cwd, launch, panes) {
|
|
167
|
+
if (cwd !== undefined && cwd !== null && typeof cwd !== 'string')
|
|
168
|
+
return null;
|
|
169
|
+
const asked = !cwd ? launch.workspace : isAbsolute(cwd) ? cwd : `${launch.workspace}${sep}${cwd}`;
|
|
170
|
+
const real = panes.realDirectory(asked);
|
|
171
|
+
return real !== null && insideWorkspace(launch.workspace, real) ? real : null;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Whether a hosted resident runs a controller's mux command, and for a ZTC pane where it opens. Default deny: only
|
|
175
|
+
* layout commands, typing into or closing a ZTC pane, and a ZTC pane inside the workspace on the launch's own provider
|
|
176
|
+
* and model (at most HOSTED_RESIDENT_MAX_ZTC_PANES of them) run. A command or shell pane, restarting a pane (it reruns
|
|
177
|
+
* the pane's launch), closing pane 0 or the agent's own shell, Codex or Claude Code pane, and every persistent setting
|
|
178
|
+
* are refused. A ZTC pane opens in the real directory the check approved (see realPaneDirectory), which the spawn must
|
|
179
|
+
* take as given; the pane then checks where the spawn's chdir left it (hostedResidentPaneStartRefusal).
|
|
180
|
+
*/
|
|
181
|
+
export function hostedResidentMuxDecision(command, launch, panes) {
|
|
182
|
+
if (LAYOUT_COMMANDS.has(command.type))
|
|
183
|
+
return { ok: true };
|
|
184
|
+
if (command.type === 'write_pane') {
|
|
185
|
+
const refusal = hostedResidentPaneInputRefusal(command.pane_id, panes.paneType(command.pane_id), panes.openedByResident(command.pane_id));
|
|
186
|
+
return refusal === null ? { ok: true } : { ok: false, error: refusal };
|
|
187
|
+
}
|
|
188
|
+
if (command.type === 'close_pane') {
|
|
189
|
+
// Checked here, since the route passes pane_id on as sent and closing an id no pane has still answers ok.
|
|
190
|
+
const paneId = command.pane_id;
|
|
191
|
+
if (!isPaneId(paneId))
|
|
192
|
+
return { ok: false, error: 'close_pane needs the id of an open pane: a whole number, 0 or more.' };
|
|
193
|
+
const paneType = panes.paneType(paneId);
|
|
194
|
+
if (paneType === undefined)
|
|
195
|
+
return { ok: false, error: `No pane ${paneId} is open.` };
|
|
196
|
+
// Closing is no typing: any of its ZTC panes but pane 0, whoever opened it.
|
|
197
|
+
return paneId !== 0 && paneType === 'ztc'
|
|
198
|
+
? { ok: true } : { ok: false, error: 'A hosted resident mirror closes only its ZTC panes.' };
|
|
199
|
+
}
|
|
200
|
+
if (command.type === 'create_ztc_pane') {
|
|
201
|
+
const cwd = realPaneDirectory(command.cwd, launch, panes);
|
|
202
|
+
if (cwd === null)
|
|
203
|
+
return { ok: false, error: OUTSIDE_WORKSPACE };
|
|
204
|
+
if ((command.provider && command.provider !== launch.provider) || (command.model && command.model !== launch.model)) {
|
|
205
|
+
return { ok: false, error: 'A hosted resident opens ZTC panes only on its own provider and model.' };
|
|
206
|
+
}
|
|
207
|
+
if (panes.ztcPaneCount() >= HOSTED_RESIDENT_MAX_ZTC_PANES) {
|
|
208
|
+
return { ok: false, error: `A hosted resident keeps at most ${HOSTED_RESIDENT_MAX_ZTC_PANES} ZTC panes open; close one before opening another.` };
|
|
209
|
+
}
|
|
210
|
+
return { ok: true, ztcPane: { cwd, args: launchFlags(launch) } };
|
|
211
|
+
}
|
|
212
|
+
return { ok: false, error: `The ${command.type} command is unavailable on a hosted resident mirror.` };
|
|
213
|
+
}
|
|
214
|
+
const TYPES_ONLY_INTO_ZTC_PANES = 'A hosted resident mirror types only into its ZTC panes.';
|
|
215
|
+
const TYPES_ONLY_INTO_ITS_OWN_PANES = 'A hosted resident mirror types only into the ZTC panes it opened itself, never one its agent opened.';
|
|
216
|
+
/**
|
|
217
|
+
* Why a hosted resident's mirror does not deliver input to a pane (a controller's write_pane, /pane-input, the bus
|
|
218
|
+
* fallback, /broadcast), or null when it does: only to one of its ZTC panes, never to pane 0 this way, and only to one
|
|
219
|
+
* the resident opened itself (`openedByResident`, see HostedResidentPanes), never one its agent opened with its own tools.
|
|
220
|
+
*/
|
|
221
|
+
export function hostedResidentPaneInputRefusal(paneId, paneType, openedByResident) {
|
|
222
|
+
if (paneId === 0 || paneType !== 'ztc')
|
|
223
|
+
return TYPES_ONLY_INTO_ZTC_PANES;
|
|
224
|
+
return openedByResident ? null : TYPES_ONLY_INTO_ITS_OWN_PANES;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Whether a hosted resident's mirror delivers input to a pane (hostedResidentPaneInputRefusal). A pane no caller says
|
|
228
|
+
* the resident opened is taken as one it did not.
|
|
229
|
+
*/
|
|
230
|
+
export function hostedResidentPaneInputAllowed(paneId, paneType, openedByResident = false) {
|
|
231
|
+
return hostedResidentPaneInputRefusal(paneId, paneType, openedByResident) === null;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Whether a hosted resident opens the ZTC pane its agent's tools asked for (a create_pane for a 'ztc' pane), held to
|
|
235
|
+
* the decision a controller's create_ztc_pane gets (hostedResidentMuxDecision): the pane's directory as the tool sent it,
|
|
236
|
+
* and every --provider and --model its arguments name, each the launch's own or refused. The tool's other arguments, its
|
|
237
|
+
* environment and its session are not the pane's: the resident starts it on its own launch (the decision's ztcPane), as
|
|
238
|
+
* it starts a controller's, and with no persona of the tool's (ZTC_SIM_PERSONA), which a controller's pane never has.
|
|
239
|
+
*/
|
|
240
|
+
export function hostedResidentAgentZtcPaneDecision(command, launch, panes) {
|
|
241
|
+
if (command.persona)
|
|
242
|
+
return { ok: false, error: "A hosted resident opens its agent's ZTC panes without a persona of their own." };
|
|
243
|
+
const args = command.args ?? [];
|
|
244
|
+
const named = (flag) => args.flatMap((arg, index) => (arg === flag && index + 1 < args.length ? [args[index + 1]] : []));
|
|
245
|
+
const other = (values, own) => values.find(value => value !== own) ?? (values.length ? own : undefined);
|
|
246
|
+
return hostedResidentMuxDecision({ type: 'create_ztc_pane', cwd: command.cwd,
|
|
247
|
+
provider: other(named('--provider'), launch.provider), model: other(named('--model'), launch.model) }, launch, panes);
|
|
248
|
+
}
|
|
249
|
+
/** Why a hosted resident opens no pane its agent asks for but a ZTC pane (hostedResidentAgentPaneTransport). */
|
|
250
|
+
export const HOSTED_RESIDENT_AGENT_PANES_ZTC_ONLY = "A hosted resident's agent opens only ZTC panes, as the resident's own: no shell, command or process pane, which would run outside its sandbox.";
|
|
251
|
+
/** Why a hosted resident relaunches no pane its agent asks it to (hostedResidentAgentPaneTransport). */
|
|
252
|
+
export const HOSTED_RESIDENT_AGENT_PANES_NO_RELAUNCH = "A hosted resident's agent relaunches no pane as another program (replace_pane), which would run outside its sandbox: it opens only ZTC panes, as the resident's own.";
|
|
253
|
+
/** Why a hosted resident's pane relay opens and relaunches no pane one of its ZTC panes asks for (hostedResidentRelayTransport). */
|
|
254
|
+
export const HOSTED_RESIDENT_RELAYED_PANES_REFUSED = "A hosted resident opens and relaunches no pane that one of its ZTC panes asks for through its pane relay: such a pane would start past the resident's own decision, and a shell, command or process pane outside its agent's sandbox.";
|
|
255
|
+
/**
|
|
256
|
+
* The pane transport a hosted resident gives its agent's pane tools in mux mode (setPaneToolsTuiProcess): `transport`,
|
|
257
|
+
* except for a create_pane. One for a ZTC pane, as the pane manager types it, named or not (create_ztc_pane,
|
|
258
|
+
* create_terminal_pane's ZTC launch, a supervision or simulation pane: every tool that opens one goes through here), is
|
|
259
|
+
* replaced by what `openZtcPane` makes of it, the pane the resident opens as it opens a controller's
|
|
260
|
+
* (hostedResidentAgentZtcPaneDecision, the resident's launch and pane environment). Any other (a shell, a command pane
|
|
261
|
+
* such as /codex's or /claude's, any process: a ZTC started without --pane or from a shell among them) is refused, since
|
|
262
|
+
* the pane manager runs it outside the agent's sandbox, as the resident's user, with network, until those panes run in
|
|
263
|
+
* that sandbox (ztc-11-2's process panes). A replace_pane, which relaunches a pane as the program it names, is refused
|
|
264
|
+
* the same way, whatever type it names: the agent's tools send none, and the pane manager would run it as it runs a
|
|
265
|
+
* create_pane's (ztc (2)'s F4). Refused, nothing is sent, and the tool hears why as the pane manager says an error, with
|
|
266
|
+
* its request id. Every other command, and every event, passes as it is.
|
|
267
|
+
*/
|
|
268
|
+
export function hostedResidentAgentPaneTransport(transport, openZtcPane) {
|
|
269
|
+
const listeners = new Set();
|
|
270
|
+
const refuse = (command, message) => {
|
|
271
|
+
for (const listener of [...listeners])
|
|
272
|
+
listener({ type: 'error', message, request_id: command.request_id });
|
|
273
|
+
return true;
|
|
274
|
+
};
|
|
275
|
+
return {
|
|
276
|
+
get isMuxMode() { return transport.isMuxMode; },
|
|
277
|
+
isOnlyPendingPaneCreate: transport.isOnlyPendingPaneCreate?.bind(transport),
|
|
278
|
+
launcherParentPid: transport.launcherParentPid?.bind(transport),
|
|
279
|
+
hasCapability: transport.hasCapability?.bind(transport),
|
|
280
|
+
on(handler) {
|
|
281
|
+
listeners.add(handler);
|
|
282
|
+
const off = transport.on(handler);
|
|
283
|
+
return () => { listeners.delete(handler); off(); };
|
|
284
|
+
},
|
|
285
|
+
send(command) {
|
|
286
|
+
if (command.type === 'replace_pane')
|
|
287
|
+
return refuse(command, HOSTED_RESIDENT_AGENT_PANES_NO_RELAUNCH);
|
|
288
|
+
if (command.type !== 'create_pane')
|
|
289
|
+
return transport.send(command);
|
|
290
|
+
// A ZTC pane as the pane manager types one, named or not (createdPaneType); no other pane at all.
|
|
291
|
+
if (createdPaneType(command) !== 'ztc')
|
|
292
|
+
return refuse(command, HOSTED_RESIDENT_AGENT_PANES_ZTC_ONLY);
|
|
293
|
+
const opened = openZtcPane(command);
|
|
294
|
+
return opened.ok ? transport.send(opened.command) : refuse(command, opened.error);
|
|
295
|
+
},
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* What a hosted resident's pane relay refuses (hostedResidentRelayTransport): every command that opens or relaunches a
|
|
300
|
+
* pane, a doc or feed pane's among them, which start no program but are panes all the same.
|
|
301
|
+
*/
|
|
302
|
+
const RELAYED_PANE_OPENINGS = new Set([
|
|
303
|
+
'create_pane', 'replace_pane', 'restart_pane', 'create_doc_pane', 'create_feed_pane'
|
|
304
|
+
]);
|
|
305
|
+
/**
|
|
306
|
+
* The pane transport a hosted resident's pane relay forwards what its ZTC panes send to, in mux mode: `transport` (the
|
|
307
|
+
* pane manager), except that it opens and relaunches no pane a ZTC pane asks for. A relayed create_pane or replace_pane,
|
|
308
|
+
* whatever its type, launch nonce or arguments, would start a pane past the decision the resident holds its agent's
|
|
309
|
+
* panes to (hostedResidentAgentPaneTransport): a shell, command or process pane outside the agent's sandbox, as the
|
|
310
|
+
* resident's user, with network, or a ZTC pane past its cap and without its pane environment (ztc (2)'s F4). A relayed
|
|
311
|
+
* create_doc_pane or create_feed_pane runs no program, but would open a pane past that decision too, and one the
|
|
312
|
+
* resident's controllers could not close (hostedResidentMuxDecision closes only its ZTC panes); a relayed restart_pane
|
|
313
|
+
* would relaunch a pane as it last ran, which the resident refuses its controllers as well. Refused, nothing is sent,
|
|
314
|
+
* and whoever listens (the relay, which tells its panes) hears why as the pane manager says an error, with the request
|
|
315
|
+
* id (a doc or feed pane's command and a restart name none, so their refusals name none). Every other command, and
|
|
316
|
+
* every event, passes as it is. The resident's relay is built over this only once landing step 3's --m3z-fixes wiring
|
|
317
|
+
* is in: F4, ztc (2)'s resolution of that merge (FOR-ZTC-DRIFT.md, pass 7c part 2b), builds cli_ratatui's
|
|
318
|
+
* PaneRelayServer over it on `this.hostedResident`. Until then (hub/m3-zergcloud alone) a mux hosted resident's relay
|
|
319
|
+
* forwards to the pane manager itself; with --no-mux (the go-live shape, which the entrypoint holds every hosted resident
|
|
320
|
+
* to) it starts no relay at all.
|
|
321
|
+
*/
|
|
322
|
+
export function hostedResidentRelayTransport(transport) {
|
|
323
|
+
const listeners = new Set();
|
|
324
|
+
return {
|
|
325
|
+
get isMuxMode() { return transport.isMuxMode; },
|
|
326
|
+
isOnlyPendingPaneCreate: transport.isOnlyPendingPaneCreate?.bind(transport),
|
|
327
|
+
launcherParentPid: transport.launcherParentPid?.bind(transport),
|
|
328
|
+
hasCapability: transport.hasCapability?.bind(transport),
|
|
329
|
+
on(handler) {
|
|
330
|
+
listeners.add(handler);
|
|
331
|
+
const off = transport.on(handler);
|
|
332
|
+
return () => { listeners.delete(handler); off(); };
|
|
333
|
+
},
|
|
334
|
+
send(command) {
|
|
335
|
+
if (!RELAYED_PANE_OPENINGS.has(command.type))
|
|
336
|
+
return transport.send(command);
|
|
337
|
+
const requestId = command.request_id;
|
|
338
|
+
for (const listener of [...listeners]) {
|
|
339
|
+
listener({ type: 'error', message: HOSTED_RESIDENT_RELAYED_PANES_REFUSED, request_id: requestId });
|
|
340
|
+
}
|
|
341
|
+
return true;
|
|
342
|
+
},
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
/** Whether a hosted resident types a bus message into a Claude Code or Codex pane: only one its own agent sends. */
|
|
346
|
+
export function hostedResidentAgentPaneMessageAllowed(fromMemberId) {
|
|
347
|
+
return fromMemberId === 0;
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Whether the mirror serves its debug log viewer (GET /debug/logs). Outside a hosted resident it follows the dev bar.
|
|
351
|
+
* A hosted resident's ring buffer holds what its controllers must not read back, so only ZTC_MIRROR_DEV_BAR=1 turns it
|
|
352
|
+
* on there.
|
|
353
|
+
*/
|
|
354
|
+
export function servesDebugLogs(options, env = process.env) {
|
|
355
|
+
if (!options.hostedResident)
|
|
356
|
+
return options.devSystemBarEnabled;
|
|
357
|
+
return devBarAskedFor(env);
|
|
358
|
+
}
|
|
359
|
+
/** Whether a hosted resident's host asked for its development surfaces: ZTC_MIRROR_DEV_BAR=1, nothing else. */
|
|
360
|
+
function devBarAskedFor(env) {
|
|
361
|
+
return env.ZTC_MIRROR_DEV_BAR?.trim() === '1';
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* The development and colony routes: a test doc pane, the bus's members and queues, and the log panes a parent colony
|
|
365
|
+
* view posts. On a hosted resident one controller could use them to rewrite what the others see (inject output as
|
|
366
|
+
* pane 0's, or delete or retype pane 0, which nulls /status primary_session), so they follow the debug log viewer.
|
|
367
|
+
*/
|
|
368
|
+
const DEVELOPMENT_ROUTES = new Set([
|
|
369
|
+
'/debug/test-doc-pane', '/bus-debug', '/api/colony/log-pane', '/api/colony/log-output', '/api/colony/log-pane-status',
|
|
370
|
+
]);
|
|
371
|
+
/** Routes whose writes change ZTC's own saved files: the mirror's layout (~/.ztc) and its notification setting (config.json). */
|
|
372
|
+
const SAVED_SETTING_ROUTES = new Set(['/mirror/layout', '/mirror/sfx']);
|
|
373
|
+
/**
|
|
374
|
+
* Voice dictation's routes: each starts (or reports) a ztc-audio binary, found first in <cwd>/bin or ~/.ztc/bin, both
|
|
375
|
+
* the agent's to write, and run with ZTC's own environment.
|
|
376
|
+
*/
|
|
377
|
+
const VOICE_ROUTES = new Set(['/voice/status', '/voice/transcribe', '/voice/stream']);
|
|
378
|
+
/** Methods that only read. */
|
|
379
|
+
const READ_METHODS = new Set(['GET', 'HEAD', 'OPTIONS']);
|
|
380
|
+
/**
|
|
381
|
+
* The project file routes a mirror view plugin may serve, as ztc-web-client does: a file's content (read through
|
|
382
|
+
* workspace links), the project's tree, and under /companion the companion pane's artifacts.
|
|
383
|
+
*/
|
|
384
|
+
const PROJECT_FILE_ROUTES = new Set(['/file-content', '/project-tree', '/companion']);
|
|
385
|
+
/** Whether `pathname` is one of the project file routes or below /companion. */
|
|
386
|
+
function projectFileRoute(pathname) {
|
|
387
|
+
return PROJECT_FILE_ROUTES.has(pathname) || pathname.startsWith('/companion/');
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* Why a hosted resident's mirror refuses a controller this route (403, or 403 to the /voice/stream upgrade), or null
|
|
391
|
+
* when the mirror serves it as it serves any controller:
|
|
392
|
+
* - the voice routes, outright;
|
|
393
|
+
* - /browse-dir, outright: it lists directories anywhere on the host;
|
|
394
|
+
* - the project file routes a view plugin may serve (/file-content, /project-tree, /companion/*), outright: ZeP ships
|
|
395
|
+
* no such plugin, and they read the host's files;
|
|
396
|
+
* - a write to /mirror/layout or /mirror/sfx, which save ZTC's own files (reading the saved layout stays);
|
|
397
|
+
* - the development and colony routes, unless ZTC_MIRROR_DEV_BAR=1 asks for them.
|
|
398
|
+
* A desktop or other mirror keeps them all: only a hosted resident applies this.
|
|
399
|
+
*/
|
|
400
|
+
export function hostedResidentRouteRefusal(method, pathname, env = process.env) {
|
|
401
|
+
if (VOICE_ROUTES.has(pathname))
|
|
402
|
+
return 'Voice dictation is not available on a hosted resident mirror.';
|
|
403
|
+
if (pathname === '/browse-dir')
|
|
404
|
+
return "A hosted resident mirror does not browse its host's directories.";
|
|
405
|
+
if (projectFileRoute(pathname))
|
|
406
|
+
return "A hosted resident mirror serves none of its host's files.";
|
|
407
|
+
if (SAVED_SETTING_ROUTES.has(pathname) && !READ_METHODS.has(method.toUpperCase())) {
|
|
408
|
+
return 'A hosted resident mirror keeps its saved layout and notification setting as they are.';
|
|
409
|
+
}
|
|
410
|
+
if (DEVELOPMENT_ROUTES.has(pathname) && !devBarAskedFor(env))
|
|
411
|
+
return `${pathname} is off on a hosted resident mirror unless ZTC_MIRROR_DEV_BAR=1.`;
|
|
412
|
+
return null;
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Whether a hosted resident takes a zlicky capture upload: only for a capture its agent's zlicky_look asked the live
|
|
416
|
+
* view for (requestZlickyCapture issued the id and still waits on it), and only one upload for it. Any other upload
|
|
417
|
+
* would be a controller writing its own bytes into TMPDIR. `capture` is the mirror's record of the id: how many
|
|
418
|
+
* requests wait on it, and whether an upload for it is already being received; undefined when it issued none.
|
|
419
|
+
*/
|
|
420
|
+
export function hostedResidentTakesCapture(capture) {
|
|
421
|
+
return capture !== undefined && capture.waiting > 0 && !capture.receiving;
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* Whether a hosted resident answers GET /zlicky/capture/<id> (where on the host a capture's file is kept, and its size)
|
|
425
|
+
* to `caller`, who the request came as (MirrorAuthDecision.source): only to ZTC itself, by its internal token, and to a
|
|
426
|
+
* pane's own credential, never to a controller's browser session (nor a caller an open mirror let in). Its agent gets
|
|
427
|
+
* the capture from requestZlickyCapture, in process.
|
|
428
|
+
*/
|
|
429
|
+
export function hostedResidentServesCaptureRead(caller) {
|
|
430
|
+
return caller === 'internal' || caller === 'pane';
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* What a hosted resident's mirror publishes to its controllers, and keeps for replay, of a tool's result (`result`, as
|
|
434
|
+
* `tool` returned it): the result as it is, except a zlicky_look result (a JSON object), without its png_path: where on
|
|
435
|
+
* the host the capture's file is kept. Its agent keeps the whole result, to read the file. With GET /zlicky/capture/<id>
|
|
436
|
+
* refused to them too (hostedResidentServesCaptureRead), and the agent's evidence ledger held to the same
|
|
437
|
+
* (hostedResidentPublishedEvidence), the mirror itself names no capture's file to a controller.
|
|
438
|
+
*/
|
|
439
|
+
export function hostedResidentPublishedToolResult(tool, result) {
|
|
440
|
+
if (tool !== 'zlicky_look')
|
|
441
|
+
return result;
|
|
442
|
+
try {
|
|
443
|
+
const parsed = JSON.parse(result);
|
|
444
|
+
// Of what JSON.parse answers, only an object has an own png_path.
|
|
445
|
+
if (Object.hasOwn(Object(parsed), 'png_path')) {
|
|
446
|
+
const { png_path: _where, ...published } = parsed;
|
|
447
|
+
return JSON.stringify(published);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
catch {
|
|
451
|
+
// No JSON: no file named in it to leave out.
|
|
452
|
+
}
|
|
453
|
+
return result;
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* What a hosted resident's mirror publishes, and keeps for replay, of its agent's evidence ledger (`snapshot`): each
|
|
457
|
+
* tool observation's excerpt (that tool's result, as the ledger keeps it) held to hostedResidentPublishedToolResult, and
|
|
458
|
+
* the rest as it is. The agent's own ledger is not changed.
|
|
459
|
+
*/
|
|
460
|
+
export function hostedResidentPublishedEvidence(snapshot) {
|
|
461
|
+
return {
|
|
462
|
+
...snapshot,
|
|
463
|
+
records: snapshot.records.map(record => record.kind === 'observation'
|
|
464
|
+
? { ...record, excerpt: hostedResidentPublishedToolResult(record.source.toolName, record.excerpt) } : record),
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
/**
|
|
468
|
+
* The variable a hosted resident names its workspace in (the real path it resolved) for each ZTC pane it opens: the pane
|
|
469
|
+
* starts only inside it (hostedResidentPaneStartRefusal).
|
|
470
|
+
*/
|
|
471
|
+
export const HOSTED_RESIDENT_WORKSPACE_ENV = 'ZTC_RESIDENT_WORKSPACE';
|
|
472
|
+
/**
|
|
473
|
+
* What a hosted resident adds to the environment of a ZTC pane it opens: that pane runs no slash commands either, and
|
|
474
|
+
* with explicit discovery loads only the `extensionRoots` its launch named, as the resident resolved them at launch
|
|
475
|
+
* (hostedResidentExtensionRoots, one value for each *_PATHS variable), never a plugin found from its cwd, the
|
|
476
|
+
* repository or HOME, nor a root relative to its cwd, whatever the launcher exported. Otherwise a file the agent may
|
|
477
|
+
* write (a .ztc/plugin.yaml and its module) would run as code once a controller opened a pane, past a profile that
|
|
478
|
+
* grants the agent no shell. It also names the resident's real `workspace`, which the pane checks its own directory
|
|
479
|
+
* against as it starts. No root replaces those three settings.
|
|
480
|
+
*/
|
|
481
|
+
export function hostedResidentPaneEnv(workspace, extensionRoots = {}) {
|
|
482
|
+
return {
|
|
483
|
+
...extensionRoots,
|
|
484
|
+
ZTC_DISABLE_SLASH_COMMANDS: '1', ZTC_EXTENSION_DISCOVERY: 'explicit', [HOSTED_RESIDENT_WORKSPACE_ENV]: workspace,
|
|
485
|
+
};
|
|
486
|
+
}
|
|
487
|
+
/** The variables whose roots explicit extension discovery honors, one per loader (extension_discovery.ts's callers). */
|
|
488
|
+
export const HOSTED_RESIDENT_EXTENSION_PATH_VARIABLES = Object.freeze([
|
|
489
|
+
'ZTC_PLUGIN_PATHS', 'ZTC_MODE_PATHS', 'ZTC_DOMAIN_PATHS', 'ZTC_BENCH_PATHS', 'ZTC_EMBEDDED_PATHS', 'ZTC_SKILL_PATHS',
|
|
490
|
+
'ZTC_WORKFLOW_PATHS',
|
|
491
|
+
]);
|
|
492
|
+
/** Whether `path` (absolute) is the workspace, a path in it, or one that holds it. */
|
|
493
|
+
function sharesWorkspace(workspace, path) {
|
|
494
|
+
return insideWorkspace(workspace, path) || insideWorkspace(path, workspace);
|
|
495
|
+
}
|
|
496
|
+
/**
|
|
497
|
+
* The extension roots a hosted resident's ZTC panes load from, as each pane's environment carries them (with explicit
|
|
498
|
+
* discovery, hostedResidentPaneEnv). For each variable: the roots the launch named, resolved once, at launch, as the
|
|
499
|
+
* resident's own loaders resolved them (a relative root against the launch's own directory, never against a pane's,
|
|
500
|
+
* which a controller chose), each to the real directory it was then. A root passes on only when it is a directory then,
|
|
501
|
+
* and neither as named nor as resolved the workspace, a directory in it, or one that holds it (whose scan can reach the
|
|
502
|
+
* workspace): a pane opened later would load what the agent wrote there since, as code, past a profile that grants the
|
|
503
|
+
* agent no shell. Every variable is set, empty when no root is left, so no pane inherits the launch's own value; and
|
|
504
|
+
* when the launch's discovery setting is not one ZTC reads, every one is empty.
|
|
505
|
+
*/
|
|
506
|
+
export function hostedResidentExtensionRoots(env, workspace, realDirectory) {
|
|
507
|
+
return Object.fromEntries(HOSTED_RESIDENT_EXTENSION_PATH_VARIABLES.map(variable => [variable, passedRoots(variable, env, workspace, realDirectory).join(delimiter)]));
|
|
508
|
+
}
|
|
509
|
+
/** The real directories of `variable`'s roots that a hosted resident's ZTC pane may load from (see above). */
|
|
510
|
+
function passedRoots(variable, env, workspace, realDirectory) {
|
|
511
|
+
let named;
|
|
512
|
+
try {
|
|
513
|
+
named = extensionPathsFromEnvironment(variable, env, workspace);
|
|
514
|
+
}
|
|
515
|
+
catch {
|
|
516
|
+
// A discovery setting ZTC does not read: the resident's own loaders loaded nothing either.
|
|
517
|
+
return [];
|
|
518
|
+
}
|
|
519
|
+
const kept = [];
|
|
520
|
+
for (const root of named) {
|
|
521
|
+
const real = realDirectory(root);
|
|
522
|
+
if (real === null || sharesWorkspace(workspace, root) || sharesWorkspace(workspace, real) || kept.includes(real))
|
|
523
|
+
continue;
|
|
524
|
+
kept.push(real);
|
|
525
|
+
}
|
|
526
|
+
return kept;
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* Why a ZTC that a hosted resident started as one of its panes does not load this extension file, or null when it does:
|
|
530
|
+
* a manifest, include, template directory or module one of its seven loaders (plugins, modes, domains, bench, embedded
|
|
531
|
+
* systems, skills, workflows) found, under a root its launch named (hostedResidentExtensionRoots), a builtin one, or one
|
|
532
|
+
* a plugin added. Each root is outside the workspace, but a link in one (its .ztc, say, or a module a manifest names)
|
|
533
|
+
* may lead into it, where the agent writes, and the loaders follow it: so the pane loads no file whose real path
|
|
534
|
+
* (`realPath`, links followed) is the workspace its environment names or below it, nor one that does not resolve. A ZTC
|
|
535
|
+
* whose environment names no workspace is not such a pane and loads as it always has, resolving nothing; one that names
|
|
536
|
+
* no absolute workspace loads nothing (it does not start either: hostedResidentPaneStartRefusal).
|
|
537
|
+
*/
|
|
538
|
+
export function hostedResidentExtensionFileRefusal(env, path, realPath) {
|
|
539
|
+
const workspace = env[HOSTED_RESIDENT_WORKSPACE_ENV];
|
|
540
|
+
if (workspace === undefined)
|
|
541
|
+
return null;
|
|
542
|
+
if (!isAbsolute(workspace)) {
|
|
543
|
+
return `${HOSTED_RESIDENT_WORKSPACE_ENV} names no absolute workspace (${JSON.stringify(workspace)}), so this hosted resident's ZTC pane loads no extension.`;
|
|
544
|
+
}
|
|
545
|
+
let real;
|
|
546
|
+
try {
|
|
547
|
+
real = realPath(path);
|
|
548
|
+
}
|
|
549
|
+
catch {
|
|
550
|
+
return `A hosted resident's ZTC pane loads no extension it cannot resolve: ${path}.`;
|
|
551
|
+
}
|
|
552
|
+
return insideWorkspace(workspace, real) ? `A hosted resident's ZTC pane loads no extension from its workspace (${workspace}): ${path} is ${real}.` : null;
|
|
553
|
+
}
|
|
554
|
+
/**
|
|
555
|
+
* Why a hosted resident's ZTC pane does not run the module at `url` (an import or a require Node resolved, from whatever
|
|
556
|
+
* module: an extension's own, or one that module imports in turn), or null when it does. An extension's module is
|
|
557
|
+
* outside the workspace, but a link beside it (its lib/ or node_modules, say) may lead into the workspace, where the
|
|
558
|
+
* agent writes, and Node follows it: so a file is held to hostedResidentExtensionFileRefusal, by where it really is
|
|
559
|
+
* (`realPath`). Anything but a file (a builtin, a data: URL) runs, and so does every module of a ZTC whose environment
|
|
560
|
+
* names no workspace, resolving nothing.
|
|
561
|
+
*/
|
|
562
|
+
export function hostedResidentModuleRefusal(env, url, realPath) {
|
|
563
|
+
return url.startsWith('file:') ? hostedResidentExtensionFileRefusal(env, fileURLToPath(url), realPath) : null;
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* The hook a hosted resident's ZTC pane holds every module to, import or require: Node resolves each before it loads
|
|
567
|
+
* it, so a resolution to a module hostedResidentModuleRefusal refuses (on `env`, read each time) throws why, which
|
|
568
|
+
* runtimeDiag says too (the Logs ring, and stderr where no interactive renderer owns the screen), and the module is
|
|
569
|
+
* never loaded; any other goes on as the next hook resolved it. It holds a require too where
|
|
570
|
+
* a loader of TypeScript (tsx, say) loads CommonJS itself. There is no load hook: on Node 22 a synchronous load hook
|
|
571
|
+
* breaks every module such a loader's asynchronous hooks load (the default load hands back no source), and a resolution
|
|
572
|
+
* already names the file as the kernel resolves it.
|
|
573
|
+
*/
|
|
574
|
+
export function hostedResidentImportHooks(env, realPath) {
|
|
575
|
+
return {
|
|
576
|
+
resolve(specifier, context, nextResolve) {
|
|
577
|
+
const resolved = nextResolve(specifier, context);
|
|
578
|
+
const refusal = hostedResidentModuleRefusal(env, resolved.url, realPath);
|
|
579
|
+
if (refusal === null)
|
|
580
|
+
return resolved;
|
|
581
|
+
runtimeDiag('warn', `[ztc] ${refusal}`);
|
|
582
|
+
throw new Error(refusal);
|
|
583
|
+
},
|
|
584
|
+
};
|
|
585
|
+
}
|
|
586
|
+
/** This Node's module.registerHooks, or null on one without it. */
|
|
587
|
+
const NODE_REGISTER_HOOKS = nodeModule.registerHooks ?? null;
|
|
588
|
+
/** The registerHooks functions holdResidentPaneImports has registered this process's hooks with. */
|
|
589
|
+
const holding = new WeakSet();
|
|
590
|
+
/**
|
|
591
|
+
* Hold every module this process loads from now on to hostedResidentImportHooks, when its environment names a hosted
|
|
592
|
+
* resident's workspace (a ZTC pane): registered once per `register` (module.registerHooks, from Node 22.15 and 23.5),
|
|
593
|
+
* resolving as the kernel does. A pane does so as it starts (cli_main), and each of its extension loaders before it loads
|
|
594
|
+
* a file (loadsExtensionFile). Answers why it cannot (a Node without module.registerHooks: such a pane does not start,
|
|
595
|
+
* and loads no extension), or null. A ZTC whose environment names no workspace registers nothing.
|
|
596
|
+
*/
|
|
597
|
+
export function holdResidentPaneImports(env = process.env, register = NODE_REGISTER_HOOKS) {
|
|
598
|
+
if (env[HOSTED_RESIDENT_WORKSPACE_ENV] === undefined)
|
|
599
|
+
return null;
|
|
600
|
+
if (register === null) {
|
|
601
|
+
return "A hosted resident's ZTC pane holds every module it loads to its workspace with Node's module.registerHooks (Node 22.15 "
|
|
602
|
+
+ `or later), which this Node (${process.version}) does not have.`;
|
|
603
|
+
}
|
|
604
|
+
if (!holding.has(register)) {
|
|
605
|
+
register(hostedResidentImportHooks(env, path => realpathSync.native(path)));
|
|
606
|
+
holding.add(register);
|
|
607
|
+
}
|
|
608
|
+
return null;
|
|
609
|
+
}
|
|
610
|
+
/**
|
|
611
|
+
* Whether this ZTC loads `path`, a file one of its extension loaders found (each asks just before it reads or imports
|
|
612
|
+
* one): hostedResidentExtensionFileRefusal on this process's environment and the real filesystem, saying why it does
|
|
613
|
+
* not through runtimeDiag (the Logs ring, and stderr where no interactive renderer owns the screen: headless, or a
|
|
614
|
+
* stdout that is not a TTY). It resolves the file as the kernel does (realpathSync.native, as the resident resolved its
|
|
615
|
+
* workspace): fs.realpathSync keeps each component as the path spells it, so on a filesystem that ignores letter case a
|
|
616
|
+
* link naming the workspace in other case would not read as inside it. In a pane it first makes sure the process holds
|
|
617
|
+
* every module it loads to the same (holdResidentPaneImports), so what an approved module imports is held too, and loads
|
|
618
|
+
* nothing on a Node that cannot.
|
|
619
|
+
*/
|
|
620
|
+
export function loadsExtensionFile(path) {
|
|
621
|
+
const refusal = holdResidentPaneImports() ?? hostedResidentExtensionFileRefusal(process.env, path, realpathSync.native);
|
|
622
|
+
if (refusal !== null)
|
|
623
|
+
runtimeDiag('warn', `[ztc] ${refusal}`);
|
|
624
|
+
return refusal === null;
|
|
625
|
+
}
|
|
626
|
+
/**
|
|
627
|
+
* A hosted resident's ZTC pane's launch arguments, less the launch's own --domain-paths and its value: the pane would
|
|
628
|
+
* resolve them again against its own directory, and its environment carries those roots as the launch resolved them
|
|
629
|
+
* (hostedResidentExtensionRoots). What follows an end of options ('--') is the pane's task, kept as it is.
|
|
630
|
+
*/
|
|
631
|
+
export function hostedResidentPaneArgs(args) {
|
|
632
|
+
const kept = [];
|
|
633
|
+
for (let index = 0; index < args.length; index++) {
|
|
634
|
+
const arg = args[index];
|
|
635
|
+
if (arg === '--')
|
|
636
|
+
return [...kept, ...args.slice(index)];
|
|
637
|
+
if (arg !== '--domain-paths') {
|
|
638
|
+
kept.push(arg);
|
|
639
|
+
continue;
|
|
640
|
+
}
|
|
641
|
+
// Its value, as the launch's flag parser takes one: the next argument, when it is no flag.
|
|
642
|
+
if (args[index + 1] !== undefined && !args[index + 1].startsWith('-'))
|
|
643
|
+
index++;
|
|
644
|
+
}
|
|
645
|
+
return kept;
|
|
646
|
+
}
|
|
647
|
+
/**
|
|
648
|
+
* Why a ZTC that a hosted resident started as one of its panes does not start where it is, or null when it does. The
|
|
649
|
+
* resident checks a pane's directory before the spawn (hostedResidentMuxDecision), but the spawn's chdir resolves the
|
|
650
|
+
* path again, so a directory swapped for a link in between would start the pane wherever the link leads. So the pane
|
|
651
|
+
* checks where the kernel left it, first thing as it starts, before it reads config or loads a plugin: the path the
|
|
652
|
+
* kernel names for its working directory (`cwd`, a real path: process.cwd()) and that path resolved again (`realPath`)
|
|
653
|
+
* must both be inside the workspace its environment names. The first is where the pane is. The second re-reads the
|
|
654
|
+
* path, so a link swapped into it since can only add a refusal, never admit a directory outside. With either path
|
|
655
|
+
* outside or not absolute, its directory gone or unreadable, or no absolute workspace named, the pane does not start. A
|
|
656
|
+
* ZTC whose environment names no workspace is not such a pane, and starts as it always has; its directory is not read.
|
|
657
|
+
*/
|
|
658
|
+
export function hostedResidentPaneStartRefusal(env, cwd, realPath) {
|
|
659
|
+
const workspace = env[HOSTED_RESIDENT_WORKSPACE_ENV];
|
|
660
|
+
if (workspace === undefined)
|
|
661
|
+
return null;
|
|
662
|
+
if (!isAbsolute(workspace)) {
|
|
663
|
+
return `${HOSTED_RESIDENT_WORKSPACE_ENV} names no absolute workspace (${JSON.stringify(workspace)}), so this hosted resident's ZTC pane does not start.`;
|
|
664
|
+
}
|
|
665
|
+
const startsOnlyInside = `A hosted resident's ZTC pane starts only inside its workspace (${workspace}); this one's directory`;
|
|
666
|
+
let here;
|
|
667
|
+
let real;
|
|
668
|
+
try {
|
|
669
|
+
here = cwd();
|
|
670
|
+
real = realPath(here);
|
|
671
|
+
}
|
|
672
|
+
catch {
|
|
673
|
+
return `${startsOnlyInside} is gone or cannot be read.`;
|
|
674
|
+
}
|
|
675
|
+
const outside = [here, real].find(path => !insideWorkspace(workspace, path));
|
|
676
|
+
return outside === undefined ? null : `${startsOnlyInside} is ${outside}.`;
|
|
677
|
+
}
|