@zergai/ztc 0.23.0-beta.1 → 0.24.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 +299 -13
- package/bin/zmux-darwin-arm64 +0 -0
- package/bin/ztc-audio-darwin-arm64 +0 -0
- package/bin/ztc-capture-darwin-arm64 +0 -0
- package/bin/ztc-tui-darwin-arm64 +0 -0
- package/bin/zterm-darwin-arm64 +2 -2
- package/dist/agent/agent.js +57 -14
- package/dist/agent/backends/http_error.d.ts +2 -0
- package/dist/agent/backends/http_error.js +2 -0
- package/dist/agent/backends/router_gate.d.ts +3 -2
- package/dist/agent/backends/router_gate.js +5 -4
- package/dist/agent/backends/router_keys.d.ts +14 -0
- package/dist/agent/backends/router_keys.js +19 -0
- package/dist/agent/backends/router_refusal.d.ts +5 -3
- package/dist/agent/backends/router_refusal.js +28 -6
- package/dist/agent/backends/types.d.ts +1 -1
- package/dist/agent/backends/zrouter_stream.js +10 -6
- package/dist/agent/commands/agent_cli.d.ts +11 -1
- package/dist/agent/commands/agent_cli.js +295 -38
- package/dist/agent/commands/diff.js +4 -1
- package/dist/agent/commands/help.js +4 -1
- package/dist/agent/commands/plan.js +3 -1
- package/dist/agent/commands/session_redaction.d.ts +16 -0
- package/dist/agent/commands/session_redaction.js +77 -0
- package/dist/agent/commands/settings.js +144 -0
- package/dist/agent/commands/types.d.ts +5 -0
- package/dist/agent/commands/usage_spec.d.ts +4 -0
- package/dist/agent/commands/usage_spec.js +10 -4
- package/dist/agent/runtime/policy.d.ts +25 -0
- package/dist/agent/runtime/policy.js +154 -26
- package/dist/agent/runtime/source_verification.js +6 -2
- package/dist/agent/runtime/tool_runtime.js +81 -3
- package/dist/agent/tools/apply_patch.js +678 -131
- package/dist/agent/tools/bus_message.d.ts +26 -1
- package/dist/agent/tools/bus_message.js +126 -10
- package/dist/agent/tools/file.d.ts +16 -1
- package/dist/agent/tools/file.js +43 -5
- package/dist/agent/tools/index.d.ts +1 -1
- package/dist/agent/tools/index.js +1 -1
- package/dist/agent/tools/pane.js +5 -0
- package/dist/agent/tools/search.js +5 -1
- package/dist/agent/tools/supervision.js +16 -2
- package/dist/agent/tools/types.d.ts +22 -0
- package/dist/app/mirror_startup.d.ts +7 -5
- package/dist/app/mirror_startup.js +7 -5
- package/dist/app/navigator_mirrors.d.ts +20 -0
- package/dist/app/navigator_mirrors.js +36 -0
- package/dist/app/workspace_layout_controller.d.ts +11 -2
- package/dist/app/workspace_layout_controller.js +13 -2
- package/dist/batch/worktree.js +9 -1
- package/dist/cli_ratatui.d.ts +264 -12
- package/dist/cli_ratatui.js +1198 -103
- package/dist/config/types.d.ts +29 -0
- package/dist/desktop/daily/source_baseline.js +3 -1
- package/dist/diagnostics/session_export.d.ts +1 -0
- package/dist/diagnostics/session_export.js +229 -26
- package/dist/emulation/tool_adapters.js +42 -5
- package/dist/headless.js +24 -5
- package/dist/instance_registry.d.ts +7 -0
- package/dist/mcp/workspace_server.d.ts +12 -1
- package/dist/mcp/workspace_server.js +75 -8
- package/dist/panes/agent_cli.d.ts +60 -3
- package/dist/panes/agent_cli.js +169 -16
- package/dist/panes/agent_pane_sessions.d.ts +22 -0
- package/dist/panes/agent_pane_sessions.js +111 -0
- package/dist/panes/agent_panes.d.ts +77 -3
- package/dist/panes/agent_panes.js +199 -10
- package/dist/panes/agent_profiles.d.ts +5 -0
- package/dist/panes/agent_profiles.js +7 -0
- package/dist/panes/agent_resume.d.ts +134 -0
- package/dist/panes/agent_resume.js +155 -0
- package/dist/panes/agent_sessions/claude_head.d.ts +32 -0
- package/dist/panes/agent_sessions/claude_head.js +122 -0
- package/dist/panes/agent_sessions/claude_store.d.ts +59 -0
- package/dist/panes/agent_sessions/claude_store.js +223 -0
- package/dist/panes/agent_sessions/codex_head.d.ts +55 -0
- package/dist/panes/agent_sessions/codex_head.js +161 -0
- package/dist/panes/agent_sessions/codex_store.d.ts +43 -0
- package/dist/panes/agent_sessions/codex_store.js +187 -0
- package/dist/panes/agent_sessions/controller.d.ts +74 -0
- package/dist/panes/agent_sessions/controller.js +110 -0
- package/dist/panes/agent_sessions/cut_json.d.ts +14 -0
- package/dist/panes/agent_sessions/cut_json.js +109 -0
- package/dist/panes/agent_sessions/limits.d.ts +48 -0
- package/dist/panes/agent_sessions/limits.js +48 -0
- package/dist/panes/agent_sessions/scan.d.ts +49 -0
- package/dist/panes/agent_sessions/scan.js +86 -0
- package/dist/panes/agent_sessions/store_fs.d.ts +51 -0
- package/dist/panes/agent_sessions/store_fs.js +128 -0
- package/dist/panes/agent_sessions/stores.d.ts +36 -0
- package/dist/panes/agent_sessions/stores.js +89 -0
- package/dist/panes/agent_sessions/title.d.ts +9 -0
- package/dist/panes/agent_sessions/title.js +51 -0
- package/dist/panes/agent_sessions/types.d.ts +48 -0
- package/dist/panes/agent_sessions/types.js +2 -0
- package/dist/panes/reply_arbitrator.d.ts +39 -0
- package/dist/panes/reply_arbitrator.js +185 -0
- package/dist/panes/reply_modes.d.ts +291 -0
- package/dist/panes/reply_modes.js +598 -0
- package/dist/runtime_info.js +11 -2
- package/dist/session.js +4 -2
- package/dist/simulation/living/context.d.ts +8 -8
- package/dist/simulation/living/types.d.ts +5 -5
- package/dist/simulation/toolkit/dynamic_state.d.ts +1 -1
- package/dist/ui/ratatui/ipc_path.d.ts +5 -0
- package/dist/ui/ratatui/ipc_path.js +23 -3
- package/dist/ui/ratatui/pane_credential_broker.d.ts +23 -8
- package/dist/ui/ratatui/pane_credential_broker.js +49 -14
- package/dist/ui/ratatui/pane_relay.d.ts +5 -2
- package/dist/ui/ratatui/pane_relay.js +86 -19
- package/dist/ui/ratatui/process.js +3 -0
- package/dist/ui/ratatui/protocol.d.ts +50 -1
- package/dist/ui/ratatui/protocol.js +56 -0
- package/dist/ui/ratatui/relay_policy.d.ts +39 -0
- package/dist/ui/ratatui/relay_policy.js +94 -0
- package/dist/ui/ratatui/screen_privacy.d.ts +15 -0
- package/dist/ui/ratatui/screen_privacy.js +24 -0
- package/dist/ui/scrollback.js +8 -0
- package/dist/ui/widgets/agent_sessions_panel.d.ts +139 -0
- package/dist/ui/widgets/agent_sessions_panel.js +542 -0
- package/dist/ui/widgets/log_view.d.ts +5 -1
- package/dist/ui/widgets/log_view.js +40 -4
- package/dist/utils/atomic_write.js +9 -11
- package/dist/utils/canonical_path.d.ts +19 -0
- package/dist/utils/canonical_path.js +44 -0
- package/dist/utils/compaction.d.ts +2 -1
- package/dist/utils/compaction.js +3 -1
- package/dist/utils/git_directory_names.d.ts +51 -0
- package/dist/utils/git_directory_names.js +429 -0
- package/dist/utils/temporary_write_root.d.ts +6 -1
- package/dist/utils/temporary_write_root.js +32 -2
- package/dist/utils/tool_result_message.d.ts +4 -1
- package/dist/utils/tool_result_message.js +6 -2
- package/dist/web/bus_hold.d.ts +70 -0
- package/dist/web/bus_hold.js +118 -0
- package/dist/web/mirror_client.d.ts +7 -3
- package/dist/web/mirror_client.js +61 -23
- package/dist/web/mirror_route_gate.d.ts +40 -0
- package/dist/web/mirror_route_gate.js +71 -0
- package/dist/web/mirror_stream.d.ts +147 -22
- package/dist/web/mirror_stream.js +350 -83
- package/dist/web/pane_credentials.d.ts +32 -1
- package/dist/web/pane_credentials.js +62 -2
- package/dist/workspace_collision.js +11 -2
- package/dist/zlm-embed.mjs +940 -273
- package/package.json +34 -12
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { lstat, mkdir } from 'fs/promises';
|
|
2
|
-
import { parse, resolve, sep } from 'path';
|
|
2
|
+
import { parse, relative, resolve, sep } from 'path';
|
|
3
|
+
import { isWellFormedText } from './canonical_path.js';
|
|
4
|
+
import { gitDirectoryComponent, gitDirectoryPlantedByWriteSet, linkedGitDirectoryHolding } from './git_directory_names.js';
|
|
3
5
|
export function isPathWithinRoot(path, root) {
|
|
4
6
|
const normalizedPath = resolve(path);
|
|
5
7
|
const normalizedRoot = resolve(root);
|
|
@@ -19,12 +21,40 @@ export async function prepareTemporaryWriteRoot(cwd, root) {
|
|
|
19
21
|
});
|
|
20
22
|
return resolvedRoot;
|
|
21
23
|
}
|
|
22
|
-
export async function assertTemporaryWritePathAllowed(target, root, action
|
|
24
|
+
export async function assertTemporaryWritePathAllowed(target, root, action,
|
|
25
|
+
/** The working directory a relative path was resolved from: a work tree there may name its git directory. */
|
|
26
|
+
cwd,
|
|
27
|
+
/** Every path the whole call writes, deletes or moves (absolute, `target` among them); `target` alone when a
|
|
28
|
+
* caller writes one file. A set that completes a git directory across its paths is refused as one. */
|
|
29
|
+
writeSet) {
|
|
23
30
|
const resolvedRoot = resolve(root);
|
|
24
31
|
const resolvedTarget = resolve(target);
|
|
32
|
+
// Node writes a lone surrogate as U+FFFD, so such a path names a file other than the one its string spells.
|
|
33
|
+
if (!isWellFormedText(resolvedTarget)) {
|
|
34
|
+
throw new Error(`${action} denied: a path must be well-formed Unicode text (${JSON.stringify(resolvedTarget)})`);
|
|
35
|
+
}
|
|
25
36
|
if (!isPathWithinRoot(resolvedTarget, resolvedRoot)) {
|
|
26
37
|
throw new Error(`${action} denied: path is outside temporary write root (${resolvedRoot})`);
|
|
27
38
|
}
|
|
39
|
+
// An atomic write makes its temporary file beside its target: for the root
|
|
40
|
+
// itself, that is outside the root.
|
|
41
|
+
if (resolvedTarget === resolvedRoot) {
|
|
42
|
+
throw new Error(`${action} denied: a write cannot replace the temporary write root itself (${resolvedRoot})`);
|
|
43
|
+
}
|
|
44
|
+
// A setting in git's own directory is a command git runs as ZTC, outside the root. A .git file may name that
|
|
45
|
+
// directory elsewhere in the root, under any name.
|
|
46
|
+
const gitDirectory = gitDirectoryComponent(relative(resolvedRoot, resolvedTarget))
|
|
47
|
+
?? linkedGitDirectoryHolding(resolvedTarget, cwd === undefined ? [resolvedRoot] : [resolvedRoot, cwd]);
|
|
48
|
+
if (gitDirectory !== null) {
|
|
49
|
+
throw new Error(`${action} denied: path is inside a git directory (${gitDirectory}), which a confined write may not change (${resolvedTarget})`);
|
|
50
|
+
}
|
|
51
|
+
// And a write that would make a plain directory into one git takes for its own (a filter or core.fsmonitor in its
|
|
52
|
+
// config runs as ZTC): refused across the whole call's write set, as the policy does under a profile.
|
|
53
|
+
// The caller's array as given: the rule indexes it once per array, so each of a call's paths does not read it again.
|
|
54
|
+
const planted = gitDirectoryPlantedByWriteSet(resolvedTarget, writeSet ?? [resolvedTarget]);
|
|
55
|
+
if (planted !== null) {
|
|
56
|
+
throw new Error(`${action} denied: this write would make a git directory (${planted}), which a confined write may not create (${resolvedTarget})`);
|
|
57
|
+
}
|
|
28
58
|
await assertPathComponentsHaveNoSymlinks(resolvedRoot, {
|
|
29
59
|
allowMissingTail: false,
|
|
30
60
|
errorPrefix: `${action} denied: temporary write root`,
|
|
@@ -7,6 +7,9 @@ export declare function isToolResultMessage(message: MessageLike): boolean;
|
|
|
7
7
|
* typed raw JSON (2026-10-27); the person sees the job's own notice instead.
|
|
8
8
|
*/
|
|
9
9
|
export declare function isCommandObservationMessage(message: MessageLike): boolean;
|
|
10
|
-
/**
|
|
10
|
+
/**
|
|
11
|
+
* An auto-continue prompt, interrupted-turn marker or the note on an agent pane's message (agentPaneNote) the host gives
|
|
12
|
+
* the model; the person sees the host's own notice, or the message as it was sent.
|
|
13
|
+
*/
|
|
11
14
|
export declare function isHostPromptMessage(message: MessageLike): boolean;
|
|
12
15
|
export {};
|
|
@@ -35,7 +35,11 @@ export function isToolResultMessage(message) {
|
|
|
35
35
|
export function isCommandObservationMessage(message) {
|
|
36
36
|
return message.role === 'user' && message.metadata?.managedCommandObservation === true;
|
|
37
37
|
}
|
|
38
|
-
/**
|
|
38
|
+
/**
|
|
39
|
+
* An auto-continue prompt, interrupted-turn marker or the note on an agent pane's message (agentPaneNote) the host gives
|
|
40
|
+
* the model; the person sees the host's own notice, or the message as it was sent.
|
|
41
|
+
*/
|
|
39
42
|
export function isHostPromptMessage(message) {
|
|
40
|
-
return message.role === 'user' && (message.metadata?.autoContinue === true || message.metadata?.interruptedTurn === true
|
|
43
|
+
return message.role === 'user' && (message.metadata?.autoContinue === true || message.metadata?.interruptedTurn === true
|
|
44
|
+
|| message.metadata?.agentPaneNote === true);
|
|
41
45
|
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the mirror holds for a ZTC agent's pane that hasn't joined its message bus yet (workspace spec §4A, after W0).
|
|
3
|
+
*
|
|
4
|
+
* A ZTC pane joins the bus as its program starts: a worker an agent just opened (open_pane, create_ztc_pane,
|
|
5
|
+
* create_supervised_ztc_pane), a native ZTC pane of the desktop app, a colony cell. An agent's message for it before
|
|
6
|
+
* then, or while it rejoins, was typed into its terminal as though the person had typed it, or refused. Now the mirror
|
|
7
|
+
* holds it, and once the pane joins delivers each message it holds, in the order sent, as a message on the bus. It is
|
|
8
|
+
* never typed into the pane.
|
|
9
|
+
*
|
|
10
|
+
* Each pane holds at most HOLD_MAX_MESSAGES messages and HOLD_MAX_BYTES; a message that doesn't fit is refused, never
|
|
11
|
+
* held in place of an older one, whose sender was told it waits. A held message is dropped when its pane closes, when
|
|
12
|
+
* the pane hasn't joined HOLD_DEADLINE_MS after the message was sent, when the pane can no longer join (its program was
|
|
13
|
+
* started again in place without a credential that joins), when the renderer reports that the pane's program has exited,
|
|
14
|
+
* or when the mirror stops; the mirror tells a sender that is on the bus (undeliveredNotice). A message for a ZTC pane
|
|
15
|
+
* whose program has exited is never held: its sender is told so at once (paneExitedRefusal), until a ZTC that joins
|
|
16
|
+
* stands for the pane again. HOLD_DEADLINE_MS is 2 minutes: a ZTC pane joins as its program starts, before
|
|
17
|
+
* its first screen, within seconds even on a busy machine, so 2 minutes covers a slow start many times over (MotSi's
|
|
18
|
+
* wrapper, the desktop's launcher admission of 15 s, a colony cell's own renderer), while a sender waiting for an answer
|
|
19
|
+
* hears within a couple of minutes that none can come. It stays under the 5 minutes the bus queues a message for a member
|
|
20
|
+
* that left.
|
|
21
|
+
*
|
|
22
|
+
* Pure: no timers and no I/O; the mirror keeps the clock and delivers.
|
|
23
|
+
*/
|
|
24
|
+
import type { BusMessage } from './mirror_stream.js';
|
|
25
|
+
/** The messages a pane holds at most. */
|
|
26
|
+
export declare const HOLD_MAX_MESSAGES = 32;
|
|
27
|
+
/** The bytes a pane holds at most, counted as each message's JSON. */
|
|
28
|
+
export declare const HOLD_MAX_BYTES: number;
|
|
29
|
+
/** How long a held message waits for its pane to join. */
|
|
30
|
+
export declare const HOLD_DEADLINE_MS: number;
|
|
31
|
+
/**
|
|
32
|
+
* Why held messages were dropped: their pane closed, didn't join in time, can no longer join, its program exited, or the
|
|
33
|
+
* mirror stopped.
|
|
34
|
+
*/
|
|
35
|
+
export type HoldDropReason = 'closed' | 'expired' | 'unjoinable' | 'exited' | 'mirror_stopped';
|
|
36
|
+
/** A dropped message and the pane it waited for. */
|
|
37
|
+
export interface DroppedMessage {
|
|
38
|
+
paneId: number;
|
|
39
|
+
message: BusMessage;
|
|
40
|
+
}
|
|
41
|
+
/** What the mirror tells the sender of a message it holds for pane PANE_ID. */
|
|
42
|
+
export declare function heldNotice(paneId: number): string;
|
|
43
|
+
/**
|
|
44
|
+
* What the mirror tells the sender of a message for pane PANE_ID, a ZTC pane whose program has exited, at once: nothing
|
|
45
|
+
* reaches the pane until a ZTC is started in it again.
|
|
46
|
+
*/
|
|
47
|
+
export declare function paneExitedRefusal(paneId: number): string;
|
|
48
|
+
/** What the mirror tells the sender of a message that doesn't fit in what pane PANE_ID holds. */
|
|
49
|
+
export declare function holdFullRefusal(paneId: number): string;
|
|
50
|
+
/** What the mirror tells a sender on the bus of COUNT of its messages for pane PANE_ID it dropped, and why. */
|
|
51
|
+
export declare function undeliveredNotice(paneId: number, count: number, reason: HoldDropReason): string;
|
|
52
|
+
/** The messages held for each pane, oldest first. */
|
|
53
|
+
export declare class BusHold {
|
|
54
|
+
private readonly panes;
|
|
55
|
+
/** Hold MESSAGE, sent at NOW_MS, for pane PANE_ID, after what it holds already. False when it doesn't fit. */
|
|
56
|
+
hold(paneId: number, message: BusMessage, nowMs: number): boolean;
|
|
57
|
+
/**
|
|
58
|
+
* Hand what pane PANE_ID holds to DELIVER, oldest first. DELIVER returns false when it can take no more (the pane's
|
|
59
|
+
* stream ended as it joined): that message and those after it stay held.
|
|
60
|
+
*/
|
|
61
|
+
release(paneId: number, deliver: (message: BusMessage) => boolean): void;
|
|
62
|
+
/** Let go of what pane PANE_ID holds, and return it, oldest first. */
|
|
63
|
+
drop(paneId: number): DroppedMessage[];
|
|
64
|
+
/** Let go of everything held, and return it. */
|
|
65
|
+
dropAll(): DroppedMessage[];
|
|
66
|
+
/** Let go of each message whose pane hasn't joined by its deadline, at NOW_MS, and return them. */
|
|
67
|
+
expire(nowMs: number): DroppedMessage[];
|
|
68
|
+
/** When the next held message comes due, or null when nothing is held. */
|
|
69
|
+
nextDeadline(): number | null;
|
|
70
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the mirror holds for a ZTC agent's pane that hasn't joined its message bus yet (workspace spec §4A, after W0).
|
|
3
|
+
*
|
|
4
|
+
* A ZTC pane joins the bus as its program starts: a worker an agent just opened (open_pane, create_ztc_pane,
|
|
5
|
+
* create_supervised_ztc_pane), a native ZTC pane of the desktop app, a colony cell. An agent's message for it before
|
|
6
|
+
* then, or while it rejoins, was typed into its terminal as though the person had typed it, or refused. Now the mirror
|
|
7
|
+
* holds it, and once the pane joins delivers each message it holds, in the order sent, as a message on the bus. It is
|
|
8
|
+
* never typed into the pane.
|
|
9
|
+
*
|
|
10
|
+
* Each pane holds at most HOLD_MAX_MESSAGES messages and HOLD_MAX_BYTES; a message that doesn't fit is refused, never
|
|
11
|
+
* held in place of an older one, whose sender was told it waits. A held message is dropped when its pane closes, when
|
|
12
|
+
* the pane hasn't joined HOLD_DEADLINE_MS after the message was sent, when the pane can no longer join (its program was
|
|
13
|
+
* started again in place without a credential that joins), when the renderer reports that the pane's program has exited,
|
|
14
|
+
* or when the mirror stops; the mirror tells a sender that is on the bus (undeliveredNotice). A message for a ZTC pane
|
|
15
|
+
* whose program has exited is never held: its sender is told so at once (paneExitedRefusal), until a ZTC that joins
|
|
16
|
+
* stands for the pane again. HOLD_DEADLINE_MS is 2 minutes: a ZTC pane joins as its program starts, before
|
|
17
|
+
* its first screen, within seconds even on a busy machine, so 2 minutes covers a slow start many times over (MotSi's
|
|
18
|
+
* wrapper, the desktop's launcher admission of 15 s, a colony cell's own renderer), while a sender waiting for an answer
|
|
19
|
+
* hears within a couple of minutes that none can come. It stays under the 5 minutes the bus queues a message for a member
|
|
20
|
+
* that left.
|
|
21
|
+
*
|
|
22
|
+
* Pure: no timers and no I/O; the mirror keeps the clock and delivers.
|
|
23
|
+
*/
|
|
24
|
+
/** The messages a pane holds at most. */
|
|
25
|
+
export const HOLD_MAX_MESSAGES = 32;
|
|
26
|
+
/** The bytes a pane holds at most, counted as each message's JSON. */
|
|
27
|
+
export const HOLD_MAX_BYTES = 512 * 1024;
|
|
28
|
+
/** How long a held message waits for its pane to join. */
|
|
29
|
+
export const HOLD_DEADLINE_MS = 2 * 60 * 1000;
|
|
30
|
+
/** What the mirror tells the sender of a message it holds for pane PANE_ID. */
|
|
31
|
+
export function heldNotice(paneId) {
|
|
32
|
+
return `Pane ${paneId} is still starting; it gets your message when it joins.`;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* What the mirror tells the sender of a message for pane PANE_ID, a ZTC pane whose program has exited, at once: nothing
|
|
36
|
+
* reaches the pane until a ZTC is started in it again.
|
|
37
|
+
*/
|
|
38
|
+
export function paneExitedRefusal(paneId) {
|
|
39
|
+
return `Pane ${paneId}'s program has exited, so it can't take messages until it's started again.`;
|
|
40
|
+
}
|
|
41
|
+
/** What the mirror tells the sender of a message that doesn't fit in what pane PANE_ID holds. */
|
|
42
|
+
export function holdFullRefusal(paneId) {
|
|
43
|
+
return `Pane ${paneId} is still starting, and this message doesn't fit in what ZTC holds for it until it joins `
|
|
44
|
+
+ `(at most ${HOLD_MAX_MESSAGES} messages, ${HOLD_MAX_BYTES / 1024} KB in all). Send it once the pane has joined.`;
|
|
45
|
+
}
|
|
46
|
+
const DROP_REASONS = {
|
|
47
|
+
closed: paneId => `pane ${paneId} closed before it joined ZTC's message bus.`,
|
|
48
|
+
expired: paneId => `pane ${paneId} didn't join ZTC's message bus within ${HOLD_DEADLINE_MS / 60_000} minutes.`,
|
|
49
|
+
unjoinable: paneId => `pane ${paneId} can no longer join ZTC's message bus.`,
|
|
50
|
+
exited: paneId => `pane ${paneId}'s program exited before it joined ZTC's message bus.`,
|
|
51
|
+
mirror_stopped: paneId => `ZTC's mirror stopped before pane ${paneId} joined its message bus.`,
|
|
52
|
+
};
|
|
53
|
+
/** What the mirror tells a sender on the bus of COUNT of its messages for pane PANE_ID it dropped, and why. */
|
|
54
|
+
export function undeliveredNotice(paneId, count, reason) {
|
|
55
|
+
const what = count === 1 ? `Your message to pane ${paneId} wasn't delivered`
|
|
56
|
+
: `Your ${count} messages to pane ${paneId} weren't delivered`;
|
|
57
|
+
return `${what}: ${DROP_REASONS[reason](paneId)}`;
|
|
58
|
+
}
|
|
59
|
+
/** The messages held for each pane, oldest first. */
|
|
60
|
+
export class BusHold {
|
|
61
|
+
panes = new Map();
|
|
62
|
+
/** Hold MESSAGE, sent at NOW_MS, for pane PANE_ID, after what it holds already. False when it doesn't fit. */
|
|
63
|
+
hold(paneId, message, nowMs) {
|
|
64
|
+
const held = this.panes.get(paneId) ?? [];
|
|
65
|
+
const bytes = Buffer.byteLength(JSON.stringify(message), 'utf8');
|
|
66
|
+
const total = held.reduce((sum, entry) => sum + entry.bytes, 0);
|
|
67
|
+
if (held.length >= HOLD_MAX_MESSAGES || total + bytes > HOLD_MAX_BYTES)
|
|
68
|
+
return false;
|
|
69
|
+
held.push({ message, bytes, deadlineMs: nowMs + HOLD_DEADLINE_MS });
|
|
70
|
+
this.panes.set(paneId, held);
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Hand what pane PANE_ID holds to DELIVER, oldest first. DELIVER returns false when it can take no more (the pane's
|
|
75
|
+
* stream ended as it joined): that message and those after it stay held.
|
|
76
|
+
*/
|
|
77
|
+
release(paneId, deliver) {
|
|
78
|
+
const held = this.panes.get(paneId);
|
|
79
|
+
if (!held)
|
|
80
|
+
return;
|
|
81
|
+
while (held.length > 0 && deliver(held[0].message))
|
|
82
|
+
held.shift();
|
|
83
|
+
if (held.length === 0)
|
|
84
|
+
this.panes.delete(paneId);
|
|
85
|
+
}
|
|
86
|
+
/** Let go of what pane PANE_ID holds, and return it, oldest first. */
|
|
87
|
+
drop(paneId) {
|
|
88
|
+
const held = this.panes.get(paneId) ?? [];
|
|
89
|
+
this.panes.delete(paneId);
|
|
90
|
+
return held.map(entry => ({ paneId, message: entry.message }));
|
|
91
|
+
}
|
|
92
|
+
/** Let go of everything held, and return it. */
|
|
93
|
+
dropAll() {
|
|
94
|
+
return [...this.panes.keys()].flatMap(paneId => this.drop(paneId));
|
|
95
|
+
}
|
|
96
|
+
/** Let go of each message whose pane hasn't joined by its deadline, at NOW_MS, and return them. */
|
|
97
|
+
expire(nowMs) {
|
|
98
|
+
const expired = [];
|
|
99
|
+
for (const [paneId, held] of this.panes) {
|
|
100
|
+
// A pane's messages wait the same while each, so they come due in the order they were held.
|
|
101
|
+
while (held.length > 0 && held[0].deadlineMs <= nowMs)
|
|
102
|
+
expired.push({ paneId, message: held.shift().message });
|
|
103
|
+
if (held.length === 0)
|
|
104
|
+
this.panes.delete(paneId);
|
|
105
|
+
}
|
|
106
|
+
return expired;
|
|
107
|
+
}
|
|
108
|
+
/** When the next held message comes due, or null when nothing is held. */
|
|
109
|
+
nextDeadline() {
|
|
110
|
+
let next = null;
|
|
111
|
+
for (const held of this.panes.values()) {
|
|
112
|
+
const due = held[0].deadlineMs;
|
|
113
|
+
if (next === null || due < next)
|
|
114
|
+
next = due;
|
|
115
|
+
}
|
|
116
|
+
return next;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
@@ -11,7 +11,9 @@
|
|
|
11
11
|
import { type EvidenceSnapshot } from '../agent/runtime/evidence.js';
|
|
12
12
|
import type { TaskHandoffProjection } from '../session_handoff_contract.js';
|
|
13
13
|
import type { SlashCommandUsage } from '../agent/commands/index.js';
|
|
14
|
-
import type { MirrorPublisher, BusMessage, BusMember, ZlickyWebCursor, ZlickyWebHighlight, ZlickyWebStatus, HookApprovalPublishPayload } from './mirror_stream.js';
|
|
14
|
+
import type { MirrorPublisher, BusMessage, BusMember, BusSendResult, ZlickyWebCursor, ZlickyWebHighlight, ZlickyWebStatus, HookApprovalPublishPayload } from './mirror_stream.js';
|
|
15
|
+
/** What the mirror answered a bus message: it took it, held it for a pane still starting, or refused it (see there). */
|
|
16
|
+
export type { BusSendResult } from './mirror_stream.js';
|
|
15
17
|
export interface MirrorClientOptions {
|
|
16
18
|
/** Absolute deadline for each local HTTP exchange, including its response body. */
|
|
17
19
|
publicationTimeoutMs?: number;
|
|
@@ -62,9 +64,11 @@ export declare class MirrorClient implements MirrorPublisher {
|
|
|
62
64
|
*/
|
|
63
65
|
connectToBus(callback: (msg: BusMessage) => void): void;
|
|
64
66
|
/**
|
|
65
|
-
* Send a message via the bus
|
|
67
|
+
* Send a message via the bus. The message is on its way when this returns, so a caller may forget it; one that awaits
|
|
68
|
+
* the result learns whether the mirror took it, whether it holds it for a pane still starting, or why it refused it (a
|
|
69
|
+
* pane's message for a pane that runs no agent, or one that doesn't fit in what a pane holds). It never rejects.
|
|
66
70
|
*/
|
|
67
|
-
sendBusMessage(to: number | 'all', text: string, type?: BusMessage['type']):
|
|
71
|
+
sendBusMessage(to: number | 'all', text: string, type?: BusMessage['type']): Promise<BusSendResult>;
|
|
68
72
|
/**
|
|
69
73
|
* Disconnect from the bus SSE stream.
|
|
70
74
|
*/
|
|
@@ -15,6 +15,32 @@ import { parseTaskHandoffView } from './view_plugin.js';
|
|
|
15
15
|
import { randomUUID } from 'node:crypto';
|
|
16
16
|
import { MAX_TOOL_IMAGE_BODY_BYTES, mirrorToolImage } from './tool_image.js';
|
|
17
17
|
import { paneMirrorCredentialHeaders } from './pane_credentials.js';
|
|
18
|
+
/** A message the mirror holds for a pane that hasn't joined the bus yet (its 202), in the words its BODY gives. */
|
|
19
|
+
function busHeld(body) {
|
|
20
|
+
let notice;
|
|
21
|
+
try {
|
|
22
|
+
notice = JSON.parse(body)?.notice;
|
|
23
|
+
}
|
|
24
|
+
catch { /* not the mirror's JSON */ }
|
|
25
|
+
return { ok: true, held: true, notice: typeof notice === 'string' && notice
|
|
26
|
+
? notice : 'The pane hasn\'t joined ZTC\'s message bus yet; it gets your message when it joins.' };
|
|
27
|
+
}
|
|
28
|
+
/** The refusal in a mirror's answer STATUS with BODY: its own words when it gave them, else the status. */
|
|
29
|
+
function busRefusal(status, body) {
|
|
30
|
+
let answer = {};
|
|
31
|
+
try {
|
|
32
|
+
const parsed = JSON.parse(body);
|
|
33
|
+
if (parsed && typeof parsed === 'object')
|
|
34
|
+
answer = parsed;
|
|
35
|
+
}
|
|
36
|
+
catch { /* not the mirror's JSON */ }
|
|
37
|
+
return {
|
|
38
|
+
ok: false,
|
|
39
|
+
status,
|
|
40
|
+
...(typeof answer.code === 'string' ? { code: answer.code } : {}),
|
|
41
|
+
error: typeof answer.error === 'string' && answer.error ? answer.error : `ZTC's message bus refused the message (HTTP ${status}).`,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
18
44
|
const MAX_PENDING_PUBLICATIONS = 256;
|
|
19
45
|
const MAX_PENDING_PUBLICATION_BYTES = 2 * 1024 * 1024;
|
|
20
46
|
const MAX_CHILD_EVENT_BYTES = 512 * 1024 - 512;
|
|
@@ -260,7 +286,9 @@ export class MirrorClient {
|
|
|
260
286
|
connect();
|
|
261
287
|
}
|
|
262
288
|
/**
|
|
263
|
-
* Send a message via the bus
|
|
289
|
+
* Send a message via the bus. The message is on its way when this returns, so a caller may forget it; one that awaits
|
|
290
|
+
* the result learns whether the mirror took it, whether it holds it for a pane still starting, or why it refused it (a
|
|
291
|
+
* pane's message for a pane that runs no agent, or one that doesn't fit in what a pane holds). It never rejects.
|
|
264
292
|
*/
|
|
265
293
|
sendBusMessage(to, text, type = 'ztc_message') {
|
|
266
294
|
const payload = JSON.stringify({
|
|
@@ -270,31 +298,41 @@ export class MirrorClient {
|
|
|
270
298
|
text,
|
|
271
299
|
});
|
|
272
300
|
const debugTag = `[BusSend pane=${this.paneId} -> ${to} port=${this.port}]`;
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
301
|
+
return new Promise(resolve => {
|
|
302
|
+
const req = request({
|
|
303
|
+
hostname: 'localhost',
|
|
304
|
+
port: this.port,
|
|
305
|
+
path: this.groupId ? `/bus/send/${this.groupId}` : '/bus/send',
|
|
306
|
+
method: 'POST',
|
|
307
|
+
headers: {
|
|
308
|
+
'Content-Type': 'application/json',
|
|
309
|
+
'Content-Length': Buffer.byteLength(payload),
|
|
310
|
+
...this.credential,
|
|
311
|
+
},
|
|
312
|
+
}, (res) => {
|
|
313
|
+
// Consume response body to avoid memory leaks
|
|
314
|
+
let body = '';
|
|
315
|
+
res.on('data', (chunk) => { body += chunk.toString(); });
|
|
316
|
+
res.on('end', () => {
|
|
317
|
+
if (res.statusCode === 200) {
|
|
318
|
+
resolve({ ok: true });
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
if (res.statusCode === 202) {
|
|
322
|
+
resolve(busHeld(body));
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
289
325
|
process.stderr.write(`${debugTag} HTTP ${res.statusCode}: ${body}\n`);
|
|
290
|
-
|
|
326
|
+
resolve(busRefusal(res.statusCode ?? 0, body));
|
|
327
|
+
});
|
|
291
328
|
});
|
|
329
|
+
req.on('error', (err) => {
|
|
330
|
+
process.stderr.write(`${debugTag} error: ${err.message}\n`);
|
|
331
|
+
resolve({ ok: false, status: 0, error: `ZTC's message bus is unreachable: ${err.message}` });
|
|
332
|
+
});
|
|
333
|
+
req.write(payload);
|
|
334
|
+
req.end();
|
|
292
335
|
});
|
|
293
|
-
req.on('error', (err) => {
|
|
294
|
-
process.stderr.write(`${debugTag} error: ${err.message}\n`);
|
|
295
|
-
});
|
|
296
|
-
req.write(payload);
|
|
297
|
-
req.end();
|
|
298
336
|
}
|
|
299
337
|
/**
|
|
300
338
|
* Disconnect from the bus SSE stream.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which of the mirror's routes a caller reaches by how it authenticated (workspace spec §4A, T0.1 and T0.2). The mirror
|
|
3
|
+
* (mirror_stream.ts) asks these before it answers a request; they decide nothing about what a route then does.
|
|
4
|
+
*
|
|
5
|
+
* - A pane's own credential reaches its own business: its bus membership and messages, its publications, a colony
|
|
6
|
+
* cell's request for its own restart, and the supervision of the panes its pane opened. Never typing (/input, /submit,
|
|
7
|
+
* /pane-input) and never the person's controls: a Claude Code or Codex pane's credential sits in its environment,
|
|
8
|
+
* where the agent's own shell reads it. A workspace monitor's also reads the workspace runtime it shows and answers its
|
|
9
|
+
* decisions.
|
|
10
|
+
* - A mirror that answers only credentialed callers (the desktop owner's, and a zmux owner's own silent one) serves a
|
|
11
|
+
* caller with no credential the page's scripts and icons, and /health, which tells it only that the mirror is up, and
|
|
12
|
+
* nothing else: the page itself loads nothing without the link ZTC gives, so a browser that came without one, or
|
|
13
|
+
* whose link expired, gets a page saying how to open the mirror; and a program on this computer that knows only the
|
|
14
|
+
* port learns that a mirror listens there, and reads and controls nothing else.
|
|
15
|
+
*
|
|
16
|
+
* Pure: node builtins and types only.
|
|
17
|
+
*/
|
|
18
|
+
import type { PanePrincipal } from './pane_credentials.js';
|
|
19
|
+
/** May a pane's own credential use the route at PATHNAME? */
|
|
20
|
+
export declare function paneCredentialReaches(pane: PanePrincipal, pathname: string): boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Does a mirror that answers only credentialed callers answer a request with no credential for PATHNAME? Only a GET of
|
|
23
|
+
* the page's scripts (/js/…) and icons, or /health.
|
|
24
|
+
*/
|
|
25
|
+
export declare function servedWithoutCredential(method: string | undefined, pathname: string): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Is a request with no credential, which a mirror that answers only credentialed callers refuses, a GET of the page
|
|
28
|
+
* itself? Its refusal is then a page saying how to open the mirror, since the browser shows it.
|
|
29
|
+
*/
|
|
30
|
+
export declare function asksForThePage(method: string | undefined, pathname: string): boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Whose sessions a supervision command reaches, for a pane's own credential, which reaches only its own pane's sessions
|
|
33
|
+
* of the panes it opened: the supervisor a list names; the supervisor a start names, and the pane it would supervise
|
|
34
|
+
* (the mirror then checks that pane is one the supervisor's pane opened); or, for a stop, which names only its target,
|
|
35
|
+
* 'target' (the mirror then checks the target is one of the pane's own sessions). Null for anything else.
|
|
36
|
+
*/
|
|
37
|
+
export declare function supervisionCommandScope(command: unknown): {
|
|
38
|
+
supervisor: number;
|
|
39
|
+
target?: number;
|
|
40
|
+
} | 'target' | null;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which of the mirror's routes a caller reaches by how it authenticated (workspace spec §4A, T0.1 and T0.2). The mirror
|
|
3
|
+
* (mirror_stream.ts) asks these before it answers a request; they decide nothing about what a route then does.
|
|
4
|
+
*
|
|
5
|
+
* - A pane's own credential reaches its own business: its bus membership and messages, its publications, a colony
|
|
6
|
+
* cell's request for its own restart, and the supervision of the panes its pane opened. Never typing (/input, /submit,
|
|
7
|
+
* /pane-input) and never the person's controls: a Claude Code or Codex pane's credential sits in its environment,
|
|
8
|
+
* where the agent's own shell reads it. A workspace monitor's also reads the workspace runtime it shows and answers its
|
|
9
|
+
* decisions.
|
|
10
|
+
* - A mirror that answers only credentialed callers (the desktop owner's, and a zmux owner's own silent one) serves a
|
|
11
|
+
* caller with no credential the page's scripts and icons, and /health, which tells it only that the mirror is up, and
|
|
12
|
+
* nothing else: the page itself loads nothing without the link ZTC gives, so a browser that came without one, or
|
|
13
|
+
* whose link expired, gets a page saying how to open the mirror; and a program on this computer that knows only the
|
|
14
|
+
* port learns that a mirror listens there, and reads and controls nothing else.
|
|
15
|
+
*
|
|
16
|
+
* Pure: node builtins and types only.
|
|
17
|
+
*/
|
|
18
|
+
/** Routes a pane's credential reaches, with whatever follows them (a mirror group's path). */
|
|
19
|
+
const PANE_CREDENTIAL_PATHS = ['/bus/connect', '/bus/send', '/pane-publication', '/pane-events'];
|
|
20
|
+
/** Routes a pane's credential reaches as they are. */
|
|
21
|
+
const PANE_CREDENTIAL_EXACT_PATHS = new Set(['/pane-store-event', '/colony/agents/restart',
|
|
22
|
+
'/supervision/command']);
|
|
23
|
+
/**
|
|
24
|
+
* What a workspace monitor's credential also reaches: the runtime it shows, the decisions it answers, and, for the
|
|
25
|
+
* desktop's Status, the native pane its row drills into and back out of.
|
|
26
|
+
*/
|
|
27
|
+
const WORKSPACE_MONITOR_PATHS = new Set(['/workspace/runtime', '/ask-user-answer', '/workspace/pane-action']);
|
|
28
|
+
/** May a pane's own credential use the route at PATHNAME? */
|
|
29
|
+
export function paneCredentialReaches(pane, pathname) {
|
|
30
|
+
if (PANE_CREDENTIAL_EXACT_PATHS.has(pathname))
|
|
31
|
+
return true;
|
|
32
|
+
if (PANE_CREDENTIAL_PATHS.some(path => pathname === path || pathname.startsWith(`${path}/`)))
|
|
33
|
+
return true;
|
|
34
|
+
return pane.scope === 'workspace-monitor' && WORKSPACE_MONITOR_PATHS.has(pathname);
|
|
35
|
+
}
|
|
36
|
+
/** What of its page a mirror that answers only credentialed callers still serves anyone on this computer, and its /health. */
|
|
37
|
+
const PAGE_SHELL_PATHS = new Set(['/favicon.svg', '/favicon.ico', '/mirror-favicon.svg', '/health']);
|
|
38
|
+
/**
|
|
39
|
+
* Does a mirror that answers only credentialed callers answer a request with no credential for PATHNAME? Only a GET of
|
|
40
|
+
* the page's scripts (/js/…) and icons, or /health.
|
|
41
|
+
*/
|
|
42
|
+
export function servedWithoutCredential(method, pathname) {
|
|
43
|
+
return method === 'GET' && (PAGE_SHELL_PATHS.has(pathname) || pathname.startsWith('/js/'));
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Is a request with no credential, which a mirror that answers only credentialed callers refuses, a GET of the page
|
|
47
|
+
* itself? Its refusal is then a page saying how to open the mirror, since the browser shows it.
|
|
48
|
+
*/
|
|
49
|
+
export function asksForThePage(method, pathname) {
|
|
50
|
+
return method === 'GET' && pathname === '/';
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Whose sessions a supervision command reaches, for a pane's own credential, which reaches only its own pane's sessions
|
|
54
|
+
* of the panes it opened: the supervisor a list names; the supervisor a start names, and the pane it would supervise
|
|
55
|
+
* (the mirror then checks that pane is one the supervisor's pane opened); or, for a stop, which names only its target,
|
|
56
|
+
* 'target' (the mirror then checks the target is one of the pane's own sessions). Null for anything else.
|
|
57
|
+
*/
|
|
58
|
+
export function supervisionCommandScope(command) {
|
|
59
|
+
if (!command || typeof command !== 'object' || Array.isArray(command))
|
|
60
|
+
return null;
|
|
61
|
+
const { type, supervisor_pane_id: supervisor, target_pane_id: target } = command;
|
|
62
|
+
if (type === 'stop_session')
|
|
63
|
+
return 'target';
|
|
64
|
+
if (!Number.isSafeInteger(supervisor))
|
|
65
|
+
return null;
|
|
66
|
+
if (type === 'list_sessions')
|
|
67
|
+
return { supervisor: supervisor };
|
|
68
|
+
if (type === 'start_session' && Number.isSafeInteger(target))
|
|
69
|
+
return { supervisor: supervisor, target: target };
|
|
70
|
+
return null;
|
|
71
|
+
}
|