@zergai/ztc 0.26.2-dev.202611210149 → 0.26.4-dev.202611231953
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 +590 -28
- 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-tui-darwin-arm64 +0 -0
- package/bin/ztc-tui-linux-x64 +0 -0
- package/bin/zterm-darwin-arm64 +2 -2
- package/dist/App.js +18 -1
- package/dist/agent/agent.d.ts +25 -1
- package/dist/agent/agent.js +26 -1
- package/dist/agent/attached_images.d.ts +58 -0
- package/dist/agent/attached_images.js +145 -0
- package/dist/agent/backends/anthropic.d.ts +14 -0
- package/dist/agent/backends/anthropic.js +49 -2
- package/dist/agent/backends/codex_app_server.d.ts +3 -1
- package/dist/agent/backends/codex_app_server.js +9 -6
- package/dist/agent/backends/codex_auth.js +3 -0
- package/dist/agent/backends/gemini.js +3 -0
- package/dist/agent/backends/host_effort_policy.js +14 -2
- package/dist/agent/backends/http_error.d.ts +17 -0
- package/dist/agent/backends/http_error.js +50 -1
- package/dist/agent/backends/index.js +2 -1
- package/dist/agent/backends/model_redirect.d.ts +47 -0
- package/dist/agent/backends/model_redirect.js +97 -0
- package/dist/agent/backends/openai_compatible.js +5 -0
- package/dist/agent/backends/openai_responses.d.ts +9 -1
- package/dist/agent/backends/openai_responses.js +21 -12
- package/dist/agent/backends/prompt_cache.d.ts +8 -0
- package/dist/agent/backends/prompt_cache.js +10 -0
- package/dist/agent/backends/provider_ids.d.ts +1 -1
- package/dist/agent/backends/provider_ids.js +1 -1
- package/dist/agent/backends/types.d.ts +8 -0
- package/dist/agent/backends/zerg-api.js +5 -1
- package/dist/agent/backends/zrouter_stream.d.ts +75 -9
- package/dist/agent/backends/zrouter_stream.js +250 -49
- package/dist/agent/commands/config.js +51 -4
- package/dist/agent/commands/connect.js +20 -0
- package/dist/agent/commands/credential_status.js +5 -3
- package/dist/agent/commands/debug.js +3 -0
- package/dist/agent/commands/diff.js +9 -5
- package/dist/agent/commands/emulation.js +7 -1
- package/dist/agent/commands/inbox.js +4 -3
- package/dist/agent/commands/mcp.js +3 -0
- package/dist/agent/commands/models.js +20 -3
- package/dist/agent/commands/permissions.js +16 -2
- package/dist/agent/commands/provider_selection.js +13 -6
- package/dist/agent/commands/settings.js +15 -4
- package/dist/agent/commands/status.js +23 -1
- package/dist/agent/commands/types.d.ts +34 -3
- package/dist/agent/commands/update.d.ts +2 -1
- package/dist/agent/commands/update.js +8 -3
- package/dist/agent/commands/zerg.d.ts +6 -0
- package/dist/agent/commands/zerg.js +152 -28
- package/dist/agent/commands/ztc_pane.js +8 -5
- package/dist/agent/factory.js +22 -4
- package/dist/agent/request_encoder.js +11 -2
- package/dist/agent/runtime/command_jobs.js +16 -1
- package/dist/agent/runtime/command_panes.js +4 -1
- package/dist/agent/runtime/execution_context.d.ts +8 -1
- package/dist/agent/runtime/execution_context.js +8 -2
- package/dist/agent/runtime/policy.d.ts +66 -5
- package/dist/agent/runtime/policy.js +161 -33
- package/dist/agent/runtime/remote_broker_transport.d.ts +7 -0
- package/dist/agent/runtime/remote_broker_transport.js +33 -3
- package/dist/agent/runtime/remote_resource_errors.d.ts +7 -0
- package/dist/agent/runtime/remote_resource_errors.js +10 -0
- package/dist/agent/runtime/remote_resources.d.ts +1 -1
- package/dist/agent/runtime/remote_resources.js +7 -3
- package/dist/agent/runtime/source_verification.js +15 -12
- package/dist/agent/runtime/tool_runtime.js +165 -58
- package/dist/agent/runtime/verifier_capture.d.ts +1 -1
- package/dist/agent/runtime/verifier_capture.js +6 -0
- package/dist/agent/runtime/work_base.js +46 -14
- package/dist/agent/tools/adaptation.js +29 -12
- package/dist/agent/tools/apply_patch.js +62 -5
- package/dist/agent/tools/background.js +2 -0
- package/dist/agent/tools/benchmark_cpu.js +2 -0
- package/dist/agent/tools/bus_message.d.ts +4 -2
- package/dist/agent/tools/bus_message.js +21 -3
- package/dist/agent/tools/delegate.js +2 -0
- package/dist/agent/tools/file.d.ts +12 -1
- package/dist/agent/tools/file.js +92 -11
- package/dist/agent/tools/lsp.d.ts +12 -0
- package/dist/agent/tools/lsp.js +61 -5
- package/dist/agent/tools/pane.js +20 -45
- package/dist/agent/tools/project_context.js +5 -2
- package/dist/agent/tools/routine.js +20 -4
- package/dist/agent/tools/screenshot.d.ts +8 -0
- package/dist/agent/tools/screenshot.js +39 -25
- package/dist/agent/tools/search.js +31 -3
- package/dist/agent/tools/shell.js +7 -1
- package/dist/agent/tools/supervision.js +6 -0
- package/dist/agent/tools/types.d.ts +18 -0
- package/dist/agent/tools/zerg.js +15 -19
- package/dist/agent/tools/ztc_command.d.ts +15 -4
- package/dist/agent/tools/ztc_command.js +48 -34
- package/dist/app/agent_runner.js +8 -6
- package/dist/app/conversation_compaction.d.ts +6 -3
- package/dist/app/conversation_compaction.js +6 -3
- package/dist/app/tool_callbacks.js +1 -0
- package/dist/audio/sfx.js +6 -0
- package/dist/batch/worktree.js +22 -10
- package/dist/board/client.js +2 -1
- package/dist/cli_main.d.ts +1 -0
- package/dist/cli_main.js +141 -45
- package/dist/cli_ratatui.d.ts +49 -4
- package/dist/cli_ratatui.js +355 -79
- package/dist/conductor_branch.js +4 -1
- package/dist/config/host_credentials.d.ts +157 -0
- package/dist/config/host_credentials.js +589 -0
- package/dist/config/model_pin.d.ts +111 -0
- package/dist/config/model_pin.js +182 -0
- package/dist/config/own_state_files.d.ts +35 -0
- package/dist/config/own_state_files.js +161 -0
- package/dist/config/permission_pin.d.ts +148 -0
- package/dist/config/permission_pin.js +203 -0
- package/dist/config/permissions.d.ts +9 -2
- package/dist/config/permissions.js +14 -3
- package/dist/config/platform_login.d.ts +72 -0
- package/dist/config/platform_login.js +111 -0
- package/dist/config/profile_limits.d.ts +34 -0
- package/dist/config/profile_limits.js +66 -0
- package/dist/config/provider_api_key_env.js +3 -2
- package/dist/config/provider_models.d.ts +6 -1
- package/dist/config/provider_models.js +38 -11
- package/dist/config/repo_config.d.ts +22 -0
- package/dist/config/repo_config.js +77 -3
- package/dist/config/router_runtime_grant.d.ts +53 -0
- package/dist/config/router_runtime_grant.js +131 -0
- package/dist/config/settings_merge.d.ts +22 -0
- package/dist/config/settings_merge.js +75 -0
- package/dist/config/types.d.ts +8 -0
- package/dist/config.d.ts +334 -4
- package/dist/config.js +1024 -41
- package/dist/context/project_intelligence.d.ts +11 -2
- package/dist/context/project_intelligence.js +38 -15
- package/dist/desktop/daily/desktop_wiring.js +8 -6
- package/dist/desktop/daily/native_pane_launcher.js +2 -1
- package/dist/desktop/daily/native_tool.d.ts +1 -1
- package/dist/desktop/daily/native_tool.js +4 -3
- package/dist/desktop/daily/process_identity.js +10 -2
- package/dist/desktop/daily/source_baseline.js +4 -3
- package/dist/diagnostics/native_terminal_benchmark.js +3 -2
- package/dist/diagnostics/session_export.js +27 -14
- package/dist/embedded/anempa_runtime.js +3 -3
- package/dist/embedded/gameboy_runtime.js +5 -4
- package/dist/embedded/zergpod_runtime.js +5 -4
- package/dist/emulation/catalog.js +6 -1
- package/dist/emulation/tool_adapters.js +13 -3
- package/dist/environment/store.js +5 -4
- package/dist/extension_discovery.d.ts +12 -2
- package/dist/extension_discovery.js +52 -7
- package/dist/headless.d.ts +8 -0
- package/dist/headless.js +52 -13
- package/dist/headless_cli.js +16 -1
- package/dist/headless_workflow_trigger.js +1 -1
- package/dist/hooks/dispatcher.d.ts +8 -0
- package/dist/hooks/dispatcher.js +22 -2
- package/dist/hooks/lifecycle.d.ts +6 -0
- package/dist/hooks/lifecycle.js +1 -0
- package/dist/hooks/program_files.d.ts +10 -0
- package/dist/hooks/program_files.js +175 -0
- package/dist/hooks/runner.d.ts +11 -1
- package/dist/hooks/runner.js +112 -11
- package/dist/hooks/types.d.ts +2 -0
- package/dist/host_credentials_startup.d.ts +6 -0
- package/dist/host_credentials_startup.js +17 -0
- package/dist/instance_registry.d.ts +6 -2
- package/dist/instance_registry.js +9 -15
- package/dist/intake/format.d.ts +6 -0
- package/dist/intake/format.js +9 -0
- package/dist/intake/notifications.js +2 -1
- package/dist/lsp/manager.d.ts +1 -1
- package/dist/lsp/manager.js +4 -5
- package/dist/mcp/client.d.ts +2 -0
- package/dist/mcp/client.js +116 -18
- package/dist/mcp/registry.d.ts +12 -0
- package/dist/mcp/registry.js +15 -1
- package/dist/modes/handlers.js +14 -5
- package/dist/panes/agent_cli.d.ts +23 -1
- package/dist/panes/agent_cli.js +96 -1
- package/dist/panes/agent_panes.d.ts +27 -4
- package/dist/panes/agent_panes.js +73 -11
- package/dist/panes/editor_program.d.ts +8 -0
- package/dist/panes/editor_program.js +25 -0
- package/dist/panes/motsi.d.ts +11 -1
- package/dist/panes/motsi.js +14 -1
- package/dist/panes/pane_registry.d.ts +28 -1
- package/dist/panes/pane_registry.js +25 -0
- package/dist/panes/worker_route.js +5 -3
- package/dist/panes/ztc_launch.d.ts +8 -0
- package/dist/panes/ztc_launch.js +26 -1
- package/dist/profile_baseline.js +2 -0
- package/dist/router_auth.d.ts +8 -3
- package/dist/router_auth.js +34 -2
- package/dist/runtime_info.js +56 -38
- package/dist/session.js +12 -2
- package/dist/shannon/runtime.js +13 -1
- package/dist/simulation/domains/loader.d.ts +2 -1
- package/dist/simulation/domains/loader.js +19 -2
- package/dist/simulation/living/context.d.ts +8 -8
- package/dist/simulation/living/types.d.ts +2 -2
- package/dist/simulation/memory/embedding.d.ts +2 -1
- package/dist/simulation/memory/embedding.js +25 -6
- package/dist/terminal_attach.js +2 -1
- package/dist/types.d.ts +21 -0
- package/dist/ui/ratatui/headless_pane_manager.d.ts +12 -6
- package/dist/ui/ratatui/headless_pane_manager.js +65 -21
- package/dist/ui/ratatui/ipc_path.d.ts +41 -2
- package/dist/ui/ratatui/ipc_path.js +164 -5
- package/dist/ui/ratatui/pane_credential_broker.d.ts +17 -4
- package/dist/ui/ratatui/pane_credential_broker.js +23 -5
- package/dist/ui/ratatui/pane_relay.d.ts +74 -0
- package/dist/ui/ratatui/pane_relay.js +188 -13
- package/dist/ui/ratatui/process.d.ts +15 -1
- package/dist/ui/ratatui/process.js +129 -163
- package/dist/ui/ratatui/protocol.d.ts +11 -1
- package/dist/ui/ratatui/protocol.js +6 -0
- package/dist/ui/ratatui/ztc_launch_trust.d.ts +87 -0
- package/dist/ui/ratatui/ztc_launch_trust.js +249 -0
- package/dist/utils/admitted_open.d.ts +40 -0
- package/dist/utils/admitted_open.js +111 -0
- package/dist/utils/clipboard.js +2 -1
- package/dist/utils/computer_native.js +12 -56
- package/dist/utils/dictation.js +15 -12
- package/dist/utils/dictation_native.js +17 -60
- package/dist/utils/git_probe_options.d.ts +28 -0
- package/dist/utils/git_probe_options.js +30 -0
- package/dist/utils/git_program_files.d.ts +145 -0
- package/dist/utils/git_program_files.js +1282 -0
- package/dist/utils/git_repository_programs.d.ts +13 -0
- package/dist/utils/git_repository_programs.js +38 -0
- package/dist/utils/host_shell.js +2 -1
- package/dist/utils/idle_compaction.js +8 -1
- package/dist/utils/image_fit.d.ts +4 -1
- package/dist/utils/image_fit.js +6 -2
- package/dist/utils/image_preview.js +3 -2
- package/dist/utils/mirror_media.d.ts +22 -4
- package/dist/utils/mirror_media.js +87 -86
- package/dist/utils/ml_native.js +11 -56
- package/dist/utils/native_install.d.ts +55 -0
- package/dist/utils/native_install.js +107 -0
- package/dist/utils/open_url.js +2 -0
- package/dist/utils/process_end.d.ts +16 -0
- package/dist/utils/process_end.js +73 -0
- package/dist/utils/router_catalog.js +9 -2
- package/dist/utils/sandbox.d.ts +64 -1
- package/dist/utils/sandbox.js +397 -89
- package/dist/utils/shell.js +31 -12
- package/dist/utils/subprocess_env.d.ts +218 -2
- package/dist/utils/subprocess_env.js +429 -11
- package/dist/utils/system_ps.d.ts +5 -0
- package/dist/utils/system_ps.js +30 -0
- package/dist/utils/tts.js +5 -0
- package/dist/utils/tts_native.js +13 -60
- package/dist/utils/zlicky_overlay.js +17 -50
- package/dist/utils/ztc_own_state.d.ts +58 -0
- package/dist/utils/ztc_own_state.js +236 -0
- package/dist/web/mirror_authenticator.d.ts +67 -6
- package/dist/web/mirror_authenticator.js +131 -22
- package/dist/web/mirror_stream.d.ts +54 -10
- package/dist/web/mirror_stream.js +379 -92
- package/dist/web/observer_query_redaction.js +53 -13
- package/dist/web/observer_secret_redaction.d.ts +59 -0
- package/dist/web/observer_secret_redaction.js +714 -0
- package/dist/web/pane_credentials.d.ts +20 -2
- package/dist/web/pane_credentials.js +58 -5
- package/dist/worker/executor.js +3 -1
- package/dist/workspace_collision.js +19 -15
- package/dist/zerg/local_zerg_launches.d.ts +27 -0
- package/dist/zerg/local_zerg_launches.js +198 -0
- package/dist/zlm-embed.mjs +6257 -2791
- package/npm-shrinkwrap.json +1 -1
- package/package.json +42 -15
|
@@ -0,0 +1,714 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Withholds credentials from what a read-only observation link is sent, by what they are rather than where they sit
|
|
3
|
+
* (review VS-2): the live value of each of ZTC's credential variables and each credential its profile holds (the keys
|
|
4
|
+
* it saved or a variable it names holds, its logins), mirror tokens wherever they appear, and the credential that
|
|
5
|
+
* follows `Authorization: Bearer`. Only the observer projection (MirrorStreamServer.observerEvent)
|
|
6
|
+
* applies it; the owner and controllers see output as it is.
|
|
7
|
+
*
|
|
8
|
+
* Streamed text arrives in pieces that can split a credential, so a stream keeps the end of what it has shown, as
|
|
9
|
+
* ObserverQueryRedaction does: a credential a later piece completes is withheld in that piece, and a piece that ends
|
|
10
|
+
* with what may be the start of one withholds that end. When it cannot tell, it withholds.
|
|
11
|
+
*/
|
|
12
|
+
import { configStore } from '../config.js';
|
|
13
|
+
import { liveCredentialValues } from '../config/host_credentials.js';
|
|
14
|
+
import { PROVIDER_API_KEY_ENV } from '../config/provider_api_key_env.js';
|
|
15
|
+
import { MODEL_ROUTE_CREDENTIAL_ENV, userSubprocessCredentialEnv, ZTC_CREDENTIAL_ENV } from '../utils/subprocess_env.js';
|
|
16
|
+
import { holdsPaneCredentials, isLivePaneCredential, PANE_CREDENTIAL_LENGTH } from './pane_credentials.js';
|
|
17
|
+
const REDACTED = '[redacted]';
|
|
18
|
+
/** A value shorter than this is too likely to occur in ordinary output to be withheld wherever it appears. */
|
|
19
|
+
export const OBSERVER_SECRET_MIN_LENGTH = 12;
|
|
20
|
+
/**
|
|
21
|
+
* The variables whose values an observer is never shown: ZTC's own credentials, as every user subprocess loses them
|
|
22
|
+
* (ZTC_CREDENTIAL_ENV, the host's Router runtime grant among them), so a credential added there is withheld here too;
|
|
23
|
+
* ZERG_API_TOKEN, which holds a resident's Router grant; and every model-route credential.
|
|
24
|
+
*/
|
|
25
|
+
export const OBSERVER_SECRET_ENV = Object.freeze([...new Set([
|
|
26
|
+
...ZTC_CREDENTIAL_ENV, 'ZERG_API_TOKEN', ...Object.values(PROVIDER_API_KEY_ENV).flat(), ...MODEL_ROUTE_CREDENTIAL_ENV,
|
|
27
|
+
])]);
|
|
28
|
+
/**
|
|
29
|
+
* The values to withhold now, each of OBSERVER_SECRET_MIN_LENGTH characters or more: what each of OBSERVER_SECRET_ENV
|
|
30
|
+
* holds, and each of the variables the host has reserved for credentials since (userSubprocessCredentialEnv: a remote
|
|
31
|
+
* resource's broker token, the key a profile names), read through the host-credential store, which took ZTC's own and
|
|
32
|
+
* every copy of one under another name out of process.env at startup or minted them there (config/host_credentials.ts
|
|
33
|
+
* liveCredentialValues: every value it holds, named or not), so the process.env it emptied can't switch this off; and
|
|
34
|
+
* HELD, every credential ZTC's profile holds (ConfigStore.listHeldCredentials: what a variable it names holds, the keys
|
|
35
|
+
* it saved, its logins), whether or not a headless run reserved them, so the desktop withholds the keys it infers with
|
|
36
|
+
* too.
|
|
37
|
+
*/
|
|
38
|
+
export function liveObserverSecrets(env = process.env, held = configStore.listHeldCredentials()) {
|
|
39
|
+
const values = new Set(liveCredentialValues([...new Set([...OBSERVER_SECRET_ENV, ...userSubprocessCredentialEnv()])], env));
|
|
40
|
+
for (const raw of held) {
|
|
41
|
+
const value = raw?.trim();
|
|
42
|
+
if (value && value.length >= OBSERVER_SECRET_MIN_LENGTH)
|
|
43
|
+
values.add(value);
|
|
44
|
+
}
|
|
45
|
+
return [...values].filter(value => value.length >= OBSERVER_SECRET_MIN_LENGTH).sort((a, b) => b.length - a.length);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Mirror tokens as mirror_authenticator mints them, whoever minted these: a controller token (<expiry s>.<prompt
|
|
49
|
+
* scope>.<hmac>, or the legacy <expiry>.<hmac>) and an observation token (<expiry ms>.observe.<share id>.<hmac>).
|
|
50
|
+
* Each expiry is bounded (an observation token's is 13 digits), so a long run of digits is scanned once, not again
|
|
51
|
+
* from each of its positions; in a longer run the token's secret part is still found, after its first digits.
|
|
52
|
+
*/
|
|
53
|
+
const TOKEN = /\d{10,13}\.(?:[01]\.)?[0-9a-f]{64}|\d{1,16}\.observe\.[0-9a-f-]{36}\.[0-9a-f]{64}/gi;
|
|
54
|
+
/**
|
|
55
|
+
* The start of a mirror token cut off at the end of a piece. Its expiry alone is no secret; once the scope, or
|
|
56
|
+
* `observe.`, follows it, the rest is.
|
|
57
|
+
*/
|
|
58
|
+
const TOKEN_START = /(?:\d{10,13}\.[01]\.[0-9a-f]{0,63}|\d{10,13}\.[0-9a-f]{16,63}|\d{1,16}\.observe\.[0-9a-f-]{0,36}(?:\.[0-9a-f]{0,63})?)$/i;
|
|
59
|
+
/**
|
|
60
|
+
* `Authorization: Bearer `, as a header line, a curl -H argument, or a JSON, JavaScript or Python mapping writes it.
|
|
61
|
+
* The spaces after the colon are one run (a quote, if any, splits it in two), so a long run of them is scanned once.
|
|
62
|
+
*/
|
|
63
|
+
const BEARER = /authorization["'`]?\s*[:=]\s*(?:["'`]\s*)?bearer\s+|ZTC_MIRROR_PANE_TOKEN["'`]?\s*[:=]\s*(?:["'`]\s*)?/gi;
|
|
64
|
+
/** What ends the credential after BEARER: also what follows ZTC_MIRROR_PANE_TOKEN=, a pane's own mirror credential. */
|
|
65
|
+
const CREDENTIAL_END = /[\s"'`<>,;]/;
|
|
66
|
+
/** Fewer leading characters of a value than this, at the end of a piece, are not taken for its start. */
|
|
67
|
+
const PARTIAL_MIN = 4;
|
|
68
|
+
/** How far back a piece's end is searched for the start of a token. */
|
|
69
|
+
const TOKEN_START_WINDOW = 160;
|
|
70
|
+
/** What a stream keeps of what it has shown: enough to find any credential a later piece completes. */
|
|
71
|
+
const MIN_TAIL = 256;
|
|
72
|
+
const MAX_TAIL = 16 * 1024;
|
|
73
|
+
/** Streams with their own context; past this a new stream's text is withheld whole, as ObserverQueryRedaction does. */
|
|
74
|
+
const MAX_STREAMS = 128;
|
|
75
|
+
/**
|
|
76
|
+
* A run of base64url characters, a pane credential's alphabet. The pane credential store keeps only digests
|
|
77
|
+
* (web/pane_credentials.ts), so a run of a credential's length is checked against them, and fewer of its characters
|
|
78
|
+
* than PANE_PARTIAL_MIN at a cut are taken for part of one, where nothing can tell: at most fifteen of its 43
|
|
79
|
+
* characters, 90 of its 256 bits, are ever shown.
|
|
80
|
+
*/
|
|
81
|
+
const BASE64URL_RUN = /[A-Za-z0-9_-]+/g;
|
|
82
|
+
const BASE64URL = /[A-Za-z0-9_-]/;
|
|
83
|
+
const PANE_PARTIAL_MIN = 16;
|
|
84
|
+
/** Where TEXT shows a live pane credential whole (isLivePaneCredential). */
|
|
85
|
+
function paneCredentialRanges(text) {
|
|
86
|
+
if (!holdsPaneCredentials())
|
|
87
|
+
return [];
|
|
88
|
+
const ranges = [];
|
|
89
|
+
for (const match of text.matchAll(BASE64URL_RUN)) {
|
|
90
|
+
if (match[0].length === PANE_CREDENTIAL_LENGTH && isLivePaneCredential(match[0]))
|
|
91
|
+
ranges.push([match.index, match.index + match[0].length]);
|
|
92
|
+
}
|
|
93
|
+
return ranges;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Where TEXT, read up to END, ends with a run of base64url characters that may be the start of a live pane credential:
|
|
97
|
+
* PANE_PARTIAL_MIN of them or more and fewer than a whole one, while a mirror here holds any. -1 otherwise.
|
|
98
|
+
*/
|
|
99
|
+
function paneCredentialStartAtEnd(text, end) {
|
|
100
|
+
if (!holdsPaneCredentials())
|
|
101
|
+
return -1;
|
|
102
|
+
let start = end;
|
|
103
|
+
while (start > 0 && end - start < PANE_CREDENTIAL_LENGTH && BASE64URL.test(text[start - 1]))
|
|
104
|
+
start--;
|
|
105
|
+
const length = end - start;
|
|
106
|
+
return length >= PANE_PARTIAL_MIN && length < PANE_CREDENTIAL_LENGTH ? start : -1;
|
|
107
|
+
}
|
|
108
|
+
/** How many characters TEXT, read from START, begins with that may be the rest of a live pane credential, or 0. */
|
|
109
|
+
function paneCredentialEndAtStart(text, start) {
|
|
110
|
+
if (!holdsPaneCredentials())
|
|
111
|
+
return 0;
|
|
112
|
+
let end = start;
|
|
113
|
+
while (end < text.length && end - start < PANE_CREDENTIAL_LENGTH && BASE64URL.test(text[end]))
|
|
114
|
+
end++;
|
|
115
|
+
const length = end - start;
|
|
116
|
+
return length >= PANE_PARTIAL_MIN && length < PANE_CREDENTIAL_LENGTH ? length : 0;
|
|
117
|
+
}
|
|
118
|
+
/** Where TEXT shows a credential whole, and whether a Bearer credential runs on past its end. */
|
|
119
|
+
function credentialRanges(text, secrets) {
|
|
120
|
+
const ranges = paneCredentialRanges(text);
|
|
121
|
+
for (const secret of secrets) {
|
|
122
|
+
for (let at = text.indexOf(secret); at >= 0; at = text.indexOf(secret, at + 1))
|
|
123
|
+
ranges.push([at, at + secret.length]);
|
|
124
|
+
}
|
|
125
|
+
for (const match of text.matchAll(TOKEN))
|
|
126
|
+
ranges.push([match.index, match.index + match[0].length]);
|
|
127
|
+
let open = false;
|
|
128
|
+
for (const match of text.matchAll(BEARER)) {
|
|
129
|
+
const start = match.index + match[0].length;
|
|
130
|
+
const length = text.slice(start).search(CREDENTIAL_END);
|
|
131
|
+
const end = length < 0 ? text.length : start + length;
|
|
132
|
+
if (end > start)
|
|
133
|
+
ranges.push([start, end]);
|
|
134
|
+
if (length < 0)
|
|
135
|
+
open = true;
|
|
136
|
+
}
|
|
137
|
+
return { ranges, open };
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Where TEXT, read from FLOOR up to END, ends with what may be the start of a credential among SECRETS (PARTIAL_MIN
|
|
141
|
+
* characters of it or more), or -1. Only the last characters before END are read, as many as the longest credential.
|
|
142
|
+
*/
|
|
143
|
+
function secretStartAtEnd(text, secrets, end = text.length, floor = 0) {
|
|
144
|
+
let earliest = -1;
|
|
145
|
+
for (const secret of secrets) {
|
|
146
|
+
const from = Math.max(floor, end - secret.length + 1);
|
|
147
|
+
const tail = text.slice(from, end);
|
|
148
|
+
for (let at = tail.indexOf(secret[0]); at >= 0 && tail.length - at >= PARTIAL_MIN; at = tail.indexOf(secret[0], at + 1)) {
|
|
149
|
+
if (secret.startsWith(tail.slice(at))) {
|
|
150
|
+
if (earliest < 0 || from + at < earliest)
|
|
151
|
+
earliest = from + at;
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return earliest;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Where TEXT, read up to END, ends with what may be the start of a credential, or -1. Only the last characters before
|
|
160
|
+
* END are read (as many as the longest credential, and TOKEN_START_WINDOW), so asking at many ENDs of one text reads
|
|
161
|
+
* each of them once.
|
|
162
|
+
*/
|
|
163
|
+
function credentialStartAtEnd(text, secrets, end = text.length) {
|
|
164
|
+
let earliest = secretStartAtEnd(text, secrets, end);
|
|
165
|
+
const pane = paneCredentialStartAtEnd(text, end);
|
|
166
|
+
if (pane >= 0 && (earliest < 0 || pane < earliest))
|
|
167
|
+
earliest = pane;
|
|
168
|
+
const windowStart = Math.max(0, end - TOKEN_START_WINDOW);
|
|
169
|
+
const token = TOKEN_START.exec(text.slice(windowStart, end));
|
|
170
|
+
if (token) {
|
|
171
|
+
const at = windowStart + token.index;
|
|
172
|
+
if (earliest < 0 || at < earliest)
|
|
173
|
+
earliest = at;
|
|
174
|
+
}
|
|
175
|
+
return earliest;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* How many characters TEXT, read from START, begins with that may be the rest of a credential among SECRETS, one whose
|
|
179
|
+
* start is not shown there (the longest such rest, of PARTIAL_MIN characters or more), or 0.
|
|
180
|
+
*/
|
|
181
|
+
function credentialEndAtStart(text, secrets, start = 0) {
|
|
182
|
+
if (start >= text.length)
|
|
183
|
+
return 0;
|
|
184
|
+
const first = text[start];
|
|
185
|
+
let longest = paneCredentialEndAtStart(text, start);
|
|
186
|
+
for (const secret of secrets) {
|
|
187
|
+
// The rest of SECRET from AT is what TEXT holds from START: the first such AT is the longest rest.
|
|
188
|
+
const from = Math.max(1, secret.length - (text.length - start));
|
|
189
|
+
for (let at = secret.indexOf(first, from); at >= 0 && secret.length - at >= PARTIAL_MIN; at = secret.indexOf(first, at + 1)) {
|
|
190
|
+
if (text.startsWith(secret.slice(at), start)) {
|
|
191
|
+
longest = Math.max(longest, secret.length - at);
|
|
192
|
+
break;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return longest;
|
|
197
|
+
}
|
|
198
|
+
/** What zmux puts between the rows of a terminal's frame, whether a row soft-wrapped or not (render_rows_ansi). */
|
|
199
|
+
const ROW_BREAK = '\r\n';
|
|
200
|
+
/** How many of a row's first characters may be a gutter a full-screen program draws before its text (rowRanges). */
|
|
201
|
+
const GUTTER_MAX = 24;
|
|
202
|
+
/**
|
|
203
|
+
* What a gutter on the rows a long line wraps onto is drawn with: blanks (Claude Code's and Codex's indent of a tool's
|
|
204
|
+
* output, less -N's and vim's number column on a wrapped row) and box drawing (bat's grid).
|
|
205
|
+
*/
|
|
206
|
+
const WRAP_GUTTER = /[\s─-╿]/;
|
|
207
|
+
/**
|
|
208
|
+
* Where the text of the row from START to END begins after a gutter of WRAP_GUTTER characters (GUTTER_MAX of them at
|
|
209
|
+
* most, or none), when it begins with a base64url character, a pane credential's alphabet; -1 otherwise.
|
|
210
|
+
*/
|
|
211
|
+
function runAfterGutter(text, start, end) {
|
|
212
|
+
let at = start;
|
|
213
|
+
while (at < end && at - start < GUTTER_MAX && WRAP_GUTTER.test(text[at]))
|
|
214
|
+
at++;
|
|
215
|
+
return at < end && BASE64URL.test(text[at]) ? at : -1;
|
|
216
|
+
}
|
|
217
|
+
/** RANGES in a view of a frame whose rows are PARTS, as ranges in the frame: each split at the rows it crosses. */
|
|
218
|
+
function framePartRanges(ranges, parts) {
|
|
219
|
+
const out = [];
|
|
220
|
+
for (const [start, end] of ranges) {
|
|
221
|
+
// The part START is in or before: the last that starts at or before it.
|
|
222
|
+
let low = 0;
|
|
223
|
+
let high = parts.length - 1;
|
|
224
|
+
while (low < high) {
|
|
225
|
+
const middle = (low + high + 1) >> 1;
|
|
226
|
+
if (parts[middle].at <= start)
|
|
227
|
+
low = middle;
|
|
228
|
+
else
|
|
229
|
+
high = middle - 1;
|
|
230
|
+
}
|
|
231
|
+
for (let index = low; index < parts.length && parts[index].at < end; index++) {
|
|
232
|
+
const part = parts[index];
|
|
233
|
+
const from = Math.max(start, part.at);
|
|
234
|
+
const to = Math.min(end, part.at + part.length);
|
|
235
|
+
if (to > from)
|
|
236
|
+
out.push([part.from + from - part.at, part.from + to - part.at]);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return out;
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* A frame's rows as the lines a terminal wrapped onto them read: where a row's text ends with a base64url character
|
|
243
|
+
* and the next row's begins with one after a gutter (runAfterGutter), the next row carries it on from that character
|
|
244
|
+
* (a LINK), so a run of a pane credential's alphabet that the rows split, under a gutter or not, reads as one; any other
|
|
245
|
+
* two rows are kept apart by a line break. Each row's text has its trailing blanks taken out.
|
|
246
|
+
*/
|
|
247
|
+
function wrappedRows(text, rowAt) {
|
|
248
|
+
let view = '';
|
|
249
|
+
const parts = [];
|
|
250
|
+
const links = [];
|
|
251
|
+
let carried = -1;
|
|
252
|
+
for (let row = 0; row < rowAt.length; row++) {
|
|
253
|
+
const start = rowAt[row];
|
|
254
|
+
let end = row + 1 < rowAt.length ? rowAt[row + 1] - ROW_BREAK.length : text.length;
|
|
255
|
+
while (end > start && /\s/.test(text[end - 1]))
|
|
256
|
+
end--;
|
|
257
|
+
if (carried >= 0)
|
|
258
|
+
links.push(view.length);
|
|
259
|
+
else if (row > 0)
|
|
260
|
+
view += '\n';
|
|
261
|
+
const from = carried >= 0 ? carried : start;
|
|
262
|
+
parts.push({ from, at: view.length, length: Math.max(0, end - from) });
|
|
263
|
+
view += text.slice(from, Math.max(from, end));
|
|
264
|
+
carried = -1;
|
|
265
|
+
if (row + 1 < rowAt.length && end > from && BASE64URL.test(text[end - 1])) {
|
|
266
|
+
const next = rowAt[row + 1];
|
|
267
|
+
const nextEnd = row + 2 < rowAt.length ? rowAt[row + 2] - ROW_BREAK.length : text.length;
|
|
268
|
+
carried = runAfterGutter(text, next, nextEnd);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return { text: view, parts, links };
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* The most 43-character windows of a frame's wrapped runs checked against the pane credential store's digests
|
|
275
|
+
* (wrappedPaneCredentialRanges), about a tenth of a second of hashing: no frame zmux draws, nor a terminal's kept output
|
|
276
|
+
* replayed, has as many. Past them, each run a link joins is withheld whole, since nothing has told what it holds.
|
|
277
|
+
*/
|
|
278
|
+
const WRAPPED_WINDOW_CHECKS_MAX = 1 << 18;
|
|
279
|
+
/**
|
|
280
|
+
* Where VIEW (wrappedRows) shows a live pane credential that one of its LINKS splits: each window of 43 characters of a
|
|
281
|
+
* run with a link inside it is checked against the pane credential store's digests (isLivePaneCredential), so one whose
|
|
282
|
+
* rows join it to the text around it (a shell's command row, whose last word runs into it) is found too.
|
|
283
|
+
*/
|
|
284
|
+
function wrappedPaneCredentialRanges(view, links) {
|
|
285
|
+
if (links.length === 0 || !holdsPaneCredentials())
|
|
286
|
+
return [];
|
|
287
|
+
const ranges = [];
|
|
288
|
+
let budget = WRAPPED_WINDOW_CHECKS_MAX;
|
|
289
|
+
let first = 0;
|
|
290
|
+
for (const match of view.matchAll(BASE64URL_RUN)) {
|
|
291
|
+
const start = match.index;
|
|
292
|
+
const end = start + match[0].length;
|
|
293
|
+
while (first < links.length && links[first] <= start)
|
|
294
|
+
first++;
|
|
295
|
+
if (first >= links.length || links[first] >= end || end - start < PANE_CREDENTIAL_LENGTH)
|
|
296
|
+
continue;
|
|
297
|
+
if (budget <= 0) {
|
|
298
|
+
ranges.push([start, end]);
|
|
299
|
+
continue;
|
|
300
|
+
}
|
|
301
|
+
// Each window from AT holds the link after AT when that link is before its end.
|
|
302
|
+
let link = first;
|
|
303
|
+
for (let at = start; at + PANE_CREDENTIAL_LENGTH <= end; at++) {
|
|
304
|
+
while (link < links.length && links[link] <= at)
|
|
305
|
+
link++;
|
|
306
|
+
if (link >= links.length || links[link] >= end)
|
|
307
|
+
break;
|
|
308
|
+
if (links[link] >= at + PANE_CREDENTIAL_LENGTH) {
|
|
309
|
+
// No link in this window: the next window that holds one starts 42 characters before it.
|
|
310
|
+
at = links[link] - PANE_CREDENTIAL_LENGTH;
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
if (budget-- <= 0) {
|
|
314
|
+
ranges.push([at, end]);
|
|
315
|
+
break;
|
|
316
|
+
}
|
|
317
|
+
if (isLivePaneCredential(view.slice(at, at + PANE_CREDENTIAL_LENGTH)))
|
|
318
|
+
ranges.push([at, at + PANE_CREDENTIAL_LENGTH]);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
return ranges;
|
|
322
|
+
}
|
|
323
|
+
/** Fewer characters than this after a row's gutter are not taken for part of a credential there (rowRanges). */
|
|
324
|
+
const ROW_PART_MIN = 8;
|
|
325
|
+
/** Each run of ROW_PART_MIN characters in SECRETS, with each credential and index it is found at. */
|
|
326
|
+
function credentialParts(secrets) {
|
|
327
|
+
const parts = new Map();
|
|
328
|
+
for (const secret of secrets) {
|
|
329
|
+
for (let at = 0; at + ROW_PART_MIN <= secret.length; at++) {
|
|
330
|
+
const part = secret.slice(at, at + ROW_PART_MIN);
|
|
331
|
+
const found = parts.get(part);
|
|
332
|
+
if (found)
|
|
333
|
+
found.push([secret, at]);
|
|
334
|
+
else
|
|
335
|
+
parts.set(part, [[secret, at]]);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
return parts;
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Where the rows of TEXT (each starting at its index in ROW_AT) show a part of a credential that a gutter keeps from
|
|
342
|
+
* the rest of it. A full-screen program that draws a gutter on each row (`less -N`, vim's number column, bat's grid)
|
|
343
|
+
* wraps a long line into rows that join, their breaks taken out, with the gutter between the credential's parts. Each
|
|
344
|
+
* row is read alone: its end when it is the start of a credential (PARTIAL_MIN characters or more), and the text after
|
|
345
|
+
* its gutter (GUTTER_MAX characters at most) when that is the rest of a credential, or a part of one that runs on to
|
|
346
|
+
* the row's end (ROW_PART_MIN characters or more). A row whose text only starts as a credential does, its first
|
|
347
|
+
* characters shared and the rest not, is neither.
|
|
348
|
+
*/
|
|
349
|
+
function rowRanges(text, rowAt, secrets) {
|
|
350
|
+
if (secrets.length === 0)
|
|
351
|
+
return [];
|
|
352
|
+
const parts = credentialParts(secrets);
|
|
353
|
+
const ranges = [];
|
|
354
|
+
for (let row = 0; row < rowAt.length; row++) {
|
|
355
|
+
const start = rowAt[row];
|
|
356
|
+
let end = row + 1 < rowAt.length ? rowAt[row + 1] - ROW_BREAK.length : text.length;
|
|
357
|
+
while (end > start && /\s/.test(text[end - 1]))
|
|
358
|
+
end--;
|
|
359
|
+
const opening = secretStartAtEnd(text, secrets, end, start);
|
|
360
|
+
if (opening >= 0)
|
|
361
|
+
ranges.push([opening, end]);
|
|
362
|
+
for (let at = start; at <= Math.min(start + GUTTER_MAX, end - ROW_PART_MIN); at++) {
|
|
363
|
+
const found = parts.get(text.slice(at, at + ROW_PART_MIN));
|
|
364
|
+
if (!found)
|
|
365
|
+
continue;
|
|
366
|
+
let longest = 0;
|
|
367
|
+
for (const [secret, offset] of found) {
|
|
368
|
+
let length = ROW_PART_MIN;
|
|
369
|
+
while (at + length < end && offset + length < secret.length && text[at + length] === secret[offset + length])
|
|
370
|
+
length++;
|
|
371
|
+
if (at + length === end || offset + length === secret.length)
|
|
372
|
+
longest = Math.max(longest, length);
|
|
373
|
+
}
|
|
374
|
+
if (longest > 0) {
|
|
375
|
+
ranges.push([at, at + longest]);
|
|
376
|
+
break;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
return ranges;
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Where TEXT, what a terminal shows of a whole frame (zmux's viewport, or a terminal's kept output replayed), shows a
|
|
384
|
+
* credential once its row breaks are taken out. zmux renders a line longer than the pane is wide as rows joined by
|
|
385
|
+
* CRLF, as it joins any two rows, so a credential on that line reaches the mirror cut into rows. The frame's top row may
|
|
386
|
+
* also be the rest of a credential whose start scrolled away, and its end the start of one. Ranges are in TEXT's indexes,
|
|
387
|
+
* each split at the row breaks inside it, so a credential's parts are withheld each on its own row and the frame keeps
|
|
388
|
+
* its rows. Each row is also read alone (rowRanges), for a credential a gutter splits.
|
|
389
|
+
*
|
|
390
|
+
* The rows are read once more as the lines they wrap (wrappedRows), a gutter on the rows a line wraps onto taken out:
|
|
391
|
+
* a live pane credential a row break splits is found by its digest wherever its first part starts on its row, so one
|
|
392
|
+
* that runs on from a shell's command row is too; the credential after ZTC_MIRROR_PANE_TOKEN= or `Authorization:
|
|
393
|
+
* Bearer` carries on past the gutter; and the rest of one at the frame's top row, after its gutter, or the start of one
|
|
394
|
+
* whose rows run to the frame's end, is withheld as at a cut (PANE_PARTIAL_MIN characters or more, fewer than a whole).
|
|
395
|
+
*/
|
|
396
|
+
function frameRanges(text, secrets) {
|
|
397
|
+
// Where each row starts in TEXT, and in JOINED, which is TEXT without its row breaks.
|
|
398
|
+
const rowAt = [];
|
|
399
|
+
const joinedParts = [];
|
|
400
|
+
let joined = '';
|
|
401
|
+
for (let from = 0;;) {
|
|
402
|
+
const at = text.indexOf(ROW_BREAK, from);
|
|
403
|
+
const end = at < 0 ? text.length : at;
|
|
404
|
+
rowAt.push(from);
|
|
405
|
+
joinedParts.push({ from, at: joined.length, length: end - from });
|
|
406
|
+
joined += text.slice(from, end);
|
|
407
|
+
if (at < 0)
|
|
408
|
+
break;
|
|
409
|
+
from = at + ROW_BREAK.length;
|
|
410
|
+
}
|
|
411
|
+
// One row is what TEXT itself shows, which has been read already: only its start is read again.
|
|
412
|
+
const found = [];
|
|
413
|
+
if (rowAt.length > 1) {
|
|
414
|
+
found.push(...credentialRanges(joined, secrets).ranges);
|
|
415
|
+
const start = credentialStartAtEnd(joined, secrets);
|
|
416
|
+
if (start >= 0)
|
|
417
|
+
found.push([start, joined.length]);
|
|
418
|
+
}
|
|
419
|
+
const rest = credentialEndAtStart(joined, secrets);
|
|
420
|
+
if (rest > 0)
|
|
421
|
+
found.push([0, rest]);
|
|
422
|
+
const ranges = framePartRanges(found, joinedParts);
|
|
423
|
+
ranges.push(...rowRanges(text, rowAt, secrets));
|
|
424
|
+
// The lines the rows wrap, read whole (see above).
|
|
425
|
+
const wrapped = wrappedRows(text, rowAt);
|
|
426
|
+
const seen = wrappedPaneCredentialRanges(wrapped.text, wrapped.links);
|
|
427
|
+
if (wrapped.links.length > 0) {
|
|
428
|
+
seen.push(...credentialRanges(wrapped.text, secrets).ranges);
|
|
429
|
+
const start = credentialStartAtEnd(wrapped.text, secrets);
|
|
430
|
+
if (start >= 0)
|
|
431
|
+
seen.push([start, wrapped.text.length]);
|
|
432
|
+
}
|
|
433
|
+
const top = runAfterGutter(text, 0, wrapped.parts[0].from + wrapped.parts[0].length);
|
|
434
|
+
const topRest = top < 0 ? 0 : credentialEndAtStart(wrapped.text, secrets, top);
|
|
435
|
+
if (topRest > 0)
|
|
436
|
+
seen.push([top, top + topRest]);
|
|
437
|
+
ranges.push(...framePartRanges(seen, wrapped.parts));
|
|
438
|
+
return ranges;
|
|
439
|
+
}
|
|
440
|
+
/** TEXT with each of RANGES (clipped to it; overlapping and touching ones as one) shown as [redacted]. */
|
|
441
|
+
function withheld(text, ranges) {
|
|
442
|
+
const clipped = ranges.map(([start, end]) => [Math.max(0, start), Math.min(text.length, end)])
|
|
443
|
+
.filter(([start, end]) => end > start).sort((a, b) => a[0] - b[0]);
|
|
444
|
+
let output = '';
|
|
445
|
+
let cursor = 0;
|
|
446
|
+
let open = null;
|
|
447
|
+
for (const range of clipped) {
|
|
448
|
+
if (open && range[0] <= open[1]) {
|
|
449
|
+
open[1] = Math.max(open[1], range[1]);
|
|
450
|
+
continue;
|
|
451
|
+
}
|
|
452
|
+
if (open) {
|
|
453
|
+
output += text.slice(cursor, open[0]) + REDACTED;
|
|
454
|
+
cursor = open[1];
|
|
455
|
+
}
|
|
456
|
+
open = [range[0], range[1]];
|
|
457
|
+
}
|
|
458
|
+
if (open) {
|
|
459
|
+
output += text.slice(cursor, open[0]) + REDACTED;
|
|
460
|
+
cursor = open[1];
|
|
461
|
+
}
|
|
462
|
+
return output + text.slice(cursor);
|
|
463
|
+
}
|
|
464
|
+
/**
|
|
465
|
+
* How a string that was cut short says so: the mark publishToolCall appends to an argument it cuts, or an ellipsis, as
|
|
466
|
+
* a command pane's title cut to 33 characters ends with.
|
|
467
|
+
*/
|
|
468
|
+
const CUT_MARKS = ['\n... (truncated)', '...', '…'];
|
|
469
|
+
/**
|
|
470
|
+
* The same marks inside a string, where a cut fell in what a line of a listing, a status block's field or a quoted
|
|
471
|
+
* prompt shows (a command job's notice, /loop status, /zerg status, /diff), a run of dots or of ellipses as one. A mark
|
|
472
|
+
* can also keep the end of a longer text, cut before it: a failed job's error starts with one, and a command pane's
|
|
473
|
+
* cut log and that error follow one ("Error: …", and "…" after the pane's omitted-output line).
|
|
474
|
+
*/
|
|
475
|
+
const INNER_CUT_MARK = /\n\.\.\. \(truncated\)|\.{3,}|…+/g;
|
|
476
|
+
/** A line break, raw or as JSON writes one inside a string, that ends the text before a mark: the cut may be before it. */
|
|
477
|
+
const BREAK_BEFORE_MARK = /(?:\r?\n|\\+n)$/;
|
|
478
|
+
/**
|
|
479
|
+
* The note capToolResult puts where it left out the middle of a string it shortened (result_cap.ts keepEnds): a line
|
|
480
|
+
* break, "…[N characters left out]…" and another line break, between the kept start and the kept end, each break raw or
|
|
481
|
+
* as JSON writes one inside a string (a tool's JSON result). The kept start may end inside a credential, and the kept end
|
|
482
|
+
* begin inside one.
|
|
483
|
+
*/
|
|
484
|
+
const LEFT_OUT_NOTE = /(?:\r?\n|\\+n)?…\[[\d,]+ characters left out\]…(?:\r?\n|\\+n)?/g;
|
|
485
|
+
/**
|
|
486
|
+
* TEXT with each credential it shows withheld. Where it may have been cut short, at its end (CUT) or before a mark
|
|
487
|
+
* that says it was (CUT_MARKS), at its end or anywhere within it, what may be the start of a credential is withheld
|
|
488
|
+
* too, as at the end of a stream's piece. A mark that ends TEXT is not read as part of a credential that runs up to it.
|
|
489
|
+
* A mark within TEXT, or at its start, may instead keep the end of a longer text cut before it: what may be the rest of
|
|
490
|
+
* a credential right after it is withheld too, as after a note saying a string's middle was left out (LEFT_OUT_NOTE).
|
|
491
|
+
* A line break between a cut and its mark (BREAK_BEFORE_MARK) is read as part of the mark.
|
|
492
|
+
*/
|
|
493
|
+
function redactText(text, secrets, cut) {
|
|
494
|
+
if (!text)
|
|
495
|
+
return text;
|
|
496
|
+
const mark = CUT_MARKS.find(candidate => text.endsWith(candidate)) ?? '';
|
|
497
|
+
const body = text.slice(0, text.length - mark.length);
|
|
498
|
+
const { ranges } = credentialRanges(body, secrets);
|
|
499
|
+
const cutAt = (at) => {
|
|
500
|
+
const start = credentialStartAtEnd(body, secrets, at);
|
|
501
|
+
if (start >= 0)
|
|
502
|
+
ranges.push([start, at]);
|
|
503
|
+
};
|
|
504
|
+
const restAt = (at) => {
|
|
505
|
+
const rest = credentialEndAtStart(body, secrets, at);
|
|
506
|
+
if (rest > 0)
|
|
507
|
+
ranges.push([at, at + rest]);
|
|
508
|
+
};
|
|
509
|
+
for (const inner of body.matchAll(INNER_CUT_MARK)) {
|
|
510
|
+
cutAt(inner.index);
|
|
511
|
+
// A line break between the cut and its mark: the cut is before the break.
|
|
512
|
+
const lineBreak = BREAK_BEFORE_MARK.exec(body.slice(Math.max(0, inner.index - 8), inner.index));
|
|
513
|
+
if (lineBreak)
|
|
514
|
+
cutAt(inner.index - lineBreak[0].length);
|
|
515
|
+
// The end of a longer text, cut before the mark.
|
|
516
|
+
restAt(inner.index + inner[0].length);
|
|
517
|
+
}
|
|
518
|
+
if (cut || mark)
|
|
519
|
+
cutAt(body.length);
|
|
520
|
+
for (const note of body.matchAll(LEFT_OUT_NOTE)) {
|
|
521
|
+
cutAt(note.index);
|
|
522
|
+
restAt(note.index + note[0].length);
|
|
523
|
+
}
|
|
524
|
+
return withheld(body, ranges) + mark;
|
|
525
|
+
}
|
|
526
|
+
/**
|
|
527
|
+
* TEXT (a message, a tool call's argument, a title) with each credential it shows withheld. Text that says with a mark
|
|
528
|
+
* where it was cut short (CUT_MARKS), at its end or within it, was cut wherever its limit fell, perhaps through a
|
|
529
|
+
* credential: what may be the start of one just before each mark is withheld too.
|
|
530
|
+
*/
|
|
531
|
+
export function redactObserverSecrets(text, secrets) {
|
|
532
|
+
return redactText(text, secrets, false);
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
535
|
+
* TEXT, which may have been cut short anywhere without saying so (a tool's result past its cap, a running command's
|
|
536
|
+
* latest line), with each credential it shows withheld and what may be the start of one at its end. When it cannot
|
|
537
|
+
* tell, it withholds.
|
|
538
|
+
*/
|
|
539
|
+
export function redactCutObserverSecrets(text, secrets) {
|
|
540
|
+
return redactText(text, secrets, true);
|
|
541
|
+
}
|
|
542
|
+
const ESC = 0x1b;
|
|
543
|
+
/** C1 controls that open a CSI sequence and a control string, in their 8-bit form. */
|
|
544
|
+
const C1_CSI = 0x9b;
|
|
545
|
+
const C1_STRINGS = new Set([0x90, 0x98, 0x9d, 0x9e, 0x9f]);
|
|
546
|
+
/** After ESC: what opens a CSI sequence ([) and a control string (] P X ^ _). */
|
|
547
|
+
const ESC_CSI = 0x5b;
|
|
548
|
+
const ESC_STRINGS = new Set([0x5d, 0x50, 0x58, 0x5e, 0x5f]);
|
|
549
|
+
/**
|
|
550
|
+
* What ends a run of characters a terminal shows, in ground state: the C0 controls but those that lay out text
|
|
551
|
+
* (backspace, tab, line feeds, carriage return), DEL and the C1 controls. ESC and the 8-bit CSI and control-string
|
|
552
|
+
* openers among them start a sequence; the rest show nothing.
|
|
553
|
+
*/
|
|
554
|
+
const HIDDEN = /[\x00-\x07\x0e-\x1f\x7f-\x9f]/g;
|
|
555
|
+
/**
|
|
556
|
+
* What a terminal shows of FRAGMENT when its parser starts in STATE: FRAGMENT less its escape sequences (CSI, the
|
|
557
|
+
* control strings and the shorter escapes, 7-bit or 8-bit) and the controls that show nothing.
|
|
558
|
+
*/
|
|
559
|
+
function shownText(fragment, state) {
|
|
560
|
+
let text = '';
|
|
561
|
+
const runs = [];
|
|
562
|
+
let index = 0;
|
|
563
|
+
while (index < fragment.length) {
|
|
564
|
+
if (state === 'ground') {
|
|
565
|
+
HIDDEN.lastIndex = index;
|
|
566
|
+
const hidden = HIDDEN.exec(fragment);
|
|
567
|
+
const end = hidden ? hidden.index : fragment.length;
|
|
568
|
+
if (end > index) {
|
|
569
|
+
runs.push([text.length, index]);
|
|
570
|
+
text += fragment.slice(index, end);
|
|
571
|
+
}
|
|
572
|
+
if (!hidden)
|
|
573
|
+
break;
|
|
574
|
+
const code = fragment.charCodeAt(end);
|
|
575
|
+
if (code === ESC)
|
|
576
|
+
state = 'escape';
|
|
577
|
+
else if (code === C1_CSI)
|
|
578
|
+
state = 'csi';
|
|
579
|
+
else if (C1_STRINGS.has(code))
|
|
580
|
+
state = 'string';
|
|
581
|
+
index = end + 1;
|
|
582
|
+
continue;
|
|
583
|
+
}
|
|
584
|
+
const code = fragment.charCodeAt(index);
|
|
585
|
+
// CAN and SUB end any sequence, and show nothing.
|
|
586
|
+
if (code === 0x18 || code === 0x1a)
|
|
587
|
+
state = 'ground';
|
|
588
|
+
else if (state === 'escape') {
|
|
589
|
+
if (code === ESC_CSI)
|
|
590
|
+
state = 'csi';
|
|
591
|
+
else if (ESC_STRINGS.has(code))
|
|
592
|
+
state = 'string';
|
|
593
|
+
else if (code >= 0x20 && code <= 0x2f)
|
|
594
|
+
state = 'intermediate';
|
|
595
|
+
else if (code >= 0x30)
|
|
596
|
+
state = 'ground';
|
|
597
|
+
}
|
|
598
|
+
else if (state === 'intermediate') {
|
|
599
|
+
if (code === ESC)
|
|
600
|
+
state = 'escape';
|
|
601
|
+
else if (code >= 0x30)
|
|
602
|
+
state = 'ground';
|
|
603
|
+
}
|
|
604
|
+
else if (state === 'csi') {
|
|
605
|
+
// Parameters, intermediates and C0 controls stay inside the sequence; a final byte ends it.
|
|
606
|
+
if (code === ESC)
|
|
607
|
+
state = 'escape';
|
|
608
|
+
else if (code >= 0x40)
|
|
609
|
+
state = 'ground';
|
|
610
|
+
}
|
|
611
|
+
else if (state === 'string') {
|
|
612
|
+
if (code === ESC)
|
|
613
|
+
state = 'string-escape';
|
|
614
|
+
else if (code === 0x07 || code === 0x9c)
|
|
615
|
+
state = 'ground';
|
|
616
|
+
}
|
|
617
|
+
else {
|
|
618
|
+
// After ESC in a string: ESC and a backslash (ST) end it; ESC and anything else end it and start another escape.
|
|
619
|
+
state = code === 0x5c ? 'ground' : 'escape';
|
|
620
|
+
if (state === 'escape')
|
|
621
|
+
continue;
|
|
622
|
+
}
|
|
623
|
+
index++;
|
|
624
|
+
}
|
|
625
|
+
return { text, runs, state };
|
|
626
|
+
}
|
|
627
|
+
/** The index in the piece SHOWN was read from of the character it shows at INDEX. */
|
|
628
|
+
function pieceIndex(shown, index) {
|
|
629
|
+
let low = 0;
|
|
630
|
+
let high = shown.runs.length - 1;
|
|
631
|
+
while (low < high) {
|
|
632
|
+
const middle = (low + high + 1) >> 1;
|
|
633
|
+
if (shown.runs[middle][0] <= index)
|
|
634
|
+
low = middle;
|
|
635
|
+
else
|
|
636
|
+
high = middle - 1;
|
|
637
|
+
}
|
|
638
|
+
const [shownAt, pieceAt] = shown.runs[low];
|
|
639
|
+
return pieceAt + index - shownAt;
|
|
640
|
+
}
|
|
641
|
+
/**
|
|
642
|
+
* Where FRAGMENT, the piece of a stream after TAIL (in which a Bearer credential ran on past the end if OPEN), shows a
|
|
643
|
+
* credential among SECRETS, a mirror token or a Bearer credential: whole, the rest of one TAIL began, or the start of
|
|
644
|
+
* one at its end. Ranges are in FRAGMENT's indexes, and one TAIL began starts before 0. Also returns TAIL + FRAGMENT
|
|
645
|
+
* and whether a Bearer credential runs on past its end.
|
|
646
|
+
*/
|
|
647
|
+
function pieceRanges(tail, open, fragment, secrets) {
|
|
648
|
+
const combined = tail + fragment;
|
|
649
|
+
const offset = tail.length;
|
|
650
|
+
const ranges = [];
|
|
651
|
+
let continued = false;
|
|
652
|
+
if (open) {
|
|
653
|
+
const length = fragment.search(CREDENTIAL_END);
|
|
654
|
+
ranges.push([offset, length < 0 ? combined.length : offset + length]);
|
|
655
|
+
continued = length < 0;
|
|
656
|
+
}
|
|
657
|
+
const found = credentialRanges(combined, secrets);
|
|
658
|
+
ranges.push(...found.ranges);
|
|
659
|
+
const start = credentialStartAtEnd(combined, secrets);
|
|
660
|
+
if (start >= 0)
|
|
661
|
+
ranges.push([start, combined.length]);
|
|
662
|
+
const shifted = ranges.map(([from, to]) => [from - offset, to - offset]);
|
|
663
|
+
return { ranges: shifted, combined, open: found.open || continued };
|
|
664
|
+
}
|
|
665
|
+
/** The pieces of each stream (a pane's output, a turn's streamed text), each with the credentials it shows withheld. */
|
|
666
|
+
export class ObserverSecretRedaction {
|
|
667
|
+
contexts = new Map();
|
|
668
|
+
reset(key) { this.contexts.delete(key); }
|
|
669
|
+
/**
|
|
670
|
+
* FRAGMENT, the stream KEY's next piece (its first when RESET), with each credential among SECRETS (its live
|
|
671
|
+
* values, liveObserverSecrets), each mirror token and each Bearer credential it shows withheld: whole, the rest of
|
|
672
|
+
* one an earlier piece began, or the start of one it ends with.
|
|
673
|
+
*
|
|
674
|
+
* A TERMINAL's output is also read as the terminal shows it, without its escape sequences, which colour a match
|
|
675
|
+
* inside a value (grep --color, rg) or a word a shell highlights, so a credential they split is found too. What is
|
|
676
|
+
* withheld then runs from the credential's first character to its last, the escape sequences inside it too. A
|
|
677
|
+
* terminal's piece that RESETs it is a whole frame (zmux's viewport, whose rows a long line wraps across), also read
|
|
678
|
+
* with its row breaks taken out (frameRanges): each part of a credential they split is withheld on its row.
|
|
679
|
+
*/
|
|
680
|
+
redact(key, fragment, secrets, reset = false, terminal = false) {
|
|
681
|
+
if (reset)
|
|
682
|
+
this.reset(key);
|
|
683
|
+
let context = this.contexts.get(key);
|
|
684
|
+
if (!context) {
|
|
685
|
+
if (this.contexts.size >= MAX_STREAMS)
|
|
686
|
+
return fragment ? REDACTED : '';
|
|
687
|
+
context = { tail: '', open: false, shownTail: '', shownOpen: false, escape: 'ground' };
|
|
688
|
+
this.contexts.set(key, context);
|
|
689
|
+
}
|
|
690
|
+
const longest = secrets.reduce((length, secret) => Math.max(length, secret.length), 0);
|
|
691
|
+
const keep = Math.min(MAX_TAIL, Math.max(MIN_TAIL, longest + 1));
|
|
692
|
+
const raw = pieceRanges(context.tail, context.open, fragment, secrets);
|
|
693
|
+
const ranges = raw.ranges;
|
|
694
|
+
context.tail = raw.combined.slice(-keep);
|
|
695
|
+
context.open = raw.open;
|
|
696
|
+
if (terminal) {
|
|
697
|
+
const shown = shownText(fragment, context.escape);
|
|
698
|
+
const view = pieceRanges(context.shownTail, context.shownOpen, shown.text, secrets);
|
|
699
|
+
const shownRanges = reset ? [...view.ranges, ...frameRanges(shown.text, secrets)] : view.ranges;
|
|
700
|
+
for (const [from, to] of shownRanges) {
|
|
701
|
+
if (to <= 0 || to <= from)
|
|
702
|
+
continue;
|
|
703
|
+
// From the credential's first shown character (or this piece's start, when an earlier piece began it) to its
|
|
704
|
+
// last, or to this piece's end when it runs on to the end of what this piece shows.
|
|
705
|
+
ranges.push([from < 0 ? 0 : pieceIndex(shown, from),
|
|
706
|
+
to >= shown.text.length ? fragment.length : pieceIndex(shown, to - 1) + 1]);
|
|
707
|
+
}
|
|
708
|
+
context.shownTail = view.combined.slice(-keep);
|
|
709
|
+
context.shownOpen = view.open;
|
|
710
|
+
context.escape = shown.state;
|
|
711
|
+
}
|
|
712
|
+
return withheld(fragment, ranges);
|
|
713
|
+
}
|
|
714
|
+
}
|