@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
165 lines • 7.08 kB
JavaScript
/**
* Shared constants and helpers for the persistent-terminal builtin extension.
*/
export const TERMINAL_BASH_TOOL = "bash";
export const TERMINAL_OUTPUT_TOOL = "bash_output";
export const TERMINAL_KILL_TOOL = "kill_bash";
export const TERMINAL_INPUT_TOOL = "bash_input";
export const TERMINAL_RESIZE_TOOL = "bash_resize";
export const TERMINAL_MONITOR_TOOL = "monitor";
/**
* Companion tools kept active beside the PTY `bash`. `bash_input` and `bash_resize` are not listed:
* they are search-exposed and activate on their first by-name call (senpi#4446).
*/
export const TERMINAL_COMPANION_TOOLS = [TERMINAL_OUTPUT_TOOL, TERMINAL_KILL_TOOL, TERMINAL_MONITOR_TOOL];
export const DEFAULT_COLS = 120;
export const DEFAULT_ROWS = 40;
export const DEFAULT_SCROLLBACK = 10_000;
export const DEFAULT_MAX_SESSIONS = 32;
/** Upper bound on decoded output retained per session for delta reads and `view:"log"`. */
export const MAX_SESSION_OUTPUT_CHARS = 1_000_000;
/** Grace window used to capture a background command's early output before returning its id. */
export const BACKGROUND_START_GRACE_MS = 250;
/**
* Restore-context environment handed to monitor commands. `ID` and `STATE_DIR` are set on every
* spawn of a persistent watch (ID alone for ephemeral ones); `RESTORED` and `DOWNTIME_MS` only on
* the re-spawn after a restart, where the downtime is an upper bound, never an exact gap.
*/
export const MONITOR_ENV_ID = "SENPI_MONITOR_ID";
export const MONITOR_ENV_STATE_DIR = "SENPI_MONITOR_STATE_DIR";
export const MONITOR_ENV_RESTORED = "SENPI_MONITOR_RESTORED";
export const MONITOR_ENV_DOWNTIME_MS = "SENPI_MONITOR_DOWNTIME_MS";
/**
* How long a re-spawned watch must stay up after a restore before it counts as restored. Long
* enough for `git`/`gh` cold starts to fail visibly, short enough that the digest is not held.
*/
export const RESTORE_GRACE_MS = 2_000;
/**
* After a foreground session has been killed (abort or timeout), how long to keep
* waiting for its exit to settle before releasing the tool anyway. A surviving
* descendant that holds the PTY open (or a kill that never lands) must not keep
* the agent blocked forever.
*/
export const KILLED_SESSION_EXIT_GRACE_MS = 5000;
/**
* Absolute lifetime of a durable monitor, measured from its registration. It is a deadline,
* never a sliding window: neither a restore nor a rearm extends it.
*/
export const DURABLE_MONITOR_EXPIRY_MS = 7 * 24 * 60 * 60 * 1000;
/**
* Width of a durable command watch's rolling fire window: the persisted count of matched lines it
* delivered inside the current window. It is a statistic only; nothing mutes on it.
*/
export const FIRE_BUDGET_WINDOW_MS = 24 * 60 * 60 * 1000;
/**
* Wake pacing of a persistent command watch: at most this many wakes an hour on average, with a
* burst of the same size. Lines matched between wakes are batched into the next one, never dropped.
*/
export const DEFAULT_MONITOR_WAKES_PER_HOUR = 12;
/** Runaway guard: a persistent command watch matching more lines than this in each minute... */
export const DEFAULT_MONITOR_RUNAWAY_LINES_PER_MINUTE = 120;
/** ...for this many consecutive minutes is paused with a visible notice until it is re-armed. */
export const DEFAULT_MONITOR_RUNAWAY_MINUTES = 5;
/** One per-monitor overflow file grows to this size before it rotates to a single `.1` copy. */
export const MONITOR_OVERFLOW_FILE_MAX_BYTES = 4 * 1024 * 1024;
/**
* Machine-consumed prefix of the runaway-guard pause summary: the monitor notifier treats a summary
* that starts with it as a pause, not a completion, so it must not clear the session wake streak.
*/
export const RUNAWAY_PAUSE_SUMMARY_PREFIX = "paused: runaway output";
export function runawayPauseSummary(bashId, linesPerMinute, minutes) {
return `${RUNAWAY_PAUSE_SUMMARY_PREFIX} (more than ${linesPerMinute} matched lines a minute for ${minutes} minute${minutes === 1 ? "" : "s"}); later lines stay in bash_output. Resume with monitor({ action: "rearm", bash_id: "${bashId}" }).`;
}
/**
* Non-interactive environment for foreground one-shot commands (codex-style):
* cooperative tools (`gh`, `git`, pagers, color libs) skip spinners/colors at
* the source instead of flooding the captured stream with redraw frames, and
* `git` never blocks the captured foreground PTY on interactive input.
* - `GIT_EDITOR: "true"`: git spawns `/usr/bin/true` as the editor, which exits 0
* immediately (git treats a zero-exit editor as accepted), so a `git commit`
* without `-m` aborts with "Aborting commit due to empty commit message" and a
* `git rebase -i` accepts the todo list instead of parking the tool inside
* nvim on COMMIT_EDITMSG until the timeout kills it.
* - `GIT_TERMINAL_PROMPT: "0"`: git fails fast ("could not read Username",
* exit 128) instead of prompting for credentials on the captured PTY where
* nobody can type (same opt-out `package-manager.ts` already uses for its own
* git calls).
* Background sessions keep the user's real TERM and git settings for
* interactive apps.
*/
export const FOREGROUND_ENV_OVERRIDES = {
NO_COLOR: "1",
TERM: "dumb",
COLORTERM: "",
PAGER: "cat",
GIT_PAGER: "cat",
GH_PAGER: "cat",
GIT_EDITOR: "true",
GIT_TERMINAL_PROMPT: "0",
};
/**
* Named-key aliases the model can send via `bash_input {keys:[...]}`. Values are the
* raw byte sequences a PTY expects (control chars + xterm cursor/function escapes).
*/
const KEY_SEQUENCES = {
enter: "\r",
return: "\r",
tab: "\t",
escape: "\x1b",
esc: "\x1b",
space: " ",
backspace: "\x7f",
delete: "\x1b[3~",
up: "\x1b[A",
down: "\x1b[B",
right: "\x1b[C",
left: "\x1b[D",
home: "\x1b[H",
end: "\x1b[F",
pageup: "\x1b[5~",
pagedown: "\x1b[6~",
"ctrl+c": "\x03",
"ctrl+d": "\x04",
"ctrl+z": "\x1a",
"ctrl+l": "\x0c",
"ctrl+u": "\x15",
"ctrl+a": "\x01",
"ctrl+e": "\x05",
"ctrl+\\": "\x1c",
};
/** Resolve a single named key to its PTY byte sequence, or null when unknown. */
export function resolveKeySequence(key) {
const normalized = key.trim().toLowerCase();
if (normalized.length === 0)
return null;
const mapped = KEY_SEQUENCES[normalized];
if (mapped !== undefined)
return mapped;
// Single printable character passes through verbatim.
if ([...normalized].length === 1)
return key;
return null;
}
/** Build a PTY-ready payload from a list of named keys, returning the unknown keys too. */
export function encodeKeys(keys) {
let data = "";
const unknown = [];
for (const key of keys) {
const sequence = resolveKeySequence(key);
if (sequence === null)
unknown.push(key);
else
data += sequence;
}
return { data, unknown };
}
/** Compile a user-supplied regex, tolerating invalid patterns by returning null. */
export function safeRegExp(pattern) {
try {
return new RegExp(pattern);
}
catch {
return null;
}
}
//# sourceMappingURL=shared.js.map