agent-afk 3.67.0 → 3.67.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.
Files changed (2) hide show
  1. package/dist/cli.mjs +185 -185
  2. package/package.json +1 -1
package/dist/cli.mjs CHANGED
@@ -1,58 +1,58 @@
1
1
  #!/usr/bin/env node
2
2
  var TA=Object.defineProperty;var xA=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>(typeof require<"u"?require:t)[n]}):e)(function(e){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+e+'" is not supported')});var rt=(e,t)=>()=>(e&&(t=e(e=0)),t);var Mi=(e,t)=>{for(var n in t)TA(e,n,{get:t[n],enumerable:!0})};function Pf(e){return If.filter(t=>!t.required||e!==void 0&&t.category!==e?!1:process.env[t.name]===void 0||process.env[t.name]==="")}var If,T,G=rt(()=>{"use strict";If=[{name:"AFK_COMPACT_KEEP_LAST_TURNS",description:"Number of recent turns the compactor keeps verbatim during /compact. Default tuned in compact-handler.ts.",type:"number",required:!1,example:"6",category:"model"},{name:"AFK_COMPACT_MODEL",description:"Override the model used by the /compact summarizer. Falls back to a cheap default (haiku-class).",type:"string",required:!1,example:"claude-haiku-4-5",category:"model"},{name:"AFK_DEFAULT_SUBAGENT_MODEL",description:"Override the default model used when a subagent is dispatched without an explicit model.",type:"string",required:!1,example:"sonnet",category:"model"},{name:"AFK_DISABLE_PROMPT_CACHE",description:"Disable Anthropic prompt caching when set to 1/true/yes/on. Unset = caching enabled.",type:"boolean",required:!1,default:"0",example:"1",category:"model"},{name:"AFK_EFFORT",description:"Reasoning-effort hint for the Codex provider. Accepts low | medium | high.",type:"string",required:!1,example:"medium",category:"model"},{name:"AFK_MAX_BUDGET_USD",description:"Per-turn USD budget ceiling. Aborts the turn when projected spend would exceed this.",type:"number",required:!1,default:"5.00",example:"10.00",category:"model"},{name:"AFK_MAX_OUTPUT_TOKENS",description:"Cap on output tokens per turn. Falls back to provider default when unset.",type:"number",required:!1,example:"8192",category:"model"},{name:"AFK_MAX_TOKENS",description:"Cap on total tokens per turn (input + output). Default 4096.",type:"number",required:!1,default:"4096",example:"8192",category:"model"},{name:"AFK_MODEL",description:"Default model for agent turns. Accepts short aliases (opus, sonnet, haiku) or full model IDs.",type:"string",required:!1,default:"sonnet",example:"claude-opus-4-5",category:"model"},{name:"AFK_PROMPT_CACHE_TTL",description:"TTL for Anthropic prompt-cache blocks. Accepts 5m or 1h.",type:"string",required:!1,default:"1h",example:"1h",category:"model"},{name:"AFK_TASK_BUDGET",description:"Per-task token budget ceiling. Aborts when cumulative usage would exceed it.",type:"number",required:!1,default:"100000",example:"200000",category:"model"},{name:"AFK_TEMPERATURE",description:"Numeric temperature override for model sampling. Provider default if unset.",type:"number",required:!1,example:"0.7",category:"model"},{name:"AFK_THINKING",description:"Extended-thinking toggle. Accepts on | off | <budget-tokens>. On by default.",type:"string",required:!1,default:"on",example:"on",category:"model"},{name:"AFK_TIMEOUT_MS",description:"Per-turn timeout in milliseconds. Provider/SDK default if unset.",type:"number",required:!1,example:"120000",category:"model"},{name:"CLAUDE_MODEL",description:"Legacy alias for AFK_MODEL \u2014 supported for back-compat with pre-AFK_* deployments.",type:"string",required:!1,example:"sonnet",category:"model"},{name:"AFK_SYSTEM_PROMPT",description:"Raw system-prompt string. Tier-1 source (highest priority over afk.config.json and AFK.md).",type:"string",required:!1,example:"You are a helpful agent.",category:"model"},{name:"AFK_DUMP_PROMPT",description:"Write the resolved system prompt to a file at startup. Accepts a path or 1 for default location.",type:"string",required:!1,example:"/tmp/afk-prompt.txt",category:"debug"},{name:"ANTHROPIC_API_KEY",description:"Anthropic API key. Tier-1 credential \u2014 overrides keychain OAuth and CLAUDE_CODE_OAUTH_TOKEN.",type:"string",required:!1,category:"auth",secret:!0},{name:"CLAUDE_CODE_OAUTH_TOKEN",description:"Claude Code OAuth token. Tier-2 credential \u2014 used when ANTHROPIC_API_KEY is unset; falls back to keychain.",type:"string",required:!1,category:"auth",secret:!0},{name:"OPENAI_API_KEY",description:"OpenAI API key for the openai-compatible provider (gpt-*, o1*, o3*, o4* models).",type:"string",required:!1,category:"auth",secret:!0},{name:"CODEX_API_KEY",description:"Codex API key for the @openai/codex-sdk provider when AFK_MODEL=codex-*.",type:"string",required:!1,category:"auth",secret:!0},{name:"AFK_LOCAL_API_KEY",description:"Placeholder API key for local Anthropic-compatible servers (vllm-mlx, etc.). Set when AFK_LOCAL_BASE_URL is configured.",type:"string",required:!1,default:"local",example:"local",category:"auth",secret:!0},{name:"AFK_LOCAL_BASE_URL",description:"Base URL for a self-hosted Anthropic-compatible server. When set, routes traffic away from api.anthropic.com.",type:"string",required:!1,example:"http://127.0.0.1:8080",category:"model"},{name:"AFK_OPENAI_BASE_URL",description:"Base URL override for the OpenAI-compatible provider. Used for local shims (mlx_lm.server, Ollama, vLLM, LM Studio). The OpenAI SDK appends `/chat/completions` itself \u2014 a value ending in `/chat/completions` will be stripped at config-load time with a one-shot warning.",type:"string",required:!1,example:"http://127.0.0.1:8000/v1",category:"model"},{name:"AFK_PROVIDER",description:"Force provider selection (anthropic | anthropic-direct | openai | openai-compatible | openai-codex). Overrides the model-name heuristic. Same surface as the --provider CLI flag; CLI flag wins when both are set.",type:"string",required:!1,example:"openai-compatible",category:"model"},{name:"TELEGRAM_BOT_TOKEN",description:"Telegram bot token from @BotFather. Required to run the Telegram bot surface.",type:"string",required:!1,category:"telegram",secret:!0},{name:"AFK_TELEGRAM_BOT_TOKEN",description:"Alternative env var name for the Telegram bot token, accepted by the setup wizard.",type:"string",required:!1,category:"telegram",secret:!0},{name:"AFK_TELEGRAM_ALLOWED_CHAT_IDS",description:"Comma-separated list of Telegram chat IDs allowed to interact with the bot. Required when the bot is running.",type:"string",required:!1,example:"123456789,987654321",category:"telegram"},{name:"TELEGRAM_DATA_DIR",description:"Override the directory where Telegram bot state is stored. Defaults to ~/.afk/state/telegram/.",type:"string",required:!1,category:"telegram"},{name:"TELEGRAM_VERBOSE",description:"Set to 1 to log per-message details from the Telegram bot \u2014 chat IDs, message text, latency.",type:"boolean",required:!1,example:"1",category:"telegram"},{name:"AFK_TELEGRAM_TRACE",description:"Set to 1 to dump raw bridge traffic between the agent and the Telegram bot \u2014 debugging only.",type:"boolean",required:!1,example:"1",category:"debug"},{name:"AFK_TELEGRAM_CWD",description:"Override the working directory used by the Telegram bot when spawning agent sessions.",type:"string",required:!1,category:"telegram"},{name:"AFK_HOME",description:"Override the AFK home directory. Default: ~/.afk/.",type:"string",required:!1,default:"~/.afk",example:"/opt/afk",category:"paths"},{name:"AFK_STATE_DIR",description:"Override the AFK state directory. Default: $AFK_HOME/state/.",type:"string",required:!1,category:"paths"},{name:"AFK_FRAMEWORK_DIR",description:"Override the AFK agent-framework directory used for telemetry and briefs. Default: $AFK_HOME/agent-framework/.",type:"string",required:!1,category:"paths"},{name:"AFK_EVAL_HARNESS_ROOT",description:"Root path for the forge evaluation harness. Used by the L1/L2 capability-gate checks.",type:"string",required:!1,category:"paths"},{name:"HOME",description:"Standard Unix home directory. Used as the fallback when AFK_HOME is unset.",type:"string",required:!1,category:"process"},{name:"PATH",description:"System PATH. Read for executable resolution (git, gh, etc.) in tool handlers.",type:"string",required:!1,category:"process"},{name:"AFK_DAEMON_CWD",description:"Working directory used by the daemon process for spawned agent sessions.",type:"string",required:!1,category:"daemon"},{name:"AFK_DAEMON_TASK",description:"Default task description for the daemon. Falls back to afk.config.json daemon.task.",type:"string",required:!1,category:"daemon"},{name:"AFK_DAEMON_TASK_ID",description:"Task identifier the daemon uses to scope its state directory and telemetry.",type:"string",required:!1,category:"daemon"},{name:"AFK_SESSIONSTART_COOLDOWN_MS",description:"Cooldown in milliseconds between SessionStart trigger fires in the daemon. Prevents thundering-herd on rapid restarts.",type:"number",required:!1,category:"daemon"},{name:"AFK_WORKTREE_AUTONAME",description:"Auto-rename worktree branches based on the first user message in interactive mode. 1 = on (default), 0 = off.",type:"boolean",required:!1,default:"1",example:"0",category:"worktree"},{name:"AFK_WORKTREE_BRANCH_PREFIX",description:"Branch-name prefix for AFK-managed worktrees. Default afk/. Set to empty string to drop the prefix.",type:"string",required:!1,default:"afk/",example:"wt/",category:"worktree"},{name:"AFK_WORKTREE_BOOT_PRUNE",description:"When set, the daemon prunes stale worktrees at boot in addition to the cron-driven sweep.",type:"boolean",required:!1,category:"worktree"},{name:"AFK_WORKTREE_PRUNE_DISABLE",description:"Disable the worktree prune job entirely. Useful for long-running tests.",type:"boolean",required:!1,category:"worktree"},{name:"AFK_WORKTREE_MAX_AGE_CLEAN",description:"Maximum age (in days) before a clean worktree is auto-pruned. Default 14.",type:"number",required:!1,default:"14",category:"worktree"},{name:"AFK_WORKTREE_MAX_AGE_DIRTY",description:"Maximum age (in days) before a dirty worktree is auto-pruned. Default 30.",type:"number",required:!1,default:"30",category:"worktree"},{name:"AFK_WORKTREE_SWEEP_ROOT",description:"Override the root directory under which AFK worktrees are tracked for pruning.",type:"string",required:!1,category:"worktree"},{name:"AFK_THREADS_ALLOWED_USERNAMES",description:"Comma-separated allowlist of Threads usernames the agent will respond to.",type:"string",required:!1,example:"alice,bob",category:"threads"},{name:"AFK_THREADS_DRY_RUN",description:"Set to 1 to log Threads replies without actually posting them.",type:"boolean",required:!1,example:"1",category:"threads"},{name:"AFK_THREADS_POLL_INTERVAL_MS",description:"Poll interval for the Threads inbox watcher in milliseconds.",type:"number",required:!1,category:"threads"},{name:"AFK_THREADS_REPLY_MODE",description:"Threads reply mode. Accepts post (default) or other modes defined in src/threads.ts.",type:"string",required:!1,default:"post",example:"post",category:"threads"},{name:"AFK_ALLOW_PROJECT_MCP",description:"Allow loading MCP configuration from <cwd>/.mcp.json. Disabled by default \u2014 opt-in to mitigate config-injection risks.",type:"boolean",required:!1,example:"1",category:"mcp"},{name:"AFK_AUTO_ROUTING",description:"Auto-route bare slash inputs to matching skills. Applies to interactive, chat, telegram, and daemon surfaces.",type:"boolean",required:!1,example:"true",category:"routing"},{name:"AFK_INTERNAL",description:'Tier gate. Set to exactly `1` to unlock \u2014 only the literal string "1" unlocks (other truthy values like "true"/"yes" leave the tier locked). When unlocked, skills tagged `audience: \'internal\'` (e.g. /forge, /audit-fit, harvest/distill plugins) become visible at end-user surfaces (slash-command list, --help, tab-complete, system-prompt skill manifest). Default unset = public tier \u2014 internal skills are hidden. Not an access-control boundary; it gates surfacing, not the underlying registry.',type:"boolean",required:!1,example:"1",category:"routing"},{name:"AFK_SHELL_PASSTHROUGH",description:"Enable the interactive REPL `!cmd` / `!&cmd` shell-passthrough feature. On by default. Set to 0, false, off, or no (case-insensitive) to disable, so inputs beginning with ! are sent to the model as literal text instead of being executed as shell commands. Equivalent to the --no-shell-passthrough flag.",type:"boolean",required:!1,default:"1",example:"0",category:"misc"},{name:"AFK_BANNER_PLAIN",description:"Suppress the ANSI-colored banner at REPL startup. Useful for non-TTY captures and CI logs.",type:"boolean",required:!1,example:"1",category:"misc"},{name:"AFK_SPINNER_TIPS",description:"Show rotating tips in the loading spinner during long calls. 1 = on, 0 = off.",type:"boolean",required:!1,category:"misc"},{name:"AFK_SHOW_DIFFS",description:"Show inline diffs in the tool-lane output for edit/write tool calls. 1 = on, 0 = off.",type:"boolean",required:!1,category:"misc"},{name:"AFK_SKILL_STREAM_VERBOSE",description:"Verbose streaming output when a skill is dispatched. Logs sub-agent setup, intermediate events, and final result.",type:"boolean",required:!1,category:"debug"},{name:"FORCE_COLOR",description:"Standard Node convention. Force-enable ANSI color output even when stdout is not a TTY.",type:"string",required:!1,example:"1",category:"process"},{name:"NO_COLOR",description:"Standard convention (https://no-color.org). When set to any non-empty value, disables ANSI color output.",type:"string",required:!1,example:"1",category:"process"},{name:"AFK_DEBUG",description:"Enable verbose debug logging across the codebase. Accepts 1 to enable.",type:"boolean",required:!1,example:"1",category:"debug"},{name:"AFK_DEBUG_CLIPBOARD",description:"Debug bracketed-paste and image-paste handling in the interactive REPL.",type:"boolean",required:!1,category:"debug"},{name:"AFK_DEBUG_COMPOSITOR",description:"Gate compositor phase-boundary traces to stderr; any truthy value enables.",type:"boolean",required:!1,category:"debug"},{name:"AFK_TRACE_DISABLED",description:"Disable the agent trace subsystem entirely. Set to 1 to skip trace file writes.",type:"boolean",required:!1,example:"1",category:"debug"},{name:"DEBUG",description:"Standard Node `debug`-package convention. When set to 1, enables verbose logging in several modules alongside AFK_DEBUG.",type:"string",required:!1,category:"debug"},{name:"AGENT_AFK_ASCII",description:"Force the interactive REPL tool-lane renderer to ASCII-only glyphs instead of the default Unicode box-drawing set. Accepts 1/true/yes (case-insensitive). Useful for terminals whose font lacks \u2503\u251C\u2570\u251C glyphs.",type:"boolean",required:!1,example:"1",category:"debug"},{name:"AGENT_SURFACE",description:"Internal surface marker propagated to subprocesses. Identifies which AFK surface (cli, telegram, daemon, threads) spawned the process.",type:"string",required:!1,example:"cli",category:"process"},{name:"CI",description:"Standard CI-detection convention. Auto-set by GitHub Actions, CircleCI, etc. Used to switch off TTY-only UX.",type:"string",required:!1,example:"true",category:"process"},{name:"NODE_ENV",description:"Standard Node environment marker. test | development | production. Used by routing-telemetry.ts to suppress test-time writes.",type:"string",required:!1,example:"production",category:"process"},{name:"VITEST",description:"Set automatically by Vitest. Used at runtime to short-circuit code paths that should not fire in tests.",type:"string",required:!1,category:"process"},{name:"NO_UPDATE_NOTIFIER",description:"Disable the update-available notifier on CLI startup. Standard convention shared with many Node CLIs.",type:"boolean",required:!1,category:"process"},{name:"AFK_BROWSER_HEADLESS",description:"Override the default headless mode for native browser-control tools. `1`/`true` forces headless; `0`/`false` forces headed. When unset the default is headless for daemon and subagent surfaces and headed for repl/interactive \u2014 so an operator can watch the agent work in REPL mode.",type:"boolean",required:!1,example:"1",category:"browser"},{name:"AFK_BROWSER_ALLOWED_DOMAINS",description:"Comma-separated allowlist of URL host globs. When set, browser_open and any navigation that targets a host outside the list returns status: blocked_by_policy. Unset means no allowlist (permissive). Patterns use simple `*` glob matching against the URL host. Combines with AFK_BROWSER_BLOCKED_DOMAINS \u2014 block wins.",type:"string",required:!1,example:"github.com,*.atlassian.net",category:"browser"},{name:"AFK_BROWSER_BLOCKED_DOMAINS",description:"Comma-separated blocklist of URL host globs. Browser navigation that matches any entry returns status: blocked_by_policy regardless of the allowlist.",type:"string",required:!1,example:"*.ads.example.com",category:"browser"},{name:"AFK_BROWSER_DOM_SNAPSHOTS",description:"Phase 2 opt-in: when set to 1, every browser_act writes a gzipped DOM snapshot sidecar under ~/.afk/state/witness/<sid>/browser/dom-snapshots/. Off by default because snapshots are large; useful for post-mortem analysis of failed actions.",type:"boolean",required:!1,example:"1",category:"browser"},{name:"AFK_BROWSER_BACKEND",description:"Select the browser provider backend. Phase 1 supports only `playwright`. Reserved for future `cdp` / `mcp` adapters. Unset defaults to `playwright`.",type:"string",required:!1,example:"playwright",category:"browser"},{name:"AFK_BROWSER_CONFIG",description:"Absolute path to an alternate browser config file. Overrides the default ~/.afk/config/browser.json lookup. Useful for per-project overrides in CI.",type:"string",required:!1,example:"/path/to/browser.json",category:"browser"},{name:"AFK_WRITE_DENYLIST",description:"Comma-separated list of additional path globs that the write_file tool refuses to write to.",type:"string",required:!1,example:"**/.env,**/secrets/**",category:"misc"},{name:"AFK_WRITE_DIFF",description:"Show a diff preview before each write_file tool call. Defaults provider-controlled when unset.",type:"boolean",required:!1,category:"misc"},{name:"AFK_DEMO_CLEAN",description:"Explicit opt-in to capture-mode. When set to 1, suppresses high-frequency repaint drivers (spinner ticker, live thinking-preview) so recorded artifacts contain each state once instead of once per timer tick.",type:"boolean",required:!1,example:"1",category:"misc"},{name:"SCRIPT",description:"Set by script(1) on BSD/macOS/Linux to the typescript filename while a terminal session is being recorded. Presence of a non-empty value triggers capture-mode.",type:"string",required:!1,example:"/tmp/typescript",category:"process"},{name:"ASCIINEMA_REC",description:"Set to 1 by asciinema rec while a session is being recorded. Triggers capture-mode.",type:"boolean",required:!1,example:"1",category:"process"},{name:"AFK_SESSION_ID",description:"Override the browser session ID used by the native browser-control tools. Defaults to 'default' for single-session use. Subagents inherit the parent's session by default. Set this when running multiple concurrent AFK processes that should each manage an isolated browser context.",type:"string",required:!1,default:"default",example:"session-abc123",category:"browser"},{name:"SHELL",description:"Standard POSIX env var pointing to the user's login shell binary. Used by shell-init and worktree commands to auto-detect the correct shell syntax for emitted wrapper code.",type:"string",required:!1,example:"/bin/zsh",category:"process"},{name:"PAGER",description:"Standard POSIX env var naming the user's preferred pager (with optional flags). Used by /transcript to render the full session in a scrollable viewer; falls back to `less -R` when unset.",type:"string",required:!1,example:"less -R",category:"process"},{name:"AFK_DIFF_LINES",description:"Maximum number of diff lines shown in the inline diff render during write_file tool calls. Set to 0 for no cap. Non-integer values are silently ignored and the default applies.",type:"number",required:!1,example:"50",category:"misc"},{name:"AFK_SHELL_WRAPPER",description:"Set to 1 or true by the optional afk shell wrapper function (installed via `afk shell-init`). Signals that the parent shell has the wrapper active so the post-exit cd can fire.",type:"boolean",required:!1,example:"1",category:"process"},{name:"AFK_USER_CARD_MAX_ROWS",description:'Maximum number of visual rows emitted by renderUserCard before collapsing the remainder into a dim "\u2026(N lines collapsed)" summary row. Defaults to 24. Non-integer or non-positive values are silently ignored and the default applies.',type:"number",required:!1,example:"24",category:"misc"}],T={get AFK_COMPACT_KEEP_LAST_TURNS(){return process.env.AFK_COMPACT_KEEP_LAST_TURNS},get AFK_COMPACT_MODEL(){return process.env.AFK_COMPACT_MODEL},get AFK_DEFAULT_SUBAGENT_MODEL(){return process.env.AFK_DEFAULT_SUBAGENT_MODEL},get AFK_DISABLE_PROMPT_CACHE(){return process.env.AFK_DISABLE_PROMPT_CACHE},get AFK_EFFORT(){return process.env.AFK_EFFORT},get AFK_MAX_BUDGET_USD(){return process.env.AFK_MAX_BUDGET_USD},get AFK_MAX_OUTPUT_TOKENS(){return process.env.AFK_MAX_OUTPUT_TOKENS},get AFK_MAX_TOKENS(){return process.env.AFK_MAX_TOKENS},get AFK_MODEL(){return process.env.AFK_MODEL},get AFK_PROMPT_CACHE_TTL(){return process.env.AFK_PROMPT_CACHE_TTL},get AFK_TASK_BUDGET(){return process.env.AFK_TASK_BUDGET},get AFK_TEMPERATURE(){return process.env.AFK_TEMPERATURE},get AFK_THINKING(){return process.env.AFK_THINKING},get AFK_TIMEOUT_MS(){return process.env.AFK_TIMEOUT_MS},get CLAUDE_MODEL(){return process.env.CLAUDE_MODEL},get AFK_SYSTEM_PROMPT(){return process.env.AFK_SYSTEM_PROMPT},get AFK_DUMP_PROMPT(){return process.env.AFK_DUMP_PROMPT},get ANTHROPIC_API_KEY(){return process.env.ANTHROPIC_API_KEY},get CLAUDE_CODE_OAUTH_TOKEN(){return process.env.CLAUDE_CODE_OAUTH_TOKEN},get OPENAI_API_KEY(){return process.env.OPENAI_API_KEY},get CODEX_API_KEY(){return process.env.CODEX_API_KEY},get AFK_LOCAL_API_KEY(){return process.env.AFK_LOCAL_API_KEY},get AFK_LOCAL_BASE_URL(){return process.env.AFK_LOCAL_BASE_URL},get AFK_OPENAI_BASE_URL(){return process.env.AFK_OPENAI_BASE_URL},get AFK_PROVIDER(){return process.env.AFK_PROVIDER},get TELEGRAM_BOT_TOKEN(){return process.env.TELEGRAM_BOT_TOKEN},get AFK_TELEGRAM_BOT_TOKEN(){return process.env.AFK_TELEGRAM_BOT_TOKEN},get AFK_TELEGRAM_ALLOWED_CHAT_IDS(){return process.env.AFK_TELEGRAM_ALLOWED_CHAT_IDS},get TELEGRAM_DATA_DIR(){return process.env.TELEGRAM_DATA_DIR},get TELEGRAM_VERBOSE(){return process.env.TELEGRAM_VERBOSE},get AFK_TELEGRAM_TRACE(){return process.env.AFK_TELEGRAM_TRACE},get AFK_TELEGRAM_CWD(){return process.env.AFK_TELEGRAM_CWD},get AFK_HOME(){return process.env.AFK_HOME},get AFK_STATE_DIR(){return process.env.AFK_STATE_DIR},get AFK_FRAMEWORK_DIR(){return process.env.AFK_FRAMEWORK_DIR},get AFK_EVAL_HARNESS_ROOT(){return process.env.AFK_EVAL_HARNESS_ROOT},get HOME(){return process.env.HOME},get PATH(){return process.env.PATH},get AFK_DAEMON_CWD(){return process.env.AFK_DAEMON_CWD},get AFK_DAEMON_TASK(){return process.env.AFK_DAEMON_TASK},get AFK_DAEMON_TASK_ID(){return process.env.AFK_DAEMON_TASK_ID},get AFK_SESSIONSTART_COOLDOWN_MS(){return process.env.AFK_SESSIONSTART_COOLDOWN_MS},get AFK_WORKTREE_AUTONAME(){return process.env.AFK_WORKTREE_AUTONAME},get AFK_WORKTREE_BRANCH_PREFIX(){return process.env.AFK_WORKTREE_BRANCH_PREFIX},get AFK_WORKTREE_BOOT_PRUNE(){return process.env.AFK_WORKTREE_BOOT_PRUNE},get AFK_WORKTREE_PRUNE_DISABLE(){return process.env.AFK_WORKTREE_PRUNE_DISABLE},get AFK_WORKTREE_MAX_AGE_CLEAN(){return process.env.AFK_WORKTREE_MAX_AGE_CLEAN},get AFK_WORKTREE_MAX_AGE_DIRTY(){return process.env.AFK_WORKTREE_MAX_AGE_DIRTY},get AFK_WORKTREE_SWEEP_ROOT(){return process.env.AFK_WORKTREE_SWEEP_ROOT},get AFK_THREADS_ALLOWED_USERNAMES(){return process.env.AFK_THREADS_ALLOWED_USERNAMES},get AFK_THREADS_DRY_RUN(){return process.env.AFK_THREADS_DRY_RUN},get AFK_THREADS_POLL_INTERVAL_MS(){return process.env.AFK_THREADS_POLL_INTERVAL_MS},get AFK_THREADS_REPLY_MODE(){return process.env.AFK_THREADS_REPLY_MODE},get AFK_ALLOW_PROJECT_MCP(){return process.env.AFK_ALLOW_PROJECT_MCP},get AFK_AUTO_ROUTING(){return process.env.AFK_AUTO_ROUTING},get AFK_INTERNAL(){return process.env.AFK_INTERNAL},get AFK_SHELL_PASSTHROUGH(){return process.env.AFK_SHELL_PASSTHROUGH},get AFK_BANNER_PLAIN(){return process.env.AFK_BANNER_PLAIN},get AFK_SPINNER_TIPS(){return process.env.AFK_SPINNER_TIPS},get AFK_SHOW_DIFFS(){return process.env.AFK_SHOW_DIFFS},get AFK_SKILL_STREAM_VERBOSE(){return process.env.AFK_SKILL_STREAM_VERBOSE},get FORCE_COLOR(){return process.env.FORCE_COLOR},get NO_COLOR(){return process.env.NO_COLOR},get AFK_DEBUG(){return process.env.AFK_DEBUG},get AFK_DEBUG_CLIPBOARD(){return process.env.AFK_DEBUG_CLIPBOARD},get AFK_DEBUG_COMPOSITOR(){return process.env.AFK_DEBUG_COMPOSITOR},get AFK_TRACE_DISABLED(){return process.env.AFK_TRACE_DISABLED},get DEBUG(){return process.env.DEBUG},get AGENT_AFK_ASCII(){return process.env.AGENT_AFK_ASCII},get AGENT_SURFACE(){return process.env.AGENT_SURFACE},get CI(){return process.env.CI},get NODE_ENV(){return process.env.NODE_ENV},get VITEST(){return process.env.VITEST},get NO_UPDATE_NOTIFIER(){return process.env.NO_UPDATE_NOTIFIER},get AFK_SESSION_ID(){return process.env.AFK_SESSION_ID},get AFK_BROWSER_HEADLESS(){return process.env.AFK_BROWSER_HEADLESS},get AFK_BROWSER_ALLOWED_DOMAINS(){return process.env.AFK_BROWSER_ALLOWED_DOMAINS},get AFK_BROWSER_BLOCKED_DOMAINS(){return process.env.AFK_BROWSER_BLOCKED_DOMAINS},get AFK_BROWSER_DOM_SNAPSHOTS(){return process.env.AFK_BROWSER_DOM_SNAPSHOTS},get AFK_BROWSER_BACKEND(){return process.env.AFK_BROWSER_BACKEND},get AFK_BROWSER_CONFIG(){return process.env.AFK_BROWSER_CONFIG},get AFK_WRITE_DENYLIST(){return process.env.AFK_WRITE_DENYLIST},get AFK_WRITE_DIFF(){return process.env.AFK_WRITE_DIFF},get AFK_DEMO_CLEAN(){return process.env.AFK_DEMO_CLEAN},get SCRIPT(){return process.env.SCRIPT},get ASCIINEMA_REC(){return process.env.ASCIINEMA_REC},get SHELL(){return process.env.SHELL},get PAGER(){return process.env.PAGER},get AFK_DIFF_LINES(){return process.env.AFK_DIFF_LINES},get AFK_SHELL_WRAPPER(){return process.env.AFK_SHELL_WRAPPER},get AFK_USER_CARD_MAX_ROWS(){return process.env.AFK_USER_CARD_MAX_ROWS}};(function(){for(let t of If){if(!t.secret)continue;let n=Object.getOwnPropertyDescriptor(T,t.name);n&&Object.defineProperty(T,t.name,{...n,enumerable:!1})}})()});import{existsSync as Mf,mkdirSync as EA,renameSync as RA,cpSync as AA,rmSync as _A}from"fs";import{join as Z,dirname as $f,isAbsolute as CA}from"path";import{homedir as ou}from"os";import{fileURLToPath as IA}from"url";function Ce(){let e=T.AFK_HOME;if(e!==void 0&&e!==""){if(!CA(e)||e==="/")throw new Error(`AFK_HOME must be an absolute path that is not /, got: ${e}`);return e}return Z(ou(),".afk")}function Tt(){return Z(Ce(),"agent-framework")}function $t(){return Z(Tt(),"forge-telemetry.jsonl")}function Sn(){return Z(Tt(),"briefs")}function Oi(){return Z(Tt(),"ceiling-ledger")}function Xn(){return Z(Ce(),"skills")}function Oe(){return Z(Ce(),"plugins")}function Df(){return Z(process.cwd(),".afk")}function Lf(){return Z(Df(),"skills")}function os(){return Z(Df(),"plugins")}function ie(){return Z(Oe(),".index.json")}function su(){return Z(Xt(),"schedules.json")}function Yt(){return Z(Oe(),"cache")}function ss(e){return Z(Yt(),e)}function is(){let e=IA(import.meta.url),t=$f(e);return Z(t,"bundled-plugins")}function Xt(){return Z(Ce(),"config")}function ye(){return Z(Ce(),"state")}function $i(){return Z(Ce(),"cache")}function kn(){return Z(Ce(),"logs")}function vn(){return Z(ye(),"sessions")}function iu(){return Z(ye(),"presence")}function au(){return Z(ye(),"todos")}function Di(){return Z(ye(),"memory")}function Dt(){return Z(ye(),"queue")}function Fr(){return Z(ye(),"session-grants.jsonl")}function Ff(){return Z(Ce(),"farms")}function lu(e){return Z(Ff(),e)}function MA(e){if(!PA.test(e))throw new Error(`Invalid AFK_SESSION_ID: must match /^[a-zA-Z0-9_-]+$/, got: ${JSON.stringify(e)}`)}function Li(e){return MA(e),Z(ye(),"witness",e)}function Nr(e="default"){return Z(ye(),"daemon",`agent-afk@${e}`)}function Nf(){return Z(ye(),"worktree-sweep.lock")}function ot(){return Z(Xt(),"afk.env")}function Fi(){return Z(Xt(),"afk.config.json")}function Bf(){return Z(ou(),".afk.env")}function Uf(){return Z(ou(),".afk.config.json")}function OA(){return Z(Ce(),"sessions")}function $A(){return Z(Ce(),"todos")}function jf(e,t){if(e!==t&&Mf(e)&&!Mf(t))try{EA($f(t),{recursive:!0});try{RA(e,t)}catch(n){if(n.code==="EXDEV")try{AA(e,t,{recursive:!0}),_A(e,{recursive:!0,force:!0})}catch(r){process.stderr.write(`[afk] migrateDirOnce: EXDEV fallback failed for ${e} \u2192 ${t}: ${String(r)}
3
- `)}}}catch{}}function Wf(){jf(OA(),vn())}function Hf(){jf($A(),au())}function cu(){return Z(ye(),"repl-history.jsonl")}function LA(e){if(typeof e!="string"||e.length===0)throw new Error("Invalid jobId: must be a non-empty string");if(e.length>Of)throw new Error(`Invalid jobId: exceeds ${Of} chars`);if(!DA.test(e))throw new Error(`Invalid jobId: ${JSON.stringify(e)} contains characters outside [A-Za-z0-9_-]`)}function as(){return Z(ye(),"bg")}function Zn(e){return LA(e),Z(as(),e)}function Ni(e){return Z(Zn(e),"events.jsonl")}function uu(e){return Z(Zn(e),"meta.json")}function ls(){return Z(ye(),"mcp","server-status.json")}var PA,DA,Of,W=rt(()=>{"use strict";G();PA=/^[a-zA-Z0-9_-]+$/;DA=/^[A-Za-z0-9_-]+$/,Of=128});function sa(e,t=()=>{}){let n=new Set;if(!e)return n;for(let r of e.split(",")){let o=r.trim();if(o){if(!/^-?\d+$/.test(o)){t("[allowlist] Ignoring non-numeric chat ID:",o);continue}n.add(Number(o))}}return n}var Uu=rt(()=>{"use strict"});var ju={};Mi(ju,{push:()=>ia,pushIfConfigured:()=>aa});async function ia(e){if(!e.token)throw new Error("push: token is required");if(e.chatId===""||e.chatId==null||e.chatId===0)throw new Error("push: chatId is required");let t=e.fetchImpl??fetch,r=`${e.apiBase??EI}/bot${e.token}/sendMessage`,o={chat_id:e.chatId,text:e.text.slice(0,4096)};e.parseMode&&(o.parse_mode=e.parseMode),e.replyMarkup&&(o.reply_markup=e.replyMarkup);let s=new AbortController,i=setTimeout(()=>s.abort(),1e4);try{let a=await t(r,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(o),signal:s.signal});if(a.ok)return{ok:!0,status:a.status};let l;try{l=(await a.json()).description}catch{l=`HTTP ${a.status}`}return{ok:!1,status:a.status,...l!==void 0?{errorMessage:l}:{}}}catch(a){return{ok:!1,status:0,errorMessage:a instanceof Error?a.message:String(a)}}finally{clearTimeout(i)}}async function aa(e,t={}){let n=T.TELEGRAM_BOT_TOKEN;if(!n)return null;let r=sa(T.AFK_TELEGRAM_ALLOWED_CHAT_IDS);if(r.size===0)return null;let o=[];for(let s of r)o.push(await ia({token:n,chatId:s,text:e,...t.parseMode!==void 0?{parseMode:t.parseMode}:{},...t.replyMarkup!==void 0?{replyMarkup:t.replyMarkup}:{},...t.fetchImpl!==void 0?{fetchImpl:t.fetchImpl}:{}}));return o}var EI,ws=rt(()=>{"use strict";Uu();G();EI="https://api.telegram.org"});import{join as eP}from"path";function tP(e){let n=e.replace(/[.+?()[\]{}/\\^$|]/g,"\\$&").replace(/\*/g,"[^.]*");return new RegExp(`^${n}$`,"i")}function yh(e,t){return tP(t).test(e)}function oP(e,t){if(e!==void 0){let n=e.trim().toLowerCase();if(n==="1"||n==="true"||n==="yes")return!0;if(n==="0"||n==="false"||n==="no")return!1}if(t!==void 0){if(nP.has(t))return!0;if(rP.has(t))return!1}return!1}function bh(e){return e===void 0||e.trim()===""?[]:e.split(",").map(t=>t.trim().toLowerCase()).filter(t=>t.length>0)}function sP(e){if(e===void 0||e===""||e==="playwright")return"playwright";throw new Error(`AFK_BROWSER_BACKEND: only "playwright" is supported in Phase 1, got: ${e}`)}function iP(e){if(e===void 0)return!1;let t=e.trim().toLowerCase();return t==="1"||t==="true"||t==="yes"}function aP(e){try{return xA("fs").readFileSync(e,"utf8")}catch(t){if(t.code==="ENOENT")return;throw t}}function lP(e,t){let n={...e};if(typeof t.headless=="boolean"&&(n.headless=t.headless),Array.isArray(t.allowedDomains)&&(n.allowedDomains=t.allowedDomains.filter(r=>typeof r=="string").map(r=>r.trim().toLowerCase()).filter(r=>r.length>0)),Array.isArray(t.blockedDomains)&&(n.blockedDomains=t.blockedDomains.filter(r=>typeof r=="string").map(r=>r.trim().toLowerCase()).filter(r=>r.length>0)),typeof t.domSnapshots=="boolean"&&(n.domSnapshots=t.domSnapshots),t.backend==="playwright")n.backend="playwright";else if(t.backend!==void 0)throw new Error(`AFK_BROWSER_BACKEND: only "playwright" is supported in Phase 1, got: ${String(t.backend)}`);return n}function wh(e){let t=e?.env??T,n=e?.readFileSync??aP,r=e?.surface??t.AGENT_SURFACE,o=oP(t.AFK_BROWSER_HEADLESS,r),s=bh(t.AFK_BROWSER_ALLOWED_DOMAINS),i=bh(t.AFK_BROWSER_BLOCKED_DOMAINS),a=iP(t.AFK_BROWSER_DOM_SNAPSHOTS),l=sP(t.AFK_BROWSER_BACKEND),c={headless:o,allowedDomains:s,blockedDomains:i,domSnapshots:a,backend:l,configPath:null},u=t.AFK_BROWSER_CONFIG,d=u!==void 0&&u.trim()!==""?u.trim():eP(Xt(),"browser.json"),m=n(d);if(m===void 0)return c;let f;try{f=JSON.parse(m)}catch(h){throw new Error(`Failed to parse browser config at ${d}: ${String(h)}`)}if(typeof f!="object"||f===null||Array.isArray(f))throw new Error(`Browser config at ${d} must be a JSON object`);let g=lP(c,f);return g.configPath=d,g}function Yu(e,t){let n;try{n=new URL(e).hostname.toLowerCase()}catch{return{allowed:!1,reason:`invalid URL: ${e}`}}for(let r of t.blockedDomains)if(yh(n,r))return{allowed:!1,reason:`blocked by AFK_BROWSER_BLOCKED_DOMAINS: ${r}`};return t.allowedDomains.length>0&&!t.allowedDomains.some(o=>yh(n,o))?{allowed:!1,reason:"not in AFK_BROWSER_ALLOWED_DOMAINS"}:{allowed:!0}}var nP,rP,Xu=rt(()=>{"use strict";G();W();nP=new Set(["daemon","subagent","threads","telegram"]),rP=new Set(["repl","interactive","cli"])});import cP from"node:fs";import uP from"node:path";import{chromium as dP}from"playwright";function pP(){try{let e=uP.resolve(import.meta.dirname,"../../../package.json"),t=cP.readFileSync(e,"utf8"),n=JSON.parse(t);return typeof n.version=="string"?n.version:"unknown"}catch{return"unknown"}}var mP,ua,Sh=rt(()=>{"use strict";mP=pP(),ua=class{config;browser;sessions=new Map;launchPromise;shutdownComplete=!1;constructor(t){this.config=t}async ensureBrowser(){return this.browser!==void 0&&this.browser.isConnected()?this.browser:(this.browser!==void 0&&!this.browser.isConnected()&&(this.browser=void 0,this.launchPromise=void 0),this.launchPromise!==void 0?this.launchPromise:(this.launchPromise=dP.launch({headless:this.config.headless}).then(t=>(this.browser=t,this.launchPromise=void 0,t)).catch(t=>{throw this.launchPromise=void 0,t}),this.launchPromise))}isBrowserActive(){return this.browser!==void 0&&this.browser.isConnected()}async ensureContext(t){let n=this.sessions.get(t);if(n!==void 0)return n.context;let o=await(await this.ensureBrowser()).newContext({viewport:{width:1280,height:800},userAgent:`Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 agent-afk/${mP}`}),s={context:o,page:void 0,consoleErrors:0,lastHttpStatus:null,openDialog:void 0};return this.sessions.set(t,s),o}async ensurePage(t){let n=this.sessions.get(t);if(n!==void 0&&n.page!==void 0)return n.page;await this.ensureContext(t);let r=this.sessions.get(t);if(r===void 0)throw new Error(`[BrowserLauncher] session entry disappeared for sessionId=${t}`);if(r.page!==void 0)return r.page;let o=await r.context.newPage();return r.page=o,o.on("console",s=>{s.type()==="error"&&(r.consoleErrors+=1)}),o.on("request",s=>{s.isNavigationRequest()&&s.frame()===o.mainFrame()&&(r.lastHttpStatus=null)}),o.on("response",s=>{s.frame()===o.mainFrame()&&s.request().isNavigationRequest()&&(r.lastHttpStatus=s.status())}),o.on("dialog",s=>{r.openDialog=s}),o}getPage(t){return this.sessions.get(t)?.page}getConsoleErrorCount(t){return this.sessions.get(t)?.consoleErrors??0}getLastHttpStatus(t){return this.sessions.get(t)?.lastHttpStatus??null}hasOpenDialog(t){return this.sessions.get(t)?.openDialog!==void 0}async dismissDialog(t,n=!0){let r=this.sessions.get(t);if(r===void 0||r.openDialog===void 0)return;let o=r.openDialog;r.openDialog=void 0,n?await o.accept():await o.dismiss()}async closeSession(t){let n=this.sessions.get(t);n!==void 0&&(this.sessions.delete(t),n.page!==void 0&&await n.page.close().catch(()=>{}),await n.context.close().catch(()=>{}))}async shutdown(){if(this.shutdownComplete)return;this.shutdownComplete=!0;let t=[...this.sessions.keys()];if(await Promise.all(t.map(n=>this.closeSession(n))),this.browser!==void 0){let n=this.browser;this.browser=void 0,await n.close().catch(()=>{})}}activeSessions(){return this.sessions.size}}});import{createHash as fP}from"crypto";function Zu(e){if(e.length===0)return e;let t=e;for(let{regex:n,name:r}of gP)r==="form-password"?t=t.replace(n,"password=[redacted]"):t=t.replace(n,"[redacted]");return t}function kh(e){return!!(e.role==="textbox"&&e.kind==="password"||e.label&&hP.test(e.label))}function vh(e){return fP("sha256").update(e,"utf8").digest("hex").slice(0,8)}function Th(e){let t=e.replace(/\s+/g," ").trim();return t.length<=80?t:t.slice(0,77)+"..."}var gP,hP,ks=rt(()=>{"use strict";gP=[{name:"aws-access-key",regex:/AKIA[0-9A-Z]{16}/g},{name:"github-pat",regex:/ghp_[a-zA-Z0-9]{36}/g},{name:"openai-bearer",regex:/sk-[a-zA-Z0-9_-]{20,}/g},{name:"slack-token",regex:/xox[abp]-[a-zA-Z0-9-]{10,}/g},{name:"jwt",regex:/eyJ[a-zA-Z0-9_-]{20,}\.[a-zA-Z0-9_-]{20,}\.[a-zA-Z0-9_-]{20,}/g},{name:"form-password",regex:/password=[^&\s]+/gi}];hP=/password|secret|token|api[_-]?key|otp|2fa/i});import{createHash as yP}from"node:crypto";function bP(e){return e?e.replace(/\s+/g," ").trim().slice(0,200):""}function wP(e,t,n){return`el_${yP("sha256").update(`${e}:${t}:${n}`).digest("hex").slice(0,6)}`}function SP(e){let t=e.replace(/\s+/g," ").trim(),n=4e3;return t.length<=n?t:t.slice(0,n)+"\u2026[truncated]"}function xh(e){return e.replace(/\s+/g," ").trim().toLowerCase().slice(0,100)}function Rh(e,t){let n=e.role??"",r=e.name??"";Eh.has(n)&&(n!=="searchbox"&&n!=="spinbutton"||r!=="")&&t.push(e);for(let s of e.children??[])Rh(s,t)}async function kP(e){return e.evaluate(t=>{let n=Array.from(document.querySelectorAll(t)),r=[];for(let o of n){let s=o.getBoundingClientRect(),i=o;if(s.width===0&&s.height===0){let u=window.getComputedStyle(i);if(u.display==="none"||u.visibility==="hidden")continue}let a=o.tagName.toLowerCase(),l=o.getAttribute("aria-label")??o.getAttribute("placeholder")??(o.textContent??"").replace(/\s+/g," ").trim().slice(0,100),c=a==="input"?o.type||null:o.getAttribute("type");r.push({name:l,tagName:a,type:c,id:o.id||null,testId:o.getAttribute("data-testid"),bbox:{x:Math.round(s.left),y:Math.round(s.top),w:Math.round(s.width),h:Math.round(s.height)}})}return r},Ah).catch(()=>[])}async function vP(e){return e.evaluate(t=>{let n={button:"button",a:"link",input:"textbox",textarea:"textbox",select:"combobox"},r=Array.from(document.querySelectorAll(t)),o=[];for(let s of r){let i=s.tagName.toLowerCase(),a=s.getAttribute("role")??"",l=s.getAttribute("aria-label")??s.getAttribute("placeholder")??(s.textContent??"").replace(/\s+/g," ").trim().slice(0,100),c=a||(n[i]??"");if(i==="input"){let h=s.type;h==="checkbox"?c="checkbox":h==="radio"?c="radio":h==="button"||h==="submit"||h==="reset"?c="button":h==="search"?c="searchbox":c="textbox"}if(!c)continue;let u="value"in s?s.value:void 0,d=u!==void 0?String(u):void 0,m=s.disabled??!1,f=i==="input"?s.checked:void 0,g={role:c,name:l,disabled:m};d!==void 0&&(g.value=d),f!==void 0&&(g.checked=f),o.push(g)}return o},Ah).catch(()=>[])}function TP(e){let n=e.accessibility;return n!==null&&typeof n=="object"?n:null}async function da(e,t){let n=t.maxElements??80,r=t.includeHidden??!1,o=[],s=TP(e),i=s?s.snapshot({interestingOnly:!1}).catch(()=>null):Promise.resolve(null),a=kP(e),l=e.evaluate(()=>document.body?.innerText??"").catch(()=>""),c=Promise.resolve(e.url()),u=e.title().catch(()=>""),[d,m,f,g,h]=await Promise.all([i,a,l,c,u]),b,y=!1;d!==null?(b=[],Rh(d,b)):(o.push("observation skipped accessibility tree (returned null)"),y=!0,b=(await vP(e)).filter($=>Eh.has($.role??"")));let w=new Map;for(let P of m){let $=xh(P.name),F=w.get($);(!F||F.bbox.w===0&&P.bbox.w>0)&&w.set($,P)}let x=b.map(P=>({ax:P,dom:w.get(xh(P.name??""))})),R=r?x:x.filter(P=>P.dom?P.dom.bbox.w>0||P.dom.bbox.h>0:!0);R.sort((P,$)=>{let F=P.dom?.bbox.y??0,k=$.dom?.bbox.y??0;if(F!==k)return F-k;let A=P.dom?.bbox.x??0,N=$.dom?.bbox.x??0;return A-N}),R.length>200&&o.push("page has 200+ interactive elements; consider scoping");let E=R.slice(0,n).map((P,$)=>{let F=P.ax.role??"generic",k=P.ax.name??"",A=wP(F,k,$),N=P.dom?.bbox??{x:0,y:0,w:0,h:0},H=P.dom?.type??null,C=null;P.ax.value!==void 0&&P.ax.value!==null&&(C=String(P.ax.value)),P.ax.checked!==void 0&&(C=String(P.ax.checked)),kh({role:F,kind:H})&&(C="[redacted]");let B={disabled:P.ax.disabled??!1};P.ax.checked!==void 0&&(B.checked=P.ax.checked===!0||P.ax.checked==="mixed"),P.ax.selected!==void 0&&(B.selected=P.ax.selected),P.ax.expanded!==void 0&&(B.expanded=P.ax.expanded);let j;P.dom?.testId?j=`[data-testid="${P.dom.testId}"]`:P.dom?.id&&(j=`#${P.dom.id}`);let ee={id:A,role:F,label:bP(k),kind:H,value:C,state:B,bbox:N};return j!==void 0&&(ee.selector=j),ee}),I="idle";try{let P=await e.evaluate(()=>document.readyState);P==="loading"?I="loading":P==="interactive"?I="navigating":I="idle"}catch{I="navigating"}I!=="idle"&&o.push("page is still loading \u2014 observation may be incomplete"),y&&!o.includes("observation skipped accessibility tree (returned null)")&&o.push("observation skipped accessibility tree (returned null)");let D=SP(f),_=`obs_${t.observationCounter.toString(36)}`,M=new Date().toISOString();return{observationId:_,url:g,title:h,textSummary:D,interactive:E,status:{httpStatus:t.httpStatus??null,loadingState:I,hasDialog:t.hasDialog??!1,consoleErrors:t.consoleErrors??0},warnings:o,screenshotPath:t.screenshotPath??null,capturedAt:M}}var Eh,Ah,_h=rt(()=>{"use strict";ks();Eh=new Set(["button","link","textbox","combobox","checkbox","radio","tab","menuitem","menuitemcheckbox","menuitemradio","switch","option","searchbox","spinbutton"]);Ah="a[href], button, input, select, textarea, [role], [tabindex], label"});async function Ch(e,t){try{let n=await e.nth(t).evaluate(i=>{let a=i,l=a.getAttribute("role")??a.tagName.toLowerCase(),c=a.getAttribute("aria-label")??a.getAttribute("placeholder")??(a.innerText!=null?a.innerText.trim().slice(0,200):"")??a.getAttribute("title")??"",u=a.getBoundingClientRect();return{role:l,label:c,x:Math.round(u.x),y:Math.round(u.y),w:Math.round(u.width),h:Math.round(u.height)}}),r=`${n.role}:${n.label}:${t}`,o=0;for(let i=0;i<r.length;i++)o=o*31+r.charCodeAt(i)>>>0;return{id:`el_${o.toString(16).padStart(6,"0").slice(0,6)}`,role:n.role,label:n.label,kind:null,value:null,state:{disabled:!1},bbox:{x:n.x,y:n.y,w:n.w,h:n.h}}}catch{return null}}async function Qu(e,t){let n=Math.min(t,5);return(await Promise.all(Array.from({length:n},(o,s)=>Ch(e,s)))).filter(o=>o!==null)}async function xP(e){let t=new Set,n=[];for(let{loc:r,count:o}of e)for(let s=0;s<o;s++){let i;try{i=await r.nth(s).evaluate(a=>{let l=a,c=l.getBoundingClientRect();return`${l.tagName}@${Math.round(c.x)},${Math.round(c.y)}`})}catch{continue}t.has(i)||(t.add(i),n.push({key:i,locator:r,index:s}))}return n}async function ed(e,t,n){switch(t.kind){case"element_id":return EP(e,t,n);case"selector":return RP(e,t);case"semantic":return AP(e,t)}}async function EP(e,t,n){let r=n.get(t.elementId);if(r===void 0)return{outcome:"not_found",query:t};if(r.selector!==void 0){let l=e.locator(r.selector);if(await l.count()===1)return{outcome:"resolved",locator:l}}let o=e.getByRole(r.role,{name:r.label,exact:!0}),s=await o.count();if(s===0)return{outcome:"not_found",query:t};if(s===1)return{outcome:"resolved",locator:o};let i=await Qu(o,s);return{outcome:"ambiguous_target",query:{text:r.label,role:r.role},candidates:i}}async function RP(e,t){let n=e.locator(t.selector),r=await n.count();if(r===0)return{outcome:"not_found",query:t};if(r===1)return{outcome:"resolved",locator:n};let o=await Qu(n,r);return{outcome:"ambiguous_target",query:{text:`[selector: ${t.selector}]`},candidates:o}}async function AP(e,t){return t.role!==void 0?_P(e,t.text,t.role):CP(e,t.text,t)}async function _P(e,t,n){let r=e.getByRole(n,{name:t}),o=await r.count();if(o===0)return{outcome:"not_found",query:{kind:"semantic",text:t,role:n}};if(o===1)return{outcome:"resolved",locator:r};let s=await Qu(r,o);return{outcome:"ambiguous_target",query:{text:t,role:n},candidates:s}}async function CP(e,t,n){let r=e.getByRole("button",{name:t}),o=e.getByRole("link",{name:t}),s=e.getByLabel(t,{exact:!1}),[i,a,l]=await Promise.all([r.count(),o.count(),s.count()]);if(i+a+l===0)return{outcome:"not_found",query:n};let u=[];i>0&&u.push({loc:r,count:i}),a>0&&u.push({loc:o,count:a}),l>0&&u.push({loc:s,count:l});let d=await xP(u);if(d.length===0)return{outcome:"not_found",query:n};if(d.length===1){let h=d[0];return h===void 0?{outcome:"not_found",query:n}:{outcome:"resolved",locator:h.locator.nth(h.index)}}let m=d.slice(0,5),f=[];for(let h=0;h<m.length;h++){let b=m[h];if(b===void 0)continue;let y=await Ch(b.locator,b.index);if(y!==null){let w=`${y.role}:${y.label}:${h}`,x=0;for(let R=0;R<w.length;R++)x=x*31+w.charCodeAt(R)>>>0;f.push({...y,id:`el_${x.toString(16).padStart(6,"0").slice(0,6)}`})}}return{outcome:"ambiguous_target",query:{text:t},candidates:f}}var Ih=rt(()=>{"use strict"});import{randomBytes as IP}from"crypto";import{mkdir as PP,stat as MP,writeFile as OP}from"fs/promises";import{join as td}from"path";import{gzip as $P}from"zlib";import{promisify as DP}from"util";function LP(e){return td(Li(e),"browser")}function FP(e){return td(LP(e),"screenshots")}function NP(){return new Date().toISOString().replace(/[:.]/g,"-")}function BP(){return IP(3).toString("hex")}async function nd(e,t,n){if(t.length>Ph)throw new Error(`writeScreenshotSidecar: buffer exceeds ${Ph} byte cap (received ${t.length} bytes). Refusing to write oversized screenshot.`);let r=FP(e);await PP(r,{recursive:!0});let o=`${NP()}-${BP()}-${n}.png`,s=td(r,o);await OP(s,t);let{size:i}=await MP(s);return{path:s,bytes:i}}var G8,Ph,Mh=rt(()=>{"use strict";W();ks();G8=DP($P);Ph=5*1024*1024});var $h={};Mi($h,{PlaywrightProvider:()=>rd});function Oh(e){switch(e.kind){case"semantic":return e.role!==void 0?`semantic('${e.text}', role='${e.role}')`:`semantic('${e.text}')`;case"element_id":return`element_id(${e.elementId})`;case"selector":return`selector(${e.selector})`}}var rd,Dh=rt(()=>{"use strict";Sh();_h();Ih();Xu();ks();Mh();rd=class{name="playwright";config;launcher;sessions=new Map;constructor(t){this.config=t,this.launcher=new ua(t)}async open(t){let n=Yu(t.url,this.config);if(!n.allowed)return{outcome:"blocked_by_policy",url:t.url,reason:n.reason};let{sessionId:r}=t,o=await this.launcher.ensurePage(r),s=this.ensureSessionState(r),i=null,a=null;try{await o.goto(t.url,{timeout:t.timeoutMs??3e4,waitUntil:t.waitFor??"load"})}catch(c){a=c}(t.screenshot===!0||a!==null)&&(i=await this.captureScreenshot(o,r,"browser_open")),s.observationCounter+=1;let l=await da(o,{observationCounter:s.observationCounter,screenshotPath:i,consoleErrors:this.launcher.getConsoleErrorCount(r),httpStatus:this.launcher.getLastHttpStatus(r),hasDialog:this.launcher.hasOpenDialog(r)});if(this.updateSessionFromObservation(s,l.interactive,l.url,l.title,"browser_open"),a!==null)throw a;return l}async observe(t){let{sessionId:n}=t,r=this.launcher.getPage(n);if(r===void 0)throw new Error(`browser_observe: no page open for session ${n}`);let o=this.ensureSessionState(n),s=null;t.screenshot===!0&&(s=await this.captureScreenshot(r,n,"browser_observe")),o.observationCounter+=1;let i=await da(r,{observationCounter:o.observationCounter,screenshotPath:s,consoleErrors:this.launcher.getConsoleErrorCount(n),httpStatus:this.launcher.getLastHttpStatus(n),hasDialog:this.launcher.hasOpenDialog(n),includeHidden:t.includeHidden,maxElements:t.maxElements});return this.updateSessionFromObservation(o,i.interactive,i.url,i.title,"browser_observe"),i}async act(t){let{sessionId:n}=t,r=this.launcher.getPage(n);if(r===void 0)throw new Error(`browser_act: no page open for session ${n}`);let o=this.ensureSessionState(n),s=r.url(),i=t.timeoutMs??3e4,a=await ed(r,t.target,o.knownElements);if(a.outcome==="not_found")throw new Error(`browser_act: target not found: ${Oh(t.target)}`);if(a.outcome==="ambiguous_target")return a;let{locator:l}=a,c=null,u=async()=>{switch(t.action){case"click":await l.click({timeout:i});break;case"fill":{let h=Zu(t.value??"");await l.fill(t.value??"");break}case"press":await l.press(t.value??"");break;case"select":await l.selectOption(t.value??"");break;case"hover":await l.hover({timeout:i});break;case"scroll_to":await l.scrollIntoViewIfNeeded({timeout:i});break;case"wait_for":await l.waitFor({timeout:i,state:"visible"});break}};try{await u()}catch(h){if(h instanceof Error&&/navigation|net::ERR/i.test(h.message))try{await u()}catch(b){c=b}else c=h}let d=r.url();if(d!==s){let h=Yu(d,this.config);if(!h.allowed)return await r.goBack().catch(()=>{}),{outcome:"blocked_by_policy",url:d,reason:h.reason}}let m=null;(t.screenshot===!0||c!==null)&&(m=await this.captureScreenshot(r,n,"browser_act")),o.observationCounter+=1;let f=await da(r,{observationCounter:o.observationCounter,screenshotPath:m,consoleErrors:this.launcher.getConsoleErrorCount(n),httpStatus:this.launcher.getLastHttpStatus(n),hasDialog:this.launcher.hasOpenDialog(n)}),g=`browser_act:${t.action}`;if(this.updateSessionFromObservation(o,f.interactive,f.url,f.title,g),c!==null)throw c;return f}async screenshot(t){let{sessionId:n}=t,r=this.launcher.getPage(n);if(r===void 0)throw new Error(`browser_screenshot: no page open for session ${n}`);let o=this.ensureSessionState(n),s;if(t.target!==void 0){let u=await ed(r,t.target,o.knownElements);if(u.outcome==="not_found")throw new Error(`browser_screenshot: target not found: ${Oh(t.target)}`);if(u.outcome==="ambiguous_target")throw new Error("screenshot target ambiguous; specify element_id or selector");s=await u.locator.screenshot()}else s=await r.screenshot({fullPage:t.fullPage??!1});let{path:i,bytes:a}=await nd(n,s,"browser_screenshot"),l=0,c=0;if(t.fullPage===!0)try{let u=await r.evaluate(()=>({w:document.documentElement.scrollWidth,h:document.documentElement.scrollHeight}));l=u.w,c=u.h}catch{let u=r.viewportSize();l=u?.width??0,c=u?.height??0}else{let u=r.viewportSize();l=u?.width??0,c=u?.height??0}return{path:i,bytes:a,width:l,height:c}}async extract(t){throw new Error("browser_extract not implemented in Phase 1")}async close(t){await this.launcher.closeSession(t.sessionId),this.sessions.delete(t.sessionId)}describe(t){let n=this.sessions.get(t);if(n===void 0)return null;let r=this.launcher.getPage(t);return{active:r!==void 0,url:n.currentUrl,title:n.currentTitle,lastAction:n.lastAction,lastActionAt:n.lastActionAt,openTabs:r!==void 0?1:0}}async shutdown(){this.sessions.clear(),await this.launcher.shutdown()}ensureSessionState(t){let n=this.sessions.get(t);if(n!==void 0)return n;let r={observationCounter:0,knownElements:new Map,lastAction:null,lastActionAt:null,currentUrl:null,currentTitle:null};return this.sessions.set(t,r),r}updateSessionFromObservation(t,n,r,o,s){t.knownElements=new Map(n.map(i=>[i.id,i])),t.currentUrl=r,t.currentTitle=o,t.lastAction=s,t.lastActionAt=new Date().toISOString()}async captureScreenshot(t,n,r){try{let o=await t.screenshot({fullPage:!1}),{path:s}=await nd(n,o,r);return s}catch{return null}}}});var Yr={};Mi(Yr,{__resetBrowserRegistryForTests:()=>KP,browserProviderActive:()=>WP,closeBrowserProvider:()=>od,getBrowserProvider:()=>jP,peekBrowserProvider:()=>HP});function Lh(){Promise.resolve(od()).then(()=>{process.exit(130)})}function Fh(){Promise.resolve(od()).then(()=>{process.exit(143)})}function Nh(){Ut=null}function UP(){pa||(process.on("SIGINT",Lh),process.on("SIGTERM",Fh),process.on("exit",Nh),pa=!0)}function Bh(){pa&&(process.removeListener("SIGINT",Lh),process.removeListener("SIGTERM",Fh),process.removeListener("exit",Nh),pa=!1)}async function jP(e){return Ut!==null?Ut:(ir!==null||(ir=(async()=>{let{PlaywrightProvider:t}=await Promise.resolve().then(()=>(Dh(),$h)),n=wh(e),r=new t(n);return UP(),Ut=r,ir=null,r})()),ir)}async function od(){if(Ut===null)return;let e=Ut;Ut=null,ir=null,Bh(),await e.shutdown()}function WP(){return Ut!==null}function HP(){return Ut}function KP(){Ut=null,ir=null,Bh()}var Ut,ir,pa,Xr=rt(()=>{"use strict";Xu();Ut=null,ir=null,pa=!1});var BS={};Mi(BS,{KeychainOAuthProvider:()=>Co,clearOauthPending:()=>cp,readOauthPending:()=>NS});import{existsSync as ll,mkdirSync as LS,readFileSync as cl,writeFileSync as lp}from"node:fs";import{execFileSync as MS}from"node:child_process";import{homedir as OS,userInfo as $S}from"node:os";import{join as DS,dirname as FS}from"node:path";function OF(){let e=process.platform==="darwin",t=process.platform==="linux";return{read(){if(e)try{return MS("security",["find-generic-password","-s","Claude Code-credentials","-a",$S().username,"-w"],{stdio:["ignore","pipe","ignore"],encoding:"utf-8"}).trim()||void 0}catch{return}if(t){let n=DS(OS(),".claude",".credentials.json");if(!ll(n))return;try{return cl(n,"utf-8")}catch{return}}},write(n){if(e)MS("security",["add-generic-password","-U","-s","Claude Code-credentials","-a",$S().username,"-w",n],{stdio:["ignore","ignore","ignore"]});else if(t){let r=DS(OS(),".claude",".credentials.json");LS(FS(r),{recursive:!0}),lp(r,n,{encoding:"utf-8",mode:384})}}}}function NS(){let e=ls();if(!ll(e))return{};let t;try{t=JSON.parse(cl(e,"utf-8"))}catch{return{}}if(t===null||typeof t!="object")return{};let n={};for(let[r,o]of Object.entries(t)){if(o===null||typeof o!="object")continue;let s=o;s.status==="oauth_pending"&&typeof s.authorizationUrl=="string"&&typeof s.timestamp=="number"&&Date.now()-s.timestamp<=MF&&(n[r]={status:"oauth_pending",authorizationUrl:s.authorizationUrl,timestamp:s.timestamp})}return n}function cp(e){let t=ls();if(!ll(t))return;let n;try{n=JSON.parse(cl(t,"utf-8"))}catch{return}e in n&&(delete n[e],lp(t,JSON.stringify(n,null,2),{encoding:"utf-8",mode:384}))}function $F(e,t){let n=ls();LS(FS(n),{recursive:!0});let r={};if(ll(n))try{r=JSON.parse(cl(n,"utf-8"))}catch{}let o=new URL(t),s=o.origin+o.pathname;r[e]={status:"oauth_pending",authorizationUrl:s,timestamp:Date.now()},lp(n,JSON.stringify(r,null,2),{encoding:"utf-8",mode:384})}var MF,Co,ul=rt(()=>{"use strict";W();MF=600*1e3;Co=class{serverName;backend;constructor(t,n=OF()){this.serverName=t,this.backend=n}get redirectUrl(){return"http://localhost:3000/oauth/callback"}get clientMetadata(){return{redirect_uris:[this.redirectUrl],client_name:"agent-afk",grant_types:["authorization_code","refresh_token"],response_types:["code"],token_endpoint_auth_method:"none"}}clientInformation(){return this._readSlot().clientInfo}saveClientInformation(t){this._updateSlot(n=>({...n,clientInfo:t}))}tokens(){return this._readSlot().tokens}saveTokens(t){this._updateSlot(n=>({...n,tokens:t}));try{cp(this.serverName)}catch{}}saveCodeVerifier(t){this._updateSlot(n=>({...n,codeVerifier:t}))}codeVerifier(){let t=this._readSlot().codeVerifier;if(!t)throw new Error(`[mcp:${this.serverName}] no PKCE code verifier stored`);return t}saveDiscoveryState(t){this._updateSlot(n=>({...n,discoveryState:t}))}discoveryState(){return this._readSlot().discoveryState}invalidateCredentials(t){this._updateSlot(n=>{if(t==="all")return{};let r={...n};return t==="client"&&delete r.clientInfo,t==="tokens"&&delete r.tokens,t==="verifier"&&delete r.codeVerifier,t==="discovery"&&delete r.discoveryState,r})}async redirectToAuthorization(t){let n=t.toString(),r=`\u{1F510} MCP server "${this.serverName}" requires authorization.
3
+ `)}}}catch{}}function Wf(){jf(OA(),vn())}function Hf(){jf($A(),au())}function cu(){return Z(ye(),"repl-history.jsonl")}function LA(e){if(typeof e!="string"||e.length===0)throw new Error("Invalid jobId: must be a non-empty string");if(e.length>Of)throw new Error(`Invalid jobId: exceeds ${Of} chars`);if(!DA.test(e))throw new Error(`Invalid jobId: ${JSON.stringify(e)} contains characters outside [A-Za-z0-9_-]`)}function as(){return Z(ye(),"bg")}function Zn(e){return LA(e),Z(as(),e)}function Ni(e){return Z(Zn(e),"events.jsonl")}function uu(e){return Z(Zn(e),"meta.json")}function ls(){return Z(ye(),"mcp","server-status.json")}var PA,DA,Of,W=rt(()=>{"use strict";G();PA=/^[a-zA-Z0-9_-]+$/;DA=/^[A-Za-z0-9_-]+$/,Of=128});function sa(e,t=()=>{}){let n=new Set;if(!e)return n;for(let r of e.split(",")){let o=r.trim();if(o){if(!/^-?\d+$/.test(o)){t("[allowlist] Ignoring non-numeric chat ID:",o);continue}n.add(Number(o))}}return n}var Uu=rt(()=>{"use strict"});var ju={};Mi(ju,{push:()=>ia,pushIfConfigured:()=>aa});async function ia(e){if(!e.token)throw new Error("push: token is required");if(e.chatId===""||e.chatId==null||e.chatId===0)throw new Error("push: chatId is required");let t=e.fetchImpl??fetch,r=`${e.apiBase??EI}/bot${e.token}/sendMessage`,o={chat_id:e.chatId,text:e.text.slice(0,4096)};e.parseMode&&(o.parse_mode=e.parseMode),e.replyMarkup&&(o.reply_markup=e.replyMarkup);let s=new AbortController,i=setTimeout(()=>s.abort(),1e4);try{let a=await t(r,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(o),signal:s.signal});if(a.ok)return{ok:!0,status:a.status};let l;try{l=(await a.json()).description}catch{l=`HTTP ${a.status}`}return{ok:!1,status:a.status,...l!==void 0?{errorMessage:l}:{}}}catch(a){return{ok:!1,status:0,errorMessage:a instanceof Error?a.message:String(a)}}finally{clearTimeout(i)}}async function aa(e,t={}){let n=T.TELEGRAM_BOT_TOKEN;if(!n)return null;let r=sa(T.AFK_TELEGRAM_ALLOWED_CHAT_IDS);if(r.size===0)return null;let o=[];for(let s of r)o.push(await ia({token:n,chatId:s,text:e,...t.parseMode!==void 0?{parseMode:t.parseMode}:{},...t.replyMarkup!==void 0?{replyMarkup:t.replyMarkup}:{},...t.fetchImpl!==void 0?{fetchImpl:t.fetchImpl}:{}}));return o}var EI,ws=rt(()=>{"use strict";Uu();G();EI="https://api.telegram.org"});import{join as eP}from"path";function tP(e){let n=e.replace(/[.+?()[\]{}/\\^$|]/g,"\\$&").replace(/\*/g,"[^.]*");return new RegExp(`^${n}$`,"i")}function yh(e,t){return tP(t).test(e)}function oP(e,t){if(e!==void 0){let n=e.trim().toLowerCase();if(n==="1"||n==="true"||n==="yes")return!0;if(n==="0"||n==="false"||n==="no")return!1}if(t!==void 0){if(nP.has(t))return!0;if(rP.has(t))return!1}return!1}function bh(e){return e===void 0||e.trim()===""?[]:e.split(",").map(t=>t.trim().toLowerCase()).filter(t=>t.length>0)}function sP(e){if(e===void 0||e===""||e==="playwright")return"playwright";throw new Error(`AFK_BROWSER_BACKEND: only "playwright" is supported in Phase 1, got: ${e}`)}function iP(e){if(e===void 0)return!1;let t=e.trim().toLowerCase();return t==="1"||t==="true"||t==="yes"}function aP(e){try{return xA("fs").readFileSync(e,"utf8")}catch(t){if(t.code==="ENOENT")return;throw t}}function lP(e,t){let n={...e};if(typeof t.headless=="boolean"&&(n.headless=t.headless),Array.isArray(t.allowedDomains)&&(n.allowedDomains=t.allowedDomains.filter(r=>typeof r=="string").map(r=>r.trim().toLowerCase()).filter(r=>r.length>0)),Array.isArray(t.blockedDomains)&&(n.blockedDomains=t.blockedDomains.filter(r=>typeof r=="string").map(r=>r.trim().toLowerCase()).filter(r=>r.length>0)),typeof t.domSnapshots=="boolean"&&(n.domSnapshots=t.domSnapshots),t.backend==="playwright")n.backend="playwright";else if(t.backend!==void 0)throw new Error(`AFK_BROWSER_BACKEND: only "playwright" is supported in Phase 1, got: ${String(t.backend)}`);return n}function wh(e){let t=e?.env??T,n=e?.readFileSync??aP,r=e?.surface??t.AGENT_SURFACE,o=oP(t.AFK_BROWSER_HEADLESS,r),s=bh(t.AFK_BROWSER_ALLOWED_DOMAINS),i=bh(t.AFK_BROWSER_BLOCKED_DOMAINS),a=iP(t.AFK_BROWSER_DOM_SNAPSHOTS),l=sP(t.AFK_BROWSER_BACKEND),c={headless:o,allowedDomains:s,blockedDomains:i,domSnapshots:a,backend:l,configPath:null},u=t.AFK_BROWSER_CONFIG,d=u!==void 0&&u.trim()!==""?u.trim():eP(Xt(),"browser.json"),p=n(d);if(p===void 0)return c;let f;try{f=JSON.parse(p)}catch(h){throw new Error(`Failed to parse browser config at ${d}: ${String(h)}`)}if(typeof f!="object"||f===null||Array.isArray(f))throw new Error(`Browser config at ${d} must be a JSON object`);let g=lP(c,f);return g.configPath=d,g}function Yu(e,t){let n;try{n=new URL(e).hostname.toLowerCase()}catch{return{allowed:!1,reason:`invalid URL: ${e}`}}for(let r of t.blockedDomains)if(yh(n,r))return{allowed:!1,reason:`blocked by AFK_BROWSER_BLOCKED_DOMAINS: ${r}`};return t.allowedDomains.length>0&&!t.allowedDomains.some(o=>yh(n,o))?{allowed:!1,reason:"not in AFK_BROWSER_ALLOWED_DOMAINS"}:{allowed:!0}}var nP,rP,Xu=rt(()=>{"use strict";G();W();nP=new Set(["daemon","subagent","threads","telegram"]),rP=new Set(["repl","interactive","cli"])});import cP from"node:fs";import uP from"node:path";import{chromium as dP}from"playwright";function pP(){try{let e=uP.resolve(import.meta.dirname,"../../../package.json"),t=cP.readFileSync(e,"utf8"),n=JSON.parse(t);return typeof n.version=="string"?n.version:"unknown"}catch{return"unknown"}}var mP,ua,Sh=rt(()=>{"use strict";mP=pP(),ua=class{config;browser;sessions=new Map;launchPromise;shutdownComplete=!1;constructor(t){this.config=t}async ensureBrowser(){return this.browser!==void 0&&this.browser.isConnected()?this.browser:(this.browser!==void 0&&!this.browser.isConnected()&&(this.browser=void 0,this.launchPromise=void 0),this.launchPromise!==void 0?this.launchPromise:(this.launchPromise=dP.launch({headless:this.config.headless}).then(t=>(this.browser=t,this.launchPromise=void 0,t)).catch(t=>{throw this.launchPromise=void 0,t}),this.launchPromise))}isBrowserActive(){return this.browser!==void 0&&this.browser.isConnected()}async ensureContext(t){let n=this.sessions.get(t);if(n!==void 0)return n.context;let o=await(await this.ensureBrowser()).newContext({viewport:{width:1280,height:800},userAgent:`Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 agent-afk/${mP}`}),s={context:o,page:void 0,consoleErrors:0,lastHttpStatus:null,openDialog:void 0};return this.sessions.set(t,s),o}async ensurePage(t){let n=this.sessions.get(t);if(n!==void 0&&n.page!==void 0)return n.page;await this.ensureContext(t);let r=this.sessions.get(t);if(r===void 0)throw new Error(`[BrowserLauncher] session entry disappeared for sessionId=${t}`);if(r.page!==void 0)return r.page;let o=await r.context.newPage();return r.page=o,o.on("console",s=>{s.type()==="error"&&(r.consoleErrors+=1)}),o.on("request",s=>{s.isNavigationRequest()&&s.frame()===o.mainFrame()&&(r.lastHttpStatus=null)}),o.on("response",s=>{s.frame()===o.mainFrame()&&s.request().isNavigationRequest()&&(r.lastHttpStatus=s.status())}),o.on("dialog",s=>{r.openDialog=s}),o}getPage(t){return this.sessions.get(t)?.page}getConsoleErrorCount(t){return this.sessions.get(t)?.consoleErrors??0}getLastHttpStatus(t){return this.sessions.get(t)?.lastHttpStatus??null}hasOpenDialog(t){return this.sessions.get(t)?.openDialog!==void 0}async dismissDialog(t,n=!0){let r=this.sessions.get(t);if(r===void 0||r.openDialog===void 0)return;let o=r.openDialog;r.openDialog=void 0,n?await o.accept():await o.dismiss()}async closeSession(t){let n=this.sessions.get(t);n!==void 0&&(this.sessions.delete(t),n.page!==void 0&&await n.page.close().catch(()=>{}),await n.context.close().catch(()=>{}))}async shutdown(){if(this.shutdownComplete)return;this.shutdownComplete=!0;let t=[...this.sessions.keys()];if(await Promise.all(t.map(n=>this.closeSession(n))),this.browser!==void 0){let n=this.browser;this.browser=void 0,await n.close().catch(()=>{})}}activeSessions(){return this.sessions.size}}});import{createHash as fP}from"crypto";function Zu(e){if(e.length===0)return e;let t=e;for(let{regex:n,name:r}of gP)r==="form-password"?t=t.replace(n,"password=[redacted]"):t=t.replace(n,"[redacted]");return t}function kh(e){return!!(e.role==="textbox"&&e.kind==="password"||e.label&&hP.test(e.label))}function vh(e){return fP("sha256").update(e,"utf8").digest("hex").slice(0,8)}function Th(e){let t=e.replace(/\s+/g," ").trim();return t.length<=80?t:t.slice(0,77)+"..."}var gP,hP,ks=rt(()=>{"use strict";gP=[{name:"aws-access-key",regex:/AKIA[0-9A-Z]{16}/g},{name:"github-pat",regex:/ghp_[a-zA-Z0-9]{36}/g},{name:"openai-bearer",regex:/sk-[a-zA-Z0-9_-]{20,}/g},{name:"slack-token",regex:/xox[abp]-[a-zA-Z0-9-]{10,}/g},{name:"jwt",regex:/eyJ[a-zA-Z0-9_-]{20,}\.[a-zA-Z0-9_-]{20,}\.[a-zA-Z0-9_-]{20,}/g},{name:"form-password",regex:/password=[^&\s]+/gi}];hP=/password|secret|token|api[_-]?key|otp|2fa/i});import{createHash as yP}from"node:crypto";function bP(e){return e?e.replace(/\s+/g," ").trim().slice(0,200):""}function wP(e,t,n){return`el_${yP("sha256").update(`${e}:${t}:${n}`).digest("hex").slice(0,6)}`}function SP(e){let t=e.replace(/\s+/g," ").trim(),n=4e3;return t.length<=n?t:t.slice(0,n)+"\u2026[truncated]"}function xh(e){return e.replace(/\s+/g," ").trim().toLowerCase().slice(0,100)}function Rh(e,t){let n=e.role??"",r=e.name??"";Eh.has(n)&&(n!=="searchbox"&&n!=="spinbutton"||r!=="")&&t.push(e);for(let s of e.children??[])Rh(s,t)}async function kP(e){return e.evaluate(t=>{let n=Array.from(document.querySelectorAll(t)),r=[];for(let o of n){let s=o.getBoundingClientRect(),i=o;if(s.width===0&&s.height===0){let u=window.getComputedStyle(i);if(u.display==="none"||u.visibility==="hidden")continue}let a=o.tagName.toLowerCase(),l=o.getAttribute("aria-label")??o.getAttribute("placeholder")??(o.textContent??"").replace(/\s+/g," ").trim().slice(0,100),c=a==="input"?o.type||null:o.getAttribute("type");r.push({name:l,tagName:a,type:c,id:o.id||null,testId:o.getAttribute("data-testid"),bbox:{x:Math.round(s.left),y:Math.round(s.top),w:Math.round(s.width),h:Math.round(s.height)}})}return r},Ah).catch(()=>[])}async function vP(e){return e.evaluate(t=>{let n={button:"button",a:"link",input:"textbox",textarea:"textbox",select:"combobox"},r=Array.from(document.querySelectorAll(t)),o=[];for(let s of r){let i=s.tagName.toLowerCase(),a=s.getAttribute("role")??"",l=s.getAttribute("aria-label")??s.getAttribute("placeholder")??(s.textContent??"").replace(/\s+/g," ").trim().slice(0,100),c=a||(n[i]??"");if(i==="input"){let h=s.type;h==="checkbox"?c="checkbox":h==="radio"?c="radio":h==="button"||h==="submit"||h==="reset"?c="button":h==="search"?c="searchbox":c="textbox"}if(!c)continue;let u="value"in s?s.value:void 0,d=u!==void 0?String(u):void 0,p=s.disabled??!1,f=i==="input"?s.checked:void 0,g={role:c,name:l,disabled:p};d!==void 0&&(g.value=d),f!==void 0&&(g.checked=f),o.push(g)}return o},Ah).catch(()=>[])}function TP(e){let n=e.accessibility;return n!==null&&typeof n=="object"?n:null}async function da(e,t){let n=t.maxElements??80,r=t.includeHidden??!1,o=[],s=TP(e),i=s?s.snapshot({interestingOnly:!1}).catch(()=>null):Promise.resolve(null),a=kP(e),l=e.evaluate(()=>document.body?.innerText??"").catch(()=>""),c=Promise.resolve(e.url()),u=e.title().catch(()=>""),[d,p,f,g,h]=await Promise.all([i,a,l,c,u]),b,y=!1;d!==null?(b=[],Rh(d,b)):(o.push("observation skipped accessibility tree (returned null)"),y=!0,b=(await vP(e)).filter($=>Eh.has($.role??"")));let w=new Map;for(let P of p){let $=xh(P.name),F=w.get($);(!F||F.bbox.w===0&&P.bbox.w>0)&&w.set($,P)}let x=b.map(P=>({ax:P,dom:w.get(xh(P.name??""))})),R=r?x:x.filter(P=>P.dom?P.dom.bbox.w>0||P.dom.bbox.h>0:!0);R.sort((P,$)=>{let F=P.dom?.bbox.y??0,k=$.dom?.bbox.y??0;if(F!==k)return F-k;let A=P.dom?.bbox.x??0,N=$.dom?.bbox.x??0;return A-N}),R.length>200&&o.push("page has 200+ interactive elements; consider scoping");let E=R.slice(0,n).map((P,$)=>{let F=P.ax.role??"generic",k=P.ax.name??"",A=wP(F,k,$),N=P.dom?.bbox??{x:0,y:0,w:0,h:0},H=P.dom?.type??null,C=null;P.ax.value!==void 0&&P.ax.value!==null&&(C=String(P.ax.value)),P.ax.checked!==void 0&&(C=String(P.ax.checked)),kh({role:F,kind:H})&&(C="[redacted]");let B={disabled:P.ax.disabled??!1};P.ax.checked!==void 0&&(B.checked=P.ax.checked===!0||P.ax.checked==="mixed"),P.ax.selected!==void 0&&(B.selected=P.ax.selected),P.ax.expanded!==void 0&&(B.expanded=P.ax.expanded);let j;P.dom?.testId?j=`[data-testid="${P.dom.testId}"]`:P.dom?.id&&(j=`#${P.dom.id}`);let ee={id:A,role:F,label:bP(k),kind:H,value:C,state:B,bbox:N};return j!==void 0&&(ee.selector=j),ee}),I="idle";try{let P=await e.evaluate(()=>document.readyState);P==="loading"?I="loading":P==="interactive"?I="navigating":I="idle"}catch{I="navigating"}I!=="idle"&&o.push("page is still loading \u2014 observation may be incomplete"),y&&!o.includes("observation skipped accessibility tree (returned null)")&&o.push("observation skipped accessibility tree (returned null)");let D=SP(f),_=`obs_${t.observationCounter.toString(36)}`,M=new Date().toISOString();return{observationId:_,url:g,title:h,textSummary:D,interactive:E,status:{httpStatus:t.httpStatus??null,loadingState:I,hasDialog:t.hasDialog??!1,consoleErrors:t.consoleErrors??0},warnings:o,screenshotPath:t.screenshotPath??null,capturedAt:M}}var Eh,Ah,_h=rt(()=>{"use strict";ks();Eh=new Set(["button","link","textbox","combobox","checkbox","radio","tab","menuitem","menuitemcheckbox","menuitemradio","switch","option","searchbox","spinbutton"]);Ah="a[href], button, input, select, textarea, [role], [tabindex], label"});async function Ch(e,t){try{let n=await e.nth(t).evaluate(i=>{let a=i,l=a.getAttribute("role")??a.tagName.toLowerCase(),c=a.getAttribute("aria-label")??a.getAttribute("placeholder")??(a.innerText!=null?a.innerText.trim().slice(0,200):"")??a.getAttribute("title")??"",u=a.getBoundingClientRect();return{role:l,label:c,x:Math.round(u.x),y:Math.round(u.y),w:Math.round(u.width),h:Math.round(u.height)}}),r=`${n.role}:${n.label}:${t}`,o=0;for(let i=0;i<r.length;i++)o=o*31+r.charCodeAt(i)>>>0;return{id:`el_${o.toString(16).padStart(6,"0").slice(0,6)}`,role:n.role,label:n.label,kind:null,value:null,state:{disabled:!1},bbox:{x:n.x,y:n.y,w:n.w,h:n.h}}}catch{return null}}async function Qu(e,t){let n=Math.min(t,5);return(await Promise.all(Array.from({length:n},(o,s)=>Ch(e,s)))).filter(o=>o!==null)}async function xP(e){let t=new Set,n=[];for(let{loc:r,count:o}of e)for(let s=0;s<o;s++){let i;try{i=await r.nth(s).evaluate(a=>{let l=a,c=l.getBoundingClientRect();return`${l.tagName}@${Math.round(c.x)},${Math.round(c.y)}`})}catch{continue}t.has(i)||(t.add(i),n.push({key:i,locator:r,index:s}))}return n}async function ed(e,t,n){switch(t.kind){case"element_id":return EP(e,t,n);case"selector":return RP(e,t);case"semantic":return AP(e,t)}}async function EP(e,t,n){let r=n.get(t.elementId);if(r===void 0)return{outcome:"not_found",query:t};if(r.selector!==void 0){let l=e.locator(r.selector);if(await l.count()===1)return{outcome:"resolved",locator:l}}let o=e.getByRole(r.role,{name:r.label,exact:!0}),s=await o.count();if(s===0)return{outcome:"not_found",query:t};if(s===1)return{outcome:"resolved",locator:o};let i=await Qu(o,s);return{outcome:"ambiguous_target",query:{text:r.label,role:r.role},candidates:i}}async function RP(e,t){let n=e.locator(t.selector),r=await n.count();if(r===0)return{outcome:"not_found",query:t};if(r===1)return{outcome:"resolved",locator:n};let o=await Qu(n,r);return{outcome:"ambiguous_target",query:{text:`[selector: ${t.selector}]`},candidates:o}}async function AP(e,t){return t.role!==void 0?_P(e,t.text,t.role):CP(e,t.text,t)}async function _P(e,t,n){let r=e.getByRole(n,{name:t}),o=await r.count();if(o===0)return{outcome:"not_found",query:{kind:"semantic",text:t,role:n}};if(o===1)return{outcome:"resolved",locator:r};let s=await Qu(r,o);return{outcome:"ambiguous_target",query:{text:t,role:n},candidates:s}}async function CP(e,t,n){let r=e.getByRole("button",{name:t}),o=e.getByRole("link",{name:t}),s=e.getByLabel(t,{exact:!1}),[i,a,l]=await Promise.all([r.count(),o.count(),s.count()]);if(i+a+l===0)return{outcome:"not_found",query:n};let u=[];i>0&&u.push({loc:r,count:i}),a>0&&u.push({loc:o,count:a}),l>0&&u.push({loc:s,count:l});let d=await xP(u);if(d.length===0)return{outcome:"not_found",query:n};if(d.length===1){let h=d[0];return h===void 0?{outcome:"not_found",query:n}:{outcome:"resolved",locator:h.locator.nth(h.index)}}let p=d.slice(0,5),f=[];for(let h=0;h<p.length;h++){let b=p[h];if(b===void 0)continue;let y=await Ch(b.locator,b.index);if(y!==null){let w=`${y.role}:${y.label}:${h}`,x=0;for(let R=0;R<w.length;R++)x=x*31+w.charCodeAt(R)>>>0;f.push({...y,id:`el_${x.toString(16).padStart(6,"0").slice(0,6)}`})}}return{outcome:"ambiguous_target",query:{text:t},candidates:f}}var Ih=rt(()=>{"use strict"});import{randomBytes as IP}from"crypto";import{mkdir as PP,stat as MP,writeFile as OP}from"fs/promises";import{join as td}from"path";import{gzip as $P}from"zlib";import{promisify as DP}from"util";function LP(e){return td(Li(e),"browser")}function FP(e){return td(LP(e),"screenshots")}function NP(){return new Date().toISOString().replace(/[:.]/g,"-")}function BP(){return IP(3).toString("hex")}async function nd(e,t,n){if(t.length>Ph)throw new Error(`writeScreenshotSidecar: buffer exceeds ${Ph} byte cap (received ${t.length} bytes). Refusing to write oversized screenshot.`);let r=FP(e);await PP(r,{recursive:!0});let o=`${NP()}-${BP()}-${n}.png`,s=td(r,o);await OP(s,t);let{size:i}=await MP(s);return{path:s,bytes:i}}var G8,Ph,Mh=rt(()=>{"use strict";W();ks();G8=DP($P);Ph=5*1024*1024});var $h={};Mi($h,{PlaywrightProvider:()=>rd});function Oh(e){switch(e.kind){case"semantic":return e.role!==void 0?`semantic('${e.text}', role='${e.role}')`:`semantic('${e.text}')`;case"element_id":return`element_id(${e.elementId})`;case"selector":return`selector(${e.selector})`}}var rd,Dh=rt(()=>{"use strict";Sh();_h();Ih();Xu();ks();Mh();rd=class{name="playwright";config;launcher;sessions=new Map;constructor(t){this.config=t,this.launcher=new ua(t)}async open(t){let n=Yu(t.url,this.config);if(!n.allowed)return{outcome:"blocked_by_policy",url:t.url,reason:n.reason};let{sessionId:r}=t,o=await this.launcher.ensurePage(r),s=this.ensureSessionState(r),i=null,a=null;try{await o.goto(t.url,{timeout:t.timeoutMs??3e4,waitUntil:t.waitFor??"load"})}catch(c){a=c}(t.screenshot===!0||a!==null)&&(i=await this.captureScreenshot(o,r,"browser_open")),s.observationCounter+=1;let l=await da(o,{observationCounter:s.observationCounter,screenshotPath:i,consoleErrors:this.launcher.getConsoleErrorCount(r),httpStatus:this.launcher.getLastHttpStatus(r),hasDialog:this.launcher.hasOpenDialog(r)});if(this.updateSessionFromObservation(s,l.interactive,l.url,l.title,"browser_open"),a!==null)throw a;return l}async observe(t){let{sessionId:n}=t,r=this.launcher.getPage(n);if(r===void 0)throw new Error(`browser_observe: no page open for session ${n}`);let o=this.ensureSessionState(n),s=null;t.screenshot===!0&&(s=await this.captureScreenshot(r,n,"browser_observe")),o.observationCounter+=1;let i=await da(r,{observationCounter:o.observationCounter,screenshotPath:s,consoleErrors:this.launcher.getConsoleErrorCount(n),httpStatus:this.launcher.getLastHttpStatus(n),hasDialog:this.launcher.hasOpenDialog(n),includeHidden:t.includeHidden,maxElements:t.maxElements});return this.updateSessionFromObservation(o,i.interactive,i.url,i.title,"browser_observe"),i}async act(t){let{sessionId:n}=t,r=this.launcher.getPage(n);if(r===void 0)throw new Error(`browser_act: no page open for session ${n}`);let o=this.ensureSessionState(n),s=r.url(),i=t.timeoutMs??3e4,a=await ed(r,t.target,o.knownElements);if(a.outcome==="not_found")throw new Error(`browser_act: target not found: ${Oh(t.target)}`);if(a.outcome==="ambiguous_target")return a;let{locator:l}=a,c=null,u=async()=>{switch(t.action){case"click":await l.click({timeout:i});break;case"fill":{let h=Zu(t.value??"");await l.fill(t.value??"");break}case"press":await l.press(t.value??"");break;case"select":await l.selectOption(t.value??"");break;case"hover":await l.hover({timeout:i});break;case"scroll_to":await l.scrollIntoViewIfNeeded({timeout:i});break;case"wait_for":await l.waitFor({timeout:i,state:"visible"});break}};try{await u()}catch(h){if(h instanceof Error&&/navigation|net::ERR/i.test(h.message))try{await u()}catch(b){c=b}else c=h}let d=r.url();if(d!==s){let h=Yu(d,this.config);if(!h.allowed)return await r.goBack().catch(()=>{}),{outcome:"blocked_by_policy",url:d,reason:h.reason}}let p=null;(t.screenshot===!0||c!==null)&&(p=await this.captureScreenshot(r,n,"browser_act")),o.observationCounter+=1;let f=await da(r,{observationCounter:o.observationCounter,screenshotPath:p,consoleErrors:this.launcher.getConsoleErrorCount(n),httpStatus:this.launcher.getLastHttpStatus(n),hasDialog:this.launcher.hasOpenDialog(n)}),g=`browser_act:${t.action}`;if(this.updateSessionFromObservation(o,f.interactive,f.url,f.title,g),c!==null)throw c;return f}async screenshot(t){let{sessionId:n}=t,r=this.launcher.getPage(n);if(r===void 0)throw new Error(`browser_screenshot: no page open for session ${n}`);let o=this.ensureSessionState(n),s;if(t.target!==void 0){let u=await ed(r,t.target,o.knownElements);if(u.outcome==="not_found")throw new Error(`browser_screenshot: target not found: ${Oh(t.target)}`);if(u.outcome==="ambiguous_target")throw new Error("screenshot target ambiguous; specify element_id or selector");s=await u.locator.screenshot()}else s=await r.screenshot({fullPage:t.fullPage??!1});let{path:i,bytes:a}=await nd(n,s,"browser_screenshot"),l=0,c=0;if(t.fullPage===!0)try{let u=await r.evaluate(()=>({w:document.documentElement.scrollWidth,h:document.documentElement.scrollHeight}));l=u.w,c=u.h}catch{let u=r.viewportSize();l=u?.width??0,c=u?.height??0}else{let u=r.viewportSize();l=u?.width??0,c=u?.height??0}return{path:i,bytes:a,width:l,height:c}}async extract(t){throw new Error("browser_extract not implemented in Phase 1")}async close(t){await this.launcher.closeSession(t.sessionId),this.sessions.delete(t.sessionId)}describe(t){let n=this.sessions.get(t);if(n===void 0)return null;let r=this.launcher.getPage(t);return{active:r!==void 0,url:n.currentUrl,title:n.currentTitle,lastAction:n.lastAction,lastActionAt:n.lastActionAt,openTabs:r!==void 0?1:0}}async shutdown(){this.sessions.clear(),await this.launcher.shutdown()}ensureSessionState(t){let n=this.sessions.get(t);if(n!==void 0)return n;let r={observationCounter:0,knownElements:new Map,lastAction:null,lastActionAt:null,currentUrl:null,currentTitle:null};return this.sessions.set(t,r),r}updateSessionFromObservation(t,n,r,o,s){t.knownElements=new Map(n.map(i=>[i.id,i])),t.currentUrl=r,t.currentTitle=o,t.lastAction=s,t.lastActionAt=new Date().toISOString()}async captureScreenshot(t,n,r){try{let o=await t.screenshot({fullPage:!1}),{path:s}=await nd(n,o,r);return s}catch{return null}}}});var Yr={};Mi(Yr,{__resetBrowserRegistryForTests:()=>KP,browserProviderActive:()=>WP,closeBrowserProvider:()=>od,getBrowserProvider:()=>jP,peekBrowserProvider:()=>HP});function Lh(){Promise.resolve(od()).then(()=>{process.exit(130)})}function Fh(){Promise.resolve(od()).then(()=>{process.exit(143)})}function Nh(){Ut=null}function UP(){pa||(process.on("SIGINT",Lh),process.on("SIGTERM",Fh),process.on("exit",Nh),pa=!0)}function Bh(){pa&&(process.removeListener("SIGINT",Lh),process.removeListener("SIGTERM",Fh),process.removeListener("exit",Nh),pa=!1)}async function jP(e){return Ut!==null?Ut:(ir!==null||(ir=(async()=>{let{PlaywrightProvider:t}=await Promise.resolve().then(()=>(Dh(),$h)),n=wh(e),r=new t(n);return UP(),Ut=r,ir=null,r})()),ir)}async function od(){if(Ut===null)return;let e=Ut;Ut=null,ir=null,Bh(),await e.shutdown()}function WP(){return Ut!==null}function HP(){return Ut}function KP(){Ut=null,ir=null,Bh()}var Ut,ir,pa,Xr=rt(()=>{"use strict";Xu();Ut=null,ir=null,pa=!1});var BS={};Mi(BS,{KeychainOAuthProvider:()=>Co,clearOauthPending:()=>cp,readOauthPending:()=>NS});import{existsSync as ll,mkdirSync as LS,readFileSync as cl,writeFileSync as lp}from"node:fs";import{execFileSync as MS}from"node:child_process";import{homedir as OS,userInfo as $S}from"node:os";import{join as DS,dirname as FS}from"node:path";function OF(){let e=process.platform==="darwin",t=process.platform==="linux";return{read(){if(e)try{return MS("security",["find-generic-password","-s","Claude Code-credentials","-a",$S().username,"-w"],{stdio:["ignore","pipe","ignore"],encoding:"utf-8"}).trim()||void 0}catch{return}if(t){let n=DS(OS(),".claude",".credentials.json");if(!ll(n))return;try{return cl(n,"utf-8")}catch{return}}},write(n){if(e)MS("security",["add-generic-password","-U","-s","Claude Code-credentials","-a",$S().username,"-w",n],{stdio:["ignore","ignore","ignore"]});else if(t){let r=DS(OS(),".claude",".credentials.json");LS(FS(r),{recursive:!0}),lp(r,n,{encoding:"utf-8",mode:384})}}}}function NS(){let e=ls();if(!ll(e))return{};let t;try{t=JSON.parse(cl(e,"utf-8"))}catch{return{}}if(t===null||typeof t!="object")return{};let n={};for(let[r,o]of Object.entries(t)){if(o===null||typeof o!="object")continue;let s=o;s.status==="oauth_pending"&&typeof s.authorizationUrl=="string"&&typeof s.timestamp=="number"&&Date.now()-s.timestamp<=MF&&(n[r]={status:"oauth_pending",authorizationUrl:s.authorizationUrl,timestamp:s.timestamp})}return n}function cp(e){let t=ls();if(!ll(t))return;let n;try{n=JSON.parse(cl(t,"utf-8"))}catch{return}e in n&&(delete n[e],lp(t,JSON.stringify(n,null,2),{encoding:"utf-8",mode:384}))}function $F(e,t){let n=ls();LS(FS(n),{recursive:!0});let r={};if(ll(n))try{r=JSON.parse(cl(n,"utf-8"))}catch{}let o=new URL(t),s=o.origin+o.pathname;r[e]={status:"oauth_pending",authorizationUrl:s,timestamp:Date.now()},lp(n,JSON.stringify(r,null,2),{encoding:"utf-8",mode:384})}var MF,Co,ul=rt(()=>{"use strict";W();MF=600*1e3;Co=class{serverName;backend;constructor(t,n=OF()){this.serverName=t,this.backend=n}get redirectUrl(){return"http://localhost:3000/oauth/callback"}get clientMetadata(){return{redirect_uris:[this.redirectUrl],client_name:"agent-afk",grant_types:["authorization_code","refresh_token"],response_types:["code"],token_endpoint_auth_method:"none"}}clientInformation(){return this._readSlot().clientInfo}saveClientInformation(t){this._updateSlot(n=>({...n,clientInfo:t}))}tokens(){return this._readSlot().tokens}saveTokens(t){this._updateSlot(n=>({...n,tokens:t}));try{cp(this.serverName)}catch{}}saveCodeVerifier(t){this._updateSlot(n=>({...n,codeVerifier:t}))}codeVerifier(){let t=this._readSlot().codeVerifier;if(!t)throw new Error(`[mcp:${this.serverName}] no PKCE code verifier stored`);return t}saveDiscoveryState(t){this._updateSlot(n=>({...n,discoveryState:t}))}discoveryState(){return this._readSlot().discoveryState}invalidateCredentials(t){this._updateSlot(n=>{if(t==="all")return{};let r={...n};return t==="client"&&delete r.clientInfo,t==="tokens"&&delete r.tokens,t==="verifier"&&delete r.codeVerifier,t==="discovery"&&delete r.discoveryState,r})}async redirectToAuthorization(t){let n=t.toString(),r=`\u{1F510} MCP server "${this.serverName}" requires authorization.
4
4
 
5
5
  Open this URL to authorize:
6
6
  ${n}`;$F(this.serverName,n);let o=!1;try{let{pushIfConfigured:s}=await Promise.resolve().then(()=>(ws(),ju));o=await s(r)!==null}catch{}o||process.stderr.write(`[mcp:${this.serverName}] OAuth authorization required.
7
7
  Open this URL to authorize:
8
8
  ${n}
9
9
  Status written to: ${ls()}
10
- `)}_readSlot(){let t=this.backend.read();if(!t)return{};try{return JSON.parse(t).mcpOAuth?.[this.serverName]??{}}catch{return{}}}_updateSlot(t){let n=this.backend.read(),r={};if(n)try{r=JSON.parse(n)}catch{}let o=r.mcpOAuth??{},s=o[this.serverName]??{};o[this.serverName]=t(s),r.mcpOAuth=o,this.backend.write(JSON.stringify(r))}}});W();import{config as Cf}from"dotenv";G();W();import{randomBytes as FA}from"node:crypto";import{mkdirSync as NA,renameSync as BA,rmSync as Kf,writeFileSync as UA}from"node:fs";import{dirname as jA,isAbsolute as WA,join as HA}from"node:path";function Bi(){return HA(ye(),"last-cwd")}function Gf(){try{Kf(Bi(),{force:!0})}catch{}}function qf(e){if(!WA(e))throw new Error(`recordCdIntent: target must be an absolute path, got ${JSON.stringify(e)}`);if(/[\n\r\0]/.test(e))throw new Error(`recordCdIntent: target must not contain newline/CR/NUL, got ${JSON.stringify(e)}`);try{let t=Bi();NA(jA(t),{recursive:!0,mode:448});let n=`${t}.tmp.${process.pid}.${FA(6).toString("hex")}`;try{UA(n,e,{encoding:"utf8",mode:384}),BA(n,t)}catch(r){try{Kf(n,{force:!0})}catch{}throw r}}catch{}}function zf(){let e=T.AFK_SHELL_WRAPPER;return e==="1"||e==="true"}import{Command as Az}from"commander";G();import du from"chalk";function Jf(){let e=T.FORCE_COLOR;if(e&&e.length>0)return;let t=T.NO_COLOR;if(t&&t.length>0){du.level=0;return}let n=T.CI;if(n&&n.length>0){du.level=0;return}process.stdout.isTTY||(du.level=0)}import mS from"chalk";import nF from"ora";var dt=class extends Error{constructor(t){super(t),this.name="AbortError"}},pt=class extends Error{constructor(n,r){super(n);this.timeoutMs=r;this.name="TimeoutError"}timeoutMs},Ae=class extends Error{constructor(n,r,o,s){super(n);this.event=r;this.reason=o;this.name="HookBlockedError",s?.cause!==void 0&&(this.cause=s.cause)}event;reason;cause};var Tn=class extends Error{constructor(n,r,o){super(o??`Budget ceiling reached: $${n.toFixed(4)} cumulative >= $${r.toFixed(4)} limit`);this.runningCostUsd=n;this.maxBudgetUsd=r;this.name="BudgetExceededError"}runningCostUsd;maxBudgetUsd},Ui=class extends Error{constructor(n,r,o){super(o??`${n} provider does not support AgentConfig.${r}.`);this.provider=n;this.field=r;this.name="UnsupportedProviderConfigError"}provider;field};function Vf(e){let t=e instanceof Error?e.message:String(e);return t.toLowerCase().includes("rate limit")||t.toLowerCase().includes("too many requests")}function Yf(e){let t=e instanceof Error?e.message:String(e);return t.toLowerCase().includes("network")||t.toLowerCase().includes("connect")||t.toLowerCase().includes("timeout")}function Br(e){if(e instanceof Tn)return{kind:"budget_exceeded",userMessage:`Session stopped: cost ceiling reached ($${e.runningCostUsd.toFixed(4)} of $${e.maxBudgetUsd.toFixed(4)} limit).`,exitCode:1,raw:e};if(e instanceof Ui)return{kind:"unsupported_config",userMessage:`The "${e.provider}" provider does not support this configuration option.`,hint:`Option "${e.field}" is not available for provider "${e.provider}". Switch to a compatible provider or remove the option.`,exitCode:1,raw:e};if(e instanceof Ae)return{kind:"hook_blocked",userMessage:`A hook blocked the operation (event: ${e.event}).`,...e.reason!==void 0?{hint:e.reason}:{},exitCode:1,raw:e};if(e instanceof pt){let s=Math.round(e.timeoutMs/1e3);return{kind:"timeout",userMessage:`The operation timed out after ${s} second${s!==1?"s":""}.`,hint:`Timeout: ${e.timeoutMs}ms (${s}s). Increase the timeout or retry.`,exitCode:124,raw:e}}let t=e,n=e instanceof Error?e.message:String(e),r=n.toLowerCase();return t.status===401||e instanceof Error&&e.name==="AuthenticationError"?{kind:"auth",userMessage:"Authentication failed. Check that your API key is valid and has not expired.",hint:"Verify the ANTHROPIC_API_KEY environment variable or run `afk login`.",exitCode:1,raw:e}:t.status===429||Vf(e)?{kind:"rate_limit",userMessage:"Anthropic rate limit reached. The request was rejected (HTTP 429).",hint:"Wait a moment and retry, or reduce the request frequency.",exitCode:1,raw:e}:t.status===529||t.status===503?{kind:"overloaded",userMessage:`Anthropic API is temporarily overloaded (HTTP ${t.status}). All retry attempts were exhausted.`,hint:"Wait a minute and try again, or switch to a less loaded model (e.g. sonnet).",exitCode:1,raw:e}:n==="Not in a git repository."||r.includes("not in a git repository")?{kind:"not_git_repo",userMessage:"This command must be run from inside a git repository.",hint:"Run `git init` to initialise a repository, or change to a directory that is already a git repo.",exitCode:1,raw:e}:Yf(e)||r.includes("econnrefused")||r.includes("etimedout")?{kind:"network",userMessage:"Network error: unable to reach the API endpoint.",hint:"Check your internet connection and try again.",exitCode:1,raw:e}:{kind:"unknown",userMessage:(e instanceof Error?e.message:String(e))||"An unexpected error occurred.",exitCode:1,raw:e}}import KA from"string-width";var pu=/\x1B(?:\[[0-?]*[ -/]*[@-~]|\][^\x07\x1B]*(?:\x07|\x1B\\)|[P^_X][^\x1B]*\x1B\\|[@-OQ-WYZ\\\-])/g,Xf=typeof Intl<"u"&&"Segmenter"in Intl?new Intl.Segmenter(void 0,{granularity:"grapheme"}):null;function Ie(e){return e.replace(pu,"")}function z(e){return KA(e)}function ji(e){return e.length===0?[]:Xf?Array.from(Xf.segment(e),t=>t.segment):Array.from(e)}function GA(e){let t=[],n=0,r;for(pu.lastIndex=0;(r=pu.exec(e))!==null;){if(r.index>n)for(let o of ji(e.slice(n,r.index)))t.push({type:"text",value:o});t.push({type:"ansi",value:r[0]}),n=r.index+r[0].length}if(n<e.length)for(let o of ji(e.slice(n)))t.push({type:"text",value:o});return t}function He(e,t){let n=Math.max(0,t-z(e));return e+" ".repeat(n)}function qA(e,t){let n=Math.max(0,t-z(e));return" ".repeat(n)+e}function mu(e,t,n="left"){let r=Math.max(0,t-z(e));if(r===0)return e;if(n==="right")return qA(e,t);if(n==="center"){let o=Math.floor(r/2);return" ".repeat(o)+e+" ".repeat(r-o)}return He(e,t)}function ae(e,t,n="\u2026"){if(t<=0)return"";if(z(e)<=t)return e;let r=z(n),o=Math.max(0,t-r),s=0,i="",a=!1;for(let l of GA(e)){if(l.type==="ansi"){i+=l.value,a=!0;continue}let c=s+z(l.value);if(c>o)break;i+=l.value,s=c}return i+n+(a?"\x1B[0m":"")}function Zf(e,t){return Number.isFinite(e)?Math.max(0,Math.min(t,Math.trunc(e))):0}function fu(e,t){let n=Zf(t,e.length);if(n===0||e.length===0)return 0;let r=0;for(let o of ji(e)){let s=r+o.length;if(s>=n)return r;r=s}return r}function cs(e,t){let n=Zf(t,e.length);if(n>=e.length||e.length===0)return e.length;let r=0;for(let o of ji(e)){let s=r+o.length;if(r>=n||n>r&&n<s)return s;r=s}return e.length}function Y(){let e=process.stdout.columns;return typeof e=="number"&&e>0?e:80}var Wi=new Set,Hi=new Set,Ur=!1,Qn=null;function zA(){for(let e of Wi)try{e()}catch{}}function JA(){for(let e of Hi)try{e()}catch{}}function VA(){Qn!==null&&clearTimeout(Qn),Qn=setTimeout(()=>{Qn=null,zA()},150)}function gu(){JA(),VA()}function YA(e){return Wi.add(e),Ur||(process.stdout.on("resize",gu),Ur=!0),()=>{Wi.delete(e),Qf()}}function XA(e){return Hi.add(e),Ur||(process.stdout.on("resize",gu),Ur=!0),()=>{Hi.delete(e),Qf()}}function Qf(){Wi.size===0&&Hi.size===0&&(Ur&&(process.stdout.off("resize",gu),Ur=!1),Qn!==null&&(clearTimeout(Qn),Qn=null))}var Ue={subscribe:YA,subscribeImmediate:XA};import ZA from"wrap-ansi";function me(e,t){if(!Number.isFinite(t)||t<=0||t===Number.POSITIVE_INFINITY)return e;let n=Math.floor(t);return ZA(e,n,{hard:!1,trim:!1,wordWrap:!0})}import be from"chalk";var p={brand:be.hex("#E67E4C"),mint:be.hex("#5FE3A1"),goblin:be.hex("#9CB04A"),user:be.cyan,tool:be.hex("#DCDCAA"),chrome:be.hex("#B0B8C2"),syntaxString:be.italic.hex("#8AB07A"),toolArg:be.dim.white,thinking:be.italic.hex("#9B8FB5"),success:be.green,error:be.red,warning:be.yellow,plan:be.hex("#9F7CE0"),meta:be.blackBright,info:be.hex("#5BA8FF"),fileRef:be.hex("#56B5A8"),heading:be.bold.white,label:be.dim,dim:be.dim,bold:be.bold,italic:be.italic,diffAdd:be.green,diffRemove:be.red,diffHunk:be.blackBright};function mt(){return Math.max(22,Y()-6)}function us(e,t){return ae(e,t)}var QA={ok:p.success("\u25CF"),warn:p.warning("\u25CF"),error:p.error("\u25CF"),info:p.info("\u25C6")};function eg(e,t){let o=t.reduce((x,R)=>Math.max(x,z(R.label)),0),s=t.reduce((x,R)=>Math.max(x,z(R.value)),0),i=o+4+2+s,a=Math.min(Y()-4,100),l=Math.max(44,z(e),i,a);l=Math.min(l,mt());let c=l+4,u=p.dim,d=u("\u256D"+"\u2500".repeat(c)+"\u256E"),m=u("\u251C"+"\u2500".repeat(c)+"\u2524"),f=u("\u2570"+"\u2500".repeat(c)+"\u256F"),g=u("\u2502"),b=me(e,l).split(`
11
- `).map(x=>g+" "+He(x,l)+" "+g),y=Math.max(1,l-o-4-2),w=t.map(x=>{let R=x.kind?QA[x.kind]+" ":" ",v=p.dim(He(us(x.label,o),o)),E=" ".repeat(4),I=us(x.value,y),D=He(I,y),_=v+E+R+D;return g+" "+_+" "+g});return[d,...b,m,...w,f].join(`
12
- `)}import{sep as bu}from"node:path";G();G();import hu from"chalk";var tg={".":null,D:[74,92,36],M:[139,166,63],L:[178,197,88],Y:[245,213,71],K:[13,18,9],W:[238,238,222],X:[42,42,26]},Ki=[".......KKKKK.......","......WKKLKKW......",".....KKWMLMWKK.....","..DDKKWLMMMLWKKDD..","DDD..KMMMMMMMK..DDD","..DDKMMMMMMMMMKDD..","...KKMMKKKKKMMKK...","...KMMKKKKKKKMMK...","...KMMMYMDMYMMMK...","...KMMMMMMMMMMMK...","...KKMMXXXXXMMKK...","....KKMMWXXMMKK....",".....KKMWMMMKK.....",".....KKMMMMMKK.....","......KKMMMKK......",".......KKKKK......."],yu=19,ds=8;function e_(e,t){if(e.length!==t.length)throw new Error(`pixel row width mismatch: top=${e.length}, bot=${t.length}`);let n="";for(let r=0;r<e.length;r++){let o=tg[e[r]??"."]??null,s=tg[t[r]??"."]??null;!o&&!s?n+=" ":o&&!s?n+=hu.rgb(o[0],o[1],o[2])("\u2580"):!o&&s?n+=hu.rgb(s[0],s[1],s[2])("\u2584"):o&&s&&(n+=hu.bgRgb(s[0],s[1],s[2]).rgb(o[0],o[1],o[2])("\u2580"))}return n}function t_(){if(Ki.length!==ds*2)throw new Error(`GOBLIN_GRID has ${Ki.length} pixel rows but MASCOT_HEIGHT*2 = ${ds*2}`);let e=[];for(let t=0;t<ds;t++){let n=Ki[t*2]??"",r=Ki[t*2+1]??"";e.push(e_(n,r))}return e}function ng(e="idle"){return T.AFK_BANNER_PLAIN==="1"?[]:t_()}function rg(){return T.AFK_BANNER_PLAIN==="1"}function sg(e){let t=e.model!==void 0||e.worktree!==void 0||e.cwd!==void 0||e.version!==void 0;return t&&!rg()?r_(e):og(t?n_(e):e)}function n_(e){let t=e.mode;e.model!==void 0&&e.model.length>0&&(t=`${e.model} \xB7 ${t}`),e.version!==void 0&&e.version.length>0&&(t=`${t} \xB7 ${ig(e.version)}`);let n=[];e.worktree!==void 0&&e.worktree.length>0&&n.push(`branch ${e.worktree}`),e.cwd!==void 0&&e.cwd.length>0&&n.push(ag(e.cwd));let r=n.join(" \xB7 "),o=e.metaLine!==void 0?e.metaLine:r.length>0?r:void 0,s={mode:t};return o!==void 0&&(s.metaLine=o),e.hintLine!==void 0&&(s.hintLine=e.hintLine),s}function og(e){let t="Agent AFK",n=" \xB7 ",r=p.bold(t)+p.dim(n)+e.mode,o=t+n+e.mode,s=Math.min(Y()-4,120),i=Math.max(54,z(o)+4,s);i=Math.min(i,mt());let a=i+4,l=p.dim,c=l("\u256D"+"\u2500".repeat(a)+"\u256E"),d=me(r,i).split(`
13
- `).map(g=>l("\u2502")+" "+He(g,i)+" "+l("\u2502")),m=l("\u2570"+"\u2500".repeat(a)+"\u256F"),f=[c,...d,m];return e.metaLine!==void 0&&f.push(...me(p.dim(" "+e.metaLine),Y()).split(`
14
- `)),e.hintLine!==void 0&&f.push(...me(p.dim(" "+e.hintLine),Y()).split(`
10
+ `)}_readSlot(){let t=this.backend.read();if(!t)return{};try{return JSON.parse(t).mcpOAuth?.[this.serverName]??{}}catch{return{}}}_updateSlot(t){let n=this.backend.read(),r={};if(n)try{r=JSON.parse(n)}catch{}let o=r.mcpOAuth??{},s=o[this.serverName]??{};o[this.serverName]=t(s),r.mcpOAuth=o,this.backend.write(JSON.stringify(r))}}});W();import{config as Cf}from"dotenv";G();W();import{randomBytes as FA}from"node:crypto";import{mkdirSync as NA,renameSync as BA,rmSync as Kf,writeFileSync as UA}from"node:fs";import{dirname as jA,isAbsolute as WA,join as HA}from"node:path";function Bi(){return HA(ye(),"last-cwd")}function Gf(){try{Kf(Bi(),{force:!0})}catch{}}function qf(e){if(!WA(e))throw new Error(`recordCdIntent: target must be an absolute path, got ${JSON.stringify(e)}`);if(/[\n\r\0]/.test(e))throw new Error(`recordCdIntent: target must not contain newline/CR/NUL, got ${JSON.stringify(e)}`);try{let t=Bi();NA(jA(t),{recursive:!0,mode:448});let n=`${t}.tmp.${process.pid}.${FA(6).toString("hex")}`;try{UA(n,e,{encoding:"utf8",mode:384}),BA(n,t)}catch(r){try{Kf(n,{force:!0})}catch{}throw r}}catch{}}function zf(){let e=T.AFK_SHELL_WRAPPER;return e==="1"||e==="true"}import{Command as Az}from"commander";G();import du from"chalk";function Jf(){let e=T.FORCE_COLOR;if(e&&e.length>0)return;let t=T.NO_COLOR;if(t&&t.length>0){du.level=0;return}let n=T.CI;if(n&&n.length>0){du.level=0;return}process.stdout.isTTY||(du.level=0)}import mS from"chalk";import nF from"ora";var dt=class extends Error{constructor(t){super(t),this.name="AbortError"}},pt=class extends Error{constructor(n,r){super(n);this.timeoutMs=r;this.name="TimeoutError"}timeoutMs},Ae=class extends Error{constructor(n,r,o,s){super(n);this.event=r;this.reason=o;this.name="HookBlockedError",s?.cause!==void 0&&(this.cause=s.cause)}event;reason;cause};var Tn=class extends Error{constructor(n,r,o){super(o??`Budget ceiling reached: $${n.toFixed(4)} cumulative >= $${r.toFixed(4)} limit`);this.runningCostUsd=n;this.maxBudgetUsd=r;this.name="BudgetExceededError"}runningCostUsd;maxBudgetUsd},Ui=class extends Error{constructor(n,r,o){super(o??`${n} provider does not support AgentConfig.${r}.`);this.provider=n;this.field=r;this.name="UnsupportedProviderConfigError"}provider;field};function Vf(e){let t=e instanceof Error?e.message:String(e);return t.toLowerCase().includes("rate limit")||t.toLowerCase().includes("too many requests")}function Yf(e){let t=e instanceof Error?e.message:String(e);return t.toLowerCase().includes("network")||t.toLowerCase().includes("connect")||t.toLowerCase().includes("timeout")}function Br(e){if(e instanceof Tn)return{kind:"budget_exceeded",userMessage:`Session stopped: cost ceiling reached ($${e.runningCostUsd.toFixed(4)} of $${e.maxBudgetUsd.toFixed(4)} limit).`,exitCode:1,raw:e};if(e instanceof Ui)return{kind:"unsupported_config",userMessage:`The "${e.provider}" provider does not support this configuration option.`,hint:`Option "${e.field}" is not available for provider "${e.provider}". Switch to a compatible provider or remove the option.`,exitCode:1,raw:e};if(e instanceof Ae)return{kind:"hook_blocked",userMessage:`A hook blocked the operation (event: ${e.event}).`,...e.reason!==void 0?{hint:e.reason}:{},exitCode:1,raw:e};if(e instanceof pt){let s=Math.round(e.timeoutMs/1e3);return{kind:"timeout",userMessage:`The operation timed out after ${s} second${s!==1?"s":""}.`,hint:`Timeout: ${e.timeoutMs}ms (${s}s). Increase the timeout or retry.`,exitCode:124,raw:e}}let t=e,n=e instanceof Error?e.message:String(e),r=n.toLowerCase();return t.status===401||e instanceof Error&&e.name==="AuthenticationError"?{kind:"auth",userMessage:"Authentication failed. Check that your API key is valid and has not expired.",hint:"Verify the ANTHROPIC_API_KEY environment variable or run `afk login`.",exitCode:1,raw:e}:t.status===429||Vf(e)?{kind:"rate_limit",userMessage:"Anthropic rate limit reached. The request was rejected (HTTP 429).",hint:"Wait a moment and retry, or reduce the request frequency.",exitCode:1,raw:e}:t.status===529||t.status===503?{kind:"overloaded",userMessage:`Anthropic API is temporarily overloaded (HTTP ${t.status}). All retry attempts were exhausted.`,hint:"Wait a minute and try again, or switch to a less loaded model (e.g. sonnet).",exitCode:1,raw:e}:n==="Not in a git repository."||r.includes("not in a git repository")?{kind:"not_git_repo",userMessage:"This command must be run from inside a git repository.",hint:"Run `git init` to initialise a repository, or change to a directory that is already a git repo.",exitCode:1,raw:e}:Yf(e)||r.includes("econnrefused")||r.includes("etimedout")?{kind:"network",userMessage:"Network error: unable to reach the API endpoint.",hint:"Check your internet connection and try again.",exitCode:1,raw:e}:{kind:"unknown",userMessage:(e instanceof Error?e.message:String(e))||"An unexpected error occurred.",exitCode:1,raw:e}}import KA from"string-width";var pu=/\x1B(?:\[[0-?]*[ -/]*[@-~]|\][^\x07\x1B]*(?:\x07|\x1B\\)|[P^_X][^\x1B]*\x1B\\|[@-OQ-WYZ\\\-])/g,Xf=typeof Intl<"u"&&"Segmenter"in Intl?new Intl.Segmenter(void 0,{granularity:"grapheme"}):null;function Ie(e){return e.replace(pu,"")}function z(e){return KA(e)}function ji(e){return e.length===0?[]:Xf?Array.from(Xf.segment(e),t=>t.segment):Array.from(e)}function GA(e){let t=[],n=0,r;for(pu.lastIndex=0;(r=pu.exec(e))!==null;){if(r.index>n)for(let o of ji(e.slice(n,r.index)))t.push({type:"text",value:o});t.push({type:"ansi",value:r[0]}),n=r.index+r[0].length}if(n<e.length)for(let o of ji(e.slice(n)))t.push({type:"text",value:o});return t}function He(e,t){let n=Math.max(0,t-z(e));return e+" ".repeat(n)}function qA(e,t){let n=Math.max(0,t-z(e));return" ".repeat(n)+e}function mu(e,t,n="left"){let r=Math.max(0,t-z(e));if(r===0)return e;if(n==="right")return qA(e,t);if(n==="center"){let o=Math.floor(r/2);return" ".repeat(o)+e+" ".repeat(r-o)}return He(e,t)}function ae(e,t,n="\u2026"){if(t<=0)return"";if(z(e)<=t)return e;let r=z(n),o=Math.max(0,t-r),s=0,i="",a=!1;for(let l of GA(e)){if(l.type==="ansi"){i+=l.value,a=!0;continue}let c=s+z(l.value);if(c>o)break;i+=l.value,s=c}return i+n+(a?"\x1B[0m":"")}function Zf(e,t){return Number.isFinite(e)?Math.max(0,Math.min(t,Math.trunc(e))):0}function fu(e,t){let n=Zf(t,e.length);if(n===0||e.length===0)return 0;let r=0;for(let o of ji(e)){let s=r+o.length;if(s>=n)return r;r=s}return r}function cs(e,t){let n=Zf(t,e.length);if(n>=e.length||e.length===0)return e.length;let r=0;for(let o of ji(e)){let s=r+o.length;if(r>=n||n>r&&n<s)return s;r=s}return e.length}function Y(){let e=process.stdout.columns;return typeof e=="number"&&e>0?e:80}var Wi=new Set,Hi=new Set,Ur=!1,Qn=null;function zA(){for(let e of Wi)try{e()}catch{}}function JA(){for(let e of Hi)try{e()}catch{}}function VA(){Qn!==null&&clearTimeout(Qn),Qn=setTimeout(()=>{Qn=null,zA()},150)}function gu(){JA(),VA()}function YA(e){return Wi.add(e),Ur||(process.stdout.on("resize",gu),Ur=!0),()=>{Wi.delete(e),Qf()}}function XA(e){return Hi.add(e),Ur||(process.stdout.on("resize",gu),Ur=!0),()=>{Hi.delete(e),Qf()}}function Qf(){Wi.size===0&&Hi.size===0&&(Ur&&(process.stdout.off("resize",gu),Ur=!1),Qn!==null&&(clearTimeout(Qn),Qn=null))}var Ue={subscribe:YA,subscribeImmediate:XA};import ZA from"wrap-ansi";function me(e,t){if(!Number.isFinite(t)||t<=0||t===Number.POSITIVE_INFINITY)return e;let n=Math.floor(t);return ZA(e,n,{hard:!1,trim:!1,wordWrap:!0})}import be from"chalk";var m={brand:be.hex("#E67E4C"),mint:be.hex("#5FE3A1"),goblin:be.hex("#9CB04A"),user:be.cyan,tool:be.hex("#DCDCAA"),chrome:be.hex("#B0B8C2"),syntaxString:be.italic.hex("#8AB07A"),toolArg:be.dim.white,thinking:be.italic.hex("#9B8FB5"),success:be.green,error:be.red,warning:be.yellow,plan:be.hex("#9F7CE0"),meta:be.blackBright,info:be.hex("#5BA8FF"),fileRef:be.hex("#56B5A8"),heading:be.bold.white,label:be.dim,dim:be.dim,bold:be.bold,italic:be.italic,diffAdd:be.green,diffRemove:be.red,diffHunk:be.blackBright};function mt(){return Math.max(22,Y()-6)}function us(e,t){return ae(e,t)}var QA={ok:m.success("\u25CF"),warn:m.warning("\u25CF"),error:m.error("\u25CF"),info:m.info("\u25C6")};function eg(e,t){let o=t.reduce((x,R)=>Math.max(x,z(R.label)),0),s=t.reduce((x,R)=>Math.max(x,z(R.value)),0),i=o+4+2+s,a=Math.min(Y()-4,100),l=Math.max(44,z(e),i,a);l=Math.min(l,mt());let c=l+4,u=m.dim,d=u("\u256D"+"\u2500".repeat(c)+"\u256E"),p=u("\u251C"+"\u2500".repeat(c)+"\u2524"),f=u("\u2570"+"\u2500".repeat(c)+"\u256F"),g=u("\u2502"),b=me(e,l).split(`
11
+ `).map(x=>g+" "+He(x,l)+" "+g),y=Math.max(1,l-o-4-2),w=t.map(x=>{let R=x.kind?QA[x.kind]+" ":" ",v=m.dim(He(us(x.label,o),o)),E=" ".repeat(4),I=us(x.value,y),D=He(I,y),_=v+E+R+D;return g+" "+_+" "+g});return[d,...b,p,...w,f].join(`
12
+ `)}import{sep as bu}from"node:path";G();G();import hu from"chalk";var tg={".":null,D:[74,92,36],M:[139,166,63],L:[178,197,88],Y:[245,213,71],K:[13,18,9],W:[238,238,222],X:[42,42,26]},Ki=[".......KKKKK.......","......WKKLKKW......",".....KKWMLMWKK.....","..DDKKWLMMMLWKKDD..","DDD..KMMMMMMMK..DDD","..DDKMMMMMMMMMKDD..","...KKMMKKKKKMMKK...","...KMMKKKKKKKMMK...","...KMMMYMDMYMMMK...","...KMMMMMMMMMMMK...","...KKMMXXXXXMMKK...","....KKMMWXXMMKK....",".....KKMWMMMKK.....",".....KKMMMMMKK.....","......KKMMMKK......",".......KKKKK......."],yu=19,ds=8;function e_(e,t){if(e.length!==t.length)throw new Error(`pixel row width mismatch: top=${e.length}, bot=${t.length}`);let n="";for(let r=0;r<e.length;r++){let o=tg[e[r]??"."]??null,s=tg[t[r]??"."]??null;!o&&!s?n+=" ":o&&!s?n+=hu.rgb(o[0],o[1],o[2])("\u2580"):!o&&s?n+=hu.rgb(s[0],s[1],s[2])("\u2584"):o&&s&&(n+=hu.bgRgb(s[0],s[1],s[2]).rgb(o[0],o[1],o[2])("\u2580"))}return n}function t_(){if(Ki.length!==ds*2)throw new Error(`GOBLIN_GRID has ${Ki.length} pixel rows but MASCOT_HEIGHT*2 = ${ds*2}`);let e=[];for(let t=0;t<ds;t++){let n=Ki[t*2]??"",r=Ki[t*2+1]??"";e.push(e_(n,r))}return e}function ng(e="idle"){return T.AFK_BANNER_PLAIN==="1"?[]:t_()}function rg(){return T.AFK_BANNER_PLAIN==="1"}function sg(e){let t=e.model!==void 0||e.worktree!==void 0||e.cwd!==void 0||e.version!==void 0;return t&&!rg()?r_(e):og(t?n_(e):e)}function n_(e){let t=e.mode;e.model!==void 0&&e.model.length>0&&(t=`${e.model} \xB7 ${t}`),e.version!==void 0&&e.version.length>0&&(t=`${t} \xB7 ${ig(e.version)}`);let n=[];e.worktree!==void 0&&e.worktree.length>0&&n.push(`branch ${e.worktree}`),e.cwd!==void 0&&e.cwd.length>0&&n.push(ag(e.cwd));let r=n.join(" \xB7 "),o=e.metaLine!==void 0?e.metaLine:r.length>0?r:void 0,s={mode:t};return o!==void 0&&(s.metaLine=o),e.hintLine!==void 0&&(s.hintLine=e.hintLine),s}function og(e){let t="Agent AFK",n=" \xB7 ",r=m.bold(t)+m.dim(n)+e.mode,o=t+n+e.mode,s=Math.min(Y()-4,120),i=Math.max(54,z(o)+4,s);i=Math.min(i,mt());let a=i+4,l=m.dim,c=l("\u256D"+"\u2500".repeat(a)+"\u256E"),d=me(r,i).split(`
13
+ `).map(g=>l("\u2502")+" "+He(g,i)+" "+l("\u2502")),p=l("\u2570"+"\u2500".repeat(a)+"\u256F"),f=[c,...d,p];return e.metaLine!==void 0&&f.push(...me(m.dim(" "+e.metaLine),Y()).split(`
14
+ `)),e.hintLine!==void 0&&f.push(...me(m.dim(" "+e.hintLine),Y()).split(`
15
15
  `)),f.join(`
16
- `)}function r_(e){let t=Y(),n=t>=42?" ":" ",r=t>=42?" ":" ",o=Math.max(1,t-r.length-yu-n.length),s=[],i=f=>{s.push(us(f,o))},a=p.bold(p.brand("Agent AFK")),l=e.version!==void 0?p.dim(" "+ig(e.version)):"";i(a+l);let c=[];if(e.model!==void 0&&c.push(p.heading(e.model)),e.mode.length>0&&c.push(p.dim(e.mode)),c.length>0&&i(c.join(p.dim(" \xB7 "))),e.worktree!==void 0&&i(p.dim("branch ")+p.goblin(e.worktree)),e.cwd!==void 0){let f=ag(e.cwd);i(p.dim(s_(f,o)))}e.metaLine!==void 0&&i(p.dim(e.metaLine));let u=ng("idle"),d=Math.max(u.length,s.length),m=[];for(let f=0;f<d;f++){let g=u[f]??" ".repeat(yu),h=s[f]??"";m.push(r+g+n+h)}return e.hintLine!==void 0&&m.push(...me(p.dim(r+o_(e.hintLine)),t).split(`
17
- `)),m.join(`
18
- `)}function ig(e){return e.startsWith("v")?e:`v${e}`}function o_(e){return e.replace(/\s+·\s+/g," \xB7 ")}function ag(e){let t=T.HOME;if(t===void 0||t.length===0)return e;if(e===t)return"~";let n=t.endsWith(bu)?t:t+bu;return e.startsWith(n)?"~"+bu+e.slice(n.length):e}function s_(e,t){if(z(e)<=t)return e;let n="\u2026",r=Math.max(2,t-1),o=Math.ceil(r*.6),s=Math.max(1,r-o);return e.slice(0,s)+n+e.slice(e.length-o)}var wu="\u256D",Su="\u256E",i_="\u2570",a_="\u256F",ps="\u2500",l_="\u2502";function jr(e,t={}){let n=t.border??p.dim,r=Math.max(0,Math.trunc(t.padding??1)),o=t.title,s=Array.isArray(e)?e:e.split(`
19
- `),i=mt(),a=o!==void 0?z(` ${o} `):0,l=s.reduce((b,y)=>Math.max(b,z(y)),0),c=t.width!==void 0?Math.max(1,Math.trunc(t.width)):Math.max(1,l,a+1);c=Math.min(c,i),c=Math.max(c,Math.min(a+1,i));let u=" ".repeat(r),d=c+r*2,m;if(o!==void 0){let b=d-1,y=` ${o} `;z(y)>b&&(y=ae(y,b));let w=z(y);if(w>0&&w<=b){let x=d-1-w;m=n(wu+ps)+n.bold(y)+n(ps.repeat(x)+Su)}else m=n(wu+ps.repeat(d)+Su)}else m=n(wu+ps.repeat(d)+Su);let f=n(l_),g=[];for(let b of s)for(let y of me(b,c).split(`
20
- `)){let w=He(ae(y,c),c);g.push(f+u+w+u+f)}let h=n(i_+ps.repeat(d)+a_);return[m,...g,h].join(`
21
- `)}function Gi(e,t){let n=Math.max(40,z(e),z(t??""))+4,r=Math.min(n,Math.min(Y()-4,100));r=Math.min(r,mt());let o=t!==void 0?[e,p.dim(t)]:[e];return jr(o,{border:p.error,title:"Error",width:r,padding:2})}function lg(e){let{resetsAt:t,reason:n,accountId:r,hotSwapped:o}=e,s=e.autoResume??!0,i=[];if(n==="usage-limit"){if(i.push("You've hit your Claude subscription limit for now."),t!==void 0){let d=Date.now(),m=t.getTime()-d,f=Math.max(0,Math.ceil(m/6e4)),g=t.toLocaleTimeString(void 0,{hour:"numeric",minute:"2-digit",hour12:!0});i.push(""),i.push(`Resets at ${g} (in ~${f} min).`)}i.push(""),s?(i.push("I'll auto-resume when the limit resets \u2014 no need to retype."),i.push(""),i.push("Other options:"),i.push(" \u2022 Switch to API-key billing: export ANTHROPIC_API_KEY=..."),i.push(" \u2022 Log into another account in any terminal: claude login")):(i.push("Options:"),i.push(" \u2022 Wait, then send the message again."),i.push(" \u2022 Switch to API-key billing: export ANTHROPIC_API_KEY=..."),i.push(" \u2022 Log into another account in any terminal: claude login"))}else i.push("Your Anthropic API credit balance is empty."),i.push(""),i.push("Top up at: https://console.anthropic.com/settings/billing"),i.push(""),i.push("Or switch to your Claude subscription on the server.");o===!0&&r!==void 0&&(i.push(""),i.push(`Resumed on ${r}.`));let a=" Usage paused ",l=i.reduce((d,m)=>Math.max(d,z(m)),0),c=Math.max(40,z(a),l)+4,u=Math.min(c,Math.min(Y()-4,100));return u=Math.min(u,mt()),jr(i,{border:p.warning,title:"Usage paused",width:u,padding:2})}G();import qi from"chalk";import{Lexer as pg}from"marked";import c_ from"chalk";import{createEmphasize as u_,common as d_}from"emphasize";var cg={keyword:p.brand,built_in:p.brand,literal:p.brand,tag:p.brand,string:p.syntaxString,regexp:p.syntaxString,attr:p.syntaxString,comment:p.meta,meta:p.meta,quote:p.meta,number:p.warning,function:p.tool,title:p.tool,class:p.tool,"selector-tag":p.tool};var ug=u_(d_),p_=2048,m_=512,f_=32,Zt=new Map;function g_(e){let t=Zt.get(e);if(t!==void 0)return Zt.delete(e),Zt.set(e,t),t}function h_(e,t){if(Zt.has(e)&&Zt.delete(e),Zt.set(e,t),Zt.size>f_){let n=Zt.keys().next().value;n!==void 0&&Zt.delete(n)}}function dg(e,t){if(c_.level===0||e.length>p_)return e;let n=e.length<m_,r=n?`${t} ${e}`:"";if(n){let s=g_(r);if(s!==void 0)return s}let o;try{if(!t||!ug.registered(t))o=e;else{let s=ug.highlight(t,e,cg);o=typeof s?.value=="string"?s.value:e}}catch{o=e}return n&&h_(r,o),o}function ku(e){return z(e)}function y_(e,t,n){return mu(e,t,n??"left")}var mg=/^\/[A-Za-z][\w:-]*$/,b_=/(?<=\s|^)(\/[A-Za-z][\w:-]*)(?=\s|[,.:;!?]?$|[,.:;!?]\s)/g;function xn(e){return e?e.map(t=>{switch(t.type){case"codespan":{let n=t.text;return mg.test(n)?p.brand(n):p.user(n)}case"strong":{let n=t;return p.bold(n.tokens?xn(n.tokens):n.text)}case"em":{let n=t;return p.italic(n.tokens?xn(n.tokens):n.text)}case"text":return t.text.replace(b_,n=>p.brand(n));case"link":{let n=t,r=n.tokens?xn(n.tokens):n.text;return r===n.href?r:r+p.dim(` (${n.href})`)}case"escape":return t.text;default:return t.raw}}).join(""):""}var w_=new Set(["code","table","blockquote","hr","html"]);function En(e){let t=pg.lex(e);return t.some(r=>w_.has(r.type))?e:t.map(r=>{switch(r.type){case"heading":{let o=r;return p.bold(xn(o.tokens))}case"list":return r.items.map(s=>{let a=s.tokens[0]?.tokens??[];return"\u2022 "+xn(a)}).join(", ");case"paragraph":return xn(r.tokens);case"text":{let o=r;return o.tokens?xn(o.tokens):o.text}case"space":return"";default:return r.raw}}).join("")}function xt(e,t={}){let n=pg.lex(e),r=Number.isFinite(t.maxWidth)?Math.floor(t.maxWidth??0):void 0;function o(i){return xn(i)}function s(i){return i.map(a=>{switch(a.type){case"heading":{let l=a,c=l.tokens?o(l.tokens):l.text;return l.depth===1?p.brand.bold(`
16
+ `)}function r_(e){let t=Y(),n=t>=42?" ":" ",r=t>=42?" ":" ",o=Math.max(1,t-r.length-yu-n.length),s=[],i=f=>{s.push(us(f,o))},a=m.bold(m.brand("Agent AFK")),l=e.version!==void 0?m.dim(" "+ig(e.version)):"";i(a+l);let c=[];if(e.model!==void 0&&c.push(m.heading(e.model)),e.mode.length>0&&c.push(m.dim(e.mode)),c.length>0&&i(c.join(m.dim(" \xB7 "))),e.worktree!==void 0&&i(m.dim("branch ")+m.goblin(e.worktree)),e.cwd!==void 0){let f=ag(e.cwd);i(m.dim(s_(f,o)))}e.metaLine!==void 0&&i(m.dim(e.metaLine));let u=ng("idle"),d=Math.max(u.length,s.length),p=[];for(let f=0;f<d;f++){let g=u[f]??" ".repeat(yu),h=s[f]??"";p.push(r+g+n+h)}return e.hintLine!==void 0&&p.push(...me(m.dim(r+o_(e.hintLine)),t).split(`
17
+ `)),p.join(`
18
+ `)}function ig(e){return e.startsWith("v")?e:`v${e}`}function o_(e){return e.replace(/\s+·\s+/g," \xB7 ")}function ag(e){let t=T.HOME;if(t===void 0||t.length===0)return e;if(e===t)return"~";let n=t.endsWith(bu)?t:t+bu;return e.startsWith(n)?"~"+bu+e.slice(n.length):e}function s_(e,t){if(z(e)<=t)return e;let n="\u2026",r=Math.max(2,t-1),o=Math.ceil(r*.6),s=Math.max(1,r-o);return e.slice(0,s)+n+e.slice(e.length-o)}var wu="\u256D",Su="\u256E",i_="\u2570",a_="\u256F",ps="\u2500",l_="\u2502";function jr(e,t={}){let n=t.border??m.dim,r=Math.max(0,Math.trunc(t.padding??1)),o=t.title,s=Array.isArray(e)?e:e.split(`
19
+ `),i=mt(),a=o!==void 0?z(` ${o} `):0,l=s.reduce((b,y)=>Math.max(b,z(y)),0),c=t.width!==void 0?Math.max(1,Math.trunc(t.width)):Math.max(1,l,a+1);c=Math.min(c,i),c=Math.max(c,Math.min(a+1,i));let u=" ".repeat(r),d=c+r*2,p;if(o!==void 0){let b=d-1,y=` ${o} `;z(y)>b&&(y=ae(y,b));let w=z(y);if(w>0&&w<=b){let x=d-1-w;p=n(wu+ps)+n.bold(y)+n(ps.repeat(x)+Su)}else p=n(wu+ps.repeat(d)+Su)}else p=n(wu+ps.repeat(d)+Su);let f=n(l_),g=[];for(let b of s)for(let y of me(b,c).split(`
20
+ `)){let w=He(ae(y,c),c);g.push(f+u+w+u+f)}let h=n(i_+ps.repeat(d)+a_);return[p,...g,h].join(`
21
+ `)}function Gi(e,t){let n=Math.max(40,z(e),z(t??""))+4,r=Math.min(n,Math.min(Y()-4,100));r=Math.min(r,mt());let o=t!==void 0?[e,m.dim(t)]:[e];return jr(o,{border:m.error,title:"Error",width:r,padding:2})}function lg(e){let{resetsAt:t,reason:n,accountId:r,hotSwapped:o}=e,s=e.autoResume??!0,i=[];if(n==="usage-limit"){if(i.push("You've hit your Claude subscription limit for now."),t!==void 0){let d=Date.now(),p=t.getTime()-d,f=Math.max(0,Math.ceil(p/6e4)),g=t.toLocaleTimeString(void 0,{hour:"numeric",minute:"2-digit",hour12:!0});i.push(""),i.push(`Resets at ${g} (in ~${f} min).`)}i.push(""),s?(i.push("I'll auto-resume when the limit resets \u2014 no need to retype."),i.push(""),i.push("Other options:"),i.push(" \u2022 Switch to API-key billing: export ANTHROPIC_API_KEY=..."),i.push(" \u2022 Log into another account in any terminal: claude login")):(i.push("Options:"),i.push(" \u2022 Wait, then send the message again."),i.push(" \u2022 Switch to API-key billing: export ANTHROPIC_API_KEY=..."),i.push(" \u2022 Log into another account in any terminal: claude login"))}else i.push("Your Anthropic API credit balance is empty."),i.push(""),i.push("Top up at: https://console.anthropic.com/settings/billing"),i.push(""),i.push("Or switch to your Claude subscription on the server.");o===!0&&r!==void 0&&(i.push(""),i.push(`Resumed on ${r}.`));let a=" Usage paused ",l=i.reduce((d,p)=>Math.max(d,z(p)),0),c=Math.max(40,z(a),l)+4,u=Math.min(c,Math.min(Y()-4,100));return u=Math.min(u,mt()),jr(i,{border:m.warning,title:"Usage paused",width:u,padding:2})}G();import qi from"chalk";import{Lexer as pg}from"marked";import c_ from"chalk";import{createEmphasize as u_,common as d_}from"emphasize";var cg={keyword:m.brand,built_in:m.brand,literal:m.brand,tag:m.brand,string:m.syntaxString,regexp:m.syntaxString,attr:m.syntaxString,comment:m.meta,meta:m.meta,quote:m.meta,number:m.warning,function:m.tool,title:m.tool,class:m.tool,"selector-tag":m.tool};var ug=u_(d_),p_=2048,m_=512,f_=32,Zt=new Map;function g_(e){let t=Zt.get(e);if(t!==void 0)return Zt.delete(e),Zt.set(e,t),t}function h_(e,t){if(Zt.has(e)&&Zt.delete(e),Zt.set(e,t),Zt.size>f_){let n=Zt.keys().next().value;n!==void 0&&Zt.delete(n)}}function dg(e,t){if(c_.level===0||e.length>p_)return e;let n=e.length<m_,r=n?`${t} ${e}`:"";if(n){let s=g_(r);if(s!==void 0)return s}let o;try{if(!t||!ug.registered(t))o=e;else{let s=ug.highlight(t,e,cg);o=typeof s?.value=="string"?s.value:e}}catch{o=e}return n&&h_(r,o),o}function ku(e){return z(e)}function y_(e,t,n){return mu(e,t,n??"left")}var mg=/^\/[A-Za-z][\w:-]*$/,b_=/(?<=\s|^)(\/[A-Za-z][\w:-]*)(?=\s|[,.:;!?]?$|[,.:;!?]\s)/g;function xn(e){return e?e.map(t=>{switch(t.type){case"codespan":{let n=t.text;return mg.test(n)?m.brand(n):m.user(n)}case"strong":{let n=t;return m.bold(n.tokens?xn(n.tokens):n.text)}case"em":{let n=t;return m.italic(n.tokens?xn(n.tokens):n.text)}case"text":return t.text.replace(b_,n=>m.brand(n));case"link":{let n=t,r=n.tokens?xn(n.tokens):n.text;return r===n.href?r:r+m.dim(` (${n.href})`)}case"escape":return t.text;default:return t.raw}}).join(""):""}var w_=new Set(["code","table","blockquote","hr","html"]);function En(e){let t=pg.lex(e);return t.some(r=>w_.has(r.type))?e:t.map(r=>{switch(r.type){case"heading":{let o=r;return m.bold(xn(o.tokens))}case"list":return r.items.map(s=>{let a=s.tokens[0]?.tokens??[];return"\u2022 "+xn(a)}).join(", ");case"paragraph":return xn(r.tokens);case"text":{let o=r;return o.tokens?xn(o.tokens):o.text}case"space":return"";default:return r.raw}}).join("")}function xt(e,t={}){let n=pg.lex(e),r=Number.isFinite(t.maxWidth)?Math.floor(t.maxWidth??0):void 0;function o(i){return xn(i)}function s(i){return i.map(a=>{switch(a.type){case"heading":{let l=a,c=l.tokens?o(l.tokens):l.text;return l.depth===1?m.brand.bold(`
22
22
  `+c+`
23
- `):l.depth===2?p.heading(`
23
+ `):l.depth===2?m.heading(`
24
24
  `+c+`
25
- `):p.bold(`
25
+ `):m.bold(`
26
26
  `+c+`
27
27
  `)}case"paragraph":return o(a.tokens)+`
28
28
 
29
- `;case"code":{let l=a,c=l.lang||"text";if(l.text.trim()===""){let h=l.lang?`(empty ${l.lang} block)`:"(empty code block)";return p.dim(`\u2502 ${h}`)+`
29
+ `;case"code":{let l=a,c=l.lang||"text";if(l.text.trim()===""){let h=l.lang?`(empty ${l.lang} block)`:"(empty code block)";return m.dim(`\u2502 ${h}`)+`
30
30
 
31
31
  `}let d=dg(l.text,c).split(`
32
- `);d.length>0&&d[d.length-1]===""&&d.pop();let m=p.dim("\u2502 "),f=d.map(h=>m+h).join(`
33
- `);return(l.lang?p.dim(`\u2502 ${l.lang}`)+`
32
+ `);d.length>0&&d[d.length-1]===""&&d.pop();let p=m.dim("\u2502 "),f=d.map(h=>p+h).join(`
33
+ `);return(l.lang?m.dim(`\u2502 ${l.lang}`)+`
34
34
  `:"")+f+`
35
- `}case"codespan":{let l=a.text;return mg.test(l)?p.brand(l):p.user(l)}case"strong":{let l=a;return p.bold(l.tokens?o(l.tokens):l.text)}case"em":{let l=a;return p.italic(l.tokens?o(l.tokens):l.text)}case"text":{let l=a;return l.tokens?o(l.tokens):l.text}case"list":{let l=a,c=[],u=l.ordered&&typeof l.start=="number"?l.start:1;for(let d=0;d<l.items.length;d++){let m=l.items[d],f=l.ordered?` ${u+d}. `:" \u2022 ",g=m.tokens?s(m.tokens):m.text,h=[],b=!0,y=ku(f);for(let w of g.trim().split(`
36
- `))b?h.push(p.dim(f)+w):h.push(mu(" ".repeat(y),y,"left")+w),b=!1;c.push(h.join(`
35
+ `}case"codespan":{let l=a.text;return mg.test(l)?m.brand(l):m.user(l)}case"strong":{let l=a;return m.bold(l.tokens?o(l.tokens):l.text)}case"em":{let l=a;return m.italic(l.tokens?o(l.tokens):l.text)}case"text":{let l=a;return l.tokens?o(l.tokens):l.text}case"list":{let l=a,c=[],u=l.ordered&&typeof l.start=="number"?l.start:1;for(let d=0;d<l.items.length;d++){let p=l.items[d],f=l.ordered?` ${u+d}. `:" \u2022 ",g=p.tokens?s(p.tokens):p.text,h=[],b=!0,y=ku(f);for(let w of g.trim().split(`
36
+ `))b?h.push(m.dim(f)+w):h.push(mu(" ".repeat(y),y,"left")+w),b=!1;c.push(h.join(`
37
37
  `))}return c.join(`
38
38
  `)+`
39
39
  `}case"space":return`
40
- `;case"hr":return p.dim("\u2500".repeat(40))+`
41
- `;case"blockquote":{let l=a,c=l.tokens?s(l.tokens):l.text,u=p.dim(" \u2502 "),m=r?Math.max(1,r-4):void 0,f=[];for(let g of c.split(`
42
- `)){let h=m?me(g,m):g;for(let b of h.split(`
40
+ `;case"hr":return m.dim("\u2500".repeat(40))+`
41
+ `;case"blockquote":{let l=a,c=l.tokens?s(l.tokens):l.text,u=m.dim(" \u2502 "),p=r?Math.max(1,r-4):void 0,f=[];for(let g of c.split(`
42
+ `)){let h=p?me(g,p):g;for(let b of h.split(`
43
43
  `))f.push(b?u+b:"")}return f.join(`
44
44
  `)+`
45
- `}case"table":{let l=a,c=D=>D.tokens?o(D.tokens):D.text,u=l.header.map(c),d=l.rows.map(D=>D.map(c)),m=u.length,f=new Array(m).fill(0);for(let D=0;D<m;D++){let _=ku(u[D]??"");for(let M of d)_=Math.max(_,ku(M[D]??""));f[D]=_}let g=r??Number.POSITIVE_INFINITY,h=3*m+1,b=g>=h+m?1:0,y=Math.max(m*b,g-h),w=f.reduce((D,_)=>D+_,0);if(Number.isFinite(g)&&w>y){let D=f.slice(),_=D.reduce((M,P)=>M+Math.max(0,P-b),0);if(_>0){let M=w-y,P=Math.min(1,M/_);for(let $=0;$<D.length;$++){let F=Math.max(0,D[$]-b);D[$]=Math.max(b,D[$]-Math.round(F*P))}}for(let M=0;M<D.length;M++)f[M]=D[M]??f[M]??0}let x=l.align,R=(D,_,M)=>p.dim(D+f.map(P=>"\u2500".repeat(P+2)).join(_)+M),v=(D,_)=>_<=0?[""]:me(D,_).split(`
46
- `).map(P=>ae(P,_)),E=(D,_=!1)=>{let M=D.map((F,k)=>v(_?p.bold(F):F,f[k]??0)),P=Math.max(1,...M.map(F=>F.length)),$=[];for(let F=0;F<P;F++)$.push(p.dim("\u2502")+M.map((k,A)=>" "+y_(k[F]??"",f[A]??0,x[A]??null)+" ").join(p.dim("\u2502"))+p.dim("\u2502"));return $},I=[R("\u250C","\u252C","\u2510")];I.push(...E(u,!0)),I.push(R("\u251C","\u253C","\u2524"));for(let D=0;D<d.length;D++)I.push(...E(d[D])),D<d.length-1&&I.push(R("\u251C","\u253C","\u2524"));return I.push(R("\u2514","\u2534","\u2518")),I.join(`
45
+ `}case"table":{let l=a,c=D=>D.tokens?o(D.tokens):D.text,u=l.header.map(c),d=l.rows.map(D=>D.map(c)),p=u.length,f=new Array(p).fill(0);for(let D=0;D<p;D++){let _=ku(u[D]??"");for(let M of d)_=Math.max(_,ku(M[D]??""));f[D]=_}let g=r??Number.POSITIVE_INFINITY,h=3*p+1,b=g>=h+p?1:0,y=Math.max(p*b,g-h),w=f.reduce((D,_)=>D+_,0);if(Number.isFinite(g)&&w>y){let D=f.slice(),_=D.reduce((M,P)=>M+Math.max(0,P-b),0);if(_>0){let M=w-y,P=Math.min(1,M/_);for(let $=0;$<D.length;$++){let F=Math.max(0,D[$]-b);D[$]=Math.max(b,D[$]-Math.round(F*P))}}for(let M=0;M<D.length;M++)f[M]=D[M]??f[M]??0}let x=l.align,R=(D,_,M)=>m.dim(D+f.map(P=>"\u2500".repeat(P+2)).join(_)+M),v=(D,_)=>_<=0?[""]:me(D,_).split(`
46
+ `).map(P=>ae(P,_)),E=(D,_=!1)=>{let M=D.map((F,k)=>v(_?m.bold(F):F,f[k]??0)),P=Math.max(1,...M.map(F=>F.length)),$=[];for(let F=0;F<P;F++)$.push(m.dim("\u2502")+M.map((k,A)=>" "+y_(k[F]??"",f[A]??0,x[A]??null)+" ").join(m.dim("\u2502"))+m.dim("\u2502"));return $},I=[R("\u250C","\u252C","\u2510")];I.push(...E(u,!0)),I.push(R("\u251C","\u253C","\u2524"));for(let D=0;D<d.length;D++)I.push(...E(d[D])),D<d.length-1&&I.push(R("\u251C","\u253C","\u2524"));return I.push(R("\u2514","\u2534","\u2518")),I.join(`
47
47
  `)+`
48
- `}default:return a.raw}}).join("")}return s(n)}var vu=class{useColors;constructor(t=!0){this.useColors=t&&!!qi.level}formatMarkdown(t){return this.useColors?xt(t):t}formatError(t,n){let r=p.error("\u2717 Error: ")+t;return n&&n.message&&(r+=`
49
- `+p.error(n.message)),n&&n.stack&&T.DEBUG&&(r+=`
50
- `+p.dim(n.stack)),r}formatSuccess(t){return p.success("\u2713 ")+t}formatInfo(t){return p.info("\u2139 ")+t}formatWarning(t){return p.warning("\u26A0 ")+t}formatCommand(t){return p.dim(t)}formatPrompt(t){return p.bold(p.plan(`afk (${t})`))+p.dim(" \u203A ")}formatModelInfo(t,n,r){return p.dim(`Model: ${qi.white(t)} | Max tokens: ${qi.white(n)} | Temperature: ${qi.white(r)}`)}separator(t="\u2500",n=50){return p.dim(t.repeat(n))}formatHelp(t){let n=[];for(let r of t){n.push(p.heading(`
48
+ `}default:return a.raw}}).join("")}return s(n)}var vu=class{useColors;constructor(t=!0){this.useColors=t&&!!qi.level}formatMarkdown(t){return this.useColors?xt(t):t}formatError(t,n){let r=m.error("\u2717 Error: ")+t;return n&&n.message&&(r+=`
49
+ `+m.error(n.message)),n&&n.stack&&T.DEBUG&&(r+=`
50
+ `+m.dim(n.stack)),r}formatSuccess(t){return m.success("\u2713 ")+t}formatInfo(t){return m.info("\u2139 ")+t}formatWarning(t){return m.warning("\u26A0 ")+t}formatCommand(t){return m.dim(t)}formatPrompt(t){return m.bold(m.plan(`afk (${t})`))+m.dim(" \u203A ")}formatModelInfo(t,n,r){return m.dim(`Model: ${qi.white(t)} | Max tokens: ${qi.white(n)} | Temperature: ${qi.white(r)}`)}separator(t="\u2500",n=50){return m.dim(t.repeat(n))}formatHelp(t){let n=[];for(let r of t){n.push(m.heading(`
51
51
  ${r.title}`)),n.push(this.separator());for(let o of r.items)n.push(` ${o}`)}return n.join(`
52
- `)}formatStreaming(t){return this.useColors,t}},lV=new vu;G();var fg=(()=>{let e=T.AFK_USER_CARD_MAX_ROWS,t=e?Number.parseInt(e,10):NaN;return Number.isFinite(t)&&t>0?t:24})(),S_={plan:p.plan,status:p.info,checkpoint:p.success,diagnosis:p.warning},k_={plan:"PLAN",status:"STATUS",checkpoint:"\u2705 CHECKPOINT",diagnosis:"DIAGNOSIS"};function Rn(e){let t=Array.isArray(e.body)?e.body:e.body.split(`
52
+ `)}formatStreaming(t){return this.useColors,t}},lV=new vu;G();var fg=(()=>{let e=T.AFK_USER_CARD_MAX_ROWS,t=e?Number.parseInt(e,10):NaN;return Number.isFinite(t)&&t>0?t:24})(),S_={plan:m.plan,status:m.info,checkpoint:m.success,diagnosis:m.warning},k_={plan:"PLAN",status:"STATUS",checkpoint:"\u2705 CHECKPOINT",diagnosis:"DIAGNOSIS"};function Rn(e){let t=Array.isArray(e.body)?e.body:e.body.split(`
53
53
  `);if(e.kind==="user")return v_(t);let n=e.title??k_[e.kind];return T_(e.kind,n,t)}function v_(e){let t=Y(),n=Math.max(20,t-4),r=[];for(let i of e)r.push(...me(En(i),n).split(`
54
- `));let o=r;if(r.length>fg){let i=fg-1,a=r.length-i;o=[...r.slice(0,i),p.dim(`\u2026(${a} lines collapsed)`)]}let s=p.user("\u2502");return o.map(i=>{let a=z(i),l=Math.max(0,t-a-2);return" ".repeat(l)+i+" "+s}).join(`
55
- `)}function T_(e,t,n){let r=S_[e],o=n.map(En),s=Math.max(z(t)+4,...o.map(l=>z(l))),i=Math.max(40,s)+4,a=Math.min(i,Math.min(Y()-4,100));return a=Math.min(a,mt()),jr(o,{border:r,title:t,width:a,padding:2})}function he(e){let t=Math.min(Y(),120);if(e===void 0)return p.dim("\u2500".repeat(t));let n=p.dim("\u2500\u2500")+" "+p.bold(e)+" ",r="\u2500\u2500 "+e+" ",o=Math.max(0,t-z(r));return n+p.dim("\u2500".repeat(o))}G();function je(){return T.AFK_DEBUG==="1"||T.DEBUG==="1"}function J(...e){je()&&console.log(...e)}function Wr(e,t){let n=t?.isTTY??process.stdout.isTTY??!1,r=t?.write??(o=>{process.stderr.write(o)});if(n)r(Gi(e.userMessage,e.hint)+`
54
+ `));let o=r;if(r.length>fg){let i=fg-1,a=r.length-i;o=[...r.slice(0,i),m.dim(`\u2026(${a} lines collapsed)`)]}let s=m.user("\u2502");return o.map(i=>{let a=z(i),l=Math.max(0,t-a-2);return" ".repeat(l)+i+" "+s}).join(`
55
+ `)}function T_(e,t,n){let r=S_[e],o=n.map(En),s=Math.max(z(t)+4,...o.map(l=>z(l))),i=Math.max(40,s)+4,a=Math.min(i,Math.min(Y()-4,100));return a=Math.min(a,mt()),jr(o,{border:r,title:t,width:a,padding:2})}function he(e){let t=Math.min(Y(),120);if(e===void 0)return m.dim("\u2500".repeat(t));let n=m.dim("\u2500\u2500")+" "+m.bold(e)+" ",r="\u2500\u2500 "+e+" ",o=Math.max(0,t-z(r));return n+m.dim("\u2500".repeat(o))}G();function je(){return T.AFK_DEBUG==="1"||T.DEBUG==="1"}function J(...e){je()&&console.log(...e)}function Wr(e,t){let n=t?.isTTY??process.stdout.isTTY??!1,r=t?.write??(o=>{process.stderr.write(o)});if(n)r(Gi(e.userMessage,e.hint)+`
56
56
  `);else{let o=e.hint?` (${e.hint})`:"";r(`afk: error: ${e.userMessage}${o}
57
57
  `)}je()&&e.raw instanceof Error&&e.raw.stack&&r(e.raw.stack+`
58
58
  `)}function K(e){let t=Br(e);Wr(t),process.exit(t.exitCode)}import*as yS from"node:os";import*as bS from"node:path";async function Tu(e,t){if(e)try{await e.write({kind:"tool_call",payload:t})}catch(n){J(`trace.emit tool_call failed: ${Qt(n)}`)}}async function Hr(e,t){if(e)try{await e.write({kind:"hook_decision",payload:t})}catch(n){J(`trace.emit hook_decision failed: ${Qt(n)}`)}}async function er(e,t){if(e)try{await e.write({kind:"subagent_lifecycle",payload:t})}catch(n){J(`trace.emit subagent_lifecycle failed: ${Qt(n)}`)}}async function Kr(e,t){if(e)try{await e.write({kind:"background_agent",payload:t})}catch(n){J(`trace.emit background_agent failed: ${Qt(n)}`)}}async function gg(e,t){if(e)try{await e.write({kind:"budget",payload:t})}catch(n){J(`trace.emit budget failed: ${Qt(n)}`)}}async function hg(e,t){if(e)try{await e.write({kind:"abort",payload:t})}catch(n){J(`trace.emit abort failed: ${Qt(n)}`)}}async function yg(e,t){if(e)try{await e.write({kind:"compaction",payload:t})}catch(n){J(`trace.emit compaction failed: ${Qt(n)}`)}}async function bg(e,t){if(e)try{await e.write({kind:"closure",payload:t})}catch(n){J(`trace.emit closure failed: ${Qt(n)}`)}}async function Gr(e,t){if(e)try{await e.write({kind:"session_phase",payload:t})}catch(n){J(`trace.emit session_phase failed: ${Qt(n)}`)}}function Qt(e){return e instanceof Error?e.message:String(e)}import Vd from"path";import{appendFileSync as ID,mkdirSync as PD}from"fs";import{dirname as MD}from"path";import xw from"@anthropic-ai/sdk";var wg="claude-code-20250219,oauth-2025-04-20,interleaved-thinking-2025-05-14,extended-cache-ttl-2025-04-11",x_="effort-2025-11-24",E_="claude-cli/1.0.0 (external, cli)",R_="x-anthropic-billing-header: cc_version=1.0.0.test; cc_entrypoint=cli; cch=00000;";function zi(e){return e.startsWith("sk-ant-oat01-")?"oauth":"api-key"}function ms(e,t,n){let r=t==="oauth"?{authToken:e}:{apiKey:e};return typeof n=="string"&&n.length>0?{...r,baseURL:n}:r}function Lt(e,t,n,r){return e!=="oauth"?{}:{"anthropic-beta":r?`${wg},${x_}`:wg,"x-app":"cli","User-Agent":E_,"X-Claude-Code-Session-Id":t,"x-client-request-id":n}}function Sg(e){return e!=="oauth"?null:[{type:"text",text:R_}]}import{execFileSync as kg}from"child_process";import{existsSync as A_,readFileSync as __,writeFileSync as C_}from"fs";import{homedir as vg,userInfo as Tg}from"os";import{join as xg}from"path";var I_="9d1c250a-e61b-44d9-88ed-5944d1962f5e",P_="https://platform.claude.com/v1/oauth/token",M_=300*1e3;function ze(){let e=Eg();if(e===void 0)return;let t=Rg(e);if(t!==void 0){if(t.expiresAt!==void 0&&t.expiresAt<=Date.now()){process.stderr.write("agent-afk: Claude Code OAuth token in keychain is expired. Run `claude login` to refresh.\n");return}return t.accessToken}}async function xu(){let e=Eg();if(e===void 0)return;let t=Rg(e);if(t===void 0)return;if(t.expiresAt!==void 0&&t.expiresAt>Date.now()+M_)return t.accessToken;if(!t.refreshToken){process.stderr.write("agent-afk: OAuth token expired and no refresh token available. Run `claude login` to refresh.\n");return}let n=await O_(t.refreshToken);if(!n){process.stderr.write("agent-afk: OAuth token refresh failed. Run `claude login` to refresh.\n");return}try{let r={};try{r=JSON.parse(e)}catch{}let o=r.claudeAiOauth??{};r.claudeAiOauth={...o,accessToken:n.accessToken,expiresAt:n.expiresAt,...n.refreshToken!==void 0?{refreshToken:n.refreshToken}:{}},$_(JSON.stringify(r))}catch{process.stderr.write(`agent-afk: Refreshed OAuth token but failed to write back to credential store.
@@ -1032,30 +1032,30 @@ SECURITY NOTE: upstream node output injected into downstream prompts is user-con
1032
1032
  ${n.join(`
1033
1033
  `)}`}import{spawnSync as yC}from"child_process";var bC={branch:null,headSha:null,dirty:null,dirtyCount:null,remoteUrl:null};function ea(e,t){try{let n=yC("git",t,{cwd:e,encoding:"utf8",maxBuffer:4096,shell:!1});if(n.status!==0||n.signal!==null||n.error!==void 0)return null;let r=typeof n.stdout=="string"?n.stdout.trim():null;return r!==null&&r.length>0?r:null}catch{return null}}function Ou(e){let t=ea(e,["rev-parse","--short","HEAD"]);if(t===null)return{...bC};let n=ea(e,["symbolic-ref","--short","HEAD"]),r=ea(e,["status","--porcelain"]),o=!1,s=0;if(r!==null){let a=r.split(`
1034
1034
  `).filter(l=>l.trim().length>0);o=a.length>0,s=a.length}r===null&&(o=null,s=null);let i=ea(e,["remote","get-url","origin"]);return{branch:n,headSha:t,dirty:o,dirtyCount:s,remoteUrl:i}}function hs(e){let t=Ou(e.cwd);return{getSelf(){return{sessionId:e.sessionId??null,surface:SC(e.surface),parentSessionId:e.parentSessionId??null,depth:e.depth??null,maxDepth:e.maxDepth??null,phaseRole:e.phaseRole??null,cwd:e.cwd,model:{provider:e.providerName,name:e.modelName},permissionMode:wC(e.permissionMode)}},getTools(){return{enabled:e.getEnabledToolNames(),mcpServers:kC(e.getMcpTools())}},getSubagents(){return e.getSubagents()},getWorkspace(){return t}}}function wC(e){switch(e){case"bypassPermissions":case"acceptEdits":case"dontAsk":case"auto":return"elevated";default:return"default"}}function SC(e){switch(e){case"cli":case"repl":case"daemon":case"telegram":case"subagent":return e;default:return"unknown"}}function kC(e){let t=new Map;for(let n of e){if(!n.name.startsWith("mcp__"))continue;let r=n.name.split("__");if(r.length<3)continue;let o=r[1];typeof o!="string"||o.length===0||t.set(o,(t.get(o)??0)+1)}return[...t.entries()].map(([n,r])=>({name:n,toolCount:r})).sort((n,r)=>n.name.localeCompare(r.name))}var Nt={name:"get_runtime_state",category:"other",concurrencySafe:!0,description:"Inspect what the runtime knows about this session: identity (sessionId, surface, depth, parent), tool affordances (currently-enabled tool names and MCP server summary), delegation state (active subagent handles, background jobs), and git workspace state (branch, HEAD SHA, dirty count, remote URL). Returns a compact JSON snapshot.\n\nUse when uncertain about: your current nesting depth, whether a tool you want is actually available right now, what MCP servers are wired, whether earlier subagents you dispatched are still running, or what git branch / commit the session started on.\n\nViews:\n- `self` \u2014 identity + model + permissions + cwd only\n- `tools` \u2014 enabled tool names + MCP server summary only\n- `subagents` \u2014 active subagent handles + background jobs only\n- `workspace` \u2014 git state (branch, headSha, dirty, dirtyCount, remoteUrl)\n- `all` \u2014 union of the four above (default)\n\nThis is a read-only, in-memory inspection. It does not probe the file system or network. Fields the runtime does not know (e.g. depth for a top-level session) come back as `null` rather than synthesised defaults.",input_schema:{type:"object",properties:{view:{type:"string",enum:["self","tools","subagents","workspace","all"],description:'Which slice of state to return. Defaults to "all". Use a narrower view when only one slice is needed to keep the response compact.'}},required:[]}},st=[Nt.name];function Jr(e){return async(t,n)=>{let r=t&&typeof t=="object"?Mu(t.view):"all",o=Pu(e,r);return{content:JSON.stringify(o)}}}function ys(e,t){let n=Jr(t),r=e,o=Array.isArray(r.toolDefs)?r.toolDefs:null,s={async execute(i){return i.name==="get_runtime_state"?n(i.input,i.signal):e.execute(i)}};if(o!==null){let i=o.some(a=>a.name==="get_runtime_state");s.toolDefs=i?o:[...o,Nt]}return s}W();import{mkdir as vC,writeFile as TC,unlink as L4,readdir as F4,readFile as N4}from"fs/promises";import{unlinkSync as xC,existsSync as EC}from"fs";import{join as RC}from"path";function jg(e){return RC(iu(),`${e}.json`)}async function AC(){try{return await vC(iu(),{recursive:!0}),!0}catch{return!1}}async function bs(e){try{if(!await AC())return;let n=jg(e.sessionId);await TC(n,JSON.stringify(e,null,2),"utf8")}catch{}}function nn(e){try{let t=jg(e);EC(t)&&xC(t)}catch{}}var PC=new Set([...en,nr,rr,or,...Cn,Nt].filter(e=>e.concurrencySafe===!0).map(e=>e.name));function MC(e){return PC.has(e)}function OC(e,t){return e.reduce((n,r,o)=>{let s=t(r.name,r.input),i=n[n.length-1];return i&&s&&i.isConcurrencySafe?i.indices.push(o):n.push({isConcurrencySafe:s,indices:[o]}),n},[])}var rn=class{handlers;schemas;hookRegistry;permissions;subagentExecutor;skillExecutor;composeExecutor;classifier;resolveBase;_readRoots;_writeRoots;_env;sessionId;parentSessionId;traceWriter;constructor(t){this.handlers=t.handlers,this.schemas=t.schemas,this.hookRegistry=t.hookRegistry,this.permissions=t.permissions,this.subagentExecutor=t.subagentExecutor,this.skillExecutor=t.skillExecutor,this.composeExecutor=t.composeExecutor,this.classifier=t.concurrencyClassifier??MC,this.resolveBase=t.cwd,this._env=t.env,this.sessionId=t.sessionId,this.parentSessionId=t.parentSessionId,this.traceWriter=t.traceWriter;let n=t.cwd?[t.cwd]:[];this._readRoots=t.readRoots??n.slice(),this._writeRoots=t.writeRoots??n.slice()}get handlerContext(){return{cwd:this.resolveBase,resolveBase:this.resolveBase,readRoots:this._readRoots.slice(),writeRoots:this._writeRoots.slice(),...this._env!==void 0?{env:this._env}:{}}}addReadRoot(t,n="slash"){let r=$u.resolve(t);this._readRoots.includes(r)||this._readRoots.push(r),this.appendAuditLog({action:"grant-read",path:r,source:n})}addWriteRoot(t,n="slash"){let r=$u.resolve(t);this._readRoots.includes(r)||this._readRoots.push(r),this._writeRoots.includes(r)||this._writeRoots.push(r),this.appendAuditLog({action:"grant-write",path:r,source:n})}revokeRoot(t,n="slash"){let r=$u.resolve(t);if(r===this.resolveBase)return;let o=this._readRoots.indexOf(r);o!==-1&&this._readRoots.splice(o,1);let s=this._writeRoots.indexOf(r);s!==-1&&this._writeRoots.splice(s,1),this.appendAuditLog({action:"revoke",path:r,source:n})}getGrants(){return{resolveBase:this.resolveBase,readRoots:this._readRoots.slice(),writeRoots:this._writeRoots.slice()}}setResolveBase(t){let n=this.resolveBase;if(n!==t)if(this.resolveBase=t,n!==void 0){let r=this._readRoots.indexOf(n);r!==-1?this._readRoots[r]=t:this._readRoots.includes(t)||this._readRoots.push(t);let o=this._writeRoots.indexOf(n);o!==-1?this._writeRoots[o]=t:this._writeRoots.includes(t)||this._writeRoots.push(t)}else this._readRoots.includes(t)||this._readRoots.push(t),this._writeRoots.includes(t)||this._writeRoots.push(t)}appendAuditLog(t){try{let n=Fr();CC(IC(n),{recursive:!0});let r=JSON.stringify({timestamp:new Date().toISOString(),sessionId:this.sessionId??null,action:t.action,path:t.path,source:t.source});_C(n,r+`
1035
- `)}catch{}}get toolDefs(){return this.schemas}async execute(t){if(t.signal.aborted)return{content:"Tool call aborted",isError:!0};if(this.hookRegistry){let s={event:"PreToolUse",toolName:t.name,input:t.input,...this.parentSessionId!==void 0?{parentSessionId:this.parentSessionId}:{}};try{await Cu(this.hookRegistry,s,{signal:t.signal,...this.traceWriter?{traceWriter:this.traceWriter}:{}})}catch(i){if(i instanceof Ae)return{content:`Tool "${t.name}" blocked by PreToolUse hook: ${i.message}`,isError:!0};throw i}}let n=Iu(t.name,this.permissions);if(!n.allowed)return{content:n.reason??`Tool "${t.name}" is not permitted`,isError:!0};if(t.name==="agent"){if(!this.subagentExecutor)return{content:"Agent tool is not available in this session configuration",isError:!0};let s;try{s=await this.subagentExecutor.execute(t)}catch(i){s={content:`Agent tool error: ${i instanceof Error?i.message:String(i)}`,isError:!0}}return this.firePostToolUse(t.name,s.content,t.signal),s}if(t.name==="skill"){if(!this.skillExecutor)return{content:"Skill tool is not available in this session configuration",isError:!0};let s;try{s=await this.skillExecutor.execute(t)}catch(i){s={content:`Skill tool error: ${i instanceof Error?i.message:String(i)}`,isError:!0}}return this.firePostToolUse(t.name,s.content,t.signal),s}if(t.name==="compose"){let s=await this.executeCompose(t);return this.firePostToolUse(t.name,s.content,t.signal),s}let r=this.handlers.get(t.name);if(!r)return{content:`Unknown tool "${t.name}". Available tools: ${[...this.handlers.keys()].join(", ")}`,isError:!0};let o;try{o=await r(t.input,t.signal,this.handlerContext)}catch(s){o={content:`Tool execution error: ${s instanceof Error?s.message:String(s)}`,isError:!0}}return this.firePostToolUse(t.name,o.content,t.signal),o}async executeBatch(t){if(t.length===0)return[];if(t.length===1)return[await this.execute(t[0])];let n=new Array(t.length),r=new Set;for(let i=0;i<t.length;i++){let a=t[i];if(a.signal.aborted){n[i]={content:"Tool call aborted",isError:!0},r.add(i);continue}if(this.hookRegistry){let c={event:"PreToolUse",toolName:a.name,input:a.input,...this.parentSessionId!==void 0?{parentSessionId:this.parentSessionId}:{}};try{await Cu(this.hookRegistry,c,{signal:a.signal,...this.traceWriter?{traceWriter:this.traceWriter}:{}})}catch(u){if(u instanceof Ae){n[i]={content:`Tool "${a.name}" blocked by PreToolUse hook: ${u.message}`,isError:!0},r.add(i);continue}throw u}}let l=Iu(a.name,this.permissions);l.allowed||(n[i]={content:l.reason??`Tool "${a.name}" is not permitted`,isError:!0},r.add(i))}let o=t.map((i,a)=>({call:i,originalIndex:a})).filter((i,a)=>!r.has(a));if(o.length===0)return n;let s=OC(o.map(i=>i.call),this.classifier);for(let i of s)if(i.isConcurrencySafe){let a=await Promise.allSettled(i.indices.map(async l=>{let{call:c,originalIndex:u}=o[l];return c.signal.aborted?{result:{content:"Tool call aborted",isError:!0},originalIndex:u}:{result:await this.executeCore(c),originalIndex:u}}));for(let l of a)if(l.status==="fulfilled")n[l.value.originalIndex]=l.value.result;else{let c=l.reason instanceof Error?l.reason.message:String(l.reason),u=i.indices[a.indexOf(l)];n[o[u].originalIndex]={content:`Tool execution error: ${c}`,isError:!0}}}else for(let a of i.indices){let{call:l,originalIndex:c}=o[a];if(l.signal.aborted){n[c]={content:"Tool call aborted",isError:!0};continue}n[c]=await this.executeCore(l)}return n}async executeCore(t){if(t.name==="agent"){if(!this.subagentExecutor)return{content:"Agent tool is not available in this session configuration",isError:!0};let o;try{o=await this.subagentExecutor.execute(t)}catch(s){o={content:`Agent tool error: ${s instanceof Error?s.message:String(s)}`,isError:!0}}return this.firePostToolUse(t.name,o.content,t.signal),o}if(t.name==="skill"){if(!this.skillExecutor)return{content:"Skill tool is not available in this session configuration",isError:!0};let o;try{o=await this.skillExecutor.execute(t)}catch(s){o={content:`Skill tool error: ${s instanceof Error?s.message:String(s)}`,isError:!0}}return this.firePostToolUse(t.name,o.content,t.signal),o}if(t.name==="compose"){let o=await this.executeCompose(t);return this.firePostToolUse(t.name,o.content,t.signal),o}let n=this.handlers.get(t.name);if(!n)return{content:`Unknown tool "${t.name}". Available tools: ${[...this.handlers.keys()].join(", ")}`,isError:!0};let r;try{r=await n(t.input,t.signal,this.handlerContext)}catch(o){r={content:`Tool execution error: ${o instanceof Error?o.message:String(o)}`,isError:!0}}return this.firePostToolUse(t.name,r.content,t.signal),r}async executeCompose(t){if(!this.composeExecutor)return{content:"Compose tool is not available in this session configuration",isError:!0};try{return await this.composeExecutor.execute(t)}catch(n){return{content:`Compose tool error: ${n instanceof Error?n.message:String(n)}`,isError:!0}}}firePostToolUse(t,n,r){if(!this.hookRegistry)return;let o={event:"PostToolUse",toolName:t,output:n};Lg(this.hookRegistry,o,{signal:r,...this.traceWriter?{traceWriter:this.traceWriter}:{}}).catch(()=>{})}};import{spawn as QC}from"child_process";var $C=/Tests\s+(\d+)\s+passed(?:\s*\|\s*(\d+)\s+failed)?(?:\s*\|\s*(\d+)\s+skipped)?/,DC=/Tests:\s+(?:(\d+)\s+failed,\s*)?(\d+)\s+passed,\s*\d+\s+total/,LC=/={3,}\s*(?:(\d+)\s+failed,\s*)?(\d+)\s+passed(?:,\s*(\d+)\s+warning)?.*in\s+[\d.]+s\s*={3,}|={3,}\s*(\d+)\s+passed.*in\s+[\d.]+s\s*={3,}/,FC=/(\d+)\s+passing/,NC=/(\d+)\s+failing/,BC=/^(ok|FAIL)\s+\S+\s+[\d.]+s/gm,UC=/test result: (?:ok|FAILED)\. (\d+) passed; (\d+) failed(?:; (\d+) ignored)?/,jC=/(\d+) examples?, (\d+) failures?/,WC=/OK \((\d+) tests?/,HC=/Tests:\s*(\d+)[^]*?Failures:\s*(\d+)/;function KC(e){let t=e.match($C);if(!t)return null;let n=parseInt(t[1]??"0",10),r=parseInt(t[2]??"0",10),o=t[3]!==void 0?parseInt(t[3],10):void 0;return{runner:"vitest",passed:n,failed:r,...o!==void 0?{skipped:o}:{}}}function GC(e){let t=e.match(DC);if(!t)return null;let n=parseInt(t[1]??"0",10);return{runner:"jest",passed:parseInt(t[2]??"0",10),failed:n}}function qC(e){let t=e.match(LC);if(!t)return null;if(t[2]!==void 0){let n=parseInt(t[2],10),r=parseInt(t[1]??"0",10);return{runner:"pytest",passed:n,failed:r}}return t[4]!==void 0?{runner:"pytest",passed:parseInt(t[4],10),failed:0}:null}function zC(e){let t=e.match(FC);if(!t)return null;let n=parseInt(t[1]??"0",10),r=e.match(NC),o=r?parseInt(r[1]??"0",10):0;return{runner:"mocha",passed:n,failed:o}}function JC(e){let t=[...e.matchAll(BC)];if(t.length===0)return null;let n=0,r=0;for(let o of t)o[1]==="ok"?n++:o[1]==="FAIL"&&r++;return{runner:"go-test",passed:n,failed:r}}function VC(e){let t=e.match(UC);if(!t)return null;let n=parseInt(t[1]??"0",10),r=parseInt(t[2]??"0",10),o=t[3]!==void 0?parseInt(t[3],10):void 0;return{runner:"cargo",passed:n,failed:r,...o!==void 0?{skipped:o}:{}}}function YC(e){let t=e.match(jC);if(!t)return null;let n=parseInt(t[1]??"0",10),r=parseInt(t[2]??"0",10);return{runner:"rspec",passed:n-r,failed:r}}function XC(e){let t=e.match(WC);if(t)return{runner:"phpunit",passed:parseInt(t[1]??"0",10),failed:0};let n=e.match(HC);if(n){let r=parseInt(n[1]??"0",10),o=parseInt(n[2]??"0",10);return{runner:"phpunit",passed:r-o,failed:o}}return null}function Du(e){return KC(e)??GC(e)??qC(e)??zC(e)??JC(e)??VC(e)??YC(e)??XC(e)??null}var Wg=/\x1B\][^\x07\x1B]*(?:\x07|\x1B\\)|\x1B[P^_X][^\x1B]*\x1B\\|\x1B\[[0-?]*[ -/]*[@-~]|\x9B[0-?]*[ -/]*[@-~]|\x1B[@-_]/g,ZC=/[\x00-\x1F\x7F-\x9F]/g;function Hg(e){return e.replace(Wg,"").replace(ZC," ").trim()}function on(e){return e.replace(Wg,"")}function eI(e){if(typeof e!="object"||e===null)throw new Error("Input must be an object");let t=e;if(typeof t.command!="string")throw new Error('Input must have a "command" field of type string');let n=12e4;if(t.timeout_ms!==void 0){if(typeof t.timeout_ms!="number")throw new Error("timeout_ms must be a number");if(t.timeout_ms<0||t.timeout_ms>6e5)throw new Error("timeout_ms must be between 0 and 600000");n=t.timeout_ms}return{command:t.command,timeout_ms:n}}function ta(e,t){let n=!1;function r(){n||e==="bypassPermissions"&&(n=!0,console.warn("[security] bash handler: shell=true with bypassPermissions \u2014 all shell metacharacters are interpreted without confirmation. Migrate to execFile to eliminate this risk (tracked: C4)."))}return async(o,s,i)=>{let{command:a,timeout_ms:l}=eI(o);return s.aborted?{content:"Command aborted",isError:!0}:(r(),new Promise(c=>{let u=!1;function d(v){u||(u=!0,clearTimeout(f),s.removeEventListener("abort",R),c(v))}let m=QC(a,{shell:!0,detached:!0,stdio:["ignore","pipe","pipe"],...(i?.resolveBase??i?.cwd??t)!==void 0?{cwd:i?.resolveBase??i?.cwd??t}:{},...i?.env!==void 0?{env:{...process.env,...i.env}}:{}});m.unref();let f=setTimeout(()=>{m.pid!==void 0&&process.kill(-m.pid,"SIGKILL"),d({content:`Command timed out after ${l}ms`,isError:!0})},l),g="",h="",b=1e5,y=0,w=!1;function x(v){if(w||u||y<b)return;w=!0,console.warn(`[bash] overflow kill: stream=${v} totalBytes=${y} command="${a}"`),Te({event:"tool.overflow_kill",tool:"bash",total_bytes:y,stream:v}),m.kill("SIGKILL");let E=(g+h).trimEnd();E=on(E);let I=Du(E)??void 0;E.length>b&&(E=E.slice(0,b)),E+=`
1036
- [output truncated \u2014 exceeded 100KB]`,d({content:E,truncated:!0,...I!==void 0?{testResult:I}:{}})}m.stdout.on("data",v=>{let E=b-y,I=v.length<=E?v:v.subarray(0,Math.max(0,E));y+=I.length,g+=I.toString("utf8"),x("stdout")}),m.stderr.on("data",v=>{let E=b-y,I=v.length<=E?v:v.subarray(0,Math.max(0,E));y+=I.length,h+=I.toString("utf8"),x("stderr")});let R=()=>{m.pid!==void 0&&process.kill(-m.pid,"SIGKILL"),d({content:"Command aborted",isError:!0})};s.addEventListener("abort",R),m.on("close",v=>{if(s.aborted){d({content:"Command aborted",isError:!0});return}if(v!==null&&v!==0){let _=h.trimEnd()||g.trimEnd();d({content:`Command exited with code ${v}${_?`
1035
+ `)}catch{}}get toolDefs(){return this.schemas}async execute(t){if(t.signal.aborted)return{content:"Tool call aborted",isError:!0};if(this.hookRegistry){let s={event:"PreToolUse",toolName:t.name,input:t.input,...this.parentSessionId!==void 0?{parentSessionId:this.parentSessionId}:{}};try{await Cu(this.hookRegistry,s,{signal:t.signal,...this.traceWriter?{traceWriter:this.traceWriter}:{}})}catch(i){if(i instanceof Ae)return{content:`Tool "${t.name}" blocked by PreToolUse hook: ${i.message}`,isError:!0};throw i}}let n=Iu(t.name,this.permissions);if(!n.allowed)return{content:n.reason??`Tool "${t.name}" is not permitted`,isError:!0};if(t.name==="agent"){if(!this.subagentExecutor)return{content:"Agent tool is not available in this session configuration",isError:!0};let s;try{s=await this.subagentExecutor.execute(t)}catch(i){s={content:`Agent tool error: ${i instanceof Error?i.message:String(i)}`,isError:!0}}return this.firePostToolUse(t.name,s.content,t.signal),s}if(t.name==="skill"){if(!this.skillExecutor)return{content:"Skill tool is not available in this session configuration",isError:!0};let s;try{s=await this.skillExecutor.execute(t)}catch(i){s={content:`Skill tool error: ${i instanceof Error?i.message:String(i)}`,isError:!0}}return this.firePostToolUse(t.name,s.content,t.signal),s}if(t.name==="compose"){let s=await this.executeCompose(t);return this.firePostToolUse(t.name,s.content,t.signal),s}let r=this.handlers.get(t.name);if(!r)return{content:`Unknown tool "${t.name}". Available tools: ${[...this.handlers.keys()].join(", ")}`,isError:!0};let o;try{o=await r(t.input,t.signal,this.handlerContext)}catch(s){o={content:`Tool execution error: ${s instanceof Error?s.message:String(s)}`,isError:!0}}return this.firePostToolUse(t.name,o.content,t.signal),o}async executeBatch(t){if(t.length===0)return[];if(t.length===1)return[await this.execute(t[0])];let n=new Array(t.length),r=new Set;for(let i=0;i<t.length;i++){let a=t[i];if(a.signal.aborted){n[i]={content:"Tool call aborted",isError:!0},r.add(i);continue}if(this.hookRegistry){let c={event:"PreToolUse",toolName:a.name,input:a.input,...this.parentSessionId!==void 0?{parentSessionId:this.parentSessionId}:{}};try{await Cu(this.hookRegistry,c,{signal:a.signal,...this.traceWriter?{traceWriter:this.traceWriter}:{}})}catch(u){if(u instanceof Ae){n[i]={content:`Tool "${a.name}" blocked by PreToolUse hook: ${u.message}`,isError:!0},r.add(i);continue}throw u}}let l=Iu(a.name,this.permissions);l.allowed||(n[i]={content:l.reason??`Tool "${a.name}" is not permitted`,isError:!0},r.add(i))}let o=t.map((i,a)=>({call:i,originalIndex:a})).filter((i,a)=>!r.has(a));if(o.length===0)return n;let s=OC(o.map(i=>i.call),this.classifier);for(let i of s)if(i.isConcurrencySafe){let a=await Promise.allSettled(i.indices.map(async l=>{let{call:c,originalIndex:u}=o[l];return c.signal.aborted?{result:{content:"Tool call aborted",isError:!0},originalIndex:u}:{result:await this.executeCore(c),originalIndex:u}}));for(let l of a)if(l.status==="fulfilled")n[l.value.originalIndex]=l.value.result;else{let c=l.reason instanceof Error?l.reason.message:String(l.reason),u=i.indices[a.indexOf(l)];n[o[u].originalIndex]={content:`Tool execution error: ${c}`,isError:!0}}}else for(let a of i.indices){let{call:l,originalIndex:c}=o[a];if(l.signal.aborted){n[c]={content:"Tool call aborted",isError:!0};continue}n[c]=await this.executeCore(l)}return n}async executeCore(t){if(t.name==="agent"){if(!this.subagentExecutor)return{content:"Agent tool is not available in this session configuration",isError:!0};let o;try{o=await this.subagentExecutor.execute(t)}catch(s){o={content:`Agent tool error: ${s instanceof Error?s.message:String(s)}`,isError:!0}}return this.firePostToolUse(t.name,o.content,t.signal),o}if(t.name==="skill"){if(!this.skillExecutor)return{content:"Skill tool is not available in this session configuration",isError:!0};let o;try{o=await this.skillExecutor.execute(t)}catch(s){o={content:`Skill tool error: ${s instanceof Error?s.message:String(s)}`,isError:!0}}return this.firePostToolUse(t.name,o.content,t.signal),o}if(t.name==="compose"){let o=await this.executeCompose(t);return this.firePostToolUse(t.name,o.content,t.signal),o}let n=this.handlers.get(t.name);if(!n)return{content:`Unknown tool "${t.name}". Available tools: ${[...this.handlers.keys()].join(", ")}`,isError:!0};let r;try{r=await n(t.input,t.signal,this.handlerContext)}catch(o){r={content:`Tool execution error: ${o instanceof Error?o.message:String(o)}`,isError:!0}}return this.firePostToolUse(t.name,r.content,t.signal),r}async executeCompose(t){if(!this.composeExecutor)return{content:"Compose tool is not available in this session configuration",isError:!0};try{return await this.composeExecutor.execute(t)}catch(n){return{content:`Compose tool error: ${n instanceof Error?n.message:String(n)}`,isError:!0}}}firePostToolUse(t,n,r){if(!this.hookRegistry)return;let o={event:"PostToolUse",toolName:t,output:n};Lg(this.hookRegistry,o,{signal:r,...this.traceWriter?{traceWriter:this.traceWriter}:{}}).catch(()=>{})}};import{spawn as QC}from"child_process";var $C=/Tests\s+(\d+)\s+passed(?:\s*\|\s*(\d+)\s+failed)?(?:\s*\|\s*(\d+)\s+skipped)?/,DC=/Tests:\s+(?:(\d+)\s+failed,\s*)?(\d+)\s+passed,\s*\d+\s+total/,LC=/={3,}\s*(?:(\d+)\s+failed,\s*)?(\d+)\s+passed(?:,\s*(\d+)\s+warning)?.*in\s+[\d.]+s\s*={3,}|={3,}\s*(\d+)\s+passed.*in\s+[\d.]+s\s*={3,}/,FC=/(\d+)\s+passing/,NC=/(\d+)\s+failing/,BC=/^(ok|FAIL)\s+\S+\s+[\d.]+s/gm,UC=/test result: (?:ok|FAILED)\. (\d+) passed; (\d+) failed(?:; (\d+) ignored)?/,jC=/(\d+) examples?, (\d+) failures?/,WC=/OK \((\d+) tests?/,HC=/Tests:\s*(\d+)[^]*?Failures:\s*(\d+)/;function KC(e){let t=e.match($C);if(!t)return null;let n=parseInt(t[1]??"0",10),r=parseInt(t[2]??"0",10),o=t[3]!==void 0?parseInt(t[3],10):void 0;return{runner:"vitest",passed:n,failed:r,...o!==void 0?{skipped:o}:{}}}function GC(e){let t=e.match(DC);if(!t)return null;let n=parseInt(t[1]??"0",10);return{runner:"jest",passed:parseInt(t[2]??"0",10),failed:n}}function qC(e){let t=e.match(LC);if(!t)return null;if(t[2]!==void 0){let n=parseInt(t[2],10),r=parseInt(t[1]??"0",10);return{runner:"pytest",passed:n,failed:r}}return t[4]!==void 0?{runner:"pytest",passed:parseInt(t[4],10),failed:0}:null}function zC(e){let t=e.match(FC);if(!t)return null;let n=parseInt(t[1]??"0",10),r=e.match(NC),o=r?parseInt(r[1]??"0",10):0;return{runner:"mocha",passed:n,failed:o}}function JC(e){let t=[...e.matchAll(BC)];if(t.length===0)return null;let n=0,r=0;for(let o of t)o[1]==="ok"?n++:o[1]==="FAIL"&&r++;return{runner:"go-test",passed:n,failed:r}}function VC(e){let t=e.match(UC);if(!t)return null;let n=parseInt(t[1]??"0",10),r=parseInt(t[2]??"0",10),o=t[3]!==void 0?parseInt(t[3],10):void 0;return{runner:"cargo",passed:n,failed:r,...o!==void 0?{skipped:o}:{}}}function YC(e){let t=e.match(jC);if(!t)return null;let n=parseInt(t[1]??"0",10),r=parseInt(t[2]??"0",10);return{runner:"rspec",passed:n-r,failed:r}}function XC(e){let t=e.match(WC);if(t)return{runner:"phpunit",passed:parseInt(t[1]??"0",10),failed:0};let n=e.match(HC);if(n){let r=parseInt(n[1]??"0",10),o=parseInt(n[2]??"0",10);return{runner:"phpunit",passed:r-o,failed:o}}return null}function Du(e){return KC(e)??GC(e)??qC(e)??zC(e)??JC(e)??VC(e)??YC(e)??XC(e)??null}var Wg=/\x1B\][^\x07\x1B]*(?:\x07|\x1B\\)|\x1B[P^_X][^\x1B]*\x1B\\|\x1B\[[0-?]*[ -/]*[@-~]|\x9B[0-?]*[ -/]*[@-~]|\x1B[@-_]/g,ZC=/[\x00-\x1F\x7F-\x9F]/g;function Hg(e){return e.replace(Wg,"").replace(ZC," ").trim()}function on(e){return e.replace(Wg,"")}function eI(e){if(typeof e!="object"||e===null)throw new Error("Input must be an object");let t=e;if(typeof t.command!="string")throw new Error('Input must have a "command" field of type string');let n=12e4;if(t.timeout_ms!==void 0){if(typeof t.timeout_ms!="number")throw new Error("timeout_ms must be a number");if(t.timeout_ms<0||t.timeout_ms>6e5)throw new Error("timeout_ms must be between 0 and 600000");n=t.timeout_ms}return{command:t.command,timeout_ms:n}}function ta(e,t){let n=!1;function r(){n||e==="bypassPermissions"&&(n=!0,console.warn("[security] bash handler: shell=true with bypassPermissions \u2014 all shell metacharacters are interpreted without confirmation. Migrate to execFile to eliminate this risk (tracked: C4)."))}return async(o,s,i)=>{let{command:a,timeout_ms:l}=eI(o);return s.aborted?{content:"Command aborted",isError:!0}:(r(),new Promise(c=>{let u=!1;function d(v){u||(u=!0,clearTimeout(f),s.removeEventListener("abort",R),c(v))}let p=QC(a,{shell:!0,detached:!0,stdio:["ignore","pipe","pipe"],...(i?.resolveBase??i?.cwd??t)!==void 0?{cwd:i?.resolveBase??i?.cwd??t}:{},...i?.env!==void 0?{env:{...process.env,...i.env}}:{}});p.unref();let f=setTimeout(()=>{p.pid!==void 0&&process.kill(-p.pid,"SIGKILL"),d({content:`Command timed out after ${l}ms`,isError:!0})},l),g="",h="",b=1e5,y=0,w=!1;function x(v){if(w||u||y<b)return;w=!0,console.warn(`[bash] overflow kill: stream=${v} totalBytes=${y} command="${a}"`),Te({event:"tool.overflow_kill",tool:"bash",total_bytes:y,stream:v}),p.kill("SIGKILL");let E=(g+h).trimEnd();E=on(E);let I=Du(E)??void 0;E.length>b&&(E=E.slice(0,b)),E+=`
1036
+ [output truncated \u2014 exceeded 100KB]`,d({content:E,truncated:!0,...I!==void 0?{testResult:I}:{}})}p.stdout.on("data",v=>{let E=b-y,I=v.length<=E?v:v.subarray(0,Math.max(0,E));y+=I.length,g+=I.toString("utf8"),x("stdout")}),p.stderr.on("data",v=>{let E=b-y,I=v.length<=E?v:v.subarray(0,Math.max(0,E));y+=I.length,h+=I.toString("utf8"),x("stderr")});let R=()=>{p.pid!==void 0&&process.kill(-p.pid,"SIGKILL"),d({content:"Command aborted",isError:!0})};s.addEventListener("abort",R),p.on("close",v=>{if(s.aborted){d({content:"Command aborted",isError:!0});return}if(v!==null&&v!==0){let _=h.trimEnd()||g.trimEnd();d({content:`Command exited with code ${v}${_?`
1037
1037
  `+_:""}`,isError:!0});return}if(w)return;let E=(g+h).trimEnd();E=on(E);let I=Du(E)??void 0,D=!1;E.length>b&&(E=E.slice(0,b)+`
1038
- [output truncated \u2014 exceeded 100KB]`,D=!0),d({content:E,...D?{truncated:!0}:{},...I!==void 0?{testResult:I}:{}})}),m.on("error",v=>{d({content:`Failed to execute: ${v.message}`,isError:!0})})}))}}var Kg=ta("default");import{promises as tI}from"fs";import Lu from"path";function ht(e,t,n="read"){let r=t?.resolveBase??t?.cwd,o=Lu.isAbsolute(e)?e:Lu.resolve(r??process.cwd(),e);if(r===void 0)return o;let s=n==="read"?t?.readRoots??[r]:t?.writeRoots??[r];for(let l of s)if(!Lu.relative(l,o).startsWith(".."))return o;let i=s.map(l=>`\`${l}\``).join(", "),a=n==="read"?"read roots":"write roots";throw new Error(`Path \`${e}\` is outside the allowed ${a} [${i}].`)}var Gg=async(e,t,n)=>{if(!e||typeof e!="object")return{content:"Invalid input: expected an object",isError:!0};let r=e,o=r.file_path,s=r.offset??1,i=r.limit??2e3;if(typeof o!="string")return{content:"Invalid input: file_path must be a string",isError:!0};if(typeof s!="number"||s<1)return{content:"Invalid input: offset must be a positive number",isError:!0};if(typeof i!="number"||i<1)return{content:"Invalid input: limit must be a positive number",isError:!0};let a;try{a=ht(o,n,"read")}catch(l){return{content:l instanceof Error?l.message:String(l),isError:!0}}try{let l=await tI.readFile(a),c=Math.min(8192,l.length);for(let w=0;w<c;w++)if(l[w]===0)return{content:`File appears to be binary: ${a}`,isError:!0};let u=l.toString("utf-8");if(u.length===0)return{content:""};let d=u.split(`
1039
- `),m=Math.max(0,s-1),f=Math.min(d.length,m+i),g=d.slice(m,f),h=d.length;if(g.length===0)return{content:`... (offset ${s} is past end of file \u2014 file has ${h} lines)`};let b=String(h).length,y=g.map((w,x)=>{let R=m+x+1;return`${String(R).padStart(b," ")} ${w}`}).join(`
1040
- `);if(g.length<h){let w=m+1,x=m+g.length,R=x<h?` \u2014 pass offset=${x+1} to continue`:"";return{content:`${y}
1041
- ... (showing lines ${w}-${x} of ${h}${R})`}}return{content:y}}catch(l){if(l instanceof Error){let c=l;return c.code==="ENOENT"?{content:`File not found: ${a}`,isError:!0}:c.code==="EACCES"?{content:`Permission denied: ${a}`,isError:!0}:{content:`Error reading file: ${l.message}`,isError:!0}}return{content:"Unknown error reading file",isError:!0}}};G();import{readFile as lI,writeFile as cI,mkdir as uI,stat as dI}from"fs/promises";import{dirname as pI}from"path";G();import{realpathSync as qg}from"fs";import{dirname as nI,resolve as na,join as rI}from"path";import{homedir as In}from"os";var oI=[`${In()}/.ssh`,`${In()}/.aws`,`${In()}/.gnupg`,`${In()}/.config/gcloud`,"/etc","/System","/private/etc","/usr/local/etc",`${In()}/.afk/config`,`${In()}/.afk/state`,`${In()}/.npmrc`,`${In()}/.docker/config.json`];function sI(){let e=T.AFK_WRITE_DENYLIST,t=e?e.split(":").map(n=>Fu(na(n))).filter(Boolean):[];return[...oI.map(n=>Fu(na(n))),...t]}function Fu(e){let t=na(e);try{return qg(t)}catch{}let n=[],r=t;for(let o=0;o<64;o++){let s=nI(r);if(s===r)break;n.unshift(r.slice(s.length+1)),r=s;try{let i=qg(r);return rI(i,...n)}catch{}}return t}function ra(e,t="write_file"){let n=Fu(na(e));for(let r of sI())if(n===r||n.startsWith(r+"/"))throw new Error(`${t}: refusing to write to protected path: ${n} (matches denylist entry: ${r})`)}function zg(e){if(e==="")return[];let t=e.split(/\r?\n/);return t.length>0&&t[t.length-1]===""&&/\r?\n$/.test(e)&&t.pop(),t}function iI(e,t){let n=e.length,r=t.length;if((n+1)*(r+1)>=4e6){let c=[];for(let u of e)c.push({op:"del",text:u});for(let u of t)c.push({op:"add",text:u});return c}let o=r+1,s=new Int32Array((n+1)*o);for(let c=1;c<=n;c++)for(let u=1;u<=r;u++)if(e[c-1]===t[u-1])s[c*o+u]=s[(c-1)*o+(u-1)]+1;else{let d=s[(c-1)*o+u],m=s[c*o+(u-1)];s[c*o+u]=d>=m?d:m}let i=[],a=n,l=r;for(;a>0||l>0;)a>0&&l>0&&e[a-1]===t[l-1]?(i.push({op:"same",text:e[a-1]}),a--,l--):l>0&&(a===0||s[a*o+(l-1)]>=s[(a-1)*o+l])?(i.push({op:"add",text:t[l-1]}),l--):(i.push({op:"del",text:e[a-1]}),a--);return i.reverse(),i}function aI(e){let t=new Int32Array(e.length+1);t[e.length]=e.length;for(let i=e.length-1;i>=0;i--)t[i]=e[i].op==="same"?t[i+1]:i;let n=[],r=1,o=1,s=0;for(;s<e.length;){if(e[s].op==="same"){r++,o++,s++;continue}let i=s,a=0;for(;i>0&&e[i-1].op==="same"&&a<3;)i--,a++;let l=Math.max(1,r-a),c=Math.max(1,o-a),u=[],d=0,m=0;for(let g=i;g<s;g++)u.push({kind:" ",text:e[g].text}),d++,m++;let f=!1;for(;!f&&s<e.length;){let g=e[s];if(g.op==="same"){let h=t[s],b=h-s;if(h===e.length||b>6){for(let w=0;w<3&&s<e.length&&e[s].op==="same";w++)u.push({kind:" ",text:e[s].text}),d++,m++,r++,o++,s++;f=!0}else u.push({kind:" ",text:g.text}),d++,m++,r++,o++,s++}else g.op==="add"?(u.push({kind:"+",text:g.text}),m++,o++,s++):(u.push({kind:"-",text:g.text}),d++,r++,s++)}n.push({oldStart:l,oldLines:d,newStart:c,newLines:m,lines:u})}return n}function oa(e,t){if(e===t)return null;let n=zg(e),r=zg(t),o=iI(n,r),s=aI(o);if(s.length===0)return null;let i=0,a=0;for(let l of s)for(let c of l.lines)c.kind==="+"?i++:c.kind==="-"&&a++;return{hunks:s,addedLines:i,removedLines:a}}function mI(e){if(typeof e!="object"||e===null)throw new Error("Input must be an object");let t=e;if(typeof t.file_path!="string")throw new Error('Input must have a "file_path" field of type string');if(typeof t.content!="string")throw new Error('Input must have a "content" field of type string');return{file_path:t.file_path,content:t.content}}var Jg=async(e,t,n)=>{if(t.aborted)return{content:"Aborted",isError:!0};let{file_path:r,content:o}=mI(e),s;try{s=ht(r,n,"write")}catch(i){return{content:i instanceof Error?i.message:String(i),isError:!0}}try{ra(s,"write_file");let i=(()=>{let m=T.AFK_WRITE_DIFF;if(m===void 0)return!1;let f=m.trim().toLowerCase();return f==="0"||f==="false"||f==="no"||f==="off"})(),a=10*1024*1024,l=null;if(!i)try{let m=await dI(s);if(m.size>a)T.AFK_DEBUG&&console.debug(`[write_file] skipping diff: prior file ${m.size} bytes > ${a}`);else{let f=await lI(s);try{l=new TextDecoder("utf-8",{fatal:!0}).decode(f)}catch{l=null}}}catch(m){m instanceof Error&&"code"in m&&m.code==="ENOENT"&&(l="")}let c=pI(s);await uI(c,{recursive:!0}),await cI(s,o,{signal:t});let u=null;if(l!==null&&!o.includes("\0")){let m=performance.now();u=oa(l,o);let f=performance.now()-m;f>=500?console.warn(`[write_file] computeLineDiff took ${f.toFixed(1)}ms`):f>=50&&T.AFK_DEBUG&&console.debug(`[write_file] computeLineDiff took ${f.toFixed(1)}ms`)}return{content:`Wrote ${Buffer.byteLength(o,"utf8")} bytes to ${s}`,...u?{render:{diff:u}}:{}}}catch(i){return i instanceof Error?"code"in i&&i.code==="EACCES"?{content:`Permission denied: ${s}`,isError:!0}:{content:`Error writing file: ${i.message}`,isError:!0}:{content:"Unknown error writing file",isError:!0}}};G();import{readFile as fI,writeFile as gI}from"fs/promises";function hI(e){if(typeof e!="object"||e===null)throw new Error("Input must be an object");let t=e;if(typeof t.file_path!="string")throw new Error('Input must have a "file_path" field of type string');if(typeof t.old_string!="string")throw new Error('Input must have an "old_string" field of type string');if(typeof t.new_string!="string")throw new Error('Input must have a "new_string" field of type string');let n=!1;if(t.replace_all!==void 0){if(typeof t.replace_all!="boolean")throw new Error("replace_all must be a boolean");n=t.replace_all}return{file_path:t.file_path,old_string:t.old_string,new_string:t.new_string,replace_all:n}}function yI(e,t){if(t.length===0)return 0;let n=0,r=0;for(;(r=e.indexOf(t,r))!==-1;)n++,r+=t.length;return n}var Vg=async(e,t,n)=>{if(t.aborted)return{content:"Aborted",isError:!0};let{file_path:r,old_string:o,new_string:s,replace_all:i}=hI(e),a;try{a=ht(r,n,"write")}catch(l){return{content:l instanceof Error?l.message:String(l),isError:!0}}try{ra(a,"edit_file");let l=await fI(a,"utf-8"),c=yI(l,o);if(c===0)return{content:`old_string not found in ${a}`,isError:!0};if(c>1&&!i)return{content:`old_string matches ${c} locations in ${a}. Use replace_all: true or provide more context.`,isError:!0};let u;if(i)u=l.split(o).join(s);else{let h=l.indexOf(o);u=l.slice(0,h)+s+l.slice(h+o.length)}await gI(a,u,"utf-8");let d=c===1?`Replaced 1 occurrence in ${a}`:`Replaced ${c} occurrences in ${a}`,m=performance.now(),f=oa(l,u),g=performance.now()-m;return g>=500?console.warn(`[edit_file] computeLineDiff took ${g.toFixed(1)}ms`):g>=50&&T.AFK_DEBUG&&console.debug(`[edit_file] computeLineDiff took ${g.toFixed(1)}ms`),{content:d,...f?{render:{diff:f}}:{}}}catch(l){return{content:`Error: ${l instanceof Error?l.message:String(l)}`,isError:!0}}};import{promises as Xg}from"fs";import bI from"path";function wI(e,t){let n=e.replace(/\\/g,"/"),r=t.replace(/\\/g,"/");if(r.includes("**")){let s=r.split("**"),i=0;for(let a=0;a<s.length;a++){let l=s[a]??"",c=Yg(l);if(a===0){let u=n.match(new RegExp(`^${c}`));if(!u)return!1;i=u[0].length}else if(a===s.length-1){let u=new RegExp(`${c}$`);if(!n.slice(i).match(u))return!1}else{let u=new RegExp(c),d=n.slice(i).match(u);if(!d)return!1;let m=d.index??0;i+=m+d[0].length}}return!0}return new RegExp(`^${Yg(r)}$`).test(n)}function Yg(e){return e.replace(/[.+^${}()|[\]\\]/g,"\\$&").replace(/\*/g,"[^/]*").replace(/\?/g,"[^/]")}async function SI(e,t){let n=[];async function o(s,i){if(n.length>=500)return!0;try{let a=await Xg.readdir(s,{withFileTypes:!0});for(let l of a){if(n.length>=500)return!0;let c=bI.join(s,l.name),u=i?`${i}/${l.name}`:l.name;if(wI(u,t)&&n.push(u),l.isDirectory()&&await o(c,u))return!0}}catch{}return!1}return await o(e,""),n}function Nu(e){return async(t,n,r)=>{if(!t||typeof t!="object")return{content:"Invalid input: expected an object",isError:!0};let o=t,s=o.pattern,i=o.path??r?.resolveBase??r?.cwd??e??process.cwd();if(typeof s!="string")return{content:"Invalid input: pattern must be a string",isError:!0};if(s.trim()==="")return{content:"Invalid input: pattern cannot be empty",isError:!0};if(typeof i!="string")return{content:"Invalid input: path must be a string",isError:!0};let a;try{a=ht(i,r,"read")}catch(l){return{content:l instanceof Error?l.message:String(l),isError:!0}}try{if(!(await Xg.stat(a)).isDirectory())return{content:`Invalid input: path is not a directory: ${a}`,isError:!0};let c=await SI(a,s);if(c.length===0)return{content:`No files matched pattern '${s}' in ${a}`};let u=c.join(`
1038
+ [output truncated \u2014 exceeded 100KB]`,D=!0),d({content:E,...D?{truncated:!0}:{},...I!==void 0?{testResult:I}:{}})}),p.on("error",v=>{d({content:`Failed to execute: ${v.message}`,isError:!0})})}))}}var Kg=ta("default");import{promises as tI}from"fs";import Lu from"path";function ht(e,t,n="read"){let r=t?.resolveBase??t?.cwd,o=Lu.isAbsolute(e)?e:Lu.resolve(r??process.cwd(),e);if(r===void 0)return o;let s=n==="read"?t?.readRoots??[r]:t?.writeRoots??[r];for(let l of s)if(!Lu.relative(l,o).startsWith(".."))return o;let i=s.map(l=>`\`${l}\``).join(", "),a=n==="read"?"read roots":"write roots";throw new Error(`Path \`${e}\` is outside the allowed ${a} [${i}].`)}var Gg=async(e,t,n)=>{if(!e||typeof e!="object")return{content:"Invalid input: expected an object",isError:!0};let r=e,o=r.file_path,s=r.offset??1,i=r.limit??2e3;if(typeof o!="string")return{content:"Invalid input: file_path must be a string",isError:!0};if(typeof s!="number"||s<1)return{content:"Invalid input: offset must be a positive number",isError:!0};if(typeof i!="number"||i<1)return{content:"Invalid input: limit must be a positive number",isError:!0};let a;try{a=ht(o,n,"read")}catch(l){return{content:l instanceof Error?l.message:String(l),isError:!0}}try{let l=await tI.readFile(a),c=Math.min(8192,l.length);for(let w=0;w<c;w++)if(l[w]===0)return{content:`File appears to be binary: ${a}`,isError:!0};let u=l.toString("utf-8");if(u.length===0)return{content:""};let d=u.split(`
1039
+ `),p=Math.max(0,s-1),f=Math.min(d.length,p+i),g=d.slice(p,f),h=d.length;if(g.length===0)return{content:`... (offset ${s} is past end of file \u2014 file has ${h} lines)`};let b=String(h).length,y=g.map((w,x)=>{let R=p+x+1;return`${String(R).padStart(b," ")} ${w}`}).join(`
1040
+ `);if(g.length<h){let w=p+1,x=p+g.length,R=x<h?` \u2014 pass offset=${x+1} to continue`:"";return{content:`${y}
1041
+ ... (showing lines ${w}-${x} of ${h}${R})`}}return{content:y}}catch(l){if(l instanceof Error){let c=l;return c.code==="ENOENT"?{content:`File not found: ${a}`,isError:!0}:c.code==="EACCES"?{content:`Permission denied: ${a}`,isError:!0}:{content:`Error reading file: ${l.message}`,isError:!0}}return{content:"Unknown error reading file",isError:!0}}};G();import{readFile as lI,writeFile as cI,mkdir as uI,stat as dI}from"fs/promises";import{dirname as pI}from"path";G();import{realpathSync as qg}from"fs";import{dirname as nI,resolve as na,join as rI}from"path";import{homedir as In}from"os";var oI=[`${In()}/.ssh`,`${In()}/.aws`,`${In()}/.gnupg`,`${In()}/.config/gcloud`,"/etc","/System","/private/etc","/usr/local/etc",`${In()}/.afk/config`,`${In()}/.afk/state`,`${In()}/.npmrc`,`${In()}/.docker/config.json`];function sI(){let e=T.AFK_WRITE_DENYLIST,t=e?e.split(":").map(n=>Fu(na(n))).filter(Boolean):[];return[...oI.map(n=>Fu(na(n))),...t]}function Fu(e){let t=na(e);try{return qg(t)}catch{}let n=[],r=t;for(let o=0;o<64;o++){let s=nI(r);if(s===r)break;n.unshift(r.slice(s.length+1)),r=s;try{let i=qg(r);return rI(i,...n)}catch{}}return t}function ra(e,t="write_file"){let n=Fu(na(e));for(let r of sI())if(n===r||n.startsWith(r+"/"))throw new Error(`${t}: refusing to write to protected path: ${n} (matches denylist entry: ${r})`)}function zg(e){if(e==="")return[];let t=e.split(/\r?\n/);return t.length>0&&t[t.length-1]===""&&/\r?\n$/.test(e)&&t.pop(),t}function iI(e,t){let n=e.length,r=t.length;if((n+1)*(r+1)>=4e6){let c=[];for(let u of e)c.push({op:"del",text:u});for(let u of t)c.push({op:"add",text:u});return c}let o=r+1,s=new Int32Array((n+1)*o);for(let c=1;c<=n;c++)for(let u=1;u<=r;u++)if(e[c-1]===t[u-1])s[c*o+u]=s[(c-1)*o+(u-1)]+1;else{let d=s[(c-1)*o+u],p=s[c*o+(u-1)];s[c*o+u]=d>=p?d:p}let i=[],a=n,l=r;for(;a>0||l>0;)a>0&&l>0&&e[a-1]===t[l-1]?(i.push({op:"same",text:e[a-1]}),a--,l--):l>0&&(a===0||s[a*o+(l-1)]>=s[(a-1)*o+l])?(i.push({op:"add",text:t[l-1]}),l--):(i.push({op:"del",text:e[a-1]}),a--);return i.reverse(),i}function aI(e){let t=new Int32Array(e.length+1);t[e.length]=e.length;for(let i=e.length-1;i>=0;i--)t[i]=e[i].op==="same"?t[i+1]:i;let n=[],r=1,o=1,s=0;for(;s<e.length;){if(e[s].op==="same"){r++,o++,s++;continue}let i=s,a=0;for(;i>0&&e[i-1].op==="same"&&a<3;)i--,a++;let l=Math.max(1,r-a),c=Math.max(1,o-a),u=[],d=0,p=0;for(let g=i;g<s;g++)u.push({kind:" ",text:e[g].text}),d++,p++;let f=!1;for(;!f&&s<e.length;){let g=e[s];if(g.op==="same"){let h=t[s],b=h-s;if(h===e.length||b>6){for(let w=0;w<3&&s<e.length&&e[s].op==="same";w++)u.push({kind:" ",text:e[s].text}),d++,p++,r++,o++,s++;f=!0}else u.push({kind:" ",text:g.text}),d++,p++,r++,o++,s++}else g.op==="add"?(u.push({kind:"+",text:g.text}),p++,o++,s++):(u.push({kind:"-",text:g.text}),d++,r++,s++)}n.push({oldStart:l,oldLines:d,newStart:c,newLines:p,lines:u})}return n}function oa(e,t){if(e===t)return null;let n=zg(e),r=zg(t),o=iI(n,r),s=aI(o);if(s.length===0)return null;let i=0,a=0;for(let l of s)for(let c of l.lines)c.kind==="+"?i++:c.kind==="-"&&a++;return{hunks:s,addedLines:i,removedLines:a}}function mI(e){if(typeof e!="object"||e===null)throw new Error("Input must be an object");let t=e;if(typeof t.file_path!="string")throw new Error('Input must have a "file_path" field of type string');if(typeof t.content!="string")throw new Error('Input must have a "content" field of type string');return{file_path:t.file_path,content:t.content}}var Jg=async(e,t,n)=>{if(t.aborted)return{content:"Aborted",isError:!0};let{file_path:r,content:o}=mI(e),s;try{s=ht(r,n,"write")}catch(i){return{content:i instanceof Error?i.message:String(i),isError:!0}}try{ra(s,"write_file");let i=(()=>{let p=T.AFK_WRITE_DIFF;if(p===void 0)return!1;let f=p.trim().toLowerCase();return f==="0"||f==="false"||f==="no"||f==="off"})(),a=10*1024*1024,l=null;if(!i)try{let p=await dI(s);if(p.size>a)T.AFK_DEBUG&&console.debug(`[write_file] skipping diff: prior file ${p.size} bytes > ${a}`);else{let f=await lI(s);try{l=new TextDecoder("utf-8",{fatal:!0}).decode(f)}catch{l=null}}}catch(p){p instanceof Error&&"code"in p&&p.code==="ENOENT"&&(l="")}let c=pI(s);await uI(c,{recursive:!0}),await cI(s,o,{signal:t});let u=null;if(l!==null&&!o.includes("\0")){let p=performance.now();u=oa(l,o);let f=performance.now()-p;f>=500?console.warn(`[write_file] computeLineDiff took ${f.toFixed(1)}ms`):f>=50&&T.AFK_DEBUG&&console.debug(`[write_file] computeLineDiff took ${f.toFixed(1)}ms`)}return{content:`Wrote ${Buffer.byteLength(o,"utf8")} bytes to ${s}`,...u?{render:{diff:u}}:{}}}catch(i){return i instanceof Error?"code"in i&&i.code==="EACCES"?{content:`Permission denied: ${s}`,isError:!0}:{content:`Error writing file: ${i.message}`,isError:!0}:{content:"Unknown error writing file",isError:!0}}};G();import{readFile as fI,writeFile as gI}from"fs/promises";function hI(e){if(typeof e!="object"||e===null)throw new Error("Input must be an object");let t=e;if(typeof t.file_path!="string")throw new Error('Input must have a "file_path" field of type string');if(typeof t.old_string!="string")throw new Error('Input must have an "old_string" field of type string');if(typeof t.new_string!="string")throw new Error('Input must have a "new_string" field of type string');let n=!1;if(t.replace_all!==void 0){if(typeof t.replace_all!="boolean")throw new Error("replace_all must be a boolean");n=t.replace_all}return{file_path:t.file_path,old_string:t.old_string,new_string:t.new_string,replace_all:n}}function yI(e,t){if(t.length===0)return 0;let n=0,r=0;for(;(r=e.indexOf(t,r))!==-1;)n++,r+=t.length;return n}var Vg=async(e,t,n)=>{if(t.aborted)return{content:"Aborted",isError:!0};let{file_path:r,old_string:o,new_string:s,replace_all:i}=hI(e),a;try{a=ht(r,n,"write")}catch(l){return{content:l instanceof Error?l.message:String(l),isError:!0}}try{ra(a,"edit_file");let l=await fI(a,"utf-8"),c=yI(l,o);if(c===0)return{content:`old_string not found in ${a}`,isError:!0};if(c>1&&!i)return{content:`old_string matches ${c} locations in ${a}. Use replace_all: true or provide more context.`,isError:!0};let u;if(i)u=l.split(o).join(s);else{let h=l.indexOf(o);u=l.slice(0,h)+s+l.slice(h+o.length)}await gI(a,u,"utf-8");let d=c===1?`Replaced 1 occurrence in ${a}`:`Replaced ${c} occurrences in ${a}`,p=performance.now(),f=oa(l,u),g=performance.now()-p;return g>=500?console.warn(`[edit_file] computeLineDiff took ${g.toFixed(1)}ms`):g>=50&&T.AFK_DEBUG&&console.debug(`[edit_file] computeLineDiff took ${g.toFixed(1)}ms`),{content:d,...f?{render:{diff:f}}:{}}}catch(l){return{content:`Error: ${l instanceof Error?l.message:String(l)}`,isError:!0}}};import{promises as Xg}from"fs";import bI from"path";function wI(e,t){let n=e.replace(/\\/g,"/"),r=t.replace(/\\/g,"/");if(r.includes("**")){let s=r.split("**"),i=0;for(let a=0;a<s.length;a++){let l=s[a]??"",c=Yg(l);if(a===0){let u=n.match(new RegExp(`^${c}`));if(!u)return!1;i=u[0].length}else if(a===s.length-1){let u=new RegExp(`${c}$`);if(!n.slice(i).match(u))return!1}else{let u=new RegExp(c),d=n.slice(i).match(u);if(!d)return!1;let p=d.index??0;i+=p+d[0].length}}return!0}return new RegExp(`^${Yg(r)}$`).test(n)}function Yg(e){return e.replace(/[.+^${}()|[\]\\]/g,"\\$&").replace(/\*/g,"[^/]*").replace(/\?/g,"[^/]")}async function SI(e,t){let n=[];async function o(s,i){if(n.length>=500)return!0;try{let a=await Xg.readdir(s,{withFileTypes:!0});for(let l of a){if(n.length>=500)return!0;let c=bI.join(s,l.name),u=i?`${i}/${l.name}`:l.name;if(wI(u,t)&&n.push(u),l.isDirectory()&&await o(c,u))return!0}}catch{}return!1}return await o(e,""),n}function Nu(e){return async(t,n,r)=>{if(!t||typeof t!="object")return{content:"Invalid input: expected an object",isError:!0};let o=t,s=o.pattern,i=o.path??r?.resolveBase??r?.cwd??e??process.cwd();if(typeof s!="string")return{content:"Invalid input: pattern must be a string",isError:!0};if(s.trim()==="")return{content:"Invalid input: pattern cannot be empty",isError:!0};if(typeof i!="string")return{content:"Invalid input: path must be a string",isError:!0};let a;try{a=ht(i,r,"read")}catch(l){return{content:l instanceof Error?l.message:String(l),isError:!0}}try{if(!(await Xg.stat(a)).isDirectory())return{content:`Invalid input: path is not a directory: ${a}`,isError:!0};let c=await SI(a,s);if(c.length===0)return{content:`No files matched pattern '${s}' in ${a}`};let u=c.join(`
1042
1042
  `);return c.length>=500&&(u+=`
1043
- [results capped at 500 entries]`),{content:u}}catch(l){return l instanceof Error?"code"in l&&l.code==="ENOENT"?{content:`Path not found: ${a}`,isError:!0}:"code"in l&&l.code==="EACCES"?{content:`Permission denied: ${a}`,isError:!0}:{content:`Error scanning directory: ${l.message}`,isError:!0}:{content:"Unknown error scanning directory",isError:!0}}}}var Zg=Nu();import{spawn as kI}from"child_process";function vI(e,t,n){if(typeof e!="object"||e===null)throw new Error("Input must be an object");let r=e;if(typeof r.pattern!="string")throw new Error('Input must have a "pattern" field of type string');let o=typeof r.path=="string"?r.path:t?.resolveBase??t?.cwd??n??process.cwd(),s=ht(o,t,"read"),i;if(r.include!==void 0){if(typeof r.include!="string")throw new Error("include must be a string");i=r.include}let a=!1;if(r.extended!==void 0){if(typeof r.extended!="boolean")throw new Error("extended must be a boolean");a=r.extended}return{pattern:r.pattern,path:s,include:i,extended:a}}function TI(e){return/(?<!\\)\|/.test(e)}function Bu(e){return async(t,n,r)=>{let{pattern:o,path:s,include:i,extended:a}=vI(t,r,e);return n.aborted?{content:"Search aborted",isError:!0}:new Promise(l=>{let c=!1;function u(R){c||(c=!0,n.removeEventListener("abort",x),l(R))}let d=["-rn"];a&&d.push("-E"),i&&d.push(`--include=${i}`),d.push(o,s);let m=kI("grep",d,e!==void 0?{cwd:e}:{}),f="",g="",h=1e5,b=0,y=!1;function w(R){if(y||c||b<h)return;y=!0,console.warn(`[grep] overflow kill: stream=${R} totalBytes=${b} pattern=${o} path=${s}`),Te({event:"tool.overflow_kill",tool:"grep",total_bytes:b,stream:R}),m.kill("SIGKILL");let v=(f+g).trimEnd();v=on(v),v.length>h&&(v=v.slice(0,h)),v+=`
1044
- [output truncated]`,u({content:v,truncated:!0})}m.stdout.on("data",R=>{let v=h-b,E=R.length<=v?R:R.subarray(0,Math.max(0,v));b+=E.length,f+=E.toString("utf8"),w("stdout")}),m.stderr.on("data",R=>{let v=h-b,E=R.length<=v?R:R.subarray(0,Math.max(0,v));b+=E.length,g+=E.toString("utf8"),w("stderr")});let x=()=>{m.kill(),u({content:"Search aborted",isError:!0})};n.addEventListener("abort",x),m.on("close",R=>{if(y)return;if(R===1){let I=`No matches found for '${o}' in ${s}`;!a&&TI(o)&&(I+=`
1043
+ [results capped at 500 entries]`),{content:u}}catch(l){return l instanceof Error?"code"in l&&l.code==="ENOENT"?{content:`Path not found: ${a}`,isError:!0}:"code"in l&&l.code==="EACCES"?{content:`Permission denied: ${a}`,isError:!0}:{content:`Error scanning directory: ${l.message}`,isError:!0}:{content:"Unknown error scanning directory",isError:!0}}}}var Zg=Nu();import{spawn as kI}from"child_process";function vI(e,t,n){if(typeof e!="object"||e===null)throw new Error("Input must be an object");let r=e;if(typeof r.pattern!="string")throw new Error('Input must have a "pattern" field of type string');let o=typeof r.path=="string"?r.path:t?.resolveBase??t?.cwd??n??process.cwd(),s=ht(o,t,"read"),i;if(r.include!==void 0){if(typeof r.include!="string")throw new Error("include must be a string");i=r.include}let a=!1;if(r.extended!==void 0){if(typeof r.extended!="boolean")throw new Error("extended must be a boolean");a=r.extended}return{pattern:r.pattern,path:s,include:i,extended:a}}function TI(e){return/(?<!\\)\|/.test(e)}function Bu(e){return async(t,n,r)=>{let{pattern:o,path:s,include:i,extended:a}=vI(t,r,e);return n.aborted?{content:"Search aborted",isError:!0}:new Promise(l=>{let c=!1;function u(R){c||(c=!0,n.removeEventListener("abort",x),l(R))}let d=["-rn"];a&&d.push("-E"),i&&d.push(`--include=${i}`),d.push(o,s);let p=kI("grep",d,e!==void 0?{cwd:e}:{}),f="",g="",h=1e5,b=0,y=!1;function w(R){if(y||c||b<h)return;y=!0,console.warn(`[grep] overflow kill: stream=${R} totalBytes=${b} pattern=${o} path=${s}`),Te({event:"tool.overflow_kill",tool:"grep",total_bytes:b,stream:R}),p.kill("SIGKILL");let v=(f+g).trimEnd();v=on(v),v.length>h&&(v=v.slice(0,h)),v+=`
1044
+ [output truncated]`,u({content:v,truncated:!0})}p.stdout.on("data",R=>{let v=h-b,E=R.length<=v?R:R.subarray(0,Math.max(0,v));b+=E.length,f+=E.toString("utf8"),w("stdout")}),p.stderr.on("data",R=>{let v=h-b,E=R.length<=v?R:R.subarray(0,Math.max(0,v));b+=E.length,g+=E.toString("utf8"),w("stderr")});let x=()=>{p.kill(),u({content:"Search aborted",isError:!0})};n.addEventListener("abort",x),p.on("close",R=>{if(y)return;if(R===1){let I=`No matches found for '${o}' in ${s}`;!a&&TI(o)&&(I+=`
1045
1045
 
1046
1046
  Note: this search ran in basic-regex (BRE) mode, where '|' is a literal pipe \u2014 not alternation. If you intended "A or B", retry with extended: true (extended regex / ERE). If you meant the literal character '|', this empty result stands.`),u({content:I});return}if(R===2){u({content:`grep error: ${g.trim()}`,isError:!0});return}let v=f.trimEnd();v=on(v);let E=!1;v.length>h&&(v=v.slice(0,h)+`
1047
- [output truncated]`,E=!0),u({content:v,...E?{truncated:!0}:{}})}),m.on("error",R=>{u({content:`Failed to execute grep: ${R.message}`,isError:!0})})})}}var Qg=Bu();import{promises as xI}from"fs";var eh=async(e,t,n)=>{if(!e||typeof e!="object")throw new Error("Invalid input: expected an object");let o=e.path;if(typeof o!="string")throw new Error("Invalid input: path must be a string");let s;try{s=ht(o,n,"read")}catch(i){return{content:i instanceof Error?i.message:String(i),isError:!0}}try{let i=await xI.readdir(s,{withFileTypes:!0}),a=i.filter(d=>d.isDirectory()).map(d=>`${d.name}/`),l=i.filter(d=>!d.isDirectory()).map(d=>d.name);a.sort(),l.sort();let c=[...a,...l];return c.length===0?{content:"(empty directory)"}:{content:c.join(`
1047
+ [output truncated]`,E=!0),u({content:v,...E?{truncated:!0}:{}})}),p.on("error",R=>{u({content:`Failed to execute grep: ${R.message}`,isError:!0})})})}}var Qg=Bu();import{promises as xI}from"fs";var eh=async(e,t,n)=>{if(!e||typeof e!="object")throw new Error("Invalid input: expected an object");let o=e.path;if(typeof o!="string")throw new Error("Invalid input: path must be a string");let s;try{s=ht(o,n,"read")}catch(i){return{content:i instanceof Error?i.message:String(i),isError:!0}}try{let i=await xI.readdir(s,{withFileTypes:!0}),a=i.filter(d=>d.isDirectory()).map(d=>`${d.name}/`),l=i.filter(d=>!d.isDirectory()).map(d=>d.name);a.sort(),l.sort();let c=[...a,...l];return c.length===0?{content:"(empty directory)"}:{content:c.join(`
1048
1048
  `)}}catch(i){if(i instanceof Error){let a=i;return a.code==="ENOENT"?{content:`Directory not found: ${s}`,isError:!0}:a.code==="ENOTDIR"?{content:`Not a directory: ${s}`,isError:!0}:a.code==="EACCES"?{content:`Permission denied: ${s}`,isError:!0}:{content:`Error listing directory: ${i.message}`,isError:!0}}return{content:"Unknown error listing directory",isError:!0}}};G();ws();Uu();var th=4096;function RI(e=ia){return async(t,n)=>{if(!t||typeof t!="object")return{content:"Invalid input: expected an object",isError:!0};let o=t.message;if(typeof o!="string")return{content:"Invalid input: message must be a string",isError:!0};if(o.length===0)return{content:"Invalid input: message must be non-empty",isError:!0};if(o.length>th)return{content:`Invalid input: message exceeds Telegram's ${th}-character limit (got ${o.length}). Split into multiple sends or trim before calling.`,isError:!0};let s=T.TELEGRAM_BOT_TOKEN;if(!s)return{content:"Telegram is not configured: TELEGRAM_BOT_TOKEN is not set. Run the bot setup wizard or export the env var before using send_telegram.",isError:!0};let i=sa(T.AFK_TELEGRAM_ALLOWED_CHAT_IDS);if(i.size===0)return{content:"Telegram is not configured: AFK_TELEGRAM_ALLOWED_CHAT_IDS is empty or unset. Add the operator chat ID(s) before using send_telegram.",isError:!0};let a=[...i],l=[];for(let c of a){let u=await e({token:s,chatId:c,text:o});u.ok||l.push(`chat ${c}: ${u.errorMessage??`HTTP ${u.status}`}`)}return l.length===a.length?{content:`Failed to send Telegram message to any chat. ${l.join("; ")}`,isError:!0}:l.length>0?{content:`Sent Telegram message to ${a.length-l.length}/${a.length} chat(s); ${l.length} failed: ${l.join("; ")}`}:{content:a.length===1?`Sent Telegram message to chat ${a[0]}.`:`Sent Telegram message to ${a.length} chats.`}}}var nh=RI();var AI=`
1049
1049
 
1050
1050
  [\u2026truncated by agent-afk web_scrape]`,_I="https://api.firecrawl.dev/v2/scrape",CI="https://api.firecrawl.dev/v2/search";function II(e){if(!e||typeof e!="object")return{error:"Invalid input: expected an object"};let t=e,n=t.mode??"markdown";if(n!=="markdown"&&n!=="raw"&&n!=="search")return{error:`Invalid input: mode must be one of "markdown", "raw", "search" (got ${JSON.stringify(n)})`};let r=n,o,s;if(r==="search"){if(typeof t.query!="string"||t.query.length===0)return{error:'Invalid input: search mode requires a non-empty "query" string'};s=t.query}else{if(typeof t.url!="string"||t.url.length===0)return{error:`Invalid input: ${r} mode requires a non-empty "url" string`};let l;try{l=new URL(t.url)}catch{return{error:`Invalid input: "${t.url}" is not a valid absolute URL`}}if(l.protocol!=="http:"&&l.protocol!=="https:")return{error:`Invalid input: protocol "${l.protocol}" not supported (http/https only)`};o=l.toString()}let i=3e4;if(t.timeout_ms!==void 0){if(typeof t.timeout_ms!="number"||!Number.isFinite(t.timeout_ms)||t.timeout_ms<=0)return{error:"Invalid input: timeout_ms must be a positive finite number"};i=Math.min(t.timeout_ms,12e4)}let a=1e6;if(t.max_bytes!==void 0){if(typeof t.max_bytes!="number"||!Number.isFinite(t.max_bytes)||t.max_bytes<=0)return{error:"Invalid input: max_bytes must be a positive finite number"};a=Math.min(t.max_bytes,1e7)}return{mode:r,url:o,query:s,timeoutMs:i,maxBytes:a}}function PI(e,t){if(e.mode==="raw")return{target:e.url,init:{method:"GET",headers:{"User-Agent":"agent-afk/web_scrape",Accept:"*/*"}}};let r={"User-Agent":"agent-afk/web_scrape",Accept:"application/json","Content-Type":"application/json",Authorization:`Bearer ${t.FIRECRAWL_API_KEY}`},o=Math.min(Math.max(e.timeoutMs,1e3),3e5);return e.mode==="markdown"?{target:_I,init:{method:"POST",headers:r,body:JSON.stringify({url:e.url,formats:["markdown"],onlyMainContent:!0,timeout:o})}}:{target:CI,init:{method:"POST",headers:r,body:JSON.stringify({query:e.query,limit:10,timeout:o})}}}function MI(e,t){if(t.length===0)return`# Search results for "${e}"
1051
1051
 
1052
1052
  (no results)`;let n=[`# Search results for "${e}"`,""];return t.forEach((r,o)=>{let s=r.title??"(untitled)";n.push(`## ${o+1}. ${s}`),r.url&&n.push(r.url),r.description&&n.push(r.description),typeof r.markdown=="string"&&r.markdown.length>0&&(n.push(""),n.push(r.markdown)),n.push("")}),n.join(`
1053
- `)}function rh(e){let t=e.code?` (${e.code})`:"",n=e.error??"unknown error";return`web_scrape Firecrawl error${t}: ${n}`}function Wu(e,t){let n=Buffer.from(e,"utf8");return n.byteLength<=t?e:n.subarray(0,t).toString("utf8")+AI}function OI(e={}){let t=e.fetchFn??globalThis.fetch,n=e.env??process.env;return async(r,o)=>{if(typeof t!="function")return{content:"web_scrape unavailable: global fetch() is not present in this runtime (agent-afk requires Node 20+).",isError:!0};let s=II(r);if("error"in s)return{content:s.error,isError:!0};if(s.mode!=="raw"&&!n.FIRECRAWL_API_KEY)return{content:`web_scrape ${s.mode} mode requires FIRECRAWL_API_KEY in the environment. Get a key at https://firecrawl.dev. Use mode: "raw" for direct fetches that don't need server-side rendering or search.`,isError:!0};let i=PI(s,n);if(o.aborted){let u=o.reason;return{content:`web_scrape aborted: ${u instanceof Error?u.message:String(u??"aborted")}`,isError:!0}}let a=new AbortController,l=()=>{a.abort(o.reason)},c;try{o.addEventListener("abort",l,{once:!0}),c=setTimeout(()=>{a.abort(new Error(`web_scrape timeout after ${s.timeoutMs}ms`))},s.timeoutMs);let u;try{u=await t(i.target,{...i.init,signal:a.signal})}catch(h){if(a.signal.aborted){let b=a.signal.reason;return{content:`web_scrape aborted: ${b instanceof Error?b.message:String(b??"aborted")}`,isError:!0}}return{content:`web_scrape network error: ${h instanceof Error?h.message:String(h)}`,isError:!0}}if(!u.ok){let h="";try{h=await u.text()}catch{}let b="";if(h)try{let y=JSON.parse(h);y.error&&(b=`: ${y.error}`)}catch{b=`: ${h.length>200?h.slice(0,200)+"\u2026":h}`}return{content:`web_scrape HTTP ${u.status} ${u.statusText||""}`.trimEnd()+` for ${i.target}${b}`,isError:!0}}let d;try{d=await u.text()}catch(h){return{content:`web_scrape read error: ${h instanceof Error?h.message:String(h)}`,isError:!0}}if(s.mode==="raw")return{content:Wu(d,s.maxBytes)};if(s.mode==="markdown"){let h;try{h=JSON.parse(d)}catch(y){return{content:`web_scrape Firecrawl response was not JSON: ${y instanceof Error?y.message:String(y)}`,isError:!0}}if(h.success===!1)return{content:rh(h),isError:!0};let b=h.data?.markdown;return typeof b!="string"?{content:`web_scrape Firecrawl returned no markdown content for ${s.url}`,isError:!0}:{content:Wu(b,s.maxBytes)}}let m;try{m=JSON.parse(d)}catch(h){return{content:`web_scrape Firecrawl response was not JSON: ${h instanceof Error?h.message:String(h)}`,isError:!0}}if(m.success===!1)return{content:rh(m),isError:!0};let f=m.data?.web??[],g=MI(s.query,f);return{content:Wu(g,s.maxBytes)}}finally{c!==void 0&&clearTimeout(c),o.removeEventListener("abort",l)}}}var oh=OI();import{existsSync as lh,readFileSync as HI}from"node:fs";import{readFile as KI}from"node:fs/promises";import{join as GI}from"node:path";W();import{existsSync as ih,mkdirSync as $I,readFileSync as DI,renameSync as LI,unlinkSync as FI,writeFileSync as NI}from"node:fs";import{dirname as sh,join as BI}from"node:path";import{randomBytes as UI}from"node:crypto";function yt(e){let t=e??su();if(!ih(t))return[];try{let n=DI(t,"utf-8");return JSON.parse(n)}catch(n){let r=n instanceof Error?n.message:String(n);return console.error(`[schedule-store] failed to parse ${t}: ${r}`),[]}}function sr(e,t){let n=t??su();$I(sh(n),{recursive:!0});let r=BI(sh(n),`.schedules.json.${process.pid}.${UI(4).toString("hex")}.tmp`),o=JSON.stringify(e,null,2);try{NI(r,o,"utf-8"),LI(r,n)}catch(s){try{ih(r)&&FI(r)}catch{}throw s}}function la(e,t){let n=yt(t),r=n.map(l=>l.id),o=jI(e.name),s=WI(o,r),i=new Date().toISOString(),a={...e,notifyOn:e.notifyOn??"failure",id:s,createdAt:i,updatedAt:i};return n.push(a),sr(n,t),a}function ca(e,t){let n=yt(t),r=n.length,o=n.filter(s=>s.id!==e);return o.length===r?!1:(sr(o,t),!0)}function Ss(e,t){return yt(t).find(n=>n.id===e)}function jI(e){return e.toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/-{2,}/g,"-").replace(/^-+|-+$/g,"")}function WI(e,t){if(!t.includes(e))return e;let n=2;for(;t.includes(`${e}-${n}`);)n+=1;return`${e}-${n}`}function ah(e){return{taskId:e.id,command:e.command,trigger:e.trigger??"cron",...e.cron!==void 0?{cronExpression:e.cron}:{},...e.notifyOn!==void 0?{notifyOn:e.notifyOn}:{}}}W();async function Hu(e,t,n){try{let r=GI(Nr("default"),"port");if(!lh(r))return;let o=HI(r,"utf-8").trim(),s=parseInt(o,10);if(Number.isNaN(s))return;await fetch(`http://localhost:${s}${t}`,{method:e,headers:{"Content-Type":"application/json"},body:n!==void 0?JSON.stringify(n):void 0,signal:AbortSignal.timeout(2e3)})}catch{}}var ch=async(e,t)=>{if(!e||typeof e!="object")return{content:"Invalid input: expected object",isError:!0};let n=e;if(typeof n.name!="string"||!n.name)return{content:"Invalid input: name required",isError:!0};if(typeof n.command!="string"||!n.command)return{content:"Invalid input: command required",isError:!0};if(typeof n.cron!="string"||!n.cron)return{content:"Invalid input: cron required",isError:!0};let r=n.cron.trim().split(/\s+/);if(r.length!==5&&r.length!==6)return{content:"Invalid input: cron must be a 5 or 6-field expression",isError:!0};let o=la({name:n.name,command:n.command,cron:n.cron,trigger:n.trigger??"cron",notifyOn:n.notifyOn,enabled:typeof n.enabled=="boolean"?n.enabled:!0});return await Hu("POST","/tasks",{taskId:o.id,command:o.command,cron:o.cron,trigger:o.trigger,notifyOn:o.notifyOn}),{content:JSON.stringify({id:o.id,name:o.name,cron:o.cron,enabled:o.enabled})}},uh=async(e,t)=>{let n=yt();return{content:JSON.stringify(n.map(r=>({id:r.id,name:r.name,cron:r.cron,trigger:r.trigger,enabled:r.enabled,notifyOn:r.notifyOn})))}},dh=async(e,t)=>{if(!e||typeof e!="object")return{content:"Invalid input: expected object",isError:!0};let n=e;if(typeof n.taskId!="string"||!n.taskId)return{content:"Invalid input: taskId required",isError:!0};let r=n.taskId,o=typeof n.limit=="number"?Math.min(Math.max(1,n.limit),50):10,s=$t();if(!lh(s))return{content:JSON.stringify([])};let i;try{let c=await KI(s);i=(c.length>1048576?c.subarray(c.length-1048576):c).toString("utf-8")}catch{return{content:JSON.stringify([])}}let a=i.split(`
1054
- `),l=[];for(let c=a.length-1;c>=0;c-=1){let u=a[c];if(u)try{let d=JSON.parse(u);if(d.taskId!==r)continue;if(l.push(d),l.length>=o)break}catch{continue}}return{content:JSON.stringify(l.reverse())}},ph=async(e,t)=>{if(!e||typeof e!="object")return{content:"Invalid input: expected object",isError:!0};let n=e;if(typeof n.taskId!="string"||!n.taskId)return{content:"Invalid input: taskId required",isError:!0};let r=n.taskId,o=n.permanent===!0;if(!Ss(r))return{content:JSON.stringify({error:"task not found"})};if(o)ca(r),await Hu("DELETE",`/tasks/${r}`);else{let a=yt().map(l=>l.id===r?{...l,enabled:!1,updatedAt:new Date().toISOString()}:l);sr(a),await Hu("DELETE",`/tasks/${r}`)}return{content:JSON.stringify({ok:!0,taskId:r,permanent:o})}};import{existsSync as fh}from"node:fs";import{readFile as gh,writeFile as qI,rename as zI}from"node:fs/promises";import{homedir as JI}from"node:os";import{join as VI}from"node:path";var Ku=6,Gu=60,YI=JI();function qu(e){return e.startsWith("~/")?VI(YI,e.slice(2)):e}function XI(){let e=[{name:"Cursor",path:"~/Library/Application Support/Cursor/User/settings.json"},{name:"VS Code",path:"~/Library/Application Support/Code/User/settings.json"},{name:"VS Code Insiders",path:"~/Library/Application Support/Code - Insiders/User/settings.json"}];return process.platform==="linux"&&e.push({name:"VS Code",path:"~/.config/Code/User/settings.json"},{name:"Cursor",path:"~/.config/Cursor/User/settings.json"}),e.filter(t=>fh(qu(t.path)))}function mh(e){return e.toLowerCase().replace(/\s+/g,"")}function zu(e={}){let t=e.discoverFn??XI,n=e.writeFn??qI;return async(r,o)=>{if(!r||typeof r!="object")return{content:"Invalid input: expected object",isError:!0};let s=r,i=s.action;if(i!=="get"&&i!=="set")return{content:'Invalid action. Use "action": "get" to read current font sizes, or "action": "set" with "size": <number> to update them.',isError:!0};let a;if(i==="set"){if(typeof s.size!="number")return{content:`Invalid input: "size" must be a number between ${Ku} and ${Gu}.`,isError:!0};if(a=s.size,a<Ku||a>Gu)return{content:`Invalid font size ${a}. Must be between ${Ku} and ${Gu}.`,isError:!0}}let l=typeof s.editor=="string"?s.editor:void 0,c=t();if(l!==void 0){let u=mh(l);if(!["cursor","vscode","vscodeinsiders"].includes(u))return{content:`Unknown editor "${l}". Supported editors: Cursor, VS Code, VS Code Insiders.`,isError:!0};c=c.filter(m=>mh(m.name)===u)}return c.length===0?{content:l!==void 0?`No settings.json found for editor "${l}".`:"No supported editors detected (Cursor or VS Code not installed, or settings file not found)."}:i==="get"?ZI(c):QI(c,a,n)}}async function ZI(e){let t=[];for(let n of e){let r=qu(n.path);if(!fh(r)){t.push(`${n.name}: terminal.integrated.fontSize = (default, ~12\u201314)`);continue}let o;try{o=await gh(r,"utf-8")}catch{t.push(`${n.name}: (could not read settings \u2014 file may contain comments or be malformed)`);continue}let s;try{s=JSON.parse(o)}catch{t.push(`${n.name}: (could not read settings \u2014 file may contain comments or be malformed)`);continue}let i=s["terminal.integrated.fontSize"];typeof i=="number"?t.push(`${n.name}: terminal.integrated.fontSize = ${i}`):t.push(`${n.name}: terminal.integrated.fontSize = (not set \u2014 editor default applies)`)}return{content:t.join(`
1053
+ `)}function rh(e){let t=e.code?` (${e.code})`:"",n=e.error??"unknown error";return`web_scrape Firecrawl error${t}: ${n}`}function Wu(e,t){let n=Buffer.from(e,"utf8");return n.byteLength<=t?e:n.subarray(0,t).toString("utf8")+AI}function OI(e={}){let t=e.fetchFn??globalThis.fetch,n=e.env??process.env;return async(r,o)=>{if(typeof t!="function")return{content:"web_scrape unavailable: global fetch() is not present in this runtime (agent-afk requires Node 20+).",isError:!0};let s=II(r);if("error"in s)return{content:s.error,isError:!0};if(s.mode!=="raw"&&!n.FIRECRAWL_API_KEY)return{content:`web_scrape ${s.mode} mode requires FIRECRAWL_API_KEY in the environment. Get a key at https://firecrawl.dev. Use mode: "raw" for direct fetches that don't need server-side rendering or search.`,isError:!0};let i=PI(s,n);if(o.aborted){let u=o.reason;return{content:`web_scrape aborted: ${u instanceof Error?u.message:String(u??"aborted")}`,isError:!0}}let a=new AbortController,l=()=>{a.abort(o.reason)},c;try{o.addEventListener("abort",l,{once:!0}),c=setTimeout(()=>{a.abort(new Error(`web_scrape timeout after ${s.timeoutMs}ms`))},s.timeoutMs);let u;try{u=await t(i.target,{...i.init,signal:a.signal})}catch(h){if(a.signal.aborted){let b=a.signal.reason;return{content:`web_scrape aborted: ${b instanceof Error?b.message:String(b??"aborted")}`,isError:!0}}return{content:`web_scrape network error: ${h instanceof Error?h.message:String(h)}`,isError:!0}}if(!u.ok){let h="";try{h=await u.text()}catch{}let b="";if(h)try{let y=JSON.parse(h);y.error&&(b=`: ${y.error}`)}catch{b=`: ${h.length>200?h.slice(0,200)+"\u2026":h}`}return{content:`web_scrape HTTP ${u.status} ${u.statusText||""}`.trimEnd()+` for ${i.target}${b}`,isError:!0}}let d;try{d=await u.text()}catch(h){return{content:`web_scrape read error: ${h instanceof Error?h.message:String(h)}`,isError:!0}}if(s.mode==="raw")return{content:Wu(d,s.maxBytes)};if(s.mode==="markdown"){let h;try{h=JSON.parse(d)}catch(y){return{content:`web_scrape Firecrawl response was not JSON: ${y instanceof Error?y.message:String(y)}`,isError:!0}}if(h.success===!1)return{content:rh(h),isError:!0};let b=h.data?.markdown;return typeof b!="string"?{content:`web_scrape Firecrawl returned no markdown content for ${s.url}`,isError:!0}:{content:Wu(b,s.maxBytes)}}let p;try{p=JSON.parse(d)}catch(h){return{content:`web_scrape Firecrawl response was not JSON: ${h instanceof Error?h.message:String(h)}`,isError:!0}}if(p.success===!1)return{content:rh(p),isError:!0};let f=p.data?.web??[],g=MI(s.query,f);return{content:Wu(g,s.maxBytes)}}finally{c!==void 0&&clearTimeout(c),o.removeEventListener("abort",l)}}}var oh=OI();import{existsSync as lh,readFileSync as HI}from"node:fs";import{readFile as KI}from"node:fs/promises";import{join as GI}from"node:path";W();import{existsSync as ih,mkdirSync as $I,readFileSync as DI,renameSync as LI,unlinkSync as FI,writeFileSync as NI}from"node:fs";import{dirname as sh,join as BI}from"node:path";import{randomBytes as UI}from"node:crypto";function yt(e){let t=e??su();if(!ih(t))return[];try{let n=DI(t,"utf-8");return JSON.parse(n)}catch(n){let r=n instanceof Error?n.message:String(n);return console.error(`[schedule-store] failed to parse ${t}: ${r}`),[]}}function sr(e,t){let n=t??su();$I(sh(n),{recursive:!0});let r=BI(sh(n),`.schedules.json.${process.pid}.${UI(4).toString("hex")}.tmp`),o=JSON.stringify(e,null,2);try{NI(r,o,"utf-8"),LI(r,n)}catch(s){try{ih(r)&&FI(r)}catch{}throw s}}function la(e,t){let n=yt(t),r=n.map(l=>l.id),o=jI(e.name),s=WI(o,r),i=new Date().toISOString(),a={...e,notifyOn:e.notifyOn??"failure",id:s,createdAt:i,updatedAt:i};return n.push(a),sr(n,t),a}function ca(e,t){let n=yt(t),r=n.length,o=n.filter(s=>s.id!==e);return o.length===r?!1:(sr(o,t),!0)}function Ss(e,t){return yt(t).find(n=>n.id===e)}function jI(e){return e.toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/-{2,}/g,"-").replace(/^-+|-+$/g,"")}function WI(e,t){if(!t.includes(e))return e;let n=2;for(;t.includes(`${e}-${n}`);)n+=1;return`${e}-${n}`}function ah(e){return{taskId:e.id,command:e.command,trigger:e.trigger??"cron",...e.cron!==void 0?{cronExpression:e.cron}:{},...e.notifyOn!==void 0?{notifyOn:e.notifyOn}:{}}}W();async function Hu(e,t,n){try{let r=GI(Nr("default"),"port");if(!lh(r))return;let o=HI(r,"utf-8").trim(),s=parseInt(o,10);if(Number.isNaN(s))return;await fetch(`http://localhost:${s}${t}`,{method:e,headers:{"Content-Type":"application/json"},body:n!==void 0?JSON.stringify(n):void 0,signal:AbortSignal.timeout(2e3)})}catch{}}var ch=async(e,t)=>{if(!e||typeof e!="object")return{content:"Invalid input: expected object",isError:!0};let n=e;if(typeof n.name!="string"||!n.name)return{content:"Invalid input: name required",isError:!0};if(typeof n.command!="string"||!n.command)return{content:"Invalid input: command required",isError:!0};if(typeof n.cron!="string"||!n.cron)return{content:"Invalid input: cron required",isError:!0};let r=n.cron.trim().split(/\s+/);if(r.length!==5&&r.length!==6)return{content:"Invalid input: cron must be a 5 or 6-field expression",isError:!0};let o=la({name:n.name,command:n.command,cron:n.cron,trigger:n.trigger??"cron",notifyOn:n.notifyOn,enabled:typeof n.enabled=="boolean"?n.enabled:!0});return await Hu("POST","/tasks",{taskId:o.id,command:o.command,cron:o.cron,trigger:o.trigger,notifyOn:o.notifyOn}),{content:JSON.stringify({id:o.id,name:o.name,cron:o.cron,enabled:o.enabled})}},uh=async(e,t)=>{let n=yt();return{content:JSON.stringify(n.map(r=>({id:r.id,name:r.name,cron:r.cron,trigger:r.trigger,enabled:r.enabled,notifyOn:r.notifyOn})))}},dh=async(e,t)=>{if(!e||typeof e!="object")return{content:"Invalid input: expected object",isError:!0};let n=e;if(typeof n.taskId!="string"||!n.taskId)return{content:"Invalid input: taskId required",isError:!0};let r=n.taskId,o=typeof n.limit=="number"?Math.min(Math.max(1,n.limit),50):10,s=$t();if(!lh(s))return{content:JSON.stringify([])};let i;try{let c=await KI(s);i=(c.length>1048576?c.subarray(c.length-1048576):c).toString("utf-8")}catch{return{content:JSON.stringify([])}}let a=i.split(`
1054
+ `),l=[];for(let c=a.length-1;c>=0;c-=1){let u=a[c];if(u)try{let d=JSON.parse(u);if(d.taskId!==r)continue;if(l.push(d),l.length>=o)break}catch{continue}}return{content:JSON.stringify(l.reverse())}},ph=async(e,t)=>{if(!e||typeof e!="object")return{content:"Invalid input: expected object",isError:!0};let n=e;if(typeof n.taskId!="string"||!n.taskId)return{content:"Invalid input: taskId required",isError:!0};let r=n.taskId,o=n.permanent===!0;if(!Ss(r))return{content:JSON.stringify({error:"task not found"})};if(o)ca(r),await Hu("DELETE",`/tasks/${r}`);else{let a=yt().map(l=>l.id===r?{...l,enabled:!1,updatedAt:new Date().toISOString()}:l);sr(a),await Hu("DELETE",`/tasks/${r}`)}return{content:JSON.stringify({ok:!0,taskId:r,permanent:o})}};import{existsSync as fh}from"node:fs";import{readFile as gh,writeFile as qI,rename as zI}from"node:fs/promises";import{homedir as JI}from"node:os";import{join as VI}from"node:path";var Ku=6,Gu=60,YI=JI();function qu(e){return e.startsWith("~/")?VI(YI,e.slice(2)):e}function XI(){let e=[{name:"Cursor",path:"~/Library/Application Support/Cursor/User/settings.json"},{name:"VS Code",path:"~/Library/Application Support/Code/User/settings.json"},{name:"VS Code Insiders",path:"~/Library/Application Support/Code - Insiders/User/settings.json"}];return process.platform==="linux"&&e.push({name:"VS Code",path:"~/.config/Code/User/settings.json"},{name:"Cursor",path:"~/.config/Cursor/User/settings.json"}),e.filter(t=>fh(qu(t.path)))}function mh(e){return e.toLowerCase().replace(/\s+/g,"")}function zu(e={}){let t=e.discoverFn??XI,n=e.writeFn??qI;return async(r,o)=>{if(!r||typeof r!="object")return{content:"Invalid input: expected object",isError:!0};let s=r,i=s.action;if(i!=="get"&&i!=="set")return{content:'Invalid action. Use "action": "get" to read current font sizes, or "action": "set" with "size": <number> to update them.',isError:!0};let a;if(i==="set"){if(typeof s.size!="number")return{content:`Invalid input: "size" must be a number between ${Ku} and ${Gu}.`,isError:!0};if(a=s.size,a<Ku||a>Gu)return{content:`Invalid font size ${a}. Must be between ${Ku} and ${Gu}.`,isError:!0}}let l=typeof s.editor=="string"?s.editor:void 0,c=t();if(l!==void 0){let u=mh(l);if(!["cursor","vscode","vscodeinsiders"].includes(u))return{content:`Unknown editor "${l}". Supported editors: Cursor, VS Code, VS Code Insiders.`,isError:!0};c=c.filter(p=>mh(p.name)===u)}return c.length===0?{content:l!==void 0?`No settings.json found for editor "${l}".`:"No supported editors detected (Cursor or VS Code not installed, or settings file not found)."}:i==="get"?ZI(c):QI(c,a,n)}}async function ZI(e){let t=[];for(let n of e){let r=qu(n.path);if(!fh(r)){t.push(`${n.name}: terminal.integrated.fontSize = (default, ~12\u201314)`);continue}let o;try{o=await gh(r,"utf-8")}catch{t.push(`${n.name}: (could not read settings \u2014 file may contain comments or be malformed)`);continue}let s;try{s=JSON.parse(o)}catch{t.push(`${n.name}: (could not read settings \u2014 file may contain comments or be malformed)`);continue}let i=s["terminal.integrated.fontSize"];typeof i=="number"?t.push(`${n.name}: terminal.integrated.fontSize = ${i}`):t.push(`${n.name}: terminal.integrated.fontSize = (not set \u2014 editor default applies)`)}return{content:t.join(`
1055
1055
  `)}}async function QI(e,t,n){let r=[],o=!1;for(let s of e){let i=qu(s.path),a={};try{let u=await gh(i,"utf-8");try{a=JSON.parse(u)}catch{r.push(`${s.name}: could not update \u2014 settings file may contain comments or be malformed. Edit manually: ${i}`),o=!0;continue}}catch(u){let d=u;if(d.code!=="ENOENT"){r.push(`${s.name}: could not read settings \u2014 ${d.message}`),o=!0;continue}a={}}a["terminal.integrated.fontSize"]=t;let l=`${i}.tmp`,c=JSON.stringify(a,null,2)+`
1056
1056
  `;try{await n(l,c,"utf-8"),await zI(l,i),r.push(`${s.name}: terminal.integrated.fontSize set to ${t}`)}catch(u){let d=u;r.push(`${s.name}: could not write settings \u2014 ${d.message}`),o=!0}}return o?{content:r.join(`
1057
1057
  `),isError:!0}:{content:r.join(`
1058
- `)}}var Ju=zu();var Vr={action:"decline"},Vu=class{handler=null;queue=Promise.resolve();queueDepth=0;install(t){this.handler=t}uninstall(){this.handler=null}pendingCount(){return this.queueDepth}route(t,n){if(n.signal.aborted)return Promise.resolve(Vr);this.queueDepth+=1;let r,o=new Promise(i=>{r=i}),s=this.handler;return this.queue=this.queue.then(async()=>{try{if(n.signal.aborted){r(Vr);return}if(!s){r(Vr);return}let i,a=new Promise(l=>{i=()=>l(Vr),n.signal.addEventListener("abort",i,{once:!0})});try{let l=await Promise.race([s(t,n).catch(()=>Vr),a]);r(l)}finally{n.signal.removeEventListener("abort",i)}}finally{this.queueDepth-=1,r(Vr)}}).catch(()=>{}),o}},Bt=new Vu;var hh=async(e,t)=>{if(!e||typeof e!="object")return{content:"Invalid input: expected an object",isError:!0};let n=e,r=n.question;if(typeof r!="string"||r.trim()==="")return{content:"Invalid input: question must be a non-empty string",isError:!0};let o=new Set(["text","confirm","choice","multi_choice","number"]),s=n.type??"text";if(typeof s!="string"||!o.has(s))return{content:"Invalid input: type must be one of: text, confirm, choice, multi_choice, number",isError:!0};if(s==="choice"||s==="multi_choice"){let f=n.choices;if(!Array.isArray(f)||f.length===0)return{content:`Invalid input: choices array is required and must be non-empty for type "${s}"`,isError:!0};if(f.length>100)return{content:`Invalid input: choices array must not exceed 100 items, got ${f.length}`,isError:!0};for(let g of f)if(typeof g!="string")return{content:"Invalid input: all choices must be strings",isError:!0}}let i=n.min,a=n.max;if(i!==void 0&&(typeof i!="number"||!Number.isFinite(i)))return{content:"Invalid input: min must be a finite number",isError:!0};if(a!==void 0&&(typeof a!="number"||!Number.isFinite(a)))return{content:"Invalid input: max must be a finite number",isError:!0};if(i!==void 0&&a!==void 0&&i>a)return{content:`Invalid input: min (${i}) must be \u2264 max (${a})`,isError:!0};let l=n.min_length,c=n.max_length;if(l!==void 0&&typeof l!="number")return{content:"Invalid input: min_length must be a number",isError:!0};if(c!==void 0&&typeof c!="number")return{content:"Invalid input: max_length must be a number",isError:!0};if((l!==void 0||c!==void 0)&&s!=="text")return{content:`Invalid input: min_length/max_length are only valid for type "text", got "${s}"`,isError:!0};let u={serverName:"agent",message:r.trim(),origin:"agent",type:s,...n.choices!==void 0&&{choices:n.choices},...n.context!==void 0&&typeof n.context=="string"&&{context:n.context},...n.default!==void 0&&{questionDefault:n.default},...l!==void 0&&{minLength:l},...c!==void 0&&{maxLength:c},...i!==void 0&&{min:i},...a!==void 0&&{max:a},...n.allow_skip!==void 0&&{allowSkip:!!n.allow_skip}},d=await Bt.route(u,{signal:t}),m=d.action==="decline"||d.action==="cancel";return{content:JSON.stringify(d),...m&&{isError:!0}}};G();var GP=["Cannot find package","ERR_MODULE_NOT_FOUND"],Uh=["load","domcontentloaded","networkidle"];function qP(e){if(!e||typeof e!="object")return{error:"browser_open: input must be an object"};let t=e,n=t.url;if(typeof n!="string"||n.length===0)return{error:'browser_open: "url" is required and must be a non-empty string'};let r;try{r=new URL(n)}catch{return{error:`browser_open: "${n}" is not a valid absolute URL`}}if(r.protocol!=="http:"&&r.protocol!=="https:")return{error:`browser_open: protocol "${r.protocol}" is not supported (http/https only)`};let o;if(t.wait_for!==void 0){if(!Uh.includes(t.wait_for))return{error:`browser_open: "wait_for" must be one of ${Uh.map(a=>`"${a}"`).join(", ")} (got ${JSON.stringify(t.wait_for)})`};o=t.wait_for}let s;if(t.screenshot!==void 0){if(typeof t.screenshot!="boolean")return{error:'browser_open: "screenshot" must be a boolean'};s=t.screenshot}let i;if(t.timeout_ms!==void 0){if(typeof t.timeout_ms!="number"||!Number.isFinite(t.timeout_ms)||t.timeout_ms<=0)return{error:'browser_open: "timeout_ms" must be a positive finite number'};i=t.timeout_ms}return{url:r.toString(),waitFor:o,screenshot:s,timeoutMs:i}}function zP(e={}){return async(t,n)=>{if(n.aborted){let i=n.reason;return{content:`browser_open aborted: ${i instanceof Error?i.message:String(i??"aborted")}`,isError:!0}}let r=qP(t);if("error"in r)return{content:r.error,isError:!0};let o=T.AFK_SESSION_ID??"default";if(!/^[a-zA-Z0-9_-]+$/.test(o))return{content:`Invalid AFK_SESSION_ID: must match /^[a-zA-Z0-9_-]+$/, got: ${JSON.stringify(o)}`,isError:!0};let s;try{if(e.getBrowserProvider)s=await e.getBrowserProvider();else{let{getBrowserProvider:i}=await Promise.resolve().then(()=>(Xr(),Yr));s=await i()}}catch(i){let a=i instanceof Error?i.message:String(i);return GP.some(l=>a.includes(l))?{content:"browser tools require the optional `playwright` peer dependency. Install via: pnpm add playwright. Or pick a different tool.",isError:!0}:{content:`browser_open failed to get provider: ${a}`,isError:!0}}try{let i=await s.open({sessionId:o,url:r.url,waitFor:r.waitFor,screenshot:r.screenshot,timeoutMs:r.timeoutMs});return"outcome"in i&&i.outcome==="blocked_by_policy"?{content:`browser_open blocked: ${i.reason}`,isError:!0}:{content:JSON.stringify(i,null,2)}}catch(i){return{content:`browser_open failed: ${i instanceof Error?i.message:String(i)}`,isError:!0}}}}var jh=zP();G();var JP=["Cannot find package","ERR_MODULE_NOT_FOUND"];function VP(e){if(e!=null&&typeof e!="object")return{error:"browser_observe: input must be an object or omitted"};if(!e)return{};let t=e,n;if(t.screenshot!==void 0){if(typeof t.screenshot!="boolean")return{error:'browser_observe: "screenshot" must be a boolean'};n=t.screenshot}let r;if(t.include_hidden!==void 0){if(typeof t.include_hidden!="boolean")return{error:'browser_observe: "include_hidden" must be a boolean'};r=t.include_hidden}let o;if(t.max_elements!==void 0){if(typeof t.max_elements!="number"||!Number.isFinite(t.max_elements)||t.max_elements<=0||!Number.isInteger(t.max_elements))return{error:'browser_observe: "max_elements" must be a positive integer'};o=t.max_elements}return{screenshot:n,includeHidden:r,maxElements:o}}function YP(e={}){return async(t,n)=>{if(n.aborted){let i=n.reason;return{content:`browser_observe aborted: ${i instanceof Error?i.message:String(i??"aborted")}`,isError:!0}}let r=VP(t);if("error"in r)return{content:r.error,isError:!0};let o=T.AFK_SESSION_ID??"default";if(!/^[a-zA-Z0-9_-]+$/.test(o))return{content:`Invalid AFK_SESSION_ID: must match /^[a-zA-Z0-9_-]+$/, got: ${JSON.stringify(o)}`,isError:!0};let s;try{if(e.getBrowserProvider)s=await e.getBrowserProvider();else{let{getBrowserProvider:i}=await Promise.resolve().then(()=>(Xr(),Yr));s=await i()}}catch(i){let a=i instanceof Error?i.message:String(i);return JP.some(l=>a.includes(l))?{content:"browser tools require the optional `playwright` peer dependency. Install via: pnpm add playwright. Or pick a different tool.",isError:!0}:{content:`browser_observe failed to get provider: ${a}`,isError:!0}}try{let i=await s.observe({sessionId:o,screenshot:r.screenshot,includeHidden:r.includeHidden,maxElements:r.maxElements});return{content:JSON.stringify(i,null,2)}}catch(i){return{content:`browser_observe failed: ${i instanceof Error?i.message:String(i)}`,isError:!0}}}}var Wh=YP();ks();G();var XP=["Cannot find package","ERR_MODULE_NOT_FOUND"],Hh=["click","fill","press","select","hover","scroll_to","wait_for"],Kh=["semantic","element_id","selector"];function ZP(e){if(!e||typeof e!="object")return{error:"browser_act: input must be an object"};let t=e;if(typeof t.action!="string")return{error:'browser_act: "action" is required and must be a string'};if(!Hh.includes(t.action))return{error:`browser_act: "action" must be one of ${Hh.map(u=>`"${u}"`).join(", ")} (got ${JSON.stringify(t.action)})`};let n=t.action,r=t.target;if(!r||typeof r!="object")return{error:'browser_act: "target" is required and must be an object'};let o=r;if(typeof o.kind!="string"||!Kh.includes(o.kind))return{error:`browser_act: "target.kind" must be one of ${Kh.map(u=>`"${u}"`).join(", ")} (got ${JSON.stringify(o.kind)})`};let s,i=o.kind;if(i==="semantic"){if(typeof o.text!="string"||o.text.length===0)return{error:'browser_act: target.kind=semantic requires "target.text" (non-empty string)'};s={kind:"semantic",text:o.text,...typeof o.role=="string"&&o.role.length>0?{role:o.role}:{}}}else if(i==="element_id"){if(typeof o.element_id!="string"||o.element_id.length===0)return{error:'browser_act: target.kind=element_id requires "target.element_id" (non-empty string)'};s={kind:"element_id",elementId:o.element_id}}else{if(typeof o.selector!="string"||o.selector.length===0)return{error:'browser_act: target.kind=selector requires "target.selector" (non-empty string)'};s={kind:"selector",selector:o.selector}}let a;if(t.value!==void 0){if(typeof t.value!="string")return{error:'browser_act: "value" must be a string when provided'};a=t.value}let l;if(t.timeout_ms!==void 0){if(typeof t.timeout_ms!="number"||!Number.isFinite(t.timeout_ms)||t.timeout_ms<=0)return{error:'browser_act: "timeout_ms" must be a positive finite number'};l=t.timeout_ms}let c;if(t.screenshot!==void 0){if(typeof t.screenshot!="boolean")return{error:'browser_act: "screenshot" must be a boolean'};c=t.screenshot}return{action:n,target:s,value:a,timeoutMs:l,screenshot:c}}function QP(e,t){let n=e.role?`"${e.text}" (role: ${e.role})`:`"${e.text}"`;return[`browser_act: ambiguous target \u2014 ${t.length} elements matched ${n}.`,'Retry with target.kind="element_id" using one of the following:',...t.map(o=>` id=${o.id} role=${o.role} label=${o.label}`)].join(`
1058
+ `)}}var Ju=zu();var Vr={action:"decline"},Vu=class{handler=null;queue=Promise.resolve();queueDepth=0;install(t){this.handler=t}uninstall(){this.handler=null}pendingCount(){return this.queueDepth}route(t,n){if(n.signal.aborted)return Promise.resolve(Vr);this.queueDepth+=1;let r,o=new Promise(i=>{r=i}),s=this.handler;return this.queue=this.queue.then(async()=>{try{if(n.signal.aborted){r(Vr);return}if(!s){r(Vr);return}let i,a=new Promise(l=>{i=()=>l(Vr),n.signal.addEventListener("abort",i,{once:!0})});try{let l=await Promise.race([s(t,n).catch(()=>Vr),a]);r(l)}finally{n.signal.removeEventListener("abort",i)}}finally{this.queueDepth-=1,r(Vr)}}).catch(()=>{}),o}},Bt=new Vu;var hh=async(e,t)=>{if(!e||typeof e!="object")return{content:"Invalid input: expected an object",isError:!0};let n=e,r=n.question;if(typeof r!="string"||r.trim()==="")return{content:"Invalid input: question must be a non-empty string",isError:!0};let o=new Set(["text","confirm","choice","multi_choice","number"]),s=n.type??"text";if(typeof s!="string"||!o.has(s))return{content:"Invalid input: type must be one of: text, confirm, choice, multi_choice, number",isError:!0};if(s==="choice"||s==="multi_choice"){let f=n.choices;if(!Array.isArray(f)||f.length===0)return{content:`Invalid input: choices array is required and must be non-empty for type "${s}"`,isError:!0};if(f.length>100)return{content:`Invalid input: choices array must not exceed 100 items, got ${f.length}`,isError:!0};for(let g of f)if(typeof g!="string")return{content:"Invalid input: all choices must be strings",isError:!0}}let i=n.min,a=n.max;if(i!==void 0&&(typeof i!="number"||!Number.isFinite(i)))return{content:"Invalid input: min must be a finite number",isError:!0};if(a!==void 0&&(typeof a!="number"||!Number.isFinite(a)))return{content:"Invalid input: max must be a finite number",isError:!0};if(i!==void 0&&a!==void 0&&i>a)return{content:`Invalid input: min (${i}) must be \u2264 max (${a})`,isError:!0};let l=n.min_length,c=n.max_length;if(l!==void 0&&typeof l!="number")return{content:"Invalid input: min_length must be a number",isError:!0};if(c!==void 0&&typeof c!="number")return{content:"Invalid input: max_length must be a number",isError:!0};if((l!==void 0||c!==void 0)&&s!=="text")return{content:`Invalid input: min_length/max_length are only valid for type "text", got "${s}"`,isError:!0};let u={serverName:"agent",message:r.trim(),origin:"agent",type:s,...n.choices!==void 0&&{choices:n.choices},...n.context!==void 0&&typeof n.context=="string"&&{context:n.context},...n.default!==void 0&&{questionDefault:n.default},...l!==void 0&&{minLength:l},...c!==void 0&&{maxLength:c},...i!==void 0&&{min:i},...a!==void 0&&{max:a},...n.allow_skip!==void 0&&{allowSkip:!!n.allow_skip}},d=await Bt.route(u,{signal:t}),p=d.action==="decline"||d.action==="cancel";return{content:JSON.stringify(d),...p&&{isError:!0}}};G();var GP=["Cannot find package","ERR_MODULE_NOT_FOUND"],Uh=["load","domcontentloaded","networkidle"];function qP(e){if(!e||typeof e!="object")return{error:"browser_open: input must be an object"};let t=e,n=t.url;if(typeof n!="string"||n.length===0)return{error:'browser_open: "url" is required and must be a non-empty string'};let r;try{r=new URL(n)}catch{return{error:`browser_open: "${n}" is not a valid absolute URL`}}if(r.protocol!=="http:"&&r.protocol!=="https:")return{error:`browser_open: protocol "${r.protocol}" is not supported (http/https only)`};let o;if(t.wait_for!==void 0){if(!Uh.includes(t.wait_for))return{error:`browser_open: "wait_for" must be one of ${Uh.map(a=>`"${a}"`).join(", ")} (got ${JSON.stringify(t.wait_for)})`};o=t.wait_for}let s;if(t.screenshot!==void 0){if(typeof t.screenshot!="boolean")return{error:'browser_open: "screenshot" must be a boolean'};s=t.screenshot}let i;if(t.timeout_ms!==void 0){if(typeof t.timeout_ms!="number"||!Number.isFinite(t.timeout_ms)||t.timeout_ms<=0)return{error:'browser_open: "timeout_ms" must be a positive finite number'};i=t.timeout_ms}return{url:r.toString(),waitFor:o,screenshot:s,timeoutMs:i}}function zP(e={}){return async(t,n)=>{if(n.aborted){let i=n.reason;return{content:`browser_open aborted: ${i instanceof Error?i.message:String(i??"aborted")}`,isError:!0}}let r=qP(t);if("error"in r)return{content:r.error,isError:!0};let o=T.AFK_SESSION_ID??"default";if(!/^[a-zA-Z0-9_-]+$/.test(o))return{content:`Invalid AFK_SESSION_ID: must match /^[a-zA-Z0-9_-]+$/, got: ${JSON.stringify(o)}`,isError:!0};let s;try{if(e.getBrowserProvider)s=await e.getBrowserProvider();else{let{getBrowserProvider:i}=await Promise.resolve().then(()=>(Xr(),Yr));s=await i()}}catch(i){let a=i instanceof Error?i.message:String(i);return GP.some(l=>a.includes(l))?{content:"browser tools require the optional `playwright` peer dependency. Install via: pnpm add playwright. Or pick a different tool.",isError:!0}:{content:`browser_open failed to get provider: ${a}`,isError:!0}}try{let i=await s.open({sessionId:o,url:r.url,waitFor:r.waitFor,screenshot:r.screenshot,timeoutMs:r.timeoutMs});return"outcome"in i&&i.outcome==="blocked_by_policy"?{content:`browser_open blocked: ${i.reason}`,isError:!0}:{content:JSON.stringify(i,null,2)}}catch(i){return{content:`browser_open failed: ${i instanceof Error?i.message:String(i)}`,isError:!0}}}}var jh=zP();G();var JP=["Cannot find package","ERR_MODULE_NOT_FOUND"];function VP(e){if(e!=null&&typeof e!="object")return{error:"browser_observe: input must be an object or omitted"};if(!e)return{};let t=e,n;if(t.screenshot!==void 0){if(typeof t.screenshot!="boolean")return{error:'browser_observe: "screenshot" must be a boolean'};n=t.screenshot}let r;if(t.include_hidden!==void 0){if(typeof t.include_hidden!="boolean")return{error:'browser_observe: "include_hidden" must be a boolean'};r=t.include_hidden}let o;if(t.max_elements!==void 0){if(typeof t.max_elements!="number"||!Number.isFinite(t.max_elements)||t.max_elements<=0||!Number.isInteger(t.max_elements))return{error:'browser_observe: "max_elements" must be a positive integer'};o=t.max_elements}return{screenshot:n,includeHidden:r,maxElements:o}}function YP(e={}){return async(t,n)=>{if(n.aborted){let i=n.reason;return{content:`browser_observe aborted: ${i instanceof Error?i.message:String(i??"aborted")}`,isError:!0}}let r=VP(t);if("error"in r)return{content:r.error,isError:!0};let o=T.AFK_SESSION_ID??"default";if(!/^[a-zA-Z0-9_-]+$/.test(o))return{content:`Invalid AFK_SESSION_ID: must match /^[a-zA-Z0-9_-]+$/, got: ${JSON.stringify(o)}`,isError:!0};let s;try{if(e.getBrowserProvider)s=await e.getBrowserProvider();else{let{getBrowserProvider:i}=await Promise.resolve().then(()=>(Xr(),Yr));s=await i()}}catch(i){let a=i instanceof Error?i.message:String(i);return JP.some(l=>a.includes(l))?{content:"browser tools require the optional `playwright` peer dependency. Install via: pnpm add playwright. Or pick a different tool.",isError:!0}:{content:`browser_observe failed to get provider: ${a}`,isError:!0}}try{let i=await s.observe({sessionId:o,screenshot:r.screenshot,includeHidden:r.includeHidden,maxElements:r.maxElements});return{content:JSON.stringify(i,null,2)}}catch(i){return{content:`browser_observe failed: ${i instanceof Error?i.message:String(i)}`,isError:!0}}}}var Wh=YP();ks();G();var XP=["Cannot find package","ERR_MODULE_NOT_FOUND"],Hh=["click","fill","press","select","hover","scroll_to","wait_for"],Kh=["semantic","element_id","selector"];function ZP(e){if(!e||typeof e!="object")return{error:"browser_act: input must be an object"};let t=e;if(typeof t.action!="string")return{error:'browser_act: "action" is required and must be a string'};if(!Hh.includes(t.action))return{error:`browser_act: "action" must be one of ${Hh.map(u=>`"${u}"`).join(", ")} (got ${JSON.stringify(t.action)})`};let n=t.action,r=t.target;if(!r||typeof r!="object")return{error:'browser_act: "target" is required and must be an object'};let o=r;if(typeof o.kind!="string"||!Kh.includes(o.kind))return{error:`browser_act: "target.kind" must be one of ${Kh.map(u=>`"${u}"`).join(", ")} (got ${JSON.stringify(o.kind)})`};let s,i=o.kind;if(i==="semantic"){if(typeof o.text!="string"||o.text.length===0)return{error:'browser_act: target.kind=semantic requires "target.text" (non-empty string)'};s={kind:"semantic",text:o.text,...typeof o.role=="string"&&o.role.length>0?{role:o.role}:{}}}else if(i==="element_id"){if(typeof o.element_id!="string"||o.element_id.length===0)return{error:'browser_act: target.kind=element_id requires "target.element_id" (non-empty string)'};s={kind:"element_id",elementId:o.element_id}}else{if(typeof o.selector!="string"||o.selector.length===0)return{error:'browser_act: target.kind=selector requires "target.selector" (non-empty string)'};s={kind:"selector",selector:o.selector}}let a;if(t.value!==void 0){if(typeof t.value!="string")return{error:'browser_act: "value" must be a string when provided'};a=t.value}let l;if(t.timeout_ms!==void 0){if(typeof t.timeout_ms!="number"||!Number.isFinite(t.timeout_ms)||t.timeout_ms<=0)return{error:'browser_act: "timeout_ms" must be a positive finite number'};l=t.timeout_ms}let c;if(t.screenshot!==void 0){if(typeof t.screenshot!="boolean")return{error:'browser_act: "screenshot" must be a boolean'};c=t.screenshot}return{action:n,target:s,value:a,timeoutMs:l,screenshot:c}}function QP(e,t){let n=e.role?`"${e.text}" (role: ${e.role})`:`"${e.text}"`;return[`browser_act: ambiguous target \u2014 ${t.length} elements matched ${n}.`,'Retry with target.kind="element_id" using one of the following:',...t.map(o=>` id=${o.id} role=${o.role} label=${o.label}`)].join(`
1059
1059
  `)}function e0(e){return e.kind==="semantic"?`semantic:${Th(e.text)}`:e.kind==="element_id"?`element_id:${e.elementId}`:`selector:${vh(e.selector)}`}function t0(e={}){return async(t,n)=>{if(n.aborted){let a=n.reason;return{content:`browser_act aborted: ${a instanceof Error?a.message:String(a??"aborted")}`,isError:!0}}let r=ZP(t);if("error"in r)return{content:r.error,isError:!0};let o=e0(r.target),s=T.AFK_SESSION_ID??"default";if(!/^[a-zA-Z0-9_-]+$/.test(s))return{content:`Invalid AFK_SESSION_ID: must match /^[a-zA-Z0-9_-]+$/, got: ${JSON.stringify(s)}`,isError:!0};let i;try{if(e.getBrowserProvider)i=await e.getBrowserProvider();else{let{getBrowserProvider:a}=await Promise.resolve().then(()=>(Xr(),Yr));i=await a()}}catch(a){let l=a instanceof Error?a.message:String(a);return XP.some(c=>l.includes(c))?{content:"browser tools require the optional `playwright` peer dependency. Install via: pnpm add playwright. Or pick a different tool.",isError:!0}:{content:`browser_act failed to get provider: ${l}`,isError:!0}}try{let a=await i.act({sessionId:s,action:r.action,target:r.target,value:r.value,timeoutMs:r.timeoutMs,screenshot:r.screenshot});if("outcome"in a){if(a.outcome==="ambiguous_target")return{content:QP(a.query,a.candidates),isError:!0};if(a.outcome==="blocked_by_policy")return{content:`browser_act blocked: ${a.reason}`,isError:!0}}return{content:JSON.stringify(a,null,2)}}catch(a){return{content:`browser_act failed: ${a instanceof Error?a.message:String(a)}`,isError:!0}}}}var Gh=t0();G();var n0=["Cannot find package","ERR_MODULE_NOT_FOUND"],qh=["semantic","element_id","selector"];function r0(e){if(typeof e.kind!="string"||!qh.includes(e.kind))return{error:`browser_screenshot: "target.kind" must be one of ${qh.map(n=>`"${n}"`).join(", ")} (got ${JSON.stringify(e.kind)})`};let t=e.kind;return t==="semantic"?typeof e.text!="string"||e.text.length===0?{error:'browser_screenshot: target.kind=semantic requires "target.text" (non-empty string)'}:{kind:"semantic",text:e.text,...typeof e.role=="string"&&e.role.length>0?{role:e.role}:{}}:t==="element_id"?typeof e.element_id!="string"||e.element_id.length===0?{error:'browser_screenshot: target.kind=element_id requires "target.element_id" (non-empty string)'}:{kind:"element_id",elementId:e.element_id}:typeof e.selector!="string"||e.selector.length===0?{error:'browser_screenshot: target.kind=selector requires "target.selector" (non-empty string)'}:{kind:"selector",selector:e.selector}}function o0(e){if(e!=null&&typeof e!="object")return{error:"browser_screenshot: input must be an object or omitted"};if(!e)return{};let t=e,n;if(t.target!==void 0){if(!t.target||typeof t.target!="object")return{error:'browser_screenshot: "target" must be an object when provided'};let o=r0(t.target);if("error"in o)return o;n=o}let r;if(t.full_page!==void 0){if(typeof t.full_page!="boolean")return{error:'browser_screenshot: "full_page" must be a boolean'};r=t.full_page}return{target:n,fullPage:r}}function s0(e={}){return async(t,n)=>{if(n.aborted){let i=n.reason;return{content:`browser_screenshot aborted: ${i instanceof Error?i.message:String(i??"aborted")}`,isError:!0}}let r=o0(t);if("error"in r)return{content:r.error,isError:!0};let o=T.AFK_SESSION_ID??"default";if(!/^[a-zA-Z0-9_-]+$/.test(o))return{content:`Invalid AFK_SESSION_ID: must match /^[a-zA-Z0-9_-]+$/, got: ${JSON.stringify(o)}`,isError:!0};let s;try{if(e.getBrowserProvider)s=await e.getBrowserProvider();else{let{getBrowserProvider:i}=await Promise.resolve().then(()=>(Xr(),Yr));s=await i()}}catch(i){let a=i instanceof Error?i.message:String(i);return n0.some(l=>a.includes(l))?{content:"browser tools require the optional `playwright` peer dependency. Install via: pnpm add playwright. Or pick a different tool.",isError:!0}:{content:`browser_screenshot failed to get provider: ${a}`,isError:!0}}try{let i=await s.screenshot({sessionId:o,target:r.target,fullPage:r.fullPage});return{content:JSON.stringify(i,null,2)}}catch(i){return{content:`browser_screenshot failed: ${i instanceof Error?i.message:String(i)}`,isError:!0}}}}var zh=s0();G();var i0=["Cannot find package","ERR_MODULE_NOT_FOUND"];function a0(e={}){return async(t,n)=>{if(n.aborted){let s=n.reason;return{content:`browser_close aborted: ${s instanceof Error?s.message:String(s??"aborted")}`,isError:!0}}let r=T.AFK_SESSION_ID??"default";if(!/^[a-zA-Z0-9_-]+$/.test(r))return{content:`Invalid AFK_SESSION_ID: must match /^[a-zA-Z0-9_-]+$/, got: ${JSON.stringify(r)}`,isError:!0};let o;try{if(e.getBrowserProvider)o=await e.getBrowserProvider();else{let{getBrowserProvider:s}=await Promise.resolve().then(()=>(Xr(),Yr));o=await s()}}catch(s){let i=s instanceof Error?s.message:String(s);return i0.some(a=>i.includes(a))?{content:"browser tools require the optional `playwright` peer dependency. Install via: pnpm add playwright. Or pick a different tool.",isError:!0}:{content:`browser_close failed to get provider: ${i}`,isError:!0}}try{return await o.close({sessionId:r}),{content:"Browser session closed."}}catch(s){return{content:`browser_close failed: ${s instanceof Error?s.message:String(s)}`,isError:!0}}}}var Jh=a0();function ma(e,t){let n=e!==void 0?ta(e,t):t!==void 0?ta("default",t):Kg,r=t!==void 0?Nu(t):Zg,o=t!==void 0?Bu(t):Qg,s=zu();return new Map([["bash",n],["read_file",Gg],["write_file",Jg],["edit_file",Vg],["glob",r],["grep",o],["list_directory",eh],["send_telegram",nh],["web_scrape",oh],["create_schedule",ch],["list_schedules",uh],["get_schedule_history",dh],["cancel_schedule",ph],["terminal_font_size",s],["ask_question",hh],["browser_open",jh],["browser_observe",Wh],["browser_act",Gh],["browser_screenshot",zh],["browser_close",Jh]])}W();import l0 from"better-sqlite3";import{existsSync as vs,mkdirSync as Vh,readFileSync as fa,writeFileSync as Yh,readdirSync as c0,appendFileSync as u0,unlinkSync as Xh,copyFileSync as d0}from"fs";import{join as bt,basename as Zh,resolve as ga,relative as p0}from"path";var Qh="HOT.md",m0="HOT.md.bak",ey="memory.db",ty="memory-wal.jsonl",ha="procedures",f0=5250,Ts=2,g0=`
1060
1060
  CREATE TABLE IF NOT EXISTS sessions (
1061
1061
  session_id TEXT PRIMARY KEY,
@@ -1167,13 +1167,13 @@ ${t.replace(/<\/?cross-session-memory\b[^>]*>/gi,"")}
1167
1167
 
1168
1168
  ${o}`};if(o&&typeof o=="object"&&"type"in o&&o.type==="preset"){let s=o.append??"";return{...e,systemPrompt:{...o,append:`${r}
1169
1169
 
1170
- ${s}`}}}return{...e,systemPrompt:r}}function sd(e,t="cli"){return n=>{if(n.event!=="SessionEnd")return{};if(n.parentSessionId)return{};try{let r=n.sessionId;r&&(e.startSession({session_id:r,surface:t}),e.endSession(r,n.reason??"session ended","completed"))}catch{}return{}}}import O0 from"openai";import{randomUUID as $0}from"node:crypto";function xs(e,t){let n=(d,m)=>{if(!(d==null&&m==null))return(d??0)+(m??0)},r=(d,m)=>m!==void 0?m:d,o={stopReason:t.stopReason??e.stopReason??null},s=n(e.inputTokens,t.inputTokens);s!==void 0&&(o.inputTokens=s);let i=n(e.outputTokens,t.outputTokens);i!==void 0&&(o.outputTokens=i);let a=r(e.cachedInputTokens,t.cachedInputTokens);a!==void 0&&(o.cachedInputTokens=a);let l=r(e.cacheCreationTokens,t.cacheCreationTokens);l!==void 0&&(o.cacheCreationTokens=l);let c=n(e.totalTokens,t.totalTokens);c!==void 0&&(o.totalTokens=c);let u=n(e.totalCostUsd,t.totalCostUsd);return u!==void 0&&(o.totalCostUsd=u),o}var ay={opus:128e3,opus_1m:128e3,sonnet:64e3,sonnet_1m:64e3,haiku:64e3,"claude-opus-4-8":128e3,"claude-sonnet-4-6":64e3,"claude-haiku-4-5-20251001":64e3},T0=64e3;function id(e){return ay[e]??T0}var Es={opus:2e5,opus_1m:1e6,sonnet:2e5,sonnet_1m:1e6,haiku:2e5,"gpt-4o":128e3,"gpt-4o-mini":128e3,"gpt-4.1":1e6,"gpt-4.1-mini":1e6,o1:2e5,"o1-mini":128e3,o3:2e5,"o3-mini":2e5,"o4-mini":2e5,"mlx-community/qwen3-30b-a3b-4bit":128e3,"mlx-community/qwen3-32b-4bit":128e3,"mlx-community/qwen2.5-coder-32b-instruct-4bit":131072},x0=2e5,E0=262144;function R0(e){if(!e)return!1;let t=e.trim().toLowerCase();return t?!!(t.includes("/")||t.startsWith("gpt-")||t.startsWith("gpt_")||t.startsWith("o1")||t.startsWith("o3")||t.startsWith("o4")||t.startsWith("codex-")||t.startsWith("codex_")||t==="codex"):!1}function it(e){let t=Es[e]??Es[e.toLowerCase()];return t!==void 0?t:R0(e)?E0:x0}import{readFileSync as A0}from"node:fs";import{homedir as _0}from"node:os";import{join as C0}from"node:path";function I0(e){try{return A0(e,"utf-8")}catch{return null}}function Rs(e,t={}){let n=t.readEnv??(l=>process.env[l]),r=(t.homedir??_0)(),o=t.readFile??I0;if(e&&e.length>0)return{apiKey:e,source:"config",last4:ad(e)};let s=n("OPENAI_API_KEY");if(s&&s.length>0)return{apiKey:s,source:"env",last4:ad(s)};let i=C0(r,".codex","auth.json"),a=o(i);if(a!==null){let l=P0(a);if(l.kind==="apikey")return{apiKey:l.apiKey,source:"codex-cli",last4:ad(l.apiKey)};if(l.kind==="chatgpt")return{apiKey:null,source:"no-usable-auth-codex-oauth"}}return{apiKey:null,source:"no-usable-auth"}}function P0(e){let t;try{t=JSON.parse(e)}catch{return{kind:"invalid"}}if(typeof t!="object"||t===null)return{kind:"invalid"};let n=t,r=n.OPENAI_API_KEY;return typeof r=="string"&&r.length>0?{kind:"apikey",apiKey:r}:n.auth_mode==="chatgpt"?{kind:"chatgpt"}:{kind:"no-key"}}function ad(e){return e.length<=4?e:e.slice(-4)}function As(e){switch(e.source){case"config":return`using explicit AFK config API key (\u2026${e.last4??"????"})`;case"env":return`using OPENAI_API_KEY env var (\u2026${e.last4??"????"})`;case"codex-cli":return`using Codex CLI API key from ~/.codex/auth.json (\u2026${e.last4??"????"})`;case"no-usable-auth-codex-oauth":return"AFK OpenAI provider currently requires API key auth. Found ChatGPT/OAuth credentials in ~/.codex/auth.json but no API key. Run `codex login --api-key` or set OPENAI_API_KEY.";default:return"No OpenAI auth found. Set OPENAI_API_KEY, pass an explicit apiKey in AFK config, or run `codex login --api-key`."}}function ly(e){return typeof e=="string"?e:e.map(t=>{if(typeof t=="object"&&t&&"type"in t){if(t.type==="text")return t.text;if(t.type==="image")return"[image omitted]"}return""}).join(`
1170
+ ${s}`}}}return{...e,systemPrompt:r}}function sd(e,t="cli"){return n=>{if(n.event!=="SessionEnd")return{};if(n.parentSessionId)return{};try{let r=n.sessionId;r&&(e.startSession({session_id:r,surface:t}),e.endSession(r,n.reason??"session ended","completed"))}catch{}return{}}}import O0 from"openai";import{randomUUID as $0}from"node:crypto";function xs(e,t){let n=(d,p)=>{if(!(d==null&&p==null))return(d??0)+(p??0)},r=(d,p)=>p!==void 0?p:d,o={stopReason:t.stopReason??e.stopReason??null},s=n(e.inputTokens,t.inputTokens);s!==void 0&&(o.inputTokens=s);let i=n(e.outputTokens,t.outputTokens);i!==void 0&&(o.outputTokens=i);let a=r(e.cachedInputTokens,t.cachedInputTokens);a!==void 0&&(o.cachedInputTokens=a);let l=r(e.cacheCreationTokens,t.cacheCreationTokens);l!==void 0&&(o.cacheCreationTokens=l);let c=n(e.totalTokens,t.totalTokens);c!==void 0&&(o.totalTokens=c);let u=n(e.totalCostUsd,t.totalCostUsd);return u!==void 0&&(o.totalCostUsd=u),o}var ay={opus:128e3,opus_1m:128e3,sonnet:64e3,sonnet_1m:64e3,haiku:64e3,"claude-opus-4-8":128e3,"claude-sonnet-4-6":64e3,"claude-haiku-4-5-20251001":64e3},T0=64e3;function id(e){return ay[e]??T0}var Es={opus:2e5,opus_1m:1e6,sonnet:2e5,sonnet_1m:1e6,haiku:2e5,"gpt-4o":128e3,"gpt-4o-mini":128e3,"gpt-4.1":1e6,"gpt-4.1-mini":1e6,o1:2e5,"o1-mini":128e3,o3:2e5,"o3-mini":2e5,"o4-mini":2e5,"mlx-community/qwen3-30b-a3b-4bit":128e3,"mlx-community/qwen3-32b-4bit":128e3,"mlx-community/qwen2.5-coder-32b-instruct-4bit":131072},x0=2e5,E0=262144;function R0(e){if(!e)return!1;let t=e.trim().toLowerCase();return t?!!(t.includes("/")||t.startsWith("gpt-")||t.startsWith("gpt_")||t.startsWith("o1")||t.startsWith("o3")||t.startsWith("o4")||t.startsWith("codex-")||t.startsWith("codex_")||t==="codex"):!1}function it(e){let t=Es[e]??Es[e.toLowerCase()];return t!==void 0?t:R0(e)?E0:x0}import{readFileSync as A0}from"node:fs";import{homedir as _0}from"node:os";import{join as C0}from"node:path";function I0(e){try{return A0(e,"utf-8")}catch{return null}}function Rs(e,t={}){let n=t.readEnv??(l=>process.env[l]),r=(t.homedir??_0)(),o=t.readFile??I0;if(e&&e.length>0)return{apiKey:e,source:"config",last4:ad(e)};let s=n("OPENAI_API_KEY");if(s&&s.length>0)return{apiKey:s,source:"env",last4:ad(s)};let i=C0(r,".codex","auth.json"),a=o(i);if(a!==null){let l=P0(a);if(l.kind==="apikey")return{apiKey:l.apiKey,source:"codex-cli",last4:ad(l.apiKey)};if(l.kind==="chatgpt")return{apiKey:null,source:"no-usable-auth-codex-oauth"}}return{apiKey:null,source:"no-usable-auth"}}function P0(e){let t;try{t=JSON.parse(e)}catch{return{kind:"invalid"}}if(typeof t!="object"||t===null)return{kind:"invalid"};let n=t,r=n.OPENAI_API_KEY;return typeof r=="string"&&r.length>0?{kind:"apikey",apiKey:r}:n.auth_mode==="chatgpt"?{kind:"chatgpt"}:{kind:"no-key"}}function ad(e){return e.length<=4?e:e.slice(-4)}function As(e){switch(e.source){case"config":return`using explicit AFK config API key (\u2026${e.last4??"????"})`;case"env":return`using OPENAI_API_KEY env var (\u2026${e.last4??"????"})`;case"codex-cli":return`using Codex CLI API key from ~/.codex/auth.json (\u2026${e.last4??"????"})`;case"no-usable-auth-codex-oauth":return"AFK OpenAI provider currently requires API key auth. Found ChatGPT/OAuth credentials in ~/.codex/auth.json but no API key. Run `codex login --api-key` or set OPENAI_API_KEY.";default:return"No OpenAI auth found. Set OPENAI_API_KEY, pass an explicit apiKey in AFK config, or run `codex login --api-key`."}}function ly(e){return typeof e=="string"?e:e.map(t=>{if(typeof t=="object"&&t&&"type"in t){if(t.type==="text")return t.text;if(t.type==="image")return"[image omitted]"}return""}).join(`
1171
1171
  `)}function M0(e){let t=e.systemPrompt;if(t!==void 0){if(typeof t=="string")return t.length>0?t:void 0;if(typeof t=="object"&&t!==null&&"append"in t){let n=t.append;return n&&n.length>0?n:void 0}}}function cy(e){let t=[],n=M0(e.config);if(n!==void 0&&t.push({role:"system",content:n}),e.resumeHistory)for(let r of e.resumeHistory)r.user&&t.push({role:"user",content:r.user}),r.assistant&&t.push({role:"assistant",content:r.assistant});if(e.priorTurns)for(let r of e.priorTurns)t.push(r);return e.currentUserText!==void 0&&t.push({role:"user",content:e.currentUserText}),t}function uy(){return{assistantText:"",reasoningText:"",toolCallsByIndex:new Map,finishReason:null,usage:null,model:null,id:null}}function*dy(e,t,n){e.id&&!t.id&&(t.id=e.id),e.model&&!t.model&&(t.model=e.model),e.usage&&(t.usage=e.usage);let r=e.choices?.[0];if(!r)return;r.finish_reason&&(t.finishReason=r.finish_reason);let o=r.delta;if(!o)return;let s=o.reasoning_content??o.reasoning;if(typeof s=="string"&&s.length>0&&(t.reasoningText+=s,yield{type:"delta.reasoning",text:s,sessionId:n}),typeof o.content=="string"&&o.content.length>0&&(t.assistantText+=o.content,yield{type:"delta.text",text:o.content,sessionId:n}),o.tool_calls&&o.tool_calls.length>0)for(let i of o.tool_calls){let a=t.toolCallsByIndex.get(i.index)??{index:i.index,id:"",name:"",argumentsRaw:"",startEmitted:!1};i.id&&(a.id=i.id),i.function?.name&&(a.name=i.function.name),i.function?.arguments&&(a.argumentsRaw+=i.function.arguments),t.toolCallsByIndex.set(i.index,a)}}function py(e){let t=e.usage;if(!t)return{stopReason:e.finishReason??null,resultSubtype:"success",isError:!1};let n=t.prompt_tokens_details?.cached_tokens??0,r=t.prompt_tokens??0,o=t.completion_tokens??0;return{inputTokens:r,outputTokens:o,cachedInputTokens:n,totalTokens:t.total_tokens??r+o,stopReason:e.finishReason??null,resultSubtype:"success",isError:!1,raw:{...t}}}function ld(e){return[...e.toolCallsByIndex.values()].sort((t,n)=>t.index-n.index)}function my(e){return e.finishReason==="tool_calls"||e.finishReason==="function_call"?!0:e.toolCallsByIndex.size>0}function fy(e){return e.map(t=>{let n={name:t.name,parameters:t.input_schema};return t.description!==void 0&&(n.description=t.description),{type:"function",function:n}})}function gy(e,t){let n=[],r=new Map;for(let o of e){let s={};if(o.argumentsRaw.length>0)try{s=JSON.parse(o.argumentsRaw)}catch(i){let a=i instanceof Error?i.message:String(i);r.set(o.id,`Failed to parse tool arguments as JSON: ${a}`),s={}}n.push({id:o.id,name:o.name,input:s,signal:t})}return{calls:n,parseErrors:r}}function hy(e,t,n=""){let r={role:"assistant",content:e.length>0?e:null,tool_calls:t.map(o=>({id:o.id,type:"function",function:{name:o.name,arguments:o.argumentsRaw}}))};return n.length>0&&(r.reasoning_content=n),r}function yy(e){return e.map(({call:t,result:n})=>({role:"tool",tool_call_id:t.id,content:n.isError?`[error] ${n.content}`:n.content}))}function Zr(e){return(e.inputTokens??0)+(e.outputTokens??0)}function ya(e){return e?{totalTokens:Zr(e),apiUsage:{input_tokens:e.inputTokens??0,output_tokens:e.outputTokens??0,cache_read_input_tokens:e.cachedInputTokens??0,cache_creation_input_tokens:e.cacheCreationTokens??0}}:{totalTokens:0,apiUsage:null}}function by(e,t,n){return t<=0||e<=0||n<=0||n>=1?!1:e/t>=n}var cd="openai-compatible",D0=50,L0=null;function wy(e){return e??"default"}var ba=class{client;opts;initSessionId;toolDispatcher;openAITools;priorTurns=[];currentModel;currentPermissionMode;abortController=null;pendingAbortReason=null;closed=!1;closeResolve=null;closedPromise;lastUsage=null;constructor(t){if(this.opts=t,this.initSessionId=t.synthesizedSessionId,this.currentModel=t.model,this.currentPermissionMode=wy(t.config.permissionMode),this.toolDispatcher=t.toolDispatcher,this.toolDispatcher){let n=this.toolDispatcher;Array.isArray(n.toolDefs)&&n.toolDefs.length>0&&(this.openAITools=fy(n.toolDefs))}if(t.auth.apiKey===null)this.client=null;else{let n=L0??F0,r={apiKey:t.auth.apiKey};t.baseURL!==void 0&&(r.baseURL=t.baseURL),this.client=n(r)}this.closedPromise=new Promise(n=>{this.closeResolve=()=>n("__closed__")})}async*[Symbol.asyncIterator](){if(yield{type:"session.init",info:{sessionId:this.initSessionId,model:this.currentModel,permissionMode:this.currentPermissionMode,cwd:process.cwd(),tools:this.openAITools?this.openAITools.map(r=>r.function.name):[],slashCommands:[],skills:[],plugins:[],mcpServers:this.opts.mcpManager?.getServerStates().map(r=>({name:r.serverName,status:r.status}))??[],apiKeySource:this.opts.auth.source,version:cd}},this.opts.auth.apiKey===null){yield{type:"error",error:new Error(As(this.opts.auth))};return}let n=this.opts.promptStream[Symbol.asyncIterator]();try{for(;!this.closed;){let r=await Promise.race([n.next(),this.closedPromise]);if(r==="__closed__")break;let o=r;if(o.done)break;let s=ly(o.value.content);yield*this.runTurn(s)}}catch(r){yield{type:"error",error:r instanceof Error?r:new Error(String(r))}}finally{try{await n.return?.()}catch{}}}async*runTurn(t){let n=new AbortController;if(this.abortController=n,this.pendingAbortReason!==null&&!n.signal.aborted&&(n.abort(this.pendingAbortReason),this.pendingAbortReason=null),n.signal.aborted)return;let r=Date.now(),o=$0();this.priorTurns.push({role:"user",content:t});let s={stopReason:null,resultSubtype:"success",isError:!1},i="",a="";for(let l=0;l<D0;l++){if(n.signal.aborted){this.abortController===n&&(this.abortController=null);return}let c=yield*this.runIteration(n);if(c===null){this.abortController===n&&(this.abortController=null);return}if(s=xs(s,py(c.state)),this.lastUsage=s,c.text.length>0&&(i=c.text),a=c.state.reasoningText,!c.needsToolDispatch)break;yield*this.dispatchAndAppend(c.state,n.signal);{let u=ld(c.state).at(-1)?.name;yield{type:"progress",progress:{taskId:o,description:"Tool-use loop",summary:`Iteration ${l+1}: used ${u??"unknown"}`,lastToolName:u,totalTokens:s.totalTokens??0,toolUses:l+1,durationMs:Date.now()-r},sessionId:this.initSessionId}}if(n.signal.aborted){this.abortController===n&&(this.abortController=null);return}}if(this.abortController===n&&(this.abortController=null),i.length>0){let l={role:"assistant",content:i};a.length>0&&(l.reasoning_content=a),this.priorTurns.push(l)}this.lastUsage=s,yield{type:"assistant.message",text:i,sessionId:this.initSessionId},yield{type:"turn.completed",usage:{...s,durationMs:Date.now()-r},sessionId:this.initSessionId}}async*runIteration(t){let n=cy({config:this.opts.config,...this.opts.config.resumeHistory!==void 0?{resumeHistory:this.opts.config.resumeHistory}:{},priorTurns:this.priorTurns});this.currentPermissionMode==="plan"&&n[0]?.role==="system"&&(n[0]={...n[0],content:n[0].content+`
1172
1172
 
1173
1173
  `+Eu});let r=uy(),o={model:this.currentModel,messages:n,stream:!0,stream_options:{include_usage:!0}};this.openAITools&&this.openAITools.length>0&&(o.tools=this.openAITools);let s;try{s=await this.client.chat.completions.create(o,{signal:t.signal})}catch(i){return t.signal.aborted||(yield{type:"error",error:i instanceof Error?i:new Error(String(i))}),null}try{for await(let i of s){if(this.closed)return null;for(let a of dy(i,r,this.initSessionId))yield a}}catch(i){return t.signal.aborted||(yield{type:"error",error:i instanceof Error?i:new Error(String(i))}),null}return{state:r,events:[],text:r.assistantText,needsToolDispatch:my(r)&&r.toolCallsByIndex.size>0}}async*dispatchAndAppend(t,n){if(!this.toolDispatcher)return;let r=ld(t),{calls:o,parseErrors:s}=gy(r,n);for(let a of o)yield{type:"tool.use.start",toolUseId:a.id,toolName:a.name,toolInput:N0(a.input),sessionId:this.initSessionId};let i=[];if(n.aborted)for(let a of o){let l={content:"Tool call aborted",isError:!0};i.push({call:a,result:l}),yield{type:"tool.output",toolUseId:a.id,toolName:a.name,content:l.content,isError:!0,sessionId:this.initSessionId}}else{let a;try{if(this.toolDispatcher.executeBatch)a=await this.toolDispatcher.executeBatch(o);else{a=[];for(let l of o){if(n.aborted){a.push({content:"Tool call aborted",isError:!0});continue}try{a.push(await this.toolDispatcher.execute(l))}catch(c){let u=c instanceof Error?c.message:String(c);a.push({content:`Tool execution threw: ${u}`,isError:!0})}}}}catch(l){let c=l instanceof Error?l.message:String(l);a=o.map(()=>({content:`Tool batch execution failed: ${c}`,isError:!0}))}for(let l=0;l<o.length;l++){let c=o[l],u=a[l],d=s.get(c.id);d!==void 0&&(u={content:`${d}
1174
1174
  --
1175
1175
  ${u.content}`,isError:!0,...u.truncated===!0?{truncated:!0}:{}}),i.push({call:c,result:u}),yield{type:"tool.output",toolUseId:c.id,toolName:c.name,content:u.content,...u.isError===!0?{isError:!0}:{},...u.truncated===!0?{truncated:!0}:{},sessionId:this.initSessionId},u.render?.diff&&(yield{type:"tool.diff",toolUseId:c.id,diff:u.render.diff,sessionId:this.initSessionId})}}this.priorTurns.push(hy(t.assistantText,r,t.reasoningText));for(let a of yy(i))this.priorTurns.push(a)}async interrupt(){let t=this.abortController;if(t&&!t.signal.aborted){t.abort("interrupted");return}this.pendingAbortReason="interrupted"}async setModel(t){t!==void 0&&(this.currentModel=t)}async setPermissionMode(t){this.currentPermissionMode=wy(t)}setCwd(t){this.toolDispatcher?.setResolveBase?.(t)}async supportedCommands(){try{return an().map(n=>{let r={name:n.name,description:n.description};return n.argumentHint&&(r.argumentHint=n.argumentHint),r})}catch{return[]}}async supportedModels(){return[{value:"gpt-4o",displayName:"GPT-4o",description:"OpenAI flagship multimodal"},{value:"gpt-4o-mini",displayName:"GPT-4o mini",description:"Fast/cheap GPT-4o"},{value:"gpt-4.1",displayName:"GPT-4.1",description:"Long-context GPT-4"},{value:"gpt-4.1-mini",displayName:"GPT-4.1 mini",description:"Fast 4.1 variant"},{value:"o1",displayName:"o1",description:"Reasoning model"},{value:"o1-mini",displayName:"o1 mini",description:"Fast reasoning"},{value:"o3-mini",displayName:"o3 mini",description:"Newer reasoning, faster"}]}async supportedAgents(){return[]}async getContextUsage(){let t=this.lastUsage,n=it(this.currentModel),r;if(t&&n>0){let i=Zr(t);r=Math.min(100,Math.max(0,i/n*100))}let{totalTokens:o,apiUsage:s}=ya(t);return{tools:[],agents:[],isAutoCompactEnabled:!1,apiUsage:s,totalTokens:o,...r!==void 0?{percentage:r}:{},maxTokens:n}}async mcpServerStatus(){return this.opts.mcpManager?this.opts.mcpManager.getServerStates().map(t=>({name:t.serverName,status:t.status})):[]}async accountInfo(){return{authSource:this.opts.auth.source}}async rewindFiles(t,n){return{canRewind:!1,error:`${cd} provider does not support file checkpoint rewind yet.`}}close(){this.closed=!0;let t=this.abortController;t&&!t.signal.aborted?t.abort("closed"):this.pendingAbortReason="closed",this.closeResolve?.(),J(`\u{1F7E2} ${cd}: closed`)}};function F0(e){let t={apiKey:e.apiKey};return e.baseURL!==void 0&&(t.baseURL=e.baseURL),new O0(t)}function N0(e){if(!e||typeof e!="object")return"";let t=e,n=t.file_path??t.path??t.filePath;if(typeof n=="string")return" "+n;let r=t.command??t.cmd;if(typeof r=="string"){let s=r.split(`
1176
- `)[0];return" "+(s.length>80?s.slice(0,77)+"\u2026":s)}let o=t.query??t.pattern??t.url??t.description;return typeof o=="string"?" "+o:""}function Sy(e,t,n={}){let r=Rs(e.apiKey),o=e.resume??`openai-pending-${Date.now()}-${Math.random().toString(36).slice(2,8)}`,s=typeof e.model=="string"?e.model:"gpt-4o-mini",i={auth:r,model:s,synthesizedSessionId:o,promptStream:t,config:e};return n.baseURL!==void 0&&(i.baseURL=n.baseURL),n.toolDispatcher!==void 0&&(i.toolDispatcher=n.toolDispatcher),n.mcpManager!==void 0&&(i.mcpManager=n.mcpManager),new ba(i)}var dd="openai-compatible",Rt=class{name=dd;providerOpts;memoryStore;schemas;_sharedReadRoots;_sharedWriteRoots;_initialResolveBase;_presenceSessionId=null;constructor(t={}){this.providerOpts=t,this.memoryStore=t.memoryStore??new We;let n=[...en];t.subagentExecutor&&n.push(nr),t.skillExecutor&&n.push(rr),t.composeExecutor&&n.push(or),n.push(...Cn),n.push(Nt),this.schemas=n}query(t){let n=t.config,r=n.permissionMode??"default";this.ensureSharedRoots(n.cwd),n.readRoots&&this._sharedReadRoots&&this._sharedReadRoots.length<=1&&(this._sharedReadRoots.length=0,this._sharedReadRoots.push(...n.readRoots)),n.writeRoots&&this._sharedWriteRoots&&this._sharedWriteRoots.length<=1&&(this._sharedWriteRoots.length=0,this._sharedWriteRoots.push(...n.writeRoots));let o,s=typeof n.model=="string"?n.model:String(n.model),i=hs({surface:this.providerOpts.surface??"cli",cwd:n.cwd??process.cwd(),modelName:s,providerName:dd,permissionMode:r,...n.sessionId!==void 0?{sessionId:n.sessionId}:{},...n.parentSessionId!==void 0?{parentSessionId:n.parentSessionId}:{},...n.depth!==void 0?{depth:n.depth}:{},...n.maxDepth!==void 0?{maxDepth:n.maxDepth}:{},...n.phaseRole!==void 0?{phaseRole:n.phaseRole}:{},getEnabledToolNames:()=>o instanceof rn?o.toolDefs.map(m=>m.name):[],getMcpTools:()=>this.providerOpts.mcpManager?.getMcpTools()??[],getSubagents:()=>this.providerOpts.subagentExecutor?this.providerOpts.subagentExecutor.getSubagentsLite():{active:[],backgroundJobs:[]}});o=this.providerOpts.tools?ys(this.providerOpts.tools,i):this.buildDispatcher(r,{...n.cwd!==void 0?{cwd:n.cwd}:{},...this._sharedReadRoots!==void 0?{readRoots:this._sharedReadRoots}:{},...this._sharedWriteRoots!==void 0?{writeRoots:this._sharedWriteRoots}:{},...n.sessionId!==void 0?{sessionId:n.sessionId}:{},...n.parentSessionId!==void 0?{parentSessionId:n.parentSessionId}:{},...n.traceWriter!==void 0?{traceWriter:n.traceWriter}:{},runtimeStateSource:i,...n.isSkillDispatch?{isSkillDispatch:!0}:{}});let a={};if(this.providerOpts.baseURL!==void 0&&(a.baseURL=this.providerOpts.baseURL),a.toolDispatcher=o,this.providerOpts.mcpManager!==void 0&&(a.mcpManager=this.providerOpts.mcpManager),(n.depth===void 0||n.depth===0)&&n.parentSessionId===void 0&&n.sessionId!==void 0&&this._presenceSessionId===null){this._presenceSessionId=n.sessionId;let m=n.sessionId,f=i.getWorkspace();bs({sessionId:m,surface:this.providerOpts.surface??"cli",cwd:n.cwd??process.cwd(),startedAt:new Date().toISOString(),model:{provider:dd,name:s},workspace:f,pid:process.pid}),process.once("exit",()=>{nn(m)}),process.once("SIGINT",()=>{nn(m),process.exit(130)}),process.once("SIGTERM",()=>{nn(m),process.exit(143)})}let c=zr({cwd:n.cwd??process.cwd(),...n.sessionId!==void 0?{sessionId:n.sessionId}:{},surface:this.providerOpts.surface??"cli",...n.depth!==void 0?{depth:n.depth}:{},...n.maxDepth!==void 0?{maxDepth:n.maxDepth}:{},workspace:i.getWorkspace()}),u=typeof n.systemPrompt=="string"?n.systemPrompt:void 0,d={...n,systemPrompt:u!==void 0?`${u}
1176
+ `)[0];return" "+(s.length>80?s.slice(0,77)+"\u2026":s)}let o=t.query??t.pattern??t.url??t.description;return typeof o=="string"?" "+o:""}function Sy(e,t,n={}){let r=Rs(e.apiKey),o=e.resume??`openai-pending-${Date.now()}-${Math.random().toString(36).slice(2,8)}`,s=typeof e.model=="string"?e.model:"gpt-4o-mini",i={auth:r,model:s,synthesizedSessionId:o,promptStream:t,config:e};return n.baseURL!==void 0&&(i.baseURL=n.baseURL),n.toolDispatcher!==void 0&&(i.toolDispatcher=n.toolDispatcher),n.mcpManager!==void 0&&(i.mcpManager=n.mcpManager),new ba(i)}var dd="openai-compatible",Rt=class{name=dd;providerOpts;memoryStore;schemas;_sharedReadRoots;_sharedWriteRoots;_initialResolveBase;_presenceSessionId=null;constructor(t={}){this.providerOpts=t,this.memoryStore=t.memoryStore??new We;let n=[...en];t.subagentExecutor&&n.push(nr),t.skillExecutor&&n.push(rr),t.composeExecutor&&n.push(or),n.push(...Cn),n.push(Nt),this.schemas=n}query(t){let n=t.config,r=n.permissionMode??"default";this.ensureSharedRoots(n.cwd),n.readRoots&&this._sharedReadRoots&&this._sharedReadRoots.length<=1&&(this._sharedReadRoots.length=0,this._sharedReadRoots.push(...n.readRoots)),n.writeRoots&&this._sharedWriteRoots&&this._sharedWriteRoots.length<=1&&(this._sharedWriteRoots.length=0,this._sharedWriteRoots.push(...n.writeRoots));let o,s=typeof n.model=="string"?n.model:String(n.model),i=hs({surface:this.providerOpts.surface??"cli",cwd:n.cwd??process.cwd(),modelName:s,providerName:dd,permissionMode:r,...n.sessionId!==void 0?{sessionId:n.sessionId}:{},...n.parentSessionId!==void 0?{parentSessionId:n.parentSessionId}:{},...n.depth!==void 0?{depth:n.depth}:{},...n.maxDepth!==void 0?{maxDepth:n.maxDepth}:{},...n.phaseRole!==void 0?{phaseRole:n.phaseRole}:{},getEnabledToolNames:()=>o instanceof rn?o.toolDefs.map(p=>p.name):[],getMcpTools:()=>this.providerOpts.mcpManager?.getMcpTools()??[],getSubagents:()=>this.providerOpts.subagentExecutor?this.providerOpts.subagentExecutor.getSubagentsLite():{active:[],backgroundJobs:[]}});o=this.providerOpts.tools?ys(this.providerOpts.tools,i):this.buildDispatcher(r,{...n.cwd!==void 0?{cwd:n.cwd}:{},...this._sharedReadRoots!==void 0?{readRoots:this._sharedReadRoots}:{},...this._sharedWriteRoots!==void 0?{writeRoots:this._sharedWriteRoots}:{},...n.sessionId!==void 0?{sessionId:n.sessionId}:{},...n.parentSessionId!==void 0?{parentSessionId:n.parentSessionId}:{},...n.traceWriter!==void 0?{traceWriter:n.traceWriter}:{},runtimeStateSource:i,...n.isSkillDispatch?{isSkillDispatch:!0}:{}});let a={};if(this.providerOpts.baseURL!==void 0&&(a.baseURL=this.providerOpts.baseURL),a.toolDispatcher=o,this.providerOpts.mcpManager!==void 0&&(a.mcpManager=this.providerOpts.mcpManager),(n.depth===void 0||n.depth===0)&&n.parentSessionId===void 0&&n.sessionId!==void 0&&this._presenceSessionId===null){this._presenceSessionId=n.sessionId;let p=n.sessionId,f=i.getWorkspace();bs({sessionId:p,surface:this.providerOpts.surface??"cli",cwd:n.cwd??process.cwd(),startedAt:new Date().toISOString(),model:{provider:dd,name:s},workspace:f,pid:process.pid}),process.once("exit",()=>{nn(p)}),process.once("SIGINT",()=>{nn(p),process.exit(130)}),process.once("SIGTERM",()=>{nn(p),process.exit(143)})}let c=zr({cwd:n.cwd??process.cwd(),...n.sessionId!==void 0?{sessionId:n.sessionId}:{},surface:this.providerOpts.surface??"cli",...n.depth!==void 0?{depth:n.depth}:{},...n.maxDepth!==void 0?{maxDepth:n.maxDepth}:{},workspace:i.getWorkspace()}),u=typeof n.systemPrompt=="string"?n.systemPrompt:void 0,d={...n,systemPrompt:u!==void 0?`${u}
1177
1177
 
1178
1178
  ${c}`:c};return Sy(d,t.prompt,a)}buildDispatcher(t,n){let r=ma(t,n.cwd),o=gs(this.memoryStore,void 0,this.providerOpts.surface??"cli");for(let[l,c]of o)r.set(l,c);n.runtimeStateSource&&r.set("get_runtime_state",Jr(n.runtimeStateSource));let s=this.providerOpts.mcpManager?this.providerOpts.mcpManager.getMcpTools():[];if(this.providerOpts.mcpManager)for(let[l,c]of this.providerOpts.mcpManager.getMcpHandlers())r.set(l,c);let i=n.isSkillDispatch?this.schemas.filter(l=>l.name!=="ask_question"&&l.name!=="terminal_font_size"):this.schemas,a={handlers:r,schemas:[...i,...s]};return this.providerOpts.hookRegistry!==void 0&&(a.hookRegistry=this.providerOpts.hookRegistry),this.providerOpts.permissions!==void 0&&(a.permissions=this.providerOpts.permissions),this.providerOpts.subagentExecutor!==void 0&&(a.subagentExecutor=this.providerOpts.subagentExecutor),this.providerOpts.skillExecutor!==void 0&&(a.skillExecutor=this.providerOpts.skillExecutor),this.providerOpts.composeExecutor!==void 0&&(a.composeExecutor=this.providerOpts.composeExecutor),n.cwd!==void 0&&(a.cwd=n.cwd),n.readRoots!==void 0&&(a.readRoots=n.readRoots),n.writeRoots!==void 0&&(a.writeRoots=n.writeRoots),n.sessionId!==void 0&&(a.sessionId=n.sessionId),n.parentSessionId!==void 0&&(a.parentSessionId=n.parentSessionId),n.traceWriter!==void 0&&(a.traceWriter=n.traceWriter),new rn(a)}ensureSharedRoots(t){if(!this._sharedReadRoots){let n=t?[t]:[];this._sharedReadRoots=n.slice(),this._sharedWriteRoots=n.slice(),t&&!this._initialResolveBase&&(this._initialResolveBase=t)}}addReadRoot(t,n="slash",r){this.ensureSharedRoots();let o=ud.resolve(t);this._sharedReadRoots.includes(o)||this._sharedReadRoots.push(o),this.appendProviderAuditLog({action:"grant-read",path:o,source:n,sessionId:r})}addWriteRoot(t,n="slash",r){this.ensureSharedRoots();let o=ud.resolve(t);this._sharedReadRoots.includes(o)||this._sharedReadRoots.push(o),this._sharedWriteRoots.includes(o)||this._sharedWriteRoots.push(o),this.appendProviderAuditLog({action:"grant-write",path:o,source:n,sessionId:r})}revokeRoot(t,n="slash",r){if(!this._sharedReadRoots)return;let o=ud.resolve(t);if(this._initialResolveBase&&o===this._initialResolveBase)return;let s=this._sharedReadRoots.indexOf(o);if(s!==-1&&this._sharedReadRoots.splice(s,1),this._sharedWriteRoots){let i=this._sharedWriteRoots.indexOf(o);i!==-1&&this._sharedWriteRoots.splice(i,1)}this.appendProviderAuditLog({action:"revoke",path:o,source:n,sessionId:r})}getGrants(){return{resolveBase:this._initialResolveBase,readRoots:this._sharedReadRoots?.slice()??[],writeRoots:this._sharedWriteRoots?.slice()??[]}}appendProviderAuditLog(t){try{let n=Fr();U0(j0(n),{recursive:!0});let r=JSON.stringify({timestamp:new Date().toISOString(),sessionId:t.sessionId??null,action:t.action,path:t.path,source:t.source});B0(n,r+`
1179
1179
  `)}catch{}}close(){this.memoryStore.close()}},W0=new Rt;var H0=new Set(["Read","Glob","Grep","NotebookRead","LS","read_file","glob","grep","list_directory","memory_search"]),K0=new Set(["Write","Edit","NotebookEdit","MultiEdit","write_file","edit_file","memory_update","procedure_write","terminal_font_size"]),G0=new Set(["Bash","BashOutput","KillBash","bash"]),Qr=new Set(["Agent","Task","agent"]),pd=new Set(["Skill","skill"]),md=new Set(["Compose","compose"]),Je=new Set([...Qr,...md,...pd]),q0=new Set(["WebFetch","WebSearch","send_telegram","web_scrape"]),z0=new Set(["browser_open","browser_observe","browser_act","browser_screenshot","browser_extract","browser_close"]),J0=new Set(["TaskCreate","TaskUpdate","TaskList","TaskGet","TaskOutput","TaskStop","EnterPlanMode","ExitPlanMode","ToolSearch"]),V0=new Set(["create_schedule","list_schedules","get_schedule_history","cancel_schedule"]);function Pn(e,t){if(e.has(t))return!0;let n=t.charAt(0).toUpperCase()+t.slice(1);return n!==t&&e.has(n)}var ky=["Read","Glob","Grep","NotebookRead","LS","read_file","glob","grep","list_directory","memory_search",...st];function Mn(e){return e.startsWith("mcp__")||e.startsWith("MCP__")?"mcp":Pn(H0,e)?"read":Pn(K0,e)?"write":Pn(G0,e)?"shell":Pn(Qr,e)?"subagent":Pn(pd,e)?"skill":Pn(md,e)?"dag":Pn(q0,e)?"web":z0.has(e)?"browser":Pn(J0,e)?"planning":V0.has(e)?"schedule":"other"}var Y0={subagent:"subagent",skill:"skill",dag:"dag"};function fd(e){return Y0[e]}import{isAbsolute as nM}from"node:path";import{EventEmitter as Z0}from"node:events";import*as On from"node:fs/promises";import*as Ey from"node:path";import{createHash as Q0}from"node:crypto";W();import*as eo from"node:fs";import*as Le from"node:fs/promises";import*as vy from"node:readline";import{randomBytes as X0}from"node:crypto";var wa=class{jobId;logPath;metaPath;stream=null;errored=!1;closed=!1;streamReady=!1;pendingLines=[];readyPromise=null;readyResolve=null;metaWriteQueue=Promise.resolve();constructor(t){this.jobId=t,this.logPath=Ni(t),this.metaPath=uu(t);try{eo.mkdirSync(Zn(t),{recursive:!0})}catch(n){process.stderr.write(`[afk] bg-job-log: mkdir failed for ${t}: ${String(n)}
@@ -1186,10 +1186,10 @@ ${c}`:c};return Sy(d,t.prompt,a)}buildDispatcher(t,n){let r=ma(t,n.cwd),o=gs(thi
1186
1186
  `);try{await Le.unlink(n)}catch{}}}},jt=class e{static async listJobs(){let t=as(),n;try{n=await Le.readdir(t)}catch{return[]}let r=[];for(let o of n.slice(0,1e3)){let s=await e.readMeta(o);s&&r.push(s)}return r.sort((o,s)=>s.startedAt-o.startedAt),r.slice(0,100)}static async readMeta(t){let n;try{n=uu(t)}catch{return null}try{let r=await Le.readFile(n,"utf8"),o=JSON.parse(r);return o.schemaVersion!==1?null:o}catch(r){return r.code==="ENOENT"||process.stderr.write(`[afk] bg-job-log: readMeta parse error for ${t}: ${String(r)}
1187
1187
  `),null}}static async*readEvents(t){let n=Ni(t),r;try{r=await Le.open(n,"r")}catch(o){if(o.code==="ENOENT")return;throw o}try{let o=vy.createInterface({input:r.createReadStream({encoding:"utf8"}),crlfDelay:1/0});for await(let s of o){let i=s.trim();if(i)try{yield JSON.parse(i)}catch{}}}finally{await r.close()}}static async*tailEvents(t,n){let r=Ni(t),{fromStart:o=!1}=n??{},s=Zn(t);try{await Le.access(s)}catch{return}let i=0,a="",l=null,c=250;async function*u(){let h=null;try{h=await Le.open(r,"r");let b=await h.stat();if(b.size<=i)return;let y=b.size-i,w=Buffer.allocUnsafe(y),{bytesRead:x}=await h.read(w,0,y,i);if(x===0)return;i+=x,a+=w.toString("utf8",0,x);let R=a.split(`
1188
1188
  `);a=R.pop()??"";for(let v of R){let E=v.trim();if(E)try{yield JSON.parse(E)}catch{}}}catch(b){b.code!=="ENOENT"&&process.stderr.write(`[afk] bg-job-log: tailEvents read error for ${t}: ${String(b)}
1189
- `)}finally{h&&await h.close().catch(()=>{})}}async function d(){let h=await e.readMeta(t);if(!h||!(h.status!=="running"))return!1;try{let y=await Le.stat(r);return i>=y.size}catch{return!0}}if(o)yield*u();else try{i=(await Le.stat(r)).size}catch{}if(await d())return;let m=null,f=null,g=()=>new Promise(h=>{f=h;let b=setTimeout(()=>{f=null,h()},c);m=()=>{clearTimeout(b),f=null,h()}});try{l=eo.watch(s,{persistent:!1},()=>{m?.(),m=null})}catch{}try{for(;await g(),yield*u(),!await d(););}finally{f?.(),l?.close()}}};W();var eM=300*1e3,Ty=4096;function gd(e){Te(e).catch(()=>{})}var xy=5e3,tM=10,_s=class extends Error{constructor(t,n){super(`Background job cap reached (${t}/${n} running). Wait for existing jobs to finish or cancel them before spawning more.`),this.name="BackgroundJobCapError"}},Sa=class extends Z0{jobs=new Map;counter=Math.floor(Math.random()*65536);traceWriter;maxConcurrentJobs;constructor(t={}){super(),this.traceWriter=t.traceWriter,this.maxConcurrentJobs=t.maxConcurrentJobs??tM,setTimeout(()=>this._sweepOldJobs().catch(r=>process.stderr.write(`[afk] bg sweep error: ${String(r)}
1189
+ `)}finally{h&&await h.close().catch(()=>{})}}async function d(){let h=await e.readMeta(t);if(!h||!(h.status!=="running"))return!1;try{let y=await Le.stat(r);return i>=y.size}catch{return!0}}if(o)yield*u();else try{i=(await Le.stat(r)).size}catch{}if(await d())return;let p=null,f=null,g=()=>new Promise(h=>{f=h;let b=setTimeout(()=>{f=null,h()},c);p=()=>{clearTimeout(b),f=null,h()}});try{l=eo.watch(s,{persistent:!1},()=>{p?.(),p=null})}catch{}try{for(;await g(),yield*u(),!await d(););}finally{f?.(),l?.close()}}};W();var eM=300*1e3,Ty=4096;function gd(e){Te(e).catch(()=>{})}var xy=5e3,tM=10,_s=class extends Error{constructor(t,n){super(`Background job cap reached (${t}/${n} running). Wait for existing jobs to finish or cancel them before spawning more.`),this.name="BackgroundJobCapError"}},Sa=class extends Z0{jobs=new Map;counter=Math.floor(Math.random()*65536);traceWriter;maxConcurrentJobs;constructor(t={}){super(),this.traceWriter=t.traceWriter,this.maxConcurrentJobs=t.maxConcurrentJobs??tM,setTimeout(()=>this._sweepOldJobs().catch(r=>process.stderr.write(`[afk] bg sweep error: ${String(r)}
1190
1190
  `)),5e3).unref()}register(t){let n=[...this.jobs.values()].filter(d=>d.status==="running").length;if(n>=this.maxConcurrentJobs)throw new _s(n,this.maxConcurrentJobs);let r=this.nextJobId(),o=t.prompt.trim().slice(0,80),s=Date.now(),i,a=new Promise(d=>{i=d}),l={jobId:r,subagentId:t.handle.id,label:o,model:t.model,startedAt:s,status:"running",handle:t.handle,joiners:[],terminalSettled:a,settle:i,transcriptTail:"",parentSessionId:t.parentSessionId};this.jobs.set(r,l),Kr(this.traceWriter,{transition:"started",jobId:r,subagentId:t.handle.id,label:o,model:t.model}),this.emit("started",this.snapshot(l));let c=new wa(r),u={jobId:r,subagentId:t.handle.id,label:o,promptHash:Q0("sha256").update(t.prompt).digest("hex"),model:t.model,startedAt:s,status:"running",...t.parentSessionId!==void 0?{parentSessionId:t.parentSessionId}:{},schemaVersion:1};return c.writeMeta(u),t.handle.runInBackground(t.prompt,d=>{this.markTerminal(r,d,c,u)},d=>{c.write(d),d.type==="chunk"&&d.chunk.type==="content"&&this.appendTranscript(r,d.chunk.content)}),this.snapshot(l)}get(t){let n=this.jobs.get(t);return n?this.snapshot(n):void 0}list(){return[...this.jobs.values()].map(t=>this.snapshot(t))}async join(t){let n=this.jobs.get(t);if(!n)throw new Error(`Background job not found: "${t}". Completed and cancelled jobs are evicted from the registry ~5 minutes after they settle. If the jobId looks correct, the job may have already been evicted.`);let r=await n.terminalSettled;return Kr(this.traceWriter,{transition:"joined",jobId:t,subagentId:n.subagentId,jobStatus:n.status}),this.emit("joined",this.snapshot(n)),r}async cancelJob(t){let n=this.jobs.get(t);return!n||n.status!=="running"?!1:(n.cancelSource="explicit",await n.handle.cancel(),!0)}async cancelAll(){let t=[...this.jobs.values()].filter(n=>n.status==="running");for(let n of t)n.cancelSource="cascade";await Promise.allSettled(t.map(n=>n.handle.cancel())),await Promise.allSettled(t.map(n=>{let r=new Promise(o=>setTimeout(()=>{console.warn(`[BackgroundAgentRegistry] cancelAll: job ${n.jobId} did not settle within ${xy}ms \u2014 continuing teardown`),o()},xy).unref());return Promise.race([n.terminalSettled,r])}))}appendTranscript(t,n){let r=this.jobs.get(t);if(!r)return;let o=r.transcriptTail+n;o.length<=Ty?r.transcriptTail=o:r.transcriptTail=o.slice(o.length-Ty)}getTranscript(t){return this.jobs.get(t)?.transcriptTail}nextJobId(){return this.counter+=1,`bg-${Date.now().toString(36)}-${this.counter}`}markTerminal(t,n,r,o){let s=this.jobs.get(t);if(!s||s.status!=="running")return;s.result=n,s.endedAt=Date.now();let i=s.endedAt-s.startedAt;if(s.status=this.statusFromResult(n.status),s.status==="completed"){let l=n.message?.content,c=typeof l=="string"?l:l!==void 0?JSON.stringify(l):"";Kr(this.traceWriter,{transition:"completed",jobId:t,subagentId:s.subagentId,durationMs:i,outputBytes:Buffer.byteLength(c,"utf8")}),gd({event:"subagent.completed",subagent_id:s.subagentId,parent_session_id:s.parentSessionId,status:n.status,duration_ms:i,content_chars:c.length}),this.emit("settled",this.snapshot(s))}else if(s.status==="failed"){let l=n.error;Kr(this.traceWriter,{transition:"failed",jobId:t,subagentId:s.subagentId,durationMs:i,errorClass:l?.name??"Error",errorMessage:l?.message??"unknown"}),gd({event:"subagent.failed",subagent_id:s.subagentId,parent_session_id:s.parentSessionId,status:n.status,duration_ms:i,error_message:l?.message}),this.emit("settled",this.snapshot(s))}else Kr(this.traceWriter,{transition:"cancelled",jobId:t,subagentId:s.subagentId,source:s.cancelSource??"explicit"}),gd({event:"subagent.failed",subagent_id:s.subagentId,parent_session_id:s.parentSessionId,status:n.status,duration_ms:i}),this.emit("settled",this.snapshot(s));if(s.settle(n),r&&o){let l=s.status,c=s.endedAt;r.writeMeta({...o,status:l,...c!==void 0?{endedAt:c}:{}}).then(()=>r.close())}setTimeout(()=>{this.jobs.delete(t)},eM).unref()}async _sweepOldJobs(){let n=as(),r;try{r=await On.readdir(n)}catch{return}for(let o of r){let s=Zn(o),i=Ey.join(s,"meta.json");try{if(!(await On.lstat(s)).isDirectory()){process.stderr.write(`[afk] bg sweep: skipping non-directory entry ${o}
1191
1191
  `);continue}let l=await On.readFile(i,"utf8"),c=JSON.parse(l);if(c.status==="running"||c.endedAt===void 0||Date.now()-c.endedAt<6048e5)continue;await On.rm(s,{recursive:!0,force:!0})}catch(a){if(a.code==="ENOENT")continue;process.stderr.write(`[afk] bg sweep: error evicting ${o}: ${String(a)}
1192
- `)}}}statusFromResult(t){return t==="succeeded"?"completed":t==="failed"?"failed":t==="cancelled"?"cancelled":"failed"}snapshot(t){return{jobId:t.jobId,subagentId:t.subagentId,label:t.label,model:t.model,startedAt:t.startedAt,status:t.status,...t.result!==void 0?{result:t.result}:{},...t.endedAt!==void 0?{endedAt:t.endedAt}:{}}}};function rM(e){if(typeof e!="object"||e===null)throw new Error("Agent tool input must be an object");let t=e,n=t.prompt;if(typeof n!="string")throw new Error('Agent tool input must have a "prompt" field of type string');if(n.trim().length===0)throw new Error("Agent tool prompt cannot be empty");let r,o=t.model;if(o!==void 0){if(typeof o!="string")throw new Error("Agent tool model must be a string");r=o}let s=10,i=t.max_turns;if(i!==void 0){if(typeof i!="number")throw new Error("Agent tool max_turns must be a number");s=Math.max(1,Math.min(50,Math.floor(i)))}let a="agent-tool",l=t.id_prefix;if(l!==void 0){if(typeof l!="string")throw new Error("Agent tool id_prefix must be a string");a=l}let c="foreground",u=t.mode;if(u!==void 0){if(u!=="foreground"&&u!=="background")throw new Error(`Agent tool mode must be "foreground" or "background", got: ${JSON.stringify(u)}`);c=u}let d,m=t.cwd;if(m!==void 0){if(typeof m!="string")throw new Error(`Agent tool cwd must be a string, got: ${JSON.stringify(m)}`);if(m.length===0)throw new Error("Agent tool cwd must be a non-empty string");if(!nM(m))throw new Error(`Agent tool cwd must be an absolute path, got: ${JSON.stringify(m)}`);if(m.split(/[/\\]/).includes(".."))throw new Error(`Agent tool cwd must not contain '..' segments, got: ${JSON.stringify(m)}`);d=m}return{prompt:n,model:r,max_turns:s,id_prefix:a,mode:c,...d!==void 0?{cwd:d}:{}}}function ka(e){try{return Te(e).catch(()=>{})}catch{return Promise.resolve()}}function to(e,t=240){return e.length<=t?e:e.slice(0,t)+"\u2026"}function Ay(e){if(e!=null){if(typeof e=="string")return e.length;try{return JSON.stringify(e).length}catch{return}}}var oM=4096,Ry=1024;function sM(e){if(e==null)return;let t=Ay(e);return t!==void 0&&t>oM?{truncated:!0,chars:t}:e}function iM(e){let t={status:e.status,error:to(e.errorMessage,Ry),subagent_id:e.subagentId};e.schemaErrorMessage&&(t.schemaError=to(e.schemaErrorMessage,Ry));let n=sM(e.partialOutput);return n!==void 0&&(t.partialOutput=n),t}var Wt=class e{constructor(t){this.ctx=t}ctx;getSubagentsLite(){let t=this.ctx.subagentManager.list().map(r=>({id:r.id,status:r.status})),n=this.ctx.backgroundRegistry?this.ctx.backgroundRegistry.list().map(r=>({jobId:r.jobId,status:r.status,startedAt:new Date(r.startedAt).toISOString(),label:r.label.length>0?r.label:null})):[];return{active:t,backgroundJobs:n}}async execute(t){if(t.signal.aborted)return{content:"Agent tool call aborted",isError:!0};let n;try{n=rM(t.input)}catch(g){return{content:`Agent tool input validation failed: ${g instanceof Error?g.message:String(g)}`,isError:!0}}let r=this.ctx.depth,o=this.ctx.maxDepth??no,s,i=n.model??this.ctx.defaultSubagentModel??"sonnet",a=xe(i)==="openai-compatible",l={model:i,apiKey:a?void 0:this.ctx.defaultConfig.apiKey,systemPrompt:this.ctx.defaultConfig.systemPrompt,baseUrl:a?void 0:this.ctx.defaultConfig.baseUrl,maxTurns:n.max_turns,depth:r+1,maxDepth:o,...n.cwd!==void 0?{cwd:n.cwd}:{}},c;if(this.ctx.childProviderFactory&&r<o){s=new X({parentAbortSignal:t.signal,...this.ctx.cwd!==void 0?{cwd:this.ctx.cwd}:{}}),c=ar(t.signal);let g=new e({subagentManager:s,parentSession:c,defaultConfig:this.ctx.defaultConfig,defaultSubagentModel:this.ctx.defaultSubagentModel,childProviderFactory:this.ctx.childProviderFactory,childSkillExecutorFactory:this.ctx.childSkillExecutorFactory,depth:r+1,maxDepth:o,...this.ctx.cwd!==void 0?{cwd:this.ctx.cwd}:{}}),h=this.ctx.childSkillExecutorFactory?this.ctx.childSkillExecutorFactory(r+1,o,t.signal):void 0;l.provider=this.ctx.childProviderFactory({childExecutor:g,...h!==void 0?{childSkillExecutor:h}:{},...l.model!==void 0?{model:l.model}:{}})}let u;try{u=await this.ctx.subagentManager.forkSubagent({parent:this.ctx.parentSession,parentId:t.id,config:l,idPrefix:n.id_prefix,agentType:n.id_prefix&&n.id_prefix!=="agent-tool"?on(n.id_prefix).replace(/[\r\n]+/g," ").trim()||"agent":on(n.prompt).replace(/[\r\n]+/g," ").slice(0,40).trim()||"agent",denyElicitations:n.mode==="background"}),c!==void 0&&(c.sessionId=u.id)}catch(g){let h=g instanceof Error?g.message:String(g);return ka({event:"subagent.failed",subagent_id:"unknown",id_prefix:n.id_prefix,parent_session_id:this.ctx.parentSession.sessionId,status:"failed",error_message:to(h),depth:r}),{content:`Failed to fork subagent: ${h}`,isError:!0}}if(n.mode==="background"){let g=this.ctx.backgroundRegistry;if(!g)return await u.teardown().catch(y=>J("subagent-executor: handle teardown failed: "+(y instanceof Error?y.message:String(y)))),{content:'Background mode is not available in this session \u2014 no BackgroundAgentRegistry is wired. Re-issue the call with mode="foreground" or run inside `afk interactive`.',isError:!0};let h;try{h=g.register({handle:u,prompt:n.prompt,model:l.model??"sonnet",parentSessionId:this.ctx.parentSession.sessionId})}catch(y){if(y instanceof _s)return await u.teardown().catch(w=>J("subagent-executor: handle teardown failed after cap error: "+(w instanceof Error?w.message:String(w)))),{content:y.message,isError:!0};throw y}let b={status:"running",jobId:h.jobId,subagentId:h.subagentId,label:h.label,message:`Background subagent started (jobId=${h.jobId}). It is running detached and its result will NOT auto-inject into this context. Retrieve it later via /bgsub:join ${h.jobId} or ask the user to join.`};return{content:JSON.stringify(b)}}let d=()=>{u.cancel()};t.signal.addEventListener("abort",d,{once:!0});let m=Date.now(),f=this.ctx.parentSession.sessionId;try{let g=await u.runToResult(n.prompt);if(g.status==="succeeded"&&g.message){let w=g.message.content,x=typeof w=="string"?w:JSON.stringify(w),R=g.trace;return ka({event:"subagent.completed",subagent_id:u.id,parent_session_id:f,status:g.status,duration_ms:Date.now()-m,content_chars:x.length,depth:r,tool_call_count:R?.toolCalls.length,thinking_present:R?.thinkingPresent,tool_names:R?.toolCalls.length?JSON.stringify([...new Set(R.toolCalls.map(v=>v.name))]):void 0}),{content:x}}let h=g.error?.message??"Subagent failed with no output",b=g.trace;ka({event:"subagent.failed",subagent_id:u.id,id_prefix:n.id_prefix,parent_session_id:f,status:g.status,duration_ms:Date.now()-m,error_message:to(h),schema_error:g.schemaError?to(g.schemaError.message):void 0,partial_output_chars:Ay(g.partialOutput),depth:r,tool_call_count:b?.toolCalls.length,thinking_present:b?.thinkingPresent,tool_names:b?.toolCalls.length?JSON.stringify([...new Set(b.toolCalls.map(w=>w.name))]):void 0});let y=iM({status:g.status,errorMessage:h,schemaErrorMessage:g.schemaError?.message,partialOutput:g.partialOutput,subagentId:u.id});return{content:JSON.stringify(y),isError:!0}}catch(g){let h=g instanceof Error?g.message:String(g);throw ka({event:"subagent.failed",subagent_id:u.id,id_prefix:n.id_prefix,parent_session_id:f,status:"failed",duration_ms:Date.now()-m,error_message:to(h),depth:r}),g}finally{t.signal.removeEventListener("abort",d),await s?.teardownAll(),await u.teardown()}}};var _y=new Set;function Cy(e){_y.add(e)}function Iy(e){return _y.has(e)}var hd=new Set,yd=new Set;function Py(e){hd.add(e)}function My(e){hd.delete(e)}function Oy(e){yd.add(e)}function $y(e){yd.delete(e)}function Dy(e){for(let t of hd)t(e)}function Ly(e){for(let t of yd)t(e)}var aM=240;function lM(e,t=aM){return e.length<=t?e:e.slice(0,t)+"\u2026"}function cM(e){if(typeof e!="object"||e===null)return;let t=e.name;if(typeof t!="string")return;let n=t.trim();return n.length>0?n:void 0}function uM(e){if(typeof e!="object"||e===null)throw new Error("Skill tool input must be an object");let t=e,n=t.name;if(typeof n!="string"||n.trim().length===0)throw new Error('Skill tool input must have a non-empty "name" field');let r,o=t.arguments;if(o!==void 0){if(typeof o!="string")throw new Error('Skill tool "arguments" must be a string');r=o}return{name:n.trim(),arguments:r}}var Ht=class{constructor(t){this.ctx=t}ctx;pluginBodies=null;async execute(t){if(t.signal.aborted)return{content:"Skill tool call aborted",isError:!0};let n=this.ctx.depth??0,r=this.ctx.maxDepth??no;if(n>=r){let l=cM(t.input);return Te({event:"delegation.skipped",parent_session_id:this.ctx.parentSession.sessionId,reason:"max_depth",depth:n,requested_name:l}).catch(()=>{}),{content:`Skill tool not available at nesting depth ${n} (max ${r})`,isError:!0}}let o;try{o=uM(t.input)}catch(l){return{content:`Skill tool input validation failed: ${l instanceof Error?l.message:String(l)}`,isError:!0}}try{let l=$e(o.name);return await this.executeRegistrySkill(l,o.arguments,t)}catch{}let s=this.getPluginSkillBody(o.name);if(s){if(s.context==="load"){let l=s.body.replace(/\$\{?PLUGIN_ROOT\}?/g,()=>s.pluginPath);return this.executeLoadedPluginSkill(o.name,l,o.arguments,t)}return await this.executePluginSkill(o.name,s.body,s.pluginPath,o.arguments,t)}let a=an(this.ctx.pluginConfigs).map(l=>l.name).join(", ");return{content:`Skill "${o.name}" not found. Available skills: ${a||"(none)"}`,isError:!0}}async executeRegistrySkill(t,n,r){if(r.signal.aborted)return{content:"Skill call aborted",isError:!0};if(t.context==="fork")return this.executeForkedRegistrySkill(t,n,r);if(t.context==="load")return this.executeLoadedRegistrySkill(t,n,r);let o=Iy(t.name);o&&Ly(t.name);let s=this.ctx.depth??0;Te({event:"skill.dispatched",requested_name:t.name,parent_session_id:this.ctx.parentSession.sessionId,depth:s,...t.model!==void 0?{model:t.model}:{}}).catch(()=>{});let i=Date.now(),a,l;try{l=await t.handler(n&&n.length>0?n:void 0,this.ctx.parentSession,{apiKey:this.ctx.apiKey,defaultModel:this.ctx.defaultModel,defaultSubagentModel:this.ctx.defaultSubagentModel,callId:r.id,dispatchSkill:this.createDispatchSkillCallback(r)})}catch(u){a=u}finally{let u=Date.now()-i;o&&Dy({skillName:t.name,durationMs:u,...a!==void 0?{isError:!0}:{}});let d=a!==void 0?a instanceof Error?a.message:String(a):void 0,m=a===void 0?typeof l=="string"?l.length:l!=null?JSON.stringify(l).length:0:void 0;Te({event:"skill.completed",requested_name:t.name,parent_session_id:this.ctx.parentSession.sessionId,status:a!==void 0?"failed":"succeeded",duration_ms:u,depth:s,...m!==void 0?{content_chars:m}:{},...d!==void 0?{error_message:lM(d)}:{},...t.model!==void 0?{model:t.model}:{}}).catch(()=>{})}return a!==void 0?{content:`Skill execution error: ${a instanceof Error?a.message:String(a)}`,isError:!0}:{content:typeof l=="string"?l:l!=null?JSON.stringify(l):"Skill completed successfully."}}buildForkedChildConfig(t,n){let r=this.ctx.depth??0,o=this.ctx.maxDepth??no,s={...t};if(!this.ctx.childProviderFactory||r>=o)return{childConfig:s,childManager:void 0};let i=new X({parentAbortSignal:n,...this.ctx.traceWriter!==void 0?{traceWriter:this.ctx.traceWriter}:{},...this.ctx.cwd!==void 0?{cwd:this.ctx.cwd}:{}}),a=new Wt({subagentManager:i,parentSession:ar(n),defaultConfig:{model:s.model,apiKey:this.ctx.apiKey,...this.ctx.baseUrl!==void 0?{baseUrl:this.ctx.baseUrl}:{}},defaultSubagentModel:this.ctx.defaultSubagentModel,childProviderFactory:this.ctx.childProviderFactory,childSkillExecutorFactory:this.ctx.childSkillExecutorFactory,depth:r+1,maxDepth:o,...this.ctx.cwd!==void 0?{cwd:this.ctx.cwd}:{},...this.ctx.backgroundRegistry!==void 0?{backgroundRegistry:this.ctx.backgroundRegistry}:{}}),l=this.ctx.childSkillExecutorFactory?this.ctx.childSkillExecutorFactory(r+1,o,n):void 0;return s.provider=this.ctx.childProviderFactory({childExecutor:a,...l!==void 0?{childSkillExecutor:l}:{},...s.model!==void 0?{model:s.model}:{}}),{childConfig:s,childManager:i}}async executeForkedRegistrySkill(t,n,r){if(r.signal.aborted)return{content:"Skill call aborted",isError:!0};let o;try{if(o=we(t.name)["system.md"],!o)return{content:`Skill "${t.name}" has context: "fork" but no prompts/system.md found`,isError:!0}}catch(c){return{content:`Failed to load skill prompts: ${c instanceof Error?c.message:String(c)}`,isError:!0}}let s=new X({parentAbortSignal:r.signal,apiKey:this.ctx.apiKey,...this.ctx.baseUrl!==void 0?{baseUrl:this.ctx.baseUrl}:{},...this.ctx.traceWriter!==void 0?{traceWriter:this.ctx.traceWriter}:{},progressSink:gt(),...this.ctx.cwd!==void 0?{cwd:this.ctx.cwd}:{}}),{childConfig:i,childManager:a}=this.buildForkedChildConfig({model:t.model??this.ctx.defaultSubagentModel??this.ctx.defaultModel??"sonnet",systemPrompt:o,isSkillDispatch:!0,...this.ctx.traceWriter!==void 0?{traceWriter:this.ctx.traceWriter}:{}},r.signal),l;try{l=await s.forkSubagent({parent:this.ctx.parentSession,config:i,idPrefix:`skill-fork-${t.name}`,parentId:r.id,agentType:t.name});let c=n&&n.length>0?n:`Run the ${t.name} skill now, following the instructions in your system prompt.`,u=await l.runToResult(c);return u.status==="succeeded"&&u.message?{content:u.message.content}:u.status==="cancelled"&&typeof u.partialOutput=="string"&&u.partialOutput.length>0?{content:`[skill cancelled mid-flight \u2014 partial output preserved below]
1192
+ `)}}}statusFromResult(t){return t==="succeeded"?"completed":t==="failed"?"failed":t==="cancelled"?"cancelled":"failed"}snapshot(t){return{jobId:t.jobId,subagentId:t.subagentId,label:t.label,model:t.model,startedAt:t.startedAt,status:t.status,...t.result!==void 0?{result:t.result}:{},...t.endedAt!==void 0?{endedAt:t.endedAt}:{}}}};function rM(e){if(typeof e!="object"||e===null)throw new Error("Agent tool input must be an object");let t=e,n=t.prompt;if(typeof n!="string")throw new Error('Agent tool input must have a "prompt" field of type string');if(n.trim().length===0)throw new Error("Agent tool prompt cannot be empty");let r,o=t.model;if(o!==void 0){if(typeof o!="string")throw new Error("Agent tool model must be a string");r=o}let s=10,i=t.max_turns;if(i!==void 0){if(typeof i!="number")throw new Error("Agent tool max_turns must be a number");s=Math.max(1,Math.min(50,Math.floor(i)))}let a="agent-tool",l=t.id_prefix;if(l!==void 0){if(typeof l!="string")throw new Error("Agent tool id_prefix must be a string");a=l}let c="foreground",u=t.mode;if(u!==void 0){if(u!=="foreground"&&u!=="background")throw new Error(`Agent tool mode must be "foreground" or "background", got: ${JSON.stringify(u)}`);c=u}let d,p=t.cwd;if(p!==void 0){if(typeof p!="string")throw new Error(`Agent tool cwd must be a string, got: ${JSON.stringify(p)}`);if(p.length===0)throw new Error("Agent tool cwd must be a non-empty string");if(!nM(p))throw new Error(`Agent tool cwd must be an absolute path, got: ${JSON.stringify(p)}`);if(p.split(/[/\\]/).includes(".."))throw new Error(`Agent tool cwd must not contain '..' segments, got: ${JSON.stringify(p)}`);d=p}return{prompt:n,model:r,max_turns:s,id_prefix:a,mode:c,...d!==void 0?{cwd:d}:{}}}function ka(e){try{return Te(e).catch(()=>{})}catch{return Promise.resolve()}}function to(e,t=240){return e.length<=t?e:e.slice(0,t)+"\u2026"}function Ay(e){if(e!=null){if(typeof e=="string")return e.length;try{return JSON.stringify(e).length}catch{return}}}var oM=4096,Ry=1024;function sM(e){if(e==null)return;let t=Ay(e);return t!==void 0&&t>oM?{truncated:!0,chars:t}:e}function iM(e){let t={status:e.status,error:to(e.errorMessage,Ry),subagent_id:e.subagentId};e.schemaErrorMessage&&(t.schemaError=to(e.schemaErrorMessage,Ry));let n=sM(e.partialOutput);return n!==void 0&&(t.partialOutput=n),t}var Wt=class e{constructor(t){this.ctx=t}ctx;getSubagentsLite(){let t=this.ctx.subagentManager.list().map(r=>({id:r.id,status:r.status})),n=this.ctx.backgroundRegistry?this.ctx.backgroundRegistry.list().map(r=>({jobId:r.jobId,status:r.status,startedAt:new Date(r.startedAt).toISOString(),label:r.label.length>0?r.label:null})):[];return{active:t,backgroundJobs:n}}async execute(t){if(t.signal.aborted)return{content:"Agent tool call aborted",isError:!0};let n;try{n=rM(t.input)}catch(g){return{content:`Agent tool input validation failed: ${g instanceof Error?g.message:String(g)}`,isError:!0}}let r=this.ctx.depth,o=this.ctx.maxDepth??no,s,i=n.model??this.ctx.defaultSubagentModel??"sonnet",a=xe(i)==="openai-compatible",l={model:i,apiKey:a?void 0:this.ctx.defaultConfig.apiKey,systemPrompt:this.ctx.defaultConfig.systemPrompt,baseUrl:a?void 0:this.ctx.defaultConfig.baseUrl,maxTurns:n.max_turns,depth:r+1,maxDepth:o,...n.cwd!==void 0?{cwd:n.cwd}:{}},c;if(this.ctx.childProviderFactory&&r<o){s=new X({parentAbortSignal:t.signal,...this.ctx.cwd!==void 0?{cwd:this.ctx.cwd}:{}}),c=ar(t.signal);let g=new e({subagentManager:s,parentSession:c,defaultConfig:this.ctx.defaultConfig,defaultSubagentModel:this.ctx.defaultSubagentModel,childProviderFactory:this.ctx.childProviderFactory,childSkillExecutorFactory:this.ctx.childSkillExecutorFactory,depth:r+1,maxDepth:o,...this.ctx.cwd!==void 0?{cwd:this.ctx.cwd}:{}}),h=this.ctx.childSkillExecutorFactory?this.ctx.childSkillExecutorFactory(r+1,o,t.signal):void 0;l.provider=this.ctx.childProviderFactory({childExecutor:g,...h!==void 0?{childSkillExecutor:h}:{},...l.model!==void 0?{model:l.model}:{}})}let u;try{u=await this.ctx.subagentManager.forkSubagent({parent:this.ctx.parentSession,parentId:t.id,config:l,idPrefix:n.id_prefix,agentType:n.id_prefix&&n.id_prefix!=="agent-tool"?on(n.id_prefix).replace(/[\r\n]+/g," ").trim()||"agent":on(n.prompt).replace(/[\r\n]+/g," ").slice(0,40).trim()||"agent",denyElicitations:n.mode==="background"}),c!==void 0&&(c.sessionId=u.id)}catch(g){let h=g instanceof Error?g.message:String(g);return ka({event:"subagent.failed",subagent_id:"unknown",id_prefix:n.id_prefix,parent_session_id:this.ctx.parentSession.sessionId,status:"failed",error_message:to(h),depth:r}),{content:`Failed to fork subagent: ${h}`,isError:!0}}if(n.mode==="background"){let g=this.ctx.backgroundRegistry;if(!g)return await u.teardown().catch(y=>J("subagent-executor: handle teardown failed: "+(y instanceof Error?y.message:String(y)))),{content:'Background mode is not available in this session \u2014 no BackgroundAgentRegistry is wired. Re-issue the call with mode="foreground" or run inside `afk interactive`.',isError:!0};let h;try{h=g.register({handle:u,prompt:n.prompt,model:l.model??"sonnet",parentSessionId:this.ctx.parentSession.sessionId})}catch(y){if(y instanceof _s)return await u.teardown().catch(w=>J("subagent-executor: handle teardown failed after cap error: "+(w instanceof Error?w.message:String(w)))),{content:y.message,isError:!0};throw y}let b={status:"running",jobId:h.jobId,subagentId:h.subagentId,label:h.label,message:`Background subagent started (jobId=${h.jobId}). It is running detached and its result will NOT auto-inject into this context. Retrieve it later via /bgsub:join ${h.jobId} or ask the user to join.`};return{content:JSON.stringify(b)}}let d=()=>{u.cancel()};t.signal.addEventListener("abort",d,{once:!0});let p=Date.now(),f=this.ctx.parentSession.sessionId;try{let g=await u.runToResult(n.prompt);if(g.status==="succeeded"&&g.message){let w=g.message.content,x=typeof w=="string"?w:JSON.stringify(w),R=g.trace;return ka({event:"subagent.completed",subagent_id:u.id,parent_session_id:f,status:g.status,duration_ms:Date.now()-p,content_chars:x.length,depth:r,tool_call_count:R?.toolCalls.length,thinking_present:R?.thinkingPresent,tool_names:R?.toolCalls.length?JSON.stringify([...new Set(R.toolCalls.map(v=>v.name))]):void 0}),{content:x}}let h=g.error?.message??"Subagent failed with no output",b=g.trace;ka({event:"subagent.failed",subagent_id:u.id,id_prefix:n.id_prefix,parent_session_id:f,status:g.status,duration_ms:Date.now()-p,error_message:to(h),schema_error:g.schemaError?to(g.schemaError.message):void 0,partial_output_chars:Ay(g.partialOutput),depth:r,tool_call_count:b?.toolCalls.length,thinking_present:b?.thinkingPresent,tool_names:b?.toolCalls.length?JSON.stringify([...new Set(b.toolCalls.map(w=>w.name))]):void 0});let y=iM({status:g.status,errorMessage:h,schemaErrorMessage:g.schemaError?.message,partialOutput:g.partialOutput,subagentId:u.id});return{content:JSON.stringify(y),isError:!0}}catch(g){let h=g instanceof Error?g.message:String(g);throw ka({event:"subagent.failed",subagent_id:u.id,id_prefix:n.id_prefix,parent_session_id:f,status:"failed",duration_ms:Date.now()-p,error_message:to(h),depth:r}),g}finally{t.signal.removeEventListener("abort",d),await s?.teardownAll(),await u.teardown()}}};var _y=new Set;function Cy(e){_y.add(e)}function Iy(e){return _y.has(e)}var hd=new Set,yd=new Set;function Py(e){hd.add(e)}function My(e){hd.delete(e)}function Oy(e){yd.add(e)}function $y(e){yd.delete(e)}function Dy(e){for(let t of hd)t(e)}function Ly(e){for(let t of yd)t(e)}var aM=240;function lM(e,t=aM){return e.length<=t?e:e.slice(0,t)+"\u2026"}function cM(e){if(typeof e!="object"||e===null)return;let t=e.name;if(typeof t!="string")return;let n=t.trim();return n.length>0?n:void 0}function uM(e){if(typeof e!="object"||e===null)throw new Error("Skill tool input must be an object");let t=e,n=t.name;if(typeof n!="string"||n.trim().length===0)throw new Error('Skill tool input must have a non-empty "name" field');let r,o=t.arguments;if(o!==void 0){if(typeof o!="string")throw new Error('Skill tool "arguments" must be a string');r=o}return{name:n.trim(),arguments:r}}var Ht=class{constructor(t){this.ctx=t}ctx;pluginBodies=null;async execute(t){if(t.signal.aborted)return{content:"Skill tool call aborted",isError:!0};let n=this.ctx.depth??0,r=this.ctx.maxDepth??no;if(n>=r){let l=cM(t.input);return Te({event:"delegation.skipped",parent_session_id:this.ctx.parentSession.sessionId,reason:"max_depth",depth:n,requested_name:l}).catch(()=>{}),{content:`Skill tool not available at nesting depth ${n} (max ${r})`,isError:!0}}let o;try{o=uM(t.input)}catch(l){return{content:`Skill tool input validation failed: ${l instanceof Error?l.message:String(l)}`,isError:!0}}try{let l=$e(o.name);return await this.executeRegistrySkill(l,o.arguments,t)}catch{}let s=this.getPluginSkillBody(o.name);if(s){if(s.context==="load"){let l=s.body.replace(/\$\{?PLUGIN_ROOT\}?/g,()=>s.pluginPath);return this.executeLoadedPluginSkill(o.name,l,o.arguments,t)}return await this.executePluginSkill(o.name,s.body,s.pluginPath,o.arguments,t)}let a=an(this.ctx.pluginConfigs).map(l=>l.name).join(", ");return{content:`Skill "${o.name}" not found. Available skills: ${a||"(none)"}`,isError:!0}}async executeRegistrySkill(t,n,r){if(r.signal.aborted)return{content:"Skill call aborted",isError:!0};if(t.context==="fork")return this.executeForkedRegistrySkill(t,n,r);if(t.context==="load")return this.executeLoadedRegistrySkill(t,n,r);let o=Iy(t.name);o&&Ly(t.name);let s=this.ctx.depth??0;Te({event:"skill.dispatched",requested_name:t.name,parent_session_id:this.ctx.parentSession.sessionId,depth:s,...t.model!==void 0?{model:t.model}:{}}).catch(()=>{});let i=Date.now(),a,l;try{l=await t.handler(n&&n.length>0?n:void 0,this.ctx.parentSession,{apiKey:this.ctx.apiKey,defaultModel:this.ctx.defaultModel,defaultSubagentModel:this.ctx.defaultSubagentModel,callId:r.id,dispatchSkill:this.createDispatchSkillCallback(r)})}catch(u){a=u}finally{let u=Date.now()-i;o&&Dy({skillName:t.name,durationMs:u,...a!==void 0?{isError:!0}:{}});let d=a!==void 0?a instanceof Error?a.message:String(a):void 0,p=a===void 0?typeof l=="string"?l.length:l!=null?JSON.stringify(l).length:0:void 0;Te({event:"skill.completed",requested_name:t.name,parent_session_id:this.ctx.parentSession.sessionId,status:a!==void 0?"failed":"succeeded",duration_ms:u,depth:s,...p!==void 0?{content_chars:p}:{},...d!==void 0?{error_message:lM(d)}:{},...t.model!==void 0?{model:t.model}:{}}).catch(()=>{})}return a!==void 0?{content:`Skill execution error: ${a instanceof Error?a.message:String(a)}`,isError:!0}:{content:typeof l=="string"?l:l!=null?JSON.stringify(l):"Skill completed successfully."}}buildForkedChildConfig(t,n){let r=this.ctx.depth??0,o=this.ctx.maxDepth??no,s={...t};if(!this.ctx.childProviderFactory||r>=o)return{childConfig:s,childManager:void 0};let i=new X({parentAbortSignal:n,...this.ctx.traceWriter!==void 0?{traceWriter:this.ctx.traceWriter}:{},...this.ctx.cwd!==void 0?{cwd:this.ctx.cwd}:{}}),a=new Wt({subagentManager:i,parentSession:ar(n),defaultConfig:{model:s.model,apiKey:this.ctx.apiKey,...this.ctx.baseUrl!==void 0?{baseUrl:this.ctx.baseUrl}:{}},defaultSubagentModel:this.ctx.defaultSubagentModel,childProviderFactory:this.ctx.childProviderFactory,childSkillExecutorFactory:this.ctx.childSkillExecutorFactory,depth:r+1,maxDepth:o,...this.ctx.cwd!==void 0?{cwd:this.ctx.cwd}:{},...this.ctx.backgroundRegistry!==void 0?{backgroundRegistry:this.ctx.backgroundRegistry}:{}}),l=this.ctx.childSkillExecutorFactory?this.ctx.childSkillExecutorFactory(r+1,o,n):void 0;return s.provider=this.ctx.childProviderFactory({childExecutor:a,...l!==void 0?{childSkillExecutor:l}:{},...s.model!==void 0?{model:s.model}:{}}),{childConfig:s,childManager:i}}async executeForkedRegistrySkill(t,n,r){if(r.signal.aborted)return{content:"Skill call aborted",isError:!0};let o;try{if(o=we(t.name)["system.md"],!o)return{content:`Skill "${t.name}" has context: "fork" but no prompts/system.md found`,isError:!0}}catch(c){return{content:`Failed to load skill prompts: ${c instanceof Error?c.message:String(c)}`,isError:!0}}let s=new X({parentAbortSignal:r.signal,apiKey:this.ctx.apiKey,...this.ctx.baseUrl!==void 0?{baseUrl:this.ctx.baseUrl}:{},...this.ctx.traceWriter!==void 0?{traceWriter:this.ctx.traceWriter}:{},progressSink:gt(),...this.ctx.cwd!==void 0?{cwd:this.ctx.cwd}:{}}),{childConfig:i,childManager:a}=this.buildForkedChildConfig({model:t.model??this.ctx.defaultSubagentModel??this.ctx.defaultModel??"sonnet",systemPrompt:o,isSkillDispatch:!0,...this.ctx.traceWriter!==void 0?{traceWriter:this.ctx.traceWriter}:{}},r.signal),l;try{l=await s.forkSubagent({parent:this.ctx.parentSession,config:i,idPrefix:`skill-fork-${t.name}`,parentId:r.id,agentType:t.name});let c=n&&n.length>0?n:`Run the ${t.name} skill now, following the instructions in your system prompt.`,u=await l.runToResult(c);return u.status==="succeeded"&&u.message?{content:u.message.content}:u.status==="cancelled"&&typeof u.partialOutput=="string"&&u.partialOutput.length>0?{content:`[skill cancelled mid-flight \u2014 partial output preserved below]
1193
1193
 
1194
1194
  ${u.partialOutput}`}:{content:u.error?.message??"Forked skill failed with no output",isError:!0}}catch(c){return{content:`Forked skill execution error: ${c instanceof Error?c.message:String(c)}`,isError:!0}}finally{l&&await l.teardown().catch(J),await a?.teardownAll(),await s.teardownAll()}}formatLoadedSkillResult(t,n,r){let o=r&&r.trim().length>0?r.trim():"(none)";return{content:`${`[Skill "${t}" loaded into your current context \u2014 act on it now]
1195
1195
  The instructions below are your operating procedure for THIS task. Execute them immediately, in this session, using the tools you already have. This is an instruction set, not reference material: follow it directly \u2014 do not merely summarize or describe it. No sub-agent was forked; you are the one carrying it out.
@@ -1199,7 +1199,7 @@ Arguments: ${o}`}
1199
1199
 
1200
1200
  ${n}`}}emitLoadTelemetry(t,n,r,o){let s=this.ctx.depth??0,i={requested_name:t,parent_session_id:this.ctx.parentSession.sessionId,depth:s,mode:"load",...o!==void 0?{model:o}:{}};Te({event:"skill.dispatched",...i}).catch(()=>{}),Te({event:"skill.completed",status:"succeeded",duration_ms:r,content_chars:n,...i}).catch(()=>{})}executeLoadedRegistrySkill(t,n,r){if(r.signal.aborted)return{content:"Skill call aborted",isError:!0};let o=Date.now(),s;try{let l=we(t.name)["system.md"];if(!l)return{content:`Skill "${t.name}" has context: "load" but no prompts/system.md found`,isError:!0};s=l}catch(a){return{content:`Failed to load skill prompts: ${a instanceof Error?a.message:String(a)}`,isError:!0}}let i=this.substituteSkillArgs(s,n);return this.emitLoadTelemetry(t.name,i.length,Date.now()-o,t.model),this.formatLoadedSkillResult(t.name,i,n)}executeLoadedPluginSkill(t,n,r,o){if(o.signal.aborted)return{content:"Skill call aborted",isError:!0};let s=Date.now(),i=this.substituteSkillArgs(n,r);return this.emitLoadTelemetry(t,i.length,Date.now()-s,void 0),this.formatLoadedSkillResult(t,i,r)}substituteSkillArgs(t,n){let r=n??"";return t.replace(/\$ARGUMENTS?\b/g,()=>r)}async executePluginSkill(t,n,r,o,s){if(s.signal.aborted)return{content:"Skill call aborted",isError:!0};let i=new X({parentAbortSignal:s.signal,apiKey:this.ctx.apiKey,...this.ctx.baseUrl!==void 0?{baseUrl:this.ctx.baseUrl}:{},...this.ctx.traceWriter!==void 0?{traceWriter:this.ctx.traceWriter}:{},progressSink:gt(),...this.ctx.cwd!==void 0?{cwd:this.ctx.cwd}:{}}),{childConfig:a,childManager:l}=this.buildForkedChildConfig({model:this.ctx.defaultSubagentModel??this.ctx.defaultModel??"sonnet",systemPrompt:this.substituteSkillArgs(n,o),env:{PLUGIN_ROOT:r},isSkillDispatch:!0,...this.ctx.traceWriter!==void 0?{traceWriter:this.ctx.traceWriter}:{}},s.signal),c;try{c=await i.forkSubagent({parent:this.ctx.parentSession,config:a,idPrefix:`skill-${t}`,parentId:s.id,agentType:t});let u=o&&o.length>0?o:`Run the ${t} skill now, following the instructions in your system prompt.`,d=await c.runToResult(u);return d.status==="succeeded"&&d.message?{content:d.message.content}:d.status==="cancelled"&&typeof d.partialOutput=="string"&&d.partialOutput.length>0?{content:`[skill cancelled mid-flight \u2014 partial output preserved below]
1201
1201
 
1202
- ${d.partialOutput}`}:{content:d.error?.message??"Plugin skill failed with no output",isError:!0}}catch(u){return{content:`Plugin skill execution error: ${u instanceof Error?u.message:String(u)}`,isError:!0}}finally{c&&await c.teardown().catch(J),await l?.teardownAll(),await i.teardownAll()}}getPluginSkillBody(t){return this.pluginBodies||(this.pluginBodies=va(this.ctx.pluginConfigs)),this.pluginBodies.get(t)}createDispatchSkillCallback(t){return async(n,r)=>{let o={id:`${t.id}-dispatch-${n}`,name:"skill",input:{name:n,...r!==void 0?{arguments:r}:{}},signal:t.signal},s=await this.execute(o);if(s.isError)throw new Error(s.content);return s.content}}};var no=3;function ar(e){return{sessionId:void 0,getInputStreamRef:()=>({pushUserMessage:()=>{}}),abortSignal:e}}var dM=[...Ft,...st,"memory_search","agent","skill"];function ro(e={}){return({childExecutor:t,childSkillExecutor:n,model:r})=>{let o={permissions:{allowedTools:dM},subagentExecutor:t,...n!==void 0?{skillExecutor:n}:{}};return xe(typeof r=="string"?r:void 0)==="openai-compatible"?new Rt({...o,...e.openaiBaseUrl!==void 0?{baseURL:e.openaiBaseUrl}:{}}):new Fe(o)}}function oo(e,t,n,r,o,s,i){let a=(l,c,u)=>new Ht({parentSession:ar(u),defaultModel:e,apiKey:t,...r!==void 0?{baseUrl:r}:{},depth:l,maxDepth:c,childProviderFactory:n,childSkillExecutorFactory:a,...o!==void 0?{traceWriter:o}:{},...s!==void 0?{backgroundRegistry:s}:{},...i!==void 0?{cwd:i}:{}});return a}function Fy(e,t){let n={allowedTools:[...ky]};return xe(typeof t=="string"?t:void 0)==="openai-compatible"?new Rt({permissions:n}):new Fe({permissions:n})}function Ny(e){let t=pM(e);return t!==void 0?t:mM(e)}function pM(e){let t=/```(?:json)?\s*([\s\S]*?)```/gi,n,r;for(;(r=t.exec(e))!==null;)n=r[1];if(n)return By(n.trim())}function mM(e){for(let t=e.length-1;t>=0;t--){if(e[t]!=="}")continue;let n=fM(e,t);if(n===-1)continue;let r=e.slice(n,t+1),o=By(r);if(o!==void 0)return o}}function fM(e,t){let n=0,r=!1,o=!1;for(let s=t;s>=0;s--){let i=e[s];if(o){o=!1;continue}if(r){if(i==="\\"){o=!0;continue}i==='"'&&(r=!1);continue}if(i==='"'){r=!0;continue}if(i==="}")n++;else if(i==="{"&&(n--,n===0))return s}return-1}function By(e){try{return JSON.parse(e)}catch{return}}function bd(){return{toolCalls:[],toolResults:[],thinkingPresent:!1,turnCount:0}}function Uy(e,t,n,r,o){if(!r)return{id:e,status:t,message:n,trace:o};let s=Ny(n.content),i=r.safeParse(s);return i.success?{id:e,status:t,message:n,output:i.data,trace:o}:{id:e,status:"failed",message:n,error:new Error(`structured output did not match schema: ${i.error.message}`,{cause:i.error}),schemaError:i.error,trace:o}}function jy(e,t,n,r){let o=n instanceof Error?n:new Error(String(n));return{id:e,status:t,error:o,trace:r}}function Ee(e){return`${e.status}${e.error?`: ${e.error.message}`:""}`}function Wy(e,t){let n=e;return t.partialOutput!==void 0&&t.partialOutput!==null&&(n.partialOutput=t.partialOutput),t.subagentId!==void 0&&(n.subagentId=t.subagentId),n}var Ta=class{constructor(t,n,r,o,s,i,a,l,c,u,d,m,f,g,h){this.id=t;this.session=n;this.controller=r;this.abortGraph=o;this.outputSchema=s;this.timeoutMs=i;this.hookRegistry=a;this.onTerminal=l;this.parentInputStreamRef=c;this.parentAbortSignal=u;this.agentType=d;this.traceWriter=g;this.onSubagentSucceeded=h;this.progressSink=m,this.parentId=f}id;session;controller;abortGraph;outputSchema;timeoutMs;hookRegistry;onTerminal;parentInputStreamRef;parentAbortSignal;agentType;traceWriter;onSubagentSucceeded;currentStatus="idle";inFlight=null;lastMessage;lastDurationMs;latestTerminalStatus;stopDispatched=!1;progressSink;parentId;currentTrace=bd();lastStreamedContent="";get status(){return this.currentStatus}async run(t,n){if(this.currentStatus==="running")throw new Error(`Subagent ${this.id} is already running`);if(this.currentStatus==="cancelled")throw new Error(`Subagent ${this.id} is cancelled`);this.currentStatus="running";let r=Date.now(),o=Qi(this.streamToFinalMessage(t,n),this.timeoutMs,{controller:this.controller,label:this.id});this.inFlight=o;try{let s=await o;this.lastMessage=s.content,this.lastDurationMs=Date.now()-r,this.currentStatus="succeeded",this.latestTerminalStatus="succeeded",er(this.traceWriter,{transition:"succeeded",subagentId:this.id,durationMs:this.lastDurationMs,turnCount:this.currentTrace.turnCount,outputBytes:Buffer.byteLength(this.lastMessage,"utf8")});let i=typeof s.metadata?.totalCostUsd=="number"?s.metadata.totalCostUsd:void 0;return this.onSubagentSucceeded?.(this.currentTrace.usage,i),this.onTerminal(),s}catch(s){throw this.lastDurationMs=Date.now()-r,this.currentStatus!=="cancelled"&&(this.controller.signal.aborted?(er(this.traceWriter,{transition:"cancelled",subagentId:this.id,source:"cascade"}),this.currentStatus="cancelled",this.latestTerminalStatus="cancelled"):(er(this.traceWriter,{transition:"failed",subagentId:this.id,errorClass:s instanceof Error?s.constructor.name:"Unknown",errorMessage:s instanceof Error?s.message:String(s),partialOutputBytes:Buffer.byteLength(this.lastStreamedContent,"utf8")}),this.currentStatus="failed",this.latestTerminalStatus="failed")),this.onTerminal(),s}finally{this.inFlight=null}}async streamToFinalMessage(t,n){let r,o;this.lastStreamedContent="",this.currentTrace=bd();let s=n??this.progressSink??gt(),i={subagentId:this.id,...this.parentId!==void 0&&{parentId:this.parentId},...this.agentType!==void 0&&{agentType:this.agentType}};for await(let a of this.session.sendMessageStream(t)){if(s&&s(a,i),a.type==="chunk"){let l=a.chunk;l.type==="content"?this.lastStreamedContent+=l.content:l.type==="tool_use_detail"?this.currentTrace.toolCalls.push({id:l.toolUseId,name:l.toolName,inputBytes:Buffer.byteLength(l.toolInput,"utf8")}):l.type==="tool_result"?this.currentTrace.toolResults.push({toolUseId:l.toolUseId,isError:l.isError,truncated:l.truncated,sizeBytes:l.sizeBytes}):l.type==="thinking"&&(this.currentTrace.thinkingPresent=!0)}if(a.type==="message")r=a.message,this.currentTrace.turnCount++;else if(a.type==="error"){o=a.error;break}else if(a.type==="done"){if(typeof a.metadata?.usage=="object"&&a.metadata.usage!==null){let l=a.metadata.usage;this.currentTrace.usage={inputTokens:typeof l.input_tokens=="number"?l.input_tokens:void 0,outputTokens:typeof l.output_tokens=="number"?l.output_tokens:void 0,cacheReadTokens:typeof l.cache_read_input_tokens=="number"?l.cache_read_input_tokens:void 0,cacheCreationTokens:typeof l.cache_creation_input_tokens=="number"?l.cache_creation_input_tokens:void 0}}break}}if(o)throw o;if(r)return r;if(this.lastStreamedContent.length>0)return{role:"assistant",content:this.lastStreamedContent,timestamp:new Date};throw new Error(`Subagent ${this.id} produced no terminal message`)}async runToResult(t,n){try{let r=await this.run(t,n);return Uy(this.id,this.currentStatus,r,this.outputSchema,this.currentTrace)}catch(r){let o=jy(this.id,this.currentStatus,r,this.currentTrace);return this.lastStreamedContent.length>0&&(o.partialOutput=this.lastStreamedContent),o}}runInBackground(t,n,r){let o;if(r){let s=this.progressSink??gt();o=(i,a)=>{r(i),s?.(i,a)}}this.runToResult(t,o).then(s=>{n?.(s)}).catch(s=>{J("runInBackground: unexpected rejection after runToResult",s),console.error("Subagent runInBackground failed unexpectedly:",s)})}async cancel(){if(this.currentStatus==="cancelled"||this.stopDispatched)return;let t=this.latestTerminalStatus??"cancelled";this.currentStatus="cancelled",er(this.traceWriter,{transition:"cancelled",subagentId:this.id,source:"explicit"});try{this.abortGraph.abort(this.id,"cancelled")}catch{}try{this.inFlight&&await this.session.interrupt()}catch{}try{await this.session.close()}finally{await this.dispatchStopAndRelease(t)}}async teardown(){if(this.stopDispatched)return;let t=this.latestTerminalStatus??"cancelled";try{this.inFlight&&await this.session.interrupt()}catch{}try{await this.session.close()}finally{await this.dispatchStopAndRelease(t)}}async dispatchStopAndRelease(t){if(this.stopDispatched){this.onTerminal();return}this.stopDispatched=!0;let n=await Dg(this.hookRegistry,{event:"SubagentStop",subagentId:this.id,status:t,lastMessage:this.lastMessage,agentType:this.agentType,durationMs:this.lastDurationMs,trace:this.currentTrace},this.traceWriter?{traceWriter:this.traceWriter}:{});if(n.injectContext&&this.parentInputStreamRef)if(this.parentAbortSignal?.aborted)J(`Skipping SubagentStop injectContext for ${this.id}: parent is aborted`);else try{this.parentInputStreamRef.pushUserMessage(n.injectContext)}catch(r){J(`Failed to inject context from SubagentStop handler: ${String(r)}`)}this.onTerminal()}};var gM=async(e,t)=>({action:"decline"}),X=class{active=new Map;parentCanUseTool;hookRegistry;progressSink;parentApiKey;parentBaseUrl;parentCwd;abortGraph;rootId;rootController;counter=0;onSubagentSucceededCb;constructor(t={}){if(this.parentCanUseTool=t.canUseTool,this.hookRegistry=t.hookRegistry,this.progressSink=t.progressSink,this.parentApiKey=t.apiKey,this.parentBaseUrl=t.baseUrl,this.parentCwd=t.cwd,this.onSubagentSucceededCb=t.onSubagentSucceeded,this.abortGraph=new Xi(t.traceWriter),this.rootId=`manager-root-${Date.now()}-${Math.random().toString(36).slice(2,8)}`,this.rootController=new AbortController,this.abortGraph.register(this.rootId,this.rootController),t.parentAbortSignal){let n=t.parentAbortSignal;n.aborted?this.rootController.abort(n.reason):n.addEventListener("abort",()=>{this.rootController.signal.aborted||this.rootController.abort(n.reason)},{once:!0})}}list(){return[...this.active.values()].map(t=>({id:t.id,status:t.status}))}get(t){return this.active.get(t)}onChildAborted(t){return this.abortGraph.onChildAborted(this.rootId,t)}setOnSubagentSucceeded(t){this.onSubagentSucceededCb=t}abortAll(t,n="user_signal"){this.abortGraph.abort(this.rootId,t,n)}async forkSubagent(t){if(t.phaseRole!==void 0&&t.phaseRole!=="read-write"&&t.config.provider!==void 0)throw new Error(`SubagentManager.forkSubagent: phaseRole "${t.phaseRole}" is mutually exclusive with config.provider. Remove one \u2014 either let the manager construct the phase-restricted provider, or use config.provider with phaseRole: "read-write" (default).`);let n=`${t.idPrefix??"subagent"}-${Date.now()}-${++this.counter}`,r=t.parent.sessionId,o=t.config.hookRegistry??this.hookRegistry??t.parent.hookRegistry;o&&await $g(o,{event:"SubagentStart",subagentId:n,parentSessionId:t.parent.sessionId},{signal:this.rootController.signal,...t.config.traceWriter?{traceWriter:t.config.traceWriter}:{}});let s=new AbortController;this.abortGraph.register(n,s),this.abortGraph.linkChild(this.rootId,n);let i={...t.config,resume:r,forkSession:r?!0:t.config.forkSession,abortSignal:s.signal,apiKey:t.config.apiKey||this.parentApiKey,baseUrl:t.config.baseUrl??this.parentBaseUrl,...t.config.parentSessionId===void 0&&t.parent.sessionId!==void 0?{parentSessionId:t.parent.sessionId}:{},...t.config.phaseRole===void 0&&t.phaseRole!==void 0?{phaseRole:t.phaseRole}:{},...t.config.cwd===void 0&&this.parentCwd!==void 0?{cwd:this.parentCwd}:{},hookRegistry:o,permissionBubbler:t.config.permissionBubbler??(this.parentCanUseTool!==void 0&&t.config.canUseTool===void 0?{canUseTool:this.parentCanUseTool}:void 0),...t.denyElicitations===!0?{onElicitation:gM}:{},...t.phaseRole==="read-only"?{provider:Fy("read-only",t.config.model)}:{}},a;try{a=new Ve(i)}catch(h){throw this.abortGraph.dispose(n),h}let l=t.parent.getInputStreamRef?.(),c=t.parent.abortSignal,u=this.progressSink??gt(),d=t.agentType?.trim()||void 0,m=t.parentId?.trim()||void 0,f=new Ta(n,a,s,this.abortGraph,t.outputSchema,t.config.timeoutMs??Zi,o,()=>{this.active.delete(n),this.abortGraph.dispose(n)},l,c,d??t.idPrefix,u,m??t.parent.sessionId,t.config.traceWriter,this.onSubagentSucceededCb);this.active.set(n,f);let g=typeof t.config.model=="string"?t.config.model:JSON.stringify(t.config.model);return er(t.config.traceWriter,{transition:"started",subagentId:n,parentId:t.parent.sessionId??this.rootId,model:g,...i.tools?.allowedTools?{allowedTools:[...i.tools.allowedTools]}:{}}),await Te({event:"subagent.dispatched",subagent_id:n,id_prefix:t.idPrefix,model:g,parent_session_id:t.parent.sessionId}),f}async kill(t){let n=this.active.get(t);return n?(await n.cancel(),!0):!1}async killAll(){await Promise.allSettled([...this.active.values()].map(t=>t.cancel()))}async teardownAll(){await Promise.allSettled([...this.active.values()].map(t=>t.teardown()))}};async function xa(e,t={}){let{failFast:n=!0,teardown:r=!0}=t;if(e.length===0)return[];let o=new Array(e.length),s=new Set(e.map((a,l)=>l)),i=e.map((a,l)=>a.handle.runToResult(a.prompt).then(c=>{if(o[l]=c,s.delete(l),n&&c.status!=="succeeded")for(let u of s){let d=e[u];d&&d.handle.status==="running"&&d.handle.cancel().catch(()=>{})}}));return await Promise.all(i),r&&await Promise.allSettled(e.map(a=>a.handle.teardown())),o}import{fileURLToPath as hM}from"node:url";import{dirname as yM}from"node:path";var bM=hM(import.meta.url),H6=yM(bM),at={name:"research-agent",systemPrompt:`---
1202
+ ${d.partialOutput}`}:{content:d.error?.message??"Plugin skill failed with no output",isError:!0}}catch(u){return{content:`Plugin skill execution error: ${u instanceof Error?u.message:String(u)}`,isError:!0}}finally{c&&await c.teardown().catch(J),await l?.teardownAll(),await i.teardownAll()}}getPluginSkillBody(t){return this.pluginBodies||(this.pluginBodies=va(this.ctx.pluginConfigs)),this.pluginBodies.get(t)}createDispatchSkillCallback(t){return async(n,r)=>{let o={id:`${t.id}-dispatch-${n}`,name:"skill",input:{name:n,...r!==void 0?{arguments:r}:{}},signal:t.signal},s=await this.execute(o);if(s.isError)throw new Error(s.content);return s.content}}};var no=3;function ar(e){return{sessionId:void 0,getInputStreamRef:()=>({pushUserMessage:()=>{}}),abortSignal:e}}var dM=[...Ft,...st,"memory_search","agent","skill"];function ro(e={}){return({childExecutor:t,childSkillExecutor:n,model:r})=>{let o={permissions:{allowedTools:dM},subagentExecutor:t,...n!==void 0?{skillExecutor:n}:{}};return xe(typeof r=="string"?r:void 0)==="openai-compatible"?new Rt({...o,...e.openaiBaseUrl!==void 0?{baseURL:e.openaiBaseUrl}:{}}):new Fe(o)}}function oo(e,t,n,r,o,s,i){let a=(l,c,u)=>new Ht({parentSession:ar(u),defaultModel:e,apiKey:t,...r!==void 0?{baseUrl:r}:{},depth:l,maxDepth:c,childProviderFactory:n,childSkillExecutorFactory:a,...o!==void 0?{traceWriter:o}:{},...s!==void 0?{backgroundRegistry:s}:{},...i!==void 0?{cwd:i}:{}});return a}function Fy(e,t){let n={allowedTools:[...ky]};return xe(typeof t=="string"?t:void 0)==="openai-compatible"?new Rt({permissions:n}):new Fe({permissions:n})}function Ny(e){let t=pM(e);return t!==void 0?t:mM(e)}function pM(e){let t=/```(?:json)?\s*([\s\S]*?)```/gi,n,r;for(;(r=t.exec(e))!==null;)n=r[1];if(n)return By(n.trim())}function mM(e){for(let t=e.length-1;t>=0;t--){if(e[t]!=="}")continue;let n=fM(e,t);if(n===-1)continue;let r=e.slice(n,t+1),o=By(r);if(o!==void 0)return o}}function fM(e,t){let n=0,r=!1,o=!1;for(let s=t;s>=0;s--){let i=e[s];if(o){o=!1;continue}if(r){if(i==="\\"){o=!0;continue}i==='"'&&(r=!1);continue}if(i==='"'){r=!0;continue}if(i==="}")n++;else if(i==="{"&&(n--,n===0))return s}return-1}function By(e){try{return JSON.parse(e)}catch{return}}function bd(){return{toolCalls:[],toolResults:[],thinkingPresent:!1,turnCount:0}}function Uy(e,t,n,r,o){if(!r)return{id:e,status:t,message:n,trace:o};let s=Ny(n.content),i=r.safeParse(s);return i.success?{id:e,status:t,message:n,output:i.data,trace:o}:{id:e,status:"failed",message:n,error:new Error(`structured output did not match schema: ${i.error.message}`,{cause:i.error}),schemaError:i.error,trace:o}}function jy(e,t,n,r){let o=n instanceof Error?n:new Error(String(n));return{id:e,status:t,error:o,trace:r}}function Ee(e){return`${e.status}${e.error?`: ${e.error.message}`:""}`}function Wy(e,t){let n=e;return t.partialOutput!==void 0&&t.partialOutput!==null&&(n.partialOutput=t.partialOutput),t.subagentId!==void 0&&(n.subagentId=t.subagentId),n}var Ta=class{constructor(t,n,r,o,s,i,a,l,c,u,d,p,f,g,h){this.id=t;this.session=n;this.controller=r;this.abortGraph=o;this.outputSchema=s;this.timeoutMs=i;this.hookRegistry=a;this.onTerminal=l;this.parentInputStreamRef=c;this.parentAbortSignal=u;this.agentType=d;this.traceWriter=g;this.onSubagentSucceeded=h;this.progressSink=p,this.parentId=f}id;session;controller;abortGraph;outputSchema;timeoutMs;hookRegistry;onTerminal;parentInputStreamRef;parentAbortSignal;agentType;traceWriter;onSubagentSucceeded;currentStatus="idle";inFlight=null;lastMessage;lastDurationMs;latestTerminalStatus;stopDispatched=!1;progressSink;parentId;currentTrace=bd();lastStreamedContent="";get status(){return this.currentStatus}async run(t,n){if(this.currentStatus==="running")throw new Error(`Subagent ${this.id} is already running`);if(this.currentStatus==="cancelled")throw new Error(`Subagent ${this.id} is cancelled`);this.currentStatus="running";let r=Date.now(),o=Qi(this.streamToFinalMessage(t,n),this.timeoutMs,{controller:this.controller,label:this.id});this.inFlight=o;try{let s=await o;this.lastMessage=s.content,this.lastDurationMs=Date.now()-r,this.currentStatus="succeeded",this.latestTerminalStatus="succeeded",er(this.traceWriter,{transition:"succeeded",subagentId:this.id,durationMs:this.lastDurationMs,turnCount:this.currentTrace.turnCount,outputBytes:Buffer.byteLength(this.lastMessage,"utf8")});let i=typeof s.metadata?.totalCostUsd=="number"?s.metadata.totalCostUsd:void 0;return this.onSubagentSucceeded?.(this.currentTrace.usage,i),this.onTerminal(),s}catch(s){throw this.lastDurationMs=Date.now()-r,this.currentStatus!=="cancelled"&&(this.controller.signal.aborted?(er(this.traceWriter,{transition:"cancelled",subagentId:this.id,source:"cascade"}),this.currentStatus="cancelled",this.latestTerminalStatus="cancelled"):(er(this.traceWriter,{transition:"failed",subagentId:this.id,errorClass:s instanceof Error?s.constructor.name:"Unknown",errorMessage:s instanceof Error?s.message:String(s),partialOutputBytes:Buffer.byteLength(this.lastStreamedContent,"utf8")}),this.currentStatus="failed",this.latestTerminalStatus="failed")),this.onTerminal(),s}finally{this.inFlight=null}}async streamToFinalMessage(t,n){let r,o;this.lastStreamedContent="",this.currentTrace=bd();let s=n??this.progressSink??gt(),i={subagentId:this.id,...this.parentId!==void 0&&{parentId:this.parentId},...this.agentType!==void 0&&{agentType:this.agentType}};for await(let a of this.session.sendMessageStream(t)){if(s&&s(a,i),a.type==="chunk"){let l=a.chunk;l.type==="content"?this.lastStreamedContent+=l.content:l.type==="tool_use_detail"?this.currentTrace.toolCalls.push({id:l.toolUseId,name:l.toolName,inputBytes:Buffer.byteLength(l.toolInput,"utf8")}):l.type==="tool_result"?this.currentTrace.toolResults.push({toolUseId:l.toolUseId,isError:l.isError,truncated:l.truncated,sizeBytes:l.sizeBytes}):l.type==="thinking"&&(this.currentTrace.thinkingPresent=!0)}if(a.type==="message")r=a.message,this.currentTrace.turnCount++;else if(a.type==="error"){o=a.error;break}else if(a.type==="done"){if(typeof a.metadata?.usage=="object"&&a.metadata.usage!==null){let l=a.metadata.usage;this.currentTrace.usage={inputTokens:typeof l.input_tokens=="number"?l.input_tokens:void 0,outputTokens:typeof l.output_tokens=="number"?l.output_tokens:void 0,cacheReadTokens:typeof l.cache_read_input_tokens=="number"?l.cache_read_input_tokens:void 0,cacheCreationTokens:typeof l.cache_creation_input_tokens=="number"?l.cache_creation_input_tokens:void 0}}break}}if(o)throw o;if(r)return r;if(this.lastStreamedContent.length>0)return{role:"assistant",content:this.lastStreamedContent,timestamp:new Date};throw new Error(`Subagent ${this.id} produced no terminal message`)}async runToResult(t,n){try{let r=await this.run(t,n);return Uy(this.id,this.currentStatus,r,this.outputSchema,this.currentTrace)}catch(r){let o=jy(this.id,this.currentStatus,r,this.currentTrace);return this.lastStreamedContent.length>0&&(o.partialOutput=this.lastStreamedContent),o}}runInBackground(t,n,r){let o;if(r){let s=this.progressSink??gt();o=(i,a)=>{r(i),s?.(i,a)}}this.runToResult(t,o).then(s=>{n?.(s)}).catch(s=>{J("runInBackground: unexpected rejection after runToResult",s),console.error("Subagent runInBackground failed unexpectedly:",s)})}async cancel(){if(this.currentStatus==="cancelled"||this.stopDispatched)return;let t=this.latestTerminalStatus??"cancelled";this.currentStatus="cancelled",er(this.traceWriter,{transition:"cancelled",subagentId:this.id,source:"explicit"});try{this.abortGraph.abort(this.id,"cancelled")}catch{}try{this.inFlight&&await this.session.interrupt()}catch{}try{await this.session.close()}finally{await this.dispatchStopAndRelease(t)}}async teardown(){if(this.stopDispatched)return;let t=this.latestTerminalStatus??"cancelled";try{this.inFlight&&await this.session.interrupt()}catch{}try{await this.session.close()}finally{await this.dispatchStopAndRelease(t)}}async dispatchStopAndRelease(t){if(this.stopDispatched){this.onTerminal();return}this.stopDispatched=!0;let n=await Dg(this.hookRegistry,{event:"SubagentStop",subagentId:this.id,status:t,lastMessage:this.lastMessage,agentType:this.agentType,durationMs:this.lastDurationMs,trace:this.currentTrace},this.traceWriter?{traceWriter:this.traceWriter}:{});if(n.injectContext&&this.parentInputStreamRef)if(this.parentAbortSignal?.aborted)J(`Skipping SubagentStop injectContext for ${this.id}: parent is aborted`);else try{this.parentInputStreamRef.pushUserMessage(n.injectContext)}catch(r){J(`Failed to inject context from SubagentStop handler: ${String(r)}`)}this.onTerminal()}};var gM=async(e,t)=>({action:"decline"}),X=class{active=new Map;parentCanUseTool;hookRegistry;progressSink;parentApiKey;parentBaseUrl;parentCwd;abortGraph;rootId;rootController;counter=0;onSubagentSucceededCb;constructor(t={}){if(this.parentCanUseTool=t.canUseTool,this.hookRegistry=t.hookRegistry,this.progressSink=t.progressSink,this.parentApiKey=t.apiKey,this.parentBaseUrl=t.baseUrl,this.parentCwd=t.cwd,this.onSubagentSucceededCb=t.onSubagentSucceeded,this.abortGraph=new Xi(t.traceWriter),this.rootId=`manager-root-${Date.now()}-${Math.random().toString(36).slice(2,8)}`,this.rootController=new AbortController,this.abortGraph.register(this.rootId,this.rootController),t.parentAbortSignal){let n=t.parentAbortSignal;n.aborted?this.rootController.abort(n.reason):n.addEventListener("abort",()=>{this.rootController.signal.aborted||this.rootController.abort(n.reason)},{once:!0})}}list(){return[...this.active.values()].map(t=>({id:t.id,status:t.status}))}get(t){return this.active.get(t)}onChildAborted(t){return this.abortGraph.onChildAborted(this.rootId,t)}setOnSubagentSucceeded(t){this.onSubagentSucceededCb=t}abortAll(t,n="user_signal"){this.abortGraph.abort(this.rootId,t,n)}async forkSubagent(t){if(t.phaseRole!==void 0&&t.phaseRole!=="read-write"&&t.config.provider!==void 0)throw new Error(`SubagentManager.forkSubagent: phaseRole "${t.phaseRole}" is mutually exclusive with config.provider. Remove one \u2014 either let the manager construct the phase-restricted provider, or use config.provider with phaseRole: "read-write" (default).`);let n=`${t.idPrefix??"subagent"}-${Date.now()}-${++this.counter}`,r=t.parent.sessionId,o=t.config.hookRegistry??this.hookRegistry??t.parent.hookRegistry;o&&await $g(o,{event:"SubagentStart",subagentId:n,parentSessionId:t.parent.sessionId},{signal:this.rootController.signal,...t.config.traceWriter?{traceWriter:t.config.traceWriter}:{}});let s=new AbortController;this.abortGraph.register(n,s),this.abortGraph.linkChild(this.rootId,n);let i={...t.config,resume:r,forkSession:r?!0:t.config.forkSession,abortSignal:s.signal,apiKey:t.config.apiKey||this.parentApiKey,baseUrl:t.config.baseUrl??this.parentBaseUrl,...t.config.parentSessionId===void 0&&t.parent.sessionId!==void 0?{parentSessionId:t.parent.sessionId}:{},...t.config.phaseRole===void 0&&t.phaseRole!==void 0?{phaseRole:t.phaseRole}:{},...t.config.cwd===void 0&&this.parentCwd!==void 0?{cwd:this.parentCwd}:{},hookRegistry:o,permissionBubbler:t.config.permissionBubbler??(this.parentCanUseTool!==void 0&&t.config.canUseTool===void 0?{canUseTool:this.parentCanUseTool}:void 0),...t.denyElicitations===!0?{onElicitation:gM}:{},...t.phaseRole==="read-only"?{provider:Fy("read-only",t.config.model)}:{}},a;try{a=new Ve(i)}catch(h){throw this.abortGraph.dispose(n),h}let l=t.parent.getInputStreamRef?.(),c=t.parent.abortSignal,u=this.progressSink??gt(),d=t.agentType?.trim()||void 0,p=t.parentId?.trim()||void 0,f=new Ta(n,a,s,this.abortGraph,t.outputSchema,t.config.timeoutMs??Zi,o,()=>{this.active.delete(n),this.abortGraph.dispose(n)},l,c,d??t.idPrefix,u,p??t.parent.sessionId,t.config.traceWriter,this.onSubagentSucceededCb);this.active.set(n,f);let g=typeof t.config.model=="string"?t.config.model:JSON.stringify(t.config.model);return er(t.config.traceWriter,{transition:"started",subagentId:n,parentId:t.parent.sessionId??this.rootId,model:g,...i.tools?.allowedTools?{allowedTools:[...i.tools.allowedTools]}:{}}),await Te({event:"subagent.dispatched",subagent_id:n,id_prefix:t.idPrefix,model:g,parent_session_id:t.parent.sessionId}),f}async kill(t){let n=this.active.get(t);return n?(await n.cancel(),!0):!1}async killAll(){await Promise.allSettled([...this.active.values()].map(t=>t.cancel()))}async teardownAll(){await Promise.allSettled([...this.active.values()].map(t=>t.teardown()))}};async function xa(e,t={}){let{failFast:n=!0,teardown:r=!0}=t;if(e.length===0)return[];let o=new Array(e.length),s=new Set(e.map((a,l)=>l)),i=e.map((a,l)=>a.handle.runToResult(a.prompt).then(c=>{if(o[l]=c,s.delete(l),n&&c.status!=="succeeded")for(let u of s){let d=e[u];d&&d.handle.status==="running"&&d.handle.cancel().catch(()=>{})}}));return await Promise.all(i),r&&await Promise.allSettled(e.map(a=>a.handle.teardown())),o}import{fileURLToPath as hM}from"node:url";import{dirname as yM}from"node:path";var bM=hM(import.meta.url),H6=yM(bM),at={name:"research-agent",systemPrompt:`---
1203
1203
  name: research-agent
1204
1204
  description: Read-only sub-agent for research, validation, verification, and codebase inspection. Mechanically locked to Read, Grep, Glob, WebFetch, WebSearch \u2014 cannot Edit, Write, Bash, commit, or push. Delegates git queries to \`git-investigator\`. Use when the dispatched task is findings-only.
1205
1205
  model: sonnet
@@ -1255,8 +1255,8 @@ If \`scope_check\` flags implementation (non-git), the orchestrator should dispa
1255
1255
  `,sourcePath:"agent-framework-private/agents/research-agent.md",allowedTools:["Read","Grep","Glob","WebFetch","WebSearch"],description:"Read-only sub-agent for research, validation, verification, and codebase inspection. Mechanically locked to Read, Grep, Glob, WebFetch, WebSearch \u2014 cannot Edit, Write, Bash, commit, or push. Delegates git queries to `git-investigator`. Use when the dispatched task is findings-only."};W();W();import{existsSync as Dn,readdirSync as PM,readFileSync as MM}from"fs";import{join as cn}from"path";W();import{existsSync as Sd,readFileSync as RM,readdirSync as AM,statSync as _M}from"fs";import{join as Ps,resolve as zy}from"path";W();import{existsSync as Ky,mkdirSync as wM,readFileSync as SM,renameSync as kM,writeFileSync as vM,unlinkSync as TM}from"fs";import{dirname as Hy,join as xM}from"path";import{randomBytes as EM}from"crypto";function fe(e=ie()){if(!Ky(e))return Ea();try{let t=SM(e,"utf8"),n=JSON.parse(t);if(!n||typeof n!="object")return Ea();let r=n,o=r.plugins&&typeof r.plugins=="object"?r.plugins:{};if(r.version===1)return{version:2,plugins:o,marketplaces:{}};if(r.version===2){let s=r.marketplaces&&typeof r.marketplaces=="object"?r.marketplaces:{};return{version:2,plugins:o,marketplaces:s}}return Ea()}catch{return Ea()}}function Cs(e,t=ie()){wM(Hy(t),{recursive:!0});let n=xM(Hy(t),`.index.json.${process.pid}.${EM(4).toString("hex")}.tmp`),r=JSON.stringify(e,null,2);try{vM(n,r,"utf8"),kM(n,t)}catch(o){try{Ky(n)&&TM(n)}catch{}throw o}}function $n(e,t,n=ie()){let r=fe(n);return r.plugins[e]=t,Cs(r,n),r}function Gy(e,t=ie()){let n=fe(t);return e in n.plugins&&(delete n.plugins[e],Cs(n,t)),n}function wd(e,t,n=ie()){let r=fe(n),o=r.plugins[e];if(!o)throw new Error(`plugin "${e}" is not in the index`);return o.enabled=t,o.updatedAt=new Date().toISOString(),Cs(r,n),r}function Is(e,t,n=ie()){let r=fe(n);return r.marketplaces[e]=t,Cs(r,n),r}function qy(e,t=ie()){let n=fe(t),r=!1;e in n.marketplaces&&(delete n.marketplaces[e],r=!0);for(let[o,s]of Object.entries(n.plugins))s.marketplace===e&&(delete n.plugins[o],r=!0);return r&&Cs(n,t),n}function Ea(){return{version:2,plugins:{},marketplaces:{}}}var CM=5,Jy="cache",so;function ln(){so=void 0}function At(e=Oe()){so||(so=new Map);let t=so.get(e);if(t)return[...t];if(!Sd(e))return so.set(e,[]),[];let n=e===Oe()?ie():Ps(e,".index.json"),r=fe(n),o=[];return Vy(e,e,0,o,new Set,r.plugins),so.set(e,o),[...o]}function Vy(e,t,n,r,o,s){if(n>CM||o.has(t))return;if(o.add(t),Sd(Ps(t,".claude-plugin","plugin.json"))){let a=kd(e,t);if(a===null){r.push({type:"local",path:t});return}if(a.layout==="cache"){let c=s[a.key];if(!c||c.enabled===!1)return;r.push({type:"local",path:t});return}let l=s[a.key];if(l&&l.enabled===!1)return;r.push({type:"local",path:t});return}let i;try{i=AM(t)}catch{return}for(let a of i){if(a.startsWith("."))continue;let l=Ps(t,a),c;try{c=_M(l)}catch{continue}c.isDirectory()&&Vy(e,l,n+1,r,o,s)}}function kd(e,t){if(!t.startsWith(e))return null;let n=t.slice(e.length).replace(/^\/+/,"");if(!n)return null;let r=n.split("/").filter(s=>s.length>0);if(r.length===0)return null;if(r[0]===Jy&&r.length>=3){let s=r[1];if(s){let i=Ps(e,Jy,s),l=IM(i,t)??r[2];if(l)return{layout:"cache",key:`${s}:${l}`}}}let o=r[0];return o?{layout:"flat",key:o}:null}function IM(e,t){let n=Ps(e,".claude-plugin","marketplace.json");if(!Sd(n))return null;let r;try{r=JSON.parse(RM(n,"utf8"))}catch{return null}if(!r||typeof r!="object")return null;let o=r.plugins;if(!Array.isArray(o))return null;let s=zy(t);for(let i of o){if(!i||typeof i!="object")continue;let a=i;if(!(typeof a.name!="string"||typeof a.source!="string")&&!(!a.source.startsWith("./")&&!a.source.startsWith("../"))&&zy(e,a.source)===s)return a.name}return null}var Yy=["command","agent"];function Xy(e=Ce()){let t=[],n=cn(e,"skills");if(Dn(n))for(let r of Ra(n)){let o=cn(n,r,"SKILL.md");Dn(o)&&t.push({path:o,type:"skill",source:"user"})}for(let r of Yy){let o=cn(e,`${r}s`);if(Dn(o))for(let s of Ra(o))s.endsWith(".md")&&t.push({path:cn(o,s),type:r,source:"user"})}return t}function Zy(e=Oe()){if(!Dn(e))return[];let t=[],n=At(e);for(let r of n){let s=kd(e,r.path)?.key,i=cn(r.path,"skills");if(Dn(i))for(let a of Ra(i)){let l=cn(i,a,"SKILL.md");if(!Dn(l))continue;let c={path:l,type:"skill",source:"plugin"};s&&(c.plugin_key=s),t.push(c)}for(let a of Yy){let l=cn(r.path,`${a}s`);if(Dn(l))for(let c of Ra(l)){if(!c.endsWith(".md"))continue;let u={path:cn(l,c),type:a,source:"plugin"};s&&(u.plugin_key=s),t.push(u)}}}return t}function Qy(e=cn(Ce(),"settings.json")){if(!Dn(e))return[];try{let t=MM(e,"utf8"),r=JSON.parse(t).hooks;if(!r||typeof r!="object")return[];let o=[];for(let[s,i]of Object.entries(r))if(Array.isArray(i))for(let a=0;a<i.length;a++)o.push({event:s,index:a,raw:i[a]});return o}catch{return[]}}function Ra(e){try{return PM(e).filter(t=>!t.startsWith("."))}catch{return[]}}var rb=ke.object({path:ke.string(),type:ke.enum(["skill","command","agent","hook"]),source:ke.enum(["user","plugin"]),plugin_key:ke.string().optional(),verdict:ke.enum(["correct","misfit","outlier"]),recommended_type:ke.string(),rationale:ke.string(),confidence:ke.enum(["high","med","low"])}),nb=ke.record(ke.string(),ke.record(ke.string(),ke.number())),hX=ke.object({inventory:ke.object({user:nb,plugin:nb}),misfits:ke.array(rb),briefs_written:ke.number(),total_artifacts:ke.number()}),OM=ke.object({writeBriefs:ke.boolean().optional(),scope:ke.enum(["user","plugin","all"]).optional()}),$M=["skill","command","agent"],ob=["skill","command","agent","hook"];function DM(e){return{runUserDiscovery:e!=="plugin",runPluginDiscovery:e!=="user",runHookInspector:e!=="plugin"}}function LM(e){let t=()=>{let s={};for(let i of ob)s[i]={correct:0,misfit:0,outlier:0};return s},n={user:t(),plugin:t()};for(let s of e)n[s.source][s.type][s.verdict]+=1;let r={high:0,med:1,low:2},o=e.filter(s=>s.verdict==="misfit").slice().sort((s,i)=>r[s.confidence]-r[i.confidence]);return{inventory:n,misfits:o}}function FM(e){return e.verdict==="misfit"&&e.confidence==="high"&&e.source==="user"}function NM(e){let t=e.filter(o=>o.source==="user"),n=e.filter(o=>o.source==="plugin"),r=["","## Discovered artifacts (audit only these)",""];if(r.push('### User-scope artifacts (set `"source": "user"`, omit `plugin_key`)'),t.length===0)r.push("(none discovered)");else for(let o of t)r.push(`- ${o.path}`);if(r.push(""),r.push('### Plugin-scope artifacts (set `"source": "plugin"`, copy `plugin_key` from each entry)'),n.length===0)r.push("(none discovered)");else for(let o of n){let s=o.plugin_key??"<unknown>";r.push(`- ${o.path} (plugin_key: ${s})`)}return r.join(`
1256
1256
  `)}function BM(e,t){let n=["","## Discovered hooks (audit only these)",""];if(n.push(`Settings file (use this absolute path verbatim in each verdict's \`path\` field): \`${e}\``),n.push(""),t.length===0)return n.push("(no hooks discovered)"),n.join(`
1257
1257
  `);for(let r of t){let o=`${r.event}-${r.index}`;n.push(`### Hook \`${o}\``),n.push(""),n.push("```json"),n.push(JSON.stringify(r.raw,null,2)),n.push("```"),n.push("")}return n.join(`
1258
- `)}function UM(e,t){if(!t)return{kind:"failure",message:`${e}: no result`};if(t.schemaError)return{kind:"failure",message:`${e}: schema mismatch \u2014 ${t.schemaError.message}`};if(t.status!=="succeeded"){let n=t.error?` \u2014 ${t.error.message}`:"";return{kind:"failure",message:`${e}: ${t.status}${n}`}}return t.output?{kind:"success",output:t.output}:{kind:"failure",message:`${e}: no output`}}async function jM(e,t,n){let r=n?.apiKey,o=n?.callId,s=typeof e=="object"&&e!==null?e:{},i=OM.parse(s),a=i.writeBriefs??!0,l=i.scope??"all",c=DM(l);if(!t?.sessionId)throw new Error("audit-fit requires a parent session with sessionId");let u=t.sessionId,d=we("audit-fit"),m={skill:d["01-skill-inspector.md"],command:d["02-command-inspector.md"],agent:d["03-agent-inspector.md"],hook:d["04-hook-inspector.md"]};for(let $ of ob)if(!m[$])throw new Error(`audit-fit skill missing inspector prompt for ${$}`);let f=c.runUserDiscovery?Xy():[],g=c.runPluginDiscovery?Zy():[],h={skill:[],command:[],agent:[]};for(let $ of[...f,...g])h[$.type].push($);let b=new X({apiKey:r}),y=()=>async $=>at.allowedTools.includes($)?{behavior:"allow"}:{behavior:"deny",message:`Tool ${$} not allowed for audit-fit inspectors. Allowed tools: ${at.allowedTools.join(", ")}`},w=[];for(let $ of $M){let F=h[$];if(F.length===0)continue;let k=m[$];k&&w.push({type:$,prompt:`${k}
1259
- ${NM(F)}`,artifacts:F,runPrompt:`Inspect every ${$} listed in the artifact section.`})}if(c.runHookInspector){let $=m.hook;if($){let F=vd(Ce(),"settings.json"),k=Qy(F);w.push({type:"hook",prompt:`${$}
1258
+ `)}function UM(e,t){if(!t)return{kind:"failure",message:`${e}: no result`};if(t.schemaError)return{kind:"failure",message:`${e}: schema mismatch \u2014 ${t.schemaError.message}`};if(t.status!=="succeeded"){let n=t.error?` \u2014 ${t.error.message}`:"";return{kind:"failure",message:`${e}: ${t.status}${n}`}}return t.output?{kind:"success",output:t.output}:{kind:"failure",message:`${e}: no output`}}async function jM(e,t,n){let r=n?.apiKey,o=n?.callId,s=typeof e=="object"&&e!==null?e:{},i=OM.parse(s),a=i.writeBriefs??!0,l=i.scope??"all",c=DM(l);if(!t?.sessionId)throw new Error("audit-fit requires a parent session with sessionId");let u=t.sessionId,d=we("audit-fit"),p={skill:d["01-skill-inspector.md"],command:d["02-command-inspector.md"],agent:d["03-agent-inspector.md"],hook:d["04-hook-inspector.md"]};for(let $ of ob)if(!p[$])throw new Error(`audit-fit skill missing inspector prompt for ${$}`);let f=c.runUserDiscovery?Xy():[],g=c.runPluginDiscovery?Zy():[],h={skill:[],command:[],agent:[]};for(let $ of[...f,...g])h[$.type].push($);let b=new X({apiKey:r}),y=()=>async $=>at.allowedTools.includes($)?{behavior:"allow"}:{behavior:"deny",message:`Tool ${$} not allowed for audit-fit inspectors. Allowed tools: ${at.allowedTools.join(", ")}`},w=[];for(let $ of $M){let F=h[$];if(F.length===0)continue;let k=p[$];k&&w.push({type:$,prompt:`${k}
1259
+ ${NM(F)}`,artifacts:F,runPrompt:`Inspect every ${$} listed in the artifact section.`})}if(c.runHookInspector){let $=p.hook;if($){let F=vd(Ce(),"settings.json"),k=Qy(F);w.push({type:"hook",prompt:`${$}
1260
1260
  ${BM(F,k)}`,artifacts:[],runPrompt:`Inspect every hook listed in the Discovered hooks section. Settings file: ${F}.`})}}let x=[];if(w.length>0){let $=await Promise.all(w.map(A=>b.forkSubagent({parent:{sessionId:u},config:{model:"sonnet",systemPrompt:`${at.systemPrompt}
1261
1261
 
1262
1262
  ${A.prompt}`,canUseTool:y()},idPrefix:`inspector-${A.type}`,outputSchema:ke.array(rb),...o?{parentId:o}:{}}))),F=await xa(w.map((A,N)=>{let H=$[N];if(!H)throw new Error(`audit-fit: missing handle for ${A.type} inspector`);return{handle:H,prompt:A.runPrompt}}),{failFast:!1}),k=[];for(let A=0;A<F.length;A++){let N=F[A],H=w[A];if(!H)continue;let C=UM(H.type,N);if(C.kind==="failure"){k.push(C.message);continue}let B=new Map;for(let j of H.artifacts)B.set(j.path,j.source);for(let j of C.output){if(H.type==="hook"){if(j.source!=="user"){k.push(`${H.type}: hook verdict has source=${j.source} (must be 'user')`);continue}}else{let ee=B.get(j.path);if(ee===void 0){k.push(`${H.type}: verdict for unknown path ${j.path} (not in discovered list)`);continue}if(j.source!==ee){k.push(`${H.type}: verdict source mismatch for ${j.path} (expected ${ee}, got ${j.source})`);continue}}x.push(j)}}if(k.length>0){let A=k.map(N=>` - ${N}`).join(`
@@ -1284,7 +1284,7 @@ ${F.rationale}
1284
1284
  ---
1285
1285
  Generated by audit-fit on ${new Date().toISOString().split(".")[0]}Z
1286
1286
  `;await tb(A,N),E++}}let I=Tt();await eb(I,{recursive:!0});let D=$=>{let F=0;for(let k of Object.values($))for(let A of Object.values(k))F+=A;return F},_=$=>{let F=R.user[$]??{},k=R.plugin[$]??{},A=N=>Object.values(N).reduce((H,C)=>H+C,0);return A(F)+A(k)},M={timestamp:new Date().toISOString(),surface:"afk",scope:l,total_artifacts:x.length,misfits_count:v.length,briefs_written:E,by_source:{user:D(R.user),plugin:D(R.plugin)},by_type:{skill:_("skill"),command:_("command"),agent:_("agent"),hook:_("hook")}},P=vd(I,"audit-fit-telemetry.jsonl");return await tb(P,JSON.stringify(M)+`
1287
- `),{inventory:R,misfits:v,briefs_written:E,total_artifacts:x.length}}var WM={name:"audit-fit",description:"Audit ~/.afk artifacts (skills, commands, agents, hooks) for correct type categorization. Walks user-scope dirs (~/.afk/{skills,commands,agents}/) and every plugin installed under ~/.afk/plugins/ (flat and marketplace-cache layouts), plus ~/.afk/settings.json for hooks. Dispatches per-type inspectors in parallel, applies decision heuristics (progressive-disclosure value, isolation need, deterministic vs. reasoning), flags misfits. Generates migration briefs only for user-scope misfits (plugin misfits are inventory-only \u2014 refactoring vendored plugin code is the maintainer's job). Optional `scope` input filters to `user`, `plugin`, or `all` (default). Use for inventory audits after bulk authoring, imports, or periodic hygiene.",handler:jM,argumentHint:"[--write-briefs]",whenToUse:"When the user wants ~/.afk artifacts (skills, commands, agents, hooks) audited for correct type categorization.",flags:["--write-briefs"],audience:"internal"};tt(WM);import{z as V}from"zod";import{execFile as qM}from"node:child_process";import{promisify as zM}from"node:util";import{tmpdir as JM}from"node:os";import{join as ib}from"node:path";function sb(e){return e.confidence<.5?{verify:!0,reason:`low confidence (${e.confidence.toFixed(2)} < ${.5})`}:e.boundary_flag&&e.boundary_flag.length>0?{verify:!0,reason:`boundary flag set: ${e.boundary_flag}`}:e.coverage_gaps&&e.coverage_gaps.length>0?{verify:!0,reason:`coverage gap${e.coverage_gaps.length===1?"":"s"}: ${e.coverage_gaps.length} unresolved`}:{verify:!1,reason:`confidence ${e.confidence.toFixed(2)} with no gaps or boundary`}}import{fileURLToPath as HM}from"node:url";import{dirname as KM}from"node:path";var GM=HM(import.meta.url),vX=KM(GM),Td={name:"git-investigator",systemPrompt:'---\nname: git-investigator\ndescription: Read-only git specialist. Dispatched by research-agent (or any research-shaped caller) when a finding requires git history, reflog, diff, blame, branch/remote state, or merge-base analysis. Runs git commands only \u2014 no mutations, no shell escapes.\nmodel: sonnet\ntools: Bash, Read, Grep, Glob\n---\n\nYou are `git-investigator`, a leaf sub-agent specialized for read-only git queries.\n\nYou have Bash, Read, Grep, and Glob. You do not dispatch other sub-agents. You do not Edit or Write. Your Bash surface is restricted **by this prompt** to `git ...` invocations and benign output-shaping pipes.\n\n## Allowed commands\n\nRead-only git only:\n\n- `git status`, `git log`, `git diff`, `git show`\n- `git rev-parse`, `git rev-list`, `git reflog`\n- `git branch -v / -vv / -a` (list only)\n- `git remote -v`, `git ls-remote`\n- `git ls-files`, `git blame`\n- `git merge-base`, `git for-each-ref`, `git describe`\n- `git cat-file`, `git shortlog`\n- `git tag` (list/show only)\n- `git stash list`, `git stash show`\n- `git config --get`, `git config --get-all`, `git config --list`\n- `git worktree list` (read only)\n\nOutput-shaping pipes are fine: `| head`, `| tail`, `| wc`, `| grep`, `| jq`, `| awk \'NR==...\'` (for formatting only \u2014 no mutations).\n\n## Forbidden\n\nAnything that mutates repo or working tree state:\n\n- `commit`, `push`, `pull`, `fetch --prune`\n- `reset`, `revert`, `rebase`, `merge`, `cherry-pick`\n- `checkout` (except `checkout -- <path>` file-restore, and even that is mutation \u2014 avoid it, just report the need)\n- `restore`, `switch`\n- `branch -d / -D / -m / -M`, `branch <new>`\n- `stash push / pop / drop / apply / clear`\n- `tag -d`, creating a new tag\n- `remote add / remove / set-url`\n- `config --set`, `config --unset`\n- `gc`, `fsck`, `prune`, `reflog delete`, `reflog expire`\n- `filter-branch`, `filter-repo`\n- `worktree add / remove / move`\n- `hooks install`, `submodule add / update`\n- Any non-`git` command that mutates: `rm`, `mv`, `cp` (writes), `sed -i`, `> file`, `>> file`, `tee`, `curl`, `wget`, `pip install`, shell builtins that change state.\n\nIf the caller asks for any of the above, do not run it. Return `scope_check: "requires mutation: <reason>"` and stop.\n\n## Behavior\n\n- Run the minimum set of commands needed. Prefer `git log -n 5 --oneline -- <path>` over `git log -- <path>` when a count is fine.\n- Cite concrete evidence: commit SHAs (short form OK), ref names, `path:line` references from blame, diff hunks trimmed to the relevant range.\n- Use `Read`/`Grep`/`Glob` for follow-up inspection of files the git output identifies (e.g., `git show SHA:path | head` then `Read` the current file to diff mentally).\n- Do not speculate beyond what the commands show. If a question needs history the commands don\'t surface (deleted-file recovery, ancient reflog that has expired), say so in `caveats`.\n- Keep output compact \u2014 dispatchers merge your findings into a larger response. No preamble, no ceremony.\n\n## Return shape\n\n```\n{\n "findings": "<summary of what the git data shows>",\n "evidence": ["<SHA>", "<ref>", "<path:line>", ...],\n "git_commands_run": ["git log ...", "git diff ...", ...],\n "caveats": "<gaps, ambiguity, or \'none\'>",\n "scope_check": "pure git research" | "requires mutation: <reason>"\n}\n```\n\nBegin your response with the first schema field. No preamble.\n',sourcePath:"agent-framework-private/agents/git-investigator.md",allowedTools:["Bash","Read","Grep","Glob"],description:"Read-only git specialist. Dispatched by research-agent (or any research-shaped caller) when a finding requires git history, reflog, diff, blame, branch/remote state, or merge-base analysis. Runs git commands only \u2014 no mutations, no shell escapes.",model:"sonnet"};function xd(e){let t={description:e.description,prompt:e.systemPrompt};return e.allowedTools&&(t.tools=[...e.allowedTools]),e.model&&(t.model=e.model),t}function VM(e){let t=e.trim().toUpperCase();return["VERIFIED","CONFIRMED","CONFIRM","SUPPORTED","TRUE","PASS","PASSED"].includes(t)?"VERIFIED":["REFUTED","REFUTE","DISAGREE","DISAGREED","CONTRADICTED","FALSE","FAIL","FAILED"].includes(t)?"REFUTED":"INCONCLUSIVE"}var YM=V.object({verifications:V.array(V.object({claim:V.string().optional(),verdict:V.string(),evidence:V.string().optional()}))});function XM(e){let t=[],n=0,r=-1,o=!1,s=!1;for(let i=0;i<e.length;i++){let a=e[i];if(o){s?s=!1:a==="\\"?s=!0:a==='"'&&(o=!1);continue}a==='"'?o=!0:a==="{"?(n===0&&(r=i),n++):a==="}"&&n>0&&(n--,n===0&&r!==-1&&(t.push(e.slice(r,i+1)),r=-1))}return t}function ZM(e){let t=XM(e);for(let n of t){let r;try{r=JSON.parse(n)}catch{continue}let o=YM.safeParse(r);if(o.success)return o.data.verifications.map(s=>({claim:s.claim??"",verdict:VM(s.verdict),evidence:s.evidence??""}))}throw new Error(`shadow-verify did not return a parseable {"verifications":[...]} envelope (${t.length} JSON-like span(s) found, none matched the schema); raw output (first 300 chars): ${e.slice(0,300)}`)}var Aa=zM(qM),cb=V.object({id:V.string(),claim:V.string(),confidence:V.number().min(0).max(1),evidence_sources:V.array(V.string()),location:V.string().optional(),proposed_fix:V.string().optional(),coverage_gaps:V.array(V.string()).nullish().transform(e=>e??void 0),boundary_flag:V.string().nullish().transform(e=>e??void 0)}),QM=V.object({hypothesis_id:V.string(),claim:V.string(),verdict:V.enum(["VERIFIED","REFUTED","INCONCLUSIVE"]),evidence:V.string(),gate_reason:V.string()}),ub=V.object({hypothesis_id:V.string(),reproducer_passed:V.boolean(),regressions:V.array(V.string()),confidence:V.number().min(0).max(1),verification_log:V.string()}),eO=V.enum(["crash","regression","logic-error","flaky","environment","unknown"]),tO=V.object({failure_type:eO,error_signature:V.string(),affected_area:V.string()}),nO=V.enum(["clear_winner","multiple_plausible","dissent","all_inconclusive","no_hypotheses"]),BX=V.object({reproducer:V.string().optional(),triage:tO.optional(),hypotheses:V.array(cb),premise_verifications:V.array(QM).optional(),winner:V.object({hypothesis_id:V.string(),verification_log:V.string(),proposed_fix:V.string()}).optional(),verification_results:V.array(ub).optional(),outcome:nO.optional(),recommended_next_skill:V.enum(["spec"]).optional()});async function rO(e,t){let n=e.map(l=>({hypothesis:l,decision:sb(l)})).filter(l=>l.decision.verify);if(n.length===0)return{premise_verifications:[],hypotheses_to_test:e};let r=[],o;try{let l=await t(n.map(c=>c.hypothesis.claim));r=Array.isArray(l)?l:[]}catch(l){o=l instanceof Error?l.message:String(l)}let s=n.map((l,c)=>{let u=r[c];return o!==void 0?{hypothesis_id:l.hypothesis.id,claim:l.hypothesis.claim,verdict:"INCONCLUSIVE",evidence:`shadow-verify dispatch failed: ${o}`,gate_reason:l.decision.reason}:u?{hypothesis_id:l.hypothesis.id,claim:l.hypothesis.claim,verdict:u.verdict,evidence:u.evidence,gate_reason:l.decision.reason}:{hypothesis_id:l.hypothesis.id,claim:l.hypothesis.claim,verdict:"INCONCLUSIVE",evidence:"no verifier result for this claim",gate_reason:l.decision.reason}}),i=new Set(s.filter(l=>l.verdict==="REFUTED").map(l=>l.hypothesis_id)),a=i.size===0?e:e.filter(l=>!i.has(l.id));return{premise_verifications:s,hypotheses_to_test:a}}async function oO(e,t,n){let r=n?.apiKey,o=(()=>{if(typeof e=="string")return{failure:e,repoPath:process.cwd(),context:"",maxHypotheses:4};if(typeof e=="object"&&e!==null){let q=e;if(typeof q.failure=="string")return{failure:q.failure,repoPath:q.repoPath||process.cwd(),context:q.context||"",maxHypotheses:Math.min(q.maxHypotheses||4,4)}}throw new Error("diagnose handler requires input.failure (string) or a string argument")})();if(!t?.sessionId)throw new Error("diagnose requires a parent session with sessionId");let s=t.sessionId,i=we("diagnose"),a=i["system.md"],l=i["research.md"],c=i["hypothesis.md"],u=i["verify.md"];if(!a||!l||!c||!u)throw new Error("diagnose skill missing required prompts (system.md, research.md, hypothesis.md, verify.md)");let d=new X({apiKey:r}),m=lO(o.context),f=sO(o.failure,o.context),g=`Triage:
1287
+ `),{inventory:R,misfits:v,briefs_written:E,total_artifacts:x.length}}var WM={name:"audit-fit",description:"Audit ~/.afk artifacts (skills, commands, agents, hooks) for correct type categorization. Walks user-scope dirs (~/.afk/{skills,commands,agents}/) and every plugin installed under ~/.afk/plugins/ (flat and marketplace-cache layouts), plus ~/.afk/settings.json for hooks. Dispatches per-type inspectors in parallel, applies decision heuristics (progressive-disclosure value, isolation need, deterministic vs. reasoning), flags misfits. Generates migration briefs only for user-scope misfits (plugin misfits are inventory-only \u2014 refactoring vendored plugin code is the maintainer's job). Optional `scope` input filters to `user`, `plugin`, or `all` (default). Use for inventory audits after bulk authoring, imports, or periodic hygiene.",handler:jM,argumentHint:"[--write-briefs]",whenToUse:"When the user wants ~/.afk artifacts (skills, commands, agents, hooks) audited for correct type categorization.",flags:["--write-briefs"],audience:"internal"};tt(WM);import{z as V}from"zod";import{execFile as qM}from"node:child_process";import{promisify as zM}from"node:util";import{tmpdir as JM}from"node:os";import{join as ib}from"node:path";function sb(e){return e.confidence<.5?{verify:!0,reason:`low confidence (${e.confidence.toFixed(2)} < ${.5})`}:e.boundary_flag&&e.boundary_flag.length>0?{verify:!0,reason:`boundary flag set: ${e.boundary_flag}`}:e.coverage_gaps&&e.coverage_gaps.length>0?{verify:!0,reason:`coverage gap${e.coverage_gaps.length===1?"":"s"}: ${e.coverage_gaps.length} unresolved`}:{verify:!1,reason:`confidence ${e.confidence.toFixed(2)} with no gaps or boundary`}}import{fileURLToPath as HM}from"node:url";import{dirname as KM}from"node:path";var GM=HM(import.meta.url),vX=KM(GM),Td={name:"git-investigator",systemPrompt:'---\nname: git-investigator\ndescription: Read-only git specialist. Dispatched by research-agent (or any research-shaped caller) when a finding requires git history, reflog, diff, blame, branch/remote state, or merge-base analysis. Runs git commands only \u2014 no mutations, no shell escapes.\nmodel: sonnet\ntools: Bash, Read, Grep, Glob\n---\n\nYou are `git-investigator`, a leaf sub-agent specialized for read-only git queries.\n\nYou have Bash, Read, Grep, and Glob. You do not dispatch other sub-agents. You do not Edit or Write. Your Bash surface is restricted **by this prompt** to `git ...` invocations and benign output-shaping pipes.\n\n## Allowed commands\n\nRead-only git only:\n\n- `git status`, `git log`, `git diff`, `git show`\n- `git rev-parse`, `git rev-list`, `git reflog`\n- `git branch -v / -vv / -a` (list only)\n- `git remote -v`, `git ls-remote`\n- `git ls-files`, `git blame`\n- `git merge-base`, `git for-each-ref`, `git describe`\n- `git cat-file`, `git shortlog`\n- `git tag` (list/show only)\n- `git stash list`, `git stash show`\n- `git config --get`, `git config --get-all`, `git config --list`\n- `git worktree list` (read only)\n\nOutput-shaping pipes are fine: `| head`, `| tail`, `| wc`, `| grep`, `| jq`, `| awk \'NR==...\'` (for formatting only \u2014 no mutations).\n\n## Forbidden\n\nAnything that mutates repo or working tree state:\n\n- `commit`, `push`, `pull`, `fetch --prune`\n- `reset`, `revert`, `rebase`, `merge`, `cherry-pick`\n- `checkout` (except `checkout -- <path>` file-restore, and even that is mutation \u2014 avoid it, just report the need)\n- `restore`, `switch`\n- `branch -d / -D / -m / -M`, `branch <new>`\n- `stash push / pop / drop / apply / clear`\n- `tag -d`, creating a new tag\n- `remote add / remove / set-url`\n- `config --set`, `config --unset`\n- `gc`, `fsck`, `prune`, `reflog delete`, `reflog expire`\n- `filter-branch`, `filter-repo`\n- `worktree add / remove / move`\n- `hooks install`, `submodule add / update`\n- Any non-`git` command that mutates: `rm`, `mv`, `cp` (writes), `sed -i`, `> file`, `>> file`, `tee`, `curl`, `wget`, `pip install`, shell builtins that change state.\n\nIf the caller asks for any of the above, do not run it. Return `scope_check: "requires mutation: <reason>"` and stop.\n\n## Behavior\n\n- Run the minimum set of commands needed. Prefer `git log -n 5 --oneline -- <path>` over `git log -- <path>` when a count is fine.\n- Cite concrete evidence: commit SHAs (short form OK), ref names, `path:line` references from blame, diff hunks trimmed to the relevant range.\n- Use `Read`/`Grep`/`Glob` for follow-up inspection of files the git output identifies (e.g., `git show SHA:path | head` then `Read` the current file to diff mentally).\n- Do not speculate beyond what the commands show. If a question needs history the commands don\'t surface (deleted-file recovery, ancient reflog that has expired), say so in `caveats`.\n- Keep output compact \u2014 dispatchers merge your findings into a larger response. No preamble, no ceremony.\n\n## Return shape\n\n```\n{\n "findings": "<summary of what the git data shows>",\n "evidence": ["<SHA>", "<ref>", "<path:line>", ...],\n "git_commands_run": ["git log ...", "git diff ...", ...],\n "caveats": "<gaps, ambiguity, or \'none\'>",\n "scope_check": "pure git research" | "requires mutation: <reason>"\n}\n```\n\nBegin your response with the first schema field. No preamble.\n',sourcePath:"agent-framework-private/agents/git-investigator.md",allowedTools:["Bash","Read","Grep","Glob"],description:"Read-only git specialist. Dispatched by research-agent (or any research-shaped caller) when a finding requires git history, reflog, diff, blame, branch/remote state, or merge-base analysis. Runs git commands only \u2014 no mutations, no shell escapes.",model:"sonnet"};function xd(e){let t={description:e.description,prompt:e.systemPrompt};return e.allowedTools&&(t.tools=[...e.allowedTools]),e.model&&(t.model=e.model),t}function VM(e){let t=e.trim().toUpperCase();return["VERIFIED","CONFIRMED","CONFIRM","SUPPORTED","TRUE","PASS","PASSED"].includes(t)?"VERIFIED":["REFUTED","REFUTE","DISAGREE","DISAGREED","CONTRADICTED","FALSE","FAIL","FAILED"].includes(t)?"REFUTED":"INCONCLUSIVE"}var YM=V.object({verifications:V.array(V.object({claim:V.string().optional(),verdict:V.string(),evidence:V.string().optional()}))});function XM(e){let t=[],n=0,r=-1,o=!1,s=!1;for(let i=0;i<e.length;i++){let a=e[i];if(o){s?s=!1:a==="\\"?s=!0:a==='"'&&(o=!1);continue}a==='"'?o=!0:a==="{"?(n===0&&(r=i),n++):a==="}"&&n>0&&(n--,n===0&&r!==-1&&(t.push(e.slice(r,i+1)),r=-1))}return t}function ZM(e){let t=XM(e);for(let n of t){let r;try{r=JSON.parse(n)}catch{continue}let o=YM.safeParse(r);if(o.success)return o.data.verifications.map(s=>({claim:s.claim??"",verdict:VM(s.verdict),evidence:s.evidence??""}))}throw new Error(`shadow-verify did not return a parseable {"verifications":[...]} envelope (${t.length} JSON-like span(s) found, none matched the schema); raw output (first 300 chars): ${e.slice(0,300)}`)}var Aa=zM(qM),cb=V.object({id:V.string(),claim:V.string(),confidence:V.number().min(0).max(1),evidence_sources:V.array(V.string()),location:V.string().optional(),proposed_fix:V.string().optional(),coverage_gaps:V.array(V.string()).nullish().transform(e=>e??void 0),boundary_flag:V.string().nullish().transform(e=>e??void 0)}),QM=V.object({hypothesis_id:V.string(),claim:V.string(),verdict:V.enum(["VERIFIED","REFUTED","INCONCLUSIVE"]),evidence:V.string(),gate_reason:V.string()}),ub=V.object({hypothesis_id:V.string(),reproducer_passed:V.boolean(),regressions:V.array(V.string()),confidence:V.number().min(0).max(1),verification_log:V.string()}),eO=V.enum(["crash","regression","logic-error","flaky","environment","unknown"]),tO=V.object({failure_type:eO,error_signature:V.string(),affected_area:V.string()}),nO=V.enum(["clear_winner","multiple_plausible","dissent","all_inconclusive","no_hypotheses"]),BX=V.object({reproducer:V.string().optional(),triage:tO.optional(),hypotheses:V.array(cb),premise_verifications:V.array(QM).optional(),winner:V.object({hypothesis_id:V.string(),verification_log:V.string(),proposed_fix:V.string()}).optional(),verification_results:V.array(ub).optional(),outcome:nO.optional(),recommended_next_skill:V.enum(["spec"]).optional()});async function rO(e,t){let n=e.map(l=>({hypothesis:l,decision:sb(l)})).filter(l=>l.decision.verify);if(n.length===0)return{premise_verifications:[],hypotheses_to_test:e};let r=[],o;try{let l=await t(n.map(c=>c.hypothesis.claim));r=Array.isArray(l)?l:[]}catch(l){o=l instanceof Error?l.message:String(l)}let s=n.map((l,c)=>{let u=r[c];return o!==void 0?{hypothesis_id:l.hypothesis.id,claim:l.hypothesis.claim,verdict:"INCONCLUSIVE",evidence:`shadow-verify dispatch failed: ${o}`,gate_reason:l.decision.reason}:u?{hypothesis_id:l.hypothesis.id,claim:l.hypothesis.claim,verdict:u.verdict,evidence:u.evidence,gate_reason:l.decision.reason}:{hypothesis_id:l.hypothesis.id,claim:l.hypothesis.claim,verdict:"INCONCLUSIVE",evidence:"no verifier result for this claim",gate_reason:l.decision.reason}}),i=new Set(s.filter(l=>l.verdict==="REFUTED").map(l=>l.hypothesis_id)),a=i.size===0?e:e.filter(l=>!i.has(l.id));return{premise_verifications:s,hypotheses_to_test:a}}async function oO(e,t,n){let r=n?.apiKey,o=(()=>{if(typeof e=="string")return{failure:e,repoPath:process.cwd(),context:"",maxHypotheses:4};if(typeof e=="object"&&e!==null){let q=e;if(typeof q.failure=="string")return{failure:q.failure,repoPath:q.repoPath||process.cwd(),context:q.context||"",maxHypotheses:Math.min(q.maxHypotheses||4,4)}}throw new Error("diagnose handler requires input.failure (string) or a string argument")})();if(!t?.sessionId)throw new Error("diagnose requires a parent session with sessionId");let s=t.sessionId,i=we("diagnose"),a=i["system.md"],l=i["research.md"],c=i["hypothesis.md"],u=i["verify.md"];if(!a||!l||!c||!u)throw new Error("diagnose skill missing required prompts (system.md, research.md, hypothesis.md, verify.md)");let d=new X({apiKey:r}),p=lO(o.context),f=sO(o.failure,o.context),g=`Triage:
1288
1288
  failure_type: ${f.failure_type}
1289
1289
  error_signature: ${f.error_signature}
1290
1290
  affected_area: ${f.affected_area}`,b=`${at.systemPrompt}
@@ -1319,13 +1319,13 @@ ${JSON.stringify(I.git,null,2)}
1319
1319
 
1320
1320
  Original failure: ${o.failure}`,M;try{M=await D.runToResult(_)}finally{await D.teardown().catch(()=>{})}if(M.status!=="succeeded"||!M.output){if(M.schemaError){let q=M.message?.content||"(no response)";throw new Error(`hypothesis synthesis schema mismatch: ${M.schemaError.message}
1321
1321
  Raw response (first 500 chars): ${q.slice(0,500)}
1322
- Hint: model response must include a fenced JSON block with a hypotheses array.`)}throw new Error(`hypothesis synthesis failed: ${Ee(M)}`)}let P=M.output.hypotheses.slice(0,o.maxHypotheses);if(P.length===0)return{reproducer:m,triage:f,hypotheses:[],verification_results:[],outcome:"no_hypotheses"};let{premise_verifications:$,hypotheses_to_test:F}=await rO(P,async q=>{if(!n?.dispatchSkill)throw new Error("shadow-verify dispatch unavailable (no dispatchSkill in ctx)");let L=JSON.stringify({claims:q,context:`Original failure: ${o.failure}
1322
+ Hint: model response must include a fenced JSON block with a hypotheses array.`)}throw new Error(`hypothesis synthesis failed: ${Ee(M)}`)}let P=M.output.hypotheses.slice(0,o.maxHypotheses);if(P.length===0)return{reproducer:p,triage:f,hypotheses:[],verification_results:[],outcome:"no_hypotheses"};let{premise_verifications:$,hypotheses_to_test:F}=await rO(P,async q=>{if(!n?.dispatchSkill)throw new Error("shadow-verify dispatch unavailable (no dispatchSkill in ctx)");let L=JSON.stringify({claims:q,context:`Original failure: ${o.failure}
1323
1323
 
1324
1324
  OUTPUT CONTRACT (required): after your verification, end your reply with a single fenced \`\`\`json block of exactly this shape \u2014 one entry per claim, in the SAME order you received them:
1325
- {"verifications":[{"claim":"<echo of the claim>","verdict":"VERIFIED|REFUTED|INCONCLUSIVE","evidence":"<1-2 sentences; cite file:line or source>"}]}`}),te=await n.dispatchSkill("shadow-verify",L);return ZM(te)});if(F.length===0)return{reproducer:m,triage:f,hypotheses:P,premise_verifications:$,verification_results:[],outcome:"no_hypotheses"};let k=m||o.failure,A=F.map(q=>uO(q,k,o.repoPath,s,u,d,w)),N=await Promise.all(A),C=N.filter(q=>q.reproducer_passed&&q.regressions.length===0).slice().sort((q,L)=>L.confidence-q.confidence)[0]??N.find(q=>q.reproducer_passed),B=aO(P,N),j=C?P.find(q=>q.id===C.hypothesis_id):void 0,ee=B==="clear_winner"&&j&&iO(j)?"spec":void 0;return{reproducer:m,triage:f,hypotheses:P,premise_verifications:$.length>0?$:void 0,winner:C?{hypothesis_id:C.hypothesis_id,verification_log:C.verification_log,proposed_fix:j?.proposed_fix||""}:void 0,verification_results:N,outcome:B,recommended_next_skill:ee}}function sO(e,t){let n=`${e}
1325
+ {"verifications":[{"claim":"<echo of the claim>","verdict":"VERIFIED|REFUTED|INCONCLUSIVE","evidence":"<1-2 sentences; cite file:line or source>"}]}`}),te=await n.dispatchSkill("shadow-verify",L);return ZM(te)});if(F.length===0)return{reproducer:p,triage:f,hypotheses:P,premise_verifications:$,verification_results:[],outcome:"no_hypotheses"};let k=p||o.failure,A=F.map(q=>uO(q,k,o.repoPath,s,u,d,w)),N=await Promise.all(A),C=N.filter(q=>q.reproducer_passed&&q.regressions.length===0).slice().sort((q,L)=>L.confidence-q.confidence)[0]??N.find(q=>q.reproducer_passed),B=aO(P,N),j=C?P.find(q=>q.id===C.hypothesis_id):void 0,ee=B==="clear_winner"&&j&&iO(j)?"spec":void 0;return{reproducer:p,triage:f,hypotheses:P,premise_verifications:$.length>0?$:void 0,winner:C?{hypothesis_id:C.hypothesis_id,verification_log:C.verification_log,proposed_fix:j?.proposed_fix||""}:void 0,verification_results:N,outcome:B,recommended_next_skill:ee}}function sO(e,t){let n=`${e}
1326
1326
  ${t}`,r="unknown",o=n.toLowerCase();/flaky|non-?deterministic|intermittent|sometimes fails|race/.test(o)?r="flaky":/regression|used to work|worked before|broke in|ci.*green.*red|was passing/.test(o)?r="regression":/\b(uncaught|unhandled)\b|panic|segfault|exit(ed)? (with )?(code )?[1-9]|sigsegv|stack overflow|fatal|traceback|core dumped|abort(ed)?|\b(type|reference|range|syntax|internal|eval|uri)error\b/.test(o)?r="crash":/platform|node version|python version|dependency|version mismatch|works on .* not |env(ironment)?|config drift/.test(o)?r="environment":/expected .* but|got .* expected|wrong|incorrect|unexpected/.test(o)&&(r="logic-error");let s=/^(why|what|how|when|where|who|is|are|does|did|can|could|should|would)\b/i,i=/\b(error|exception|panic|throws?|traceback|fail(ed|ure|s)?|undefined|null|nan|segfault|sigsegv|stack ?overflow|abort(ed)?)\w*|:\s*\d+|\bat\s+\S|\bcore dumped\b/i,a=e.split(`
1327
1327
  `).map(d=>d.trim()).find(d=>d.length>0),l;a?s.test(a)&&!i.test(e)?l="prose-question":a.length>200?l=`${a.slice(0,197)}...`:l=a:l="unknown";let u=n.match(/(?:^|[\s'"`(])((?:\.{1,2}\/)?[\w@./-]+\.(?:ts|tsx|js|jsx|mjs|cjs|py|rb|go|rs|java|kt|cpp|c|h|hpp|md|json|yaml|yml)(?::\d+(?::\d+)?)?)/)?.[1]??"unknown";return{failure_type:r,error_signature:l,affected_area:u}}function iO(e){let n=`${e.proposed_fix??""}
1328
- ${e.location??""}`.match(/(?:^|[\s'"`(])((?:\.{1,2}\/)?[\w@./-]+\.(?:ts|tsx|js|jsx|mjs|cjs|py|rb|go|rs|java|kt|cpp|c|h|hpp))/g);return n?new Set(n.map(o=>o.trim().replace(/^[\s'"`(]+/,"").split(":")[0])).size>2:!1}function aO(e,t){if(e.length===0)return"no_hypotheses";let n=t.filter(o=>o.reproducer_passed&&o.regressions.length===0);return n.length===1?"clear_winner":n.length>=2?"multiple_plausible":e.filter(o=>o.confidence>=.7).length>=2?"dissent":"all_inconclusive"}function lO(e){if(!e)return;let t=[/test:\s*(.+)/i,/command:\s*(.+)/i,/reproducer:\s*(.+)/i,/failing test:\s*(.+)/i];for(let n of t){let r=e.match(n);if(r)return r[1]}}function ab(){return async e=>at.allowedTools.includes(e)?{behavior:"allow"}:{behavior:"deny",message:`Tool ${e} not allowed. Allowed tools: ${at.allowedTools.join(", ")}`}}var lb=[...at.allowedTools,"Agent"];function cO(){return async e=>lb.includes(e)?{behavior:"allow"}:{behavior:"deny",message:`Tool ${e} not allowed for git orchestrator. Allowed tools: ${lb.join(", ")}`}}async function uO(e,t,n,r,o,s,i){let a=ib(JM(),`diagnose-hyp-${e.id}-${Date.now()}`),l;try{await Aa("git",["worktree","add","--detach",a,"HEAD"],{cwd:n});try{let{writeFile:d}=await import("node:fs/promises"),m="",f="";try{m=(await Aa("git",["rev-parse","HEAD"],{cwd:n})).stdout.trim()}catch{}try{f=(await Aa("git",["symbolic-ref","--short","HEAD"],{cwd:n})).stdout.trim()}catch{}await d(ib(a,".afk-worktree-meta.json"),JSON.stringify({owner:"diagnose",createdAt:new Date().toISOString(),baseSha:m,baseBranch:f},null,2),"utf-8")}catch{}l=await s.forkSubagent({parent:{sessionId:r},config:{model:"sonnet",systemPrompt:`${o}
1328
+ ${e.location??""}`.match(/(?:^|[\s'"`(])((?:\.{1,2}\/)?[\w@./-]+\.(?:ts|tsx|js|jsx|mjs|cjs|py|rb|go|rs|java|kt|cpp|c|h|hpp))/g);return n?new Set(n.map(o=>o.trim().replace(/^[\s'"`(]+/,"").split(":")[0])).size>2:!1}function aO(e,t){if(e.length===0)return"no_hypotheses";let n=t.filter(o=>o.reproducer_passed&&o.regressions.length===0);return n.length===1?"clear_winner":n.length>=2?"multiple_plausible":e.filter(o=>o.confidence>=.7).length>=2?"dissent":"all_inconclusive"}function lO(e){if(!e)return;let t=[/test:\s*(.+)/i,/command:\s*(.+)/i,/reproducer:\s*(.+)/i,/failing test:\s*(.+)/i];for(let n of t){let r=e.match(n);if(r)return r[1]}}function ab(){return async e=>at.allowedTools.includes(e)?{behavior:"allow"}:{behavior:"deny",message:`Tool ${e} not allowed. Allowed tools: ${at.allowedTools.join(", ")}`}}var lb=[...at.allowedTools,"Agent"];function cO(){return async e=>lb.includes(e)?{behavior:"allow"}:{behavior:"deny",message:`Tool ${e} not allowed for git orchestrator. Allowed tools: ${lb.join(", ")}`}}async function uO(e,t,n,r,o,s,i){let a=ib(JM(),`diagnose-hyp-${e.id}-${Date.now()}`),l;try{await Aa("git",["worktree","add","--detach",a,"HEAD"],{cwd:n});try{let{writeFile:d}=await import("node:fs/promises"),p="",f="";try{p=(await Aa("git",["rev-parse","HEAD"],{cwd:n})).stdout.trim()}catch{}try{f=(await Aa("git",["symbolic-ref","--short","HEAD"],{cwd:n})).stdout.trim()}catch{}await d(ib(a,".afk-worktree-meta.json"),JSON.stringify({owner:"diagnose",createdAt:new Date().toISOString(),baseSha:p,baseBranch:f},null,2),"utf-8")}catch{}l=await s.forkSubagent({parent:{sessionId:r},config:{model:"sonnet",systemPrompt:`${o}
1329
1329
 
1330
1330
  You are testing in an isolated worktree at: ${a}`,canUseTool:dO()},idPrefix:`diagnose-verifier-${e.id}`,outputSchema:ub,...i?{parentId:i}:{}});let c=`Test this hypothesis:
1331
1331
 
@@ -1585,7 +1585,7 @@ Be skeptical. Protect the plugin from fluff. Stage 2 catches patterns that are s
1585
1585
  `))return t;let n=e.indexOf(`
1586
1586
  ---
1587
1587
  `);if(n===-1)return t;let r=e.slice(n+1),o=Sb(r);return o.ok?{ok:!0,skillName:o.skillName,recoveredContent:r}:t}function kb(e){let t=/^[a-z][a-z0-9-]*$/,n=e.match(/^name:[ \t]*["']?([a-z][a-z0-9-]*)["']?\s*$/m);if(n?.[1]&&t.test(n[1]))return n[1];let r=e.match(/^#{1,3}[ \t]+([a-z][a-z0-9-]*)\s*$/m);if(r?.[1]&&t.test(r[1]))return r[1];let s=e.slice(0,1024).match(/`([a-z][a-z0-9-]*)`/);return s?.[1]&&t.test(s[1])?s[1]:null}function GO(e,t){let n=[];return t?n.push(`Identify a DIFFERENT impactful skill gap. The previous candidate "${t}" collides with an already-installed skill \u2014 propose something else.`):n.push("Identify the most impactful skill gap."),e.length>0&&(n.push(""),n.push(`Already-installed skills (DO NOT propose any of these): ${e.join(", ")}.`)),n.join(`
1588
- `)}function Ad(e,t){if(t.registeredSkills.includes(e))return{ok:!1,reason:"name_collision",message:`skill name "${e}" is already registered in this session (built-in, plugin, or user-scope). Delete or rename the existing skill before forging a replacement.`,skillNameAttempted:e};let n=un(t.skillsDir,e);return Ca(n)?{ok:!1,reason:"name_collision",message:`target directory already exists on disk: ${n}. Forge would silently overwrite or shadow it. Remove the directory first if you intend to replace the skill.`,skillNameAttempted:e}:null}async function qO(e,t,n){if(T.AFK_INTERNAL!=="1")throw new Error("/forge is maintainer-only; set AFK_INTERNAL=1 to enable it");let r=n?.apiKey,o=n?.callId,s=typeof e=="string"?{brief:e}:typeof e=="object"&&e!==null?e:{},i=s.brief,a=s.forceThaw??!1,l=s.maxIterations??3,c="",u=[],d="REJECTED",m,f;try{let g=await jO();if(g.gate_status==="CLOSED"&&!a)return c=await Ye({event:"forge.gate_check",gate_status:"CLOSED"}),{status:"GATE_CLOSED",qualify_verdicts:[],telemetry_ref:c};a&&g.gate_status==="CLOSED"&&(await mb(),c=await Ye({event:"forge.thaw_override",gate_status:"CLOSED"})),c=await Ye({event:"forge.gate_check",gate_status:"OPEN"});let h="",b=!1,y=ft();if(i)h=i,b=!0;else{let _=await hb();if(_.length>0){let M=_[0],P=await gb(M);h=P.content,f=P.id,b=!0}else{if(!t?.sessionId)throw new Error("forge requires parent session for gap discovery");let P=we("forge")["gap-discovery.md"];if(!P)throw new Error("forge skill missing gap-discovery.md prompt");let $,F=2;for(let k=1;k<=F;k++){let N=await new X({apiKey:r}).forkSubagent({parent:{sessionId:t.sessionId},config:{model:"sonnet",systemPrompt:P},idPrefix:`forge-gap-discovery-${k}`,...o?{parentId:o}:{}}),H=GO(y,$),C=await N.runToResult(H);if(C.status!=="succeeded")throw new Error(`gap discovery failed (attempt ${k}): ${Ee(C)}`);let B=C.message?.content||"";if(!B)throw new Error(`gap discovery returned no concept (attempt ${k})`);let j=kb(B);if(!j){h=B;break}if(!Ad(j,{skillsDir:Xn(),registeredSkills:y})){h=B;break}if(c=await Ye({event:"forge.preflight_collision",candidate_name:j,attempt:k}),k===F)throw new Error(`forge preflight: autonomous gap discovery converged on already-installed skill "${j}" after ${F} attempts. Pass an explicit --brief, or run /forge-friction to surface a different gap.`);$=j}if(!h)throw new Error("gap discovery returned no usable concept after retries")}}if(c=await Ye({event:"forge.brief_loaded",used_brief:b,brief_id:f||null}),b){let _=kb(h);if(_&&Ad(_,{skillsDir:Xn(),registeredSkills:y}))throw c=await Ye({event:"forge.preflight_collision",candidate_name:_,attempt:0}),new Error(`forge preflight: brief proposes "${_}" but that name is already installed. Rename the brief or remove the existing skill first.`)}if(!t?.sessionId)throw new Error("forge requires parent session for skill generation");let w=we("forge"),x=w["generate.md"],R=w["system.md"];if(!x)throw new Error("forge skill missing generate.md prompt");if(!R)throw new Error("forge skill missing system.md prompt");let I=await(await new X({apiKey:r}).forkSubagent({parent:{sessionId:t.sessionId},config:{model:"sonnet",systemPrompt:R},idPrefix:"forge-generate",...o?{parentId:o}:{}})).runToResult(`${x}
1588
+ `)}function Ad(e,t){if(t.registeredSkills.includes(e))return{ok:!1,reason:"name_collision",message:`skill name "${e}" is already registered in this session (built-in, plugin, or user-scope). Delete or rename the existing skill before forging a replacement.`,skillNameAttempted:e};let n=un(t.skillsDir,e);return Ca(n)?{ok:!1,reason:"name_collision",message:`target directory already exists on disk: ${n}. Forge would silently overwrite or shadow it. Remove the directory first if you intend to replace the skill.`,skillNameAttempted:e}:null}async function qO(e,t,n){if(T.AFK_INTERNAL!=="1")throw new Error("/forge is maintainer-only; set AFK_INTERNAL=1 to enable it");let r=n?.apiKey,o=n?.callId,s=typeof e=="string"?{brief:e}:typeof e=="object"&&e!==null?e:{},i=s.brief,a=s.forceThaw??!1,l=s.maxIterations??3,c="",u=[],d="REJECTED",p,f;try{let g=await jO();if(g.gate_status==="CLOSED"&&!a)return c=await Ye({event:"forge.gate_check",gate_status:"CLOSED"}),{status:"GATE_CLOSED",qualify_verdicts:[],telemetry_ref:c};a&&g.gate_status==="CLOSED"&&(await mb(),c=await Ye({event:"forge.thaw_override",gate_status:"CLOSED"})),c=await Ye({event:"forge.gate_check",gate_status:"OPEN"});let h="",b=!1,y=ft();if(i)h=i,b=!0;else{let _=await hb();if(_.length>0){let M=_[0],P=await gb(M);h=P.content,f=P.id,b=!0}else{if(!t?.sessionId)throw new Error("forge requires parent session for gap discovery");let P=we("forge")["gap-discovery.md"];if(!P)throw new Error("forge skill missing gap-discovery.md prompt");let $,F=2;for(let k=1;k<=F;k++){let N=await new X({apiKey:r}).forkSubagent({parent:{sessionId:t.sessionId},config:{model:"sonnet",systemPrompt:P},idPrefix:`forge-gap-discovery-${k}`,...o?{parentId:o}:{}}),H=GO(y,$),C=await N.runToResult(H);if(C.status!=="succeeded")throw new Error(`gap discovery failed (attempt ${k}): ${Ee(C)}`);let B=C.message?.content||"";if(!B)throw new Error(`gap discovery returned no concept (attempt ${k})`);let j=kb(B);if(!j){h=B;break}if(!Ad(j,{skillsDir:Xn(),registeredSkills:y})){h=B;break}if(c=await Ye({event:"forge.preflight_collision",candidate_name:j,attempt:k}),k===F)throw new Error(`forge preflight: autonomous gap discovery converged on already-installed skill "${j}" after ${F} attempts. Pass an explicit --brief, or run /forge-friction to surface a different gap.`);$=j}if(!h)throw new Error("gap discovery returned no usable concept after retries")}}if(c=await Ye({event:"forge.brief_loaded",used_brief:b,brief_id:f||null}),b){let _=kb(h);if(_&&Ad(_,{skillsDir:Xn(),registeredSkills:y}))throw c=await Ye({event:"forge.preflight_collision",candidate_name:_,attempt:0}),new Error(`forge preflight: brief proposes "${_}" but that name is already installed. Rename the brief or remove the existing skill first.`)}if(!t?.sessionId)throw new Error("forge requires parent session for skill generation");let w=we("forge"),x=w["generate.md"],R=w["system.md"];if(!x)throw new Error("forge skill missing generate.md prompt");if(!R)throw new Error("forge skill missing system.md prompt");let I=await(await new X({apiKey:r}).forkSubagent({parent:{sessionId:t.sessionId},config:{model:"sonnet",systemPrompt:R},idPrefix:"forge-generate",...o?{parentId:o}:{}})).runToResult(`${x}
1589
1589
 
1590
1590
  ---
1591
1591
 
@@ -1593,7 +1593,7 @@ Be skeptical. Protect the plugin from fluff. Stage 2 catches patterns that are s
1593
1593
 
1594
1594
  ${h}`);if(I.status!=="succeeded")throw new Error(`skill generation failed: ${Ee(I)}`);let D=I.message?.content||"";if(!D)throw new Error("skill generation returned no output");for(let _=1;_<=l;_++){let M=Ed.systemPrompt;if(!M)throw new Error("qualify agent missing system prompt");let F=await(await new X({apiKey:r}).forkSubagent({parent:{sessionId:t.sessionId},config:{model:"sonnet",systemPrompt:M},idPrefix:`forge-qualify-${_}`,...o?{parentId:o}:{}})).runToResult(`Evaluate this amplifier skill against the force-multiplier criteria:
1595
1595
 
1596
- ${D}`);if(F.status!=="succeeded")throw new Error(`qualify iteration ${_} failed: ${Ee(F)}`);let k=F.message?.content||"",{verdict:A,score:N,feedback:H}=wb(k),C={iteration:_,verdict:A,score:N,feedback:H};if(u.push(C),c=await Ye({event:"forge.qualify_iteration",iteration:_,verdict:A,score:N||null,feedback:H||null}),A==="APPROVE"){d="APPROVED";break}else if(A==="SALVAGE"&&_<l){let B=w["qualify-rework.md"];if(!B)throw new Error("forge skill missing qualify-rework.md prompt");let j=B.replace("{feedback}",H).replace("{original_skill}",D),L=await(await new X({apiKey:r}).forkSubagent({parent:{sessionId:t.sessionId},config:{model:"sonnet",systemPrompt:j},idPrefix:`forge-rework-${_}`,...o?{parentId:o}:{}})).runToResult("Refine the skill based on the feedback.");if(L.status!=="succeeded")throw new Error(`rework iteration ${_} failed: ${Ee(L)}`);if(D=L.message?.content||"",!D)throw new Error(`rework iteration ${_} returned no output`)}else A==="REJECT"&&_>=l&&(d="MAX_ITERATIONS")}if(d==="APPROVED"){let _=KO(D);if(!_.ok)throw c=await Ye({event:"forge.write_failed",reason:_.reason,skill_name_attempted:_.skillNameAttempted}),new Error(`forge write-step invariant failed (${_.reason}): ${_.message}`);let M=_.skillName,P=_.recoveredContent??D;_.recoveredContent!==void 0&&(c=await Ye({event:"forge.preamble_recovered",skill_name:M,bytes_trimmed:D.length-_.recoveredContent.length}));let $=Ad(M,{skillsDir:Xn(),registeredSkills:ft()});if($)throw c=await Ye({event:"forge.write_failed",reason:$.reason,skill_name_attempted:$.skillNameAttempted}),new Error(`forge write-step invariant failed (${$.reason}): ${$.message}`);let F=un(Xn(),M);await vb(F,{recursive:!0});let k=un(F,"SKILL.md");await Tb(k,P,"utf-8");let A;try{A=await MO(k,"utf-8")}catch{A=""}if(!A.startsWith("---"))throw c=await Ye({event:"forge.write_failed",reason:"readback_failed",skill_name_attempted:M}),new Error(`forge write-step invariant failed (readback_failed): written file at ${k} did not read back with expected frontmatter`);m=k,b&&f&&await Rd(f,"consumed"),c=await Ye({event:"forge.complete",status:"APPROVED",skill_name:M,iterations:u.length})}else d==="MAX_ITERATIONS"&&(b&&f&&await Rd(f,"failed"),c=await Ye({event:"forge.complete",status:"MAX_ITERATIONS",skill_name_attempted:h||null,iterations:u.length}))}catch(g){throw c=await Ye({event:"forge.error",error:g instanceof Error?g.message:String(g)}),g}return{status:d,skill_path:m,qualify_verdicts:u,brief_id:f,telemetry_ref:c}}var zO={name:"forge",description:'Creates new amplifier skills gated by forge-gate-check, with autonomous gap discovery, skill generation, and qualify iteration loop \u22643\xD7. Writes approved skills and appends telemetry to shared JSONL with surface: "afk".',handler:qO,argumentHint:"[--brief <path>]",whenToUse:"When the user wants to grow the plugin with a new amplifier skill \u2014 autonomously generates and validates one.",flags:["--brief"],audience:"internal"};tt(zO);import{readFileSync as Lb,existsSync as Od}from"fs";import{join as $a}from"path";import{config as c$}from"dotenv";var Ia={opus:"claude-opus-4-8",opus_1m:"claude-opus-4-8",sonnet:"claude-sonnet-4-6",sonnet_1m:"claude-sonnet-4-6",haiku:"claude-haiku-4-5-20251001"};function Pa(e){return e in Ia}function Eb(e){let t=Ia[e];if(!t)throw new Error(`Invalid model: ${e}`);return t}function dn(e){if(e!==void 0)return typeof e=="string"&&Pa(e)?Eb(e):e}W();G();import{execFile as XO}from"node:child_process";import{promisify as ZO}from"node:util";import{promises as Cd}from"node:fs";import{dirname as QO,isAbsolute as e$,join as Id,resolve as t$}from"node:path";import{randomBytes as n$}from"node:crypto";G();import{basename as Rb}from"node:path";function JO(e){return`# >>> afk shell-init >>>
1596
+ ${D}`);if(F.status!=="succeeded")throw new Error(`qualify iteration ${_} failed: ${Ee(F)}`);let k=F.message?.content||"",{verdict:A,score:N,feedback:H}=wb(k),C={iteration:_,verdict:A,score:N,feedback:H};if(u.push(C),c=await Ye({event:"forge.qualify_iteration",iteration:_,verdict:A,score:N||null,feedback:H||null}),A==="APPROVE"){d="APPROVED";break}else if(A==="SALVAGE"&&_<l){let B=w["qualify-rework.md"];if(!B)throw new Error("forge skill missing qualify-rework.md prompt");let j=B.replace("{feedback}",H).replace("{original_skill}",D),L=await(await new X({apiKey:r}).forkSubagent({parent:{sessionId:t.sessionId},config:{model:"sonnet",systemPrompt:j},idPrefix:`forge-rework-${_}`,...o?{parentId:o}:{}})).runToResult("Refine the skill based on the feedback.");if(L.status!=="succeeded")throw new Error(`rework iteration ${_} failed: ${Ee(L)}`);if(D=L.message?.content||"",!D)throw new Error(`rework iteration ${_} returned no output`)}else A==="REJECT"&&_>=l&&(d="MAX_ITERATIONS")}if(d==="APPROVED"){let _=KO(D);if(!_.ok)throw c=await Ye({event:"forge.write_failed",reason:_.reason,skill_name_attempted:_.skillNameAttempted}),new Error(`forge write-step invariant failed (${_.reason}): ${_.message}`);let M=_.skillName,P=_.recoveredContent??D;_.recoveredContent!==void 0&&(c=await Ye({event:"forge.preamble_recovered",skill_name:M,bytes_trimmed:D.length-_.recoveredContent.length}));let $=Ad(M,{skillsDir:Xn(),registeredSkills:ft()});if($)throw c=await Ye({event:"forge.write_failed",reason:$.reason,skill_name_attempted:$.skillNameAttempted}),new Error(`forge write-step invariant failed (${$.reason}): ${$.message}`);let F=un(Xn(),M);await vb(F,{recursive:!0});let k=un(F,"SKILL.md");await Tb(k,P,"utf-8");let A;try{A=await MO(k,"utf-8")}catch{A=""}if(!A.startsWith("---"))throw c=await Ye({event:"forge.write_failed",reason:"readback_failed",skill_name_attempted:M}),new Error(`forge write-step invariant failed (readback_failed): written file at ${k} did not read back with expected frontmatter`);p=k,b&&f&&await Rd(f,"consumed"),c=await Ye({event:"forge.complete",status:"APPROVED",skill_name:M,iterations:u.length})}else d==="MAX_ITERATIONS"&&(b&&f&&await Rd(f,"failed"),c=await Ye({event:"forge.complete",status:"MAX_ITERATIONS",skill_name_attempted:h||null,iterations:u.length}))}catch(g){throw c=await Ye({event:"forge.error",error:g instanceof Error?g.message:String(g)}),g}return{status:d,skill_path:p,qualify_verdicts:u,brief_id:f,telemetry_ref:c}}var zO={name:"forge",description:'Creates new amplifier skills gated by forge-gate-check, with autonomous gap discovery, skill generation, and qualify iteration loop \u22643\xD7. Writes approved skills and appends telemetry to shared JSONL with surface: "afk".',handler:qO,argumentHint:"[--brief <path>]",whenToUse:"When the user wants to grow the plugin with a new amplifier skill \u2014 autonomously generates and validates one.",flags:["--brief"],audience:"internal"};tt(zO);import{readFileSync as Lb,existsSync as Od}from"fs";import{join as $a}from"path";import{config as c$}from"dotenv";var Ia={opus:"claude-opus-4-8",opus_1m:"claude-opus-4-8",sonnet:"claude-sonnet-4-6",sonnet_1m:"claude-sonnet-4-6",haiku:"claude-haiku-4-5-20251001"};function Pa(e){return e in Ia}function Eb(e){let t=Ia[e];if(!t)throw new Error(`Invalid model: ${e}`);return t}function dn(e){if(e!==void 0)return typeof e=="string"&&Pa(e)?Eb(e):e}W();G();import{execFile as XO}from"node:child_process";import{promisify as ZO}from"node:util";import{promises as Cd}from"node:fs";import{dirname as QO,isAbsolute as e$,join as Id,resolve as t$}from"node:path";import{randomBytes as n$}from"node:crypto";G();import{basename as Rb}from"node:path";function JO(e){return`# >>> afk shell-init >>>
1597
1597
  # Auto-cd into a preserved worktree after \`afk\` exits.
1598
1598
  # REQUIRES: bash or zsh. Uses \`local\` (also works in dash/ash but
1599
1599
  # breaks in ksh93). For fish, use \`afk shell-init fish | source\`.
@@ -1657,7 +1657,7 @@ end
1657
1657
  `)&&(s+=`
1658
1658
  `),s+=`${n}
1659
1659
  `,await Cd.writeFile(t,s,"utf8")}function l$(e,t,n){if(!io(e))return new Error(`git worktree add failed: ${String(e)}`);let r=e.stderr??"",o=e.message??"",s=`${r}
1660
- ${o}`;return s.includes("already checked out")||s.includes("is already used by worktree")?new Error(`Branch '${t}' is already checked out in another worktree. Pick a different name.`):s.includes("already exists")?new Error(`Worktree path '${n}' already exists. Remove it or pick a different branch name.`):new Error(o||r||"git worktree add failed")}async function Oa(e,t){let n=t?.execFile??Cb,r=Ma(t?.branchPrefix),o=await Ib(n);return await Pb(o),Ob(o,e,{execFile:n,prefix:r})}async function Mb(e){let t=e?.execFile??Cb,n=Ma(e?.branchPrefix),r=await Ib(t);await Pb(r);let o;return{repoRoot:r,handle:()=>o,async create(s){return o===void 0&&(o=await Ob(r,s,{execFile:t,prefix:n})),o}}}async function Ob(e,t,n){let{execFile:r,prefix:o}=n,s=t===!0?i$(o):t;a$(s);let i=s.replaceAll("/","-"),a=Id(e,".afk-worktrees",i);try{await r("git",["-C",e,"worktree","add","-b",s,a])}catch(c){throw l$(c,s,a)}let l={path:a,branch:s,cleanup:async c=>{let u=l.path,d=l.branch;if(c?.force===!0){console.log(`Worktree removed (zero turns \u2014 no work done): ${u}`);try{await r("git",["-C",e,"worktree","remove","--force",u])}catch(f){let g=io(f)?f.message||f.stderr||"":String(f);console.warn(`Worktree cleanup: 'git worktree remove --force ${u}' failed (${g}). Manual removal may be needed.`);return}try{await r("git",["-C",e,"branch","-d",d])}catch(f){let g=io(f)?f.message||f.stderr||"":String(f);console.warn(`Could not delete branch '${d}': ${g}`)}return}let m;try{m=await r("git",["-C",u,"status","--porcelain"])}catch(f){let g=io(f)?f.message||f.stderr||"":String(f);console.warn(`Worktree cleanup: could not check status at ${u} (${g}). Skipping removal \u2014 manual cleanup may be needed.`);return}if(m.stdout.trim().length>0){if(console.log(`Worktree preserved at ${u} (branch: ${d}) \u2014 uncommitted changes.`),qf(u),!zf()){let g=_d(T.SHELL)==="fish"?"afk shell-init fish | source (add to ~/.config/fish/config.fish)":'eval "$(afk shell-init)" (add to ~/.zshrc or ~/.bashrc)';console.log(` \u2192 cd ${u}
1660
+ ${o}`;return s.includes("already checked out")||s.includes("is already used by worktree")?new Error(`Branch '${t}' is already checked out in another worktree. Pick a different name.`):s.includes("already exists")?new Error(`Worktree path '${n}' already exists. Remove it or pick a different branch name.`):new Error(o||r||"git worktree add failed")}async function Oa(e,t){let n=t?.execFile??Cb,r=Ma(t?.branchPrefix),o=await Ib(n);return await Pb(o),Ob(o,e,{execFile:n,prefix:r})}async function Mb(e){let t=e?.execFile??Cb,n=Ma(e?.branchPrefix),r=await Ib(t);await Pb(r);let o;return{repoRoot:r,handle:()=>o,async create(s){return o===void 0&&(o=await Ob(r,s,{execFile:t,prefix:n})),o}}}async function Ob(e,t,n){let{execFile:r,prefix:o}=n,s=t===!0?i$(o):t;a$(s);let i=s.replaceAll("/","-"),a=Id(e,".afk-worktrees",i);try{await r("git",["-C",e,"worktree","add","-b",s,a])}catch(c){throw l$(c,s,a)}let l={path:a,branch:s,cleanup:async c=>{let u=l.path,d=l.branch;if(c?.force===!0){console.log(`Worktree removed (zero turns \u2014 no work done): ${u}`);try{await r("git",["-C",e,"worktree","remove","--force",u])}catch(f){let g=io(f)?f.message||f.stderr||"":String(f);console.warn(`Worktree cleanup: 'git worktree remove --force ${u}' failed (${g}). Manual removal may be needed.`);return}try{await r("git",["-C",e,"branch","-d",d])}catch(f){let g=io(f)?f.message||f.stderr||"":String(f);console.warn(`Could not delete branch '${d}': ${g}`)}return}let p;try{p=await r("git",["-C",u,"status","--porcelain"])}catch(f){let g=io(f)?f.message||f.stderr||"":String(f);console.warn(`Worktree cleanup: could not check status at ${u} (${g}). Skipping removal \u2014 manual cleanup may be needed.`);return}if(p.stdout.trim().length>0){if(console.log(`Worktree preserved at ${u} (branch: ${d}) \u2014 uncommitted changes.`),qf(u),!zf()){let g=_d(T.SHELL)==="fish"?"afk shell-init fish | source (add to ~/.config/fish/config.fish)":'eval "$(afk shell-init)" (add to ~/.zshrc or ~/.bashrc)';console.log(` \u2192 cd ${u}
1661
1661
  \u2192 Or install one-time: ${g}`)}return}try{await r("git",["-C",e,"worktree","remove","--force",u])}catch(f){let g=io(f)?f.message||f.stderr||"":String(f);console.warn(`Worktree cleanup: 'git worktree remove --force ${u}' failed (${g}). Manual removal may be needed.`);return}try{await r("git",["-C",e,"branch","-d",d])}catch(f){let g=io(f)?f.message||f.stderr||"":String(f);console.warn(`Could not delete branch '${d}': ${g}`)}}};try{let c="",u="";try{c=(await r("git",["-C",e,"rev-parse","HEAD"])).stdout.trim()}catch{}try{u=(await r("git",["-C",e,"symbolic-ref","--short","HEAD"])).stdout.trim()}catch{}let d={owner:"interactive",pid:process.pid,createdAt:new Date().toISOString(),baseSha:c,baseBranch:u};await Cd.writeFile(Id(a,".afk-worktree-meta.json"),JSON.stringify(d,null,2),"utf-8")}catch{}return l}G();var Ms={model:"sonnet",maxTokens:4096,temperature:1,updatePolicy:"notify"},$b=!1;function Os(){return T.ANTHROPIC_API_KEY||T.CLAUDE_CODE_OAUTH_TOKEN||ze()}var Md,Db=new Set;function u$(e){let t=e.trim();if(!t)return t;let n="/chat/completions";if(t.endsWith(n)){let r=t.slice(0,-n.length);return Db.has(t)||(Db.add(t),console.warn(`[afk] AFK_OPENAI_BASE_URL: stripped trailing "/chat/completions" \u2014 the OpenAI SDK appends it automatically.
1662
1662
  Effective base URL: ${r}`)),r}return t}function d$(){if(Md!==void 0)return Md;if(!$b){let o=[$a(process.cwd(),".env"),ot(),Bf()];for(let s of o)Od(s)&&c$({path:s,override:!1});$b=!0}let e={},t=T.AFK_MODEL??T.CLAUDE_MODEL;if(t){let o=t.toLowerCase();e.model=Pa(o)?o:t}if(xe(t)==="anthropic-direct"){let o=Os();o!==void 0&&(e.apiKey=o)}let r=T.AFK_LOCAL_BASE_URL;if(r&&r.length>0&&(e.baseUrl=r,e.apiKey=T.AFK_LOCAL_API_KEY||"local"),T.AFK_MAX_TOKENS&&(e.maxTokens=parseInt(T.AFK_MAX_TOKENS,10)),T.AFK_TEMPERATURE&&(e.temperature=parseFloat(T.AFK_TEMPERATURE)),T.AFK_SYSTEM_PROMPT&&(e.systemPrompt=T.AFK_SYSTEM_PROMPT),T.AFK_AUTO_ROUTING){let o=T.AFK_AUTO_ROUTING.toLowerCase()==="true";e.autoRouting={interactive:o,chat:o,telegram:o,daemon:o}}return T.AFK_OPENAI_BASE_URL&&(e.openaiBaseUrl=u$(T.AFK_OPENAI_BASE_URL)),Md=e,e}var ao,lo;function p$(){if(ao!==void 0)return ao;let e=[$a(process.cwd(),"afk.config.json"),Fi(),Uf()];for(let t of e)if(Od(t))try{let n=Lb(t,"utf-8"),r=JSON.parse(n),o={};if(typeof r.model=="string"&&r.model.length>0){let s=r.model.toLowerCase();o.model=Pa(s)?s:r.model}if(typeof r.maxTokens=="number"&&(o.maxTokens=r.maxTokens),typeof r.temperature=="number"&&(o.temperature=r.temperature),r.systemPrompt&&(o.systemPrompt=r.systemPrompt),r.autoRouting&&typeof r.autoRouting=="object"){let s={};typeof r.autoRouting.interactive=="boolean"&&(s.interactive=r.autoRouting.interactive),typeof r.autoRouting.chat=="boolean"&&(s.chat=r.autoRouting.chat),typeof r.autoRouting.telegram=="boolean"&&(s.telegram=r.autoRouting.telegram),typeof r.autoRouting.daemon=="boolean"&&(s.daemon=r.autoRouting.daemon),o.autoRouting=s}if(r.daemon&&typeof r.daemon=="object"){let s={};typeof r.daemon.task=="string"&&(s.task=r.daemon.task),typeof r.daemon.taskId=="string"&&(s.taskId=r.daemon.taskId);let i=r.daemon.worktreePrune;i&&typeof i=="object"&&(s.worktreePrune={enabled:typeof i.enabled=="boolean"?i.enabled:!0,cron:typeof i.cron=="string"?i.cron:"0 4 * * *",maxAgeDaysClean:typeof i.maxAgeDaysClean=="number"?i.maxAgeDaysClean:14,maxAgeDaysDirty:typeof i.maxAgeDaysDirty=="number"?i.maxAgeDaysDirty:30,scope:typeof i.scope=="string"?i.scope:"all"}),o.daemon=s}if(r.updatePolicy&&["notify","auto","off"].includes(r.updatePolicy)&&(o.updatePolicy=r.updatePolicy),typeof r.autoResumeOnUsageLimit=="boolean"&&(o.autoResumeOnUsageLimit=r.autoResumeOnUsageLimit),typeof r.bgSummaries=="boolean"&&(o.bgSummaries=r.bgSummaries),typeof r.maxSummaryCallsPerSession=="number"&&(o.maxSummaryCallsPerSession=Math.min(500,Math.max(1,r.maxSummaryCallsPerSession))),r.interactive&&typeof r.interactive=="object"){let s={};typeof r.interactive.worktreeAutoname=="boolean"&&(s.worktreeAutoname=r.interactive.worktreeAutoname),typeof r.interactive.worktreeBranchPrefix=="string"&&(s.worktreeBranchPrefix=Pd(r.interactive.worktreeBranchPrefix,`${t}#/interactive/worktreeBranchPrefix`)),Object.keys(s).length>0&&(o.interactive=s)}return ao={config:o,sourcePath:t},ao}catch(n){console.error(`Warning: Failed to parse ${t}:`,n)}return ao={config:{},sourcePath:void 0},ao}function m$(){if(lo!==void 0)return lo.value;let e=[$a(process.cwd(),"AFK.md"),$a(Ce(),"AFK.md")];for(let t of e)if(Od(t))try{let n=Lb(t,"utf-8").trim();if(n.length>0)return lo={value:{content:n,path:t}},lo.value}catch{}return lo={value:null},lo.value}function nt(e){let t=d$(),{config:n,sourcePath:r}=p$(),o={...Ms,...t,...n,...e},s;if(t.systemPrompt!==void 0)s="env:AFK_SYSTEM_PROMPT";else if(n.systemPrompt!==void 0&&r!==void 0)s=`file:${r}`;else if(o.systemPrompt===void 0){let a=m$();a!==null&&(o.systemPrompt=a.content,s=`afk-md:${a.path}`)}let i={model:o.model??Ms.model,maxTokens:o.maxTokens??Ms.maxTokens,temperature:o.temperature??Ms.temperature,updatePolicy:o.updatePolicy??Ms.updatePolicy,...o.apiKey!==void 0?{apiKey:o.apiKey}:{},...o.baseUrl!==void 0?{baseUrl:o.baseUrl}:{},...o.openaiBaseUrl!==void 0?{openaiBaseUrl:o.openaiBaseUrl}:{},...o.systemPrompt!==void 0?{systemPrompt:o.systemPrompt}:{},...s!==void 0?{systemPromptSource:s}:{},...o.autoRouting!==void 0?{autoRouting:o.autoRouting}:{},...o.daemon!==void 0?{daemon:o.daemon}:{},...o.bgSummaries!==void 0?{bgSummaries:o.bgSummaries}:{},...o.maxSummaryCallsPerSession!==void 0?{maxSummaryCallsPerSession:o.maxSummaryCallsPerSession}:{}};if(typeof i.model=="string"&&i.model.toLowerCase().startsWith("local-")&&(i.baseUrl===void 0||i.baseUrl.length===0))throw new Error(`Model '${i.model}' requires AFK_LOCAL_BASE_URL to be set (e.g. AFK_LOCAL_BASE_URL=http://127.0.0.1:8080). Point it at your local Anthropic-Messages-compatible server.`);return i}G();function co(){return`# Agent AFK
1663
1663
 
@@ -1826,7 +1826,7 @@ ${e}
1826
1826
  `+(t?`Wave orchestration plan:
1827
1827
  ${JSON.stringify(t,null,2)}
1828
1828
 
1829
- `:"")+"Execute the implementation plan following TDD (test-first) principles.",u=await l.runToResult(c);if(u.status!=="succeeded"||!u.output)throw new Error(`build phase failed: ${Ee(u)}`);let d=u.output,m={filesChanged:d.files_changed,testsPassed:d.tests_passed,notes:d.notes};return yo({kind:"checkpoint",title:"build",body:[`Files changed: ${m.filesChanged.length}`,`Tests: ${m.testsPassed?"passed":"failed"}`,"Next: verify"]}),m}import{z as bo}from"zod";var h$=bo.object({status:bo.enum(["PASS","FAIL"]),status_reason:bo.string().optional(),issues:bo.array(bo.string()).default([]),summary:bo.string().optional()});async function Fd(e,t,n,r,o,s,i){let l=await new X(s!==void 0?{cwd:s}:{}).forkSubagent({parent:{sessionId:r},config:{model:"sonnet",systemPrompt:o,apiKey:ue()},idPrefix:`mint-verify-${e}`,outputSchema:h$,...i?{parentId:i}:{}}),c=`Plan:
1829
+ `:"")+"Execute the implementation plan following TDD (test-first) principles.",u=await l.runToResult(c);if(u.status!=="succeeded"||!u.output)throw new Error(`build phase failed: ${Ee(u)}`);let d=u.output,p={filesChanged:d.files_changed,testsPassed:d.tests_passed,notes:d.notes};return yo({kind:"checkpoint",title:"build",body:[`Files changed: ${p.filesChanged.length}`,`Tests: ${p.testsPassed?"passed":"failed"}`,"Next: verify"]}),p}import{z as bo}from"zod";var h$=bo.object({status:bo.enum(["PASS","FAIL"]),status_reason:bo.string().optional(),issues:bo.array(bo.string()).default([]),summary:bo.string().optional()});async function Fd(e,t,n,r,o,s,i){let l=await new X(s!==void 0?{cwd:s}:{}).forkSubagent({parent:{sessionId:r},config:{model:"sonnet",systemPrompt:o,apiKey:ue()},idPrefix:`mint-verify-${e}`,outputSchema:h$,...i?{parentId:i}:{}}),c=`Plan:
1830
1830
  ${t}
1831
1831
 
1832
1832
  Build results:
@@ -1834,7 +1834,7 @@ ${JSON.stringify(n,null,2)}
1834
1834
 
1835
1835
  Mode: ${e}
1836
1836
 
1837
- Run ${e} verification on the implementation.`,u;try{u=await l.runToResult(c)}finally{await l.teardown().catch(()=>{})}if(u.status!=="succeeded"||!u.output)return{passed:!1,issues:[`${e} verification failed: ${Ee(u)}`]};let d=u.output,m=d.status==="PASS";return{passed:m,issues:m?void 0:d.issues}}async function Da(e,t,n,r,o){let i=we("mint")["verify.md"];if(!i)throw new Error("mint skill missing verify.md prompt");let[a,l,c]=await Promise.all([Fd("test",e,t,n,i,r,o),Fd("lint",e,t,n,i,r,o),Fd("design-review",e,t,n,i,r,o)]),u=[];a.issues&&u.push(...a.issues),l.issues&&u.push(...l.issues),c.issues&&u.push(...c.issues);let d={testsPassed:a.passed,lintPassed:l.passed,designReviewPassed:c.passed,...u.length>0?{issues:u}:{}},m=d.testsPassed&&d.lintPassed&&d.designReviewPassed,f=g=>g?"passed":"failed";return yo({kind:m?"checkpoint":"diagnosis",title:"verify",body:[`Tests: ${f(d.testsPassed)} \xB7 Lint: ${f(d.lintPassed)}`,`Design review: ${f(d.designReviewPassed)}`,...m?["Next: ship"]:[`Issues: ${u.length} (heal loop will retry)`]]}),d}async function Kb(e,t,n,r,o,s){if(n.testsPassed&&n.lintPassed&&n.designReviewPassed)return{healed:!0,newHealIterations:r,newVerifyResults:n};if(r>=2)return{healed:!1,newHealIterations:r,newVerifyResults:n};try{let i=$e("diagnose"),a=`Verification failures:
1837
+ Run ${e} verification on the implementation.`,u;try{u=await l.runToResult(c)}finally{await l.teardown().catch(()=>{})}if(u.status!=="succeeded"||!u.output)return{passed:!1,issues:[`${e} verification failed: ${Ee(u)}`]};let d=u.output,p=d.status==="PASS";return{passed:p,issues:p?void 0:d.issues}}async function Da(e,t,n,r,o){let i=we("mint")["verify.md"];if(!i)throw new Error("mint skill missing verify.md prompt");let[a,l,c]=await Promise.all([Fd("test",e,t,n,i,r,o),Fd("lint",e,t,n,i,r,o),Fd("design-review",e,t,n,i,r,o)]),u=[];a.issues&&u.push(...a.issues),l.issues&&u.push(...l.issues),c.issues&&u.push(...c.issues);let d={testsPassed:a.passed,lintPassed:l.passed,designReviewPassed:c.passed,...u.length>0?{issues:u}:{}},p=d.testsPassed&&d.lintPassed&&d.designReviewPassed,f=g=>g?"passed":"failed";return yo({kind:p?"checkpoint":"diagnosis",title:"verify",body:[`Tests: ${f(d.testsPassed)} \xB7 Lint: ${f(d.lintPassed)}`,`Design review: ${f(d.designReviewPassed)}`,...p?["Next: ship"]:[`Issues: ${u.length} (heal loop will retry)`]]}),d}async function Kb(e,t,n,r,o,s){if(n.testsPassed&&n.lintPassed&&n.designReviewPassed)return{healed:!0,newHealIterations:r,newVerifyResults:n};if(r>=2)return{healed:!1,newHealIterations:r,newVerifyResults:n};try{let i=$e("diagnose"),a=`Verification failures:
1838
1838
  Tests: ${n.testsPassed?"PASS":"FAIL"}
1839
1839
  Lint: ${n.lintPassed?"PASS":"FAIL"}
1840
1840
  Design: ${n.designReviewPassed?"PASS":"FAIL"}
@@ -1867,14 +1867,14 @@ Create a ship-ready summary with next steps.`,c=await a.runToResult(l);if(c.stat
1867
1867
  `))return{frontmatter:null,frontmatterFlags:null,body:e};let t=e.indexOf(`
1868
1868
  ---
1869
1869
  `,4);if(t===-1)return{frontmatter:null,frontmatterFlags:null,body:e};let n=e.slice(4,t),r=e.slice(t+5),o={},s=null,i=n.split(`
1870
- `);for(let a=0;a<i.length;a++){let l=i[a];if(!l||!l.trim()||l.trimStart().startsWith("#"))continue;if(l.startsWith("flags:")){let u=l.slice(6).trim();if(u.startsWith("[")){let d=u.match(/\[(.*?)\]/);if(d?.[1]){let m=d[1].split(",").map(f=>f.trim()).filter(f=>f.length>0);m.length>0&&(s=m.map(tw).sort())}continue}if(u===""||u==="null"){let d=[];for(let m=a+1;m<i.length;m++){let f=i[m];if(!f||!f.match(/^\s+-\s/))break;let g=f.match(/^\s+-\s+(.+)/);g?.[1]&&d.push(g[1].trim())}d.length>0&&(s=d.map(tw).sort());continue}}let c=l.match(/^([a-zA-Z][a-zA-Z0-9_-]*):\s*(.*)$/);if(c&&c[1]!==void 0&&c[2]!==void 0){let u=c[2].trim().replace(/^['"]|['"]$/g,"");u.length>0&&(o[c[1]]=u)}}return{frontmatter:o,frontmatterFlags:s,body:r}}function nw(e){let t=Ls(e);return t.frontmatterFlags&&t.frontmatterFlags.length>0?t.frontmatterFlags:Ud(t.body)}function L$(e,t){return e.length===0||e.length>64?{valid:!1,reason:`name must be 1\u201364 characters, got ${e.length}`}:/^[a-z0-9]+(-[a-z0-9]+)*$/.test(e)?e!==t?{valid:!1,reason:`name field "${e}" does not match parent directory name "${t}"`}:{valid:!0}:{valid:!1,reason:`name "${e}" does not match spec pattern ^[a-z0-9]+(-[a-z0-9]+)*$ (only lowercase a-z0-9 and hyphens, no leading/trailing/consecutive hyphens)`}}var ow=1024;function F$(e,t){let n=Ls(e);if(!n.frontmatter)return null;let r=n.frontmatter.name,o=n.frontmatter.description,s=n.body.trim();if(!r||!o||s.length===0)return null;let i=L$(r,t);if(!i.valid)return process.stderr.write(`[afk] skipping skill ${t}: ${i.reason}
1870
+ `);for(let a=0;a<i.length;a++){let l=i[a];if(!l||!l.trim()||l.trimStart().startsWith("#"))continue;if(l.startsWith("flags:")){let u=l.slice(6).trim();if(u.startsWith("[")){let d=u.match(/\[(.*?)\]/);if(d?.[1]){let p=d[1].split(",").map(f=>f.trim()).filter(f=>f.length>0);p.length>0&&(s=p.map(tw).sort())}continue}if(u===""||u==="null"){let d=[];for(let p=a+1;p<i.length;p++){let f=i[p];if(!f||!f.match(/^\s+-\s/))break;let g=f.match(/^\s+-\s+(.+)/);g?.[1]&&d.push(g[1].trim())}d.length>0&&(s=d.map(tw).sort());continue}}let c=l.match(/^([a-zA-Z][a-zA-Z0-9_-]*):\s*(.*)$/);if(c&&c[1]!==void 0&&c[2]!==void 0){let u=c[2].trim().replace(/^['"]|['"]$/g,"");u.length>0&&(o[c[1]]=u)}}return{frontmatter:o,frontmatterFlags:s,body:r}}function nw(e){let t=Ls(e);return t.frontmatterFlags&&t.frontmatterFlags.length>0?t.frontmatterFlags:Ud(t.body)}function L$(e,t){return e.length===0||e.length>64?{valid:!1,reason:`name must be 1\u201364 characters, got ${e.length}`}:/^[a-z0-9]+(-[a-z0-9]+)*$/.test(e)?e!==t?{valid:!1,reason:`name field "${e}" does not match parent directory name "${t}"`}:{valid:!0}:{valid:!1,reason:`name "${e}" does not match spec pattern ^[a-z0-9]+(-[a-z0-9]+)*$ (only lowercase a-z0-9 and hyphens, no leading/trailing/consecutive hyphens)`}}var ow=1024;function F$(e,t){let n=Ls(e);if(!n.frontmatter)return null;let r=n.frontmatter.name,o=n.frontmatter.description,s=n.body.trim();if(!r||!o||s.length===0)return null;let i=L$(r,t);if(!i.valid)return process.stderr.write(`[afk] skipping skill ${t}: ${i.reason}
1871
1871
  `),null;if(o.length>ow)return process.stderr.write(`[afk] skipping skill ${t}: description exceeds ${ow} characters (got ${o.length})
1872
1872
  `),null;let a=n.frontmatter["argument-hint"]??n.frontmatter.argumentHint,l=nw(e),c={name:r,description:o,body:s,dir:""};return a&&a.length>0&&(c.argumentHint=a),l.length>0&&(c.flags=l),c}function N$(e){return async(t,n,r)=>{let o=typeof t=="string"&&t.length>0?t:`Run the ${e.name} skill now, following the instructions in your system prompt.`,s=new X({parentAbortSignal:n?.abortSignal}),i=r?.callId;return await(await s.forkSubagent({parent:{sessionId:n?.sessionId,getInputStreamRef:n?.getInputStreamRef?.bind(n),abortSignal:n?.abortSignal},config:{model:"sonnet",systemPrompt:e.body,env:{SKILL_ROOT:e.dir},isSkillDispatch:!0},idPrefix:`user-skill-${e.name}`,...i?{parentId:i}:{}})).runToResult(o)}}function B$(e,t){try{return $e(e),`${t}:${e}`}catch{return e}}function La(e,t){let n;try{n=$$(e,{withFileTypes:!0})}catch{return 0}let r=0;for(let o of n){if(!o.isDirectory()||o.name.startsWith("_")||o.name.startsWith("."))continue;let s;try{s=D$(rw(e,o.name,"SKILL.md"),"utf-8")}catch{continue}let i=F$(s,o.name);if(!i)continue;i.dir=rw(e,o.name);let l={name:B$(i.name,t),description:i.description,handler:N$(i),origin:t};i.argumentHint&&(l.argumentHint=i.argumentHint),i.flags&&i.flags.length>0&&(l.flags=i.flags),tt(l),r++}return r}function jd(){return La(Xn(),"user")}import{existsSync as U$,readdirSync as j$,readFileSync as W$,statSync as H$}from"fs";import{join as K$}from"path";function Wd(e){let t=[];function n(r,o=0){if(o>10||!U$(r))return;let s;try{s=j$(r)}catch{return}for(let i of s){if(i.startsWith("."))continue;let a=K$(r,i),l;try{l=H$(a)}catch{continue}if(l.isFile()&&i==="SKILL.md"){let c=G$(a);c.name&&t.push(c)}else l.isDirectory()&&n(a,o+1)}}return n(e),t}function G$(e){try{let t=W$(e,"utf-8");if(!t.startsWith(`---
1873
1873
  `))return{};let n=t.slice(4),r=n.indexOf(`
1874
1874
  ---`);if(r===-1)return{};let o=n.slice(0,r),s=n.slice(r+4).trim(),i={},a=o.split(`
1875
- `);for(let l of a){if(!l)continue;let c=l.indexOf(":");if(c===-1)continue;let u=l.slice(0,c).trim(),d=l.slice(c+1).trim();if(u==="name")i.name=d.replace(/^["']|["']$/g,"");else if(u==="description")i.description=d.replace(/^["']|["']$/g,"");else if(u==="argumentHint")i.argumentHint=d.replace(/^["']|["']$/g,"");else if(u==="audience"){let m=d.replace(/^["']|["']$/g,"");(m==="public"||m==="internal")&&(i.audience=m)}else u==="context"&&(i.context=d.replace(/^["']|["']$/g,""))}return s.length>0&&(i.body=s),i}catch{return{}}}W();G();function sw(e){let t=an(e);if(t.length===0)return"";let n=[];for(let r of t){let o=r.argumentHint?`${r.argumentHint}`:"",s=o?`- \`${r.name} ${o}\`: ${r.description}`:`- ${r.name}: ${r.description}`;n.push(s),r.whenToUse&&n.push(` When to use: ${r.whenToUse}`)}return["Available skills (invoke via the `skill` tool):","","Each skill either dispatches one or more context-isolated subagents (delegation \u2014 preserves the main session's context) or loads its instructions directly into your current context (`load` mode). Calling `skill` is the entry point for both; the executor picks the mode per skill. Prefer a skill over inline investigation when the task shape matches.","",...n].join(`
1876
- `)}function an(e){let t=[],n=new Set,r=T.AFK_INTERNAL==="1";for(let s of ft()){let i=$e(s);Et(i,r)&&(t.push({name:s,description:i.description,source:i.origin==="user"?"user":i.origin==="project"?"project":"builtin",argumentHint:i.argumentHint,whenToUse:i.whenToUse}),n.add(s))}let o=e??[...At(os()),...At(),...At(is())];for(let s of o){if(s.type!=="local")continue;let i=Wd(s.path);for(let a of i)!a.name||n.has(a.name)||Et({audience:a.audience},r)&&(t.push({name:a.name,description:a.description??`Skill from plugin at ${s.path}`,source:"plugin"}),n.add(a.name))}return t}function va(e){let t=new Map,n=e??[...At(os()),...At(),...At(is())];for(let r of n){if(r.type!=="local")continue;let o=Wd(r.path);for(let s of o)s.name&&s.body&&s.body.length>0&&!t.has(s.name)&&t.set(s.name,{body:s.body,pluginPath:r.path,...s.context!==void 0?{context:s.context}:{}})}return t}function iw(e){if(e.length===0)return;let t=e[e.length-1];if(!t||t.role!=="assistant"||typeof t.content=="string")return;let n=t.content,r=[];for(let s of n)s.type==="tool_use"&&typeof s.id=="string"&&r.push(s.id);if(r.length===0)return;let o={role:"user",content:r.map(s=>({type:"tool_result",tool_use_id:s,content:"Tool call interrupted before completing \u2014 no result recorded.",is_error:!0}))};e.push(o)}function aw(e){return{messages:e.initialMessages?[...e.initialMessages]:[],currentModel:e.model,requestedModel:e.requestedModel??e.model,currentPermissionMode:e.permissionMode,userSystem:e.userSystem,toolDispatcher:e.toolDispatcher,lastUsage:null,closed:!1,autoCompactThreshold:e.autoCompactThreshold}}var q$="__closed__",Fa=class{current=null;pendingReason=null;closedPromise;closeResolve=null;constructor(){this.closedPromise=new Promise(t=>{this.closeResolve=()=>t(q$)})}begin(){let t=new AbortController;return this.current=t,this.pendingReason!==null&&!t.signal.aborted&&(t.abort(this.pendingReason),this.pendingReason=null),t}clear(t){this.current===t&&(this.current=null)}requestAbort(t){let n=this.current;if(n&&!n.signal.aborted){n.abort(t);return}this.pendingReason=t}isIdle(){return this.current===null}markClosed(){this.closeResolve?.()}};import{randomUUID as Kd}from"node:crypto";import{randomUUID as X$}from"node:crypto";var z$=new Map([["claude-sonnet-4-5-20250929",{inputPerMTok:3,outputPerMTok:15,cacheWritePerMTok:3.75,cacheReadPerMTok:.3}],["claude-opus-4-5-20250929",{inputPerMTok:15,outputPerMTok:75,cacheWritePerMTok:18.75,cacheReadPerMTok:1.5}],["claude-haiku-4-5-20250929",{inputPerMTok:1,outputPerMTok:5,cacheWritePerMTok:1.25,cacheReadPerMTok:.1}],["claude-haiku-4-5-20251001",{inputPerMTok:1,outputPerMTok:5,cacheWritePerMTok:1.25,cacheReadPerMTok:.1}],["claude-3-7-sonnet-20250219",{inputPerMTok:3,outputPerMTok:15,cacheWritePerMTok:3.75,cacheReadPerMTok:.3}],["claude-3-5-sonnet-20241022",{inputPerMTok:3,outputPerMTok:15,cacheWritePerMTok:3.75,cacheReadPerMTok:.3}],["claude-3-5-sonnet-20240620",{inputPerMTok:3,outputPerMTok:15,cacheWritePerMTok:3.75,cacheReadPerMTok:.3}],["claude-3-5-haiku-20241022",{inputPerMTok:.8,outputPerMTok:4,cacheWritePerMTok:1,cacheReadPerMTok:.08}],["claude-3-opus-20240229",{inputPerMTok:15,outputPerMTok:75,cacheWritePerMTok:18.75,cacheReadPerMTok:1.5}],["claude-3-sonnet-20240229",{inputPerMTok:3,outputPerMTok:15,cacheWritePerMTok:3.75,cacheReadPerMTok:.3}],["claude-3-haiku-20240307",{inputPerMTok:.25,outputPerMTok:1.25,cacheWritePerMTok:.3,cacheReadPerMTok:.03}]]);function J$(e,t,n,r,o){let s=z$.get(e);if(!s)return;let i=1e6,l=Math.max(0,t-r-o)/i*s.inputPerMTok,c=n/i*s.outputPerMTok,u=s.cacheWritePerMTok??s.inputPerMTok*1.25,d=s.cacheReadPerMTok??s.inputPerMTok*.1,m=o/i*u,f=r/i*d;return l+c+m+f}function lw(e,t,n){if(!e)return{stopReason:t??null};let r={inputTokens:e.input_tokens,outputTokens:e.output_tokens,stopReason:t??null};if(e.cache_read_input_tokens!=null&&(r.cachedInputTokens=e.cache_read_input_tokens),e.cache_creation_input_tokens!=null&&(r.cacheCreationTokens=e.cache_creation_input_tokens),r.totalTokens=(e.input_tokens??0)+(e.output_tokens??0),n){let o=J$(n,e.input_tokens??0,e.output_tokens??0,e.cache_read_input_tokens??0,e.cache_creation_input_tokens??0);o!==void 0&&(r.totalCostUsd=o)}return r}G();function V$(e){let t=e.trim();if(t.length===0)return{};try{return JSON.parse(t)}catch{return{}}}function Y$(e,t,n){let r=[],o=[];for(let a of e)a&&(a.kind==="text"?(r.push({type:"text",text:a.text}),o.push(a.text)):a.kind==="thinking"?a.thinking&&a.signature&&r.push({type:"thinking",thinking:a.thinking,signature:a.signature}):r.push({type:"tool_use",id:a.id,name:a.name,input:V$(a.partialJson)}));let s=a=>a.type==="tool_use",i=r.filter(s);return{stopReason:t,assistantBlocks:r,toolUseBlocks:i,usage:n,text:o.join("")}}async function*cw(e,t){let n=[],r=null,o=null,s=!1,i=!!T.AFK_TELEGRAM_TRACE;try{i&&console.log("[translate] starting SDK event iteration");for await(let a of e){switch(i&&console.log("[translate] SDK evt:",a.type),a.type){case"message_start":{let l=a.message?.usage;l&&(o={...l});break}case"content_block_start":{let l=a.content_block;l.type==="text"?n[a.index]={kind:"text",text:""}:l.type==="thinking"?n[a.index]={kind:"thinking",thinking:"",signature:""}:l.type==="tool_use"&&(n[a.index]={kind:"tool_use",id:l.id,name:l.name,partialJson:""},yield{kind:"event",event:{type:"tool.use.start",toolUseId:l.id,toolName:l.name,toolInput:" \u2026",sessionId:t.sessionId}});break}case"content_block_delta":{let l=n[a.index],c=a.delta;c.type==="text_delta"?(l&&l.kind==="text"&&(l.text+=c.text),yield{kind:"event",event:{type:"delta.text",text:c.text,sessionId:t.sessionId}}):c.type==="input_json_delta"?l&&l.kind==="tool_use"&&(l.partialJson+=c.partial_json):c.type==="thinking_delta"?(l&&l.kind==="thinking"&&(l.thinking+=c.thinking),yield{kind:"event",event:{type:"delta.reasoning",text:c.thinking,sessionId:t.sessionId}}):c.type==="signature_delta"&&l&&l.kind==="thinking"&&(l.signature=c.signature);break}case"content_block_stop":{let l=n[a.index];l&&l.kind==="tool_use"&&(yield{kind:"event",event:{type:"tool.use",summary:l.name,toolUseIds:[l.id],sessionId:t.sessionId}});break}case"message_delta":{a.delta&&a.delta.stop_reason!==void 0&&(r=a.delta.stop_reason);let l=a.usage;l&&(o!==null?(o.output_tokens=l.output_tokens,l.cache_creation_input_tokens!=null&&(o.cache_creation_input_tokens=l.cache_creation_input_tokens),l.cache_read_input_tokens!=null&&(o.cache_read_input_tokens=l.cache_read_input_tokens),l.input_tokens!=null&&(o.input_tokens=l.input_tokens)):o={cache_creation:null,cache_creation_input_tokens:l.cache_creation_input_tokens??null,cache_read_input_tokens:l.cache_read_input_tokens??null,inference_geo:null,input_tokens:l.input_tokens??0,output_tokens:l.output_tokens,server_tool_use:null,service_tier:null});break}case"message_stop":{s=!0;break}default:break}if(s)break}i&&console.log("[translate] SDK iteration ended naturally, stopped=",s)}catch(a){i&&console.log("[translate] SDK iteration threw:",a.message),yield{kind:"event",event:{type:"error",error:a instanceof Error?a:new Error(String(a))}};return}i&&console.log("[translate] yielding turn-result"),yield{kind:"turn-result",result:Y$(n,r,o)}}G();var Z$=0;function Q$(e){let{name:t,description:n,input_schema:r}=e;return{name:t,...n!==void 0?{description:n}:{},input_schema:r}}var eD=3,tD=5e3;function nD(e){if(!("status"in e))return!1;let t=e.status;return t===529||t===503}function rD(e,t){return new Promise(n=>{if(t.aborted){n();return}let r=setTimeout(n,e);r.unref(),t.addEventListener("abort",()=>{clearTimeout(r),n()},{once:!0})})}async function oD(e,t,n,r){for(let o=0;;o++){if(o>0){let s=tD*Math.pow(2,o-1);if(await rD(s,r),r.aborted)throw new Error("aborted")}try{return await Promise.resolve(e.messages.create(t,{headers:n,signal:r}))}catch(s){if(r.aborted)throw s;let i=s instanceof Error?s:new Error(String(s));if(nD(i)&&o<eD)continue;throw i}}}function sD(e){if(!e||typeof e!="object")return"";let t=e,n=t.file_path??t.path??t.filePath;if(typeof n=="string")return" "+n;let r=t.command??t.cmd;if(typeof r=="string"){let s=r.split(`
1877
- `)[0];return" "+(s.length>80?s.slice(0,77)+"\u2026":s)}let o=t.query??t.pattern??t.url??t.description;return typeof o=="string"?" "+o:""}async function*Hd(e){let t=e.maxToolUseIterations??Z$,n={stopReason:null},r=0,o=X$(),s=Date.now(),i=a=>({...a,durationMs:Date.now()-s});Gr(e.traceWriter,{phase:"loop_start"});try{for(;;){if(e.signal.aborted){yield{type:"turn.completed",usage:i(n),sessionId:e.ctx.sessionId};return}let a=Ji({baseUrl:e.baseUrl})?_g(e.messages,Vi()):e.messages,l={model:e.model,max_tokens:e.maxTokens,messages:a,stream:!0,...e.system!==null?{system:e.system}:{},...e.tools!==null&&e.tools.length>0?{tools:e.tools.map(Q$)}:{},...e.thinking!==void 0?{thinking:e.thinking}:{},...e.effort!==void 0?{output_config:{effort:e.effort}}:{}},c;try{c=await oD(e.client,l,e.headers,e.signal)}catch(g){if(e.signal.aborted){yield{type:"turn.completed",usage:i(n),sessionId:e.ctx.sessionId};return}let h=g instanceof Error?g:new Error(String(g));h.message.includes("thinking")&&iD(e.messages,h),yield{type:"error",error:h};return}let u=null,d=!1;try{T.AFK_TELEGRAM_TRACE&&console.log("[loop] awaiting translateMessageStream events");for await(let g of cw(c,e.ctx))if(T.AFK_TELEGRAM_TRACE&&console.log("[loop] translate yielded:",g.kind,g.kind==="event"?g.event.type:""),g.kind==="event"){if(g.event.type==="error"){yield g.event,d=!0;break}yield g.event}else{u=g.result;break}T.AFK_TELEGRAM_TRACE&&console.log("[loop] translate loop exited, turnResult=",u?"set":"null")}catch(g){if(e.signal.aborted){yield{type:"turn.completed",usage:i(n),sessionId:e.ctx.sessionId};return}yield{type:"error",error:g instanceof Error?g:new Error(String(g))};return}if(d){e.signal.aborted&&(yield{type:"turn.completed",usage:i(n),sessionId:e.ctx.sessionId});return}if(u===null){yield{type:"turn.completed",usage:i(n),sessionId:e.ctx.sessionId};return}if(n=xs(n,lw(u.usage,u.stopReason,e.model)),e.onUsageProgress?.(n),u.stopReason!=="tool_use"){u.text.length>0&&(yield{type:"assistant.message",text:u.text,sessionId:e.ctx.sessionId},u.text.length<=200&&(yield{type:"suggestion",suggestion:u.text,sessionId:e.ctx.sessionId}));let g=u.assistantBlocks.filter(h=>h.type!=="tool_use");g.length>0&&e.messages.push({role:"assistant",content:g}),yield{type:"turn.completed",usage:i(n),sessionId:e.ctx.sessionId};return}let m=e.messages.length;e.messages.push({role:"assistant",content:u.assistantBlocks});try{let g=[],h=new Map;for(let x of u.toolUseBlocks){g.push({id:x.id,name:x.name,input:x.input,signal:e.signal});let R=Date.now();h.set(x.id,R),Tu(e.traceWriter,{phase:"started",toolUseId:x.id,name:x.name,inputBytes:Buffer.byteLength(JSON.stringify(x.input??{}),"utf8")}),yield{type:"tool.use.start",toolUseId:x.id,toolName:x.name,toolInput:sD(x.input),sessionId:e.ctx.sessionId}}if(e.signal.aborted){let x=g.map(R=>({type:"tool_result",tool_use_id:R.id,content:"Tool call aborted",is_error:!0}));e.messages.push({role:"user",content:x}),yield{type:"turn.completed",usage:i(n),sessionId:e.ctx.sessionId};return}let b;if(e.toolDispatcher.executeBatch)try{b=await e.toolDispatcher.executeBatch(g)}catch(x){b=g.map(()=>({content:`Tool batch execution failed: ${x instanceof Error?x.message:String(x)}`,isError:!0}))}else{b=[];for(let x of g){if(e.signal.aborted){b.push({content:"Tool call aborted",isError:!0});continue}try{b.push(await e.toolDispatcher.execute(x))}catch(R){let v=R instanceof Error?R.message:String(R);b.push({content:`Tool execution threw: ${v}`,isError:!0})}}}let y=[];for(let x=0;x<g.length;x++){let R=g[x],v=b[x],E=h.get(R.id),I=typeof E=="number"?Date.now()-E:0,D=v.truncated===!0||v.content.includes("[output truncated");Tu(e.traceWriter,{phase:"completed",toolUseId:R.id,name:R.name,resultBytes:Buffer.byteLength(v.content,"utf8"),isError:v.isError===!0,truncated:D,durationMs:I}),yield{type:"tool.output",toolUseId:R.id,toolName:R.name,content:v.content,...v.isError===!0?{isError:!0}:{},...D?{truncated:!0}:{},sessionId:e.ctx.sessionId},v.render?.diff&&(yield{type:"tool.diff",toolUseId:R.id,diff:v.render.diff,sessionId:e.ctx.sessionId});let{content:_,isError:M}=v;y.push({type:"tool_result",tool_use_id:R.id,content:_,...M===!0?{is_error:!0}:{}})}let w={role:"user",content:y};e.messages.push(w)}catch(g){throw e.messages.splice(m),g}r+=1;let f=u.toolUseBlocks[u.toolUseBlocks.length-1];if(yield{type:"progress",progress:{taskId:o,description:"Tool-use loop",summary:`Iteration ${r}: used ${f?.name??"unknown"}`,lastToolName:f?.name,totalTokens:n.totalTokens??0,toolUses:r,durationMs:Date.now()-s},sessionId:e.ctx.sessionId},t>0&&r>=t){yield{type:"turn.completed",usage:i({...n,stopReason:"tool_use_loop_capped"}),sessionId:e.ctx.sessionId};return}}}finally{Gr(e.traceWriter,{phase:"loop_end",durationMs:Date.now()-s})}}function iD(e,t){try{let n=[];for(let r=0;r<e.length;r++){let o=e[r];if(o.role!=="assistant"||typeof o.content=="string")continue;let s=o.content;for(let i=0;i<s.length;i++){let a=s[i];if(a.type==="thinking"){let l=a;(!l.thinking||!l.signature)&&n.push({msgIdx:r,blockIdx:i,thinking:l.thinking?`(${l.thinking.length} chars)`:"(empty)",sigLen:l.signature?.length??0})}}}console.error("[afk] thinking-block diagnostic \u2014 API rejected request with:",t.message),console.error(`[afk] messages.length=${e.length}, invalid thinking blocks:`,n.length>0?JSON.stringify(n):"none found (cause may be elsewhere)")}catch{}}function uw(e){if(!("status"in e))return null;let t=e.status;if(t===429){let n=e.message.split("|");if(n.length>=2){let r=parseInt(n[1].trim(),10);if(!isNaN(r)&&r>0)return{kind:"oauth-limit",resetsAt:new Date(r*1e3)}}return{kind:"oauth-limit-no-ts"}}return t===400&&e.message.includes("invalid_request_error")&&e.message.includes("credit balance")?{kind:"credit-exhausted"}:null}async function dw(e){let{resetsAt:t,signal:n,readToken:r=ze}=e,o=r(),s=t.getTime()+3e4;return new Promise(i=>{let a=()=>n.aborted?(i("aborted"),!0):Date.now()>=s?(i("timer"),!0):r()!==o?(i("hot-swap"),!0):!1;if(a())return;let l=setInterval(()=>{a()&&clearInterval(l)},3e4);l.unref(),n.addEventListener("abort",()=>{clearInterval(l),i("aborted")},{once:!0})})}async function pw(e){let{signal:t,readToken:n=ze}=e,r=n();return new Promise(o=>{let s=()=>t.aborted?(o("aborted"),!0):n()!==r?(o("hot-swap"),!0):!1;if(s())return;let i=setInterval(()=>{s()&&clearInterval(i)},3e4);t.addEventListener("abort",()=>{clearInterval(i),o("aborted")},{once:!0})})}var aD=7200*1e3,Na=class{_client;_authMode;initSessionId;tokenRefresher;autoResumeOnUsageLimit;refreshPromise=null;usageLimitWaitPromise=null;constructor(t){this._client=t.client,this._authMode=t.authMode,this.initSessionId=t.initSessionId,this.tokenRefresher=t.tokenRefresher,this.autoResumeOnUsageLimit=t.autoResumeOnUsageLimit}get client(){return this._client}get authMode(){return this._authMode}async forceClientRefresh(){if(!this.tokenRefresher)return null;let t=ze(),n=null;try{if(this.refreshPromise)n=await this.refreshPromise;else{this.refreshPromise=this.tokenRefresher();try{n=await this.refreshPromise??null}finally{this.refreshPromise=null}}}catch{return this.refreshPromise=null,null}if(!n)return null;this._client=n;let r=ze();return{accountId:An(r??""),swapped:t!==r}}async*turnWithRetries(t,n){yield*this.turnWithUsageLimitRetry(t,n)}async*turnWithUsageLimitRetry(t,n){let r=null,o=null,s=!1;for await(let c of this.turnWithAuthRetry(t,n)){if(c.type==="error"){let u=uw(c.error);if(u&&u.kind==="oauth-limit"){o=u.resetsAt,r=c;break}if(u&&u.kind==="oauth-limit-no-ts"){s=!0,r=c;break}}yield c}if(!r)return;if(s){if(yield{type:"paused",reason:"usage-limit",accountId:An(ze()??""),autoResume:this.autoResumeOnUsageLimit},!this.autoResumeOnUsageLimit){yield r;return}let u;if(this.usageLimitWaitPromise)u="aborted";else{this.usageLimitWaitPromise=pw({signal:t.signal});try{u=await this.usageLimitWaitPromise}finally{this.usageLimitWaitPromise=null}}if(u==="aborted")return;let d=await this.forceClientRefresh();if(!d){yield r;return}t.client=this._client,t.headers=Lt(this._authMode,this.initSessionId,Kd()),yield{type:"resumed",hotSwapped:!0,accountId:d.accountId},yield*this.turnWithAuthRetry(t,n);return}if(!o)return;if(o.getTime()-Date.now()>aD){yield r;return}let i=An(ze()??"");if(yield{type:"paused",reason:"usage-limit",resetsAt:o,accountId:i,autoResume:this.autoResumeOnUsageLimit},!this.autoResumeOnUsageLimit){yield r;return}let a;if(this.usageLimitWaitPromise)a=await this.usageLimitWaitPromise;else{this.usageLimitWaitPromise=dw({resetsAt:o,signal:t.signal});try{a=await this.usageLimitWaitPromise}finally{this.usageLimitWaitPromise=null}}if(a==="aborted")return;let l=i;if(a==="hot-swap"){let c=await this.forceClientRefresh();c&&(t.client=this._client,l=c.accountId)}t.headers=Lt(this._authMode,this.initSessionId,Kd()),yield{type:"resumed",hotSwapped:a==="hot-swap",accountId:l},yield*this.turnWithAuthRetry(t,n)}async*turnWithAuthRetry(t,n){let r=null;for await(let s of Hd(t)){if(n())return;if(s.type==="error"&&this.isRetryableAuth(s.error)){r=s;break}yield s}if(!r)return;if(!await this.forceClientRefresh()){yield r;return}t.client=this._client,t.headers=Lt(this._authMode,this.initSessionId,Kd()),yield*Hd(t)}isRetryableAuth(t){return this._authMode==="oauth"&&this.tokenRefresher!==void 0&&"status"in t&&t.status===401}};import{randomUUID as pD}from"node:crypto";var lD=["You are a conversation-summarization assistant. The user will paste a","prior conversation between a user and an AI assistant that includes tool","calls and tool results. Produce a concise but complete summary that lets","the AI continue the conversation without losing track.","","Preserve, in this priority order:","1. The user's original intent, explicit asks, constraints, corrections,"," and preferences stated during the conversation.","2. Tool decisions and their outcomes \u2014 file paths read or written, shell"," commands run, search queries, URLs fetched, code edits made, tests"," run, errors observed, and whether each action succeeded or failed.","3. Current state: what has been completed, what remains unresolved, and"," the safest next action.","4. Open questions, pending decisions, blockers, and assumptions.","5. Key facts the assistant discovered (function locations, schemas,"," observed behaviors, important external findings).","","Drop prose narration, conversational filler, and exploratory dead-ends.","Drop verbatim tool output unless an exact snippet, error, path, command,","or result is needed for continuation.","Do not invent details. If something is uncertain, mark it explicitly.","Output plain text, no markdown headers. Aim for ~250 words; use up to","~400 only when needed to preserve tool state or unresolved tasks."].join(`
1875
+ `);for(let l of a){if(!l)continue;let c=l.indexOf(":");if(c===-1)continue;let u=l.slice(0,c).trim(),d=l.slice(c+1).trim();if(u==="name")i.name=d.replace(/^["']|["']$/g,"");else if(u==="description")i.description=d.replace(/^["']|["']$/g,"");else if(u==="argumentHint")i.argumentHint=d.replace(/^["']|["']$/g,"");else if(u==="audience"){let p=d.replace(/^["']|["']$/g,"");(p==="public"||p==="internal")&&(i.audience=p)}else u==="context"&&(i.context=d.replace(/^["']|["']$/g,""))}return s.length>0&&(i.body=s),i}catch{return{}}}W();G();function sw(e){let t=an(e);if(t.length===0)return"";let n=[];for(let r of t){let o=r.argumentHint?`${r.argumentHint}`:"",s=o?`- \`${r.name} ${o}\`: ${r.description}`:`- ${r.name}: ${r.description}`;n.push(s),r.whenToUse&&n.push(` When to use: ${r.whenToUse}`)}return["Available skills (invoke via the `skill` tool):","","Each skill either dispatches one or more context-isolated subagents (delegation \u2014 preserves the main session's context) or loads its instructions directly into your current context (`load` mode). Calling `skill` is the entry point for both; the executor picks the mode per skill. Prefer a skill over inline investigation when the task shape matches.","",...n].join(`
1876
+ `)}function an(e){let t=[],n=new Set,r=T.AFK_INTERNAL==="1";for(let s of ft()){let i=$e(s);Et(i,r)&&(t.push({name:s,description:i.description,source:i.origin==="user"?"user":i.origin==="project"?"project":"builtin",argumentHint:i.argumentHint,whenToUse:i.whenToUse}),n.add(s))}let o=e??[...At(os()),...At(),...At(is())];for(let s of o){if(s.type!=="local")continue;let i=Wd(s.path);for(let a of i)!a.name||n.has(a.name)||Et({audience:a.audience},r)&&(t.push({name:a.name,description:a.description??`Skill from plugin at ${s.path}`,source:"plugin"}),n.add(a.name))}return t}function va(e){let t=new Map,n=e??[...At(os()),...At(),...At(is())];for(let r of n){if(r.type!=="local")continue;let o=Wd(r.path);for(let s of o)s.name&&s.body&&s.body.length>0&&!t.has(s.name)&&t.set(s.name,{body:s.body,pluginPath:r.path,...s.context!==void 0?{context:s.context}:{}})}return t}function iw(e){if(e.length===0)return;let t=e[e.length-1];if(!t||t.role!=="assistant"||typeof t.content=="string")return;let n=t.content,r=[];for(let s of n)s.type==="tool_use"&&typeof s.id=="string"&&r.push(s.id);if(r.length===0)return;let o={role:"user",content:r.map(s=>({type:"tool_result",tool_use_id:s,content:"Tool call interrupted before completing \u2014 no result recorded.",is_error:!0}))};e.push(o)}function aw(e){return{messages:e.initialMessages?[...e.initialMessages]:[],currentModel:e.model,requestedModel:e.requestedModel??e.model,currentPermissionMode:e.permissionMode,userSystem:e.userSystem,toolDispatcher:e.toolDispatcher,lastUsage:null,closed:!1,autoCompactThreshold:e.autoCompactThreshold}}var q$="__closed__",Fa=class{current=null;pendingReason=null;closedPromise;closeResolve=null;constructor(){this.closedPromise=new Promise(t=>{this.closeResolve=()=>t(q$)})}begin(){let t=new AbortController;return this.current=t,this.pendingReason!==null&&!t.signal.aborted&&(t.abort(this.pendingReason),this.pendingReason=null),t}clear(t){this.current===t&&(this.current=null)}requestAbort(t){let n=this.current;if(n&&!n.signal.aborted){n.abort(t);return}this.pendingReason=t}isIdle(){return this.current===null}markClosed(){this.closeResolve?.()}};import{randomUUID as Kd}from"node:crypto";import{randomUUID as X$}from"node:crypto";var z$=new Map([["claude-sonnet-4-5-20250929",{inputPerMTok:3,outputPerMTok:15,cacheWritePerMTok:3.75,cacheReadPerMTok:.3}],["claude-opus-4-5-20250929",{inputPerMTok:15,outputPerMTok:75,cacheWritePerMTok:18.75,cacheReadPerMTok:1.5}],["claude-haiku-4-5-20250929",{inputPerMTok:1,outputPerMTok:5,cacheWritePerMTok:1.25,cacheReadPerMTok:.1}],["claude-haiku-4-5-20251001",{inputPerMTok:1,outputPerMTok:5,cacheWritePerMTok:1.25,cacheReadPerMTok:.1}],["claude-3-7-sonnet-20250219",{inputPerMTok:3,outputPerMTok:15,cacheWritePerMTok:3.75,cacheReadPerMTok:.3}],["claude-3-5-sonnet-20241022",{inputPerMTok:3,outputPerMTok:15,cacheWritePerMTok:3.75,cacheReadPerMTok:.3}],["claude-3-5-sonnet-20240620",{inputPerMTok:3,outputPerMTok:15,cacheWritePerMTok:3.75,cacheReadPerMTok:.3}],["claude-3-5-haiku-20241022",{inputPerMTok:.8,outputPerMTok:4,cacheWritePerMTok:1,cacheReadPerMTok:.08}],["claude-3-opus-20240229",{inputPerMTok:15,outputPerMTok:75,cacheWritePerMTok:18.75,cacheReadPerMTok:1.5}],["claude-3-sonnet-20240229",{inputPerMTok:3,outputPerMTok:15,cacheWritePerMTok:3.75,cacheReadPerMTok:.3}],["claude-3-haiku-20240307",{inputPerMTok:.25,outputPerMTok:1.25,cacheWritePerMTok:.3,cacheReadPerMTok:.03}]]);function J$(e,t,n,r,o){let s=z$.get(e);if(!s)return;let i=1e6,l=Math.max(0,t-r-o)/i*s.inputPerMTok,c=n/i*s.outputPerMTok,u=s.cacheWritePerMTok??s.inputPerMTok*1.25,d=s.cacheReadPerMTok??s.inputPerMTok*.1,p=o/i*u,f=r/i*d;return l+c+p+f}function lw(e,t,n){if(!e)return{stopReason:t??null};let r={inputTokens:e.input_tokens,outputTokens:e.output_tokens,stopReason:t??null};if(e.cache_read_input_tokens!=null&&(r.cachedInputTokens=e.cache_read_input_tokens),e.cache_creation_input_tokens!=null&&(r.cacheCreationTokens=e.cache_creation_input_tokens),r.totalTokens=(e.input_tokens??0)+(e.output_tokens??0),n){let o=J$(n,e.input_tokens??0,e.output_tokens??0,e.cache_read_input_tokens??0,e.cache_creation_input_tokens??0);o!==void 0&&(r.totalCostUsd=o)}return r}G();function V$(e){let t=e.trim();if(t.length===0)return{};try{return JSON.parse(t)}catch{return{}}}function Y$(e,t,n){let r=[],o=[];for(let a of e)a&&(a.kind==="text"?(r.push({type:"text",text:a.text}),o.push(a.text)):a.kind==="thinking"?a.thinking&&a.signature&&r.push({type:"thinking",thinking:a.thinking,signature:a.signature}):r.push({type:"tool_use",id:a.id,name:a.name,input:V$(a.partialJson)}));let s=a=>a.type==="tool_use",i=r.filter(s);return{stopReason:t,assistantBlocks:r,toolUseBlocks:i,usage:n,text:o.join("")}}async function*cw(e,t){let n=[],r=null,o=null,s=!1,i=!!T.AFK_TELEGRAM_TRACE;try{i&&console.log("[translate] starting SDK event iteration");for await(let a of e){switch(i&&console.log("[translate] SDK evt:",a.type),a.type){case"message_start":{let l=a.message?.usage;l&&(o={...l});break}case"content_block_start":{let l=a.content_block;l.type==="text"?n[a.index]={kind:"text",text:""}:l.type==="thinking"?n[a.index]={kind:"thinking",thinking:"",signature:""}:l.type==="tool_use"&&(n[a.index]={kind:"tool_use",id:l.id,name:l.name,partialJson:""},yield{kind:"event",event:{type:"tool.use.start",toolUseId:l.id,toolName:l.name,toolInput:" \u2026",sessionId:t.sessionId}});break}case"content_block_delta":{let l=n[a.index],c=a.delta;c.type==="text_delta"?(l&&l.kind==="text"&&(l.text+=c.text),yield{kind:"event",event:{type:"delta.text",text:c.text,sessionId:t.sessionId}}):c.type==="input_json_delta"?l&&l.kind==="tool_use"&&(l.partialJson+=c.partial_json):c.type==="thinking_delta"?(l&&l.kind==="thinking"&&(l.thinking+=c.thinking),yield{kind:"event",event:{type:"delta.reasoning",text:c.thinking,sessionId:t.sessionId}}):c.type==="signature_delta"&&l&&l.kind==="thinking"&&(l.signature=c.signature);break}case"content_block_stop":{let l=n[a.index];l&&l.kind==="tool_use"&&(yield{kind:"event",event:{type:"tool.use",summary:l.name,toolUseIds:[l.id],sessionId:t.sessionId}});break}case"message_delta":{a.delta&&a.delta.stop_reason!==void 0&&(r=a.delta.stop_reason);let l=a.usage;l&&(o!==null?(o.output_tokens=l.output_tokens,l.cache_creation_input_tokens!=null&&(o.cache_creation_input_tokens=l.cache_creation_input_tokens),l.cache_read_input_tokens!=null&&(o.cache_read_input_tokens=l.cache_read_input_tokens),l.input_tokens!=null&&(o.input_tokens=l.input_tokens)):o={cache_creation:null,cache_creation_input_tokens:l.cache_creation_input_tokens??null,cache_read_input_tokens:l.cache_read_input_tokens??null,inference_geo:null,input_tokens:l.input_tokens??0,output_tokens:l.output_tokens,server_tool_use:null,service_tier:null});break}case"message_stop":{s=!0;break}default:break}if(s)break}i&&console.log("[translate] SDK iteration ended naturally, stopped=",s)}catch(a){i&&console.log("[translate] SDK iteration threw:",a.message),yield{kind:"event",event:{type:"error",error:a instanceof Error?a:new Error(String(a))}};return}i&&console.log("[translate] yielding turn-result"),yield{kind:"turn-result",result:Y$(n,r,o)}}G();var Z$=0;function Q$(e){let{name:t,description:n,input_schema:r}=e;return{name:t,...n!==void 0?{description:n}:{},input_schema:r}}var eD=3,tD=5e3;function nD(e){if(!("status"in e))return!1;let t=e.status;return t===529||t===503}function rD(e,t){return new Promise(n=>{if(t.aborted){n();return}let r=setTimeout(n,e);r.unref(),t.addEventListener("abort",()=>{clearTimeout(r),n()},{once:!0})})}async function oD(e,t,n,r){for(let o=0;;o++){if(o>0){let s=tD*Math.pow(2,o-1);if(await rD(s,r),r.aborted)throw new Error("aborted")}try{return await Promise.resolve(e.messages.create(t,{headers:n,signal:r}))}catch(s){if(r.aborted)throw s;let i=s instanceof Error?s:new Error(String(s));if(nD(i)&&o<eD)continue;throw i}}}function sD(e){if(!e||typeof e!="object")return"";let t=e,n=t.file_path??t.path??t.filePath;if(typeof n=="string")return" "+n;let r=t.command??t.cmd;if(typeof r=="string"){let s=r.split(`
1877
+ `)[0];return" "+(s.length>80?s.slice(0,77)+"\u2026":s)}let o=t.query??t.pattern??t.url??t.description;return typeof o=="string"?" "+o:""}async function*Hd(e){let t=e.maxToolUseIterations??Z$,n={stopReason:null},r=0,o=X$(),s=Date.now(),i=a=>({...a,durationMs:Date.now()-s});Gr(e.traceWriter,{phase:"loop_start"});try{for(;;){if(e.signal.aborted){yield{type:"turn.completed",usage:i(n),sessionId:e.ctx.sessionId};return}let a=Ji({baseUrl:e.baseUrl})?_g(e.messages,Vi()):e.messages,l={model:e.model,max_tokens:e.maxTokens,messages:a,stream:!0,...e.system!==null?{system:e.system}:{},...e.tools!==null&&e.tools.length>0?{tools:e.tools.map(Q$)}:{},...e.thinking!==void 0?{thinking:e.thinking}:{},...e.effort!==void 0?{output_config:{effort:e.effort}}:{}},c;try{c=await oD(e.client,l,e.headers,e.signal)}catch(g){if(e.signal.aborted){yield{type:"turn.completed",usage:i(n),sessionId:e.ctx.sessionId};return}let h=g instanceof Error?g:new Error(String(g));h.message.includes("thinking")&&iD(e.messages,h),yield{type:"error",error:h};return}let u=null,d=!1;try{T.AFK_TELEGRAM_TRACE&&console.log("[loop] awaiting translateMessageStream events");for await(let g of cw(c,e.ctx))if(T.AFK_TELEGRAM_TRACE&&console.log("[loop] translate yielded:",g.kind,g.kind==="event"?g.event.type:""),g.kind==="event"){if(g.event.type==="error"){yield g.event,d=!0;break}yield g.event}else{u=g.result;break}T.AFK_TELEGRAM_TRACE&&console.log("[loop] translate loop exited, turnResult=",u?"set":"null")}catch(g){if(e.signal.aborted){yield{type:"turn.completed",usage:i(n),sessionId:e.ctx.sessionId};return}yield{type:"error",error:g instanceof Error?g:new Error(String(g))};return}if(d){e.signal.aborted&&(yield{type:"turn.completed",usage:i(n),sessionId:e.ctx.sessionId});return}if(u===null){yield{type:"turn.completed",usage:i(n),sessionId:e.ctx.sessionId};return}if(n=xs(n,lw(u.usage,u.stopReason,e.model)),e.onUsageProgress?.(n),u.stopReason!=="tool_use"){u.text.length>0&&(yield{type:"assistant.message",text:u.text,sessionId:e.ctx.sessionId},u.text.length<=200&&(yield{type:"suggestion",suggestion:u.text,sessionId:e.ctx.sessionId}));let g=u.assistantBlocks.filter(h=>h.type!=="tool_use");g.length>0&&e.messages.push({role:"assistant",content:g}),yield{type:"turn.completed",usage:i(n),sessionId:e.ctx.sessionId};return}let p=e.messages.length;e.messages.push({role:"assistant",content:u.assistantBlocks});try{let g=[],h=new Map;for(let x of u.toolUseBlocks){g.push({id:x.id,name:x.name,input:x.input,signal:e.signal});let R=Date.now();h.set(x.id,R),Tu(e.traceWriter,{phase:"started",toolUseId:x.id,name:x.name,inputBytes:Buffer.byteLength(JSON.stringify(x.input??{}),"utf8")}),yield{type:"tool.use.start",toolUseId:x.id,toolName:x.name,toolInput:sD(x.input),sessionId:e.ctx.sessionId}}if(e.signal.aborted){let x=g.map(R=>({type:"tool_result",tool_use_id:R.id,content:"Tool call aborted",is_error:!0}));e.messages.push({role:"user",content:x}),yield{type:"turn.completed",usage:i(n),sessionId:e.ctx.sessionId};return}let b;if(e.toolDispatcher.executeBatch)try{b=await e.toolDispatcher.executeBatch(g)}catch(x){b=g.map(()=>({content:`Tool batch execution failed: ${x instanceof Error?x.message:String(x)}`,isError:!0}))}else{b=[];for(let x of g){if(e.signal.aborted){b.push({content:"Tool call aborted",isError:!0});continue}try{b.push(await e.toolDispatcher.execute(x))}catch(R){let v=R instanceof Error?R.message:String(R);b.push({content:`Tool execution threw: ${v}`,isError:!0})}}}let y=[];for(let x=0;x<g.length;x++){let R=g[x],v=b[x],E=h.get(R.id),I=typeof E=="number"?Date.now()-E:0,D=v.truncated===!0||v.content.includes("[output truncated");Tu(e.traceWriter,{phase:"completed",toolUseId:R.id,name:R.name,resultBytes:Buffer.byteLength(v.content,"utf8"),isError:v.isError===!0,truncated:D,durationMs:I}),yield{type:"tool.output",toolUseId:R.id,toolName:R.name,content:v.content,...v.isError===!0?{isError:!0}:{},...D?{truncated:!0}:{},sessionId:e.ctx.sessionId},v.render?.diff&&(yield{type:"tool.diff",toolUseId:R.id,diff:v.render.diff,sessionId:e.ctx.sessionId});let{content:_,isError:M}=v;y.push({type:"tool_result",tool_use_id:R.id,content:_,...M===!0?{is_error:!0}:{}})}let w={role:"user",content:y};e.messages.push(w)}catch(g){throw e.messages.splice(p),g}r+=1;let f=u.toolUseBlocks[u.toolUseBlocks.length-1];if(yield{type:"progress",progress:{taskId:o,description:"Tool-use loop",summary:`Iteration ${r}: used ${f?.name??"unknown"}`,lastToolName:f?.name,totalTokens:n.totalTokens??0,toolUses:r,durationMs:Date.now()-s},sessionId:e.ctx.sessionId},t>0&&r>=t){yield{type:"turn.completed",usage:i({...n,stopReason:"tool_use_loop_capped"}),sessionId:e.ctx.sessionId};return}}}finally{Gr(e.traceWriter,{phase:"loop_end",durationMs:Date.now()-s})}}function iD(e,t){try{let n=[];for(let r=0;r<e.length;r++){let o=e[r];if(o.role!=="assistant"||typeof o.content=="string")continue;let s=o.content;for(let i=0;i<s.length;i++){let a=s[i];if(a.type==="thinking"){let l=a;(!l.thinking||!l.signature)&&n.push({msgIdx:r,blockIdx:i,thinking:l.thinking?`(${l.thinking.length} chars)`:"(empty)",sigLen:l.signature?.length??0})}}}console.error("[afk] thinking-block diagnostic \u2014 API rejected request with:",t.message),console.error(`[afk] messages.length=${e.length}, invalid thinking blocks:`,n.length>0?JSON.stringify(n):"none found (cause may be elsewhere)")}catch{}}function uw(e){if(!("status"in e))return null;let t=e.status;if(t===429){let n=e.message.split("|");if(n.length>=2){let r=parseInt(n[1].trim(),10);if(!isNaN(r)&&r>0)return{kind:"oauth-limit",resetsAt:new Date(r*1e3)}}return{kind:"oauth-limit-no-ts"}}return t===400&&e.message.includes("invalid_request_error")&&e.message.includes("credit balance")?{kind:"credit-exhausted"}:null}async function dw(e){let{resetsAt:t,signal:n,readToken:r=ze}=e,o=r(),s=t.getTime()+3e4;return new Promise(i=>{let a=()=>n.aborted?(i("aborted"),!0):Date.now()>=s?(i("timer"),!0):r()!==o?(i("hot-swap"),!0):!1;if(a())return;let l=setInterval(()=>{a()&&clearInterval(l)},3e4);l.unref(),n.addEventListener("abort",()=>{clearInterval(l),i("aborted")},{once:!0})})}async function pw(e){let{signal:t,readToken:n=ze}=e,r=n();return new Promise(o=>{let s=()=>t.aborted?(o("aborted"),!0):n()!==r?(o("hot-swap"),!0):!1;if(s())return;let i=setInterval(()=>{s()&&clearInterval(i)},3e4);t.addEventListener("abort",()=>{clearInterval(i),o("aborted")},{once:!0})})}var aD=7200*1e3,Na=class{_client;_authMode;initSessionId;tokenRefresher;autoResumeOnUsageLimit;refreshPromise=null;usageLimitWaitPromise=null;constructor(t){this._client=t.client,this._authMode=t.authMode,this.initSessionId=t.initSessionId,this.tokenRefresher=t.tokenRefresher,this.autoResumeOnUsageLimit=t.autoResumeOnUsageLimit}get client(){return this._client}get authMode(){return this._authMode}async forceClientRefresh(){if(!this.tokenRefresher)return null;let t=ze(),n=null;try{if(this.refreshPromise)n=await this.refreshPromise;else{this.refreshPromise=this.tokenRefresher();try{n=await this.refreshPromise??null}finally{this.refreshPromise=null}}}catch{return this.refreshPromise=null,null}if(!n)return null;this._client=n;let r=ze();return{accountId:An(r??""),swapped:t!==r}}async*turnWithRetries(t,n){yield*this.turnWithUsageLimitRetry(t,n)}async*turnWithUsageLimitRetry(t,n){let r=null,o=null,s=!1;for await(let c of this.turnWithAuthRetry(t,n)){if(c.type==="error"){let u=uw(c.error);if(u&&u.kind==="oauth-limit"){o=u.resetsAt,r=c;break}if(u&&u.kind==="oauth-limit-no-ts"){s=!0,r=c;break}}yield c}if(!r)return;if(s){if(yield{type:"paused",reason:"usage-limit",accountId:An(ze()??""),autoResume:this.autoResumeOnUsageLimit},!this.autoResumeOnUsageLimit){yield r;return}let u;if(this.usageLimitWaitPromise)u="aborted";else{this.usageLimitWaitPromise=pw({signal:t.signal});try{u=await this.usageLimitWaitPromise}finally{this.usageLimitWaitPromise=null}}if(u==="aborted")return;let d=await this.forceClientRefresh();if(!d){yield r;return}t.client=this._client,t.headers=Lt(this._authMode,this.initSessionId,Kd()),yield{type:"resumed",hotSwapped:!0,accountId:d.accountId},yield*this.turnWithAuthRetry(t,n);return}if(!o)return;if(o.getTime()-Date.now()>aD){yield r;return}let i=An(ze()??"");if(yield{type:"paused",reason:"usage-limit",resetsAt:o,accountId:i,autoResume:this.autoResumeOnUsageLimit},!this.autoResumeOnUsageLimit){yield r;return}let a;if(this.usageLimitWaitPromise)a=await this.usageLimitWaitPromise;else{this.usageLimitWaitPromise=dw({resetsAt:o,signal:t.signal});try{a=await this.usageLimitWaitPromise}finally{this.usageLimitWaitPromise=null}}if(a==="aborted")return;let l=i;if(a==="hot-swap"){let c=await this.forceClientRefresh();c&&(t.client=this._client,l=c.accountId)}t.headers=Lt(this._authMode,this.initSessionId,Kd()),yield{type:"resumed",hotSwapped:a==="hot-swap",accountId:l},yield*this.turnWithAuthRetry(t,n)}async*turnWithAuthRetry(t,n){let r=null;for await(let s of Hd(t)){if(n())return;if(s.type==="error"&&this.isRetryableAuth(s.error)){r=s;break}yield s}if(!r)return;if(!await this.forceClientRefresh()){yield r;return}t.client=this._client,t.headers=Lt(this._authMode,this.initSessionId,Kd()),yield*Hd(t)}isRetryableAuth(t){return this._authMode==="oauth"&&this.tokenRefresher!==void 0&&"status"in t&&t.status===401}};import{randomUUID as pD}from"node:crypto";var lD=["You are a conversation-summarization assistant. The user will paste a","prior conversation between a user and an AI assistant that includes tool","calls and tool results. Produce a concise but complete summary that lets","the AI continue the conversation without losing track.","","Preserve, in this priority order:","1. The user's original intent, explicit asks, constraints, corrections,"," and preferences stated during the conversation.","2. Tool decisions and their outcomes \u2014 file paths read or written, shell"," commands run, search queries, URLs fetched, code edits made, tests"," run, errors observed, and whether each action succeeded or failed.","3. Current state: what has been completed, what remains unresolved, and"," the safest next action.","4. Open questions, pending decisions, blockers, and assumptions.","5. Key facts the assistant discovered (function locations, schemas,"," observed behaviors, important external findings).","","Drop prose narration, conversational filler, and exploratory dead-ends.","Drop verbatim tool output unless an exact snippet, error, path, command,","or result is needed for continuation.","Do not invent details. If something is uncertain, mark it explicitly.","Output plain text, no markdown headers. Aim for ~250 words; use up to","~400 only when needed to preserve tool state or unresolved tasks."].join(`
1878
1878
  `),mw="[Compacted summary of earlier conversation]",fw="Acknowledged. Continuing from the summary above.";function cD(e){if(e.role!=="user")return!1;let t=e.content;if(typeof t=="string")return!0;if(!Array.isArray(t))return!1;for(let n of t)if(n.type==="tool_result")return!1;return!0}function gw(e,t){if(t<=0)return e.length;let n=0;for(let r=e.length-1;r>=0;r--){let o=e[r];if(o&&cD(o)&&(n+=1,n===t))return r}return-1}function hw(e,t,n){let r=uD(e);return{model:t,max_tokens:n,system:lD,messages:[{role:"user",content:`Summarize the following conversation transcript. Follow the system instructions exactly.
1879
1879
 
1880
1880
  <transcript>
@@ -1882,7 +1882,7 @@ Create a ship-ready summary with next steps.`,c=await a.runToResult(l);if(c.stat
1882
1882
  </transcript>`}],stream:!0}}function yw(e,t,n){return[{role:"user",content:mw+`
1883
1883
 
1884
1884
  `+n},{role:"assistant",content:fw},...e.slice(t)]}function bw(e,t,n){let r=dD(e.slice(0,t)),o=mw.length+2+n.length+fw.length,s=Math.max(0,r-o);return Math.round(s/4)}function uD(e){let t=[];for(let n of e){let r=n.role==="user"?"User":"Assistant";if(t.push(r+":"),typeof n.content=="string")t.push(n.content);else if(Array.isArray(n.content))for(let o of n.content){let s=o.type;if(s==="text"&&"text"in o)t.push(o.text);else if(s==="tool_use"){let i=o.name??"unknown",a=ww(o.input);t.push(`[tool call: ${i} ${a}]`)}else if(s==="tool_result"){let i=o.content;t.push(`[tool result: ${Sw(i)}]`)}else s==="image"?t.push("[image]"):s==="document"&&t.push("[document]")}t.push("")}return t.join(`
1885
- `).trim()}function ww(e){try{let t=JSON.stringify(e);return t.length>240?t.slice(0,237)+"...":t}catch{return"{}"}}function Sw(e){if(typeof e=="string")return e.length>320?e.slice(0,317)+"...":e;if(Array.isArray(e)){let t=[];for(let r of e)r.type==="text"&&"text"in r&&t.push(r.text);let n=t.join(" ");return n.length>320?n.slice(0,317)+"...":n}return""}function dD(e){let t=0;for(let n of e)if(typeof n.content=="string")t+=n.content.length;else if(Array.isArray(n.content))for(let r of n.content){let o=r.type;o==="text"&&"text"in r?t+=r.text.length:o==="tool_use"?t+=ww(r.input).length:o==="tool_result"&&(t+=Sw(r.content).length)}return t}G();var mD=2,fD="claude-haiku-4-5-20251001",gD=1024;async function kw(e){let{state:t,abort:n,retry:r,initSessionId:o,traceWriter:s}=e,i=t.messages.length;if(t.closed)return{compacted:!1,reason:"session-closed",messagesBefore:i,messagesAfter:i};if(!n.isIdle())return{compacted:!1,reason:"turn-in-flight",messagesBefore:i,messagesAfter:i};let a=hD(),l=gw(t.messages,a);if(l<0)return{compacted:!1,reason:"history-too-short",messagesBefore:i,messagesAfter:i};if(l===0)return{compacted:!1,reason:"nothing-to-summarize",messagesBefore:i,messagesAfter:i};let c=t.messages.slice(0,l),u=yD(),d=hw(c,u,gD),m=n.begin(),f;try{if(m.signal.aborted)return{compacted:!1,reason:"aborted",messagesBefore:i,messagesAfter:i};let y=Lt(r.authMode,o,pD()),w=r.client,x=await Promise.resolve(w.messages.create(d,{headers:y,signal:m.signal}));f=await bD(x)}catch(y){return m.signal.aborted?{compacted:!1,reason:"aborted",messagesBefore:i,messagesAfter:i}:{compacted:!1,reason:"summarization-failed: "+(y instanceof Error?y.message:String(y)),messagesBefore:i,messagesAfter:i}}finally{n.clear(m)}if(f.trim().length===0)return{compacted:!1,reason:"empty-summary",messagesBefore:i,messagesAfter:i};let g=bw(t.messages,l,f),h=yw(t.messages,l,f);t.messages.splice(0,t.messages.length,...h);let b=t.messages.length;return yg(s,{trigger:"manual",preCompactionMessages:c,summary:f,keptTailCount:i-l,keepLastNConfig:a,messagesBefore:i,messagesAfter:b,tokensSavedEstimate:g}),{compacted:!0,messagesBefore:i,messagesAfter:b,tokensSavedEstimate:g}}function hD(){let e=T.AFK_COMPACT_KEEP_LAST_TURNS;if(e!==void 0&&e.length>0){let t=Number.parseInt(e,10);if(Number.isFinite(t)&&t>0)return t}return mD}function yD(){let e=T.AFK_COMPACT_MODEL;return e!==void 0&&e.length>0?e:fD}async function bD(e){let t="";for await(let n of e)if(n.type==="content_block_delta"){let r=n.delta;r.type==="text_delta"&&typeof r.text=="string"&&(t+=r.text)}return t}var wD=[{value:"claude-sonnet-4-5-20250929",displayName:"Claude Sonnet 4.5",description:"Latest balanced Claude \u2014 recommended default"},{value:"claude-opus-4-5-20250929",displayName:"Claude Opus 4.5",description:"Highest-capability Claude"},{value:"claude-haiku-4-5-20250929",displayName:"Claude Haiku 4.5",description:"Fastest, cheapest Claude"}],Ba=class{initSessionId;promptStream;maxTokens;tools;systemPrefix;thinking;effort;baseUrl;traceWriter;state;abort;retry;cwdDependentsFactory;mcpManager;constructor(t){this.initSessionId=t.sessionId??vw(),this.promptStream=t.promptStream,this.maxTokens=t.maxTokens,this.tools=t.tools,this.systemPrefix=t.systemPrefix,this.thinking=t.thinking,t.effort!==void 0&&(this.effort=t.effort),t.baseUrl!==void 0&&(this.baseUrl=t.baseUrl),this.traceWriter=t.traceWriter,this.cwdDependentsFactory=t.cwdDependentsFactory,this.mcpManager=t.mcpManager,this.retry=new Na({client:t.client,authMode:t.authMode,initSessionId:this.initSessionId,...t.tokenRefresher?{tokenRefresher:t.tokenRefresher}:{},autoResumeOnUsageLimit:t.autoResumeOnUsageLimit??!0}),this.state=aw({model:t.model,...t.requestedModel!==void 0?{requestedModel:t.requestedModel}:{},permissionMode:t.permissionMode??"default",userSystem:t.userSystem,toolDispatcher:t.toolDispatcher,...t.initialMessages?{initialMessages:t.initialMessages}:{},...t.autoCompactThreshold!==void 0?{autoCompactThreshold:t.autoCompactThreshold}:{}}),this.abort=new Fa}async*[Symbol.asyncIterator](){yield{type:"session.init",info:{sessionId:this.initSessionId,model:this.state.currentModel,permissionMode:this.state.currentPermissionMode,cwd:process.cwd(),tools:[],slashCommands:[],skills:[],plugins:[],mcpServers:this.mcpManager?.getServerStates().map(r=>({name:r.serverName,status:r.status}))??[],apiKeySource:this.retry.authMode,version:"anthropic-direct-v1"}};let n=this.promptStream[Symbol.asyncIterator]();try{for(;!this.state.closed;){let r=await Promise.race([n.next(),this.abort.closedPromise]);if(r==="__closed__")break;let o=r;if(o.done)break;let s=o.value,i=this.abort.begin();if(i.signal.aborted){this.abort.clear(i);return}iw(this.state.messages),this.state.messages.push({role:"user",content:s.content});let a=this.composeSystem(),l=Lt(this.retry.authMode,this.initSessionId,vw(),this.effort!==void 0),c={client:this.retry.client,messages:this.state.messages,system:a,tools:this.tools,toolDispatcher:this.state.toolDispatcher,model:this.state.currentModel,maxTokens:this.maxTokens,headers:l,signal:i.signal,ctx:{sessionId:this.initSessionId},...this.thinking!==void 0?{thinking:this.thinking}:{},...this.effort!==void 0?{effort:this.effort}:{},...this.baseUrl!==void 0?{baseUrl:this.baseUrl}:{},...this.traceWriter?{traceWriter:this.traceWriter}:{},onUsageProgress:u=>{this.state.lastUsage=u}};try{for await(let u of this.retry.turnWithRetries(c,()=>this.state.closed)){if(this.state.closed)return;u.type==="turn.completed"&&(this.state.lastUsage=u.usage,this.abort.clear(i)),yield u}}catch(u){if(i.signal.aborted)return;yield{type:"error",error:u instanceof Error?u:new Error(String(u))};return}finally{this.abort.clear(i)}if(this.state.autoCompactThreshold!==void 0&&!this.state.closed){let u=this.state.lastUsage,d=it(this.state.requestedModel);if(u!==null&&d>0){let m=Zr(u);by(m,d,this.state.autoCompactThreshold)&&await this.compact()}}}}catch(r){yield{type:"error",error:r instanceof Error?r:new Error(String(r))}}finally{try{await n.return?.()}catch{}}}composeSystem(){let t=this.systemPrefix,n=this.state.userSystem,r=[];t&&t.length>0&&r.push(...t),n&&n.length>0&&r.push({type:"text",text:n});let o=Ig(this.state.currentPermissionMode);return o!==null&&r.push(o),r.length===0?null:Ji({baseUrl:this.baseUrl})?Ag(r,Vi()):r}async interrupt(){this.abort.requestAbort("interrupted")}async setModel(t){t!==void 0&&t.length>0&&(this.state.requestedModel=t,this.state.currentModel=dn(t)??t)}async setPermissionMode(t){this.state.currentPermissionMode=t}setCwd(t){if(this.state.toolDispatcher.setResolveBase?.(t),!this.cwdDependentsFactory)return;let{userSystem:n,dispatcher:r}=this.cwdDependentsFactory(t);this.state.userSystem=n,this.state.toolDispatcher=r}async supportedCommands(){try{return an().map(n=>{let r={name:n.name,description:n.description};return n.argumentHint&&(r.argumentHint=n.argumentHint),r})}catch{return[]}}async supportedModels(){return wD.map(t=>({...t}))}async supportedAgents(){return[]}async getContextUsage(){let t=this.state.lastUsage,n=it(this.state.requestedModel),r;if(t&&n>0){let i=Zr(t);r=Math.min(100,Math.max(0,i/n*100))}let{totalTokens:o,apiUsage:s}=ya(t);return{tools:[],agents:[],isAutoCompactEnabled:this.state.autoCompactThreshold!==void 0,apiUsage:s,totalTokens:o,...r!==void 0?{percentage:r}:{},maxTokens:n}}async mcpServerStatus(){return this.mcpManager?this.mcpManager.getServerStates().map(t=>({name:t.serverName,status:t.status})):[]}async accountInfo(){return{subscriptionType:this.retry.authMode==="oauth"?"claude-subscription":"api-key"}}async reauth(){return this.retry.forceClientRefresh()}async rewindFiles(t,n){return{canRewind:!1,error:"anthropic-direct provider does not support file checkpoint rewind"}}async compact(){return kw({state:this.state,abort:this.abort,retry:this.retry,initSessionId:this.initSessionId,...this.traceWriter?{traceWriter:this.traceWriter}:{}})}close(){this.state.closed=!0,this.abort.requestAbort("closed"),this.abort.markClosed()}};var Ua=`You have access to tools for working with the filesystem and running commands. Follow these conventions:
1885
+ `).trim()}function ww(e){try{let t=JSON.stringify(e);return t.length>240?t.slice(0,237)+"...":t}catch{return"{}"}}function Sw(e){if(typeof e=="string")return e.length>320?e.slice(0,317)+"...":e;if(Array.isArray(e)){let t=[];for(let r of e)r.type==="text"&&"text"in r&&t.push(r.text);let n=t.join(" ");return n.length>320?n.slice(0,317)+"...":n}return""}function dD(e){let t=0;for(let n of e)if(typeof n.content=="string")t+=n.content.length;else if(Array.isArray(n.content))for(let r of n.content){let o=r.type;o==="text"&&"text"in r?t+=r.text.length:o==="tool_use"?t+=ww(r.input).length:o==="tool_result"&&(t+=Sw(r.content).length)}return t}G();var mD=2,fD="claude-haiku-4-5-20251001",gD=1024;async function kw(e){let{state:t,abort:n,retry:r,initSessionId:o,traceWriter:s}=e,i=t.messages.length;if(t.closed)return{compacted:!1,reason:"session-closed",messagesBefore:i,messagesAfter:i};if(!n.isIdle())return{compacted:!1,reason:"turn-in-flight",messagesBefore:i,messagesAfter:i};let a=hD(),l=gw(t.messages,a);if(l<0)return{compacted:!1,reason:"history-too-short",messagesBefore:i,messagesAfter:i};if(l===0)return{compacted:!1,reason:"nothing-to-summarize",messagesBefore:i,messagesAfter:i};let c=t.messages.slice(0,l),u=yD(),d=hw(c,u,gD),p=n.begin(),f;try{if(p.signal.aborted)return{compacted:!1,reason:"aborted",messagesBefore:i,messagesAfter:i};let y=Lt(r.authMode,o,pD()),w=r.client,x=await Promise.resolve(w.messages.create(d,{headers:y,signal:p.signal}));f=await bD(x)}catch(y){return p.signal.aborted?{compacted:!1,reason:"aborted",messagesBefore:i,messagesAfter:i}:{compacted:!1,reason:"summarization-failed: "+(y instanceof Error?y.message:String(y)),messagesBefore:i,messagesAfter:i}}finally{n.clear(p)}if(f.trim().length===0)return{compacted:!1,reason:"empty-summary",messagesBefore:i,messagesAfter:i};let g=bw(t.messages,l,f),h=yw(t.messages,l,f);t.messages.splice(0,t.messages.length,...h);let b=t.messages.length;return yg(s,{trigger:"manual",preCompactionMessages:c,summary:f,keptTailCount:i-l,keepLastNConfig:a,messagesBefore:i,messagesAfter:b,tokensSavedEstimate:g}),{compacted:!0,messagesBefore:i,messagesAfter:b,tokensSavedEstimate:g}}function hD(){let e=T.AFK_COMPACT_KEEP_LAST_TURNS;if(e!==void 0&&e.length>0){let t=Number.parseInt(e,10);if(Number.isFinite(t)&&t>0)return t}return mD}function yD(){let e=T.AFK_COMPACT_MODEL;return e!==void 0&&e.length>0?e:fD}async function bD(e){let t="";for await(let n of e)if(n.type==="content_block_delta"){let r=n.delta;r.type==="text_delta"&&typeof r.text=="string"&&(t+=r.text)}return t}var wD=[{value:"claude-sonnet-4-5-20250929",displayName:"Claude Sonnet 4.5",description:"Latest balanced Claude \u2014 recommended default"},{value:"claude-opus-4-5-20250929",displayName:"Claude Opus 4.5",description:"Highest-capability Claude"},{value:"claude-haiku-4-5-20250929",displayName:"Claude Haiku 4.5",description:"Fastest, cheapest Claude"}],Ba=class{initSessionId;promptStream;maxTokens;tools;systemPrefix;thinking;effort;baseUrl;traceWriter;state;abort;retry;cwdDependentsFactory;mcpManager;constructor(t){this.initSessionId=t.sessionId??vw(),this.promptStream=t.promptStream,this.maxTokens=t.maxTokens,this.tools=t.tools,this.systemPrefix=t.systemPrefix,this.thinking=t.thinking,t.effort!==void 0&&(this.effort=t.effort),t.baseUrl!==void 0&&(this.baseUrl=t.baseUrl),this.traceWriter=t.traceWriter,this.cwdDependentsFactory=t.cwdDependentsFactory,this.mcpManager=t.mcpManager,this.retry=new Na({client:t.client,authMode:t.authMode,initSessionId:this.initSessionId,...t.tokenRefresher?{tokenRefresher:t.tokenRefresher}:{},autoResumeOnUsageLimit:t.autoResumeOnUsageLimit??!0}),this.state=aw({model:t.model,...t.requestedModel!==void 0?{requestedModel:t.requestedModel}:{},permissionMode:t.permissionMode??"default",userSystem:t.userSystem,toolDispatcher:t.toolDispatcher,...t.initialMessages?{initialMessages:t.initialMessages}:{},...t.autoCompactThreshold!==void 0?{autoCompactThreshold:t.autoCompactThreshold}:{}}),this.abort=new Fa}async*[Symbol.asyncIterator](){yield{type:"session.init",info:{sessionId:this.initSessionId,model:this.state.currentModel,permissionMode:this.state.currentPermissionMode,cwd:process.cwd(),tools:[],slashCommands:[],skills:[],plugins:[],mcpServers:this.mcpManager?.getServerStates().map(r=>({name:r.serverName,status:r.status}))??[],apiKeySource:this.retry.authMode,version:"anthropic-direct-v1"}};let n=this.promptStream[Symbol.asyncIterator]();try{for(;!this.state.closed;){let r=await Promise.race([n.next(),this.abort.closedPromise]);if(r==="__closed__")break;let o=r;if(o.done)break;let s=o.value,i=this.abort.begin();if(i.signal.aborted){this.abort.clear(i);return}iw(this.state.messages),this.state.messages.push({role:"user",content:s.content});let a=this.composeSystem(),l=Lt(this.retry.authMode,this.initSessionId,vw(),this.effort!==void 0),c={client:this.retry.client,messages:this.state.messages,system:a,tools:this.tools,toolDispatcher:this.state.toolDispatcher,model:this.state.currentModel,maxTokens:this.maxTokens,headers:l,signal:i.signal,ctx:{sessionId:this.initSessionId},...this.thinking!==void 0?{thinking:this.thinking}:{},...this.effort!==void 0?{effort:this.effort}:{},...this.baseUrl!==void 0?{baseUrl:this.baseUrl}:{},...this.traceWriter?{traceWriter:this.traceWriter}:{},onUsageProgress:u=>{this.state.lastUsage=u}};try{for await(let u of this.retry.turnWithRetries(c,()=>this.state.closed)){if(this.state.closed)return;u.type==="turn.completed"&&(this.state.lastUsage=u.usage,this.abort.clear(i)),yield u}}catch(u){if(i.signal.aborted)return;yield{type:"error",error:u instanceof Error?u:new Error(String(u))};return}finally{this.abort.clear(i)}if(this.state.autoCompactThreshold!==void 0&&!this.state.closed){let u=this.state.lastUsage,d=it(this.state.requestedModel);if(u!==null&&d>0){let p=Zr(u);by(p,d,this.state.autoCompactThreshold)&&await this.compact()}}}}catch(r){yield{type:"error",error:r instanceof Error?r:new Error(String(r))}}finally{try{await n.return?.()}catch{}}}composeSystem(){let t=this.systemPrefix,n=this.state.userSystem,r=[];t&&t.length>0&&r.push(...t),n&&n.length>0&&r.push({type:"text",text:n});let o=Ig(this.state.currentPermissionMode);return o!==null&&r.push(o),r.length===0?null:Ji({baseUrl:this.baseUrl})?Ag(r,Vi()):r}async interrupt(){this.abort.requestAbort("interrupted")}async setModel(t){t!==void 0&&t.length>0&&(this.state.requestedModel=t,this.state.currentModel=dn(t)??t)}async setPermissionMode(t){this.state.currentPermissionMode=t}setCwd(t){if(this.state.toolDispatcher.setResolveBase?.(t),!this.cwdDependentsFactory)return;let{userSystem:n,dispatcher:r}=this.cwdDependentsFactory(t);this.state.userSystem=n,this.state.toolDispatcher=r}async supportedCommands(){try{return an().map(n=>{let r={name:n.name,description:n.description};return n.argumentHint&&(r.argumentHint=n.argumentHint),r})}catch{return[]}}async supportedModels(){return wD.map(t=>({...t}))}async supportedAgents(){return[]}async getContextUsage(){let t=this.state.lastUsage,n=it(this.state.requestedModel),r;if(t&&n>0){let i=Zr(t);r=Math.min(100,Math.max(0,i/n*100))}let{totalTokens:o,apiUsage:s}=ya(t);return{tools:[],agents:[],isAutoCompactEnabled:this.state.autoCompactThreshold!==void 0,apiUsage:s,totalTokens:o,...r!==void 0?{percentage:r}:{},maxTokens:n}}async mcpServerStatus(){return this.mcpManager?this.mcpManager.getServerStates().map(t=>({name:t.serverName,status:t.status})):[]}async accountInfo(){return{subscriptionType:this.retry.authMode==="oauth"?"claude-subscription":"api-key"}}async reauth(){return this.retry.forceClientRefresh()}async rewindFiles(t,n){return{canRewind:!1,error:"anthropic-direct provider does not support file checkpoint rewind"}}async compact(){return kw({state:this.state,abort:this.abort,retry:this.retry,initSessionId:this.initSessionId,...this.traceWriter?{traceWriter:this.traceWriter}:{}})}close(){this.state.closed=!0,this.abort.requestAbort("closed"),this.abort.markClosed()}};var Ua=`You have access to tools for working with the filesystem and running commands. Follow these conventions:
1886
1886
 
1887
1887
  - Use read_file before editing to verify the exact content you want to change.
1888
1888
  - Prefer edit_file over write_file for modifying existing files \u2014 write_file is for new files or complete rewrites.
@@ -1929,22 +1929,22 @@ Save reusable multi-step workflows the user teaches you or that you discover wor
1929
1929
  `;process.stderr.write(l);return}let i=TD(t),a=xD(i);try{SD(a,{recursive:!0});let c=(!vD(i)?ED:"")+JSON.stringify(s)+`
1930
1930
  `;kD(i,c)}catch(l){let c=`[prompt-dump] Failed to write to ${i}: ${String(l)}
1931
1931
  `;process.stderr.write(c)}}W();G();var ja="anthropic-direct",OD="claude-sonnet-4-5-20250929",$D=e=>/opus-4-(7|[89])/.test(e),Ew=null;var Fe=class{name=ja;externalTools;memoryStore;providerFactory;skillExecutor;schemas;hookRegistry;permissions;subagentExecutor;composeExecutor;surface;mcpManager;_sharedReadRoots;_sharedWriteRoots;_initialResolveBase;_currentCwd;_mcpToolsCache=null;_mcpHandlersCache=null;_presenceSessionId=null;constructor(t={}){let n=[...en];if(t.subagentExecutor&&n.push(nr),t.skillExecutor&&n.push(rr),t.composeExecutor&&n.push(or),n.push(...Cn),n.push(Nt),this.memoryStore=t.memoryStore??new We,this.externalTools=t.tools,this.skillExecutor=t.skillExecutor,this.schemas=n,this.hookRegistry=t.hookRegistry,this.permissions=t.permissions,this.subagentExecutor=t.subagentExecutor,this.composeExecutor=t.composeExecutor,this.surface=t.surface??"cli",this.mcpManager=t.mcpManager,t.mcpManager){let r=t.mcpManager.onToolsRefreshed;t.mcpManager.onToolsRefreshed=o=>{this._mcpToolsCache=null,this._mcpHandlersCache=null,r?.(o)}}t.clientFactory&&(this.providerFactory=t.clientFactory)}buildDispatcher(t,n){let r=ma(t,n?.cwd),o=gs(this.memoryStore,void 0,this.surface);for(let[i,a]of o)r.set(i,a);if(n?.runtimeStateSource&&r.set("get_runtime_state",Jr(n.runtimeStateSource)),this.mcpManager){this._mcpToolsCache||(this._mcpToolsCache=this.mcpManager.getMcpTools()),this._mcpHandlersCache||(this._mcpHandlersCache=this.mcpManager.getMcpHandlers());for(let[i,a]of this._mcpHandlersCache)r.set(i,a)}let s=this._mcpToolsCache??[];return new rn({handlers:r,schemas:[...this.schemas,...s],hookRegistry:this.hookRegistry,permissions:this.permissions,subagentExecutor:this.subagentExecutor,skillExecutor:this.skillExecutor,composeExecutor:this.composeExecutor,cwd:n?.cwd,readRoots:n?.readRoots,writeRoots:n?.writeRoots,...n?.env!==void 0?{env:n.env}:{},sessionId:n?.sessionId,parentSessionId:n?.parentSessionId,...n?.traceWriter?{traceWriter:n.traceWriter}:{}})}close(){this.memoryStore.close()}ensureSharedRoots(t){if(!this._sharedReadRoots){let n=t?[t]:[];this._sharedReadRoots=n.slice(),this._sharedWriteRoots=n.slice(),t&&!this._initialResolveBase&&(this._initialResolveBase=t),t&&!this._currentCwd&&(this._currentCwd=t)}}addReadRoot(t,n="slash",r){this.ensureSharedRoots();let o=Vd.resolve(t);this._sharedReadRoots.includes(o)||this._sharedReadRoots.push(o),this.appendProviderAuditLog({action:"grant-read",path:o,source:n,sessionId:r})}addWriteRoot(t,n="slash",r){this.ensureSharedRoots();let o=Vd.resolve(t);this._sharedReadRoots.includes(o)||this._sharedReadRoots.push(o),this._sharedWriteRoots.includes(o)||this._sharedWriteRoots.push(o),this.appendProviderAuditLog({action:"grant-write",path:o,source:n,sessionId:r})}revokeRoot(t,n="slash",r){if(!this._sharedReadRoots)return;let o=Vd.resolve(t);if(this._initialResolveBase&&o===this._initialResolveBase)return;let s=this._sharedReadRoots.indexOf(o);if(s!==-1&&this._sharedReadRoots.splice(s,1),this._sharedWriteRoots){let i=this._sharedWriteRoots.indexOf(o);i!==-1&&this._sharedWriteRoots.splice(i,1)}this.appendProviderAuditLog({action:"revoke",path:o,source:n,sessionId:r})}getGrants(){return{resolveBase:this._initialResolveBase,readRoots:this._sharedReadRoots?.slice()??[],writeRoots:this._sharedWriteRoots?.slice()??[]}}appendProviderAuditLog(t){try{let n=Fr();PD(MD(n),{recursive:!0});let r=JSON.stringify({timestamp:new Date().toISOString(),sessionId:t.sessionId??null,action:t.action,path:t.path,source:t.source});ID(n,r+`
1932
- `)}catch{}}query(t){let n=t.config,r=typeof n.baseUrl=="string"&&n.baseUrl.length>0,o=r?n.apiKey&&n.apiKey.length>0?n.apiKey:T.AFK_LOCAL_API_KEY||"local":n.apiKey&&n.apiKey.length>0?n.apiKey:T.ANTHROPIC_API_KEY||T.CLAUDE_CODE_OAUTH_TOKEN||"";if(!o||o.length===0)throw new Error(`${ja} provider requires config.apiKey (resolved from ANTHROPIC_API_KEY or CLAUDE_CODE_OAUTH_TOKEN)`);let s=zi(o),i=ms(o,s,n.baseUrl),a=this.providerFactory??Ew,l=a?a(i):new xw(i),c=r?null:Sg(s),u=DD(n.systemPrompt),d=typeof n.model=="string"&&n.model.length>0?dn(n.model)??n.model:OD,m=FD(n,d),f=n.permissionMode??"default";this.ensureSharedRoots(n.cwd),n.readRoots&&this._sharedReadRoots&&this._sharedReadRoots.length<=1&&(this._sharedReadRoots.length=0,this._sharedReadRoots.push(...n.readRoots)),n.writeRoots&&this._sharedWriteRoots&&this._sharedWriteRoots.length<=1&&(this._sharedWriteRoots.length=0,this._sharedWriteRoots.push(...n.writeRoots));let g,h=hs({surface:this.surface,cwd:n.cwd??process.cwd(),modelName:d,providerName:ja,permissionMode:f,...n.sessionId!==void 0?{sessionId:n.sessionId}:{},...n.parentSessionId!==void 0?{parentSessionId:n.parentSessionId}:{},...n.depth!==void 0?{depth:n.depth}:{},...n.maxDepth!==void 0?{maxDepth:n.maxDepth}:{},...n.phaseRole!==void 0?{phaseRole:n.phaseRole}:{},getEnabledToolNames:()=>g instanceof rn?g.toolDefs.map(k=>k.name):[],getMcpTools:()=>this.mcpManager?.getMcpTools()??[],getSubagents:()=>this.subagentExecutor?this.subagentExecutor.getSubagentsLite():{active:[],backgroundJobs:[]}});if((n.depth===void 0||n.depth===0)&&n.parentSessionId===void 0&&n.sessionId!==void 0&&this._presenceSessionId===null){this._presenceSessionId=n.sessionId;let k=n.sessionId,A=h.getWorkspace();bs({sessionId:k,surface:this.surface,cwd:n.cwd??process.cwd(),startedAt:new Date().toISOString(),model:{provider:ja,name:d},workspace:A,pid:process.pid}),process.once("exit",()=>{nn(k)}),process.once("SIGINT",()=>{nn(k),process.exit(130)}),process.once("SIGTERM",()=>{nn(k),process.exit(143)})}g=this.externalTools?ys(this.externalTools,h):this.buildDispatcher(f,{cwd:n.cwd,readRoots:this._sharedReadRoots,writeRoots:this._sharedWriteRoots,...n.env!==void 0?{env:n.env}:{},sessionId:n.sessionId,parentSessionId:n.parentSessionId,traceWriter:n.traceWriter,runtimeStateSource:h});let y=g instanceof rn?[...g.toolDefs]:[...en,Nt],w=n.isSkillDispatch?y.filter(k=>k.name!=="ask_question"&&k.name!=="terminal_font_size"):y,x=this.skillExecutor?sw():"",R=n.cwd||process.cwd(),v=n.isSkillDispatch?Ua:`${Ua}
1932
+ `)}catch{}}query(t){let n=t.config,r=typeof n.baseUrl=="string"&&n.baseUrl.length>0,o=r?n.apiKey&&n.apiKey.length>0?n.apiKey:T.AFK_LOCAL_API_KEY||"local":n.apiKey&&n.apiKey.length>0?n.apiKey:T.ANTHROPIC_API_KEY||T.CLAUDE_CODE_OAUTH_TOKEN||"";if(!o||o.length===0)throw new Error(`${ja} provider requires config.apiKey (resolved from ANTHROPIC_API_KEY or CLAUDE_CODE_OAUTH_TOKEN)`);let s=zi(o),i=ms(o,s,n.baseUrl),a=this.providerFactory??Ew,l=a?a(i):new xw(i),c=r?null:Sg(s),u=DD(n.systemPrompt),d=typeof n.model=="string"&&n.model.length>0?dn(n.model)??n.model:OD,p=FD(n,d),f=n.permissionMode??"default";this.ensureSharedRoots(n.cwd),n.readRoots&&this._sharedReadRoots&&this._sharedReadRoots.length<=1&&(this._sharedReadRoots.length=0,this._sharedReadRoots.push(...n.readRoots)),n.writeRoots&&this._sharedWriteRoots&&this._sharedWriteRoots.length<=1&&(this._sharedWriteRoots.length=0,this._sharedWriteRoots.push(...n.writeRoots));let g,h=hs({surface:this.surface,cwd:n.cwd??process.cwd(),modelName:d,providerName:ja,permissionMode:f,...n.sessionId!==void 0?{sessionId:n.sessionId}:{},...n.parentSessionId!==void 0?{parentSessionId:n.parentSessionId}:{},...n.depth!==void 0?{depth:n.depth}:{},...n.maxDepth!==void 0?{maxDepth:n.maxDepth}:{},...n.phaseRole!==void 0?{phaseRole:n.phaseRole}:{},getEnabledToolNames:()=>g instanceof rn?g.toolDefs.map(k=>k.name):[],getMcpTools:()=>this.mcpManager?.getMcpTools()??[],getSubagents:()=>this.subagentExecutor?this.subagentExecutor.getSubagentsLite():{active:[],backgroundJobs:[]}});if((n.depth===void 0||n.depth===0)&&n.parentSessionId===void 0&&n.sessionId!==void 0&&this._presenceSessionId===null){this._presenceSessionId=n.sessionId;let k=n.sessionId,A=h.getWorkspace();bs({sessionId:k,surface:this.surface,cwd:n.cwd??process.cwd(),startedAt:new Date().toISOString(),model:{provider:ja,name:d},workspace:A,pid:process.pid}),process.once("exit",()=>{nn(k)}),process.once("SIGINT",()=>{nn(k),process.exit(130)}),process.once("SIGTERM",()=>{nn(k),process.exit(143)})}g=this.externalTools?ys(this.externalTools,h):this.buildDispatcher(f,{cwd:n.cwd,readRoots:this._sharedReadRoots,writeRoots:this._sharedWriteRoots,...n.env!==void 0?{env:n.env}:{},sessionId:n.sessionId,parentSessionId:n.parentSessionId,traceWriter:n.traceWriter,runtimeStateSource:h});let y=g instanceof rn?[...g.toolDefs]:[...en,Nt],w=n.isSkillDispatch?y.filter(k=>k.name!=="ask_question"&&k.name!=="terminal_font_size"):y,x=this.skillExecutor?sw():"",R=n.cwd||process.cwd(),v=n.isSkillDispatch?Ua:`${Ua}
1933
1933
 
1934
1934
  ${Gd}
1935
1935
 
1936
1936
  ${qd}`,E=[v,zd];E.push(zr({cwd:R,...n.sessionId!==void 0?{sessionId:n.sessionId}:{},surface:this.surface,...n.depth!==void 0?{depth:n.depth}:{},...n.maxDepth!==void 0?{maxDepth:n.maxDepth}:{},workspace:h.getWorkspace()})),x.length>0&&E.push(x),u&&E.push(u);let I=E.join(`
1937
1937
 
1938
- `),D=[v,zd];x.length>0&&D.push(x),u&&D.push(u),Tw({prompt:t.prompt,options:{model:d,maxTokens:m,system:I},provenance:{systemPrompt:{source:n.systemPromptSource??"none",shape:typeof n.systemPrompt=="string"?"string":Array.isArray(n.systemPrompt)?"string[]":n.systemPrompt!=null?"preset":"undefined",...typeof n.systemPrompt=="string"?{length:n.systemPrompt.length}:{}},...n.apiKey?{apiKey:{source:"config"}}:{}}});let _;if(s==="oauth"&&!r){let k=this.providerFactory??Ew;_=async()=>{let A=await xu();if(!A)return null;let N=ms(A,"oauth",n.baseUrl);return k?k(N):new xw(N)}}let M=n.sessionId??n.resume,P=ND(n.resumeHistory),$=this.externalTools?void 0:k=>{let A=this._currentCwd;if(this._sharedReadRoots&&A!==void 0&&A!==k){let B=this._sharedReadRoots.indexOf(A);B!==-1?this._sharedReadRoots[B]=k:this._sharedReadRoots.includes(k)||this._sharedReadRoots.push(k)}if(this._sharedWriteRoots&&A!==void 0&&A!==k){let B=this._sharedWriteRoots.indexOf(A);B!==-1?this._sharedWriteRoots[B]=k:this._sharedWriteRoots.includes(k)||this._sharedWriteRoots.push(k)}this._currentCwd=k;let H=[D[0],D[1],zr({cwd:k,...n.sessionId!==void 0?{sessionId:n.sessionId}:{},surface:this.surface,...n.depth!==void 0?{depth:n.depth}:{},...n.maxDepth!==void 0?{maxDepth:n.maxDepth}:{},workspace:h.getWorkspace()}),...D.slice(2)].join(`
1938
+ `),D=[v,zd];x.length>0&&D.push(x),u&&D.push(u),Tw({prompt:t.prompt,options:{model:d,maxTokens:p,system:I},provenance:{systemPrompt:{source:n.systemPromptSource??"none",shape:typeof n.systemPrompt=="string"?"string":Array.isArray(n.systemPrompt)?"string[]":n.systemPrompt!=null?"preset":"undefined",...typeof n.systemPrompt=="string"?{length:n.systemPrompt.length}:{}},...n.apiKey?{apiKey:{source:"config"}}:{}}});let _;if(s==="oauth"&&!r){let k=this.providerFactory??Ew;_=async()=>{let A=await xu();if(!A)return null;let N=ms(A,"oauth",n.baseUrl);return k?k(N):new xw(N)}}let M=n.sessionId??n.resume,P=ND(n.resumeHistory),$=this.externalTools?void 0:k=>{let A=this._currentCwd;if(this._sharedReadRoots&&A!==void 0&&A!==k){let B=this._sharedReadRoots.indexOf(A);B!==-1?this._sharedReadRoots[B]=k:this._sharedReadRoots.includes(k)||this._sharedReadRoots.push(k)}if(this._sharedWriteRoots&&A!==void 0&&A!==k){let B=this._sharedWriteRoots.indexOf(A);B!==-1?this._sharedWriteRoots[B]=k:this._sharedWriteRoots.includes(k)||this._sharedWriteRoots.push(k)}this._currentCwd=k;let H=[D[0],D[1],zr({cwd:k,...n.sessionId!==void 0?{sessionId:n.sessionId}:{},surface:this.surface,...n.depth!==void 0?{depth:n.depth}:{},...n.maxDepth!==void 0?{maxDepth:n.maxDepth}:{},workspace:h.getWorkspace()}),...D.slice(2)].join(`
1939
1939
 
1940
- `),C=this.buildDispatcher(f,{cwd:k,readRoots:this._sharedReadRoots,writeRoots:this._sharedWriteRoots,...n.env!==void 0?{env:n.env}:{},sessionId:n.sessionId,parentSessionId:n.parentSessionId,traceWriter:n.traceWriter,runtimeStateSource:h});return{userSystem:H,dispatcher:C}},F=UD(n.effort,d);return new Ba({client:l,authMode:r?"api-key":s,promptStream:t.prompt,toolDispatcher:g,...M!==void 0?{sessionId:M}:{},...P!==void 0?{initialMessages:P}:{},model:d,requestedModel:typeof n.model=="string"&&n.model.length>0?n.model:d,...n.permissionMode!==void 0?{permissionMode:n.permissionMode}:{},maxTokens:m,tools:w,userSystem:I,systemPrefix:c,tokenRefresher:_,...n.thinking!==void 0?{thinking:BD(n.thinking,m,d)}:{},...F!==void 0?{effort:F}:{},...r?{baseUrl:n.baseUrl}:{},...n.traceWriter?{traceWriter:n.traceWriter}:{},...n.autoResumeOnUsageLimit!==void 0?{autoResumeOnUsageLimit:n.autoResumeOnUsageLimit}:{},...$!==void 0?{cwdDependentsFactory:$}:{},...this.mcpManager!==void 0?{mcpManager:this.mcpManager}:{},...Rw(n.autoCompact)!==void 0?{autoCompactThreshold:Rw(n.autoCompact)}:{}})}};function DD(e){if(e===void 0)return null;if(typeof e=="string")return e.length>0?e:null;if(typeof e=="object"&&e!==null&&"append"in e){let t=e.append;return t&&t.length>0?t:null}return null}var LD=.9;function Rw(e){if(e===void 0||e===!1)return;if(e===!0)return LD;let t=e.threshold;if(!(typeof t!="number"||!Number.isFinite(t)||t<=0||t>=1))return t}function FD(e,t){let n=e.maxOutputTokens;return typeof n=="number"&&Number.isFinite(n)&&n>0?Math.floor(n):id(t)}function ND(e){if(!e||e.length===0)return;let t=[];for(let n of e)n.user.length>0&&t.push({role:"user",content:n.user}),n.assistant.length>0&&t.push({role:"assistant",content:n.assistant});return t.length>0?t:void 0}function BD(e,t,n){switch(e.type){case"adaptive":return{type:"adaptive",display:"summarized"};case"disabled":return{type:"disabled"};case"enabled":{if(typeof n=="string"&&$D(n))return{type:"adaptive",display:"summarized"};let r=e.budgetTokens!==void 0&&Number.isFinite(e.budgetTokens)?Math.min(e.budgetTokens,t-1):t-1;return{type:"enabled",budget_tokens:Math.max(r,1024),display:"summarized"}}}}function UD(e,t){if(e!==void 0)return e;let n=t.toLowerCase();if(/(claude-)?(opus|sonnet)-4-[678]/.test(n))return"max"}var jD=new Fe;G();var WD=new Set([...Object.keys(Ia),"auto"]);function HD(e){if(!e)return;let t=e.trim().toLowerCase();if(t){if(t==="anthropic"||t==="anthropic-direct")return"anthropic-direct";if(t==="openai"||t==="openai-compatible"||t==="openai-codex")return"openai-compatible"}}function xe(e,t){let n=t?.explicit??T.AFK_PROVIDER,r=t?.openaiBaseUrl??T.AFK_OPENAI_BASE_URL,o=HD(n);if(o)return o;let s=(e??"").trim().toLowerCase();return s&&(WD.has(s)||s.startsWith("claude-")||s.startsWith("claude_")||s.startsWith("local-")||s.startsWith("local_"))?"anthropic-direct":s&&(s.startsWith("gpt-")||s.startsWith("gpt_")||s.startsWith("o1")||s.startsWith("o3")||s.startsWith("o4")||s.startsWith("codex-")||s.startsWith("codex_")||s==="codex"||s.startsWith("deepseek-")||s.startsWith("deepseek_")||s.startsWith("mistral-")||s.startsWith("mistral_")||s.startsWith("mixtral-")||s.startsWith("mixtral_")||s.startsWith("llama-")||s.startsWith("llama_")||s.startsWith("qwen-")||s.startsWith("qwen_")||s.includes("/"))||r&&r.trim()?"openai-compatible":"anthropic-direct"}function Aw(e,t){switch(xe(e,t)){case"openai-compatible":case"openai-codex":return new Rt;default:return new Fe}}async function Wa(e,t,n){if(!e)return;if(n.kind==="blocked"){await Hr(e,{hookEvent:t,decision:"block",...n.err.reason!==void 0?{reason:n.err.reason}:{}});return}let r=n.decision;await Hr(e,{hookEvent:t,decision:r.decision,...r.reason!==void 0?{reason:r.reason}:{},...r.injectContext!==void 0?{injectedContextBytes:Buffer.byteLength(r.injectContext,"utf8")}:{}})}async function _w(e,t,n={}){if(e)try{let r=await e.dispatch(t,n.signal);await Wa(n.traceWriter,"SessionStart",{kind:"decision",decision:r})}catch(r){throw r instanceof Ae&&await Wa(n.traceWriter,"SessionStart",{kind:"blocked",err:r}),r}}async function Cw(e,t,n={}){if(e)try{let r=await e.dispatch(t,n.signal);await Wa(n.traceWriter,"SessionEnd",{kind:"decision",decision:r})}catch(r){if(r instanceof Ae&&await Wa(n.traceWriter,"SessionEnd",{kind:"blocked",err:r}),r instanceof Ae||r instanceof dt){J(`SessionEnd hook swallowed ${r.name}: ${r.message}`),n.onError?.(r);return}J(`SessionEnd hook unexpected error: ${String(r)}`),n.onError?.(r instanceof Error?r:new Error(String(r)))}}var Ha=class{pendingResolve=null;bufferedMessages=[];getSessionId;constructor(t){this.getSessionId=t}pushUserMessage(t){if(this.pendingResolve){let n=this.pendingResolve;this.pendingResolve=null;let r=this.getSessionId();n({content:t,...r!==void 0?{sessionId:r}:{}});return}this.bufferedMessages.push(t)}createIterable(){let t=this;return{[Symbol.asyncIterator](){return{next(){if(t.bufferedMessages.length>0){let n=t.bufferedMessages.shift(),r=t.getSessionId();return Promise.resolve({value:{content:n,...r!==void 0?{sessionId:r}:{}},done:!1})}return new Promise(n=>{t.pendingResolve=r=>n({value:r,done:!1})})},return(){return Promise.resolve({value:void 0,done:!0})}}}}}};function Iw(e,t,n){e&&(e.aborted?t.abort(e.reason):e.addEventListener("abort",()=>{t.signal.aborted||t.abort(e.reason)},{once:!0})),t.signal.addEventListener("abort",n,{once:!0})}function Pw(e,t){let n=e.permissionMode??"default",r=e.persistSession??!0,o={sessionId:e.sessionId,configuredSessionId:e.sessionId,resume:e.resume,resumeSessionAt:e.resumeSessionAt,continue:e.continue,forkSession:e.forkSession,persistSession:r},s={sessionId:e.sessionId,model:t,permissionMode:n};return{sessionIdentity:o,metadata:s}}var Ka=class{initializationPromise;resolveInitialization;rejectInitialization;initializationSettled=!1;sessionMetadata;sessionIdentity;constructor(t,n){this.sessionIdentity=t,this.sessionMetadata=n,this.initializationPromise=new Promise((r,o)=>{this.resolveInitialization=r,this.rejectInitialization=o})}waitForInitialization(){return this.initializationPromise}getSessionIdentity(){return{...this.sessionIdentity,sessionId:this.getSessionId()}}getSessionMetadata(){return{...this.sessionMetadata,sessionId:this.getSessionId()}}getSessionId(){return this.sessionMetadata.sessionId??this.sessionIdentity.sessionId}updateSessionIdentity(t){t&&(this.sessionIdentity={...this.sessionIdentity,sessionId:t},this.sessionMetadata={...this.sessionMetadata,sessionId:t})}setSessionMetadata(t){this.sessionMetadata=t(this.sessionMetadata)}resolveInitializationIfNeeded(){this.initializationSettled||(this.initializationSettled=!0,this.resolveInitialization(this.getSessionMetadata()))}resolveInitializationOnce(){this.initializationSettled||(this.initializationSettled=!0,this.resolveInitialization(this.getSessionMetadata()))}rejectInitializationOnce(t){this.initializationSettled||(this.initializationSettled=!0,this.rejectInitialization(t))}isInitializationSettled(){return this.initializationSettled}};function Mw(e){try{let t=JSON.parse(e);if(!Array.isArray(t))return null;let n=t.length;if(n===0)return"no results";let r=0,o=0;for(let a of t)if(a&&typeof a=="object"){let l=a.type;l==="fact"?r++:l==="procedure"&&o++}let s=`${n} result${n===1?"":"s"}`;if(r+o!==n)return s;let i=[];return r>0&&i.push(`${r} fact${r===1?"":"s"}`),o>0&&i.push(`${o} procedure${o===1?"":"s"}`),i.length===0?s:`${s} (${i.join(", ")})`}catch{return null}}function Ow(e){try{let t=JSON.parse(e);if(!t||typeof t!="object")return null;let n=t;if(n.target==="hot"&&n.saved===!0)return"hot memory saved";if(n.target==="fact"){if(n.action==="remove")return n.removed===!0?"fact removed":"fact not found";if(n.action==="set"&&typeof n.id=="number")return`fact #${n.id} set`;if(n.action==="supersede"&&typeof n.id=="number"&&typeof n.supersedes=="number")return`fact #${n.id} supersedes #${n.supersedes}`}return null}catch{return null}}function $w(e){try{let t=JSON.parse(e);if(!t||typeof t!="object")return null;let n=t;return typeof n.name=="string"&&n.written===!0?`wrote procedure '${n.name}'`:null}catch{return null}}function Yd(e){let t=e.trim();if(t.length===0)return null;let n=t[0];if(n!=="{"&&n!=="[")return null;let r=t[t.length-1];if(n==="{"&&r!=="}"||n==="["&&r!=="]")return null;let o;try{o=JSON.parse(t)}catch{return null}return Array.isArray(o)?Dw(KD(o)):o!==null&&typeof o=="object"?Dw(GD(o)):null}function KD(e){return e.length===0?"[empty array]":e.length===1?"[1 item]":`[${e.length} items]`}function GD(e){let t=Object.keys(e);if(t.length===0)return"{empty object}";let n=t.slice(0,4),r=t.length>4?", \u2026":"";return`{${n.join(", ")}${r}}`}function Dw(e){return e.length<=80?e:e.slice(0,79)+"\u2026"}var qD=new Map([["memory_search",Mw],["memory_update",Ow],["procedure_write",$w],["bash",Yd],["Bash",Yd]]);function Lw(e,t){if(!e)return null;let n=qD.get(e);if(!n)return null;try{let r=n(t);if(r===null)return null;let o=Hg(r);return o.length>0?o:null}catch{return null}}function zD(e){let t=/Output too large \((\d+(?:\.\d+)?)\s*(B|KB|MB|GB)\)\.\s*Full output saved to:\s*(\/[^\n]+)/,n=e.match(t);if(!n||!n[1]||!n[2]||!n[3])return null;let r=n[1],o=n[2],s=n[3],i=parseFloat(r),a=i;o==="KB"?a=i*1024:o==="MB"?a=i*1024*1024:o==="GB"&&(a=i*1024*1024*1024);let l=r;return i%1===0&&(l=String(Math.floor(i))),l+=o,{sizeLabel:l,sizeBytes:Math.round(a),absolutePath:s.trim()}}function JD(e){if(e<1024)return`${e}B`;let t=e/1024;if(t<1024)return t%1===0?`${Math.floor(t)}KB`:`${t.toFixed(1)}KB`;let n=t/1024;if(n<1024)return n%1===0?`${Math.floor(n)}MB`:`${n.toFixed(1)}MB`;let r=n/1024;return r%1===0?`${Math.floor(r)}GB`:`${r.toFixed(1)}GB`}function VD(e){let t=Buffer.byteLength(e,"utf8"),n=JD(t),r=e.split(`
1940
+ `),C=this.buildDispatcher(f,{cwd:k,readRoots:this._sharedReadRoots,writeRoots:this._sharedWriteRoots,...n.env!==void 0?{env:n.env}:{},sessionId:n.sessionId,parentSessionId:n.parentSessionId,traceWriter:n.traceWriter,runtimeStateSource:h});return{userSystem:H,dispatcher:C}},F=UD(n.effort,d);return new Ba({client:l,authMode:r?"api-key":s,promptStream:t.prompt,toolDispatcher:g,...M!==void 0?{sessionId:M}:{},...P!==void 0?{initialMessages:P}:{},model:d,requestedModel:typeof n.model=="string"&&n.model.length>0?n.model:d,...n.permissionMode!==void 0?{permissionMode:n.permissionMode}:{},maxTokens:p,tools:w,userSystem:I,systemPrefix:c,tokenRefresher:_,...n.thinking!==void 0?{thinking:BD(n.thinking,p,d)}:{},...F!==void 0?{effort:F}:{},...r?{baseUrl:n.baseUrl}:{},...n.traceWriter?{traceWriter:n.traceWriter}:{},...n.autoResumeOnUsageLimit!==void 0?{autoResumeOnUsageLimit:n.autoResumeOnUsageLimit}:{},...$!==void 0?{cwdDependentsFactory:$}:{},...this.mcpManager!==void 0?{mcpManager:this.mcpManager}:{},...Rw(n.autoCompact)!==void 0?{autoCompactThreshold:Rw(n.autoCompact)}:{}})}};function DD(e){if(e===void 0)return null;if(typeof e=="string")return e.length>0?e:null;if(typeof e=="object"&&e!==null&&"append"in e){let t=e.append;return t&&t.length>0?t:null}return null}var LD=.9;function Rw(e){if(e===void 0||e===!1)return;if(e===!0)return LD;let t=e.threshold;if(!(typeof t!="number"||!Number.isFinite(t)||t<=0||t>=1))return t}function FD(e,t){let n=e.maxOutputTokens;return typeof n=="number"&&Number.isFinite(n)&&n>0?Math.floor(n):id(t)}function ND(e){if(!e||e.length===0)return;let t=[];for(let n of e)n.user.length>0&&t.push({role:"user",content:n.user}),n.assistant.length>0&&t.push({role:"assistant",content:n.assistant});return t.length>0?t:void 0}function BD(e,t,n){switch(e.type){case"adaptive":return{type:"adaptive",display:"summarized"};case"disabled":return{type:"disabled"};case"enabled":{if(typeof n=="string"&&$D(n))return{type:"adaptive",display:"summarized"};let r=e.budgetTokens!==void 0&&Number.isFinite(e.budgetTokens)?Math.min(e.budgetTokens,t-1):t-1;return{type:"enabled",budget_tokens:Math.max(r,1024),display:"summarized"}}}}function UD(e,t){if(e!==void 0)return e;let n=t.toLowerCase();if(/(claude-)?(opus|sonnet)-4-[678]/.test(n))return"max"}var jD=new Fe;G();var WD=new Set([...Object.keys(Ia),"auto"]);function HD(e){if(!e)return;let t=e.trim().toLowerCase();if(t){if(t==="anthropic"||t==="anthropic-direct")return"anthropic-direct";if(t==="openai"||t==="openai-compatible"||t==="openai-codex")return"openai-compatible"}}function xe(e,t){let n=t?.explicit??T.AFK_PROVIDER,r=t?.openaiBaseUrl??T.AFK_OPENAI_BASE_URL,o=HD(n);if(o)return o;let s=(e??"").trim().toLowerCase();return s&&(WD.has(s)||s.startsWith("claude-")||s.startsWith("claude_")||s.startsWith("local-")||s.startsWith("local_"))?"anthropic-direct":s&&(s.startsWith("gpt-")||s.startsWith("gpt_")||s.startsWith("o1")||s.startsWith("o3")||s.startsWith("o4")||s.startsWith("codex-")||s.startsWith("codex_")||s==="codex"||s.startsWith("deepseek-")||s.startsWith("deepseek_")||s.startsWith("mistral-")||s.startsWith("mistral_")||s.startsWith("mixtral-")||s.startsWith("mixtral_")||s.startsWith("llama-")||s.startsWith("llama_")||s.startsWith("qwen-")||s.startsWith("qwen_")||s.includes("/"))||r&&r.trim()?"openai-compatible":"anthropic-direct"}function Aw(e,t){switch(xe(e,t)){case"openai-compatible":case"openai-codex":return new Rt;default:return new Fe}}async function Wa(e,t,n){if(!e)return;if(n.kind==="blocked"){await Hr(e,{hookEvent:t,decision:"block",...n.err.reason!==void 0?{reason:n.err.reason}:{}});return}let r=n.decision;await Hr(e,{hookEvent:t,decision:r.decision,...r.reason!==void 0?{reason:r.reason}:{},...r.injectContext!==void 0?{injectedContextBytes:Buffer.byteLength(r.injectContext,"utf8")}:{}})}async function _w(e,t,n={}){if(e)try{let r=await e.dispatch(t,n.signal);await Wa(n.traceWriter,"SessionStart",{kind:"decision",decision:r})}catch(r){throw r instanceof Ae&&await Wa(n.traceWriter,"SessionStart",{kind:"blocked",err:r}),r}}async function Cw(e,t,n={}){if(e)try{let r=await e.dispatch(t,n.signal);await Wa(n.traceWriter,"SessionEnd",{kind:"decision",decision:r})}catch(r){if(r instanceof Ae&&await Wa(n.traceWriter,"SessionEnd",{kind:"blocked",err:r}),r instanceof Ae||r instanceof dt){J(`SessionEnd hook swallowed ${r.name}: ${r.message}`),n.onError?.(r);return}J(`SessionEnd hook unexpected error: ${String(r)}`),n.onError?.(r instanceof Error?r:new Error(String(r)))}}var Ha=class{pendingResolve=null;bufferedMessages=[];getSessionId;constructor(t){this.getSessionId=t}pushUserMessage(t){if(this.pendingResolve){let n=this.pendingResolve;this.pendingResolve=null;let r=this.getSessionId();n({content:t,...r!==void 0?{sessionId:r}:{}});return}this.bufferedMessages.push(t)}createIterable(){let t=this;return{[Symbol.asyncIterator](){return{next(){if(t.bufferedMessages.length>0){let n=t.bufferedMessages.shift(),r=t.getSessionId();return Promise.resolve({value:{content:n,...r!==void 0?{sessionId:r}:{}},done:!1})}return new Promise(n=>{t.pendingResolve=r=>n({value:r,done:!1})})},return(){return Promise.resolve({value:void 0,done:!0})}}}}}};function Iw(e,t,n){e&&(e.aborted?t.abort(e.reason):e.addEventListener("abort",()=>{t.signal.aborted||t.abort(e.reason)},{once:!0})),t.signal.addEventListener("abort",n,{once:!0})}function Pw(e,t){let n=e.permissionMode??"default",r=e.persistSession??!0,o={sessionId:e.sessionId,configuredSessionId:e.sessionId,resume:e.resume,resumeSessionAt:e.resumeSessionAt,continue:e.continue,forkSession:e.forkSession,persistSession:r},s={sessionId:e.sessionId,model:t,permissionMode:n};return{sessionIdentity:o,metadata:s}}var Ka=class{initializationPromise;resolveInitialization;rejectInitialization;initializationSettled=!1;sessionMetadata;sessionIdentity;constructor(t,n){this.sessionIdentity=t,this.sessionMetadata=n,this.initializationPromise=new Promise((r,o)=>{this.resolveInitialization=r,this.rejectInitialization=o})}waitForInitialization(){return this.initializationPromise}getSessionIdentity(){return{...this.sessionIdentity,sessionId:this.getSessionId()}}getSessionMetadata(){return{...this.sessionMetadata,sessionId:this.getSessionId()}}getSessionId(){return this.sessionMetadata.sessionId??this.sessionIdentity.sessionId}updateSessionIdentity(t){t&&(this.sessionIdentity={...this.sessionIdentity,sessionId:t},this.sessionMetadata={...this.sessionMetadata,sessionId:t})}setSessionMetadata(t){this.sessionMetadata=t(this.sessionMetadata)}resolveInitializationIfNeeded(){this.initializationSettled||(this.initializationSettled=!0,this.resolveInitialization(this.getSessionMetadata()))}resolveInitializationOnce(){this.initializationSettled||(this.initializationSettled=!0,this.resolveInitialization(this.getSessionMetadata()))}rejectInitializationOnce(t){this.initializationSettled||(this.initializationSettled=!0,this.rejectInitialization(t))}isInitializationSettled(){return this.initializationSettled}};function Mw(e){try{let t=JSON.parse(e);if(!Array.isArray(t))return null;let n=t.length;if(n===0)return"no results";let r=0,o=0;for(let a of t)if(a&&typeof a=="object"){let l=a.type;l==="fact"?r++:l==="procedure"&&o++}let s=`${n} result${n===1?"":"s"}`;if(r+o!==n)return s;let i=[];return r>0&&i.push(`${r} fact${r===1?"":"s"}`),o>0&&i.push(`${o} procedure${o===1?"":"s"}`),i.length===0?s:`${s} (${i.join(", ")})`}catch{return null}}function Ow(e){try{let t=JSON.parse(e);if(!t||typeof t!="object")return null;let n=t;if(n.target==="hot"&&n.saved===!0)return"hot memory saved";if(n.target==="fact"){if(n.action==="remove")return n.removed===!0?"fact removed":"fact not found";if(n.action==="set"&&typeof n.id=="number")return`fact #${n.id} set`;if(n.action==="supersede"&&typeof n.id=="number"&&typeof n.supersedes=="number")return`fact #${n.id} supersedes #${n.supersedes}`}return null}catch{return null}}function $w(e){try{let t=JSON.parse(e);if(!t||typeof t!="object")return null;let n=t;return typeof n.name=="string"&&n.written===!0?`wrote procedure '${n.name}'`:null}catch{return null}}function Yd(e){let t=e.trim();if(t.length===0)return null;let n=t[0];if(n!=="{"&&n!=="[")return null;let r=t[t.length-1];if(n==="{"&&r!=="}"||n==="["&&r!=="]")return null;let o;try{o=JSON.parse(t)}catch{return null}return Array.isArray(o)?Dw(KD(o)):o!==null&&typeof o=="object"?Dw(GD(o)):null}function KD(e){return e.length===0?"[empty array]":e.length===1?"[1 item]":`[${e.length} items]`}function GD(e){let t=Object.keys(e);if(t.length===0)return"{empty object}";let n=t.slice(0,4),r=t.length>4?", \u2026":"";return`{${n.join(", ")}${r}}`}function Dw(e){return e.length<=80?e:e.slice(0,79)+"\u2026"}var qD=new Map([["memory_search",Mw],["memory_update",Ow],["procedure_write",$w],["bash",Yd],["Bash",Yd]]);function Lw(e,t){if(!e)return null;let n=qD.get(e);if(!n)return null;try{let r=n(t);if(r===null)return null;let o=Hg(r);return o.length>0?o:null}catch{return null}}function zD(e){let t=/Output too large \((\d+(?:\.\d+)?)\s*(B|KB|MB|GB)\)\.\s*Full output saved to:\s*(\/[^\n]+)/,n=e.match(t);if(!n||!n[1]||!n[2]||!n[3])return null;let r=n[1],o=n[2],s=n[3],i=parseFloat(r),a=i;o==="KB"?a=i*1024:o==="MB"?a=i*1024*1024:o==="GB"&&(a=i*1024*1024*1024);let l=r;return i%1===0&&(l=String(Math.floor(i))),l+=o,{sizeLabel:l,sizeBytes:Math.round(a),absolutePath:s.trim()}}function JD(e){if(e<1024)return`${e}B`;let t=e/1024;if(t<1024)return t%1===0?`${Math.floor(t)}KB`:`${t.toFixed(1)}KB`;let n=t/1024;if(n<1024)return n%1===0?`${Math.floor(n)}MB`:`${n.toFixed(1)}MB`;let r=n/1024;return r%1===0?`${Math.floor(r)}GB`:`${r.toFixed(1)}GB`}function VD(e){let t=Buffer.byteLength(e,"utf8"),n=JD(t),r=e.split(`
1941
1941
  `);if(r.length<=1&&e.length<=80)return{content:e,truncated:!1,sizeBytes:t,sizeLabel:n};if(r.length<=1)return e.length<=80?{content:e,truncated:!1,sizeBytes:t,sizeLabel:n}:{content:e.substring(0,80)+"\u2026",truncated:!0,sizeBytes:t,sizeLabel:n};if(e.length<=80)return{content:e,truncated:!1,sizeBytes:t,sizeLabel:n};let o=r[0]??"",s=o;return o.length>80&&(s=o.substring(0,80)+"\u2026"),{content:s+`\u2026+${r.length} lines`,truncated:!0,lineCount:r.length,sizeBytes:t,sizeLabel:n}}function YD(e,t){let n={...e.raw??{}};return e.inputTokens!==void 0&&(n.input_tokens=e.inputTokens),e.outputTokens!==void 0&&(n.output_tokens=e.outputTokens),e.cachedInputTokens!==void 0&&(n.cache_read_input_tokens=e.cachedInputTokens),e.cacheCreationTokens!==void 0&&(n.cache_creation_input_tokens=e.cacheCreationTokens),e.totalTokens!==void 0&&(n.total_tokens=e.totalTokens),{sessionId:t,stopReason:e.stopReason??void 0,resultSubtype:e.resultSubtype,durationMs:e.durationMs,durationApiMs:e.durationApiMs,totalCostUsd:e.totalCostUsd,isError:e.isError,usage:Object.keys(n).length>0?n:void 0,modelUsage:e.modelUsage,permissionDenials:e.permissionDenials,errors:e.errors}}function XD(e){let t=e.isError===!0?null:Lw(e.toolName,e.content),n=t!==null?{display:t}:{},r=zD(e.content);if(r)return{type:"chunk",chunk:{type:"tool_result",toolUseId:e.toolUseId,content:`Output persisted (${r.sizeLabel}) \u2192 ${r.absolutePath}`,isError:e.isError===!0,persistedPath:r.absolutePath,sizeBytes:r.sizeBytes,sizeLabel:r.sizeLabel,...n}};let{content:o,lineCount:s,sizeBytes:i,sizeLabel:a}=VD(e.content);return{type:"chunk",chunk:{type:"tool_result",toolUseId:e.toolUseId,content:o,isError:e.isError===!0,sizeBytes:i,sizeLabel:a,...e.truncated===!0&&{truncated:!0},...s!==void 0&&{lineCount:s},...n}}}function Xd(e,t){switch(e.type){case"session.init":{let n=e.info;return t.setSessionMetadata(r=>({...r,sessionId:n.sessionId,model:n.model??r.model,...n.permissionMode!==void 0?{permissionMode:n.permissionMode}:{},...n.cwd!==void 0?{cwd:n.cwd}:{},tools:n.tools?[...n.tools]:r.tools,slashCommands:n.slashCommands?[...n.slashCommands]:r.slashCommands,skills:n.skills?[...n.skills]:r.skills,plugins:n.plugins?n.plugins.map(o=>({...o})):r.plugins,mcpServers:n.mcpServers?n.mcpServers.map(o=>({...o})):r.mcpServers,...n.apiKeySource!==void 0?{apiKeySource:n.apiKeySource}:{},...n.version!==void 0?{claudeCodeVersion:n.version}:{},...n.outputStyle!==void 0?{outputStyle:n.outputStyle}:{}})),t.updateSessionIdentity(n.sessionId),t.resolveInitialization(),null}case"session.status":return t.setSessionMetadata(n=>({...n,sessionId:e.sessionId,...e.permissionMode!==void 0?{permissionMode:e.permissionMode}:{permissionMode:n.permissionMode},...e.status!==void 0?{status:e.status}:{}})),null;case"delta.text":return{type:"chunk",chunk:{type:"content",content:e.text,metadata:{eventType:"delta",deltaType:"text_delta"}}};case"delta.reasoning":return{type:"chunk",chunk:{type:"thinking",content:e.text,metadata:{eventType:"delta",deltaType:"thinking_delta"}}};case"assistant.message":if(e.sessionId&&t.updateSessionIdentity(e.sessionId),e.text){let n={role:"assistant",content:e.text,timestamp:new Date};return t.conversationHistory.push(n),{type:"message",message:n}}return null;case"tool.use.start":return{type:"chunk",chunk:{type:"tool_use_detail",toolUseId:e.toolUseId,toolName:e.toolName,toolInput:e.toolInput}};case"tool.use":return{type:"chunk",chunk:{type:"tool_use",content:e.summary,metadata:{eventType:"tool_use_summary",precedingToolUseIds:e.toolUseIds}}};case"tool.output":return XD(e);case"tool.diff":return{type:"chunk",chunk:{type:"tool_diff",toolUseId:e.toolUseId,diff:e.diff}};case"progress":return{type:"progress",progress:{taskId:e.progress.taskId,description:e.progress.description,...e.progress.summary!==void 0?{summary:e.progress.summary}:{},...e.progress.lastToolName!==void 0?{lastToolName:e.progress.lastToolName}:{},totalTokens:e.progress.totalTokens,toolUses:e.progress.toolUses,durationMs:e.progress.durationMs}};case"suggestion":return{type:"suggestion",suggestion:e.suggestion};case"turn.completed":{let n=YD(e.usage,e.sessionId??t.getSessionMetadata().sessionId);t.setLastResponseMetadata(n);for(let r=t.conversationHistory.length-1;r>=0;r--){let o=t.conversationHistory[r];if(o?.role==="assistant"){o.metadata=n;break}}if(t.maxBudgetUsd!==void 0&&t.abortBudget!==void 0&&typeof n.totalCostUsd=="number"&&(t._runningCostUsd=(t._runningCostUsd??0)+n.totalCostUsd,t._runningCostUsd>=t.maxBudgetUsd)){gg(t.traceWriter,{kind:"monetary",runningCostUsd:t._runningCostUsd,maxBudgetUsd:t.maxBudgetUsd,lastTurnCostUsd:n.totalCostUsd});let r=new Tn(t._runningCostUsd,t.maxBudgetUsd);return t.abortBudget(r.message),{type:"error",error:r}}return{type:"done",metadata:n}}case"error":return{type:"error",error:e.error};case"paused":return{type:"paused",reason:e.reason,...e.resetsAt!==void 0?{resetsAt:e.resetsAt}:{},...e.accountId!==void 0?{accountId:e.accountId}:{},...e.autoResume!==void 0?{autoResume:e.autoResume}:{}};case"resumed":return{type:"resumed",hotSwapped:e.hotSwapped,...e.accountId!==void 0?{accountId:e.accountId}:{}};default:return null}}var Ve=class{config;currentState="idle";providerQuery;providerIterator;conversationHistory=[];turnCount=0;lastResponseMetadata=null;initPromise=null;inputStream;abortController;_hookRegistry;sessionEndDispatched=!1;stateManager;sessionRunningCostUsd=0;sessionRunningTokens={input:0,output:0,cacheRead:0,cacheCreation:0};lastStopReason;sessionStartedAt=Date.now();subagentCompletedCount=0;subagentRunningTokens={input:0,output:0,cacheRead:0,cacheCreation:0};subagentRunningCostUsd=0;constructor(t){this.config=t,this.abortController=new AbortController,this._hookRegistry=t.hookRegistry,Iw(t.abortSignal,this.abortController,()=>{this.onAbort()}),Gr(t.traceWriter,{phase:"session_init_start"}),this.initSdkLifecycle()}initSdkLifecycle(){let t=dn(this.config.model)??this.config.model,{sessionIdentity:n,metadata:r}=Pw(this.config,t);this.stateManager=new Ka(n,r),this.inputStream=new Ha(()=>this.sessionId);let o=this.config.provider??Aw(t);J(`\u{1F7E2} AgentSession: Creating query session via provider=${o.name}`),this.providerQuery=o.query({prompt:this.inputStream.createIterable(),config:this.config}),this.conversationHistory=[],this.turnCount=0,this.lastResponseMetadata=null,this.sessionRunningCostUsd=0,this.sessionRunningTokens={input:0,output:0,cacheRead:0,cacheCreation:0},this.lastStopReason=void 0,this.sessionEndDispatched=!1,this.currentState="idle",this.subagentCompletedCount=0,this.subagentRunningTokens={input:0,output:0,cacheRead:0,cacheCreation:0},this.subagentRunningCostUsd=0;let s=this.providerQuery;this.providerIterator=s[Symbol.asyncIterator](),this.initPromise=this.pullInitialization()}async pullInitialization(){try{for(await _w(this._hookRegistry,{event:"SessionStart",sessionId:this.sessionId},{signal:this.abortController.signal,...this.config.traceWriter?{traceWriter:this.config.traceWriter}:{}});;){let t=await this.providerIterator.next();if(t.done){this.stateManager.resolveInitializationIfNeeded();return}let n=t.value,r=Xd(n,this.buildTransformDeps());if(n.type==="session.init"){Gr(this.config.traceWriter,{phase:"session_init_done",durationMs:Date.now()-this.sessionStartedAt});return}if(r&&r.type==="error")return}}catch(t){let n=t instanceof Error?t:new Error(String(t));this.stateManager.isInitializationSettled()||this.stateManager.rejectInitializationOnce(n),await this.dispatchSessionEndOnce("error").catch(()=>{})}}buildTransformDeps(){return{conversationHistory:this.conversationHistory,getSessionMetadata:()=>this.stateManager.getSessionMetadata(),setSessionMetadata:t=>this.stateManager.setSessionMetadata(t),updateSessionIdentity:t=>this.stateManager.updateSessionIdentity(t),resolveInitialization:()=>this.stateManager.resolveInitializationOnce(),setLastResponseMetadata:t=>{this.lastResponseMetadata=t,typeof t.totalCostUsd=="number"&&Number.isFinite(t.totalCostUsd)&&(this.sessionRunningCostUsd+=t.totalCostUsd);let n=t.usage;if(n&&typeof n=="object"){let r=n,o=(s,i)=>{let a=r[s];typeof a=="number"&&Number.isFinite(a)&&(this.sessionRunningTokens[i]+=a)};o("input_tokens","input"),o("output_tokens","output"),o("cache_read_input_tokens","cacheRead"),o("cache_creation_input_tokens","cacheCreation")}typeof t.stopReason=="string"&&(this.lastStopReason=t.stopReason)},maxBudgetUsd:this.config.maxBudgetUsd,abortBudget:t=>{this.abortController.signal.aborted||this.abortController.abort(t)},...this.config.traceWriter?{traceWriter:this.config.traceWriter}:{}}}get state(){return this.currentState}get sessionId(){return this.stateManager.getSessionId()}get cwd(){return this.config.cwd}get abortSignal(){return this.abortController.signal}get hookRegistry(){return this._hookRegistry}abort(t){if(t==="closed"||t.startsWith("Budget ")||t.includes("timed out"))throw new Error(`AgentSession.abort: reserved reason "${t}" (use a caller-specific string like 'sigint')`);this.abortController.signal.aborted||this.abortController.abort(t)}async sendMessage(t,n={}){this.assertCanSend();let r=this.config.timeoutMs??Zi,o=async()=>{let s=null,i="";this.currentState=n.stream?"streaming":"processing";for await(let a of this.sendMessageStreamInternal(t)){if(a.type==="chunk"&&a.chunk.type==="content"&&(i+=a.chunk.content),a.type==="message"&&a.message.role==="assistant"&&(s=a.message),a.type==="error")throw a.error;if(a.type==="done"){if(s)return{...s,metadata:a.metadata};if(i)return{role:"assistant",content:i,metadata:a.metadata,timestamp:new Date}}}if(s)return s;if(i)return{role:"assistant",content:i,timestamp:new Date};throw new Error("No assistant response received")};try{return await Qi(o(),r,{controller:this.abortController,label:this.sessionId??"session"})}finally{this.currentState==="processing"&&(this.currentState="idle")}}async*sendMessageStream(t){this.assertCanSend(),this.currentState="streaming";try{yield*this.sendMessageStreamInternal(t)}finally{this.currentState==="streaming"&&(this.currentState="idle")}}async*sendMessageStreamInternal(t){this.initPromise&&await this.initPromise;let r={role:"user",content:typeof t=="string"?t:this.summarizeContentBlocks(t),timestamp:new Date};this.conversationHistory.push(r),this.inputStream.pushUserMessage(t);let o=this.buildTransformDeps();try{for(;;){let s=await this.providerIterator.next();if(s.done)break;let i=s.value,a=Xd(i,o);if(a&&(a.type==="done"&&this.turnCount++,yield a,a.type==="done"||a.type==="error"))break}}finally{this.currentState==="streaming"&&(this.currentState="idle")}}summarizeContentBlocks(t){let n=[],r=0;for(let s of t)s.type==="text"?n.push(s.text):s.type==="image"&&r++;let o=n.join(" ");return r>0&&(o=o?`${o} [+ ${r} image(s)]`:`[+ ${r} image(s)]`),o||"[content block(s)]"}async interrupt(){this.currentState!=="streaming"&&this.currentState!=="processing"||(this.currentState="idle",await this.providerQuery.interrupt())}async reset(){if(this.currentState==="closed")throw new Error("Cannot reset: session is closed");if(this.abortController.signal.aborted)throw new dt("Cannot reset: session aborted");if(this.currentState==="processing"||this.currentState==="streaming")try{await this.providerQuery.interrupt()}catch{}await this.dispatchSessionEndOnce("reset");try{await this.providerQuery.close()}catch{}await this.providerIterator.return?.(),this.initPromise&&await Promise.race([this.initPromise,new Promise(t=>setTimeout(t,Ru))]).catch(()=>{}),this.stateManager.resolveInitializationIfNeeded(),this.config={...this.config},delete this.config.resume,delete this.config.sessionId,delete this.config.resumeHistory,delete this.config.resumeSessionAt,delete this.config.continue,delete this.config.forkSession;try{this.initSdkLifecycle()}catch(t){throw this.currentState="closed",new Error(`Session reset failed during lifecycle rebuild: ${t instanceof Error?t.message:String(t)}`,{cause:t})}}async onAbort(){try{await this.providerQuery.interrupt()}catch{}}async setModel(t){let n=dn(t);typeof t=="string"&&t.length>0&&await this.providerQuery.setModel(t),n&&this.stateManager.setSessionMetadata(r=>({...r,model:n}))}async setPermissionMode(t){await this.providerQuery.setPermissionMode(t),this.stateManager.setSessionMetadata(n=>({...n,permissionMode:t}))}setCwd(t){this.config={...this.config,cwd:t},this.providerQuery.setCwd?.(t)}async reauth(){return await this.providerQuery.reauth?.()??null}waitForInitialization(){return this.stateManager.waitForInitialization()}getSessionIdentity(){return this.stateManager.getSessionIdentity()}getSessionMetadata(){return this.stateManager.getSessionMetadata()}getQuery(){return this.providerQuery}supportedCommands(){return this.providerQuery.supportedCommands()}supportedModels(){return this.providerQuery.supportedModels()}supportedAgents(){return this.providerQuery.supportedAgents()}getContextUsage(){return this.providerQuery.getContextUsage()}mcpServerStatus(){return this.providerQuery.mcpServerStatus()}accountInfo(){return this.providerQuery.accountInfo()}rewindFiles(t,n){return this.providerQuery.rewindFiles(t,n)}async compact(){if(this.currentState==="closed")throw new Error("Cannot compact: session is closed");if(this.currentState!=="idle")return{compacted:!1,reason:"session-busy",messagesBefore:0,messagesAfter:0};let t=this.providerQuery.compact?.bind(this.providerQuery);if(!t)return{compacted:!1,reason:"not-supported",messagesBefore:0,messagesAfter:0};this.currentState="compacting";try{return await t()}finally{this.currentState="idle"}}getLastResponseMetadata(){return this.lastResponseMetadata}getOutputStream(){throw new Error("getOutputStream() is not supported \u2014 use sendMessageStream() instead")}getInputStreamRef(){return{pushUserMessage:t=>this.inputStream.pushUserMessage(t)}}getHistory(){return[...this.conversationHistory]}getTurnCount(){return this.turnCount}async close(){if(this.currentState!=="closed"){this.currentState="closed",this.abortController.signal.aborted||this.abortController.abort("closed"),this.stateManager.resolveInitializationIfNeeded();try{await this.providerQuery.close()}catch{}if(await this.providerIterator.return?.(),this.initPromise)try{await Promise.race([this.initPromise,new Promise(t=>setTimeout(t,Ru))])}catch{}await this.dispatchSessionEndOnce("close")}}async dispatchSessionEndOnce(t){this.sessionEndDispatched||(this.sessionEndDispatched=!0,await this.emitClosure(t).catch(()=>{}),await this.sealTraceWriter(t).catch(()=>{}),await Cw(this._hookRegistry,{event:"SessionEnd",sessionId:this.sessionId,reason:t,parentSessionId:this.config.parentSessionId},this.config.traceWriter?{traceWriter:this.config.traceWriter}:{}))}async emitClosure(t){let n=this.config.traceWriter;if(!n)return;let r=this.deriveClosureReason(t),o={};this.sessionRunningTokens.input>0&&(o.input=this.sessionRunningTokens.input),this.sessionRunningTokens.output>0&&(o.output=this.sessionRunningTokens.output),this.sessionRunningTokens.cacheRead>0&&(o.cacheRead=this.sessionRunningTokens.cacheRead),this.sessionRunningTokens.cacheCreation>0&&(o.cacheCreation=this.sessionRunningTokens.cacheCreation),await bg(n,{reason:r,finalTurnCount:this.turnCount,finalCostUsd:this.sessionRunningCostUsd,finalTokens:o,...this.lastStopReason!==void 0?{lastStopReason:this.lastStopReason}:{}})}deriveClosureReason(t){if(t==="error")return"abort";let n=this.abortController.signal;if(n.aborted&&n.reason!=="closed"){let r=n.reason;if(r instanceof Tn)return"budget_exceeded";if(r instanceof pt)return"timeout";if(typeof r=="string"){if(r.startsWith("Budget "))return"budget_exceeded";if(r.includes("timed out"))return"timeout"}return"abort"}return"model_end_turn"}async sealTraceWriter(t){let n=this.config.traceWriter;if(!n)return;let r=this.deriveSealStatus(t),o=this.subagentCompletedCount>0?this.subagentCompletedCount:void 0,s=this.subagentRunningTokens,a=s.input>0||s.output>0||s.cacheRead>0||s.cacheCreation>0?{...s.input>0?{input:s.input}:{},...s.output>0?{output:s.output}:{},...s.cacheRead>0?{cacheRead:s.cacheRead}:{},...s.cacheCreation>0?{cacheCreation:s.cacheCreation}:{}}:void 0,l=this.subagentRunningCostUsd>0?this.subagentRunningCostUsd:void 0;await n.seal({status:r,finalCostUsd:this.sessionRunningCostUsd,finalTurnCount:this.turnCount,closedAt:new Date().toISOString(),...o!==void 0?{subagentCount:o}:{},...a!==void 0?{subagentTokens:a}:{},...l!==void 0?{subagentCostUsd:l}:{}})}recordSubagentCompletion(t,n){if(this.subagentCompletedCount++,t){let r=(o,s)=>{typeof o=="number"&&Number.isFinite(o)&&o>0&&(this.subagentRunningTokens[s]+=o)};r(t.inputTokens,"input"),r(t.outputTokens,"output"),r(t.cacheReadTokens,"cacheRead"),r(t.cacheCreationTokens,"cacheCreation")}typeof n=="number"&&Number.isFinite(n)&&n>0&&(this.subagentRunningCostUsd+=n)}deriveSealStatus(t){if(t==="error")return"failed";let n=this.abortController.signal;return n.aborted&&n.reason!=="closed"?"cancelled":"succeeded"}assertCanSend(){if(this.currentState==="closed")throw new Error("Cannot send message: session is closed");if(this.abortController.signal.aborted)throw new dt("Cannot send message: session aborted");if(this.currentState==="processing"||this.currentState==="streaming"||this.currentState==="compacting")throw new Error("Cannot send message: session is busy");if(this.config.maxTurns&&this.turnCount>=this.config.maxTurns)throw new Error(`Maximum turns (${this.config.maxTurns}) exceeded`)}};function Fw(){return Og()}var ZD=["shadow-verify","shadow_verify","resolve","diagnose","appmap","qualify","mint","review"],QD=[/\bverdict(s)?\b/i,/\brecommend(ation)?s?\b/i,/\bshould\s+(delete|remove|rewrite|refactor|rename|reject|merge|revert|disable)\b/i,/\b(USELESS|KEEP|REJECT|APPROVE|SALVAGE|BLOCK|FAIL)\b/,/\b(redundant|duplicated|superseded|obsolete)\b/i,/\bvulnerab\w*\b/i,/\bunused\b/i,/\bbroken\b/i,/\bregress\w*\b/i,/\|\s*(status|verdict|decision|severity|risk|finding|priority|holds\??)\s*\|/i,/\bfound\s+\d+\s*(issue|problem|bug|error|finding|vulnerabilit)/i,/\b(critical|high|medium|low)\s+(severity|priority|risk)\b/i,/\bclaim(s)?\b[^\n]{0,80}\b(holds?|refuted|verified|partial|confirmed|disputed)\b/i,/\b(root\s*cause|incident)\b/i,/\brecommend\s+(removing|deleting|rewriting|refactoring|merging|reverting)\b/i,/\bI\s+(applied|committed|pushed|edited|wrote|fixed|patched|reset|restored|staged)\b/i,/\b(applied|committed|pushed|fixed|patched)\s+(the|these|those)\s+(change|commit|fix|patch|edit)/i],eL=[/\bverifier_verdict\b/i,/"\s*claim\s*"\s*:/i,/\bre-derived\b[^.\n]{0,80}\bindependent/i,/\bindependently\s+(re-derived|re-verified|verified|checked)\b/i,/\bverifier\s+(agrees|disagrees|confirms|refutes)\b/i],tL=`shadow-verify nudge:
1942
1942
 
1943
1943
  The sub-agent that just finished returned output that reads like **decision-driving findings** (verdicts, recommendations, audit conclusions, or claim-style results that could drive file edits, deletions, commits, or external side-effects).
1944
1944
 
1945
1945
  Single-pass sub-agent reports are prone to confident hallucination \u2014 polished output that falls apart on re-derivation. Before acting on these conclusions, consider dispatching \`/shadow-verify\`. Independent verifiers will re-derive the 2\u20133 most load-bearing claims from scratch (without seeing the original reasoning) and flag any that don't hold up.
1946
1946
 
1947
- Skip when: the findings are purely exploratory, the sub-agent ran inside an already-verifying orchestrator, the user is about to dismiss the report, or the stakes are low (read-only Q&A).`,nL=ZD.map(e=>new RegExp(`(?:^|-)${e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}(?:-|$)`,"i"));function rL(e){return e?nL.some(t=>t.test(e)):!1}function oL(e){return eL.some(t=>t.test(e))}function sL(e){let t=0;for(let n of QD)n.test(e)&&t++;return t}function Nw(e){if(e.event!=="SubagentStop")return{};let t=e.lastMessage??"";return t.length<600?{}:rL(e.agentType)?{}:oL(t)?{}:sL(t)<2?{}:{injectContext:tL}}var iL=["git commit","git push","git reset","rm ","mv ","mkdir","touch","chmod","chown","cp ","tee "," > "," >> ","npm install","pnpm install","pip install","apt ","apt-get ","brew install"," && "];function Bw(e){return function(n){if(n.event!=="PreToolUse")return{};if(n.parentSessionId)return{};if(e()!=="plan")return{};let{toolName:r}=n;if(Mn(r)==="write")return{decision:"block",reason:`plan mode: ${r} is refused. Use /plan off to exit plan mode.`};if(r==="bash"){let o=typeof n.input=="object"&&n.input!==null?String(n.input.command??""):"";if(iL.some(i=>o.includes(i)))return{decision:"block",reason:"plan mode: write-intent bash is refused. Use /plan off or rephrase as a read-only command."}}return{}}}import{mkdirSync as cL,rmSync as uL,writeFileSync as dL}from"fs";import{join as Zd}from"path";function aL(e){let t=new Set;for(let a of e.nodes){if(t.has(a.id))throw new Error(`Duplicate node ID: ${a.id}`);t.add(a.id)}let n=new Set;for(let a of e.edges){if(!t.has(a.from))throw new Error(`Edge references non-existent node: ${a.from}`);if(!t.has(a.to))throw new Error(`Edge references non-existent node: ${a.to}`);let l=`${a.from}->${a.to}`;if(n.has(l))throw new Error(`Duplicate edge: ${a.from} -> ${a.to}`);n.add(l)}let r=Uw(e),o=new Map(r.inDegree),s=[];for(let[a,l]of o)l===0&&s.push(a);let i=0;for(;s.length>0;){let a=s.shift();i+=1;for(let l of r.downstream.get(a)??[]){let c=o.get(l)-1;o.set(l,c),c===0&&s.push(l)}}if(i!==t.size)throw new Error("Cycle detected in DAG")}function Uw(e){let t=new Map,n=new Map,r=new Map;for(let o of e.nodes)t.set(o.id,new Set),n.set(o.id,new Set),r.set(o.id,0);for(let o of e.edges)t.get(o.from).add(o.to),n.get(o.to).add(o.from),r.set(o.to,r.get(o.to)+1);return{downstream:t,upstream:n,inDegree:r}}function lL(e,t,n){let r=[e];for(;r.length>0;){let o=r.shift();for(let s of t.get(o)??[])n.has(s)||(n.add(s),r.push(s))}}async function jw(e,t,n={}){if(e.nodes.length===0)return{outputs:{},failed:[],skipped:[]};aL(e);let{failFast:r=!0,nodeTimeoutMs:o}=n,s=o!==void 0&&Number.isFinite(o)&&o>0,i=Uw(e),a=new Map(e.nodes.map(h=>[h.id,h])),l={},c=[],u=new Set,d=new Set,m=new Map(i.inDegree),f=new AbortController,g=()=>{f.signal.aborted||f.abort(t.reason)};t.aborted?f.abort(t.reason):t.addEventListener("abort",g,{once:!0});try{for(;!f.signal.aborted;){let h=[];for(let[y,w]of m)w===0&&!d.has(y)&&!u.has(y)&&h.push(y);if(h.length===0)break;let b=await Promise.allSettled(h.map(async y=>{let w=a.get(y),x=new AbortController,R=()=>{x.signal.aborted||x.abort(f.signal.reason)};f.signal.aborted?x.abort(f.signal.reason):f.signal.addEventListener("abort",R,{once:!0});let v;s&&!x.signal.aborted&&(v=setTimeout(()=>{x.signal.aborted||x.abort(new pt(`DAG node "${y}" exceeded nodeTimeoutMs of ${o}ms`,o))},o));let E={};for(let I of i.upstream.get(y)??[])E[I]=l[I];try{let I=await w.run(E,x.signal);return{id:y,result:I}}finally{v!==void 0&&clearTimeout(v),f.signal.removeEventListener("abort",R)}}));for(let y=0;y<b.length;y++){let w=b[y];if(w.status==="fulfilled"){let{id:x,result:R}=w.value;l[x]=R,d.add(x),m.delete(x);for(let v of i.downstream.get(x)??[])m.set(v,m.get(v)-1)}else{let x=w.reason instanceof Error?w.reason:new Error(String(w.reason)),R=h[y];c.push({id:R,error:x}),d.add(R),m.delete(R),lL(R,i.downstream,u),r&&f.abort("fail-fast")}}}}finally{t.removeEventListener("abort",g)}return{outputs:l,failed:c,skipped:Array.from(u)}}async function Ga(e){let{manager:t,parentSession:n,nodes:r,edges:o,failFast:s,nodeTimeoutMs:i}=e,a=n.abortSignal??new AbortController().signal,l=r.map(c=>({id:c.id,async run(u,d){let m=await t.forkSubagent({parent:{sessionId:n.sessionId},config:{model:c.model??"sonnet",systemPrompt:c.systemPrompt,...c.canUseTool!==void 0?{canUseTool:c.canUseTool}:{},...c.cwd!==void 0?{cwd:c.cwd}:{},...c.readRoots!==void 0?{readRoots:c.readRoots}:{},...c.writeRoots!==void 0?{writeRoots:c.writeRoots}:{}},idPrefix:c.idPrefix??`dag-${c.id}`,...c.outputSchema!==void 0?{outputSchema:c.outputSchema}:{},...c.agentType!==void 0?{agentType:c.agentType}:{},...c.parentId!==void 0?{parentId:c.parentId}:{}}),f=()=>{m.cancel().catch(()=>{})};d.aborted?m.cancel().catch(()=>{}):d.addEventListener("abort",f,{once:!0});try{if(d.aborted)throw new DOMException("Aborted","AbortError");let g=c.promptBuilder(u),h=await m.runToResult(g);if(h.status!=="succeeded"){let b,y=d.reason;throw y instanceof pt?b=new Error(`Subagent ${c.id} aborted: ${y.message}`,h.error?{cause:h.error}:{}):b=h.error??new Error(`Subagent ${c.id} ${h.status}`),Wy(b,{partialOutput:h.partialOutput,subagentId:h.id})}return h.output??h.message?.content}finally{d.removeEventListener("abort",f),await m.teardown().catch(()=>{})}}}));return jw({nodes:l,edges:o},a,{failFast:s,nodeTimeoutMs:i})}W();var Ww=1e3,qa=36e5,Hw=1,Kw=1e3;function pL(e){if(typeof e!="object"||e===null)throw new Error("Compose tool input must be an object");let t=e,n=t.nodes;if(!Array.isArray(n)||n.length===0)throw new Error('Compose tool requires a non-empty "nodes" array');let r=20;if(n.length>r)throw new Error(`Compose tool supports at most ${r} nodes (got ${n.length}). Split into multiple compose calls for larger workloads.`);let o=[],s=new Set;for(let d of n){if(typeof d!="object"||d===null)throw new Error("Each node must be an object");let m=d,f=m.id;if(typeof f!="string"||f.trim().length===0)throw new Error('Each node must have a non-empty "id" string');if(!/^[A-Za-z0-9_-]+$/.test(f)){let b=f.replace(/[\x00-\x1f\x7f]/g,"?").slice(0,32);throw new Error(`Node id "${b}" must match /^[A-Za-z0-9_-]+$/ (alphanumeric, underscore, hyphen)`)}if(s.has(f))throw new Error(`Duplicate node ID: ${f}`);s.add(f);let g=m.prompt;if(typeof g!="string"||g.trim().length===0)throw new Error(`Node "${f}" must have a non-empty "prompt" string`);let h;if(m.model!==void 0){if(typeof m.model!="string")throw new Error(`Node "${f}" model must be a string`);h=m.model}o.push({id:f,prompt:g,model:h})}let i;if(t.edges!==void 0){if(!Array.isArray(t.edges))throw new Error('"edges" must be an array');i=[];for(let d of t.edges){if(typeof d!="object"||d===null)throw new Error("Each edge must be an object");let m=d;if(typeof m.from!="string"||typeof m.to!="string")throw new Error('Each edge must have "from" and "to" strings');if(!s.has(m.from))throw new Error(`Edge references non-existent node: ${m.from}`);if(!s.has(m.to))throw new Error(`Edge references non-existent node: ${m.to}`);i.push({from:m.from,to:m.to})}}let a;if(t.fail_fast!==void 0){if(typeof t.fail_fast!="boolean")throw new Error('"fail_fast" must be a boolean');a=t.fail_fast}let l=[],c;if(t.node_timeout_ms!==void 0){let d=t.node_timeout_ms;if(typeof d!="number"||!Number.isFinite(d)||d<=0)throw new Error('"node_timeout_ms" must be a positive finite number (milliseconds)');if(d<Ww)throw new Error(`"node_timeout_ms" must be at least ${Ww}ms (got ${d}). Sub-second timeouts are almost always a unit mistake.`);c=Math.min(qa,d),d>qa&&l.push(`node_timeout_ms clamped: requested ${d}ms exceeds the maximum ${qa}ms; using ${qa}ms.`)}let u;if(t.max_tool_calls_per_node!==void 0){let d=t.max_tool_calls_per_node;if(typeof d!="number"||!Number.isFinite(d)||d<=0)throw new Error('"max_tool_calls_per_node" must be a positive finite number');if(!Number.isInteger(d))throw new Error(`"max_tool_calls_per_node" must be an integer (got ${d}). Tool calls are discrete events; fractional budgets are not meaningful.`);if(d<Hw)throw new Error(`"max_tool_calls_per_node" must be at least ${Hw}`);if(d>Kw)throw new Error(`"max_tool_calls_per_node" must be at most ${Kw} (got ${d}). A larger budget no longer constrains useful work.`);u=d}return{parsed:{nodes:o,edges:i,fail_fast:a,node_timeout_ms:c,max_tool_calls_per_node:u},warnings:l}}var Fs=8e3,Gw=500,qw=4e3;function mL(e){if(e==null)return;let t=typeof e=="string"?e:JSON.stringify(e);if(t.length!==0)return t.length>qw?t.slice(0,qw)+`
1947
+ Skip when: the findings are purely exploratory, the sub-agent ran inside an already-verifying orchestrator, the user is about to dismiss the report, or the stakes are low (read-only Q&A).`,nL=ZD.map(e=>new RegExp(`(?:^|-)${e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}(?:-|$)`,"i"));function rL(e){return e?nL.some(t=>t.test(e)):!1}function oL(e){return eL.some(t=>t.test(e))}function sL(e){let t=0;for(let n of QD)n.test(e)&&t++;return t}function Nw(e){if(e.event!=="SubagentStop")return{};let t=e.lastMessage??"";return t.length<600?{}:rL(e.agentType)?{}:oL(t)?{}:sL(t)<2?{}:{injectContext:tL}}var iL=["git commit","git push","git reset","rm ","mv ","mkdir","touch","chmod","chown","cp ","tee "," > "," >> ","npm install","pnpm install","pip install","apt ","apt-get ","brew install"," && "];function Bw(e){return function(n){if(n.event!=="PreToolUse")return{};if(n.parentSessionId)return{};if(e()!=="plan")return{};let{toolName:r}=n;if(Mn(r)==="write")return{decision:"block",reason:`plan mode: ${r} is refused. Use /plan off to exit plan mode.`};if(r==="bash"){let o=typeof n.input=="object"&&n.input!==null?String(n.input.command??""):"";if(iL.some(i=>o.includes(i)))return{decision:"block",reason:"plan mode: write-intent bash is refused. Use /plan off or rephrase as a read-only command."}}return{}}}import{mkdirSync as cL,rmSync as uL,writeFileSync as dL}from"fs";import{join as Zd}from"path";function aL(e){let t=new Set;for(let a of e.nodes){if(t.has(a.id))throw new Error(`Duplicate node ID: ${a.id}`);t.add(a.id)}let n=new Set;for(let a of e.edges){if(!t.has(a.from))throw new Error(`Edge references non-existent node: ${a.from}`);if(!t.has(a.to))throw new Error(`Edge references non-existent node: ${a.to}`);let l=`${a.from}->${a.to}`;if(n.has(l))throw new Error(`Duplicate edge: ${a.from} -> ${a.to}`);n.add(l)}let r=Uw(e),o=new Map(r.inDegree),s=[];for(let[a,l]of o)l===0&&s.push(a);let i=0;for(;s.length>0;){let a=s.shift();i+=1;for(let l of r.downstream.get(a)??[]){let c=o.get(l)-1;o.set(l,c),c===0&&s.push(l)}}if(i!==t.size)throw new Error("Cycle detected in DAG")}function Uw(e){let t=new Map,n=new Map,r=new Map;for(let o of e.nodes)t.set(o.id,new Set),n.set(o.id,new Set),r.set(o.id,0);for(let o of e.edges)t.get(o.from).add(o.to),n.get(o.to).add(o.from),r.set(o.to,r.get(o.to)+1);return{downstream:t,upstream:n,inDegree:r}}function lL(e,t,n){let r=[e];for(;r.length>0;){let o=r.shift();for(let s of t.get(o)??[])n.has(s)||(n.add(s),r.push(s))}}async function jw(e,t,n={}){if(e.nodes.length===0)return{outputs:{},failed:[],skipped:[]};aL(e);let{failFast:r=!0,nodeTimeoutMs:o}=n,s=o!==void 0&&Number.isFinite(o)&&o>0,i=Uw(e),a=new Map(e.nodes.map(h=>[h.id,h])),l={},c=[],u=new Set,d=new Set,p=new Map(i.inDegree),f=new AbortController,g=()=>{f.signal.aborted||f.abort(t.reason)};t.aborted?f.abort(t.reason):t.addEventListener("abort",g,{once:!0});try{for(;!f.signal.aborted;){let h=[];for(let[y,w]of p)w===0&&!d.has(y)&&!u.has(y)&&h.push(y);if(h.length===0)break;let b=await Promise.allSettled(h.map(async y=>{let w=a.get(y),x=new AbortController,R=()=>{x.signal.aborted||x.abort(f.signal.reason)};f.signal.aborted?x.abort(f.signal.reason):f.signal.addEventListener("abort",R,{once:!0});let v;s&&!x.signal.aborted&&(v=setTimeout(()=>{x.signal.aborted||x.abort(new pt(`DAG node "${y}" exceeded nodeTimeoutMs of ${o}ms`,o))},o));let E={};for(let I of i.upstream.get(y)??[])E[I]=l[I];try{let I=await w.run(E,x.signal);return{id:y,result:I}}finally{v!==void 0&&clearTimeout(v),f.signal.removeEventListener("abort",R)}}));for(let y=0;y<b.length;y++){let w=b[y];if(w.status==="fulfilled"){let{id:x,result:R}=w.value;l[x]=R,d.add(x),p.delete(x);for(let v of i.downstream.get(x)??[])p.set(v,p.get(v)-1)}else{let x=w.reason instanceof Error?w.reason:new Error(String(w.reason)),R=h[y];c.push({id:R,error:x}),d.add(R),p.delete(R),lL(R,i.downstream,u),r&&f.abort("fail-fast")}}}}finally{t.removeEventListener("abort",g)}return{outputs:l,failed:c,skipped:Array.from(u)}}async function Ga(e){let{manager:t,parentSession:n,nodes:r,edges:o,failFast:s,nodeTimeoutMs:i}=e,a=n.abortSignal??new AbortController().signal,l=r.map(c=>({id:c.id,async run(u,d){let p=await t.forkSubagent({parent:{sessionId:n.sessionId},config:{model:c.model??"sonnet",systemPrompt:c.systemPrompt,...c.canUseTool!==void 0?{canUseTool:c.canUseTool}:{},...c.cwd!==void 0?{cwd:c.cwd}:{},...c.readRoots!==void 0?{readRoots:c.readRoots}:{},...c.writeRoots!==void 0?{writeRoots:c.writeRoots}:{}},idPrefix:c.idPrefix??`dag-${c.id}`,...c.outputSchema!==void 0?{outputSchema:c.outputSchema}:{},...c.agentType!==void 0?{agentType:c.agentType}:{},...c.parentId!==void 0?{parentId:c.parentId}:{}}),f=()=>{p.cancel().catch(()=>{})};d.aborted?p.cancel().catch(()=>{}):d.addEventListener("abort",f,{once:!0});try{if(d.aborted)throw new DOMException("Aborted","AbortError");let g=c.promptBuilder(u),h=await p.runToResult(g);if(h.status!=="succeeded"){let b,y=d.reason;throw y instanceof pt?b=new Error(`Subagent ${c.id} aborted: ${y.message}`,h.error?{cause:h.error}:{}):b=h.error??new Error(`Subagent ${c.id} ${h.status}`),Wy(b,{partialOutput:h.partialOutput,subagentId:h.id})}return h.output??h.message?.content}finally{d.removeEventListener("abort",f),await p.teardown().catch(()=>{})}}}));return jw({nodes:l,edges:o},a,{failFast:s,nodeTimeoutMs:i})}W();var Ww=1e3,qa=36e5,Hw=1,Kw=1e3;function pL(e){if(typeof e!="object"||e===null)throw new Error("Compose tool input must be an object");let t=e,n=t.nodes;if(!Array.isArray(n)||n.length===0)throw new Error('Compose tool requires a non-empty "nodes" array');let r=20;if(n.length>r)throw new Error(`Compose tool supports at most ${r} nodes (got ${n.length}). Split into multiple compose calls for larger workloads.`);let o=[],s=new Set;for(let d of n){if(typeof d!="object"||d===null)throw new Error("Each node must be an object");let p=d,f=p.id;if(typeof f!="string"||f.trim().length===0)throw new Error('Each node must have a non-empty "id" string');if(!/^[A-Za-z0-9_-]+$/.test(f)){let b=f.replace(/[\x00-\x1f\x7f]/g,"?").slice(0,32);throw new Error(`Node id "${b}" must match /^[A-Za-z0-9_-]+$/ (alphanumeric, underscore, hyphen)`)}if(s.has(f))throw new Error(`Duplicate node ID: ${f}`);s.add(f);let g=p.prompt;if(typeof g!="string"||g.trim().length===0)throw new Error(`Node "${f}" must have a non-empty "prompt" string`);let h;if(p.model!==void 0){if(typeof p.model!="string")throw new Error(`Node "${f}" model must be a string`);h=p.model}o.push({id:f,prompt:g,model:h})}let i;if(t.edges!==void 0){if(!Array.isArray(t.edges))throw new Error('"edges" must be an array');i=[];for(let d of t.edges){if(typeof d!="object"||d===null)throw new Error("Each edge must be an object");let p=d;if(typeof p.from!="string"||typeof p.to!="string")throw new Error('Each edge must have "from" and "to" strings');if(!s.has(p.from))throw new Error(`Edge references non-existent node: ${p.from}`);if(!s.has(p.to))throw new Error(`Edge references non-existent node: ${p.to}`);i.push({from:p.from,to:p.to})}}let a;if(t.fail_fast!==void 0){if(typeof t.fail_fast!="boolean")throw new Error('"fail_fast" must be a boolean');a=t.fail_fast}let l=[],c;if(t.node_timeout_ms!==void 0){let d=t.node_timeout_ms;if(typeof d!="number"||!Number.isFinite(d)||d<=0)throw new Error('"node_timeout_ms" must be a positive finite number (milliseconds)');if(d<Ww)throw new Error(`"node_timeout_ms" must be at least ${Ww}ms (got ${d}). Sub-second timeouts are almost always a unit mistake.`);c=Math.min(qa,d),d>qa&&l.push(`node_timeout_ms clamped: requested ${d}ms exceeds the maximum ${qa}ms; using ${qa}ms.`)}let u;if(t.max_tool_calls_per_node!==void 0){let d=t.max_tool_calls_per_node;if(typeof d!="number"||!Number.isFinite(d)||d<=0)throw new Error('"max_tool_calls_per_node" must be a positive finite number');if(!Number.isInteger(d))throw new Error(`"max_tool_calls_per_node" must be an integer (got ${d}). Tool calls are discrete events; fractional budgets are not meaningful.`);if(d<Hw)throw new Error(`"max_tool_calls_per_node" must be at least ${Hw}`);if(d>Kw)throw new Error(`"max_tool_calls_per_node" must be at most ${Kw} (got ${d}). A larger budget no longer constrains useful work.`);u=d}return{parsed:{nodes:o,edges:i,fail_fast:a,node_timeout_ms:c,max_tool_calls_per_node:u},warnings:l}}var Fs=8e3,Gw=500,qw=4e3;function mL(e){if(e==null)return;let t=typeof e=="string"?e:JSON.stringify(e);if(t.length!==0)return t.length>qw?t.slice(0,qw)+`
1948
1948
  \u2026 (truncated)`:t}function fL(e,t,n,r){try{let o=Zd(vn(),e,"compose",t);cL(o,{recursive:!0});let s=Zd(o,`${n}.txt`);return dL(s,r,"utf8"),s}catch{return}}function gL(e,t){let n=[],r=[];for(let[o,s]of Object.entries(e.outputs)){let i=typeof s=="string"?s:s!=null?JSON.stringify(s):"(no output)",a;if(i.length>Fs){let l=fL(t.sessionId,t.callId,o,i);r.push({nodeId:o,emittedChars:Fs,totalChars:i.length,...l!==void 0?{spillPath:l}:{}});let c=l!==void 0?`
1949
1949
  \u2026 (truncated at ${Fs} / ${i.length} chars \u2014 full output at ${l})`:`
1950
1950
  \u2026 (truncated at ${Fs} / ${i.length} chars)`;a=i.slice(0,Fs)+c}else a=i;n.push(`## ${o}
@@ -1955,7 +1955,7 @@ ${i}`:s;n.push(`## ${o.id} [FAILED]
1955
1955
  ${a}`)}return e.skipped.length>0&&n.push(`## Skipped
1956
1956
  ${e.skipped.join(", ")}`),{content:n.join(`
1957
1957
 
1958
- `),truncations:r}}function zw(e){if(e)try{let t=Zd(vn(),e,"compose");uL(t,{recursive:!0,force:!0})}catch{}}function hL(e){let t=`node "${e.nodeId}" output truncated: emitted ${e.emittedChars} of ${e.totalChars} chars`;return e.spillPath!==void 0?`${t}; full output at ${e.spillPath} (use read_file to retrieve)`:`${t}; full output unavailable (spill write failed)`}var Nn=class{constructor(t){this.ctx=t}ctx;async execute(t){if(t.signal.aborted)return{content:"Compose tool call aborted",isError:!0};let n,r;try{({parsed:n,warnings:r}=pL(t.input))}catch(d){return{content:`Compose tool input validation failed: ${d instanceof Error?d.message:String(d)}`,isError:!0}}if(!this.ctx.apiKey||this.ctx.apiKey.length===0)return{content:"Compose tool requires an API key (ctx.apiKey is missing or empty)",isError:!0};let o=n.max_tool_calls_per_node,s=new Map,i=new Set,a=gt(),l,c=(d,m)=>{if(a!==void 0)try{a(d,m)}catch{}if(!l||o===void 0||d.type!=="chunk"||d.chunk.type!=="tool_use_detail")return;let f=(s.get(m.subagentId)??0)+1;s.set(m.subagentId,f),f>o&&!i.has(m.subagentId)&&(i.add(m.subagentId),l.kill(m.subagentId).catch(()=>{}))};l=new X({parentAbortSignal:t.signal,apiKey:this.ctx.apiKey,progressSink:c,...this.ctx.baseUrl!==void 0?{baseUrl:this.ctx.baseUrl}:{}});let u=Date.now();Te({event:"compose.started",parent_session_id:this.ctx.parentSession.sessionId,node_count:n.nodes.length,edge_count:n.edges?.length??0}).catch(()=>{});try{let d=t.id,m=n.nodes.length,f=n.nodes.map((I,D)=>({id:I.id,agentType:`${I.id} [${D+1}/${m}]`,parentId:d,systemPrompt:this.ctx.systemPrompt,promptBuilder:_=>{let M=Object.entries(_).map(([P,$])=>{let F=typeof $=="string"?$:JSON.stringify($);return`<<<UPSTREAM_OUTPUT_BEGIN node="${P}">>>
1958
+ `),truncations:r}}function zw(e){if(e)try{let t=Zd(vn(),e,"compose");uL(t,{recursive:!0,force:!0})}catch{}}function hL(e){let t=`node "${e.nodeId}" output truncated: emitted ${e.emittedChars} of ${e.totalChars} chars`;return e.spillPath!==void 0?`${t}; full output at ${e.spillPath} (use read_file to retrieve)`:`${t}; full output unavailable (spill write failed)`}var Nn=class{constructor(t){this.ctx=t}ctx;async execute(t){if(t.signal.aborted)return{content:"Compose tool call aborted",isError:!0};let n,r;try{({parsed:n,warnings:r}=pL(t.input))}catch(d){return{content:`Compose tool input validation failed: ${d instanceof Error?d.message:String(d)}`,isError:!0}}if(!this.ctx.apiKey||this.ctx.apiKey.length===0)return{content:"Compose tool requires an API key (ctx.apiKey is missing or empty)",isError:!0};let o=n.max_tool_calls_per_node,s=new Map,i=new Set,a=gt(),l,c=(d,p)=>{if(a!==void 0)try{a(d,p)}catch{}if(!l||o===void 0||d.type!=="chunk"||d.chunk.type!=="tool_use_detail")return;let f=(s.get(p.subagentId)??0)+1;s.set(p.subagentId,f),f>o&&!i.has(p.subagentId)&&(i.add(p.subagentId),l.kill(p.subagentId).catch(()=>{}))};l=new X({parentAbortSignal:t.signal,apiKey:this.ctx.apiKey,progressSink:c,...this.ctx.baseUrl!==void 0?{baseUrl:this.ctx.baseUrl}:{}});let u=Date.now();Te({event:"compose.started",parent_session_id:this.ctx.parentSession.sessionId,node_count:n.nodes.length,edge_count:n.edges?.length??0}).catch(()=>{});try{let d=t.id,p=n.nodes.length,f=n.nodes.map((I,D)=>({id:I.id,agentType:`${I.id} [${D+1}/${p}]`,parentId:d,systemPrompt:this.ctx.systemPrompt,promptBuilder:_=>{let M=Object.entries(_).map(([P,$])=>{let F=typeof $=="string"?$:JSON.stringify($);return`<<<UPSTREAM_OUTPUT_BEGIN node="${P}">>>
1959
1959
  ${F}
1960
1960
  <<<UPSTREAM_OUTPUT_END node="${P}">>>`}).join(`
1961
1961
 
@@ -1969,7 +1969,7 @@ ${M}`:I.prompt},model:I.model??this.ctx.defaultSubagentModel??this.ctx.defaultMo
1969
1969
  ${x.map(I=>`> - ${I}`).join(`
1970
1970
  `)}
1971
1971
 
1972
- `:"")+b,E=g.failed.length>0;return{content:v,isError:E}}catch(d){let m=d instanceof Error?d.message:String(d);return Te({event:"compose.failed",parent_session_id:this.ctx.parentSession.sessionId,error_message:m.slice(0,240),duration_ms:Date.now()-u}).catch(()=>{}),{content:`Compose execution error: ${m}`,isError:!0}}finally{await l.teardownAll()}}};function So(e,t,n,r){let o=Fw();o.register("SubagentStop",Nw);let s=n??new We;return r!==void 0&&o.register("PreToolUse",Bw(r)),o.register("SessionEnd",sd(s,t)),o.register("SessionEnd",i=>i.event!=="SessionEnd"?{}:(i.sessionId&&zw(i.sessionId),{})),e&&o.register("SubagentStop",i=>i.event!=="SubagentStop"?{}:i.status==="idle"||i.status==="running"?{}:(e({subagentId:i.subagentId,status:i.status,durationMs:i.durationMs,agentType:i.agentType}),{})),{registry:o,memoryStore:s}}function ne(e){let t=Math.round(e/1e3);if(t<=0)return"0s";let n=Math.floor(t/86400),r=Math.floor(t%86400/3600),o=Math.floor(t%3600/60),s=t%60;return n>0?`${n}d ${r}h`:r>0?`${r}h ${o}m`:o>0?`${o}m ${s}s`:`${s}s`}function Ne(e){return e===0?"$0.00":e<.01?`$${e.toFixed(4)}`:`$${e.toFixed(2)}`}function re(e){if(!Number.isFinite(e))return"0";if(e<1e3)return String(e);if(e<1e6){let n=e/1e3;return n%1===0?`${n}k`:`${n.toFixed(1).replace(/\.0$/,"")}k`}let t=e/1e6;return t%1===0?`${t}m`:`${t.toFixed(1).replace(/\.0$/,"")}m`}var yL="[skill-routing: active]\n\nRoute recurring work through registered skills instead of rolling ad-hoc solutions:\n\n- Before non-trivial implementation (multi-file edits, new features, config/build changes \u2014 anything that writes) \u2192 `/ground-state` first. Do NOT substitute inline `git status`/`get_runtime_state` \u2014 the skill triangulates git + infra + prior-session memory in parallel, which the inline checks miss. If `/ground-state` dispatch fails (depth limit, unavailable), fall back to inline checks AND note the coverage gap.\n- Bugs, failing tests, or regressions \u2192 `/diagnose`\n- High-stakes sub-agent output that will drive edits or commits \u2192 `/shadow-verify` before acting\n- Refactor needing parallel waves \u2192 `/parallelize`\n- Parallel or dependent multi-task work \u2192 `compose` tool (DAG of subagent nodes)\n- Greenfield feature where a written spec would genuinely help (novel scope, multi-day work, or external stakeholders involved) \u2192 `/mint`\n\nDo NOT reach for `/mint` for: bug fixes (use `/diagnose`), refactors with known shape, single-feature edits, work already spec'd in chat, or anything where the spec/approve pause would feel like ceremony. Implement directly in those cases.\n\nCommon composed sequences \u2014 reach for these when the task shape matches:\n\n- Bug with failing test and non-trivial fix \u2192 `/diagnose` \u2192 `/shadow-verify` on the proposed fix\n- Refactor needing parallel waves \u2192 plan \u2192 `/parallelize` \u2192 build waves\n- Diagnose + fix in parallel \u2192 `compose` with two independent nodes\n- Research \u2192 implement \u2192 verify pipeline \u2192 `compose` with edges: research\u2192implement\u2192verify\n- Multiple independent investigations \u2192 `compose` with N nodes, no edges\n\nReach for context-isolated investigators when the task is exploratory:\n\n- Map an unfamiliar module before editing \u2192 `/gather` or `/research`\n- Re-derive a load-bearing claim independently \u2192 `/shadow-verify`\n- Audit a diff before merge \u2192 `/review`\n- Generate alternatives before committing to a plan \u2192 `/devils-advocate`\n\nOr dispatch a raw `agent` call when no skill matches but the work is parallelizable, verification-heavy, or would otherwise consume substantial inline context.\n\nSkip orchestration for: single-line edits, trivial Q&A, and direct tool calls the user explicitly requested. The goal is leverage, not ceremony. If a skill would add overhead without adding value, don't invoke it.\n\nDefault to acting autonomously. `ask_question` is a last resort, not a first move \u2014 every question blocks on the operator, who is often away from keyboard.\n\nBefore you ask, you MUST exhaust the tools you have: read the files, check git, search the codebase and docs, inspect runtime state. If any tool can get you the answer, use the tool \u2014 never ask the operator for something you can discover yourself. When a wrong guess would be cheap or reversible, make a reasonable assumption, proceed, and state the assumption instead of asking.\n\nReserve `ask_question` for the narrow set of things no tool can resolve: a genuinely ambiguous requirement whose readings lead to materially different work, a decision with significant or irreversible consequences, or context that lives only in the operator's head (a preference, a secret, an external constraint):\n\n- Question types: `text` (open-ended), `confirm` (yes/no), `choice` (single pick from list), `multi_choice` (multi-pick), `number` (numeric with optional bounds).\n- Ask one focused question at a time. Do NOT ask multiple questions in a single call, and do NOT stack several ask_question calls across a turn \u2014 fold the genuine unknowns into the single most decision-relevant question.\n- Do NOT use when the user has already provided sufficient context \u2014 infer and proceed instead.\n- The result `action` will be `accept` (answered), `cancel` (user interrupted), `decline` (no handler), or `skip` (optional question skipped).\n- After a `cancel` or `decline`, stop and tell the user what information you need \u2014 do not loop and re-ask.",bL=`[end-of-turn protocol]
1972
+ `:"")+b,E=g.failed.length>0;return{content:v,isError:E}}catch(d){let p=d instanceof Error?d.message:String(d);return Te({event:"compose.failed",parent_session_id:this.ctx.parentSession.sessionId,error_message:p.slice(0,240),duration_ms:Date.now()-u}).catch(()=>{}),{content:`Compose execution error: ${p}`,isError:!0}}finally{await l.teardownAll()}}};function So(e,t,n,r){let o=Fw();o.register("SubagentStop",Nw);let s=n??new We;return r!==void 0&&o.register("PreToolUse",Bw(r)),o.register("SessionEnd",sd(s,t)),o.register("SessionEnd",i=>i.event!=="SessionEnd"?{}:(i.sessionId&&zw(i.sessionId),{})),e&&o.register("SubagentStop",i=>i.event!=="SubagentStop"?{}:i.status==="idle"||i.status==="running"?{}:(e({subagentId:i.subagentId,status:i.status,durationMs:i.durationMs,agentType:i.agentType}),{})),{registry:o,memoryStore:s}}function ne(e){let t=Math.round(e/1e3);if(t<=0)return"0s";let n=Math.floor(t/86400),r=Math.floor(t%86400/3600),o=Math.floor(t%3600/60),s=t%60;return n>0?`${n}d ${r}h`:r>0?`${r}h ${o}m`:o>0?`${o}m ${s}s`:`${s}s`}function Ne(e){return e===0?"$0.00":e<.01?`$${e.toFixed(4)}`:`$${e.toFixed(2)}`}function re(e){if(!Number.isFinite(e))return"0";if(e<1e3)return String(e);if(e<1e6){let n=e/1e3;return n%1===0?`${n}k`:`${n.toFixed(1).replace(/\.0$/,"")}k`}let t=e/1e6;return t%1===0?`${t}m`:`${t.toFixed(1).replace(/\.0$/,"")}m`}var yL="[skill-routing: active]\n\nRoute recurring work through registered skills instead of rolling ad-hoc solutions:\n\n- Before non-trivial implementation (multi-file edits, new features, config/build changes \u2014 anything that writes) \u2192 `/ground-state` first. Do NOT substitute inline `git status`/`get_runtime_state` \u2014 the skill triangulates git + infra + prior-session memory in parallel, which the inline checks miss. If `/ground-state` dispatch fails (depth limit, unavailable), fall back to inline checks AND note the coverage gap.\n- Bugs, failing tests, or regressions \u2192 `/diagnose`\n- High-stakes sub-agent output that will drive edits or commits \u2192 `/shadow-verify` before acting\n- Refactor needing parallel waves \u2192 `/parallelize`\n- Parallel or dependent multi-task work \u2192 `compose` tool (DAG of subagent nodes)\n- Greenfield feature where a written spec would genuinely help (novel scope, multi-day work, or external stakeholders involved) \u2192 `/mint`\n\nDo NOT reach for `/mint` for: bug fixes (use `/diagnose`), refactors with known shape, single-feature edits, work already spec'd in chat, or anything where the spec/approve pause would feel like ceremony. Implement directly in those cases.\n\nCommon composed sequences \u2014 reach for these when the task shape matches:\n\n- Bug with failing test and non-trivial fix \u2192 `/diagnose` \u2192 `/shadow-verify` on the proposed fix\n- Refactor needing parallel waves \u2192 plan \u2192 `/parallelize` \u2192 build waves\n- Diagnose + fix in parallel \u2192 `compose` with two independent nodes\n- Research \u2192 implement \u2192 verify pipeline \u2192 `compose` with edges: research\u2192implement\u2192verify\n- Multiple independent investigations \u2192 `compose` with N nodes, no edges\n\nReach for context-isolated investigators when the task is exploratory:\n\n- Map an unfamiliar module before editing \u2192 `/gather` or `/research`\n- Re-derive a load-bearing claim independently \u2192 `/shadow-verify`\n- Audit a diff before merge \u2192 `/review`\n- Generate alternatives before committing to a plan \u2192 `/devils-advocate`\n\nOr dispatch a raw `agent` call when no skill matches but the work is parallelizable, verification-heavy, or would otherwise consume substantial inline context.\n\nSkip orchestration for: single-line edits, trivial Q&A, and direct tool calls the user explicitly requested. The goal is leverage, not ceremony. If a skill would add overhead without adding value, don't invoke it.\n\nDefault to acting autonomously. `ask_question` is a last resort, not a first move \u2014 every question blocks on the operator, who is often away from keyboard.\n\nBefore you ask, you MUST exhaust the tools you have: read the files, check git, search the codebase and docs, inspect runtime state. If any tool can get you the answer, use the tool \u2014 never ask the operator for something you can discover yourself. When a wrong guess would be cheap or reversible, make a reasonable assumption, proceed, and state the assumption instead of asking.\n\nReserve `ask_question` for the narrow set of things no tool can resolve: a genuinely ambiguous requirement whose readings lead to materially different work, a decision with significant or irreversible consequences, or context that lives only in the operator's head (a preference, a secret, an external constraint):\n\n- Question types: `text` (open-ended), `confirm` (yes/no), `choice` (single pick from list), `multi_choice` (multi-pick), `number` (numeric with optional bounds).\n- Ask one focused question at a time. Do NOT ask multiple questions in a single call, and do NOT stack several ask_question calls across a turn \u2014 fold the genuine unknowns into the single most decision-relevant question.\n- Do NOT use when the user has already provided sufficient context \u2014 infer and proceed instead.\n- The result `action` will be `accept` (answered), `cancel` (user interrupted), `decline` (no handler), or `skip` (optional question skipped).\n- After a `cancel` or `decline`, stop and tell the user what information you need \u2014 do not loop and re-ask.",bL=`[end-of-turn protocol]
1973
1973
 
1974
1974
  Every turn must end in one externally identifiable terminal state. AFK users need inspectable artifacts, not ceremony.
1975
1975
 
@@ -1996,33 +1996,33 @@ Every turn must end in one externally identifiable terminal state. AFK users nee
1996
1996
 
1997
1997
  Never end a turn mid-loop without one of these. The terminal-state heading must be the last block of the response, with no trailing prose after it.`,wL=new Set(["repl","telegram"]);function za(e,t,n="one-shot"){if(!e)return e;let r=[e];return t&&r.push(yL),wL.has(n)&&r.push(bL),r.join(`
1998
1998
 
1999
- `)}import mn from"chalk";var SL={read:mn.hex("#C9B584"),write:mn.hex("#E8A33D"),shell:mn.hex("#A8E060"),subagent:p.plan,skill:mn.hex("#F08AC4"),dag:mn.hex("#4EC9B0"),mcp:mn.hex("#5FE0C0"),web:mn.hex("#A0C4C0"),browser:mn.hex("#FF8A65"),planning:p.meta,schedule:mn.hex("#D4A84B"),other:p.meta},kL={read:"\u25CF",write:"\u270E",shell:"$",subagent:"\u2192",skill:"\u25C6",dag:"\u2B21",mcp:"\u22A1",web:"\u2316",browser:"\u25C9",planning:"\u25B1",schedule:"\u23F2",other:"\u25CF"};function Qd(e){return{color:SL[e],glyph:kL[e]}}function ko(e){return Qd(Mn(e))}function Ja(e){let{description:t,summary:n,lastToolName:r,totalTokens:o,toolUses:s,durationMs:i}=e,a=[];if(r){let{color:c,glyph:u}=ko(r);a.push(`via ${c(`${u} ${r}`)}`)}s&&a.push(`${s} tool${s===1?"":"s"}`),o&&a.push(`${re(o)} tok`),i&&a.push(ne(i)),a.push("esc to interrupt \xB7 ctrl+b background");let l=a.length>0?` (${a.join(" \xB7 ")})`:"";return n?[p.dim(` \u25E6 ${t}`),p.dim(` ${n}${l}`)]:[p.dim(` \u25E6 ${t}${l}`)]}function Va(e){let t=e.status==="succeeded"?"\u2713":e.status==="failed"?"\u2717":"\u2298",n=e.agentType??e.subagentId,r=[t,n];return e.durationMs!==void 0&&r.push(`\xB7 ${ne(e.durationMs)}`),p.dim(` ${r.join(" ")}`)}G();W();import{randomUUID as JL}from"node:crypto";import{createHash as KL}from"crypto";import{mkdir as GL,open as qL,writeFile as zL}from"fs/promises";import{join as aS}from"path";import{z as S}from"zod";var vL=S.object({phase:S.literal("started"),toolUseId:S.string(),name:S.string(),inputBytes:S.number().int().nonnegative(),subagentId:S.string().optional()}),TL=S.object({phase:S.literal("completed"),toolUseId:S.string(),name:S.string(),resultBytes:S.number().int().nonnegative(),isError:S.boolean(),truncated:S.boolean(),durationMs:S.number().nonnegative(),subagentId:S.string().optional()}),Jw=S.discriminatedUnion("phase",[vL,TL]),xL=S.enum(["PreToolUse","PostToolUse","SessionStart","SessionEnd","SubagentStart","SubagentStop"]),Vw=S.object({hookEvent:xL,decision:S.union([S.literal("block"),S.literal("approve"),S.undefined()]),reason:S.string().optional(),blockedTool:S.string().optional(),injectedContextBytes:S.number().int().nonnegative().optional()}),EL=S.object({transition:S.literal("started"),subagentId:S.string(),parentId:S.string(),model:S.string(),allowedTools:S.array(S.string()).readonly().optional(),systemPromptHash:S.string().optional()}),RL=S.object({transition:S.literal("succeeded"),subagentId:S.string(),durationMs:S.number().nonnegative(),turnCount:S.number().int().nonnegative(),totalCostUsd:S.number().nonnegative().optional(),outputBytes:S.number().int().nonnegative()}),AL=S.object({transition:S.literal("failed"),subagentId:S.string(),errorClass:S.string(),errorMessage:S.string(),partialOutputBytes:S.number().int().nonnegative()}),_L=S.object({transition:S.literal("cancelled"),subagentId:S.string(),source:S.enum(["cascade","explicit"])}),Yw=S.discriminatedUnion("transition",[EL,RL,AL,_L]),CL=S.object({transition:S.literal("started"),jobId:S.string(),subagentId:S.string(),label:S.string(),model:S.string()}),IL=S.object({transition:S.literal("completed"),jobId:S.string(),subagentId:S.string(),durationMs:S.number().nonnegative(),outputBytes:S.number().int().nonnegative()}),PL=S.object({transition:S.literal("failed"),jobId:S.string(),subagentId:S.string(),durationMs:S.number().nonnegative(),errorClass:S.string(),errorMessage:S.string()}),ML=S.object({transition:S.literal("cancelled"),jobId:S.string(),subagentId:S.string(),source:S.enum(["explicit","cascade"])}),OL=S.object({transition:S.literal("joined"),jobId:S.string(),subagentId:S.string(),jobStatus:S.enum(["completed","failed","cancelled"])}),Xw=S.discriminatedUnion("transition",[CL,IL,PL,ML,OL]),Zw=S.object({kind:S.literal("monetary"),runningCostUsd:S.number().nonnegative(),maxBudgetUsd:S.number().nonnegative(),lastTurnCostUsd:S.number().nonnegative()}),$L=S.enum(["user_signal","cascade","timeout","budget","hook_block"]),Qw=S.object({origin:$L,cascadedTo:S.array(S.string()).readonly(),reason:S.string().optional()}),eS=S.enum(["manual","token_threshold","turn_count"]),DL=S.object({path:S.string(),sizeBytes:S.number().int().nonnegative(),sha256:S.string().regex(/^[0-9a-f]{64}$/)}),LL=S.object({trigger:eS,preCompactionMessages:S.array(S.unknown()),summary:S.string(),keptTailCount:S.number().int().nonnegative(),keepLastNConfig:S.number().int().nonnegative(),messagesBefore:S.number().int().nonnegative(),messagesAfter:S.number().int().nonnegative(),tokensSavedEstimate:S.number().nonnegative().optional(),summarizationTokens:S.object({input:S.number().int().nonnegative(),output:S.number().int().nonnegative()}).optional()}),FL=S.object({trigger:eS,preCompactionMessagesRef:DL,summary:S.string(),keptTailCount:S.number().int().nonnegative(),keepLastNConfig:S.number().int().nonnegative(),messagesBefore:S.number().int().nonnegative(),messagesAfter:S.number().int().nonnegative(),tokensSavedEstimate:S.number().nonnegative().optional(),summarizationTokens:S.object({input:S.number().int().nonnegative(),output:S.number().int().nonnegative()}).optional()}),NL=S.enum(["model_end_turn","iteration_cap","abort","timeout","budget_exceeded","hook_blocked","max_turns_exceeded"]),tS=S.object({reason:NL,finalTurnCount:S.number().int().nonnegative(),finalCostUsd:S.number().nonnegative(),finalTokens:S.object({input:S.number().int().nonnegative().optional(),output:S.number().int().nonnegative().optional(),cacheRead:S.number().int().nonnegative().optional(),cacheCreation:S.number().int().nonnegative().optional()}),lastStopReason:S.string().optional()}),nS=S.object({source:S.string(),assertion:S.string(),evidence:S.array(S.string()).readonly(),confidence:S.number().min(0).max(1),dissent:S.string().optional()}),BL=S.enum(["browser_open","browser_observe","browser_act","browser_screenshot","browser_extract","browser_close"]),UL=S.enum(["click","fill","press","select","hover","scroll_to","wait_for"]),jL=S.object({kind:S.enum(["semantic","element_id","selector"]),text:S.string().max(80).optional(),role:S.string().optional(),elementId:S.string().optional(),selectorHash:S.string().regex(/^[0-9a-f]{8}$/).optional()}),rS=S.object({tool:BL,action:UL.optional(),toolUseId:S.string(),target:jL.optional(),urlBefore:S.string().nullable(),urlAfter:S.string().nullable(),status:S.enum(["ok","error","ambiguous_target","blocked_by_policy"]),screenshotPath:S.string().optional(),observationSummary:S.string().max(500).optional(),error:S.object({reason:S.string(),recoverable:S.boolean()}).optional(),durationMs:S.number().nonnegative()}),WL=S.enum(["session_init_start","session_init_done","mcp_connect_start","mcp_connect_done","loop_start","loop_end"]),oS=S.object({phase:WL,durationMs:S.number().nonnegative().optional(),metadata:S.record(S.string(),S.union([S.string(),S.number(),S.boolean()])).optional()}),HL=S.object({status:S.enum(["succeeded","failed","cancelled"]),finalCostUsd:S.number().nonnegative(),finalTurnCount:S.number().int().nonnegative(),closedAt:S.string().datetime(),subagentCount:S.number().int().nonnegative().optional(),subagentTokens:S.object({input:S.number().int().nonnegative().optional(),output:S.number().int().nonnegative().optional(),cacheRead:S.number().int().nonnegative().optional(),cacheCreation:S.number().int().nonnegative().optional()}).optional(),subagentCostUsd:S.number().nonnegative().optional()}),sS=S.discriminatedUnion("kind",[S.object({kind:S.literal("tool_call"),payload:Jw}),S.object({kind:S.literal("hook_decision"),payload:Vw}),S.object({kind:S.literal("subagent_lifecycle"),payload:Yw}),S.object({kind:S.literal("background_agent"),payload:Xw}),S.object({kind:S.literal("budget"),payload:Zw}),S.object({kind:S.literal("abort"),payload:Qw}),S.object({kind:S.literal("compaction"),payload:LL}),S.object({kind:S.literal("closure"),payload:tS}),S.object({kind:S.literal("claim"),payload:nS}),S.object({kind:S.literal("browser_event"),payload:rS}),S.object({kind:S.literal("session_phase"),payload:oS})]),iS=S.discriminatedUnion("kind",[S.object({ts:S.string().datetime(),seq:S.number().int().nonnegative(),kind:S.literal("tool_call"),payload:Jw}),S.object({ts:S.string().datetime(),seq:S.number().int().nonnegative(),kind:S.literal("hook_decision"),payload:Vw}),S.object({ts:S.string().datetime(),seq:S.number().int().nonnegative(),kind:S.literal("subagent_lifecycle"),payload:Yw}),S.object({ts:S.string().datetime(),seq:S.number().int().nonnegative(),kind:S.literal("background_agent"),payload:Xw}),S.object({ts:S.string().datetime(),seq:S.number().int().nonnegative(),kind:S.literal("budget"),payload:Zw}),S.object({ts:S.string().datetime(),seq:S.number().int().nonnegative(),kind:S.literal("abort"),payload:Qw}),S.object({ts:S.string().datetime(),seq:S.number().int().nonnegative(),kind:S.literal("compaction"),payload:FL}),S.object({ts:S.string().datetime(),seq:S.number().int().nonnegative(),kind:S.literal("closure"),payload:tS}),S.object({ts:S.string().datetime(),seq:S.number().int().nonnegative(),kind:S.literal("claim"),payload:nS}),S.object({ts:S.string().datetime(),seq:S.number().int().nonnegative(),kind:S.literal("browser_event"),payload:rS}),S.object({ts:S.string().datetime(),seq:S.number().int().nonnegative(),kind:S.literal("session_phase"),payload:oS}),S.object({ts:S.string().datetime(),seq:S.number().int().nonnegative(),kind:S.literal("session_sealed"),payload:HL})]);var Ya=class{traceDir;tracePath;seq=0;sealed=!1;fh=null;writeQueue=Promise.resolve();constructor(t){this.traceDir=t.traceDir,this.tracePath=aS(this.traceDir,"trace.jsonl")}getTracePath(){return this.tracePath}async write(t){if(this.sealed)throw new Error("NdjsonTraceWriter: trace is sealed; write() rejected");return sS.parse(t),this.enqueue(async()=>{await this.ensureOpen();let n=await this.materializePersistedEvent(t);await this.appendLine(n)})}async seal(t){this.sealed||(this.sealed=!0,await this.enqueue(async()=>{await this.ensureOpen();let n={ts:new Date().toISOString(),seq:this.seq++,kind:"session_sealed",payload:t};await this.appendLine(n),this.fh&&await this.fh.sync()}),await this.closeHandle())}async close(){await this.enqueue(async()=>{}),await this.closeHandle()}enqueue(t){let n=this.writeQueue.then(t,t);return this.writeQueue=n.then(()=>{},()=>{}),n}async ensureOpen(){this.fh||(await GL(this.traceDir,{recursive:!0}),this.fh=await qL(this.tracePath,"a"))}async closeHandle(){if(!this.fh)return;let t=this.fh;this.fh=null,await t.close()}async appendLine(t){if(!this.fh)throw new Error("NdjsonTraceWriter: file handle missing");await this.fh.appendFile(`${JSON.stringify(t)}
2000
- `)}async materializePersistedEvent(t){let n=new Date().toISOString(),r=this.seq++;if(t.kind==="compaction"){let o=await this.persistCompactionSidecar(t.payload,r,n);return{ts:n,seq:r,kind:"compaction",payload:o}}return{ts:n,seq:r,kind:t.kind,payload:t.payload}}async persistCompactionSidecar(t,n,r){let o=r.replace(/[:.]/g,"-"),s=aS(this.traceDir,`${String(n).padStart(6,"0")}-${o}-pre-compaction.json`),i=JSON.stringify(t.preCompactionMessages),a=Buffer.byteLength(i,"utf8"),l=KL("sha256").update(i).digest("hex");await zL(s,i,{encoding:"utf8",flag:"w"});let c={path:s,sizeBytes:a,sha256:l};return{trigger:t.trigger,preCompactionMessagesRef:c,summary:t.summary,keptTailCount:t.keptTailCount,keepLastNConfig:t.keepLastNConfig,messagesBefore:t.messagesBefore,messagesAfter:t.messagesAfter,...t.tokensSavedEstimate!==void 0?{tokensSavedEstimate:t.tokensSavedEstimate}:{},...t.summarizationTokens!==void 0?{summarizationTokens:t.summarizationTokens}:{}}}};function vo(e={}){if(T.AFK_TRACE_DISABLED==="1")return null;let t=e.sessionLabel??JL(),n=Li(t),r=new Ya({traceDir:n});return{writer:r,tracePath:r.getTracePath(),sessionLabel:t}}W();import{readFileSync as VL,writeFileSync as YL,existsSync as Xa,mkdirSync as XL,readdirSync as ZL,statSync as QL,realpathSync as lS}from"fs";import{join as uS,basename as dS,resolve as cS,sep as eF}from"path";function Ns(){return Wf(),vn()}function tF(e){return uS(Ns(),`${e}.json`)}function ep(e,{write:t=!1}={}){let n=e.includes("/")?e:tF(e),r,o;if(!t&&Xa(n)?(r=lS(n),o=lS(Ns())):(r=cS(n),o=cS(Ns())),!r.startsWith(o+eF)&&r!==o)throw new Error(`Session path escapes sessions directory: ${e}`);return r}function To(e,t){let n=Ns();Xa(n)||XL(n,{recursive:!0});let r=t??e.sessionId??`session-${Date.now()}`,o={sessionId:e.sessionId,model:e.model,startedAt:e.sessionStartTime,savedAt:Date.now(),totalTurns:e.totalTurns,totalCostUsd:e.totalCostUsd,totalTokens:e.totalTokens,totalDurationMs:e.totalDurationMs,turns:e.turns},s=ep(r,{write:!0});return YL(s,JSON.stringify(o,null,2)),s}function Bs(e){let t;try{t=ep(e)}catch{return}if(Xa(t))try{let n=VL(t,"utf-8");return JSON.parse(n)}catch{return}}function xo(e){let t;try{t=ep(e)}catch{return}let n=Bs(t);if(n)return{path:t,id:dS(t,".json"),data:n};for(let r of Us()){if(r.id!==e&&r.sessionId!==e)continue;let o=Bs(r.path);if(o)return{path:r.path,id:r.id,data:o}}}function Us(){let e=Ns();if(!Xa(e))return[];let t=[];for(let n of ZL(e)){if(!n.endsWith(".json"))continue;let r=uS(e,n);try{if(!QL(r).isFile())continue;let s=Bs(r);if(!s||typeof s.savedAt!="number"||typeof s.model!="string")continue;t.push({path:r,id:dS(n,".json"),sessionId:s.sessionId,model:s.model,startedAt:s.startedAt,savedAt:s.savedAt,totalTurns:s.totalTurns,totalCostUsd:s.totalCostUsd})}catch{}}return t.sort((n,r)=>r.savedAt-n.savedAt),t}function Eo(e){if(e.resume&&e.continue)throw new Error("Use either --resume <id> or --continue, not both.");if(e.resume){let t=xo(e.resume);return t?{id:t.id,resumeId:t.data.sessionId??t.id,stored:t.data}:{id:e.resume,resumeId:e.resume}}if(e.continue){let t=Us()[0];if(!t)throw new Error("No saved sessions found for --continue. Run a session first or use /save.");let n=Bs(t.path);if(!n)throw new Error(`Could not load latest saved session: ${t.id}`);return{id:t.id,resumeId:n.sessionId??t.id,stored:n}}}function Ro(e){return e?{resume:e.resumeId,sessionId:e.resumeId,...e.stored?{resumeHistory:e.stored.turns.map(t=>({user:t.user,assistant:t.assistant}))}:{}}:{}}function Za(e){return{totalTurns:0,totalCostUsd:0,totalTokens:0,totalDurationMs:0,sessionStartTime:Date.now(),turnCosts:[],turnTokens:[],turns:[],model:e,planMode:!1}}function pS(e){e.totalTurns=0,e.totalCostUsd=0,e.totalTokens=0,e.totalDurationMs=0,e.sessionStartTime=Date.now(),e.turnCosts.length=0,e.turnTokens.length=0,e.turns.length=0,delete e.sessionId,delete e.pendingPlanExit}function lr(e,t,n,r,o){let s=r?.totalCostUsd??0,i=r?.durationMs??0,a=Number(r?.usage?.input_tokens??0),l=Number(r?.usage?.output_tokens??0),c=a,u=l,d=Number(r?.usage?.cache_read_input_tokens??0)+Number(r?.usage?.cache_creation_input_tokens??0),m=r?.usage?.iterations;if(Array.isArray(m)&&m.length>0){let g=m[m.length-1];if(g&&typeof g=="object"){let h=g;c=Number(h.input_tokens??0),u=Number(h.output_tokens??0),d=Number(h.cache_read_input_tokens??0)+Number(h.cache_creation_input_tokens??0)}}e.totalTurns+=1,e.totalCostUsd+=s,e.totalDurationMs+=i,e.totalTokens+=a+l,e.turnCosts.push(s),e.turnTokens.push({input:c,output:u,cache:d}),r?.sessionId&&!e.sessionId&&(e.sessionId=String(r.sessionId));let f={user:t,assistant:n,timestamp:Date.now(),costUsd:s,durationMs:i,inputTokens:a,outputTokens:l,...o&&o.length>0?{toolEvents:o}:{}};return e.turns.push(f),f}function rF(e){return/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(e)}var fS=10*1024*1024;function gS(){return new Promise((e,t)=>{let n=[],r=0;if(process.stdin.readableEnded){e("");return}let o=s=>{if(r+=s.length,r>fS){process.stdin.destroy(new Error(`stdin exceeds ${fS}-byte limit`));return}n.push(s)};process.stdin.on("data",o),process.stdin.once("end",()=>{process.stdin.removeListener("data",o),e(Buffer.concat(n).toString("utf-8").replace(/\n+$/,""))}),process.stdin.once("error",s=>{process.stdin.removeListener("data",o),t(s)}),process.stdin.resume()})}function hS(e,t){return new Promise((n,r)=>{let o=e.write(t,s=>{s?r(s):o&&n()});if(!o){let s=()=>{e.removeListener("error",i),n()},i=a=>{e.removeListener("drain",s),r(a)};e.once("drain",s),e.once("error",i)}})}function wS(e){e.command("chat").description("Send a message to the agent").argument("[message]","Message to send; use `-` or omit to read from stdin").option("-m, --model <model>","Model to use. Short aliases: opus|opus_1m|sonnet|sonnet_1m|haiku. Any other value (e.g. `auto` for cursor-api-proxy, or a full `claude-*` ID) passes through to the SDK/proxy untouched.",Xe()).option("-s, --stream","[no-op] reserved; use --format stream-json for headless streaming",!1).option("-f, --format <format>","Output format (text|json|stream-json)","text").option("--max-turns <number>","Maximum conversation turns","10").option("--thinking <mode>","Thinking mode: 'adaptive' | 'disabled' | 'enabled:<N>'","enabled:max").option("--effort <level>","Effort level: low|medium|high|xhigh|max").option("--max-budget-usd <usd>","Hard session cost ceiling in USD. Env: AFK_MAX_BUDGET_USD").option("--task-budget <tokens>","Soft per-task token budget. Env: AFK_TASK_BUDGET").option("--max-output-tokens <n|max>","Per-response output cap ('max' = model ceiling). Env: AFK_MAX_OUTPUT_TOKENS").option("--provider <name>","Provider to use: anthropic|anthropic-direct|openai|openai-compatible. Default: auto-selected by model").option("--dump-prompt [path]",'Dump resolved SDK prompt+options+provenance to file (default: ~/.afk/logs/prompt-dump-<ISO>.json) or "stderr"').option("-w, --worktree [branch]","Create a git worktree for an isolated one-shot. Optional value sets the branch name; otherwise auto-named. On clean exit (no uncommitted changes) the worktree and branch are auto-removed; on dirty exit the worktree is preserved. Mirrors `afk interactive -w`.").option("--resume <id>","Resume a persisted session by id").option("--continue","Continue the most recent persisted session in cwd").option("--session-id <uuid>","Assign a specific UUID to this session (creates new; errors if already exists)").action(async(t,n)=>{if(n.resume&&n.continue){process.stderr.write(`Error: --resume and --continue are mutually exclusive
1999
+ `)}import mn from"chalk";var SL={read:mn.hex("#C9B584"),write:mn.hex("#E8A33D"),shell:mn.hex("#A8E060"),subagent:m.plan,skill:mn.hex("#F08AC4"),dag:mn.hex("#4EC9B0"),mcp:mn.hex("#5FE0C0"),web:mn.hex("#A0C4C0"),browser:mn.hex("#FF8A65"),planning:m.meta,schedule:mn.hex("#D4A84B"),other:m.meta},kL={read:"\u25CF",write:"\u270E",shell:"$",subagent:"\u2192",skill:"\u25C6",dag:"\u2B21",mcp:"\u22A1",web:"\u2316",browser:"\u25C9",planning:"\u25B1",schedule:"\u23F2",other:"\u25CF"};function Qd(e){return{color:SL[e],glyph:kL[e]}}function ko(e){return Qd(Mn(e))}function Ja(e){let{description:t,summary:n,lastToolName:r,totalTokens:o,toolUses:s,durationMs:i}=e,a=[];if(r){let{color:c,glyph:u}=ko(r);a.push(`via ${c(`${u} ${r}`)}`)}s&&a.push(`${s} tool${s===1?"":"s"}`),o&&a.push(`${re(o)} tok`),i&&a.push(ne(i)),a.push("esc to interrupt \xB7 ctrl+b background");let l=a.length>0?` (${a.join(" \xB7 ")})`:"";return n?[m.dim(` \u25E6 ${t}`),m.dim(` ${n}${l}`)]:[m.dim(` \u25E6 ${t}${l}`)]}function Va(e){let t=e.status==="succeeded"?"\u2713":e.status==="failed"?"\u2717":"\u2298",n=e.agentType??e.subagentId,r=[t,n];return e.durationMs!==void 0&&r.push(`\xB7 ${ne(e.durationMs)}`),m.dim(` ${r.join(" ")}`)}G();W();import{randomUUID as JL}from"node:crypto";import{createHash as KL}from"crypto";import{mkdir as GL,open as qL,writeFile as zL}from"fs/promises";import{join as aS}from"path";import{z as S}from"zod";var vL=S.object({phase:S.literal("started"),toolUseId:S.string(),name:S.string(),inputBytes:S.number().int().nonnegative(),subagentId:S.string().optional()}),TL=S.object({phase:S.literal("completed"),toolUseId:S.string(),name:S.string(),resultBytes:S.number().int().nonnegative(),isError:S.boolean(),truncated:S.boolean(),durationMs:S.number().nonnegative(),subagentId:S.string().optional()}),Jw=S.discriminatedUnion("phase",[vL,TL]),xL=S.enum(["PreToolUse","PostToolUse","SessionStart","SessionEnd","SubagentStart","SubagentStop"]),Vw=S.object({hookEvent:xL,decision:S.union([S.literal("block"),S.literal("approve"),S.undefined()]),reason:S.string().optional(),blockedTool:S.string().optional(),injectedContextBytes:S.number().int().nonnegative().optional()}),EL=S.object({transition:S.literal("started"),subagentId:S.string(),parentId:S.string(),model:S.string(),allowedTools:S.array(S.string()).readonly().optional(),systemPromptHash:S.string().optional()}),RL=S.object({transition:S.literal("succeeded"),subagentId:S.string(),durationMs:S.number().nonnegative(),turnCount:S.number().int().nonnegative(),totalCostUsd:S.number().nonnegative().optional(),outputBytes:S.number().int().nonnegative()}),AL=S.object({transition:S.literal("failed"),subagentId:S.string(),errorClass:S.string(),errorMessage:S.string(),partialOutputBytes:S.number().int().nonnegative()}),_L=S.object({transition:S.literal("cancelled"),subagentId:S.string(),source:S.enum(["cascade","explicit"])}),Yw=S.discriminatedUnion("transition",[EL,RL,AL,_L]),CL=S.object({transition:S.literal("started"),jobId:S.string(),subagentId:S.string(),label:S.string(),model:S.string()}),IL=S.object({transition:S.literal("completed"),jobId:S.string(),subagentId:S.string(),durationMs:S.number().nonnegative(),outputBytes:S.number().int().nonnegative()}),PL=S.object({transition:S.literal("failed"),jobId:S.string(),subagentId:S.string(),durationMs:S.number().nonnegative(),errorClass:S.string(),errorMessage:S.string()}),ML=S.object({transition:S.literal("cancelled"),jobId:S.string(),subagentId:S.string(),source:S.enum(["explicit","cascade"])}),OL=S.object({transition:S.literal("joined"),jobId:S.string(),subagentId:S.string(),jobStatus:S.enum(["completed","failed","cancelled"])}),Xw=S.discriminatedUnion("transition",[CL,IL,PL,ML,OL]),Zw=S.object({kind:S.literal("monetary"),runningCostUsd:S.number().nonnegative(),maxBudgetUsd:S.number().nonnegative(),lastTurnCostUsd:S.number().nonnegative()}),$L=S.enum(["user_signal","cascade","timeout","budget","hook_block"]),Qw=S.object({origin:$L,cascadedTo:S.array(S.string()).readonly(),reason:S.string().optional()}),eS=S.enum(["manual","token_threshold","turn_count"]),DL=S.object({path:S.string(),sizeBytes:S.number().int().nonnegative(),sha256:S.string().regex(/^[0-9a-f]{64}$/)}),LL=S.object({trigger:eS,preCompactionMessages:S.array(S.unknown()),summary:S.string(),keptTailCount:S.number().int().nonnegative(),keepLastNConfig:S.number().int().nonnegative(),messagesBefore:S.number().int().nonnegative(),messagesAfter:S.number().int().nonnegative(),tokensSavedEstimate:S.number().nonnegative().optional(),summarizationTokens:S.object({input:S.number().int().nonnegative(),output:S.number().int().nonnegative()}).optional()}),FL=S.object({trigger:eS,preCompactionMessagesRef:DL,summary:S.string(),keptTailCount:S.number().int().nonnegative(),keepLastNConfig:S.number().int().nonnegative(),messagesBefore:S.number().int().nonnegative(),messagesAfter:S.number().int().nonnegative(),tokensSavedEstimate:S.number().nonnegative().optional(),summarizationTokens:S.object({input:S.number().int().nonnegative(),output:S.number().int().nonnegative()}).optional()}),NL=S.enum(["model_end_turn","iteration_cap","abort","timeout","budget_exceeded","hook_blocked","max_turns_exceeded"]),tS=S.object({reason:NL,finalTurnCount:S.number().int().nonnegative(),finalCostUsd:S.number().nonnegative(),finalTokens:S.object({input:S.number().int().nonnegative().optional(),output:S.number().int().nonnegative().optional(),cacheRead:S.number().int().nonnegative().optional(),cacheCreation:S.number().int().nonnegative().optional()}),lastStopReason:S.string().optional()}),nS=S.object({source:S.string(),assertion:S.string(),evidence:S.array(S.string()).readonly(),confidence:S.number().min(0).max(1),dissent:S.string().optional()}),BL=S.enum(["browser_open","browser_observe","browser_act","browser_screenshot","browser_extract","browser_close"]),UL=S.enum(["click","fill","press","select","hover","scroll_to","wait_for"]),jL=S.object({kind:S.enum(["semantic","element_id","selector"]),text:S.string().max(80).optional(),role:S.string().optional(),elementId:S.string().optional(),selectorHash:S.string().regex(/^[0-9a-f]{8}$/).optional()}),rS=S.object({tool:BL,action:UL.optional(),toolUseId:S.string(),target:jL.optional(),urlBefore:S.string().nullable(),urlAfter:S.string().nullable(),status:S.enum(["ok","error","ambiguous_target","blocked_by_policy"]),screenshotPath:S.string().optional(),observationSummary:S.string().max(500).optional(),error:S.object({reason:S.string(),recoverable:S.boolean()}).optional(),durationMs:S.number().nonnegative()}),WL=S.enum(["session_init_start","session_init_done","mcp_connect_start","mcp_connect_done","loop_start","loop_end"]),oS=S.object({phase:WL,durationMs:S.number().nonnegative().optional(),metadata:S.record(S.string(),S.union([S.string(),S.number(),S.boolean()])).optional()}),HL=S.object({status:S.enum(["succeeded","failed","cancelled"]),finalCostUsd:S.number().nonnegative(),finalTurnCount:S.number().int().nonnegative(),closedAt:S.string().datetime(),subagentCount:S.number().int().nonnegative().optional(),subagentTokens:S.object({input:S.number().int().nonnegative().optional(),output:S.number().int().nonnegative().optional(),cacheRead:S.number().int().nonnegative().optional(),cacheCreation:S.number().int().nonnegative().optional()}).optional(),subagentCostUsd:S.number().nonnegative().optional()}),sS=S.discriminatedUnion("kind",[S.object({kind:S.literal("tool_call"),payload:Jw}),S.object({kind:S.literal("hook_decision"),payload:Vw}),S.object({kind:S.literal("subagent_lifecycle"),payload:Yw}),S.object({kind:S.literal("background_agent"),payload:Xw}),S.object({kind:S.literal("budget"),payload:Zw}),S.object({kind:S.literal("abort"),payload:Qw}),S.object({kind:S.literal("compaction"),payload:LL}),S.object({kind:S.literal("closure"),payload:tS}),S.object({kind:S.literal("claim"),payload:nS}),S.object({kind:S.literal("browser_event"),payload:rS}),S.object({kind:S.literal("session_phase"),payload:oS})]),iS=S.discriminatedUnion("kind",[S.object({ts:S.string().datetime(),seq:S.number().int().nonnegative(),kind:S.literal("tool_call"),payload:Jw}),S.object({ts:S.string().datetime(),seq:S.number().int().nonnegative(),kind:S.literal("hook_decision"),payload:Vw}),S.object({ts:S.string().datetime(),seq:S.number().int().nonnegative(),kind:S.literal("subagent_lifecycle"),payload:Yw}),S.object({ts:S.string().datetime(),seq:S.number().int().nonnegative(),kind:S.literal("background_agent"),payload:Xw}),S.object({ts:S.string().datetime(),seq:S.number().int().nonnegative(),kind:S.literal("budget"),payload:Zw}),S.object({ts:S.string().datetime(),seq:S.number().int().nonnegative(),kind:S.literal("abort"),payload:Qw}),S.object({ts:S.string().datetime(),seq:S.number().int().nonnegative(),kind:S.literal("compaction"),payload:FL}),S.object({ts:S.string().datetime(),seq:S.number().int().nonnegative(),kind:S.literal("closure"),payload:tS}),S.object({ts:S.string().datetime(),seq:S.number().int().nonnegative(),kind:S.literal("claim"),payload:nS}),S.object({ts:S.string().datetime(),seq:S.number().int().nonnegative(),kind:S.literal("browser_event"),payload:rS}),S.object({ts:S.string().datetime(),seq:S.number().int().nonnegative(),kind:S.literal("session_phase"),payload:oS}),S.object({ts:S.string().datetime(),seq:S.number().int().nonnegative(),kind:S.literal("session_sealed"),payload:HL})]);var Ya=class{traceDir;tracePath;seq=0;sealed=!1;fh=null;writeQueue=Promise.resolve();constructor(t){this.traceDir=t.traceDir,this.tracePath=aS(this.traceDir,"trace.jsonl")}getTracePath(){return this.tracePath}async write(t){if(this.sealed)throw new Error("NdjsonTraceWriter: trace is sealed; write() rejected");return sS.parse(t),this.enqueue(async()=>{await this.ensureOpen();let n=await this.materializePersistedEvent(t);await this.appendLine(n)})}async seal(t){this.sealed||(this.sealed=!0,await this.enqueue(async()=>{await this.ensureOpen();let n={ts:new Date().toISOString(),seq:this.seq++,kind:"session_sealed",payload:t};await this.appendLine(n),this.fh&&await this.fh.sync()}),await this.closeHandle())}async close(){await this.enqueue(async()=>{}),await this.closeHandle()}enqueue(t){let n=this.writeQueue.then(t,t);return this.writeQueue=n.then(()=>{},()=>{}),n}async ensureOpen(){this.fh||(await GL(this.traceDir,{recursive:!0}),this.fh=await qL(this.tracePath,"a"))}async closeHandle(){if(!this.fh)return;let t=this.fh;this.fh=null,await t.close()}async appendLine(t){if(!this.fh)throw new Error("NdjsonTraceWriter: file handle missing");await this.fh.appendFile(`${JSON.stringify(t)}
2000
+ `)}async materializePersistedEvent(t){let n=new Date().toISOString(),r=this.seq++;if(t.kind==="compaction"){let o=await this.persistCompactionSidecar(t.payload,r,n);return{ts:n,seq:r,kind:"compaction",payload:o}}return{ts:n,seq:r,kind:t.kind,payload:t.payload}}async persistCompactionSidecar(t,n,r){let o=r.replace(/[:.]/g,"-"),s=aS(this.traceDir,`${String(n).padStart(6,"0")}-${o}-pre-compaction.json`),i=JSON.stringify(t.preCompactionMessages),a=Buffer.byteLength(i,"utf8"),l=KL("sha256").update(i).digest("hex");await zL(s,i,{encoding:"utf8",flag:"w"});let c={path:s,sizeBytes:a,sha256:l};return{trigger:t.trigger,preCompactionMessagesRef:c,summary:t.summary,keptTailCount:t.keptTailCount,keepLastNConfig:t.keepLastNConfig,messagesBefore:t.messagesBefore,messagesAfter:t.messagesAfter,...t.tokensSavedEstimate!==void 0?{tokensSavedEstimate:t.tokensSavedEstimate}:{},...t.summarizationTokens!==void 0?{summarizationTokens:t.summarizationTokens}:{}}}};function vo(e={}){if(T.AFK_TRACE_DISABLED==="1")return null;let t=e.sessionLabel??JL(),n=Li(t),r=new Ya({traceDir:n});return{writer:r,tracePath:r.getTracePath(),sessionLabel:t}}W();import{readFileSync as VL,writeFileSync as YL,existsSync as Xa,mkdirSync as XL,readdirSync as ZL,statSync as QL,realpathSync as lS}from"fs";import{join as uS,basename as dS,resolve as cS,sep as eF}from"path";function Ns(){return Wf(),vn()}function tF(e){return uS(Ns(),`${e}.json`)}function ep(e,{write:t=!1}={}){let n=e.includes("/")?e:tF(e),r,o;if(!t&&Xa(n)?(r=lS(n),o=lS(Ns())):(r=cS(n),o=cS(Ns())),!r.startsWith(o+eF)&&r!==o)throw new Error(`Session path escapes sessions directory: ${e}`);return r}function To(e,t){let n=Ns();Xa(n)||XL(n,{recursive:!0});let r=t??e.sessionId??`session-${Date.now()}`,o={sessionId:e.sessionId,model:e.model,startedAt:e.sessionStartTime,savedAt:Date.now(),totalTurns:e.totalTurns,totalCostUsd:e.totalCostUsd,totalTokens:e.totalTokens,totalDurationMs:e.totalDurationMs,turns:e.turns},s=ep(r,{write:!0});return YL(s,JSON.stringify(o,null,2)),s}function Bs(e){let t;try{t=ep(e)}catch{return}if(Xa(t))try{let n=VL(t,"utf-8");return JSON.parse(n)}catch{return}}function xo(e){let t;try{t=ep(e)}catch{return}let n=Bs(t);if(n)return{path:t,id:dS(t,".json"),data:n};for(let r of Us()){if(r.id!==e&&r.sessionId!==e)continue;let o=Bs(r.path);if(o)return{path:r.path,id:r.id,data:o}}}function Us(){let e=Ns();if(!Xa(e))return[];let t=[];for(let n of ZL(e)){if(!n.endsWith(".json"))continue;let r=uS(e,n);try{if(!QL(r).isFile())continue;let s=Bs(r);if(!s||typeof s.savedAt!="number"||typeof s.model!="string")continue;t.push({path:r,id:dS(n,".json"),sessionId:s.sessionId,model:s.model,startedAt:s.startedAt,savedAt:s.savedAt,totalTurns:s.totalTurns,totalCostUsd:s.totalCostUsd})}catch{}}return t.sort((n,r)=>r.savedAt-n.savedAt),t}function Eo(e){if(e.resume&&e.continue)throw new Error("Use either --resume <id> or --continue, not both.");if(e.resume){let t=xo(e.resume);return t?{id:t.id,resumeId:t.data.sessionId??t.id,stored:t.data}:{id:e.resume,resumeId:e.resume}}if(e.continue){let t=Us()[0];if(!t)throw new Error("No saved sessions found for --continue. Run a session first or use /save.");let n=Bs(t.path);if(!n)throw new Error(`Could not load latest saved session: ${t.id}`);return{id:t.id,resumeId:n.sessionId??t.id,stored:n}}}function Ro(e){return e?{resume:e.resumeId,sessionId:e.resumeId,...e.stored?{resumeHistory:e.stored.turns.map(t=>({user:t.user,assistant:t.assistant}))}:{}}:{}}function Za(e){return{totalTurns:0,totalCostUsd:0,totalTokens:0,totalDurationMs:0,sessionStartTime:Date.now(),turnCosts:[],turnTokens:[],turns:[],model:e,planMode:!1}}function pS(e){e.totalTurns=0,e.totalCostUsd=0,e.totalTokens=0,e.totalDurationMs=0,e.sessionStartTime=Date.now(),e.turnCosts.length=0,e.turnTokens.length=0,e.turns.length=0,delete e.sessionId,delete e.pendingPlanExit}function lr(e,t,n,r,o){let s=r?.totalCostUsd??0,i=r?.durationMs??0,a=Number(r?.usage?.input_tokens??0),l=Number(r?.usage?.output_tokens??0),c=a,u=l,d=Number(r?.usage?.cache_read_input_tokens??0)+Number(r?.usage?.cache_creation_input_tokens??0),p=r?.usage?.iterations;if(Array.isArray(p)&&p.length>0){let g=p[p.length-1];if(g&&typeof g=="object"){let h=g;c=Number(h.input_tokens??0),u=Number(h.output_tokens??0),d=Number(h.cache_read_input_tokens??0)+Number(h.cache_creation_input_tokens??0)}}e.totalTurns+=1,e.totalCostUsd+=s,e.totalDurationMs+=i,e.totalTokens+=a+l,e.turnCosts.push(s),e.turnTokens.push({input:c,output:u,cache:d}),r?.sessionId&&!e.sessionId&&(e.sessionId=String(r.sessionId));let f={user:t,assistant:n,timestamp:Date.now(),costUsd:s,durationMs:i,inputTokens:a,outputTokens:l,...o&&o.length>0?{toolEvents:o}:{}};return e.turns.push(f),f}function rF(e){return/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(e)}var fS=10*1024*1024;function gS(){return new Promise((e,t)=>{let n=[],r=0;if(process.stdin.readableEnded){e("");return}let o=s=>{if(r+=s.length,r>fS){process.stdin.destroy(new Error(`stdin exceeds ${fS}-byte limit`));return}n.push(s)};process.stdin.on("data",o),process.stdin.once("end",()=>{process.stdin.removeListener("data",o),e(Buffer.concat(n).toString("utf-8").replace(/\n+$/,""))}),process.stdin.once("error",s=>{process.stdin.removeListener("data",o),t(s)}),process.stdin.resume()})}function hS(e,t){return new Promise((n,r)=>{let o=e.write(t,s=>{s?r(s):o&&n()});if(!o){let s=()=>{e.removeListener("error",i),n()},i=a=>{e.removeListener("drain",s),r(a)};e.once("drain",s),e.once("error",i)}})}function wS(e){e.command("chat").description("Send a message to the agent").argument("[message]","Message to send; use `-` or omit to read from stdin").option("-m, --model <model>","Model to use. Short aliases: opus|opus_1m|sonnet|sonnet_1m|haiku. Any other value (e.g. `auto` for cursor-api-proxy, or a full `claude-*` ID) passes through to the SDK/proxy untouched.",Xe()).option("-s, --stream","[no-op] reserved; use --format stream-json for headless streaming",!1).option("-f, --format <format>","Output format (text|json|stream-json)","text").option("--max-turns <number>","Maximum conversation turns","10").option("--thinking <mode>","Thinking mode: 'adaptive' | 'disabled' | 'enabled:<N>'","enabled:max").option("--effort <level>","Effort level: low|medium|high|xhigh|max").option("--max-budget-usd <usd>","Hard session cost ceiling in USD. Env: AFK_MAX_BUDGET_USD").option("--task-budget <tokens>","Soft per-task token budget. Env: AFK_TASK_BUDGET").option("--max-output-tokens <n|max>","Per-response output cap ('max' = model ceiling). Env: AFK_MAX_OUTPUT_TOKENS").option("--provider <name>","Provider to use: anthropic|anthropic-direct|openai|openai-compatible. Default: auto-selected by model").option("--dump-prompt [path]",'Dump resolved SDK prompt+options+provenance to file (default: ~/.afk/logs/prompt-dump-<ISO>.json) or "stderr"').option("-w, --worktree [branch]","Create a git worktree for an isolated one-shot. Optional value sets the branch name; otherwise auto-named. On clean exit (no uncommitted changes) the worktree and branch are auto-removed; on dirty exit the worktree is preserved. Mirrors `afk interactive -w`.").option("--resume <id>","Resume a persisted session by id").option("--continue","Continue the most recent persisted session in cwd").option("--session-id <uuid>","Assign a specific UUID to this session (creates new; errors if already exists)").action(async(t,n)=>{if(n.resume&&n.continue){process.stderr.write(`Error: --resume and --continue are mutually exclusive
2001
2001
  `),process.exitCode=1;return}if(n.sessionId!==void 0&&(n.resume||n.continue)){process.stderr.write(`Error: --session-id is mutually exclusive with --resume and --continue
2002
2002
  `),process.exitCode=1;return}if(n.sessionId!==void 0&&!rF(n.sessionId)){process.stderr.write(`Error: --session-id must be a UUID (got: ${n.sessionId})
2003
2003
  `),process.exitCode=1;return}if(n.sessionId!==void 0&&xo(n.sessionId)!==void 0){process.stderr.write(`Error: session already exists: ${n.sessionId} \u2014 use --resume to continue it
2004
2004
  `),process.exitCode=1;return}let r,o=!process.stdin.isTTY;if(t==="-"){if(!o){process.stderr.write(`Error: no stdin available \u2014 pass a message or pipe one in
2005
2005
  `),process.exitCode=1;return}r=await gS()}else if(t===void 0&&o)r=await gS();else if(t!==void 0)r=t;else{process.stderr.write(`Error: missing message \u2014 pass a message argument or pipe via stdin
2006
2006
  `),process.exitCode=1;return}if(r.trim()===""){process.stderr.write(`Error: message is empty \u2014 stdin contained only whitespace
2007
- `),process.exitCode=1;return}let s=nF("Initializing agent...").start(),i=null,a,l,c,u=!1,d,m=Za(n.model),f=!1;try{if(n.worktree!==void 0)try{l=await Oa(n.worktree),c=l.path,s.text=`Worktree ready at ${l.path} (branch: ${l.branch})`}catch(te){s.fail("Failed to create worktree"),K(te)}let g,h,b,y,w,x;try{g=Ln(n.thinking)??po(),h=Fn(n.effort)??mo(),b=fo(n.maxBudgetUsd)??Dd(),y=fo(n.taskBudget)??Ld(),w=go(n.maxOutputTokens)??Ds(),x=void 0}catch(te){s.fail("Invalid options"),K(te)}if(n.dumpPrompt!==void 0){let te=n.dumpPrompt===!0?bS.join(yS.homedir(),".afk","logs",`prompt-dump-${new Date().toISOString().replace(/[:.]/g,"-")}.json`):String(n.dumpPrompt);process.env.AFK_DUMP_PROMPT=te,n.provider!==void 0&&n.provider!=="anthropic"&&n.provider!=="anthropic-direct"&&console.error(`[--dump-prompt] WARNING: active provider (${n.provider}) does not support prompt dumping. No file will be written.`)}let R=ue(),v=uo()??co(),E=nt(),I=E.systemPromptSource,D=E.autoRouting?.chat??!1,_=za(v,D,"one-shot"),M={},P=Eo({resume:n.resume,continue:n.continue});if(n.resume&&P&&!P.stored){s.fail("Session not found"),process.stderr.write(`Error: session not found: ${JSON.stringify(n.resume)}
2007
+ `),process.exitCode=1;return}let s=nF("Initializing agent...").start(),i=null,a,l,c,u=!1,d,p=Za(n.model),f=!1;try{if(n.worktree!==void 0)try{l=await Oa(n.worktree),c=l.path,s.text=`Worktree ready at ${l.path} (branch: ${l.branch})`}catch(te){s.fail("Failed to create worktree"),K(te)}let g,h,b,y,w,x;try{g=Ln(n.thinking)??po(),h=Fn(n.effort)??mo(),b=fo(n.maxBudgetUsd)??Dd(),y=fo(n.taskBudget)??Ld(),w=go(n.maxOutputTokens)??Ds(),x=void 0}catch(te){s.fail("Invalid options"),K(te)}if(n.dumpPrompt!==void 0){let te=n.dumpPrompt===!0?bS.join(yS.homedir(),".afk","logs",`prompt-dump-${new Date().toISOString().replace(/[:.]/g,"-")}.json`):String(n.dumpPrompt);process.env.AFK_DUMP_PROMPT=te,n.provider!==void 0&&n.provider!=="anthropic"&&n.provider!=="anthropic-direct"&&console.error(`[--dump-prompt] WARNING: active provider (${n.provider}) does not support prompt dumping. No file will be written.`)}let R=ue(),v=uo()??co(),E=nt(),I=E.systemPromptSource,D=E.autoRouting?.chat??!1,_=za(v,D,"one-shot"),M={},P=Eo({resume:n.resume,continue:n.continue});if(n.resume&&P&&!P.stored){s.fail("Session not found"),process.stderr.write(`Error: session not found: ${JSON.stringify(n.resume)}
2008
2008
  Run \`afk i\` then \`/resume\` to list saved sessions.
2009
- `),process.exitCode=1;return}P&&(M=Ro(P),u=!0,d=P.id),n.sessionId!==void 0&&(M={sessionId:n.sessionId},u=!0,d=n.sessionId);let $=P?.stored?.model??n.model;m.model=$,P?.stored&&(m.totalTurns=P.stored.totalTurns,m.totalCostUsd=P.stored.totalCostUsd,m.totalTokens=P.stored.totalTokens,m.totalDurationMs=P.stored.totalDurationMs,m.turns=[...P.stored.turns],m.sessionId=P.stored.sessionId??P.resumeId,m.sessionStartTime=P.stored.startedAt??Date.now()),n.sessionId!==void 0&&(m.sessionId=n.sessionId);let F,k=vo(),A=new X({apiKey:R,...E.baseUrl!==void 0?{baseUrl:E.baseUrl}:{},...c!==void 0?{cwd:c}:{}}),N=ro(E.openaiBaseUrl!==void 0?{openaiBaseUrl:E.openaiBaseUrl}:{}),H={get sessionId(){return F?.sessionId},getInputStreamRef(){return F?.getInputStreamRef?.()??{pushUserMessage:()=>{}}},get abortSignal(){return F?.abortSignal??new AbortController().signal},get hookRegistry(){return F?.hookRegistry}},C=oo(n.model,R,N,E.baseUrl,k?.writer,void 0,c),B=new Wt({subagentManager:A,parentSession:H,defaultConfig:{apiKey:R,systemPrompt:v,...E.baseUrl!==void 0?{baseUrl:E.baseUrl}:{}},defaultSubagentModel:wt(n.model),childProviderFactory:N,childSkillExecutorFactory:C,depth:0,...c!==void 0?{cwd:c}:{}}),j=new Ht({parentSession:H,defaultModel:n.model,defaultSubagentModel:wt(n.model),apiKey:R,childProviderFactory:N,childSkillExecutorFactory:C,...E.baseUrl!==void 0?{baseUrl:E.baseUrl}:{},...k?.writer!==void 0?{traceWriter:k.writer}:{},...c!==void 0?{cwd:c}:{}}),ee=new Nn({parentSession:H,defaultModel:n.model,defaultSubagentModel:wt(n.model),apiKey:R,...E.baseUrl!==void 0?{baseUrl:E.baseUrl}:{},systemPrompt:v??""});if(a=new We,x=ho(n.provider,{subagentExecutor:B,skillExecutor:j,composeExecutor:ee,memoryStore:a,model:String(n.model),...E.openaiBaseUrl!==void 0?{openaiBaseUrl:E.openaiBaseUrl}:{}})??new Fe({permissions:{allowedTools:[...Ft,...tn,...st,"agent","skill","compose"]},subagentExecutor:B,skillExecutor:j,composeExecutor:ee,memoryStore:a,surface:"cli"}),i=new Ve(sn({model:$,apiKey:R,maxTurns:parseInt(n.maxTurns,10),hookRegistry:So(te=>{console.log(Va(te))},"cli",a).registry,..._!==void 0?{systemPrompt:_}:{},...I!==void 0?{systemPromptSource:I}:{},...g!==void 0?{thinking:g}:{},...h!==void 0?{effort:h}:{},...b!==void 0?{maxBudgetUsd:b}:{},...y!==void 0?{taskBudget:y}:{},...w!==void 0?{maxOutputTokens:w}:{},...E.baseUrl!==void 0?{baseUrl:E.baseUrl}:{},...k?{traceWriter:k.writer}:{},...E.autoResumeOnUsageLimit!==void 0?{autoResumeOnUsageLimit:E.autoResumeOnUsageLimit}:{},...c!==void 0?{cwd:c}:{},...M,provider:x})),F=i,s.text="Sending message...",n.format==="stream-json"){let te=(Me,ut)=>ut instanceof Date?ut.toISOString():ut instanceof Error?{message:ut.message,name:ut.name}:ut;s.stop();let Re="",De=i.sendMessageStream(r);for await(let Me of De)if(await hS(process.stdout,JSON.stringify(Me,te)+`
2010
- `),Me.type==="chunk"&&Me.chunk.type==="content"&&(Re+=Me.chunk.content),Me.type==="done"&&(lr(m,r,Re,Me.metadata),Me.metadata?.sessionId&&!m.sessionId&&(m.sessionId=String(Me.metadata.sessionId))),Me.type==="error"){process.exitCode=1;break}return}let q=await i.sendMessage(r,{stream:n.stream});s.succeed("Response received");let L=i.getLastResponseMetadata();if(lr(m,r,q.content,L??void 0),L?.sessionId&&!m.sessionId&&(m.sessionId=String(L.sessionId)),n.format==="json"){let te=L?Number(L.usage?.input_tokens??0):0,Re=L?Number(L.usage?.output_tokens??0):0;console.log(JSON.stringify({success:!0,model:$,message:q.content,timestamp:q.timestamp,...L?.totalCostUsd!==void 0?{costUsd:L.totalCostUsd}:{},...L?.durationMs!==void 0?{durationMs:L.durationMs}:{},...te>0?{inputTokens:te}:{},...Re>0?{outputTokens:Re}:{}},null,2))}else{if(console.log(mS.cyan(`
2009
+ `),process.exitCode=1;return}P&&(M=Ro(P),u=!0,d=P.id),n.sessionId!==void 0&&(M={sessionId:n.sessionId},u=!0,d=n.sessionId);let $=P?.stored?.model??n.model;p.model=$,P?.stored&&(p.totalTurns=P.stored.totalTurns,p.totalCostUsd=P.stored.totalCostUsd,p.totalTokens=P.stored.totalTokens,p.totalDurationMs=P.stored.totalDurationMs,p.turns=[...P.stored.turns],p.sessionId=P.stored.sessionId??P.resumeId,p.sessionStartTime=P.stored.startedAt??Date.now()),n.sessionId!==void 0&&(p.sessionId=n.sessionId);let F,k=vo(),A=new X({apiKey:R,...E.baseUrl!==void 0?{baseUrl:E.baseUrl}:{},...c!==void 0?{cwd:c}:{}}),N=ro(E.openaiBaseUrl!==void 0?{openaiBaseUrl:E.openaiBaseUrl}:{}),H={get sessionId(){return F?.sessionId},getInputStreamRef(){return F?.getInputStreamRef?.()??{pushUserMessage:()=>{}}},get abortSignal(){return F?.abortSignal??new AbortController().signal},get hookRegistry(){return F?.hookRegistry}},C=oo(n.model,R,N,E.baseUrl,k?.writer,void 0,c),B=new Wt({subagentManager:A,parentSession:H,defaultConfig:{apiKey:R,systemPrompt:v,...E.baseUrl!==void 0?{baseUrl:E.baseUrl}:{}},defaultSubagentModel:wt(n.model),childProviderFactory:N,childSkillExecutorFactory:C,depth:0,...c!==void 0?{cwd:c}:{}}),j=new Ht({parentSession:H,defaultModel:n.model,defaultSubagentModel:wt(n.model),apiKey:R,childProviderFactory:N,childSkillExecutorFactory:C,...E.baseUrl!==void 0?{baseUrl:E.baseUrl}:{},...k?.writer!==void 0?{traceWriter:k.writer}:{},...c!==void 0?{cwd:c}:{}}),ee=new Nn({parentSession:H,defaultModel:n.model,defaultSubagentModel:wt(n.model),apiKey:R,...E.baseUrl!==void 0?{baseUrl:E.baseUrl}:{},systemPrompt:v??""});if(a=new We,x=ho(n.provider,{subagentExecutor:B,skillExecutor:j,composeExecutor:ee,memoryStore:a,model:String(n.model),...E.openaiBaseUrl!==void 0?{openaiBaseUrl:E.openaiBaseUrl}:{}})??new Fe({permissions:{allowedTools:[...Ft,...tn,...st,"agent","skill","compose"]},subagentExecutor:B,skillExecutor:j,composeExecutor:ee,memoryStore:a,surface:"cli"}),i=new Ve(sn({model:$,apiKey:R,maxTurns:parseInt(n.maxTurns,10),hookRegistry:So(te=>{console.log(Va(te))},"cli",a).registry,..._!==void 0?{systemPrompt:_}:{},...I!==void 0?{systemPromptSource:I}:{},...g!==void 0?{thinking:g}:{},...h!==void 0?{effort:h}:{},...b!==void 0?{maxBudgetUsd:b}:{},...y!==void 0?{taskBudget:y}:{},...w!==void 0?{maxOutputTokens:w}:{},...E.baseUrl!==void 0?{baseUrl:E.baseUrl}:{},...k?{traceWriter:k.writer}:{},...E.autoResumeOnUsageLimit!==void 0?{autoResumeOnUsageLimit:E.autoResumeOnUsageLimit}:{},...c!==void 0?{cwd:c}:{},...M,provider:x})),F=i,s.text="Sending message...",n.format==="stream-json"){let te=(Me,ut)=>ut instanceof Date?ut.toISOString():ut instanceof Error?{message:ut.message,name:ut.name}:ut;s.stop();let Re="",De=i.sendMessageStream(r);for await(let Me of De)if(await hS(process.stdout,JSON.stringify(Me,te)+`
2010
+ `),Me.type==="chunk"&&Me.chunk.type==="content"&&(Re+=Me.chunk.content),Me.type==="done"&&(lr(p,r,Re,Me.metadata),Me.metadata?.sessionId&&!p.sessionId&&(p.sessionId=String(Me.metadata.sessionId))),Me.type==="error"){process.exitCode=1;break}return}let q=await i.sendMessage(r,{stream:n.stream});s.succeed("Response received");let L=i.getLastResponseMetadata();if(lr(p,r,q.content,L??void 0),L?.sessionId&&!p.sessionId&&(p.sessionId=String(L.sessionId)),n.format==="json"){let te=L?Number(L.usage?.input_tokens??0):0,Re=L?Number(L.usage?.output_tokens??0):0;console.log(JSON.stringify({success:!0,model:$,message:q.content,timestamp:q.timestamp,...L?.totalCostUsd!==void 0?{costUsd:L.totalCostUsd}:{},...L?.durationMs!==void 0?{durationMs:L.durationMs}:{},...te>0?{inputTokens:te}:{},...Re>0?{outputTokens:Re}:{}},null,2))}else{if(console.log(mS.cyan(`
2011
2011
  \u{1F916} Claude:`)),console.log(xt(q.content)),L){let te=[];L.durationMs&&te.push(ne(L.durationMs)),L.totalCostUsd!==void 0&&te.push(Ne(L.totalCostUsd));let Re=Number(L.usage?.input_tokens??0),De=Number(L.usage?.output_tokens??0);Re+De>0&&te.push(re(Re+De)+" tokens"),te.length>0&&console.log(mS.dim(" \xB7 "+te.join(" \xB7 ")))}console.log("")}}catch(g){if(f=!0,n.format==="stream-json"){let h=g instanceof Error?g:new Error(String(g));try{await hS(process.stdout,JSON.stringify({type:"error",error:{message:h.message,name:h.name}})+`
2012
- `)}catch{}process.exitCode=1}s.fail("Failed to send message"),K(g)}finally{if(u&&m.totalTurns>0&&!f)try{let h=To(m,d).replace(/\.json$/,"").split("/").pop()??d??m.sessionId??"unknown";process.stderr.write(`Continue with: afk chat <msg> --resume ${h}
2013
- `)}catch{}i&&await i.close(),a?.close(),l!==void 0&&await l.cleanup()}})}G();import Nx from"ora";import*as Bx from"node:os";import*as Ar from"node:path";import{execFileSync as lH}from"node:child_process";var Qa=new Set;function cr(e){return Qa.add(e),()=>{Qa.delete(e)}}async function el(){await Promise.all([...Qa].map(e=>e())),Qa.clear()}var oF=/^[A-Za-z0-9_@%+=:,./-]+$/;function SS(e){return oF.test(e)?e:`'${e.replace(/'/g,"'\\''")}'`}function Ao(e,t){let n=["afk","interactive"];return typeof t=="string"&&t.length>0&&n.push("--model",SS(t)),n.push("--resume",SS(e)),n.join(" ")}import{homedir as sF}from"node:os";import{sep as Gt}from"node:path";function js(e,t={}){if(!e)return"";let n=t.homedir??sF(),r=iF(e,n),o=t.maxWidth;if(o===void 0||o<=0||z(r)<=o)return r;let s=r.split(Gt).filter(d=>d.length>0);if(s.length<=1)return ae(r,o);let a=r.startsWith("~")?"~":s[0],l=1,c=s[s.length-1],u=[];for(let d=s.length-2;d>=l;d--){let m=s.slice(l,d+1),f=m.length>0?m.join(Gt)+Gt:"";u.push(`${a}${Gt}${f}\u2026${Gt}${c}`)}u.push(`${a}${Gt}\u2026${Gt}${c}`);for(let d of u)if(z(d)<=o)return d;return ae(u[u.length-1],o)}function iF(e,t){if(!t)return e;if(e===t)return"~";let n=t.endsWith(Gt)?t:t+Gt;return e.startsWith(n)?"~"+Gt+e.slice(n.length):e}import*as ZT from"node:readline";import{emitKeypressEvents as aF}from"readline";var lF=50;function ur(e){aF(e,{escapeCodeTimeout:lF})}var cF=/\x1B\][^\x07\x1B]*(?:\x07|\x1B\\)|\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])|\x9B[0-?]*[ -/]*[@-~]|[\x80-\x9F]/g;function le(e,t=128){let n=e.replace(cF,"");return n.length>t?n.slice(0,t)+"\u2026":n}function kS(e=process.env){return e.AFK_DEMO_CLEAN==="1"||typeof e.SCRIPT=="string"&&e.SCRIPT.length>0||e.ASCIINEMA_REC==="1"}function uF(e=process.env){return e.AFK_BELL==="1"}function vS(e=process.env){return e.AFK_REDUCED_MOTION==="1"}function tl(e,t=process.env){uF(t)&&e.isTTY&&e.write("\x07")}var np=64,tp=256,nl=20,rp=new Set(["__proto__","constructor","prototype"]);async function dF(e,t){if(!process.stdout.isTTY||!process.stdin.isTTY||e.length===0)return null;let n=10,r=0,o=0;function s(){let u=o,d=Math.min(u+n,e.length);return{start:u,end:d}}function i(){let{start:u,end:d}=s(),m=[];for(let f=u;f<d;f++){let g=le(e[f],80);f===r?m.push(` ${p.bold("\u25B6 "+g)}`):m.push(` ${p.dim(g)}`)}if(e.length>n){let{end:f}=s();m.push(p.dim(` (${o+1}\u2013${f} of ${e.length} \u2191/\u2193 to scroll)`))}else m.push(p.dim(" \u2191/\u2193 navigate Enter select Esc cancel"));return m}let a=i();process.stdout.write(a.join(`
2012
+ `)}catch{}process.exitCode=1}s.fail("Failed to send message"),K(g)}finally{if(u&&p.totalTurns>0&&!f)try{let h=To(p,d).replace(/\.json$/,"").split("/").pop()??d??p.sessionId??"unknown";process.stderr.write(`Continue with: afk chat <msg> --resume ${h}
2013
+ `)}catch{}i&&await i.close(),a?.close(),l!==void 0&&await l.cleanup()}})}G();import Nx from"ora";import*as Bx from"node:os";import*as Ar from"node:path";import{execFileSync as lH}from"node:child_process";var Qa=new Set;function cr(e){return Qa.add(e),()=>{Qa.delete(e)}}async function el(){await Promise.all([...Qa].map(e=>e())),Qa.clear()}var oF=/^[A-Za-z0-9_@%+=:,./-]+$/;function SS(e){return oF.test(e)?e:`'${e.replace(/'/g,"'\\''")}'`}function Ao(e,t){let n=["afk","interactive"];return typeof t=="string"&&t.length>0&&n.push("--model",SS(t)),n.push("--resume",SS(e)),n.join(" ")}import{homedir as sF}from"node:os";import{sep as Gt}from"node:path";function js(e,t={}){if(!e)return"";let n=t.homedir??sF(),r=iF(e,n),o=t.maxWidth;if(o===void 0||o<=0||z(r)<=o)return r;let s=r.split(Gt).filter(d=>d.length>0);if(s.length<=1)return ae(r,o);let a=r.startsWith("~")?"~":s[0],l=1,c=s[s.length-1],u=[];for(let d=s.length-2;d>=l;d--){let p=s.slice(l,d+1),f=p.length>0?p.join(Gt)+Gt:"";u.push(`${a}${Gt}${f}\u2026${Gt}${c}`)}u.push(`${a}${Gt}\u2026${Gt}${c}`);for(let d of u)if(z(d)<=o)return d;return ae(u[u.length-1],o)}function iF(e,t){if(!t)return e;if(e===t)return"~";let n=t.endsWith(Gt)?t:t+Gt;return e.startsWith(n)?"~"+Gt+e.slice(n.length):e}import*as ZT from"node:readline";import{emitKeypressEvents as aF}from"readline";var lF=50;function ur(e){aF(e,{escapeCodeTimeout:lF})}var cF=/\x1B\][^\x07\x1B]*(?:\x07|\x1B\\)|\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])|\x9B[0-?]*[ -/]*[@-~]|[\x80-\x9F]/g;function le(e,t=128){let n=e.replace(cF,"");return n.length>t?n.slice(0,t)+"\u2026":n}function kS(e=process.env){return e.AFK_DEMO_CLEAN==="1"||typeof e.SCRIPT=="string"&&e.SCRIPT.length>0||e.ASCIINEMA_REC==="1"}function uF(e=process.env){return e.AFK_BELL==="1"}function vS(e=process.env){return e.AFK_REDUCED_MOTION==="1"}function tl(e,t=process.env){uF(t)&&e.isTTY&&e.write("\x07")}var np=64,tp=256,nl=20,rp=new Set(["__proto__","constructor","prototype"]);async function dF(e,t){if(!process.stdout.isTTY||!process.stdin.isTTY||e.length===0)return null;let n=10,r=0,o=0;function s(){let u=o,d=Math.min(u+n,e.length);return{start:u,end:d}}function i(){let{start:u,end:d}=s(),p=[];for(let f=u;f<d;f++){let g=le(e[f],80);f===r?p.push(` ${m.bold("\u25B6 "+g)}`):p.push(` ${m.dim(g)}`)}if(e.length>n){let{end:f}=s();p.push(m.dim(` (${o+1}\u2013${f} of ${e.length} \u2191/\u2193 to scroll)`))}else p.push(m.dim(" \u2191/\u2193 navigate Enter select Esc cancel"));return p}let a=i();process.stdout.write(a.join(`
2014
2014
  `)+`
2015
2015
  `);let l=a.length;function c(){let u=i();process.stdout.write(`\x1B[${l}A\x1B[0J`+u.join(`
2016
2016
  `)+`
2017
- `),l=u.length}return new Promise(u=>{process.stdin.setRawMode(!0),ur(process.stdin);let d=(f,g)=>{if(g)if(g.name==="up"||g.sequence==="\x1B[A")r>0&&(r--,r<o&&(o=r),c());else if(g.name==="down"||g.sequence==="\x1B[B"){if(r<e.length-1){r++;let{end:h}=s();r>=h&&o++,c()}}else g.name==="return"||g.name==="enter"?(m(),u(r)):(g.name==="escape"||g.ctrl&&g.name==="c")&&(m(),u(":cancel"))};function m(){process.stdin.removeListener("keypress",d);try{process.stdin.setRawMode(!1)}catch{}}process.stdin.on("keypress",d)})}async function pF(e,t){if(!process.stdout.isTTY||!process.stdin.isTTY||e.length===0)return null;let n=10,r=0,o=0,s=new Set;function i(){let d=o,m=Math.min(d+n,e.length);return{start:d,end:m}}function a(){let{start:d,end:m}=i(),f=[];for(let g=d;g<m;g++){let h=le(e[g],80),b=s.has(g)?p.bold("\u2713"):p.dim("\u25CB"),y=g===r?p.bold("\u25B6"):" ";f.push(` ${y} ${b} ${g===r?p.bold(h):p.dim(h)}`)}return e.length>n&&f.push(p.dim(` (${o+1}\u2013${m} of ${e.length} \u2191/\u2193 scroll)`)),f.push(p.dim(" \u2191/\u2193 navigate Space toggle Enter confirm Esc cancel")),f}let l=a();process.stdout.write(l.join(`
2017
+ `),l=u.length}return new Promise(u=>{process.stdin.setRawMode(!0),ur(process.stdin);let d=(f,g)=>{if(g)if(g.name==="up"||g.sequence==="\x1B[A")r>0&&(r--,r<o&&(o=r),c());else if(g.name==="down"||g.sequence==="\x1B[B"){if(r<e.length-1){r++;let{end:h}=s();r>=h&&o++,c()}}else g.name==="return"||g.name==="enter"?(p(),u(r)):(g.name==="escape"||g.ctrl&&g.name==="c")&&(p(),u(":cancel"))};function p(){process.stdin.removeListener("keypress",d);try{process.stdin.setRawMode(!1)}catch{}}process.stdin.on("keypress",d)})}async function pF(e,t){if(!process.stdout.isTTY||!process.stdin.isTTY||e.length===0)return null;let n=10,r=0,o=0,s=new Set;function i(){let d=o,p=Math.min(d+n,e.length);return{start:d,end:p}}function a(){let{start:d,end:p}=i(),f=[];for(let g=d;g<p;g++){let h=le(e[g],80),b=s.has(g)?m.bold("\u2713"):m.dim("\u25CB"),y=g===r?m.bold("\u25B6"):" ";f.push(` ${y} ${b} ${g===r?m.bold(h):m.dim(h)}`)}return e.length>n&&f.push(m.dim(` (${o+1}\u2013${p} of ${e.length} \u2191/\u2193 scroll)`)),f.push(m.dim(" \u2191/\u2193 navigate Space toggle Enter confirm Esc cancel")),f}let l=a();process.stdout.write(l.join(`
2018
2018
  `)+`
2019
2019
  `);let c=l.length;function u(){let d=a();process.stdout.write(`\x1B[${c}A\x1B[0J`+d.join(`
2020
2020
  `)+`
2021
- `),c=d.length}return new Promise(d=>{process.stdin.setRawMode(!0),ur(process.stdin);let m=(g,h)=>{if(h)if(h.name==="up"||h.sequence==="\x1B[A")r>0&&(r--,r<o&&(o=r),u());else if(h.name==="down"||h.sequence==="\x1B[B"){if(r<e.length-1){r++;let{end:b}=i();r>=b&&o++,u()}}else h.name==="space"?(s.has(r)?s.delete(r):s.add(r),u()):h.name==="return"||h.name==="enter"?(f(),d([...s].sort((b,y)=>b-y))):(h.name==="escape"||h.ctrl&&h.name==="c")&&(f(),d(":cancel"))};function f(){process.stdin.removeListener("keypress",m);try{process.stdin.setRawMode(!1)}catch{}}process.stdin.on("keypress",m)})}var _o={action:"decline"},se={action:"cancel"},mF={action:"accept"};function fF(e){let t=e.properties,n={},r=0,o=!1;if(typeof t=="object"&&t!==null){let a=0;for(let[l,c]of Object.entries(t))if(!rp.has(l)){if(r+=1,a>=np){o=!0;continue}n[l]=c,a+=1}}let s=e.required,i=new Set(Array.isArray(s)?s.slice(0,np*2).filter(a=>typeof a=="string").filter(a=>!rp.has(a)):[]);return{properties:n,required:i,fieldsTruncated:o,originalFieldCount:r}}function gF(e,t){e.line(),e.line(p.warning("\u26A0 MCP form elicitation")),e.line(p.dim(" server: ")+p.bold(le(t.serverName,64))),e.line(p.dim(" message: ")+le(t.message,256)),t.elicitationId&&e.line(p.dim(" id: ")+le(t.elicitationId,64)),e.line(p.dim(" Type :decline or :cancel at any prompt to exit.")),e.line()}async function hF(e,t,n,r,o,s){if(s.aborted)return{tag:"cancel"};let i=le(t.description??t.title??e),a=le(t.type??"string",32),l=le(e,64),c;if(t.enum!==void 0){let d=t.enum.slice(0,nl).map(f=>le(String(f),32)).join("|"),m=t.enum.length>nl?"|\u2026":"";c=` (enum: ${d}${m})`}else a==="boolean"?c=" (boolean: y/n)":a==="number"||a==="integer"?c=` (${a})`:a==="string"?c=" (string)":(c=` (${a} \u2014 treated as string)`,o.line(p.warning(` \u26A0 Unknown field type '${a}' for '${l}' \u2014 collecting as string.`)));let u=n?"":p.dim(" [optional, enter to skip]");for(o.line(p.dim(` [${l}]`)+p.dim(` ${i}`)+p.dim(c)+u),t.enum!==void 0&&t.enum.length>tp&&o.line(p.warning(` \u26A0 Field '${l}' has ${t.enum.length} enum values; only the first ${tp} are valid for input.`));;){let d;try{d=await r(p.dim(" > "))}catch{return{tag:"cancel"}}if(s.aborted)return{tag:"cancel"};let m=d.trim();if(m===":cancel")return{tag:"cancel"};if(m===":decline")return{tag:"decline"};if(m===""){if(n){o.line(p.warning(" (required \u2014 cannot be skipped)"));continue}return{tag:"value",value:t.default}}let f;if(a==="boolean"){let g=m.toLowerCase();if(g==="y"||g==="yes"||g==="true"||g==="1")f=!0;else if(g==="n"||g==="no"||g==="false"||g==="0")f=!1;else{o.line(p.warning(" Invalid boolean \u2014 enter y/yes/true/1 or n/no/false/0."));continue}}else if(a==="number"){let g=Number(m);if(!isFinite(g)){o.line(p.warning(" Invalid number \u2014 enter a numeric value."));continue}f=g}else if(a==="integer"){let g=parseInt(m,10);if(!isFinite(g)||String(g)!==m.replace(/\.0+$/,"")){o.line(p.warning(" Invalid integer \u2014 enter a whole number."));continue}f=g}else f=m;if(t.enum!==void 0){let g=t.enum.slice(0,tp),h=!1;for(let b of g)if(String(b)===String(f)){h=!0;break}if(!h){let b=le(String(f),64),y=g.slice(0,nl).map(x=>le(String(x),32)).join(", "),w=g.length>nl?", \u2026":"";o.line(p.warning(` '${b}' is not a valid choice. Valid: ${y}${w}`));continue}}return{tag:"value",value:f}}}function yF(e,t){e.line(),e.line(p.warning("\u26A0 MCP elicitation")),e.line(p.dim(" server: ")+p.bold(le(t.serverName,64))),e.line(p.dim(" message: ")+le(t.message,256)),t.url&&e.line(p.dim(" url: ")+p.brand(le(t.url,512))),t.elicitationId&&e.line(p.dim(" id: ")+le(t.elicitationId,64)),e.line()}var dr={action:"skip"};function op(e){let t=[];return t.push(p.warning(" \u{1F4AC} Agent question")),t.push(p.bold(" ? "+le(e.message,512))),e.context&&t.push(p.dim(" "+le(e.context,512))),t.push(""),t}async function bF(e,t,n,r){let o=op(t),s=t.allowSkip===!0;if(e==="number"){let u=t.min,d=t.max,f=`enter to submit \xB7 esc to cancel${u!==void 0&&d!==void 0?` [${u}\u2013${d}]`:u!==void 0?` [\u2265${u}]`:d!==void 0?` [\u2264${d}]`:""}`,h=await n({header:o,help:f,validate:y=>{let w=y.trim();if(w==="")return s?null:"Please enter a number (or esc to cancel).";let x=Number(w);return Number.isFinite(x)?u!==void 0&&x<u?`Value must be \u2265 ${u}.`:d!==void 0&&x>d?`Value must be \u2264 ${d}.`:null:"Please enter a valid number."},signal:r});if(h===null)return null;let b=h.trim();return b===""&&s?{tag:"skip"}:{tag:"number",value:Number(b)}}let i=t.minLength,a=t.maxLength,c=await n({header:o,validate:u=>u===""?s?null:"Please enter a response (or esc to cancel).":i!==void 0&&u.length<i?`Response must be at least ${i} characters.`:a!==void 0&&u.length>a?`Response must be at most ${a} characters.`:null,signal:r});return c===null?null:c===""&&s?{tag:"skip"}:{tag:"text",value:c}}async function wF(e,t,n){if(n.aborted)return _o;let{readLine:r,writer:o,pendingCount:s,pickFromList:i,readTextOverlay:a}=t,l=e.type??"text",c=s();if(c>1&&o.line(p.dim(` [${c} questions queued]`)),(l==="choice"||l==="multi_choice")&&i&&(e.choices?.length??0)>0){let m=(e.choices??[]).map(h=>le(h,128)),f;try{f=await i({header:op(e),options:m,multi:l==="multi_choice",signal:n})}catch{return se}if(n.aborted||f===null)return se;if(f.length===0)return e.allowSkip?dr:se;let g=f.length===1?le(f[0]??"",128):f.map(h=>le(h,128)).join(", ");return o.line(p.dim(" \u2713 ")+p.brand(g)),l==="choice"?{action:"accept",content:{value:f[0]}}:{action:"accept",content:{value:[...f]}}}if(l==="confirm"&&i){let f=e.questionDefault!==!1?["Yes","No"]:["No","Yes"],g;try{g=await i({header:op(e),options:f,multi:!1,signal:n})}catch{return se}if(n.aborted||g===null)return se;let h=g[0];if(h===void 0)return se;let b=h==="Yes";return o.line(p.dim(" \u2713 ")+(b?p.success("Yes"):p.error("No"))),{action:"accept",content:{value:b}}}if((l==="text"||l==="number")&&a){let m=await bF(l,e,a,n);if(m===null)return se;if(m.tag==="skip")return dr;let f=m.tag==="text"?le(m.value,256):String(m.value);return o.line(p.dim(" \u2713 ")+p.brand(f)),{action:"accept",content:{value:(m.tag==="text",m.value)}}}if(o.line(),o.line(p.warning("\u{1F4AC} Agent question")),e.context&&o.line(p.dim(" context: ")+le(e.context,512)),o.line(p.bold(" "+le(e.message,512))),o.line(p.dim(" Type :cancel to skip this question.")),o.line(),l==="confirm"){o.line("\x07");let m=e.questionDefault===!0?"Y/n":"y/N";for(;;){if(n.aborted)return se;let f;try{f=(await r(p.dim(` Continue? [${m}] `))).trim().toLowerCase()}catch{return se}if(n.aborted||f===":cancel")return se;if(f==="")return{action:"accept",content:{value:e.questionDefault===!0}};if(f==="y"||f==="yes")return{action:"accept",content:{value:!0}};if(f==="n"||f==="no")return{action:"accept",content:{value:!1}};o.line(p.warning(" Please enter y or n."))}}if(l==="choice"){o.line("\x07");let m=e.choices??[],f=await dF(m,n);if(f!==null){if(f===":cancel")return se;let g=m[f];return g!==void 0?(o.line(p.dim(` Selected: ${le(g,128)}`)),{action:"accept",content:{value:g}}):se}for(m.forEach((g,h)=>{o.line(` ${h+1}. ${le(g,128)}`)});;){if(n.aborted)return se;let g;try{g=(await r(p.dim(" Enter number: "))).trim()}catch{return se}if(n.aborted||g===":cancel")return se;if(g===""&&e.allowSkip)return dr;let h=parseInt(g,10);if(!isFinite(h)||String(h)!==g||h<1||h>m.length){o.line(p.warning(` Please enter a number between 1 and ${m.length}.`));continue}return{action:"accept",content:{value:m[h-1]}}}}if(l==="multi_choice"){let m=e.choices??[],f=await pF(m,n);if(f!==null){if(f===":cancel")return se;if(f.length===0&&e.allowSkip)return dr;if(f.length>0){let g=f.map(h=>m[h]);return o.line(p.dim(` Selected: ${g.map(h=>le(h,64)).join(", ")}`)),{action:"accept",content:{value:g}}}}for(m.forEach((g,h)=>{o.line(` ${h+1}. ${le(g,128)}`)});;){if(n.aborted)return se;let g;try{g=(await r(p.dim(" Enter numbers (comma-separated): "))).trim()}catch{return se}if(n.aborted||g===":cancel")return se;if(g===""&&e.allowSkip)return dr;if(g===""){o.line(p.warning(" Please enter at least one selection."));continue}let h=g.split(",").map(w=>w.trim()),b=[],y=!0;for(let w of h){let x=parseInt(w,10);if(!isFinite(x)||String(x)!==w||x<1||x>m.length){o.line(p.warning(` Invalid selection "${le(w,32)}". Enter numbers between 1 and ${m.length}.`)),y=!1;break}b.push(m[x-1])}if(y)return{action:"accept",content:{value:b}}}}if(l==="number"){let m=e.min,f=e.max,g=m!==void 0&&f!==void 0?` [${m}\u2013${f}]`:m!==void 0?` [\u2265${m}]`:f!==void 0?` [\u2264${f}]`:"";for(;;){if(n.aborted)return se;let h;try{h=(await r(p.dim(` Enter a number${g}: `))).trim()}catch{return se}if(n.aborted||h===":cancel")return se;if(h===""&&e.allowSkip)return dr;if(h===""&&!e.allowSkip){o.line(p.warning(" Please enter a number (or :cancel to skip)."));continue}let b=Number(h);if(!isFinite(b)){o.line(p.warning(" Please enter a valid number."));continue}if(m!==void 0&&b<m){o.line(p.warning(` Value must be \u2265 ${m}.`));continue}if(f!==void 0&&b>f){o.line(p.warning(` Value must be \u2264 ${f}.`));continue}return{action:"accept",content:{value:b}}}}let u=e.minLength,d=e.maxLength;for(;;){if(n.aborted)return se;let m;try{m=(await r(p.dim(" > "))).trim()}catch{return se}if(n.aborted||m===":cancel")return se;if(m===""&&e.allowSkip)return dr;if(m===""){o.line(p.warning(" Please enter a response (or type :cancel to skip)."));continue}if(u!==void 0&&m.length<u){o.line(p.warning(` Response must be at least ${u} characters.`));continue}if(d!==void 0&&m.length>d){o.line(p.warning(` Response must be at most ${d} characters.`));continue}return{action:"accept",content:{value:m}}}}function rl(e){return async(t,{signal:n})=>{if(n.aborted)return _o;tl(process.stdout),e.suspendInput?.();try{if(t.origin==="agent")return await wF(t,e,n);if(t.mode==="form"){let o=t.requestedSchema,{properties:s,required:i,fieldsTruncated:a,originalFieldCount:l}=typeof o=="object"&&o!==null?fF(o):{properties:{},required:new Set,fieldsTruncated:!1,originalFieldCount:0};gF(e.writer,t),a&&e.writer.line(p.warning(` \u26A0 Schema has ${l} fields; only the first ${np} will be prompted (server may be malformed or compromised).`));let c=Object.create(null);if(Object.keys(s).length===0)return e.writer.line(p.warning(" \u26A0 Form schema has no usable fields \u2014 declining.")),_o;for(let u of i)if(!(u in s))return e.writer.line(p.warning(` \u26A0 Required field '${le(u,64)}' has no schema entry \u2014 declining.`)),_o;for(let[u,d]of Object.entries(s)){if(n.aborted)return se;let m=await hF(u,d,i.has(u),e.readLine,e.writer,n);if(m.tag==="cancel")return se;if(m.tag==="decline")return _o;m.value!==void 0&&!rp.has(u)&&(c[u]=m.value)}return{action:"accept",content:{...c}}}yF(e.writer,t);let r=(await e.readLine(p.dim("Continue? [y/N] "))).trim().toLowerCase();return r===""?se:r==="y"||r==="yes"?mF:_o}finally{e.resumeInput?.()}}}function TS(e){let t=Math.max(0,Math.min(1,e.ratio)),n=t>.8?p.error:t>.5?p.warning:p.meta,r=20,o=Math.round(t*r),s=r-o,i="["+"\u2588".repeat(o)+"\u2591".repeat(s)+"]",a=Math.round(t*100)+"%",l="";e.used!==void 0&&e.limit!==void 0&&(l=re(e.used)+"/"+re(e.limit));let c=l?`${i} ${a} ${l}`:`${i} ${a}`,u=`${i} ${a}`,d=`ctx ${a}`,m=e.sparkline?p.meta(e.sparkline)+" ":"",f=e.sparkline?z(e.sparkline)+1:0,g=Math.max(0,e.width-f);if(z(c)<=g&&g>90)return m+n(c);if(z(u)<=g&&g>32)return m+n(u);if(z(d)<=g)return m+n(d);if(e.width>0){let h=m+n(d);return ae(h,e.width)}return n(a)}var ol=class{stream;force;throttleMs;started=!1;lastRepaint=0;lastFields=null;resizeUnsub=null;lastPaintedRow=null;extraRows=0;constructor(t={}){this.stream=t.stream??process.stdout,this.force=t.force??!1,this.throttleMs=t.throttleMs??100}get enabled(){return this.force||!!this.stream.isTTY}start(){if(this.started||!this.enabled)return;this.started=!0,this.lastRepaint=0;let t=this.currentRows();this.stream.write("\x1B[s"),this.writeScrollRegion(t),this.stream.write("\x1B[u"),this.resizeUnsub===null&&(this.resizeUnsub=Ue.subscribe(()=>{this.onResize()}))}onResize(){if(!this.started||!this.enabled)return;let t=this.currentRows();this.stream.write("\x1B[s"),this.lastPaintedRow!==null&&this.lastPaintedRow!==this.paintRow(t)&&(this.stream.write(`\x1B[${this.lastPaintedRow};1H`),this.stream.write("\x1B[2K")),this.writeScrollRegion(t),this.stream.write("\x1B[u"),this.flush()}repaint(t){if(!this.enabled||!this.started){this.lastFields=t;return}let n=Date.now();if(n-this.lastRepaint<this.throttleMs){this.lastFields=t;return}this.lastRepaint=n,this.lastFields=t;let r=this.currentRows();this.stream.write("\x1B[s"),this.stream.write(`\x1B[${this.paintRow(r)};1H`),this.stream.write("\x1B[2K"),this.stream.write(this.formatLine(t)),this.stream.write("\x1B[u"),this.lastPaintedRow=this.paintRow(r)}flush(){this.lastRepaint=0,this.lastFields&&this.repaint(this.lastFields)}setExtraRows(t){if(this.extraRows=t,this.started&&this.enabled){let n=this.currentRows();this.stream.write("\x1B[s"),this.writeScrollRegion(n),this.stream.write("\x1B[u"),this.flush()}}getExtraRows(){return this.extraRows}rearm(){if(!this.started||!this.enabled)return;let t=this.currentRows();this.stream.write("\x1B[s"),this.writeScrollRegion(t),this.stream.write("\x1B[u"),this.flush()}withFullScrollRegion(t){if(!this.started||!this.enabled)return t();this.stream.write("\x1B[s"),this.stream.write("\x1B[r"),this.stream.write("\x1B[u");try{return t()}finally{let n=this.currentRows();this.stream.write("\x1B[s"),this.writeScrollRegion(n),this.stream.write("\x1B[u"),this.flush()}}stop(){if(this.resizeUnsub!==null&&(this.resizeUnsub(),this.resizeUnsub=null),!this.started||!this.enabled){this.started=!1;return}let t=this.currentRows();this.stream.write("\x1B[s"),this.stream.write(`\x1B[${this.lastPaintedRow??this.paintRow(t)};1H`),this.stream.write("\x1B[2K"),this.stream.write("\x1B[r"),this.stream.write("\x1B[u"),this.started=!1,this.lastRepaint=0,this.lastPaintedRow=null}formatLine(t){let n=[],r=Math.max(4,(this.stream.columns??80)-2);if(t.cwd){let a=Math.max(8,Math.floor(r*.4)),l=js(t.cwd,{maxWidth:a});l&&n.push({text:p.dim(l)})}if(n.push({text:p.brand(t.model)}),t.planMode&&n.push({text:p.warning("\u25CF plan")}),t.contextPct!==void 0){let a=TS({ratio:t.contextPct,used:t.contextUsedTokens,limit:t.contextLimit,sparkline:t.contextSparkline,width:r});n.push({text:a,droppablePriority:1})}t.cost!==void 0&&n.push({text:p.meta(`$${t.cost.toFixed(2)}`),droppablePriority:2}),t.tokens!==void 0&&n.push({text:p.meta(`${SF(t.tokens)} tok`),droppablePriority:3});let o=p.dim(" \xB7 "),s=n.map(a=>a.text).join(o);if(z(s)<=r)return s;let i=n.filter(a=>a.droppablePriority!==void 0);for(;i.length>0&&z(s)>r;){let a=Math.max(...i.map(l=>l.droppablePriority));n=n.filter(l=>l.droppablePriority===void 0||l.droppablePriority!==a),s=n.map(l=>l.text).join(o),i=n.filter(l=>l.droppablePriority!==void 0)}return ae(s,r)}currentRows(){let t=this.stream.rows;return typeof t=="number"&&t>0?t:24}paintRow(t){return t>1?t:1}writeScrollRegion(t){let n=1+this.extraRows;if(t>n){this.stream.write(`\x1B[1;${t-n}r`);return}this.stream.write("\x1B[r")}};function SF(e){return e>=1e6?`${(e/1e6).toFixed(1)}M`:e>=1e3?`${(e/1e3).toFixed(1)}k`:`${e}`}var lt=new Map,pr=new Map;function ce(e){if(lt.has(e.name))throw new Error(`Slash command already registered: ${e.name}`);lt.set(e.name,e);for(let t of e.aliases??[]){if(pr.has(t)||lt.has(t))throw new Error(`Slash alias collides: ${t}`);pr.set(t,e.name)}}function Bn(e){if(lt.has(e.name)){for(let[t,n]of pr.entries())n===e.name&&pr.delete(t);lt.delete(e.name)}ce(e)}function xS(e){lt.has(e.name)||ce(e)}function ES(){lt.clear(),pr.clear()}function Ze(){return[...lt.values()].sort((e,t)=>e.name.localeCompare(t.name))}function RS(){let e=[];for(let[t,n]of pr.entries()){let r=lt.get(n);r&&e.push({alias:t,canonical:n,summary:r.summary})}return e.sort((t,n)=>t.alias.localeCompare(n.alias))}function kF(e){if(lt.has(e))return lt.get(e);let t=pr.get(e);return t?lt.get(t):void 0}function vF(e,t){let n=Array.from({length:e.length+1},()=>new Array(t.length+1).fill(0));for(let r=0;r<=e.length;r++)n[r][0]=r;for(let r=0;r<=t.length;r++)n[0][r]=r;for(let r=1;r<=e.length;r++)for(let o=1;o<=t.length;o++){let s=e[r-1]===t[o-1]?0:1;n[r][o]=Math.min(n[r-1][o]+1,n[r][o-1]+1,n[r-1][o-1]+s)}return n[e.length][t.length]}function TF(e,t=3){let n;for(let r of lt.keys()){let o=vF(e,r);o<=t&&(n===void 0||o<n.dist)&&(n={name:r,dist:o})}return n?.name}function sp(e){let t=e.trim();if(!t.startsWith("/"))return null;let n=t.indexOf(" ");return n===-1?{name:t,args:""}:{name:t.slice(0,n),args:t.slice(n+1).trim()}}async function AS(e,t,n){let r=sp(e);if(r===null)return{handled:!1};let o=kF(r.name);if(!o){let a=TF(r.name);return a?t.out.warn(`Unknown command: ${r.name} (did you mean ${a}?)`):t.out.warn(`Unknown command: ${r.name} (type /help for commands)`),{handled:!0,result:"continue"}}let s=n??[];s.length>0&&o.acceptsAttachments!==!0&&t.out.warn(`\u26A0 Image attachments are ignored by ${r.name} (images only reach the model on skill commands like /forge, /mint).`);let i=await o.handler(t,r.args,o.acceptsAttachments===!0?s:void 0);return i==="forward"?{handled:!1}:{handled:!0,result:i}}import AF from"ora";function _S(e,t=5){if(e.length===0)return"";let n=e.slice(-t),r=["\u2581","\u2582","\u2583","\u2584","\u2585","\u2586","\u2587","\u2588"];return n.map(o=>{let s=Math.max(0,Math.min(1,o)),i=Math.min(7,Math.floor(s*8));return r[i]}).join("")}function sl(e,t,n){e.totalTurns=t.totalTurns,e.totalCostUsd=t.totalCostUsd,e.totalTokens=t.totalTokens,e.totalDurationMs=t.totalDurationMs,e.turns=[...t.turns],e.sessionId=t.sessionId??n,e.model=t.model,e.sessionStartTime=t.startedAt??Date.now()}function il(e,t){let n=e.turns;if(n.length===0)return;let r=n[n.length-1];if(!r)return;let o=IS(CS(r.user),80),s=IS(RF(CS(r.assistant)),120);o.length>0&&t.fn(p.dim(` Last: ${o}`)),s.length>0&&t.fn(p.dim(` \u21B3 ${s}`)),t.fn(p.dim(" \u21AA /history for full review"))}function CS(e){return e.replace(EF,"").replace(/\s+/g," ").trim()}var EF=/\u001B\][\s\S]*?(?:\u0007|\u001B\\|\u009C)|\u001B[PX^_][\s\S]*?(?:\u0007|\u001B\\|\u009C)|\u001B[@-OQ-WY-Z\\`6-9=]|[\u001B\u009B][[\]()#;?]*(?:\d{1,4}(?:[;:]\d{0,4})*)?[\dA-PR-TZcf-nq-uy=><~]/g;function RF(e){let t=e.match(/^.*?(?<![A-Za-z]\.[A-Za-z])[.!?](?=\s|$)/);return t?t[0]:e}function IS(e,t){let n=[...e];return n.length<=t?e:n.slice(0,t-1).join("")+"\u2026"}var Ws={stream:process.stdout,hideCursor:!1,discardStdin:!1};function ip(e,t){if(t?.getRatio()!==void 0)return t.getRatio();let n=e.turnTokens[e.turnTokens.length-1];return n?(n.input+n.output+n.cache)/it(e.model):0}function mr(e,t){let n=ip(e,t),r=it(e.model),o,s=t?.getDetail();if(s!==void 0)o=s.used;else{let a=e.turnTokens[e.turnTokens.length-1];a&&(o=a.input+a.output+a.cache)}let i;if(e.turnTokens.length>=2){let a=e.turnTokens.map(c=>(c.input+c.output+c.cache)/r),l=_S(a,5);l.length>0&&(i=l)}return{model:e.model,cost:e.totalCostUsd,tokens:e.totalTokens,contextPct:n,contextLimit:r,contextUsedTokens:o,contextSparkline:i,planMode:e.planMode,...e.cwd!==void 0?{cwd:e.cwd}:{}}}var _F={name:"/exit",aliases:["/quit"],summary:"Exit the session",hint:"When you want to tear down the REPL \u2014 Ctrl+D on an empty prompt does the same.",async handler(){return"exit"}},CF={name:"/clear",summary:"Clear conversation history",hint:"When the current thread has drifted off-topic or you want a clean slate without restarting the session.",async handler(e){try{await e.session.current.reset(),e.ui.clearScreen(),pS(e.stats),e.ledger?.clear(),e.out.success("Conversation history cleared.")}catch(t){e.out.error(t instanceof Error?t.message:"Unknown error")}return"continue"}},IF={name:"/compact",summary:"Compact history (summarize older messages)",hint:"When context is filling up but you want to keep the thread \u2014 summarizes old turns and keeps the recent ones intact.",async handler(e){let t=AF({text:p.meta("Summarizing earlier turns..."),...Ws}).start();try{let n=await e.session.current.compact();if(t.stop(),n.compacted){let r=n.tokensSavedEstimate?` (~${n.tokensSavedEstimate} input tokens saved)`:"";e.out.success(`Compacted ${n.messagesBefore} \u2192 ${n.messagesAfter} messages${r}.`)}else{let r=n.reason??"unknown";r==="aborted"?e.out.info("Compaction cancelled."):r.startsWith("summarization-failed")?e.out.error(`Compaction failed: ${r}. History unchanged.`):r==="nothing-to-summarize"?e.out.info("Nothing to compact \u2014 all history is within the keep window."):r==="not-supported"?e.out.warn("Compaction is not supported for this model or provider \u2014 use a Claude model to enable /compact."):e.out.info(`Nothing to compact (${r}).`)}}catch(n){t.stop(),e.out.error(n instanceof Error?n.message:"Unknown error")}return"continue"}},PF={name:"/help",summary:"Show this help",hint:"When you want the full command list with usage strings \u2014 broader than this inline dropdown.",async handler(e){let t=Ze(),n=t.reduce((r,o)=>Math.max(r,o.name.length),0)+2;e.out.line(),e.out.line(p.bold(p.brand("Commands"))),e.out.line(he());for(let r of t){let o=r.usage??r.name,s=" ".repeat(Math.max(0,n-o.length));e.out.line(` ${p.warning(o)}${s} ${p.dim(r.summary)}`)}return e.out.line(),e.out.line(p.dim(" Tip: Ctrl+C interrupts a running turn; a second press exits.")),e.out.line(),"continue"}},PS=[_F,CF,IF,PF];function ap(e,t=30){return!e||e.length===0?p.dim("(none)"):e.length<=t?e.join(", "):`${e.slice(0,t).join(", ")}, ${p.dim(`+${e.length-t} more`)}`}function St(e,t){return` ${p.label(e.padEnd(16))} ${t}`}function al(e){let t=[];t.push(" "+he("Session Debug")),e.sessionId&&t.push(St("session",e.sessionId)),e.model&&t.push(St("model",e.model)),e.permissionMode&&t.push(St("permission",e.permissionMode)),e.cwd&&t.push(St("cwd",e.cwd)),e.claudeCodeVersion&&t.push(St("sdk",`v${e.claudeCodeVersion}`)),e.apiKeySource&&t.push(St("api key",e.apiKeySource)),e.outputStyle&&t.push(St("output style",e.outputStyle));let n=e.tools?.length??0;t.push(St(`tools (${n})`,ap(e.tools)));let r=e.mcpServers??[],o=r.length?r.map(c=>`${c.name}[${c.status}]`).join(", "):p.dim("(none)");t.push(St(`mcp (${r.length})`,o));let s=e.skills?.length??0;t.push(St(`skills (${s})`,ap(e.skills)));let i=e.plugins?.length??0,a=i?(e.plugins??[]).map(c=>c.name).join(", "):p.dim("(none)");t.push(St(`plugins (${i})`,a));let l=e.slashCommands?.length??0;return t.push(St(`slash (${l})`,ap(e.slashCommands))),t.push(" "+he()),t.join(`
2022
- `)}var up=["opus","opus_1m","sonnet","sonnet_1m","haiku"],DF={name:"/cost",summary:"Show total and per-turn cost",hint:"When you want a dollar breakdown of this session \u2014 total, average per turn, and the recent turn-by-turn series.",async handler(e){let{stats:t,out:n}=e;if(n.line(),n.line(p.bold("Session cost")),n.line(he()),n.line(` total ${p.success(Ne(t.totalCostUsd))}`),n.line(` turns ${p.meta(String(t.totalTurns))}`),t.totalTurns>0){let r=t.totalCostUsd/t.totalTurns;n.line(` avg/turn ${p.meta(Ne(r))}`)}if(t.turnCosts.length>0){let r=t.turnCosts.slice(-5).map(Ne).join(p.dim(" \xB7 "));n.line(` last 5 ${r}`)}return n.line(),"continue"}};function LF(e,t){let n=t.apiUsage,r=n?.input_tokens??0,o=n?.output_tokens??0,s=n?.cache_read_input_tokens??0,i=n?.cache_creation_input_tokens??0,a=r+o+s+i;e.line(),e.line(p.bold("Token usage")+p.dim(" (SDK breakdown)")),e.line(he()),e.line(` total ${p.success(re(t.totalTokens))} of ${p.meta(re(t.maxTokens))} (${p.meta(`${Math.round(t.percentage*100)/100}%`)})`),t.autoCompactThreshold&&t.isAutoCompactEnabled&&e.line(` compact at ${p.meta(re(t.autoCompactThreshold))}`),e.line(),e.line(p.dim(" Last turn (API):")),e.line(` input ${p.meta(re(r))}`),e.line(` output ${p.meta(re(o))}`),e.line(` cache read ${p.meta(re(s))}`),e.line(` cache creat ${p.meta(re(i))}`),e.line(` total ${p.meta(re(a))}`);let l=t.categories??[];if(l.length>0){let m=[...l].sort((f,g)=>g.tokens-f.tokens).slice(0,5);e.line(),e.line(p.dim(" Top categories:"));for(let f of m)e.line(` ${p.warning(f.name.padEnd(18))} ${p.meta(re(f.tokens))}`)}let c=t.systemTools??[],u=t.mcpTools??[];if(c.length>0||u.length>0){e.line();let m=c.reduce((g,h)=>g+h.tokens,0),f=u.reduce((g,h)=>g+h.tokens,0);c.length>0&&e.line(p.dim(` system tools ${c.length} tools, ${re(m)} tokens`)),u.length>0&&e.line(p.dim(` MCP tools ${u.length} tools, ${re(f)} tokens`))}let d=t.agents??[];if(d.length>0){let m=d.reduce((f,g)=>f+g.tokens,0);e.line(p.dim(` agents ${d.length} loaded, ${re(m)} tokens`))}if(t.skills){let m=t.skills;e.line(p.dim(` skills ${m.includedSkills}/${m.totalSkills} included, ${re(m.tokens)} tokens`))}if(t.slashCommands){let m=t.slashCommands;e.line(p.dim(` slash cmds ${m.includedCommands}/${m.totalCommands} included, ${re(m.tokens)} tokens`))}e.line()}function FF(e,t){let n=t.turnTokens.reduce((c,u)=>c+u.input,0),r=t.turnTokens.reduce((c,u)=>c+u.output,0),o=t.turnTokens.reduce((c,u)=>c+u.cache,0),s=n+r,i=it(t.model),a=n+r+o,l=Math.round(a/i*100);e.line(),e.line(p.bold("Token usage")+p.dim(" (local stats \u2014 SDK breakdown unavailable)")),e.line(he()),e.line(` input ${p.meta(re(n))}`),e.line(` output ${p.meta(re(r))}`),e.line(` cache read ${p.meta(re(o))}`),e.line(` total ${p.success(re(s))}`),e.line(` context ${p.meta(`${l}% of ${re(i)} (${t.model})`)}`),e.line()}var NF={name:"/tokens",aliases:["/ctx"],summary:"Show token usage (SDK breakdown with local-stats fallback)",hint:"When you want to know how full the context window is \u2014 input/output/cache breakdown plus % of the model's limit used.",async handler(e){try{let t=await e.session.current.getContextUsage();LF(e.out,t)}catch{FF(e.out,e.stats)}return"continue"}},BF={name:"/history",summary:"Show conversation history",async handler(e){let{stats:t,out:n}=e;return t.turns.length===0?(n.info("No turns yet in this session."),"continue"):(n.line(),n.line(p.bold(`Session history (${t.turns.length} turn${t.turns.length===1?"":"s"})`)),n.line(he()),t.turns.forEach((r,o)=>{let s=p.meta(`#${o+1}`),i=r.user.length>100?r.user.slice(0,97)+"...":r.user,a=r.assistant.length>100?r.assistant.slice(0,97)+"...":r.assistant;n.line(` ${s} ${p.user("\u25B6")} ${i}`),n.line(` ${p.brand("\u25C6")} ${p.dim(a)}`)}),n.line(),"continue")}},UF={name:"/reset",summary:"Clear screen, history, and session stats",async handler(e){let{stats:t,ui:n,out:r}=e;try{await e.session.current.sendMessage("/clear")}catch{}return t.totalTurns=0,t.totalCostUsd=0,t.totalTokens=0,t.totalDurationMs=0,t.turnCosts.length=0,t.turnTokens.length=0,t.turns.length=0,t.sessionStartTime=Date.now(),n.clearScreen(),r.success("Session reset."),"continue"}},jF={name:"/model",usage:"/model <opus|opus_1m|sonnet|sonnet_1m|haiku|org/model>",summary:"Switch the active model mid-session",hint:"When you want to upgrade to opus for a hard problem or downshift to haiku for cheap iteration \u2014 context carries over. Also accepts full HuggingFace-style IDs (e.g. mlx-community/Qwen3-30B-A3B-4bit).",async handler(e,t){let n=t.trim().toLowerCase();if(!n)return e.out.info(`Current model: ${p.brand(e.stats.model)}`),e.out.line(p.dim(` Aliases: ${up.join(", ")} (or any org/model HF id)`)),"continue";let r=up.includes(n),o=xe(n)==="openai-compatible";if(!r&&!o)return e.out.warn(`Unknown model: ${n}. Aliases: ${up.join(", ")} (or org/model for local/OpenAI-compatible)`),"continue";try{await e.session.current.setModel(n),e.stats.model=n,e.ui.repaintStatusLine(),e.out.success(`Model switched to ${p.brand(n)}`)}catch(s){e.out.error(`Failed to switch model: ${s instanceof Error?s.message:String(s)}`)}return"continue"}},WF={name:"/tools",summary:"List tools available to the session",async handler(e){try{let n=(await e.session.current.waitForInitialization()).tools??[];if(n.length===0)return e.out.info("No tools reported by the session."),"continue";e.out.line(),e.out.line(p.bold(`Tools (${n.length})`)),e.out.line(he());let r=3,o=Math.ceil(n.length/r),s=Math.max(...n.map(i=>i.length))+2;for(let i=0;i<o;i++){let a=[];for(let l=0;l<r;l++){let c=l*o+i;c<n.length&&a.push(n[c].padEnd(s))}e.out.line(" "+a.join(""))}e.out.line()}catch(t){e.out.error(`Could not read session tools: ${t instanceof Error?t.message:String(t)}`)}return"continue"}},HF={name:"/mcp",summary:'List MCP servers connected to the session ("/mcp auth" to surface pending OAuth URLs)',async handler(e,t){let n=(t??"").trim().toLowerCase();if(n==="auth"){try{let{readOauthPending:r}=await Promise.resolve().then(()=>(ul(),BS)),o=r();if(Object.keys(o).length===0)return e.out.info("No MCP servers are waiting for OAuth."),"continue";e.out.line(),e.out.line(p.bold(`MCP OAuth pending (${Object.keys(o).length})`)),e.out.line(he());for(let[s,i]of Object.entries(o)){let a=Date.now()-i.timestamp,l=Math.round(a/6e4);e.out.line(` ${p.warning("\u25CF")} ${s} ${p.dim(`(${l}m ago)`)}`),e.out.line(` ${p.info(i.authorizationUrl)}`)}e.out.line(),e.out.line(p.dim(" Open each URL in a browser. After authorizing, paste the code with:")),e.out.line(p.dim(" /mcp auth complete <serverName> <code>")),e.out.line()}catch(r){e.out.error(`Could not read OAuth state: ${r instanceof Error?r.message:String(r)}`)}return"continue"}if(n.startsWith("auth complete ")){let r=(t??"").trim(),o=r.slice(r.toLowerCase().indexOf("auth complete ")+14).trim(),s=o.indexOf(" ");if(s===-1||o.slice(0,s).length===0||o.slice(s+1).trim().length===0)return e.out.error("Usage: /mcp auth complete <serverName> <code>"),"continue";let i=o.slice(0,s).trim(),a=o.slice(s+1).trim();if(!e.mcpManager)return e.out.error("No MCP manager available in this session. Make sure an mcp.json config is present and at least one server is enabled."),"continue";try{e.out.info(`Completing OAuth for "${i}"\u2026`),await e.mcpManager.completeAuth(i,a),e.out.success(`OAuth complete for "${i}" \u2014 server is now connected.`)}catch(l){e.out.error(`OAuth completion failed for "${i}": ${l instanceof Error?l.message:String(l)}`)}return"continue"}if(n!==""&&n!=="auth")return e.out.error(`Unknown /mcp subcommand: "${n}". Try: /mcp, /mcp auth, /mcp auth complete <server> <code>.`),"continue";try{let o=(await e.session.current.waitForInitialization()).mcpServers??[];if(o.length===0)return e.out.info("No MCP servers connected."),"continue";e.out.line(),e.out.line(p.bold(`MCP servers (${o.length})`)),e.out.line(he());let s=0;for(let i of o){let a=typeof i=="string"?i:i.name??JSON.stringify(i),l=typeof i=="object"&&i!==null&&"status"in i?String(i.status):"",c=l==="connected"?p.success("\u25CF"):p.warning("\u25CF");e.out.line(` ${c} ${a}${l?p.dim(` (${l})`):""}`),l==="oauth_pending"&&s++}e.out.line(),s>0&&(e.out.line(p.dim(` ${s} server(s) need OAuth \u2014 run "/mcp auth" to see authorization URLs.`)),e.out.line())}catch(r){e.out.error(`Could not read MCP servers: ${r instanceof Error?r.message:String(r)}`)}return"continue"}},KF={name:"/limits",summary:"Show known per-model context-window limits",async handler(e){e.out.line(),e.out.line(p.bold("Context-window limits")),e.out.line(he());for(let[t,n]of Object.entries(Es)){let r=t===e.stats.model?p.brand(" \u2190 active"):"";e.out.line(` ${p.warning(t.padEnd(12))} ${p.meta(re(n))}${r}`)}return e.out.line(),"continue"}},GF={name:"/debug",summary:"Show SDK session metadata (tools, MCP, skills, plugins, etc.)",hint:"When something feels broken and you want to inspect what the session actually loaded \u2014 tools, MCP servers, plugins, system prompt source.",async handler(e){try{let t=await e.session.current.waitForInitialization();e.out.line(),e.out.line(al(t)),e.out.line()}catch(t){e.out.error(`Could not read session metadata: ${t instanceof Error?t.message:String(t)}`)}return"continue"}},US=[DF,NF,BF,UF,jF,WF,HF,KF,GF];var dp=!1;async function Ct(e,t,n={}){let r=e.stats.planMode,o=t!==void 0?t:!r;try{if(await e.session.current.setPermissionMode(o?"plan":"default"),e.stats.planMode=o,e.ui.repaintStatusLine(),o){let s=dp?"":p.dim(" Shift+Tab or /plan to exit.");dp||(dp=!0),e.out.success(p.warning("\u25CF plan mode ON")+p.dim(" \u2014 write_file, edit_file, and write-intent bash are refused.")+s)}else n.closureSummarySkipped?e.out.success(p.success("\u25CB plan mode OFF")+p.dim(" \u2014 force-exit (closure summary skipped). Default permissions restored.")):e.out.success(p.success("\u25CB plan mode OFF")+p.dim(" \u2014 default permissions restored"))}catch(s){e.out.error(`Could not toggle plan mode: ${s instanceof Error?s.message:String(s)}`)}}async function jS(e){e.stats.pendingPlanExit&&(await Ct(e,!1),e.stats.planMode||(e.stats.pendingPlanExit=!1))}var qF=["You are about to exit plan mode. Before I flip permissions back to default on the next turn, emit your final plan in three sections:",""," - **Chosen approach** \u2014 the plan you recommend, in one to three sentences."," - **Risks named** \u2014 the concrete failure modes, constraints, or unknowns this plan does not eliminate."," - **Alternatives considered** \u2014 the options you weighed and why you rejected them.","","This is the record. Be specific. Do not propose write actions in this turn \u2014 writes are still refused until the mode flips."].join(`
2023
- `);async function zF(e){return e.stats.pendingPlanExit=!0,e.ui.repaintStatusLine(),e.out.success(p.warning("\u25CF plan exit queued")+p.dim(" \u2014 plan mode still ON; writes still refused. Submitting closure summary (chosen approach, risks, alternatives); mode flips after the model responds. Force-exit now: /plan off again or Shift+Tab.")),{kind:"submit",message:qF}}async function JF(e){return e.stats.pendingPlanExit=!1,await Ct(e,!1,{closureSummarySkipped:!0}),"continue"}async function VF(e){return e.stats.pendingPlanExit=!1,e.ui.repaintStatusLine(),e.out.success(p.warning("\u25CF plan mode ON")+p.dim(" \u2014 plan exit cancelled, staying in plan mode.")),"continue"}function YF(e){e.out.info(p.dim("(plan exit cancelled \u2014 submitting your prompt instead.)"))}var WS={name:"/plan",usage:"/plan [on|off|<prompt>]",summary:"Toggle plan mode (write_file, edit_file, and write-intent bash refused)",hint:"When you want the model to think through an approach without touching files \u2014 refuses writes until you flip back. Shift+Tab toggles too.",async handler(e,t){let n=t.trim(),r=n.toLowerCase();if(n!==""&&r!=="on"&&r!=="off"){let s=!!e.stats.pendingPlanExit;return e.stats.planMode||await Ct(e,!0),s&&(e.stats.pendingPlanExit=!1,YF(e)),{kind:"submit",message:n}}return(r==="on"?!0:r==="off"?!1:!e.stats.planMode)===!0?e.stats.pendingPlanExit?VF(e):(e.stats.planMode||await Ct(e,!0),"continue"):e.stats.planMode?e.stats.pendingPlanExit?JF(e):zF(e):(await Ct(e,!1),"continue")}};W();import{readFileSync as XF,writeFileSync as ZF,existsSync as HS,mkdirSync as QF}from"fs";import{join as eN}from"path";function KS(){return Hf(),au()}function GS(e){return eN(KS(),`${e}.json`)}function dl(e){let t=GS(e);if(!HS(t))return{sessionId:e,items:[]};try{let n=XF(t,"utf-8"),r=JSON.parse(n);return Array.isArray(r.items)?r:{sessionId:e,items:[]}}catch{return{sessionId:e,items:[]}}}function Hs(e){let t=KS();HS(t)||QF(t,{recursive:!0}),ZF(GS(e.sessionId),JSON.stringify(e,null,2))}function qS(e,t){let r={id:e.items.reduce((o,s)=>Math.max(o,s.id),0)+1,text:t,done:!1,createdAt:Date.now()};return e.items.push(r),r}function zS(e,t){let n=e.items.find(r=>r.id===t);return n&&(n.done=!0),n}function JS(e,t){let n=e.items.findIndex(r=>r.id===t);return n===-1?!1:(e.items.splice(n,1),!0)}function VS(e){e.items.length=0}function pl(e){if(e.items.length===0)return[];let t=[],n=Math.max(20,Y());{let r=p.dim("\u250C\u2500 todos "),o=Math.max(0,Math.min(n-10,120));t.push(r+p.dim("\u2500".repeat(o)))}for(let r of e.items){let o=r.done?p.success("[x]"):p.dim("[ ]"),s=r.done?p.dim(r.text):En(r.text),a=` ${p.meta(`#${r.id}`)} ${o} `,l=Math.max(8,n-z(a)),c=me(s,l).split(`
2024
- `);t.push(a+(c[0]??""));let u=" ".repeat(z(a));for(let d of c.slice(1))t.push(u+d)}{let r=Math.max(0,Math.min(n-1,120));t.push(p.dim("\u2514"+"\u2500".repeat(r)))}return t}function YS(e){return e.items.length===0?"":e.items.map(t=>`${t.id}:${t.done?1:0}:${t.text}`).join(`
2025
- `)}function XS(e){let t=e.stats.sessionId??"unbound";return dl(t)}function tN(e){let t=XS(e),n=pl(t);if(n.length===0){e.out.info("No todos yet. Try /todo add buy milk");return}for(let r of n)e.out.line(r)}var ZS={name:"/todo",usage:"/todo [add|done|rm|clear|list] ...",summary:"Manage this session's todo list",hint:"When you want a durable checklist the model and you both see \u2014 survives across turns and shows above each prompt.",async handler(e,t){let n=t.trim();if(!n||n==="list")return tN(e),"continue";let[r,...o]=n.split(/\s+/),s=o.join(" "),i=XS(e);switch(r){case"add":{if(!s)return e.out.warn("Usage: /todo add <text>"),"continue";let a=qS(i,s);return Hs(i),e.out.success(`Added ${p.meta(`#${a.id}`)} ${a.text}`),"continue"}case"done":{let a=parseInt(s,10);if(!Number.isFinite(a))return e.out.warn("Usage: /todo done <id>"),"continue";let l=zS(i,a);return l?(Hs(i),e.out.success(`Marked done ${p.meta(`#${a}`)} ${l.text}`)):e.out.warn(`No todo with id ${a}`),"continue"}case"rm":case"remove":{let a=parseInt(s,10);return Number.isFinite(a)?(JS(i,a)?(Hs(i),e.out.success(`Removed ${p.meta(`#${a}`)}`)):e.out.warn(`No todo with id ${a}`),"continue"):(e.out.warn("Usage: /todo rm <id>"),"continue")}case"clear":return VS(i),Hs(i),e.out.success("All todos cleared."),"continue";default:return e.out.warn(`Unknown subcommand: ${r}. Try /todo add, done, rm, clear, list.`),"continue"}}};var QS={name:"/save",usage:"/save [name]",hint:"When you want to checkpoint this conversation so /resume can pick it back up in a future session.",summary:"Save this session to disk (for /resume)",async handler(e,t){if(e.stats.totalTurns===0)return e.out.warn("Nothing to save \u2014 no turns in this session yet."),"continue";try{let n=t.trim()||void 0,r=To(e.stats,n);e.out.success(p.success("Saved")+p.dim(` ${r}`));let o=e.stats.sessionId??n;o&&e.out.line(p.dim(` Resume: ${Ao(o,e.stats.model)}`))}catch(n){e.out.error(`Could not save: ${n instanceof Error?n.message:String(n)}`)}return"continue"}};function nN(e){if(typeof e!="number"||!Number.isFinite(e))return" \u2014 ";let t=new Date(e);return Number.isNaN(t.getTime())?" \u2014 ":t.toISOString().replace("T"," ").slice(0,16)}function rN(e){return{id:e.id,resumeId:e.data.sessionId??e.id,stored:e.data}}var ek={name:"/resume",usage:"/resume [id]",hint:"When you want to continue a previously /saved session \u2014 runs interactively to pick one if no id is given.",summary:"List saved sessions, or swap the active session for a stored one",async handler(e,t){let n=t.trim();if(n){let o=xo(n);if(!o)return e.out.warn(`No saved session: ${n}`),"continue";if(typeof e.requestResume=="function"){let a=e.session.current.sessionId,l=o.data.sessionId;if(a!==void 0&&l!==void 0&&a===l||a!==void 0&&a===o.id)return e.out.warn(`Already on session ${o.id}.`),"continue";e.out.info(`Resuming session ${o.id} \u2026`);let u=await e.requestResume(rN(o));return u.ok?e.out.success(`Resumed ${o.id} (sdk id: ${u.sessionId})`):e.out.warn(u.reason),"continue"}let s=o.data,i=s.sessionId??o.id;return e.out.line(),e.out.line(p.bold(`Session ${o.id}`)),e.out.line(he()),e.out.line(` model ${p.brand(s.model)}`),e.out.line(` turns ${p.meta(String(s.totalTurns))}`),e.out.line(` cost ${p.meta(Ne(s.totalCostUsd))}`),e.out.line(` sdk id ${p.meta(s.sessionId??"\u2014")}`),e.out.line(),e.out.line(p.dim(" Resume with:")),e.out.line(p.brand(` ${Ao(i,s.model)}`)),e.out.line(),"continue"}let r=Us();if(r.length===0)return e.out.info("No saved sessions found. Use /save first."),"continue";e.out.line(),e.out.line(p.bold(`Saved sessions (${r.length})`)),e.out.line(he());for(let o of r.slice(0,20)){let s=nN(o.savedAt),i=p.brand(o.model.padEnd(7)),a=p.meta(`${o.totalTurns} turn${o.totalTurns===1?"":"s"}`.padEnd(9)),l=p.meta(Ne(o.totalCostUsd).padStart(8)),c=p.warning(o.id);e.out.line(` ${s} ${i} ${a} ${l} ${c}`)}return e.out.line(),e.out.line(p.dim(" Resume with: /resume <id>")),e.out.line(),"continue"}};import{existsSync as uN,readFileSync as dN}from"node:fs";import{resolve as sk}from"node:path";import{execFileSync as tk}from"node:child_process";import{readFileSync as oN,writeFileSync as sN}from"node:fs";import{resolve as iN}from"node:path";var aN={feat:"Added",fix:"Fixed",refactor:"Changed",perf:"Changed",docs:"Changed",chore:"Changed",ci:"Changed",test:"Changed","test+fix":"Fixed",build:"Changed",style:"Changed"},lN=["Added","Fixed","Changed","Deprecated","Removed","Security"];function nk(e){let t;try{t=tk("git",["describe","--tags","--abbrev=0"],{cwd:e,encoding:"utf8",stdio:["ignore","pipe","ignore"]}).trim()}catch{t=""}let n=t?["log",`${t}..HEAD`,"--format=%h %s"]:["log","-50","--format=%h %s"],r=tk("git",n,{cwd:e,encoding:"utf8"}).trim();return r?r.split(`
2021
+ `),c=d.length}return new Promise(d=>{process.stdin.setRawMode(!0),ur(process.stdin);let p=(g,h)=>{if(h)if(h.name==="up"||h.sequence==="\x1B[A")r>0&&(r--,r<o&&(o=r),u());else if(h.name==="down"||h.sequence==="\x1B[B"){if(r<e.length-1){r++;let{end:b}=i();r>=b&&o++,u()}}else h.name==="space"?(s.has(r)?s.delete(r):s.add(r),u()):h.name==="return"||h.name==="enter"?(f(),d([...s].sort((b,y)=>b-y))):(h.name==="escape"||h.ctrl&&h.name==="c")&&(f(),d(":cancel"))};function f(){process.stdin.removeListener("keypress",p);try{process.stdin.setRawMode(!1)}catch{}}process.stdin.on("keypress",p)})}var _o={action:"decline"},se={action:"cancel"},mF={action:"accept"};function fF(e){let t=e.properties,n={},r=0,o=!1;if(typeof t=="object"&&t!==null){let a=0;for(let[l,c]of Object.entries(t))if(!rp.has(l)){if(r+=1,a>=np){o=!0;continue}n[l]=c,a+=1}}let s=e.required,i=new Set(Array.isArray(s)?s.slice(0,np*2).filter(a=>typeof a=="string").filter(a=>!rp.has(a)):[]);return{properties:n,required:i,fieldsTruncated:o,originalFieldCount:r}}function gF(e,t){e.line(),e.line(m.warning("\u26A0 MCP form elicitation")),e.line(m.dim(" server: ")+m.bold(le(t.serverName,64))),e.line(m.dim(" message: ")+le(t.message,256)),t.elicitationId&&e.line(m.dim(" id: ")+le(t.elicitationId,64)),e.line(m.dim(" Type :decline or :cancel at any prompt to exit.")),e.line()}async function hF(e,t,n,r,o,s){if(s.aborted)return{tag:"cancel"};let i=le(t.description??t.title??e),a=le(t.type??"string",32),l=le(e,64),c;if(t.enum!==void 0){let d=t.enum.slice(0,nl).map(f=>le(String(f),32)).join("|"),p=t.enum.length>nl?"|\u2026":"";c=` (enum: ${d}${p})`}else a==="boolean"?c=" (boolean: y/n)":a==="number"||a==="integer"?c=` (${a})`:a==="string"?c=" (string)":(c=` (${a} \u2014 treated as string)`,o.line(m.warning(` \u26A0 Unknown field type '${a}' for '${l}' \u2014 collecting as string.`)));let u=n?"":m.dim(" [optional, enter to skip]");for(o.line(m.dim(` [${l}]`)+m.dim(` ${i}`)+m.dim(c)+u),t.enum!==void 0&&t.enum.length>tp&&o.line(m.warning(` \u26A0 Field '${l}' has ${t.enum.length} enum values; only the first ${tp} are valid for input.`));;){let d;try{d=await r(m.dim(" > "))}catch{return{tag:"cancel"}}if(s.aborted)return{tag:"cancel"};let p=d.trim();if(p===":cancel")return{tag:"cancel"};if(p===":decline")return{tag:"decline"};if(p===""){if(n){o.line(m.warning(" (required \u2014 cannot be skipped)"));continue}return{tag:"value",value:t.default}}let f;if(a==="boolean"){let g=p.toLowerCase();if(g==="y"||g==="yes"||g==="true"||g==="1")f=!0;else if(g==="n"||g==="no"||g==="false"||g==="0")f=!1;else{o.line(m.warning(" Invalid boolean \u2014 enter y/yes/true/1 or n/no/false/0."));continue}}else if(a==="number"){let g=Number(p);if(!isFinite(g)){o.line(m.warning(" Invalid number \u2014 enter a numeric value."));continue}f=g}else if(a==="integer"){let g=parseInt(p,10);if(!isFinite(g)||String(g)!==p.replace(/\.0+$/,"")){o.line(m.warning(" Invalid integer \u2014 enter a whole number."));continue}f=g}else f=p;if(t.enum!==void 0){let g=t.enum.slice(0,tp),h=!1;for(let b of g)if(String(b)===String(f)){h=!0;break}if(!h){let b=le(String(f),64),y=g.slice(0,nl).map(x=>le(String(x),32)).join(", "),w=g.length>nl?", \u2026":"";o.line(m.warning(` '${b}' is not a valid choice. Valid: ${y}${w}`));continue}}return{tag:"value",value:f}}}function yF(e,t){e.line(),e.line(m.warning("\u26A0 MCP elicitation")),e.line(m.dim(" server: ")+m.bold(le(t.serverName,64))),e.line(m.dim(" message: ")+le(t.message,256)),t.url&&e.line(m.dim(" url: ")+m.brand(le(t.url,512))),t.elicitationId&&e.line(m.dim(" id: ")+le(t.elicitationId,64)),e.line()}var dr={action:"skip"};function op(e){let t=[];return t.push(m.warning(" \u{1F4AC} Agent question")),t.push(m.bold(" ? "+le(e.message,512))),e.context&&t.push(m.dim(" "+le(e.context,512))),t.push(""),t}async function bF(e,t,n,r){let o=op(t),s=t.allowSkip===!0;if(e==="number"){let u=t.min,d=t.max,f=`enter to submit \xB7 esc to cancel${u!==void 0&&d!==void 0?` [${u}\u2013${d}]`:u!==void 0?` [\u2265${u}]`:d!==void 0?` [\u2264${d}]`:""}`,h=await n({header:o,help:f,validate:y=>{let w=y.trim();if(w==="")return s?null:"Please enter a number (or esc to cancel).";let x=Number(w);return Number.isFinite(x)?u!==void 0&&x<u?`Value must be \u2265 ${u}.`:d!==void 0&&x>d?`Value must be \u2264 ${d}.`:null:"Please enter a valid number."},signal:r});if(h===null)return null;let b=h.trim();return b===""&&s?{tag:"skip"}:{tag:"number",value:Number(b)}}let i=t.minLength,a=t.maxLength,c=await n({header:o,validate:u=>u===""?s?null:"Please enter a response (or esc to cancel).":i!==void 0&&u.length<i?`Response must be at least ${i} characters.`:a!==void 0&&u.length>a?`Response must be at most ${a} characters.`:null,signal:r});return c===null?null:c===""&&s?{tag:"skip"}:{tag:"text",value:c}}async function wF(e,t,n){if(n.aborted)return _o;let{readLine:r,writer:o,pendingCount:s,pickFromList:i,readTextOverlay:a}=t,l=e.type??"text",c=s();if(c>1&&o.line(m.dim(` [${c} questions queued]`)),(l==="choice"||l==="multi_choice")&&i&&(e.choices?.length??0)>0){let p=(e.choices??[]).map(h=>le(h,128)),f;try{f=await i({header:op(e),options:p,multi:l==="multi_choice",signal:n})}catch{return se}if(n.aborted||f===null)return se;if(f.length===0)return e.allowSkip?dr:se;let g=f.length===1?le(f[0]??"",128):f.map(h=>le(h,128)).join(", ");return o.line(m.dim(" \u2713 ")+m.brand(g)),l==="choice"?{action:"accept",content:{value:f[0]}}:{action:"accept",content:{value:[...f]}}}if(l==="confirm"&&i){let f=e.questionDefault!==!1?["Yes","No"]:["No","Yes"],g;try{g=await i({header:op(e),options:f,multi:!1,signal:n})}catch{return se}if(n.aborted||g===null)return se;let h=g[0];if(h===void 0)return se;let b=h==="Yes";return o.line(m.dim(" \u2713 ")+(b?m.success("Yes"):m.error("No"))),{action:"accept",content:{value:b}}}if((l==="text"||l==="number")&&a){let p=await bF(l,e,a,n);if(p===null)return se;if(p.tag==="skip")return dr;let f=p.tag==="text"?le(p.value,256):String(p.value);return o.line(m.dim(" \u2713 ")+m.brand(f)),{action:"accept",content:{value:(p.tag==="text",p.value)}}}if(o.line(),o.line(m.warning("\u{1F4AC} Agent question")),e.context&&o.line(m.dim(" context: ")+le(e.context,512)),o.line(m.bold(" "+le(e.message,512))),o.line(m.dim(" Type :cancel to skip this question.")),o.line(),l==="confirm"){o.line("\x07");let p=e.questionDefault===!0?"Y/n":"y/N";for(;;){if(n.aborted)return se;let f;try{f=(await r(m.dim(` Continue? [${p}] `))).trim().toLowerCase()}catch{return se}if(n.aborted||f===":cancel")return se;if(f==="")return{action:"accept",content:{value:e.questionDefault===!0}};if(f==="y"||f==="yes")return{action:"accept",content:{value:!0}};if(f==="n"||f==="no")return{action:"accept",content:{value:!1}};o.line(m.warning(" Please enter y or n."))}}if(l==="choice"){o.line("\x07");let p=e.choices??[],f=await dF(p,n);if(f!==null){if(f===":cancel")return se;let g=p[f];return g!==void 0?(o.line(m.dim(` Selected: ${le(g,128)}`)),{action:"accept",content:{value:g}}):se}for(p.forEach((g,h)=>{o.line(` ${h+1}. ${le(g,128)}`)});;){if(n.aborted)return se;let g;try{g=(await r(m.dim(" Enter number: "))).trim()}catch{return se}if(n.aborted||g===":cancel")return se;if(g===""&&e.allowSkip)return dr;let h=parseInt(g,10);if(!isFinite(h)||String(h)!==g||h<1||h>p.length){o.line(m.warning(` Please enter a number between 1 and ${p.length}.`));continue}return{action:"accept",content:{value:p[h-1]}}}}if(l==="multi_choice"){let p=e.choices??[],f=await pF(p,n);if(f!==null){if(f===":cancel")return se;if(f.length===0&&e.allowSkip)return dr;if(f.length>0){let g=f.map(h=>p[h]);return o.line(m.dim(` Selected: ${g.map(h=>le(h,64)).join(", ")}`)),{action:"accept",content:{value:g}}}}for(p.forEach((g,h)=>{o.line(` ${h+1}. ${le(g,128)}`)});;){if(n.aborted)return se;let g;try{g=(await r(m.dim(" Enter numbers (comma-separated): "))).trim()}catch{return se}if(n.aborted||g===":cancel")return se;if(g===""&&e.allowSkip)return dr;if(g===""){o.line(m.warning(" Please enter at least one selection."));continue}let h=g.split(",").map(w=>w.trim()),b=[],y=!0;for(let w of h){let x=parseInt(w,10);if(!isFinite(x)||String(x)!==w||x<1||x>p.length){o.line(m.warning(` Invalid selection "${le(w,32)}". Enter numbers between 1 and ${p.length}.`)),y=!1;break}b.push(p[x-1])}if(y)return{action:"accept",content:{value:b}}}}if(l==="number"){let p=e.min,f=e.max,g=p!==void 0&&f!==void 0?` [${p}\u2013${f}]`:p!==void 0?` [\u2265${p}]`:f!==void 0?` [\u2264${f}]`:"";for(;;){if(n.aborted)return se;let h;try{h=(await r(m.dim(` Enter a number${g}: `))).trim()}catch{return se}if(n.aborted||h===":cancel")return se;if(h===""&&e.allowSkip)return dr;if(h===""&&!e.allowSkip){o.line(m.warning(" Please enter a number (or :cancel to skip)."));continue}let b=Number(h);if(!isFinite(b)){o.line(m.warning(" Please enter a valid number."));continue}if(p!==void 0&&b<p){o.line(m.warning(` Value must be \u2265 ${p}.`));continue}if(f!==void 0&&b>f){o.line(m.warning(` Value must be \u2264 ${f}.`));continue}return{action:"accept",content:{value:b}}}}let u=e.minLength,d=e.maxLength;for(;;){if(n.aborted)return se;let p;try{p=(await r(m.dim(" > "))).trim()}catch{return se}if(n.aborted||p===":cancel")return se;if(p===""&&e.allowSkip)return dr;if(p===""){o.line(m.warning(" Please enter a response (or type :cancel to skip)."));continue}if(u!==void 0&&p.length<u){o.line(m.warning(` Response must be at least ${u} characters.`));continue}if(d!==void 0&&p.length>d){o.line(m.warning(` Response must be at most ${d} characters.`));continue}return{action:"accept",content:{value:p}}}}function rl(e){return async(t,{signal:n})=>{if(n.aborted)return _o;tl(process.stdout),e.suspendInput?.();try{if(t.origin==="agent")return await wF(t,e,n);if(t.mode==="form"){let o=t.requestedSchema,{properties:s,required:i,fieldsTruncated:a,originalFieldCount:l}=typeof o=="object"&&o!==null?fF(o):{properties:{},required:new Set,fieldsTruncated:!1,originalFieldCount:0};gF(e.writer,t),a&&e.writer.line(m.warning(` \u26A0 Schema has ${l} fields; only the first ${np} will be prompted (server may be malformed or compromised).`));let c=Object.create(null);if(Object.keys(s).length===0)return e.writer.line(m.warning(" \u26A0 Form schema has no usable fields \u2014 declining.")),_o;for(let u of i)if(!(u in s))return e.writer.line(m.warning(` \u26A0 Required field '${le(u,64)}' has no schema entry \u2014 declining.`)),_o;for(let[u,d]of Object.entries(s)){if(n.aborted)return se;let p=await hF(u,d,i.has(u),e.readLine,e.writer,n);if(p.tag==="cancel")return se;if(p.tag==="decline")return _o;p.value!==void 0&&!rp.has(u)&&(c[u]=p.value)}return{action:"accept",content:{...c}}}yF(e.writer,t);let r=(await e.readLine(m.dim("Continue? [y/N] "))).trim().toLowerCase();return r===""?se:r==="y"||r==="yes"?mF:_o}finally{e.resumeInput?.()}}}function TS(e){let t=Math.max(0,Math.min(1,e.ratio)),n=t>.8?m.error:t>.5?m.warning:m.meta,r=20,o=Math.round(t*r),s=r-o,i="["+"\u2588".repeat(o)+"\u2591".repeat(s)+"]",a=Math.round(t*100)+"%",l="";e.used!==void 0&&e.limit!==void 0&&(l=re(e.used)+"/"+re(e.limit));let c=l?`${i} ${a} ${l}`:`${i} ${a}`,u=`${i} ${a}`,d=`ctx ${a}`,p=e.sparkline?m.meta(e.sparkline)+" ":"",f=e.sparkline?z(e.sparkline)+1:0,g=Math.max(0,e.width-f);if(z(c)<=g&&g>90)return p+n(c);if(z(u)<=g&&g>32)return p+n(u);if(z(d)<=g)return p+n(d);if(e.width>0){let h=p+n(d);return ae(h,e.width)}return n(a)}var ol=class{stream;force;throttleMs;started=!1;lastRepaint=0;lastFields=null;resizeUnsub=null;lastPaintedRow=null;extraRows=0;afterScrollRestore=null;constructor(t={}){this.stream=t.stream??process.stdout,this.force=t.force??!1,this.throttleMs=t.throttleMs??100}get enabled(){return this.force||!!this.stream.isTTY}start(){if(this.started||!this.enabled)return;this.started=!0,this.lastRepaint=0;let t=this.currentRows();this.stream.write("\x1B[s"),this.writeScrollRegion(t),this.stream.write("\x1B[u"),this.resizeUnsub===null&&(this.resizeUnsub=Ue.subscribe(()=>{this.onResize()}))}onResize(){if(!this.started||!this.enabled)return;let t=this.currentRows();this.stream.write("\x1B[s"),this.lastPaintedRow!==null&&this.lastPaintedRow!==this.paintRow(t)&&(this.stream.write(`\x1B[${this.lastPaintedRow};1H`),this.stream.write("\x1B[2K")),this.writeScrollRegion(t),this.stream.write("\x1B[u"),this.flush()}repaint(t){if(!this.enabled||!this.started){this.lastFields=t;return}let n=Date.now();if(n-this.lastRepaint<this.throttleMs){this.lastFields=t;return}this.lastRepaint=n,this.lastFields=t;let r=this.currentRows();this.stream.write("\x1B[s"),this.stream.write(`\x1B[${this.paintRow(r)};1H`),this.stream.write("\x1B[2K"),this.stream.write(this.formatLine(t)),this.stream.write("\x1B[u"),this.lastPaintedRow=this.paintRow(r)}flush(){this.lastRepaint=0,this.lastFields&&this.repaint(this.lastFields)}setExtraRows(t){if(this.extraRows=t,this.started&&this.enabled){let n=this.currentRows();this.stream.write("\x1B[s"),this.writeScrollRegion(n),this.stream.write("\x1B[u"),this.flush()}}getExtraRows(){return this.extraRows}setAfterScrollRestore(t){this.afterScrollRestore=t}rearm(){if(!this.started||!this.enabled)return;let t=this.currentRows();this.stream.write("\x1B[s"),this.writeScrollRegion(t),this.stream.write("\x1B[u"),this.flush()}withFullScrollRegion(t){if(!this.started||!this.enabled)return t();this.stream.write("\x1B[s"),this.stream.write("\x1B[r"),this.stream.write("\x1B[u");try{return t()}finally{let n=this.currentRows();this.stream.write("\x1B[s"),this.writeScrollRegion(n),this.stream.write("\x1B[u"),this.flush(),this.afterScrollRestore?.()}}stop(){if(this.resizeUnsub!==null&&(this.resizeUnsub(),this.resizeUnsub=null),!this.started||!this.enabled){this.started=!1;return}let t=this.currentRows();this.stream.write("\x1B[s"),this.stream.write(`\x1B[${this.lastPaintedRow??this.paintRow(t)};1H`),this.stream.write("\x1B[2K"),this.stream.write("\x1B[r"),this.stream.write("\x1B[u"),this.started=!1,this.lastRepaint=0,this.lastPaintedRow=null}formatLine(t){let n=[],r=Math.max(4,(this.stream.columns??80)-2);if(t.cwd){let a=Math.max(8,Math.floor(r*.4)),l=js(t.cwd,{maxWidth:a});l&&n.push({text:m.dim(l)})}if(n.push({text:m.brand(t.model)}),t.planMode&&n.push({text:m.warning("\u25CF plan")}),t.contextPct!==void 0){let a=TS({ratio:t.contextPct,used:t.contextUsedTokens,limit:t.contextLimit,sparkline:t.contextSparkline,width:r});n.push({text:a,droppablePriority:1})}t.cost!==void 0&&n.push({text:m.meta(`$${t.cost.toFixed(2)}`),droppablePriority:2}),t.tokens!==void 0&&n.push({text:m.meta(`${SF(t.tokens)} tok`),droppablePriority:3});let o=m.dim(" \xB7 "),s=n.map(a=>a.text).join(o);if(z(s)<=r)return s;let i=n.filter(a=>a.droppablePriority!==void 0);for(;i.length>0&&z(s)>r;){let a=Math.max(...i.map(l=>l.droppablePriority));n=n.filter(l=>l.droppablePriority===void 0||l.droppablePriority!==a),s=n.map(l=>l.text).join(o),i=n.filter(l=>l.droppablePriority!==void 0)}return ae(s,r)}currentRows(){let t=this.stream.rows;return typeof t=="number"&&t>0?t:24}paintRow(t){return t>1?t:1}writeScrollRegion(t){let n=1+this.extraRows;if(t>n){this.stream.write(`\x1B[1;${t-n}r`);return}this.stream.write("\x1B[r")}};function SF(e){return e>=1e6?`${(e/1e6).toFixed(1)}M`:e>=1e3?`${(e/1e3).toFixed(1)}k`:`${e}`}var lt=new Map,pr=new Map;function ce(e){if(lt.has(e.name))throw new Error(`Slash command already registered: ${e.name}`);lt.set(e.name,e);for(let t of e.aliases??[]){if(pr.has(t)||lt.has(t))throw new Error(`Slash alias collides: ${t}`);pr.set(t,e.name)}}function Bn(e){if(lt.has(e.name)){for(let[t,n]of pr.entries())n===e.name&&pr.delete(t);lt.delete(e.name)}ce(e)}function xS(e){lt.has(e.name)||ce(e)}function ES(){lt.clear(),pr.clear()}function Ze(){return[...lt.values()].sort((e,t)=>e.name.localeCompare(t.name))}function RS(){let e=[];for(let[t,n]of pr.entries()){let r=lt.get(n);r&&e.push({alias:t,canonical:n,summary:r.summary})}return e.sort((t,n)=>t.alias.localeCompare(n.alias))}function kF(e){if(lt.has(e))return lt.get(e);let t=pr.get(e);return t?lt.get(t):void 0}function vF(e,t){let n=Array.from({length:e.length+1},()=>new Array(t.length+1).fill(0));for(let r=0;r<=e.length;r++)n[r][0]=r;for(let r=0;r<=t.length;r++)n[0][r]=r;for(let r=1;r<=e.length;r++)for(let o=1;o<=t.length;o++){let s=e[r-1]===t[o-1]?0:1;n[r][o]=Math.min(n[r-1][o]+1,n[r][o-1]+1,n[r-1][o-1]+s)}return n[e.length][t.length]}function TF(e,t=3){let n;for(let r of lt.keys()){let o=vF(e,r);o<=t&&(n===void 0||o<n.dist)&&(n={name:r,dist:o})}return n?.name}function sp(e){let t=e.trim();if(!t.startsWith("/"))return null;let n=t.indexOf(" ");return n===-1?{name:t,args:""}:{name:t.slice(0,n),args:t.slice(n+1).trim()}}async function AS(e,t,n){let r=sp(e);if(r===null)return{handled:!1};let o=kF(r.name);if(!o){let a=TF(r.name);return a?t.out.warn(`Unknown command: ${r.name} (did you mean ${a}?)`):t.out.warn(`Unknown command: ${r.name} (type /help for commands)`),{handled:!0,result:"continue"}}let s=n??[];s.length>0&&o.acceptsAttachments!==!0&&t.out.warn(`\u26A0 Image attachments are ignored by ${r.name} (images only reach the model on skill commands like /forge, /mint).`);let i=await o.handler(t,r.args,o.acceptsAttachments===!0?s:void 0);return i==="forward"?{handled:!1}:{handled:!0,result:i}}import AF from"ora";function _S(e,t=5){if(e.length===0)return"";let n=e.slice(-t),r=["\u2581","\u2582","\u2583","\u2584","\u2585","\u2586","\u2587","\u2588"];return n.map(o=>{let s=Math.max(0,Math.min(1,o)),i=Math.min(7,Math.floor(s*8));return r[i]}).join("")}function sl(e,t,n){e.totalTurns=t.totalTurns,e.totalCostUsd=t.totalCostUsd,e.totalTokens=t.totalTokens,e.totalDurationMs=t.totalDurationMs,e.turns=[...t.turns],e.sessionId=t.sessionId??n,e.model=t.model,e.sessionStartTime=t.startedAt??Date.now()}function il(e,t){let n=e.turns;if(n.length===0)return;let r=n[n.length-1];if(!r)return;let o=IS(CS(r.user),80),s=IS(RF(CS(r.assistant)),120);o.length>0&&t.fn(m.dim(` Last: ${o}`)),s.length>0&&t.fn(m.dim(` \u21B3 ${s}`)),t.fn(m.dim(" \u21AA /history for full review"))}function CS(e){return e.replace(EF,"").replace(/\s+/g," ").trim()}var EF=/\u001B\][\s\S]*?(?:\u0007|\u001B\\|\u009C)|\u001B[PX^_][\s\S]*?(?:\u0007|\u001B\\|\u009C)|\u001B[@-OQ-WY-Z\\`6-9=]|[\u001B\u009B][[\]()#;?]*(?:\d{1,4}(?:[;:]\d{0,4})*)?[\dA-PR-TZcf-nq-uy=><~]/g;function RF(e){let t=e.match(/^.*?(?<![A-Za-z]\.[A-Za-z])[.!?](?=\s|$)/);return t?t[0]:e}function IS(e,t){let n=[...e];return n.length<=t?e:n.slice(0,t-1).join("")+"\u2026"}var Ws={stream:process.stdout,hideCursor:!1,discardStdin:!1};function ip(e,t){if(t?.getRatio()!==void 0)return t.getRatio();let n=e.turnTokens[e.turnTokens.length-1];return n?(n.input+n.output+n.cache)/it(e.model):0}function mr(e,t){let n=ip(e,t),r=it(e.model),o,s=t?.getDetail();if(s!==void 0)o=s.used;else{let a=e.turnTokens[e.turnTokens.length-1];a&&(o=a.input+a.output+a.cache)}let i;if(e.turnTokens.length>=2){let a=e.turnTokens.map(c=>(c.input+c.output+c.cache)/r),l=_S(a,5);l.length>0&&(i=l)}return{model:e.model,cost:e.totalCostUsd,tokens:e.totalTokens,contextPct:n,contextLimit:r,contextUsedTokens:o,contextSparkline:i,planMode:e.planMode,...e.cwd!==void 0?{cwd:e.cwd}:{}}}var _F={name:"/exit",aliases:["/quit"],summary:"Exit the session",hint:"When you want to tear down the REPL \u2014 Ctrl+D on an empty prompt does the same.",async handler(){return"exit"}},CF={name:"/clear",summary:"Clear conversation history",hint:"When the current thread has drifted off-topic or you want a clean slate without restarting the session.",async handler(e){try{await e.session.current.reset(),e.ui.clearScreen(),pS(e.stats),e.ledger?.clear(),e.out.success("Conversation history cleared.")}catch(t){e.out.error(t instanceof Error?t.message:"Unknown error")}return"continue"}},IF={name:"/compact",summary:"Compact history (summarize older messages)",hint:"When context is filling up but you want to keep the thread \u2014 summarizes old turns and keeps the recent ones intact.",async handler(e){let t=AF({text:m.meta("Summarizing earlier turns..."),...Ws}).start();try{let n=await e.session.current.compact();if(t.stop(),n.compacted){let r=n.tokensSavedEstimate?` (~${n.tokensSavedEstimate} input tokens saved)`:"";e.out.success(`Compacted ${n.messagesBefore} \u2192 ${n.messagesAfter} messages${r}.`)}else{let r=n.reason??"unknown";r==="aborted"?e.out.info("Compaction cancelled."):r.startsWith("summarization-failed")?e.out.error(`Compaction failed: ${r}. History unchanged.`):r==="nothing-to-summarize"?e.out.info("Nothing to compact \u2014 all history is within the keep window."):r==="not-supported"?e.out.warn("Compaction is not supported for this model or provider \u2014 use a Claude model to enable /compact."):e.out.info(`Nothing to compact (${r}).`)}}catch(n){t.stop(),e.out.error(n instanceof Error?n.message:"Unknown error")}return"continue"}},PF={name:"/help",summary:"Show this help",hint:"When you want the full command list with usage strings \u2014 broader than this inline dropdown.",async handler(e){let t=Ze(),n=t.reduce((r,o)=>Math.max(r,o.name.length),0)+2;e.out.line(),e.out.line(m.bold(m.brand("Commands"))),e.out.line(he());for(let r of t){let o=r.usage??r.name,s=" ".repeat(Math.max(0,n-o.length));e.out.line(` ${m.warning(o)}${s} ${m.dim(r.summary)}`)}return e.out.line(),e.out.line(m.dim(" Tip: Ctrl+C interrupts a running turn; a second press exits.")),e.out.line(),"continue"}},PS=[_F,CF,IF,PF];function ap(e,t=30){return!e||e.length===0?m.dim("(none)"):e.length<=t?e.join(", "):`${e.slice(0,t).join(", ")}, ${m.dim(`+${e.length-t} more`)}`}function St(e,t){return` ${m.label(e.padEnd(16))} ${t}`}function al(e){let t=[];t.push(" "+he("Session Debug")),e.sessionId&&t.push(St("session",e.sessionId)),e.model&&t.push(St("model",e.model)),e.permissionMode&&t.push(St("permission",e.permissionMode)),e.cwd&&t.push(St("cwd",e.cwd)),e.claudeCodeVersion&&t.push(St("sdk",`v${e.claudeCodeVersion}`)),e.apiKeySource&&t.push(St("api key",e.apiKeySource)),e.outputStyle&&t.push(St("output style",e.outputStyle));let n=e.tools?.length??0;t.push(St(`tools (${n})`,ap(e.tools)));let r=e.mcpServers??[],o=r.length?r.map(c=>`${c.name}[${c.status}]`).join(", "):m.dim("(none)");t.push(St(`mcp (${r.length})`,o));let s=e.skills?.length??0;t.push(St(`skills (${s})`,ap(e.skills)));let i=e.plugins?.length??0,a=i?(e.plugins??[]).map(c=>c.name).join(", "):m.dim("(none)");t.push(St(`plugins (${i})`,a));let l=e.slashCommands?.length??0;return t.push(St(`slash (${l})`,ap(e.slashCommands))),t.push(" "+he()),t.join(`
2022
+ `)}var up=["opus","opus_1m","sonnet","sonnet_1m","haiku"],DF={name:"/cost",summary:"Show total and per-turn cost",hint:"When you want a dollar breakdown of this session \u2014 total, average per turn, and the recent turn-by-turn series.",async handler(e){let{stats:t,out:n}=e;if(n.line(),n.line(m.bold("Session cost")),n.line(he()),n.line(` total ${m.success(Ne(t.totalCostUsd))}`),n.line(` turns ${m.meta(String(t.totalTurns))}`),t.totalTurns>0){let r=t.totalCostUsd/t.totalTurns;n.line(` avg/turn ${m.meta(Ne(r))}`)}if(t.turnCosts.length>0){let r=t.turnCosts.slice(-5).map(Ne).join(m.dim(" \xB7 "));n.line(` last 5 ${r}`)}return n.line(),"continue"}};function LF(e,t){let n=t.apiUsage,r=n?.input_tokens??0,o=n?.output_tokens??0,s=n?.cache_read_input_tokens??0,i=n?.cache_creation_input_tokens??0,a=r+o+s+i;e.line(),e.line(m.bold("Token usage")+m.dim(" (SDK breakdown)")),e.line(he()),e.line(` total ${m.success(re(t.totalTokens))} of ${m.meta(re(t.maxTokens))} (${m.meta(`${Math.round(t.percentage*100)/100}%`)})`),t.autoCompactThreshold&&t.isAutoCompactEnabled&&e.line(` compact at ${m.meta(re(t.autoCompactThreshold))}`),e.line(),e.line(m.dim(" Last turn (API):")),e.line(` input ${m.meta(re(r))}`),e.line(` output ${m.meta(re(o))}`),e.line(` cache read ${m.meta(re(s))}`),e.line(` cache creat ${m.meta(re(i))}`),e.line(` total ${m.meta(re(a))}`);let l=t.categories??[];if(l.length>0){let p=[...l].sort((f,g)=>g.tokens-f.tokens).slice(0,5);e.line(),e.line(m.dim(" Top categories:"));for(let f of p)e.line(` ${m.warning(f.name.padEnd(18))} ${m.meta(re(f.tokens))}`)}let c=t.systemTools??[],u=t.mcpTools??[];if(c.length>0||u.length>0){e.line();let p=c.reduce((g,h)=>g+h.tokens,0),f=u.reduce((g,h)=>g+h.tokens,0);c.length>0&&e.line(m.dim(` system tools ${c.length} tools, ${re(p)} tokens`)),u.length>0&&e.line(m.dim(` MCP tools ${u.length} tools, ${re(f)} tokens`))}let d=t.agents??[];if(d.length>0){let p=d.reduce((f,g)=>f+g.tokens,0);e.line(m.dim(` agents ${d.length} loaded, ${re(p)} tokens`))}if(t.skills){let p=t.skills;e.line(m.dim(` skills ${p.includedSkills}/${p.totalSkills} included, ${re(p.tokens)} tokens`))}if(t.slashCommands){let p=t.slashCommands;e.line(m.dim(` slash cmds ${p.includedCommands}/${p.totalCommands} included, ${re(p.tokens)} tokens`))}e.line()}function FF(e,t){let n=t.turnTokens.reduce((c,u)=>c+u.input,0),r=t.turnTokens.reduce((c,u)=>c+u.output,0),o=t.turnTokens.reduce((c,u)=>c+u.cache,0),s=n+r,i=it(t.model),a=n+r+o,l=Math.round(a/i*100);e.line(),e.line(m.bold("Token usage")+m.dim(" (local stats \u2014 SDK breakdown unavailable)")),e.line(he()),e.line(` input ${m.meta(re(n))}`),e.line(` output ${m.meta(re(r))}`),e.line(` cache read ${m.meta(re(o))}`),e.line(` total ${m.success(re(s))}`),e.line(` context ${m.meta(`${l}% of ${re(i)} (${t.model})`)}`),e.line()}var NF={name:"/tokens",aliases:["/ctx"],summary:"Show token usage (SDK breakdown with local-stats fallback)",hint:"When you want to know how full the context window is \u2014 input/output/cache breakdown plus % of the model's limit used.",async handler(e){try{let t=await e.session.current.getContextUsage();LF(e.out,t)}catch{FF(e.out,e.stats)}return"continue"}},BF={name:"/history",summary:"Show conversation history",async handler(e){let{stats:t,out:n}=e;return t.turns.length===0?(n.info("No turns yet in this session."),"continue"):(n.line(),n.line(m.bold(`Session history (${t.turns.length} turn${t.turns.length===1?"":"s"})`)),n.line(he()),t.turns.forEach((r,o)=>{let s=m.meta(`#${o+1}`),i=r.user.length>100?r.user.slice(0,97)+"...":r.user,a=r.assistant.length>100?r.assistant.slice(0,97)+"...":r.assistant;n.line(` ${s} ${m.user("\u25B6")} ${i}`),n.line(` ${m.brand("\u25C6")} ${m.dim(a)}`)}),n.line(),"continue")}},UF={name:"/reset",summary:"Clear screen, history, and session stats",async handler(e){let{stats:t,ui:n,out:r}=e;try{await e.session.current.sendMessage("/clear")}catch{}return t.totalTurns=0,t.totalCostUsd=0,t.totalTokens=0,t.totalDurationMs=0,t.turnCosts.length=0,t.turnTokens.length=0,t.turns.length=0,t.sessionStartTime=Date.now(),n.clearScreen(),r.success("Session reset."),"continue"}},jF={name:"/model",usage:"/model <opus|opus_1m|sonnet|sonnet_1m|haiku|org/model>",summary:"Switch the active model mid-session",hint:"When you want to upgrade to opus for a hard problem or downshift to haiku for cheap iteration \u2014 context carries over. Also accepts full HuggingFace-style IDs (e.g. mlx-community/Qwen3-30B-A3B-4bit).",async handler(e,t){let n=t.trim().toLowerCase();if(!n)return e.out.info(`Current model: ${m.brand(e.stats.model)}`),e.out.line(m.dim(` Aliases: ${up.join(", ")} (or any org/model HF id)`)),"continue";let r=up.includes(n),o=xe(n)==="openai-compatible";if(!r&&!o)return e.out.warn(`Unknown model: ${n}. Aliases: ${up.join(", ")} (or org/model for local/OpenAI-compatible)`),"continue";try{await e.session.current.setModel(n),e.stats.model=n,e.ui.repaintStatusLine(),e.out.success(`Model switched to ${m.brand(n)}`)}catch(s){e.out.error(`Failed to switch model: ${s instanceof Error?s.message:String(s)}`)}return"continue"}},WF={name:"/tools",summary:"List tools available to the session",async handler(e){try{let n=(await e.session.current.waitForInitialization()).tools??[];if(n.length===0)return e.out.info("No tools reported by the session."),"continue";e.out.line(),e.out.line(m.bold(`Tools (${n.length})`)),e.out.line(he());let r=3,o=Math.ceil(n.length/r),s=Math.max(...n.map(i=>i.length))+2;for(let i=0;i<o;i++){let a=[];for(let l=0;l<r;l++){let c=l*o+i;c<n.length&&a.push(n[c].padEnd(s))}e.out.line(" "+a.join(""))}e.out.line()}catch(t){e.out.error(`Could not read session tools: ${t instanceof Error?t.message:String(t)}`)}return"continue"}},HF={name:"/mcp",summary:'List MCP servers connected to the session ("/mcp auth" to surface pending OAuth URLs)',async handler(e,t){let n=(t??"").trim().toLowerCase();if(n==="auth"){try{let{readOauthPending:r}=await Promise.resolve().then(()=>(ul(),BS)),o=r();if(Object.keys(o).length===0)return e.out.info("No MCP servers are waiting for OAuth."),"continue";e.out.line(),e.out.line(m.bold(`MCP OAuth pending (${Object.keys(o).length})`)),e.out.line(he());for(let[s,i]of Object.entries(o)){let a=Date.now()-i.timestamp,l=Math.round(a/6e4);e.out.line(` ${m.warning("\u25CF")} ${s} ${m.dim(`(${l}m ago)`)}`),e.out.line(` ${m.info(i.authorizationUrl)}`)}e.out.line(),e.out.line(m.dim(" Open each URL in a browser. After authorizing, paste the code with:")),e.out.line(m.dim(" /mcp auth complete <serverName> <code>")),e.out.line()}catch(r){e.out.error(`Could not read OAuth state: ${r instanceof Error?r.message:String(r)}`)}return"continue"}if(n.startsWith("auth complete ")){let r=(t??"").trim(),o=r.slice(r.toLowerCase().indexOf("auth complete ")+14).trim(),s=o.indexOf(" ");if(s===-1||o.slice(0,s).length===0||o.slice(s+1).trim().length===0)return e.out.error("Usage: /mcp auth complete <serverName> <code>"),"continue";let i=o.slice(0,s).trim(),a=o.slice(s+1).trim();if(!e.mcpManager)return e.out.error("No MCP manager available in this session. Make sure an mcp.json config is present and at least one server is enabled."),"continue";try{e.out.info(`Completing OAuth for "${i}"\u2026`),await e.mcpManager.completeAuth(i,a),e.out.success(`OAuth complete for "${i}" \u2014 server is now connected.`)}catch(l){e.out.error(`OAuth completion failed for "${i}": ${l instanceof Error?l.message:String(l)}`)}return"continue"}if(n!==""&&n!=="auth")return e.out.error(`Unknown /mcp subcommand: "${n}". Try: /mcp, /mcp auth, /mcp auth complete <server> <code>.`),"continue";try{let o=(await e.session.current.waitForInitialization()).mcpServers??[];if(o.length===0)return e.out.info("No MCP servers connected."),"continue";e.out.line(),e.out.line(m.bold(`MCP servers (${o.length})`)),e.out.line(he());let s=0;for(let i of o){let a=typeof i=="string"?i:i.name??JSON.stringify(i),l=typeof i=="object"&&i!==null&&"status"in i?String(i.status):"",c=l==="connected"?m.success("\u25CF"):m.warning("\u25CF");e.out.line(` ${c} ${a}${l?m.dim(` (${l})`):""}`),l==="oauth_pending"&&s++}e.out.line(),s>0&&(e.out.line(m.dim(` ${s} server(s) need OAuth \u2014 run "/mcp auth" to see authorization URLs.`)),e.out.line())}catch(r){e.out.error(`Could not read MCP servers: ${r instanceof Error?r.message:String(r)}`)}return"continue"}},KF={name:"/limits",summary:"Show known per-model context-window limits",async handler(e){e.out.line(),e.out.line(m.bold("Context-window limits")),e.out.line(he());for(let[t,n]of Object.entries(Es)){let r=t===e.stats.model?m.brand(" \u2190 active"):"";e.out.line(` ${m.warning(t.padEnd(12))} ${m.meta(re(n))}${r}`)}return e.out.line(),"continue"}},GF={name:"/debug",summary:"Show SDK session metadata (tools, MCP, skills, plugins, etc.)",hint:"When something feels broken and you want to inspect what the session actually loaded \u2014 tools, MCP servers, plugins, system prompt source.",async handler(e){try{let t=await e.session.current.waitForInitialization();e.out.line(),e.out.line(al(t)),e.out.line()}catch(t){e.out.error(`Could not read session metadata: ${t instanceof Error?t.message:String(t)}`)}return"continue"}},US=[DF,NF,BF,UF,jF,WF,HF,KF,GF];var dp=!1;async function Ct(e,t,n={}){let r=e.stats.planMode,o=t!==void 0?t:!r;try{if(await e.session.current.setPermissionMode(o?"plan":"default"),e.stats.planMode=o,e.ui.repaintStatusLine(),o){let s=dp?"":m.dim(" Shift+Tab or /plan to exit.");dp||(dp=!0),e.out.success(m.warning("\u25CF plan mode ON")+m.dim(" \u2014 write_file, edit_file, and write-intent bash are refused.")+s)}else n.closureSummarySkipped?e.out.success(m.success("\u25CB plan mode OFF")+m.dim(" \u2014 force-exit (closure summary skipped). Default permissions restored.")):e.out.success(m.success("\u25CB plan mode OFF")+m.dim(" \u2014 default permissions restored"))}catch(s){e.out.error(`Could not toggle plan mode: ${s instanceof Error?s.message:String(s)}`)}}async function jS(e){e.stats.pendingPlanExit&&(await Ct(e,!1),e.stats.planMode||(e.stats.pendingPlanExit=!1))}var qF=["You are about to exit plan mode. Before I flip permissions back to default on the next turn, emit your final plan in three sections:",""," - **Chosen approach** \u2014 the plan you recommend, in one to three sentences."," - **Risks named** \u2014 the concrete failure modes, constraints, or unknowns this plan does not eliminate."," - **Alternatives considered** \u2014 the options you weighed and why you rejected them.","","This is the record. Be specific. Do not propose write actions in this turn \u2014 writes are still refused until the mode flips."].join(`
2023
+ `);async function zF(e){return e.stats.pendingPlanExit=!0,e.ui.repaintStatusLine(),e.out.success(m.warning("\u25CF plan exit queued")+m.dim(" \u2014 plan mode still ON; writes still refused. Submitting closure summary (chosen approach, risks, alternatives); mode flips after the model responds. Force-exit now: /plan off again or Shift+Tab.")),{kind:"submit",message:qF}}async function JF(e){return e.stats.pendingPlanExit=!1,await Ct(e,!1,{closureSummarySkipped:!0}),"continue"}async function VF(e){return e.stats.pendingPlanExit=!1,e.ui.repaintStatusLine(),e.out.success(m.warning("\u25CF plan mode ON")+m.dim(" \u2014 plan exit cancelled, staying in plan mode.")),"continue"}function YF(e){e.out.info(m.dim("(plan exit cancelled \u2014 submitting your prompt instead.)"))}var WS={name:"/plan",usage:"/plan [on|off|<prompt>]",summary:"Toggle plan mode (write_file, edit_file, and write-intent bash refused)",hint:"When you want the model to think through an approach without touching files \u2014 refuses writes until you flip back. Shift+Tab toggles too.",async handler(e,t){let n=t.trim(),r=n.toLowerCase();if(n!==""&&r!=="on"&&r!=="off"){let s=!!e.stats.pendingPlanExit;return e.stats.planMode||await Ct(e,!0),s&&(e.stats.pendingPlanExit=!1,YF(e)),{kind:"submit",message:n}}return(r==="on"?!0:r==="off"?!1:!e.stats.planMode)===!0?e.stats.pendingPlanExit?VF(e):(e.stats.planMode||await Ct(e,!0),"continue"):e.stats.planMode?e.stats.pendingPlanExit?JF(e):zF(e):(await Ct(e,!1),"continue")}};W();import{readFileSync as XF,writeFileSync as ZF,existsSync as HS,mkdirSync as QF}from"fs";import{join as eN}from"path";function KS(){return Hf(),au()}function GS(e){return eN(KS(),`${e}.json`)}function dl(e){let t=GS(e);if(!HS(t))return{sessionId:e,items:[]};try{let n=XF(t,"utf-8"),r=JSON.parse(n);return Array.isArray(r.items)?r:{sessionId:e,items:[]}}catch{return{sessionId:e,items:[]}}}function Hs(e){let t=KS();HS(t)||QF(t,{recursive:!0}),ZF(GS(e.sessionId),JSON.stringify(e,null,2))}function qS(e,t){let r={id:e.items.reduce((o,s)=>Math.max(o,s.id),0)+1,text:t,done:!1,createdAt:Date.now()};return e.items.push(r),r}function zS(e,t){let n=e.items.find(r=>r.id===t);return n&&(n.done=!0),n}function JS(e,t){let n=e.items.findIndex(r=>r.id===t);return n===-1?!1:(e.items.splice(n,1),!0)}function VS(e){e.items.length=0}function pl(e){if(e.items.length===0)return[];let t=[],n=Math.max(20,Y());{let r=m.dim("\u250C\u2500 todos "),o=Math.max(0,Math.min(n-10,120));t.push(r+m.dim("\u2500".repeat(o)))}for(let r of e.items){let o=r.done?m.success("[x]"):m.dim("[ ]"),s=r.done?m.dim(r.text):En(r.text),a=` ${m.meta(`#${r.id}`)} ${o} `,l=Math.max(8,n-z(a)),c=me(s,l).split(`
2024
+ `);t.push(a+(c[0]??""));let u=" ".repeat(z(a));for(let d of c.slice(1))t.push(u+d)}{let r=Math.max(0,Math.min(n-1,120));t.push(m.dim("\u2514"+"\u2500".repeat(r)))}return t}function YS(e){return e.items.length===0?"":e.items.map(t=>`${t.id}:${t.done?1:0}:${t.text}`).join(`
2025
+ `)}function XS(e){let t=e.stats.sessionId??"unbound";return dl(t)}function tN(e){let t=XS(e),n=pl(t);if(n.length===0){e.out.info("No todos yet. Try /todo add buy milk");return}for(let r of n)e.out.line(r)}var ZS={name:"/todo",usage:"/todo [add|done|rm|clear|list] ...",summary:"Manage this session's todo list",hint:"When you want a durable checklist the model and you both see \u2014 survives across turns and shows above each prompt.",async handler(e,t){let n=t.trim();if(!n||n==="list")return tN(e),"continue";let[r,...o]=n.split(/\s+/),s=o.join(" "),i=XS(e);switch(r){case"add":{if(!s)return e.out.warn("Usage: /todo add <text>"),"continue";let a=qS(i,s);return Hs(i),e.out.success(`Added ${m.meta(`#${a.id}`)} ${a.text}`),"continue"}case"done":{let a=parseInt(s,10);if(!Number.isFinite(a))return e.out.warn("Usage: /todo done <id>"),"continue";let l=zS(i,a);return l?(Hs(i),e.out.success(`Marked done ${m.meta(`#${a}`)} ${l.text}`)):e.out.warn(`No todo with id ${a}`),"continue"}case"rm":case"remove":{let a=parseInt(s,10);return Number.isFinite(a)?(JS(i,a)?(Hs(i),e.out.success(`Removed ${m.meta(`#${a}`)}`)):e.out.warn(`No todo with id ${a}`),"continue"):(e.out.warn("Usage: /todo rm <id>"),"continue")}case"clear":return VS(i),Hs(i),e.out.success("All todos cleared."),"continue";default:return e.out.warn(`Unknown subcommand: ${r}. Try /todo add, done, rm, clear, list.`),"continue"}}};var QS={name:"/save",usage:"/save [name]",hint:"When you want to checkpoint this conversation so /resume can pick it back up in a future session.",summary:"Save this session to disk (for /resume)",async handler(e,t){if(e.stats.totalTurns===0)return e.out.warn("Nothing to save \u2014 no turns in this session yet."),"continue";try{let n=t.trim()||void 0,r=To(e.stats,n);e.out.success(m.success("Saved")+m.dim(` ${r}`));let o=e.stats.sessionId??n;o&&e.out.line(m.dim(` Resume: ${Ao(o,e.stats.model)}`))}catch(n){e.out.error(`Could not save: ${n instanceof Error?n.message:String(n)}`)}return"continue"}};function nN(e){if(typeof e!="number"||!Number.isFinite(e))return" \u2014 ";let t=new Date(e);return Number.isNaN(t.getTime())?" \u2014 ":t.toISOString().replace("T"," ").slice(0,16)}function rN(e){return{id:e.id,resumeId:e.data.sessionId??e.id,stored:e.data}}var ek={name:"/resume",usage:"/resume [id]",hint:"When you want to continue a previously /saved session \u2014 runs interactively to pick one if no id is given.",summary:"List saved sessions, or swap the active session for a stored one",async handler(e,t){let n=t.trim();if(n){let o=xo(n);if(!o)return e.out.warn(`No saved session: ${n}`),"continue";if(typeof e.requestResume=="function"){let a=e.session.current.sessionId,l=o.data.sessionId;if(a!==void 0&&l!==void 0&&a===l||a!==void 0&&a===o.id)return e.out.warn(`Already on session ${o.id}.`),"continue";e.out.info(`Resuming session ${o.id} \u2026`);let u=await e.requestResume(rN(o));return u.ok?e.out.success(`Resumed ${o.id} (sdk id: ${u.sessionId})`):e.out.warn(u.reason),"continue"}let s=o.data,i=s.sessionId??o.id;return e.out.line(),e.out.line(m.bold(`Session ${o.id}`)),e.out.line(he()),e.out.line(` model ${m.brand(s.model)}`),e.out.line(` turns ${m.meta(String(s.totalTurns))}`),e.out.line(` cost ${m.meta(Ne(s.totalCostUsd))}`),e.out.line(` sdk id ${m.meta(s.sessionId??"\u2014")}`),e.out.line(),e.out.line(m.dim(" Resume with:")),e.out.line(m.brand(` ${Ao(i,s.model)}`)),e.out.line(),"continue"}let r=Us();if(r.length===0)return e.out.info("No saved sessions found. Use /save first."),"continue";e.out.line(),e.out.line(m.bold(`Saved sessions (${r.length})`)),e.out.line(he());for(let o of r.slice(0,20)){let s=nN(o.savedAt),i=m.brand(o.model.padEnd(7)),a=m.meta(`${o.totalTurns} turn${o.totalTurns===1?"":"s"}`.padEnd(9)),l=m.meta(Ne(o.totalCostUsd).padStart(8)),c=m.warning(o.id);e.out.line(` ${s} ${i} ${a} ${l} ${c}`)}return e.out.line(),e.out.line(m.dim(" Resume with: /resume <id>")),e.out.line(),"continue"}};import{existsSync as uN,readFileSync as dN}from"node:fs";import{resolve as sk}from"node:path";import{execFileSync as tk}from"node:child_process";import{readFileSync as oN,writeFileSync as sN}from"node:fs";import{resolve as iN}from"node:path";var aN={feat:"Added",fix:"Fixed",refactor:"Changed",perf:"Changed",docs:"Changed",chore:"Changed",ci:"Changed",test:"Changed","test+fix":"Fixed",build:"Changed",style:"Changed"},lN=["Added","Fixed","Changed","Deprecated","Removed","Security"];function nk(e){let t;try{t=tk("git",["describe","--tags","--abbrev=0"],{cwd:e,encoding:"utf8",stdio:["ignore","pipe","ignore"]}).trim()}catch{t=""}let n=t?["log",`${t}..HEAD`,"--format=%h %s"]:["log","-50","--format=%h %s"],r=tk("git",n,{cwd:e,encoding:"utf8"}).trim();return r?r.split(`
2026
2026
  `).map(o=>{let s=o.indexOf(" "),i=o.slice(0,s),a=o.slice(s+1),l=a.match(/^(\w+(?:\+\w+)?)(?:\([^)]*\))?!?:\s*(.+)/);if(l){let c=l[1].toLowerCase(),u=aN[c]??"Changed";return{hash:i,subject:l[2],category:u}}return{hash:i,subject:a,category:"Changed"}}):[]}function rk(e,t={}){let n=t.includeHash??!1,r=new Map;for(let s of e){let i=r.get(s.category)??[];i.push(s),r.set(s.category,i)}let o=[];for(let s of lN){let i=r.get(s);if(i?.length){o.push(`### ${s}`);for(let a of i)o.push(n?`- ${a.subject} (${a.hash})`:`- ${a.subject}`);o.push("")}}return o.join(`
2027
2027
  `)}function pp(e){return e.match(/## \[Unreleased\][^\n]*\n([\s\S]*?)(?=\n## \[|$)/)?.[1]?.trim()??""}function cN(e){let t=new Set;for(let n of e.split(`
2028
2028
  `)){let r=n.match(/\(([0-9a-f]{7,})\)\s*$/);r?.[1]&&t.add(r[1])}return t}function ok(e,t){let n=iN(e,"CHANGELOG.md"),r=oN(n,"utf8"),o=pp(r),s=cN(o),i=t.split(`
@@ -2034,15 +2034,15 @@ Run \`afk i\` then \`/resume\` to list saved sessions.
2034
2034
  `+i:o||i,l=r.replace(/## \[Unreleased\][^\n]*\n[\s\S]*?(?=\n## \[|$)/,`## [Unreleased]
2035
2035
 
2036
2036
  ${a}
2037
- `);if(l===r)throw new Error("No ## [Unreleased] section found");sN(n,l)}var ik={name:"/changelog",usage:"/changelog [--dry-run]",summary:"Generate CHANGELOG entries from commits since last tag",async handler(e,t){let n=t.split(/\s+/).includes("--dry-run"),r=process.cwd(),o;try{o=nk(r)}catch(a){return e.out.error(`Failed to read git log: ${a instanceof Error?a.message:String(a)}`),"continue"}if(o.length===0)return e.out.warn("No commits found since the last tag."),"continue";let s=rk(o),i=(()=>{try{let a=dN(sk(r,"CHANGELOG.md"),"utf8");return pp(a)}catch{return""}})();e.out.line(),e.out.info(`${o.length} commits since last tag`),e.out.line(),i&&(e.out.warn("Existing [Unreleased] entries will be preserved above new entries."),e.out.line()),e.out.line(p.heading("Preview:")),e.out.line(p.dim("\u2500".repeat(60)));for(let a of s.split(`
2038
- `))a.startsWith("### ")?e.out.line(p.bold(a)):a.startsWith("- ")?e.out.line(p.dim(" ")+a):e.out.line(a);if(e.out.line(p.dim("\u2500".repeat(60))),n)return e.out.info("Dry run \u2014 CHANGELOG.md not modified."),"continue";try{if(!uN(sk(r,"CHANGELOG.md")))return e.out.error("CHANGELOG.md not found. Create one with a ## [Unreleased] section first."),"continue";ok(r,s),e.out.success("Wrote entries to CHANGELOG.md [Unreleased] section."),e.out.line(p.dim(" Review with: git diff CHANGELOG.md"))}catch(a){e.out.error(`Failed to update CHANGELOG.md: ${a instanceof Error?a.message:String(a)}`)}return"continue"}};import{EventEmitter as pN}from"node:events";var ml=class extends pN{tasks=new Map;counter=0;register(t){let n=`bg-${++this.counter}`,r={id:n,label:t,startedAt:Date.now(),status:"running",stats:{tokens:0,toolUses:0,durationMs:0}};return this.tasks.set(n,r),this.emit("update",r),r}updateStats(t,n,r){let o=this.tasks.get(t);!o||o.status!=="running"||(n.tokens!==void 0&&(o.stats.tokens=n.tokens),n.toolUses!==void 0&&(o.stats.toolUses=n.toolUses),o.stats.durationMs=Date.now()-o.startedAt,r!==void 0&&(o.progressDescription=r),this.emit("update",o))}complete(t,n,r){let o=this.tasks.get(t);!o||o.status!=="running"||(o.status="succeeded",o.resultText=n,o.resultMeta=r,o.stats.durationMs=Date.now()-o.startedAt,this.emit("update",o),this.emit("complete",o))}fail(t,n){let r=this.tasks.get(t);!r||r.status!=="running"||(r.status="failed",r.error=n,r.stats.durationMs=Date.now()-r.startedAt,this.emit("update",r),this.emit("complete",r))}cancel(t){let n=this.tasks.get(t);!n||n.status!=="running"||(n.status="cancelled",n.stats.durationMs=Date.now()-n.startedAt,this.emit("update",n),this.emit("complete",n))}running(){return[...this.tasks.values()].filter(t=>t.status==="running")}all(){return[...this.tasks.values()]}get(t){return this.tasks.get(t)}};function fl(e,t){return(n,r)=>{n.type==="progress"&&t.updateStats(e.id,{tokens:n.progress.totalTokens,toolUses:n.progress.toolUses},n.progress.description)}}function gl(e,t,n,r,o,s,i,a,l){(async()=>{let c=t,u,d=[],m=new Map,f=()=>{o.cancel(r.id)};l?.addEventListener("abort",f,{once:!0});try{for await(let g of e){if(l?.aborted){o.cancel(r.id);return}if(s(g,{subagentId:"__main__"}),g.type==="chunk"&&g.chunk.type==="content")c+=g.chunk.content;else if(g.type==="chunk"&&g.chunk.type==="tool_use_detail"){let h=g.chunk,b={toolName:h.toolName,toolUseId:h.toolUseId,input:h.toolInput};m.set(h.toolUseId,b),d.push(b)}else if(g.type==="chunk"&&g.chunk.type==="tool_result"){let h=g.chunk,b=m.get(h.toolUseId);b&&(b.result=h.content,b.isError=h.isError,m.delete(h.toolUseId))}if(g.type==="done"){u=g.metadata;break}if(g.type==="error"){o.fail(r.id,g.error);return}}o.complete(r.id,c,u),i&&u&&lr(i,n,c,u,d),a&&await a(n,c).catch(()=>{})}catch(g){o.fail(r.id,g instanceof Error?g:new Error(String(g)))}finally{l?.removeEventListener("abort",f)}})()}var Ks;function ak(e){Ks=e}var lk={name:"/bg",summary:"Start a turn in the background",usage:"/bg <prompt>",hint:"When you want a long task to run while you keep typing \u2014 completion lands in the next prompt as a notification card.",async handler(e,t){let n=t.trim();if(!n)return e.out.info("Usage: /bg <prompt>"),"continue";if(!Ks)return e.out.error("Background tasks not available in this session."),"continue";let r=n.slice(0,40),o=Ks.register(r),s=fl(o,Ks),i=e.session.current.sendMessageStream(n);return gl(i,"",n,o,Ks,s,e.stats,void 0,e.session.current.abortSignal),e.out.line(p.dim(` \u2192 backgrounded as ${o.id}: ${r}`)),"continue"}};var mp,fp;function ck(e){mp=e}function uk(e){fp=e}var dk={running:"\u25D0",completed:"\u2713",failed:"\u2717",cancelled:"\u2298"};function hl(e){return mp||(e.out.error("Background subagent jobs are not available in this session."),null)}var gp=/\x1b\[[0-9;]*[a-zA-Z]/g;function mN(e,t=120){let n=e.replace(gp,"").replace(/[\r\n]+/g," ").trim();return n.length>t?`${n.slice(0,t)}\u2026`:n}function fN(e){let t=dk[e.status],n=e.endedAt!==void 0?ne(e.endedAt-e.startedAt):ne(Date.now()-e.startedAt),r=e.label.length>60?`${e.label.slice(0,60)}\u2026`:e.label,o=` ${t} ${p.bold(e.jobId)} ${r} ${p.dim(`(${e.status} \xB7 ${n} \xB7 ${e.model})`)}`;if(!fp||e.status!=="running")return o;let s=fp.getSummary(e.jobId);if(!s)return o;let i=process.stdout.columns??120,a=mN(s.text,Math.max(40,i-10)),l=Math.round((Date.now()-s.refreshedAt)/1e3),c=s.stale?" [stale]":"",u=p.dim(` \u21B3 ${a} (${l}s ago)${c}`);return`${o}
2039
- ${u}`}function hp(e,t){let n=dk[t.status];e.out.line(` ${n} ${p.bold(t.jobId)} ${t.label}`);let r=t.endedAt!==void 0?ne(t.endedAt-t.startedAt):ne(Date.now()-t.startedAt);e.out.line(` Status: ${t.status} \xB7 ${r}`),e.out.line(` Subagent: ${t.subagentId}`),e.out.line(` Model: ${t.model}`);let o=t.result;if(o?.message?.content){e.out.line("");let s=typeof o.message.content=="string"?o.message.content:JSON.stringify(o.message.content),i=s.split(`
2037
+ `);if(l===r)throw new Error("No ## [Unreleased] section found");sN(n,l)}var ik={name:"/changelog",usage:"/changelog [--dry-run]",summary:"Generate CHANGELOG entries from commits since last tag",async handler(e,t){let n=t.split(/\s+/).includes("--dry-run"),r=process.cwd(),o;try{o=nk(r)}catch(a){return e.out.error(`Failed to read git log: ${a instanceof Error?a.message:String(a)}`),"continue"}if(o.length===0)return e.out.warn("No commits found since the last tag."),"continue";let s=rk(o),i=(()=>{try{let a=dN(sk(r,"CHANGELOG.md"),"utf8");return pp(a)}catch{return""}})();e.out.line(),e.out.info(`${o.length} commits since last tag`),e.out.line(),i&&(e.out.warn("Existing [Unreleased] entries will be preserved above new entries."),e.out.line()),e.out.line(m.heading("Preview:")),e.out.line(m.dim("\u2500".repeat(60)));for(let a of s.split(`
2038
+ `))a.startsWith("### ")?e.out.line(m.bold(a)):a.startsWith("- ")?e.out.line(m.dim(" ")+a):e.out.line(a);if(e.out.line(m.dim("\u2500".repeat(60))),n)return e.out.info("Dry run \u2014 CHANGELOG.md not modified."),"continue";try{if(!uN(sk(r,"CHANGELOG.md")))return e.out.error("CHANGELOG.md not found. Create one with a ## [Unreleased] section first."),"continue";ok(r,s),e.out.success("Wrote entries to CHANGELOG.md [Unreleased] section."),e.out.line(m.dim(" Review with: git diff CHANGELOG.md"))}catch(a){e.out.error(`Failed to update CHANGELOG.md: ${a instanceof Error?a.message:String(a)}`)}return"continue"}};import{EventEmitter as pN}from"node:events";var ml=class extends pN{tasks=new Map;counter=0;register(t){let n=`bg-${++this.counter}`,r={id:n,label:t,startedAt:Date.now(),status:"running",stats:{tokens:0,toolUses:0,durationMs:0}};return this.tasks.set(n,r),this.emit("update",r),r}updateStats(t,n,r){let o=this.tasks.get(t);!o||o.status!=="running"||(n.tokens!==void 0&&(o.stats.tokens=n.tokens),n.toolUses!==void 0&&(o.stats.toolUses=n.toolUses),o.stats.durationMs=Date.now()-o.startedAt,r!==void 0&&(o.progressDescription=r),this.emit("update",o))}complete(t,n,r){let o=this.tasks.get(t);!o||o.status!=="running"||(o.status="succeeded",o.resultText=n,o.resultMeta=r,o.stats.durationMs=Date.now()-o.startedAt,this.emit("update",o),this.emit("complete",o))}fail(t,n){let r=this.tasks.get(t);!r||r.status!=="running"||(r.status="failed",r.error=n,r.stats.durationMs=Date.now()-r.startedAt,this.emit("update",r),this.emit("complete",r))}cancel(t){let n=this.tasks.get(t);!n||n.status!=="running"||(n.status="cancelled",n.stats.durationMs=Date.now()-n.startedAt,this.emit("update",n),this.emit("complete",n))}running(){return[...this.tasks.values()].filter(t=>t.status==="running")}all(){return[...this.tasks.values()]}get(t){return this.tasks.get(t)}};function fl(e,t){return(n,r)=>{n.type==="progress"&&t.updateStats(e.id,{tokens:n.progress.totalTokens,toolUses:n.progress.toolUses},n.progress.description)}}function gl(e,t,n,r,o,s,i,a,l){(async()=>{let c=t,u,d=[],p=new Map,f=()=>{o.cancel(r.id)};l?.addEventListener("abort",f,{once:!0});try{for await(let g of e){if(l?.aborted){o.cancel(r.id);return}if(s(g,{subagentId:"__main__"}),g.type==="chunk"&&g.chunk.type==="content")c+=g.chunk.content;else if(g.type==="chunk"&&g.chunk.type==="tool_use_detail"){let h=g.chunk,b={toolName:h.toolName,toolUseId:h.toolUseId,input:h.toolInput};p.set(h.toolUseId,b),d.push(b)}else if(g.type==="chunk"&&g.chunk.type==="tool_result"){let h=g.chunk,b=p.get(h.toolUseId);b&&(b.result=h.content,b.isError=h.isError,p.delete(h.toolUseId))}if(g.type==="done"){u=g.metadata;break}if(g.type==="error"){o.fail(r.id,g.error);return}}o.complete(r.id,c,u),i&&u&&lr(i,n,c,u,d),a&&await a(n,c).catch(()=>{})}catch(g){o.fail(r.id,g instanceof Error?g:new Error(String(g)))}finally{l?.removeEventListener("abort",f)}})()}var Ks;function ak(e){Ks=e}var lk={name:"/bg",summary:"Start a turn in the background",usage:"/bg <prompt>",hint:"When you want a long task to run while you keep typing \u2014 completion lands in the next prompt as a notification card.",async handler(e,t){let n=t.trim();if(!n)return e.out.info("Usage: /bg <prompt>"),"continue";if(!Ks)return e.out.error("Background tasks not available in this session."),"continue";let r=n.slice(0,40),o=Ks.register(r),s=fl(o,Ks),i=e.session.current.sendMessageStream(n);return gl(i,"",n,o,Ks,s,e.stats,void 0,e.session.current.abortSignal),e.out.line(m.dim(` \u2192 backgrounded as ${o.id}: ${r}`)),"continue"}};var mp,fp;function ck(e){mp=e}function uk(e){fp=e}var dk={running:"\u25D0",completed:"\u2713",failed:"\u2717",cancelled:"\u2298"};function hl(e){return mp||(e.out.error("Background subagent jobs are not available in this session."),null)}var gp=/\x1b\[[0-9;]*[a-zA-Z]/g;function mN(e,t=120){let n=e.replace(gp,"").replace(/[\r\n]+/g," ").trim();return n.length>t?`${n.slice(0,t)}\u2026`:n}function fN(e){let t=dk[e.status],n=e.endedAt!==void 0?ne(e.endedAt-e.startedAt):ne(Date.now()-e.startedAt),r=e.label.length>60?`${e.label.slice(0,60)}\u2026`:e.label,o=` ${t} ${m.bold(e.jobId)} ${r} ${m.dim(`(${e.status} \xB7 ${n} \xB7 ${e.model})`)}`;if(!fp||e.status!=="running")return o;let s=fp.getSummary(e.jobId);if(!s)return o;let i=process.stdout.columns??120,a=mN(s.text,Math.max(40,i-10)),l=Math.round((Date.now()-s.refreshedAt)/1e3),c=s.stale?" [stale]":"",u=m.dim(` \u21B3 ${a} (${l}s ago)${c}`);return`${o}
2039
+ ${u}`}function hp(e,t){let n=dk[t.status];e.out.line(` ${n} ${m.bold(t.jobId)} ${t.label}`);let r=t.endedAt!==void 0?ne(t.endedAt-t.startedAt):ne(Date.now()-t.startedAt);e.out.line(` Status: ${t.status} \xB7 ${r}`),e.out.line(` Subagent: ${t.subagentId}`),e.out.line(` Model: ${t.model}`);let o=t.result;if(o?.message?.content){e.out.line("");let s=typeof o.message.content=="string"?o.message.content:JSON.stringify(o.message.content),i=s.split(`
2040
2040
  `).slice(0,40);for(let a of i)e.out.line(` ${a}`);s.split(`
2041
- `).length>40&&e.out.line(p.dim(" \u2026 (truncated; full result available via /bgsub:join again)"))}o?.error&&e.out.error(` Error: ${o.error.message}`)}var gN={name:"/bgsub",summary:"List background subagent jobs",usage:"/bgsub [id]",async handler(e,t){let n=hl(e);if(!n)return"continue";let r=t.trim();if(r){let s=n.get(r);return s?(hp(e,s),"continue"):(e.out.error(`No background job with ID "${r}".`),"continue")}let o=n.list();if(o.length===0)return e.out.info('No background subagent jobs. Spawn one by calling the agent tool with mode="background".'),"continue";for(let s of o)e.out.line(fN(s));return"continue"}},hN={name:"/bgsub:status",summary:"Show one background job",usage:"/bgsub:status <id>",async handler(e,t){let n=hl(e);if(!n)return"continue";let r=t.trim();if(!r)return e.out.info("Usage: /bgsub:status <id>"),"continue";let o=n.get(r);return o?(hp(e,o),"continue"):(e.out.error(`No background job with ID "${r}".`),"continue")}},yN={name:"/bgsub:join",summary:"Wait for a background subagent job and print its result",usage:"/bgsub:join <id>",async handler(e,t){let n=hl(e);if(!n)return"continue";let r=t.trim();if(!r)return e.out.info("Usage: /bgsub:join <id>"),"continue";if(n.get(r)){try{await n.join(r)}catch(i){let a=i instanceof Error?i.message:String(i);e.out.error(`/bgsub:join failed: ${a}`)}let s=n.get(r);return s&&hp(e,s),"continue"}let o=await jt.readMeta(r);if(o){e.out.info("Job evicted from memory \u2014 replaying from log.");let s=0;for await(let i of jt.readEvents(r)){let a=bN(i);a!==null&&(e.out.line(a),s++)}return s===0&&e.out.info(` (no events recorded for job ${r})`),e.out.line(""),e.out.line(` Status: ${o.status}`+(o.endedAt!==void 0?` \xB7 ended ${new Date(o.endedAt).toISOString()}`:"")),"continue"}return e.out.error(`No background job with ID "${r}".`),"continue"}};function bN(e){if(e.type==="chunk"){let t=e.chunk;return t.type==="content"?t.content.replace(gp,""):t.type==="tool_use_detail"?p.dim(` [tool: ${t.toolName}]`):null}if(e.type==="error")return p.dim(` [error: ${e.error.message}]`);if(e.type==="message"){let t=e.message.content;return(typeof t=="string"?t:JSON.stringify(t)).replace(gp,"")}return null}var wN={name:"/bgsub:cancel",summary:"Cancel a running background subagent job",usage:"/bgsub:cancel <id>",async handler(e,t){let n=hl(e);if(!n)return"continue";let r=t.trim();if(!r)return e.out.info("Usage: /bgsub:cancel <id>"),"continue";let o=n.get(r);return o?o.status!=="running"?(e.out.info(`Job ${r} is already ${o.status}; nothing to cancel.`),"continue"):(await n.cancelJob(r)?e.out.line(p.dim(` \u2192 cancellation requested for ${r}`)):e.out.info(`Job ${r} could not be cancelled (already terminal).`),"continue"):(e.out.error(`No background job with ID "${r}".`),"continue")}},pk=[gN,hN,yN,wN];var Io;function fk(e){Io=e}function mk(e){let t=e.trim();return t.startsWith("sh-")?t:/^\d+$/.test(t)?`sh-${t}`:t}function SN(e){switch(e){case"running":return"\u25B6";case"completed":return"\u2713";case"failed":return"\u2717";case"killed":return"\u2298";default:return"?"}}var gk={name:"/sh",summary:"Inspect or kill user-typed `!cmd` shell jobs",usage:"/sh [list | show <id> | kill <id>]",hint:"Manages shell processes started with `!cmd` (foreground) or `!&cmd` (background). No args \u2192 list. `show <id>` prints captured output, `kill <id>` terminates a running job.",async handler(e,t){if(!Io)return e.out.error("Shell passthrough not available in this session."),"continue";let n=t.trim(),[r,...o]=n===""?["list"]:n.split(/\s+/),s=o.join(" ");switch(r){case"list":{let i=Io.registry.list();if(i.length===0)return e.out.line(p.dim(" no shell jobs in this session yet \u2014 type !<cmd> to start one")),"continue";e.out.line(p.dim(" id status duration mode command"));for(let a of i){let l=SN(a.status),c=a.status.padEnd(10),u=a.result?ne(a.result.durationMs).padEnd(12):ne(Date.now()-a.startedAt).padEnd(12),d=a.mode==="background"?"bg":"fg",m=a.command.length>60?a.command.slice(0,57)+"...":a.command;e.out.line(` ${l} ${a.id.padEnd(5)} ${c} ${u} ${d.padEnd(4)} ${m}`)}return"continue"}case"show":{if(!s)return e.out.info("Usage: /sh show <id>"),"continue";let i=mk(s),a=Io.registry.get(i);if(!a)return e.out.error(`Job ${i} not found.`),"continue";if(e.out.line(p.dim(`$ ${a.command}`)),!a.result)return e.out.line(p.dim(" (still running \u2014 output captured so far is not yet flushed)")),"continue";let l=a.result.displayCaptured;l.length===0?e.out.line(p.dim(" (no output)")):e.out.raw(l.endsWith(`
2041
+ `).length>40&&e.out.line(m.dim(" \u2026 (truncated; full result available via /bgsub:join again)"))}o?.error&&e.out.error(` Error: ${o.error.message}`)}var gN={name:"/bgsub",summary:"List background subagent jobs",usage:"/bgsub [id]",async handler(e,t){let n=hl(e);if(!n)return"continue";let r=t.trim();if(r){let s=n.get(r);return s?(hp(e,s),"continue"):(e.out.error(`No background job with ID "${r}".`),"continue")}let o=n.list();if(o.length===0)return e.out.info('No background subagent jobs. Spawn one by calling the agent tool with mode="background".'),"continue";for(let s of o)e.out.line(fN(s));return"continue"}},hN={name:"/bgsub:status",summary:"Show one background job",usage:"/bgsub:status <id>",async handler(e,t){let n=hl(e);if(!n)return"continue";let r=t.trim();if(!r)return e.out.info("Usage: /bgsub:status <id>"),"continue";let o=n.get(r);return o?(hp(e,o),"continue"):(e.out.error(`No background job with ID "${r}".`),"continue")}},yN={name:"/bgsub:join",summary:"Wait for a background subagent job and print its result",usage:"/bgsub:join <id>",async handler(e,t){let n=hl(e);if(!n)return"continue";let r=t.trim();if(!r)return e.out.info("Usage: /bgsub:join <id>"),"continue";if(n.get(r)){try{await n.join(r)}catch(i){let a=i instanceof Error?i.message:String(i);e.out.error(`/bgsub:join failed: ${a}`)}let s=n.get(r);return s&&hp(e,s),"continue"}let o=await jt.readMeta(r);if(o){e.out.info("Job evicted from memory \u2014 replaying from log.");let s=0;for await(let i of jt.readEvents(r)){let a=bN(i);a!==null&&(e.out.line(a),s++)}return s===0&&e.out.info(` (no events recorded for job ${r})`),e.out.line(""),e.out.line(` Status: ${o.status}`+(o.endedAt!==void 0?` \xB7 ended ${new Date(o.endedAt).toISOString()}`:"")),"continue"}return e.out.error(`No background job with ID "${r}".`),"continue"}};function bN(e){if(e.type==="chunk"){let t=e.chunk;return t.type==="content"?t.content.replace(gp,""):t.type==="tool_use_detail"?m.dim(` [tool: ${t.toolName}]`):null}if(e.type==="error")return m.dim(` [error: ${e.error.message}]`);if(e.type==="message"){let t=e.message.content;return(typeof t=="string"?t:JSON.stringify(t)).replace(gp,"")}return null}var wN={name:"/bgsub:cancel",summary:"Cancel a running background subagent job",usage:"/bgsub:cancel <id>",async handler(e,t){let n=hl(e);if(!n)return"continue";let r=t.trim();if(!r)return e.out.info("Usage: /bgsub:cancel <id>"),"continue";let o=n.get(r);return o?o.status!=="running"?(e.out.info(`Job ${r} is already ${o.status}; nothing to cancel.`),"continue"):(await n.cancelJob(r)?e.out.line(m.dim(` \u2192 cancellation requested for ${r}`)):e.out.info(`Job ${r} could not be cancelled (already terminal).`),"continue"):(e.out.error(`No background job with ID "${r}".`),"continue")}},pk=[gN,hN,yN,wN];var Io;function fk(e){Io=e}function mk(e){let t=e.trim();return t.startsWith("sh-")?t:/^\d+$/.test(t)?`sh-${t}`:t}function SN(e){switch(e){case"running":return"\u25B6";case"completed":return"\u2713";case"failed":return"\u2717";case"killed":return"\u2298";default:return"?"}}var gk={name:"/sh",summary:"Inspect or kill user-typed `!cmd` shell jobs",usage:"/sh [list | show <id> | kill <id>]",hint:"Manages shell processes started with `!cmd` (foreground) or `!&cmd` (background). No args \u2192 list. `show <id>` prints captured output, `kill <id>` terminates a running job.",async handler(e,t){if(!Io)return e.out.error("Shell passthrough not available in this session."),"continue";let n=t.trim(),[r,...o]=n===""?["list"]:n.split(/\s+/),s=o.join(" ");switch(r){case"list":{let i=Io.registry.list();if(i.length===0)return e.out.line(m.dim(" no shell jobs in this session yet \u2014 type !<cmd> to start one")),"continue";e.out.line(m.dim(" id status duration mode command"));for(let a of i){let l=SN(a.status),c=a.status.padEnd(10),u=a.result?ne(a.result.durationMs).padEnd(12):ne(Date.now()-a.startedAt).padEnd(12),d=a.mode==="background"?"bg":"fg",p=a.command.length>60?a.command.slice(0,57)+"...":a.command;e.out.line(` ${l} ${a.id.padEnd(5)} ${c} ${u} ${d.padEnd(4)} ${p}`)}return"continue"}case"show":{if(!s)return e.out.info("Usage: /sh show <id>"),"continue";let i=mk(s),a=Io.registry.get(i);if(!a)return e.out.error(`Job ${i} not found.`),"continue";if(e.out.line(m.dim(`$ ${a.command}`)),!a.result)return e.out.line(m.dim(" (still running \u2014 output captured so far is not yet flushed)")),"continue";let l=a.result.displayCaptured;l.length===0?e.out.line(m.dim(" (no output)")):e.out.raw(l.endsWith(`
2042
2042
  `)?l:l+`
2043
- `);let c=a.result.errorReason==="abort"?"killed":a.result.errorReason==="timeout"?"timed out":`exit ${a.result.exitCode??0}`;return e.out.line(p.dim(` [${a.id} \xB7 ${c} \xB7 ${ne(a.result.durationMs)}]`)),a.result.truncated&&e.out.line(p.warning(" \u26A0 output was truncated \u2014 re-run with smaller scope if you need the tail")),"continue"}case"kill":{if(!s)return e.out.info("Usage: /sh kill <id>"),"continue";let i=mk(s);if(Io.registry.kill(i))e.out.success(`Killed ${i}.`);else{let l=Io.registry.get(i);l?e.out.warn(`${i} is not running (status: ${l.status}).`):e.out.error(`Job ${i} not found.`)}return"continue"}default:return e.out.warn(`Unknown subcommand: ${r}. Try /sh list, /sh show <id>, or /sh kill <id>.`),"continue"}}};function yp(e){return e.kind==="turn"?e.task.startedAt:e.job.startedAt}var wl,bp;function hk(e){wl=e}function yk(e){bp=e}var yl={running:"\u25D0",succeeded:"\u2713",completed:"\u2713",failed:"\u2717",cancelled:"\u2298"},bl={turn:"\u25B8",subagent:"\u25C6"},bk={name:"/tasks",summary:"List background tasks and subagent jobs",hint:"When you have running or completed /bg turns and want to see status, IDs, and outputs at a glance.",usage:"/tasks [id]",async handler(e,t){if(!wl)return e.out.error("Background tasks not available in this session."),"continue";let n=t.trim();if(n){let o=wl.get(n);if(o){let i=yl[o.status]??"?";if(e.out.line(` ${bl.turn} ${i} ${p.bold(o.id)} ${o.label}`),e.out.line(` Status: ${o.status} \xB7 ${ne(o.stats.durationMs)}`),o.resultText){e.out.line("");let a=o.resultText.split(`
2043
+ `);let c=a.result.errorReason==="abort"?"killed":a.result.errorReason==="timeout"?"timed out":`exit ${a.result.exitCode??0}`;return e.out.line(m.dim(` [${a.id} \xB7 ${c} \xB7 ${ne(a.result.durationMs)}]`)),a.result.truncated&&e.out.line(m.warning(" \u26A0 output was truncated \u2014 re-run with smaller scope if you need the tail")),"continue"}case"kill":{if(!s)return e.out.info("Usage: /sh kill <id>"),"continue";let i=mk(s);if(Io.registry.kill(i))e.out.success(`Killed ${i}.`);else{let l=Io.registry.get(i);l?e.out.warn(`${i} is not running (status: ${l.status}).`):e.out.error(`Job ${i} not found.`)}return"continue"}default:return e.out.warn(`Unknown subcommand: ${r}. Try /sh list, /sh show <id>, or /sh kill <id>.`),"continue"}}};function yp(e){return e.kind==="turn"?e.task.startedAt:e.job.startedAt}var wl,bp;function hk(e){wl=e}function yk(e){bp=e}var yl={running:"\u25D0",succeeded:"\u2713",completed:"\u2713",failed:"\u2717",cancelled:"\u2298"},bl={turn:"\u25B8",subagent:"\u25C6"},bk={name:"/tasks",summary:"List background tasks and subagent jobs",hint:"When you have running or completed /bg turns and want to see status, IDs, and outputs at a glance.",usage:"/tasks [id]",async handler(e,t){if(!wl)return e.out.error("Background tasks not available in this session."),"continue";let n=t.trim();if(n){let o=wl.get(n);if(o){let i=yl[o.status]??"?";if(e.out.line(` ${bl.turn} ${i} ${m.bold(o.id)} ${o.label}`),e.out.line(` Status: ${o.status} \xB7 ${ne(o.stats.durationMs)}`),o.resultText){e.out.line("");let a=o.resultText.split(`
2044
2044
  `).slice(0,20);for(let l of a)e.out.line(` ${l}`);o.resultText.split(`
2045
- `).length>20&&e.out.line(p.dim(" ... (truncated)"))}return o.error&&e.out.error(` Error: ${o.error.message}`),"continue"}let s=bp?.get(n);if(s){let i=yl[s.status]??"?",a=s.endedAt!==void 0?ne(s.endedAt-s.startedAt):ne(Date.now()-s.startedAt);return e.out.line(` ${bl.subagent} ${i} ${p.bold(s.jobId)} ${s.label}`),e.out.line(` Status: ${s.status} \xB7 ${a}`),e.out.line(` Subagent: ${s.subagentId}`),e.out.line(` Model: ${s.model}`),e.out.line(""),e.out.line(p.dim(` Use /bgsub:join ${n} to wait for and print this subagent's result.`)),"continue"}return e.out.error(`No task or job found with ID "${n}".`),"continue"}let r=[...wl.all().map(o=>({kind:"turn",task:o})),...(bp?.list()??[]).map(o=>({kind:"subagent",job:o}))].sort((o,s)=>yp(s)-yp(o));if(r.length===0)return e.out.info("No background tasks. Use Ctrl+B to background a running turn, /bg <prompt>, or dispatch a subagent with mode:'background'."),"continue";for(let o of r)if(o.kind==="turn"){let{task:s}=o,i=yl[s.status]??"?",a=s.status==="running"?ne(Date.now()-s.startedAt):ne(s.stats.durationMs),l=s.progressDescription?p.dim(` ${s.progressDescription}`):"";e.out.line(` ${bl.turn} ${i} ${p.bold(s.id)} ${s.label}${l} ${p.dim(a)}`)}else{let{job:s}=o,i=yl[s.status]??"?",a=s.endedAt!==void 0?ne(s.endedAt-s.startedAt):ne(Date.now()-s.startedAt);e.out.line(` ${bl.subagent} ${i} ${p.bold(s.jobId)} ${s.label} ${p.dim(a)}`)}return"continue"}};var wp;function wk(e){wp=e}var Sk={name:"/attach",summary:"View output of a background task",hint:"When a /bg task has completed and you want to read its full output inline rather than just the notification card.",usage:"/attach <id>",async handler(e,t){let n=t.trim();if(!n)return e.out.info("Usage: /attach <id> (use /tasks to see IDs)"),"continue";if(!wp)return e.out.error("Background tasks not available in this session."),"continue";let r=wp.get(n);if(!r)return e.out.error(`No task found with ID "${n}".`),"continue";if(r.status==="running"){let o=r.progressDescription?` \u2014 ${r.progressDescription}`:"";return e.out.info(`Task ${r.id} is still running${o}. Use /tasks to check progress.`),"continue"}if(e.out.line(p.dim(` \u2500\u2500\u2500 ${r.id} ${r.label} (${r.status}) \u2500\u2500\u2500`)),e.out.line(""),r.resultText){let o=xt(r.resultText);for(let s of o.split(`
2045
+ `).length>20&&e.out.line(m.dim(" ... (truncated)"))}return o.error&&e.out.error(` Error: ${o.error.message}`),"continue"}let s=bp?.get(n);if(s){let i=yl[s.status]??"?",a=s.endedAt!==void 0?ne(s.endedAt-s.startedAt):ne(Date.now()-s.startedAt);return e.out.line(` ${bl.subagent} ${i} ${m.bold(s.jobId)} ${s.label}`),e.out.line(` Status: ${s.status} \xB7 ${a}`),e.out.line(` Subagent: ${s.subagentId}`),e.out.line(` Model: ${s.model}`),e.out.line(""),e.out.line(m.dim(` Use /bgsub:join ${n} to wait for and print this subagent's result.`)),"continue"}return e.out.error(`No task or job found with ID "${n}".`),"continue"}let r=[...wl.all().map(o=>({kind:"turn",task:o})),...(bp?.list()??[]).map(o=>({kind:"subagent",job:o}))].sort((o,s)=>yp(s)-yp(o));if(r.length===0)return e.out.info("No background tasks. Use Ctrl+B to background a running turn, /bg <prompt>, or dispatch a subagent with mode:'background'."),"continue";for(let o of r)if(o.kind==="turn"){let{task:s}=o,i=yl[s.status]??"?",a=s.status==="running"?ne(Date.now()-s.startedAt):ne(s.stats.durationMs),l=s.progressDescription?m.dim(` ${s.progressDescription}`):"";e.out.line(` ${bl.turn} ${i} ${m.bold(s.id)} ${s.label}${l} ${m.dim(a)}`)}else{let{job:s}=o,i=yl[s.status]??"?",a=s.endedAt!==void 0?ne(s.endedAt-s.startedAt):ne(Date.now()-s.startedAt);e.out.line(` ${bl.subagent} ${i} ${m.bold(s.jobId)} ${s.label} ${m.dim(a)}`)}return"continue"}};var wp;function wk(e){wp=e}var Sk={name:"/attach",summary:"View output of a background task",hint:"When a /bg task has completed and you want to read its full output inline rather than just the notification card.",usage:"/attach <id>",async handler(e,t){let n=t.trim();if(!n)return e.out.info("Usage: /attach <id> (use /tasks to see IDs)"),"continue";if(!wp)return e.out.error("Background tasks not available in this session."),"continue";let r=wp.get(n);if(!r)return e.out.error(`No task found with ID "${n}".`),"continue";if(r.status==="running"){let o=r.progressDescription?` \u2014 ${r.progressDescription}`:"";return e.out.info(`Task ${r.id} is still running${o}. Use /tasks to check progress.`),"continue"}if(e.out.line(m.dim(` \u2500\u2500\u2500 ${r.id} ${r.label} (${r.status}) \u2500\u2500\u2500`)),e.out.line(""),r.resultText){let o=xt(r.resultText);for(let s of o.split(`
2046
2046
  `))e.out.line(s)}else r.error?e.out.error(r.error.message):e.out.info("No output captured.");return"continue"}};import{existsSync as Iv}from"node:fs";import{resolve as Pv}from"node:path";var Sl="command-name",kl="command-message",vl="command-args";function Tl(e,t){return`<${Sl}>/${e}</${Sl}>
2047
2047
  <${kl}>${e}</${kl}>
2048
2048
  <${vl}>${t}</${vl}>`}var kk=`<(?:${Sl}|${kl}|${vl})[\\s\\S]*?</(?:${Sl}|${kl}|${vl})>`,kN=new RegExp(`(\\n|^)[ \\t]*${kk}[ \\t]*\\n?`,"gm"),vN=new RegExp(kk,"g");function Sp(e){let t=!1,n=e.replace(kN,(r,o)=>(t=!0,o));return n=n.replace(vN,()=>(t=!0,"")),n=n.replace(/^[ \t]+$/gm,""),n=n.replace(/\n{3,}/g,`
@@ -2052,13 +2052,13 @@ ${u}`}function hp(e,t){let n=dk[t.status];e.out.line(` ${n} ${p.bold(t.jobId)}
2052
2052
  `),l&&(c=c.replace(/^\n+/,"").replace(/\n+$/,"")),{text:c,found:o}}G();G();import{randomBytes as jN}from"node:crypto";G();import xN from"wrap-ansi";var vk="\x1B[?2026h",Tk="\x1B[?2026l",Gs=(e,t)=>`\x1B[${e};${t}H`,vp="\x1B[2K",EN="\x1B[?25l",xk="\x1B[?25h",xl=class{stream;previousTopRow=0;previousLineCount=0;previousRawLineCount=0;constructor(t){this.stream=t}get topRow(){return this.previousTopRow}render(t,n){let r=Math.max(1,n),o=this.stream.columns??80,s=this.stream.isTTY===!0,i=t.endsWith(`
2053
2053
  `)?t:`${t}
2054
2054
  `,l=xN(i,o,{trim:!1,hard:!0,wordWrap:!1}).split(`
2055
- `);for(;l.length>0&&l[l.length-1]==="";)l.pop();let c=Math.max(1,l.length),u=this.previousRawLineCount>c?[...Array(this.previousRawLineCount-c).fill(""),...l]:l,d=u.length,m=Math.max(1,r-d+1),f="";if(s&&(f+=vk),this.previousLineCount>0)for(let g=0;g<this.previousLineCount;g++){let h=this.previousTopRow+g;f+=Gs(h,1)+vp}for(let g=0;g<d;g++){let h=m+g;f+=Gs(h,1)+vp+(u[g]??"")}if(f+=Gs(m+d-1,1),s&&(f+=Tk),this.stream.isTTY)try{this.stream.write(EN)}catch{}try{this.stream.write(f)}catch{try{this.stream.isTTY&&this.stream.write(xk)}catch{}}if(T.NODE_ENV!=="production"&&d<c)throw new Error(`CupFrameRenderer invariant violation: lineCount (${d}) < rawLineCount (${c}). previousLineCount must cover at least previousRawLineCount \u2014 padded footprint must be \u2265 raw content size; see PR #557.`);this.previousTopRow=m,this.previousLineCount=d,this.previousRawLineCount=c}resetGeometry(){this.previousTopRow=0,this.previousLineCount=0,this.previousRawLineCount=0}clear(t=0){if(this.previousLineCount===0)return;let n="",r=this.stream.isTTY===!0;r&&(n+=vk);for(let o=0;o<this.previousLineCount;o++){let s=this.previousTopRow+o;n+=Gs(s,1)+vp}n+=Gs(Math.max(1,(this.stream.rows??24)-1-t),1),r&&(n+=Tk);try{this.stream.write(n)}catch{}this.previousTopRow=0,this.previousLineCount=0,this.previousRawLineCount=0}done(){if(this.previousTopRow=0,this.previousLineCount=0,this.previousRawLineCount=0,this.stream.isTTY)try{this.stream.write(xk)}catch{}}};import qs from"string-width";function fr(e){let{buffer:t,promptText:n,isTTY:r,attachmentSummary:o}=e,s=o&&o.length>0?o:null;if(!r){let g=n+t;return s!==null?`${g} ${s}`:g}let i=e.terminalWidth??Y(),a=t.includes(`
2056
- `),l=qs(Ie(t)),c=l>=i-2,u;if(!a&&!c){let g=Math.max(0,i-l);u=" ".repeat(g)+t}else u=Rn({kind:"user",body:t});if(s===null)return u;let d=qs(s),m=Math.max(0,i-d),f=" ".repeat(m)+p.dim(s);return u+`
2055
+ `);for(;l.length>0&&l[l.length-1]==="";)l.pop();let c=Math.max(1,l.length),u=this.previousRawLineCount>c?[...Array(this.previousRawLineCount-c).fill(""),...l]:l,d=u.length,p=Math.max(1,r-d+1),f="";if(s&&(f+=vk),this.previousLineCount>0)for(let g=0;g<this.previousLineCount;g++){let h=this.previousTopRow+g;f+=Gs(h,1)+vp}for(let g=0;g<d;g++){let h=p+g;f+=Gs(h,1)+vp+(u[g]??"")}if(f+=Gs(p+d-1,1),s&&(f+=Tk),this.stream.isTTY)try{this.stream.write(EN)}catch{}try{this.stream.write(f)}catch{try{this.stream.isTTY&&this.stream.write(xk)}catch{}}if(T.NODE_ENV!=="production"&&d<c)throw new Error(`CupFrameRenderer invariant violation: lineCount (${d}) < rawLineCount (${c}). previousLineCount must cover at least previousRawLineCount \u2014 padded footprint must be \u2265 raw content size; see PR #557.`);this.previousTopRow=p,this.previousLineCount=d,this.previousRawLineCount=c}resetGeometry(){this.previousTopRow=0,this.previousLineCount=0,this.previousRawLineCount=0}clear(t=0){if(this.previousLineCount===0)return;let n="",r=this.stream.isTTY===!0;r&&(n+=vk);for(let o=0;o<this.previousLineCount;o++){let s=this.previousTopRow+o;n+=Gs(s,1)+vp}n+=Gs(Math.max(1,(this.stream.rows??24)-1-t),1),r&&(n+=Tk);try{this.stream.write(n)}catch{}this.previousTopRow=0,this.previousLineCount=0,this.previousRawLineCount=0}done(){if(this.previousTopRow=0,this.previousLineCount=0,this.previousRawLineCount=0,this.stream.isTTY)try{this.stream.write(xk)}catch{}}};import qs from"string-width";function fr(e){let{buffer:t,promptText:n,isTTY:r,attachmentSummary:o}=e,s=o&&o.length>0?o:null;if(!r){let g=n+t;return s!==null?`${g} ${s}`:g}let i=e.terminalWidth??Y(),a=t.includes(`
2056
+ `),l=qs(Ie(t)),c=l>=i-2,u;if(!a&&!c){let g=Math.max(0,i-l);u=" ".repeat(g)+t}else u=Rn({kind:"user",body:t});if(s===null)return u;let d=qs(s),p=Math.max(0,i-d),f=" ".repeat(p)+m.dim(s);return u+`
2057
2057
  `+f}function Tp(e,t,n){let r=n||80,o=e.split(`
2058
2058
  `),s=0;for(let i=0;i<o.length;i++){let a=Ie(o[i]),l=(i===0?t:0)+qs(a);s+=Math.max(1,Math.ceil(l/r))}return Math.max(0,s-1)}function Un(e,t,n,r){let o=r||80,s=e.split(`
2059
- `),i=0,a=0;for(let l=0;l<s.length;l++){let c=s[l],u=c.length+(l<s.length-1?1:0);if(t<=a+c.length){let m=t-a,f=Ie(c.slice(0,m)),g=(l===0?n:0)+qs(f),h=Math.floor(g/o),b=g%o;return{row:i+h,col:b}}let d=(l===0?n:0)+qs(Ie(c));i+=Math.max(1,Math.ceil(d/o)),a+=u}return{row:i,col:0}}function gr(e,t){return Number.isFinite(e)?Math.max(0,Math.min(t,Math.trunc(e))):0}function qt(e,t){let n=gr(t,e.buffer.length);return n===e.cursor?e:{buffer:e.buffer,cursor:n}}function Ek(e,t){let n=gr(t,e.length);for(;n>0&&/\s/.test(e.charAt(n-1));)n--;for(;n>0&&!/\s/.test(e.charAt(n-1));)n--;return n}function Rk(e,t){let n=e.length,r=gr(t,n);for(;r<n&&/\s/.test(e.charAt(r));)r++;for(;r<n&&!/\s/.test(e.charAt(r));)r++;return r}function Ak(e,t){let n=gr(t,e.length),r=n===0?-1:e.lastIndexOf(`
2059
+ `),i=0,a=0;for(let l=0;l<s.length;l++){let c=s[l],u=c.length+(l<s.length-1?1:0);if(t<=a+c.length){let p=t-a,f=Ie(c.slice(0,p)),g=(l===0?n:0)+qs(f),h=Math.floor(g/o),b=g%o;return{row:i+h,col:b}}let d=(l===0?n:0)+qs(Ie(c));i+=Math.max(1,Math.ceil(d/o)),a+=u}return{row:i,col:0}}function gr(e,t){return Number.isFinite(e)?Math.max(0,Math.min(t,Math.trunc(e))):0}function qt(e,t){let n=gr(t,e.buffer.length);return n===e.cursor?e:{buffer:e.buffer,cursor:n}}function Ek(e,t){let n=gr(t,e.length);for(;n>0&&/\s/.test(e.charAt(n-1));)n--;for(;n>0&&!/\s/.test(e.charAt(n-1));)n--;return n}function Rk(e,t){let n=e.length,r=gr(t,n);for(;r<n&&/\s/.test(e.charAt(r));)r++;for(;r<n&&!/\s/.test(e.charAt(r));)r++;return r}function Ak(e,t){let n=gr(t,e.length),r=n===0?-1:e.lastIndexOf(`
2060
2060
  `,n-1);return r<0?0:r+1}function _k(e,t){let n=gr(t,e.length),r=e.indexOf(`
2061
- `,n);return r<0?e.length:r}function jn(e,t,n){let r=gr(t.start,e.buffer.length),o=gr(t.end,e.buffer.length),s=Math.min(r,o),i=Math.max(r,o);if(s===i&&n.length===0)return e;let a=e.buffer.slice(0,s)+n+e.buffer.slice(i),l=s+n.length;return a===e.buffer&&l===e.cursor?e:{buffer:a,cursor:l}}var U={seed(e=""){return{buffer:e,cursor:e.length}},replaceRange:jn,insert(e,t){return t.length===0?e:jn(e,{start:e.cursor,end:e.cursor},t)},backspace(e){if(e.cursor===0)return e;let t=fu(e.buffer,e.cursor);return jn(e,{start:t,end:e.cursor},"")},deleteForward(e){if(e.cursor>=e.buffer.length)return e;let t=cs(e.buffer,e.cursor);return jn(e,{start:e.cursor,end:t},"")},deleteWordBackward(e){if(e.cursor===0)return e;let t=Ek(e.buffer,e.cursor);return t===e.cursor?e:jn(e,{start:t,end:e.cursor},"")},deleteWordForward(e){if(e.cursor>=e.buffer.length)return e;let t=Rk(e.buffer,e.cursor);return t===e.cursor?e:jn(e,{start:e.cursor,end:t},"")},deleteToLineStart(e){let t=Ak(e.buffer,e.cursor);return t===e.cursor?e:jn(e,{start:t,end:e.cursor},"")},deleteToLineEnd(e){let t=_k(e.buffer,e.cursor);return t===e.cursor?e:jn(e,{start:e.cursor,end:t},"")},moveLeft(e){return qt(e,fu(e.buffer,e.cursor))},moveRight(e){return qt(e,cs(e.buffer,e.cursor))},moveHome(e){return qt(e,0)},moveEnd(e){return qt(e,e.buffer.length)},moveLineStart(e){return qt(e,Ak(e.buffer,e.cursor))},moveLineEnd(e){return qt(e,_k(e.buffer,e.cursor))},moveWordBackward(e){return qt(e,Ek(e.buffer,e.cursor))},moveWordForward(e){return qt(e,Rk(e.buffer,e.cursor))},moveUpLine(e,t,n){let r=t||80,{row:o,col:s}=Un(e.buffer,e.cursor,n,r);if(o===0)return{moved:!1};let i=o-1,a=s,l=0,c=1/0;for(let d=0;d<=e.buffer.length;d++){let m=Un(e.buffer,d,n,r);if(m.row===i){let f=Math.abs(m.col-a);f<c&&(c=f,l=d)}else if(m.row>i)break}let u=qt(e,l);return u===e?{moved:!1}:{moved:!0,state:u}},moveDownLine(e,t,n){let r=t||80,{row:o,col:s}=Un(e.buffer,e.cursor,n,r),i=Un(e.buffer,e.buffer.length,n,r);if(o>=i.row)return{moved:!1};let a=o+1,l=s,c=e.buffer.length,u=1/0;for(let m=0;m<e.buffer.length;m++){let f=Un(e.buffer,m,n,r);if(f.row===a){let g=Math.abs(f.col-l);g<u&&(u=g,c=m)}else if(f.row>a)break}i.row===a&&Math.abs(i.col-l)<u&&(c=e.buffer.length);let d=qt(e,c);return d===e?{moved:!1}:{moved:!0,state:d}}};import RN from"chalk";import AN from"string-width";function Ck(e,t){if(t<=0)return"";let n=0;for(let r=0;r<e.length;r++)if(n+=AN(e[r]),n>t)return e.slice(0,r)+p.dim("\u2026");return e}function El(e,t,n,r){let o=t?">":" ",s=e.value,i=e.summary?` ${e.summary}`:"",a=`${s}${i}`,l=Ck(a,n-4),c=` ${o} ${l}`;return t?RN.inverse(p.user(c)):p.meta(c)}function Rl(e,t){if(!e)return null;let n=e.trim();if(n.length===0)return null;let r=Ck(n,Math.max(0,t-7));return r.length===0?null:p.dim(` \u21B3 ${r}`)}import{readdirSync as _N,statSync as CN}from"fs";import{join as Ik}from"path";function Pk(e,t=process.cwd()){try{let n=e.lastIndexOf("/"),r=n===-1?"":e.slice(0,n),o=n===-1?e:e.slice(n+1),s=r?Ik(t,r):t,i=_N(s),a=[];for(let l of i){if(!l.startsWith(o)||l.startsWith(".")&&!o.startsWith("."))continue;let c=r?`${r}/${l}`:l;try{CN(Ik(s,l)).isDirectory()&&(c+="/")}catch{}if(a.push(c),a.length>=30)break}return a.sort()}catch{return[]}}async function Mk(e){let t="",n=e.promptFn(),r=e.continuationPrompt??" \u203A ";for(;;){e.rl.setPrompt(n),e.rl.prompt();let o=await new Promise(s=>{e.rl.once("line",i=>s(i))});if(o.endsWith("\\")){t+=o.slice(0,-1)+`
2061
+ `,n);return r<0?e.length:r}function jn(e,t,n){let r=gr(t.start,e.buffer.length),o=gr(t.end,e.buffer.length),s=Math.min(r,o),i=Math.max(r,o);if(s===i&&n.length===0)return e;let a=e.buffer.slice(0,s)+n+e.buffer.slice(i),l=s+n.length;return a===e.buffer&&l===e.cursor?e:{buffer:a,cursor:l}}var U={seed(e=""){return{buffer:e,cursor:e.length}},replaceRange:jn,insert(e,t){return t.length===0?e:jn(e,{start:e.cursor,end:e.cursor},t)},backspace(e){if(e.cursor===0)return e;let t=fu(e.buffer,e.cursor);return jn(e,{start:t,end:e.cursor},"")},deleteForward(e){if(e.cursor>=e.buffer.length)return e;let t=cs(e.buffer,e.cursor);return jn(e,{start:e.cursor,end:t},"")},deleteWordBackward(e){if(e.cursor===0)return e;let t=Ek(e.buffer,e.cursor);return t===e.cursor?e:jn(e,{start:t,end:e.cursor},"")},deleteWordForward(e){if(e.cursor>=e.buffer.length)return e;let t=Rk(e.buffer,e.cursor);return t===e.cursor?e:jn(e,{start:e.cursor,end:t},"")},deleteToLineStart(e){let t=Ak(e.buffer,e.cursor);return t===e.cursor?e:jn(e,{start:t,end:e.cursor},"")},deleteToLineEnd(e){let t=_k(e.buffer,e.cursor);return t===e.cursor?e:jn(e,{start:e.cursor,end:t},"")},moveLeft(e){return qt(e,fu(e.buffer,e.cursor))},moveRight(e){return qt(e,cs(e.buffer,e.cursor))},moveHome(e){return qt(e,0)},moveEnd(e){return qt(e,e.buffer.length)},moveLineStart(e){return qt(e,Ak(e.buffer,e.cursor))},moveLineEnd(e){return qt(e,_k(e.buffer,e.cursor))},moveWordBackward(e){return qt(e,Ek(e.buffer,e.cursor))},moveWordForward(e){return qt(e,Rk(e.buffer,e.cursor))},moveUpLine(e,t,n){let r=t||80,{row:o,col:s}=Un(e.buffer,e.cursor,n,r);if(o===0)return{moved:!1};let i=o-1,a=s,l=0,c=1/0;for(let d=0;d<=e.buffer.length;d++){let p=Un(e.buffer,d,n,r);if(p.row===i){let f=Math.abs(p.col-a);f<c&&(c=f,l=d)}else if(p.row>i)break}let u=qt(e,l);return u===e?{moved:!1}:{moved:!0,state:u}},moveDownLine(e,t,n){let r=t||80,{row:o,col:s}=Un(e.buffer,e.cursor,n,r),i=Un(e.buffer,e.buffer.length,n,r);if(o>=i.row)return{moved:!1};let a=o+1,l=s,c=e.buffer.length,u=1/0;for(let p=0;p<e.buffer.length;p++){let f=Un(e.buffer,p,n,r);if(f.row===a){let g=Math.abs(f.col-l);g<u&&(u=g,c=p)}else if(f.row>a)break}i.row===a&&Math.abs(i.col-l)<u&&(c=e.buffer.length);let d=qt(e,c);return d===e?{moved:!1}:{moved:!0,state:d}}};import RN from"chalk";import AN from"string-width";function Ck(e,t){if(t<=0)return"";let n=0;for(let r=0;r<e.length;r++)if(n+=AN(e[r]),n>t)return e.slice(0,r)+m.dim("\u2026");return e}function El(e,t,n,r){let o=t?">":" ",s=e.value,i=e.summary?` ${e.summary}`:"",a=`${s}${i}`,l=Ck(a,n-4),c=` ${o} ${l}`;return t?RN.inverse(m.user(c)):m.meta(c)}function Rl(e,t){if(!e)return null;let n=e.trim();if(n.length===0)return null;let r=Ck(n,Math.max(0,t-7));return r.length===0?null:m.dim(` \u21B3 ${r}`)}import{readdirSync as _N,statSync as CN}from"fs";import{join as Ik}from"path";function Pk(e,t=process.cwd()){try{let n=e.lastIndexOf("/"),r=n===-1?"":e.slice(0,n),o=n===-1?e:e.slice(n+1),s=r?Ik(t,r):t,i=_N(s),a=[];for(let l of i){if(!l.startsWith(o)||l.startsWith(".")&&!o.startsWith("."))continue;let c=r?`${r}/${l}`:l;try{CN(Ik(s,l)).isDirectory()&&(c+="/")}catch{}if(a.push(c),a.length>=30)break}return a.sort()}catch{return[]}}async function Mk(e){let t="",n=e.promptFn(),r=e.continuationPrompt??" \u203A ";for(;;){e.rl.setPrompt(n),e.rl.prompt();let o=await new Promise(s=>{e.rl.once("line",i=>s(i))});if(o.endsWith("\\")){t+=o.slice(0,-1)+`
2062
2062
  `,n=r;continue}return t+=o,t}}function zs(e,t){let n=e.slice(0,t);if(/^\/[A-Za-z_-]*$/.test(n))return{kind:"slash",query:n.slice(1)};let r=n.split(/\s+/),o=r[r.length-1]??"";if(o.startsWith("@")&&/^@[^\s]*$/.test(o))return{kind:"file",query:o.slice(1)};let s=/^\/([A-Za-z][A-Za-z0-9_:-]*)\s+(?:.*\s)?--([a-z0-9-]*)$/.exec(n);if(s){let i=s[1],a=s[2],l=Ze().find(c=>c.name===`/${i}`);if(l?.flags&&l.flags.length>0)return{kind:"flag",command:i,query:a}}return null}function Al(e){let t=Ze(),n=t.filter(s=>s.name.slice(1).startsWith(e)).map(s=>({value:s.name,summary:s.summary,...s.hint?{hint:s.hint}:{}})),r=RS().filter(s=>s.alias.slice(1).startsWith(e)).map(s=>{let i=t.find(a=>a.name===s.canonical);return{value:s.alias,summary:s.summary,...i?.hint?{hint:i.hint}:{}}});return[...n,...r].sort((s,i)=>s.value.localeCompare(i.value)).slice(0,20)}function _l(e,t=process.cwd()){return Pk(e,t).slice(0,20).map(n=>({value:"@"+n}))}function Js(e,t){let n=Ze().find(s=>s.name===`/${e}`);if(!n?.flags||n.flags.length===0)return[];let r=t.startsWith("--")?t.slice(2):t;return n.flags.filter(s=>(s.startsWith("--")?s.slice(2):s).startsWith(r)).map(s=>({value:s})).sort((s,i)=>s.value.localeCompare(i.value)).slice(0,20)}var IN={"image/png":"PNG","image/jpeg":"JPEG","image/gif":"GIF","image/webp":"WEBP"};function PN(e){if(e<1024)return`${e} B`;let t=e/1024;return t<1024?`${t.toFixed(1)} KiB`:`${(t/1024).toFixed(1)} MiB`}function Vs(e){if(e.length===0)return"";let t=e.reduce((a,l)=>a+l.sizeBytes,0),n=PN(t),r=e.length===1?"image":"images",o=new Set,s=[];for(let a of e){let l=IN[a.mediaType];o.has(l)||(o.add(l),s.push(l))}let i=s.join(", ");return`[${e.length} ${r} attached \xB7 ${n} \xB7 ${i} \xB7 Ctrl+X to discard]`}function Ok(e,t){if(t.length===0)return e;if(e){let n=t.length===1?"image":"images";return`${e} [+ ${t.length} ${n}]`}return t.length===1?"[image attached]":`[${t.length} images attached]`}function Po(e){return e.length===0?"":e.length===1?"[image attached]":`[${e.length} images attached]`}G();import{spawn as $k}from"child_process";import{randomUUID as xp}from"crypto";import{readFile as Dk,unlink as Ep}from"fs/promises";import{tmpdir as Lk}from"os";import{join as Fk}from"path";var hr=!!T.AFK_DEBUG_CLIPBOARD;function yr(e){process.stderr.write(`[afk-clipboard] ${e}
2063
2063
  `)}async function br(){if(process.platform!=="darwin")return null;hr&&yr("probing clipboard for image data");for(let e of["PNGf","TIFF"]){let t=Fk(Lk(),`afk-clipboard-${xp()}.bin`);try{let{ok:n,exitCode:r,stderr:o}=await $N(e,t);if(hr&&yr(`class=${e} osascript exitCode=${r} stderr=${JSON.stringify(o)} ok=${n}`),!n)continue;let s=await Dk(t);if(s.length===0)continue;if(MN(s)){hr&&yr(`class=${e} magic=TIFF detected, transcoding via sips`);let a=await ON(t);if(!a){hr&&yr(`class=${e} sips transcode failed, skipping`);continue}s=a}let i=LN(s);if(hr&&yr(`class=${e} magic-byte detection result: ${i??"unrecognized"}`),!i)continue;return hr&&yr(`probe success: mediaType=${i} size=${s.byteLength}`),{id:xp(),mediaType:i,bytes:s,sizeBytes:s.byteLength}}catch{}finally{Ep(t).catch(()=>{})}}return hr&&yr("probe result: null (no image found on clipboard)"),null}function MN(e){return e.length<4?!1:e[0]===73&&e[1]===73&&e[2]===42&&e[3]===0||e[0]===77&&e[1]===77&&e[2]===0&&e[3]===42}async function ON(e){let t=Fk(Lk(),`afk-clipboard-${xp()}.png`);if(!await new Promise(r=>{let o=$k("sips",["-s","format","png",e,"--out",t],{stdio:["ignore","ignore","ignore"]});o.on("error",()=>r(!1)),o.on("close",s=>r(s===0))}))return Ep(t).catch(()=>{}),null;try{let r=await Dk(t);return r.length>0?r:null}catch{return null}finally{Ep(t).catch(()=>{})}}async function $N(e,t){let n=`
2064
2064
  try
@@ -2074,26 +2074,26 @@ ${u}`}function hp(e,t){let n=dk[t.status];e.out.line(` ${n} ${p.bold(t.jobId)}
2074
2074
  end try
2075
2075
  return "no"
2076
2076
  end try
2077
- `;return new Promise(r=>{let o=$k("osascript",["-e",n],{stdio:["ignore","pipe","pipe"]}),s="",i="";o.stdout?.on("data",a=>{s+=a.toString("utf8")}),o.stderr?.on("data",a=>{i+=a.toString("utf8")}),o.on("error",()=>r({ok:!1,exitCode:null,stderr:i})),o.on("close",a=>{r({ok:a===0&&s.trim()==="ok",exitCode:a,stderr:i})})})}function DN(e){return'"'+e.replace(/\\/g,"\\\\").replace(/"/g,'\\"')+'"'}function LN(e){return e.length>=8&&e[0]===137&&e[1]===80&&e[2]===78&&e[3]===71?"image/png":e.length>=3&&e[0]===255&&e[1]===216&&e[2]===255?"image/jpeg":e.length>=4&&e[0]===71&&e[1]===73&&e[2]===70&&e[3]===56?"image/gif":e.length>=12&&e[0]===82&&e[1]===73&&e[2]===70&&e[3]===70&&e[8]===87&&e[9]===69&&e[10]===66&&e[11]===80?"image/webp":null}var Nk=["Stalking","Shadowing","Tailing","Casing","Sleuthing","Investigating","Deducing","Interrogating","Profiling","Canvassing","Prowling","Lurking","Scanning","Probing","Inspecting","Querying","Invoking","Parsing","Validating","Resolving","Compiling","Executing","Hunting","Sweeping","Tracing","Tracking","Triangulating","Decoding","Decrypting","Intercepting","Hacking","Bugging","Wiretapping","Dispatching","Deploying","Patching","Hooking","Unmasking","Cornering","Striking","Surveilling","Scouting"];function Rp(){return Nk[Math.floor(Math.random()*Nk.length)]}G();var FN=[{id:"kbd:ctrl-b",text:"Ctrl+B during a turn detaches it to the background so you can keep typing \u2014 find it later with /tasks.",source:"static"},{id:"kbd:shift-tab",text:"Shift+Tab toggles plan mode (no file writes) without leaving the prompt.",source:"static"},{id:"kbd:ctrl-c",text:"Ctrl+C interrupts the current turn; press it twice in a row to exit the REPL.",source:"static"},{id:"kbd:at-path",text:"Type @ in the prompt to autocomplete a file path and attach its contents to your turn.",source:"static"},{id:"kbd:cmd-v",text:"Paste an image with Cmd+V (macOS) or Ctrl+V \u2014 clipboard images attach automatically.",source:"static"},{id:"env:tips-opt-out",text:"Set AFK_SPINNER_TIPS=0 to silence these loading-screen tips.",source:"static"}];function NN(){let e=[];for(let t of Ze())t.hint&&(t.name.includes(":")||e.push({id:`cmd:${t.name}`,text:`${t.name} \u2014 ${t.hint}`,source:"command"}));return e}function BN(e){let t=[];for(let n of ft()){let r=`cmd:/${n}`;if(e.has(r))continue;let o;try{o=$e(n)}catch{continue}Et(o,T.AFK_INTERNAL==="1")&&o.whenToUse&&t.push({id:`skill:${n}`,text:`/${n} \u2014 ${o.whenToUse}`,source:"skill"})}return t}function jk(){if(T.AFK_SPINNER_TIPS==="0")return[];let e=NN(),t=new Set(e.map(r=>r.id)),n=BN(t);return[...FN,...e,...n]}var Bk=new Set,Uk=new Map;function Wk(e,t){if(e.length===0)return null;let n=t.now??Date.now(),r=t.warmupMs??1500;if(n-t.startedAt<r)return null;let o=t.rotateMs??7e3,s=Math.floor((n-t.startedAt)/o),i=`${t.startedAt}:${s}`,a=Uk.get(i);if(a)return a;let l=null,c=e.filter(u=>!Bk.has(u.id));if(c.length>0){let u=(s%c.length+c.length)%c.length;l=c[u]??null}else{let u=(s%e.length+e.length)%e.length;l=e[u]??null}return l&&(Bk.add(l.id),Uk.set(i,l)),l}var Ap=["\u280B","\u2819","\u2839","\u2838","\u283C","\u2834","\u2826","\u2827","\u2807","\u280F"],UN=5e3;function Hk(e){let t=Date.now()-e;if(t<UN)return"";let n=Math.floor(t/1e3);if(n<60)return p.dim(` ${n}s`);let r=Math.floor(n/60),o=n%60;return p.dim(` ${r}m${o.toString().padStart(2,"0")}s`)}function Kk(e,t){let n=" \u{1F4A1} ",r=Math.max(8,t-n.length-1),o=e.length>r?e.slice(0,r-1)+"\u2026":e;return p.dim(n+o)}var Cl=class{state=null;interval=null;captureMode;onTick;constructor(t){this.captureMode=t.captureMode,this.onTick=t.onTick}set(t){if(!t.enabled){this.interval&&(clearInterval(this.interval),this.interval=null),this.state&&(this.state=null,this.onTick());return}if(this.captureMode||this.state)return;let n=t.rotateVerbEveryMs??3500,r=Date.now();this.state={frameIndex:0,verb:Rp(),nextVerbRotateAt:r+n,startedAt:r,tipPool:jk(),currentTip:null},this.interval=setInterval(()=>this.tick(n),80),this.onTick()}dispose(){this.interval&&(clearInterval(this.interval),this.interval=null),this.state=null}renderSpinnerRow(){return this.state?p.meta(`${Ap[this.state.frameIndex]} ${this.state.verb}...`)+Hk(this.state.startedAt):null}renderTipRow(t){return this.state?.currentTip?Kk(this.state.currentTip.text,t):null}tick(t){if(!this.state)return;this.state.frameIndex=(this.state.frameIndex+1)%Ap.length;let n=Date.now();n>=this.state.nextVerbRotateAt&&(this.state.verb=Rp(),this.state.nextVerbRotateAt=n+t),this.state.currentTip=Wk(this.state.tipPool,{startedAt:this.state.startedAt,now:n}),this.onTick()}};var WN=5,HN=1e3,KN=/\[Pasted text #([0-9a-f]+) \+\d+ (?:lines|chars)\]/g;function GN(e,t){let n=t.match(/\n/g),r=n?n.length:0;return r>0?`[Pasted text #${e} +${r+1} lines]`:`[Pasted text #${e} +${t.length} chars]`}var Mo=class e{stdout;stdin;onCancel;onSoftStop;softStopped=!1;onBackground;onShiftTab;promptTextFn;history;autocompleteState;formatInputBuffer;scrollRegion;anchorRow;declaredAnchorRow;onSubmit;attachments=[];pasting=!1;pasteStartBufferLen=0;pasteStartCursor=0;pasteRegistry=new Map;clipboardInFlight=!1;clipboardFailureMsg=null;inputMode="streaming";pickerController=null;pickerSavedMode="streaming";armed=!1;suspended=!1;canceled=!1;backgrounded=!1;wasRaw=!1;logUpdate=null;overlay="";input=U.seed("");queued=!1;handleKeypress=null;resizeUnsub=null;resizeImmediateUnsub=null;spinnerController;committing=!1;hasCommitted=!1;committedBand=[];committedBandTopRow=0;committedBandBottomRow=0;commitInFlight=!1;debugCompositor=!!T.AFK_DEBUG_COMPOSITOR;debugLog(t,n={}){if(!this.debugCompositor)return;let r=process.hrtime.bigint(),o=Object.entries(n).map(([s,i])=>{let a=typeof i=="string"?JSON.stringify(i.length>60?i.slice(0,57)+"...":i):String(i);return`${s}=${a}`}).join(" ");process.stderr.write(`[compositor] t=${r} ${t}${o?" "+o:""}
2078
- `)}static MAX_DROPDOWN_ROWS=6;constructor(t){this.stdout=t.stdout,this.stdin=t.stdin,this.onCancel=t.onCancel,this.onSoftStop=t.onSoftStop,this.onBackground=t.onBackground,this.onShiftTab=t.onShiftTab;let n=t.promptText;if(typeof n=="function")this.promptTextFn=n;else if(typeof n=="string")this.promptTextFn=()=>n;else{let r=" "+p.dim("\u23AF")+" ";this.promptTextFn=()=>r}this.history=t.history,this.autocompleteState=t.autocompleteState,this.formatInputBuffer=t.formatInputBuffer,this.scrollRegion=t.scrollRegion,this.spinnerController=new Cl({captureMode:t.captureMode??!1,onTick:()=>this.repaint()}),this.onSubmit=t.onSubmit,this.anchorRow=t.anchorRow,this.declaredAnchorRow=t.anchorRow}isArmed(){return this.armed}setAnchorRow(t){this.anchorRow=t,this.declaredAnchorRow=t}suspendInput(){if(!(!this.armed||this.suspended)){if(this.logUpdate)try{this.logUpdate.clear(this.scrollRegion?.getExtraRows()??0),this.logUpdate.done()}catch{}this.handleKeypress&&this.stdin.removeListener("keypress",this.handleKeypress);try{this.stdin.setRawMode(!1)}catch{}this.suspended=!0}}resumeInput(){if(!(!this.armed||!this.suspended)){try{this.stdin.setRawMode(!0)}catch{}this.handleKeypress&&this.stdin.on("keypress",this.handleKeypress),this.suspended=!1,this.repaint()}}setOnSubmit(t){this.onSubmit=t??void 0}setOnCancel(t){this.onCancel=t??void 0}getOnCancel(){return this.onCancel}setOnSoftStop(t){this.onSoftStop=t??void 0}setOnBackground(t){this.onBackground=t??void 0}setOnShiftTab(t){this.onShiftTab=t??void 0}enterPickerMode(t){if(this.pickerController)throw new Error("enterPickerMode: a picker is already active; call exitPickerMode first");this.pickerSavedMode=this.inputMode,this.pickerController=t,this.inputMode="picker",this.autocompleteState?.reset(),this.repaint()}exitPickerMode(){this.pickerController&&(this.pickerController=null,this.inputMode=this.pickerSavedMode,this.repaint())}repaintPicker(){this.pickerController&&this.repaint()}setInputMode(t){let n=this.inputMode;if(this.inputMode=t,n==="idle"&&t==="streaming"){this.canceled=!1,this.backgrounded=!1,this.softStopped=!1,this.autocompleteState?.reset(),this.repaint();return}if(t==="idle"&&this.softStopped){this.queued=!1,this.softStopped=!1,this.repaint();return}if(t==="idle"&&this.queued&&this.onSubmit){let r=this.input.buffer,o=this.expandPastePlaceholders(r),s=[...this.attachments],i=this.onSubmit;this.queued=!1,this.input=U.seed(""),this.attachments=[],this.pasteRegistry.clear(),this.repaint(),i(o===r?{text:o,attachments:s}:{text:o,displayText:r,attachments:s});return}n!==t&&this.repaint()}getInputMode(){return this.inputMode}async arm(){if(this.armed)throw new Error("TerminalCompositor: arm() called while already armed");if(!(!this.stdout.isTTY||!this.stdin.isTTY)){this.anchorRow=this.declaredAnchorRow,this.logUpdate||(this.logUpdate=new xl(this.stdout)),this.wasRaw=this.stdin.isRaw??!1;try{this.stdin.setRawMode(!0)}catch{this.logUpdate=null,this.wasRaw=!1;return}try{this.stdout.write("\x1B[?2004h")}catch{}this.stdin.resume(),ur(this.stdin),this.handleKeypress=(t,n)=>this.dispatchKey(t,n),this.stdin.on("keypress",this.handleKeypress),this.armed=!0,this.canceled=!1,this.resizeUnsub=Ue.subscribe(()=>{this.armed&&this.repaint()}),this.resizeImmediateUnsub=Ue.subscribeImmediate(()=>{this.armed&&(this.logUpdate?.resetGeometry?.(),this.clearCommittedBand())}),this.repaint()}}disarm(){if(this.spinnerController.dispose(),!this.armed){this.resetState();return}if(this.handleKeypress&&(this.stdin.removeListener("keypress",this.handleKeypress),this.handleKeypress=null),this.resizeUnsub&&(this.resizeUnsub(),this.resizeUnsub=null),this.resizeImmediateUnsub&&(this.resizeImmediateUnsub(),this.resizeImmediateUnsub=null),this.logUpdate)try{this.logUpdate.clear(this.scrollRegion?.getExtraRows()??0),this.logUpdate.done()}catch{}if(this.stdout.isTTY&&this.stdin.isTTY){try{this.stdout.write("\x1B[?2004l")}catch{}try{this.stdin.setRawMode(this.wasRaw)}catch{}}this.armed=!1,this.resetState()}setOverlay(t){t!==this.overlay&&(this.debugLog("setOverlay",{framesLen:t.length,anchorRow:this.anchorRow??null}),this.overlay=t,this.repaint())}setSpinner(t){this.stdout.isTTY&&this.spinnerController.set(t)}commitAbove(t){this.debugLog("commitAbove:enter",{textLen:t.length,anchorRow:this.anchorRow??null,committing:this.committing,topRow:this.logUpdate?.topRow??null});let n=g=>{this.scrollRegion?this.scrollRegion.withFullScrollRegion(g):g()};if(!this.armed||!this.logUpdate){n(()=>{this.stdout.write(t+`
2077
+ `;return new Promise(r=>{let o=$k("osascript",["-e",n],{stdio:["ignore","pipe","pipe"]}),s="",i="";o.stdout?.on("data",a=>{s+=a.toString("utf8")}),o.stderr?.on("data",a=>{i+=a.toString("utf8")}),o.on("error",()=>r({ok:!1,exitCode:null,stderr:i})),o.on("close",a=>{r({ok:a===0&&s.trim()==="ok",exitCode:a,stderr:i})})})}function DN(e){return'"'+e.replace(/\\/g,"\\\\").replace(/"/g,'\\"')+'"'}function LN(e){return e.length>=8&&e[0]===137&&e[1]===80&&e[2]===78&&e[3]===71?"image/png":e.length>=3&&e[0]===255&&e[1]===216&&e[2]===255?"image/jpeg":e.length>=4&&e[0]===71&&e[1]===73&&e[2]===70&&e[3]===56?"image/gif":e.length>=12&&e[0]===82&&e[1]===73&&e[2]===70&&e[3]===70&&e[8]===87&&e[9]===69&&e[10]===66&&e[11]===80?"image/webp":null}var Nk=["Stalking","Shadowing","Tailing","Casing","Sleuthing","Investigating","Deducing","Interrogating","Profiling","Canvassing","Prowling","Lurking","Scanning","Probing","Inspecting","Querying","Invoking","Parsing","Validating","Resolving","Compiling","Executing","Hunting","Sweeping","Tracing","Tracking","Triangulating","Decoding","Decrypting","Intercepting","Hacking","Bugging","Wiretapping","Dispatching","Deploying","Patching","Hooking","Unmasking","Cornering","Striking","Surveilling","Scouting"];function Rp(){return Nk[Math.floor(Math.random()*Nk.length)]}G();var FN=[{id:"kbd:ctrl-b",text:"Ctrl+B during a turn detaches it to the background so you can keep typing \u2014 find it later with /tasks.",source:"static"},{id:"kbd:shift-tab",text:"Shift+Tab toggles plan mode (no file writes) without leaving the prompt.",source:"static"},{id:"kbd:ctrl-c",text:"Ctrl+C interrupts the current turn; press it twice in a row to exit the REPL.",source:"static"},{id:"kbd:at-path",text:"Type @ in the prompt to autocomplete a file path and attach its contents to your turn.",source:"static"},{id:"kbd:cmd-v",text:"Paste an image with Cmd+V (macOS) or Ctrl+V \u2014 clipboard images attach automatically.",source:"static"},{id:"env:tips-opt-out",text:"Set AFK_SPINNER_TIPS=0 to silence these loading-screen tips.",source:"static"}];function NN(){let e=[];for(let t of Ze())t.hint&&(t.name.includes(":")||e.push({id:`cmd:${t.name}`,text:`${t.name} \u2014 ${t.hint}`,source:"command"}));return e}function BN(e){let t=[];for(let n of ft()){let r=`cmd:/${n}`;if(e.has(r))continue;let o;try{o=$e(n)}catch{continue}Et(o,T.AFK_INTERNAL==="1")&&o.whenToUse&&t.push({id:`skill:${n}`,text:`/${n} \u2014 ${o.whenToUse}`,source:"skill"})}return t}function jk(){if(T.AFK_SPINNER_TIPS==="0")return[];let e=NN(),t=new Set(e.map(r=>r.id)),n=BN(t);return[...FN,...e,...n]}var Bk=new Set,Uk=new Map;function Wk(e,t){if(e.length===0)return null;let n=t.now??Date.now(),r=t.warmupMs??1500;if(n-t.startedAt<r)return null;let o=t.rotateMs??7e3,s=Math.floor((n-t.startedAt)/o),i=`${t.startedAt}:${s}`,a=Uk.get(i);if(a)return a;let l=null,c=e.filter(u=>!Bk.has(u.id));if(c.length>0){let u=(s%c.length+c.length)%c.length;l=c[u]??null}else{let u=(s%e.length+e.length)%e.length;l=e[u]??null}return l&&(Bk.add(l.id),Uk.set(i,l)),l}var Ap=["\u280B","\u2819","\u2839","\u2838","\u283C","\u2834","\u2826","\u2827","\u2807","\u280F"],UN=5e3;function Hk(e){let t=Date.now()-e;if(t<UN)return"";let n=Math.floor(t/1e3);if(n<60)return m.dim(` ${n}s`);let r=Math.floor(n/60),o=n%60;return m.dim(` ${r}m${o.toString().padStart(2,"0")}s`)}function Kk(e,t){let n=" \u{1F4A1} ",r=Math.max(8,t-n.length-1),o=e.length>r?e.slice(0,r-1)+"\u2026":e;return m.dim(n+o)}var Cl=class{state=null;interval=null;captureMode;onTick;constructor(t){this.captureMode=t.captureMode,this.onTick=t.onTick}set(t){if(!t.enabled){this.interval&&(clearInterval(this.interval),this.interval=null),this.state&&(this.state=null,this.onTick());return}if(this.captureMode||this.state)return;let n=t.rotateVerbEveryMs??3500,r=Date.now();this.state={frameIndex:0,verb:Rp(),nextVerbRotateAt:r+n,startedAt:r,tipPool:jk(),currentTip:null},this.interval=setInterval(()=>this.tick(n),80),this.onTick()}dispose(){this.interval&&(clearInterval(this.interval),this.interval=null),this.state=null}renderSpinnerRow(){return this.state?m.meta(`${Ap[this.state.frameIndex]} ${this.state.verb}...`)+Hk(this.state.startedAt):null}renderTipRow(t){return this.state?.currentTip?Kk(this.state.currentTip.text,t):null}tick(t){if(!this.state)return;this.state.frameIndex=(this.state.frameIndex+1)%Ap.length;let n=Date.now();n>=this.state.nextVerbRotateAt&&(this.state.verb=Rp(),this.state.nextVerbRotateAt=n+t),this.state.currentTip=Wk(this.state.tipPool,{startedAt:this.state.startedAt,now:n}),this.onTick()}};var WN=5,HN=1e3,KN=/\[Pasted text #([0-9a-f]+) \+\d+ (?:lines|chars)\]/g;function GN(e,t){let n=t.match(/\n/g),r=n?n.length:0;return r>0?`[Pasted text #${e} +${r+1} lines]`:`[Pasted text #${e} +${t.length} chars]`}var Mo=class e{stdout;stdin;onCancel;onSoftStop;softStopped=!1;onBackground;onShiftTab;promptTextFn;history;autocompleteState;formatInputBuffer;scrollRegion;anchorRow;declaredAnchorRow;onSubmit;attachments=[];pasting=!1;pasteStartBufferLen=0;pasteStartCursor=0;pasteRegistry=new Map;clipboardInFlight=!1;clipboardFailureMsg=null;inputMode="streaming";pickerController=null;pickerSavedMode="streaming";armed=!1;suspended=!1;canceled=!1;backgrounded=!1;wasRaw=!1;logUpdate=null;overlay="";input=U.seed("");queued=!1;handleKeypress=null;resizeUnsub=null;resizeImmediateUnsub=null;spinnerController;committing=!1;hasCommitted=!1;committedBand=[];committedBandTopRow=0;committedBandBottomRow=0;commitInFlight=!1;debugCompositor=!!T.AFK_DEBUG_COMPOSITOR;debugLog(t,n={}){if(!this.debugCompositor)return;let r=process.hrtime.bigint(),o=Object.entries(n).map(([s,i])=>{let a=typeof i=="string"?JSON.stringify(i.length>60?i.slice(0,57)+"...":i):String(i);return`${s}=${a}`}).join(" ");process.stderr.write(`[compositor] t=${r} ${t}${o?" "+o:""}
2078
+ `)}static MAX_DROPDOWN_ROWS=6;constructor(t){this.stdout=t.stdout,this.stdin=t.stdin,this.onCancel=t.onCancel,this.onSoftStop=t.onSoftStop,this.onBackground=t.onBackground,this.onShiftTab=t.onShiftTab;let n=t.promptText;if(typeof n=="function")this.promptTextFn=n;else if(typeof n=="string")this.promptTextFn=()=>n;else{let r=" "+m.dim("\u23AF")+" ";this.promptTextFn=()=>r}this.history=t.history,this.autocompleteState=t.autocompleteState,this.formatInputBuffer=t.formatInputBuffer,this.scrollRegion=t.scrollRegion,this.spinnerController=new Cl({captureMode:t.captureMode??!1,onTick:()=>this.repaint()}),this.onSubmit=t.onSubmit,this.anchorRow=t.anchorRow,this.declaredAnchorRow=t.anchorRow}isArmed(){return this.armed}setAnchorRow(t){this.anchorRow=t,this.declaredAnchorRow=t}suspendInput(){if(!(!this.armed||this.suspended)){if(this.logUpdate)try{this.logUpdate.clear(this.scrollRegion?.getExtraRows()??0),this.logUpdate.done()}catch{}this.handleKeypress&&this.stdin.removeListener("keypress",this.handleKeypress);try{this.stdin.setRawMode(!1)}catch{}this.suspended=!0}}resumeInput(){if(!(!this.armed||!this.suspended)){try{this.stdin.setRawMode(!0)}catch{}this.handleKeypress&&this.stdin.on("keypress",this.handleKeypress),this.suspended=!1,this.repaint()}}setOnSubmit(t){this.onSubmit=t??void 0}setOnCancel(t){this.onCancel=t??void 0}getOnCancel(){return this.onCancel}setOnSoftStop(t){this.onSoftStop=t??void 0}setOnBackground(t){this.onBackground=t??void 0}setOnShiftTab(t){this.onShiftTab=t??void 0}enterPickerMode(t){if(this.pickerController)throw new Error("enterPickerMode: a picker is already active; call exitPickerMode first");this.pickerSavedMode=this.inputMode,this.pickerController=t,this.inputMode="picker",this.autocompleteState?.reset(),this.repaint()}exitPickerMode(){this.pickerController&&(this.pickerController=null,this.inputMode=this.pickerSavedMode,this.repaint())}repaintPicker(){this.pickerController&&this.repaint()}setInputMode(t){let n=this.inputMode;if(this.inputMode=t,n==="idle"&&t==="streaming"){this.canceled=!1,this.backgrounded=!1,this.softStopped=!1,this.autocompleteState?.reset(),this.repaint();return}if(t==="idle"&&this.softStopped){this.queued=!1,this.softStopped=!1,this.repaint();return}if(t==="idle"&&this.queued&&this.onSubmit){let r=this.input.buffer,o=this.expandPastePlaceholders(r),s=[...this.attachments],i=this.onSubmit;this.queued=!1,this.input=U.seed(""),this.attachments=[],this.pasteRegistry.clear(),this.repaint(),i(o===r?{text:o,attachments:s}:{text:o,displayText:r,attachments:s});return}n!==t&&this.repaint()}getInputMode(){return this.inputMode}async arm(){if(this.armed)throw new Error("TerminalCompositor: arm() called while already armed");if(!(!this.stdout.isTTY||!this.stdin.isTTY)){this.anchorRow=this.declaredAnchorRow,this.logUpdate||(this.logUpdate=new xl(this.stdout)),this.wasRaw=this.stdin.isRaw??!1;try{this.stdin.setRawMode(!0)}catch{this.logUpdate=null,this.wasRaw=!1;return}try{this.stdout.write("\x1B[?2004h")}catch{}this.stdin.resume(),ur(this.stdin),this.handleKeypress=(t,n)=>this.dispatchKey(t,n),this.stdin.on("keypress",this.handleKeypress),this.armed=!0,this.canceled=!1,this.resizeUnsub=Ue.subscribe(()=>{this.armed&&this.repaint()}),this.resizeImmediateUnsub=Ue.subscribeImmediate(()=>{this.armed&&(this.logUpdate?.resetGeometry?.(),this.clearCommittedBand())}),this.repaint()}}disarm(){if(this.spinnerController.dispose(),!this.armed){this.resetState();return}if(this.handleKeypress&&(this.stdin.removeListener("keypress",this.handleKeypress),this.handleKeypress=null),this.resizeUnsub&&(this.resizeUnsub(),this.resizeUnsub=null),this.resizeImmediateUnsub&&(this.resizeImmediateUnsub(),this.resizeImmediateUnsub=null),this.logUpdate)try{this.logUpdate.clear(this.scrollRegion?.getExtraRows()??0),this.logUpdate.done()}catch{}if(this.stdout.isTTY&&this.stdin.isTTY){try{this.stdout.write("\x1B[?2004l")}catch{}try{this.stdin.setRawMode(this.wasRaw)}catch{}}this.armed=!1,this.resetState()}setOverlay(t){t!==this.overlay&&(this.debugLog("setOverlay",{framesLen:t.length,anchorRow:this.anchorRow??null}),this.overlay=t,this.repaint())}setSpinner(t){this.stdout.isTTY&&this.spinnerController.set(t)}commitAbove(t){this.debugLog("commitAbove:enter",{textLen:t.length,anchorRow:this.anchorRow??null,committing:this.committing,topRow:this.logUpdate?.topRow??null});let n=g=>{this.scrollRegion?this.scrollRegion.withFullScrollRegion(g):g()};if(!this.armed||!this.logUpdate){n(()=>{this.stdout.write(t+`
2079
2079
  `)});return}let r=Math.max(1,this.stdout.rows??24),o=t.endsWith(`
2080
2080
  `)?t.slice(0,-1):t,s=o.match(/\n/g)?.length??0,i=Math.max(1,s+1),a=o.split(`
2081
- `),l=this.scrollRegion?.getExtraRows()??0,c=this.logUpdate.topRow??0,u=c>1?c:Math.max(1,r-1-l),d=Math.max(this.anchorRow??1,1),m=c>1&&i<=u-d;this.commitInFlight=!0,this.committing=!0;try{this.logUpdate.clear(l),n(()=>{if(this.debugLog("commitAbove:phase1",{lineCount:i,fitsAboveFrame:m}),m)this.stdout.write(`\x1B[${r};1H${`
2081
+ `),l=this.scrollRegion?.getExtraRows()??0,c=this.logUpdate.topRow??0,u=c>1?c:Math.max(1,r-1-l),d=Math.max(this.anchorRow??1,1),p=c>1&&i<=u-d;this.commitInFlight=!0,this.committing=!0;try{this.logUpdate.clear(l),n(()=>{if(this.debugLog("commitAbove:phase1",{lineCount:i,fitsAboveFrame:p}),p)this.stdout.write(`\x1B[${r};1H${`
2082
2082
  `.repeat(i)}`);else{let g=a.map(h=>`\x1B[2K${h??""}`).join(`
2083
2083
  `);this.stdout.write(`\x1B[${d};1H${g}\x1B[${r};1H${`
2084
- `.repeat(i)}`)}})}finally{this.committing=!1,this.debugLog("commitAbove:finally")}this.hasCommitted=!0,this.debugLog("commitAbove:phase2:repaint"),this.repaint(),this.debugLog("commitAbove:phase2:done",{newTopRow:this.logUpdate.topRow??null});let f=this.logUpdate.topRow??0;if(f>1){let g=Math.max(d,f-i),h="",b=0;for(let y=0;y<a.length;y++){let w=g+y;if(w>=f)break;h+=`\x1B[${w};1H\x1B[2K${a[y]??""}`,b=y+1}h.length>0&&n(()=>{this.stdout.write(h)}),b>0?(this.committedBand=a.slice(0,b),this.committedBandTopRow=g,this.committedBandBottomRow=g+b-1):this.clearCommittedBand()}else this.clearCommittedBand();this.commitInFlight=!1,this.debugLog("commitAbove:phase3:done")}clearCommittedBand(){this.committedBand=[],this.committedBandTopRow=0,this.committedBandBottomRow=0}repositionCommittedBand(t,n,r){if(this.commitInFlight||!this.logUpdate||this.committedBand.length===0)return;let o=Math.max(this.anchorRow??1,1),s=t-1;if(s<this.committedBandBottomRow||s<o)return;let i=s-o+1,a=Math.min(this.committedBand.length,i);if(a<=0)return;let l=s-a+1,c=l!==this.committedBandTopRow||s!==this.committedBandBottomRow,u=n>0&&n<=this.committedBandBottomRow;if(!c&&!u)return;let d=this.committedBand.slice(this.committedBand.length-a),m="\x1B[?25l";for(let f=Math.max(o,this.committedBandTopRow);f<l;f++)m+=`\x1B[${f};1H\x1B[2K`;for(let f=0;f<d.length;f++)m+=`\x1B[${l+f};1H\x1B[2K${d[f]??""}`;m+=`\x1B[${Math.max(1,r)};1H`;try{this.stdout.write(m)}catch{}this.committedBandTopRow=l,this.committedBandBottomRow=s}getBuffer(){return{text:this.expandPastePlaceholders(this.input.buffer),queued:this.queued}}getAttachments(){return[...this.attachments]}renderInputLine(){let t=this.queued?" "+p.dim("[queued]"):"",n=this.input.buffer.slice(0,this.input.cursor),r=cs(this.input.buffer,this.input.cursor),o=this.input.cursor<this.input.buffer.length?this.input.buffer.slice(this.input.cursor,r):" ",s=this.input.cursor<this.input.buffer.length?this.input.buffer.slice(r):"",i=this.formatInputBuffer?.(n)??n,a=this.formatInputBuffer?.(s)??s,l=p.user.inverse(o);return this.promptTextFn()+i+l+a+t}updateAutocomplete(){let t=this.autocompleteState;if(!t)return;t.trigger=zs(this.input.buffer,this.input.cursor);let n=`${this.input.cursor}:${this.input.buffer}`;t.suppressedSignature!==null&&t.suppressedSignature!==n&&(t.suppressedSignature=null),t.trigger&&t.suppressedSignature===null?(t.trigger.kind==="slash"?t.candidates=Al(t.trigger.query).slice(0,12):t.trigger.kind==="file"?t.candidates=_l(t.trigger.query).slice(0,12):t.candidates=Js(t.trigger.command,t.trigger.query),t.dropdownOpen=t.candidates.length>0):(t.dropdownOpen=!1,t.candidates=[]),t.selectedIndex>=t.candidates.length&&(t.selectedIndex=Math.max(0,t.candidates.length-1)),t.viewportStart>t.selectedIndex&&(t.viewportStart=t.selectedIndex),t.selectedIndex>=t.viewportStart+e.MAX_DROPDOWN_ROWS&&(t.viewportStart=t.selectedIndex-e.MAX_DROPDOWN_ROWS+1)}renderDropdownRows(){let t=this.autocompleteState;if(!t?.dropdownOpen)return[];let n=this.stdout.columns||80;if(n<=40)return[];let r=Math.min(n-4,60),o=Math.min(t.candidates.length-t.viewportStart,e.MAX_DROPDOWN_ROWS),s=[];for(let i=0;i<o;i++){let a=t.viewportStart+i,l=t.candidates[a];if(!l)continue;let c=El(l,a===t.selectedIndex,r,t.trigger?.kind),u=Ie(c).length,d=Math.max(0,Math.ceil(u/n)-1);s.push(c);for(let m=0;m<d;m++)s.push("")}return s.reverse()}renderHintRow(){let t=this.autocompleteState;if(!t?.dropdownOpen)return null;let n=this.stdout.columns||80;if(n<=40)return null;let r=t.candidates[t.selectedIndex];if(!r)return null;let o=Math.min(n-4,80);return Rl(r.hint,o)??""}repaint(){if(!this.armed||!this.logUpdate||this.committing||this.suspended)return;if(this.inputMode==="picker"&&this.pickerController){this.repaintPickerFrame();return}let t=this.renderInputLine(),n=this.overlay?this.overlay.split(`
2085
- `):[],r=this.spinnerController.renderSpinnerRow(),o=this.spinnerController.renderTipRow(this.stdout.columns??80),s=null;this.attachments.length>0?s=Vs(this.attachments):this.clipboardFailureMsg!==null&&(s=p.dim(this.clipboardFailureMsg),this.clipboardFailureMsg=null);let i=this.renderDropdownRows(),a=this.renderHintRow(),l=!!r||!!o||!!s,c=l||n.length>0,u=this.scrollRegion?.getExtraRows()??0,d=Math.max(1,(this.stdout.rows??24)-1-u),m=c?1:0,f=(r?1:0)+(o?1:0)+(s?1:0)+m+i.length+(a!==null?1:0)+1,g=Math.max(0,d-f),h=n.length>g?n.slice(-g):n,b=l||h.length>0,y=[];y.push(...h),r&&y.push(r),o&&y.push(o),s&&y.push(s),b&&y.push(""),y.push(...i),a!==null&&y.push(a),y.push(t);let w=Math.max(1,(this.stdout.rows??24)-1-u),x=y.length,R=Math.max(1,w-x+1);this.preserveRowsBeforeFrameRender(R);let v=this.logUpdate.topRow??0;this.logUpdate.render(y.join(`
2084
+ `.repeat(i)}`)}})}finally{this.committing=!1,this.debugLog("commitAbove:finally")}this.hasCommitted=!0,this.debugLog("commitAbove:phase2:repaint"),this.repaint(),this.debugLog("commitAbove:phase2:done",{newTopRow:this.logUpdate.topRow??null});let f=this.logUpdate.topRow??0;if(f>1){let g=Math.max(d,f-i),h="",b=0;for(let y=0;y<a.length;y++){let w=g+y;if(w>=f)break;h+=`\x1B[${w};1H\x1B[2K${a[y]??""}`,b=y+1}if(h.length>0&&n(()=>{this.stdout.write(h)}),b>0){let y=a.slice(0,b),x=p&&b===i&&(this.anchorRow??1)<=1&&this.committedBand.length>0&&this.committedBandBottomRow===f-1?[...this.committedBand,...y]:y,R=Math.max(0,f-d),v=x.length>R?x.slice(x.length-R):x;this.committedBand=v,this.committedBandBottomRow=f-1,this.committedBandTopRow=f-v.length}else this.clearCommittedBand()}else this.clearCommittedBand();this.commitInFlight=!1,this.debugLog("commitAbove:phase3:done")}clearCommittedBand(){this.committedBand=[],this.committedBandTopRow=0,this.committedBandBottomRow=0}repositionCommittedBand(t,n,r){if(this.commitInFlight||!this.logUpdate||this.committedBand.length===0)return;let o=Math.max(this.anchorRow??1,1),s=t-1;if(s<this.committedBandBottomRow||s<o)return;let i=s-o+1,a=Math.min(this.committedBand.length,i);if(a<=0)return;let l=s-a+1,c=l!==this.committedBandTopRow||s!==this.committedBandBottomRow,u=n>0&&n<=this.committedBandBottomRow;if(!c&&!u)return;let d=this.committedBand.slice(this.committedBand.length-a),p="\x1B[?25l";for(let f=Math.max(o,this.committedBandTopRow);f<l;f++)p+=`\x1B[${f};1H\x1B[2K`;for(let f=0;f<d.length;f++)p+=`\x1B[${l+f};1H\x1B[2K${d[f]??""}`;p+=`\x1B[${Math.max(1,r)};1H`;try{this.stdout.write(p)}catch{}this.committedBandTopRow=l,this.committedBandBottomRow=s}getBuffer(){return{text:this.expandPastePlaceholders(this.input.buffer),queued:this.queued}}getAttachments(){return[...this.attachments]}renderInputLine(){let t=this.queued?" "+m.dim("[queued]"):"",n=this.input.buffer.slice(0,this.input.cursor),r=cs(this.input.buffer,this.input.cursor),o=this.input.cursor<this.input.buffer.length?this.input.buffer.slice(this.input.cursor,r):" ",s=this.input.cursor<this.input.buffer.length?this.input.buffer.slice(r):"",i=this.formatInputBuffer?.(n)??n,a=this.formatInputBuffer?.(s)??s,l=m.user.inverse(o);return this.promptTextFn()+i+l+a+t}updateAutocomplete(){let t=this.autocompleteState;if(!t)return;t.trigger=zs(this.input.buffer,this.input.cursor);let n=`${this.input.cursor}:${this.input.buffer}`;t.suppressedSignature!==null&&t.suppressedSignature!==n&&(t.suppressedSignature=null),t.trigger&&t.suppressedSignature===null?(t.trigger.kind==="slash"?t.candidates=Al(t.trigger.query).slice(0,12):t.trigger.kind==="file"?t.candidates=_l(t.trigger.query).slice(0,12):t.candidates=Js(t.trigger.command,t.trigger.query),t.dropdownOpen=t.candidates.length>0):(t.dropdownOpen=!1,t.candidates=[]),t.selectedIndex>=t.candidates.length&&(t.selectedIndex=Math.max(0,t.candidates.length-1)),t.viewportStart>t.selectedIndex&&(t.viewportStart=t.selectedIndex),t.selectedIndex>=t.viewportStart+e.MAX_DROPDOWN_ROWS&&(t.viewportStart=t.selectedIndex-e.MAX_DROPDOWN_ROWS+1)}renderDropdownRows(){let t=this.autocompleteState;if(!t?.dropdownOpen)return[];let n=this.stdout.columns||80;if(n<=40)return[];let r=Math.min(n-4,60),o=Math.min(t.candidates.length-t.viewportStart,e.MAX_DROPDOWN_ROWS),s=[];for(let i=0;i<o;i++){let a=t.viewportStart+i,l=t.candidates[a];if(!l)continue;let c=El(l,a===t.selectedIndex,r,t.trigger?.kind),u=Ie(c).length,d=Math.max(0,Math.ceil(u/n)-1);s.push(c);for(let p=0;p<d;p++)s.push("")}return s.reverse()}renderHintRow(){let t=this.autocompleteState;if(!t?.dropdownOpen)return null;let n=this.stdout.columns||80;if(n<=40)return null;let r=t.candidates[t.selectedIndex];if(!r)return null;let o=Math.min(n-4,80);return Rl(r.hint,o)??""}repaint(){if(!this.armed||!this.logUpdate||this.committing||this.suspended)return;if(this.inputMode==="picker"&&this.pickerController){this.repaintPickerFrame();return}let t=this.renderInputLine(),n=this.overlay?this.overlay.split(`
2085
+ `):[],r=this.spinnerController.renderSpinnerRow(),o=this.spinnerController.renderTipRow(this.stdout.columns??80),s=null;this.attachments.length>0?s=Vs(this.attachments):this.clipboardFailureMsg!==null&&(s=m.dim(this.clipboardFailureMsg),this.clipboardFailureMsg=null);let i=this.renderDropdownRows(),a=this.renderHintRow(),l=!!r||!!o||!!s,c=l||n.length>0,u=this.scrollRegion?.getExtraRows()??0,d=Math.max(1,(this.stdout.rows??24)-1-u),p=c?1:0,f=(r?1:0)+(o?1:0)+(s?1:0)+p+i.length+(a!==null?1:0)+1,g=Math.max(0,d-f),h=n.length>g?n.slice(-g):n,b=l||h.length>0,y=[];y.push(...h),r&&y.push(r),o&&y.push(o),s&&y.push(s),b&&y.push(""),y.push(...i),a!==null&&y.push(a),y.push(t);let w=Math.max(1,(this.stdout.rows??24)-1-u),x=y.length,R=Math.max(1,w-x+1);this.preserveRowsBeforeFrameRender(R);let v=this.logUpdate.topRow??0;this.logUpdate.render(y.join(`
2086
2086
  `),w),this.repositionCommittedBand(R,v,w)}preserveRowsBeforeFrameRender(t){let n=this.logUpdate?.topRow??0,r=this.hasCommitted&&n>1?Math.max(0,n-t):0,o=this.anchorRow!==void 0&&this.anchorRow>1&&t<this.anchorRow?this.anchorRow-t:0,s=Math.max(r,o);if(s>0&&(this.evictRowsToScrollback(s),this.anchorRow!==void 0&&this.anchorRow>1&&(this.anchorRow=Math.max(1,this.anchorRow-s)),this.committedBand.length>0)){this.committedBandTopRow-=s,this.committedBandBottomRow-=s;let i=Math.max(this.anchorRow??1,1);if(this.committedBandTopRow<i){let a=i-this.committedBandTopRow;this.committedBand=this.committedBand.slice(a),this.committedBandTopRow=i}(this.committedBand.length===0||this.committedBandBottomRow<i)&&this.clearCommittedBand()}}evictRowsToScrollback(t){if(t<=0)return;this.debugLog("evict:enter",{rows:t,anchorRow:this.anchorRow??null});let r=`\x1B[${Math.max(1,this.stdout.rows??24)};1H${`
2087
2087
  `.repeat(t)}`,o=()=>{try{this.stdout.write(r)}catch(s){this.debugLog("evict:error",{msg:s?.message??String(s)})}};this.scrollRegion!==void 0?this.scrollRegion.withFullScrollRegion(o):o()}repaintPickerFrame(){if(!this.logUpdate||!this.pickerController)return;let t=[...this.pickerController.renderRows()],n=this.overlay?this.overlay.split(`
2088
- `):[],r=this.spinnerController.renderSpinnerRow(),o=this.spinnerController.renderTipRow(this.stdout.columns??80),s=null;this.attachments.length>0?s=Vs(this.attachments):this.clipboardFailureMsg!==null&&(s=p.dim(this.clipboardFailureMsg),this.clipboardFailureMsg=null);let i=!!r||!!o||!!s,a=i||n.length>0,l=this.scrollRegion?.getExtraRows()??0,c=Math.max(1,(this.stdout.rows??24)-1-l),u=a?1:0,d=(r?1:0)+(o?1:0)+(s?1:0)+u+t.length,m=Math.max(0,c-d),f=n.length>m?n.slice(-m):n,g=i||f.length>0,h=[];if(h.push(...f),r&&h.push(r),o&&h.push(o),s&&h.push(s),g&&h.push(""),h.push(...t),h.length===0)return;let b=Math.max(1,(this.stdout.rows??24)-1-l),y=h.length,w=Math.max(1,b-y+1);this.preserveRowsBeforeFrameRender(w);let x=this.logUpdate.topRow??0;this.logUpdate.render(h.join(`
2088
+ `):[],r=this.spinnerController.renderSpinnerRow(),o=this.spinnerController.renderTipRow(this.stdout.columns??80),s=null;this.attachments.length>0?s=Vs(this.attachments):this.clipboardFailureMsg!==null&&(s=m.dim(this.clipboardFailureMsg),this.clipboardFailureMsg=null);let i=!!r||!!o||!!s,a=i||n.length>0,l=this.scrollRegion?.getExtraRows()??0,c=Math.max(1,(this.stdout.rows??24)-1-l),u=a?1:0,d=(r?1:0)+(o?1:0)+(s?1:0)+u+t.length,p=Math.max(0,c-d),f=n.length>p?n.slice(-p):n,g=i||f.length>0,h=[];if(h.push(...f),r&&h.push(r),o&&h.push(o),s&&h.push(s),g&&h.push(""),h.push(...t),h.length===0)return;let b=Math.max(1,(this.stdout.rows??24)-1-l),y=h.length,w=Math.max(1,b-y+1);this.preserveRowsBeforeFrameRender(w);let x=this.logUpdate.topRow??0;this.logUpdate.render(h.join(`
2089
2089
  `),b),this.repositionCommittedBand(w,x,b)}resetState(){this.overlay="",this.input=U.seed(""),this.queued=!1,this.canceled=!1,this.backgrounded=!1,this.softStopped=!1,this.anchorRow=void 0,this.hasCommitted=!1,this.clearCommittedBand(),this.commitInFlight=!1,this.pickerController=null,this.inputMode="streaming",this.attachments=[],this.pasting=!1,this.pasteStartBufferLen=0,this.pasteStartCursor=0,this.pasteRegistry.clear(),this.clipboardFailureMsg=null,this.autocompleteState?.reset(),this.resizeUnsub&&(this.resizeUnsub(),this.resizeUnsub=null),this.resizeImmediateUnsub&&(this.resizeImmediateUnsub(),this.resizeImmediateUnsub=null)}applyEdit(t){return t===this.input?!1:(this.input=t,this.queued=!1,this.pasting||(this.updateAutocomplete(),this.repaint()),!0)}expandPastePlaceholders(t){return this.pasteRegistry.size===0?t:t.replace(KN,(n,r)=>this.pasteRegistry.get(r)??n)}maybeTruncatePaste(){let t=this.pasteStartCursor,n=this.input.cursor;if(n<=t)return;let o=this.input.buffer.slice(t,n).replace(/\x1b\[200~/g,"").replace(/\x1b\[201~/g,""),s=o.match(/\n/g),i=s?s.length:0,a=o.length;if(i<WN&&a<HN)return;let l=jN(4).toString("hex");this.pasteRegistry.set(l,o);let c=GN(l,o);this.input=U.replaceRange(this.input,{start:t,end:n},c)}maybeAtomicPlaceholderDelete(t){if(this.pasteRegistry.size===0)return null;let n=this.input.buffer,r=this.input.cursor;if(t==="backward"){let l=/\[Pasted text #([0-9a-f]+) \+\d+ (?:lines|chars)\]$/.exec(n.slice(0,r));if(!l)return null;let c=r-l[0].length,u=l[1],d=U.replaceRange(this.input,{start:c,end:r},"");return this.pasteRegistry.delete(u),d}let o=/^\[Pasted text #([0-9a-f]+) \+\d+ (?:lines|chars)\]/.exec(n.slice(r));if(!o)return null;let s=r+o[0].length,i=o[1],a=U.replaceRange(this.input,{start:r,end:s},"");return this.pasteRegistry.delete(i),a}applyDropdownSelection(){let t=this.autocompleteState;if(!t?.dropdownOpen||t.candidates.length===0)return!1;let n=t.candidates[t.selectedIndex];if(!n)return!1;let r=this.input.buffer.slice(0,this.input.cursor),o=this.input.buffer.slice(this.input.cursor),s,i;if(t.trigger?.kind==="slash"){let l=/\/[A-Za-z_-]*$/.exec(r);s=l?r.length-l[0].length:this.input.cursor,i=n.value+(o.startsWith(" ")?"":" ")}else if(t.trigger?.kind==="flag"){let l=/--[a-z0-9-]*$/.exec(r);s=l?r.length-l[0].length:this.input.cursor,i=n.value+(o.startsWith(" ")?"":" ")}else{let l=r.search(/[^\s]*$/);s=l>=0?l:this.input.cursor,i=n.value}let a=U.replaceRange(this.input,{start:s,end:this.input.cursor},i);return a===this.input?!1:(this.input=a,t.dropdownOpen=!1,t.candidates=[],t.viewportStart=0,t.selectedIndex=0,this.queued=!1,this.updateAutocomplete(),this.repaint(),!0)}dispatchKey(t,n){if(!this.armed||this.handlePickerKey(t,n))return;let r=n?.sequence??"";this.handlePasteMarkers(r)||this.handleEscape(n)||this.handleInterrupt(n)||this.handleClipboardImageKey(n)||this.handleVerticalNav(n)||this.handleEnter(n,r)||this.handleBackspace(n)||this.handleCursorAndEdit(n)||this.handleBackground(n)||this.handleTab(n)||this.handlePrintable(t,n)}handlePickerKey(t,n){return this.inputMode==="picker"&&this.pickerController?(this.pickerController.onKey(t,n),!0):!1}handlePasteMarkers(t){if(t==="\x1B[200~")return this.pasting=!0,this.pasteStartBufferLen=this.input.buffer.length,this.pasteStartCursor=this.input.cursor,!0;if(t==="\x1B[201~"){this.pasting=!1;let n=r=>{this.clipboardInFlight||(this.clipboardInFlight=!0,br().then(o=>{o?(this.clipboardFailureMsg=null,this.attachments.push(o),this.repaint()):r==="flag-missing"&&(this.clipboardFailureMsg="[clipboard: no image found]",this.repaint())}).catch(()=>{}).finally(()=>{this.clipboardInFlight=!1}))};return this.input.buffer.length===this.pasteStartBufferLen?n("flag-missing"):(this.maybeTruncatePaste(),this.updateAutocomplete(),this.repaint(),n("silent")),!0}return!1}handleEscape(t){if(t?.name!=="escape")return!1;let n=this.autocompleteState;return n?.dropdownOpen?(n.suppressedSignature=`${this.input.cursor}:${this.input.buffer}`,n.dropdownOpen=!1,n.candidates=[],this.repaint(),!0):(this.inputMode==="idle"||this.softStopped||(this.input.buffer.length>0&&!this.queued&&(this.queued=!0,this.repaint()),this.softStopped=!0,this.onSoftStop&&this.onSoftStop()),!0)}handleInterrupt(t){return t?.ctrl&&t?.name==="c"?this.inputMode==="idle"?(this.onCancel&&this.onCancel(),!0):(this.canceled||(this.canceled=!0,this.input.buffer.length>0&&!this.queued&&(this.queued=!0,this.repaint()),this.onCancel&&this.onCancel()),!0):!1}handleClipboardImageKey(t){return t?.ctrl&&t?.name==="v"?(this.clipboardInFlight||(this.clipboardInFlight=!0,br().then(n=>{n?(this.clipboardFailureMsg=null,this.attachments.push(n)):this.clipboardFailureMsg="[clipboard: no image found]",this.repaint()}).catch(()=>{}).finally(()=>{this.clipboardInFlight=!1})),!0):!1}handleVerticalNav(t){let n=this.autocompleteState;if(t?.ctrl&&t?.name==="p"||t?.name==="up"){if(n?.dropdownOpen)return n.selectedIndex>0&&(n.selectedIndex--,n.selectedIndex<n.viewportStart&&(n.viewportStart=n.selectedIndex),this.repaint()),!0;if(this.history){let r=this.history.back(this.input.buffer);r!==null&&this.applyEdit(U.seed(r))}return!0}if(t?.ctrl&&t?.name==="n"||t?.name==="down"){if(n?.dropdownOpen)return n.selectedIndex<n.candidates.length-1&&(n.selectedIndex++,n.selectedIndex>=n.viewportStart+e.MAX_DROPDOWN_ROWS&&(n.viewportStart=n.selectedIndex-e.MAX_DROPDOWN_ROWS+1),this.repaint()),!0;if(this.history){let r=this.history.forward();r!==null&&this.applyEdit(U.seed(r))}return!0}return!1}handleEnter(t,n){if(t?.name!=="return")return!1;let r=t?.shift===!0||n==="\x1B[13;2u",o=t?.meta===!0;if(r||o)return this.history?.resetRecall(),this.applyEdit(U.insert(this.input,`
2090
2090
  `)),!0;if(this.pasting)return this.input=U.insert(this.input,`
2091
- `),this.queued=!1,!0;let s=this.autocompleteState;if(s?.dropdownOpen){let i=s.trigger?.kind,a=this.applyDropdownSelection();if(i!=="slash"||!a)return!0}if(this.input.buffer.length===0&&this.attachments.length===0)return!0;if(this.inputMode==="idle"&&this.onSubmit){let i=this.input.buffer,a=this.expandPastePlaceholders(i),l=[...this.attachments],c=this.onSubmit;return this.queued=!1,this.input=U.seed(""),this.attachments=[],this.pasteRegistry.clear(),this.autocompleteState?.reset(),this.repaint(),c(a===i?{text:a,attachments:l}:{text:a,displayText:i,attachments:l}),!0}return this.queued||(this.queued=!0,this.repaint()),!0}handleBackspace(t){if(t?.name!=="backspace")return!1;if(t?.meta){let o=U.deleteWordBackward(this.input);return o!==this.input&&(this.history?.resetRecall(),this.applyEdit(o)),!0}let n=this.maybeAtomicPlaceholderDelete("backward");if(n)return this.history?.resetRecall(),this.applyEdit(n),!0;let r=U.backspace(this.input);return r!==this.input?(this.history?.resetRecall(),this.applyEdit(r)):this.queued?(this.queued=!1,this.repaint()):this.attachments.length>0&&(this.attachments.pop(),this.repaint()),!0}handleCursorAndEdit(t){if(t?.ctrl&&t?.name==="a")return this.applyEdit(U.moveLineStart(this.input)),!0;if(t?.ctrl&&t?.name==="e")return this.applyEdit(U.moveLineEnd(this.input)),!0;if((t?.meta||t?.ctrl)&&t?.name==="left")return this.applyEdit(U.moveWordBackward(this.input)),!0;if((t?.meta||t?.ctrl)&&t?.name==="right")return this.applyEdit(U.moveWordForward(this.input)),!0;if(t?.meta&&t?.name==="b")return this.applyEdit(U.moveWordBackward(this.input)),!0;if(t?.meta&&t?.name==="f")return this.applyEdit(U.moveWordForward(this.input)),!0;if(t?.ctrl&&t?.name==="w"){let n=U.deleteWordBackward(this.input);return n!==this.input&&(this.history?.resetRecall(),this.applyEdit(n)),!0}if(t?.ctrl&&t?.name==="u"){let n=U.deleteToLineStart(this.input);return n!==this.input&&(this.history?.resetRecall(),this.applyEdit(n)),!0}if(t?.ctrl&&t?.name==="k"){let n=U.deleteToLineEnd(this.input);return n!==this.input&&(this.history?.resetRecall(),this.applyEdit(n)),!0}if(t?.name==="left")return this.applyEdit(U.moveLeft(this.input)),!0;if(t?.name==="right")return this.applyEdit(U.moveRight(this.input)),!0;if(t?.name==="home")return this.applyEdit(U.moveHome(this.input)),!0;if(t?.name==="end")return this.applyEdit(U.moveEnd(this.input)),!0;if(t?.name==="delete"){if(t?.meta){let r=U.deleteWordForward(this.input);return r!==this.input&&(this.history?.resetRecall(),this.applyEdit(r)),!0}let n=this.maybeAtomicPlaceholderDelete("forward");return n?(this.history?.resetRecall(),this.applyEdit(n),!0):(this.history?.resetRecall(),this.applyEdit(U.deleteForward(this.input)),!0)}return!1}handleBackground(t){return t?.ctrl&&t?.name==="b"?(this.inputMode==="idle"||this.backgrounded||(this.backgrounded=!0,this.onBackground&&this.onBackground()),!0):!1}handleTab(t){return t?.name==="tab"&&t?.shift?(this.onShiftTab&&this.onShiftTab(),!0):t?.name==="tab"?(this.applyDropdownSelection(),!0):!1}handlePrintable(t,n){let r=["tab","pageup","pagedown"];if(n?.name&&r.includes(n.name)||n?.ctrl||n?.meta)return;let o=typeof t=="string"&&t.length===1&&t>=" "?t:typeof n?.sequence=="string"&&n.sequence.length===1&&n.sequence>=" "?n.sequence:null;o!==null&&(this.history?.resetRecall(),this.applyEdit(U.insert(this.input,o)))}};import qN from"chalk";var zN=/(?<=\s|^)(\/[A-Za-z][\w:-]*)(?=\s|$)/g,JN=/(?<=\s|^)(@[\w./-]*)(?=\s|$)/g,VN=/\[Pasted text #[0-9a-f]+ \+\d+ (?:lines|chars)\]/g,Gk={mint:p.mint};function YN(e){let t=Gk[e];if(t)return t;let n=e.lastIndexOf(":");if(n>=0){let r=e.slice(n+1),o=Gk[r];if(o)return o}return null}function Wn(e,t){return qN.level===0?e:e.replace(zN,o=>{let s=o.slice(1);return t.has(s)?(YN(s)??p.brand)(o):p.meta(o)}).replace(JN,o=>p.fileRef(o)).replace(VN,o=>p.meta(o))}G();var qk=/[\x00-\x1F\x7F-\x9F]/g;function Ke(e){return Ie(e).replace(qk," ").replace(/ {2,}/g," ").trim()}function _p(e){return Ie(e).replace(qk," ")}function Ys(e){let t=/[a-z][a-z0-9+.-]*:\/\/[^\s<>\"`]+/gi,n="",r=0;for(let o of e.matchAll(t)){let s=o[0]??"",i=o.index??r;n+=zk(e.slice(r,i)),n+=s,r=i+s.length}return n+=zk(e.slice(r)),n}function zk(e){return e.replace(/\/(?:[^/\s,)]+\/){2,}([^/\s,)]+)/g,"$1")}function XN(e){let t=/^(\s*[("]?\s*)cd\s+\S+\s+&&\s+(?!cd\s)(.+)$/.exec(e);return t?(t[1]??"")+(t[2]??""):e}var Jk=60;function Cp(e,t){let n=e.trim().replace(/^\((.*)\)$/s,"$1"),r;try{r=JSON.parse(n)}catch{return e}if(!r||typeof r!="object")return e;let o=r;for(let s of t){let i=o[s];if(typeof i=="string"&&i.length>0){let a=Ke(i);if(a.length===0)continue;return`(${a.length>Jk?ae(a,Jk,"\u2026"):a})`}}return e}function Vk(e,t){if(e==="bash"||e==="Bash")return XN(t);if(e==="compose"||e==="Compose"){let n=t.trim().replace(/^\((.*)\)$/s,"$1");try{let r=JSON.parse(n);if(r&&typeof r=="object"){let o=Array.isArray(r.nodes)?r.nodes.length:void 0,s=Array.isArray(r.edges)?r.edges.length:0;if(o!==void 0){let i=`${o} node${o===1?"":"s"}`,a=s>0?`, ${s} edge${s===1?"":"s"}`:"";return`(${i}${a})`}}}catch{}}return e==="agent"||e==="Agent"?Cp(t,["id_prefix","prompt"]):e==="Task"?Cp(t,["description","prompt"]):e==="skill"||e==="Skill"?Cp(t,["name","arguments"]):e==="ask_question"?ZN(t):t}function ZN(e){let t=e.trim().replace(/^\((.*)\)$/s,"$1");try{let n=JSON.parse(t);if(!n||typeof n!="object")return e;let r=n,o=typeof r.type=="string"?r.type:"text";if(o==="choice"||o==="multi_choice"){let s=r.choices,i=Array.isArray(s)?s.length:0;return i>0?`(${o}: ${i} option${i===1?"":"s"})`:`(${o})`}return`(${o})`}catch{return e}}function QN(e){return e.replace(/[\r\n]+/g," ")}var eB={"(":")","{":"}","[":"]"};function Yk(e,t,n="\u2026"){let r=e.charAt(0),o=eB[r];return!(o&&e.endsWith(o)&&e.length>=2)||z(e)<=t?ae(e,t,n):t<3?t>=2?r+o:ae(e,t,n):ae(e,t-1,n)+o}function fn(e,t){let n=/^([A-Za-z_][A-Za-z0-9_]*)(.*)$/s.exec(e);if(n){let r=n[1],o=Ys(Vk(r,n[2]??"")),s=Mn(r),{color:i,glyph:a}=ko(r),l=fd(s),c=l?` [${l}]`:"";if(t!==void 0){let d=(a+" ").length+r.length+c.length,m=Math.max(1,t-d);o=Yk(o,m)}o=QN(o);let u=i(a+" ")+i.bold(r)+p.toolArg(o);return l?u+p.dim(c):u}return p.chrome("\u25CF ")+p.toolArg(e)}G();var Xk=8,Il=30;function tB(){let e=T.AFK_DIFF_LINES;if(e===void 0)return Il;let t=e.trim();if(!/^\d+$/.test(t))return Il;let n=Number.parseInt(t,10);return Number.isFinite(n)?n:Il}function nB(){let e=T.AFK_SHOW_DIFFS;if(e===void 0)return!1;let t=e.trim().toLowerCase();return t==="0"||t==="false"||t==="no"||t==="off"}function rB(e){let t=p.diffAdd(`+${e.addedLines}`),n=p.diffRemove(`-${e.removedLines}`),r=e.hunks.length,o=p.dim(`across ${r} hunk${r===1?"":"s"}`);return`${t} ${n} ${o}`}var oB=/[\x00-\x08\x0B-\x1F\x7F-\x9F]/g;function sB(e){let t=Ie(e.text).replace(oB,"");return e.kind==="+"?p.diffAdd("+ "+t):e.kind==="-"?p.diffRemove("- "+t):p.dim(" "+t)}var Pp=new WeakMap;function gn(e,t,n){if(nB())return[];if(e.hunks.length===0)return[];let r=t==="overlay"?Xk:tB(),o=t+"|"+n+"|"+r,s=Pp.get(e);if(s!==void 0){let f=s.get(o);if(f!==void 0)return f}let i=[];i.push(n+rB(e));let a=[];for(let f of e.hunks){let g=`@@ -${f.oldStart},${f.oldLines} +${f.newStart},${f.newLines} @@`;a.push({kind:"header",text:p.diffHunk(g)});for(let h of f.lines)a.push({kind:"body",text:sB(h)})}if(r===0){for(let f of a)i.push(n+f.text);return Ip(e,o,i),i}let l=0;for(let f of a)f.kind==="body"&&l++;if(l<=r){for(let f of a)i.push(n+f.text);return Ip(e,o,i),i}let c=0;for(let f of a)f.kind==="header"?i.push(n+f.text):c<r&&(i.push(n+f.text),c++);let u=l-r,d=`line${u===1?"":"s"}`,m=t==="flush"?" (set AFK_DIFF_LINES=0 to expand)":"";return i.push(n+p.dim(`\u2026 +${u} more diff ${d}${m}`)),Ip(e,o,i),i}function Ip(e,t,n){let r=Pp.get(e);r===void 0&&(r=new Map,Pp.set(e,r)),r.set(t,n)}var iB=p.success("\u2713"),aB=p.error("\u2717");function wr(e){return e?aB:iB}var Xs=3,Zk=2,Qk=3;function ev(e){switch(Mn(e)){case"read":return"Reading\u2026";case"write":return"Writing\u2026";case"web":return"Fetching\u2026";case"shell":return"Running\u2026";default:return"Running\u2026"}}function lB(e,t){return e?e==="grep"||e==="Grep"?t===1?"match":"matches":e==="glob"||e==="Glob"?t===1?"path":"paths":t===1?"line":"lines":t===1?"line":"lines"}function Hn(e,t,n=60,r){let o=e.isError?p.error:p.dim,s=t??T.HOME??"___NOHOME___";if(e.display!==void 0&&!e.isError)return o(e.display);if(e.persistedPath){let a=e.persistedPath.startsWith(s)?"~"+e.persistedPath.slice(s.length):e.persistedPath;return o(`saved \u2192 ${a}`)}if(e.lineCount!==void 0&&e.lineCount>1)return o(`${e.lineCount} ${lB(r,e.lineCount)}`);let i=e.content.length>n?e.content.slice(0,n-3)+"\u2026":e.content;return o(Ke(i))}G();function Zs(e,t){if(e.length<=t)return e;let n=e.slice(0,t),r=e.slice(t),o=nv(r);return[...n,{kind:"overflow",count:r.length,text:o}]}function cB(e,t){return t<=1||e.endsWith("s")||/(sh|ch|x|z)$/i.test(e)?e:e+"s"}var uB=5,tv=60;function nv(e){let t=new Map,n=0;for(let s of e)s.kind==="group"?(t.set(s.toolName,(t.get(s.toolName)??0)+s.entries.length),n+=s.entries.length):(t.set(s.toolName,(t.get(s.toolName)??0)+1),n+=1);if(n===0)return"";if(e.length>0&&e.every(s=>Je.has(s.toolName))&&new Set(e.map(s=>s.toolName)).size===1){let s=[],i=!1;for(let a of e){let c=(a.kind==="group"?a.label:a.toolInput).trim();if(!c.startsWith("(")||!c.endsWith(")")){i=!0;break}let u=c.slice(1,-1),d=Ke(u);if(!d){i=!0;break}let m=d.length>tv?d.slice(0,tv-1)+"\u2026":d,f=a.kind==="group"?a.entries.length:1;s.push({display:m,entries:f})}if(!i&&s.length>0){let a=s.slice(0,uB),l=a.map(({display:m,entries:f})=>f>1?`${m} \xD7${f}`:m),c=a.reduce((m,f)=>m+f.entries,0),u=n-c,d=l.join(", ")+(u>0?` (+${u})`:"");return`\u2026 +${n} more: ${d}`}}let o=[];for(let[s,i]of t)o.push(`${i} ${cB(s,i)}`);return`\u2026 +${n} (${o.join(", ")})`}function Pl(e,t=It()){return e.map((n,r)=>({sibling:n,connector:r===e.length-1?t.lastConnector:t.midConnector}))}function Mp(e,t){return t?[...e,{kind:"resultSummary",summary:t}]:e}function Op(e){if(e.length===0)return[];let t=new Map;for(let o of e){let s=rv(o),i=t.get(s);i||(i=[],t.set(s,i)),i.push(o)}let n=[],r=new Set;for(let o of e){let s=rv(o),i=t.get(s);i.length>=dB(o.toolName)?r.has(s)||(n.push({kind:"group",toolName:o.toolName,label:Je.has(o.toolName)?o.toolInput:"",entries:i}),r.add(s)):n.push(o)}return n}function $p(e){let t=e.entries.length,n=e.entries.filter(a=>a.result),r=n.filter(a=>a.result.isError),o=n.length,s;if(r.length>0){let a=o-r.length,l=[];a>0&&l.push(`${a} ok`),l.push(`${r.length} error${r.length===1?"":"s"}`),s=l.join(", ")}else o===t?s=`${t} done`:o===0?s=`${t} running`:s=`${o}/${t} done`;return fn(e.toolName+e.label)+p.dim(` \xD7${t} \u2014 ${s}`)}function rv(e){return Je.has(e.toolName)?e.toolName+"::"+e.toolInput:e.toolName}function dB(e){return Je.has(e)?Zk:Qk}function ov(e,t,n,r){return e>0||t?Oo([...n,!0],r):Oo(n,r)}function Ml(e,t,n,r=Y(),o=[],s=It()){let i=Oo(o,s),a=Qs(i,s),l=e.filter(g=>g.kind==="text"),c=e.filter(g=>g.kind==="tool"),u=Op(c),d=Zs(u,Xs),m=Pl(d,s);for(let{sibling:g,connector:h}of m){let b=p.dim(h),y=h===s.lastConnector;if(g.kind==="overflow")n.push(Pe(a+b+p.dim(g.text),r));else if(g.kind==="group")n.push(Pe(a+b+$p(g),r));else if(g.kind==="resultSummary")n.push(Pe(a+b+p.dim(g.summary),r));else{let w=g,x=t.get(w.toolUseId);if(Je.has(w.toolName)&&x&&x.length>0){if(w.headerEmitted?n.push(Pe(a+b,r)):n.push(Pe(a+b+w.prefix,r)),Ml(x,t,n,r,[...o,y],s),w.thinkingTail){let R=Qs(Oo([...o,y],s),s);n.push(Pe(R+p.thinking("\u2307 "+Ke(w.thinkingTail)),r))}}else if(w.result){if(n.push(Pe(a+b+w.prefix+p.dim(" \u2014 ")+wr(w.result.isError)+" "+Hn(w.result,void 0,60,w.toolName),r)),w.diff&&!w.result.isError){let R=a+(y?s.spineClosed:p.dim(s.spine))+" ";for(let v of gn(w.diff,"overlay",R))n.push(v)}}else{n.push(Pe(a+b+w.prefix,r));let R=a+(y?s.spineClosed:p.dim(s.spine))+" ";w.thinkingTail?n.push(Pe(R+p.thinking("\u2307 "+Ke(w.thinkingTail)),r)):n.push(Pe(R+p.dim(ev(w.toolName)),r))}}}let f=ov(c.length,!1,o,s);for(let g of l)for(let h of Dp(g.text,f,s))n.push(Pe(h,r))}function ei(e,t,n,r,o=Y(),s=[],i=It()){let a=Oo(s,i),l=Qs(a,i),c=[],u=e.filter(y=>y.kind==="text"),d=e.filter(y=>y.kind==="tool"),m=Op(d),f=Zs(m,Xs),g=Mp(f,r),h=Pl(g,i);for(let{sibling:y,connector:w}of h){let x=p.dim(w),R=w===i.lastConnector;if(y.kind==="overflow")c.push(Pe(l+x+p.dim(y.text),o));else if(y.kind==="resultSummary")c.push(Pe(l+x+p.dim(y.summary),o));else if(y.kind==="group")c.push(Pe(l+x+$p(y),o));else{let v=y,E=t.get(v.toolUseId);if(Je.has(v.toolName)&&E&&E.length>0){if(v.headerEmitted){let I=v.toolInput?`${v.toolName} ${Ke(v.toolInput)}`:v.toolName;c.push(Pe(l+x+p.dim("\u21B3 "+I),o))}else c.push(Pe(l+x+v.prefix,o));c.push(...ei(E,t,n,void 0,o,[...s,R],i))}else if(v.result){if(c.push(Pe(l+x+v.prefix+p.dim(" \u2014 ")+wr(v.result.isError)+" "+Hn(v.result,n,60,v.toolName),o)),v.diff&&!v.result.isError){let I=l+(R?i.spineClosed:p.dim(i.spine))+" ";for(let D of gn(v.diff,"flush",I))c.push(D)}}else c.push(Pe(l+x+v.prefix,o))}}let b=ov(d.length,r!=null,s,i);for(let y of u)for(let w of Dp(y.text,b,i))c.push(Pe(w,o));return c}function ti(e,t,n,r,o=0){let s=It(),i=t.filter(h=>h.kind==="tool"),l=i.filter(h=>h.result).reduce((h,b)=>h+(b.result.lineCount??0),0),c=[];i.length>Xs&&(c.push(`${i.length} tools`),l>0&&c.push(`${l} lines`));let u=p.dim(s.spine.repeat(o)),d=Array.from({length:o},()=>!1),m=p.dim(s.turnRoot),f=c.length>0?u+m+e.prefix+p.dim(" \u2014 "+c.join(" \xB7 ")):u+m+e.prefix,g=ei(t,n,r,e.agentResultSummary,Y(),d,s);return[f,...g].join(`
2092
- `)}function Lp(e,t=0){let n=It(),r=p.dim(n.spine.repeat(t)),o=p.dim(n.turnRoot);return r+o+e.prefix}function ni(e,t,n,r,o=0){let s=It(),i=Array.from({length:o},()=>!1);return ei(t,n,r,e.agentResultSummary,Y(),i,s)}function $o(e,t,n){let r=[];for(let o of t){let s=e.get(o);if(s.length===1){let i=s[0];if(i.result){if(r.push(" "+i.prefix+p.dim(" \u2014 ")+wr(i.result.isError)+" "+Hn(i.result,n,60,i.toolName)),i.diff&&!i.result.isError)for(let a of gn(i.diff,"flush"," "))r.push(a)}else r.push(" "+i.prefix)}else{r.push(sv(o,s,n));let i=s.filter(l=>l.diff&&l.result&&!l.result.isError),a=i.length>1;for(let l of i){if(a){let c=Ke(Ys(l.toolInput).trim()||l.toolInput.trim());r.push(" "+p.dim(`\u2500\u2500 ${c} \u2500\u2500`))}for(let c of gn(l.diff,"flush"," "))r.push(c)}}}return r}function sv(e,t,n){let{color:r,glyph:o}=ko(e),s=t.map(u=>Ke(Ys(u.toolInput).trim())),i=r(o+" ")+r.bold(e)+p.dim(` \xD7${t.length}`)+" "+p.toolArg(s.join(", ")),a=t.filter(u=>u.result),l=a.filter(u=>u.result.isError);if(l.length>0){let u=a.length-l.length,m=a.filter(g=>!g.result.isError).map(g=>g.result.lineCount).filter(g=>g!==void 0).reduce((g,h)=>g+h,0),f=[];return m>0&&f.push(`${m} lines`),u>0&&f.push(`${u} ok`),f.push(p.error(`${l.length} error${l.length>1?"s":""}`))," "+i+p.dim(" \u2014 ")+f.join(p.dim(", "))}let c=a.map(u=>u.result?.lineCount).filter(u=>u!==void 0);if(c.length===a.length&&c.length>0){if(c.every(m=>m===c[0]))return" "+i+p.dim(` \u2014 ${c[0]} lines each`);let d=c.reduce((m,f)=>m+f,0);return" "+i+p.dim(` \u2014 ${d} lines total`)}if(a.length>0){let u=a.map(d=>Hn(d.result,n,60,d.toolName));return" "+i+p.dim(" \u2014 ")+u.join(p.dim(", "))}return" "+i}function Pe(e,t){return ae(e,t)}var Fp=Object.freeze({spine:"\u2502 ",spineClosed:" ",lead:" ",turnRoot:"\u25C9 ",midConnector:"\u251C\u2500 ",lastConnector:"\u2570\u2500 ",textPrefix:"\u2502 "}),pB=Object.freeze({spine:"| ",spineClosed:" ",lead:" ",turnRoot:"o ",midConnector:"+- ",lastConnector:"\\- ",textPrefix:"| "}),Pie=Fp.midConnector,Mie=Fp.lastConnector;function It(){let e=T.AGENT_AFK_ASCII;return e&&/^(1|true|yes)$/i.test(e)?pB:Fp}function Oo(e,t){let n="";for(let r of e)n+=r?t.spineClosed:t.spine;return n+=t.spine,n}function Qs(e,t){let n="";for(let r=0;r<e.length;r+=2){let o=e.slice(r,r+2);n+=o===t.spine?p.dim(o):o}return n}function Dp(e,t,n){if(!e||!e.trim())return[];let r=p.dim(n.textPrefix),o=Math.max(1,Y()-t.length-2-2),s=Qs(t,n),i=[];for(let a of e.split(`
2091
+ `),this.queued=!1,!0;let s=this.autocompleteState;if(s?.dropdownOpen){let i=s.trigger?.kind,a=this.applyDropdownSelection();if(i!=="slash"||!a)return!0}if(this.input.buffer.length===0&&this.attachments.length===0)return!0;if(this.inputMode==="idle"&&this.onSubmit){let i=this.input.buffer,a=this.expandPastePlaceholders(i),l=[...this.attachments],c=this.onSubmit;return this.queued=!1,this.input=U.seed(""),this.attachments=[],this.pasteRegistry.clear(),this.autocompleteState?.reset(),this.repaint(),c(a===i?{text:a,attachments:l}:{text:a,displayText:i,attachments:l}),!0}return this.queued||(this.queued=!0,this.repaint()),!0}handleBackspace(t){if(t?.name!=="backspace")return!1;if(t?.meta){let o=U.deleteWordBackward(this.input);return o!==this.input&&(this.history?.resetRecall(),this.applyEdit(o)),!0}let n=this.maybeAtomicPlaceholderDelete("backward");if(n)return this.history?.resetRecall(),this.applyEdit(n),!0;let r=U.backspace(this.input);return r!==this.input?(this.history?.resetRecall(),this.applyEdit(r)):this.queued?(this.queued=!1,this.repaint()):this.attachments.length>0&&(this.attachments.pop(),this.repaint()),!0}handleCursorAndEdit(t){if(t?.ctrl&&t?.name==="a")return this.applyEdit(U.moveLineStart(this.input)),!0;if(t?.ctrl&&t?.name==="e")return this.applyEdit(U.moveLineEnd(this.input)),!0;if((t?.meta||t?.ctrl)&&t?.name==="left")return this.applyEdit(U.moveWordBackward(this.input)),!0;if((t?.meta||t?.ctrl)&&t?.name==="right")return this.applyEdit(U.moveWordForward(this.input)),!0;if(t?.meta&&t?.name==="b")return this.applyEdit(U.moveWordBackward(this.input)),!0;if(t?.meta&&t?.name==="f")return this.applyEdit(U.moveWordForward(this.input)),!0;if(t?.ctrl&&t?.name==="w"){let n=U.deleteWordBackward(this.input);return n!==this.input&&(this.history?.resetRecall(),this.applyEdit(n)),!0}if(t?.ctrl&&t?.name==="u"){let n=U.deleteToLineStart(this.input);return n!==this.input&&(this.history?.resetRecall(),this.applyEdit(n)),!0}if(t?.ctrl&&t?.name==="k"){let n=U.deleteToLineEnd(this.input);return n!==this.input&&(this.history?.resetRecall(),this.applyEdit(n)),!0}if(t?.name==="left")return this.applyEdit(U.moveLeft(this.input)),!0;if(t?.name==="right")return this.applyEdit(U.moveRight(this.input)),!0;if(t?.name==="home")return this.applyEdit(U.moveHome(this.input)),!0;if(t?.name==="end")return this.applyEdit(U.moveEnd(this.input)),!0;if(t?.name==="delete"){if(t?.meta){let r=U.deleteWordForward(this.input);return r!==this.input&&(this.history?.resetRecall(),this.applyEdit(r)),!0}let n=this.maybeAtomicPlaceholderDelete("forward");return n?(this.history?.resetRecall(),this.applyEdit(n),!0):(this.history?.resetRecall(),this.applyEdit(U.deleteForward(this.input)),!0)}return!1}handleBackground(t){return t?.ctrl&&t?.name==="b"?(this.inputMode==="idle"||this.backgrounded||(this.backgrounded=!0,this.onBackground&&this.onBackground()),!0):!1}handleTab(t){return t?.name==="tab"&&t?.shift?(this.onShiftTab&&this.onShiftTab(),!0):t?.name==="tab"?(this.applyDropdownSelection(),!0):!1}handlePrintable(t,n){let r=["tab","pageup","pagedown"];if(n?.name&&r.includes(n.name)||n?.ctrl||n?.meta)return;let o=typeof t=="string"&&t.length===1&&t>=" "?t:typeof n?.sequence=="string"&&n.sequence.length===1&&n.sequence>=" "?n.sequence:null;o!==null&&(this.history?.resetRecall(),this.applyEdit(U.insert(this.input,o)))}};import qN from"chalk";var zN=/(?<=\s|^)(\/[A-Za-z][\w:-]*)(?=\s|$)/g,JN=/(?<=\s|^)(@[\w./-]*)(?=\s|$)/g,VN=/\[Pasted text #[0-9a-f]+ \+\d+ (?:lines|chars)\]/g,Gk={mint:m.mint};function YN(e){let t=Gk[e];if(t)return t;let n=e.lastIndexOf(":");if(n>=0){let r=e.slice(n+1),o=Gk[r];if(o)return o}return null}function Wn(e,t){return qN.level===0?e:e.replace(zN,o=>{let s=o.slice(1);return t.has(s)?(YN(s)??m.brand)(o):m.meta(o)}).replace(JN,o=>m.fileRef(o)).replace(VN,o=>m.meta(o))}G();var qk=/[\x00-\x1F\x7F-\x9F]/g;function Ke(e){return Ie(e).replace(qk," ").replace(/ {2,}/g," ").trim()}function _p(e){return Ie(e).replace(qk," ")}function Ys(e){let t=/[a-z][a-z0-9+.-]*:\/\/[^\s<>\"`]+/gi,n="",r=0;for(let o of e.matchAll(t)){let s=o[0]??"",i=o.index??r;n+=zk(e.slice(r,i)),n+=s,r=i+s.length}return n+=zk(e.slice(r)),n}function zk(e){return e.replace(/\/(?:[^/\s,)]+\/){2,}([^/\s,)]+)/g,"$1")}function XN(e){let t=/^(\s*[("]?\s*)cd\s+\S+\s+&&\s+(?!cd\s)(.+)$/.exec(e);return t?(t[1]??"")+(t[2]??""):e}var Jk=60;function Cp(e,t){let n=e.trim().replace(/^\((.*)\)$/s,"$1"),r;try{r=JSON.parse(n)}catch{return e}if(!r||typeof r!="object")return e;let o=r;for(let s of t){let i=o[s];if(typeof i=="string"&&i.length>0){let a=Ke(i);if(a.length===0)continue;return`(${a.length>Jk?ae(a,Jk,"\u2026"):a})`}}return e}function Vk(e,t){if(e==="bash"||e==="Bash")return XN(t);if(e==="compose"||e==="Compose"){let n=t.trim().replace(/^\((.*)\)$/s,"$1");try{let r=JSON.parse(n);if(r&&typeof r=="object"){let o=Array.isArray(r.nodes)?r.nodes.length:void 0,s=Array.isArray(r.edges)?r.edges.length:0;if(o!==void 0){let i=`${o} node${o===1?"":"s"}`,a=s>0?`, ${s} edge${s===1?"":"s"}`:"";return`(${i}${a})`}}}catch{}}return e==="agent"||e==="Agent"?Cp(t,["id_prefix","prompt"]):e==="Task"?Cp(t,["description","prompt"]):e==="skill"||e==="Skill"?Cp(t,["name","arguments"]):e==="ask_question"?ZN(t):t}function ZN(e){let t=e.trim().replace(/^\((.*)\)$/s,"$1");try{let n=JSON.parse(t);if(!n||typeof n!="object")return e;let r=n,o=typeof r.type=="string"?r.type:"text";if(o==="choice"||o==="multi_choice"){let s=r.choices,i=Array.isArray(s)?s.length:0;return i>0?`(${o}: ${i} option${i===1?"":"s"})`:`(${o})`}return`(${o})`}catch{return e}}function QN(e){return e.replace(/[\r\n]+/g," ")}var eB={"(":")","{":"}","[":"]"};function Yk(e,t,n="\u2026"){let r=e.charAt(0),o=eB[r];return!(o&&e.endsWith(o)&&e.length>=2)||z(e)<=t?ae(e,t,n):t<3?t>=2?r+o:ae(e,t,n):ae(e,t-1,n)+o}function fn(e,t){let n=/^([A-Za-z_][A-Za-z0-9_]*)(.*)$/s.exec(e);if(n){let r=n[1],o=Ys(Vk(r,n[2]??"")),s=Mn(r),{color:i,glyph:a}=ko(r),l=fd(s),c=l?` [${l}]`:"";if(t!==void 0){let d=(a+" ").length+r.length+c.length,p=Math.max(1,t-d);o=Yk(o,p)}o=QN(o);let u=i(a+" ")+i.bold(r)+m.toolArg(o);return l?u+m.dim(c):u}return m.chrome("\u25CF ")+m.toolArg(e)}G();var Xk=8,Il=30;function tB(){let e=T.AFK_DIFF_LINES;if(e===void 0)return Il;let t=e.trim();if(!/^\d+$/.test(t))return Il;let n=Number.parseInt(t,10);return Number.isFinite(n)?n:Il}function nB(){let e=T.AFK_SHOW_DIFFS;if(e===void 0)return!1;let t=e.trim().toLowerCase();return t==="0"||t==="false"||t==="no"||t==="off"}function rB(e){let t=m.diffAdd(`+${e.addedLines}`),n=m.diffRemove(`-${e.removedLines}`),r=e.hunks.length,o=m.dim(`across ${r} hunk${r===1?"":"s"}`);return`${t} ${n} ${o}`}var oB=/[\x00-\x08\x0B-\x1F\x7F-\x9F]/g;function sB(e){let t=Ie(e.text).replace(oB,"");return e.kind==="+"?m.diffAdd("+ "+t):e.kind==="-"?m.diffRemove("- "+t):m.dim(" "+t)}var Pp=new WeakMap;function gn(e,t,n){if(nB())return[];if(e.hunks.length===0)return[];let r=t==="overlay"?Xk:tB(),o=t+"|"+n+"|"+r,s=Pp.get(e);if(s!==void 0){let f=s.get(o);if(f!==void 0)return f}let i=[];i.push(n+rB(e));let a=[];for(let f of e.hunks){let g=`@@ -${f.oldStart},${f.oldLines} +${f.newStart},${f.newLines} @@`;a.push({kind:"header",text:m.diffHunk(g)});for(let h of f.lines)a.push({kind:"body",text:sB(h)})}if(r===0){for(let f of a)i.push(n+f.text);return Ip(e,o,i),i}let l=0;for(let f of a)f.kind==="body"&&l++;if(l<=r){for(let f of a)i.push(n+f.text);return Ip(e,o,i),i}let c=0;for(let f of a)f.kind==="header"?i.push(n+f.text):c<r&&(i.push(n+f.text),c++);let u=l-r,d=`line${u===1?"":"s"}`,p=t==="flush"?" (set AFK_DIFF_LINES=0 to expand)":"";return i.push(n+m.dim(`\u2026 +${u} more diff ${d}${p}`)),Ip(e,o,i),i}function Ip(e,t,n){let r=Pp.get(e);r===void 0&&(r=new Map,Pp.set(e,r)),r.set(t,n)}var iB=m.success("\u2713"),aB=m.error("\u2717");function wr(e){return e?aB:iB}var Xs=3,Zk=2,Qk=3;function ev(e){switch(Mn(e)){case"read":return"Reading\u2026";case"write":return"Writing\u2026";case"web":return"Fetching\u2026";case"shell":return"Running\u2026";default:return"Running\u2026"}}function lB(e,t){return e?e==="grep"||e==="Grep"?t===1?"match":"matches":e==="glob"||e==="Glob"?t===1?"path":"paths":t===1?"line":"lines":t===1?"line":"lines"}function Hn(e,t,n=60,r){let o=e.isError?m.error:m.dim,s=t??T.HOME??"___NOHOME___";if(e.display!==void 0&&!e.isError)return o(e.display);if(e.persistedPath){let a=e.persistedPath.startsWith(s)?"~"+e.persistedPath.slice(s.length):e.persistedPath;return o(`saved \u2192 ${a}`)}if(e.lineCount!==void 0&&e.lineCount>1)return o(`${e.lineCount} ${lB(r,e.lineCount)}`);let i=e.content.length>n?e.content.slice(0,n-3)+"\u2026":e.content;return o(Ke(i))}G();function Zs(e,t){if(e.length<=t)return e;let n=e.slice(0,t),r=e.slice(t),o=nv(r);return[...n,{kind:"overflow",count:r.length,text:o}]}function cB(e,t){return t<=1||e.endsWith("s")||/(sh|ch|x|z)$/i.test(e)?e:e+"s"}var uB=5,tv=60;function nv(e){let t=new Map,n=0;for(let s of e)s.kind==="group"?(t.set(s.toolName,(t.get(s.toolName)??0)+s.entries.length),n+=s.entries.length):(t.set(s.toolName,(t.get(s.toolName)??0)+1),n+=1);if(n===0)return"";if(e.length>0&&e.every(s=>Je.has(s.toolName))&&new Set(e.map(s=>s.toolName)).size===1){let s=[],i=!1;for(let a of e){let c=(a.kind==="group"?a.label:a.toolInput).trim();if(!c.startsWith("(")||!c.endsWith(")")){i=!0;break}let u=c.slice(1,-1),d=Ke(u);if(!d){i=!0;break}let p=d.length>tv?d.slice(0,tv-1)+"\u2026":d,f=a.kind==="group"?a.entries.length:1;s.push({display:p,entries:f})}if(!i&&s.length>0){let a=s.slice(0,uB),l=a.map(({display:p,entries:f})=>f>1?`${p} \xD7${f}`:p),c=a.reduce((p,f)=>p+f.entries,0),u=n-c,d=l.join(", ")+(u>0?` (+${u})`:"");return`\u2026 +${n} more: ${d}`}}let o=[];for(let[s,i]of t)o.push(`${i} ${cB(s,i)}`);return`\u2026 +${n} (${o.join(", ")})`}function Pl(e,t=It()){return e.map((n,r)=>({sibling:n,connector:r===e.length-1?t.lastConnector:t.midConnector}))}function Mp(e,t){return t?[...e,{kind:"resultSummary",summary:t}]:e}function Op(e){if(e.length===0)return[];let t=new Map;for(let o of e){let s=rv(o),i=t.get(s);i||(i=[],t.set(s,i)),i.push(o)}let n=[],r=new Set;for(let o of e){let s=rv(o),i=t.get(s);i.length>=dB(o.toolName)?r.has(s)||(n.push({kind:"group",toolName:o.toolName,label:Je.has(o.toolName)?o.toolInput:"",entries:i}),r.add(s)):n.push(o)}return n}function $p(e){let t=e.entries.length,n=e.entries.filter(a=>a.result),r=n.filter(a=>a.result.isError),o=n.length,s;if(r.length>0){let a=o-r.length,l=[];a>0&&l.push(`${a} ok`),l.push(`${r.length} error${r.length===1?"":"s"}`),s=l.join(", ")}else o===t?s=`${t} done`:o===0?s=`${t} running`:s=`${o}/${t} done`;return fn(e.toolName+e.label)+m.dim(` \xD7${t} \u2014 ${s}`)}function rv(e){return Je.has(e.toolName)?e.toolName+"::"+e.toolInput:e.toolName}function dB(e){return Je.has(e)?Zk:Qk}function ov(e,t,n,r){return e>0||t?Oo([...n,!0],r):Oo(n,r)}function Ml(e,t,n,r=Y(),o=[],s=It()){let i=Oo(o,s),a=Qs(i,s),l=e.filter(g=>g.kind==="text"),c=e.filter(g=>g.kind==="tool"),u=Op(c),d=Zs(u,Xs),p=Pl(d,s);for(let{sibling:g,connector:h}of p){let b=m.dim(h),y=h===s.lastConnector;if(g.kind==="overflow")n.push(Pe(a+b+m.dim(g.text),r));else if(g.kind==="group")n.push(Pe(a+b+$p(g),r));else if(g.kind==="resultSummary")n.push(Pe(a+b+m.dim(g.summary),r));else{let w=g,x=t.get(w.toolUseId);if(Je.has(w.toolName)&&x&&x.length>0){if(w.headerEmitted?n.push(Pe(a+b,r)):n.push(Pe(a+b+w.prefix,r)),Ml(x,t,n,r,[...o,y],s),w.thinkingTail){let R=Qs(Oo([...o,y],s),s);n.push(Pe(R+m.thinking("\u2307 "+Ke(w.thinkingTail)),r))}}else if(w.result){if(n.push(Pe(a+b+w.prefix+m.dim(" \u2014 ")+wr(w.result.isError)+" "+Hn(w.result,void 0,60,w.toolName),r)),w.diff&&!w.result.isError){let R=a+(y?s.spineClosed:m.dim(s.spine))+" ";for(let v of gn(w.diff,"overlay",R))n.push(v)}}else{n.push(Pe(a+b+w.prefix,r));let R=a+(y?s.spineClosed:m.dim(s.spine))+" ";w.thinkingTail?n.push(Pe(R+m.thinking("\u2307 "+Ke(w.thinkingTail)),r)):n.push(Pe(R+m.dim(ev(w.toolName)),r))}}}let f=ov(c.length,!1,o,s);for(let g of l)for(let h of Dp(g.text,f,s))n.push(Pe(h,r))}function ei(e,t,n,r,o=Y(),s=[],i=It()){let a=Oo(s,i),l=Qs(a,i),c=[],u=e.filter(y=>y.kind==="text"),d=e.filter(y=>y.kind==="tool"),p=Op(d),f=Zs(p,Xs),g=Mp(f,r),h=Pl(g,i);for(let{sibling:y,connector:w}of h){let x=m.dim(w),R=w===i.lastConnector;if(y.kind==="overflow")c.push(Pe(l+x+m.dim(y.text),o));else if(y.kind==="resultSummary")c.push(Pe(l+x+m.dim(y.summary),o));else if(y.kind==="group")c.push(Pe(l+x+$p(y),o));else{let v=y,E=t.get(v.toolUseId);if(Je.has(v.toolName)&&E&&E.length>0){if(v.headerEmitted){let I=v.toolInput?`${v.toolName} ${Ke(v.toolInput)}`:v.toolName;c.push(Pe(l+x+m.dim("\u21B3 "+I),o))}else c.push(Pe(l+x+v.prefix,o));c.push(...ei(E,t,n,void 0,o,[...s,R],i))}else if(v.result){if(c.push(Pe(l+x+v.prefix+m.dim(" \u2014 ")+wr(v.result.isError)+" "+Hn(v.result,n,60,v.toolName),o)),v.diff&&!v.result.isError){let I=l+(R?i.spineClosed:m.dim(i.spine))+" ";for(let D of gn(v.diff,"flush",I))c.push(D)}}else c.push(Pe(l+x+v.prefix,o))}}let b=ov(d.length,r!=null,s,i);for(let y of u)for(let w of Dp(y.text,b,i))c.push(Pe(w,o));return c}function ti(e,t,n,r,o=0){let s=It(),i=t.filter(h=>h.kind==="tool"),l=i.filter(h=>h.result).reduce((h,b)=>h+(b.result.lineCount??0),0),c=[];i.length>Xs&&(c.push(`${i.length} tools`),l>0&&c.push(`${l} lines`));let u=m.dim(s.spine.repeat(o)),d=Array.from({length:o},()=>!1),p=m.dim(s.turnRoot),f=c.length>0?u+p+e.prefix+m.dim(" \u2014 "+c.join(" \xB7 ")):u+p+e.prefix,g=ei(t,n,r,e.agentResultSummary,Y(),d,s);return[f,...g].join(`
2092
+ `)}function Lp(e,t=0){let n=It(),r=m.dim(n.spine.repeat(t)),o=m.dim(n.turnRoot);return r+o+e.prefix}function ni(e,t,n,r,o=0){let s=It(),i=Array.from({length:o},()=>!1);return ei(t,n,r,e.agentResultSummary,Y(),i,s)}function $o(e,t,n){let r=[];for(let o of t){let s=e.get(o);if(s.length===1){let i=s[0];if(i.result){if(r.push(" "+i.prefix+m.dim(" \u2014 ")+wr(i.result.isError)+" "+Hn(i.result,n,60,i.toolName)),i.diff&&!i.result.isError)for(let a of gn(i.diff,"flush"," "))r.push(a)}else r.push(" "+i.prefix)}else{r.push(sv(o,s,n));let i=s.filter(l=>l.diff&&l.result&&!l.result.isError),a=i.length>1;for(let l of i){if(a){let c=Ke(Ys(l.toolInput).trim()||l.toolInput.trim());r.push(" "+m.dim(`\u2500\u2500 ${c} \u2500\u2500`))}for(let c of gn(l.diff,"flush"," "))r.push(c)}}}return r}function sv(e,t,n){let{color:r,glyph:o}=ko(e),s=t.map(u=>Ke(Ys(u.toolInput).trim())),i=r(o+" ")+r.bold(e)+m.dim(` \xD7${t.length}`)+" "+m.toolArg(s.join(", ")),a=t.filter(u=>u.result),l=a.filter(u=>u.result.isError);if(l.length>0){let u=a.length-l.length,p=a.filter(g=>!g.result.isError).map(g=>g.result.lineCount).filter(g=>g!==void 0).reduce((g,h)=>g+h,0),f=[];return p>0&&f.push(`${p} lines`),u>0&&f.push(`${u} ok`),f.push(m.error(`${l.length} error${l.length>1?"s":""}`))," "+i+m.dim(" \u2014 ")+f.join(m.dim(", "))}let c=a.map(u=>u.result?.lineCount).filter(u=>u!==void 0);if(c.length===a.length&&c.length>0){if(c.every(p=>p===c[0]))return" "+i+m.dim(` \u2014 ${c[0]} lines each`);let d=c.reduce((p,f)=>p+f,0);return" "+i+m.dim(` \u2014 ${d} lines total`)}if(a.length>0){let u=a.map(d=>Hn(d.result,n,60,d.toolName));return" "+i+m.dim(" \u2014 ")+u.join(m.dim(", "))}return" "+i}function Pe(e,t){return ae(e,t)}var Fp=Object.freeze({spine:"\u2502 ",spineClosed:" ",lead:" ",turnRoot:"\u25C9 ",midConnector:"\u251C\u2500 ",lastConnector:"\u2570\u2500 ",textPrefix:"\u2502 "}),pB=Object.freeze({spine:"| ",spineClosed:" ",lead:" ",turnRoot:"o ",midConnector:"+- ",lastConnector:"\\- ",textPrefix:"| "}),Pie=Fp.midConnector,Mie=Fp.lastConnector;function It(){let e=T.AGENT_AFK_ASCII;return e&&/^(1|true|yes)$/i.test(e)?pB:Fp}function Oo(e,t){let n="";for(let r of e)n+=r?t.spineClosed:t.spine;return n+=t.spine,n}function Qs(e,t){let n="";for(let r=0;r<e.length;r+=2){let o=e.slice(r,r+2);n+=o===t.spine?m.dim(o):o}return n}function Dp(e,t,n){if(!e||!e.trim())return[];let r=m.dim(n.textPrefix),o=Math.max(1,Y()-t.length-2-2),s=Qs(t,n),i=[];for(let a of e.split(`
2093
2093
  `)){let l=_p(a),c=me(l,o);for(let u of c.split(`
2094
- `))i.push(s+r+u)}return i}var iv=6,ri=class{entries=new Map;order=[];agentIdStack=[];addStart(t,n,r){let o=Ie(r),s=fn(n+o),i=this.agentIdStack.at(-1)??void 0,a={kind:"tool",toolUseId:t,toolName:n,toolInput:o,prefix:s,...i!==void 0?{agentContext:i}:{}};this.entries.set(t,a),this.order.push(t),Qr.has(n)&&this.agentIdStack.push(t)}addStartWithAgentContext(t,n,r,o,s){let i=Ie(r),a=this.entries.get(t);if(a?.kind==="tool"){a.toolInput=i,a.prefix=fn(n+i,s),o!==void 0&&(a.agentContext=o);return}let l=fn(n+i,s),c={kind:"tool",toolUseId:t,toolName:n,toolInput:i,prefix:l,...o!==void 0?{agentContext:o}:{}};this.entries.set(t,c),this.order.push(t)}mergeAgentLabel(t,n,r){let o=this.entries.get(t);if(o?.kind!=="tool"||!Qr.has(o.toolName)||o.toolName==="Agent")return!1;let i=`(${Ie(n)})`;return o.toolName="Agent",o.toolInput=i,o.prefix=fn("Agent"+i,r),!0}setAgentContext(t,n){let r=this.entries.get(t);r?.kind==="tool"&&(n===void 0?delete r.agentContext:r.agentContext=n)}setAgentResultSummary(t,n){let r=this.entries.get(t);r?.kind==="tool"&&(r.agentResultSummary=n)}setThinkingTail(t,n){let r=this.entries.get(t);r?.kind==="tool"&&(n===void 0?delete r.thinkingTail:r.thinkingTail=n)}addResult(t,n){let r=this.entries.get(t);r?.kind==="tool"&&(r.result=n),this.agentIdStack.at(-1)===t&&this.agentIdStack.pop()}addDiff(t,n){let r=this.entries.get(t);r?.kind==="tool"&&(r.diff=n)}upsertTextChild(t,n,r){let o=this.entries.get(t);if(o?.kind==="text"){o.text=r,o.agentContext=n;return}let s={kind:"text",toolUseId:t,text:r,agentContext:n};this.entries.set(t,s),this.order.push(t)}removeTextChildrenUnder(t){let n=[];for(let[r,o]of this.entries)o.kind==="text"&&o.agentContext===t&&n.push(r);for(let r of n)this.entries.delete(r);if(n.length>0){let r=new Set(n);this.order=this.order.filter(o=>!r.has(o))}}hasPending(){return this.entries.size>0}hasEntry(t){return this.entries.get(t)?.kind==="tool"}getOverlay(){let t=this.buildChildMap(),n=[],r=It(),o=Y(),s=c=>ae(c,o),i=[];for(let c of this.order){let u=this.entries.get(c);!u||u.kind!=="tool"||u.agentContext||i.push(u)}let a=i,l=0;if(i.length>iv){let c=i.filter(f=>!f.result),u=i.filter(f=>f.result),d=Math.max(0,iv-c.length),m=new Set(u.slice(-d));l=u.length-m.size,a=i.filter(f=>!f.result||m.has(f))}for(let c of a){let u=t.get(c.toolUseId);if(Je.has(c.toolName)&&u&&u.length>0)c.headerEmitted?n.push(s(p.dim(r.turnRoot))):n.push(s(p.dim(r.turnRoot)+c.prefix)),Ml(u,t,n,o,void 0,r),c.thinkingTail&&n.push(s(p.dim(r.spine)+p.thinking("\u2307 "+Ke(c.thinkingTail))));else if(!(Je.has(c.toolName)&&c.headerEmitted))if(c.result){if(n.push(s(" "+c.prefix+p.dim(" \u2014 ")+wr(c.result.isError)+" "+Hn(c.result,void 0,60,c.toolName))),c.diff&&!c.result.isError)for(let d of gn(c.diff,"overlay"," "))n.push(s(d))}else n.push(s(" "+c.prefix+p.dim(" \u2026"))),c.thinkingTail&&n.push(s(p.dim(r.spine)+p.thinking("\u2307 "+Ke(c.thinkingTail))))}return l>0&&n.push(s(" "+p.dim(`\u2026 +${l} done`))),n.join(`
2095
- `)}ancestorDepthOf(t){let n=new Set([t]),r=0,o=t,s=32;for(;o!==void 0&&r<s;){let i=this.entries.get(o);if(!i||i.kind!=="tool")break;let a=i.agentContext;if(a===void 0||n.has(a))break;n.add(a);let l=this.entries.get(a);if(!l||l.kind!=="tool")break;r+=1,o=a}return r}flushSource(t,n){let r=this.entries.get(t);if(!r||r.kind!=="tool")return[];let o=this.ancestorDepthOf(t),s=[];{let m=[],f=new Set([t]),g=r.agentContext;for(;g!==void 0&&!f.has(g);){f.add(g);let h=this.entries.get(g);if(!h||h.kind!=="tool")break;m.push({entry:h,depth:this.ancestorDepthOf(h.toolUseId)}),g=h.agentContext}m.reverse();for(let{entry:h,depth:b}of m)h.headerEmitted||(s.push(Lp(h,b)),h.headerEmitted=!0)}let i=new Set([t]),a=[t];for(;a.length>0;){let m=a.shift();for(let[f,g]of this.entries){if(i.has(f))continue;(g.kind==="tool",g.agentContext)===m&&(i.add(f),g.kind==="tool"&&a.push(f))}}let l=new Map;for(let m of this.order){if(!i.has(m))continue;let f=this.entries.get(m);if(!f)continue;let g=(f.kind==="tool",f.agentContext);if(!g)continue;let h=l.get(g);h||(h=[],l.set(g,h)),h.push(f)}let c=l.get(r.toolUseId)??[],u=r.headerEmitted?ni(r,c,l,n,o).join(`
2096
- `):ti(r,c,l,n,o);for(let m of i)this.entries.delete(m);this.order=this.order.filter(m=>!i.has(m));let d=u===""?[]:[u];return[...s,...d]}flushCompletedRoots(t){if(this.entries.size===0)return[];let n=this.buildChildMap(),r=[];for(let c of this.order){let u=this.entries.get(c);!u||u.kind!=="tool"||u.agentContext||u.result!==void 0&&r.push(c)}if(r.length===0)return[];let o=[],s=new Map,i=[];for(let c of r){let u=this.entries.get(c);if(!u||u.kind!=="tool")continue;let d=n.get(u.toolUseId);if(Je.has(u.toolName))if(o.push(...$o(s,i,t)),s.clear(),i.length=0,u.headerEmitted){let m=ni(u,d??[],n,t,0);o.push(...m)}else o.push(ti(u,d??[],n,t));else s.has(u.toolName)||(s.set(u.toolName,[]),i.push(u.toolName)),s.get(u.toolName).push(u)}o.push(...$o(s,i,t));let a=new Set(r),l=[...r];for(;l.length>0;){let c=l.shift();for(let[u,d]of this.entries){if(a.has(u))continue;(d.kind==="tool",d.agentContext)===c&&(a.add(u),d.kind==="tool"&&l.push(u))}}for(let c of a)this.entries.delete(c);return this.order=this.order.filter(c=>!a.has(c)),o}flush(t){if(this.entries.size===0)return[];let n=this.buildChildMap(),r=[];for(let a of this.order){let l=this.entries.get(a);!l||l.kind!=="tool"||l.agentContext||r.push(a)}let o=[],s=new Map,i=[];for(let a of r){let l=this.entries.get(a);if(!l||l.kind!=="tool")continue;let c=n.get(l.toolUseId);if(Je.has(l.toolName))if(o.push(...$o(s,i,t)),s.clear(),i.length=0,l.headerEmitted){let u=ni(l,c??[],n,t,0);o.push(...u)}else o.push(ti(l,c??[],n,t));else s.has(l.toolName)||(s.set(l.toolName,[]),i.push(l.toolName)),s.get(l.toolName).push(l)}return o.push(...$o(s,i,t)),this.entries.clear(),this.order=[],this.agentIdStack=[],o}buildChildMap(){let t=new Map;for(let n of this.order){let r=this.entries.get(n);if(!r)continue;let o=(r.kind==="tool",r.agentContext);if(!o)continue;let s=t.get(o);s||(s=[],t.set(o,s)),s.push(r)}return t}};function Np(e,t){let n=Math.max(0,e),r=n<1e3?`${n}ms`:`${(n/1e3).toFixed(1)}s`,o=Math.ceil(t/4);return` ${p.thinking("\u25C6 thought for "+r+" \xB7 "+o+" tok")}`}var Do=class{buffer="";startedAt=null;endedAt=null;hasEmitted=!1;committedUpTo=0;push(t){this.hasEmitted||(this.buffer+=t,this.startedAt||(this.startedAt=Date.now()))}markEnded(){this.endedAt===null&&this.startedAt!==null&&(this.endedAt=Date.now())}isActive(){return!this.hasEmitted}hasBufferedContent(){return this.buffer.trim().length>0}peek(){return this.buffer}consume(){return this.hasEmitted=!0,this.buffer}peekPhase(){return this.buffer.slice(this.committedUpTo)}drainPhase(){let t=this.buffer.slice(this.committedUpTo);return this.committedUpTo=this.buffer.length,t}collapse(){if(this.hasEmitted||!this.startedAt)return!this.startedAt&&!this.hasEmitted&&je()&&console.error("[afk:thinking] collapse() short-circuited: no thinking chunks received this turn. Model may not support extended thinking, or API ignored the thinking parameter."),null;this.hasEmitted=!0;let t=this.endedAt??Date.now();return Np(t-this.startedAt,this.buffer.length)}inlineSummary(){if(!this.startedAt||!this.hasBufferedContent())return null;let t=this.endedAt??Date.now(),n=Math.max(0,t-this.startedAt),r=n<1e3?`${n}ms`:`${(n/1e3).toFixed(1)}s`,o=Math.ceil(this.buffer.length/4);return`thought ${r} \xB7 ${o} tok`}};var Bp="__main__";function lv(e){let t={startedAt:Date.now(),lastEventAt:Date.now(),stats:{tokens:0,toolUses:0},contentBuffer:"",done:!1,errored:!1,stalledTicks:0};return e!==void 0&&(t.agentType=e),t}function Lo(e,t){return{type:"tool_result",toolUseId:"synthetic",content:e,isError:t}}function cv(e){let t=["Done"],n=[];e.stats.toolUses&&n.push(`${e.stats.toolUses} tool${e.stats.toolUses===1?"":"s"}`),e.stats.tokens&&n.push(`${re(e.stats.tokens)} tok`);let r=e.responseMetadata?.durationMs,o=Date.now()-e.startedAt;typeof r=="number"&&(n.push(ne(r)),o>r*1.5&&o-r>=1e3&&n.push(`${ne(o)} wall`)),n.length===0&&o>0&&n.push(ne(o));let s=e.thinkingLane?.inlineSummary();return s&&n.push(s),n.length>0&&t.push(`(${n.join(" \xB7 ")})`),t.join(" ")}function Up(e){let t=Math.max(1,Y()-2);return Math.max(1,t-e)}function uv(e,t){return mB(e)?xt(e,{maxWidth:t}):me(e,t)}function dv(e,t,n){if(!n||!e.trim())return"";let r;return mv(e)?r=`
2094
+ `))i.push(s+r+u)}return i}var iv=6,ri=class{entries=new Map;order=[];agentIdStack=[];addStart(t,n,r){let o=Ie(r),s=fn(n+o),i=this.agentIdStack.at(-1)??void 0,a={kind:"tool",toolUseId:t,toolName:n,toolInput:o,prefix:s,...i!==void 0?{agentContext:i}:{}};this.entries.set(t,a),this.order.push(t),Qr.has(n)&&this.agentIdStack.push(t)}addStartWithAgentContext(t,n,r,o,s){let i=Ie(r),a=this.entries.get(t);if(a?.kind==="tool"){a.toolInput=i,a.prefix=fn(n+i,s),o!==void 0&&(a.agentContext=o);return}let l=fn(n+i,s),c={kind:"tool",toolUseId:t,toolName:n,toolInput:i,prefix:l,...o!==void 0?{agentContext:o}:{}};this.entries.set(t,c),this.order.push(t)}mergeAgentLabel(t,n,r){let o=this.entries.get(t);if(o?.kind!=="tool"||!Qr.has(o.toolName)||o.toolName==="Agent")return!1;let i=`(${Ie(n)})`;return o.toolName="Agent",o.toolInput=i,o.prefix=fn("Agent"+i,r),!0}setAgentContext(t,n){let r=this.entries.get(t);r?.kind==="tool"&&(n===void 0?delete r.agentContext:r.agentContext=n)}setAgentResultSummary(t,n){let r=this.entries.get(t);r?.kind==="tool"&&(r.agentResultSummary=n)}setThinkingTail(t,n){let r=this.entries.get(t);r?.kind==="tool"&&(n===void 0?delete r.thinkingTail:r.thinkingTail=n)}addResult(t,n){let r=this.entries.get(t);r?.kind==="tool"&&(r.result=n),this.agentIdStack.at(-1)===t&&this.agentIdStack.pop()}addDiff(t,n){let r=this.entries.get(t);r?.kind==="tool"&&(r.diff=n)}upsertTextChild(t,n,r){let o=this.entries.get(t);if(o?.kind==="text"){o.text=r,o.agentContext=n;return}let s={kind:"text",toolUseId:t,text:r,agentContext:n};this.entries.set(t,s),this.order.push(t)}removeTextChildrenUnder(t){let n=[];for(let[r,o]of this.entries)o.kind==="text"&&o.agentContext===t&&n.push(r);for(let r of n)this.entries.delete(r);if(n.length>0){let r=new Set(n);this.order=this.order.filter(o=>!r.has(o))}}hasPending(){return this.entries.size>0}hasEntry(t){return this.entries.get(t)?.kind==="tool"}getOverlay(){let t=this.buildChildMap(),n=[],r=It(),o=Y(),s=c=>ae(c,o),i=[];for(let c of this.order){let u=this.entries.get(c);!u||u.kind!=="tool"||u.agentContext||i.push(u)}let a=i,l=0;if(i.length>iv){let c=i.filter(f=>!f.result),u=i.filter(f=>f.result),d=Math.max(0,iv-c.length),p=new Set(u.slice(-d));l=u.length-p.size,a=i.filter(f=>!f.result||p.has(f))}for(let c of a){let u=t.get(c.toolUseId);if(Je.has(c.toolName)&&u&&u.length>0)c.headerEmitted?n.push(s(m.dim(r.turnRoot))):n.push(s(m.dim(r.turnRoot)+c.prefix)),Ml(u,t,n,o,void 0,r),c.thinkingTail&&n.push(s(m.dim(r.spine)+m.thinking("\u2307 "+Ke(c.thinkingTail))));else if(!(Je.has(c.toolName)&&c.headerEmitted))if(c.result){if(n.push(s(" "+c.prefix+m.dim(" \u2014 ")+wr(c.result.isError)+" "+Hn(c.result,void 0,60,c.toolName))),c.diff&&!c.result.isError)for(let d of gn(c.diff,"overlay"," "))n.push(s(d))}else n.push(s(" "+c.prefix+m.dim(" \u2026"))),c.thinkingTail&&n.push(s(m.dim(r.spine)+m.thinking("\u2307 "+Ke(c.thinkingTail))))}return l>0&&n.push(s(" "+m.dim(`\u2026 +${l} done`))),n.join(`
2095
+ `)}ancestorDepthOf(t){let n=new Set([t]),r=0,o=t,s=32;for(;o!==void 0&&r<s;){let i=this.entries.get(o);if(!i||i.kind!=="tool")break;let a=i.agentContext;if(a===void 0||n.has(a))break;n.add(a);let l=this.entries.get(a);if(!l||l.kind!=="tool")break;r+=1,o=a}return r}flushSource(t,n){let r=this.entries.get(t);if(!r||r.kind!=="tool")return[];let o=this.ancestorDepthOf(t),s=[];{let p=[],f=new Set([t]),g=r.agentContext;for(;g!==void 0&&!f.has(g);){f.add(g);let h=this.entries.get(g);if(!h||h.kind!=="tool")break;p.push({entry:h,depth:this.ancestorDepthOf(h.toolUseId)}),g=h.agentContext}p.reverse();for(let{entry:h,depth:b}of p)h.headerEmitted||(s.push(Lp(h,b)),h.headerEmitted=!0)}let i=new Set([t]),a=[t];for(;a.length>0;){let p=a.shift();for(let[f,g]of this.entries){if(i.has(f))continue;(g.kind==="tool",g.agentContext)===p&&(i.add(f),g.kind==="tool"&&a.push(f))}}let l=new Map;for(let p of this.order){if(!i.has(p))continue;let f=this.entries.get(p);if(!f)continue;let g=(f.kind==="tool",f.agentContext);if(!g)continue;let h=l.get(g);h||(h=[],l.set(g,h)),h.push(f)}let c=l.get(r.toolUseId)??[],u=r.headerEmitted?ni(r,c,l,n,o).join(`
2096
+ `):ti(r,c,l,n,o);for(let p of i)this.entries.delete(p);this.order=this.order.filter(p=>!i.has(p));let d=u===""?[]:[u];return[...s,...d]}flushCompletedRoots(t){if(this.entries.size===0)return[];let n=this.buildChildMap(),r=[];for(let c of this.order){let u=this.entries.get(c);!u||u.kind!=="tool"||u.agentContext||u.result!==void 0&&r.push(c)}if(r.length===0)return[];let o=[],s=new Map,i=[];for(let c of r){let u=this.entries.get(c);if(!u||u.kind!=="tool")continue;let d=n.get(u.toolUseId);if(Je.has(u.toolName))if(o.push(...$o(s,i,t)),s.clear(),i.length=0,u.headerEmitted){let p=ni(u,d??[],n,t,0);o.push(...p)}else o.push(ti(u,d??[],n,t));else s.has(u.toolName)||(s.set(u.toolName,[]),i.push(u.toolName)),s.get(u.toolName).push(u)}o.push(...$o(s,i,t));let a=new Set(r),l=[...r];for(;l.length>0;){let c=l.shift();for(let[u,d]of this.entries){if(a.has(u))continue;(d.kind==="tool",d.agentContext)===c&&(a.add(u),d.kind==="tool"&&l.push(u))}}for(let c of a)this.entries.delete(c);return this.order=this.order.filter(c=>!a.has(c)),o}flush(t){if(this.entries.size===0)return[];let n=this.buildChildMap(),r=[];for(let a of this.order){let l=this.entries.get(a);!l||l.kind!=="tool"||l.agentContext||r.push(a)}let o=[],s=new Map,i=[];for(let a of r){let l=this.entries.get(a);if(!l||l.kind!=="tool")continue;let c=n.get(l.toolUseId);if(Je.has(l.toolName))if(o.push(...$o(s,i,t)),s.clear(),i.length=0,l.headerEmitted){let u=ni(l,c??[],n,t,0);o.push(...u)}else o.push(ti(l,c??[],n,t));else s.has(l.toolName)||(s.set(l.toolName,[]),i.push(l.toolName)),s.get(l.toolName).push(l)}return o.push(...$o(s,i,t)),this.entries.clear(),this.order=[],this.agentIdStack=[],o}buildChildMap(){let t=new Map;for(let n of this.order){let r=this.entries.get(n);if(!r)continue;let o=(r.kind==="tool",r.agentContext);if(!o)continue;let s=t.get(o);s||(s=[],t.set(o,s)),s.push(r)}return t}};function Np(e,t){let n=Math.max(0,e),r=n<1e3?`${n}ms`:`${(n/1e3).toFixed(1)}s`,o=Math.ceil(t/4);return` ${m.thinking("\u25C6 thought for "+r+" \xB7 "+o+" tok")}`}var Do=class{buffer="";startedAt=null;endedAt=null;hasEmitted=!1;committedUpTo=0;push(t){this.hasEmitted||(this.buffer+=t,this.startedAt||(this.startedAt=Date.now()))}markEnded(){this.endedAt===null&&this.startedAt!==null&&(this.endedAt=Date.now())}isActive(){return!this.hasEmitted}hasBufferedContent(){return this.buffer.trim().length>0}peek(){return this.buffer}consume(){return this.hasEmitted=!0,this.buffer}peekPhase(){return this.buffer.slice(this.committedUpTo)}drainPhase(){let t=this.buffer.slice(this.committedUpTo);return this.committedUpTo=this.buffer.length,t}collapse(){if(this.hasEmitted||!this.startedAt)return!this.startedAt&&!this.hasEmitted&&je()&&console.error("[afk:thinking] collapse() short-circuited: no thinking chunks received this turn. Model may not support extended thinking, or API ignored the thinking parameter."),null;this.hasEmitted=!0;let t=this.endedAt??Date.now();return Np(t-this.startedAt,this.buffer.length)}inlineSummary(){if(!this.startedAt||!this.hasBufferedContent())return null;let t=this.endedAt??Date.now(),n=Math.max(0,t-this.startedAt),r=n<1e3?`${n}ms`:`${(n/1e3).toFixed(1)}s`,o=Math.ceil(this.buffer.length/4);return`thought ${r} \xB7 ${o} tok`}};var Bp="__main__";function lv(e){let t={startedAt:Date.now(),lastEventAt:Date.now(),stats:{tokens:0,toolUses:0},contentBuffer:"",done:!1,errored:!1,stalledTicks:0};return e!==void 0&&(t.agentType=e),t}function Lo(e,t){return{type:"tool_result",toolUseId:"synthetic",content:e,isError:t}}function cv(e){let t=["Done"],n=[];e.stats.toolUses&&n.push(`${e.stats.toolUses} tool${e.stats.toolUses===1?"":"s"}`),e.stats.tokens&&n.push(`${re(e.stats.tokens)} tok`);let r=e.responseMetadata?.durationMs,o=Date.now()-e.startedAt;typeof r=="number"&&(n.push(ne(r)),o>r*1.5&&o-r>=1e3&&n.push(`${ne(o)} wall`)),n.length===0&&o>0&&n.push(ne(o));let s=e.thinkingLane?.inlineSummary();return s&&n.push(s),n.length>0&&t.push(`(${n.join(" \xB7 ")})`),t.join(" ")}function Up(e){let t=Math.max(1,Y()-2);return Math.max(1,t-e)}function uv(e,t){return mB(e)?xt(e,{maxWidth:t}):me(e,t)}function dv(e,t,n){if(!n||!e.trim())return"";let r;return mv(e)?r=`
2097
2097
  \u258D streaming code\u2026
2098
2098
  `:r=uv(e,t),me(r,t)}function jp(e,t){return e.split(`
2099
2099
  `).map(n=>n?t+n:"").join(`
@@ -2105,22 +2105,22 @@ ${u}`}function hp(e,t){let n=dk[t.status];e.out.line(` ${n} ${p.bold(t.jobId)}
2105
2105
 
2106
2106
  `),this.committed=hv(this.committed,r)}scheduleRepaint(){!this.isTTY||this.flushing||(this.throttleTimer=yv(()=>{this.throttleTimer=null,this.repaint()},this.throttleMs,this.throttleTimer))}renderPending(){let t=Up(this.indent.length),n=dv(this.buffer,t,this.isTTY&&!this.flushing);return jp(n,this.indent)}async repaint(){if(this.flushing)return;let t=this.renderPending();t&&(gv({indented:t,overlayComposer:this.overlayComposer,compositor:this.compositor,logUpdate:this.logUpdate})||(this.logUpdate||await this.initLogUpdate(),this.logUpdate&&(this.flushing||this.logUpdate(t))))}push(t){if(this.flushing)return;this.buffer+=t;let n=Wp(this.buffer);for(;n!==-1;){let r=this.buffer.slice(0,n);this.buffer=this.buffer.slice(n),this.commitBlock(r),n=Wp(this.buffer)}this.scheduleRepaint()}async flush(){this.throttleTimer&&(clearTimeout(this.throttleTimer),this.throttleTimer=null),this.flushing=!0,this.overlayComposer?(this.overlayComposer.markDirty("markdown-pending"),this.overlayComposer.flush()):this.compositor&&this.compositor.setOverlay(""),this.buffer.trim()&&(this.commitBlock(this.buffer),this.buffer=""),!(this.compositor||this.overlayComposer)&&(this.isTTY&&this.logUpdate?(this.logUpdate.clear(),this.out.write(this.committed+`
2107
2107
  `)):this.committed&&this.out.write(this.committed+`
2108
- `))}getCommittedOutput(){return this.committed}hasEmitted(){return this.buffer.length>0||this.committed.length>0}getPendingBuffer(){return this.buffer}commitPending(){this.buffer.trim()&&(this.commitBlock(this.buffer),this.buffer="",this.overlayComposer?(this.overlayComposer.markDirty("markdown-pending"),this.overlayComposer.flush()):this.compositor&&this.compositor.setOverlay(""))}dispose(){this.throttleTimer&&(clearTimeout(this.throttleTimer),this.throttleTimer=null),this.resizeUnsub&&(this.resizeUnsub(),this.resizeUnsub=null),this.logUpdate&&(this.logUpdate.clear(),this.logUpdate=null),this.buffer="",this.committed=""}};var fB=["observing","modeling","choosing","acting","updating"],gB={observing:"observe",modeling:"model",choosing:"choose",acting:"act",updating:"update"};function bv(){return{stage:"observing",pendingTools:new Set}}function wv(e,t){let n=e.stage;switch(t.type){case"chunk":{let r=t.chunk;r.type==="tool_use_detail"?(e.pendingTools.add(r.toolUseId),e.stage="acting"):r.type==="tool_result"?(e.pendingTools.delete(r.toolUseId),e.stage=e.pendingTools.size>0?"acting":"updating"):r.type==="thinking"?e.pendingTools.size===0&&(e.stage="modeling"):r.type==="content"&&e.pendingTools.size===0&&(e.stage="choosing");break}case"done":e.pendingTools.clear();break;default:break}return e.stage!==n}function hB(e,t){return fB.map(r=>{let o=r===e,s=o?"\u25C6":"\u25C7",i=gB[r],a=`${s} ${i}`;return o?t.accent(t.bold(a)):t.dim(a)}).join(t.dim(" \xB7 "))}var $l=class{stream;getExtraRows;started=!1;currentStage="observing";resizeUnsub=null;onRowCountChange;constructor(t){this.stream=t.stream??process.stdout,this.getExtraRows=t.getExtraRows}setRowCountChangeHandler(t){this.onRowCountChange=t}start(){this.started||(this.started=!0,this.onRowCountChange?.(1),this.resizeUnsub=Ue.subscribe(()=>this.repaint(this.currentStage)),this.repaint(this.currentStage))}stop(){this.started&&(this.started=!1,this.resizeUnsub&&(this.resizeUnsub(),this.resizeUnsub=null),this.clearRow(),this.onRowCountChange?.(0))}repaint(t){if(!this.started||!this.stream.isTTY)return;this.currentStage=t;let n=this.stream.rows??24,r=this.getExtraRows(),o=Math.max(1,n-r);this.stream.write("\x1B[s"),this.stream.write(`\x1B[${o};1H`),this.stream.write("\x1B[2K"),this.stream.write(" "+hB(t,{dim:p.dim,accent:p.brand,bold:p.bold})),this.stream.write("\x1B[u")}clearRow(){if(!this.stream.isTTY)return;let t=this.stream.rows??24,n=this.getExtraRows(),r=Math.max(1,t-n);this.stream.write("\x1B[s"),this.stream.write(`\x1B[${r};1H`),this.stream.write("\x1B[2K"),this.stream.write("\x1B[u")}};import yB from"wrap-ansi";var bB="\u25C6 thinking",Dl=" ",wB=5,SB=16;function Ll(e,t){let n=e.replace(/\s+/g," ").trim();if(!n)return"";let r=t.maxLines??wB,o=Math.max(SB,t.cols-Dl.length),i=yB(n,o,{hard:!1,trim:!0,wordWrap:!0}).split(`
2109
- `),a=i,l=0;i.length>r&&(l=i.slice(0,i.length-r).reduce((d,m,f)=>d+m.length+(f>0?1:0),0),a=i.slice(-r));let c=[];c.push(Dl+p.thinking(bB));for(let u of a)c.push(Dl+p.thinking(u));return l>0&&c.push(Dl+p.dim(`\u22EF +${l} chars earlier`)),c.join(`
2108
+ `))}getCommittedOutput(){return this.committed}hasEmitted(){return this.buffer.length>0||this.committed.length>0}getPendingBuffer(){return this.buffer}commitPending(){this.buffer.trim()&&(this.commitBlock(this.buffer),this.buffer="",this.overlayComposer?(this.overlayComposer.markDirty("markdown-pending"),this.overlayComposer.flush()):this.compositor&&this.compositor.setOverlay(""))}dispose(){this.throttleTimer&&(clearTimeout(this.throttleTimer),this.throttleTimer=null),this.resizeUnsub&&(this.resizeUnsub(),this.resizeUnsub=null),this.logUpdate&&(this.logUpdate.clear(),this.logUpdate=null),this.buffer="",this.committed=""}};var fB=["observing","modeling","choosing","acting","updating"],gB={observing:"observe",modeling:"model",choosing:"choose",acting:"act",updating:"update"};function bv(){return{stage:"observing",pendingTools:new Set}}function wv(e,t){let n=e.stage;switch(t.type){case"chunk":{let r=t.chunk;r.type==="tool_use_detail"?(e.pendingTools.add(r.toolUseId),e.stage="acting"):r.type==="tool_result"?(e.pendingTools.delete(r.toolUseId),e.stage=e.pendingTools.size>0?"acting":"updating"):r.type==="thinking"?e.pendingTools.size===0&&(e.stage="modeling"):r.type==="content"&&e.pendingTools.size===0&&(e.stage="choosing");break}case"done":e.pendingTools.clear();break;default:break}return e.stage!==n}function hB(e,t){return fB.map(r=>{let o=r===e,s=o?"\u25C6":"\u25C7",i=gB[r],a=`${s} ${i}`;return o?t.accent(t.bold(a)):t.dim(a)}).join(t.dim(" \xB7 "))}var $l=class{stream;getExtraRows;started=!1;currentStage="observing";resizeUnsub=null;onRowCountChange;constructor(t){this.stream=t.stream??process.stdout,this.getExtraRows=t.getExtraRows}setRowCountChangeHandler(t){this.onRowCountChange=t}start(){this.started||(this.started=!0,this.onRowCountChange?.(1),this.resizeUnsub=Ue.subscribe(()=>this.repaint(this.currentStage)),this.repaint(this.currentStage))}stop(){this.started&&(this.started=!1,this.resizeUnsub&&(this.resizeUnsub(),this.resizeUnsub=null),this.clearRow(),this.onRowCountChange?.(0))}repaint(t){if(!this.started||!this.stream.isTTY)return;this.currentStage=t;let n=this.stream.rows??24,r=this.getExtraRows(),o=Math.max(1,n-r);this.stream.write("\x1B[s"),this.stream.write(`\x1B[${o};1H`),this.stream.write("\x1B[2K"),this.stream.write(" "+hB(t,{dim:m.dim,accent:m.brand,bold:m.bold})),this.stream.write("\x1B[u")}redraw(){this.repaint(this.currentStage)}clearRow(){if(!this.stream.isTTY)return;let t=this.stream.rows??24,n=this.getExtraRows(),r=Math.max(1,t-n);this.stream.write("\x1B[s"),this.stream.write(`\x1B[${r};1H`),this.stream.write("\x1B[2K"),this.stream.write("\x1B[u")}};import yB from"wrap-ansi";var bB="\u25C6 thinking",Dl=" ",wB=5,SB=16;function Ll(e,t){let n=e.replace(/\s+/g," ").trim();if(!n)return"";let r=t.maxLines??wB,o=Math.max(SB,t.cols-Dl.length),i=yB(n,o,{hard:!1,trim:!0,wordWrap:!0}).split(`
2109
+ `),a=i,l=0;i.length>r&&(l=i.slice(0,i.length-r).reduce((d,p,f)=>d+p.length+(f>0?1:0),0),a=i.slice(-r));let c=[];c.push(Dl+m.thinking(bB));for(let u of a)c.push(Dl+m.thinking(u));return l>0&&c.push(Dl+m.dim(`\u22EF +${l} chars earlier`)),c.join(`
2110
2110
  `)}function Hp(e,t,n){n.streamingMarkdown.current?n.streamingMarkdown.current.commitPending():t.contentBuffer.trim()&&(Gp(t.contentBuffer,n.out),t.contentBuffer=""),Fo(n);let o=Rn(e).split(`
2111
2111
  `);if(n.isTTY&&n.compositor)for(let s of o)n.compositor.commitAbove(s);else for(let s of o)n.out.line(s)}function Sv(e,t){let n=e.thinkingPhaseStartedAt;if(n==null)return null;e.thinkingPhaseStartedAt=void 0;let r=t.thinkingLane.drainPhase();return r.trim()?Np(Date.now()-n,r.length):null}function Fl(e,t){let n=Sv(e,t);n!=null&&(t.isTTY&&t.compositor?t.compositor.commitAbove(n):t.out.line(n))}function Kp(e,t){if(!t.streamingMarkdown.current&&e.contentBuffer.trim()&&Gp(e.contentBuffer,t.out),e.contentBuffer="",t.coordinator){if(!t.isTTY&&t.thinkingMode!=="off"){let n=t.thinkingLane.collapse();if(n){let r=n,o=t.out;t.coordinator.schedule({anchor:"before-content",commits:[()=>{o.line(r)}]})}}if(t.toolLane.hasPending()){let n=t.toolLane.flush(),r=t.compositor,o=t.overlayComposer,s=t.isTTY,i=t.out;t.coordinator.schedule({anchor:"before-content",commits:[()=>{if(s&&r){for(let a of n)r.commitAbove(a);r.commitAbove(""),o?(o.invalidate(),o.flush()):r.setOverlay("")}else{for(let a of n)i.line(a);i.line("")}}]})}if(t.isTTY&&t.thinkingMode!=="off"){let n=Sv(e,t);if(n!=null){let r=t.compositor,o=t.out,s=t.isTTY;t.coordinator.schedule({anchor:"before-content",commits:[()=>{s&&r?r.commitAbove(n):o.line(n)}]})}}}else{if(!t.isTTY&&t.thinkingMode!=="off"){let n=t.thinkingLane.collapse();n&&t.out.line(n)}Fo(t),t.isTTY&&t.thinkingMode!=="off"&&Fl(e,t)}}function Gp(e,t){let n=xt(e);for(let r of n.split(`
2112
2112
  `))t.line(r)}function qp(e,t){let n=Gi(e.message,e.stack);for(let r of n.split(`
2113
2113
  `))t.line(r)}function Fo(e){if(!e.toolLane.hasPending())return;let t=e.toolLane.flushCompletedRoots();if(e.isTTY&&e.compositor){if(t.length>0){for(let n of t)e.compositor.commitAbove(n);e.compositor.commitAbove("")}e.overlayComposer?(e.overlayComposer.markDirty("tool-lane"),e.overlayComposer.flush()):e.compositor.setOverlay(e.toolLane.getOverlay())}else if(t.length>0){for(let n of t)e.out.line(n);e.out.line("")}}function kv(e,t,n,r){switch(n.stageTracker&&wv(n.stageTracker,e),e.type){case"progress":r.set(e.progress.taskId,e.progress),n.isTTY&&Sr(n,r);return;case"chunk":{let o=e.chunk;if(o.type==="tool_use_detail")n.thinkingLane.markEnded(),n.streamingMarkdown.current?.commitPending(),n.isTTY&&(Fo(n),Fl(t,n)),n.toolLane.addStartWithAgentContext(o.toolUseId,o.toolName,o.toolInput,void 0),t.stats.toolUses+=1,n.isTTY&&n.compositor&&n.compositor.setSpinner({enabled:!0,rotateVerbEveryMs:3500}),n.isTTY&&Sr(n,r);else if(o.type==="tool_result")n.streamingMarkdown.current?.commitPending(),n.toolLane.addResult(o.toolUseId,o),n.isTTY&&Sr(n,r);else if(o.type==="tool_diff")n.toolLane.addDiff(o.toolUseId,o.diff),n.isTTY&&Sr(n,r);else if(o.type==="content"){n.thinkingLane.markEnded();let s=Sp(o.content);if(n.activeSkillName&&!n.skillBadgeEmitted){let i=kp(s,n.activeSkillName);if(s=i.text,i.found){n.skillBadgeEmitted=!0;let{color:a,glyph:l}=Qd("skill"),c=" "+a(l+" ")+a.bold(n.activeSkillName);n.isTTY&&n.compositor?n.compositor.commitAbove(c):n.out.line(c)}}else n.activeSkillName&&(s=kp(s,n.activeSkillName).text);if(!s)return;t.contentBuffer+=s,n.isTTY&&(n.compositor&&n.compositor.setSpinner({enabled:!1}),Fo(n),Fl(t,n),n.streamingMarkdown.current||(n.streamingMarkdown.current=new Ol({...n.compositor?{compositor:n.compositor}:{},...n.overlayComposer?{overlayComposer:n.overlayComposer}:{}})),n.streamingMarkdown.current.push(s))}else if(o.type==="thinking"){if(n.thinkingMode==="off")return;t.thinkingPhaseStartedAt==null&&(t.thinkingPhaseStartedAt=Date.now()),n.thinkingLane.push(o.content),n.isTTY&&Sr(n,r)}return}case"message":t.contentBuffer||(t.contentBuffer=Sp(e.message.content));return;case"error":t.errored=!0,qp(e.error,n.out);return;case"done":t.done=!0,e.metadata&&(t.responseMetadata=e.metadata),Kp(t,n);return;case"suggestion":return;case"panel":Hp(e.spec,t,n);return}}function Sr(e,t){if(!e.compositor)return;if(e.overlayComposer){e.overlayComposer.invalidate(),e.overlayComposer.flush();return}let n=[];if(e.thinkingMode==="live"&&e.thinkingLane.isActive()&&e.thinkingLane.hasBufferedContent()){let r=Ll(e.thinkingLane.peekPhase(),{cols:Y()});r&&n.push(r)}if(e.toolLane.hasPending()&&n.push(e.toolLane.getOverlay()),t&&t.size>0){let r=[];for(let o of t.values())r.push(...Ja(o));r.length>0&&n.push(r.join(`
2114
2114
  `))}n.length>0&&e.compositor.setOverlay(n.join(`
2115
- `))}var Nl=class{beforeContent=[];afterSubagent=new Map;afterContent=[];schedule(t){let{anchor:n}=t;if(n==="before-content")this.beforeContent.push(t);else if(n==="after-content")this.afterContent.push(t);else{let r=n.slice(15),o=this.afterSubagent.get(r);o||(o=[],this.afterSubagent.set(r,o)),o.push(t)}}drainSubagent(t){for(let r of this.beforeContent.splice(0))for(let o of r.commits)o();let n=this.afterSubagent.get(t);if(n){this.afterSubagent.delete(t);for(let r of n)for(let o of r.commits)o()}}async flushAll(t){for(let n of this.beforeContent.splice(0))for(let r of n.commits)r();if(t)try{await t()}catch{}for(let[n,r]of Array.from(this.afterSubagent)){this.afterSubagent.delete(n);for(let o of r)for(let s of o.commits)s()}for(let n of this.afterContent.splice(0))for(let r of n.commits)r()}};function zp(e,t){let n=e.trimEnd();if(!n.trim())return"";let r=Math.max(t*4,400),o=n.length>r?n.slice(-r):n,s=/[.!?](?=\s)|\n+/g,i=-1,a;for(;(a=s.exec(o))!==null;)i=a.index+a[0].length;let l=i>=0?o.slice(i):o;return l=l.replace(/\s+/g," ").trim(),l?(l.length>t&&(l=l.slice(0,Math.max(1,t-1))+"\u2026"),l):""}function Bl(e,t){if(!(!e||!e.trim())&&!t.isTTY)for(let n of kB(e))t.out.line(n)}function kB(e){if(!e)return[];let t=p.dim("\u2502 "),n=" ",r=Math.max(1,Y()-n.length-2-2),o=[];for(let s of e.split(`
2115
+ `))}var Nl=class{beforeContent=[];afterSubagent=new Map;afterContent=[];schedule(t){let{anchor:n}=t;if(n==="before-content")this.beforeContent.push(t);else if(n==="after-content")this.afterContent.push(t);else{let r=n.slice(15),o=this.afterSubagent.get(r);o||(o=[],this.afterSubagent.set(r,o)),o.push(t)}}drainSubagent(t){for(let r of this.beforeContent.splice(0))for(let o of r.commits)o();let n=this.afterSubagent.get(t);if(n){this.afterSubagent.delete(t);for(let r of n)for(let o of r.commits)o()}}async flushAll(t){for(let n of this.beforeContent.splice(0))for(let r of n.commits)r();if(t)try{await t()}catch{}for(let[n,r]of Array.from(this.afterSubagent)){this.afterSubagent.delete(n);for(let o of r)for(let s of o.commits)s()}for(let n of this.afterContent.splice(0))for(let r of n.commits)r()}};function zp(e,t){let n=e.trimEnd();if(!n.trim())return"";let r=Math.max(t*4,400),o=n.length>r?n.slice(-r):n,s=/[.!?](?=\s)|\n+/g,i=-1,a;for(;(a=s.exec(o))!==null;)i=a.index+a[0].length;let l=i>=0?o.slice(i):o;return l=l.replace(/\s+/g," ").trim(),l?(l.length>t&&(l=l.slice(0,Math.max(1,t-1))+"\u2026"),l):""}function Bl(e,t){if(!(!e||!e.trim())&&!t.isTTY)for(let n of kB(e))t.out.line(n)}function kB(e){if(!e)return[];let t=m.dim("\u2502 "),n=" ",r=Math.max(1,Y()-n.length-2-2),o=[];for(let s of e.split(`
2116
2116
  `)){let i=me(s,r);for(let a of i.split(`
2117
2117
  `))o.push(n+t+a)}return o}function vv(e,t,n,r){if(r.isTTY){let s=n.syntheticAgentToolUseId;s&&r.toolLane.setThinkingTail(s,void 0),n.contentBuffer=""}else n.contentBuffer.trim()&&(Bl(n.contentBuffer,r),n.contentBuffer="");let o=Rn(e);for(let s of o.split(`
2118
2118
  `))r.isTTY&&r.compositor?r.compositor.commitAbove(s):r.out.line(s);r.isTTY&&r.compositor?r.compositor.commitAbove(""):r.out.line("")}function Jp(e,t,n,r){if(t.syntheticAgentToolUseId)return;let o=t.agentType?.trim()||"agent",s=process.stdout.columns??100,i=Math.max(20,s-14);if(r!==void 0&&n.toolLane.mergeAgentLabel(r,o,i)){t.syntheticAgentToolUseId=r;return}let a=`__synth_agent_${e}`;n.toolLane.addStartWithAgentContext(a,"Agent",`(${o})`,r,i),t.syntheticAgentToolUseId=a}function Tv(e,t,n,r,o){let s=t.syntheticAgentToolUseId;if(!s||t.errored)return;if(!n.isTTY&&t.contentBuffer&&Bl(t.contentBuffer,n),t.contentBuffer="",!n.isTTY&&t.thinkingLane?.hasBufferedContent()){let a=t.thinkingLane.collapse();a&&(n.out.line(a),n.out.line(""))}n.toolLane.setThinkingTail(s,void 0),r.delete(s),o.delete(s);let i=cv(t);n.toolLane.setAgentResultSummary(s,i),n.toolLane.addResult(s,Lo(i,!1)),n.isTTY&&n.compositor&&n.compositor.setOverlay(n.toolLane.getOverlay())}var Ul=new Map,No=new Map;function xv(e,t,n,r){let o=n.syntheticAgentToolUseId;if(o)switch(e.type){case"progress":e.progress.totalTokens&&(n.stats.tokens=e.progress.totalTokens),e.progress.toolUses!==void 0&&(n.stats.progressReportedToolUses=e.progress.toolUses);return;case"chunk":{let s=e.chunk;if(s.type==="tool_use_detail"){n.thinkingLane?.markEnded(),n.currentTextEntryId=void 0;let i=r.streamingMarkdown.get(t);i&&i.commitPending(),r.toolLane.setThinkingTail(o,void 0);let a=process.stdout.columns??100,l=Math.max(20,a-14);r.toolLane.addStartWithAgentContext(s.toolUseId,s.toolName,s.toolInput,o,l),n.stats.toolUses+=1,r.isTTY&&(r.overlayComposer?(r.overlayComposer.markDirty("tool-lane"),r.overlayComposer.flush()):r.compositor&&r.compositor.setOverlay(r.toolLane.getOverlay()))}else if(s.type==="tool_result"){let i=r.streamingMarkdown.get(t);i&&i.commitPending(),r.toolLane.addResult(s.toolUseId,s),r.isTTY&&(r.overlayComposer?(r.overlayComposer.markDirty("tool-lane"),r.overlayComposer.flush()):r.compositor&&r.compositor.setOverlay(r.toolLane.getOverlay()))}else if(s.type==="tool_diff")r.toolLane.addDiff(s.toolUseId,s.diff),r.isTTY&&(r.overlayComposer?(r.overlayComposer.markDirty("tool-lane"),r.overlayComposer.flush()):r.compositor&&r.compositor.setOverlay(r.toolLane.getOverlay()));else if(s.type==="content")if(n.thinkingLane?.markEnded(),n.currentTextEntryId||(n.currentTextEntryId="__in_text_block__"),n.contentBuffer+=s.content,r.isTTY&&r.compositor){if(s.content.trim()){let i=process.stdout.columns??100,a=Math.max(20,i-14),l=zp(n.contentBuffer,a),c=Date.now(),u=Ul.get(o)??0,d=l?/[.!?…]$/.test(l):!1;l&&(c-u>=1500||d)&&(Ul.set(o,c),r.toolLane.setThinkingTail(o,l))}{let i=Date.now(),a=No.get(o)??0;i-a>=1500&&(No.set(o,i),r.compositor.setOverlay(r.toolLane.getOverlay()))}}else{let i=n.contentBuffer.lastIndexOf(`
2119
2119
  `);if(i!==-1){let a=n.contentBuffer.slice(0,i);n.contentBuffer=n.contentBuffer.slice(i+1),Bl(a,r)}}else if(s.type==="thinking"){if(r.thinkingMode==="off")return;if(n.thinkingLane||(n.thinkingLane=new Do),n.thinkingLane.push(s.content),r.thinkingMode==="live"&&r.isTTY&&r.compositor){let i=process.stdout.columns??100,a=Math.max(20,i-14),l=zp(n.thinkingLane.peek(),a);l&&r.toolLane.setThinkingTail(o,l);{let c=Date.now(),u=No.get(o)??0;c-u>=1500&&(No.set(o,c),r.compositor.setOverlay(r.toolLane.getOverlay()))}}}return}case"message":return;case"error":n.errored=!0,r.toolLane.setThinkingTail(o,void 0),Ul.delete(o),No.delete(o);{let s=`error \u2014 ${e.error.message}`;r.toolLane.setAgentResultSummary(o,s),r.toolLane.addResult(o,Lo(s,!0))}r.isTTY&&r.compositor&&r.compositor.setOverlay(r.toolLane.getOverlay());return;case"done":n.done=!0,e.metadata&&(n.responseMetadata=e.metadata),Tv(t,n,r,Ul,No);return;case"suggestion":return;case"panel":vv(e.spec,t,n,r);return}}var jl=class{sink;order;slots=new Map;dirty=!1;constructor(t,n){this.sink=t,this.order=[...n]}register(t){this.slots.set(t.key,t),this.dirty=!0}markDirty(t){this.slots.has(t)&&(this.dirty=!0)}flush(){if(!this.dirty)return;this.dirty=!1;let t=[];for(let n of this.order){let r=this.slots.get(n);if(r===void 0)continue;let o=r.render();o.length>0&&t.push(o)}this.sink.setOverlay(t.join(`
2120
2120
  `))}invalidate(){this.dirty=!0}};function Ev(e,t=2){if(t<1||!Number.isInteger(t))throw new RangeError(`makeDedupingLineWriter: maxRepeat must be a positive integer, got ${t}`);let n=null,r=0,o=()=>{if(n!==null&&r>t){let s=r-t;e.line(` \u2026 (line repeated ${s} more time${s===1?"":"s"})`)}};return{line(s){let i=s??"";i===n?(r++,r<=t&&e.line(i)):(o(),n=i,r=1,e.line(i))},raw(s){o(),n=null,r=0,e.raw(s)},success(s){o(),n=null,r=0,e.success(s)},info(s){o(),n=null,r=0,e.info(s)},warn(s){o(),n=null,r=0,e.warn(s)},error(s){o(),n=null,r=0,e.error(s)},flush(){o(),n=null,r=0}}}var vB=3e4,TB=" \xB7 waiting ",xB=375;function Rv(e,t){e.register({key:"thinking-live",render:()=>t.thinkingMode!=="live"||!t.thinkingLane.isActive()||!t.thinkingLane.hasBufferedContent()?"":Ll(t.thinkingLane.peekPhase(),{cols:Y()})??""}),e.register({key:"markdown-pending",render:()=>{let n=t.streamingMarkdownRef.current;return n?n.renderPending():""}}),e.register({key:"tool-lane",render:()=>t.toolLane.hasPending()?t.toolLane.getOverlay():""}),e.register({key:"progress-banner",render:()=>{let n=[];for(let r of t.lastProgressByTask.values())n.push(...Ja(r));return n.length>0?n.join(`
2121
- `):""}}),e.register({key:"interrupt",render:()=>EB(t.getInterrupting())})}function EB(e){return e?" "+p.warning("\u26A0 interrupting\u2026 (Ctrl+C again to exit)"):""}function Av(e,t){return Ue.subscribe(()=>{t||!e||(e.invalidate(),e.flush())})}function _v(e){if(e.disposed)return!1;let t=!1,n=Date.now();for(let[r,o]of e.sources){if(o.done||o.errored||!o.syntheticAgentToolUseId)continue;let s=n-o.lastEventAt;if(s>vB)if(o.stalledTicks+=1,o.stalledTicks>=xB*2)je()&&process.stderr.write(`[stream-renderer] auto_settle_timeout ${JSON.stringify({sourceId:r,elapsedMs:s,syntheticAgentToolUseId:o.syntheticAgentToolUseId})}
2121
+ `):""}}),e.register({key:"interrupt",render:()=>EB(t.getInterrupting())})}function EB(e){return e?" "+m.warning("\u26A0 interrupting\u2026 (Ctrl+C again to exit)"):""}function Av(e,t){return Ue.subscribe(()=>{t||!e||(e.invalidate(),e.flush())})}function _v(e){if(e.disposed)return!1;let t=!1,n=Date.now();for(let[r,o]of e.sources){if(o.done||o.errored||!o.syntheticAgentToolUseId)continue;let s=n-o.lastEventAt;if(s>vB)if(o.stalledTicks+=1,o.stalledTicks>=xB*2)je()&&process.stderr.write(`[stream-renderer] auto_settle_timeout ${JSON.stringify({sourceId:r,elapsedMs:s,syntheticAgentToolUseId:o.syntheticAgentToolUseId})}
2122
2122
  `),e.toolLane.addResult(o.syntheticAgentToolUseId,Lo("[no-result \u2014 timed out]",!1)),o.done=!0,t=!0;else{let i=o.agentType??r,a=TB+ne(s);o.pauseAnnotation!==a&&(o.pauseAnnotation=a,e.toolLane.addStartWithAgentContext(o.syntheticAgentToolUseId,"Agent",`(${i})${a}`,void 0),t=!0)}}return t&&e.isTTY&&e.overlayComposer&&(e.overlayComposer.markDirty("tool-lane"),e.overlayComposer.flush()),t}function Vp(e){return{out:e.out,isTTY:e.isTTY,compositor:e.compositor,overlayComposer:e.overlayComposer,toolLane:e.toolLane,thinkingLane:e.thinkingLane,thinkingMode:e.thinkingMode,streamingMarkdown:e.streamingMarkdown,coordinator:e.coordinator,...e.isTTY&&e.stageTracker?{stageTracker:e.stageTracker}:{},...e.activeSkillName?{activeSkillName:e.activeSkillName}:{}}}function Yp(e){return{isTTY:e.isTTY,compositor:e.compositor,overlayComposer:e.overlayComposer,toolLane:e.toolLane,out:e.out,streamingMarkdown:e.streamingMarkdown,thinkingMode:e.thinkingMode}}function Cv(e){if(e.parentId===void 0)return;let t=e.sources.get(e.parentId);if(t!==void 0)return t.syntheticAgentToolUseId;if(e.toolLane.hasEntry(e.parentId))return e.parentId;je()&&process.stderr.write(`[stream-renderer] parentId_fallback_unresolved ${JSON.stringify({parentId:e.parentId,sourceId:e.sourceId})}
2123
- `)}var Bo=class{out;thinkingMode;isTTY;captureMode;reducedMotion;onCancel;onBackground;activeSkillName;history;autocompleteState;promptText;scrollRegion;onStageChange;ownsCompositor=!0;borrowedCompositor=null;priorOnCancel=void 0;interrupting=!1;coordinator=new Nl;compositor=null;overlayComposer=null;streamingMarkdownRef={current:null};toolLane=new ri;thinkingLane=new Do;stageTracker=bv();sources=new Map;subagentMarkdown=new Map;lastProgressByTask=new Map;disposed=!1;pauseTickInterval=null;resizeUnsub=null;sink;constructor(t){this.captureMode=t.captureMode??kS(),this.reducedMotion=t.reducedMotion??vS(),this.out=this.captureMode?Ev(t.out,2):t.out;let n=t.thinkingMode??(t.verbose===!0?"live":"summary");this.thinkingMode=this.captureMode&&n==="live"?"summary":n,this.onCancel=t.onCancel,this.onBackground=t.onBackground,this.isTTY=!(t.forceNonTty??!1)&&!!process.stdout.isTTY&&!!process.stdin.isTTY,this.activeSkillName=t.activeSkillName,this.history=t.history,this.autocompleteState=t.autocompleteState,this.promptText=t.promptText,this.scrollRegion=t.scrollRegion,this.onStageChange=t.onStageChange,t.compositor&&(this.borrowedCompositor=t.compositor,this.ownsCompositor=!1),this.sink=(r,o)=>this.process(r,o)}async arm(){if(this.disposed||!this.isTTY||this.compositor)return;let t;if(this.borrowedCompositor)t=this.borrowedCompositor,t.setInputMode("streaming"),this.priorOnCancel=t.getOnCancel(),this.onCancel&&t.setOnCancel(this.onCancel);else{let n={has:r=>Ze().some(o=>o.name===`/${r}`)};t=new Mo({stdout:process.stdout,stdin:process.stdin,...this.onCancel?{onCancel:this.onCancel}:{},...this.onBackground?{onBackground:this.onBackground}:{},...this.history?{history:this.history}:{},...this.autocompleteState?{autocompleteState:this.autocompleteState}:{},...this.promptText!==void 0?{promptText:this.promptText}:{},formatInputBuffer:r=>Wn(r,n),...this.scrollRegion?{scrollRegion:this.scrollRegion}:{},captureMode:this.captureMode}),await t.arm()}this.compositor=t,this.overlayComposer=new jl(t,["thinking-live","markdown-pending","tool-lane","progress-banner","interrupt"]),Rv(this.overlayComposer,{stageTracker:this.stageTracker,thinkingMode:this.thinkingMode,thinkingLane:this.thinkingLane,streamingMarkdownRef:this.streamingMarkdownRef,toolLane:this.toolLane,lastProgressByTask:this.lastProgressByTask,getInterrupting:()=>this.interrupting}),t.setSpinner({enabled:!this.reducedMotion,rotateVerbEveryMs:3500}),this.pauseTickInterval=setInterval(()=>this.checkPauseAnnotations(),80),this.resizeUnsub=Av(this.overlayComposer,!1)}getCompositor(){return this.compositor}setInterrupting(t){this.disposed||(this.interrupting=t,this.overlayComposer&&(this.overlayComposer.markDirty("interrupt"),this.overlayComposer.flush()))}process(t,n){if(this.disposed)return;let r=n?.subagentId??Bp,o=r===Bp,s=this.sources.get(r);if(!s&&(s=lv(n?.agentType),this.sources.set(r,s),!o)){let i=Cv({parentId:n?.parentId,sources:this.sources,toolLane:this.toolLane,sourceId:r});Jp(r,s,Yp({isTTY:this.isTTY,compositor:this.compositor,overlayComposer:this.overlayComposer,toolLane:this.toolLane,out:this.out,streamingMarkdown:this.subagentMarkdown,thinkingMode:this.thinkingMode}),i)}if(o){let i=this.stageTracker.stage;if(kv(t,s,Vp({out:this.out,isTTY:this.isTTY,compositor:this.compositor,overlayComposer:this.overlayComposer,toolLane:this.toolLane,thinkingLane:this.thinkingLane,thinkingMode:this.thinkingMode,streamingMarkdown:this.streamingMarkdownRef,coordinator:this.coordinator,...this.isTTY?{stageTracker:this.stageTracker}:{},...this.activeSkillName?{activeSkillName:this.activeSkillName}:{}}),this.lastProgressByTask),this.onStageChange&&this.stageTracker.stage!==i)try{this.onStageChange(this.stageTracker.stage)}catch{}}else{if(xv(t,r,s,Yp({isTTY:this.isTTY,compositor:this.compositor,overlayComposer:this.overlayComposer,toolLane:this.toolLane,out:this.out,streamingMarkdown:this.subagentMarkdown,thinkingMode:this.thinkingMode})),s.lastEventAt=Date.now(),s.pauseAnnotation!==void 0&&s.syntheticAgentToolUseId){s.pauseAnnotation=void 0,s.stalledTicks=0;let a=s.agentType??r;this.toolLane.addStartWithAgentContext(s.syntheticAgentToolUseId,"Agent",`(${a})`,void 0)}if((t.type==="done"||t.type==="error")&&this.isTTY){let a=s.syntheticAgentToolUseId;if(a&&this.toolLane.hasEntry(a)){let c=this.toolLane.flushSource(a),u=this.compositor,d=this.overlayComposer,m=this.toolLane,f=this.out;this.coordinator.schedule({anchor:`after-subagent:${r}`,commits:[()=>{if(u){for(let g of c)u.commitAbove(g);u.commitAbove(""),d?(d.markDirty("tool-lane"),d.flush()):u.setOverlay(m.getOverlay())}else{for(let g of c)f.line(g);f.line("")}}]});try{this.streamingMarkdownRef.current&&this.streamingMarkdownRef.current.commitPending()}finally{this.coordinator.drainSubagent(r)}}let l=Vp({out:this.out,isTTY:this.isTTY,compositor:this.compositor,overlayComposer:this.overlayComposer,toolLane:this.toolLane,thinkingLane:this.thinkingLane,thinkingMode:this.thinkingMode,streamingMarkdown:this.streamingMarkdownRef,coordinator:this.coordinator,...this.isTTY?{stageTracker:this.stageTracker}:{},...this.activeSkillName?{activeSkillName:this.activeSkillName}:{}});Sr(l,this.lastProgressByTask)}}}async dispose(){if(this.disposed)return;this.disposed=!0,this.resizeUnsub&&(this.resizeUnsub(),this.resizeUnsub=null);let t=this.streamingMarkdownRef.current?()=>this.streamingMarkdownRef.current.flush():void 0;await this.coordinator.flushAll(t),this.streamingMarkdownRef.current&&(this.streamingMarkdownRef.current.dispose(),this.streamingMarkdownRef.current=null);for(let r of this.subagentMarkdown.values()){try{await r.flush()}catch{}r.dispose()}if(this.subagentMarkdown.clear(),this.toolLane.hasPending()){let r=this.toolLane.flush();if(this.isTTY&&this.compositor){for(let o of r)this.compositor.commitAbove(o);this.compositor.commitAbove(""),this.overlayComposer?(this.overlayComposer.markDirty("tool-lane"),this.overlayComposer.flush()):this.compositor.setOverlay(this.toolLane.getOverlay())}else{for(let o of r)this.out.line(o);this.out.line("")}}if(this.pauseTickInterval&&(clearInterval(this.pauseTickInterval),this.pauseTickInterval=null),this.compositor){if(this.ownsCompositor)try{this.compositor.disarm()}catch{}else{try{this.compositor.setSpinner({enabled:!1})}catch(r){J("[stream-renderer] borrow-dispose setSpinner: "+String(r))}try{this.overlayComposer?(this.overlayComposer.invalidate(),this.overlayComposer.flush()):this.compositor.setOverlay("")}catch(r){J("[stream-renderer] borrow-dispose setOverlay: "+String(r))}try{this.compositor.setInputMode("idle")}catch(r){J("[stream-renderer] borrow-dispose setInputMode: "+String(r))}try{this.compositor.setOnCancel(this.priorOnCancel??null)}catch(r){J("[stream-renderer] borrow-dispose setOnCancel: "+String(r))}this.priorOnCancel=void 0}this.compositor=null,this.borrowedCompositor=null}let n=this.out;if(typeof n.flush=="function")try{n.flush()}catch{}}checkPauseAnnotations(){_v({compositor:this.compositor,disposed:this.disposed,sources:this.sources,toolLane:this.toolLane,isTTY:this.isTTY,overlayComposer:this.overlayComposer,stageTracker:this.stageTracker,thinkingMode:this.thinkingMode,thinkingLane:this.thinkingLane,streamingMarkdownRef:this.streamingMarkdownRef,lastProgressByTask:this.lastProgressByTask,out:this.out,pauseTickInterval:this.pauseTickInterval,resizeUnsub:this.resizeUnsub})}};function Wl(e,t){let n=t.verbose??T.AFK_SKILL_STREAM_VERBOSE==="1",r=t.out??e.out,o=e.getCompositor?.()??null;return new Bo({out:r,verbose:n,activeSkillName:t.skillName,onCancel:t.onCancel??(()=>{}),...o?{compositor:o}:{}})}function Uo(e){let t=e!==void 0?r=>{e.fn(r)}:r=>{console.log(r)},n=e!==void 0&&e.rawFn!==void 0?r=>{e.rawFn(r)}:r=>{process.stdout.write(r)};return{line(r=""){t(r)},raw(r){n(r)},success(r){t(p.success("\u2713 ")+r)},info(r){t(p.info("\u2139 ")+r)},warn(r){t(p.warning("\u26A0 ")+r)},error(r){t(p.error("\u2717 ")+r)}}}var RB="You are initializing this project for use with AFK (an AI agent CLI).\n\nYour job: scan this project and generate an `AFK.md` file in the project root. AFK.md is a plain markdown file (no YAML frontmatter) that serves as the system prompt for all AFK sessions in this project.\n\n## Steps\n\n1. **Discover project metadata** \u2014 read these files if they exist:\n - `package.json`, `tsconfig.json`, `pyproject.toml`, `Cargo.toml`, `go.mod`, `Makefile`, `CMakeLists.txt`\n - `.github/workflows/` (CI config)\n - `docker-compose.yml`, `Dockerfile`\n - `README.md` or `README`\n - Any existing `CLAUDE.md` or `AGENTS.md` (borrow relevant context)\n\n2. **Scan directory structure** \u2014 list top-level directories and key subdirectories to understand the project layout.\n\n3. **Generate AFK.md** with these sections:\n - **What This Is** \u2014 one-paragraph description of the project (language, framework, purpose)\n - **Commands** \u2014 build, test, lint, dev commands (extracted from package.json scripts, Makefile targets, etc.)\n - **Architecture** \u2014 key directories and their purpose, entry points, major subsystems\n - **Conventions** \u2014 coding style, naming patterns, anything notable from config files (strictness levels, linting rules)\n - Only include sections where you found real content. Skip empty sections.\n\n4. **Write the file** \u2014 write AFK.md to the project root. Keep it concise \u2014 under 150 lines. This file is loaded into every session, so brevity matters.\n\n## Format rules\n- Plain markdown, no YAML frontmatter\n- Use code blocks for commands\n- Use tables for directory layouts\n- Don't include boilerplate or filler \u2014 every line should earn its place",Mv={name:"/init",summary:"Scan project and generate AFK.md",hint:"When you're in a fresh repo and want the model to bootstrap an AFK.md system prompt that captures conventions, commands, and architecture.",async handler(e,t){let n=Pv(process.cwd(),"AFK.md"),r=Pv(process.cwd(),"CLAUDE.md"),o=[];o.push({type:"text",text:Tl("init",t)});let s=RB;if(Iv(n)&&!t.includes("--force")&&(s+=`
2123
+ `)}var Bo=class{out;thinkingMode;isTTY;captureMode;reducedMotion;onCancel;onBackground;activeSkillName;history;autocompleteState;promptText;scrollRegion;onStageChange;ownsCompositor=!0;borrowedCompositor=null;priorOnCancel=void 0;interrupting=!1;coordinator=new Nl;compositor=null;overlayComposer=null;streamingMarkdownRef={current:null};toolLane=new ri;thinkingLane=new Do;stageTracker=bv();sources=new Map;subagentMarkdown=new Map;lastProgressByTask=new Map;disposed=!1;pauseTickInterval=null;resizeUnsub=null;sink;constructor(t){this.captureMode=t.captureMode??kS(),this.reducedMotion=t.reducedMotion??vS(),this.out=this.captureMode?Ev(t.out,2):t.out;let n=t.thinkingMode??(t.verbose===!0?"live":"summary");this.thinkingMode=this.captureMode&&n==="live"?"summary":n,this.onCancel=t.onCancel,this.onBackground=t.onBackground,this.isTTY=!(t.forceNonTty??!1)&&!!process.stdout.isTTY&&!!process.stdin.isTTY,this.activeSkillName=t.activeSkillName,this.history=t.history,this.autocompleteState=t.autocompleteState,this.promptText=t.promptText,this.scrollRegion=t.scrollRegion,this.onStageChange=t.onStageChange,t.compositor&&(this.borrowedCompositor=t.compositor,this.ownsCompositor=!1),this.sink=(r,o)=>this.process(r,o)}async arm(){if(this.disposed||!this.isTTY||this.compositor)return;let t;if(this.borrowedCompositor)t=this.borrowedCompositor,t.setInputMode("streaming"),this.priorOnCancel=t.getOnCancel(),this.onCancel&&t.setOnCancel(this.onCancel);else{let n={has:r=>Ze().some(o=>o.name===`/${r}`)};t=new Mo({stdout:process.stdout,stdin:process.stdin,...this.onCancel?{onCancel:this.onCancel}:{},...this.onBackground?{onBackground:this.onBackground}:{},...this.history?{history:this.history}:{},...this.autocompleteState?{autocompleteState:this.autocompleteState}:{},...this.promptText!==void 0?{promptText:this.promptText}:{},formatInputBuffer:r=>Wn(r,n),...this.scrollRegion?{scrollRegion:this.scrollRegion}:{},captureMode:this.captureMode}),await t.arm()}this.compositor=t,this.overlayComposer=new jl(t,["thinking-live","markdown-pending","tool-lane","progress-banner","interrupt"]),Rv(this.overlayComposer,{stageTracker:this.stageTracker,thinkingMode:this.thinkingMode,thinkingLane:this.thinkingLane,streamingMarkdownRef:this.streamingMarkdownRef,toolLane:this.toolLane,lastProgressByTask:this.lastProgressByTask,getInterrupting:()=>this.interrupting}),t.setSpinner({enabled:!this.reducedMotion,rotateVerbEveryMs:3500}),this.pauseTickInterval=setInterval(()=>this.checkPauseAnnotations(),80),this.resizeUnsub=Av(this.overlayComposer,!1)}getCompositor(){return this.compositor}setInterrupting(t){this.disposed||(this.interrupting=t,this.overlayComposer&&(this.overlayComposer.markDirty("interrupt"),this.overlayComposer.flush()))}process(t,n){if(this.disposed)return;let r=n?.subagentId??Bp,o=r===Bp,s=this.sources.get(r);if(!s&&(s=lv(n?.agentType),this.sources.set(r,s),!o)){let i=Cv({parentId:n?.parentId,sources:this.sources,toolLane:this.toolLane,sourceId:r});Jp(r,s,Yp({isTTY:this.isTTY,compositor:this.compositor,overlayComposer:this.overlayComposer,toolLane:this.toolLane,out:this.out,streamingMarkdown:this.subagentMarkdown,thinkingMode:this.thinkingMode}),i)}if(o){let i=this.stageTracker.stage;if(kv(t,s,Vp({out:this.out,isTTY:this.isTTY,compositor:this.compositor,overlayComposer:this.overlayComposer,toolLane:this.toolLane,thinkingLane:this.thinkingLane,thinkingMode:this.thinkingMode,streamingMarkdown:this.streamingMarkdownRef,coordinator:this.coordinator,...this.isTTY?{stageTracker:this.stageTracker}:{},...this.activeSkillName?{activeSkillName:this.activeSkillName}:{}}),this.lastProgressByTask),this.onStageChange&&this.stageTracker.stage!==i)try{this.onStageChange(this.stageTracker.stage)}catch{}}else{if(xv(t,r,s,Yp({isTTY:this.isTTY,compositor:this.compositor,overlayComposer:this.overlayComposer,toolLane:this.toolLane,out:this.out,streamingMarkdown:this.subagentMarkdown,thinkingMode:this.thinkingMode})),s.lastEventAt=Date.now(),s.pauseAnnotation!==void 0&&s.syntheticAgentToolUseId){s.pauseAnnotation=void 0,s.stalledTicks=0;let a=s.agentType??r;this.toolLane.addStartWithAgentContext(s.syntheticAgentToolUseId,"Agent",`(${a})`,void 0)}if((t.type==="done"||t.type==="error")&&this.isTTY){let a=s.syntheticAgentToolUseId;if(a&&this.toolLane.hasEntry(a)){let c=this.toolLane.flushSource(a),u=this.compositor,d=this.overlayComposer,p=this.toolLane,f=this.out;this.coordinator.schedule({anchor:`after-subagent:${r}`,commits:[()=>{if(u){for(let g of c)u.commitAbove(g);u.commitAbove(""),d?(d.markDirty("tool-lane"),d.flush()):u.setOverlay(p.getOverlay())}else{for(let g of c)f.line(g);f.line("")}}]});try{this.streamingMarkdownRef.current&&this.streamingMarkdownRef.current.commitPending()}finally{this.coordinator.drainSubagent(r)}}let l=Vp({out:this.out,isTTY:this.isTTY,compositor:this.compositor,overlayComposer:this.overlayComposer,toolLane:this.toolLane,thinkingLane:this.thinkingLane,thinkingMode:this.thinkingMode,streamingMarkdown:this.streamingMarkdownRef,coordinator:this.coordinator,...this.isTTY?{stageTracker:this.stageTracker}:{},...this.activeSkillName?{activeSkillName:this.activeSkillName}:{}});Sr(l,this.lastProgressByTask)}}}async dispose(){if(this.disposed)return;this.disposed=!0,this.resizeUnsub&&(this.resizeUnsub(),this.resizeUnsub=null);let t=this.streamingMarkdownRef.current?()=>this.streamingMarkdownRef.current.flush():void 0;await this.coordinator.flushAll(t),this.streamingMarkdownRef.current&&(this.streamingMarkdownRef.current.dispose(),this.streamingMarkdownRef.current=null);for(let r of this.subagentMarkdown.values()){try{await r.flush()}catch{}r.dispose()}if(this.subagentMarkdown.clear(),this.toolLane.hasPending()){let r=this.toolLane.flush();if(this.isTTY&&this.compositor){for(let o of r)this.compositor.commitAbove(o);this.compositor.commitAbove(""),this.overlayComposer?(this.overlayComposer.markDirty("tool-lane"),this.overlayComposer.flush()):this.compositor.setOverlay(this.toolLane.getOverlay())}else{for(let o of r)this.out.line(o);this.out.line("")}}if(this.pauseTickInterval&&(clearInterval(this.pauseTickInterval),this.pauseTickInterval=null),this.compositor){if(this.ownsCompositor)try{this.compositor.disarm()}catch{}else{try{this.compositor.setSpinner({enabled:!1})}catch(r){J("[stream-renderer] borrow-dispose setSpinner: "+String(r))}try{this.overlayComposer?(this.overlayComposer.invalidate(),this.overlayComposer.flush()):this.compositor.setOverlay("")}catch(r){J("[stream-renderer] borrow-dispose setOverlay: "+String(r))}try{this.compositor.setInputMode("idle")}catch(r){J("[stream-renderer] borrow-dispose setInputMode: "+String(r))}try{this.compositor.setOnCancel(this.priorOnCancel??null)}catch(r){J("[stream-renderer] borrow-dispose setOnCancel: "+String(r))}this.priorOnCancel=void 0}this.compositor=null,this.borrowedCompositor=null}let n=this.out;if(typeof n.flush=="function")try{n.flush()}catch{}}checkPauseAnnotations(){_v({compositor:this.compositor,disposed:this.disposed,sources:this.sources,toolLane:this.toolLane,isTTY:this.isTTY,overlayComposer:this.overlayComposer,stageTracker:this.stageTracker,thinkingMode:this.thinkingMode,thinkingLane:this.thinkingLane,streamingMarkdownRef:this.streamingMarkdownRef,lastProgressByTask:this.lastProgressByTask,out:this.out,pauseTickInterval:this.pauseTickInterval,resizeUnsub:this.resizeUnsub})}};function Wl(e,t){let n=t.verbose??T.AFK_SKILL_STREAM_VERBOSE==="1",r=t.out??e.out,o=e.getCompositor?.()??null;return new Bo({out:r,verbose:n,activeSkillName:t.skillName,onCancel:t.onCancel??(()=>{}),...o?{compositor:o}:{}})}function Uo(e){let t=e!==void 0?r=>{e.fn(r)}:r=>{console.log(r)},n=e!==void 0&&e.rawFn!==void 0?r=>{e.rawFn(r)}:r=>{process.stdout.write(r)};return{line(r=""){t(r)},raw(r){n(r)},success(r){t(m.success("\u2713 ")+r)},info(r){t(m.info("\u2139 ")+r)},warn(r){t(m.warning("\u26A0 ")+r)},error(r){t(m.error("\u2717 ")+r)}}}var RB="You are initializing this project for use with AFK (an AI agent CLI).\n\nYour job: scan this project and generate an `AFK.md` file in the project root. AFK.md is a plain markdown file (no YAML frontmatter) that serves as the system prompt for all AFK sessions in this project.\n\n## Steps\n\n1. **Discover project metadata** \u2014 read these files if they exist:\n - `package.json`, `tsconfig.json`, `pyproject.toml`, `Cargo.toml`, `go.mod`, `Makefile`, `CMakeLists.txt`\n - `.github/workflows/` (CI config)\n - `docker-compose.yml`, `Dockerfile`\n - `README.md` or `README`\n - Any existing `CLAUDE.md` or `AGENTS.md` (borrow relevant context)\n\n2. **Scan directory structure** \u2014 list top-level directories and key subdirectories to understand the project layout.\n\n3. **Generate AFK.md** with these sections:\n - **What This Is** \u2014 one-paragraph description of the project (language, framework, purpose)\n - **Commands** \u2014 build, test, lint, dev commands (extracted from package.json scripts, Makefile targets, etc.)\n - **Architecture** \u2014 key directories and their purpose, entry points, major subsystems\n - **Conventions** \u2014 coding style, naming patterns, anything notable from config files (strictness levels, linting rules)\n - Only include sections where you found real content. Skip empty sections.\n\n4. **Write the file** \u2014 write AFK.md to the project root. Keep it concise \u2014 under 150 lines. This file is loaded into every session, so brevity matters.\n\n## Format rules\n- Plain markdown, no YAML frontmatter\n- Use code blocks for commands\n- Use tables for directory layouts\n- Don't include boilerplate or filler \u2014 every line should earn its place",Mv={name:"/init",summary:"Scan project and generate AFK.md",hint:"When you're in a fresh repo and want the model to bootstrap an AFK.md system prompt that captures conventions, commands, and architecture.",async handler(e,t){let n=Pv(process.cwd(),"AFK.md"),r=Pv(process.cwd(),"CLAUDE.md"),o=[];o.push({type:"text",text:Tl("init",t)});let s=RB;if(Iv(n)&&!t.includes("--force")&&(s+=`
2124
2124
 
2125
2125
  ## Existing AFK.md detected
2126
2126
  An AFK.md already exists at \`${n}\`. Read it first \u2014 then update it with any new information from the project scan. Preserve user-written content and only add/refresh sections derived from project metadata. If the existing file is already good, say so and make minimal changes.`),Iv(r)&&(s+=`
@@ -2129,41 +2129,41 @@ An AFK.md already exists at \`${n}\`. Read it first \u2014 then update it with a
2129
2129
  A CLAUDE.md exists at \`${r}\`. Read it and incorporate relevant context (commands, conventions, architecture) into the AFK.md. Don't duplicate \u2014 adapt.`),t.trim()){let a=t.replace("--force","").trim();a&&(s+=`
2130
2130
 
2131
2131
  ## Additional context from user
2132
- ${a}`)}o.push({type:"text",text:s});let i=Wl(e,{skillName:"init",out:Uo(),onCancel:()=>{e.session.current.interrupt().catch(()=>{})}});try{await i.arm(),await qr(i.sink,async()=>{for await(let a of e.session.current.sendMessageStream(o))i.sink(a)})}catch(a){e.out.line(),e.out.error(`init failed: ${a instanceof Error?a.message:String(a)}`)}finally{await i.dispose()}return"continue"}};import Ov from"chalk";var Xp=new Map;function $v(e,t){let n=Xp.get(e);if(n){if(n.glyph===t.glyph&&n.color===t.color&&n.inFlightVerb===t.inFlightVerb)return;throw new Error(`Trusted skill "${e}" already registered with different config`)}Xp.set(e,t),Cy(e)}function Hl(e){return Xp.get(e)}function AB(e){return(e/1e3).toFixed(1)+"s"}function Dv(e,t){let n=Hl(e.skillName),r=AB(e.durationMs);if(!n){let s=`[${e.skillName} \xB7 ${r}]`;return jo(s,t?.columns)}if(t?.isTTY!==!1){let s;if(e.claimsTotal!==void 0){let a=e.claimsTotal===e.claimsConfirmed&&e.claimsRefuted===void 0&&e.claimsInconclusive===void 0,l;if(a)l=`${e.claimsTotal} claims \xB7 all confirmed`;else{let c=`${e.claimsTotal} claims`;e.claimsConfirmed!==void 0&&(c+=` \xB7 ${e.claimsConfirmed} confirmed`),e.claimsRefuted!==void 0&&(c+=` \xB7 ${e.claimsRefuted} refuted`),e.claimsInconclusive!==void 0&&(c+=` \xB7 ${e.claimsInconclusive} inconclusive`),l=c}s=`${n.glyph} ${e.skillName} \xB7 ${l} \xB7 ${r}`}else s=`${n.glyph} ${e.skillName} \xB7 ${r}`;let i=Ov.hex(n.color)(s);return jo(i,t?.columns)}else{let s;if(e.claimsTotal!==void 0){let i=e.claimsConfirmed??0;s=`[${e.skillName} \xB7 ${i}/${e.claimsTotal} confirmed \xB7 ${r}]`}else s=`[${e.skillName} \xB7 ${r}]`;return jo(s,t?.columns)}}function jo(e,t){return t!==void 0&&z(e)>t?ae(e,t):e}function Lv(e,t){let n=Hl(e);if(!n){let s=`[${e} \xB7 running\u2026]`;return jo(s,t?.columns)}if(t?.isTTY!==!1){let s=`${n.glyph} ${e} \xB7 ${n.inFlightVerb}`,i=Ov.hex(n.color)(s);return jo(i,t?.columns)}let o=`[${e} \xB7 ${n.inFlightVerb}]`;return jo(o,t?.columns)}var Fv={name:"/stats",summary:"Show session statistics including skill runs",async handler(e){let t=e.ledger;if(!t)return e.out.info("No skill stats available."),"continue";let n=t.summary();if(!n)return e.out.info("No skill runs recorded this session."),"continue";e.out.line(),e.out.line(p.bold("Skill runs"));for(let[r,o]of n){let s=Hl(r)?.glyph??"",i=s?`${s} `:"",a=`${(o.totalDurationMs/1e3).toFixed(1)}s total`,l=`${o.runs} run${o.runs!==1?"s":""}`,c="";o.totalClaims!==void 0&&(c=` \xB7 ${o.totalClaims} claims`,o.totalConfirmed!==void 0&&(c+=` \xB7 ${o.totalConfirmed} confirmed`),o.totalRefuted!==void 0&&(c+=` \xB7 ${o.totalRefuted} refuted`),o.totalInconclusive!==void 0&&(c+=` \xB7 ${o.totalInconclusive} inconclusive`)),e.out.line(` ${i}${r} ${l}${c} \xB7 ${a}`)}return e.out.line(),"continue"}};function _B(e=Ju){return{name:"/font-size",summary:"Get or set the terminal font size in Cursor / VS Code",usage:"/font-size [size] [editor]",hint:"Direct shortcut to the terminal_font_size tool \u2014 bypasses the LLM and the first-write permission prompt. Examples: `/font-size` (read all), `/font-size 18` (set all), `/font-size 14 cursor` (set Cursor only).",async handler(t,n){let r=n.split(/\s+/).filter(Boolean),o=new AbortController;if(r.length===0){let u=await e({action:"get"},o.signal);return u.isError?t.out.error(u.content):t.out.line(u.content),"continue"}let s=r[0],i=Number(s);if(!Number.isFinite(i))return t.out.error(`Invalid size: "${s}". Usage: /font-size [size] [editor]`),"continue";let a=r[1],l={action:"set",size:i};a!==void 0&&(l.editor=a);let c=await e(l,o.signal);return c.isError?t.out.error(c.content):t.out.success(c.content),"continue"}}}var Nv=_B();import CB from"path";import{statSync as IB}from"fs";var kr;function Bv(e){kr=e}var Uv={name:"/allow-dir",summary:"Manage per-session directory access grants for tool handlers",usage:"/allow-dir [--rw | --revoke] [<path>]",flags:["--rw","--revoke"],async handler(e,t){if(!kr)return e.out.error("Directory grants not available in this session."),"continue";let n=t.trim();if(!n){let a=kr.getGrants();return e.out.line(" Session directory grants:"),e.out.line(` resolveBase : ${a.resolveBase??"(none)"}`),e.out.line(` readRoots : ${a.readRoots.length>0?a.readRoots.join(", "):"(none)"}`),e.out.line(` writeRoots : ${a.writeRoots.length>0?a.writeRoots.join(", "):"(none)"}`),"continue"}let r="read",o=n;if(o.startsWith("--rw ")||o==="--rw"?(r="write",o=o.slice(5).trim()):(o.startsWith("--revoke ")||o==="--revoke")&&(r="revoke",o=o.slice(9).trim()),!o)return e.out.error("Usage: /allow-dir [--rw | --revoke] <path>"),"continue";let s=CB.resolve(process.cwd(),o);if(r!=="revoke")try{IB(s)}catch{return e.out.error(`Path does not exist: ${s}`),"continue"}let i=e.stats.sessionId;if(r==="revoke"){let a=kr.getGrants();kr.revokeRoot(s,"slash",i),a.resolveBase&&s===a.resolveBase?e.out.warn(`Cannot revoke the session's initial resolveBase: ${s}`):e.out.line(`\u2713 Revoked: ${s}`)}else r==="write"?(kr.addWriteRoot(s,"slash",i),e.out.line(`\u2713 Read+write grant: ${s}`)):(kr.addReadRoot(s,"slash",i),e.out.line(`\u2713 Read-only grant: ${s}`));return"continue"}};var jv=[{group:"Navigation",rows:[["ctrl+a","Move to start of current line"],["ctrl+e","Move to end of current line"],["ctrl+b","Move one character backward (input mode) / Run turn in background (streaming mode)"],["ctrl+f","Move one character forward"],["alt+b","Move one word backward"],["alt+f","Move one word forward"],["\u2190 / \u2192","Character left / right"],["home / end","Buffer start / end"]]},{group:"Editing",rows:[["ctrl+u","Delete to start of current line"],["ctrl+k","Delete to end of current line"],["ctrl+w","Delete previous word"],["backspace","Delete previous character"],["delete","Delete next character"],["alt+backspace","Delete previous word (Option+Delete on macOS)"],["alt+delete","Delete next word (Option+Fn-Delete on macOS)"]]},{group:"History",rows:[["ctrl+p / \u2191","Previous history entry (or move up in multi-line draft)"],["ctrl+n / \u2193","Next history entry (or move down in multi-line draft)"]]},{group:"Multi-line",rows:[["shift+enter","Insert newline (no submit)"],["alt+enter","Insert newline (no submit)"],["<text>\\","Trailing \\ + Enter inserts newline (backwards-compat)"]]},{group:"Misc",rows:[["ctrl+l","Clear screen and repaint"],["ctrl+v","Paste image from clipboard"],["ctrl+x","Remove last attached image"],["ctrl+c","Interrupt running turn / exit (second press)"],["ctrl+d","EOF / exit (when buffer is empty)"],["tab","Accept autocomplete suggestion"],["enter","Submit prompt"]]}],Wv={name:"/keys",summary:"Show keybinding reference",async handler(e){e.out.line(),e.out.line(p.bold(p.brand("Keybindings"))),e.out.line(he());let n=jv.flatMap(r=>r.rows).reduce((r,[o])=>Math.max(r,o.length),0);for(let{group:r,rows:o}of jv){e.out.line(),e.out.line(p.bold(r));for(let[s,i]of o){let a=" ".repeat(Math.max(0,n-s.length));e.out.line(` ${p.warning(s)}${a} ${p.dim(i)}`)}}return e.out.line(),"continue"}};import{execFile as UB}from"node:child_process";import{promises as jB}from"node:fs";import{dirname as WB,isAbsolute as HB,join as KB,resolve as GB}from"node:path";import{promisify as qB}from"node:util";W();import{promises as Pt,existsSync as Hv,createReadStream as PB}from"node:fs";import{join as Kl}from"node:path";import{createInterface as MB}from"node:readline";var OB=36e5,$B=30*864e5;function DB(e){try{return process.kill(e,0),!0}catch(t){return t.code==="EPERM"}}function LB(e){let t=e.trim().split(/\n\n+/),n=[];for(let r of t){let o=r.split(`
2133
- `),s="",i="",a="",l=!1,c=!1,u=!1;for(let d of o)d.startsWith("worktree ")?s=d.slice(9).trim():d.startsWith("HEAD ")?i=d.slice(5).trim():d.startsWith("branch ")?a=d.slice(7).trim():d.trim()==="locked"?l=!0:d.trim()==="prunable"?c=!0:d.trim()==="bare"&&(u=!0);s&&n.push({path:s,head:i,branch:a,locked:l,prunable:c,isBare:u})}return n}function FB(e,t,n){if(e.locked)return"locked";let r=864e5,o=t*r,s=n*r;return e.ownerLiveness==="dead"&&!e.isDirty&&e.commitsAhead===0?"dead-owner":e.commitsAhead===0&&!e.isDirty&&e.ageMs>=OB?"empty":e.isDirty&&e.ageMs>s?"stale-dirty":!e.isDirty&&e.ageMs>o?"stale-clean":"active"}async function NB(e){if(!Hv(e))return 0;let t=0;try{let n=MB({input:PB(e),crlfDelay:1/0});for await(let r of n){let o=r.trim();if(o)try{let s=JSON.parse(o);s.taskId==="worktree-prune"&&(s.status==="success"||s.status==="error")&&t++}catch{}}}catch{}return t}var Gl=class extends Error{constructor(t){super(`Worktree sweep lock contested: ${t} \u2014 another sweep may be running.`),this.name="LockContestedError"}};async function BB(e){let t=Kl(e,"..");await Pt.mkdir(t,{recursive:!0}).catch(()=>{});let r=await(async()=>{try{return await Pt.open(e,"wx")}catch(o){if(o.code!=="EEXIST")throw o;let i=null;try{let a=await Pt.readFile(e,"utf-8");i=parseInt(a.trim(),10)}catch{}if(i!==null&&!Number.isNaN(i)){let a=!1;try{process.kill(i,0),a=!0}catch{}if(!a)return await Pt.unlink(e).catch(()=>{}),await Pt.open(e,"wx")}throw new Gl(e)}})();return await r.writeFile(String(process.pid),"utf-8"),await r.close(),async()=>{await Pt.unlink(e).catch(()=>{})}}async function zt(e){let{execFile:t,repoRoot:n,maxAgeDaysClean:r=14,maxAgeDaysDirty:o=30,scope:s="all",telemetryPath:i}=e,a=i??$t(),l=e.lockPath??Nf(),c={removed:[],warnings:[],dryRun:e.dryRun??!1,candidates:[]},u=e.bypassSoftLaunch?Number.POSITIVE_INFINITY:await NB(a),d=e.dryRun===!0||u<3;c.dryRun=d;let m=null;try{m=await BB(l)}catch(f){if(f instanceof Gl)return c.warnings.push(`[WARN] ${f.message}`),c;throw f}try{let f=await t("git",["-C",n,"worktree","list","--porcelain"]),g=LB(f.stdout),h=Kl(n,".afk-worktrees"),b=new Set(g.map(v=>v.path)),y=[];try{y=(await Pt.readdir(h,{withFileTypes:!0})).filter(E=>E.isDirectory()).map(E=>Kl(h,E.name))}catch{}let w=y.filter(v=>!b.has(v));if(s==="all"||s==="interactive")for(let v of w){let E=0;try{let I=await Pt.stat(v);E=Date.now()-I.birthtimeMs}catch{}if(c.candidates.push({path:v,verdict:"orphaned-dir",owner:"interactive",ageMs:E}),!d)try{await Pt.rm(v,{recursive:!0,force:!0}),c.removed.push(v)}catch(I){c.warnings.push(`[ERROR] Failed to remove orphaned dir ${v}: ${I instanceof Error?I.message:String(I)}`)}}let x=!1,R=g[0]?.path;for(let v of g){if(v.path===R||v.isBare||!v.path.startsWith(h))continue;let E;try{let A=await Pt.readFile(Kl(v.path,".afk-worktree-meta.json"),"utf-8");E=JSON.parse(A)}catch{}if(s!=="all"&&E?.owner!==s)continue;let I=E?.owner==="interactive"||E?.owner==="diagnose"?E.owner:"unknown";if(!Hv(v.path)){c.candidates.push({path:v.path,verdict:"orphaned-registration",owner:I,ageMs:0}),d||(x=!0);continue}let D=0,_=E?.createdAt;if(_)D=Date.now()-new Date(_).getTime();else try{let A=await Pt.stat(v.path);D=Date.now()-A.birthtimeMs}catch{}let M=!1,P=0;try{M=(await t("git",["-C",v.path,"status","--porcelain"])).stdout.trim().length>0}catch{M=!0}if(!M&&v.head){let A=E?.baseSha??v.head;try{let N=await t("git",["-C",n,"rev-list",`${A}..${v.head}`,"--count"]);P=parseInt(N.stdout.trim(),10)||0}catch{P=0}}let $="unknown";typeof E?.pid=="number"&&Number.isInteger(E.pid)&&E.pid>0&&D<=$B&&($=DB(E.pid)?"alive":"dead");let F={path:v.path,head:v.head,branch:v.branch,locked:v.locked,prunable:v.prunable,meta:E,ageMs:D,isDirty:M,commitsAhead:P,ownerLiveness:$},k=FB(F,r,o);if(c.candidates.push({path:v.path,verdict:k,owner:I,ageMs:D}),!d)try{k==="empty"?(await t("git",["-C",n,"worktree","remove","--force",v.path]),v.branch&&await t("git",["-C",n,"branch","-d",v.branch]).catch(()=>{}),c.removed.push(v.path)):k==="dead-owner"?(await t("git",["-C",n,"worktree","remove","--force",v.path]),v.branch&&await t("git",["-C",n,"branch","-d",v.branch]).catch(()=>{}),c.removed.push(v.path)):k==="stale-clean"?(await t("git",["-C",n,"worktree","remove","--force",v.path]),c.removed.push(v.path)):k==="stale-dirty"&&c.warnings.push(`[WARN] stale-dirty worktree preserved (uncommitted changes): ${v.path}`)}catch(A){c.warnings.push(`[ERROR] Failed to process ${v.path} (${k}): ${A instanceof Error?A.message:String(A)}`)}}if(x&&!d)try{await t("git",["-C",n,"worktree","prune"])}catch(v){c.warnings.push(`[ERROR] git worktree prune failed: ${v instanceof Error?v.message:String(v)}`)}}finally{m&&await m()}return c}var Zp=qB(UB),Kv=["interactive","diagnose","all"],ql=new Set(["empty","stale-clean","orphaned-dir","orphaned-registration","dead-owner"]);async function Gv(){let t=(await Zp("git",["rev-parse","--git-common-dir"])).stdout.trim();if(!t)throw new Error("Not in a git repository.");let n=HB(t)?t:GB(process.cwd(),t);return WB(n)}function zB(e){if(e<=0)return"-";let t=e/864e5;return t<1?`${Math.max(1,Math.round(e/36e5))}h`:`${Math.round(t)}d`}function JB(e,t){return ql.has(e)?p.error(t):e==="stale-dirty"?p.warning(t):e==="locked"?p.dim(t):p.dim(t)}function qv(e,t){let n=e.split(/\s+/).map(o=>o.trim()).filter(o=>o.length>0),r={scope:t,apply:!1,unknown:[]};for(let o=0;o<n.length;o++){let s=n[o];if(s==="--apply")r.apply=!0;else if(s==="--scope"&&o+1<n.length){let i=n[o+1];o++,Kv.includes(i)?r.scope=i:r.unknown.push(`--scope=${i}`)}else if(s.startsWith("--scope=")){let i=s.slice(8);Kv.includes(i)?r.scope=i:r.unknown.push(s)}else r.unknown.push(s)}return r}async function VB(e){try{let t=await jB.readFile(KB(e,".afk-worktree-meta.json"),"utf-8"),n=JSON.parse(t);if(typeof n.pid=="number"&&Number.isInteger(n.pid)&&n.pid>0)return n.pid}catch{}}async function YB(e,t,n){if(t.length===0){e.info("No afk-managed worktrees found.");return}let r=process.pid;e.line(),e.line(p.bold("Worktrees")),e.line(p.dim(" "+"PATH".padEnd(45)+" "+"OWNER".padEnd(12)+" "+"AGE".padEnd(5)+" "+"VERDICT".padEnd(22)+" PRUNE?"));for(let o of t){let a=n.get(o.path)===r?p.brand("\u2192 "):" ",l=o.path.slice(-44).padEnd(45),c=o.owner.padEnd(12),u=zB(o.ageMs).padEnd(5),d=o.verdict.padEnd(22),m=ql.has(o.verdict)?p.error("yes"):o.verdict==="stale-dirty"?p.warning("warn"):p.dim("no"),f=JB(o.verdict,d);e.line(`${a}${l} ${c} ${u} ${f} ${m}`)}e.line(),e.line(p.dim(" \u2192 this session")),e.line()}async function XB(e){let t=new Map;for(let n of e)t.set(n.path,await VB(n.path));return t}async function ZB(e,t){let n=qv(t,"interactive");n.unknown.length>0&&e.out.warn(`Unknown args ignored: ${n.unknown.join(" ")}`);let r;try{r=await Gv()}catch(i){return e.out.error(`Not in a git repository: ${i.message}`),"continue"}let o;try{let i={execFile:Zp,repoRoot:r,dryRun:!0,scope:n.scope};o=await zt(i)}catch(i){return e.out.error(`Sweep failed: ${i.message}`),"continue"}let s=await XB(o.candidates);await YB(e.out,o.candidates,s);for(let i of o.warnings)e.out.warn(i);return"continue"}async function QB(e,t){let n=qv(t,"interactive");n.unknown.length>0&&e.out.warn(`Unknown args ignored: ${n.unknown.join(" ")}`);let r;try{r=await Gv()}catch(a){return e.out.error(`Not in a git repository: ${a.message}`),"continue"}let o;try{let a={execFile:Zp,repoRoot:r,dryRun:!n.apply,scope:n.scope};o=await zt(a)}catch(a){return e.out.error(`Sweep failed: ${a.message}`),"continue"}let s={};for(let a of o.candidates)s[a.verdict]=(s[a.verdict]??0)+1;let i=Object.entries(s).sort(([a],[l])=>a.localeCompare(l)).map(([a,l])=>`${a}=${l}`);if(o.dryRun){let a=o.candidates.filter(l=>ql.has(l.verdict)).length;e.out.line(),e.out.line(p.warning("\u{1F50D} Dry-run \u2014 pass --apply to actually remove.")+` Would prune ${a} worktree(s).`+(i.length>0?` [${i.join(" ")}]`:""))}else{let a=o.warnings.filter(c=>c.startsWith("[WARN]")).length,l=o.warnings.filter(c=>c.startsWith("[ERROR]")).length;e.out.line(),e.out.success(`Removed ${o.removed.length}, warned ${a}, errors ${l}`+(i.length>0?` [${i.join(" ")}]`:""))}for(let a of o.candidates){let c=o.removed.includes(a.path)?p.error("\u2717"):ql.has(a.verdict)?p.warning("\u2022"):p.dim("\xB7");e.out.line(` ${c} [${a.verdict.padEnd(22)}] ${a.path}`)}for(let a of o.warnings)a.startsWith("[ERROR]")?e.out.error(a):e.out.warn(a);return e.out.line(),"continue"}var zv={name:"/worktree",summary:"List or prune afk-managed git worktrees",usage:"/worktree list | /worktree prune [--apply] [--scope <interactive|diagnose|all>]",async handler(e,t){let n=t.trim();return n.length===0||n.startsWith("list")?ZB(e,n.replace(/^list\s*/,"")):n.startsWith("prune")?QB(e,n.replace(/^prune\s*/,"")):(e.out.error(`Unknown /worktree subcommand. Usage:
2132
+ ${a}`)}o.push({type:"text",text:s});let i=Wl(e,{skillName:"init",out:Uo(),onCancel:()=>{e.session.current.interrupt().catch(()=>{})}});try{await i.arm(),await qr(i.sink,async()=>{for await(let a of e.session.current.sendMessageStream(o))i.sink(a)})}catch(a){e.out.line(),e.out.error(`init failed: ${a instanceof Error?a.message:String(a)}`)}finally{await i.dispose()}return"continue"}};import Ov from"chalk";var Xp=new Map;function $v(e,t){let n=Xp.get(e);if(n){if(n.glyph===t.glyph&&n.color===t.color&&n.inFlightVerb===t.inFlightVerb)return;throw new Error(`Trusted skill "${e}" already registered with different config`)}Xp.set(e,t),Cy(e)}function Hl(e){return Xp.get(e)}function AB(e){return(e/1e3).toFixed(1)+"s"}function Dv(e,t){let n=Hl(e.skillName),r=AB(e.durationMs);if(!n){let s=`[${e.skillName} \xB7 ${r}]`;return jo(s,t?.columns)}if(t?.isTTY!==!1){let s;if(e.claimsTotal!==void 0){let a=e.claimsTotal===e.claimsConfirmed&&e.claimsRefuted===void 0&&e.claimsInconclusive===void 0,l;if(a)l=`${e.claimsTotal} claims \xB7 all confirmed`;else{let c=`${e.claimsTotal} claims`;e.claimsConfirmed!==void 0&&(c+=` \xB7 ${e.claimsConfirmed} confirmed`),e.claimsRefuted!==void 0&&(c+=` \xB7 ${e.claimsRefuted} refuted`),e.claimsInconclusive!==void 0&&(c+=` \xB7 ${e.claimsInconclusive} inconclusive`),l=c}s=`${n.glyph} ${e.skillName} \xB7 ${l} \xB7 ${r}`}else s=`${n.glyph} ${e.skillName} \xB7 ${r}`;let i=Ov.hex(n.color)(s);return jo(i,t?.columns)}else{let s;if(e.claimsTotal!==void 0){let i=e.claimsConfirmed??0;s=`[${e.skillName} \xB7 ${i}/${e.claimsTotal} confirmed \xB7 ${r}]`}else s=`[${e.skillName} \xB7 ${r}]`;return jo(s,t?.columns)}}function jo(e,t){return t!==void 0&&z(e)>t?ae(e,t):e}function Lv(e,t){let n=Hl(e);if(!n){let s=`[${e} \xB7 running\u2026]`;return jo(s,t?.columns)}if(t?.isTTY!==!1){let s=`${n.glyph} ${e} \xB7 ${n.inFlightVerb}`,i=Ov.hex(n.color)(s);return jo(i,t?.columns)}let o=`[${e} \xB7 ${n.inFlightVerb}]`;return jo(o,t?.columns)}var Fv={name:"/stats",summary:"Show session statistics including skill runs",async handler(e){let t=e.ledger;if(!t)return e.out.info("No skill stats available."),"continue";let n=t.summary();if(!n)return e.out.info("No skill runs recorded this session."),"continue";e.out.line(),e.out.line(m.bold("Skill runs"));for(let[r,o]of n){let s=Hl(r)?.glyph??"",i=s?`${s} `:"",a=`${(o.totalDurationMs/1e3).toFixed(1)}s total`,l=`${o.runs} run${o.runs!==1?"s":""}`,c="";o.totalClaims!==void 0&&(c=` \xB7 ${o.totalClaims} claims`,o.totalConfirmed!==void 0&&(c+=` \xB7 ${o.totalConfirmed} confirmed`),o.totalRefuted!==void 0&&(c+=` \xB7 ${o.totalRefuted} refuted`),o.totalInconclusive!==void 0&&(c+=` \xB7 ${o.totalInconclusive} inconclusive`)),e.out.line(` ${i}${r} ${l}${c} \xB7 ${a}`)}return e.out.line(),"continue"}};function _B(e=Ju){return{name:"/font-size",summary:"Get or set the terminal font size in Cursor / VS Code",usage:"/font-size [size] [editor]",hint:"Direct shortcut to the terminal_font_size tool \u2014 bypasses the LLM and the first-write permission prompt. Examples: `/font-size` (read all), `/font-size 18` (set all), `/font-size 14 cursor` (set Cursor only).",async handler(t,n){let r=n.split(/\s+/).filter(Boolean),o=new AbortController;if(r.length===0){let u=await e({action:"get"},o.signal);return u.isError?t.out.error(u.content):t.out.line(u.content),"continue"}let s=r[0],i=Number(s);if(!Number.isFinite(i))return t.out.error(`Invalid size: "${s}". Usage: /font-size [size] [editor]`),"continue";let a=r[1],l={action:"set",size:i};a!==void 0&&(l.editor=a);let c=await e(l,o.signal);return c.isError?t.out.error(c.content):t.out.success(c.content),"continue"}}}var Nv=_B();import CB from"path";import{statSync as IB}from"fs";var kr;function Bv(e){kr=e}var Uv={name:"/allow-dir",summary:"Manage per-session directory access grants for tool handlers",usage:"/allow-dir [--rw | --revoke] [<path>]",flags:["--rw","--revoke"],async handler(e,t){if(!kr)return e.out.error("Directory grants not available in this session."),"continue";let n=t.trim();if(!n){let a=kr.getGrants();return e.out.line(" Session directory grants:"),e.out.line(` resolveBase : ${a.resolveBase??"(none)"}`),e.out.line(` readRoots : ${a.readRoots.length>0?a.readRoots.join(", "):"(none)"}`),e.out.line(` writeRoots : ${a.writeRoots.length>0?a.writeRoots.join(", "):"(none)"}`),"continue"}let r="read",o=n;if(o.startsWith("--rw ")||o==="--rw"?(r="write",o=o.slice(5).trim()):(o.startsWith("--revoke ")||o==="--revoke")&&(r="revoke",o=o.slice(9).trim()),!o)return e.out.error("Usage: /allow-dir [--rw | --revoke] <path>"),"continue";let s=CB.resolve(process.cwd(),o);if(r!=="revoke")try{IB(s)}catch{return e.out.error(`Path does not exist: ${s}`),"continue"}let i=e.stats.sessionId;if(r==="revoke"){let a=kr.getGrants();kr.revokeRoot(s,"slash",i),a.resolveBase&&s===a.resolveBase?e.out.warn(`Cannot revoke the session's initial resolveBase: ${s}`):e.out.line(`\u2713 Revoked: ${s}`)}else r==="write"?(kr.addWriteRoot(s,"slash",i),e.out.line(`\u2713 Read+write grant: ${s}`)):(kr.addReadRoot(s,"slash",i),e.out.line(`\u2713 Read-only grant: ${s}`));return"continue"}};var jv=[{group:"Navigation",rows:[["ctrl+a","Move to start of current line"],["ctrl+e","Move to end of current line"],["ctrl+b","Move one character backward (input mode) / Run turn in background (streaming mode)"],["ctrl+f","Move one character forward"],["alt+b","Move one word backward"],["alt+f","Move one word forward"],["\u2190 / \u2192","Character left / right"],["home / end","Buffer start / end"]]},{group:"Editing",rows:[["ctrl+u","Delete to start of current line"],["ctrl+k","Delete to end of current line"],["ctrl+w","Delete previous word"],["backspace","Delete previous character"],["delete","Delete next character"],["alt+backspace","Delete previous word (Option+Delete on macOS)"],["alt+delete","Delete next word (Option+Fn-Delete on macOS)"]]},{group:"History",rows:[["ctrl+p / \u2191","Previous history entry (or move up in multi-line draft)"],["ctrl+n / \u2193","Next history entry (or move down in multi-line draft)"]]},{group:"Multi-line",rows:[["shift+enter","Insert newline (no submit)"],["alt+enter","Insert newline (no submit)"],["<text>\\","Trailing \\ + Enter inserts newline (backwards-compat)"]]},{group:"Misc",rows:[["ctrl+l","Clear screen and repaint"],["ctrl+v","Paste image from clipboard"],["ctrl+x","Remove last attached image"],["ctrl+c","Interrupt running turn / exit (second press)"],["ctrl+d","EOF / exit (when buffer is empty)"],["tab","Accept autocomplete suggestion"],["enter","Submit prompt"]]}],Wv={name:"/keys",summary:"Show keybinding reference",async handler(e){e.out.line(),e.out.line(m.bold(m.brand("Keybindings"))),e.out.line(he());let n=jv.flatMap(r=>r.rows).reduce((r,[o])=>Math.max(r,o.length),0);for(let{group:r,rows:o}of jv){e.out.line(),e.out.line(m.bold(r));for(let[s,i]of o){let a=" ".repeat(Math.max(0,n-s.length));e.out.line(` ${m.warning(s)}${a} ${m.dim(i)}`)}}return e.out.line(),"continue"}};import{execFile as UB}from"node:child_process";import{promises as jB}from"node:fs";import{dirname as WB,isAbsolute as HB,join as KB,resolve as GB}from"node:path";import{promisify as qB}from"node:util";W();import{promises as Pt,existsSync as Hv,createReadStream as PB}from"node:fs";import{join as Kl}from"node:path";import{createInterface as MB}from"node:readline";var OB=36e5,$B=30*864e5;function DB(e){try{return process.kill(e,0),!0}catch(t){return t.code==="EPERM"}}function LB(e){let t=e.trim().split(/\n\n+/),n=[];for(let r of t){let o=r.split(`
2133
+ `),s="",i="",a="",l=!1,c=!1,u=!1;for(let d of o)d.startsWith("worktree ")?s=d.slice(9).trim():d.startsWith("HEAD ")?i=d.slice(5).trim():d.startsWith("branch ")?a=d.slice(7).trim():d.trim()==="locked"?l=!0:d.trim()==="prunable"?c=!0:d.trim()==="bare"&&(u=!0);s&&n.push({path:s,head:i,branch:a,locked:l,prunable:c,isBare:u})}return n}function FB(e,t,n){if(e.locked)return"locked";let r=864e5,o=t*r,s=n*r;return e.ownerLiveness==="dead"&&!e.isDirty&&e.commitsAhead===0?"dead-owner":e.commitsAhead===0&&!e.isDirty&&e.ageMs>=OB?"empty":e.isDirty&&e.ageMs>s?"stale-dirty":!e.isDirty&&e.ageMs>o?"stale-clean":"active"}async function NB(e){if(!Hv(e))return 0;let t=0;try{let n=MB({input:PB(e),crlfDelay:1/0});for await(let r of n){let o=r.trim();if(o)try{let s=JSON.parse(o);s.taskId==="worktree-prune"&&(s.status==="success"||s.status==="error")&&t++}catch{}}}catch{}return t}var Gl=class extends Error{constructor(t){super(`Worktree sweep lock contested: ${t} \u2014 another sweep may be running.`),this.name="LockContestedError"}};async function BB(e){let t=Kl(e,"..");await Pt.mkdir(t,{recursive:!0}).catch(()=>{});let r=await(async()=>{try{return await Pt.open(e,"wx")}catch(o){if(o.code!=="EEXIST")throw o;let i=null;try{let a=await Pt.readFile(e,"utf-8");i=parseInt(a.trim(),10)}catch{}if(i!==null&&!Number.isNaN(i)){let a=!1;try{process.kill(i,0),a=!0}catch{}if(!a)return await Pt.unlink(e).catch(()=>{}),await Pt.open(e,"wx")}throw new Gl(e)}})();return await r.writeFile(String(process.pid),"utf-8"),await r.close(),async()=>{await Pt.unlink(e).catch(()=>{})}}async function zt(e){let{execFile:t,repoRoot:n,maxAgeDaysClean:r=14,maxAgeDaysDirty:o=30,scope:s="all",telemetryPath:i}=e,a=i??$t(),l=e.lockPath??Nf(),c={removed:[],warnings:[],dryRun:e.dryRun??!1,candidates:[]},u=e.bypassSoftLaunch?Number.POSITIVE_INFINITY:await NB(a),d=e.dryRun===!0||u<3;c.dryRun=d;let p=null;try{p=await BB(l)}catch(f){if(f instanceof Gl)return c.warnings.push(`[WARN] ${f.message}`),c;throw f}try{let f=await t("git",["-C",n,"worktree","list","--porcelain"]),g=LB(f.stdout),h=Kl(n,".afk-worktrees"),b=new Set(g.map(v=>v.path)),y=[];try{y=(await Pt.readdir(h,{withFileTypes:!0})).filter(E=>E.isDirectory()).map(E=>Kl(h,E.name))}catch{}let w=y.filter(v=>!b.has(v));if(s==="all"||s==="interactive")for(let v of w){let E=0;try{let I=await Pt.stat(v);E=Date.now()-I.birthtimeMs}catch{}if(c.candidates.push({path:v,verdict:"orphaned-dir",owner:"interactive",ageMs:E}),!d)try{await Pt.rm(v,{recursive:!0,force:!0}),c.removed.push(v)}catch(I){c.warnings.push(`[ERROR] Failed to remove orphaned dir ${v}: ${I instanceof Error?I.message:String(I)}`)}}let x=!1,R=g[0]?.path;for(let v of g){if(v.path===R||v.isBare||!v.path.startsWith(h))continue;let E;try{let A=await Pt.readFile(Kl(v.path,".afk-worktree-meta.json"),"utf-8");E=JSON.parse(A)}catch{}if(s!=="all"&&E?.owner!==s)continue;let I=E?.owner==="interactive"||E?.owner==="diagnose"?E.owner:"unknown";if(!Hv(v.path)){c.candidates.push({path:v.path,verdict:"orphaned-registration",owner:I,ageMs:0}),d||(x=!0);continue}let D=0,_=E?.createdAt;if(_)D=Date.now()-new Date(_).getTime();else try{let A=await Pt.stat(v.path);D=Date.now()-A.birthtimeMs}catch{}let M=!1,P=0;try{M=(await t("git",["-C",v.path,"status","--porcelain"])).stdout.trim().length>0}catch{M=!0}if(!M&&v.head){let A=E?.baseSha??v.head;try{let N=await t("git",["-C",n,"rev-list",`${A}..${v.head}`,"--count"]);P=parseInt(N.stdout.trim(),10)||0}catch{P=0}}let $="unknown";typeof E?.pid=="number"&&Number.isInteger(E.pid)&&E.pid>0&&D<=$B&&($=DB(E.pid)?"alive":"dead");let F={path:v.path,head:v.head,branch:v.branch,locked:v.locked,prunable:v.prunable,meta:E,ageMs:D,isDirty:M,commitsAhead:P,ownerLiveness:$},k=FB(F,r,o);if(c.candidates.push({path:v.path,verdict:k,owner:I,ageMs:D}),!d)try{k==="empty"?(await t("git",["-C",n,"worktree","remove","--force",v.path]),v.branch&&await t("git",["-C",n,"branch","-d",v.branch]).catch(()=>{}),c.removed.push(v.path)):k==="dead-owner"?(await t("git",["-C",n,"worktree","remove","--force",v.path]),v.branch&&await t("git",["-C",n,"branch","-d",v.branch]).catch(()=>{}),c.removed.push(v.path)):k==="stale-clean"?(await t("git",["-C",n,"worktree","remove","--force",v.path]),c.removed.push(v.path)):k==="stale-dirty"&&c.warnings.push(`[WARN] stale-dirty worktree preserved (uncommitted changes): ${v.path}`)}catch(A){c.warnings.push(`[ERROR] Failed to process ${v.path} (${k}): ${A instanceof Error?A.message:String(A)}`)}}if(x&&!d)try{await t("git",["-C",n,"worktree","prune"])}catch(v){c.warnings.push(`[ERROR] git worktree prune failed: ${v instanceof Error?v.message:String(v)}`)}}finally{p&&await p()}return c}var Zp=qB(UB),Kv=["interactive","diagnose","all"],ql=new Set(["empty","stale-clean","orphaned-dir","orphaned-registration","dead-owner"]);async function Gv(){let t=(await Zp("git",["rev-parse","--git-common-dir"])).stdout.trim();if(!t)throw new Error("Not in a git repository.");let n=HB(t)?t:GB(process.cwd(),t);return WB(n)}function zB(e){if(e<=0)return"-";let t=e/864e5;return t<1?`${Math.max(1,Math.round(e/36e5))}h`:`${Math.round(t)}d`}function JB(e,t){return ql.has(e)?m.error(t):e==="stale-dirty"?m.warning(t):e==="locked"?m.dim(t):m.dim(t)}function qv(e,t){let n=e.split(/\s+/).map(o=>o.trim()).filter(o=>o.length>0),r={scope:t,apply:!1,unknown:[]};for(let o=0;o<n.length;o++){let s=n[o];if(s==="--apply")r.apply=!0;else if(s==="--scope"&&o+1<n.length){let i=n[o+1];o++,Kv.includes(i)?r.scope=i:r.unknown.push(`--scope=${i}`)}else if(s.startsWith("--scope=")){let i=s.slice(8);Kv.includes(i)?r.scope=i:r.unknown.push(s)}else r.unknown.push(s)}return r}async function VB(e){try{let t=await jB.readFile(KB(e,".afk-worktree-meta.json"),"utf-8"),n=JSON.parse(t);if(typeof n.pid=="number"&&Number.isInteger(n.pid)&&n.pid>0)return n.pid}catch{}}async function YB(e,t,n){if(t.length===0){e.info("No afk-managed worktrees found.");return}let r=process.pid;e.line(),e.line(m.bold("Worktrees")),e.line(m.dim(" "+"PATH".padEnd(45)+" "+"OWNER".padEnd(12)+" "+"AGE".padEnd(5)+" "+"VERDICT".padEnd(22)+" PRUNE?"));for(let o of t){let a=n.get(o.path)===r?m.brand("\u2192 "):" ",l=o.path.slice(-44).padEnd(45),c=o.owner.padEnd(12),u=zB(o.ageMs).padEnd(5),d=o.verdict.padEnd(22),p=ql.has(o.verdict)?m.error("yes"):o.verdict==="stale-dirty"?m.warning("warn"):m.dim("no"),f=JB(o.verdict,d);e.line(`${a}${l} ${c} ${u} ${f} ${p}`)}e.line(),e.line(m.dim(" \u2192 this session")),e.line()}async function XB(e){let t=new Map;for(let n of e)t.set(n.path,await VB(n.path));return t}async function ZB(e,t){let n=qv(t,"interactive");n.unknown.length>0&&e.out.warn(`Unknown args ignored: ${n.unknown.join(" ")}`);let r;try{r=await Gv()}catch(i){return e.out.error(`Not in a git repository: ${i.message}`),"continue"}let o;try{let i={execFile:Zp,repoRoot:r,dryRun:!0,scope:n.scope};o=await zt(i)}catch(i){return e.out.error(`Sweep failed: ${i.message}`),"continue"}let s=await XB(o.candidates);await YB(e.out,o.candidates,s);for(let i of o.warnings)e.out.warn(i);return"continue"}async function QB(e,t){let n=qv(t,"interactive");n.unknown.length>0&&e.out.warn(`Unknown args ignored: ${n.unknown.join(" ")}`);let r;try{r=await Gv()}catch(a){return e.out.error(`Not in a git repository: ${a.message}`),"continue"}let o;try{let a={execFile:Zp,repoRoot:r,dryRun:!n.apply,scope:n.scope};o=await zt(a)}catch(a){return e.out.error(`Sweep failed: ${a.message}`),"continue"}let s={};for(let a of o.candidates)s[a.verdict]=(s[a.verdict]??0)+1;let i=Object.entries(s).sort(([a],[l])=>a.localeCompare(l)).map(([a,l])=>`${a}=${l}`);if(o.dryRun){let a=o.candidates.filter(l=>ql.has(l.verdict)).length;e.out.line(),e.out.line(m.warning("\u{1F50D} Dry-run \u2014 pass --apply to actually remove.")+` Would prune ${a} worktree(s).`+(i.length>0?` [${i.join(" ")}]`:""))}else{let a=o.warnings.filter(c=>c.startsWith("[WARN]")).length,l=o.warnings.filter(c=>c.startsWith("[ERROR]")).length;e.out.line(),e.out.success(`Removed ${o.removed.length}, warned ${a}, errors ${l}`+(i.length>0?` [${i.join(" ")}]`:""))}for(let a of o.candidates){let c=o.removed.includes(a.path)?m.error("\u2717"):ql.has(a.verdict)?m.warning("\u2022"):m.dim("\xB7");e.out.line(` ${c} [${a.verdict.padEnd(22)}] ${a.path}`)}for(let a of o.warnings)a.startsWith("[ERROR]")?e.out.error(a):e.out.warn(a);return e.out.line(),"continue"}var zv={name:"/worktree",summary:"List or prune afk-managed git worktrees",usage:"/worktree list | /worktree prune [--apply] [--scope <interactive|diagnose|all>]",async handler(e,t){let n=t.trim();return n.length===0||n.startsWith("list")?ZB(e,n.replace(/^list\s*/,"")):n.startsWith("prune")?QB(e,n.replace(/^prune\s*/,"")):(e.out.error(`Unknown /worktree subcommand. Usage:
2134
2134
  /worktree list
2135
- /worktree prune [--apply] [--scope <interactive|diagnose|all>]`),"continue")}};var Jv={name:"/reauth",summary:"Re-read keychain credentials and swap the running session's client",usage:"/reauth [--check]",hint:"Force the running session to pick up a new keychain token (e.g. after `claude /login` in another terminal)",async handler(e,t){if(t.trim()==="--check"){let s=ze();if(!s)return e.out.warn("No OAuth token found. Run `claude login` in a terminal to authenticate."),"continue";let i=An(s);return e.out.success(`Active keychain account: ${i}`),e.out.info("Note: --check only inspects the keychain. The running SDK client may still hold an older token \u2014 run `/reauth` (no args) to actually swap."),"continue"}let r=ze();if(r){let s=An(r);e.out.info(`Current keychain account: ${s}`)}e.out.info("Rebuilding session client from keychain credentials\u2026");let o;try{o=await e.session.current.reauth()}catch(s){return e.out.error(`Client refresh failed: ${s instanceof Error?s.message:String(s)}`),e.out.warn("Run `claude login` in a terminal to re-authenticate."),"continue"}return o?(o.swapped?(e.out.success(`\u2713 Client swapped. Session now authenticated as: ${o.accountId}`),e.out.info("Next turn will use the new credential. If a usage-limit pause is active, it will resume automatically within ~30s (or send a message to retry immediately).")):e.out.success(`\u2713 Client refreshed. Authenticated as: ${o.accountId} (token unchanged)`),"continue"):(ze()?e.out.warn("This session is not using OAuth (probably api-key mode) \u2014 nothing to refresh. The active credential is whatever ANTHROPIC_API_KEY held at session start."):e.out.warn("No OAuth credentials found in the keychain. Run `claude login` in a terminal to authenticate."),"continue")}};G();import{spawn as eU}from"node:child_process";import{promises as Qp}from"node:fs";import*as Vv from"node:os";import*as Yv from"node:path";function tU(e){return new Date(e).toLocaleTimeString(void 0,{hour:"2-digit",minute:"2-digit",second:"2-digit"})}function nU(e,t){let n=[p.meta(`#${t+1}`),p.dim(tU(e.timestamp))];if(e.durationMs!==void 0&&e.durationMs>0&&n.push(p.dim(ne(e.durationMs))),e.costUsd!==void 0&&e.costUsd>0&&n.push(p.dim(Ne(e.costUsd))),e.inputTokens!==void 0||e.outputTokens!==void 0){let r=e.inputTokens??0,o=e.outputTokens??0;n.push(p.dim(`${Math.round((r+o)/1e3)}k tok`))}return n.join(" ")}function rU(e){if(!e.toolEvents||e.toolEvents.length===0)return"";let t=[];for(let n of e.toolEvents){let r=n.isError?p.error("\u2717"):p.dim("\u25CF"),o=p.chrome(n.toolName),s="";if(n.result){let i=n.result.trim().split(`
2136
- `)[0]??"";s=i.length>80?i.slice(0,77)+"...":i}t.push(` ${r} ${o}${s?p.dim(` ${s}`):""}`)}return t.join(`
2135
+ /worktree prune [--apply] [--scope <interactive|diagnose|all>]`),"continue")}};var Jv={name:"/reauth",summary:"Re-read keychain credentials and swap the running session's client",usage:"/reauth [--check]",hint:"Force the running session to pick up a new keychain token (e.g. after `claude /login` in another terminal)",async handler(e,t){if(t.trim()==="--check"){let s=ze();if(!s)return e.out.warn("No OAuth token found. Run `claude login` in a terminal to authenticate."),"continue";let i=An(s);return e.out.success(`Active keychain account: ${i}`),e.out.info("Note: --check only inspects the keychain. The running SDK client may still hold an older token \u2014 run `/reauth` (no args) to actually swap."),"continue"}let r=ze();if(r){let s=An(r);e.out.info(`Current keychain account: ${s}`)}e.out.info("Rebuilding session client from keychain credentials\u2026");let o;try{o=await e.session.current.reauth()}catch(s){return e.out.error(`Client refresh failed: ${s instanceof Error?s.message:String(s)}`),e.out.warn("Run `claude login` in a terminal to re-authenticate."),"continue"}return o?(o.swapped?(e.out.success(`\u2713 Client swapped. Session now authenticated as: ${o.accountId}`),e.out.info("Next turn will use the new credential. If a usage-limit pause is active, it will resume automatically within ~30s (or send a message to retry immediately).")):e.out.success(`\u2713 Client refreshed. Authenticated as: ${o.accountId} (token unchanged)`),"continue"):(ze()?e.out.warn("This session is not using OAuth (probably api-key mode) \u2014 nothing to refresh. The active credential is whatever ANTHROPIC_API_KEY held at session start."):e.out.warn("No OAuth credentials found in the keychain. Run `claude login` in a terminal to authenticate."),"continue")}};G();import{spawn as eU}from"node:child_process";import{promises as Qp}from"node:fs";import*as Vv from"node:os";import*as Yv from"node:path";function tU(e){return new Date(e).toLocaleTimeString(void 0,{hour:"2-digit",minute:"2-digit",second:"2-digit"})}function nU(e,t){let n=[m.meta(`#${t+1}`),m.dim(tU(e.timestamp))];if(e.durationMs!==void 0&&e.durationMs>0&&n.push(m.dim(ne(e.durationMs))),e.costUsd!==void 0&&e.costUsd>0&&n.push(m.dim(Ne(e.costUsd))),e.inputTokens!==void 0||e.outputTokens!==void 0){let r=e.inputTokens??0,o=e.outputTokens??0;n.push(m.dim(`${Math.round((r+o)/1e3)}k tok`))}return n.join(" ")}function rU(e){if(!e.toolEvents||e.toolEvents.length===0)return"";let t=[];for(let n of e.toolEvents){let r=n.isError?m.error("\u2717"):m.dim("\u25CF"),o=m.chrome(n.toolName),s="";if(n.result){let i=n.result.trim().split(`
2136
+ `)[0]??"";s=i.length>80?i.slice(0,77)+"...":i}t.push(` ${r} ${o}${s?m.dim(` ${s}`):""}`)}return t.join(`
2137
2137
  `)+`
2138
- `}function oU(e,t,n,r){let o=[];o.push(he("Session Transcript"));let s=[p.dim(`Started ${new Date(n).toLocaleString()}`),p.dim(`model: ${t}`),p.dim(`${e.length} turn${e.length===1?"":"s"}`)];r>0&&s.push(p.dim(Ne(r))),o.push(s.join(" \xB7 ")),o.push("");for(let i=0;i<e.length;i++){let a=e[i];o.push(nU(a,i)),o.push(""),o.push(` ${p.user("\u25B6")} ${p.user("User")}`),o.push("");let l=a.user.split(`
2139
- `).map(m=>` ${m}`);o.push(l.join(`
2140
- `)),o.push("");let c=rU(a);c&&(o.push(` ${p.chrome("Tools")}`),o.push(c)),o.push(` ${p.brand("\u25C6")} ${p.brand("Assistant")}`),o.push("");let d=xt(a.assistant).split(`
2141
- `).map(m=>` ${m}`);o.push(d.join(`
2138
+ `}function oU(e,t,n,r){let o=[];o.push(he("Session Transcript"));let s=[m.dim(`Started ${new Date(n).toLocaleString()}`),m.dim(`model: ${t}`),m.dim(`${e.length} turn${e.length===1?"":"s"}`)];r>0&&s.push(m.dim(Ne(r))),o.push(s.join(" \xB7 ")),o.push("");for(let i=0;i<e.length;i++){let a=e[i];o.push(nU(a,i)),o.push(""),o.push(` ${m.user("\u25B6")} ${m.user("User")}`),o.push("");let l=a.user.split(`
2139
+ `).map(p=>` ${p}`);o.push(l.join(`
2140
+ `)),o.push("");let c=rU(a);c&&(o.push(` ${m.chrome("Tools")}`),o.push(c)),o.push(` ${m.brand("\u25C6")} ${m.brand("Assistant")}`),o.push("");let d=xt(a.assistant).split(`
2141
+ `).map(p=>` ${p}`);o.push(d.join(`
2142
2142
  `)),o.push(""),i<e.length-1&&(o.push(he()),o.push(""))}return o.push(he()),o.join(`
2143
2143
  `)+`
2144
- `}function sU(){let e=T.PAGER;if(e){let t=e.split(/\s+/);return{cmd:t[0],args:t.slice(1)}}return{cmd:"less",args:["-R"]}}var Xv={name:"/transcript",aliases:["/t"],summary:"View full session transcript in $PAGER",hint:"When you want to read the full conversation \u2014 all turns, tool calls, costs \u2014 in a scrollable pager.",async handler(e){let{stats:t,out:n}=e;if(t.turns.length===0)return n.info("No turns yet in this session."),"continue";let r=oU(t.turns,String(t.model),t.sessionStartTime,t.totalCostUsd);if(!process.stdout.isTTY)return n.raw(r),"continue";let o=sU();if(!o)return n.raw(r),"continue";let s=Yv.join(Vv.tmpdir(),`afk-transcript-${Date.now()}.txt`);return await Qp.writeFile(s,r,{mode:384}),new Promise(i=>{let a=eU(o.cmd,[...o.args,s],{stdio:"inherit"});a.on("error",()=>{n.raw(r),Qp.unlink(s).catch(()=>{}),i("continue")}),a.on("exit",()=>{Qp.unlink(s).catch(()=>{}),i("continue")})})}};import{readdirSync as OU,readFileSync as $U,statSync as uT}from"fs";import{join as DU}from"path";W();import{existsSync as iU,readFileSync as aU}from"fs";import{basename as lU,join as cU}from"path";function uU(e){let t=cU(e,".claude-plugin","plugin.json");if(!iU(t))return{name:null,version:null};try{let n=JSON.parse(aU(t,"utf8"));return{name:typeof n.name=="string"&&n.name.trim()?n.name.trim():null,version:typeof n.version=="string"&&n.version.trim()?n.version.trim():null}}catch{return{name:null,version:null}}}function dU(e,t,n){let r=new Set([e]);t&&r.add(t);for(let[o,s]of Object.entries(n)){if(r.has(o))return s;let i=o.includes(":")?o.split(":").pop():o;if(r.has(i)||s.manifestName&&r.has(s.manifestName))return s}return null}function Zv(e={}){let t=e.roots??[os(),Oe(),is()],n=fe(e.indexPath??ie()),r=[],o=new Set;for(let s of t)for(let i of At(s)){if(i.type!=="local")continue;let a=i.path,l=lU(a),{name:c,version:u}=uU(a),d=c??l;if(o.has(d))continue;o.add(d);let m=dU(l,c,n.plugins);r.push({name:d,version:u,ref:m?.ref??null,commit:m?.commit??null,source:m?.source??null,sourceType:m?.sourceType??null,dir:a})}return r.sort((s,i)=>s.name.localeCompare(i.name)),r}function Qv(e){let t;return e.version?t=/^v/i.test(e.version)?e.version:`v${e.version}`:e.ref?t=e.ref:t="(local)",e.commit&&e.commit!==e.ref&&(t+=` @ ${e.commit.slice(0,7)}`),t}W();var em=[],pU={name:"/agents",summary:"List plugin-provided subagents (loads after session init)",async handler(e){return e.out.line(),e.out.line(p.dim(" Plugin agents are still loading \u2014 try again after the session is ready.")),e.out.line(),"continue"}};function mU(e){return{name:"/agents",summary:"List Task-tool subagents loaded by the SDK (plugin + user + project)",async handler(t){if(t.out.line(),e.length===0)return t.out.line(p.dim(" No plugin agents loaded. Agents come from `agents:` entries in plugin.json or from ~/.afk/agents/.")),t.out.line(),"continue";t.out.line(p.bold("Plugin agents")+p.dim(` (${e.length} loaded)`)),t.out.line(p.dim("\u2500".repeat(60)));let n=e.reduce((r,o)=>Math.max(r,o.name.length),0)+2;for(let r of e){let o=p.warning(r.name.padEnd(n)),s=r.model?p.dim(`[${r.model}]`):"",i=r.description?p.dim(` ${r.description}`):"";t.out.line(` ${o} ${s}${i}`)}return t.out.line(),t.out.line(p.dim(" Agents are dispatched by the model via the Task tool \u2014 not user-invoked.")),t.out.line(),"continue"}}}async function tm(e){let t;try{t=await e.supportedAgents()}catch(n){return console.error(p.dim(" \u26A0 Plugin-agent discovery failed: ")+(n instanceof Error?n.message:String(n))),null}return em=t.map(n=>{let r={name:n.name,description:n.description};return n.model&&(r.model=n.model),r}),Bn(mU(em)),em.length}function eT(){ce(pU)}function zl(e,t){for(let n of t??[])e.push({type:"image",source:{type:"base64",media_type:n.mediaType,data:n.bytes.toString("base64")}})}function tT(e,t,n,r){let o=Tl(e.name,t),s=e.context==="fork"?" This skill runs with context: 'fork' \u2014 the executor will fork a subagent.":"",i=`Use the \`skill\` tool with {"name": "${e.name}", "arguments": "${t}"} to dispatch this skill.${s}`,a=[];return n&&n.trim().length>0&&a.push({type:"text",text:n}),a.push({type:"text",text:o}),a.push({type:"text",text:i}),zl(a,r),a}async function Jl(e,t){let n=Wl(e,{skillName:t.skillName,onCancel:()=>{e.session.current.interrupt().catch(()=>{})}}),r=!1;try{await n.arm(),e.setSoftStopHandler?.(()=>{r=!0});let o;if(t.preflight)try{o=await t.preflight()}catch{o=void 0}let s=tT(t.skillMeta,t.args,o,t.attachments);await qr(n.sink,async()=>{for await(let i of e.session.current.sendMessageStream(s)){if(r){e.session.current.interrupt().catch(()=>{});break}n.sink(i)}})}finally{e.setSoftStopHandler?.(null),await n.dispose()}}var Vl=new Map,nT=new Set;function rT(){for(let e of Vl.keys())nT.add(e)}function nm(e,t,n={}){if(!n.force&&nT.has(e)){process.stderr.write(`[afk preflight] \u26A0 Rejected attempt to overwrite first-party preflight "${e}". Use { force: true } in tests if this is intentional.
2144
+ `}function sU(){let e=T.PAGER;if(e){let t=e.split(/\s+/);return{cmd:t[0],args:t.slice(1)}}return{cmd:"less",args:["-R"]}}var Xv={name:"/transcript",aliases:["/t"],summary:"View full session transcript in $PAGER",hint:"When you want to read the full conversation \u2014 all turns, tool calls, costs \u2014 in a scrollable pager.",async handler(e){let{stats:t,out:n}=e;if(t.turns.length===0)return n.info("No turns yet in this session."),"continue";let r=oU(t.turns,String(t.model),t.sessionStartTime,t.totalCostUsd);if(!process.stdout.isTTY)return n.raw(r),"continue";let o=sU();if(!o)return n.raw(r),"continue";let s=Yv.join(Vv.tmpdir(),`afk-transcript-${Date.now()}.txt`);return await Qp.writeFile(s,r,{mode:384}),new Promise(i=>{let a=eU(o.cmd,[...o.args,s],{stdio:"inherit"});a.on("error",()=>{n.raw(r),Qp.unlink(s).catch(()=>{}),i("continue")}),a.on("exit",()=>{Qp.unlink(s).catch(()=>{}),i("continue")})})}};import{readdirSync as OU,readFileSync as $U,statSync as uT}from"fs";import{join as DU}from"path";W();import{existsSync as iU,readFileSync as aU}from"fs";import{basename as lU,join as cU}from"path";function uU(e){let t=cU(e,".claude-plugin","plugin.json");if(!iU(t))return{name:null,version:null};try{let n=JSON.parse(aU(t,"utf8"));return{name:typeof n.name=="string"&&n.name.trim()?n.name.trim():null,version:typeof n.version=="string"&&n.version.trim()?n.version.trim():null}}catch{return{name:null,version:null}}}function dU(e,t,n){let r=new Set([e]);t&&r.add(t);for(let[o,s]of Object.entries(n)){if(r.has(o))return s;let i=o.includes(":")?o.split(":").pop():o;if(r.has(i)||s.manifestName&&r.has(s.manifestName))return s}return null}function Zv(e={}){let t=e.roots??[os(),Oe(),is()],n=fe(e.indexPath??ie()),r=[],o=new Set;for(let s of t)for(let i of At(s)){if(i.type!=="local")continue;let a=i.path,l=lU(a),{name:c,version:u}=uU(a),d=c??l;if(o.has(d))continue;o.add(d);let p=dU(l,c,n.plugins);r.push({name:d,version:u,ref:p?.ref??null,commit:p?.commit??null,source:p?.source??null,sourceType:p?.sourceType??null,dir:a})}return r.sort((s,i)=>s.name.localeCompare(i.name)),r}function Qv(e){let t;return e.version?t=/^v/i.test(e.version)?e.version:`v${e.version}`:e.ref?t=e.ref:t="(local)",e.commit&&e.commit!==e.ref&&(t+=` @ ${e.commit.slice(0,7)}`),t}W();var em=[],pU={name:"/agents",summary:"List plugin-provided subagents (loads after session init)",async handler(e){return e.out.line(),e.out.line(m.dim(" Plugin agents are still loading \u2014 try again after the session is ready.")),e.out.line(),"continue"}};function mU(e){return{name:"/agents",summary:"List Task-tool subagents loaded by the SDK (plugin + user + project)",async handler(t){if(t.out.line(),e.length===0)return t.out.line(m.dim(" No plugin agents loaded. Agents come from `agents:` entries in plugin.json or from ~/.afk/agents/.")),t.out.line(),"continue";t.out.line(m.bold("Plugin agents")+m.dim(` (${e.length} loaded)`)),t.out.line(m.dim("\u2500".repeat(60)));let n=e.reduce((r,o)=>Math.max(r,o.name.length),0)+2;for(let r of e){let o=m.warning(r.name.padEnd(n)),s=r.model?m.dim(`[${r.model}]`):"",i=r.description?m.dim(` ${r.description}`):"";t.out.line(` ${o} ${s}${i}`)}return t.out.line(),t.out.line(m.dim(" Agents are dispatched by the model via the Task tool \u2014 not user-invoked.")),t.out.line(),"continue"}}}async function tm(e){let t;try{t=await e.supportedAgents()}catch(n){return console.error(m.dim(" \u26A0 Plugin-agent discovery failed: ")+(n instanceof Error?n.message:String(n))),null}return em=t.map(n=>{let r={name:n.name,description:n.description};return n.model&&(r.model=n.model),r}),Bn(mU(em)),em.length}function eT(){ce(pU)}function zl(e,t){for(let n of t??[])e.push({type:"image",source:{type:"base64",media_type:n.mediaType,data:n.bytes.toString("base64")}})}function tT(e,t,n,r){let o=Tl(e.name,t),s=e.context==="fork"?" This skill runs with context: 'fork' \u2014 the executor will fork a subagent.":"",i=`Use the \`skill\` tool with {"name": "${e.name}", "arguments": "${t}"} to dispatch this skill.${s}`,a=[];return n&&n.trim().length>0&&a.push({type:"text",text:n}),a.push({type:"text",text:o}),a.push({type:"text",text:i}),zl(a,r),a}async function Jl(e,t){let n=Wl(e,{skillName:t.skillName,onCancel:()=>{e.session.current.interrupt().catch(()=>{})}}),r=!1;try{await n.arm(),e.setSoftStopHandler?.(()=>{r=!0});let o;if(t.preflight)try{o=await t.preflight()}catch{o=void 0}let s=tT(t.skillMeta,t.args,o,t.attachments);await qr(n.sink,async()=>{for await(let i of e.session.current.sendMessageStream(s)){if(r){e.session.current.interrupt().catch(()=>{});break}n.sink(i)}})}finally{e.setSoftStopHandler?.(null),await n.dispose()}}var Vl=new Map,nT=new Set;function rT(){for(let e of Vl.keys())nT.add(e)}function nm(e,t,n={}){if(!n.force&&nT.has(e)){process.stderr.write(`[afk preflight] \u26A0 Rejected attempt to overwrite first-party preflight "${e}". Use { force: true } in tests if this is intentional.
2145
2145
  `);return}Vl.set(e,t)}function rm(e){return Vl.get(e)}async function vr(e,t,n){let r=Vl.get(e.skillName);if(!r)return null;try{return await r(e,t)}catch(o){return n&&n(o),null}}G();import{execFile as fU}from"child_process";import{promisify as gU}from"util";import{writeFileSync as hU}from"fs";import{join as yU}from"path";var bU=gU(fU),wU=4*1024*1024,om=!1;function SU(e){let t=e.trim();if(!t)return null;if(/^#?\d+$/.test(t)){let r=t.replace(/^#/,""),o=parseInt(r,10);if(!(o>0&&o<1e6))throw new Error(`[afk preflight] Invalid PR number: ${r}. Must be 1\u2013999999.`);return r}let n=t.match(/github\.com\/[^/]+\/[^/]+\/pull\/(\d+)/);if(n&&n[1]){let r=n[1],o=parseInt(r,10);if(!(o>0&&o<1e6))throw new Error(`[afk preflight] Invalid PR number in URL: ${r}. Must be 1\u2013999999.`);return r}return null}async function kU(e,t,n){try{let{stdout:r}=await bU(e,t,{encoding:"utf-8",...n?.cwd?{cwd:n.cwd}:{},maxBuffer:n?.maxBuffer??wU,timeout:8e3});return r}catch(r){if(T.AFK_DEBUG==="1"&&r instanceof Error&&"stderr"in r){let s=String(r.stderr??"").replace(/\x1b\[[0-9;]*m/g,"").slice(0,200).trim();s&&process.stderr.write(`[afk preflight] ${e} stderr: ${s}
2146
2146
  `)}return null}}async function vU(e,t,n={}){let r=n.exec??kU,o=n.writeFile??((f,g)=>hU(f,g,"utf-8")),[s,i,a]=await Promise.all([r("gh",["pr","view",e,"--json","title,baseRefName,headRefName,additions,deletions,changedFiles,files"],{cwd:t.cwd}),r("gh",["pr","diff",e],{cwd:t.cwd}),r("git",["status","--porcelain"],{cwd:t.cwd})]),l=null;if(s)try{l=JSON.parse(s)}catch{l=null}let c=null,u=null;if(i!==null){let f=yU(t.artifactDir,`pr-${e}.diff`);try{o(f,i),c=f,u=i.trimEnd().split(`
2147
2147
  `).length}catch{}}let d=a?a.split(`
2148
- `).filter(f=>f.trim().length>0).length:0,m=d>0;return{pr:e,metadata:l,diffPath:c,diffLineCount:u,dirty:m,dirtyFiles:d}}function Yl(e){return e.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/[\r\n]/g,"")}function TU(e){let t=[];if(t.push(`<preflight-context skill="review" pr="${e.pr}">`),e.metadata){let n=e.metadata;n.title&&t.push(`Title: ${Yl(n.title)}`),n.baseRefName&&n.headRefName&&t.push(`Branch: ${Yl(n.headRefName)} \u2192 ${Yl(n.baseRefName)}`);let r=[];if(n.additions!==void 0&&r.push(`+${n.additions}`),n.deletions!==void 0&&r.push(`-${n.deletions}`),n.changedFiles!==void 0&&r.push(`${n.changedFiles} file${n.changedFiles===1?"":"s"}`),r.length>0&&t.push(`Stats: ${r.join(" / ")}`),n.files&&n.files.length>0){let s=n.files.filter(i=>!!i.path);t.push("Files changed:");for(let i of s.slice(0,40)){let a=i.additions!==void 0?`+${i.additions}`:"",l=i.deletions!==void 0?`-${i.deletions}`:"",c=a||l?` (${[a,l].filter(Boolean).join("/")})`:"";t.push(` - ${Yl(i.path)}${c}`)}s.length>40&&t.push(` \u2026and ${s.length-40} more (see diff artifact)`)}}else t.push("PR metadata: UNAVAILABLE (gh pr view failed \u2014 check `gh auth status`)");return e.diffPath&&e.diffLineCount!==null?t.push(`Diff artifact: ${e.diffPath} (${e.diffLineCount} lines)`):t.push("Diff artifact: UNAVAILABLE (gh pr diff failed)"),e.dirty?(t.push(`Working tree: DIRTY (${e.dirtyFiles} uncommitted change${e.dirtyFiles===1?"":"s"})`),t.push(" \u2192 DO NOT stash, commit, reset, or otherwise mutate the working tree."),t.push(" \u2192 Review is read-only. The user is mid-work; preserve their state.")):(t.push("Working tree: clean"),t.push(" \u2192 Review is read-only. Do not stash, commit, or modify files.")),t.push("Capabilities: compose available, subagents available."),t.push("Use the diff artifact path above instead of re-running `gh pr diff`."),t.push("</preflight-context>"),t.join(`
2148
+ `).filter(f=>f.trim().length>0).length:0,p=d>0;return{pr:e,metadata:l,diffPath:c,diffLineCount:u,dirty:p,dirtyFiles:d}}function Yl(e){return e.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/[\r\n]/g,"")}function TU(e){let t=[];if(t.push(`<preflight-context skill="review" pr="${e.pr}">`),e.metadata){let n=e.metadata;n.title&&t.push(`Title: ${Yl(n.title)}`),n.baseRefName&&n.headRefName&&t.push(`Branch: ${Yl(n.headRefName)} \u2192 ${Yl(n.baseRefName)}`);let r=[];if(n.additions!==void 0&&r.push(`+${n.additions}`),n.deletions!==void 0&&r.push(`-${n.deletions}`),n.changedFiles!==void 0&&r.push(`${n.changedFiles} file${n.changedFiles===1?"":"s"}`),r.length>0&&t.push(`Stats: ${r.join(" / ")}`),n.files&&n.files.length>0){let s=n.files.filter(i=>!!i.path);t.push("Files changed:");for(let i of s.slice(0,40)){let a=i.additions!==void 0?`+${i.additions}`:"",l=i.deletions!==void 0?`-${i.deletions}`:"",c=a||l?` (${[a,l].filter(Boolean).join("/")})`:"";t.push(` - ${Yl(i.path)}${c}`)}s.length>40&&t.push(` \u2026and ${s.length-40} more (see diff artifact)`)}}else t.push("PR metadata: UNAVAILABLE (gh pr view failed \u2014 check `gh auth status`)");return e.diffPath&&e.diffLineCount!==null?t.push(`Diff artifact: ${e.diffPath} (${e.diffLineCount} lines)`):t.push("Diff artifact: UNAVAILABLE (gh pr diff failed)"),e.dirty?(t.push(`Working tree: DIRTY (${e.dirtyFiles} uncommitted change${e.dirtyFiles===1?"":"s"})`),t.push(" \u2192 DO NOT stash, commit, reset, or otherwise mutate the working tree."),t.push(" \u2192 Review is read-only. The user is mid-work; preserve their state.")):(t.push("Working tree: clean"),t.push(" \u2192 Review is read-only. Do not stash, commit, or modify files.")),t.push("Capabilities: compose available, subagents available."),t.push("Use the diff artifact path above instead of re-running `gh pr diff`."),t.push("</preflight-context>"),t.join(`
2149
2149
  `)}var oT=async(e,t)=>{if(om)return null;om=!0;try{let n=SU(e.rawArgs);if(!n)return null;let r=await vU(n,t),o=TU(r),s={};return r.diffPath&&(s.diff=r.diffPath),{manifestBlock:o,artifacts:s}}finally{om=!1}};W();import{mkdirSync as xU,readdirSync as EU,rmSync as RU,lstatSync as aT}from"fs";import{join as sm,resolve as sT,sep as AU}from"path";import{randomBytes as _U}from"crypto";var CU=/^[0-9a-f-]{8,128}$/i,IU=10080*60*1e3,PU=300*1e3,iT=0;function MU(e,t){let n;try{n=EU(e)}catch{return}let r=Date.now()-t;for(let o of n){let s=sm(e,o);try{let i=aT(s);i.isDirectory()&&i.mtimeMs<r&&RU(s,{recursive:!0,force:!0})}catch(i){J(`[afk preflight] warn: pruneStaleDirs failed to remove "${s}": `+(i instanceof Error?i.message:String(i)))}}}function Tr(e){let t=e&&e.length>0?e:void 0,n=t!==void 0&&CU.test(t)?t:`unbound-${_U(8).toString("hex")}`,r=sm(ye(),"skill-preflight"),o=sT(r),s=sT(sm(r,n));if(!s.startsWith(o+AU))throw new Error(`[afk preflight] Path traversal detected: resolved dir "${s}" escapes root "${o}".`);let i=Date.now();if(i-iT>=PU){iT=i;let l=r;setImmediate(()=>{MU(l,IU)})}xU(s,{recursive:!0,mode:448});let a=aT(s);if(!a.isDirectory())throw new Error(`[afk preflight] Expected a real directory at "${s}" but got mode ${a.mode.toString(8)}.`);if(typeof process.getuid=="function"&&process.getuid()!==0&&a.uid!==process.getuid())throw new Error(`[afk preflight] Directory "${s}" is owned by uid ${a.uid}, expected ${process.getuid()}.`);return s}function im(e,t){return!e||e.trim().length===0?t:`<system-reminder>
2150
2150
  ${e.replace(/<\/system-reminder>/gi,"")}
2151
2151
  </system-reminder>
2152
2152
 
2153
- ${t}`}var lT=!1;function cT(){lT||(lT=!0,nm("review",oT),rT())}G();var LU=new Set(["/exit","/quit","/clear","/compact","/help"]),Wo={discovered:[],collisions:[],shadowedBareNames:new Set};function Ho(e){return e.includes(":")?e.split(":").pop():e}function mT(e){let t=e??Yt(),n=new Map;try{uT(t)}catch{return n}let r=(o,s)=>{if(s>8)return;let i;try{i=OU(o)}catch{return}for(let a of i){let l=DU(o,a),c;try{c=uT(l)}catch{continue}if(c.isDirectory()){r(l,s+1);continue}if(a!=="SKILL.md"||!c.isFile())continue;let u;try{u=$U(l,"utf-8")}catch{continue}let d=l.split("/"),m=d[d.length-2];if(!m)continue;let f=Ls(u),g=f.frontmatterFlags&&f.frontmatterFlags.length>0?f.frontmatterFlags:Ud(f.body);if(g.length===0)continue;let h=n.get(m)??[],b=new Set([...h,...g]);n.set(m,Array.from(b).sort())}};return r(t,0),n}function FU(e){if(!e)return;let t=e.split(/(?<=[.!?])\s+/);for(let n of t){let r=/^(Use(?:d)? when\b.*|When\s+(?:the\s+user\s+|to\s+)?\b.*)$/i.exec(n.trim());if(r&&r[1]){let o=r[1].trim();if(o.length>=12)return o}}}function dT(e,t){let n=`/${e.name}`,r=e.argumentHint?`${n} ${e.argumentHint}`:void 0,o=FU(e.description);return{name:n,summary:e.description,acceptsAttachments:!0,...r!==void 0?{usage:r}:{},...o?{hint:o}:{},...t&&t.length>0?{flags:t}:{},async handler(s,i,a){let l={name:e.name,description:e.description,handler:async()=>{},context:"inline"};try{await Jl(s,{skillName:e.name,skillMeta:l,args:i,attachments:a,preflight:async()=>{let c=e.name.includes(":")?e.name.split(":").pop()??e.name:e.name,u={skillName:c,rawArgs:i,source:"plugin",capabilities:{compose:!0,subagents:!0}},d=s.session.current.sessionId,m=Tr(d);return(await vr(u,{cwd:s.stats.cwd??process.cwd(),artifactDir:m},g=>{T.AFK_SKILL_STREAM_VERBOSE==="1"&&s.out.warn(`preflight(${c}) failed: ${g instanceof Error?g.message:String(g)}`)}))?.manifestBlock}})}catch(c){s.out.line(),s.out.error(`${e.name} failed: ${c instanceof Error?c.message:String(c)}`)}return"continue"}}}function NU(e,t){let n=new Map,r=s=>{let i=Ho(s.slashName.replace(/^\//,"")),a=n.get(i);a?a.alts.push(s):n.set(i,{main:s,alts:[]})};for(let s of Mg(t)){let i=$e(s),a=`/${s}`,l=i.argumentHint?`${a} ${i.argumentHint}`:a,c={slashName:a,display:l,description:i.description};i.origin==="user"&&(c.sourceLabel="user"),r(c)}let o=new Map(Wo.collisions.map(s=>[s.bare,s.altSlash]));for(let s of e){let i=Ho(s.name),l=o.get(i)??`/${s.name}`,c=s.argumentHint?`${l} ${s.argumentHint}`:l;r({slashName:l,display:c,description:s.description,sourceLabel:"plugin"})}return n}function pT(e,t=80){let n=e.indexOf(". "),r=n>=0?e.slice(0,n+1):e,o=r.length<=t?r:e;return o.length<=t?o:o.slice(0,t-1)+"\u2026"}function BU(e,t,n){let r=t.main,o=p.warning(r.display.padEnd(n)),s=r.sourceLabel?p.dim(`(${r.sourceLabel}) `):"";e.out.line(` ${o} ${s}${p.dim(pT(r.description))}`);for(let i of t.alts){let a=p.warning(i.display.padEnd(Math.max(0,n-4))),l=i.sourceLabel?p.dim(`(${i.sourceLabel} alt) `):p.dim("(alt) ");e.out.line(` ${p.dim("\u2514")} ${a} ${l}${p.dim(pT(i.description))}`)}}function fT(e,t,n){let r=NU(t,n),o=Array.from(r.values()).reduce((a,l)=>a+1+l.alts.length,0);if(e.out.line(),r.size===0){e.out.line(p.dim(" No skills available. Built-in skills should always load \u2014 check your install.")),e.out.line();return}e.out.line(p.bold("Skills")+p.dim(` (${o} loaded)`)),e.out.line(he());let s=Array.from(r.keys()).sort(),i=s.reduce((a,l)=>{let c=r.get(l);return Math.max(a,c.main.display.length)},0)+2;for(let a of s)BU(e,r.get(a),i);e.out.line(),e.out.line(p.dim(" Tip: /skills <name> for full details on a skill.")),e.out.line(p.dim(" Source: vendored (no badge), (user), (plugin). Shadowed entries listed under their winner.")),e.out.line()}function UU(e,t){try{let n=$e(e);return Et(n,t)?n:void 0}catch{return}}function gT(e,t,n,r){let o=t.replace(/^\//,"").trim(),s=UU(o,r),i=n.find(h=>Ho(h.name)===o||h.name===o);if(!s&&!i){e.out.line(),e.out.line(p.dim(` No skill found matching "${o}".`)),e.out.line();return}let a=s?.name??Ho(i.name),l=s?.description??i.description,c=s?.argumentHint??i?.argumentHint,u=c?`/${a} ${c}`:`/${a}`,d=s?s.origin??"builtin":"plugin";e.out.line(),e.out.line(` ${p.warning(u)}`),e.out.line(),e.out.line(` ${l}`),s?.whenToUse&&(e.out.line(),e.out.line(` ${p.bold("When to use:")}`),e.out.line(` ${p.dim(s.whenToUse)}`));let m=s?.flags,f=mT().get(o),g=m??f;g&&g.length>0&&(e.out.line(),e.out.line(` ${p.bold("Flags:")} ${p.dim(g.join(", "))}`)),e.out.line(),e.out.line(` ${p.bold("Source:")} ${p.dim(d)}`),e.out.line()}var jU={name:"/skills",aliases:["/builtin-skills"],summary:"List all skills available in this session \u2014 vendored, user, and plugin",usage:"/skills [name]",hint:"When you want to browse every skill the session can dispatch \u2014 pass a name for full details on one.",async handler(e,t){let n=T.AFK_INTERNAL==="1";return t.trim()?gT(e,t.trim(),[],n):fT(e,[],n),"continue"}};function WU(e){return{name:"/skills",aliases:["/builtin-skills"],summary:"List all skills available in this session \u2014 vendored, user, and plugin",usage:"/skills [name]",hint:"When you want to browse every skill the session can dispatch \u2014 pass a name for full details on one.",async handler(t,n){let r=T.AFK_INTERNAL==="1";return n.trim()?gT(t,n.trim(),e,r):fT(t,e,r),"continue"}}}async function hT(e){let t;try{t=await e.supportedCommands()}catch(l){return console.error(p.dim(" \u26A0 Plugin-skill discovery failed: ")+(l instanceof Error?l.message:String(l))),null}let n=t.map(l=>({name:l.name,description:l.description,...l.argumentHint?{argumentHint:l.argumentHint}:{}})),r=mT(),o=T.AFK_INTERNAL==="1",s=new Set(ft().filter(l=>Et($e(l),o)).map(Ho)),i=[],a=new Set;for(let l of n){let c=`/${l.name}`;if(LU.has(c))continue;let u=Ho(l.name),d=r.get(u);if(s.has(u)){let m=l.name.includes(":")?l.name:`plugin:${l.name}`,f={...l,name:m};Bn(dT(f,d)),i.push({bare:u,altSlash:`/${m}`,altDescription:l.description}),a.add(u);continue}Bn(dT(l,d))}return Wo={discovered:n,collisions:i,shadowedBareNames:a},Bn(WU(n)),n.length}function yT(){return Wo.collisions.length===0?[]:Wo.collisions.map(e=>p.dim(` /${e.bare}: vendored or user skill wins; plugin form ${e.altSlash} stays reachable.`))}async function Xl(e){let[t,n]=await Promise.all([hT(e),tm(e)]);return{skillCount:t,agentCount:n}}function HU(e){let t={builtin:0,plugin:0,user:0,project:0};for(let r of e)t[r.source]++;return[["builtin","built-in"],["plugin","plugin"],["user","user"],["project","project"]].filter(([r])=>t[r]>0).map(([r,o])=>`${t[r]} ${o}`).join(" \xB7 ")}function KU(e,t){return e.size===0?null:{added:[...t].filter(n=>!e.has(n)).sort(),removed:[...e].filter(n=>!t.has(n)).sort()}}function GU(e){let{added:t,removed:n}=e;if(t.length===0&&n.length===0)return"";let r=[];t.length&&r.push(`+${t.length}`),n.length&&r.push(`\u2212${n.length}`);let o=`${r.join(" ")} since last reload`,s=[];return t.length>0&&t.length<=3&&s.push(`new: ${t.map(i=>`/${i}`).join(", ")}`),n.length>0&&n.length<=3&&s.push(`gone: ${n.map(i=>`/${i}`).join(", ")}`),s.length>0&&(o+=` (${s.join("; ")})`),o}function qU(e){if(e.length===0)return[];let t=8,n=e.slice(0,t),r=Math.min(24,n.reduce((s,i)=>Math.max(s,i.name.length),0)),o=n.map(s=>p.dim(` ${s.name.padEnd(r)} ${Qv(s)}`));return e.length>t&&o.push(p.dim(` \u2026and ${e.length-t} more`)),o}var zU={name:"/reload-plugins",summary:"Reload plugin skills from disk and refresh the slash registry",async handler(e){e.out.line(),e.out.info("Reloading plugins\u2026");let t=new Set(Wo.discovered.map(a=>a.name));ln();try{let a=e.session.current.getQuery();typeof a.reloadPlugins=="function"&&await a.reloadPlugins()}catch(a){e.out.warn(`Plugin reload failed: ${a instanceof Error?a.message:String(a)}`)}let[n,r]=await Promise.all([hT(e.session.current),tm(e.session.current)]);if(n===null&&r===null)return e.out.error("Could not refresh plugin skills or agents."),e.out.line(),"continue";let o=Zv(),s=[];n!==null&&s.push(`${n} skill${n===1?"":"s"}`),r!==null&&s.push(`${r} agent${r===1?"":"s"}`);let i=o.length>0?` from ${o.length} plugin${o.length===1?"":"s"}`:"";if(e.out.success(`Reloaded ${s.join(" + ")}${i}.`),n!==null){let a=HU(an()),l=KU(t,new Set(Wo.discovered.map(d=>d.name))),c=l?GU(l):"",u=[a,c].filter(d=>d.length>0);u.length>0&&e.out.line(p.dim(` ${u.join(" \xB7 ")}`))}for(let a of qU(o))e.out.line(a);return e.out.line(),"continue"}};function bT(){ce(jU),ce(zU)}W();G();function JU(e){let t=e??"builtin";switch(t){case"builtin":return"builtin";case"user":return"user";case"project":return"project";default:{let n=t;throw new Error(`[afk builtin-skills] Unhandled origin: ${String(n)}`)}}}function VU(e){let t=`/${e.name}`,n=e.argumentHint?`${t} ${e.argumentHint}`:void 0;return{name:t,summary:e.description,acceptsAttachments:!0,...n!==void 0?{usage:n}:{},...e.whenToUse?{hint:e.whenToUse}:{},...e.flags&&e.flags.length>0?{flags:e.flags}:{},async handler(r,o,s){try{await Jl(r,{skillName:e.name,skillMeta:e,args:o,attachments:s,preflight:async()=>{let i={skillName:e.name,rawArgs:o,source:JU(e.origin),capabilities:{compose:!0,subagents:!0}},a=r.session.current.sessionId,l=Tr(a);return(await vr(i,{cwd:r.stats.cwd??process.cwd(),artifactDir:l},u=>{T.AFK_SKILL_STREAM_VERBOSE==="1"&&r.out.warn(`preflight(${e.name}) failed: ${u instanceof Error?u.message:String(u)}`)}))?.manifestBlock}})}catch(i){r.out.line(),r.out.error(`${e.name} failed: ${i instanceof Error?i.message:String(i)}`)}return"continue"}}}function wT(){cT(),jd(),La(Lf(),"project");let e=T.AFK_INTERNAL==="1";for(let t of ft()){let n=$e(t);Et(n,e)&&Bn(VU(n))}}W();import{existsSync as oi,mkdirSync as Ij,renameSync as Pj,rmSync as Mj,symlinkSync as Oj,lstatSync as $j,unlinkSync as Dj}from"fs";import{basename as Lj,join as pm}from"path";W();import{existsSync as zn,mkdirSync as dj,readFileSync as pj,realpathSync as xT,renameSync as mj,rmSync as fj,symlinkSync as gj,lstatSync as hj,unlinkSync as yj}from"fs";import{basename as ET,dirname as bj,join as xr,resolve as lm,relative as wj}from"path";import{existsSync as ST}from"fs";import{isAbsolute as YU,resolve as TT}from"path";import{homedir as kT}from"os";var XU=/^(?:https?:\/\/|git:\/\/|ssh:\/\/|git\+ssh:\/\/|file:\/\/|git@[^:]+:)/,ZU=/^([a-zA-Z0-9][a-zA-Z0-9_.-]*)\/([a-zA-Z0-9][a-zA-Z0-9._-]*?)(?:\.git)?$/,QU=/^([a-zA-Z0-9][a-zA-Z0-9_.-]*):([a-zA-Z0-9][a-zA-Z0-9_.-]*)$/;function ej(e){return e==="~"?kT():e.startsWith("~/")?TT(kT(),e.slice(2)):e}function Zl(e){if(!e.startsWith("https://"))throw new Error(`Plugin source must use https:// (got: ${e}). git://, http://, ssh://, file://, and git@ are rejected to avoid downgrade attacks and unauthenticated clones.`)}function Ko(e){let t=e.trim();if(!t)throw new Error('plugin source is required (examples: "owner/repo", "https://github.com/owner/repo.git", "./my-plugin")');if(tj(t)){let o=vT(t);if(!ST(o))throw new Error(`could not resolve plugin source: "${t}" looks like a local path but does not exist on disk`);return{type:"local",path:o}}if(XU.test(t))return{type:"git",url:t};let n=QU.exec(t);if(n&&n[1]&&n[2])return{type:"marketplace-ref",marketplace:n[1],plugin:n[2]};let r=ZU.exec(t);if(r&&r[1]&&r[2]){let o=r[1],s=r[2];return{type:"github",owner:o,repo:s,url:`https://github.com/${o}/${s}.git`}}if(ST(t))return{type:"local",path:vT(t)};throw new Error(`could not resolve plugin source: "${t}". Use a git URL (https://\u2026/repo.git), GitHub shorthand (owner/repo), or a local path to a directory that exists on disk.`)}function tj(e){return e.startsWith("./")||e.startsWith("../")||e.startsWith("~")||e.startsWith("/")}function vT(e){let t=ej(e);return YU(t)?t:TT(process.cwd(),t)}import{execFile as nj}from"child_process";import{promisify as rj}from"util";var oj=rj(nj),Go=async(e,t,n)=>{try{let{stdout:r,stderr:o}=await oj("git",Array.from(e),{cwd:t,env:n,maxBuffer:20971520});return{stdout:r,stderr:o}}catch(r){throw sj(r)&&r.code==="ENOENT"?new Error("git not found on PATH \u2014 install git first"):r}};function sj(e){return typeof e=="object"&&e!==null&&"code"in e}var ij=Object.freeze(["-c","core.hooksPath=/dev/null","-c","filter.process=","-c","filter.smudge=","-c","filter.clean="]);function am(e){return[...ij,...e]}async function Ql(e,t,n={}){await(n.runner??Go)(am(["clone","--",e,t]),void 0,n.env)}async function ec(e,t={}){await(t.runner??Go)(am(["fetch","--tags","--prune"]),e,t.env)}async function Kn(e,t={}){let n=t.runner??Go,{stdout:r}=await n(["tag","--list","--sort=-v:refname"],e,t.env);return r.split(`
2154
- `).map(o=>o.trim()).filter(Boolean)}async function Gn(e,t,n={}){await(n.runner??Go)(am(["checkout","--detach",t]),e,n.env)}async function Jt(e,t={}){let n=t.runner??Go,{stdout:r}=await n(["rev-parse","HEAD"],e,t.env);return r.trim()}async function Vt(e,t={}){let n=t.runner??Go;try{let{stdout:r}=await n(["symbolic-ref","refs/remotes/origin/HEAD","--short"],e,t.env);return r.trim().replace(/^origin\//,"")||"main"}catch{return"main"}}var aj=/^v?(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/;function lj(e){let t=aj.exec(e);if(!t)return null;let[,n,r,o,s]=t;return{raw:e,major:Number(n),minor:Number(r),patch:Number(o),prerelease:s??null}}function cj(e,t){if(e===null&&t===null)return 0;if(e===null)return 1;if(t===null)return-1;let n=e.split("."),r=t.split("."),o=Math.max(n.length,r.length);for(let s=0;s<o;s++){let i=n[s],a=r[s];if(i===void 0)return-1;if(a===void 0)return 1;let l=/^\d+$/.test(i),c=/^\d+$/.test(a);if(l&&c){let u=Number(i)-Number(a);if(u!==0)return u}else{if(l)return-1;if(c)return 1;if(i<a)return-1;if(i>a)return 1}}return 0}function uj(e,t){return e.major!==t.major?e.major-t.major:e.minor!==t.minor?e.minor-t.minor:e.patch!==t.patch?e.patch-t.patch:cj(e.prerelease,t.prerelease)}function qn(e){let t=e.map(n=>lj(n.trim())).filter(n=>n!==null);return t.length===0?null:(t.sort((n,r)=>uj(r,n)),t[0]?.raw??null)}async function rc(e,t={},n={}){let r=n.pluginsDir??Oe(),o=n.indexPath??ie(),s=n.now??(()=>new Date),i=n.gitRunner?{runner:n.gitRunner}:{},a=n.confirm??!0,l=n.confirmDelayMs??3e3,c=Ko(e);if(dj(r,{recursive:!0}),c.type==="local")return Sj(c,t,r,o,s);if(c.type==="marketplace-ref")throw new Error(`marketplace-ref source "${c.marketplace}:${c.plugin}" must be installed via the marketplace resolver, not installPlugin directly`);return Zl(c.url),kj(c,t,r,o,s,i,{confirm:a,confirmDelayMs:l})}function Sj(e,t,n,r,o){RT(e.path);let s=AT(e.path),i=t.name??s??ET(e.path);hn(i);let a=xr(n,i);Er(a,n),cm(a,t.force??!1),(zn(a)||um(a))&&nc(a),gj(e.path,a,"dir");let l=o().toISOString(),c={source:e.path,sourceType:"local",ref:null,commit:null,enabled:!0,installedAt:l,updatedAt:l,...s&&s!==i?{manifestName:s}:{}};return $n(i,c,r),ln(),{name:i,dir:a,entry:c}}async function kj(e,t,n,r,o,s,i){let a=t.name??Tj(e);hn(a);let l=xr(n,a);Er(l,n),cm(l,t.force??!1),zn(l)&&nc(l);let c=e.type==="github"?`${e.owner}/${e.repo}`:e.url;i.confirm&&await xj(e.url,i.confirmDelayMs),await Ql(e.url,l,s);try{let u;if(t.ref)u=t.ref;else{let y=await Kn(l,s);u=qn(y)??await Vt(l,s)}(t.ref||await vj(l,u,s))&&await Gn(l,u,s);let d=await Jt(l,s);RT(l);let m=AT(l),f=a,g=l;if(!t.name&&m&&m!==a){hn(m);let y=xr(n,m);Er(y,n),cm(y,t.force??!1),zn(y)&&nc(y),mj(l,y),f=m,g=y}let h=o().toISOString(),b={source:c,sourceType:e.type,ref:u,commit:d,enabled:!0,installedAt:h,updatedAt:h,...m&&m!==f?{manifestName:m}:{}};return $n(f,b,r),ln(),{name:f,dir:g,entry:b}}catch(u){try{zn(l)&&nc(l)}catch{}throw u}}async function vj(e,t,n){let r=await Vt(e,n);return t!==r}function Tj(e){if(e.type==="github")return e.repo;let t=e.url.replace(/\.git$/,""),n=t.lastIndexOf("/"),r=t.lastIndexOf(":"),o=Math.max(n,r);return o>=0?t.slice(o+1):t}async function xj(e,t){let n="\u2550".repeat(70),r=o=>process.stderr.write(o+`
2155
- `);if(r(""),r(n),r(" \u26A0\uFE0F PLUGIN INSTALL WARNING \u2014 READ BEFORE CONTINUING"),r(n),r(""),r(` Source : ${e}`),r(""),r(" Installing a plugin grants ARBITRARY CODE EXECUTION to whoever controls"),r(" that git ref. The plugin's SKILL.md becomes a system prompt that runs"),r(" inside a subagent with full tool access (bash, write_file, web_scrape,"),r(" and any other tool enabled in your session)."),r(""),r(" \u25BA Audit the repository source code before proceeding."),r(" \u25BA Only install plugins from authors you trust."),r(" \u25BA Run `afk plugin install --yes <source>` to suppress this warning."),r(""),t>0){let o=Math.ceil(t/1e3);r(` Proceeding in ${o} second(s)\u2026 Press Ctrl-C to abort.`),r(""),await new Promise(s=>setTimeout(s,t))}r(n),r("")}function RT(e){let t=xr(e,".claude-plugin","plugin.json");if(zn(t))return;let n=xr(e,".claude-plugin","marketplace.json");if(zn(n))throw new Error(`${e} contains .claude-plugin/marketplace.json instead of plugin.json. Use \`afk marketplace install <source>\` to install a marketplace, then \`afk plugin install <marketplace>:<plugin>\` to install a plugin from it.`)}function AT(e){let t=xr(e,".claude-plugin","plugin.json");if(!zn(t))return null;try{let n=JSON.parse(pj(t,"utf8"));return typeof n.name=="string"&&n.name.trim()?n.name.trim():null}catch{return null}}var Ej=/^[a-zA-Z0-9][a-zA-Z0-9_-]*$/;function hn(e){if(!e||e.length>100||!Ej.test(e))throw new Error(`Invalid plugin name "${e}": must be 1\u2013100 chars, starting with alphanumeric, containing only letters, digits, hyphens, or underscores.`)}function Er(e,t){let n;try{n=xT(lm(t))}catch{n=lm(t)}let r=lm(e),o;try{o=xr(xT(bj(r)),ET(r))}catch{o=r}let s=wj(n,o);if(s.startsWith("..")||s==="")throw new Error(`Path traversal detected: resolved path "${e}" escapes plugin dir "${t}"`);if(s.startsWith("/"))throw new Error(`Path traversal detected: resolved path "${e}" escapes plugin dir "${t}"`)}function cm(e,t){if(!(!zn(e)&&!um(e))&&!t)throw new Error(`plugin directory already exists: ${e} (re-run with --force to replace)`)}function um(e){try{return hj(e).isSymbolicLink()}catch{return!1}}function nc(e){if(um(e)){yj(e);return}fj(e,{recursive:!0,force:!0})}import{existsSync as CT,readFileSync as Rj}from"fs";import{join as Aj}from"path";var _j=".claude-plugin/marketplace.json";function IT(e){return Aj(e,_j)}function dm(e){return CT(IT(e))}function yn(e){let t=IT(e);if(!CT(t))throw new Error(`marketplace manifest not found: ${t}`);let n;try{n=Rj(t,"utf8")}catch(o){throw new Error(`could not read marketplace manifest at ${t}: ${_T(o)}`)}let r;try{r=JSON.parse(n)}catch(o){throw new Error(`marketplace manifest at ${t} is not valid JSON: ${_T(o)}`)}return Cj(r,t)}function PT(e){try{return yn(e)}catch{return null}}function Cj(e,t){if(!e||typeof e!="object")throw new Error(`marketplace manifest at ${t} must be a JSON object`);let n=e,r=n.name;if(typeof r!="string"||!r.trim())throw new Error(`marketplace manifest at ${t} is missing required "name" field`);let o=n.plugins;if(!Array.isArray(o))throw new Error(`marketplace manifest at ${t} is missing required "plugins" array`);let s=new Set,i=o.map((u,d)=>{if(!u||typeof u!="object")throw new Error(`marketplace manifest at ${t}: plugins[${d}] must be an object`);let m=u,f=m.name,g=m.source;if(typeof f!="string"||!f.trim())throw new Error(`marketplace manifest at ${t}: plugins[${d}] missing required "name"`);if(typeof g!="string"||!g.trim())throw new Error(`marketplace manifest at ${t}: plugins[${d}] missing required "source"`);let h=f.trim();if(s.has(h))throw new Error(`marketplace manifest at ${t}: duplicate plugin name "${h}"`);s.add(h);let b={name:h,source:g.trim()},y=m.description;return typeof y=="string"&&(b.description=y),b}),a={name:r.trim(),plugins:i},l=n.metadata;if(l&&typeof l=="object"){let u=l,d={};typeof u.description=="string"&&(d.description=u.description),Object.keys(d).length>0&&(a.metadata=d)}let c=n.owner;if(c&&typeof c=="object"){let u=c,d={};typeof u.name=="string"&&(d.name=u.name),typeof u.email=="string"&&(d.email=u.email),Object.keys(d).length>0&&(a.owner=d)}return a}function _T(e){return e instanceof Error?e.message:String(e)}async function sc(e,t={},n={}){let r=n.cacheDir??Yt(),o=n.indexPath??ie(),s=n.now??(()=>new Date),i=n.gitRunner?{runner:n.gitRunner}:{},a=Ko(e);if(a.type==="marketplace-ref")throw new Error(`marketplace source cannot itself be a marketplace reference ("${e}")`);return Ij(r,{recursive:!0}),a.type==="local"?Fj(a,t,r,o,s):Nj(a,t,r,o,s,i)}function Fj(e,t,n,r,o){let s=yn(e.path),i=t.name??s.name;hn(i);let a=pm(n,i);Er(a,n),mm(a,t.force??!1),(oi(a)||fm(a))&&oc(a),Oj(e.path,a,"dir");let l=o().toISOString(),c={source:e.path,sourceType:"local",ref:null,commit:null,installedAt:l,updatedAt:l};return Is(i,c,r),{name:i,dir:a,entry:c,plugins:s.plugins.map(MT)}}async function Nj(e,t,n,r,o,s){Zl(e.url);let i=t.name??Uj(e);hn(i);let a=pm(n,i);Er(a,n),mm(a,t.force??!1),oi(a)&&oc(a);let l=e.type==="github"?`${e.owner}/${e.repo}`:e.url;await Ql(e.url,a,s);try{let c;if(t.ref)c=t.ref;else{let b=await Kn(a,s);c=qn(b)??await Vt(a,s)}(t.ref||await Bj(a,c,s))&&await Gn(a,c,s);let u=await Jt(a,s),d=yn(a),m=i,f=a;if(!t.name&&d.name!==i){hn(d.name);let b=pm(n,d.name);Er(b,n),mm(b,t.force??!1),oi(b)&&oc(b),Pj(a,b),m=d.name,f=b}let g=o().toISOString(),h={source:l,sourceType:e.type,ref:c,commit:u,installedAt:g,updatedAt:g};return Is(m,h,r),{name:m,dir:f,entry:h,plugins:d.plugins.map(MT)}}catch(c){try{oi(a)&&oc(a)}catch{}throw c}}async function Bj(e,t,n){let r=await Vt(e,n);return t!==r}function Uj(e){if(e.type==="github")return e.repo;let t=e.url.replace(/\.git$/,""),n=t.lastIndexOf("/"),r=t.lastIndexOf(":"),o=Math.max(n,r);return o>=0?t.slice(o+1):Lj(t)}function mm(e,t){if(!(!oi(e)&&!fm(e))&&!t)throw new Error(`marketplace directory already exists: ${e} (re-run with --force to replace)`)}function fm(e){try{return $j(e).isSymbolicLink()}catch{return!1}}function oc(e){if(fm(e)){Dj(e);return}Mj(e,{recursive:!0,force:!0})}function MT(e){return e.description?{name:e.name,description:e.description}:{name:e.name}}W();import{existsSync as jj,lstatSync as Wj,rmSync as Hj,unlinkSync as Kj}from"fs";import{join as Gj}from"path";function ic(e,t={}){let n=t.cacheDir??Yt(),r=t.indexPath??ie(),o=Gj(n,e),s=!1;qj(o)?(Kj(o),s=!0):jj(o)&&(Hj(o,{recursive:!0,force:!0}),s=!0);let i=fe(r),a=Object.prototype.hasOwnProperty.call(i.marketplaces,e),l=Object.entries(i.plugins).filter(([,c])=>c.marketplace===e).map(([c])=>c);return(a||l.length>0)&&qy(e,r),{name:e,removedDir:s,removedIndexEntry:a,removedPluginEntries:l}}function qj(e){try{return Wj(e).isSymbolicLink()}catch{return!1}}W();import{existsSync as zj}from"fs";import{join as Jj}from"path";async function si(e,t={},n={}){let r=n.indexPath??ie(),o=n.now??(()=>new Date),s=n.gitRunner?{runner:n.gitRunner}:{},a=fe(r).marketplaces[e];if(!a)throw new Error(`marketplace "${e}" is not installed`);let l=n.cacheDir?Jj(n.cacheDir,e):ss(e);if(!zj(l))return{name:e,status:"missing-dir",dir:l};if(a.sourceType==="local")return{name:e,status:"skipped-local"};let c=new Set((PT(l)?.plugins??[]).map(y=>y.name));await ec(l,s);let u;if(t.ref)u=t.ref;else{let y=await Kn(l,s);u=qn(y)??a.ref??await Vt(l,s)}if(u===a.ref){let y=await Jt(l,s);return{name:e,status:"up-to-date",ref:u,commit:y}}await Gn(l,u,s);let d=await Jt(l,s),m=o().toISOString(),f={...a,ref:u,commit:d,updatedAt:m};Is(e,f,r);let g=new Set(yn(l).plugins.map(y=>y.name)),h=[...g].filter(y=>!c.has(y)),b=[...c].filter(y=>!g.has(y));return{name:e,status:"updated",fromRef:a.ref,toRef:u,commit:d,addedPlugins:h,removedPlugins:b}}async function OT(e={}){let t=e.indexPath??ie(),n=fe(t),r=[];for(let o of Object.keys(n.marketplaces))try{r.push(await si(o,{},e))}catch(s){let i=s instanceof Error?s.message:String(s);r.push({name:o,status:"missing-dir",dir:i})}return r}G();W();import{existsSync as ac,statSync as Vj}from"fs";import{isAbsolute as Yj,join as Xj,resolve as $T}from"path";async function qo(e,t,n={},r={}){let o=r.marketplaceDirFor??ss,s=r.indexPath??ie(),i=r.now??(()=>new Date),a=o(e);if(!ac(a)||!dm(a))throw new Error(`marketplace "${e}" is not installed (looked for manifest under ${a})`);let l=yn(a),c=l.plugins.find(u=>u.name===t);if(!c){let u=l.plugins.map(d=>d.name).join(", ")||"(none)";throw new Error(`marketplace "${e}" does not list a plugin named "${t}". Available: ${u}`)}return Zj(c.source)?Qj(e,c,a,s,i,n):e1(e,c,n,r)}function lc(e,t={}){let n=t.marketplaceDirFor??ss,r=t.indexPath??ie(),o=n(e);if(!ac(o)||!dm(o))throw new Error(`marketplace "${e}" is not installed`);let s=yn(o),i=fe(r);return s.plugins.map(a=>{let l=`${e}:${a.name}`,c=l in i.plugins||a.name in i.plugins&&i.plugins[a.name]?.marketplace===e,u={name:a.name,installed:c,key:l};return a.description&&(u.description=a.description),u})}function Zj(e){return e.startsWith("./")||e.startsWith("../")||e.startsWith("/")||e.startsWith("~")}function Qj(e,t,n,r,o,s){let i=t.source,a=Yj(i)||i.startsWith("~")?t1(i):$T(n,i);if(!ac(a))throw new Error(`marketplace "${e}" lists plugin "${t.name}" at ${a}, but that path does not exist on disk`);if(!Vj(a).isDirectory())throw new Error(`marketplace "${e}" lists plugin "${t.name}" at ${a}, but that path is not a directory`);let c=Xj(a,".claude-plugin","plugin.json");if(!ac(c))throw new Error(`marketplace "${e}" lists plugin "${t.name}" at ${a}, but no .claude-plugin/plugin.json was found`);let u=`${e}:${t.name}`,d=fe(r);if(!s.force&&u in d.plugins&&d.plugins[u]?.enabled)throw new Error(`plugin "${u}" is already installed (re-run with --force to overwrite)`);let m=o().toISOString(),f={source:`${e}:${t.name}`,sourceType:"marketplace",ref:null,commit:null,enabled:!0,installedAt:m,updatedAt:m,marketplace:e};return $n(u,f,r),{key:u,name:t.name,dir:a,entry:f}}async function e1(e,t,n,r){let o={name:t.name,...n.ref?{ref:n.ref}:{},...n.force?{force:!0}:{}},s=await rc(t.source,o,r),i=r.indexPath??ie(),a={...s.entry,marketplace:e};return $n(s.name,a,i),{key:s.name,name:s.name,dir:s.dir,entry:a}}function t1(e){if(e.startsWith("~")){let t=T.HOME??"";if(e==="~")return t;if(e.startsWith("~/"))return $T(t,e.slice(2))}return e}var DT=["add","plugins","install","remove","update"],n1={name:"/marketplaces",summary:"List installed plugin marketplaces",async handler(e){return o1(e),"continue"}},r1={name:"/marketplace",summary:"Manage plugin marketplaces (add | plugins | install | remove | update)",usage:"/marketplace <add|plugins|install|remove|update> [args]",async handler(e,t){let n=t.trim();if(!n)return s1(e),"continue";let[r,...o]=n.split(/\s+/);if(!r||!DT.includes(r))return e.out.error(`Unknown subcommand "${r??""}". Try one of: ${DT.join(", ")}.`),"continue";switch(r){case"add":return i1(e,o);case"plugins":return a1(e,o);case"install":return l1(e,o);case"remove":return c1(e,o);case"update":return u1(e,o);default:return"continue"}}};function LT(){ce(n1),ce(r1)}function o1(e){let t=fe(),n=Object.entries(t.marketplaces).sort(([r],[o])=>r.localeCompare(o));if(e.out.line(),n.length===0){e.out.line(p.dim(" No marketplaces installed.")),e.out.line(p.dim(" Try: /marketplace add anthropics/claude-plugins-official")),e.out.line();return}e.out.line(p.bold("Installed marketplaces:"));for(let[r,o]of n){let s=o.ref?p.brand(o.ref):p.dim("(local)");e.out.line(` ${p.bold(r.padEnd(28))} ${s.padEnd(12)} ${p.dim(o.source)}`)}e.out.line()}function s1(e){e.out.line(),e.out.line(p.bold("/marketplace usage:")),e.out.line(` ${p.brand("/marketplace add")} <git-url|owner/repo|local-path>`),e.out.line(` ${p.brand("/marketplace plugins")} <marketplace>`),e.out.line(` ${p.brand("/marketplace install")} <marketplace> <plugin>`),e.out.line(` ${p.brand("/marketplace remove")} <marketplace>`),e.out.line(` ${p.brand("/marketplace update")} [<marketplace>]`),e.out.line()}async function i1(e,t){if(t.length===0)return e.out.error("Usage: /marketplace add <source> [name]"),"continue";let[n,r,...o]=t;if(!n)return e.out.error("Usage: /marketplace add <source> [name]"),"continue";let s=d1(o);e.out.info(`Installing marketplace ${n}\u2026`);try{let i=await sc(n,{...r&&!r.startsWith("-")?{name:r}:{},...s.ref?{ref:s.ref}:{},...s.force?{force:!0}:{}});e.out.success(`Installed marketplace ${i.name} (${i.plugins.length} plugin(s) available).`),e.out.line(p.dim(` Next: /marketplace plugins ${i.name}`))}catch(i){e.out.error(`Install failed: ${cc(i)}`)}return"continue"}function a1(e,t){let n=t[0];if(!n)return e.out.error("Usage: /marketplace plugins <marketplace>"),"continue";try{let r=lc(n);if(e.out.line(),r.length===0)return e.out.line(p.dim(` Marketplace "${n}" lists no plugins.`)),e.out.line(),"continue";e.out.line(p.bold(`Plugins in ${n}:`)),r.forEach((o,s)=>{let i=o.installed?p.brand("[\u2713]"):p.dim("[ ]"),a=o.description?p.dim(` \u2014 ${o.description}`):"";e.out.line(` ${i} ${p.bold(String(s+1).padStart(2))}. ${p.bold(o.name)}${a}`)}),e.out.line(),e.out.line(p.dim(` Install one: /marketplace install ${n} <plugin>`)),e.out.line()}catch(r){e.out.error(`List failed: ${cc(r)}`)}return"continue"}async function l1(e,t){let n,r;if(t.length===1&&t[0]?.includes(":")){let o=t[0].split(":");o.length===2&&([n,r]=o)}else[n,r]=t;if(!n||!r)return e.out.error("Usage: /marketplace install <marketplace> <plugin>"),"continue";e.out.info(`Installing ${n}:${r}\u2026`);try{let o=await qo(n,r);e.out.success(`Installed ${o.key}.`),e.out.line(p.dim(" Run /reload-plugins to refresh this session's slash commands."))}catch(o){e.out.error(`Install failed: ${cc(o)}`)}return"continue"}function c1(e,t){let n=t[0];if(!n)return e.out.error("Usage: /marketplace remove <marketplace>"),"continue";let r=ic(n);if(!r.removedDir&&!r.removedIndexEntry&&r.removedPluginEntries.length===0)return e.out.line(p.dim(` No marketplace named "${n}" to remove.`)),"continue";let o=r.removedPluginEntries.length>0?` + ${r.removedPluginEntries.length} plugin(s)`:"";return e.out.success(`Removed marketplace ${n}${o}.`),"continue"}async function u1(e,t){let n=t[0];if(!n)return e.out.error("Usage: /marketplace update <marketplace>"),"continue";e.out.info(`Updating ${n}\u2026`);try{let r=await si(n);switch(r.status){case"updated":{let o=r.addedPlugins.length>0?` +${r.addedPlugins.join(", ")}`:"",s=r.removedPlugins.length>0?` -${r.removedPlugins.join(", ")}`:"";e.out.success(`Updated ${n}: ${r.fromRef??"(none)"} \u2192 ${r.toRef}${o}${s}`);break}case"up-to-date":e.out.info(`${n} is up-to-date (${r.ref}).`);break;case"skipped-local":e.out.info(`${n} skipped (local source).`);break;case"missing-dir":e.out.warn(`${n}: marketplace dir missing (${r.dir}).`);break}}catch(r){e.out.error(`Update failed: ${cc(r)}`)}return"continue"}function d1(e){let t={};for(let n=0;n<e.length;n+=1){let r=e[n];r==="-f"||r==="--force"?t.force=!0:(r==="-r"||r==="--ref")&&e[n+1]&&(t.ref=e[n+1],n+=1)}return t}function cc(e){return e instanceof Error?e.message:String(e)}$v("shadow-verify",{glyph:"\u25C8",color:"#7B5EA7",inFlightVerb:"verifying\u2026"});function FT(){ES();for(let e of PS)ce(e);for(let e of US)ce(e);ce(WS),ce(ZS),ce(QS),ce(ek),ce(ik),ce(lk);for(let e of pk)ce(e);ce(gk),ce(bk),ce(Sk),ce(Mv),ce(Fv),ce(Nv),ce(Uv),ce(zv),ce(Jv),ce(Xv),wT(),xS(Wv),bT(),eT(),LT()}function NT(e,t={}){if(!e.isTTY)return{writeLine:o=>{e.write(o+`
2153
+ ${t}`}var lT=!1;function cT(){lT||(lT=!0,nm("review",oT),rT())}G();var LU=new Set(["/exit","/quit","/clear","/compact","/help"]),Wo={discovered:[],collisions:[],shadowedBareNames:new Set};function Ho(e){return e.includes(":")?e.split(":").pop():e}function mT(e){let t=e??Yt(),n=new Map;try{uT(t)}catch{return n}let r=(o,s)=>{if(s>8)return;let i;try{i=OU(o)}catch{return}for(let a of i){let l=DU(o,a),c;try{c=uT(l)}catch{continue}if(c.isDirectory()){r(l,s+1);continue}if(a!=="SKILL.md"||!c.isFile())continue;let u;try{u=$U(l,"utf-8")}catch{continue}let d=l.split("/"),p=d[d.length-2];if(!p)continue;let f=Ls(u),g=f.frontmatterFlags&&f.frontmatterFlags.length>0?f.frontmatterFlags:Ud(f.body);if(g.length===0)continue;let h=n.get(p)??[],b=new Set([...h,...g]);n.set(p,Array.from(b).sort())}};return r(t,0),n}function FU(e){if(!e)return;let t=e.split(/(?<=[.!?])\s+/);for(let n of t){let r=/^(Use(?:d)? when\b.*|When\s+(?:the\s+user\s+|to\s+)?\b.*)$/i.exec(n.trim());if(r&&r[1]){let o=r[1].trim();if(o.length>=12)return o}}}function dT(e,t){let n=`/${e.name}`,r=e.argumentHint?`${n} ${e.argumentHint}`:void 0,o=FU(e.description);return{name:n,summary:e.description,acceptsAttachments:!0,...r!==void 0?{usage:r}:{},...o?{hint:o}:{},...t&&t.length>0?{flags:t}:{},async handler(s,i,a){let l={name:e.name,description:e.description,handler:async()=>{},context:"inline"};try{await Jl(s,{skillName:e.name,skillMeta:l,args:i,attachments:a,preflight:async()=>{let c=e.name.includes(":")?e.name.split(":").pop()??e.name:e.name,u={skillName:c,rawArgs:i,source:"plugin",capabilities:{compose:!0,subagents:!0}},d=s.session.current.sessionId,p=Tr(d);return(await vr(u,{cwd:s.stats.cwd??process.cwd(),artifactDir:p},g=>{T.AFK_SKILL_STREAM_VERBOSE==="1"&&s.out.warn(`preflight(${c}) failed: ${g instanceof Error?g.message:String(g)}`)}))?.manifestBlock}})}catch(c){s.out.line(),s.out.error(`${e.name} failed: ${c instanceof Error?c.message:String(c)}`)}return"continue"}}}function NU(e,t){let n=new Map,r=s=>{let i=Ho(s.slashName.replace(/^\//,"")),a=n.get(i);a?a.alts.push(s):n.set(i,{main:s,alts:[]})};for(let s of Mg(t)){let i=$e(s),a=`/${s}`,l=i.argumentHint?`${a} ${i.argumentHint}`:a,c={slashName:a,display:l,description:i.description};i.origin==="user"&&(c.sourceLabel="user"),r(c)}let o=new Map(Wo.collisions.map(s=>[s.bare,s.altSlash]));for(let s of e){let i=Ho(s.name),l=o.get(i)??`/${s.name}`,c=s.argumentHint?`${l} ${s.argumentHint}`:l;r({slashName:l,display:c,description:s.description,sourceLabel:"plugin"})}return n}function pT(e,t=80){let n=e.indexOf(". "),r=n>=0?e.slice(0,n+1):e,o=r.length<=t?r:e;return o.length<=t?o:o.slice(0,t-1)+"\u2026"}function BU(e,t,n){let r=t.main,o=m.warning(r.display.padEnd(n)),s=r.sourceLabel?m.dim(`(${r.sourceLabel}) `):"";e.out.line(` ${o} ${s}${m.dim(pT(r.description))}`);for(let i of t.alts){let a=m.warning(i.display.padEnd(Math.max(0,n-4))),l=i.sourceLabel?m.dim(`(${i.sourceLabel} alt) `):m.dim("(alt) ");e.out.line(` ${m.dim("\u2514")} ${a} ${l}${m.dim(pT(i.description))}`)}}function fT(e,t,n){let r=NU(t,n),o=Array.from(r.values()).reduce((a,l)=>a+1+l.alts.length,0);if(e.out.line(),r.size===0){e.out.line(m.dim(" No skills available. Built-in skills should always load \u2014 check your install.")),e.out.line();return}e.out.line(m.bold("Skills")+m.dim(` (${o} loaded)`)),e.out.line(he());let s=Array.from(r.keys()).sort(),i=s.reduce((a,l)=>{let c=r.get(l);return Math.max(a,c.main.display.length)},0)+2;for(let a of s)BU(e,r.get(a),i);e.out.line(),e.out.line(m.dim(" Tip: /skills <name> for full details on a skill.")),e.out.line(m.dim(" Source: vendored (no badge), (user), (plugin). Shadowed entries listed under their winner.")),e.out.line()}function UU(e,t){try{let n=$e(e);return Et(n,t)?n:void 0}catch{return}}function gT(e,t,n,r){let o=t.replace(/^\//,"").trim(),s=UU(o,r),i=n.find(h=>Ho(h.name)===o||h.name===o);if(!s&&!i){e.out.line(),e.out.line(m.dim(` No skill found matching "${o}".`)),e.out.line();return}let a=s?.name??Ho(i.name),l=s?.description??i.description,c=s?.argumentHint??i?.argumentHint,u=c?`/${a} ${c}`:`/${a}`,d=s?s.origin??"builtin":"plugin";e.out.line(),e.out.line(` ${m.warning(u)}`),e.out.line(),e.out.line(` ${l}`),s?.whenToUse&&(e.out.line(),e.out.line(` ${m.bold("When to use:")}`),e.out.line(` ${m.dim(s.whenToUse)}`));let p=s?.flags,f=mT().get(o),g=p??f;g&&g.length>0&&(e.out.line(),e.out.line(` ${m.bold("Flags:")} ${m.dim(g.join(", "))}`)),e.out.line(),e.out.line(` ${m.bold("Source:")} ${m.dim(d)}`),e.out.line()}var jU={name:"/skills",aliases:["/builtin-skills"],summary:"List all skills available in this session \u2014 vendored, user, and plugin",usage:"/skills [name]",hint:"When you want to browse every skill the session can dispatch \u2014 pass a name for full details on one.",async handler(e,t){let n=T.AFK_INTERNAL==="1";return t.trim()?gT(e,t.trim(),[],n):fT(e,[],n),"continue"}};function WU(e){return{name:"/skills",aliases:["/builtin-skills"],summary:"List all skills available in this session \u2014 vendored, user, and plugin",usage:"/skills [name]",hint:"When you want to browse every skill the session can dispatch \u2014 pass a name for full details on one.",async handler(t,n){let r=T.AFK_INTERNAL==="1";return n.trim()?gT(t,n.trim(),e,r):fT(t,e,r),"continue"}}}async function hT(e){let t;try{t=await e.supportedCommands()}catch(l){return console.error(m.dim(" \u26A0 Plugin-skill discovery failed: ")+(l instanceof Error?l.message:String(l))),null}let n=t.map(l=>({name:l.name,description:l.description,...l.argumentHint?{argumentHint:l.argumentHint}:{}})),r=mT(),o=T.AFK_INTERNAL==="1",s=new Set(ft().filter(l=>Et($e(l),o)).map(Ho)),i=[],a=new Set;for(let l of n){let c=`/${l.name}`;if(LU.has(c))continue;let u=Ho(l.name),d=r.get(u);if(s.has(u)){let p=l.name.includes(":")?l.name:`plugin:${l.name}`,f={...l,name:p};Bn(dT(f,d)),i.push({bare:u,altSlash:`/${p}`,altDescription:l.description}),a.add(u);continue}Bn(dT(l,d))}return Wo={discovered:n,collisions:i,shadowedBareNames:a},Bn(WU(n)),n.length}function yT(){return Wo.collisions.length===0?[]:Wo.collisions.map(e=>m.dim(` /${e.bare}: vendored or user skill wins; plugin form ${e.altSlash} stays reachable.`))}async function Xl(e){let[t,n]=await Promise.all([hT(e),tm(e)]);return{skillCount:t,agentCount:n}}function HU(e){let t={builtin:0,plugin:0,user:0,project:0};for(let r of e)t[r.source]++;return[["builtin","built-in"],["plugin","plugin"],["user","user"],["project","project"]].filter(([r])=>t[r]>0).map(([r,o])=>`${t[r]} ${o}`).join(" \xB7 ")}function KU(e,t){return e.size===0?null:{added:[...t].filter(n=>!e.has(n)).sort(),removed:[...e].filter(n=>!t.has(n)).sort()}}function GU(e){let{added:t,removed:n}=e;if(t.length===0&&n.length===0)return"";let r=[];t.length&&r.push(`+${t.length}`),n.length&&r.push(`\u2212${n.length}`);let o=`${r.join(" ")} since last reload`,s=[];return t.length>0&&t.length<=3&&s.push(`new: ${t.map(i=>`/${i}`).join(", ")}`),n.length>0&&n.length<=3&&s.push(`gone: ${n.map(i=>`/${i}`).join(", ")}`),s.length>0&&(o+=` (${s.join("; ")})`),o}function qU(e){if(e.length===0)return[];let t=8,n=e.slice(0,t),r=Math.min(24,n.reduce((s,i)=>Math.max(s,i.name.length),0)),o=n.map(s=>m.dim(` ${s.name.padEnd(r)} ${Qv(s)}`));return e.length>t&&o.push(m.dim(` \u2026and ${e.length-t} more`)),o}var zU={name:"/reload-plugins",summary:"Reload plugin skills from disk and refresh the slash registry",async handler(e){e.out.line(),e.out.info("Reloading plugins\u2026");let t=new Set(Wo.discovered.map(a=>a.name));ln();try{let a=e.session.current.getQuery();typeof a.reloadPlugins=="function"&&await a.reloadPlugins()}catch(a){e.out.warn(`Plugin reload failed: ${a instanceof Error?a.message:String(a)}`)}let[n,r]=await Promise.all([hT(e.session.current),tm(e.session.current)]);if(n===null&&r===null)return e.out.error("Could not refresh plugin skills or agents."),e.out.line(),"continue";let o=Zv(),s=[];n!==null&&s.push(`${n} skill${n===1?"":"s"}`),r!==null&&s.push(`${r} agent${r===1?"":"s"}`);let i=o.length>0?` from ${o.length} plugin${o.length===1?"":"s"}`:"";if(e.out.success(`Reloaded ${s.join(" + ")}${i}.`),n!==null){let a=HU(an()),l=KU(t,new Set(Wo.discovered.map(d=>d.name))),c=l?GU(l):"",u=[a,c].filter(d=>d.length>0);u.length>0&&e.out.line(m.dim(` ${u.join(" \xB7 ")}`))}for(let a of qU(o))e.out.line(a);return e.out.line(),"continue"}};function bT(){ce(jU),ce(zU)}W();G();function JU(e){let t=e??"builtin";switch(t){case"builtin":return"builtin";case"user":return"user";case"project":return"project";default:{let n=t;throw new Error(`[afk builtin-skills] Unhandled origin: ${String(n)}`)}}}function VU(e){let t=`/${e.name}`,n=e.argumentHint?`${t} ${e.argumentHint}`:void 0;return{name:t,summary:e.description,acceptsAttachments:!0,...n!==void 0?{usage:n}:{},...e.whenToUse?{hint:e.whenToUse}:{},...e.flags&&e.flags.length>0?{flags:e.flags}:{},async handler(r,o,s){try{await Jl(r,{skillName:e.name,skillMeta:e,args:o,attachments:s,preflight:async()=>{let i={skillName:e.name,rawArgs:o,source:JU(e.origin),capabilities:{compose:!0,subagents:!0}},a=r.session.current.sessionId,l=Tr(a);return(await vr(i,{cwd:r.stats.cwd??process.cwd(),artifactDir:l},u=>{T.AFK_SKILL_STREAM_VERBOSE==="1"&&r.out.warn(`preflight(${e.name}) failed: ${u instanceof Error?u.message:String(u)}`)}))?.manifestBlock}})}catch(i){r.out.line(),r.out.error(`${e.name} failed: ${i instanceof Error?i.message:String(i)}`)}return"continue"}}}function wT(){cT(),jd(),La(Lf(),"project");let e=T.AFK_INTERNAL==="1";for(let t of ft()){let n=$e(t);Et(n,e)&&Bn(VU(n))}}W();import{existsSync as oi,mkdirSync as I1,renameSync as P1,rmSync as M1,symlinkSync as O1,lstatSync as $1,unlinkSync as D1}from"fs";import{basename as L1,join as pm}from"path";W();import{existsSync as zn,mkdirSync as d1,readFileSync as p1,realpathSync as xT,renameSync as m1,rmSync as f1,symlinkSync as g1,lstatSync as h1,unlinkSync as y1}from"fs";import{basename as ET,dirname as b1,join as xr,resolve as lm,relative as w1}from"path";import{existsSync as ST}from"fs";import{isAbsolute as YU,resolve as TT}from"path";import{homedir as kT}from"os";var XU=/^(?:https?:\/\/|git:\/\/|ssh:\/\/|git\+ssh:\/\/|file:\/\/|git@[^:]+:)/,ZU=/^([a-zA-Z0-9][a-zA-Z0-9_.-]*)\/([a-zA-Z0-9][a-zA-Z0-9._-]*?)(?:\.git)?$/,QU=/^([a-zA-Z0-9][a-zA-Z0-9_.-]*):([a-zA-Z0-9][a-zA-Z0-9_.-]*)$/;function e1(e){return e==="~"?kT():e.startsWith("~/")?TT(kT(),e.slice(2)):e}function Zl(e){if(!e.startsWith("https://"))throw new Error(`Plugin source must use https:// (got: ${e}). git://, http://, ssh://, file://, and git@ are rejected to avoid downgrade attacks and unauthenticated clones.`)}function Ko(e){let t=e.trim();if(!t)throw new Error('plugin source is required (examples: "owner/repo", "https://github.com/owner/repo.git", "./my-plugin")');if(t1(t)){let o=vT(t);if(!ST(o))throw new Error(`could not resolve plugin source: "${t}" looks like a local path but does not exist on disk`);return{type:"local",path:o}}if(XU.test(t))return{type:"git",url:t};let n=QU.exec(t);if(n&&n[1]&&n[2])return{type:"marketplace-ref",marketplace:n[1],plugin:n[2]};let r=ZU.exec(t);if(r&&r[1]&&r[2]){let o=r[1],s=r[2];return{type:"github",owner:o,repo:s,url:`https://github.com/${o}/${s}.git`}}if(ST(t))return{type:"local",path:vT(t)};throw new Error(`could not resolve plugin source: "${t}". Use a git URL (https://\u2026/repo.git), GitHub shorthand (owner/repo), or a local path to a directory that exists on disk.`)}function t1(e){return e.startsWith("./")||e.startsWith("../")||e.startsWith("~")||e.startsWith("/")}function vT(e){let t=e1(e);return YU(t)?t:TT(process.cwd(),t)}import{execFile as n1}from"child_process";import{promisify as r1}from"util";var o1=r1(n1),Go=async(e,t,n)=>{try{let{stdout:r,stderr:o}=await o1("git",Array.from(e),{cwd:t,env:n,maxBuffer:20971520});return{stdout:r,stderr:o}}catch(r){throw s1(r)&&r.code==="ENOENT"?new Error("git not found on PATH \u2014 install git first"):r}};function s1(e){return typeof e=="object"&&e!==null&&"code"in e}var i1=Object.freeze(["-c","core.hooksPath=/dev/null","-c","filter.process=","-c","filter.smudge=","-c","filter.clean="]);function am(e){return[...i1,...e]}async function Ql(e,t,n={}){await(n.runner??Go)(am(["clone","--",e,t]),void 0,n.env)}async function ec(e,t={}){await(t.runner??Go)(am(["fetch","--tags","--prune"]),e,t.env)}async function Kn(e,t={}){let n=t.runner??Go,{stdout:r}=await n(["tag","--list","--sort=-v:refname"],e,t.env);return r.split(`
2154
+ `).map(o=>o.trim()).filter(Boolean)}async function Gn(e,t,n={}){await(n.runner??Go)(am(["checkout","--detach",t]),e,n.env)}async function Jt(e,t={}){let n=t.runner??Go,{stdout:r}=await n(["rev-parse","HEAD"],e,t.env);return r.trim()}async function Vt(e,t={}){let n=t.runner??Go;try{let{stdout:r}=await n(["symbolic-ref","refs/remotes/origin/HEAD","--short"],e,t.env);return r.trim().replace(/^origin\//,"")||"main"}catch{return"main"}}var a1=/^v?(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/;function l1(e){let t=a1.exec(e);if(!t)return null;let[,n,r,o,s]=t;return{raw:e,major:Number(n),minor:Number(r),patch:Number(o),prerelease:s??null}}function c1(e,t){if(e===null&&t===null)return 0;if(e===null)return 1;if(t===null)return-1;let n=e.split("."),r=t.split("."),o=Math.max(n.length,r.length);for(let s=0;s<o;s++){let i=n[s],a=r[s];if(i===void 0)return-1;if(a===void 0)return 1;let l=/^\d+$/.test(i),c=/^\d+$/.test(a);if(l&&c){let u=Number(i)-Number(a);if(u!==0)return u}else{if(l)return-1;if(c)return 1;if(i<a)return-1;if(i>a)return 1}}return 0}function u1(e,t){return e.major!==t.major?e.major-t.major:e.minor!==t.minor?e.minor-t.minor:e.patch!==t.patch?e.patch-t.patch:c1(e.prerelease,t.prerelease)}function qn(e){let t=e.map(n=>l1(n.trim())).filter(n=>n!==null);return t.length===0?null:(t.sort((n,r)=>u1(r,n)),t[0]?.raw??null)}async function rc(e,t={},n={}){let r=n.pluginsDir??Oe(),o=n.indexPath??ie(),s=n.now??(()=>new Date),i=n.gitRunner?{runner:n.gitRunner}:{},a=n.confirm??!0,l=n.confirmDelayMs??3e3,c=Ko(e);if(d1(r,{recursive:!0}),c.type==="local")return S1(c,t,r,o,s);if(c.type==="marketplace-ref")throw new Error(`marketplace-ref source "${c.marketplace}:${c.plugin}" must be installed via the marketplace resolver, not installPlugin directly`);return Zl(c.url),k1(c,t,r,o,s,i,{confirm:a,confirmDelayMs:l})}function S1(e,t,n,r,o){RT(e.path);let s=AT(e.path),i=t.name??s??ET(e.path);hn(i);let a=xr(n,i);Er(a,n),cm(a,t.force??!1),(zn(a)||um(a))&&nc(a),g1(e.path,a,"dir");let l=o().toISOString(),c={source:e.path,sourceType:"local",ref:null,commit:null,enabled:!0,installedAt:l,updatedAt:l,...s&&s!==i?{manifestName:s}:{}};return $n(i,c,r),ln(),{name:i,dir:a,entry:c}}async function k1(e,t,n,r,o,s,i){let a=t.name??T1(e);hn(a);let l=xr(n,a);Er(l,n),cm(l,t.force??!1),zn(l)&&nc(l);let c=e.type==="github"?`${e.owner}/${e.repo}`:e.url;i.confirm&&await x1(e.url,i.confirmDelayMs),await Ql(e.url,l,s);try{let u;if(t.ref)u=t.ref;else{let y=await Kn(l,s);u=qn(y)??await Vt(l,s)}(t.ref||await v1(l,u,s))&&await Gn(l,u,s);let d=await Jt(l,s);RT(l);let p=AT(l),f=a,g=l;if(!t.name&&p&&p!==a){hn(p);let y=xr(n,p);Er(y,n),cm(y,t.force??!1),zn(y)&&nc(y),m1(l,y),f=p,g=y}let h=o().toISOString(),b={source:c,sourceType:e.type,ref:u,commit:d,enabled:!0,installedAt:h,updatedAt:h,...p&&p!==f?{manifestName:p}:{}};return $n(f,b,r),ln(),{name:f,dir:g,entry:b}}catch(u){try{zn(l)&&nc(l)}catch{}throw u}}async function v1(e,t,n){let r=await Vt(e,n);return t!==r}function T1(e){if(e.type==="github")return e.repo;let t=e.url.replace(/\.git$/,""),n=t.lastIndexOf("/"),r=t.lastIndexOf(":"),o=Math.max(n,r);return o>=0?t.slice(o+1):t}async function x1(e,t){let n="\u2550".repeat(70),r=o=>process.stderr.write(o+`
2155
+ `);if(r(""),r(n),r(" \u26A0\uFE0F PLUGIN INSTALL WARNING \u2014 READ BEFORE CONTINUING"),r(n),r(""),r(` Source : ${e}`),r(""),r(" Installing a plugin grants ARBITRARY CODE EXECUTION to whoever controls"),r(" that git ref. The plugin's SKILL.md becomes a system prompt that runs"),r(" inside a subagent with full tool access (bash, write_file, web_scrape,"),r(" and any other tool enabled in your session)."),r(""),r(" \u25BA Audit the repository source code before proceeding."),r(" \u25BA Only install plugins from authors you trust."),r(" \u25BA Run `afk plugin install --yes <source>` to suppress this warning."),r(""),t>0){let o=Math.ceil(t/1e3);r(` Proceeding in ${o} second(s)\u2026 Press Ctrl-C to abort.`),r(""),await new Promise(s=>setTimeout(s,t))}r(n),r("")}function RT(e){let t=xr(e,".claude-plugin","plugin.json");if(zn(t))return;let n=xr(e,".claude-plugin","marketplace.json");if(zn(n))throw new Error(`${e} contains .claude-plugin/marketplace.json instead of plugin.json. Use \`afk marketplace install <source>\` to install a marketplace, then \`afk plugin install <marketplace>:<plugin>\` to install a plugin from it.`)}function AT(e){let t=xr(e,".claude-plugin","plugin.json");if(!zn(t))return null;try{let n=JSON.parse(p1(t,"utf8"));return typeof n.name=="string"&&n.name.trim()?n.name.trim():null}catch{return null}}var E1=/^[a-zA-Z0-9][a-zA-Z0-9_-]*$/;function hn(e){if(!e||e.length>100||!E1.test(e))throw new Error(`Invalid plugin name "${e}": must be 1\u2013100 chars, starting with alphanumeric, containing only letters, digits, hyphens, or underscores.`)}function Er(e,t){let n;try{n=xT(lm(t))}catch{n=lm(t)}let r=lm(e),o;try{o=xr(xT(b1(r)),ET(r))}catch{o=r}let s=w1(n,o);if(s.startsWith("..")||s==="")throw new Error(`Path traversal detected: resolved path "${e}" escapes plugin dir "${t}"`);if(s.startsWith("/"))throw new Error(`Path traversal detected: resolved path "${e}" escapes plugin dir "${t}"`)}function cm(e,t){if(!(!zn(e)&&!um(e))&&!t)throw new Error(`plugin directory already exists: ${e} (re-run with --force to replace)`)}function um(e){try{return h1(e).isSymbolicLink()}catch{return!1}}function nc(e){if(um(e)){y1(e);return}f1(e,{recursive:!0,force:!0})}import{existsSync as CT,readFileSync as R1}from"fs";import{join as A1}from"path";var _1=".claude-plugin/marketplace.json";function IT(e){return A1(e,_1)}function dm(e){return CT(IT(e))}function yn(e){let t=IT(e);if(!CT(t))throw new Error(`marketplace manifest not found: ${t}`);let n;try{n=R1(t,"utf8")}catch(o){throw new Error(`could not read marketplace manifest at ${t}: ${_T(o)}`)}let r;try{r=JSON.parse(n)}catch(o){throw new Error(`marketplace manifest at ${t} is not valid JSON: ${_T(o)}`)}return C1(r,t)}function PT(e){try{return yn(e)}catch{return null}}function C1(e,t){if(!e||typeof e!="object")throw new Error(`marketplace manifest at ${t} must be a JSON object`);let n=e,r=n.name;if(typeof r!="string"||!r.trim())throw new Error(`marketplace manifest at ${t} is missing required "name" field`);let o=n.plugins;if(!Array.isArray(o))throw new Error(`marketplace manifest at ${t} is missing required "plugins" array`);let s=new Set,i=o.map((u,d)=>{if(!u||typeof u!="object")throw new Error(`marketplace manifest at ${t}: plugins[${d}] must be an object`);let p=u,f=p.name,g=p.source;if(typeof f!="string"||!f.trim())throw new Error(`marketplace manifest at ${t}: plugins[${d}] missing required "name"`);if(typeof g!="string"||!g.trim())throw new Error(`marketplace manifest at ${t}: plugins[${d}] missing required "source"`);let h=f.trim();if(s.has(h))throw new Error(`marketplace manifest at ${t}: duplicate plugin name "${h}"`);s.add(h);let b={name:h,source:g.trim()},y=p.description;return typeof y=="string"&&(b.description=y),b}),a={name:r.trim(),plugins:i},l=n.metadata;if(l&&typeof l=="object"){let u=l,d={};typeof u.description=="string"&&(d.description=u.description),Object.keys(d).length>0&&(a.metadata=d)}let c=n.owner;if(c&&typeof c=="object"){let u=c,d={};typeof u.name=="string"&&(d.name=u.name),typeof u.email=="string"&&(d.email=u.email),Object.keys(d).length>0&&(a.owner=d)}return a}function _T(e){return e instanceof Error?e.message:String(e)}async function sc(e,t={},n={}){let r=n.cacheDir??Yt(),o=n.indexPath??ie(),s=n.now??(()=>new Date),i=n.gitRunner?{runner:n.gitRunner}:{},a=Ko(e);if(a.type==="marketplace-ref")throw new Error(`marketplace source cannot itself be a marketplace reference ("${e}")`);return I1(r,{recursive:!0}),a.type==="local"?F1(a,t,r,o,s):N1(a,t,r,o,s,i)}function F1(e,t,n,r,o){let s=yn(e.path),i=t.name??s.name;hn(i);let a=pm(n,i);Er(a,n),mm(a,t.force??!1),(oi(a)||fm(a))&&oc(a),O1(e.path,a,"dir");let l=o().toISOString(),c={source:e.path,sourceType:"local",ref:null,commit:null,installedAt:l,updatedAt:l};return Is(i,c,r),{name:i,dir:a,entry:c,plugins:s.plugins.map(MT)}}async function N1(e,t,n,r,o,s){Zl(e.url);let i=t.name??U1(e);hn(i);let a=pm(n,i);Er(a,n),mm(a,t.force??!1),oi(a)&&oc(a);let l=e.type==="github"?`${e.owner}/${e.repo}`:e.url;await Ql(e.url,a,s);try{let c;if(t.ref)c=t.ref;else{let b=await Kn(a,s);c=qn(b)??await Vt(a,s)}(t.ref||await B1(a,c,s))&&await Gn(a,c,s);let u=await Jt(a,s),d=yn(a),p=i,f=a;if(!t.name&&d.name!==i){hn(d.name);let b=pm(n,d.name);Er(b,n),mm(b,t.force??!1),oi(b)&&oc(b),P1(a,b),p=d.name,f=b}let g=o().toISOString(),h={source:l,sourceType:e.type,ref:c,commit:u,installedAt:g,updatedAt:g};return Is(p,h,r),{name:p,dir:f,entry:h,plugins:d.plugins.map(MT)}}catch(c){try{oi(a)&&oc(a)}catch{}throw c}}async function B1(e,t,n){let r=await Vt(e,n);return t!==r}function U1(e){if(e.type==="github")return e.repo;let t=e.url.replace(/\.git$/,""),n=t.lastIndexOf("/"),r=t.lastIndexOf(":"),o=Math.max(n,r);return o>=0?t.slice(o+1):L1(t)}function mm(e,t){if(!(!oi(e)&&!fm(e))&&!t)throw new Error(`marketplace directory already exists: ${e} (re-run with --force to replace)`)}function fm(e){try{return $1(e).isSymbolicLink()}catch{return!1}}function oc(e){if(fm(e)){D1(e);return}M1(e,{recursive:!0,force:!0})}function MT(e){return e.description?{name:e.name,description:e.description}:{name:e.name}}W();import{existsSync as j1,lstatSync as W1,rmSync as H1,unlinkSync as K1}from"fs";import{join as G1}from"path";function ic(e,t={}){let n=t.cacheDir??Yt(),r=t.indexPath??ie(),o=G1(n,e),s=!1;q1(o)?(K1(o),s=!0):j1(o)&&(H1(o,{recursive:!0,force:!0}),s=!0);let i=fe(r),a=Object.prototype.hasOwnProperty.call(i.marketplaces,e),l=Object.entries(i.plugins).filter(([,c])=>c.marketplace===e).map(([c])=>c);return(a||l.length>0)&&qy(e,r),{name:e,removedDir:s,removedIndexEntry:a,removedPluginEntries:l}}function q1(e){try{return W1(e).isSymbolicLink()}catch{return!1}}W();import{existsSync as z1}from"fs";import{join as J1}from"path";async function si(e,t={},n={}){let r=n.indexPath??ie(),o=n.now??(()=>new Date),s=n.gitRunner?{runner:n.gitRunner}:{},a=fe(r).marketplaces[e];if(!a)throw new Error(`marketplace "${e}" is not installed`);let l=n.cacheDir?J1(n.cacheDir,e):ss(e);if(!z1(l))return{name:e,status:"missing-dir",dir:l};if(a.sourceType==="local")return{name:e,status:"skipped-local"};let c=new Set((PT(l)?.plugins??[]).map(y=>y.name));await ec(l,s);let u;if(t.ref)u=t.ref;else{let y=await Kn(l,s);u=qn(y)??a.ref??await Vt(l,s)}if(u===a.ref){let y=await Jt(l,s);return{name:e,status:"up-to-date",ref:u,commit:y}}await Gn(l,u,s);let d=await Jt(l,s),p=o().toISOString(),f={...a,ref:u,commit:d,updatedAt:p};Is(e,f,r);let g=new Set(yn(l).plugins.map(y=>y.name)),h=[...g].filter(y=>!c.has(y)),b=[...c].filter(y=>!g.has(y));return{name:e,status:"updated",fromRef:a.ref,toRef:u,commit:d,addedPlugins:h,removedPlugins:b}}async function OT(e={}){let t=e.indexPath??ie(),n=fe(t),r=[];for(let o of Object.keys(n.marketplaces))try{r.push(await si(o,{},e))}catch(s){let i=s instanceof Error?s.message:String(s);r.push({name:o,status:"missing-dir",dir:i})}return r}G();W();import{existsSync as ac,statSync as V1}from"fs";import{isAbsolute as Y1,join as X1,resolve as $T}from"path";async function qo(e,t,n={},r={}){let o=r.marketplaceDirFor??ss,s=r.indexPath??ie(),i=r.now??(()=>new Date),a=o(e);if(!ac(a)||!dm(a))throw new Error(`marketplace "${e}" is not installed (looked for manifest under ${a})`);let l=yn(a),c=l.plugins.find(u=>u.name===t);if(!c){let u=l.plugins.map(d=>d.name).join(", ")||"(none)";throw new Error(`marketplace "${e}" does not list a plugin named "${t}". Available: ${u}`)}return Z1(c.source)?Q1(e,c,a,s,i,n):ej(e,c,n,r)}function lc(e,t={}){let n=t.marketplaceDirFor??ss,r=t.indexPath??ie(),o=n(e);if(!ac(o)||!dm(o))throw new Error(`marketplace "${e}" is not installed`);let s=yn(o),i=fe(r);return s.plugins.map(a=>{let l=`${e}:${a.name}`,c=l in i.plugins||a.name in i.plugins&&i.plugins[a.name]?.marketplace===e,u={name:a.name,installed:c,key:l};return a.description&&(u.description=a.description),u})}function Z1(e){return e.startsWith("./")||e.startsWith("../")||e.startsWith("/")||e.startsWith("~")}function Q1(e,t,n,r,o,s){let i=t.source,a=Y1(i)||i.startsWith("~")?tj(i):$T(n,i);if(!ac(a))throw new Error(`marketplace "${e}" lists plugin "${t.name}" at ${a}, but that path does not exist on disk`);if(!V1(a).isDirectory())throw new Error(`marketplace "${e}" lists plugin "${t.name}" at ${a}, but that path is not a directory`);let c=X1(a,".claude-plugin","plugin.json");if(!ac(c))throw new Error(`marketplace "${e}" lists plugin "${t.name}" at ${a}, but no .claude-plugin/plugin.json was found`);let u=`${e}:${t.name}`,d=fe(r);if(!s.force&&u in d.plugins&&d.plugins[u]?.enabled)throw new Error(`plugin "${u}" is already installed (re-run with --force to overwrite)`);let p=o().toISOString(),f={source:`${e}:${t.name}`,sourceType:"marketplace",ref:null,commit:null,enabled:!0,installedAt:p,updatedAt:p,marketplace:e};return $n(u,f,r),{key:u,name:t.name,dir:a,entry:f}}async function ej(e,t,n,r){let o={name:t.name,...n.ref?{ref:n.ref}:{},...n.force?{force:!0}:{}},s=await rc(t.source,o,r),i=r.indexPath??ie(),a={...s.entry,marketplace:e};return $n(s.name,a,i),{key:s.name,name:s.name,dir:s.dir,entry:a}}function tj(e){if(e.startsWith("~")){let t=T.HOME??"";if(e==="~")return t;if(e.startsWith("~/"))return $T(t,e.slice(2))}return e}var DT=["add","plugins","install","remove","update"],nj={name:"/marketplaces",summary:"List installed plugin marketplaces",async handler(e){return oj(e),"continue"}},rj={name:"/marketplace",summary:"Manage plugin marketplaces (add | plugins | install | remove | update)",usage:"/marketplace <add|plugins|install|remove|update> [args]",async handler(e,t){let n=t.trim();if(!n)return sj(e),"continue";let[r,...o]=n.split(/\s+/);if(!r||!DT.includes(r))return e.out.error(`Unknown subcommand "${r??""}". Try one of: ${DT.join(", ")}.`),"continue";switch(r){case"add":return ij(e,o);case"plugins":return aj(e,o);case"install":return lj(e,o);case"remove":return cj(e,o);case"update":return uj(e,o);default:return"continue"}}};function LT(){ce(nj),ce(rj)}function oj(e){let t=fe(),n=Object.entries(t.marketplaces).sort(([r],[o])=>r.localeCompare(o));if(e.out.line(),n.length===0){e.out.line(m.dim(" No marketplaces installed.")),e.out.line(m.dim(" Try: /marketplace add anthropics/claude-plugins-official")),e.out.line();return}e.out.line(m.bold("Installed marketplaces:"));for(let[r,o]of n){let s=o.ref?m.brand(o.ref):m.dim("(local)");e.out.line(` ${m.bold(r.padEnd(28))} ${s.padEnd(12)} ${m.dim(o.source)}`)}e.out.line()}function sj(e){e.out.line(),e.out.line(m.bold("/marketplace usage:")),e.out.line(` ${m.brand("/marketplace add")} <git-url|owner/repo|local-path>`),e.out.line(` ${m.brand("/marketplace plugins")} <marketplace>`),e.out.line(` ${m.brand("/marketplace install")} <marketplace> <plugin>`),e.out.line(` ${m.brand("/marketplace remove")} <marketplace>`),e.out.line(` ${m.brand("/marketplace update")} [<marketplace>]`),e.out.line()}async function ij(e,t){if(t.length===0)return e.out.error("Usage: /marketplace add <source> [name]"),"continue";let[n,r,...o]=t;if(!n)return e.out.error("Usage: /marketplace add <source> [name]"),"continue";let s=dj(o);e.out.info(`Installing marketplace ${n}\u2026`);try{let i=await sc(n,{...r&&!r.startsWith("-")?{name:r}:{},...s.ref?{ref:s.ref}:{},...s.force?{force:!0}:{}});e.out.success(`Installed marketplace ${i.name} (${i.plugins.length} plugin(s) available).`),e.out.line(m.dim(` Next: /marketplace plugins ${i.name}`))}catch(i){e.out.error(`Install failed: ${cc(i)}`)}return"continue"}function aj(e,t){let n=t[0];if(!n)return e.out.error("Usage: /marketplace plugins <marketplace>"),"continue";try{let r=lc(n);if(e.out.line(),r.length===0)return e.out.line(m.dim(` Marketplace "${n}" lists no plugins.`)),e.out.line(),"continue";e.out.line(m.bold(`Plugins in ${n}:`)),r.forEach((o,s)=>{let i=o.installed?m.brand("[\u2713]"):m.dim("[ ]"),a=o.description?m.dim(` \u2014 ${o.description}`):"";e.out.line(` ${i} ${m.bold(String(s+1).padStart(2))}. ${m.bold(o.name)}${a}`)}),e.out.line(),e.out.line(m.dim(` Install one: /marketplace install ${n} <plugin>`)),e.out.line()}catch(r){e.out.error(`List failed: ${cc(r)}`)}return"continue"}async function lj(e,t){let n,r;if(t.length===1&&t[0]?.includes(":")){let o=t[0].split(":");o.length===2&&([n,r]=o)}else[n,r]=t;if(!n||!r)return e.out.error("Usage: /marketplace install <marketplace> <plugin>"),"continue";e.out.info(`Installing ${n}:${r}\u2026`);try{let o=await qo(n,r);e.out.success(`Installed ${o.key}.`),e.out.line(m.dim(" Run /reload-plugins to refresh this session's slash commands."))}catch(o){e.out.error(`Install failed: ${cc(o)}`)}return"continue"}function cj(e,t){let n=t[0];if(!n)return e.out.error("Usage: /marketplace remove <marketplace>"),"continue";let r=ic(n);if(!r.removedDir&&!r.removedIndexEntry&&r.removedPluginEntries.length===0)return e.out.line(m.dim(` No marketplace named "${n}" to remove.`)),"continue";let o=r.removedPluginEntries.length>0?` + ${r.removedPluginEntries.length} plugin(s)`:"";return e.out.success(`Removed marketplace ${n}${o}.`),"continue"}async function uj(e,t){let n=t[0];if(!n)return e.out.error("Usage: /marketplace update <marketplace>"),"continue";e.out.info(`Updating ${n}\u2026`);try{let r=await si(n);switch(r.status){case"updated":{let o=r.addedPlugins.length>0?` +${r.addedPlugins.join(", ")}`:"",s=r.removedPlugins.length>0?` -${r.removedPlugins.join(", ")}`:"";e.out.success(`Updated ${n}: ${r.fromRef??"(none)"} \u2192 ${r.toRef}${o}${s}`);break}case"up-to-date":e.out.info(`${n} is up-to-date (${r.ref}).`);break;case"skipped-local":e.out.info(`${n} skipped (local source).`);break;case"missing-dir":e.out.warn(`${n}: marketplace dir missing (${r.dir}).`);break}}catch(r){e.out.error(`Update failed: ${cc(r)}`)}return"continue"}function dj(e){let t={};for(let n=0;n<e.length;n+=1){let r=e[n];r==="-f"||r==="--force"?t.force=!0:(r==="-r"||r==="--ref")&&e[n+1]&&(t.ref=e[n+1],n+=1)}return t}function cc(e){return e instanceof Error?e.message:String(e)}$v("shadow-verify",{glyph:"\u25C8",color:"#7B5EA7",inFlightVerb:"verifying\u2026"});function FT(){ES();for(let e of PS)ce(e);for(let e of US)ce(e);ce(WS),ce(ZS),ce(QS),ce(ek),ce(ik),ce(lk);for(let e of pk)ce(e);ce(gk),ce(bk),ce(Sk),ce(Mv),ce(Fv),ce(Nv),ce(Uv),ce(zv),ce(Jv),ce(Xv),wT(),xS(Wv),bT(),eT(),LT()}function NT(e,t={}){if(!e.isTTY)return{writeLine:o=>{e.write(o+`
2156
2156
  `)},setCompositor:()=>{}};let n=null,r=t.statusLine;return{writeLine(o){if(n?.isArmed()){n.commitAbove(o);return}r?r.withFullScrollRegion(()=>{e.write(o+`
2157
2157
  `)}):e.write(o+`
2158
- `)},setCompositor(o){n=o}}}var uc=class{entries=new Map;record(t){let n=this.entries.get(t.skillName);n?(n.runs+=1,n.totalDurationMs+=t.durationMs,t.claimsTotal!==void 0&&(n.totalClaims=(n.totalClaims??0)+t.claimsTotal),t.claimsConfirmed!==void 0&&(n.totalConfirmed=(n.totalConfirmed??0)+t.claimsConfirmed),t.claimsRefuted!==void 0&&(n.totalRefuted=(n.totalRefuted??0)+t.claimsRefuted),t.claimsInconclusive!==void 0&&(n.totalInconclusive=(n.totalInconclusive??0)+t.claimsInconclusive)):this.entries.set(t.skillName,{skillName:t.skillName,runs:1,totalDurationMs:t.durationMs,...t.claimsTotal!==void 0?{totalClaims:t.claimsTotal}:{},...t.claimsConfirmed!==void 0?{totalConfirmed:t.claimsConfirmed}:{},...t.claimsRefuted!==void 0?{totalRefuted:t.claimsRefuted}:{},...t.claimsInconclusive!==void 0?{totalInconclusive:t.claimsInconclusive}:{}})}summary(){return this.entries.size===0?null:new Map(this.entries)}clear(){this.entries=new Map}};var dc=class{source;sampleEveryNTurns;cachedRatio;cachedDetail;inFlight=null;disposed=!1;generation=0;constructor(t,n={}){this.source=t,this.sampleEveryNTurns=n.sampleEveryNTurns??3}attach(t){return this.source=t,this.generation+=1,this.reset(),this}getRatio(){return this.cachedRatio}getDetail(){return this.cachedDetail}async refresh(){if(this.disposed)return;if(this.inFlight)return this.inFlight;let t=this.doFetch().finally(()=>{this.inFlight=null});return this.inFlight=t,t}async onTurn(t){this.disposed||t%this.sampleEveryNTurns===1&&await this.refresh()}reset(){this.cachedRatio=void 0,this.cachedDetail=void 0,this.inFlight=null}dispose(){this.disposed=!0}async doFetch(){let t=this.generation;try{let n=await this.source.getContextUsage();if(this.generation!==t)return;let r=p1(n.apiUsage),o=n.maxTokens??0,s=n.percentage;typeof s=="number"&&(this.cachedRatio=Math.min(1,Math.max(0,s/100)),this.cachedDetail={used:r,limit:o,percentage:s})}catch{}}};function p1(e){return e?e.input_tokens+e.output_tokens+e.cache_creation_input_tokens+e.cache_read_input_tokens:0}import m1 from"@anthropic-ai/sdk";import{randomUUID as BT}from"node:crypto";async function pc(e){let{token:t,model:n,system:r,user:o,maxTokens:s=64,signal:i,clientFactory:a}=e;if(!t)throw new Error("oneShotCompletion: token required");let l=zi(t),c=ms(t,l),u=a?a(c):new m1(c),d=BT(),m=BT(),f=Lt(l,d,m),g=dn(n)??n,h={};Object.keys(f).length>0&&(h.headers=f),i&&(h.signal=i);let b=await u.messages.create({model:g,max_tokens:s,system:r,messages:[{role:"user",content:o}]},Object.keys(h).length>0?h:void 0),y=[];for(let x of b.content)x.type==="text"&&y.push(x.text);let w=y.join("").trim();return w.length===0&&console.warn("oneShotCompletion: response contained no text blocks \u2014 returning empty string"),w}var f1="claude-haiku-4-5",g1=15e3,h1=1e3,y1=80,b1=200,w1=3e3,S1='Summarize what this background subagent is currently doing in \u226480 tokens. Be concrete: name specific tools used, files examined, decisions made. Avoid filler ("appears to be working on\u2026").';function k1(e){return e.replace(/\bauthorization:\s*bearer\s+\S+/gi,"Authorization: Bearer [REDACTED]").replace(/\bsk-ant-[A-Za-z0-9_-]{20,}/g,"[REDACTED]").replace(/\beyJ[A-Za-z0-9_-]+\.eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+/g,"[REDACTED]").replace(/\b(?:AKIA|ASIA|AROA|AIDA|AGPA|AIPA|ANPA|ANVA|APKA|ABIA|ACCA)[A-Z0-9]{16}\b/g,"[REDACTED]").replace(/(?<![/.\w])[A-Za-z0-9+/=_-]{32,}(?![/.\w])/g,"[REDACTED]")}var mc=class{registry;apiKey;model;intervalMs;maxInputTokens;maxOutputTokens;maxCallsPerSession;callLLM;getTranscriptFn;summaries=new Map;jobIndexMap=new Map;jobIndexCounter=0;callsThisSession=0;lastRefreshedAt=new Map;abortController=new AbortController;tickTimer;tickIntervalMs;constructor(t){this.registry=t.registry,this.apiKey=t.apiKey,this.model=t.model??f1,this.intervalMs=t.intervalMs??g1,this.maxInputTokens=t.maxInputTokens??h1,this.maxOutputTokens=t.maxOutputTokens??y1,this.maxCallsPerSession=t.maxCallsPerSession??b1,this.tickIntervalMs=Math.max(1e3,Math.floor(this.intervalMs/10)),t.callLLM!==void 0?this.callLLM=t.callLLM:this.callLLM=(n,r)=>pc({token:this.apiKey,model:this.model,system:S1,user:n,maxTokens:this.maxOutputTokens,signal:r}),this.getTranscriptFn=t.getTranscript??(n=>this.registry.getTranscript(n)),this.registry.on("started",n=>{this.jobIndexMap.set(n.jobId,this.jobIndexCounter++)}),this.registry.on("settled",n=>{this.summaries.delete(n.jobId),this.jobIndexMap.delete(n.jobId),this.lastRefreshedAt.delete(n.jobId)})}start(){this.tickTimer===void 0&&(this.abortController=new AbortController,this.tickTimer=setInterval(()=>{this.tick()},this.tickIntervalMs))}stop(){this.tickTimer!==void 0&&(clearInterval(this.tickTimer),this.tickTimer=void 0),this.abortController.abort()}getSummary(t){return this.summaries.get(t)}async tick(){let t=Date.now(),n=this.registry.list().filter(o=>o.status==="running"),r=[];for(let o of n){let i=(this.jobIndexMap.get(o.jobId)??0)*w1%this.intervalMs,a=this.intervalMs-1e3-i,l=this.lastRefreshedAt.get(o.jobId)??0;if(!(t-l<a)){if(this.callsThisSession>=this.maxCallsPerSession){J(`[BackgroundSummarizer] budget cap (${this.callsThisSession}/${this.maxCallsPerSession}) \u2014 skipping ${o.jobId}`);continue}this.callsThisSession++,r.push(o.jobId)}}await Promise.allSettled(r.map(o=>this.refreshJob(o,t)))}async refreshJob(t,n){let r=!1;try{let o=this.getTranscriptFn(t);if(o===void 0||o.trim().length===0)return;let s=this.maxInputTokens*4,i=o.length>s?o.slice(-s):o,l=`Transcript tail:
2158
+ `)},setCompositor(o){n=o}}}var uc=class{entries=new Map;record(t){let n=this.entries.get(t.skillName);n?(n.runs+=1,n.totalDurationMs+=t.durationMs,t.claimsTotal!==void 0&&(n.totalClaims=(n.totalClaims??0)+t.claimsTotal),t.claimsConfirmed!==void 0&&(n.totalConfirmed=(n.totalConfirmed??0)+t.claimsConfirmed),t.claimsRefuted!==void 0&&(n.totalRefuted=(n.totalRefuted??0)+t.claimsRefuted),t.claimsInconclusive!==void 0&&(n.totalInconclusive=(n.totalInconclusive??0)+t.claimsInconclusive)):this.entries.set(t.skillName,{skillName:t.skillName,runs:1,totalDurationMs:t.durationMs,...t.claimsTotal!==void 0?{totalClaims:t.claimsTotal}:{},...t.claimsConfirmed!==void 0?{totalConfirmed:t.claimsConfirmed}:{},...t.claimsRefuted!==void 0?{totalRefuted:t.claimsRefuted}:{},...t.claimsInconclusive!==void 0?{totalInconclusive:t.claimsInconclusive}:{}})}summary(){return this.entries.size===0?null:new Map(this.entries)}clear(){this.entries=new Map}};var dc=class{source;sampleEveryNTurns;cachedRatio;cachedDetail;inFlight=null;disposed=!1;generation=0;constructor(t,n={}){this.source=t,this.sampleEveryNTurns=n.sampleEveryNTurns??3}attach(t){return this.source=t,this.generation+=1,this.reset(),this}getRatio(){return this.cachedRatio}getDetail(){return this.cachedDetail}async refresh(){if(this.disposed)return;if(this.inFlight)return this.inFlight;let t=this.doFetch().finally(()=>{this.inFlight=null});return this.inFlight=t,t}async onTurn(t){this.disposed||t%this.sampleEveryNTurns===1&&await this.refresh()}reset(){this.cachedRatio=void 0,this.cachedDetail=void 0,this.inFlight=null}dispose(){this.disposed=!0}async doFetch(){let t=this.generation;try{let n=await this.source.getContextUsage();if(this.generation!==t)return;let r=pj(n.apiUsage),o=n.maxTokens??0,s=n.percentage;typeof s=="number"&&(this.cachedRatio=Math.min(1,Math.max(0,s/100)),this.cachedDetail={used:r,limit:o,percentage:s})}catch{}}};function pj(e){return e?e.input_tokens+e.output_tokens+e.cache_creation_input_tokens+e.cache_read_input_tokens:0}import mj from"@anthropic-ai/sdk";import{randomUUID as BT}from"node:crypto";async function pc(e){let{token:t,model:n,system:r,user:o,maxTokens:s=64,signal:i,clientFactory:a}=e;if(!t)throw new Error("oneShotCompletion: token required");let l=zi(t),c=ms(t,l),u=a?a(c):new mj(c),d=BT(),p=BT(),f=Lt(l,d,p),g=dn(n)??n,h={};Object.keys(f).length>0&&(h.headers=f),i&&(h.signal=i);let b=await u.messages.create({model:g,max_tokens:s,system:r,messages:[{role:"user",content:o}]},Object.keys(h).length>0?h:void 0),y=[];for(let x of b.content)x.type==="text"&&y.push(x.text);let w=y.join("").trim();return w.length===0&&console.warn("oneShotCompletion: response contained no text blocks \u2014 returning empty string"),w}var fj="claude-haiku-4-5",gj=15e3,hj=1e3,yj=80,bj=200,wj=3e3,Sj='Summarize what this background subagent is currently doing in \u226480 tokens. Be concrete: name specific tools used, files examined, decisions made. Avoid filler ("appears to be working on\u2026").';function kj(e){return e.replace(/\bauthorization:\s*bearer\s+\S+/gi,"Authorization: Bearer [REDACTED]").replace(/\bsk-ant-[A-Za-z0-9_-]{20,}/g,"[REDACTED]").replace(/\beyJ[A-Za-z0-9_-]+\.eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+/g,"[REDACTED]").replace(/\b(?:AKIA|ASIA|AROA|AIDA|AGPA|AIPA|ANPA|ANVA|APKA|ABIA|ACCA)[A-Z0-9]{16}\b/g,"[REDACTED]").replace(/(?<![/.\w])[A-Za-z0-9+/=_-]{32,}(?![/.\w])/g,"[REDACTED]")}var mc=class{registry;apiKey;model;intervalMs;maxInputTokens;maxOutputTokens;maxCallsPerSession;callLLM;getTranscriptFn;summaries=new Map;jobIndexMap=new Map;jobIndexCounter=0;callsThisSession=0;lastRefreshedAt=new Map;abortController=new AbortController;tickTimer;tickIntervalMs;constructor(t){this.registry=t.registry,this.apiKey=t.apiKey,this.model=t.model??fj,this.intervalMs=t.intervalMs??gj,this.maxInputTokens=t.maxInputTokens??hj,this.maxOutputTokens=t.maxOutputTokens??yj,this.maxCallsPerSession=t.maxCallsPerSession??bj,this.tickIntervalMs=Math.max(1e3,Math.floor(this.intervalMs/10)),t.callLLM!==void 0?this.callLLM=t.callLLM:this.callLLM=(n,r)=>pc({token:this.apiKey,model:this.model,system:Sj,user:n,maxTokens:this.maxOutputTokens,signal:r}),this.getTranscriptFn=t.getTranscript??(n=>this.registry.getTranscript(n)),this.registry.on("started",n=>{this.jobIndexMap.set(n.jobId,this.jobIndexCounter++)}),this.registry.on("settled",n=>{this.summaries.delete(n.jobId),this.jobIndexMap.delete(n.jobId),this.lastRefreshedAt.delete(n.jobId)})}start(){this.tickTimer===void 0&&(this.abortController=new AbortController,this.tickTimer=setInterval(()=>{this.tick()},this.tickIntervalMs))}stop(){this.tickTimer!==void 0&&(clearInterval(this.tickTimer),this.tickTimer=void 0),this.abortController.abort()}getSummary(t){return this.summaries.get(t)}async tick(){let t=Date.now(),n=this.registry.list().filter(o=>o.status==="running"),r=[];for(let o of n){let i=(this.jobIndexMap.get(o.jobId)??0)*wj%this.intervalMs,a=this.intervalMs-1e3-i,l=this.lastRefreshedAt.get(o.jobId)??0;if(!(t-l<a)){if(this.callsThisSession>=this.maxCallsPerSession){J(`[BackgroundSummarizer] budget cap (${this.callsThisSession}/${this.maxCallsPerSession}) \u2014 skipping ${o.jobId}`);continue}this.callsThisSession++,r.push(o.jobId)}}await Promise.allSettled(r.map(o=>this.refreshJob(o,t)))}async refreshJob(t,n){let r=!1;try{let o=this.getTranscriptFn(t);if(o===void 0||o.trim().length===0)return;let s=this.maxInputTokens*4,i=o.length>s?o.slice(-s):o,l=`Transcript tail:
2159
2159
  <transcript>
2160
- ${k1(i)}
2161
- </transcript>`;this.lastRefreshedAt.set(t,n);let c=await this.callLLM(l,this.abortController.signal);this.summaries.set(t,{text:c.trim(),refreshedAt:n,stale:!1}),r=!0}catch(o){if(J(`[BackgroundSummarizer] Haiku call failed for ${t}:`,o),!this.abortController.signal.aborted){let s=this.summaries.get(t);s!==void 0&&this.summaries.set(t,{...s,stale:!0})}}finally{r||this.callsThisSession--}}};import{Client as jT}from"@modelcontextprotocol/sdk/client/index.js";import{CallToolResultSchema as _1}from"@modelcontextprotocol/sdk/types.js";import{StreamableHTTPError as KT}from"@modelcontextprotocol/sdk/client/streamableHttp.js";import{UnauthorizedError as ym}from"@modelcontextprotocol/sdk/client/auth.js";import{StdioClientTransport as T1}from"@modelcontextprotocol/sdk/client/stdio.js";import{StreamableHTTPClientTransport as x1}from"@modelcontextprotocol/sdk/client/streamableHttp.js";import{SSEClientTransport as E1}from"@modelcontextprotocol/sdk/client/sse.js";var v1=/\$(\$)?\{([A-Z_][A-Z0-9_]*)\}/gi;function fc(e,t=process.env){let n=[];return{value:e.replace(v1,(o,s,i)=>{if(s==="$")return`\${${i}}`;let a=t[i];return a===void 0||a===""?(n.push(i),""):a}),missing:n}}function gm(e,t=process.env){if(e===void 0)return{value:{},missing:[]};let n={},r=new Set;for(let[o,s]of Object.entries(e)){let{value:i,missing:a}=fc(s,t);n[o]=i;for(let l of a)r.add(l)}return{value:n,missing:[...r]}}function R1(e){return e==="localhost"||e==="127.0.0.1"||e==="[::1]"||e==="::1"||e==="0.0.0.0"}function A1(){let e=["PATH","HOME","USER","USERNAME","LOGNAME","SHELL","TERM","TMPDIR","TEMP","TMP","LANG","LC_ALL","SYSTEMROOT","APPDATA","LOCALAPPDATA","PROGRAMDATA","PROGRAMFILES","NODE_PATH"],t={};for(let n of e){let r=process.env[n];typeof r=="string"&&(t[n]=r)}return t}function UT(e,t=process.env){if(e===void 0)return{headers:{},missing:[]};let n={},r=new Set;for(let[o,s]of Object.entries(e)){let{value:i,missing:a}=fc(s,t);if(a.length>0)for(let l of a)r.add(l);else n[o]=i}return{headers:n,missing:[...r]}}function gc(e,t,n){let r=t.type??(t.command?"stdio":"streamable-http");if(r==="stdio"){if(typeof t.command!="string"||t.command.length===0)throw new Error(`McpTransport(${e}): stdio requires \`command\``);let{value:o,missing:s}=gm(t.env);s.length>0&&console.warn(`[mcp:${e}] missing env vars (passing as empty): ${s.join(", ")}`);let i={command:t.command,...t.args?{args:t.args}:{},env:{...A1(),...o}};return{transport:new T1(i),isSSE:!1}}if(r==="streamable-http"||r==="sse"){if(typeof t.url!="string"||t.url.length===0)throw new Error(`McpTransport(${e}): ${r} requires \`url\``);let o=new URL(t.url);if(o.protocol!=="https:"&&!R1(o.hostname))throw new Error(`McpTransport(${e}): refusing ${r} URL ${o.protocol}//${o.hostname} \u2014 credentials and tool I/O would transit in plaintext. Use https:, or point the URL at localhost / 127.0.0.1.`);let{headers:s,missing:i}=UT(t.headers);return i.length>0&&console.warn(`[mcp:${e}] missing header vars (passing as omitted): ${i.join(", ")}`),r==="sse"?(process.stderr.write(`[mcp:${e}] WARNING: SSE transport is deprecated. Upgrade your MCP server to use streamable-HTTP.
2162
- `),{transport:new E1(o,{...Object.keys(s).length>0?{requestInit:{headers:s}}:{},...n?{authProvider:n}:{}}),isSSE:!0}):{transport:new x1(o,{...Object.keys(s).length>0?{requestInit:{headers:s}}:{},...n?{authProvider:n}:{}}),isSSE:!1}}throw new Error(`McpTransport(${e}): unknown transport type "${String(r)}"`)}ul();var WT={name:"agent-afk",version:"2.x"},HT={},hm=3e4,zo=class{serverName;config;client;connected=!1;pendingAuthTransport;onTransportError;onToolListChanged;constructor(t,n){this.serverName=t,this.config=n}async connect(){if(this.connected)throw new Error(`McpClient(${this.serverName}): already connected`);let t=this.config.oauth===!0?new Co(this.serverName):void 0,{primary:n,fallback:r}=I1(this.serverName,this.config,t),o=new jT(WT,{capabilities:HT});n.transport.onerror=u=>{this.onTransportError?.(u)};try{let{ToolListChangedNotificationSchema:u}=await import("@modelcontextprotocol/sdk/types.js");o.setNotificationHandler(u,()=>{this.onToolListChanged?.()})}catch{}let s=this.config.timeout??hm,i=n.isSSE,a=o;try{await hc(o.connect(n.transport),s,()=>new Error(`MCP server "${this.serverName}" connect timed out after ${s}ms`),()=>n.transport.close().catch(()=>{}))}catch(u){if(P1(u)&&r!==null){console.warn(`[mcp:${this.serverName}] streamable-HTTP got ${M1(u)}; falling back to SSE transport`);let d=r();d.transport.onerror=f=>{this.onTransportError?.(f)};let m=new jT(WT,{capabilities:HT});try{let{ToolListChangedNotificationSchema:f}=await import("@modelcontextprotocol/sdk/types.js");m.setNotificationHandler(f,()=>{this.onToolListChanged?.()})}catch{}await hc(m.connect(d.transport),s,()=>new Error(`MCP server "${this.serverName}" (SSE fallback) connect timed out after ${s}ms`),()=>d.transport.close().catch(()=>{})),a=m,i=!0}else throw u instanceof ym&&(this.pendingAuthTransport=n.transport),u}i&&!n.isSSE&&process.stderr.write(`[mcp:${this.serverName}] WARNING: connected via deprecated SSE transport. Upgrade your MCP server to streamable-HTTP.
2163
- `),this.client=a,this.connected=!0,this.pendingAuthTransport=void 0;let l=await hc(this.client.listTools(),s,()=>new Error(`MCP server "${this.serverName}" listTools timed out after ${s}ms`),()=>a.close().catch(()=>{})),c=this.client.getServerVersion();return{tools:l.tools,serverInfo:c?{name:c.name,version:c.version}:void 0}}async listTools(){if(!this.client)throw new Error(`McpClient(${this.serverName}): not connected`);let t=this.config.timeout??hm;return(await hc(this.client.listTools(),t,()=>new Error(`MCP server "${this.serverName}" listTools timed out after ${t}ms`))).tools}async refreshTools(){return this.listTools()}async callTool(t,n,r){if(!this.client)return{content:`MCP server "${this.serverName}" is not connected`,isError:!0};if(r.aborted)return{content:"Tool call aborted",isError:!0};let o=this.config.timeout??hm,s;try{s=await this.client.callTool({name:t,arguments:n??{}},_1,{signal:r,timeout:o})}catch(i){let a=i instanceof Error?i.message:String(i);return{content:`MCP tool "${this.serverName}.${t}" failed: ${a}`,isError:!0}}return C1(s)}async finishAuth(t){if(!this.pendingAuthTransport)throw new Error(`McpClient(${this.serverName}): no pending OAuth transport \u2014 server is not in oauth_pending state`);await this.pendingAuthTransport.finishAuth(t)}async disconnect(){if(this.pendingAuthTransport=void 0,!this.client)return;let t=this.client;this.client=void 0,this.connected=!1;try{await t.close()}catch{}}};function C1(e){let t=[];for(let r of e.content??[])if(r.type==="text")t.push(r.text);else if(r.type==="image")t.push(`[image block: mimeType=${r.mimeType}, ${r.data.length} bytes base64]`);else if(r.type==="resource"){let o="resource"in r&&typeof r.resource=="object"?r.resource.uri??"(unknown)":"(unknown)";t.push(`[resource block: ${o}]`)}else t.push(`[unknown block: ${JSON.stringify(r)}]`);let n=t.join(`
2164
- `);return{content:n.length===0?"(empty tool result)":n,...e.isError?{isError:!0}:{}}}function I1(e,t,n){let r=t.type??(t.command?"stdio":"streamable-http");return{primary:gc(e,t,n),fallback:r==="streamable-http"?()=>gc(e,{...t,type:"sse"},n):null}}function P1(e){return e instanceof KT&&(e.code===404||e.code===405)}function M1(e){return e instanceof KT?e.code:void 0}function hc(e,t,n,r){let o=null,s=new Promise((i,a)=>{o=setTimeout(()=>{if(r!==void 0)try{let l=r();l&&typeof l.then=="function"&&l.catch(()=>{})}catch{}a(n())},t)});return Promise.race([e,s]).finally(()=>{o!==null&&clearTimeout(o)})}import{createHash as O1}from"node:crypto";var GT="mcp__",qT="__",bm=64,$1=6;function ii(e){if(e.length===0)return"_";let n=e.replace(/[^a-zA-Z0-9_-]/g,"_").replace(/_{2,}/g,"_");return n.length===0?"_":n}function D1(e){return O1("sha256").update(e).digest("hex").slice(0,$1)}function zT(e,t){let n=ii(e),r=ii(t),o=`${GT}${n}${qT}${r}`;if(o.length<=bm)return o;let i=`${`${GT}${D1(e)}${qT}`}${r}`;return i.length<=bm?i:i.slice(0,bm)}function ai(e){let t=new Map,n=new Map;for(let{serverName:o,toolNames:s}of e)for(let i of s){let a=zT(o,i),l={serverName:o,originalToolName:i},c=t.get(a);if(c===void 0){t.set(a,l);continue}if(c.serverName===o&&c.originalToolName===i)continue;let u=n.get(a)??[c];u.push(l),n.set(a,u)}let r=[];for(let[o,s]of n)r.push({wireName:o,pairs:s});return{tools:t,conflicts:r}}var li=class e{records;nameRegistry;onToolsRefreshed;constructor(t){this.records=t;let n=[];for(let[o,s]of t)s.state.status==="connected"&&n.push({serverName:o,toolNames:s.tools.map(i=>i.name)});let r=ai(n);if(r.conflicts.length>0){let o=r.conflicts.map(s=>{let i=s.pairs.map(a=>`${a.serverName}.${a.originalToolName}`).join(", ");return` ${s.wireName} \u2190 ${i}`});throw new Error(`MCP tool name conflicts (rename one of the servers in mcp.json):
2160
+ ${kj(i)}
2161
+ </transcript>`;this.lastRefreshedAt.set(t,n);let c=await this.callLLM(l,this.abortController.signal);this.summaries.set(t,{text:c.trim(),refreshedAt:n,stale:!1}),r=!0}catch(o){if(J(`[BackgroundSummarizer] Haiku call failed for ${t}:`,o),!this.abortController.signal.aborted){let s=this.summaries.get(t);s!==void 0&&this.summaries.set(t,{...s,stale:!0})}}finally{r||this.callsThisSession--}}};import{Client as jT}from"@modelcontextprotocol/sdk/client/index.js";import{CallToolResultSchema as _j}from"@modelcontextprotocol/sdk/types.js";import{StreamableHTTPError as KT}from"@modelcontextprotocol/sdk/client/streamableHttp.js";import{UnauthorizedError as ym}from"@modelcontextprotocol/sdk/client/auth.js";import{StdioClientTransport as Tj}from"@modelcontextprotocol/sdk/client/stdio.js";import{StreamableHTTPClientTransport as xj}from"@modelcontextprotocol/sdk/client/streamableHttp.js";import{SSEClientTransport as Ej}from"@modelcontextprotocol/sdk/client/sse.js";var vj=/\$(\$)?\{([A-Z_][A-Z0-9_]*)\}/gi;function fc(e,t=process.env){let n=[];return{value:e.replace(vj,(o,s,i)=>{if(s==="$")return`\${${i}}`;let a=t[i];return a===void 0||a===""?(n.push(i),""):a}),missing:n}}function gm(e,t=process.env){if(e===void 0)return{value:{},missing:[]};let n={},r=new Set;for(let[o,s]of Object.entries(e)){let{value:i,missing:a}=fc(s,t);n[o]=i;for(let l of a)r.add(l)}return{value:n,missing:[...r]}}function Rj(e){return e==="localhost"||e==="127.0.0.1"||e==="[::1]"||e==="::1"||e==="0.0.0.0"}function Aj(){let e=["PATH","HOME","USER","USERNAME","LOGNAME","SHELL","TERM","TMPDIR","TEMP","TMP","LANG","LC_ALL","SYSTEMROOT","APPDATA","LOCALAPPDATA","PROGRAMDATA","PROGRAMFILES","NODE_PATH"],t={};for(let n of e){let r=process.env[n];typeof r=="string"&&(t[n]=r)}return t}function UT(e,t=process.env){if(e===void 0)return{headers:{},missing:[]};let n={},r=new Set;for(let[o,s]of Object.entries(e)){let{value:i,missing:a}=fc(s,t);if(a.length>0)for(let l of a)r.add(l);else n[o]=i}return{headers:n,missing:[...r]}}function gc(e,t,n){let r=t.type??(t.command?"stdio":"streamable-http");if(r==="stdio"){if(typeof t.command!="string"||t.command.length===0)throw new Error(`McpTransport(${e}): stdio requires \`command\``);let{value:o,missing:s}=gm(t.env);s.length>0&&console.warn(`[mcp:${e}] missing env vars (passing as empty): ${s.join(", ")}`);let i={command:t.command,...t.args?{args:t.args}:{},env:{...Aj(),...o}};return{transport:new Tj(i),isSSE:!1}}if(r==="streamable-http"||r==="sse"){if(typeof t.url!="string"||t.url.length===0)throw new Error(`McpTransport(${e}): ${r} requires \`url\``);let o=new URL(t.url);if(o.protocol!=="https:"&&!Rj(o.hostname))throw new Error(`McpTransport(${e}): refusing ${r} URL ${o.protocol}//${o.hostname} \u2014 credentials and tool I/O would transit in plaintext. Use https:, or point the URL at localhost / 127.0.0.1.`);let{headers:s,missing:i}=UT(t.headers);return i.length>0&&console.warn(`[mcp:${e}] missing header vars (passing as omitted): ${i.join(", ")}`),r==="sse"?(process.stderr.write(`[mcp:${e}] WARNING: SSE transport is deprecated. Upgrade your MCP server to use streamable-HTTP.
2162
+ `),{transport:new Ej(o,{...Object.keys(s).length>0?{requestInit:{headers:s}}:{},...n?{authProvider:n}:{}}),isSSE:!0}):{transport:new xj(o,{...Object.keys(s).length>0?{requestInit:{headers:s}}:{},...n?{authProvider:n}:{}}),isSSE:!1}}throw new Error(`McpTransport(${e}): unknown transport type "${String(r)}"`)}ul();var WT={name:"agent-afk",version:"2.x"},HT={},hm=3e4,zo=class{serverName;config;client;connected=!1;pendingAuthTransport;onTransportError;onToolListChanged;constructor(t,n){this.serverName=t,this.config=n}async connect(){if(this.connected)throw new Error(`McpClient(${this.serverName}): already connected`);let t=this.config.oauth===!0?new Co(this.serverName):void 0,{primary:n,fallback:r}=Ij(this.serverName,this.config,t),o=new jT(WT,{capabilities:HT});n.transport.onerror=u=>{this.onTransportError?.(u)};try{let{ToolListChangedNotificationSchema:u}=await import("@modelcontextprotocol/sdk/types.js");o.setNotificationHandler(u,()=>{this.onToolListChanged?.()})}catch{}let s=this.config.timeout??hm,i=n.isSSE,a=o;try{await hc(o.connect(n.transport),s,()=>new Error(`MCP server "${this.serverName}" connect timed out after ${s}ms`),()=>n.transport.close().catch(()=>{}))}catch(u){if(Pj(u)&&r!==null){console.warn(`[mcp:${this.serverName}] streamable-HTTP got ${Mj(u)}; falling back to SSE transport`);let d=r();d.transport.onerror=f=>{this.onTransportError?.(f)};let p=new jT(WT,{capabilities:HT});try{let{ToolListChangedNotificationSchema:f}=await import("@modelcontextprotocol/sdk/types.js");p.setNotificationHandler(f,()=>{this.onToolListChanged?.()})}catch{}await hc(p.connect(d.transport),s,()=>new Error(`MCP server "${this.serverName}" (SSE fallback) connect timed out after ${s}ms`),()=>d.transport.close().catch(()=>{})),a=p,i=!0}else throw u instanceof ym&&(this.pendingAuthTransport=n.transport),u}i&&!n.isSSE&&process.stderr.write(`[mcp:${this.serverName}] WARNING: connected via deprecated SSE transport. Upgrade your MCP server to streamable-HTTP.
2163
+ `),this.client=a,this.connected=!0,this.pendingAuthTransport=void 0;let l=await hc(this.client.listTools(),s,()=>new Error(`MCP server "${this.serverName}" listTools timed out after ${s}ms`),()=>a.close().catch(()=>{})),c=this.client.getServerVersion();return{tools:l.tools,serverInfo:c?{name:c.name,version:c.version}:void 0}}async listTools(){if(!this.client)throw new Error(`McpClient(${this.serverName}): not connected`);let t=this.config.timeout??hm;return(await hc(this.client.listTools(),t,()=>new Error(`MCP server "${this.serverName}" listTools timed out after ${t}ms`))).tools}async refreshTools(){return this.listTools()}async callTool(t,n,r){if(!this.client)return{content:`MCP server "${this.serverName}" is not connected`,isError:!0};if(r.aborted)return{content:"Tool call aborted",isError:!0};let o=this.config.timeout??hm,s;try{s=await this.client.callTool({name:t,arguments:n??{}},_j,{signal:r,timeout:o})}catch(i){let a=i instanceof Error?i.message:String(i);return{content:`MCP tool "${this.serverName}.${t}" failed: ${a}`,isError:!0}}return Cj(s)}async finishAuth(t){if(!this.pendingAuthTransport)throw new Error(`McpClient(${this.serverName}): no pending OAuth transport \u2014 server is not in oauth_pending state`);await this.pendingAuthTransport.finishAuth(t)}async disconnect(){if(this.pendingAuthTransport=void 0,!this.client)return;let t=this.client;this.client=void 0,this.connected=!1;try{await t.close()}catch{}}};function Cj(e){let t=[];for(let r of e.content??[])if(r.type==="text")t.push(r.text);else if(r.type==="image")t.push(`[image block: mimeType=${r.mimeType}, ${r.data.length} bytes base64]`);else if(r.type==="resource"){let o="resource"in r&&typeof r.resource=="object"?r.resource.uri??"(unknown)":"(unknown)";t.push(`[resource block: ${o}]`)}else t.push(`[unknown block: ${JSON.stringify(r)}]`);let n=t.join(`
2164
+ `);return{content:n.length===0?"(empty tool result)":n,...e.isError?{isError:!0}:{}}}function Ij(e,t,n){let r=t.type??(t.command?"stdio":"streamable-http");return{primary:gc(e,t,n),fallback:r==="streamable-http"?()=>gc(e,{...t,type:"sse"},n):null}}function Pj(e){return e instanceof KT&&(e.code===404||e.code===405)}function Mj(e){return e instanceof KT?e.code:void 0}function hc(e,t,n,r){let o=null,s=new Promise((i,a)=>{o=setTimeout(()=>{if(r!==void 0)try{let l=r();l&&typeof l.then=="function"&&l.catch(()=>{})}catch{}a(n())},t)});return Promise.race([e,s]).finally(()=>{o!==null&&clearTimeout(o)})}import{createHash as Oj}from"node:crypto";var GT="mcp__",qT="__",bm=64,$j=6;function ii(e){if(e.length===0)return"_";let n=e.replace(/[^a-zA-Z0-9_-]/g,"_").replace(/_{2,}/g,"_");return n.length===0?"_":n}function Dj(e){return Oj("sha256").update(e).digest("hex").slice(0,$j)}function zT(e,t){let n=ii(e),r=ii(t),o=`${GT}${n}${qT}${r}`;if(o.length<=bm)return o;let i=`${`${GT}${Dj(e)}${qT}`}${r}`;return i.length<=bm?i:i.slice(0,bm)}function ai(e){let t=new Map,n=new Map;for(let{serverName:o,toolNames:s}of e)for(let i of s){let a=zT(o,i),l={serverName:o,originalToolName:i},c=t.get(a);if(c===void 0){t.set(a,l);continue}if(c.serverName===o&&c.originalToolName===i)continue;let u=n.get(a)??[c];u.push(l),n.set(a,u)}let r=[];for(let[o,s]of n)r.push({wireName:o,pairs:s});return{tools:t,conflicts:r}}var li=class e{records;nameRegistry;onToolsRefreshed;constructor(t){this.records=t;let n=[];for(let[o,s]of t)s.state.status==="connected"&&n.push({serverName:o,toolNames:s.tools.map(i=>i.name)});let r=ai(n);if(r.conflicts.length>0){let o=r.conflicts.map(s=>{let i=s.pairs.map(a=>`${a.serverName}.${a.originalToolName}`).join(", ");return` ${s.wireName} \u2190 ${i}`});throw new Error(`MCP tool name conflicts (rename one of the servers in mcp.json):
2165
2165
  ${o.join(`
2166
- `)}`)}this.nameRegistry=r.tools}static async fromConfig(t,n={}){if(n.warnings&&n.warnings.length>0)for(let c of n.warnings)console.warn(`[mcp] ${c}`);let r=new Map,o=[],s={manager:void 0};for(let[c,u]of Object.entries(t)){let d=ii(c);if(d!==c&&console.warn(`[mcp] server name "${c}" sanitized to "${d}" for wire encoding`),u.disabled){r.set(c,{client:void 0,tools:[],state:{serverName:c,config:u,status:"disabled",toolCount:0}});continue}let f={client:void 0,tools:[],state:{serverName:c,config:u,status:"connecting",toolCount:0}};r.set(c,f);let g=new zo(c,u);f.client=g,g.onTransportError=b=>{f.state.status="error",f.state.error=yc(b.message,200),console.warn(`[mcp:${c}] transport error: ${b.message}`)},g.onToolListChanged=()=>{s.manager?.refreshServer(c).catch(b=>{let y=b instanceof Error?b.message:String(b);console.warn(`[mcp:${c}] refreshServer failed: ${y}`)})};let h=(async()=>{try{let{tools:b,serverInfo:y}=await g.connect();f.tools=b,f.state.status="connected",f.state.toolCount=b.length,f.state.lastListedAt=Date.now();let w=y?`${y.name}@${y.version}`:"unknown";console.log(`[mcp:${c}] connected (${w}) \u2014 ${b.length} tool(s)`)}catch(b){if(b instanceof ym){f.state.status="oauth_pending",console.log(`[mcp:${c}] OAuth authorization required \u2014 check Telegram or stderr for the auth URL`);return}let y=b instanceof Error?b.message:String(b);if(f.state.status="error",f.state.error=yc(y,200),u.alwaysLoad===!0)throw new Error(`MCP server "${c}" is marked alwaysLoad but failed to connect: ${y}`);console.warn(`[mcp:${c}] connect failed: ${y}`)}})();o.push(h)}let a=(await Promise.allSettled(o)).find(c=>c.status==="rejected");if(a){for(let c of r.values())c.client&&await c.client.disconnect().catch(()=>{});throw a.reason}let l=new e(r);return s.manager=l,l}getMcpTools(){let t=[];for(let[n,{serverName:r,originalToolName:o}]of this.nameRegistry){let s=this.records.get(r);if(!s||s.state.status!=="connected")continue;let i=s.tools.find(a=>a.name===o);i&&t.push(L1(n,i))}return t}getMcpHandlers(){let t=new Map;for(let[n,{serverName:r,originalToolName:o}]of this.nameRegistry){let s=this.records.get(r);!s||s.state.status!=="connected"||t.set(n,async(i,a)=>s.client?s.client.callTool(o,i,a):{content:`MCP server "${r}" is not connected`,isError:!0})}return t}async refreshServer(t){let n=this.records.get(t);if(!n||!n.client||n.state.status!=="connected")throw new Error(`McpManager.refreshServer("${t}"): server is not connected`);let r=await n.client.refreshTools();for(let[s,i]of this.nameRegistry)i.serverName===t&&this.nameRegistry.delete(s);let o=ai([{serverName:t,toolNames:r.map(s=>s.name)}]);if(o.conflicts.length>0)for(let s of o.conflicts){let i=s.pairs.map(a=>`${a.serverName}.${a.originalToolName}`).join(", ");console.warn(`[mcp:${t}] wire-name conflict after refresh \u2014 skipping: ${s.wireName} \u2190 ${i}`)}for(let[s,i]of o.tools)this.nameRegistry.set(s,i);n.tools=r,n.state.toolCount=r.length,n.state.lastListedAt=Date.now(),console.log(`[mcp:${t}] tool list refreshed \u2014 ${r.length} tool(s)`),this.onToolsRefreshed?.(t)}getServerStates(){return[...this.records.values()].map(t=>({...t.state}))}getMcpToolWireNames(){return[...this.nameRegistry.keys()]}async completeAuth(t,n){let r=this.records.get(t);if(!r)throw new Error(`McpManager.completeAuth("${t}"): server not found`);if(r.state.status!=="oauth_pending")throw new Error(`McpManager.completeAuth("${t}"): server is not in oauth_pending state (current status: ${r.state.status})`);if(!r.client)throw new Error(`McpManager.completeAuth("${t}"): no client record \u2014 server was never connected`);await r.client.finishAuth(n),await r.client.disconnect().catch(i=>{let a=i instanceof Error?i.message:String(i);console.warn(`[mcp:${t}] completeAuth disconnect warning: ${a}`)});let o=new zo(t,r.state.config);o.onTransportError=i=>{r.state.status="error",r.state.error=yc(i.message,200),console.warn(`[mcp:${t}] transport error: ${i.message}`)},o.onToolListChanged=()=>{this.refreshServer(t).catch(i=>{let a=i instanceof Error?i.message:String(i);console.warn(`[mcp:${t}] refreshServer failed: ${a}`)})},r.state.status="connecting",r.state.error=void 0,r.client=o;try{let{tools:i,serverInfo:a}=await o.connect();r.tools=i,r.state.status="connected",r.state.toolCount=i.length,r.state.lastListedAt=Date.now();let l=a?`${a.name}@${a.version}`:"unknown";console.log(`[mcp:${t}] OAuth complete \u2014 connected (${l}) \u2014 ${i.length} tool(s)`)}catch(i){let a=i instanceof Error?i.message:String(i);throw r.state.status="error",r.state.error=yc(a,200),new Error(`McpManager.completeAuth("${t}"): reconnect failed: ${a}`)}for(let[i,a]of this.nameRegistry)a.serverName===t&&this.nameRegistry.delete(i);let s=ai([{serverName:t,toolNames:r.tools.map(i=>i.name)}]);for(let[i,a]of s.tools)this.nameRegistry.set(i,a);this.onToolsRefreshed?.(t)}hasAnyConnected(){for(let t of this.records.values())if(t.state.status==="connected")return!0;return!1}size(){return this.records.size}async disconnectAll(){let t=[];for(let[n,r]of this.records)r.client&&t.push(r.client.disconnect().catch(o=>{let s=o instanceof Error?o.message:String(o);console.warn(`[mcp:${n}] disconnect error: ${s}`)}));await Promise.all(t)}};function L1(e,t){let n=t.description??`MCP tool ${t.name}`;return{name:e,description:n,input_schema:t.inputSchema}}function yc(e,t){return e.length<=t?e:`${e.slice(0,t-1)}\u2026`}G();W();import{existsSync as di,lstatSync as F1,readFileSync as N1,readdirSync as B1}from"node:fs";import{join as ui}from"node:path";function bc(){return ui(Xt(),"mcp.json")}function JT(e=process.cwd()){return ui(e,".mcp.json")}var U1=5;function wm(e=Oe()){if(!di(e))return[];let t=[];return VT(e,e,0,t,new Set),t}function VT(e,t,n,r,o){if(n>U1||o.has(t))return;o.add(t);let s=ui(t,".claude-plugin","plugin.json");if(di(s)){let a=ui(t,".claude-plugin","mcp.json");di(a)&&r.push(a);return}let i;try{i=B1(t)}catch{return}for(let a of i){if(a.startsWith("."))continue;let l=ui(t,a),c;try{c=F1(l)}catch{continue}c.isDirectory()&&VT(e,l,n+1,r,o)}}function j1(e,t){if(t===null||typeof t!="object"||Array.isArray(t))return{ok:!1,error:`server "${e}" must be an object`};let n=t,r=n.type;if(r===void 0)if(typeof n.command=="string")r="stdio";else if(typeof n.url=="string")r="streamable-http";else return{ok:!1,error:`server "${e}" has no \`command\` or \`url\`; cannot infer transport`};if(r==="stdio"){if(typeof n.command!="string"||n.command.length===0)return{ok:!1,error:`stdio server "${e}" requires non-empty \`command\``}}else if(r==="streamable-http"||r==="sse"){if(typeof n.url!="string"||n.url.length===0)return{ok:!1,error:`${r} server "${e}" requires non-empty \`url\``}}else return{ok:!1,error:`server "${e}" has unsupported \`type\`: ${String(r)}`};let o={type:r};if(typeof n.command=="string"&&(o.command=n.command),Array.isArray(n.args)&&(o.args=n.args.filter(s=>typeof s=="string")),n.env!==void 0&&typeof n.env=="object"&&n.env!==null){let s={};for(let[i,a]of Object.entries(n.env))typeof a=="string"&&(s[i]=a);o.env=s}if(typeof n.url=="string"&&(o.url=n.url),n.headers!==void 0&&typeof n.headers=="object"&&n.headers!==null){let s={};for(let[i,a]of Object.entries(n.headers))typeof a=="string"&&(s[i]=a);o.headers=s}return typeof n.oauth=="boolean"&&(o.oauth=n.oauth),typeof n.disabled=="boolean"&&(o.disabled=n.disabled),typeof n.alwaysLoad=="boolean"&&(o.alwaysLoad=n.alwaysLoad),typeof n.timeout=="number"&&n.timeout>0&&(o.timeout=n.timeout),{ok:!0,config:o}}function ci(e){if(!di(e))return{mcpServers:{},sources:[],warnings:[]};let t=[],n;try{n=JSON.parse(N1(e,"utf-8"))}catch(i){let a=i instanceof Error?i.message:String(i);return t.push(`mcp.json at ${e}: parse error \u2014 ${a}`),{mcpServers:{},sources:[e],warnings:t}}if(n===null||typeof n!="object"||Array.isArray(n))return t.push(`mcp.json at ${e}: top-level must be an object`),{mcpServers:{},sources:[e],warnings:t};let o=n.mcpServers;if(o==null||typeof o!="object")return{mcpServers:{},sources:[e],warnings:t};let s={};for(let[i,a]of Object.entries(o)){let l=j1(i,a);l.ok?s[i]=l.config:t.push(`mcp.json at ${e}: skipping ${l.error}`)}return{mcpServers:s,sources:[e],warnings:t}}function Sm(e={}){let t=[],n=[];if(e.pluginsRoot!==null){let a=e.pluginsRoot,l=a?wm(a):wm();for(let c of l)t.push({path:c,loaded:ci(c)})}if(!e.skipUserGlobal){let a=bc();t.push({path:a,loaded:ci(a)})}if(!e.skipProjectLocal&&T.AFK_ALLOW_PROJECT_MCP!=="0"){let a=JT(e.cwd);di(a)&&(t.push({path:a,loaded:ci(a)}),n.push(`mcp: loaded project-local config from ${a} \u2014 set AFK_ALLOW_PROJECT_MCP=0 to disable auto-load`))}e.cliOverride!==void 0&&t.push({path:e.cliOverride,loaded:ci(e.cliOverride)});let r=new Map,o=[...n],s=[];for(let a of t){for(let l of a.loaded.warnings)o.push(l);if(a.loaded.sources.length>0)for(let l of a.loaded.sources)s.includes(l)||s.push(l);for(let[l,c]of Object.entries(a.loaded.mcpServers)){let u=r.get(l);u&&o.push(`mcp: server "${l}" defined in ${u.source} is overridden by ${a.path}`),r.set(l,{config:c,source:a.path})}}let i={};for(let[a,l]of r)i[a]=l.config;return{mcpServers:i,sources:s,warnings:o}}ul();function km(e){return e.replace(/\bsk-ant-[A-Za-z0-9_-]{10,}/g,"sk-ant-[REDACTED]").replace(/\bsk-[A-Za-z0-9_-]{10,}/g,"sk-[REDACTED]").replace(/Bearer\s+[A-Za-z0-9._-]{10,}/gi,"Bearer [REDACTED]").replace(/Authorization:\s*\S+/gi,"Authorization: [REDACTED]")}function pi(e,t,n){let r=n instanceof Error?n.message:String(n);e.fn(p.warning(`\u26A0 [resume-swap] ${t}: ${km(r)}`))}async function YT(e,t){if(t.isInFlight()===!0)return{ok:!1,reason:"A turn is in flight \u2014 wait for it to finish before resuming."};let n;try{n=t.buildSession(e)}catch(i){let a=i instanceof Error?i.message:String(i);return{ok:!1,reason:`buildSession failed: ${km(a)}`}}let r;if(!await n.waitForInitialization().then(()=>!0,i=>(r=km(i instanceof Error?i.message:String(i)),!1))){await n.close().catch(a=>{pi(t.completionWriter,"new session close after init failure",a)});let i=`Session initialization failed: ${r??"unknown error"}`;return t.completionWriter.fn(p.warning(`\u26A0 ${i}`)),{ok:!1,reason:i}}await t.backgroundRegistry.cancelAll().catch(i=>{pi(t.completionWriter,"cancelAll failed",i)}),await t.sessionRef.current.close().catch(i=>{pi(t.completionWriter,"session close failed",i)}),t.sessionRef.current=n,e.stored?(sl(t.stats,e.stored,e.resumeId),t.stats.turnCosts=[],t.stats.turnTokens=[]):(t.stats.totalTurns=0,t.stats.totalCostUsd=0,t.stats.totalTokens=0,t.stats.totalDurationMs=0,t.stats.turns=[],t.stats.sessionId=e.resumeId,t.stats.sessionStartTime=Date.now(),t.stats.turnCosts=[],t.stats.turnTokens=[]),t.stats.planMode=!1,delete t.stats.pendingPlanExit;try{t.onSwapped(e)}catch(i){pi(t.completionWriter,"onSwapped callback threw",i)}t.contextSampler.attach(n),await Xl(n).catch(i=>{pi(t.completionWriter,"autoRegisterPluginPassthroughs failed",i)});let s=[`\u21AA Resumed ${e.id}`];return t.stats.totalTurns>0&&s.push(`${t.stats.totalTurns} prior turn${t.stats.totalTurns===1?"":"s"}`),t.stats.totalCostUsd>0&&s.push(Ne(t.stats.totalCostUsd)),t.stats.totalTokens>0&&s.push(re(t.stats.totalTokens)+" tokens"),t.completionWriter.fn(p.brand(s.join(" \xB7 "))),il(t.stats,t.completionWriter),t.statusLine.repaint(mr(t.stats,t.contextSampler)),{ok:!0,sessionId:n.sessionId??t.stats.sessionId??e.resumeId}}function XT(e){return new Ve(sn({model:e.model,apiKey:ue(),maxTurns:e.maxTurns,hookRegistry:e.hookRegistry,...e.systemPrompt!==void 0?{systemPrompt:e.systemPrompt}:{},...e.systemPromptSource!==void 0?{systemPromptSource:e.systemPromptSource}:{},...e.thinking!==void 0?{thinking:e.thinking}:{},...e.effort!==void 0?{effort:e.effort}:{},...e.maxOutputTokens!==void 0?{maxOutputTokens:e.maxOutputTokens}:{},...e.resumeConfig,...e.cwd!==void 0?{cwd:e.cwd}:{},...e.traceWriter!==void 0?{traceWriter:e.traceWriter}:{},...e.autoResumeOnUsageLimit!==void 0?{autoResumeOnUsageLimit:e.autoResumeOnUsageLimit}:{},...e.baseUrl!==void 0?{baseUrl:e.baseUrl}:{},provider:e.provider}))}async function QT(e,t){let n=Eo(e),r=Ro(n),o=n?.stored?.model??e.model,s,i,a;s=Ln(e.thinking)??po(),i=Fn(e.effort)??mo(),a=go(e.maxOutputTokens)??Ds();let l=uo()??co(),c=nt(),u=c.systemPromptSource,d=c.autoRouting?.interactive??!0,m=za(l,d,"repl"),f={current:null},g=ue(),h=new X({apiKey:g,...c.baseUrl!==void 0?{baseUrl:c.baseUrl}:{},...t?.cwd!==void 0?{cwd:t.cwd}:{}}),b=n?.stored?.sessionId,y=vo(b?{sessionLabel:b}:{}),w=new Sa(y?{traceWriter:y.writer}:{});ck(w);let R=c.bgSummaries===!0&&g?new mc({registry:w,apiKey:g,maxCallsPerSession:c.maxSummaryCallsPerSession??200}):void 0;R?.start(),uk(R);let v=ro(c.openaiBaseUrl!==void 0?{openaiBaseUrl:c.openaiBaseUrl}:{}),E={get sessionId(){return f.current?.sessionId},getInputStreamRef(){return f.current?.getInputStreamRef?.()??{pushUserMessage:()=>{}}},get abortSignal(){return f.current?.abortSignal??new AbortController().signal},get hookRegistry(){return f.current?.hookRegistry}},I=oo(o,g,v,c.baseUrl,y?.writer,w,t?.cwd),D=new Wt({subagentManager:h,parentSession:E,defaultConfig:{apiKey:g,systemPrompt:l,...c.baseUrl!==void 0?{baseUrl:c.baseUrl}:{}},defaultSubagentModel:wt(o),childProviderFactory:v,childSkillExecutorFactory:I,backgroundRegistry:w,depth:0,...t?.cwd!==void 0?{cwd:t.cwd}:{}}),_=new Ht({parentSession:E,defaultModel:o,defaultSubagentModel:wt(o),apiKey:g,childProviderFactory:v,childSkillExecutorFactory:I,backgroundRegistry:w,...c.baseUrl!==void 0?{baseUrl:c.baseUrl}:{},...y?.writer!==void 0?{traceWriter:y.writer}:{},...t?.cwd!==void 0?{cwd:t.cwd}:{}}),M=new Nn({parentSession:E,defaultModel:o,defaultSubagentModel:wt(o),apiKey:g,...c.baseUrl!==void 0?{baseUrl:c.baseUrl}:{},systemPrompt:l??""}),P=new We,$;{let _e=t?.cwd??process.cwd(),Be=Sm({cwd:_e,...e.mcpConfig!==void 0?{cliOverride:e.mcpConfig}:{}}),Pi=Object.values(Be.mcpServers).filter(rs=>!rs.disabled).length;if(Pi>0){let rs=Be.sources.length===1?Be.sources[0]:`${Be.sources.length} source(s)`;console.log(p.dim(` mcp: ${Pi} server(s) from ${rs??bc()}`)),$=await li.fromConfig(Be.mcpServers,{warnings:Be.warnings})}else if(Be.warnings.length>0)for(let rs of Be.warnings)console.warn(`[mcp] ${rs}`)}let F=ho(e.provider,{subagentExecutor:D,skillExecutor:_,composeExecutor:M,memoryStore:P,model:String(o),...c.openaiBaseUrl!==void 0?{openaiBaseUrl:c.openaiBaseUrl}:{},...$!==void 0?{mcpManager:$}:{}})??new Fe({permissions:{allowedTools:[...Ft,...tn,...st,"agent","skill","compose",...$?.getMcpToolWireNames()??[]]},subagentExecutor:D,skillExecutor:_,composeExecutor:M,memoryStore:P,surface:"cli",...$!==void 0?{mcpManager:$}:{}}),k=Za(o);n?.stored&&sl(k,n.stored,n.resumeId),k.cwd=t?.cwd??process.cwd(),y&&console.log(p.dim(` trace: ${y.tracePath}`));let A={fn:_e=>console.log(_e),idleFn:_e=>console.log(_e)},N=new ol,H=NT(process.stdout,{statusLine:N}),C=So(_e=>{A.fn(Va(_e))},"cli",P,()=>k.planMode?"plan":"default").registry,B={model:o,resumeConfig:r,systemPrompt:m,systemPromptSource:u,thinking:s,effort:i,maxOutputTokens:a,...c.baseUrl!==void 0?{baseUrl:c.baseUrl}:{},provider:F,hookRegistry:C,traceWriter:y?.writer,cwd:t?.cwd,maxTurns:parseInt(e.maxTurns,10),autoResumeOnUsageLimit:c.autoResumeOnUsageLimit},j=XT(B);f.current=j,h.setOnSubagentSucceeded((_e,Be)=>{f.current?.recordSubagentCompletion(_e,Be)});let ee=new uc,q=Uo(A),L=new dc(j),te={session:f,stats:k,out:q,ui:{clearScreen:()=>{te.getCompositor?.()?.setOverlay(""),N.stop(),L.reset(),process.stdout.write("\x1B[3J\x1B[2J\x1B[H"),N.start(),N.repaint(mr(k,L))},repaintStatusLine:()=>N.repaint(mr(k,L))},ledger:ee,...$!==void 0?{mcpManager:$}:{}},Re=_e=>(ee.clear(),YT(_e,{sessionRef:f,stats:k,contextSampler:L,statusLine:N,backgroundRegistry:w,completionWriter:A,isInFlight:()=>De.getInFlight?.()??!1,onSwapped:Be=>{De.resumeTarget=Be,De.clearVerdictLedger?.()},buildSession:Be=>XT({...B,model:Be.stored?.model??B.model,resumeConfig:Ro(Be)})})),De={session:f,memoryStore:P,stats:k,statusLine:N,contextSampler:L,completionWriter:A,replRenderer:H,slashCtx:te,rl:null,options:e,...n!==void 0?{resumeTarget:n}:{},teardownTrustedSkillEvents:void 0,backgroundRegistry:w,...R!==void 0?{bgSummarizer:R}:{},requestResume:Re,getInFlight:()=>!1,...$!==void 0?{mcpManager:$}:{}},Me=_e=>{A.fn(Lv(_e,{isTTY:process.stdout.isTTY,columns:process.stdout.columns}))},ut=_e=>{A.fn(Dv(_e,{isTTY:process.stdout.isTTY,columns:process.stdout.columns})),ee.record(_e)};Oy(Me),Py(ut),De.teardownTrustedSkillEvents=()=>{$y(Me),My(ut)},FT(),F instanceof Fe&&Bv(F);let Lr=ZT.createInterface({input:process.stdin,output:process.stdout,terminal:!1});De.rl=Lr;let ns={current:null};return De.inputSurfaceRef=ns,Bt.install(rl({readLine:_e=>new Promise((Be,Pi)=>{Lr.question(_e,Be),Lr.once("close",()=>Pi(new Error("readline closed")))}),writer:{line:(_e="")=>process.stdout.write(_e+`
2166
+ `)}`)}this.nameRegistry=r.tools}static async fromConfig(t,n={}){if(n.warnings&&n.warnings.length>0)for(let c of n.warnings)console.warn(`[mcp] ${c}`);let r=new Map,o=[],s={manager:void 0};for(let[c,u]of Object.entries(t)){let d=ii(c);if(d!==c&&console.warn(`[mcp] server name "${c}" sanitized to "${d}" for wire encoding`),u.disabled){r.set(c,{client:void 0,tools:[],state:{serverName:c,config:u,status:"disabled",toolCount:0}});continue}let f={client:void 0,tools:[],state:{serverName:c,config:u,status:"connecting",toolCount:0}};r.set(c,f);let g=new zo(c,u);f.client=g,g.onTransportError=b=>{f.state.status="error",f.state.error=yc(b.message,200),console.warn(`[mcp:${c}] transport error: ${b.message}`)},g.onToolListChanged=()=>{s.manager?.refreshServer(c).catch(b=>{let y=b instanceof Error?b.message:String(b);console.warn(`[mcp:${c}] refreshServer failed: ${y}`)})};let h=(async()=>{try{let{tools:b,serverInfo:y}=await g.connect();f.tools=b,f.state.status="connected",f.state.toolCount=b.length,f.state.lastListedAt=Date.now();let w=y?`${y.name}@${y.version}`:"unknown";console.log(`[mcp:${c}] connected (${w}) \u2014 ${b.length} tool(s)`)}catch(b){if(b instanceof ym){f.state.status="oauth_pending",console.log(`[mcp:${c}] OAuth authorization required \u2014 check Telegram or stderr for the auth URL`);return}let y=b instanceof Error?b.message:String(b);if(f.state.status="error",f.state.error=yc(y,200),u.alwaysLoad===!0)throw new Error(`MCP server "${c}" is marked alwaysLoad but failed to connect: ${y}`);console.warn(`[mcp:${c}] connect failed: ${y}`)}})();o.push(h)}let a=(await Promise.allSettled(o)).find(c=>c.status==="rejected");if(a){for(let c of r.values())c.client&&await c.client.disconnect().catch(()=>{});throw a.reason}let l=new e(r);return s.manager=l,l}getMcpTools(){let t=[];for(let[n,{serverName:r,originalToolName:o}]of this.nameRegistry){let s=this.records.get(r);if(!s||s.state.status!=="connected")continue;let i=s.tools.find(a=>a.name===o);i&&t.push(Lj(n,i))}return t}getMcpHandlers(){let t=new Map;for(let[n,{serverName:r,originalToolName:o}]of this.nameRegistry){let s=this.records.get(r);!s||s.state.status!=="connected"||t.set(n,async(i,a)=>s.client?s.client.callTool(o,i,a):{content:`MCP server "${r}" is not connected`,isError:!0})}return t}async refreshServer(t){let n=this.records.get(t);if(!n||!n.client||n.state.status!=="connected")throw new Error(`McpManager.refreshServer("${t}"): server is not connected`);let r=await n.client.refreshTools();for(let[s,i]of this.nameRegistry)i.serverName===t&&this.nameRegistry.delete(s);let o=ai([{serverName:t,toolNames:r.map(s=>s.name)}]);if(o.conflicts.length>0)for(let s of o.conflicts){let i=s.pairs.map(a=>`${a.serverName}.${a.originalToolName}`).join(", ");console.warn(`[mcp:${t}] wire-name conflict after refresh \u2014 skipping: ${s.wireName} \u2190 ${i}`)}for(let[s,i]of o.tools)this.nameRegistry.set(s,i);n.tools=r,n.state.toolCount=r.length,n.state.lastListedAt=Date.now(),console.log(`[mcp:${t}] tool list refreshed \u2014 ${r.length} tool(s)`),this.onToolsRefreshed?.(t)}getServerStates(){return[...this.records.values()].map(t=>({...t.state}))}getMcpToolWireNames(){return[...this.nameRegistry.keys()]}async completeAuth(t,n){let r=this.records.get(t);if(!r)throw new Error(`McpManager.completeAuth("${t}"): server not found`);if(r.state.status!=="oauth_pending")throw new Error(`McpManager.completeAuth("${t}"): server is not in oauth_pending state (current status: ${r.state.status})`);if(!r.client)throw new Error(`McpManager.completeAuth("${t}"): no client record \u2014 server was never connected`);await r.client.finishAuth(n),await r.client.disconnect().catch(i=>{let a=i instanceof Error?i.message:String(i);console.warn(`[mcp:${t}] completeAuth disconnect warning: ${a}`)});let o=new zo(t,r.state.config);o.onTransportError=i=>{r.state.status="error",r.state.error=yc(i.message,200),console.warn(`[mcp:${t}] transport error: ${i.message}`)},o.onToolListChanged=()=>{this.refreshServer(t).catch(i=>{let a=i instanceof Error?i.message:String(i);console.warn(`[mcp:${t}] refreshServer failed: ${a}`)})},r.state.status="connecting",r.state.error=void 0,r.client=o;try{let{tools:i,serverInfo:a}=await o.connect();r.tools=i,r.state.status="connected",r.state.toolCount=i.length,r.state.lastListedAt=Date.now();let l=a?`${a.name}@${a.version}`:"unknown";console.log(`[mcp:${t}] OAuth complete \u2014 connected (${l}) \u2014 ${i.length} tool(s)`)}catch(i){let a=i instanceof Error?i.message:String(i);throw r.state.status="error",r.state.error=yc(a,200),new Error(`McpManager.completeAuth("${t}"): reconnect failed: ${a}`)}for(let[i,a]of this.nameRegistry)a.serverName===t&&this.nameRegistry.delete(i);let s=ai([{serverName:t,toolNames:r.tools.map(i=>i.name)}]);for(let[i,a]of s.tools)this.nameRegistry.set(i,a);this.onToolsRefreshed?.(t)}hasAnyConnected(){for(let t of this.records.values())if(t.state.status==="connected")return!0;return!1}size(){return this.records.size}async disconnectAll(){let t=[];for(let[n,r]of this.records)r.client&&t.push(r.client.disconnect().catch(o=>{let s=o instanceof Error?o.message:String(o);console.warn(`[mcp:${n}] disconnect error: ${s}`)}));await Promise.all(t)}};function Lj(e,t){let n=t.description??`MCP tool ${t.name}`;return{name:e,description:n,input_schema:t.inputSchema}}function yc(e,t){return e.length<=t?e:`${e.slice(0,t-1)}\u2026`}G();W();import{existsSync as di,lstatSync as Fj,readFileSync as Nj,readdirSync as Bj}from"node:fs";import{join as ui}from"node:path";function bc(){return ui(Xt(),"mcp.json")}function JT(e=process.cwd()){return ui(e,".mcp.json")}var Uj=5;function wm(e=Oe()){if(!di(e))return[];let t=[];return VT(e,e,0,t,new Set),t}function VT(e,t,n,r,o){if(n>Uj||o.has(t))return;o.add(t);let s=ui(t,".claude-plugin","plugin.json");if(di(s)){let a=ui(t,".claude-plugin","mcp.json");di(a)&&r.push(a);return}let i;try{i=Bj(t)}catch{return}for(let a of i){if(a.startsWith("."))continue;let l=ui(t,a),c;try{c=Fj(l)}catch{continue}c.isDirectory()&&VT(e,l,n+1,r,o)}}function jj(e,t){if(t===null||typeof t!="object"||Array.isArray(t))return{ok:!1,error:`server "${e}" must be an object`};let n=t,r=n.type;if(r===void 0)if(typeof n.command=="string")r="stdio";else if(typeof n.url=="string")r="streamable-http";else return{ok:!1,error:`server "${e}" has no \`command\` or \`url\`; cannot infer transport`};if(r==="stdio"){if(typeof n.command!="string"||n.command.length===0)return{ok:!1,error:`stdio server "${e}" requires non-empty \`command\``}}else if(r==="streamable-http"||r==="sse"){if(typeof n.url!="string"||n.url.length===0)return{ok:!1,error:`${r} server "${e}" requires non-empty \`url\``}}else return{ok:!1,error:`server "${e}" has unsupported \`type\`: ${String(r)}`};let o={type:r};if(typeof n.command=="string"&&(o.command=n.command),Array.isArray(n.args)&&(o.args=n.args.filter(s=>typeof s=="string")),n.env!==void 0&&typeof n.env=="object"&&n.env!==null){let s={};for(let[i,a]of Object.entries(n.env))typeof a=="string"&&(s[i]=a);o.env=s}if(typeof n.url=="string"&&(o.url=n.url),n.headers!==void 0&&typeof n.headers=="object"&&n.headers!==null){let s={};for(let[i,a]of Object.entries(n.headers))typeof a=="string"&&(s[i]=a);o.headers=s}return typeof n.oauth=="boolean"&&(o.oauth=n.oauth),typeof n.disabled=="boolean"&&(o.disabled=n.disabled),typeof n.alwaysLoad=="boolean"&&(o.alwaysLoad=n.alwaysLoad),typeof n.timeout=="number"&&n.timeout>0&&(o.timeout=n.timeout),{ok:!0,config:o}}function ci(e){if(!di(e))return{mcpServers:{},sources:[],warnings:[]};let t=[],n;try{n=JSON.parse(Nj(e,"utf-8"))}catch(i){let a=i instanceof Error?i.message:String(i);return t.push(`mcp.json at ${e}: parse error \u2014 ${a}`),{mcpServers:{},sources:[e],warnings:t}}if(n===null||typeof n!="object"||Array.isArray(n))return t.push(`mcp.json at ${e}: top-level must be an object`),{mcpServers:{},sources:[e],warnings:t};let o=n.mcpServers;if(o==null||typeof o!="object")return{mcpServers:{},sources:[e],warnings:t};let s={};for(let[i,a]of Object.entries(o)){let l=jj(i,a);l.ok?s[i]=l.config:t.push(`mcp.json at ${e}: skipping ${l.error}`)}return{mcpServers:s,sources:[e],warnings:t}}function Sm(e={}){let t=[],n=[];if(e.pluginsRoot!==null){let a=e.pluginsRoot,l=a?wm(a):wm();for(let c of l)t.push({path:c,loaded:ci(c)})}if(!e.skipUserGlobal){let a=bc();t.push({path:a,loaded:ci(a)})}if(!e.skipProjectLocal&&T.AFK_ALLOW_PROJECT_MCP!=="0"){let a=JT(e.cwd);di(a)&&(t.push({path:a,loaded:ci(a)}),n.push(`mcp: loaded project-local config from ${a} \u2014 set AFK_ALLOW_PROJECT_MCP=0 to disable auto-load`))}e.cliOverride!==void 0&&t.push({path:e.cliOverride,loaded:ci(e.cliOverride)});let r=new Map,o=[...n],s=[];for(let a of t){for(let l of a.loaded.warnings)o.push(l);if(a.loaded.sources.length>0)for(let l of a.loaded.sources)s.includes(l)||s.push(l);for(let[l,c]of Object.entries(a.loaded.mcpServers)){let u=r.get(l);u&&o.push(`mcp: server "${l}" defined in ${u.source} is overridden by ${a.path}`),r.set(l,{config:c,source:a.path})}}let i={};for(let[a,l]of r)i[a]=l.config;return{mcpServers:i,sources:s,warnings:o}}ul();function km(e){return e.replace(/\bsk-ant-[A-Za-z0-9_-]{10,}/g,"sk-ant-[REDACTED]").replace(/\bsk-[A-Za-z0-9_-]{10,}/g,"sk-[REDACTED]").replace(/Bearer\s+[A-Za-z0-9._-]{10,}/gi,"Bearer [REDACTED]").replace(/Authorization:\s*\S+/gi,"Authorization: [REDACTED]")}function pi(e,t,n){let r=n instanceof Error?n.message:String(n);e.fn(m.warning(`\u26A0 [resume-swap] ${t}: ${km(r)}`))}async function YT(e,t){if(t.isInFlight()===!0)return{ok:!1,reason:"A turn is in flight \u2014 wait for it to finish before resuming."};let n;try{n=t.buildSession(e)}catch(i){let a=i instanceof Error?i.message:String(i);return{ok:!1,reason:`buildSession failed: ${km(a)}`}}let r;if(!await n.waitForInitialization().then(()=>!0,i=>(r=km(i instanceof Error?i.message:String(i)),!1))){await n.close().catch(a=>{pi(t.completionWriter,"new session close after init failure",a)});let i=`Session initialization failed: ${r??"unknown error"}`;return t.completionWriter.fn(m.warning(`\u26A0 ${i}`)),{ok:!1,reason:i}}await t.backgroundRegistry.cancelAll().catch(i=>{pi(t.completionWriter,"cancelAll failed",i)}),await t.sessionRef.current.close().catch(i=>{pi(t.completionWriter,"session close failed",i)}),t.sessionRef.current=n,e.stored?(sl(t.stats,e.stored,e.resumeId),t.stats.turnCosts=[],t.stats.turnTokens=[]):(t.stats.totalTurns=0,t.stats.totalCostUsd=0,t.stats.totalTokens=0,t.stats.totalDurationMs=0,t.stats.turns=[],t.stats.sessionId=e.resumeId,t.stats.sessionStartTime=Date.now(),t.stats.turnCosts=[],t.stats.turnTokens=[]),t.stats.planMode=!1,delete t.stats.pendingPlanExit;try{t.onSwapped(e)}catch(i){pi(t.completionWriter,"onSwapped callback threw",i)}t.contextSampler.attach(n),await Xl(n).catch(i=>{pi(t.completionWriter,"autoRegisterPluginPassthroughs failed",i)});let s=[`\u21AA Resumed ${e.id}`];return t.stats.totalTurns>0&&s.push(`${t.stats.totalTurns} prior turn${t.stats.totalTurns===1?"":"s"}`),t.stats.totalCostUsd>0&&s.push(Ne(t.stats.totalCostUsd)),t.stats.totalTokens>0&&s.push(re(t.stats.totalTokens)+" tokens"),t.completionWriter.fn(m.brand(s.join(" \xB7 "))),il(t.stats,t.completionWriter),t.statusLine.repaint(mr(t.stats,t.contextSampler)),{ok:!0,sessionId:n.sessionId??t.stats.sessionId??e.resumeId}}function XT(e){return new Ve(sn({model:e.model,apiKey:ue(),maxTurns:e.maxTurns,hookRegistry:e.hookRegistry,...e.systemPrompt!==void 0?{systemPrompt:e.systemPrompt}:{},...e.systemPromptSource!==void 0?{systemPromptSource:e.systemPromptSource}:{},...e.thinking!==void 0?{thinking:e.thinking}:{},...e.effort!==void 0?{effort:e.effort}:{},...e.maxOutputTokens!==void 0?{maxOutputTokens:e.maxOutputTokens}:{},...e.resumeConfig,...e.cwd!==void 0?{cwd:e.cwd}:{},...e.traceWriter!==void 0?{traceWriter:e.traceWriter}:{},...e.autoResumeOnUsageLimit!==void 0?{autoResumeOnUsageLimit:e.autoResumeOnUsageLimit}:{},...e.baseUrl!==void 0?{baseUrl:e.baseUrl}:{},provider:e.provider}))}async function QT(e,t){let n=Eo(e),r=Ro(n),o=n?.stored?.model??e.model,s,i,a;s=Ln(e.thinking)??po(),i=Fn(e.effort)??mo(),a=go(e.maxOutputTokens)??Ds();let l=uo()??co(),c=nt(),u=c.systemPromptSource,d=c.autoRouting?.interactive??!0,p=za(l,d,"repl"),f={current:null},g=ue(),h=new X({apiKey:g,...c.baseUrl!==void 0?{baseUrl:c.baseUrl}:{},...t?.cwd!==void 0?{cwd:t.cwd}:{}}),b=n?.stored?.sessionId,y=vo(b?{sessionLabel:b}:{}),w=new Sa(y?{traceWriter:y.writer}:{});ck(w);let R=c.bgSummaries===!0&&g?new mc({registry:w,apiKey:g,maxCallsPerSession:c.maxSummaryCallsPerSession??200}):void 0;R?.start(),uk(R);let v=ro(c.openaiBaseUrl!==void 0?{openaiBaseUrl:c.openaiBaseUrl}:{}),E={get sessionId(){return f.current?.sessionId},getInputStreamRef(){return f.current?.getInputStreamRef?.()??{pushUserMessage:()=>{}}},get abortSignal(){return f.current?.abortSignal??new AbortController().signal},get hookRegistry(){return f.current?.hookRegistry}},I=oo(o,g,v,c.baseUrl,y?.writer,w,t?.cwd),D=new Wt({subagentManager:h,parentSession:E,defaultConfig:{apiKey:g,systemPrompt:l,...c.baseUrl!==void 0?{baseUrl:c.baseUrl}:{}},defaultSubagentModel:wt(o),childProviderFactory:v,childSkillExecutorFactory:I,backgroundRegistry:w,depth:0,...t?.cwd!==void 0?{cwd:t.cwd}:{}}),_=new Ht({parentSession:E,defaultModel:o,defaultSubagentModel:wt(o),apiKey:g,childProviderFactory:v,childSkillExecutorFactory:I,backgroundRegistry:w,...c.baseUrl!==void 0?{baseUrl:c.baseUrl}:{},...y?.writer!==void 0?{traceWriter:y.writer}:{},...t?.cwd!==void 0?{cwd:t.cwd}:{}}),M=new Nn({parentSession:E,defaultModel:o,defaultSubagentModel:wt(o),apiKey:g,...c.baseUrl!==void 0?{baseUrl:c.baseUrl}:{},systemPrompt:l??""}),P=new We,$;{let _e=t?.cwd??process.cwd(),Be=Sm({cwd:_e,...e.mcpConfig!==void 0?{cliOverride:e.mcpConfig}:{}}),Pi=Object.values(Be.mcpServers).filter(rs=>!rs.disabled).length;if(Pi>0){let rs=Be.sources.length===1?Be.sources[0]:`${Be.sources.length} source(s)`;console.log(m.dim(` mcp: ${Pi} server(s) from ${rs??bc()}`)),$=await li.fromConfig(Be.mcpServers,{warnings:Be.warnings})}else if(Be.warnings.length>0)for(let rs of Be.warnings)console.warn(`[mcp] ${rs}`)}let F=ho(e.provider,{subagentExecutor:D,skillExecutor:_,composeExecutor:M,memoryStore:P,model:String(o),...c.openaiBaseUrl!==void 0?{openaiBaseUrl:c.openaiBaseUrl}:{},...$!==void 0?{mcpManager:$}:{}})??new Fe({permissions:{allowedTools:[...Ft,...tn,...st,"agent","skill","compose",...$?.getMcpToolWireNames()??[]]},subagentExecutor:D,skillExecutor:_,composeExecutor:M,memoryStore:P,surface:"cli",...$!==void 0?{mcpManager:$}:{}}),k=Za(o);n?.stored&&sl(k,n.stored,n.resumeId),k.cwd=t?.cwd??process.cwd(),y&&console.log(m.dim(` trace: ${y.tracePath}`));let A={fn:_e=>console.log(_e),idleFn:_e=>console.log(_e)},N=new ol,H=NT(process.stdout,{statusLine:N}),C=So(_e=>{A.fn(Va(_e))},"cli",P,()=>k.planMode?"plan":"default").registry,B={model:o,resumeConfig:r,systemPrompt:p,systemPromptSource:u,thinking:s,effort:i,maxOutputTokens:a,...c.baseUrl!==void 0?{baseUrl:c.baseUrl}:{},provider:F,hookRegistry:C,traceWriter:y?.writer,cwd:t?.cwd,maxTurns:parseInt(e.maxTurns,10),autoResumeOnUsageLimit:c.autoResumeOnUsageLimit},j=XT(B);f.current=j,h.setOnSubagentSucceeded((_e,Be)=>{f.current?.recordSubagentCompletion(_e,Be)});let ee=new uc,q=Uo(A),L=new dc(j),te={session:f,stats:k,out:q,ui:{clearScreen:()=>{te.getCompositor?.()?.setOverlay(""),N.stop(),L.reset(),process.stdout.write("\x1B[3J\x1B[2J\x1B[H"),N.start(),N.repaint(mr(k,L))},repaintStatusLine:()=>N.repaint(mr(k,L))},ledger:ee,...$!==void 0?{mcpManager:$}:{}},Re=_e=>(ee.clear(),YT(_e,{sessionRef:f,stats:k,contextSampler:L,statusLine:N,backgroundRegistry:w,completionWriter:A,isInFlight:()=>De.getInFlight?.()??!1,onSwapped:Be=>{De.resumeTarget=Be,De.clearVerdictLedger?.()},buildSession:Be=>XT({...B,model:Be.stored?.model??B.model,resumeConfig:Ro(Be)})})),De={session:f,memoryStore:P,stats:k,statusLine:N,contextSampler:L,completionWriter:A,replRenderer:H,slashCtx:te,rl:null,options:e,...n!==void 0?{resumeTarget:n}:{},teardownTrustedSkillEvents:void 0,backgroundRegistry:w,...R!==void 0?{bgSummarizer:R}:{},requestResume:Re,getInFlight:()=>!1,...$!==void 0?{mcpManager:$}:{}},Me=_e=>{A.fn(Lv(_e,{isTTY:process.stdout.isTTY,columns:process.stdout.columns}))},ut=_e=>{A.fn(Dv(_e,{isTTY:process.stdout.isTTY,columns:process.stdout.columns})),ee.record(_e)};Oy(Me),Py(ut),De.teardownTrustedSkillEvents=()=>{$y(Me),My(ut)},FT(),F instanceof Fe&&Bv(F);let Lr=ZT.createInterface({input:process.stdin,output:process.stdout,terminal:!1});De.rl=Lr;let ns={current:null};return De.inputSurfaceRef=ns,Bt.install(rl({readLine:_e=>new Promise((Be,Pi)=>{Lr.question(_e,Be),Lr.once("close",()=>Pi(new Error("readline closed")))}),writer:{line:(_e="")=>process.stdout.write(_e+`
2167
2167
  `)},pendingCount:()=>Bt.pendingCount(),suspendInput:()=>ns.current?.suspendForElicitation(),resumeInput:()=>ns.current?.resumeAfterElicitation()})),te.requestResume=Re,De}G();import{promises as mi}from"node:fs";import*as tx from"node:os";import*as wc from"node:path";async function ex(e,t,n=!1){await mi.mkdir(e,{recursive:!0});let r=new Date().toISOString().replace(/[:.]/g,"-"),o=wc.join(e,`${r}.md`),s=n?" (continued)":"";return await mi.writeFile(o,`# Session \u2014 ${new Date().toISOString()}${s}
2168
2168
 
2169
2169
  - model: ${t}
@@ -2186,40 +2186,40 @@ ${o}
2186
2186
  _cleared_
2187
2187
  `,{mode:384}).catch(()=>{}),n=await ex(t,e(),!0)},async appendEnded(){await mi.appendFile(n,`
2188
2188
  _ended: ${new Date().toISOString()}_
2189
- `,{mode:384}).catch(()=>{})}}}W();import{readFile as sx,mkdir as W1,stat as H1,open as vm}from"fs/promises";import{dirname as K1}from"path";import{O_WRONLY as Tm,O_CREAT as xm,O_APPEND as rx,O_NOFOLLOW as Em,O_TRUNC as G1}from"node:constants";var fi=1e3,q1=/(?:^sk-[A-Za-z0-9]|^ghp_[A-Za-z0-9]|^github_pat_[A-Za-z0-9]|^ghs_[A-Za-z0-9]|^xoxb-[0-9]|^glpat-[A-Za-z0-9]|bearer\s+\S|password\s*=\s*\S|token\s*=\s*\S|key\s*=\s*\S)/i;function z1(e){return e.replace(/\x1b\[[^@-~]*[@-~]|\x1b[^[]/g,"")}var ox=Promise.resolve();function J1(e){let t=ox.then(e,e);return ox=t.then(()=>{},()=>{}),t}var Rr=null,Sc=class{_entries;_index;_draft;constructor(t){this._entries=t,this._index=-1,this._draft=""}get length(){return this._entries.length}push(t){if(t.startsWith(" "))return;let n=t.trim();!n||q1.test(n)||this._entries[this._entries.length-1]===n||(this._entries.push(n),this._entries.length>fi&&this._entries.shift(),this._index=-1,this._draft="",V1(n).catch(o=>{process.stderr.write(`[afk] history write failed: ${o.message}
2189
+ `,{mode:384}).catch(()=>{})}}}W();import{readFile as sx,mkdir as Wj,stat as Hj,open as vm}from"fs/promises";import{dirname as Kj}from"path";import{O_WRONLY as Tm,O_CREAT as xm,O_APPEND as rx,O_NOFOLLOW as Em,O_TRUNC as Gj}from"node:constants";var fi=1e3,qj=/(?:^sk-[A-Za-z0-9]|^ghp_[A-Za-z0-9]|^github_pat_[A-Za-z0-9]|^ghs_[A-Za-z0-9]|^xoxb-[0-9]|^glpat-[A-Za-z0-9]|bearer\s+\S|password\s*=\s*\S|token\s*=\s*\S|key\s*=\s*\S)/i;function zj(e){return e.replace(/\x1b\[[^@-~]*[@-~]|\x1b[^[]/g,"")}var ox=Promise.resolve();function Jj(e){let t=ox.then(e,e);return ox=t.then(()=>{},()=>{}),t}var Rr=null,Sc=class{_entries;_index;_draft;constructor(t){this._entries=t,this._index=-1,this._draft=""}get length(){return this._entries.length}push(t){if(t.startsWith(" "))return;let n=t.trim();!n||qj.test(n)||this._entries[this._entries.length-1]===n||(this._entries.push(n),this._entries.length>fi&&this._entries.shift(),this._index=-1,this._draft="",Vj(n).catch(o=>{process.stderr.write(`[afk] history write failed: ${o.message}
2190
2190
  `)}))}back(t){return this._entries.length===0?null:(this._index===-1?(this._draft=t,this._index=this._entries.length-1):this._index>0&&this._index--,this._entries[this._index]??null)}forward(){if(this._index===-1)return null;if(this._index<this._entries.length-1)return this._index++,this._entries[this._index]??null;this._index=-1;let t=this._draft;return this._draft="",t}resetRecall(){this._index=-1,this._draft=""}get inRecall(){return this._index!==-1}};async function ix(){let e=cu();try{let t=await sx(e,"utf8"),n=[];for(let r of t.split(`
2191
- `)){let o=r.trim();if(o)try{let s=JSON.parse(o);if(s!==null&&typeof s=="object"&&"text"in s&&typeof s.text=="string"){let a=z1(s.text);a.trim()&&a!==n[n.length-1]&&n.push(a)}}catch{}}return Rr=n.length,new Sc(n)}catch(t){return t&&t.code!=="ENOENT"&&process.stderr.write(`[afk] history load failed: ${t.message}
2192
- `),new Sc([])}}function V1(e){return J1(async()=>{let t=cu();await W1(K1(t),{recursive:!0});let n={text:e,ts:Date.now()},r=JSON.stringify(n)+`
2193
- `;if(Rr!==null&&Rr<fi-1){let i=await vm(t,Tm|xm|rx|Em,384);try{await i.writeFile(r)}finally{await i.close()}Rr++;return}let o=await H1(t).catch(()=>null);if(o&&o.size>5*1024*1024){process.stderr.write(`[afk] history file exceeds 5MB cap (${o.size} bytes); skipping write
2191
+ `)){let o=r.trim();if(o)try{let s=JSON.parse(o);if(s!==null&&typeof s=="object"&&"text"in s&&typeof s.text=="string"){let a=zj(s.text);a.trim()&&a!==n[n.length-1]&&n.push(a)}}catch{}}return Rr=n.length,new Sc(n)}catch(t){return t&&t.code!=="ENOENT"&&process.stderr.write(`[afk] history load failed: ${t.message}
2192
+ `),new Sc([])}}function Vj(e){return Jj(async()=>{let t=cu();await Wj(Kj(t),{recursive:!0});let n={text:e,ts:Date.now()},r=JSON.stringify(n)+`
2193
+ `;if(Rr!==null&&Rr<fi-1){let i=await vm(t,Tm|xm|rx|Em,384);try{await i.writeFile(r)}finally{await i.close()}Rr++;return}let o=await Hj(t).catch(()=>null);if(o&&o.size>5*1024*1024){process.stderr.write(`[afk] history file exceeds 5MB cap (${o.size} bytes); skipping write
2194
2194
  `);return}let s=[];try{let i=await sx(t,"utf8");for(let a of i.split(`
2195
2195
  `)){let l=a.trim();if(l)try{let c=JSON.parse(l);c!==null&&typeof c=="object"&&"text"in c&&typeof c.text=="string"&&s.push(c)}catch{}}}catch{}if(Rr=s.length,s.length<fi-1){let i=await vm(t,Tm|xm|rx|Em,384);try{await i.writeFile(r)}finally{await i.close()}Rr++}else{let i=s.slice(-(fi-1));i.push(n);let a=i.map(c=>JSON.stringify(c)).join(`
2196
2196
  `)+`
2197
- `,l=await vm(t,Tm|xm|G1|Em,384);try{await l.writeFile(a)}finally{await l.close()}Rr=fi}})}async function ax(e){if(e.initialBuffer!==void 0&&e.initialBuffer.length>0)return{text:e.initialBuffer,attachments:[]};let t=null;if(e.onSigint){let n=e.onSigint;t=()=>n(),process.on("SIGINT",t)}try{return{text:await Mk({rl:e.rl,promptFn:e.promptFn}),attachments:[]}}finally{t&&process.removeListener("SIGINT",t)}}import*as ct from"ansi-escapes";import Rm from"string-width";var Y1="\x1B[?2004h",X1="\x1B[?2004l";function lx(e,t){let n=e.isRaw;e.setRawMode(!0),e.resume(),t.write(Y1);let r=!1;return{restore(){if(!r){r=!0;try{t.write(X1)}catch{}try{e.setRawMode(n)}catch{}}}}}function kc(){let e={dropdownOpen:!1,candidates:[],selectedIndex:0,viewportStart:0,suppressedSignature:null,trigger:null,reset(){e.dropdownOpen=!1,e.candidates=[],e.selectedIndex=0,e.viewportStart=0,e.suppressedSignature=null,e.trigger=null}};return e}async function cx(e){let t=process.stdin,n=process.stdout,r=e.compositor?.isArmed()?{restore:()=>{}}:lx(t,n),o=e.statusLine?.getExtraRows()??0;ur(t);let s=e.promptFn(),i=Rm(Ie(s)),a=null,l=null;try{return e.statusLine?.setExtraRows(o+1),await new Promise((c,u)=>{let d=U.seed(e.initialBuffer??""),m=e.autocompleteState??kc();m.reset();let f=0,g=!1,h=!1,b=0,y=0,w=0,x=null,R=[],v=6,E=0,I=!1,D=8,_={has:H=>Ze().some(C=>C.name===`/${H}`)},M=()=>{if((w>0||y>0)&&n.write(ct.cursorUp(w+y)),n.write("\r"),n.write(ct.eraseDown),R.length>0)n.write(Vs(R)+`
2197
+ `,l=await vm(t,Tm|xm|Gj|Em,384);try{await l.writeFile(a)}finally{await l.close()}Rr=fi}})}async function ax(e){if(e.initialBuffer!==void 0&&e.initialBuffer.length>0)return{text:e.initialBuffer,attachments:[]};let t=null;if(e.onSigint){let n=e.onSigint;t=()=>n(),process.on("SIGINT",t)}try{return{text:await Mk({rl:e.rl,promptFn:e.promptFn}),attachments:[]}}finally{t&&process.removeListener("SIGINT",t)}}import*as ct from"ansi-escapes";import Rm from"string-width";var Yj="\x1B[?2004h",Xj="\x1B[?2004l";function lx(e,t){let n=e.isRaw;e.setRawMode(!0),e.resume(),t.write(Yj);let r=!1;return{restore(){if(!r){r=!0;try{t.write(Xj)}catch{}try{e.setRawMode(n)}catch{}}}}}function kc(){let e={dropdownOpen:!1,candidates:[],selectedIndex:0,viewportStart:0,suppressedSignature:null,trigger:null,reset(){e.dropdownOpen=!1,e.candidates=[],e.selectedIndex=0,e.viewportStart=0,e.suppressedSignature=null,e.trigger=null}};return e}async function cx(e){let t=process.stdin,n=process.stdout,r=e.compositor?.isArmed()?{restore:()=>{}}:lx(t,n),o=e.statusLine?.getExtraRows()??0;ur(t);let s=e.promptFn(),i=Rm(Ie(s)),a=null,l=null;try{return e.statusLine?.setExtraRows(o+1),await new Promise((c,u)=>{let d=U.seed(e.initialBuffer??""),p=e.autocompleteState??kc();p.reset();let f=0,g=!1,h=!1,b=0,y=0,w=0,x=null,R=[],v=6,E=0,I=!1,D=8,_={has:H=>Ze().some(C=>C.name===`/${H}`)},M=()=>{if((w>0||y>0)&&n.write(ct.cursorUp(w+y)),n.write("\r"),n.write(ct.eraseDown),R.length>0)n.write(Vs(R)+`
2198
2198
  `),w=1;else if(x!==null){let L=x;x=null,n.write(L+`
2199
- `),w=1}else w=0;n.write(s+Wn(d.buffer,_)),m.trigger=zs(d.buffer,d.cursor);let H=`${d.cursor}:${d.buffer}`;m.suppressedSignature!==null&&m.suppressedSignature!==H&&(m.suppressedSignature=null),m.trigger&&m.suppressedSignature===null?(m.trigger.kind==="slash"?m.candidates=Al(m.trigger.query).slice(0,12):m.trigger.kind==="file"?m.candidates=_l(m.trigger.query).slice(0,12):m.candidates=Js(m.trigger.command,m.trigger.query),m.dropdownOpen=m.candidates.length>0):(m.dropdownOpen=!1,m.candidates=[]),m.selectedIndex>=m.candidates.length&&(m.selectedIndex=Math.max(0,m.candidates.length-1)),m.viewportStart>m.selectedIndex&&(m.viewportStart=m.selectedIndex),m.selectedIndex>=m.viewportStart+v&&(m.viewportStart=m.selectedIndex-v+1);let C=n.columns||80;if(f=0,m.dropdownOpen&&C>40){let L=Math.min(C-4,60),te=Math.min(m.candidates.length-m.viewportStart,v);for(let Me=0;Me<te;Me++){let ut=m.viewportStart+Me,Lr=El(m.candidates[ut],ut===m.selectedIndex,L,m.trigger?.kind);n.write(`
2200
- `+Lr);let ns=Rm(Ie(Lr));f+=Math.max(1,Math.ceil(ns/C))}let Re=Math.min(C-4,80),De=Rl(m.candidates[m.selectedIndex]?.hint,Re);if(De!==null){n.write(`
2201
- `+De);let Me=Rm(Ie(De));f+=Math.max(1,Math.ceil(Me/C))}}let B=Tp(d.buffer,i,C),{row:j,col:ee}=Un(d.buffer,d.cursor,i,C),q=Math.max(0,B-j+f);q>0&&n.write(ct.cursorUp(q)),n.write("\r"),ee>0&&n.write(ct.cursorForward(ee)),y=B},P=!1,$=()=>{I||(I=!0,setImmediate(()=>{I&&!P&&(I=!1,M())}))};M();let F=()=>{let H=m.candidates[m.selectedIndex];if(!H)return!1;let C=d.buffer.slice(0,d.cursor),B=d.buffer.slice(d.cursor),j,ee;if(m.trigger?.kind==="slash"){let q=/\/[A-Za-z_-]*$/.exec(C);j=q?C.length-q[0].length:d.cursor,ee=H.value+(B.startsWith(" ")?"":" ")}else if(m.trigger?.kind==="flag"){let q=/--[a-z0-9-]*$/.exec(C);j=q?C.length-q[0].length:d.cursor,ee=H.value+(B.startsWith(" ")?"":" ")}else{let q=C.search(/[^\s]*$/);j=q>=0?q:d.cursor,ee=H.value}return d=U.replaceRange(d,{start:j,end:d.cursor},ee),m.dropdownOpen=!1,m.viewportStart=0,m.selectedIndex=0,M(),!0},k=()=>{(w>0||y>0)&&n.write(ct.cursorUp(w+y)),n.write("\r"),n.write(ct.eraseDown),f=0;let H=fr({buffer:Wn(d.buffer,_),promptText:s,isTTY:!!n.isTTY,attachmentSummary:Po(R)}),C=()=>n.write(H+`
2199
+ `),w=1}else w=0;n.write(s+Wn(d.buffer,_)),p.trigger=zs(d.buffer,d.cursor);let H=`${d.cursor}:${d.buffer}`;p.suppressedSignature!==null&&p.suppressedSignature!==H&&(p.suppressedSignature=null),p.trigger&&p.suppressedSignature===null?(p.trigger.kind==="slash"?p.candidates=Al(p.trigger.query).slice(0,12):p.trigger.kind==="file"?p.candidates=_l(p.trigger.query).slice(0,12):p.candidates=Js(p.trigger.command,p.trigger.query),p.dropdownOpen=p.candidates.length>0):(p.dropdownOpen=!1,p.candidates=[]),p.selectedIndex>=p.candidates.length&&(p.selectedIndex=Math.max(0,p.candidates.length-1)),p.viewportStart>p.selectedIndex&&(p.viewportStart=p.selectedIndex),p.selectedIndex>=p.viewportStart+v&&(p.viewportStart=p.selectedIndex-v+1);let C=n.columns||80;if(f=0,p.dropdownOpen&&C>40){let L=Math.min(C-4,60),te=Math.min(p.candidates.length-p.viewportStart,v);for(let Me=0;Me<te;Me++){let ut=p.viewportStart+Me,Lr=El(p.candidates[ut],ut===p.selectedIndex,L,p.trigger?.kind);n.write(`
2200
+ `+Lr);let ns=Rm(Ie(Lr));f+=Math.max(1,Math.ceil(ns/C))}let Re=Math.min(C-4,80),De=Rl(p.candidates[p.selectedIndex]?.hint,Re);if(De!==null){n.write(`
2201
+ `+De);let Me=Rm(Ie(De));f+=Math.max(1,Math.ceil(Me/C))}}let B=Tp(d.buffer,i,C),{row:j,col:ee}=Un(d.buffer,d.cursor,i,C),q=Math.max(0,B-j+f);q>0&&n.write(ct.cursorUp(q)),n.write("\r"),ee>0&&n.write(ct.cursorForward(ee)),y=B},P=!1,$=()=>{I||(I=!0,setImmediate(()=>{I&&!P&&(I=!1,M())}))};M();let F=()=>{let H=p.candidates[p.selectedIndex];if(!H)return!1;let C=d.buffer.slice(0,d.cursor),B=d.buffer.slice(d.cursor),j,ee;if(p.trigger?.kind==="slash"){let q=/\/[A-Za-z_-]*$/.exec(C);j=q?C.length-q[0].length:d.cursor,ee=H.value+(B.startsWith(" ")?"":" ")}else if(p.trigger?.kind==="flag"){let q=/--[a-z0-9-]*$/.exec(C);j=q?C.length-q[0].length:d.cursor,ee=H.value+(B.startsWith(" ")?"":" ")}else{let q=C.search(/[^\s]*$/);j=q>=0?q:d.cursor,ee=H.value}return d=U.replaceRange(d,{start:j,end:d.cursor},ee),p.dropdownOpen=!1,p.viewportStart=0,p.selectedIndex=0,M(),!0},k=()=>{(w>0||y>0)&&n.write(ct.cursorUp(w+y)),n.write("\r"),n.write(ct.eraseDown),f=0;let H=fr({buffer:Wn(d.buffer,_),promptText:s,isTTY:!!n.isTTY,attachmentSummary:Po(R)}),C=()=>n.write(H+`
2202
2202
  `);e.statusLine?.withFullScrollRegion?e.statusLine.withFullScrollRegion(C):C(),N(),c({text:d.buffer,attachments:[...R]}),y=0},A=H=>{y>0&&n.write(ct.cursorUp(y)),f>0&&(n.write(ct.eraseDown),f=0),n.write(`
2203
2203
  `),N(),u(H),y=0},N=()=>{P=!0,a&&t.removeListener("keypress",a),l&&l(),a=null,l=null};a=(H,C)=>{let B=Date.now(),j=B-E<D;E=B;let ee=C?.sequence||"";if(ee==="\x1B[200~"){g=!0,b=d.buffer.length;return}if(ee==="\x1B[201~"){g=!1,d.buffer.length===b?h||(h=!0,br().then(L=>{L?(x=null,R.push(L)):x="[clipboard: no image found]",$()}).catch(()=>{}).finally(()=>{h=!1})):(M(),h||(h=!0,br().then(L=>{L&&(x=null,R.push(L),$())}).catch(()=>{}).finally(()=>{h=!1})));return}if(C?.ctrl&&C?.name==="c"){e.onSigint?e.onSigint():A(new Error("SIGINT"));return}if(C?.ctrl&&C?.name==="d"){d.buffer.length===0&&((w>0||y>0)&&n.write(ct.cursorUp(w+y)),f>0&&(n.write(ct.eraseDown),f=0),n.write(`
2204
- `),N(),c({text:"",attachments:[...R]}),y=0);return}if(C?.ctrl&&C?.name==="v"){h||(h=!0,br().then(L=>{L?(x=null,R.push(L)):x="[clipboard: no image found]",$()}).catch(()=>{}).finally(()=>{h=!1}));return}if(C?.name==="escape"){m.dropdownOpen&&(m.suppressedSignature=`${d.cursor}:${d.buffer}`,m.dropdownOpen=!1,m.candidates=[],M());return}if(C?.ctrl&&C?.name==="a"){let L=U.moveLineStart(d);L!==d&&(d=L,M());return}if(C?.ctrl&&C?.name==="e"){let L=U.moveLineEnd(d);L!==d&&(d=L,M());return}if(C?.ctrl&&C?.name==="b"){let L=U.moveLeft(d);L!==d&&(d=L,M());return}if(C?.ctrl&&C?.name==="f"){let L=U.moveRight(d);L!==d&&(d=L,M());return}if(C?.meta&&C?.name==="b"){let L=U.moveWordBackward(d);L!==d&&(d=L,M());return}if(C?.meta&&C?.name==="f"){let L=U.moveWordForward(d);L!==d&&(d=L,M());return}if(C?.ctrl&&C?.name==="w"){let L=U.deleteWordBackward(d);L!==d&&(d=L,e.history?.resetRecall(),M());return}if(C?.ctrl&&C?.name==="l"){y=0,w=0,n.write("\x1B[H\x1B[2J"),M();return}if(C?.ctrl&&C?.name==="p"||C?.name==="up"){if(m.dropdownOpen){m.selectedIndex>0&&(m.selectedIndex--,m.selectedIndex<m.viewportStart&&(m.viewportStart=m.selectedIndex),M());return}let L=n.columns||80,te=U.moveUpLine(d,L,i);if(te.moved)d=te.state,e.history?.resetRecall(),M();else if(e.history){let Re=e.history.back(d.buffer);Re!==null&&(d=U.seed(Re),M())}return}if(C?.ctrl&&C?.name==="n"||C?.name==="down"){if(m.dropdownOpen){m.selectedIndex<m.candidates.length-1&&(m.selectedIndex++,m.selectedIndex>=m.viewportStart+v&&(m.viewportStart=m.selectedIndex-v+1),M());return}let L=n.columns||80,te=U.moveDownLine(d,L,i);if(te.moved)d=te.state,e.history?.resetRecall(),M();else if(e.history){let Re=e.history.forward();Re!==null&&(d=U.seed(Re),M())}return}if(C?.name==="left"){let L=U.moveLeft(d);L!==d&&(d=L,M());return}if(C?.name==="right"){let L=U.moveRight(d);L!==d&&(d=L,M());return}if(C?.name==="home"){let L=U.moveHome(d);L!==d&&(d=L,M());return}if(C?.name==="end"){let L=U.moveEnd(d);L!==d&&(d=L,M());return}if(C?.ctrl&&C?.name==="u"){let L=U.deleteToLineStart(d);L!==d&&(d=L,e.history?.resetRecall(),M());return}if(C?.ctrl&&C?.name==="k"){let L=U.deleteToLineEnd(d);L!==d&&(d=L,e.history?.resetRecall(),M());return}if(C?.ctrl&&C?.name==="x"){R.length>0&&(R.pop(),M());return}if(C?.name==="backspace"){if(C?.meta){let te=U.deleteWordBackward(d);te!==d&&(d=te,e.history?.resetRecall(),M());return}let L=U.backspace(d);L!==d?(d=L,e.history?.resetRecall(),M()):R.length>0&&(R.pop(),M());return}if(C?.name==="delete"){if(C?.meta){let te=U.deleteWordForward(d);te!==d&&(d=te,e.history?.resetRecall(),M());return}let L=U.deleteForward(d);L!==d&&(d=L,e.history?.resetRecall(),M());return}if(C?.name==="return"){let L=C?.shift===!0||ee==="\x1B[13;2u",te=C?.meta===!0;if(L||te){d=U.insert(d,`
2204
+ `),N(),c({text:"",attachments:[...R]}),y=0);return}if(C?.ctrl&&C?.name==="v"){h||(h=!0,br().then(L=>{L?(x=null,R.push(L)):x="[clipboard: no image found]",$()}).catch(()=>{}).finally(()=>{h=!1}));return}if(C?.name==="escape"){p.dropdownOpen&&(p.suppressedSignature=`${d.cursor}:${d.buffer}`,p.dropdownOpen=!1,p.candidates=[],M());return}if(C?.ctrl&&C?.name==="a"){let L=U.moveLineStart(d);L!==d&&(d=L,M());return}if(C?.ctrl&&C?.name==="e"){let L=U.moveLineEnd(d);L!==d&&(d=L,M());return}if(C?.ctrl&&C?.name==="b"){let L=U.moveLeft(d);L!==d&&(d=L,M());return}if(C?.ctrl&&C?.name==="f"){let L=U.moveRight(d);L!==d&&(d=L,M());return}if(C?.meta&&C?.name==="b"){let L=U.moveWordBackward(d);L!==d&&(d=L,M());return}if(C?.meta&&C?.name==="f"){let L=U.moveWordForward(d);L!==d&&(d=L,M());return}if(C?.ctrl&&C?.name==="w"){let L=U.deleteWordBackward(d);L!==d&&(d=L,e.history?.resetRecall(),M());return}if(C?.ctrl&&C?.name==="l"){y=0,w=0,n.write("\x1B[H\x1B[2J"),M();return}if(C?.ctrl&&C?.name==="p"||C?.name==="up"){if(p.dropdownOpen){p.selectedIndex>0&&(p.selectedIndex--,p.selectedIndex<p.viewportStart&&(p.viewportStart=p.selectedIndex),M());return}let L=n.columns||80,te=U.moveUpLine(d,L,i);if(te.moved)d=te.state,e.history?.resetRecall(),M();else if(e.history){let Re=e.history.back(d.buffer);Re!==null&&(d=U.seed(Re),M())}return}if(C?.ctrl&&C?.name==="n"||C?.name==="down"){if(p.dropdownOpen){p.selectedIndex<p.candidates.length-1&&(p.selectedIndex++,p.selectedIndex>=p.viewportStart+v&&(p.viewportStart=p.selectedIndex-v+1),M());return}let L=n.columns||80,te=U.moveDownLine(d,L,i);if(te.moved)d=te.state,e.history?.resetRecall(),M();else if(e.history){let Re=e.history.forward();Re!==null&&(d=U.seed(Re),M())}return}if(C?.name==="left"){let L=U.moveLeft(d);L!==d&&(d=L,M());return}if(C?.name==="right"){let L=U.moveRight(d);L!==d&&(d=L,M());return}if(C?.name==="home"){let L=U.moveHome(d);L!==d&&(d=L,M());return}if(C?.name==="end"){let L=U.moveEnd(d);L!==d&&(d=L,M());return}if(C?.ctrl&&C?.name==="u"){let L=U.deleteToLineStart(d);L!==d&&(d=L,e.history?.resetRecall(),M());return}if(C?.ctrl&&C?.name==="k"){let L=U.deleteToLineEnd(d);L!==d&&(d=L,e.history?.resetRecall(),M());return}if(C?.ctrl&&C?.name==="x"){R.length>0&&(R.pop(),M());return}if(C?.name==="backspace"){if(C?.meta){let te=U.deleteWordBackward(d);te!==d&&(d=te,e.history?.resetRecall(),M());return}let L=U.backspace(d);L!==d?(d=L,e.history?.resetRecall(),M()):R.length>0&&(R.pop(),M());return}if(C?.name==="delete"){if(C?.meta){let te=U.deleteWordForward(d);te!==d&&(d=te,e.history?.resetRecall(),M());return}let L=U.deleteForward(d);L!==d&&(d=L,e.history?.resetRecall(),M());return}if(C?.name==="return"){let L=C?.shift===!0||ee==="\x1B[13;2u",te=C?.meta===!0;if(L||te){d=U.insert(d,`
2205
2205
  `),e.history?.resetRecall(),M();return}if(g){d=U.insert(d,`
2206
2206
  `);return}if(j){d=U.insert(d,`
2207
- `),$();return}if(m.dropdownOpen){let Re=m.trigger?.kind,De=F();Re==="slash"&&De&&k()}else d.buffer.endsWith("\\")?(d=U.replaceRange(d,{start:d.buffer.length-1,end:d.buffer.length},`
2208
- `),M()):k();return}if(C?.shift&&C?.name==="tab"||C?.sequence==="\x1B[Z"){e.onShiftTab?.();return}if(C?.name==="tab"){m.dropdownOpen&&F();return}let q=typeof H=="string"&&H.length===1&&H>=" "&&!C?.ctrl&&!C?.meta?H:typeof C?.sequence=="string"&&C.sequence.length===1&&C.sequence>=" "&&!C?.ctrl&&!C?.meta?C.sequence:null;q!==null&&(d=U.insert(d,q),e.history?.resetRecall(),g||(j?$():M()))},l=Ue.subscribe(()=>{y=0,w=0,f=0,M()}),t.on("keypress",a)})}finally{e.statusLine?.setExtraRows(o),r.restore()}}async function ux(e){return!process.stdout.isTTY||!process.stdin.isTTY?ax(e):cx(e)}var vc=class{history;autocompleteState;rl;statusLine;compositor=null;armedStdout=null;backgroundHandler=null;softStopHandler=null;pendingReadReject=null;slashRegistryView={has:t=>Ze().some(n=>n.name===`/${t}`)};constructor(t){this.rl=t.rl,this.history=t.history,this.statusLine=t.statusLine,this.autocompleteState=kc()}async armCompositor(t){if(this.compositor)return;let n=t.stdout??process.stdout,r=t.stdin??process.stdin;if(!n.isTTY||!r.isTTY)return;let o=new Mo({stdout:n,stdin:r,promptText:t.promptFn,onCancel:t.onCancel,onSoftStop:()=>{this.softStopHandler?.()},onBackground:()=>{this.backgroundHandler?.()},...t.onShiftTab?{onShiftTab:t.onShiftTab}:{},history:this.history,autocompleteState:this.autocompleteState,formatInputBuffer:s=>Wn(s,this.slashRegistryView),...t.scrollRegion?{scrollRegion:t.scrollRegion}:{},...t.anchorRow!==void 0?{anchorRow:t.anchorRow}:{}});await o.arm(),o.setInputMode("idle"),this.compositor=o,this.armedStdout=n}async dispose(){if(this.compositor){if(this.pendingReadReject){this.compositor.setOnSubmit(null);let t=this.pendingReadReject;this.pendingReadReject=null,t(new Error("InputSurface disposed while readLine was in progress"))}try{this.compositor.disarm()}catch{}this.compositor=null,this.armedStdout=null,this.backgroundHandler=null,this.softStopHandler=null}}getCompositor(){return this.compositor}setBackgroundHandler(t){this.backgroundHandler=t}setSoftStopHandler(t){this.softStopHandler=t}suspendForElicitation(){this.compositor?.suspendInput()}resumeAfterElicitation(){this.compositor?.resumeInput()}async readLine(t){if(this.compositor&&this.compositor.isArmed()){let n=this.compositor;return new Promise((r,o)=>{this.pendingReadReject=o;let s=i=>{n.setOnSubmit(null),this.pendingReadReject=null;let a=this.armedStdout??process.stdout,l=i.displayText??i.text,c=fr({buffer:Wn(l,this.slashRegistryView),promptText:t.promptFn(),isTTY:!!a.isTTY,attachmentSummary:Po([...i.attachments])});for(let u of c.split(`
2209
- `))n.commitAbove(u);r({text:i.text,attachments:[...i.attachments]})};n.setOnSubmit(s),n.setInputMode("idle")})}return ux({rl:this.rl,promptFn:t.promptFn,...t.onSigint?{onSigint:t.onSigint}:{},...t.onShiftTab?{onShiftTab:t.onShiftTab}:{},...t.compositor?{compositor:t.compositor}:{},history:this.history,autocompleteState:this.autocompleteState,...this.statusLine?{statusLine:this.statusLine}:{}})}toRunTurnRefs(t){return{history:this.history,autocompleteState:this.autocompleteState,promptText:t}}};var Z1="\u25B8",Q1=" ",eW="\u25C9",tW="\u25EF",nW="\u2191/\u2193 navigate \xB7 enter select \xB7 esc cancel",rW="\u2191/\u2193 navigate \xB7 space toggle \xB7 enter confirm \xB7 esc cancel";function dx(e,t){return new Promise(n=>{let{header:r,options:o,multi:s=!1,signal:i,initialIndex:a=0}=t;if(o.length===0){n(null);return}if(i?.aborted){n(null);return}let l=oW(a,0,o.length-1),c=new Set(t.initialSelected??[]),u=!1,d=b=>{u||(u=!0,i&&i.removeEventListener("abort",m),e.exitPickerMode(),n(b))},m=()=>d(null);i&&i.addEventListener("abort",m,{once:!0});let h={renderRows:()=>{let b=[];for(let y of r)b.push(y);for(let y=0;y<o.length;y++){let w=o[y]??"",x=y===l,R=x?p.brand(Z1):Q1,v;if(s){let E=c.has(y),I=E?p.success(eW):p.dim(tW),D=x&&!E?p.bold(w):w;v=` ${R} ${I} ${D}`}else{let E=x?p.bold(w):p.dim(w);v=` ${R} ${E}`}b.push(v)}return b.push(p.dim(" "+(s?rW:nW))),b},onKey:(b,y)=>{if(!u){if(y.name==="escape"||y.ctrl&&y.name==="c"){d(null);return}if(y.name==="up"||y.ctrl&&y.name==="p"){l=l===0?o.length-1:l-1,e.repaintPicker();return}if(y.name==="down"||y.ctrl&&y.name==="n"){l=l===o.length-1?0:l+1,e.repaintPicker();return}if(y.name==="return"){if(s){let w=[];for(let x=0;x<o.length;x++)if(c.has(x)){let R=o[x];R!==void 0&&w.push(R)}d(w)}else{let w=o[l];d(w!==void 0?[w]:[])}return}if(s&&(y.name==="space"||b===" ")){c.has(l)?c.delete(l):c.add(l),e.repaintPicker();return}if(y.name==="home"){l=0,e.repaintPicker();return}if(y.name==="end"){l=o.length-1,e.repaintPicker();return}}}};e.enterPickerMode(h)})}function oW(e,t,n){return n<t||e<t?t:e>n?n:e}var sW="enter to submit \xB7 esc to cancel",iW=">";function px(e,t){return new Promise(n=>{let{header:r,initial:o="",help:s=sW,validate:i,signal:a}=t;if(a?.aborted){n(null);return}let l=U.seed(o),c=null,u=!1,d=b=>{u||(u=!0,a&&a.removeEventListener("abort",m),e.exitPickerMode(),n(b))},m=()=>d(null);a&&a.addEventListener("abort",m,{once:!0});let h={renderRows:()=>{let b=[];for(let y of r)b.push(y);return b.push(aW(l)),c!==null&&b.push(p.warning(" "+c)),b.push(p.dim(" "+s)),b},onKey:(b,y)=>{if(u)return;if(y.name==="escape"||y.ctrl&&y.name==="c"){d(null);return}if(y.name==="return"){if(i){let x=i(l.buffer);if(x!==null){c=x,e.repaintPicker();return}}d(l.buffer);return}let w=c!==null;if(y.name==="left"||y.ctrl&&y.name==="b"){l=U.moveLeft(l),c=null,e.repaintPicker();return}if(y.name==="right"||y.ctrl&&y.name==="f"){l=U.moveRight(l),c=null,e.repaintPicker();return}if(y.name==="home"||y.ctrl&&y.name==="a"){l=U.moveHome(l),c=null,e.repaintPicker();return}if(y.name==="end"||y.ctrl&&y.name==="e"){l=U.moveEnd(l),c=null,e.repaintPicker();return}if(y.name==="backspace"){l=U.backspace(l),c=null,e.repaintPicker();return}if(y.name==="delete"){l=U.deleteForward(l),c=null,e.repaintPicker();return}if(y.ctrl&&y.name==="w"){l=U.deleteWordBackward(l),c=null,e.repaintPicker();return}if(y.ctrl&&y.name==="u"){l=U.deleteToLineStart(l),c=null,e.repaintPicker();return}if(y.ctrl&&y.name==="k"){l=U.deleteToLineEnd(l),c=null,e.repaintPicker();return}if(b!==void 0&&b.length>0&&!lW(b)){l=U.insert(l,b),c=null,e.repaintPicker();return}w&&(c=null,e.repaintPicker())}};e.enterPickerMode(h)})}function aW(e){let{buffer:t,cursor:n}=e,r=t.slice(0,n),o=n<t.length?t.charAt(n):" ",s=n<t.length?t.slice(n+1):"",i=p.user.inverse(o);return` ${p.dim(iW)} ${r}${i}${s}`}function lW(e){if(e.length!==1)return!1;let t=e.charCodeAt(0);return t<32||t===127}G();function mx(e){if(!e)return null;let t=e.split(`
2207
+ `),$();return}if(p.dropdownOpen){let Re=p.trigger?.kind,De=F();Re==="slash"&&De&&k()}else d.buffer.endsWith("\\")?(d=U.replaceRange(d,{start:d.buffer.length-1,end:d.buffer.length},`
2208
+ `),M()):k();return}if(C?.shift&&C?.name==="tab"||C?.sequence==="\x1B[Z"){e.onShiftTab?.();return}if(C?.name==="tab"){p.dropdownOpen&&F();return}let q=typeof H=="string"&&H.length===1&&H>=" "&&!C?.ctrl&&!C?.meta?H:typeof C?.sequence=="string"&&C.sequence.length===1&&C.sequence>=" "&&!C?.ctrl&&!C?.meta?C.sequence:null;q!==null&&(d=U.insert(d,q),e.history?.resetRecall(),g||(j?$():M()))},l=Ue.subscribe(()=>{y=0,w=0,f=0,M()}),t.on("keypress",a)})}finally{e.statusLine?.setExtraRows(o),r.restore()}}async function ux(e){return!process.stdout.isTTY||!process.stdin.isTTY?ax(e):cx(e)}var vc=class{history;autocompleteState;rl;statusLine;compositor=null;armedStdout=null;backgroundHandler=null;softStopHandler=null;pendingReadReject=null;slashRegistryView={has:t=>Ze().some(n=>n.name===`/${t}`)};constructor(t){this.rl=t.rl,this.history=t.history,this.statusLine=t.statusLine,this.autocompleteState=kc()}async armCompositor(t){if(this.compositor)return;let n=t.stdout??process.stdout,r=t.stdin??process.stdin;if(!n.isTTY||!r.isTTY)return;let o=new Mo({stdout:n,stdin:r,promptText:t.promptFn,onCancel:t.onCancel,onSoftStop:()=>{this.softStopHandler?.()},onBackground:()=>{this.backgroundHandler?.()},...t.onShiftTab?{onShiftTab:t.onShiftTab}:{},history:this.history,autocompleteState:this.autocompleteState,formatInputBuffer:s=>Wn(s,this.slashRegistryView),...t.scrollRegion?{scrollRegion:t.scrollRegion}:{},...t.anchorRow!==void 0?{anchorRow:t.anchorRow}:{}});await o.arm(),o.setInputMode("idle"),this.compositor=o,this.armedStdout=n}async dispose(){if(this.compositor){if(this.pendingReadReject){this.compositor.setOnSubmit(null);let t=this.pendingReadReject;this.pendingReadReject=null,t(new Error("InputSurface disposed while readLine was in progress"))}try{this.compositor.disarm()}catch{}this.compositor=null,this.armedStdout=null,this.backgroundHandler=null,this.softStopHandler=null}}getCompositor(){return this.compositor}setBackgroundHandler(t){this.backgroundHandler=t}setSoftStopHandler(t){this.softStopHandler=t}suspendForElicitation(){this.compositor?.suspendInput()}resumeAfterElicitation(){this.compositor?.resumeInput()}async readLine(t){if(this.compositor&&this.compositor.isArmed()){let n=this.compositor;return new Promise((r,o)=>{this.pendingReadReject=o;let s=i=>{n.setOnSubmit(null),this.pendingReadReject=null;let a=this.armedStdout??process.stdout,l=i.displayText??i.text,c=fr({buffer:Wn(l,this.slashRegistryView),promptText:t.promptFn(),isTTY:!!a.isTTY,attachmentSummary:Po([...i.attachments])});for(let u of c.split(`
2209
+ `))n.commitAbove(u);r({text:i.text,attachments:[...i.attachments]})};n.setOnSubmit(s),n.setInputMode("idle")})}return ux({rl:this.rl,promptFn:t.promptFn,...t.onSigint?{onSigint:t.onSigint}:{},...t.onShiftTab?{onShiftTab:t.onShiftTab}:{},...t.compositor?{compositor:t.compositor}:{},history:this.history,autocompleteState:this.autocompleteState,...this.statusLine?{statusLine:this.statusLine}:{}})}toRunTurnRefs(t){return{history:this.history,autocompleteState:this.autocompleteState,promptText:t}}};var Zj="\u25B8",Qj=" ",eW="\u25C9",tW="\u25EF",nW="\u2191/\u2193 navigate \xB7 enter select \xB7 esc cancel",rW="\u2191/\u2193 navigate \xB7 space toggle \xB7 enter confirm \xB7 esc cancel";function dx(e,t){return new Promise(n=>{let{header:r,options:o,multi:s=!1,signal:i,initialIndex:a=0}=t;if(o.length===0){n(null);return}if(i?.aborted){n(null);return}let l=oW(a,0,o.length-1),c=new Set(t.initialSelected??[]),u=!1,d=b=>{u||(u=!0,i&&i.removeEventListener("abort",p),e.exitPickerMode(),n(b))},p=()=>d(null);i&&i.addEventListener("abort",p,{once:!0});let h={renderRows:()=>{let b=[];for(let y of r)b.push(y);for(let y=0;y<o.length;y++){let w=o[y]??"",x=y===l,R=x?m.brand(Zj):Qj,v;if(s){let E=c.has(y),I=E?m.success(eW):m.dim(tW),D=x&&!E?m.bold(w):w;v=` ${R} ${I} ${D}`}else{let E=x?m.bold(w):m.dim(w);v=` ${R} ${E}`}b.push(v)}return b.push(m.dim(" "+(s?rW:nW))),b},onKey:(b,y)=>{if(!u){if(y.name==="escape"||y.ctrl&&y.name==="c"){d(null);return}if(y.name==="up"||y.ctrl&&y.name==="p"){l=l===0?o.length-1:l-1,e.repaintPicker();return}if(y.name==="down"||y.ctrl&&y.name==="n"){l=l===o.length-1?0:l+1,e.repaintPicker();return}if(y.name==="return"){if(s){let w=[];for(let x=0;x<o.length;x++)if(c.has(x)){let R=o[x];R!==void 0&&w.push(R)}d(w)}else{let w=o[l];d(w!==void 0?[w]:[])}return}if(s&&(y.name==="space"||b===" ")){c.has(l)?c.delete(l):c.add(l),e.repaintPicker();return}if(y.name==="home"){l=0,e.repaintPicker();return}if(y.name==="end"){l=o.length-1,e.repaintPicker();return}}}};e.enterPickerMode(h)})}function oW(e,t,n){return n<t||e<t?t:e>n?n:e}var sW="enter to submit \xB7 esc to cancel",iW=">";function px(e,t){return new Promise(n=>{let{header:r,initial:o="",help:s=sW,validate:i,signal:a}=t;if(a?.aborted){n(null);return}let l=U.seed(o),c=null,u=!1,d=b=>{u||(u=!0,a&&a.removeEventListener("abort",p),e.exitPickerMode(),n(b))},p=()=>d(null);a&&a.addEventListener("abort",p,{once:!0});let h={renderRows:()=>{let b=[];for(let y of r)b.push(y);return b.push(aW(l)),c!==null&&b.push(m.warning(" "+c)),b.push(m.dim(" "+s)),b},onKey:(b,y)=>{if(u)return;if(y.name==="escape"||y.ctrl&&y.name==="c"){d(null);return}if(y.name==="return"){if(i){let x=i(l.buffer);if(x!==null){c=x,e.repaintPicker();return}}d(l.buffer);return}let w=c!==null;if(y.name==="left"||y.ctrl&&y.name==="b"){l=U.moveLeft(l),c=null,e.repaintPicker();return}if(y.name==="right"||y.ctrl&&y.name==="f"){l=U.moveRight(l),c=null,e.repaintPicker();return}if(y.name==="home"||y.ctrl&&y.name==="a"){l=U.moveHome(l),c=null,e.repaintPicker();return}if(y.name==="end"||y.ctrl&&y.name==="e"){l=U.moveEnd(l),c=null,e.repaintPicker();return}if(y.name==="backspace"){l=U.backspace(l),c=null,e.repaintPicker();return}if(y.name==="delete"){l=U.deleteForward(l),c=null,e.repaintPicker();return}if(y.ctrl&&y.name==="w"){l=U.deleteWordBackward(l),c=null,e.repaintPicker();return}if(y.ctrl&&y.name==="u"){l=U.deleteToLineStart(l),c=null,e.repaintPicker();return}if(y.ctrl&&y.name==="k"){l=U.deleteToLineEnd(l),c=null,e.repaintPicker();return}if(b!==void 0&&b.length>0&&!lW(b)){l=U.insert(l,b),c=null,e.repaintPicker();return}w&&(c=null,e.repaintPicker())}};e.enterPickerMode(h)})}function aW(e){let{buffer:t,cursor:n}=e,r=t.slice(0,n),o=n<t.length?t.charAt(n):" ",s=n<t.length?t.slice(n+1):"",i=m.user.inverse(o);return` ${m.dim(iW)} ${r}${i}${s}`}function lW(e){if(e.length!==1)return!1;let t=e.charCodeAt(0);return t<32||t===127}G();function mx(e){if(!e)return null;let t=e.split(`
2210
2210
  `),n=t.slice(Math.max(0,t.length-40)),r=-1,o=null;for(let l=n.length-1;l>=0;l--){let c=n[l]??"",u=cW(c);if(u){r=l,o=u;break}}if(o===null||r<0)return null;let s=n.slice(r+1).map(l=>l.trim());for(;s.length>0&&s[0]==="";)s.shift();for(;s.length>0&&s[s.length-1]==="";)s.pop();let i=uW(s),a=s.join(`
2211
- `).trim();return{kind:o,rawBody:a,...dW(o,i)}}function cW(e){let t=e.trim().replace(/^#{1,6}\s+/,"").replace(/^[-•▶▸]\s+/,"").replace(/^\*\s+/,"").replace(/^\*\*(.+?)\*\*$/,"$1").replace(/^__(.+?)__$/,"$1").replace(/^\*(.+?)\*$/,"$1").replace(/^_(.+?)_$/,"$1").replace(/[.:!?\s]+$/,"").trim();if(t.length===0||t.length>24)return null;let n=t.toLowerCase();return n==="done"?"done":n==="blocked"?"blocked":n==="asking"?"asking":n==="interrupted"?"interrupted":null}function uW(e){let t=[];for(let n of e){let r=n.trim();if(r==="")continue;let o=/^(?:[-*•▶▸]|\d+[.)])\s+(.*)$/.exec(r),s=o?(o[1]??"").trim():r;if(!o&&t.length>0&&r.length>0){let a=t[t.length-1];a.value=`${a.value} ${r}`.trim();continue}let i=s.indexOf(":");if(i>0&&i<60){let a=s.slice(0,i).trim().replace(/^\*\*(.+?)\*\*$/,"$1").replace(/^__(.+?)__$/,"$1").toLowerCase(),l=s.slice(i+1).trim();t.push({label:a,value:l})}else t.push({label:"",value:s})}return t}function dW(e,t){let n=(...r)=>{for(let o of t)if(o.label!==""){for(let s of r)if(o.label.includes(s))return o.value}};switch(e){case"done":{let r={},o=n("what was done","what i did","completed","done");o!==void 0&&(r.whatWasDone=o);let s=n("evidence","what changed","change","artifact","output");s!==void 0&&(r.evidence=s);let i=n("pending","deferred","follow-up","followup","next");return i!==void 0&&(r.deferred=i),r}case"blocked":{let r={},o=n("what blocks","blocker","blocked by");o!==void 0&&(r.whatBlocks=o);let s=n("unblock","must change","to unblock","condition");s!==void 0&&(r.unblockCondition=s);let i=n("has already","been done","already done","what has been done","progress");return i!==void 0&&(r.alreadyDone=i),r}case"asking":{let r={},o=n("question","asking");o!==void 0&&(r.question=o);let s=n("assumption","resolves");s!==void 0&&(r.assumption=s);let i=n("once answered","follow-up","next","will do","after");return i!==void 0&&(r.followup=i),r}case"interrupted":{let r={},o=n("what you were doing","in progress","doing","task");o!==void 0&&(r.whatWasInProgress=o);let s=n("state was saved","state","saved","where");s!==void 0&&(r.stateLocation=s);let i=n("resumption","resume","requires");return i!==void 0&&(r.resumeRequires=i),r}}}var pW={done:{color:p.success,chip:"\u2713 Done",affordance:"Objective satisfied \u2014 review evidence and close."},blocked:{color:p.error,chip:"\u2298 Blocked",affordance:"External dependency \u2014 unblock above to resume."},asking:{color:p.warning,chip:"? Asking",affordance:"Waiting on you \u2014 answer above to continue."},interrupted:{color:p.meta,chip:"\u23F8 Interrupted",affordance:"Halted with state preserved \u2014 resume when ready."}};function fx(e){let t=pW[e.kind],n=Math.max(34,Math.min(Y()-6,100)),r=n+4,o=t.color("\u256D\u2500")+t.color.call(null,` ${t.chip} `)+t.color("\u2500".repeat(Math.max(0,r-1-z(` ${t.chip} `)))+"\u256E"),s=t.color("\u2570"+"\u2500".repeat(r)+"\u256F"),i=t.color("\u2502"),a=i+" ".repeat(n+4)+i,l=mW(e),c=l.reduce((f,g)=>Math.max(f,z(g.label)),0),u=Math.max(8,n-c-2),d=[o,a];if(l.length===0){let f=e.rawBody.split(`
2211
+ `).trim();return{kind:o,rawBody:a,...dW(o,i)}}function cW(e){let t=e.trim().replace(/^#{1,6}\s+/,"").replace(/^[-•▶▸]\s+/,"").replace(/^\*\s+/,"").replace(/^\*\*(.+?)\*\*$/,"$1").replace(/^__(.+?)__$/,"$1").replace(/^\*(.+?)\*$/,"$1").replace(/^_(.+?)_$/,"$1").replace(/[.:!?\s]+$/,"").trim();if(t.length===0||t.length>24)return null;let n=t.toLowerCase();return n==="done"?"done":n==="blocked"?"blocked":n==="asking"?"asking":n==="interrupted"?"interrupted":null}function uW(e){let t=[];for(let n of e){let r=n.trim();if(r==="")continue;let o=/^(?:[-*•▶▸]|\d+[.)])\s+(.*)$/.exec(r),s=o?(o[1]??"").trim():r;if(!o&&t.length>0&&r.length>0){let a=t[t.length-1];a.value=`${a.value} ${r}`.trim();continue}let i=s.indexOf(":");if(i>0&&i<60){let a=s.slice(0,i).trim().replace(/^\*\*(.+?)\*\*$/,"$1").replace(/^__(.+?)__$/,"$1").toLowerCase(),l=s.slice(i+1).trim();t.push({label:a,value:l})}else t.push({label:"",value:s})}return t}function dW(e,t){let n=(...r)=>{for(let o of t)if(o.label!==""){for(let s of r)if(o.label.includes(s))return o.value}};switch(e){case"done":{let r={},o=n("what was done","what i did","completed","done");o!==void 0&&(r.whatWasDone=o);let s=n("evidence","what changed","change","artifact","output");s!==void 0&&(r.evidence=s);let i=n("pending","deferred","follow-up","followup","next");return i!==void 0&&(r.deferred=i),r}case"blocked":{let r={},o=n("what blocks","blocker","blocked by");o!==void 0&&(r.whatBlocks=o);let s=n("unblock","must change","to unblock","condition");s!==void 0&&(r.unblockCondition=s);let i=n("has already","been done","already done","what has been done","progress");return i!==void 0&&(r.alreadyDone=i),r}case"asking":{let r={},o=n("question","asking");o!==void 0&&(r.question=o);let s=n("assumption","resolves");s!==void 0&&(r.assumption=s);let i=n("once answered","follow-up","next","will do","after");return i!==void 0&&(r.followup=i),r}case"interrupted":{let r={},o=n("what you were doing","in progress","doing","task");o!==void 0&&(r.whatWasInProgress=o);let s=n("state was saved","state","saved","where");s!==void 0&&(r.stateLocation=s);let i=n("resumption","resume","requires");return i!==void 0&&(r.resumeRequires=i),r}}}var pW={done:{color:m.success,chip:"\u2713 Done",affordance:"Objective satisfied \u2014 review evidence and close."},blocked:{color:m.error,chip:"\u2298 Blocked",affordance:"External dependency \u2014 unblock above to resume."},asking:{color:m.warning,chip:"? Asking",affordance:"Waiting on you \u2014 answer above to continue."},interrupted:{color:m.meta,chip:"\u23F8 Interrupted",affordance:"Halted with state preserved \u2014 resume when ready."}};function fx(e){let t=pW[e.kind],n=Math.max(34,Math.min(Y()-6,100)),r=n+4,o=t.color("\u256D\u2500")+t.color.call(null,` ${t.chip} `)+t.color("\u2500".repeat(Math.max(0,r-1-z(` ${t.chip} `)))+"\u256E"),s=t.color("\u2570"+"\u2500".repeat(r)+"\u256F"),i=t.color("\u2502"),a=i+" ".repeat(n+4)+i,l=mW(e),c=l.reduce((f,g)=>Math.max(f,z(g.label)),0),u=Math.max(8,n-c-2),d=[o,a];if(l.length===0){let f=e.rawBody.split(`
2212
2212
  `).find(b=>b.trim().length>0)?.trim()??"",g=f.length>0?f:`${e.kind} (no structured fields)`,h=me(En(g),n).split(`
2213
- `);for(let b of h)d.push(i+" "+He(b,n)+" "+i)}else for(let f of l){let g=p.dim(He(f.label,c)),h=me(En(f.value),u).split(`
2214
- `),b=h[0]??"";d.push(i+" "+g+" "+He(b,u)+" "+i);for(let y of h.slice(1))d.push(i+" "+" ".repeat(c)+" "+He(y,u)+" "+i)}d.push(a);let m=p.dim(ae(t.affordance,n));return d.push(i+" "+He(m,n)+" "+i),d.push(s),d.join(`
2215
- `)}function mW(e){let t=[],n=(r,o)=>{o&&o.trim().length>0&&t.push({label:r,value:o.trim()})};switch(e.kind){case"done":n("done",e.whatWasDone),n("evidence",e.evidence),n("deferred",e.deferred);break;case"blocked":n("blocks",e.whatBlocks),n("unblock",e.unblockCondition),n("progress",e.alreadyDone);break;case"asking":n("question",e.question),n("resolves",e.assumption),n("after",e.followup);break;case"interrupted":n("was doing",e.whatWasInProgress),n("saved at",e.stateLocation),n("resume",e.resumeRequires);break}return t}function gx(e,t,n){let r=[];return n&&n.trim().length>0&&r.push({type:"text",text:n}),e&&r.push({type:"text",text:e}),zl(r,t),r}async function hx(e,t,n,r,o="summary",s,i,a){let l=Ok(e.text,e.attachments);r.setInFlight(!0);let c="",u=!1,d=!1,m=!1,f=!1,g,h=!1,b=!1,y=0,w=15e3,x=[],R=new Map,v=e.text.startsWith("/")?e.text.split(/[\s:]/)[0]?.slice(1):void 0,E=r.getCompositor?r.getCompositor():null,I=()=>new Bo({out:Uo(s),thinkingMode:o,...v?{activeSkillName:v}:{},onCancel:()=>{t.interrupt().catch(P=>{je()&&console.error(" "+p.error("session.interrupt() failed:"),P)})},...i?{onBackground:()=>{h=!0}}:{},...a?.history?{history:a.history}:{},...a?.autocompleteState?{autocompleteState:a.autocompleteState}:{},...a?.promptText!==void 0?{promptText:a.promptText}:{},...r.scrollRegion?{scrollRegion:r.scrollRegion}:{},...E?{compositor:E}:{},...r.onStageChange?{onStageChange:r.onStageChange}:{}}),D=I(),_=async()=>{if(!m){m=!0;try{await D.dispose()}catch{}}},M=async()=>{await D.arm();let P=D.getCompositor();if(s&&P){let $=P;s.fn=F=>$.commitAbove(F)}r.setActiveCompositor?.(P),r.setInterruptNotifier?.($=>D.setInterrupting($)),r.rearmStatus?.()};try{E?E.commitAbove(""):console.log(),r.setSoftStopHandler&&r.setSoftStopHandler(()=>{b=!0}),await M(),i&&r.setBackgroundHandler&&r.setBackgroundHandler(()=>{h=!0});let P=e.attachments.length===0?e.text:gx(e.text,e.attachments),$=t.sendMessageStream(P);if(await qr((k,A)=>{D.process(k,A)},async()=>{for await(let k of $){if(b){t.interrupt().catch(A=>{je()&&console.error(" "+p.error("soft-stop session.interrupt() failed:"),A)});break}if(h&&i){let A=v??e.text.slice(0,40),N=i.register(A),H=fl(N,i);gl($,c,l,N,i,H,n,r.onTurnComplete,t.abortSignal),await _(),(s??{fn:console.log}).fn(p.dim(` \u2192 backgrounded as ${N.id}: ${N.label}`)),r.setInFlight(!1),r.rearmStatus?.();return}if(k.type==="chunk"&&k.chunk.type==="content"?(c+=k.chunk.content,u=!0):k.type==="message"&&!u&&(c=k.message.content),k.type==="chunk"&&k.chunk.type==="tool_use_detail"){let A=k.chunk,N={toolName:A.toolName,toolUseId:A.toolUseId,input:A.toolInput};R.set(A.toolUseId,N),x.push(N)}else if(k.type==="chunk"&&k.chunk.type==="tool_result"){let A=k.chunk,N=R.get(A.toolUseId);if(N&&(N.result=A.content,N.isError=A.isError,R.delete(A.toolUseId)),r.onContextProgress){let H=Date.now();if(H-y>=w){y=H;try{let C=r.onContextProgress();C instanceof Promise&&await C}catch(C){je()&&console.error(" "+p.error("onContextProgress (status refresh) failed:"),C)}}}}if(k.type==="paused"){await _(),(s??{fn:console.log}).fn(lg({reason:k.reason,...k.resetsAt!==void 0?{resetsAt:k.resetsAt}:{},...k.accountId!==void 0?{accountId:k.accountId}:{},...k.autoResume!==void 0?{autoResume:k.autoResume}:{}}));continue}if(k.type==="resumed"){let A=k.hotSwapped&&k.accountId?`\u25B6 Resumed on ${k.accountId}`:"\u25B6 Resumed";c="",u=!1,x.length=0,R.clear(),f=!1,g=void 0,d=!1,h=!1,D=I(),m=!1,await M(),(s??{fn:console.log}).fn(p.success(A));continue}if(k.type==="error"){await _(),Wr(Br(k.error)),d=!0;continue}D.process(k),k.type==="done"&&(f=!0,g=k.metadata)}}),await _(),b){let k=s?s.fn:console.log;k(p.warning("\u23F8 Stopped \u2014 work so far kept.")+p.dim(" Use /resume or --resume to continue.")),k("")}if(f&&!b){lr(n,l,c,g,x),r.onTurnComplete&&await r.onTurnComplete(l,c).catch(()=>{}),tl(process.stdout);let k=N=>{s?s.fn(N):console.log(N)},A=mx(c);if(A&&(k(fx(A)),k(""),r.onTerminalState))try{r.onTerminalState(A)}catch{}if(fW(g,n,k),r.onAfterTurn){let N=r.onAfterTurn();N instanceof Promise&&await N.catch(()=>{})}}}catch(P){await _(),d||Wr(Br(P))}finally{await _(),s&&(s.fn=s.idleFn),r.setActiveCompositor?.(null),r.setInterruptNotifier?.(null),r.setBackgroundHandler?.(null),r.setSoftStopHandler?.(null),r.setInFlight(!1),r.rearmStatus?.()}}function fW(e,t,n=console.log){if(!e)return;let r=[];e.durationMs&&r.push(ne(e.durationMs)),e.totalCostUsd!==void 0&&r.push(Ne(e.totalCostUsd));let o=Number(e.usage?.input_tokens??0),s=Number(e.usage?.output_tokens??0);o+s>0&&r.push(re(o+s)+" tok"),r.length>0&&n(p.dim(" \u25E6 "+r.join(" \xB7 ")));let i=ip(t),a=it(t.model);if(i>=1){let l=Math.round((i-1)*a),c=Math.round(a/1e3);console.log(p.error(` context OVER ${c}k tok by ~${re(l)} tok \u2014 model output may be silently truncated`))}else if(i>.5){let l=i>.8?p.error:p.warning;n(l(` context ${Math.round(i*100)}% used of ${re(a)}`))}n("")}function yx(e={}){let t=e.load??dl,n=e.onResize??(i=>Ue.subscribe(i)),r="",o,s=n(()=>{r=""});return{renderIfChanged(i){let a=i??"unbound",l=t(a),c=YS(l);return a===o&&c===r?[]:(o=a,r=c,c===""?[]:pl(l))},invalidate(){r=""},dispose(){try{s()}catch{}}}}var Am={done:{glyph:"\u2713",color:p.success,label:"done"},blocked:{glyph:"\u2298",color:p.error,label:"blocked"},asking:{glyph:"?",color:p.warning,label:"asking"},interrupted:{glyph:"\u23F8",color:p.meta,label:"interrupted"}};function bx(e={}){let t=Math.max(2,e.capacity??8),n=[];return{push(r){n.push(r.kind),n.length>t&&(n=n.slice(n.length-t))},reset(){n=[]},entries(){return n},render(){if(n.length===0)return null;let r=p.dim(" ledger "),o=p.dim(" \xB7 "),s=n.map(u=>{let d=Am[u];return d.color(`${d.glyph} ${d.label}`)}),i=p.dim(` (${n.length} turn${n.length===1?"":"s"})`),a=r+s.join(o)+i,l=Math.max(20,Y()-2);if(z(a)<=l)return a;let c=r+n.map(u=>Am[u].color(Am[u].glyph)).join(p.dim(" "))+i;return ae(c,l)}}}var _m=["\u25D0","\u25D1","\u25D2","\u25D3"],Tc=class{stream;manager;registry;throttleMs;started=!1;lastRepaint=0;spinnerIndex=0;spinnerInterval=null;resizeUnsub=null;updateHandler=null;registryStartedHandler=null;registrySettledHandler=null;rowCount=0;onRowCountChange;constructor(t,n,r={}){this.manager=t,this.registry=n,this.stream=r.stream??process.stdout,this.throttleMs=r.throttleMs??200}setRowCountChangeHandler(t){this.onRowCountChange=t}start(){this.started||(this.started=!0,this.updateHandler=()=>this.scheduleRepaint(),this.manager.on("update",this.updateHandler),this.manager.on("complete",this.updateHandler),this.registry&&(this.registryStartedHandler=t=>{this.scheduleRepaint()},this.registrySettledHandler=t=>{this.scheduleRepaint()},this.registry.on("started",this.registryStartedHandler),this.registry.on("settled",this.registrySettledHandler)),this.resizeUnsub=Ue.subscribe(()=>this.repaint()),this.spinnerInterval=setInterval(()=>{this.spinnerIndex=(this.spinnerIndex+1)%_m.length,this.rowCount>0&&this.repaint()},Math.max(this.throttleMs,50)))}stop(){this.started&&(this.started=!1,this.updateHandler&&(this.manager.removeListener("update",this.updateHandler),this.manager.removeListener("complete",this.updateHandler),this.updateHandler=null),this.registry&&(this.registryStartedHandler&&(this.registry.off("started",this.registryStartedHandler),this.registryStartedHandler=null),this.registrySettledHandler&&(this.registry.off("settled",this.registrySettledHandler),this.registrySettledHandler=null)),this.resizeUnsub&&(this.resizeUnsub(),this.resizeUnsub=null),this.spinnerInterval&&(clearInterval(this.spinnerInterval),this.spinnerInterval=null),this.rowCount>0&&(this.clearRows(),this.rowCount=0,this.onRowCountChange?.(0)))}scheduleRepaint(){Date.now()-this.lastRepaint<this.throttleMs||this.repaint()}repaint(){if(!this.started||!this.stream.isTTY)return;this.lastRepaint=Date.now();let t=[...this.manager.running().map(s=>({kind:"turn",task:s})),...(this.registry?.list()??[]).filter(s=>s.status==="running").map(s=>({kind:"subagent",job:s}))],n=this.stream.rows??24,r=Math.max(0,Math.min(t.length,n-1));if(r!==this.rowCount&&(this.rowCount>0&&this.clearRows(),this.rowCount=r,this.onRowCountChange?.(r)),r===0)return;let o=Math.max(1,n-r);this.stream.write("\x1B[s");for(let s=0;s<r;s++){let i=t[s],a=o+s;this.stream.write(`\x1B[${a};1H`),this.stream.write("\x1B[2K"),this.stream.write(this.formatItemLine(i))}this.stream.write("\x1B[u")}clearRows(){if(!this.stream.isTTY)return;let t=this.stream.rows??24,n=Math.min(this.rowCount,t-1),r=Math.max(1,t-n);this.stream.write("\x1B[s");for(let o=0;o<n;o++)this.stream.write(`\x1B[${r+o};1H`),this.stream.write("\x1B[2K");this.stream.write("\x1B[u")}formatItemLine(t){return t.kind==="turn"?this.formatTaskLine(t.task):this.formatJobLine(t.job)}formatTaskLine(t){let n=Math.max(4,(this.stream.columns??80)-2),r=p.brand(_m[this.spinnerIndex]),o=p.dim(t.id),s=p.bold(t.label),i=[r,o,s];t.progressDescription&&i.push(p.dim(t.progressDescription));let a=[];t.stats.toolUses>0&&a.push(`${t.stats.toolUses} tool${t.stats.toolUses===1?"":"s"}`),t.stats.tokens>0&&a.push(`${re(t.stats.tokens)} tok`);let l=Date.now()-t.startedAt;return a.push(ne(l)),a.length>0&&i.push(p.dim(a.join(" \xB7 "))),ae(" "+i.join(" "),n)}formatJobLine(t){let n=Math.max(4,(this.stream.columns??80)-2),r=p.brand(_m[this.spinnerIndex]),o=p.dim(t.jobId),s=p.bold(t.label||t.jobId),i=[r,o,s],a=Date.now()-t.startedAt;return i.push(p.dim(ne(a))),ae(" "+i.join(" "),n)}};import{spawn as gW}from"node:child_process";var hW=/\x1b(?:\[[0-9;]*[a-zA-Z]|[\]P^_X][^\x07\x1b]*(?:\x07|\x1b\\)|[@-OQ-WYZ\\])/g;function yW(){let e="";return{strip(t){let r=(e+t).replace(hW,""),o=r.match(/\x1b(?:[\]P^_X][^\x07\x1b]*|\[[0-9;]*)?$/);return o?(e=o[0],r.slice(0,r.length-e.length)):(e="",r)},flush(){return e="",""}}}function wx(e,t){if(e.length<=t)return e;let n=t;for(;n>0&&(e[n]&192)===128;)n--;return e.subarray(0,n)}var Sx=12e4,kx=1e5;function Cm(e){let t=Date.now(),n=e.timeoutMs??Sx,r=e.maxBytes??kx,o=yW(),s;try{s=gW(e.command,{shell:!0,detached:!0,stdio:["ignore","pipe","pipe"],...e.cwd!==void 0?{cwd:e.cwd}:{},...e.env!==void 0?{env:{...process.env,...e.env}}:{}})}catch(v){let E=v instanceof Error?v.message:String(v);return{pid:void 0,kill:()=>{},promise:Promise.resolve({exitCode:null,durationMs:Date.now()-t,displayCaptured:"",modelCaptured:"",truncated:!1,errorReason:"spawn-failed",errorMessage:`Failed to spawn shell: ${E}`})}}s.unref();let i,a=!1,l=new Promise(v=>{i=E=>{a||(a=!0,v(E))}}),c="",u="",d=0,m=0,f=!1,g=!1,h=!1;function b(v){let E=r-d;if(E>0){let I=v.length<=E?v:wx(v,E);d+=I.length,c+=I.toString("utf8"),v.length>E&&(f=!0)}else v.length>0&&(f=!0);if(m<r){let I=o.strip(v.toString("utf8")),D=Buffer.byteLength(I,"utf8"),_=r-m;if(D<=_)u+=I,m+=D;else{let M=Buffer.from(I,"utf8");u+=wx(M,_).toString("utf8"),m=r,f=!0}}}function y(){if(s.pid!==void 0&&s.pid!==0&&!s.killed)try{process.kill(-s.pid,"SIGKILL")}catch{}}let w=setTimeout(()=>{y(),e.abort.removeEventListener("abort",x),i({exitCode:null,durationMs:Date.now()-t,displayCaptured:c,modelCaptured:u,truncated:f,errorReason:"timeout",errorMessage:`Command timed out after ${n}ms`})},n),x=()=>{y(),clearTimeout(w),i({exitCode:null,durationMs:Date.now()-t,displayCaptured:c,modelCaptured:u,truncated:f,errorReason:"abort",errorMessage:h?"Command killed":"Command aborted"})};e.abort.addEventListener("abort",x);function R(){g||a||d<r&&m<r||(g=!0,y(),clearTimeout(w),e.abort.removeEventListener("abort",x),i({exitCode:null,durationMs:Date.now()-t,displayCaptured:c,modelCaptured:u,truncated:!0,errorReason:"overflow",errorMessage:`Output exceeded ${r} bytes`}))}return s.stdout?.on("data",v=>{b(v);try{e.onChunk?.(v,"stdout")}catch{}R()}),s.stderr?.on("data",v=>{b(v);try{e.onChunk?.(v,"stderr")}catch{}R()}),s.on("error",v=>{clearTimeout(w),e.abort.removeEventListener("abort",x),i({exitCode:null,durationMs:Date.now()-t,displayCaptured:c,modelCaptured:u,truncated:f,errorReason:"spawn-failed",errorMessage:`Shell process error: ${v.message}`})}),s.on("close",(v,E)=>{if(clearTimeout(w),e.abort.removeEventListener("abort",x),!(g||a)){if(o.flush(),h){i({exitCode:v,durationMs:Date.now()-t,displayCaptured:c,modelCaptured:u,truncated:f,errorReason:"abort",errorMessage:"Command killed"});return}if(v!==null&&v!==0){i({exitCode:v,durationMs:Date.now()-t,displayCaptured:c,modelCaptured:u,truncated:f,errorReason:"nonzero-exit",errorMessage:`Command exited with code ${v}`});return}if(v===null){i({exitCode:null,durationMs:Date.now()-t,displayCaptured:c,modelCaptured:u,truncated:f,errorReason:"signal-killed",errorMessage:E?`Command killed by signal ${E}`:"Command killed by signal"});return}i({exitCode:v,durationMs:Date.now()-t,displayCaptured:c,modelCaptured:u,truncated:f})}}),{pid:s.pid,promise:l,kill:(v="SIGKILL")=>{if(!(s.pid===void 0||s.pid===0||s.killed)){h=!0;try{process.kill(-s.pid,v)}catch{}}}}}import{EventEmitter as bW}from"node:events";var vx=200,gi=class extends bW{jobs=new Map;handles=new Map;aborts=new Map;counter=0;start(t){let n=`sh-${++this.counter}`,r=new AbortController,o=Cm({command:t.command,...t.cwd!==void 0?{cwd:t.cwd}:{},abort:r.signal,...t.timeoutMs!==void 0?{timeoutMs:t.timeoutMs}:{},...t.maxBytes!==void 0?{maxBytes:t.maxBytes}:{},...t.env!==void 0?{env:t.env}:{},...t.onChunk!==void 0?{onChunk:t.onChunk}:{}}),s={id:n,command:t.command,pid:o.pid,startedAt:Date.now(),mode:t.mode,status:"running"};return this.jobs.set(n,s),this.handles.set(n,o),this.aborts.set(n,r),o.promise.then(i=>{s.result=i,s.status=wW(i),this.aborts.delete(n),this.handles.delete(n);try{this.emit("complete",s)}catch(a){console.warn(`[shell-jobs] listener for 'complete' threw on ${n}: ${a instanceof Error?a.message:String(a)}`)}this.pruneHistory()}),{job:s,handle:o}}get(t){return this.jobs.get(t)}list(){return[...this.jobs.values()].sort((t,n)=>t.startedAt-n.startedAt)}running(){return this.list().filter(t=>t.status==="running")}kill(t){let n=this.jobs.get(t);if(!n||n.status!=="running")return!1;let r=this.aborts.get(t);if(r)return r.abort(),!0;let o=this.handles.get(t);return o?(o.kill(),!0):!1}killAll(){let t=this.running();for(let n of t)this.kill(n.id);return t}runningCount(){let t=0;for(let n of this.jobs.values())n.status==="running"&&t++;return t}pruneHistory(){if(!(this.jobs.size<=vx))for(let[t,n]of this.jobs){if(this.jobs.size<=vx)break;n.status!=="running"&&this.jobs.delete(t)}}};function wW(e){return e.errorReason===void 0?"completed":e.errorReason==="abort"?"killed":"failed"}function SW(e){if(!e.startsWith("!"))return null;if(e.startsWith("!&")){let n=e.slice(2).trim();return n.length===0?null:{mode:"background",command:n}}let t=e.slice(1).trim();return t.length===0?null:{mode:"foreground",command:t}}function kW(e,t){let n=ne(t.durationMs);return t.errorReason==="abort"?p.dim(` [${e.id} \xB7 killed \xB7 ${n}]`):t.errorReason==="timeout"?p.dim(` [${e.id} \xB7 timed out \xB7 ${n}]`):t.errorReason==="overflow"?p.dim(` [${e.id} \xB7 output overflow \xB7 ${n}]`):t.errorReason==="spawn-failed"?p.warning(` [${e.id} \xB7 spawn failed \xB7 ${n}]`):t.errorReason==="nonzero-exit"?p.warning(` [${e.id} \xB7 exit ${t.exitCode} \xB7 ${n}]`):t.errorReason==="signal-killed"?p.warning(` [${e.id} \xB7 killed by signal \xB7 ${n}]`):p.dim(` [${e.id} \xB7 exit ${t.exitCode??0} \xB7 ${n}]`)}function Tx(e){return e.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function vW(e,t,n){let r=[],o=[`mode="${t}"`];return n.exitCode!==null&&o.push(`exit="${n.exitCode}"`),n.errorReason!==void 0&&o.push(`reason="${n.errorReason}"`),o.push(`duration="${ne(n.durationMs)}"`),n.truncated&&o.push('truncated="true"'),r.push(`<bash-passthrough ${o.join(" ")}>`),r.push(`<command>${Tx(e)}</command>`),r.push("<output>"),r.push(Tx(n.modelCaptured)),r.push("</output>"),r.push("</bash-passthrough>"),r.join(`
2216
- `)}var xc=class e{constructor(t){this.opts=t;this.registry.on("complete",n=>{if(n.mode!=="background")return;let r=n.result;r&&(this.queueInjection({command:n.command,mode:"background",result:r}),this.pendingNotifications.push({job:n,result:r}),this.pendingNotifications.length>e.MAX_PENDING_NOTIFICATIONS&&this.pendingNotifications.shift())})}opts;registry=new gi;pendingInjections=[];pendingNotifications=[];activeFgJobId=null;static MAX_PENDING_INJECTIONS=25;static MAX_PENDING_NOTIFICATIONS=50;async dispatch(t){let n=SW(t);return n===null?t==="!"||t==="!&"||t.startsWith("! ")||t.startsWith("!& ")?(this.opts.writeLine(p.dim(" usage: !<cmd> (foreground) !&<cmd> (background)")),!0):!1:(n.mode==="foreground"?await this.runForeground(n.command):this.startBackground(n.command),!0)}drainInjections(){if(this.pendingInjections.length===0)return"";let t=this.pendingInjections.map(n=>vW(n.command,n.mode,n.result));return this.pendingInjections=[],t.join(`
2213
+ `);for(let b of h)d.push(i+" "+He(b,n)+" "+i)}else for(let f of l){let g=m.dim(He(f.label,c)),h=me(En(f.value),u).split(`
2214
+ `),b=h[0]??"";d.push(i+" "+g+" "+He(b,u)+" "+i);for(let y of h.slice(1))d.push(i+" "+" ".repeat(c)+" "+He(y,u)+" "+i)}d.push(a);let p=m.dim(ae(t.affordance,n));return d.push(i+" "+He(p,n)+" "+i),d.push(s),d.join(`
2215
+ `)}function mW(e){let t=[],n=(r,o)=>{o&&o.trim().length>0&&t.push({label:r,value:o.trim()})};switch(e.kind){case"done":n("done",e.whatWasDone),n("evidence",e.evidence),n("deferred",e.deferred);break;case"blocked":n("blocks",e.whatBlocks),n("unblock",e.unblockCondition),n("progress",e.alreadyDone);break;case"asking":n("question",e.question),n("resolves",e.assumption),n("after",e.followup);break;case"interrupted":n("was doing",e.whatWasInProgress),n("saved at",e.stateLocation),n("resume",e.resumeRequires);break}return t}function gx(e,t,n){let r=[];return n&&n.trim().length>0&&r.push({type:"text",text:n}),e&&r.push({type:"text",text:e}),zl(r,t),r}async function hx(e,t,n,r,o="summary",s,i,a){let l=Ok(e.text,e.attachments);r.setInFlight(!0);let c="",u=!1,d=!1,p=!1,f=!1,g,h=!1,b=!1,y=0,w=15e3,x=[],R=new Map,v=e.text.startsWith("/")?e.text.split(/[\s:]/)[0]?.slice(1):void 0,E=r.getCompositor?r.getCompositor():null,I=()=>new Bo({out:Uo(s),thinkingMode:o,...v?{activeSkillName:v}:{},onCancel:()=>{t.interrupt().catch(P=>{je()&&console.error(" "+m.error("session.interrupt() failed:"),P)})},...i?{onBackground:()=>{h=!0}}:{},...a?.history?{history:a.history}:{},...a?.autocompleteState?{autocompleteState:a.autocompleteState}:{},...a?.promptText!==void 0?{promptText:a.promptText}:{},...r.scrollRegion?{scrollRegion:r.scrollRegion}:{},...E?{compositor:E}:{},...r.onStageChange?{onStageChange:r.onStageChange}:{}}),D=I(),_=async()=>{if(!p){p=!0;try{await D.dispose()}catch{}}},M=async()=>{await D.arm();let P=D.getCompositor();if(s&&P){let $=P;s.fn=F=>$.commitAbove(F)}r.setActiveCompositor?.(P),r.setInterruptNotifier?.($=>D.setInterrupting($)),r.rearmStatus?.()};try{E?E.commitAbove(""):console.log(),r.setSoftStopHandler&&r.setSoftStopHandler(()=>{b=!0}),await M(),i&&r.setBackgroundHandler&&r.setBackgroundHandler(()=>{h=!0});let P=e.attachments.length===0?e.text:gx(e.text,e.attachments),$=t.sendMessageStream(P);if(await qr((k,A)=>{D.process(k,A)},async()=>{for await(let k of $){if(b){t.interrupt().catch(A=>{je()&&console.error(" "+m.error("soft-stop session.interrupt() failed:"),A)});break}if(h&&i){let A=v??e.text.slice(0,40),N=i.register(A),H=fl(N,i);gl($,c,l,N,i,H,n,r.onTurnComplete,t.abortSignal),await _(),(s??{fn:console.log}).fn(m.dim(` \u2192 backgrounded as ${N.id}: ${N.label}`)),r.setInFlight(!1),r.rearmStatus?.();return}if(k.type==="chunk"&&k.chunk.type==="content"?(c+=k.chunk.content,u=!0):k.type==="message"&&!u&&(c=k.message.content),k.type==="chunk"&&k.chunk.type==="tool_use_detail"){let A=k.chunk,N={toolName:A.toolName,toolUseId:A.toolUseId,input:A.toolInput};R.set(A.toolUseId,N),x.push(N)}else if(k.type==="chunk"&&k.chunk.type==="tool_result"){let A=k.chunk,N=R.get(A.toolUseId);if(N&&(N.result=A.content,N.isError=A.isError,R.delete(A.toolUseId)),r.onContextProgress){let H=Date.now();if(H-y>=w){y=H;try{let C=r.onContextProgress();C instanceof Promise&&await C}catch(C){je()&&console.error(" "+m.error("onContextProgress (status refresh) failed:"),C)}}}}if(k.type==="paused"){await _(),(s??{fn:console.log}).fn(lg({reason:k.reason,...k.resetsAt!==void 0?{resetsAt:k.resetsAt}:{},...k.accountId!==void 0?{accountId:k.accountId}:{},...k.autoResume!==void 0?{autoResume:k.autoResume}:{}}));continue}if(k.type==="resumed"){let A=k.hotSwapped&&k.accountId?`\u25B6 Resumed on ${k.accountId}`:"\u25B6 Resumed";c="",u=!1,x.length=0,R.clear(),f=!1,g=void 0,d=!1,h=!1,D=I(),p=!1,await M(),(s??{fn:console.log}).fn(m.success(A));continue}if(k.type==="error"){await _(),Wr(Br(k.error)),d=!0;continue}D.process(k),k.type==="done"&&(f=!0,g=k.metadata)}}),await _(),b){let k=s?s.fn:console.log;k(m.warning("\u23F8 Stopped \u2014 work so far kept.")+m.dim(" Use /resume or --resume to continue.")),k("")}if(f&&!b){lr(n,l,c,g,x),r.onTurnComplete&&await r.onTurnComplete(l,c).catch(()=>{}),tl(process.stdout);let k=N=>{s?s.fn(N):console.log(N)},A=mx(c);if(A&&(k(fx(A)),k(""),r.onTerminalState))try{r.onTerminalState(A)}catch{}if(fW(g,n,k),r.onAfterTurn){let N=r.onAfterTurn();N instanceof Promise&&await N.catch(()=>{})}}}catch(P){await _(),d||Wr(Br(P))}finally{await _(),s&&(s.fn=s.idleFn),r.setActiveCompositor?.(null),r.setInterruptNotifier?.(null),r.setBackgroundHandler?.(null),r.setSoftStopHandler?.(null),r.setInFlight(!1),r.rearmStatus?.()}}function fW(e,t,n=console.log){if(!e)return;let r=[];e.durationMs&&r.push(ne(e.durationMs)),e.totalCostUsd!==void 0&&r.push(Ne(e.totalCostUsd));let o=Number(e.usage?.input_tokens??0),s=Number(e.usage?.output_tokens??0);o+s>0&&r.push(re(o+s)+" tok"),r.length>0&&n(m.dim(" \u25E6 "+r.join(" \xB7 ")));let i=ip(t),a=it(t.model);if(i>=1){let l=Math.round((i-1)*a),c=Math.round(a/1e3);console.log(m.error(` context OVER ${c}k tok by ~${re(l)} tok \u2014 model output may be silently truncated`))}else if(i>.5){let l=i>.8?m.error:m.warning;n(l(` context ${Math.round(i*100)}% used of ${re(a)}`))}n("")}function yx(e={}){let t=e.load??dl,n=e.onResize??(i=>Ue.subscribe(i)),r="",o,s=n(()=>{r=""});return{renderIfChanged(i){let a=i??"unbound",l=t(a),c=YS(l);return a===o&&c===r?[]:(o=a,r=c,c===""?[]:pl(l))},invalidate(){r=""},dispose(){try{s()}catch{}}}}var Am={done:{glyph:"\u2713",color:m.success,label:"done"},blocked:{glyph:"\u2298",color:m.error,label:"blocked"},asking:{glyph:"?",color:m.warning,label:"asking"},interrupted:{glyph:"\u23F8",color:m.meta,label:"interrupted"}};function bx(e={}){let t=Math.max(2,e.capacity??8),n=[];return{push(r){n.push(r.kind),n.length>t&&(n=n.slice(n.length-t))},reset(){n=[]},entries(){return n},render(){if(n.length===0)return null;let r=m.dim(" ledger "),o=m.dim(" \xB7 "),s=n.map(u=>{let d=Am[u];return d.color(`${d.glyph} ${d.label}`)}),i=m.dim(` (${n.length} turn${n.length===1?"":"s"})`),a=r+s.join(o)+i,l=Math.max(20,Y()-2);if(z(a)<=l)return a;let c=r+n.map(u=>Am[u].color(Am[u].glyph)).join(m.dim(" "))+i;return ae(c,l)}}}var _m=["\u25D0","\u25D1","\u25D2","\u25D3"],Tc=class{stream;manager;registry;throttleMs;started=!1;lastRepaint=0;spinnerIndex=0;spinnerInterval=null;resizeUnsub=null;updateHandler=null;registryStartedHandler=null;registrySettledHandler=null;rowCount=0;onRowCountChange;constructor(t,n,r={}){this.manager=t,this.registry=n,this.stream=r.stream??process.stdout,this.throttleMs=r.throttleMs??200}setRowCountChangeHandler(t){this.onRowCountChange=t}start(){this.started||(this.started=!0,this.updateHandler=()=>this.scheduleRepaint(),this.manager.on("update",this.updateHandler),this.manager.on("complete",this.updateHandler),this.registry&&(this.registryStartedHandler=t=>{this.scheduleRepaint()},this.registrySettledHandler=t=>{this.scheduleRepaint()},this.registry.on("started",this.registryStartedHandler),this.registry.on("settled",this.registrySettledHandler)),this.resizeUnsub=Ue.subscribe(()=>this.repaint()),this.spinnerInterval=setInterval(()=>{this.spinnerIndex=(this.spinnerIndex+1)%_m.length,this.rowCount>0&&this.repaint()},Math.max(this.throttleMs,50)))}stop(){this.started&&(this.started=!1,this.updateHandler&&(this.manager.removeListener("update",this.updateHandler),this.manager.removeListener("complete",this.updateHandler),this.updateHandler=null),this.registry&&(this.registryStartedHandler&&(this.registry.off("started",this.registryStartedHandler),this.registryStartedHandler=null),this.registrySettledHandler&&(this.registry.off("settled",this.registrySettledHandler),this.registrySettledHandler=null)),this.resizeUnsub&&(this.resizeUnsub(),this.resizeUnsub=null),this.spinnerInterval&&(clearInterval(this.spinnerInterval),this.spinnerInterval=null),this.rowCount>0&&(this.clearRows(),this.rowCount=0,this.onRowCountChange?.(0)))}scheduleRepaint(){Date.now()-this.lastRepaint<this.throttleMs||this.repaint()}redraw(){this.repaint()}repaint(){if(!this.started||!this.stream.isTTY)return;this.lastRepaint=Date.now();let t=[...this.manager.running().map(s=>({kind:"turn",task:s})),...(this.registry?.list()??[]).filter(s=>s.status==="running").map(s=>({kind:"subagent",job:s}))],n=this.stream.rows??24,r=Math.max(0,Math.min(t.length,n-1));if(r!==this.rowCount&&(this.rowCount>0&&this.clearRows(),this.rowCount=r,this.onRowCountChange?.(r)),r===0)return;let o=Math.max(1,n-r);this.stream.write("\x1B[s");for(let s=0;s<r;s++){let i=t[s],a=o+s;this.stream.write(`\x1B[${a};1H`),this.stream.write("\x1B[2K"),this.stream.write(this.formatItemLine(i))}this.stream.write("\x1B[u")}clearRows(){if(!this.stream.isTTY)return;let t=this.stream.rows??24,n=Math.min(this.rowCount,t-1),r=Math.max(1,t-n);this.stream.write("\x1B[s");for(let o=0;o<n;o++)this.stream.write(`\x1B[${r+o};1H`),this.stream.write("\x1B[2K");this.stream.write("\x1B[u")}formatItemLine(t){return t.kind==="turn"?this.formatTaskLine(t.task):this.formatJobLine(t.job)}formatTaskLine(t){let n=Math.max(4,(this.stream.columns??80)-2),r=m.brand(_m[this.spinnerIndex]),o=m.dim(t.id),s=m.bold(t.label),i=[r,o,s];t.progressDescription&&i.push(m.dim(t.progressDescription));let a=[];t.stats.toolUses>0&&a.push(`${t.stats.toolUses} tool${t.stats.toolUses===1?"":"s"}`),t.stats.tokens>0&&a.push(`${re(t.stats.tokens)} tok`);let l=Date.now()-t.startedAt;return a.push(ne(l)),a.length>0&&i.push(m.dim(a.join(" \xB7 "))),ae(" "+i.join(" "),n)}formatJobLine(t){let n=Math.max(4,(this.stream.columns??80)-2),r=m.brand(_m[this.spinnerIndex]),o=m.dim(t.jobId),s=m.bold(t.label||t.jobId),i=[r,o,s],a=Date.now()-t.startedAt;return i.push(m.dim(ne(a))),ae(" "+i.join(" "),n)}};import{spawn as gW}from"node:child_process";var hW=/\x1b(?:\[[0-9;]*[a-zA-Z]|[\]P^_X][^\x07\x1b]*(?:\x07|\x1b\\)|[@-OQ-WYZ\\])/g;function yW(){let e="";return{strip(t){let r=(e+t).replace(hW,""),o=r.match(/\x1b(?:[\]P^_X][^\x07\x1b]*|\[[0-9;]*)?$/);return o?(e=o[0],r.slice(0,r.length-e.length)):(e="",r)},flush(){return e="",""}}}function wx(e,t){if(e.length<=t)return e;let n=t;for(;n>0&&(e[n]&192)===128;)n--;return e.subarray(0,n)}var Sx=12e4,kx=1e5;function Cm(e){let t=Date.now(),n=e.timeoutMs??Sx,r=e.maxBytes??kx,o=yW(),s;try{s=gW(e.command,{shell:!0,detached:!0,stdio:["ignore","pipe","pipe"],...e.cwd!==void 0?{cwd:e.cwd}:{},...e.env!==void 0?{env:{...process.env,...e.env}}:{}})}catch(v){let E=v instanceof Error?v.message:String(v);return{pid:void 0,kill:()=>{},promise:Promise.resolve({exitCode:null,durationMs:Date.now()-t,displayCaptured:"",modelCaptured:"",truncated:!1,errorReason:"spawn-failed",errorMessage:`Failed to spawn shell: ${E}`})}}s.unref();let i,a=!1,l=new Promise(v=>{i=E=>{a||(a=!0,v(E))}}),c="",u="",d=0,p=0,f=!1,g=!1,h=!1;function b(v){let E=r-d;if(E>0){let I=v.length<=E?v:wx(v,E);d+=I.length,c+=I.toString("utf8"),v.length>E&&(f=!0)}else v.length>0&&(f=!0);if(p<r){let I=o.strip(v.toString("utf8")),D=Buffer.byteLength(I,"utf8"),_=r-p;if(D<=_)u+=I,p+=D;else{let M=Buffer.from(I,"utf8");u+=wx(M,_).toString("utf8"),p=r,f=!0}}}function y(){if(s.pid!==void 0&&s.pid!==0&&!s.killed)try{process.kill(-s.pid,"SIGKILL")}catch{}}let w=setTimeout(()=>{y(),e.abort.removeEventListener("abort",x),i({exitCode:null,durationMs:Date.now()-t,displayCaptured:c,modelCaptured:u,truncated:f,errorReason:"timeout",errorMessage:`Command timed out after ${n}ms`})},n),x=()=>{y(),clearTimeout(w),i({exitCode:null,durationMs:Date.now()-t,displayCaptured:c,modelCaptured:u,truncated:f,errorReason:"abort",errorMessage:h?"Command killed":"Command aborted"})};e.abort.addEventListener("abort",x);function R(){g||a||d<r&&p<r||(g=!0,y(),clearTimeout(w),e.abort.removeEventListener("abort",x),i({exitCode:null,durationMs:Date.now()-t,displayCaptured:c,modelCaptured:u,truncated:!0,errorReason:"overflow",errorMessage:`Output exceeded ${r} bytes`}))}return s.stdout?.on("data",v=>{b(v);try{e.onChunk?.(v,"stdout")}catch{}R()}),s.stderr?.on("data",v=>{b(v);try{e.onChunk?.(v,"stderr")}catch{}R()}),s.on("error",v=>{clearTimeout(w),e.abort.removeEventListener("abort",x),i({exitCode:null,durationMs:Date.now()-t,displayCaptured:c,modelCaptured:u,truncated:f,errorReason:"spawn-failed",errorMessage:`Shell process error: ${v.message}`})}),s.on("close",(v,E)=>{if(clearTimeout(w),e.abort.removeEventListener("abort",x),!(g||a)){if(o.flush(),h){i({exitCode:v,durationMs:Date.now()-t,displayCaptured:c,modelCaptured:u,truncated:f,errorReason:"abort",errorMessage:"Command killed"});return}if(v!==null&&v!==0){i({exitCode:v,durationMs:Date.now()-t,displayCaptured:c,modelCaptured:u,truncated:f,errorReason:"nonzero-exit",errorMessage:`Command exited with code ${v}`});return}if(v===null){i({exitCode:null,durationMs:Date.now()-t,displayCaptured:c,modelCaptured:u,truncated:f,errorReason:"signal-killed",errorMessage:E?`Command killed by signal ${E}`:"Command killed by signal"});return}i({exitCode:v,durationMs:Date.now()-t,displayCaptured:c,modelCaptured:u,truncated:f})}}),{pid:s.pid,promise:l,kill:(v="SIGKILL")=>{if(!(s.pid===void 0||s.pid===0||s.killed)){h=!0;try{process.kill(-s.pid,v)}catch{}}}}}import{EventEmitter as bW}from"node:events";var vx=200,gi=class extends bW{jobs=new Map;handles=new Map;aborts=new Map;counter=0;start(t){let n=`sh-${++this.counter}`,r=new AbortController,o=Cm({command:t.command,...t.cwd!==void 0?{cwd:t.cwd}:{},abort:r.signal,...t.timeoutMs!==void 0?{timeoutMs:t.timeoutMs}:{},...t.maxBytes!==void 0?{maxBytes:t.maxBytes}:{},...t.env!==void 0?{env:t.env}:{},...t.onChunk!==void 0?{onChunk:t.onChunk}:{}}),s={id:n,command:t.command,pid:o.pid,startedAt:Date.now(),mode:t.mode,status:"running"};return this.jobs.set(n,s),this.handles.set(n,o),this.aborts.set(n,r),o.promise.then(i=>{s.result=i,s.status=wW(i),this.aborts.delete(n),this.handles.delete(n);try{this.emit("complete",s)}catch(a){console.warn(`[shell-jobs] listener for 'complete' threw on ${n}: ${a instanceof Error?a.message:String(a)}`)}this.pruneHistory()}),{job:s,handle:o}}get(t){return this.jobs.get(t)}list(){return[...this.jobs.values()].sort((t,n)=>t.startedAt-n.startedAt)}running(){return this.list().filter(t=>t.status==="running")}kill(t){let n=this.jobs.get(t);if(!n||n.status!=="running")return!1;let r=this.aborts.get(t);if(r)return r.abort(),!0;let o=this.handles.get(t);return o?(o.kill(),!0):!1}killAll(){let t=this.running();for(let n of t)this.kill(n.id);return t}runningCount(){let t=0;for(let n of this.jobs.values())n.status==="running"&&t++;return t}pruneHistory(){if(!(this.jobs.size<=vx))for(let[t,n]of this.jobs){if(this.jobs.size<=vx)break;n.status!=="running"&&this.jobs.delete(t)}}};function wW(e){return e.errorReason===void 0?"completed":e.errorReason==="abort"?"killed":"failed"}function SW(e){if(!e.startsWith("!"))return null;if(e.startsWith("!&")){let n=e.slice(2).trim();return n.length===0?null:{mode:"background",command:n}}let t=e.slice(1).trim();return t.length===0?null:{mode:"foreground",command:t}}function kW(e,t){let n=ne(t.durationMs);return t.errorReason==="abort"?m.dim(` [${e.id} \xB7 killed \xB7 ${n}]`):t.errorReason==="timeout"?m.dim(` [${e.id} \xB7 timed out \xB7 ${n}]`):t.errorReason==="overflow"?m.dim(` [${e.id} \xB7 output overflow \xB7 ${n}]`):t.errorReason==="spawn-failed"?m.warning(` [${e.id} \xB7 spawn failed \xB7 ${n}]`):t.errorReason==="nonzero-exit"?m.warning(` [${e.id} \xB7 exit ${t.exitCode} \xB7 ${n}]`):t.errorReason==="signal-killed"?m.warning(` [${e.id} \xB7 killed by signal \xB7 ${n}]`):m.dim(` [${e.id} \xB7 exit ${t.exitCode??0} \xB7 ${n}]`)}function Tx(e){return e.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function vW(e,t,n){let r=[],o=[`mode="${t}"`];return n.exitCode!==null&&o.push(`exit="${n.exitCode}"`),n.errorReason!==void 0&&o.push(`reason="${n.errorReason}"`),o.push(`duration="${ne(n.durationMs)}"`),n.truncated&&o.push('truncated="true"'),r.push(`<bash-passthrough ${o.join(" ")}>`),r.push(`<command>${Tx(e)}</command>`),r.push("<output>"),r.push(Tx(n.modelCaptured)),r.push("</output>"),r.push("</bash-passthrough>"),r.join(`
2216
+ `)}var xc=class e{constructor(t){this.opts=t;this.registry.on("complete",n=>{if(n.mode!=="background")return;let r=n.result;r&&(this.queueInjection({command:n.command,mode:"background",result:r}),this.pendingNotifications.push({job:n,result:r}),this.pendingNotifications.length>e.MAX_PENDING_NOTIFICATIONS&&this.pendingNotifications.shift())})}opts;registry=new gi;pendingInjections=[];pendingNotifications=[];activeFgJobId=null;static MAX_PENDING_INJECTIONS=25;static MAX_PENDING_NOTIFICATIONS=50;async dispatch(t){let n=SW(t);return n===null?t==="!"||t==="!&"||t.startsWith("! ")||t.startsWith("!& ")?(this.opts.writeLine(m.dim(" usage: !<cmd> (foreground) !&<cmd> (background)")),!0):!1:(n.mode==="foreground"?await this.runForeground(n.command):this.startBackground(n.command),!0)}drainInjections(){if(this.pendingInjections.length===0)return"";let t=this.pendingInjections.map(n=>vW(n.command,n.mode,n.result));return this.pendingInjections=[],t.join(`
2217
2217
  `)+`
2218
- `}drainNotifications(){if(this.pendingNotifications.length===0)return[];let t=this.pendingNotifications;return this.pendingNotifications=[],t}abortActiveForeground(){return this.activeFgJobId===null?!1:(this.registry.kill(this.activeFgJobId),!0)}hasActiveForeground(){return this.activeFgJobId!==null}drainOnExit(){let t=this.registry.killAll();t.length>0&&this.opts.writeLine(p.dim(` Killing ${t.length} background shell job${t.length===1?"":"s"} on exit.`))}async runForeground(t){this.opts.writeLine(p.dim(`$ ${t}`));let n="",r=!1,o=()=>{let c;for(;(c=n.indexOf(`
2219
- `))!==-1;){let u=n.slice(0,c);n=n.slice(c+1),this.opts.writeLine(u)}},s={command:t,mode:"foreground",onChunk:c=>{r||(n+=c.toString("utf8"),o())}},i=this.opts.getCwd();i!==void 0&&(s.cwd=i);let{job:a,handle:l}=this.registry.start(s);this.activeFgJobId=a.id;try{let c=await l.promise;r=!0,n.length>0&&(this.opts.writeLine(n),n=""),this.opts.writeLine(kW(a,c)),this.queueInjection({command:t,mode:"foreground",result:c})}finally{this.activeFgJobId=null}}startBackground(t){let n={command:t,mode:"background"},r=this.opts.getCwd();r!==void 0&&(n.cwd=r);let{job:o}=this.registry.start(n);this.opts.writeLine(p.dim(` [${o.id}] background: `)+t)}queueInjection(t){this.pendingInjections.push(t),this.pendingInjections.length>e.MAX_PENDING_INJECTIONS&&this.pendingInjections.shift()}};async function TW(e,t){if(e.firstTurnHook&&e.stats.totalTurns===0){let n=e.firstTurnHook;e.firstTurnHook=void 0;try{await n(t)}catch(r){e.completionWriter.fn(p.warning("\u26A0 ")+"first-turn hook failed: "+(r instanceof Error?r.message:String(r)))}}}function Ec(e,t){let n=p.brand("afk")+p.dim(` (${e})`),r=t?p.warning(" \u25CF plan"):"";return n+r+p.dim(" \u203A ")}async function xx(e,t,n,r){let o=null,s=[];e.session.current.waitForInitialization().then(async h=>{je()&&(o=al(h)),await Xl(e.session.current),je()&&(s=yT())}).catch(()=>{});let i=await ix(),a=new vc({rl:e.rl,history:i,statusLine:e.statusLine}),l,c,u,d,m,f,g=!1;try{await a.armCompositor({promptFn:()=>Ec(e.stats.model,e.stats.planMode),onCancel:r,onShiftTab:()=>{let E=e.slashCtx;E.stats.planMode&&E.stats.pendingPlanExit?(E.stats.pendingPlanExit=!1,Ct(E,!1,{closureSummarySkipped:!0}).catch(()=>{})):Ct(E).catch(()=>{}),e.statusLine.rearm()},scrollRegion:e.statusLine,...e.preArmAnchorRow!==void 0?{anchorRow:e.preArmAnchorRow}:{}});let h=a.getCompositor();Bt.install(rl({readLine:E=>a.readLine({promptFn:()=>E}).then(I=>I.text),writer:{line:(E="")=>{let I=a.getCompositor();I?I.commitAbove(E):process.stdout.write(E+`
2220
- `)}},pendingCount:()=>Bt.pendingCount(),...h?{pickFromList:E=>dx(h,E),readTextOverlay:E=>px(h,E)}:{}})),e.replRenderer.setCompositor(a.getCompositor()),e.slashCtx.getCompositor=()=>a.getCompositor();let b=a.getCompositor();if(b){let E=I=>b.commitAbove(I);e.completionWriter.fn=E,e.completionWriter.idleFn=E}e.slashCtx.setSoftStopHandler=E=>a.setSoftStopHandler(E),e.inputSurfaceRef&&(e.inputSurfaceRef.current=a),c=yx();let y=bx();e.clearVerdictLedger=()=>y.reset(),u=new ml,ak(u),hk(u),wk(u),yk(e.backgroundRegistry),d=new Tc(u,e.backgroundRegistry);let w=0,x=1;d.setRowCountChangeHandler(E=>{w=E,e.statusLine.setExtraRows(x+w)}),m=new $l({getExtraRows:()=>e.statusLine.getExtraRows()}),m.setRowCountChangeHandler(E=>{e.statusLine.setExtraRows(x+w)}),d.start(),m.start();let R=50,v=[];for(u.on("complete",E=>{v.length>=R&&v.shift(),v.push(E)}),f=new xc({writeLine:E=>e.replRenderer.writeLine(E),getCwd:()=>e.stats.cwd}),fk(f),n.tryAbortShellForeground=()=>f.abortActiveForeground();;){if(o&&(e.replRenderer.writeLine(o),e.replRenderer.writeLine(""),o=null),s.length>0){for(let k of s)e.replRenderer.writeLine(k);e.replRenderer.writeLine(""),s=[]}for(;v.length>0;){let k=v.shift(),A=k.status==="succeeded"?"\u2713":"\u2717",N=[];if(k.resultText){let C=k.resultText.trim().split(`
2221
- `)[0]?.slice(0,80)??"";C&&N.push(C)}k.error&&N.push(k.error.message);let H=[k.stats.toolUses>0?`${k.stats.toolUses} tools`:"",k.stats.tokens>0?`${Math.round(k.stats.tokens/1e3)}k tok`:"",k.stats.durationMs>0?`${Math.round(k.stats.durationMs/1e3)}s`:""].filter(Boolean).join(" \xB7 ");H&&N.push(H),e.replRenderer.writeLine(Rn({kind:k.status==="succeeded"?"checkpoint":"diagnosis",title:`${A} ${k.id} ${k.label}`,body:N})),e.replRenderer.writeLine("")}let E=f.drainNotifications();for(let{job:k,result:A}of E){let N=A.errorReason===void 0?"\u2713":"\u2717",H=A.errorReason==="abort"?"killed":A.errorReason==="timeout"?"timed out":A.errorReason==="signal-killed"?"killed by signal":`exit ${A.exitCode??0}`,C=Math.max(0,Math.round(A.durationMs/100)/10);e.replRenderer.writeLine(p.dim(` ${N} [${k.id}] ${H} \xB7 ${C}s \xB7 `)+k.command)}let I=c.renderIfChanged(e.stats.sessionId);if(I.length>0){for(let k of I)e.replRenderer.writeLine(k);e.replRenderer.writeLine("")}let D=y.render();D&&e.replRenderer.writeLine(D);let _,M;if(l!==void 0){let k=l;l=void 0;let A=Ec(e.stats.model,e.stats.planMode),N=fr({buffer:k.text,promptText:A,isTTY:!!process.stdout.isTTY,attachmentSummary:Po([...k.attachments])});e.replRenderer.writeLine(N),_=k.text.trim(),M=k.attachments}else{let k=await a.readLine({promptFn:()=>Ec(e.stats.model,e.stats.planMode),onSigint:r,onShiftTab:()=>{let A=e.slashCtx;A.stats.planMode&&A.stats.pendingPlanExit?(A.stats.pendingPlanExit=!1,Ct(A,!1,{closureSummarySkipped:!0}).catch(()=>{})):Ct(A).catch(()=>{}),e.statusLine.rearm()}});_=k.text.trim(),M=k.attachments}if(!_&&M.length===0)continue;if(_.startsWith("!")){let k=/^(0|false|off|no)$/i.test(T.AFK_SHELL_PASSTHROUGH??"");if(e.options.shellPassthrough!==!1&&!k&&(g||(g=!0,e.replRenderer.writeLine(p.dim(" \u2139 ! prefix shells out. Pass --no-shell-passthrough (or set AFK_SHELL_PASSTHROUGH=0) to send ! text to the model instead."))),await f.dispatch(_))){e.statusLine.rearm();continue}}let P=!1;if(_.startsWith("/")){let k=await AS(_,e.slashCtx,M);if(k.handled){if(k.result==="exit"){e.rl.close();return}if((_==="/clear"||_.startsWith("/clear "))&&(await t.rotateOnClear(),e.replRenderer.writeLine(p.dim(` transcript: ${t.path()}`)),y.reset()),k.result!==null&&typeof k.result=="object"&&"kind"in k.result&&k.result.kind==="submit"){l={text:k.result.message,attachments:M??[]},e.statusLine.rearm();continue}e.statusLine.rearm();continue}P=!0}i.push(_),await TW(e,_);let $=_;if(P){let k=sp(_);if(k){let A=k.name.replace(/^\//,"").split(":").pop()??"";if(A&&rm(A)){let N={skillName:A,rawArgs:k.args,source:"plugin",capabilities:{compose:!0,subagents:!0}},H=e.session.current.sessionId,C=Tr(H),B=Date.now();J(`[afk trace] preflight.start commandName=${A}`);let j=!1,ee=await vr(N,{cwd:e.stats.cwd??process.cwd(),artifactDir:C},q=>{je()&&e.replRenderer.writeLine(p.warning(`\u26A0 preflight(${A}) failed: `)+(q instanceof Error?q.message:String(q)))});j=ee!==null,J(`[afk trace] preflight.end commandName=${A} durationMs=${Date.now()-B} success=${j}`),$=im(ee?.manifestBlock,_)}}}let F=f.drainInjections();F.length>0&&($=F+$),await hx({text:$,attachments:M},e.session.current,e.stats,{setInFlight(k){n.turnInFlight=k},async onTurnComplete(k,A){await t.appendTurn(k,A)},async onAfterTurn(){await e.contextSampler.onTurn(e.stats.totalTurns),await jS(e.slashCtx),e.statusLine.rearm(),m?.repaint("observing")},rearmStatus:()=>e.statusLine.rearm(),onTerminalState:k=>y.push(k),setActiveCompositor:k=>{n.activeCompositor=k},setInterruptNotifier:k=>{n.notifyInterrupting=k},scrollRegion:e.statusLine,getCompositor:()=>a.getCompositor(),setBackgroundHandler:k=>a.setBackgroundHandler(k),setSoftStopHandler:k=>a.setSoftStopHandler(k),async onContextProgress(){await e.contextSampler.refresh(),e.statusLine.repaint(mr(e.stats,e.contextSampler))},...m?{onStageChange:k=>m.repaint(k)}:{}},e.options.thinkingUi,e.completionWriter,u,a.toRunTurnRefs(Ec(e.stats.model,e.stats.planMode)))}}finally{if(u!==void 0)for(let b of u.running())u.cancel(b.id);n.tryAbortShellForeground=null,f?.drainOnExit(),m?.stop(),d?.stop(),c?.dispose();let h=b=>console.log(b);e.completionWriter.fn=h,e.completionWriter.idleFn=h,await a.dispose(),e.inputSurfaceRef&&(e.inputSurfaceRef.current=null)}}import{execFile as xW}from"node:child_process";import{dirname as EW,isAbsolute as RW,resolve as AW}from"node:path";import{promisify as _W}from"node:util";var Ex=_W(xW),CW=3e3,IW=new Set(["empty","orphaned-dir","orphaned-registration","dead-owner"]);async function PW(){let t=(await Ex("git",["rev-parse","--git-common-dir"])).stdout.trim();if(!t)throw new Error("Not in a git repository.");let n=RW(t)?t:AW(process.cwd(),t);return EW(n)}async function Rx(e){if(e?.disabled)return{ran:!1,removedCount:0,skippedReason:"disabled"};let t;try{t=await PW()}catch{return{ran:!1,removedCount:0,skippedReason:"not-in-repo"}}let n,r=new Promise(o=>{n=setTimeout(()=>o("timeout"),CW)});try{let o=zt({execFile:Ex,repoRoot:t,dryRun:!1,scope:"interactive",bypassSoftLaunch:!0}),s=await Promise.race([o,r]);if(s==="timeout")return{ran:!1,removedCount:0,skippedReason:"timeout"};let i=s;return i.warnings.some(c=>c.toLowerCase().includes("contested"))?{ran:!1,removedCount:0,skippedReason:"lock-contested"}:{ran:!0,removedCount:i.candidates.filter(c=>IW.has(c.verdict)&&i.removed.includes(c.path)).length}}catch{return{ran:!1,removedCount:0,skippedReason:"error"}}finally{n&&clearTimeout(n)}}import{promises as MW}from"node:fs";import{dirname as OW,join as Cx}from"node:path";import{randomBytes as $W}from"node:crypto";var DW=["Generate a 2-4 word kebab-case slug describing this work request.","Rules:","- ASCII lowercase letters and digits only, separated by single hyphens","- 2 to 4 hyphen-separated words","- Maximum 30 characters total","- No prefix, no quotes, no punctuation other than hyphens","- Output ONLY the slug \u2014 no explanation, no preamble","Examples: fix-cleanup-race, add-telegram-allowlist, refactor-prompt-loader, debug-flaky-test"].join(`
2222
- `),Ax=/^[a-z0-9]+(-[a-z0-9]+){1,3}$/,Im=30,LW=1024,FW=8e3,NW="haiku";async function BW(e,t){let n=e.trim();if(n.length===0)return t.onSkip?.("empty-message"),null;if(n.startsWith("/"))return t.onSkip?.("slash-command"),null;let r=HW(n,LW),o=new AbortController,s=setTimeout(()=>o.abort(),t.timeoutMs??FW),i=t.signal?KW([t.signal,o.signal]):o.signal,a;try{t.slugGenerator?a=await t.slugGenerator(r,i):a=await pc({token:t.token,model:t.model??NW,system:DW,user:r,maxTokens:32,signal:i})}catch(d){let m=d instanceof Error?d.message:String(d);return t.onSkip?.("slug-generator-error",m.slice(0,200)),null}finally{clearTimeout(s)}let l=UW(a);if(l===null)return t.onSkip?.("invalid-slug-output",a.slice(0,60)),null;let c=OW(t.worktreePath);return await jW(l,c)}function UW(e){let t=e.trim().toLowerCase();if(t.length===0)return null;if(Ax.test(t)&&t.length<=Im)return t;let n=t.replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"");if(n.length===0)return null;let r=n.split("-").filter(s=>s.length>0).slice(0,4);if(r.length<2)return null;let o=r[0];for(let s=1;s<r.length;s++){let i=`${o}-${r[s]}`;if(i.length>Im)break;o=i}return Ax.test(o)?o:null}async function jW(e,t){if(!await WW(Cx(t,e)))return e;let n=$W(2).toString("hex");return`${e.split("-").slice(0,3).join("-").slice(0,Im-5)}-${n}`}async function WW(e){try{return await MW.access(e),!0}catch{return!1}}function HW(e,t){let n=Buffer.from(e,"utf8");if(n.length<=t)return e;let r=t;for(;r>0&&n[r]!==void 0&&(n[r]&192)===128;)r--;return n.slice(0,r).toString("utf8")}function KW(e){let t=AbortSignal.any;if(typeof t=="function")return t.call(AbortSignal,e);let n=new AbortController;for(let r of e){if(r.aborted)return n.abort(r.reason),n.signal;r.addEventListener("abort",()=>n.abort(r.reason),{once:!0})}return n.signal}async function Ix(e){let t,n,r=Cx(e.deferred.repoRoot,".afk-worktrees","unnamed"),o=await BW(e.message,{token:e.token,...e.model!==void 0?{model:e.model}:{},...e.timeoutMs!==void 0?{timeoutMs:e.timeoutMs}:{},worktreePath:r,...e.signal!==void 0?{signal:e.signal}:{},...e.slugGenerator!==void 0?{slugGenerator:e.slugGenerator}:{},onSkip:(a,l)=>{t=a,n=l}}),s=t??"unknown",i=n;if(o!==null){let l=`${Ma(e.branchPrefix)}${o}`;try{let c=await e.deferred.create(l);return _x(e.session,c.path),{status:"created",path:c.path,branch:c.branch,slug:o}}catch(c){s="create-failed",i=(c instanceof Error?c.message:String(c)).slice(0,200)}}try{let a=await e.deferred.create(!0);return _x(e.session,a.path),{status:"created-fallback",path:a.path,branch:a.branch,reason:s,...i!==void 0?{detail:i}:{}}}catch(a){return{status:"failed",reason:a instanceof Error?a.message:String(a)}}}function _x(e,t){e&&e.setCwd(t),GW(t)}function GW(e){try{process.chdir(e)}catch{}}W();import{spawn as Px}from"child_process";import{existsSync as YW,mkdirSync as XW,readFileSync as Mx,unlinkSync as ZW,writeFileSync as QW}from"fs";import{get as eH}from"https";import{join as Ox}from"path";import{readFileSync as qW}from"fs";import{dirname as zW,join as JW}from"path";import{fileURLToPath as VW}from"url";function bn(){try{return"3.67.0"}catch{}try{let e=zW(VW(import.meta.url));for(let t of["../../package.json","../package.json"])try{let n=JSON.parse(qW(JW(e,t),"utf-8"));if(typeof n.version=="string")return n.version}catch{}}catch{}return"0.0.0-unknown"}G();var tH=64*1024,nH=1440*60*1e3,rH="update-check.json",oH="pending-update.json";function $x(){return Ox($i(),rH)}function Pm(){return Ox($i(),oH)}function Dx(){let e=$i();YW(e)||XW(e,{recursive:!0})}function sH(e,t){let n=e.split(".").map(Number),r=t.split(".").map(Number),o=Math.max(n.length,r.length);for(let s=0;s<o;s++){let i=n[s]??0,a=r[s]??0;if(a>i)return!0;if(a<i)return!1}return!1}function iH(){try{let e=Mx($x(),"utf-8"),t=JSON.parse(e);if(typeof t.latestVersion=="string"&&typeof t.checkedAt=="number")return t}catch{}return null}function aH(){try{Dx();let e=`
2218
+ `}drainNotifications(){if(this.pendingNotifications.length===0)return[];let t=this.pendingNotifications;return this.pendingNotifications=[],t}abortActiveForeground(){return this.activeFgJobId===null?!1:(this.registry.kill(this.activeFgJobId),!0)}hasActiveForeground(){return this.activeFgJobId!==null}drainOnExit(){let t=this.registry.killAll();t.length>0&&this.opts.writeLine(m.dim(` Killing ${t.length} background shell job${t.length===1?"":"s"} on exit.`))}async runForeground(t){this.opts.writeLine(m.dim(`$ ${t}`));let n="",r=!1,o=()=>{let c;for(;(c=n.indexOf(`
2219
+ `))!==-1;){let u=n.slice(0,c);n=n.slice(c+1),this.opts.writeLine(u)}},s={command:t,mode:"foreground",onChunk:c=>{r||(n+=c.toString("utf8"),o())}},i=this.opts.getCwd();i!==void 0&&(s.cwd=i);let{job:a,handle:l}=this.registry.start(s);this.activeFgJobId=a.id;try{let c=await l.promise;r=!0,n.length>0&&(this.opts.writeLine(n),n=""),this.opts.writeLine(kW(a,c)),this.queueInjection({command:t,mode:"foreground",result:c})}finally{this.activeFgJobId=null}}startBackground(t){let n={command:t,mode:"background"},r=this.opts.getCwd();r!==void 0&&(n.cwd=r);let{job:o}=this.registry.start(n);this.opts.writeLine(m.dim(` [${o.id}] background: `)+t)}queueInjection(t){this.pendingInjections.push(t),this.pendingInjections.length>e.MAX_PENDING_INJECTIONS&&this.pendingInjections.shift()}};async function TW(e,t){if(e.firstTurnHook&&e.stats.totalTurns===0){let n=e.firstTurnHook;e.firstTurnHook=void 0;try{await n(t)}catch(r){e.completionWriter.fn(m.warning("\u26A0 ")+"first-turn hook failed: "+(r instanceof Error?r.message:String(r)))}}}function Ec(e,t){let n=m.brand("afk")+m.dim(` (${e})`),r=t?m.warning(" \u25CF plan"):"";return n+r+m.dim(" \u203A ")}async function xx(e,t,n,r){let o=null,s=[];e.session.current.waitForInitialization().then(async h=>{je()&&(o=al(h)),await Xl(e.session.current),je()&&(s=yT())}).catch(()=>{});let i=await ix(),a=new vc({rl:e.rl,history:i,statusLine:e.statusLine}),l,c,u,d,p,f,g=!1;try{await a.armCompositor({promptFn:()=>Ec(e.stats.model,e.stats.planMode),onCancel:r,onShiftTab:()=>{let E=e.slashCtx;E.stats.planMode&&E.stats.pendingPlanExit?(E.stats.pendingPlanExit=!1,Ct(E,!1,{closureSummarySkipped:!0}).catch(()=>{})):Ct(E).catch(()=>{}),e.statusLine.rearm()},scrollRegion:e.statusLine,...e.preArmAnchorRow!==void 0?{anchorRow:e.preArmAnchorRow}:{}});let h=a.getCompositor();Bt.install(rl({readLine:E=>a.readLine({promptFn:()=>E}).then(I=>I.text),writer:{line:(E="")=>{let I=a.getCompositor();I?I.commitAbove(E):process.stdout.write(E+`
2220
+ `)}},pendingCount:()=>Bt.pendingCount(),...h?{pickFromList:E=>dx(h,E),readTextOverlay:E=>px(h,E)}:{}})),e.replRenderer.setCompositor(a.getCompositor()),e.slashCtx.getCompositor=()=>a.getCompositor();let b=a.getCompositor();if(b){let E=I=>b.commitAbove(I);e.completionWriter.fn=E,e.completionWriter.idleFn=E}e.slashCtx.setSoftStopHandler=E=>a.setSoftStopHandler(E),e.inputSurfaceRef&&(e.inputSurfaceRef.current=a),c=yx();let y=bx();e.clearVerdictLedger=()=>y.reset(),u=new ml,ak(u),hk(u),wk(u),yk(e.backgroundRegistry),d=new Tc(u,e.backgroundRegistry);let w=0,x=1;d.setRowCountChangeHandler(E=>{w=E,e.statusLine.setExtraRows(x+w)}),p=new $l({getExtraRows:()=>e.statusLine.getExtraRows()}),p.setRowCountChangeHandler(E=>{e.statusLine.setExtraRows(x+w)}),e.statusLine.setAfterScrollRestore(()=>{d?.redraw(),p?.redraw()}),d.start(),p.start();let R=50,v=[];for(u.on("complete",E=>{v.length>=R&&v.shift(),v.push(E)}),f=new xc({writeLine:E=>e.replRenderer.writeLine(E),getCwd:()=>e.stats.cwd}),fk(f),n.tryAbortShellForeground=()=>f.abortActiveForeground();;){if(o&&(e.replRenderer.writeLine(o),e.replRenderer.writeLine(""),o=null),s.length>0){for(let k of s)e.replRenderer.writeLine(k);e.replRenderer.writeLine(""),s=[]}for(;v.length>0;){let k=v.shift(),A=k.status==="succeeded"?"\u2713":"\u2717",N=[];if(k.resultText){let C=k.resultText.trim().split(`
2221
+ `)[0]?.slice(0,80)??"";C&&N.push(C)}k.error&&N.push(k.error.message);let H=[k.stats.toolUses>0?`${k.stats.toolUses} tools`:"",k.stats.tokens>0?`${Math.round(k.stats.tokens/1e3)}k tok`:"",k.stats.durationMs>0?`${Math.round(k.stats.durationMs/1e3)}s`:""].filter(Boolean).join(" \xB7 ");H&&N.push(H),e.replRenderer.writeLine(Rn({kind:k.status==="succeeded"?"checkpoint":"diagnosis",title:`${A} ${k.id} ${k.label}`,body:N})),e.replRenderer.writeLine("")}let E=f.drainNotifications();for(let{job:k,result:A}of E){let N=A.errorReason===void 0?"\u2713":"\u2717",H=A.errorReason==="abort"?"killed":A.errorReason==="timeout"?"timed out":A.errorReason==="signal-killed"?"killed by signal":`exit ${A.exitCode??0}`,C=Math.max(0,Math.round(A.durationMs/100)/10);e.replRenderer.writeLine(m.dim(` ${N} [${k.id}] ${H} \xB7 ${C}s \xB7 `)+k.command)}let I=c.renderIfChanged(e.stats.sessionId);if(I.length>0){for(let k of I)e.replRenderer.writeLine(k);e.replRenderer.writeLine("")}let D=y.render();D&&e.replRenderer.writeLine(D);let _,M;if(l!==void 0){let k=l;l=void 0;let A=Ec(e.stats.model,e.stats.planMode),N=fr({buffer:k.text,promptText:A,isTTY:!!process.stdout.isTTY,attachmentSummary:Po([...k.attachments])});e.replRenderer.writeLine(N),_=k.text.trim(),M=k.attachments}else{let k=await a.readLine({promptFn:()=>Ec(e.stats.model,e.stats.planMode),onSigint:r,onShiftTab:()=>{let A=e.slashCtx;A.stats.planMode&&A.stats.pendingPlanExit?(A.stats.pendingPlanExit=!1,Ct(A,!1,{closureSummarySkipped:!0}).catch(()=>{})):Ct(A).catch(()=>{}),e.statusLine.rearm()}});_=k.text.trim(),M=k.attachments}if(!_&&M.length===0)continue;if(_.startsWith("!")){let k=/^(0|false|off|no)$/i.test(T.AFK_SHELL_PASSTHROUGH??"");if(e.options.shellPassthrough!==!1&&!k&&(g||(g=!0,e.replRenderer.writeLine(m.dim(" \u2139 ! prefix shells out. Pass --no-shell-passthrough (or set AFK_SHELL_PASSTHROUGH=0) to send ! text to the model instead."))),await f.dispatch(_))){e.statusLine.rearm();continue}}let P=!1;if(_.startsWith("/")){let k=await AS(_,e.slashCtx,M);if(k.handled){if(k.result==="exit"){e.rl.close();return}if((_==="/clear"||_.startsWith("/clear "))&&(await t.rotateOnClear(),e.replRenderer.writeLine(m.dim(` transcript: ${t.path()}`)),y.reset()),k.result!==null&&typeof k.result=="object"&&"kind"in k.result&&k.result.kind==="submit"){l={text:k.result.message,attachments:M??[]},e.statusLine.rearm();continue}e.statusLine.rearm();continue}P=!0}i.push(_),await TW(e,_);let $=_;if(P){let k=sp(_);if(k){let A=k.name.replace(/^\//,"").split(":").pop()??"";if(A&&rm(A)){let N={skillName:A,rawArgs:k.args,source:"plugin",capabilities:{compose:!0,subagents:!0}},H=e.session.current.sessionId,C=Tr(H),B=Date.now();J(`[afk trace] preflight.start commandName=${A}`);let j=!1,ee=await vr(N,{cwd:e.stats.cwd??process.cwd(),artifactDir:C},q=>{je()&&e.replRenderer.writeLine(m.warning(`\u26A0 preflight(${A}) failed: `)+(q instanceof Error?q.message:String(q)))});j=ee!==null,J(`[afk trace] preflight.end commandName=${A} durationMs=${Date.now()-B} success=${j}`),$=im(ee?.manifestBlock,_)}}}let F=f.drainInjections();F.length>0&&($=F+$),await hx({text:$,attachments:M},e.session.current,e.stats,{setInFlight(k){n.turnInFlight=k},async onTurnComplete(k,A){await t.appendTurn(k,A)},async onAfterTurn(){await e.contextSampler.onTurn(e.stats.totalTurns),await jS(e.slashCtx),e.statusLine.rearm(),p?.repaint("observing")},rearmStatus:()=>e.statusLine.rearm(),onTerminalState:k=>y.push(k),setActiveCompositor:k=>{n.activeCompositor=k},setInterruptNotifier:k=>{n.notifyInterrupting=k},scrollRegion:e.statusLine,getCompositor:()=>a.getCompositor(),setBackgroundHandler:k=>a.setBackgroundHandler(k),setSoftStopHandler:k=>a.setSoftStopHandler(k),async onContextProgress(){await e.contextSampler.refresh(),e.statusLine.repaint(mr(e.stats,e.contextSampler))},...p?{onStageChange:k=>p.repaint(k)}:{}},e.options.thinkingUi,e.completionWriter,u,a.toRunTurnRefs(Ec(e.stats.model,e.stats.planMode)))}}finally{if(u!==void 0)for(let b of u.running())u.cancel(b.id);n.tryAbortShellForeground=null,f?.drainOnExit(),p?.stop(),d?.stop(),c?.dispose();let h=b=>console.log(b);e.completionWriter.fn=h,e.completionWriter.idleFn=h,await a.dispose(),e.inputSurfaceRef&&(e.inputSurfaceRef.current=null)}}import{execFile as xW}from"node:child_process";import{dirname as EW,isAbsolute as RW,resolve as AW}from"node:path";import{promisify as _W}from"node:util";var Ex=_W(xW),CW=3e3,IW=new Set(["empty","orphaned-dir","orphaned-registration","dead-owner"]);async function PW(){let t=(await Ex("git",["rev-parse","--git-common-dir"])).stdout.trim();if(!t)throw new Error("Not in a git repository.");let n=RW(t)?t:AW(process.cwd(),t);return EW(n)}async function Rx(e){if(e?.disabled)return{ran:!1,removedCount:0,skippedReason:"disabled"};let t;try{t=await PW()}catch{return{ran:!1,removedCount:0,skippedReason:"not-in-repo"}}let n,r=new Promise(o=>{n=setTimeout(()=>o("timeout"),CW)});try{let o=zt({execFile:Ex,repoRoot:t,dryRun:!1,scope:"interactive",bypassSoftLaunch:!0}),s=await Promise.race([o,r]);if(s==="timeout")return{ran:!1,removedCount:0,skippedReason:"timeout"};let i=s;return i.warnings.some(c=>c.toLowerCase().includes("contested"))?{ran:!1,removedCount:0,skippedReason:"lock-contested"}:{ran:!0,removedCount:i.candidates.filter(c=>IW.has(c.verdict)&&i.removed.includes(c.path)).length}}catch{return{ran:!1,removedCount:0,skippedReason:"error"}}finally{n&&clearTimeout(n)}}import{promises as MW}from"node:fs";import{dirname as OW,join as Cx}from"node:path";import{randomBytes as $W}from"node:crypto";var DW=["Generate a 2-4 word kebab-case slug describing this work request.","Rules:","- ASCII lowercase letters and digits only, separated by single hyphens","- 2 to 4 hyphen-separated words","- Maximum 30 characters total","- No prefix, no quotes, no punctuation other than hyphens","- Output ONLY the slug \u2014 no explanation, no preamble","Examples: fix-cleanup-race, add-telegram-allowlist, refactor-prompt-loader, debug-flaky-test"].join(`
2222
+ `),Ax=/^[a-z0-9]+(-[a-z0-9]+){1,3}$/,Im=30,LW=1024,FW=8e3,NW="haiku";async function BW(e,t){let n=e.trim();if(n.length===0)return t.onSkip?.("empty-message"),null;if(n.startsWith("/"))return t.onSkip?.("slash-command"),null;let r=HW(n,LW),o=new AbortController,s=setTimeout(()=>o.abort(),t.timeoutMs??FW),i=t.signal?KW([t.signal,o.signal]):o.signal,a;try{t.slugGenerator?a=await t.slugGenerator(r,i):a=await pc({token:t.token,model:t.model??NW,system:DW,user:r,maxTokens:32,signal:i})}catch(d){let p=d instanceof Error?d.message:String(d);return t.onSkip?.("slug-generator-error",p.slice(0,200)),null}finally{clearTimeout(s)}let l=UW(a);if(l===null)return t.onSkip?.("invalid-slug-output",a.slice(0,60)),null;let c=OW(t.worktreePath);return await jW(l,c)}function UW(e){let t=e.trim().toLowerCase();if(t.length===0)return null;if(Ax.test(t)&&t.length<=Im)return t;let n=t.replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"");if(n.length===0)return null;let r=n.split("-").filter(s=>s.length>0).slice(0,4);if(r.length<2)return null;let o=r[0];for(let s=1;s<r.length;s++){let i=`${o}-${r[s]}`;if(i.length>Im)break;o=i}return Ax.test(o)?o:null}async function jW(e,t){if(!await WW(Cx(t,e)))return e;let n=$W(2).toString("hex");return`${e.split("-").slice(0,3).join("-").slice(0,Im-5)}-${n}`}async function WW(e){try{return await MW.access(e),!0}catch{return!1}}function HW(e,t){let n=Buffer.from(e,"utf8");if(n.length<=t)return e;let r=t;for(;r>0&&n[r]!==void 0&&(n[r]&192)===128;)r--;return n.slice(0,r).toString("utf8")}function KW(e){let t=AbortSignal.any;if(typeof t=="function")return t.call(AbortSignal,e);let n=new AbortController;for(let r of e){if(r.aborted)return n.abort(r.reason),n.signal;r.addEventListener("abort",()=>n.abort(r.reason),{once:!0})}return n.signal}async function Ix(e){let t,n,r=Cx(e.deferred.repoRoot,".afk-worktrees","unnamed"),o=await BW(e.message,{token:e.token,...e.model!==void 0?{model:e.model}:{},...e.timeoutMs!==void 0?{timeoutMs:e.timeoutMs}:{},worktreePath:r,...e.signal!==void 0?{signal:e.signal}:{},...e.slugGenerator!==void 0?{slugGenerator:e.slugGenerator}:{},onSkip:(a,l)=>{t=a,n=l}}),s=t??"unknown",i=n;if(o!==null){let l=`${Ma(e.branchPrefix)}${o}`;try{let c=await e.deferred.create(l);return _x(e.session,c.path),{status:"created",path:c.path,branch:c.branch,slug:o}}catch(c){s="create-failed",i=(c instanceof Error?c.message:String(c)).slice(0,200)}}try{let a=await e.deferred.create(!0);return _x(e.session,a.path),{status:"created-fallback",path:a.path,branch:a.branch,reason:s,...i!==void 0?{detail:i}:{}}}catch(a){return{status:"failed",reason:a instanceof Error?a.message:String(a)}}}function _x(e,t){e&&e.setCwd(t),GW(t)}function GW(e){try{process.chdir(e)}catch{}}W();import{spawn as Px}from"child_process";import{existsSync as YW,mkdirSync as XW,readFileSync as Mx,unlinkSync as ZW,writeFileSync as QW}from"fs";import{get as eH}from"https";import{join as Ox}from"path";import{readFileSync as qW}from"fs";import{dirname as zW,join as JW}from"path";import{fileURLToPath as VW}from"url";function bn(){try{return"3.67.1"}catch{}try{let e=zW(VW(import.meta.url));for(let t of["../../package.json","../package.json"])try{let n=JSON.parse(qW(JW(e,t),"utf-8"));if(typeof n.version=="string")return n.version}catch{}}catch{}return"0.0.0-unknown"}G();var tH=64*1024,nH=1440*60*1e3,rH="update-check.json",oH="pending-update.json";function $x(){return Ox($i(),rH)}function Pm(){return Ox($i(),oH)}function Dx(){let e=$i();YW(e)||XW(e,{recursive:!0})}function sH(e,t){let n=e.split(".").map(Number),r=t.split(".").map(Number),o=Math.max(n.length,r.length);for(let s=0;s<o;s++){let i=n[s]??0,a=r[s]??0;if(a>i)return!0;if(a<i)return!1}return!1}function iH(){try{let e=Mx($x(),"utf-8"),t=JSON.parse(e);if(typeof t.latestVersion=="string"&&typeof t.checkedAt=="number")return t}catch{}return null}function aH(){try{Dx();let e=`
2223
2223
  const https = require('https');
2224
2224
  const fs = require('fs');
2225
2225
  const url = 'https://registry.npmjs.org/agent-afk/latest';
@@ -2241,36 +2241,36 @@ _ended: ${new Date().toISOString()}_
2241
2241
  `;Px(process.execPath,["-e",e],{detached:!0,stdio:"ignore"}).unref()}catch{}}function Lx(e){if(e==="off"||T.NO_UPDATE_NOTIFIER||T.CI)return null;let t=iH(),n=Date.now();if((!t||n-t.checkedAt>nH)&&aH(),!t)return null;let r=bn();return sH(r,t.latestVersion)?{currentVersion:r,latestVersion:t.latestVersion}:null}function Rc(e){let t="\x1B[33m",n="\x1B[1m",r="\x1B[2m",o="\x1B[0m";process.stderr.write(`
2242
2242
  ${t}${n}Update available:${o} ${r}${e.currentVersion}${o} \u2192 ${n}${e.latestVersion}${o}
2243
2243
  ${r}Run \`npm install -g agent-afk\` to update${o}
2244
- `)}var Mm=/^\d+\.\d+\.\d+(-[\da-z.]+)?$/i;function Om(e){if(Mm.test(e))try{Dx(),QW(Pm(),JSON.stringify({targetVersion:e,triggeredAt:Date.now()}))}catch{}}function $m(e=5e3,t="https://registry.npmjs.org/agent-afk/latest"){return new Promise(n=>{let r=!1,o=i=>{r||(r=!0,n(i))},s=(i,a)=>{try{let l=eH(i,{headers:{Accept:"application/json"}},c=>{if((c.statusCode===301||c.statusCode===302)&&typeof c.headers.location=="string"&&a>0){c.resume(),s(c.headers.location,a-1);return}if(c.statusCode!==200){c.resume(),o(void 0);return}let u="",d=0,m=!1;c.on("data",f=>{if(!m){if(d+=f.byteLength,d>tH){m=!0,l.destroy(),o(void 0);return}u+=f.toString("utf-8")}}),c.on("end",()=>{if(!m){try{let f=JSON.parse(u);if(typeof f.version=="string"&&Mm.test(f.version)){o(f.version);return}}catch{}o(void 0)}})});l.on("error",()=>o(void 0)),l.setTimeout(e,()=>{l.destroy(),o(void 0)})}catch{o(void 0)}};s(t,3)})}function Dm(e){if(Mm.test(e))try{Om(e),Px("npm",["install","-g",`agent-afk@${e}`],{detached:!0,stdio:"ignore"}).unref()}catch{}}function Fx(){try{let e=Mx(Pm(),"utf-8"),t=JSON.parse(e);if(typeof t.targetVersion=="string"){let n=bn();ZW(Pm()),n===t.targetVersion&&process.stderr.write(`\x1B[32m\x1B[1mUpdated to agent-afk v${n}\x1B[0m
2244
+ `)}var Mm=/^\d+\.\d+\.\d+(-[\da-z.]+)?$/i;function Om(e){if(Mm.test(e))try{Dx(),QW(Pm(),JSON.stringify({targetVersion:e,triggeredAt:Date.now()}))}catch{}}function $m(e=5e3,t="https://registry.npmjs.org/agent-afk/latest"){return new Promise(n=>{let r=!1,o=i=>{r||(r=!0,n(i))},s=(i,a)=>{try{let l=eH(i,{headers:{Accept:"application/json"}},c=>{if((c.statusCode===301||c.statusCode===302)&&typeof c.headers.location=="string"&&a>0){c.resume(),s(c.headers.location,a-1);return}if(c.statusCode!==200){c.resume(),o(void 0);return}let u="",d=0,p=!1;c.on("data",f=>{if(!p){if(d+=f.byteLength,d>tH){p=!0,l.destroy(),o(void 0);return}u+=f.toString("utf-8")}}),c.on("end",()=>{if(!p){try{let f=JSON.parse(u);if(typeof f.version=="string"&&Mm.test(f.version)){o(f.version);return}}catch{}o(void 0)}})});l.on("error",()=>o(void 0)),l.setTimeout(e,()=>{l.destroy(),o(void 0)})}catch{o(void 0)}};s(t,3)})}function Dm(e){if(Mm.test(e))try{Om(e),Px("npm",["install","-g",`agent-afk@${e}`],{detached:!0,stdio:"ignore"}).unref()}catch{}}function Fx(){try{let e=Mx(Pm(),"utf-8"),t=JSON.parse(e);if(typeof t.targetVersion=="string"){let n=bn();ZW(Pm()),n===t.targetVersion&&process.stderr.write(`\x1B[32m\x1B[1mUpdated to agent-afk v${n}\x1B[0m
2245
2245
  `)}}catch{}}var Ac=null;function Ux(e,t){Ac={updateInfo:e,pendingMessage:t}}function cH(e){if(e==="summary"||e==="live"||e==="off")return e;throw new Error(`Invalid --thinking-ui value: ${e}. Expected summary|live|off`)}function uH(e,t){switch(e){case"empty-message":case"slash-command":return;case"slug-generator-error":return t?`slug generation failed: ${t}`:"slug generation failed";case"invalid-slug-output":return t?`model returned invalid slug: ${JSON.stringify(t)}`:"model returned invalid slug";case"create-failed":return t?`named worktree create failed: ${t}`:"named worktree create failed";default:return"unknown reason"}}function dH(e,t){if(e.worktreeAutoname===!1)return!1;let n=T.AFK_WORKTREE_AUTONAME;if(n!==void 0){let r=n.toLowerCase();return!(r==="0"||r==="false"||r==="off"||r==="no")}return typeof t.interactive?.worktreeAutoname=="boolean"?t.interactive.worktreeAutoname:!0}function jx(e){e.command("interactive",{isDefault:!0}).description("Start interactive chat session").option("-m, --model <model>","Model to use. Short aliases: opus|opus_1m|sonnet|sonnet_1m|haiku. Any other value (e.g. `auto` for cursor-api-proxy, or a full `claude-*` ID) passes through to the SDK/proxy untouched.",Xe()).option("--max-turns <number>","Maximum conversation turns","100").option("--thinking <mode>","Thinking mode: 'adaptive' | 'disabled' | 'enabled:<N>'","enabled:max").option("--thinking-ui <mode>","Thinking display mode: summary|live|off",cH,"live").option("--effort <level>","Effort level: low|medium|high|xhigh|max").option("--max-output-tokens <n|max>","Per-response output cap ('max' = model ceiling). Env: AFK_MAX_OUTPUT_TOKENS").option("--resume <id>","Resume a persisted SDK session by id").option("--continue","Continue the most recent persisted session in cwd").option("--debug","Show SDK init metadata on startup; enables /debug command",!1).option("-w, --worktree [branch]","Create a git worktree for an isolated session. Optional value sets the branch name; otherwise auto-named. On clean exit (no uncommitted changes) the worktree and branch are auto-removed; on dirty exit the worktree is preserved.").option("--no-worktree-autoname","Disable mid-session rename of auto-named worktrees from the first user message via haiku. Default on. Also: AFK_WORKTREE_AUTONAME=0, or interactive.worktreeAutoname:false in afk.config.json.").option("--no-shell-passthrough","Disable the ! shell-passthrough feature. When set, inputs beginning with ! are sent to the model as literal text instead of being executed as shell commands. Also: AFK_SHELL_PASSTHROUGH set to 0, false, off, or no.").option("--provider <name>","Provider to use: anthropic|anthropic-direct|openai|openai-compatible. Default: auto-selected by model").option("--dump-prompt [path]",'Dump resolved SDK prompt+options+provenance to file (default: ~/.afk/logs/prompt-dump-<ISO>.json) or "stderr"').option("--mcp-config <path>","Path to an additional MCP config file (highest priority \u2014 merges over ~/.afk/config/mcp.json, project-local .mcp.json, and plugin-contributed configs). File format identical to mcp.json.").action(async t=>{if(t.debug&&(process.env.AFK_DEBUG="1"),t.dumpPrompt!==void 0){let k=t.dumpPrompt===!0?Ar.join(Bx.homedir(),".afk","logs",`prompt-dump-${new Date().toISOString().replace(/[:.]/g,"-")}.json`):String(t.dumpPrompt);process.env.AFK_DUMP_PROMPT=k,t.provider!==void 0&&t.provider!=="anthropic"&&t.provider!=="anthropic-direct"&&console.error(`[--dump-prompt] WARNING: active provider (${t.provider}) does not support prompt dumping. No file will be written.`)}let n=Nx({text:"Initializing interactive session...",...Ws}).start();if(t.resume||t.continue)try{let k=Eo({resume:t.resume,continue:t.continue});if(k&&!k.stored){n.fail("Session not found"),process.stderr.write(`Error: session not found: ${JSON.stringify(t.resume)}
2246
2246
  Run \`afk i\` then \`/resume\` to list saved sessions.
2247
2247
  `),process.exitCode=1;return}}catch(k){n.fail("Session not found");let A=k instanceof Error?k.message:String(k);process.stderr.write(`Error: ${A}
2248
2248
  Run \`afk i\` then \`/resume\` to list saved sessions.
2249
- `),process.exitCode=1;return}let r=nt(),o=T.AFK_WORKTREE_BRANCH_PREFIX??r.interactive?.worktreeBranchPrefix,s=T.AFK_WORKTREE_BOOT_PRUNE==="0",i=await Rx({disabled:s}),a=dH(t,r),l=ue(),c=t.worktree===!0&&a&&l!==void 0,u,d,m;if(t.worktree!==void 0)try{c?(m=await Mb(o!==void 0?{branchPrefix:o}:void 0),n.text="Worktree will be named from your first message"):(d=await Oa(t.worktree,o!==void 0?{branchPrefix:o}:void 0),u=d.path,n.text=`Worktree ready at ${d.path} (branch: ${d.branch})`)}catch(k){n.fail("Worktree setup failed"),K(k)}let f=i.ran&&i.removedCount>0?`Pruned ${i.removedCount} stale worktree(s). Run /worktree list for details.`:void 0,g;try{g=await QT(t,u!==void 0?{cwd:u}:void 0)}catch(k){n.fail("Invalid options"),K(k)}if(m!==void 0&&l!==void 0){let k=m,A=l;g.firstTurnHook=async N=>{let H=Nx({text:"Naming & creating worktree\u2026",...Ws}).start(),C=await Ix({deferred:k,message:N,token:A,session:g.session.current,...o!==void 0?{branchPrefix:o}:{}}).finally(()=>H.stop());if(C.status==="created"||C.status==="created-fallback"){d=k.handle(),g.stats.cwd=C.path;let B=Ar.relative(process.cwd(),C.path)||C.path;if(C.status==="created")console.log(p.dim(" \u21AA worktree: ")+`${B} `+p.dim(`(branch: ${C.branch})`));else{let j=uH(C.reason,C.detail),ee=j!==void 0?p.dim(` \u2014 ${j}`):"";console.log(p.dim(" \u21AA worktree: ")+`${B} `+p.dim(`(branch: ${C.branch})`)+ee)}}else console.warn(p.warning("\u26A0 ")+`Worktree creation failed: ${C.reason}. `+p.dim(`Continuing in ${js(process.cwd(),{maxWidth:60})} (no isolation).`))}}cr(async()=>{g.teardownTrustedSkillEvents?.(),Bt.uninstall(),g.bgSummarizer?.stop(),await g.backgroundRegistry.cancelAll().catch(()=>{}),await g.session.current.close(),g.mcpManager&&await g.mcpManager.disconnectAll(),g.memoryStore.close(),d!==void 0&&await d.cleanup({force:g.stats.totalTurns===0})}),n.succeed("Session ready"),d!==void 0?console.log(p.dim(" \u21AA worktree: ")+p.dim(js(d.path,{maxWidth:60}))+p.dim(` (branch: ${d.branch})`)):m!==void 0&&console.log(p.dim(" \u21AA worktree: named & created from your first message"));let h=await nx(()=>g.stats.model);console.log(p.dim(` transcript: ${h.path()}`)),cr(async()=>{await h.appendEnded()});let b=!1,y=()=>{if(g.stats.totalTurns===0)return;let k=To(g.stats);return b=!0,k};cr(async()=>{if(!b)try{y()}catch{}});let w={turnInFlight:!1,lastSigintAt:0};g.getInFlight=()=>w.turnInFlight;let x=1500,R=()=>{let k=Date.now();if(w.tryAbortShellForeground&&w.tryAbortShellForeground()){w.lastSigintAt=k;return}if(w.turnInFlight){g.session.current.interrupt().catch(()=>{}),w.lastSigintAt=k,w.notifyInterrupting?.(!0);let A=`
2250
- `+p.warning("\u26A0 Interrupted. Press Ctrl+C again to exit."),N=w.activeCompositor;if(N&&N.isArmed())try{N.commitAbove(A)}catch{console.log(A)}else console.log(A);return}if(k-w.lastSigintAt<x){g.session.current?.abort("sigint"),g.rl.close();return}w.lastSigintAt=k,console.log(`
2251
- `+p.info("\u2139 ")+"Press Ctrl+C again (or /exit) to quit.")};process.on("SIGINT",R),cr(async()=>{process.removeListener("SIGINT",R)});let v=!1,E=()=>{if(v)return;v=!0,g.session.current?.abort("sigterm");try{g.rl.close()}catch{}setTimeout(()=>{el().finally(()=>process.exit(0))},2e3).unref()};process.on("SIGTERM",E),cr(async()=>{process.removeListener("SIGTERM",E)});let I=!1,D=()=>{if(I)return;I=!0,g.session.current?.abort("sighup");try{g.rl.close()}catch{}setTimeout(()=>{el().finally(()=>process.exit(0))},2e3).unref()};process.on("SIGHUP",D),cr(async()=>{process.removeListener("SIGHUP",D)}),process.stdout.write("\x1B[3J\x1B[2J\x1B[H");let _=1,M=process.stdout.write.bind(process.stdout),P=process.stderr.write.bind(process.stderr),$=k=>(typeof k=="string"?k:k instanceof Uint8Array?Buffer.from(k).toString("utf8"):String(k)).match(/\n/g)?.length??0,F=k=>((A,...N)=>(_+=$(A),k(A,...N)));process.stdout.write=F(M),process.stderr.write=F(P);try{if(Ac!==null){let{updateInfo:A,pendingMessage:N}=Ac;Ac=null,N!==null&&process.stderr.write(N),A!==null&&Rc(A)}let k=g.resumeTarget?`Resuming ${g.resumeTarget.id} \xB7 ${g.stats.totalTurns} prior turn${g.stats.totalTurns===1?"":"s"}`:void 0;console.log(`
2252
- `+sg({mode:"Interactive Mode",model:g.stats.model,version:bn(),...d!==void 0?{worktree:d.branch}:{},cwd:u??process.cwd(),...k!==void 0?{metaLine:k}:{},hintLine:"/help \xB7 /model \xB7 /resume \xB7 Esc to interrupt \xB7 /exit to quit"})),f!==void 0&&console.log(p.dim(` ${f}`)),g.resumeTarget&&il(g.stats,g.completionWriter),console.log()}finally{process.stdout.write=M,process.stderr.write=P}g.preArmAnchorRow=_,g.statusLine.start(),g.slashCtx.ui.repaintStatusLine(),g.rl.on("close",async()=>{g.statusLine.stop(),pH(g,d,y),console.log(p.info("\u2139 ")+"Goodbye!"),await el(),process.exit(0)}),await xx(g,h,w,R)})}function pH(e,t,n){if(e.stats.totalTurns===0)return;console.log(he("Session Summary"));let r=[`${e.stats.totalTurns} turn${e.stats.totalTurns===1?"":"s"}`,ne(Date.now()-e.stats.sessionStartTime)];e.stats.totalCostUsd>0&&r.push(Ne(e.stats.totalCostUsd)),e.stats.totalTokens>0&&r.push(re(e.stats.totalTokens)+" tokens"),console.log(p.dim(" "+r.join(" \xB7 ")));let o=t?Ar.basename(t.path):"none";console.log(p.dim(` model: ${e.stats.model} \xB7 worktree: ${o}`));try{let i=e.stats.cwd??process.cwd(),l=lH("git",["diff","--shortstat","HEAD"],{cwd:i,encoding:"utf8",timeout:2e3}).trim();console.log(p.dim(` edits: ${l||"no files changed"}`))}catch{}let s=e.stats.sessionId;try{let i=n();!s&&i&&(s=Ar.basename(i,".json"))}catch{}s&&console.log(p.dim(" Continue with: ")+p.brand(Ao(s,e.stats.model))),console.log()}G();import mH from"ora";function Wx(e){e.command("status").description("Check agent connection status").option("-f, --format <format>","Output format (text|json)","text").action(async t=>{let n=mH("Checking status...").start();try{let r=Xe(),o=xe(r),s=$d(r),i=o==="openai-compatible"||o==="openai-codex";if(await new Ve({model:i?"gpt-4o-mini":"haiku",...s!==void 0?{apiKey:s}:{},maxTurns:1}).close(),n.succeed(`${o} provider reachable`),t.format==="json"){let l=ue(),c=$s(),u=l?T.ANTHROPIC_API_KEY?"ANTHROPIC_API_KEY":"CLAUDE_CODE_OAUTH_TOKEN":null,d=c?T.OPENAI_API_KEY?"OPENAI_API_KEY":"CODEX_API_KEY":null;console.log(JSON.stringify({providers:{anthropic:{ok:!!l,source:u},codex:{ok:!!c,source:d}},model:String(r),bypass:!0},null,2))}else console.log(`
2249
+ `),process.exitCode=1;return}let r=nt(),o=T.AFK_WORKTREE_BRANCH_PREFIX??r.interactive?.worktreeBranchPrefix,s=T.AFK_WORKTREE_BOOT_PRUNE==="0",i=await Rx({disabled:s}),a=dH(t,r),l=ue(),c=t.worktree===!0&&a&&l!==void 0,u,d,p;if(t.worktree!==void 0)try{c?(p=await Mb(o!==void 0?{branchPrefix:o}:void 0),n.text="Worktree will be named from your first message"):(d=await Oa(t.worktree,o!==void 0?{branchPrefix:o}:void 0),u=d.path,n.text=`Worktree ready at ${d.path} (branch: ${d.branch})`)}catch(k){n.fail("Worktree setup failed"),K(k)}let f=i.ran&&i.removedCount>0?`Pruned ${i.removedCount} stale worktree(s). Run /worktree list for details.`:void 0,g;try{g=await QT(t,u!==void 0?{cwd:u}:void 0)}catch(k){n.fail("Invalid options"),K(k)}if(p!==void 0&&l!==void 0){let k=p,A=l;g.firstTurnHook=async N=>{let H=Nx({text:"Naming & creating worktree\u2026",...Ws}).start(),C=await Ix({deferred:k,message:N,token:A,session:g.session.current,...o!==void 0?{branchPrefix:o}:{}}).finally(()=>H.stop());if(C.status==="created"||C.status==="created-fallback"){d=k.handle(),g.stats.cwd=C.path;let B=Ar.relative(process.cwd(),C.path)||C.path;if(C.status==="created")console.log(m.dim(" \u21AA worktree: ")+`${B} `+m.dim(`(branch: ${C.branch})`));else{let j=uH(C.reason,C.detail),ee=j!==void 0?m.dim(` \u2014 ${j}`):"";console.log(m.dim(" \u21AA worktree: ")+`${B} `+m.dim(`(branch: ${C.branch})`)+ee)}}else console.warn(m.warning("\u26A0 ")+`Worktree creation failed: ${C.reason}. `+m.dim(`Continuing in ${js(process.cwd(),{maxWidth:60})} (no isolation).`))}}cr(async()=>{g.teardownTrustedSkillEvents?.(),Bt.uninstall(),g.bgSummarizer?.stop(),await g.backgroundRegistry.cancelAll().catch(()=>{}),await g.session.current.close(),g.mcpManager&&await g.mcpManager.disconnectAll(),g.memoryStore.close(),d!==void 0&&await d.cleanup({force:g.stats.totalTurns===0})}),n.succeed("Session ready"),d!==void 0?console.log(m.dim(" \u21AA worktree: ")+m.dim(js(d.path,{maxWidth:60}))+m.dim(` (branch: ${d.branch})`)):p!==void 0&&console.log(m.dim(" \u21AA worktree: named & created from your first message"));let h=await nx(()=>g.stats.model);console.log(m.dim(` transcript: ${h.path()}`)),cr(async()=>{await h.appendEnded()});let b=!1,y=()=>{if(g.stats.totalTurns===0)return;let k=To(g.stats);return b=!0,k};cr(async()=>{if(!b)try{y()}catch{}});let w={turnInFlight:!1,lastSigintAt:0};g.getInFlight=()=>w.turnInFlight;let x=1500,R=()=>{let k=Date.now();if(w.tryAbortShellForeground&&w.tryAbortShellForeground()){w.lastSigintAt=k;return}if(w.turnInFlight){g.session.current.interrupt().catch(()=>{}),w.lastSigintAt=k,w.notifyInterrupting?.(!0);let A=`
2250
+ `+m.warning("\u26A0 Interrupted. Press Ctrl+C again to exit."),N=w.activeCompositor;if(N&&N.isArmed())try{N.commitAbove(A)}catch{console.log(A)}else console.log(A);return}if(k-w.lastSigintAt<x){g.session.current?.abort("sigint"),g.rl.close();return}w.lastSigintAt=k,console.log(`
2251
+ `+m.info("\u2139 ")+"Press Ctrl+C again (or /exit) to quit.")};process.on("SIGINT",R),cr(async()=>{process.removeListener("SIGINT",R)});let v=!1,E=()=>{if(v)return;v=!0,g.session.current?.abort("sigterm");try{g.rl.close()}catch{}setTimeout(()=>{el().finally(()=>process.exit(0))},2e3).unref()};process.on("SIGTERM",E),cr(async()=>{process.removeListener("SIGTERM",E)});let I=!1,D=()=>{if(I)return;I=!0,g.session.current?.abort("sighup");try{g.rl.close()}catch{}setTimeout(()=>{el().finally(()=>process.exit(0))},2e3).unref()};process.on("SIGHUP",D),cr(async()=>{process.removeListener("SIGHUP",D)}),process.stdout.write("\x1B[3J\x1B[2J\x1B[H");let _=1,M=process.stdout.write.bind(process.stdout),P=process.stderr.write.bind(process.stderr),$=k=>(typeof k=="string"?k:k instanceof Uint8Array?Buffer.from(k).toString("utf8"):String(k)).match(/\n/g)?.length??0,F=k=>((A,...N)=>(_+=$(A),k(A,...N)));process.stdout.write=F(M),process.stderr.write=F(P);try{if(Ac!==null){let{updateInfo:A,pendingMessage:N}=Ac;Ac=null,N!==null&&process.stderr.write(N),A!==null&&Rc(A)}let k=g.resumeTarget?`Resuming ${g.resumeTarget.id} \xB7 ${g.stats.totalTurns} prior turn${g.stats.totalTurns===1?"":"s"}`:void 0;console.log(`
2252
+ `+sg({mode:"Interactive Mode",model:g.stats.model,version:bn(),...d!==void 0?{worktree:d.branch}:{},cwd:u??process.cwd(),...k!==void 0?{metaLine:k}:{},hintLine:"/help \xB7 /model \xB7 /resume \xB7 Esc to interrupt \xB7 /exit to quit"})),f!==void 0&&console.log(m.dim(` ${f}`)),g.resumeTarget&&il(g.stats,g.completionWriter),console.log()}finally{process.stdout.write=M,process.stderr.write=P}g.preArmAnchorRow=_,g.statusLine.start(),g.slashCtx.ui.repaintStatusLine(),g.rl.on("close",async()=>{g.statusLine.stop(),pH(g,d,y),console.log(m.info("\u2139 ")+"Goodbye!"),await el(),process.exit(0)}),await xx(g,h,w,R)})}function pH(e,t,n){if(e.stats.totalTurns===0)return;console.log(he("Session Summary"));let r=[`${e.stats.totalTurns} turn${e.stats.totalTurns===1?"":"s"}`,ne(Date.now()-e.stats.sessionStartTime)];e.stats.totalCostUsd>0&&r.push(Ne(e.stats.totalCostUsd)),e.stats.totalTokens>0&&r.push(re(e.stats.totalTokens)+" tokens"),console.log(m.dim(" "+r.join(" \xB7 ")));let o=t?Ar.basename(t.path):"none";console.log(m.dim(` model: ${e.stats.model} \xB7 worktree: ${o}`));try{let i=e.stats.cwd??process.cwd(),l=lH("git",["diff","--shortstat","HEAD"],{cwd:i,encoding:"utf8",timeout:2e3}).trim();console.log(m.dim(` edits: ${l||"no files changed"}`))}catch{}let s=e.stats.sessionId;try{let i=n();!s&&i&&(s=Ar.basename(i,".json"))}catch{}s&&console.log(m.dim(" Continue with: ")+m.brand(Ao(s,e.stats.model))),console.log()}G();import mH from"ora";function Wx(e){e.command("status").description("Check agent connection status").option("-f, --format <format>","Output format (text|json)","text").action(async t=>{let n=mH("Checking status...").start();try{let r=Xe(),o=xe(r),s=$d(r),i=o==="openai-compatible"||o==="openai-codex";if(await new Ve({model:i?"gpt-4o-mini":"haiku",...s!==void 0?{apiKey:s}:{},maxTurns:1}).close(),n.succeed(`${o} provider reachable`),t.format==="json"){let l=ue(),c=$s(),u=l?T.ANTHROPIC_API_KEY?"ANTHROPIC_API_KEY":"CLAUDE_CODE_OAUTH_TOKEN":null,d=c?T.OPENAI_API_KEY?"OPENAI_API_KEY":"CODEX_API_KEY":null;console.log(JSON.stringify({providers:{anthropic:{ok:!!l,source:u},codex:{ok:!!c,source:d}},model:String(r),bypass:!0},null,2))}else console.log(`
2253
2253
  `+eg("Agent AFK \xB7 Status",[{label:"Provider",value:o,kind:"info"},{label:"Auth",value:i?s?"Found (OPENAI_API_KEY / CODEX_API_KEY)":"Reading ~/.codex/auth.json (run `afk provider auth diagnose`)":s?"Found (ANTHROPIC_API_KEY)":"Falling back to Claude OAuth",kind:s?"ok":"warn"},{label:"Model",value:String(r),kind:"info"},{label:"Bypass",value:"Permissions disabled",kind:"warn"}])+`
2254
- `)}catch(r){n.fail("Connection failed"),K(r)}})}G();function Hx(e){e.command("config").description("View current configuration").option("-f, --format <format>","Output format (text|json)","text").action(t=>{let n=T.AFK_MODEL??T.CLAUDE_MODEL,r=n??"sonnet",o=xe(n),s=T.ANTHROPIC_API_KEY||T.CLAUDE_CODE_OAUTH_TOKEN,i=T.OPENAI_API_KEY||T.CODEX_API_KEY,a=o==="anthropic"?s:i,l=s?T.ANTHROPIC_API_KEY?"ANTHROPIC_API_KEY":"CLAUDE_CODE_OAUTH_TOKEN":null,c=i?T.OPENAI_API_KEY?"OPENAI_API_KEY":"CODEX_API_KEY":null;if(t.format==="json")console.log(JSON.stringify({model:r,provider:o,apiKey:{present:!!a,source:o==="anthropic"?l:c},thinking:T.AFK_THINKING||null,effort:T.AFK_EFFORT||null,bypass:!0,raw_env:{AFK_MODEL:T.AFK_MODEL??null,AFK_THINKING:T.AFK_THINKING??null,AFK_EFFORT:T.AFK_EFFORT??null,ANTHROPIC_API_KEY:T.ANTHROPIC_API_KEY?"set":"unset",CLAUDE_CODE_OAUTH_TOKEN:T.CLAUDE_CODE_OAUTH_TOKEN?"set":"unset",OPENAI_API_KEY:T.OPENAI_API_KEY?"set":"unset",CODEX_API_KEY:T.CODEX_API_KEY?"set":"unset"}},null,2));else{console.log(p.info(`\u{1F4CB} Current Configuration:
2255
- `)),console.log(` Model: ${p.info(n?r:r+" (default)")}`),console.log(` Provider: ${p.plan(o)}`),console.log(o==="anthropic"?` API Key: ${a?p.success("\u2713 Set (ANTHROPIC_API_KEY / CLAUDE_CODE_OAUTH_TOKEN)"):p.warning("\u26A0 Not set \u2014 subprocess will fall back to OAuth / keychain")}`:` API Key: ${a?p.success("\u2713 Set (OPENAI_API_KEY / CODEX_API_KEY)"):p.warning("\u26A0 Not set \u2014 falling back to `codex login` state")}`);let u=T.AFK_THINKING||"(unset \u2014 SDK default)";console.log(` Thinking: ${p.info(u)}`);let d=T.AFK_EFFORT||"(unset \u2014 SDK default)";console.log(` Effort: ${p.info(d)}`),console.log(` Bypass Permissions: ${p.warning("true (enabled)")}`),console.log(p.meta(`
2256
- Environment variables:`)),console.log(p.meta(" AFK_MODEL - Default model id (canonical; accepts short aliases or full ids)")),console.log(p.meta(" CLAUDE_MODEL - Legacy alias for AFK_MODEL (Claude-only deployments)")),console.log(p.meta(" ANTHROPIC_API_KEY - Anthropic API key (Claude models)")),console.log(p.meta(" CLAUDE_CODE_OAUTH_TOKEN - Anthropic OAuth token (Claude models)")),console.log(p.meta(" OPENAI_API_KEY / CODEX_API_KEY - OpenAI API key (Codex models)")),console.log(p.meta(" AFK_THINKING - Thinking mode (Claude only: adaptive|disabled|enabled:<N>)")),console.log(p.meta(" AFK_EFFORT - Effort level (low|medium|high|xhigh|max)")),console.log(p.meta(" AFK_TIMEOUT_MS - Per-tick daemon session timeout in ms")),console.log(p.meta(" AFK_SESSIONSTART_COOLDOWN_MS - Phase 6 cooldown between sessionstart fires (default 6h)")),console.log("")}})}G();import jH from"path";import WH from"os";import{createServer as CH}from"node:http";import{writeFileSync as IH,unlinkSync as PH,mkdirSync as MH}from"node:fs";import{dirname as OH,join as $H}from"node:path";G();import{mkdirSync as kH,appendFileSync as vH}from"node:fs";import{dirname as TH}from"node:path";import{execFile as xH}from"node:child_process";import{promisify as EH}from"node:util";import{randomUUID as RH}from"node:crypto";import*as tE from"node-cron";var _c=class{_count=0;increment(){this._count++}decrement(){this._count>0&&this._count--}isIdle(){return this._count===0}count(){return this._count}};W();import{mkdirSync as Gx,readdirSync as qx,readFileSync as fH,renameSync as gH,unlinkSync as zx,writeFileSync as hH}from"node:fs";import{randomBytes as Kx}from"node:crypto";import{join as Lm}from"node:path";function Jx(e,t={},n=Dt()){Gx(n,{recursive:!0});let o=qx(n).filter(f=>f.endsWith(".json")).length+1,s=`q-${Date.now()}-${Kx(3).toString("hex")}`,i=new Date().toISOString(),a={id:s,command:e,enqueuedAt:i,sequence:o,...t.notifyOn!==void 0?{notifyOn:t.notifyOn}:{}},c=`${String(o).padStart(4,"0")}-${s}.json`,u=Lm(n,c),d=Kx(4).toString("hex"),m=Lm(n,`.tmp-${d}.json`);try{hH(m,JSON.stringify(a),"utf-8"),gH(m,u)}catch(f){try{zx(m)}catch{}throw f}return a}function Vx(e=Dt()){Gx(e,{recursive:!0});let n=qx(e).filter(i=>i.endsWith(".json")&&!i.startsWith(".tmp-")).sort()[0];if(n===void 0)return null;let r=Lm(e,n),o=fH(r,"utf-8"),s=JSON.parse(o);return zx(r),s}W();W();function Yx(e){if(!e.taskId)throw new Error("ScheduledTask.taskId is required");if(!e.command)throw new Error(`task ${e.taskId}: command is required`);if((e.trigger==="cron"||e.trigger==="both")&&!e.cronExpression)throw new Error(`task ${e.taskId}: cronExpression required for trigger=${e.trigger}`);if(e.trigger==="pull"&&e.cronExpression!==void 0)throw new Error(`task ${e.taskId}: cronExpression must not be set when trigger='pull' \u2014 pull tasks are dequeued from the queue directory, not scheduled via cron`)}W();import{existsSync as Xx,readFileSync as yH,readdirSync as bH}from"node:fs";var Zx=360*60*1e3;function Qx(){return Sn()}function wH(e,t){if(!Xx(t))return null;let n;try{n=yH(t,"utf-8")}catch{return null}let r=n.split(`
2257
- `);for(let o=r.length-1;o>=0;o-=1){let s=r[o];if(s)try{let i=JSON.parse(s);if(i.taskId!==e||typeof i.triggeredAt!="string")continue;let a=Date.parse(i.triggeredAt);if(Number.isNaN(a))continue;return a}catch{continue}}return null}function SH(e){if(!Xx(e))return 0;try{return bH(e).filter(t=>!t.startsWith(".")).length}catch{return 0}}function eE(e){let t=wH(e.taskId,e.telemetryPath);if(t!==null&&e.cooldownMs>0){let r=e.nowMs-t;if(r<e.cooldownMs)return{fire:!1,skipReason:"cooldown",lastFiredAtMs:t,cooldownRemainingMs:e.cooldownMs-r}}let n=SH(e.briefsDir);return n>0?{fire:!1,skipReason:"briefs_pending",pendingBriefCount:n,...t!==null?{lastFiredAtMs:t}:{}}:{fire:!0,...t!==null?{lastFiredAtMs:t}:{}}}var AH=EH(xH);function _H(e){return`${e.replace(/[^A-Za-z0-9_-]+/g,"-").replace(/^-+|-+$/g,"")||"task"}-${RH()}`}var Cc=class{registry=new Map;options;defaultCooldownMs;briefsDir;now;idleDetector=new _c;pullPollTimer;isDequeuing=!1;queueDir;constructor(t={}){this.options=t,this.defaultCooldownMs=t.cooldownMs??Zx,this.briefsDir=t.briefsDir??Qx(),this.now=t.now??Date.now,this.queueDir=t.queueDir??Dt(),this.ensureTelemetrySink()}register(t){if(Yx(t),this.registry.has(t.taskId))throw new Error(`task ${t.taskId} is already registered`);let n;(t.trigger==="cron"||t.trigger==="both")&&(n=tE.schedule(t.cronExpression,()=>{this.runOnce(t,"cron").catch(()=>{})},{name:t.taskId})),this.registry.set(t.taskId,{task:t,cronTask:n})}unregister(t){let n=this.registry.get(t);n&&(n.cronTask&&(Promise.resolve(n.cronTask.stop()).catch(()=>{}),Promise.resolve(n.cronTask.destroy()).catch(()=>{})),this.registry.delete(t))}list(){return Array.from(this.registry.values()).map(t=>t.task)}async tick(t){let n=this.registry.get(t);if(!n)throw new Error(`task ${t} is not registered`);return this.runOnce(n.task,"cron")}async fireOnStart(){let t=Array.from(this.registry.values()).map(r=>r.task).filter(r=>r.trigger==="sessionstart"||r.trigger==="both"),n=[];for(let r of t){let o=r.debounceMs??this.defaultCooldownMs,s=eE({taskId:r.taskId,cooldownMs:o,nowMs:this.now(),telemetryPath:this.telemetryPath(),briefsDir:this.briefsDir});s.fire?n.push(await this.runOnce(r,"sessionstart")):n.push(this.recordSkip(r,s))}return n}async stop(){this.pullPollTimer!==void 0&&(clearInterval(this.pullPollTimer),this.pullPollTimer=void 0);for(let t of Array.from(this.registry.keys()))this.unregister(t)}startPullLoop(){if(this.pullPollTimer!==void 0)return;let t=this.options.pullPollIntervalMs;!t||t<=0||(this.pullPollTimer=setInterval(()=>{this.pullTick()},t).unref())}async pullTick(){if(this.idleDetector.isIdle()&&!this.isDequeuing){this.isDequeuing=!0;try{let t=Vx(this.queueDir);if(t===null)return;let n={taskId:t.id,command:t.command,trigger:"pull",...t.notifyOn!==void 0?{notifyOn:t.notifyOn}:{}};await this.runOnce(n,"pull")}catch{}finally{this.isDequeuing=!1}}}async runOnce(t,n){if(t.command==="__BUILTIN_WORKTREE_PRUNE__")return this.runBuiltinWorktreePrune(t,n);let r=new Date(this.now()),o=this.now(),s={taskId:t.taskId,command:wo(t.command),trigger:n,...t.cronExpression!==void 0?{cronExpression:t.cronExpression}:{},triggeredAt:r.toISOString()},i=null,a=null;this.idleDetector.increment();try{let l=this.spawnSession(t.taskId);i=l.session,a=l.memoryStore;let c=await i.sendMessage(t.command),u={...s,durationMs:this.now()-o,status:"success",responseExcerpt:wo(c.content.slice(0,280))};return this.writeTelemetry(u,t),u}catch(l){let c={...s,durationMs:this.now()-o,status:"error",errorMessage:wo(l instanceof Error?l.message:String(l))};return this.writeTelemetry(c,t),c}finally{if(this.idleDetector.decrement(),i)try{await i.close()}catch{}a?.close()}}recordSkip(t,n){let r=new Date(this.now()),o={taskId:t.taskId,command:t.command,trigger:"sessionstart",...t.cronExpression!==void 0?{cronExpression:t.cronExpression}:{},triggeredAt:r.toISOString(),durationMs:0,status:"skipped",...n.skipReason!==void 0?{skipReason:n.skipReason}:{}};return this.writeTelemetry(o,t),o}async runBuiltinWorktreePrune(t,n){let r=new Date(this.now()),o=this.now(),s={taskId:t.taskId,command:t.command,trigger:n,...t.cronExpression!==void 0?{cronExpression:t.cronExpression}:{},triggeredAt:r.toISOString()};try{let i=T.AFK_WORKTREE_SWEEP_ROOT??process.cwd(),a=parseInt(T.AFK_WORKTREE_MAX_AGE_CLEAN??"",10)||14,l=parseInt(T.AFK_WORKTREE_MAX_AGE_DIRTY??"",10)||30,c=await zt({execFile:AH,repoRoot:i,dryRun:!1,maxAgeDaysClean:a,maxAgeDaysDirty:l,scope:"all",telemetryPath:this.telemetryPath()}),u=new Set(["empty","stale-clean","orphaned-dir","orphaned-registration","dead-owner"]),d=c.dryRun?`\u{1F50D} worktree-prune (dry-run): would remove ${c.candidates.filter(f=>u.has(f.verdict)).length} worktree(s)`:`\u2702\uFE0F worktree-prune: removed ${c.removed.length}, warned ${c.warnings.length}`,m={...s,durationMs:this.now()-o,status:"success",responseExcerpt:d};return this.writeTelemetry(m,t),m}catch(i){let a={...s,durationMs:this.now()-o,status:"error",errorMessage:wo(i instanceof Error?i.message:String(i))};return this.writeTelemetry(a,t),a}}spawnSession(t){let{registry:n,memoryStore:r}=So(void 0,"daemon"),o=vo({sessionLabel:_H(t)}),s={model:"sonnet",permissionMode:"bypassPermissions",hookRegistry:n,...o?{traceWriter:o.writer}:{},...this.options.sessionConfig};return{session:this.options.sessionFactory?this.options.sessionFactory(s):new Ve(sn(s)),memoryStore:r}}telemetryPath(){return this.options.telemetryPath??$t()}ensureTelemetrySink(){try{kH(TH(this.telemetryPath()),{recursive:!0})}catch{}}writeTelemetry(t,n){try{vH(this.telemetryPath(),`${JSON.stringify(t)}
2254
+ `)}catch(r){n.fail("Connection failed"),K(r)}})}G();function Hx(e){e.command("config").description("View current configuration").option("-f, --format <format>","Output format (text|json)","text").action(t=>{let n=T.AFK_MODEL??T.CLAUDE_MODEL,r=n??"sonnet",o=xe(n),s=T.ANTHROPIC_API_KEY||T.CLAUDE_CODE_OAUTH_TOKEN,i=T.OPENAI_API_KEY||T.CODEX_API_KEY,a=o==="anthropic"?s:i,l=s?T.ANTHROPIC_API_KEY?"ANTHROPIC_API_KEY":"CLAUDE_CODE_OAUTH_TOKEN":null,c=i?T.OPENAI_API_KEY?"OPENAI_API_KEY":"CODEX_API_KEY":null;if(t.format==="json")console.log(JSON.stringify({model:r,provider:o,apiKey:{present:!!a,source:o==="anthropic"?l:c},thinking:T.AFK_THINKING||null,effort:T.AFK_EFFORT||null,bypass:!0,raw_env:{AFK_MODEL:T.AFK_MODEL??null,AFK_THINKING:T.AFK_THINKING??null,AFK_EFFORT:T.AFK_EFFORT??null,ANTHROPIC_API_KEY:T.ANTHROPIC_API_KEY?"set":"unset",CLAUDE_CODE_OAUTH_TOKEN:T.CLAUDE_CODE_OAUTH_TOKEN?"set":"unset",OPENAI_API_KEY:T.OPENAI_API_KEY?"set":"unset",CODEX_API_KEY:T.CODEX_API_KEY?"set":"unset"}},null,2));else{console.log(m.info(`\u{1F4CB} Current Configuration:
2255
+ `)),console.log(` Model: ${m.info(n?r:r+" (default)")}`),console.log(` Provider: ${m.plan(o)}`),console.log(o==="anthropic"?` API Key: ${a?m.success("\u2713 Set (ANTHROPIC_API_KEY / CLAUDE_CODE_OAUTH_TOKEN)"):m.warning("\u26A0 Not set \u2014 subprocess will fall back to OAuth / keychain")}`:` API Key: ${a?m.success("\u2713 Set (OPENAI_API_KEY / CODEX_API_KEY)"):m.warning("\u26A0 Not set \u2014 falling back to `codex login` state")}`);let u=T.AFK_THINKING||"(unset \u2014 SDK default)";console.log(` Thinking: ${m.info(u)}`);let d=T.AFK_EFFORT||"(unset \u2014 SDK default)";console.log(` Effort: ${m.info(d)}`),console.log(` Bypass Permissions: ${m.warning("true (enabled)")}`),console.log(m.meta(`
2256
+ Environment variables:`)),console.log(m.meta(" AFK_MODEL - Default model id (canonical; accepts short aliases or full ids)")),console.log(m.meta(" CLAUDE_MODEL - Legacy alias for AFK_MODEL (Claude-only deployments)")),console.log(m.meta(" ANTHROPIC_API_KEY - Anthropic API key (Claude models)")),console.log(m.meta(" CLAUDE_CODE_OAUTH_TOKEN - Anthropic OAuth token (Claude models)")),console.log(m.meta(" OPENAI_API_KEY / CODEX_API_KEY - OpenAI API key (Codex models)")),console.log(m.meta(" AFK_THINKING - Thinking mode (Claude only: adaptive|disabled|enabled:<N>)")),console.log(m.meta(" AFK_EFFORT - Effort level (low|medium|high|xhigh|max)")),console.log(m.meta(" AFK_TIMEOUT_MS - Per-tick daemon session timeout in ms")),console.log(m.meta(" AFK_SESSIONSTART_COOLDOWN_MS - Phase 6 cooldown between sessionstart fires (default 6h)")),console.log("")}})}G();import jH from"path";import WH from"os";import{createServer as CH}from"node:http";import{writeFileSync as IH,unlinkSync as PH,mkdirSync as MH}from"node:fs";import{dirname as OH,join as $H}from"node:path";G();import{mkdirSync as kH,appendFileSync as vH}from"node:fs";import{dirname as TH}from"node:path";import{execFile as xH}from"node:child_process";import{promisify as EH}from"node:util";import{randomUUID as RH}from"node:crypto";import*as tE from"node-cron";var _c=class{_count=0;increment(){this._count++}decrement(){this._count>0&&this._count--}isIdle(){return this._count===0}count(){return this._count}};W();import{mkdirSync as Gx,readdirSync as qx,readFileSync as fH,renameSync as gH,unlinkSync as zx,writeFileSync as hH}from"node:fs";import{randomBytes as Kx}from"node:crypto";import{join as Lm}from"node:path";function Jx(e,t={},n=Dt()){Gx(n,{recursive:!0});let o=qx(n).filter(f=>f.endsWith(".json")).length+1,s=`q-${Date.now()}-${Kx(3).toString("hex")}`,i=new Date().toISOString(),a={id:s,command:e,enqueuedAt:i,sequence:o,...t.notifyOn!==void 0?{notifyOn:t.notifyOn}:{}},c=`${String(o).padStart(4,"0")}-${s}.json`,u=Lm(n,c),d=Kx(4).toString("hex"),p=Lm(n,`.tmp-${d}.json`);try{hH(p,JSON.stringify(a),"utf-8"),gH(p,u)}catch(f){try{zx(p)}catch{}throw f}return a}function Vx(e=Dt()){Gx(e,{recursive:!0});let n=qx(e).filter(i=>i.endsWith(".json")&&!i.startsWith(".tmp-")).sort()[0];if(n===void 0)return null;let r=Lm(e,n),o=fH(r,"utf-8"),s=JSON.parse(o);return zx(r),s}W();W();function Yx(e){if(!e.taskId)throw new Error("ScheduledTask.taskId is required");if(!e.command)throw new Error(`task ${e.taskId}: command is required`);if((e.trigger==="cron"||e.trigger==="both")&&!e.cronExpression)throw new Error(`task ${e.taskId}: cronExpression required for trigger=${e.trigger}`);if(e.trigger==="pull"&&e.cronExpression!==void 0)throw new Error(`task ${e.taskId}: cronExpression must not be set when trigger='pull' \u2014 pull tasks are dequeued from the queue directory, not scheduled via cron`)}W();import{existsSync as Xx,readFileSync as yH,readdirSync as bH}from"node:fs";var Zx=360*60*1e3;function Qx(){return Sn()}function wH(e,t){if(!Xx(t))return null;let n;try{n=yH(t,"utf-8")}catch{return null}let r=n.split(`
2257
+ `);for(let o=r.length-1;o>=0;o-=1){let s=r[o];if(s)try{let i=JSON.parse(s);if(i.taskId!==e||typeof i.triggeredAt!="string")continue;let a=Date.parse(i.triggeredAt);if(Number.isNaN(a))continue;return a}catch{continue}}return null}function SH(e){if(!Xx(e))return 0;try{return bH(e).filter(t=>!t.startsWith(".")).length}catch{return 0}}function eE(e){let t=wH(e.taskId,e.telemetryPath);if(t!==null&&e.cooldownMs>0){let r=e.nowMs-t;if(r<e.cooldownMs)return{fire:!1,skipReason:"cooldown",lastFiredAtMs:t,cooldownRemainingMs:e.cooldownMs-r}}let n=SH(e.briefsDir);return n>0?{fire:!1,skipReason:"briefs_pending",pendingBriefCount:n,...t!==null?{lastFiredAtMs:t}:{}}:{fire:!0,...t!==null?{lastFiredAtMs:t}:{}}}var AH=EH(xH);function _H(e){return`${e.replace(/[^A-Za-z0-9_-]+/g,"-").replace(/^-+|-+$/g,"")||"task"}-${RH()}`}var Cc=class{registry=new Map;options;defaultCooldownMs;briefsDir;now;idleDetector=new _c;pullPollTimer;isDequeuing=!1;queueDir;constructor(t={}){this.options=t,this.defaultCooldownMs=t.cooldownMs??Zx,this.briefsDir=t.briefsDir??Qx(),this.now=t.now??Date.now,this.queueDir=t.queueDir??Dt(),this.ensureTelemetrySink()}register(t){if(Yx(t),this.registry.has(t.taskId))throw new Error(`task ${t.taskId} is already registered`);let n;(t.trigger==="cron"||t.trigger==="both")&&(n=tE.schedule(t.cronExpression,()=>{this.runOnce(t,"cron").catch(()=>{})},{name:t.taskId})),this.registry.set(t.taskId,{task:t,cronTask:n})}unregister(t){let n=this.registry.get(t);n&&(n.cronTask&&(Promise.resolve(n.cronTask.stop()).catch(()=>{}),Promise.resolve(n.cronTask.destroy()).catch(()=>{})),this.registry.delete(t))}list(){return Array.from(this.registry.values()).map(t=>t.task)}async tick(t){let n=this.registry.get(t);if(!n)throw new Error(`task ${t} is not registered`);return this.runOnce(n.task,"cron")}async fireOnStart(){let t=Array.from(this.registry.values()).map(r=>r.task).filter(r=>r.trigger==="sessionstart"||r.trigger==="both"),n=[];for(let r of t){let o=r.debounceMs??this.defaultCooldownMs,s=eE({taskId:r.taskId,cooldownMs:o,nowMs:this.now(),telemetryPath:this.telemetryPath(),briefsDir:this.briefsDir});s.fire?n.push(await this.runOnce(r,"sessionstart")):n.push(this.recordSkip(r,s))}return n}async stop(){this.pullPollTimer!==void 0&&(clearInterval(this.pullPollTimer),this.pullPollTimer=void 0);for(let t of Array.from(this.registry.keys()))this.unregister(t)}startPullLoop(){if(this.pullPollTimer!==void 0)return;let t=this.options.pullPollIntervalMs;!t||t<=0||(this.pullPollTimer=setInterval(()=>{this.pullTick()},t).unref())}async pullTick(){if(this.idleDetector.isIdle()&&!this.isDequeuing){this.isDequeuing=!0;try{let t=Vx(this.queueDir);if(t===null)return;let n={taskId:t.id,command:t.command,trigger:"pull",...t.notifyOn!==void 0?{notifyOn:t.notifyOn}:{}};await this.runOnce(n,"pull")}catch{}finally{this.isDequeuing=!1}}}async runOnce(t,n){if(t.command==="__BUILTIN_WORKTREE_PRUNE__")return this.runBuiltinWorktreePrune(t,n);let r=new Date(this.now()),o=this.now(),s={taskId:t.taskId,command:wo(t.command),trigger:n,...t.cronExpression!==void 0?{cronExpression:t.cronExpression}:{},triggeredAt:r.toISOString()},i=null,a=null;this.idleDetector.increment();try{let l=this.spawnSession(t.taskId);i=l.session,a=l.memoryStore;let c=await i.sendMessage(t.command),u={...s,durationMs:this.now()-o,status:"success",responseExcerpt:wo(c.content.slice(0,280))};return this.writeTelemetry(u,t),u}catch(l){let c={...s,durationMs:this.now()-o,status:"error",errorMessage:wo(l instanceof Error?l.message:String(l))};return this.writeTelemetry(c,t),c}finally{if(this.idleDetector.decrement(),i)try{await i.close()}catch{}a?.close()}}recordSkip(t,n){let r=new Date(this.now()),o={taskId:t.taskId,command:t.command,trigger:"sessionstart",...t.cronExpression!==void 0?{cronExpression:t.cronExpression}:{},triggeredAt:r.toISOString(),durationMs:0,status:"skipped",...n.skipReason!==void 0?{skipReason:n.skipReason}:{}};return this.writeTelemetry(o,t),o}async runBuiltinWorktreePrune(t,n){let r=new Date(this.now()),o=this.now(),s={taskId:t.taskId,command:t.command,trigger:n,...t.cronExpression!==void 0?{cronExpression:t.cronExpression}:{},triggeredAt:r.toISOString()};try{let i=T.AFK_WORKTREE_SWEEP_ROOT??process.cwd(),a=parseInt(T.AFK_WORKTREE_MAX_AGE_CLEAN??"",10)||14,l=parseInt(T.AFK_WORKTREE_MAX_AGE_DIRTY??"",10)||30,c=await zt({execFile:AH,repoRoot:i,dryRun:!1,maxAgeDaysClean:a,maxAgeDaysDirty:l,scope:"all",telemetryPath:this.telemetryPath()}),u=new Set(["empty","stale-clean","orphaned-dir","orphaned-registration","dead-owner"]),d=c.dryRun?`\u{1F50D} worktree-prune (dry-run): would remove ${c.candidates.filter(f=>u.has(f.verdict)).length} worktree(s)`:`\u2702\uFE0F worktree-prune: removed ${c.removed.length}, warned ${c.warnings.length}`,p={...s,durationMs:this.now()-o,status:"success",responseExcerpt:d};return this.writeTelemetry(p,t),p}catch(i){let a={...s,durationMs:this.now()-o,status:"error",errorMessage:wo(i instanceof Error?i.message:String(i))};return this.writeTelemetry(a,t),a}}spawnSession(t){let{registry:n,memoryStore:r}=So(void 0,"daemon"),o=vo({sessionLabel:_H(t)}),s={model:"sonnet",permissionMode:"bypassPermissions",hookRegistry:n,...o?{traceWriter:o.writer}:{},...this.options.sessionConfig};return{session:this.options.sessionFactory?this.options.sessionFactory(s):new Ve(sn(s)),memoryStore:r}}telemetryPath(){return this.options.telemetryPath??$t()}ensureTelemetrySink(){try{kH(TH(this.telemetryPath()),{recursive:!0})}catch{}}writeTelemetry(t,n){try{vH(this.telemetryPath(),`${JSON.stringify(t)}
2258
2258
  `,"utf-8"),this.fireOnTaskComplete(t,n)}catch(r){let o=r instanceof Error?r.message:String(r);console.error(`[daemon] telemetry write failed: ${o}`)}}fireOnTaskComplete(t,n){let r=this.options.onTaskComplete;if(r&&!(n!==void 0&&(n.notifyOn==="never"||n.notifyOn==="failure"&&t.status!=="error")))try{let o=r(t);o instanceof Promise&&o.catch(s=>{let i=s instanceof Error?s.message:String(s);console.error(`[daemon] onTaskComplete callback failed: ${i}`)})}catch(o){let s=o instanceof Error?o.message:String(o);console.error(`[daemon] onTaskComplete callback failed: ${s}`)}}};W();var DH=7777;async function nE(e={}){let t=new Cc({...e.sessionConfig!==void 0?{sessionConfig:e.sessionConfig}:{},...e.telemetryPath!==void 0?{telemetryPath:e.telemetryPath}:{},...e.sessionFactory!==void 0?{sessionFactory:e.sessionFactory}:{},...e.cooldownMs!==void 0?{cooldownMs:e.cooldownMs}:{},...e.briefsDir!==void 0?{briefsDir:e.briefsDir}:{},...e.now!==void 0?{now:e.now}:{},...e.onTaskComplete!==void 0?{onTaskComplete:e.onTaskComplete}:{},...e.pullPollIntervalMs!==void 0?{pullPollIntervalMs:e.pullPollIntervalMs}:{},...e.queueDir!==void 0?{queueDir:e.queueDir}:{}});e.pullPollIntervalMs!==void 0&&e.pullPollIntervalMs>0&&t.startPullLoop();for(let s of e.tasks??[])t.register(s);let n=$H(Nr("default"),"port");MH(OH(n),{recursive:!0});let r=CH((s,i)=>FH(s,i,t)),o=await BH(r,e.port??DH);try{IH(n,String(o),"utf-8")}catch{}return{port:o,scheduler:t,registerTask(s){t.register(s)},unregisterTask(s){t.unregister(s)},tickOnce(s){return t.tick(s)},fireOnStart(){return t.fireOnStart()},async stop(){await t.stop();try{PH(n)}catch{}await UH(r)}}}function LH(e){return new Promise((t,n)=>{let r=[];e.on("data",o=>r.push(o)),e.on("end",()=>t(Buffer.concat(r).toString("utf-8"))),e.on("error",n)})}function FH(e,t,n){NH(e,t,n).catch(r=>{let o=r instanceof Error?r.message:String(r);t.headersSent||t.writeHead(500,{"Content-Type":"application/json"}),t.end(JSON.stringify({error:o}))})}async function NH(e,t,n){let r=e.url??"/";if(e.method==="GET"&&r==="/health"){let o=JSON.stringify({status:"ok",tasks:n.list().length});t.writeHead(200,{"Content-Type":"application/json"}),t.end(o);return}if(e.method==="GET"&&r==="/tasks"){t.writeHead(200,{"Content-Type":"application/json"}),t.end(JSON.stringify(n.list()));return}if(e.method==="POST"&&r==="/tasks"){let o=await LH(e),s;try{s=JSON.parse(o)}catch{t.writeHead(400,{"Content-Type":"application/json"}),t.end(JSON.stringify({error:"invalid JSON body"}));return}if(!s||typeof s!="object"){t.writeHead(400,{"Content-Type":"application/json"}),t.end(JSON.stringify({error:"body must be an object"}));return}let i=s;if(typeof i.taskId!="string"||typeof i.command!="string"||typeof i.cron!="string"){t.writeHead(400,{"Content-Type":"application/json"}),t.end(JSON.stringify({error:"taskId, command, and cron are required strings"}));return}let a={taskId:i.taskId,command:i.command,trigger:i.trigger??"cron",cronExpression:i.cron,...i.notifyOn!==void 0?{notifyOn:i.notifyOn}:{}};try{n.register(a)}catch(l){let c=l instanceof Error?l.message:String(l),u=c.includes("already registered")?409:400;t.writeHead(u,{"Content-Type":"application/json"}),t.end(JSON.stringify({error:c}));return}t.writeHead(201,{"Content-Type":"application/json"}),t.end(JSON.stringify({ok:!0}));return}if(e.method==="DELETE"&&r.startsWith("/tasks/")){let o=r.slice(7);if(!o){t.writeHead(400,{"Content-Type":"application/json"}),t.end(JSON.stringify({error:"taskId required in URL"}));return}if(!n.list().some(i=>i.taskId===o)){t.writeHead(404,{"Content-Type":"application/json"}),t.end(JSON.stringify({error:"not found"}));return}n.unregister(o),t.writeHead(200,{"Content-Type":"application/json"}),t.end(JSON.stringify({ok:!0}));return}t.writeHead(404,{"Content-Type":"application/json"}),t.end(JSON.stringify({error:"not found"}))}function BH(e,t){return new Promise((n,r)=>{e.once("error",r),e.listen(t,()=>{e.removeListener("error",r);let o=e.address();n(typeof o=="object"&&o?o.port:t)})})}function UH(e){return new Promise((t,n)=>{e.close(r=>{r?n(r):t()})})}W();ws();function rE(e,t){let n=e??t;if(n===void 0||n==="")return;let r=Number(n);if(!Number.isFinite(r)||!Number.isInteger(r)||r<=0)throw new Error(`Invalid timeout-ms: '${n}' \u2014 must be a positive integer (milliseconds).`);return r}function oE(e,t){let n=e??t;if(n===void 0||n==="")return;let r=Number(n);if(!Number.isFinite(r)||!Number.isInteger(r)||r<0)throw new Error(`Invalid sessionstart-cooldown-ms: '${n}' \u2014 must be a non-negative integer (milliseconds).`);return r}function sE(e,t){if(e!==void 0&&e!==""){if(e==="cron"||e==="sessionstart"||e==="both"||e==="pull")return e;throw new Error(`Invalid trigger: '${e}' \u2014 must be one of cron | sessionstart | both | pull.`)}return t!==void 0&&t!==""?"cron":"sessionstart"}var Fm="/forge-friction --auto",Nm="default";function Jo(e){if(e!==void 0&&e.trim()!=="")return e}function iE(e,t,n){return Jo(e)??Jo(t)??Jo(n)??Fm}function aE(e,t,n){return Jo(e)??Jo(t)??Jo(n)??Nm}function HH(e){let t;return n=>{let r=new AbortController,o=ar(r.signal),s=new X({...e.apiKey!==void 0?{apiKey:e.apiKey}:{},...e.baseUrl!==void 0?{baseUrl:e.baseUrl}:{},...e.cwd!==void 0?{cwd:e.cwd}:{}}),i=ro(e.openaiBaseUrl!==void 0?{openaiBaseUrl:e.openaiBaseUrl}:{}),a=oo(e.model,e.apiKey,i,e.baseUrl,void 0,void 0,e.cwd),l=new Wt({subagentManager:s,parentSession:o,defaultConfig:{...e.apiKey!==void 0?{apiKey:e.apiKey}:{},...e.baseUrl!==void 0?{baseUrl:e.baseUrl}:{}},defaultSubagentModel:wt(e.model),childProviderFactory:i,childSkillExecutorFactory:a,depth:0,...e.cwd!==void 0?{cwd:e.cwd}:{}}),c=new Ht({parentSession:o,defaultModel:e.model,defaultSubagentModel:wt(e.model),...e.apiKey!==void 0?{apiKey:e.apiKey}:{},childProviderFactory:i,childSkillExecutorFactory:a,...e.baseUrl!==void 0?{baseUrl:e.baseUrl}:{},...e.cwd!==void 0?{cwd:e.cwd}:{}}),u=new Nn({parentSession:o,defaultModel:e.model,defaultSubagentModel:wt(e.model),...e.apiKey!==void 0?{apiKey:e.apiKey}:{},...e.baseUrl!==void 0?{baseUrl:e.baseUrl}:{},systemPrompt:""});t??=new We;let d=ho(void 0,{subagentExecutor:l,skillExecutor:c,composeExecutor:u,memoryStore:t,model:String(e.model),...e.openaiBaseUrl!==void 0?{openaiBaseUrl:e.openaiBaseUrl}:{}})??new Fe({permissions:{allowedTools:[...Ft,...tn,...st,"agent","skill","compose"]},subagentExecutor:l,skillExecutor:c,composeExecutor:u,memoryStore:t,surface:"daemon"});return new Ve(sn({...n,provider:d}))}}function KH(e){let t=e.status==="success"?"\u2705":e.status==="skipped"?"\u23ED\uFE0F":"\u274C",n=(e.durationMs/1e3).toFixed(1),r=[`${t} daemon task: ${e.taskId} (${e.status})`,`trigger=${e.trigger} duration=${n}s`];return e.skipReason&&r.push(`skipReason=${e.skipReason}`),e.errorMessage&&r.push(`error: ${e.errorMessage.slice(0,400)}`),e.responseExcerpt&&r.push("",e.responseExcerpt.slice(0,600)),r.join(`
2259
- `)}function lE(e){e.command("daemon").description("Run agent-afk as a daemon that fires scheduled tasks (e.g. /forge-friction --auto)").option("-p, --port <number>","Control HTTP port","7777").option("-t, --task <command>",`Command to fire on each tick (default: ${Fm})`).option("-c, --cron <expression>",'Cron expression (e.g. "0 */6 * * *"). Required when --trigger includes cron.').option("-i, --task-id <id>",`Task identifier (default: ${Nm})`).option("--once","Fire one tick and exit (for testing)",!1).option("--timeout-ms <ms>","Per-tick session timeout in ms. Overrides AFK_TIMEOUT_MS. Defaults to the session default (120000).").option("--thinking <mode>","Thinking mode: 'adaptive' | 'disabled' | 'enabled:<N>'").option("--effort <level>","Effort level: low|medium|high|xhigh|max").option("--trigger <mode>","Trigger mode: cron | sessionstart | both | pull. Defaults to cron.").option("--sessionstart-cooldown-ms <ms>","Cooldown between Phase 6 sessionstart fires. Overrides AFK_SESSIONSTART_COOLDOWN_MS. Defaults to 6h.").option("--briefs-dir <path>","Override directory scanned for pending briefs (defaults to ~/.afk/agent-framework/briefs).").option("--dump-prompt [path]",'Dump resolved SDK prompt+options+provenance to file (default: ~/.afk/logs/prompt-dump-<ISO>.json) or "stderr"').action(async t=>{let n=parseInt(t.port,10);(Number.isNaN(n)||n<=0)&&K(new Error(`Invalid port: ${t.port}`));let r=nt(),o=iE(t.task,T.AFK_DAEMON_TASK,r.daemon?.task),s=aE(t.taskId,T.AFK_DAEMON_TASK_ID,r.daemon?.taskId),i,a,l;try{i=rE(t.timeoutMs,T.AFK_TIMEOUT_MS),a=oE(t.sessionstartCooldownMs,T.AFK_SESSIONSTART_COOLDOWN_MS),l=sE(t.trigger,t.cron)}catch(_){K(_)}(l==="cron"||l==="both")&&!t.cron&&K(new Error(`--cron is required when --trigger is '${l}'.`));let c,u;try{c=Ln(t.thinking)??po(),u=Fn(t.effort)??mo()}catch(_){K(_)}let d=r.daemon?.worktreePrune,m=T.AFK_WORKTREE_PRUNE_DISABLE==="1",f=d?.cron??"0 4 * * *",g={taskId:"worktree-prune",command:"__BUILTIN_WORKTREE_PRUNE__",trigger:"cron",cronExpression:f},h=l==="pull"?[]:[{taskId:s,command:o,trigger:l,...t.cron!==void 0?{cronExpression:t.cron}:{}}];!m&&d?.enabled!==!1&&h.push(g);let b=yt();for(let _ of b)_.enabled&&h.push(ah(_));if(t.dumpPrompt!==void 0&&t.dumpPrompt!==!1){let _=t.dumpPrompt===!0?jH.join(WH.homedir(),".afk","logs",`prompt-dump-${new Date().toISOString().replace(/[:.]/g,"-")}.json`):t.dumpPrompt;process.env.AFK_DUMP_PROMPT=_}let y=0,w=6e4,x=(_,M)=>{let P=Date.now();if(P-y<w)return;y=P;let $=M instanceof Error?`${M.name}: ${M.message}`:String(M);aa(`\u{1F6D1} agent-afk daemon ${_}
2260
- ${$.slice(0,500)}`).catch(F=>{console.error("[daemon] crash notification push failed:",F instanceof Error?F.message:String(F))})};process.on("uncaughtException",_=>{x("uncaughtException",_),process.exit(1)}),process.on("unhandledRejection",_=>{x("unhandledRejection",_),process.exit(1)});let R=T.AFK_DAEMON_CWD,v=Xe(),E=ue(),I=R!==void 0&&R.length>0?R:void 0,D=HH({model:v,...E!==void 0?{apiKey:E}:{},...r.baseUrl!==void 0?{baseUrl:r.baseUrl}:{},...r.openaiBaseUrl!==void 0?{openaiBaseUrl:r.openaiBaseUrl}:{},...I!==void 0?{cwd:I}:{}});try{let _=await nE({port:n,sessionConfig:{model:v,...E!==void 0?{apiKey:E}:{},...r.baseUrl!==void 0?{baseUrl:r.baseUrl}:{},...i!==void 0?{timeoutMs:i}:{},...c!==void 0?{thinking:c}:{},...u!==void 0?{effort:u}:{},...I!==void 0?{cwd:I}:{}},sessionFactory:D,...a!==void 0?{cooldownMs:a}:{},...t.briefsDir!==void 0?{briefsDir:t.briefsDir}:{},...l==="pull"?{pullPollIntervalMs:3e4,queueDir:Dt()}:{},tasks:h,onTaskComplete:P=>{aa(KH(P)).catch(()=>{})}});if(t.once){console.log(p.info(`\u25B6 Firing task '${s}' once...`));let P=await _.tickOnce(s);console.log(JSON.stringify(P,null,2)),await _.stop(),process.exit(P.status==="success"?0:1)}if(l==="sessionstart"||l==="both"){let P=await _.fireOnStart();for(let $ of P){let F=$.status==="success"?"\u2714":$.status==="skipped"?"\u23ED":"\u2717";console.log(p.info(`${F} sessionstart: ${JSON.stringify($)}`))}}console.log(p.success(`\u2714 Daemon listening on http://localhost:${_.port}`)),l==="pull"?(console.log(p.success("\u2714 Daemon in pull mode")),console.log(p.dim(` polling queue: ${Dt()} every 30s`))):console.log(p.dim(` task='${s}' command='${o}' trigger='${l}'${t.cron?` cron='${t.cron}'`:""}`)),h.length>1&&console.log(p.meta(` + built-in: worktree-prune (cron: ${f})`)),console.log(p.dim(" Press Ctrl+C to stop."));let M=async()=>{console.log(p.dim(`
2261
- \xB7 Shutting down daemon...`)),await _.stop(),process.exit(0)};process.on("SIGINT",M),process.on("SIGTERM",M)}catch(_){K(_)}})}import{mkdirSync as GH}from"node:fs";import{join as qH}from"node:path";W();function cE(e){e.command("queue").description("Manage the pull-trigger task queue (used with `afk daemon --trigger pull`)").command("add <command>").description("Enqueue a command for the pull-trigger daemon to execute").option("--notify-on <mode>","When to send a notification: failure | always | never").action((n,r)=>{try{let o=Dt();GH(o,{recursive:!0});let s=r.notifyOn,i=Jx(n,{notifyOn:s},o),a=String(i.sequence).padStart(4,"0"),l=qH(o,`${a}-${i.id}.json`);console.log(p.success(`\u2714 Queued task #${a} (id: ${i.id})`)),console.log(p.dim(` command: ${n}`)),console.log(p.dim(` file: ${l}`))}catch(o){K(o)}})}import Vo from"chalk";import pE from"chalk";W();import{existsSync as zH,readFileSync as JH,writeFileSync as VH,mkdirSync as YH}from"fs";import{dirname as XH}from"path";function wn(e,t,n,r=[]){YH(XH(e),{recursive:!0});let o="";zH(e)&&(o=JH(e,"utf-8"));for(let a of r){let l=new RegExp(`^${a}=.*$
2259
+ `)}function lE(e){e.command("daemon").description("Run agent-afk as a daemon that fires scheduled tasks (e.g. /forge-friction --auto)").option("-p, --port <number>","Control HTTP port","7777").option("-t, --task <command>",`Command to fire on each tick (default: ${Fm})`).option("-c, --cron <expression>",'Cron expression (e.g. "0 */6 * * *"). Required when --trigger includes cron.').option("-i, --task-id <id>",`Task identifier (default: ${Nm})`).option("--once","Fire one tick and exit (for testing)",!1).option("--timeout-ms <ms>","Per-tick session timeout in ms. Overrides AFK_TIMEOUT_MS. Defaults to the session default (120000).").option("--thinking <mode>","Thinking mode: 'adaptive' | 'disabled' | 'enabled:<N>'").option("--effort <level>","Effort level: low|medium|high|xhigh|max").option("--trigger <mode>","Trigger mode: cron | sessionstart | both | pull. Defaults to cron.").option("--sessionstart-cooldown-ms <ms>","Cooldown between Phase 6 sessionstart fires. Overrides AFK_SESSIONSTART_COOLDOWN_MS. Defaults to 6h.").option("--briefs-dir <path>","Override directory scanned for pending briefs (defaults to ~/.afk/agent-framework/briefs).").option("--dump-prompt [path]",'Dump resolved SDK prompt+options+provenance to file (default: ~/.afk/logs/prompt-dump-<ISO>.json) or "stderr"').action(async t=>{let n=parseInt(t.port,10);(Number.isNaN(n)||n<=0)&&K(new Error(`Invalid port: ${t.port}`));let r=nt(),o=iE(t.task,T.AFK_DAEMON_TASK,r.daemon?.task),s=aE(t.taskId,T.AFK_DAEMON_TASK_ID,r.daemon?.taskId),i,a,l;try{i=rE(t.timeoutMs,T.AFK_TIMEOUT_MS),a=oE(t.sessionstartCooldownMs,T.AFK_SESSIONSTART_COOLDOWN_MS),l=sE(t.trigger,t.cron)}catch(_){K(_)}(l==="cron"||l==="both")&&!t.cron&&K(new Error(`--cron is required when --trigger is '${l}'.`));let c,u;try{c=Ln(t.thinking)??po(),u=Fn(t.effort)??mo()}catch(_){K(_)}let d=r.daemon?.worktreePrune,p=T.AFK_WORKTREE_PRUNE_DISABLE==="1",f=d?.cron??"0 4 * * *",g={taskId:"worktree-prune",command:"__BUILTIN_WORKTREE_PRUNE__",trigger:"cron",cronExpression:f},h=l==="pull"?[]:[{taskId:s,command:o,trigger:l,...t.cron!==void 0?{cronExpression:t.cron}:{}}];!p&&d?.enabled!==!1&&h.push(g);let b=yt();for(let _ of b)_.enabled&&h.push(ah(_));if(t.dumpPrompt!==void 0&&t.dumpPrompt!==!1){let _=t.dumpPrompt===!0?jH.join(WH.homedir(),".afk","logs",`prompt-dump-${new Date().toISOString().replace(/[:.]/g,"-")}.json`):t.dumpPrompt;process.env.AFK_DUMP_PROMPT=_}let y=0,w=6e4,x=(_,M)=>{let P=Date.now();if(P-y<w)return;y=P;let $=M instanceof Error?`${M.name}: ${M.message}`:String(M);aa(`\u{1F6D1} agent-afk daemon ${_}
2260
+ ${$.slice(0,500)}`).catch(F=>{console.error("[daemon] crash notification push failed:",F instanceof Error?F.message:String(F))})};process.on("uncaughtException",_=>{x("uncaughtException",_),process.exit(1)}),process.on("unhandledRejection",_=>{x("unhandledRejection",_),process.exit(1)});let R=T.AFK_DAEMON_CWD,v=Xe(),E=ue(),I=R!==void 0&&R.length>0?R:void 0,D=HH({model:v,...E!==void 0?{apiKey:E}:{},...r.baseUrl!==void 0?{baseUrl:r.baseUrl}:{},...r.openaiBaseUrl!==void 0?{openaiBaseUrl:r.openaiBaseUrl}:{},...I!==void 0?{cwd:I}:{}});try{let _=await nE({port:n,sessionConfig:{model:v,...E!==void 0?{apiKey:E}:{},...r.baseUrl!==void 0?{baseUrl:r.baseUrl}:{},...i!==void 0?{timeoutMs:i}:{},...c!==void 0?{thinking:c}:{},...u!==void 0?{effort:u}:{},...I!==void 0?{cwd:I}:{}},sessionFactory:D,...a!==void 0?{cooldownMs:a}:{},...t.briefsDir!==void 0?{briefsDir:t.briefsDir}:{},...l==="pull"?{pullPollIntervalMs:3e4,queueDir:Dt()}:{},tasks:h,onTaskComplete:P=>{aa(KH(P)).catch(()=>{})}});if(t.once){console.log(m.info(`\u25B6 Firing task '${s}' once...`));let P=await _.tickOnce(s);console.log(JSON.stringify(P,null,2)),await _.stop(),process.exit(P.status==="success"?0:1)}if(l==="sessionstart"||l==="both"){let P=await _.fireOnStart();for(let $ of P){let F=$.status==="success"?"\u2714":$.status==="skipped"?"\u23ED":"\u2717";console.log(m.info(`${F} sessionstart: ${JSON.stringify($)}`))}}console.log(m.success(`\u2714 Daemon listening on http://localhost:${_.port}`)),l==="pull"?(console.log(m.success("\u2714 Daemon in pull mode")),console.log(m.dim(` polling queue: ${Dt()} every 30s`))):console.log(m.dim(` task='${s}' command='${o}' trigger='${l}'${t.cron?` cron='${t.cron}'`:""}`)),h.length>1&&console.log(m.meta(` + built-in: worktree-prune (cron: ${f})`)),console.log(m.dim(" Press Ctrl+C to stop."));let M=async()=>{console.log(m.dim(`
2261
+ \xB7 Shutting down daemon...`)),await _.stop(),process.exit(0)};process.on("SIGINT",M),process.on("SIGTERM",M)}catch(_){K(_)}})}import{mkdirSync as GH}from"node:fs";import{join as qH}from"node:path";W();function cE(e){e.command("queue").description("Manage the pull-trigger task queue (used with `afk daemon --trigger pull`)").command("add <command>").description("Enqueue a command for the pull-trigger daemon to execute").option("--notify-on <mode>","When to send a notification: failure | always | never").action((n,r)=>{try{let o=Dt();GH(o,{recursive:!0});let s=r.notifyOn,i=Jx(n,{notifyOn:s},o),a=String(i.sequence).padStart(4,"0"),l=qH(o,`${a}-${i.id}.json`);console.log(m.success(`\u2714 Queued task #${a} (id: ${i.id})`)),console.log(m.dim(` command: ${n}`)),console.log(m.dim(` file: ${l}`))}catch(o){K(o)}})}import Vo from"chalk";import pE from"chalk";W();import{existsSync as zH,readFileSync as JH,writeFileSync as VH,mkdirSync as YH}from"fs";import{dirname as XH}from"path";function wn(e,t,n,r=[]){YH(XH(e),{recursive:!0});let o="";zH(e)&&(o=JH(e,"utf-8"));for(let a of r){let l=new RegExp(`^${a}=.*$
2262
2262
  ?`,"m");o=o.replace(l,"")}let s=`${t}=${n}`,i=new RegExp(`^${t}=.*$`,"m");i.test(o)?o=o.replace(i,s):(o&&!o.endsWith(`
2263
2263
  `)&&(o+=`
2264
2264
  `),o+=s+`
2265
2265
  `),VH(e,o,{mode:384})}import uE from"chalk";function dE(e){return process.stdin.isTTY||(console.error(uE.red(`Cannot securely prompt for secret on a non-TTY stdin: "${e.trim()}"`)),console.error(uE.gray(" Supply the token via environment variable or ~/.afk/config/afk.env instead.")),process.exit(1)),new Promise(t=>{process.stdout.write(e);let n=[];process.stdin.setRawMode(!0),process.stdin.resume(),process.stdin.setEncoding("utf-8");let r=o=>{o==="\r"||o===`
2266
2266
  `||o===""?(process.stdin.setRawMode(!1),process.stdin.pause(),process.stdin.removeListener("data",r),process.stdout.write(`
2267
2267
  `),t(n.join("").trim())):o===""?(process.stdin.setRawMode(!1),process.stdin.pause(),process.stdout.write(`
2268
- `),process.exit(1)):o==="\x7F"?n.pop():n.push(o)};process.stdin.on("data",r)})}function mE(){return dE("Anthropic API key or OAuth token: ")}async function Ic(e){let t=e??await mE();t||(console.error(pE.red("No token provided. Nothing saved.")),process.exit(1));let n=ot(),r,o;t.startsWith("sk-ant-oat")?(r="CLAUDE_CODE_OAUTH_TOKEN",o=["ANTHROPIC_API_KEY"]):(r="ANTHROPIC_API_KEY",o=["CLAUDE_CODE_OAUTH_TOKEN"]),wn(n,r,t,o),console.log(pE.green(`\u2713 Saved ${r} to ${n}`)),console.log(p.meta("Restart any running afk daemon to pick up the new token."))}function fE(e){e.command("login [token]").description("Save an Anthropic API key or OAuth token for afk to use").action(async t=>{let n=xe(Xe());if(n==="openai-compatible"||n==="openai-codex"){console.log(Vo.yellow("`afk login` is Anthropic-only.")),console.log(""),console.log("For OpenAI-backed models (gpt-*, o1*, o3*, o4*, codex-*), authenticate with one of:"),console.log(Vo.cyan(" export OPENAI_API_KEY=sk-proj-...")),console.log(Vo.cyan(" # or: export CODEX_API_KEY=...")),console.log(Vo.cyan(" codex login --api-key sk-proj-...")),console.log(""),console.log(Vo.gray("Run `afk provider auth diagnose` to see which auth source AFK will use.")),console.log(Vo.gray("To save an Anthropic key for Claude models instead, re-run with AFK_MODEL=sonnet (or similar) first."));return}await Ic(t)})}import de from"chalk";import Pc from"ora";W();import{existsSync as ZH}from"fs";import{join as QH}from"path";async function Bm(e,t={},n={}){let r=n.pluginsDir??Oe(),o=n.indexPath??ie(),s=n.now??(()=>new Date),i=n.gitRunner?{runner:n.gitRunner}:{},l=fe(o).plugins[e];if(!l)throw new Error(`plugin "${e}" is not installed`);let c=QH(r,e);if(!ZH(c))return{name:e,status:"missing-dir",dir:c};if(ln(),l.sourceType==="local")return{name:e,status:"skipped-local"};await ec(c,i);let u;if(t.ref)u=t.ref;else{let g=await Kn(c,i);u=qn(g)??l.ref??await Vt(c,i)}if(u===l.ref){let g=await Jt(c,i);return{name:e,status:"up-to-date",ref:u,commit:g}}await Gn(c,u,i);let d=await Jt(c,i),m=s().toISOString(),f={...l,ref:u,commit:d,updatedAt:m};return $n(e,f,o),{name:e,status:"updated",fromRef:l.ref,toRef:u,commit:d}}async function gE(e={}){let t=e.indexPath??ie(),n=fe(t),r=[];for(let o of Object.keys(n.plugins))try{r.push(await Bm(o,{},e))}catch(s){let i=s instanceof Error?s.message:String(s);r.push({name:o,status:"missing-dir",dir:i})}return r}W();import{existsSync as eK,lstatSync as tK,rmSync as nK,unlinkSync as rK}from"fs";import{join as oK}from"path";function hE(e,t={}){hn(e);let n=t.pluginsDir??Oe(),r=t.indexPath??ie(),o=oK(n,e),s=!1;sK(o)?(rK(o),s=!0):eK(o)&&(nK(o,{recursive:!0,force:!0}),s=!0);let i=fe(r),a=Object.prototype.hasOwnProperty.call(i.plugins,e);return a&&Gy(e,r),ln(),{name:e,removedDir:s,removedIndexEntry:a}}function sK(e){try{return tK(e).isSymbolicLink()}catch{return!1}}W();function yE(e,t={}){let n=t.logger??console,r=t.pluginsDir??Oe(),o=t.indexPath??ie(),s={...t,pluginsDir:r,indexPath:o},i=e.command("plugin").description("Manage AFK plugins (install / update / list / remove / enable / disable)");i.command("install <source> [name]").description("Install a plugin from a git URL, owner/repo shorthand, local path, or <marketplace>:<plugin>").option("-r, --ref <ref>","Install a specific tag, branch, or SHA").option("-f, --force","Replace an existing plugin with the same name").option("-y, --yes","Skip the install warning and countdown (non-interactive / CI)").action(async(a,l,c)=>{let u;try{u=Ko(a)}catch(f){Pc(`Installing ${a}\u2026`).start().fail("Failed"),K(f)}let d=process.stderr.isTTY===!0&&!c.yes;if(u.type==="marketplace-ref"){let f=Pc(`Installing ${u.marketplace}:${u.plugin}\u2026`).start();try{let g=await qo(u.marketplace,u.plugin,{...c.ref?{ref:c.ref}:{},...c.force?{force:!0}:{}},{...s,confirm:d});f.succeed(de.green(`Installed ${de.bold(g.key)}`)+de.gray(` at ${g.dir}`))}catch(g){f.fail("Failed"),K(g)}return}let m=Pc(`Installing ${a}\u2026`).start();try{let f={...l?{name:l}:{},...c.ref?{ref:c.ref}:{},...c.force?{force:!0}:{}},g=await rc(a,f,{...s,confirm:d});m.succeed(de.green(`Installed ${de.bold(g.name)}`)+de.gray(` at ${g.dir}${g.entry.ref?` (ref: ${g.entry.ref})`:""}`))}catch(f){m.fail("Failed"),K(f)}}),i.command("update [name]").description("Update one plugin, or all if no name is given").option("-r, --ref <ref>","Pin to a specific ref instead of the latest tag").action(async(a,l)=>{try{if(a){let c=Pc(`Updating ${a}\u2026`).start(),u=await Bm(a,l.ref?{ref:l.ref}:{},s);aK(u,c)}else{n.log(de.cyan("Updating all plugins\u2026"));let c=await gE(s);if(c.length===0){n.log(de.gray(" (nothing installed)"));return}for(let u of c)n.log(" "+bE(u))}}catch(c){K(c)}}),i.command("list").description("List installed plugins with their source, version, and enabled state").option("-f, --format <format>","Output format (text|json)","text").action(a=>{let l=fe(o);if(a.format==="json"){let c=Object.entries(l.plugins).map(([u,d])=>({name:u,enabled:d.enabled,...d.ref?{ref:d.ref}:{},source:d.source}));n.log(JSON.stringify({plugins:c},null,2))}else iK(l,n)}),i.command("remove <name>").description("Remove a plugin (directory + index entry)").action(a=>{let l=hE(a,{pluginsDir:r,indexPath:o});if(!l.removedDir&&!l.removedIndexEntry){n.log(de.gray(`No plugin named "${a}" to remove.`));return}let c=[l.removedDir?"directory":null,l.removedIndexEntry?"index entry":null].filter(Boolean);n.log(de.green(`Removed ${a}: ${c.join(" + ")}`))}),i.command("enable <name>").description("Re-enable a previously disabled plugin").action(a=>{try{wd(a,!0,o),n.log(de.green(`Enabled ${a}`))}catch(l){K(l)}}),i.command("disable <name>").description("Keep the plugin on disk but skip it from SDK init").action(a=>{try{wd(a,!1,o),n.log(de.yellow(`Disabled ${a} (dir preserved at ${r}/${a})`))}catch(l){K(l)}})}function iK(e,t){let n=Object.keys(e.plugins).sort();if(n.length===0){t.log(de.gray("No plugins installed.")),t.log(de.gray(" Try: afk plugin install anthropics/claude-plugins-official"));return}t.log(de.cyan.bold(`
2269
- Installed plugins:`));for(let r of n){let o=e.plugins[r];if(!o)continue;let s=o.enabled?de.green("enabled "):de.yellow("disabled"),i=o.ref?de.blue(o.ref):de.gray("(local)"),a=de.gray(o.source);t.log(` ${de.bold(r.padEnd(30))} ${s} ${i.padEnd(12)} ${a}`)}t.log("")}function bE(e){switch(e.status){case"updated":return`${de.green("\u2713")} ${de.bold(e.name)}: ${e.fromRef??"(none)"} \u2192 ${e.toRef}`;case"up-to-date":return`${de.gray("\xB7")} ${de.bold(e.name)}: up-to-date (${e.ref})`;case"skipped-local":return`${de.gray("\xB7")} ${de.bold(e.name)}: skipped (local source)`;case"missing-dir":return`${de.yellow("!")} ${de.bold(e.name)}: plugin dir missing (${e.dir})`}}function aK(e,t){let n=bE(e);e.status==="updated"?t.succeed(n):e.status==="up-to-date"||e.status==="skipped-local"?t.info(n):t.warn(n)}import oe from"chalk";import Um from"ora";W();function wE(e,t={}){let n=t.logger??console,r=t.cacheDir??Yt(),o=t.indexPath??ie(),s={...t,cacheDir:r,indexPath:o},i=e.command("marketplace").description("Manage AFK plugin marketplaces (install / list / plugins / install-plugin / remove / update)");i.command("install <source> [name]").description("Clone or symlink a marketplace into the local plugin cache").option("-r, --ref <ref>","Install a specific tag, branch, or SHA").option("-f, --force","Replace an existing marketplace with the same name").action(async(a,l,c)=>{let u=Um(`Installing marketplace ${a}\u2026`).start();try{let d={...l?{name:l}:{},...c.ref?{ref:c.ref}:{},...c.force?{force:!0}:{}},m=await sc(a,d,s),f=m.entry.ref?` (ref: ${m.entry.ref})`:"";u.succeed(oe.green(`Installed marketplace ${oe.bold(m.name)}`)+oe.gray(`${f} at ${m.dir}`)),n.log(oe.gray(` ${m.plugins.length} plugin(s) available \u2014 run \`afk marketplace plugins ${m.name}\` to list.`))}catch(d){u.fail("Failed"),K(d)}}),i.command("list").description("List installed marketplaces with their source and ref").option("-f, --format <format>","Output format (text|json)","text").action(a=>{let l=fe(o),c=Object.entries(l.marketplaces);if(a.format==="json"){n.log(JSON.stringify({marketplaces:c.map(([u,d])=>({name:u,source:d.source,sourceType:d.sourceType,...d.ref?{ref:d.ref}:{}}))},null,2));return}if(c.length===0){n.log(oe.gray("No marketplaces installed.")),n.log(oe.gray(" Try: afk marketplace install griffinwork40/awa-private"));return}n.log(oe.cyan.bold(`
2270
- Installed marketplaces:`));for(let[u,d]of c.sort()){let m=d.ref?oe.blue(d.ref):oe.gray("(local)"),f=oe.gray(d.source);n.log(` ${oe.bold(u.padEnd(30))} ${m.padEnd(12)} ${f}`)}n.log("")}),i.command("plugins <name>").description("List plugins inside a marketplace, with [installed] / [available] markers").option("-f, --format <format>","Output format (text|json)","text").action((a,l)=>{try{let c=lc(a,s);if(l.format==="json"){n.log(JSON.stringify({marketplace:a,plugins:c},null,2));return}if(c.length===0){n.log(oe.gray(`Marketplace "${a}" lists no plugins.`));return}n.log(oe.cyan.bold(`
2271
- Plugins in ${a}:`)),c.forEach((u,d)=>{let m=u.installed?oe.green("[\u2713]"):oe.gray("[ ]"),f=u.description?oe.gray(` \u2014 ${u.description}`):"";n.log(` ${m} ${oe.bold((d+1).toString().padStart(2))}. ${oe.bold(u.name)}${f}`)}),n.log(oe.gray(`
2272
- Install one: afk plugin install ${a}:<plugin>`))}catch(c){K(c)}}),i.command("install-plugin <marketplace> <plugin>").description("Install a single plugin from a marketplace").option("-r, --ref <ref>","For git-sourced plugins, pin to a specific tag/branch/SHA").option("-f, --force","Replace an existing plugin with the same key").option("-y, --yes","Skip the install warning and countdown (non-interactive / CI)").action(async(a,l,c)=>{let u=process.stderr.isTTY===!0&&!c.yes,d=Um(`Installing ${a}:${l}\u2026`).start();try{let m=await qo(a,l,{...c.ref?{ref:c.ref}:{},...c.force?{force:!0}:{}},{...s,confirm:u});d.succeed(oe.green(`Installed ${oe.bold(m.key)}`)+oe.gray(` at ${m.dir}`))}catch(m){d.fail("Failed"),K(m)}}),i.command("remove <name>").description("Remove a marketplace and cascade-delete its installed plugins").action(a=>{let l=ic(a,{cacheDir:r,indexPath:o});if(!l.removedDir&&!l.removedIndexEntry&&l.removedPluginEntries.length===0){n.log(oe.gray(`No marketplace named "${a}" to remove.`));return}let c=[l.removedDir?"directory":null,l.removedIndexEntry?"index entry":null,l.removedPluginEntries.length>0?`${l.removedPluginEntries.length} plugin entry`:null].filter(Boolean);if(n.log(oe.green(`Removed ${a}: ${c.join(" + ")}`)),l.removedPluginEntries.length>0)for(let u of l.removedPluginEntries)n.log(oe.gray(` - ${u}`))}),i.command("update [name]").description("Update one marketplace, or all if no name is given").option("-r, --ref <ref>","Pin to a specific ref instead of the latest tag").action(async(a,l)=>{try{if(a){let c=Um(`Updating ${a}\u2026`).start(),u=await si(a,l.ref?{ref:l.ref}:{},s);lK(u,c)}else{n.log(oe.cyan("Updating all marketplaces\u2026"));let c=await OT(s);if(c.length===0){n.log(oe.gray(" (no marketplaces installed)"));return}for(let u of c)n.log(" "+SE(u))}}catch(c){K(c)}})}function SE(e){switch(e.status){case"updated":{let t=e.addedPlugins.length>0?` +${e.addedPlugins.join(", ")}`:"",n=e.removedPlugins.length>0?` -${e.removedPlugins.join(", ")}`:"";return`${oe.green("\u2713")} ${oe.bold(e.name)}: ${e.fromRef??"(none)"} \u2192 ${e.toRef}${oe.gray(t+n)}`}case"up-to-date":return`${oe.gray("\xB7")} ${oe.bold(e.name)}: up-to-date (${e.ref})`;case"skipped-local":return`${oe.gray("\xB7")} ${oe.bold(e.name)}: skipped (local source)`;case"missing-dir":return`${oe.yellow("!")} ${oe.bold(e.name)}: marketplace dir missing (${e.dir})`}}function lK(e,t){let n=SE(e);e.status==="updated"?t.succeed(n):e.status==="up-to-date"||e.status==="skipped-local"?t.info(n):t.warn(n)}G();import{access as cK,constants as uK,mkdir as dK,readFile as pK}from"fs/promises";import{execSync as mK}from"child_process";W();async function fK(){return ue()?{name:"Anthropic API Key",state:"pass",detail:"ANTHROPIC_API_KEY set"}:{name:"Anthropic API Key",state:"fail",fix:"Set ANTHROPIC_API_KEY or run `afk login`"}}async function gK(){return $s()?{name:"Codex/OpenAI API Key",state:"pass",detail:"OPENAI_API_KEY or CODEX_API_KEY set"}:{name:"Codex/OpenAI API Key",state:"warn",fix:"Set OPENAI_API_KEY or CODEX_API_KEY to use Codex models"}}async function hK(){try{let t=`${mK("npm config get prefix",{timeout:2e3,encoding:"utf-8",stdio:["ignore","pipe","ignore"]}).trim().replace(/\/$/,"")}/bin`;return(T.PATH??"").split(":").map(r=>r.replace(/\/$/,"")).includes(t)?{name:"npm bin on PATH",state:"pass",detail:t}:{name:"npm bin on PATH",state:"fail",detail:t,fix:`Add ${t} to PATH: echo 'export PATH="${t}:$PATH"' >> ~/.zshrc`}}catch{return{name:"npm bin on PATH",state:"warn",detail:"could not query npm prefix"}}}async function jm(e,t){let n=t();try{return await cK(n,uK.W_OK),{name:e,state:"pass",detail:n}}catch{try{return await dK(n,{recursive:!0}),{name:e,state:"pass",detail:`${n} (created)`}}catch{return{name:e,state:"fail",detail:n,fix:`Unable to create or write to ${n}`}}}}async function yK(){let e=Fi();try{let t=await pK(e,"utf-8");return JSON.parse(t),{name:"Config File",state:"pass",detail:`${e} (valid JSON)`}}catch(t){return t.code==="ENOENT"?{name:"Config File",state:"pass",detail:"no config file (using defaults)"}:{name:"Config File",state:"fail",detail:e,fix:`Unable to parse config file: ${t instanceof Error?t.message:"unknown error"}`}}}async function bK(){let e=Pf();return e.length===0?null:{name:"Required env vars",state:"fail",detail:`Missing: ${e.map(t=>t.name).join(", ")}`,fix:"Set these env vars before running. See docs/env-registry.md for descriptions."}}async function wK(){let e=T.AFK_TELEGRAM_BOT_TOKEN;if(!e)return null;try{let t=new AbortController,n=setTimeout(()=>t.abort(),5e3),r=await fetch(`https://api.telegram.org/bot${e}/getMe`,{signal:t.signal});if(clearTimeout(n),r.ok){let s=(await r.json()).result?.username;return{name:"Telegram Bot",state:"pass",detail:s?`@${s}`:"connected"}}return{name:"Telegram Bot",state:"fail",fix:`Telegram API returned ${r.status}. Check AFK_TELEGRAM_BOT_TOKEN.`}}catch(t){return t.name==="AbortError"?{name:"Telegram Bot",state:"warn",detail:"connection timeout"}:{name:"Telegram Bot",state:"warn",detail:`network error: ${t instanceof Error?t.message:"unknown"}`}}}function kE(e){e.command("doctor").description("Check system health and configuration").option("-f, --format <format>","Output format (text|json)","text").action(async t=>{let n=[];n.push(await fK()),n.push(await gK()),n.push(await hK()),n.push(await jm("Config Directory",Xt)),n.push(await jm("State Directory",ye)),n.push(await jm("Logs Directory",kn)),n.push(await yK());let r=await bK();r!==null&&n.push(r);let o=await wK();o!==null&&n.push(o);let s={passed:n.filter(i=>i.state==="pass").length,warned:n.filter(i=>i.state==="warn").length,failed:n.filter(i=>i.state==="fail").length};t.format==="json"?console.log(JSON.stringify({checks:n,summary:s},null,2)):(n.forEach(i=>{let a;i.state==="pass"?a=p.success("\u2713"):i.state==="warn"?a=p.warning("\u26A0"):a=p.error("\u2717");let l=`${a} ${i.name}`;i.detail&&(l+=` \u2014 ${i.detail}`),console.log(l),i.state==="fail"&&i.fix&&console.log(` Fix: ${i.fix}`)}),console.log(`
2273
- Summary: ${s.passed} passed, ${s.warned} warned, ${s.failed} failed`)),process.exit(s.failed>0?1:0)})}function SK(e){let t=Rs(e),n=As(t),r=t.apiKey===null?1:0,o={source:t.source,message:n,exitCode:r};return t.last4!==void 0&&(o.last4=t.last4),o}function vE(e){e.command("provider").description("Provider diagnostics and configuration").command("auth").description("Inspect provider auth state").command("diagnose").description("Report which OpenAI auth source would be used by the openai-compatible provider. Never prints raw tokens.").option("-f, --format <format>","Output format (text|json)","text").action(r=>{let o=SK(void 0);if(r.format==="json"){let s={source:o.source,message:o.message,ok:o.exitCode===0};o.last4!==void 0&&(s.last4=o.last4),console.log(JSON.stringify(s,null,2))}else{let s=o.exitCode===0?p.success("\u2713"):p.warning("\u26A0");console.log(`${s} ${o.message}`)}process.exit(o.exitCode)})}var kK=["chat","interactive","status","config","daemon","login","plugin","doctor","completion"],vK=["install","update","list","remove","enable","disable"],Wm=["sonnet","opus","haiku"],Hm=["json","text"],Km=["cron","sessionstart","both"];function Mt(e){return e.join(" ")}function TK(){let e=Mt(Wm),t=Mt(Hm),n=Mt(Km);return`#compdef afk
2268
+ `),process.exit(1)):o==="\x7F"?n.pop():n.push(o)};process.stdin.on("data",r)})}function mE(){return dE("Anthropic API key or OAuth token: ")}async function Ic(e){let t=e??await mE();t||(console.error(pE.red("No token provided. Nothing saved.")),process.exit(1));let n=ot(),r,o;t.startsWith("sk-ant-oat")?(r="CLAUDE_CODE_OAUTH_TOKEN",o=["ANTHROPIC_API_KEY"]):(r="ANTHROPIC_API_KEY",o=["CLAUDE_CODE_OAUTH_TOKEN"]),wn(n,r,t,o),console.log(pE.green(`\u2713 Saved ${r} to ${n}`)),console.log(m.meta("Restart any running afk daemon to pick up the new token."))}function fE(e){e.command("login [token]").description("Save an Anthropic API key or OAuth token for afk to use").action(async t=>{let n=xe(Xe());if(n==="openai-compatible"||n==="openai-codex"){console.log(Vo.yellow("`afk login` is Anthropic-only.")),console.log(""),console.log("For OpenAI-backed models (gpt-*, o1*, o3*, o4*, codex-*), authenticate with one of:"),console.log(Vo.cyan(" export OPENAI_API_KEY=sk-proj-...")),console.log(Vo.cyan(" # or: export CODEX_API_KEY=...")),console.log(Vo.cyan(" codex login --api-key sk-proj-...")),console.log(""),console.log(Vo.gray("Run `afk provider auth diagnose` to see which auth source AFK will use.")),console.log(Vo.gray("To save an Anthropic key for Claude models instead, re-run with AFK_MODEL=sonnet (or similar) first."));return}await Ic(t)})}import de from"chalk";import Pc from"ora";W();import{existsSync as ZH}from"fs";import{join as QH}from"path";async function Bm(e,t={},n={}){let r=n.pluginsDir??Oe(),o=n.indexPath??ie(),s=n.now??(()=>new Date),i=n.gitRunner?{runner:n.gitRunner}:{},l=fe(o).plugins[e];if(!l)throw new Error(`plugin "${e}" is not installed`);let c=QH(r,e);if(!ZH(c))return{name:e,status:"missing-dir",dir:c};if(ln(),l.sourceType==="local")return{name:e,status:"skipped-local"};await ec(c,i);let u;if(t.ref)u=t.ref;else{let g=await Kn(c,i);u=qn(g)??l.ref??await Vt(c,i)}if(u===l.ref){let g=await Jt(c,i);return{name:e,status:"up-to-date",ref:u,commit:g}}await Gn(c,u,i);let d=await Jt(c,i),p=s().toISOString(),f={...l,ref:u,commit:d,updatedAt:p};return $n(e,f,o),{name:e,status:"updated",fromRef:l.ref,toRef:u,commit:d}}async function gE(e={}){let t=e.indexPath??ie(),n=fe(t),r=[];for(let o of Object.keys(n.plugins))try{r.push(await Bm(o,{},e))}catch(s){let i=s instanceof Error?s.message:String(s);r.push({name:o,status:"missing-dir",dir:i})}return r}W();import{existsSync as eK,lstatSync as tK,rmSync as nK,unlinkSync as rK}from"fs";import{join as oK}from"path";function hE(e,t={}){hn(e);let n=t.pluginsDir??Oe(),r=t.indexPath??ie(),o=oK(n,e),s=!1;sK(o)?(rK(o),s=!0):eK(o)&&(nK(o,{recursive:!0,force:!0}),s=!0);let i=fe(r),a=Object.prototype.hasOwnProperty.call(i.plugins,e);return a&&Gy(e,r),ln(),{name:e,removedDir:s,removedIndexEntry:a}}function sK(e){try{return tK(e).isSymbolicLink()}catch{return!1}}W();function yE(e,t={}){let n=t.logger??console,r=t.pluginsDir??Oe(),o=t.indexPath??ie(),s={...t,pluginsDir:r,indexPath:o},i=e.command("plugin").description("Manage AFK plugins (install / update / list / remove / enable / disable)");i.command("install <source> [name]").description("Install a plugin from a git URL, owner/repo shorthand, local path, or <marketplace>:<plugin>").option("-r, --ref <ref>","Install a specific tag, branch, or SHA").option("-f, --force","Replace an existing plugin with the same name").option("-y, --yes","Skip the install warning and countdown (non-interactive / CI)").action(async(a,l,c)=>{let u;try{u=Ko(a)}catch(f){Pc(`Installing ${a}\u2026`).start().fail("Failed"),K(f)}let d=process.stderr.isTTY===!0&&!c.yes;if(u.type==="marketplace-ref"){let f=Pc(`Installing ${u.marketplace}:${u.plugin}\u2026`).start();try{let g=await qo(u.marketplace,u.plugin,{...c.ref?{ref:c.ref}:{},...c.force?{force:!0}:{}},{...s,confirm:d});f.succeed(de.green(`Installed ${de.bold(g.key)}`)+de.gray(` at ${g.dir}`))}catch(g){f.fail("Failed"),K(g)}return}let p=Pc(`Installing ${a}\u2026`).start();try{let f={...l?{name:l}:{},...c.ref?{ref:c.ref}:{},...c.force?{force:!0}:{}},g=await rc(a,f,{...s,confirm:d});p.succeed(de.green(`Installed ${de.bold(g.name)}`)+de.gray(` at ${g.dir}${g.entry.ref?` (ref: ${g.entry.ref})`:""}`))}catch(f){p.fail("Failed"),K(f)}}),i.command("update [name]").description("Update one plugin, or all if no name is given").option("-r, --ref <ref>","Pin to a specific ref instead of the latest tag").action(async(a,l)=>{try{if(a){let c=Pc(`Updating ${a}\u2026`).start(),u=await Bm(a,l.ref?{ref:l.ref}:{},s);aK(u,c)}else{n.log(de.cyan("Updating all plugins\u2026"));let c=await gE(s);if(c.length===0){n.log(de.gray(" (nothing installed)"));return}for(let u of c)n.log(" "+bE(u))}}catch(c){K(c)}}),i.command("list").description("List installed plugins with their source, version, and enabled state").option("-f, --format <format>","Output format (text|json)","text").action(a=>{let l=fe(o);if(a.format==="json"){let c=Object.entries(l.plugins).map(([u,d])=>({name:u,enabled:d.enabled,...d.ref?{ref:d.ref}:{},source:d.source}));n.log(JSON.stringify({plugins:c},null,2))}else iK(l,n)}),i.command("remove <name>").description("Remove a plugin (directory + index entry)").action(a=>{let l=hE(a,{pluginsDir:r,indexPath:o});if(!l.removedDir&&!l.removedIndexEntry){n.log(de.gray(`No plugin named "${a}" to remove.`));return}let c=[l.removedDir?"directory":null,l.removedIndexEntry?"index entry":null].filter(Boolean);n.log(de.green(`Removed ${a}: ${c.join(" + ")}`))}),i.command("enable <name>").description("Re-enable a previously disabled plugin").action(a=>{try{wd(a,!0,o),n.log(de.green(`Enabled ${a}`))}catch(l){K(l)}}),i.command("disable <name>").description("Keep the plugin on disk but skip it from SDK init").action(a=>{try{wd(a,!1,o),n.log(de.yellow(`Disabled ${a} (dir preserved at ${r}/${a})`))}catch(l){K(l)}})}function iK(e,t){let n=Object.keys(e.plugins).sort();if(n.length===0){t.log(de.gray("No plugins installed.")),t.log(de.gray(" Try: afk plugin install anthropics/claude-plugins-official"));return}t.log(de.cyan.bold(`
2269
+ Installed plugins:`));for(let r of n){let o=e.plugins[r];if(!o)continue;let s=o.enabled?de.green("enabled "):de.yellow("disabled"),i=o.ref?de.blue(o.ref):de.gray("(local)"),a=de.gray(o.source);t.log(` ${de.bold(r.padEnd(30))} ${s} ${i.padEnd(12)} ${a}`)}t.log("")}function bE(e){switch(e.status){case"updated":return`${de.green("\u2713")} ${de.bold(e.name)}: ${e.fromRef??"(none)"} \u2192 ${e.toRef}`;case"up-to-date":return`${de.gray("\xB7")} ${de.bold(e.name)}: up-to-date (${e.ref})`;case"skipped-local":return`${de.gray("\xB7")} ${de.bold(e.name)}: skipped (local source)`;case"missing-dir":return`${de.yellow("!")} ${de.bold(e.name)}: plugin dir missing (${e.dir})`}}function aK(e,t){let n=bE(e);e.status==="updated"?t.succeed(n):e.status==="up-to-date"||e.status==="skipped-local"?t.info(n):t.warn(n)}import oe from"chalk";import Um from"ora";W();function wE(e,t={}){let n=t.logger??console,r=t.cacheDir??Yt(),o=t.indexPath??ie(),s={...t,cacheDir:r,indexPath:o},i=e.command("marketplace").description("Manage AFK plugin marketplaces (install / list / plugins / install-plugin / remove / update)");i.command("install <source> [name]").description("Clone or symlink a marketplace into the local plugin cache").option("-r, --ref <ref>","Install a specific tag, branch, or SHA").option("-f, --force","Replace an existing marketplace with the same name").action(async(a,l,c)=>{let u=Um(`Installing marketplace ${a}\u2026`).start();try{let d={...l?{name:l}:{},...c.ref?{ref:c.ref}:{},...c.force?{force:!0}:{}},p=await sc(a,d,s),f=p.entry.ref?` (ref: ${p.entry.ref})`:"";u.succeed(oe.green(`Installed marketplace ${oe.bold(p.name)}`)+oe.gray(`${f} at ${p.dir}`)),n.log(oe.gray(` ${p.plugins.length} plugin(s) available \u2014 run \`afk marketplace plugins ${p.name}\` to list.`))}catch(d){u.fail("Failed"),K(d)}}),i.command("list").description("List installed marketplaces with their source and ref").option("-f, --format <format>","Output format (text|json)","text").action(a=>{let l=fe(o),c=Object.entries(l.marketplaces);if(a.format==="json"){n.log(JSON.stringify({marketplaces:c.map(([u,d])=>({name:u,source:d.source,sourceType:d.sourceType,...d.ref?{ref:d.ref}:{}}))},null,2));return}if(c.length===0){n.log(oe.gray("No marketplaces installed.")),n.log(oe.gray(" Try: afk marketplace install griffinwork40/awa-private"));return}n.log(oe.cyan.bold(`
2270
+ Installed marketplaces:`));for(let[u,d]of c.sort()){let p=d.ref?oe.blue(d.ref):oe.gray("(local)"),f=oe.gray(d.source);n.log(` ${oe.bold(u.padEnd(30))} ${p.padEnd(12)} ${f}`)}n.log("")}),i.command("plugins <name>").description("List plugins inside a marketplace, with [installed] / [available] markers").option("-f, --format <format>","Output format (text|json)","text").action((a,l)=>{try{let c=lc(a,s);if(l.format==="json"){n.log(JSON.stringify({marketplace:a,plugins:c},null,2));return}if(c.length===0){n.log(oe.gray(`Marketplace "${a}" lists no plugins.`));return}n.log(oe.cyan.bold(`
2271
+ Plugins in ${a}:`)),c.forEach((u,d)=>{let p=u.installed?oe.green("[\u2713]"):oe.gray("[ ]"),f=u.description?oe.gray(` \u2014 ${u.description}`):"";n.log(` ${p} ${oe.bold((d+1).toString().padStart(2))}. ${oe.bold(u.name)}${f}`)}),n.log(oe.gray(`
2272
+ Install one: afk plugin install ${a}:<plugin>`))}catch(c){K(c)}}),i.command("install-plugin <marketplace> <plugin>").description("Install a single plugin from a marketplace").option("-r, --ref <ref>","For git-sourced plugins, pin to a specific tag/branch/SHA").option("-f, --force","Replace an existing plugin with the same key").option("-y, --yes","Skip the install warning and countdown (non-interactive / CI)").action(async(a,l,c)=>{let u=process.stderr.isTTY===!0&&!c.yes,d=Um(`Installing ${a}:${l}\u2026`).start();try{let p=await qo(a,l,{...c.ref?{ref:c.ref}:{},...c.force?{force:!0}:{}},{...s,confirm:u});d.succeed(oe.green(`Installed ${oe.bold(p.key)}`)+oe.gray(` at ${p.dir}`))}catch(p){d.fail("Failed"),K(p)}}),i.command("remove <name>").description("Remove a marketplace and cascade-delete its installed plugins").action(a=>{let l=ic(a,{cacheDir:r,indexPath:o});if(!l.removedDir&&!l.removedIndexEntry&&l.removedPluginEntries.length===0){n.log(oe.gray(`No marketplace named "${a}" to remove.`));return}let c=[l.removedDir?"directory":null,l.removedIndexEntry?"index entry":null,l.removedPluginEntries.length>0?`${l.removedPluginEntries.length} plugin entry`:null].filter(Boolean);if(n.log(oe.green(`Removed ${a}: ${c.join(" + ")}`)),l.removedPluginEntries.length>0)for(let u of l.removedPluginEntries)n.log(oe.gray(` - ${u}`))}),i.command("update [name]").description("Update one marketplace, or all if no name is given").option("-r, --ref <ref>","Pin to a specific ref instead of the latest tag").action(async(a,l)=>{try{if(a){let c=Um(`Updating ${a}\u2026`).start(),u=await si(a,l.ref?{ref:l.ref}:{},s);lK(u,c)}else{n.log(oe.cyan("Updating all marketplaces\u2026"));let c=await OT(s);if(c.length===0){n.log(oe.gray(" (no marketplaces installed)"));return}for(let u of c)n.log(" "+SE(u))}}catch(c){K(c)}})}function SE(e){switch(e.status){case"updated":{let t=e.addedPlugins.length>0?` +${e.addedPlugins.join(", ")}`:"",n=e.removedPlugins.length>0?` -${e.removedPlugins.join(", ")}`:"";return`${oe.green("\u2713")} ${oe.bold(e.name)}: ${e.fromRef??"(none)"} \u2192 ${e.toRef}${oe.gray(t+n)}`}case"up-to-date":return`${oe.gray("\xB7")} ${oe.bold(e.name)}: up-to-date (${e.ref})`;case"skipped-local":return`${oe.gray("\xB7")} ${oe.bold(e.name)}: skipped (local source)`;case"missing-dir":return`${oe.yellow("!")} ${oe.bold(e.name)}: marketplace dir missing (${e.dir})`}}function lK(e,t){let n=SE(e);e.status==="updated"?t.succeed(n):e.status==="up-to-date"||e.status==="skipped-local"?t.info(n):t.warn(n)}G();import{access as cK,constants as uK,mkdir as dK,readFile as pK}from"fs/promises";import{execSync as mK}from"child_process";W();async function fK(){return ue()?{name:"Anthropic API Key",state:"pass",detail:"ANTHROPIC_API_KEY set"}:{name:"Anthropic API Key",state:"fail",fix:"Set ANTHROPIC_API_KEY or run `afk login`"}}async function gK(){return $s()?{name:"Codex/OpenAI API Key",state:"pass",detail:"OPENAI_API_KEY or CODEX_API_KEY set"}:{name:"Codex/OpenAI API Key",state:"warn",fix:"Set OPENAI_API_KEY or CODEX_API_KEY to use Codex models"}}async function hK(){try{let t=`${mK("npm config get prefix",{timeout:2e3,encoding:"utf-8",stdio:["ignore","pipe","ignore"]}).trim().replace(/\/$/,"")}/bin`;return(T.PATH??"").split(":").map(r=>r.replace(/\/$/,"")).includes(t)?{name:"npm bin on PATH",state:"pass",detail:t}:{name:"npm bin on PATH",state:"fail",detail:t,fix:`Add ${t} to PATH: echo 'export PATH="${t}:$PATH"' >> ~/.zshrc`}}catch{return{name:"npm bin on PATH",state:"warn",detail:"could not query npm prefix"}}}async function jm(e,t){let n=t();try{return await cK(n,uK.W_OK),{name:e,state:"pass",detail:n}}catch{try{return await dK(n,{recursive:!0}),{name:e,state:"pass",detail:`${n} (created)`}}catch{return{name:e,state:"fail",detail:n,fix:`Unable to create or write to ${n}`}}}}async function yK(){let e=Fi();try{let t=await pK(e,"utf-8");return JSON.parse(t),{name:"Config File",state:"pass",detail:`${e} (valid JSON)`}}catch(t){return t.code==="ENOENT"?{name:"Config File",state:"pass",detail:"no config file (using defaults)"}:{name:"Config File",state:"fail",detail:e,fix:`Unable to parse config file: ${t instanceof Error?t.message:"unknown error"}`}}}async function bK(){let e=Pf();return e.length===0?null:{name:"Required env vars",state:"fail",detail:`Missing: ${e.map(t=>t.name).join(", ")}`,fix:"Set these env vars before running. See docs/env-registry.md for descriptions."}}async function wK(){let e=T.AFK_TELEGRAM_BOT_TOKEN;if(!e)return null;try{let t=new AbortController,n=setTimeout(()=>t.abort(),5e3),r=await fetch(`https://api.telegram.org/bot${e}/getMe`,{signal:t.signal});if(clearTimeout(n),r.ok){let s=(await r.json()).result?.username;return{name:"Telegram Bot",state:"pass",detail:s?`@${s}`:"connected"}}return{name:"Telegram Bot",state:"fail",fix:`Telegram API returned ${r.status}. Check AFK_TELEGRAM_BOT_TOKEN.`}}catch(t){return t.name==="AbortError"?{name:"Telegram Bot",state:"warn",detail:"connection timeout"}:{name:"Telegram Bot",state:"warn",detail:`network error: ${t instanceof Error?t.message:"unknown"}`}}}function kE(e){e.command("doctor").description("Check system health and configuration").option("-f, --format <format>","Output format (text|json)","text").action(async t=>{let n=[];n.push(await fK()),n.push(await gK()),n.push(await hK()),n.push(await jm("Config Directory",Xt)),n.push(await jm("State Directory",ye)),n.push(await jm("Logs Directory",kn)),n.push(await yK());let r=await bK();r!==null&&n.push(r);let o=await wK();o!==null&&n.push(o);let s={passed:n.filter(i=>i.state==="pass").length,warned:n.filter(i=>i.state==="warn").length,failed:n.filter(i=>i.state==="fail").length};t.format==="json"?console.log(JSON.stringify({checks:n,summary:s},null,2)):(n.forEach(i=>{let a;i.state==="pass"?a=m.success("\u2713"):i.state==="warn"?a=m.warning("\u26A0"):a=m.error("\u2717");let l=`${a} ${i.name}`;i.detail&&(l+=` \u2014 ${i.detail}`),console.log(l),i.state==="fail"&&i.fix&&console.log(` Fix: ${i.fix}`)}),console.log(`
2273
+ Summary: ${s.passed} passed, ${s.warned} warned, ${s.failed} failed`)),process.exit(s.failed>0?1:0)})}function SK(e){let t=Rs(e),n=As(t),r=t.apiKey===null?1:0,o={source:t.source,message:n,exitCode:r};return t.last4!==void 0&&(o.last4=t.last4),o}function vE(e){e.command("provider").description("Provider diagnostics and configuration").command("auth").description("Inspect provider auth state").command("diagnose").description("Report which OpenAI auth source would be used by the openai-compatible provider. Never prints raw tokens.").option("-f, --format <format>","Output format (text|json)","text").action(r=>{let o=SK(void 0);if(r.format==="json"){let s={source:o.source,message:o.message,ok:o.exitCode===0};o.last4!==void 0&&(s.last4=o.last4),console.log(JSON.stringify(s,null,2))}else{let s=o.exitCode===0?m.success("\u2713"):m.warning("\u26A0");console.log(`${s} ${o.message}`)}process.exit(o.exitCode)})}var kK=["chat","interactive","status","config","daemon","login","plugin","doctor","completion"],vK=["install","update","list","remove","enable","disable"],Wm=["sonnet","opus","haiku"],Hm=["json","text"],Km=["cron","sessionstart","both"];function Mt(e){return e.join(" ")}function TK(){let e=Mt(Wm),t=Mt(Hm),n=Mt(Km);return`#compdef afk
2274
2274
 
2275
2275
  _afk() {
2276
2276
  local -a commands
@@ -2368,7 +2368,7 @@ complete -c afk -n '__fish_seen_subcommand_from plugin' -a 'disable' -d 'Disable
2368
2368
  complete -c afk -l model -s m -x -a '${e}' -d 'Model to use'
2369
2369
  complete -c afk -l format -s f -x -a '${t}' -d 'Output format'
2370
2370
  complete -c afk -l trigger -x -a '${n}' -d 'Trigger type'`}function TE(e){e.command("completion <shell>").description("Emit shell completion script (zsh|bash|fish)").action(t=>{let n=["zsh","bash","fish"];if(!n.includes(t)){e.error(`unknown shell: ${t}. Choose from: ${n.join(", ")}`);return}let r="";switch(t){case"zsh":r=TK();break;case"bash":r=xK();break;case"fish":r=EK();break}console.log(r)})}import Se from"chalk";import{spawn as UK}from"child_process";import{existsSync as jK,readFileSync as WK}from"fs";W();import{execFileSync as RK,spawn as AK}from"child_process";import{existsSync as Xo,mkdirSync as xE,readFileSync as Mc,statSync as _K,unlinkSync as Oc,writeFileSync as CK,openSync as EE}from"fs";import{join as Yo,dirname as Gm}from"path";import{fileURLToPath as IK}from"url";var PK=Gm(IK(import.meta.url));function zm(){let e=Yo(ye(),"telegram");return{pidFile:Yo(e,"bot.pid"),logFile:Yo(kn(),"telegram.log")}}function hi(e){if(!Xo(e))return null;let t=Mc(e,"utf-8").trim(),n=Number.parseInt(t,10);if(!Number.isFinite(n)||n<=0)return Oc(e),null;try{return process.kill(n,0),n}catch{return Oc(e),null}}function Jm(e=PK,t=Xo){let n=[Yo(e,"telegram.mjs"),Yo(e,"..","telegram.js"),Yo(e,"..","telegram.ts")];for(let r of n)if(t(r))return r;throw new Error(`Telegram entrypoint not found. Searched: ${n.join(", ")}`)}async function Vm(){let{pidFile:e,logFile:t}=zm(),n=hi(e);if(n!==null)return{kind:"already-running",pid:n,message:`Bot already running (PID ${n}). Use 'afk telegram stop' first.`};xE(Gm(e),{recursive:!0}),xE(Gm(t),{recursive:!0});let r=Jm(),o=EE(t,"a"),s=EE(t,"a"),i;try{i=AK(process.execPath,[r],{detached:!0,stdio:["ignore",o,s],env:process.env})}catch(a){return{kind:"spawn-failed",message:`Failed to spawn bot: ${a.message}`}}return i.pid===void 0?{kind:"spawn-failed",message:"Spawned child has no PID"}:(CK(e,String(i.pid),{mode:420}),i.unref(),await new Promise(a=>setTimeout(a,1500)),hi(e)===null?{kind:"exited-immediately",logTail:qm(t,20),message:"Bot exited immediately after launch. Check the log for details."}:{kind:"started",pid:i.pid,logFile:t})}async function Ym(){let{pidFile:e}=zm(),t=hi(e);if(t===null)return{kind:"not-running"};try{process.kill(t,"SIGTERM")}catch{return Xo(e)&&Oc(e),{kind:"stopped",pid:t}}for(let n=0;n<50;n++)if(await new Promise(r=>setTimeout(r,100)),hi(e)===null)return{kind:"stopped",pid:t};try{process.kill(t,"SIGKILL")}catch{}return Xo(e)&&Oc(e),{kind:"force-killed",pid:t}}function Xm(){let{pidFile:e,logFile:t}=zm(),n=hi(e),r={running:n!==null,pidFile:e,logFile:t};if(n===null)return{...r,logTail:qm(t,10)};let o=MK(n);return{...r,pid:n,...o,logTail:qm(t,10)}}function qm(e,t){if(!Xo(e))return[];try{let r=Mc(e,"utf-8").split(`
2371
- `);return r.length>0&&r[r.length-1]===""&&r.pop(),r.slice(-t)}catch{return[]}}function MK(e){try{if(process.platform==="linux"){let t=`/proc/${e}/stat`;if(!Xo(t))return{};let r=Mc(t,"utf-8").split(" "),o=Number.parseInt(r[21]??"0",10),l=_K("/proc/1").mtimeMs/1e3+o/100,c=Math.floor(Date.now()/1e3-l),d=Mc(`/proc/${e}/status`,"utf-8").match(/VmRSS:\s+(\d+)\s+kB/),m=d?Math.round(Number.parseInt(d[1]??"0",10)/1024):void 0;return{uptimeSec:c,memoryMb:m}}if(process.platform==="darwin"){let t=RK("ps",["-p",String(e),"-o","etime=,rss="],{encoding:"utf-8",stdio:["ignore","pipe","ignore"]}).trim(),[n,r]=t.split(/\s+/);return{uptimeSec:OK(n??""),memoryMb:r?Math.round(Number.parseInt(r,10)/1024):void 0}}}catch{}return{}}function OK(e){if(!e)return;let t=e.split("-"),n=0,r=e;t.length===2&&(n=Number.parseInt(t[0]??"0",10),r=t[1]??"");let o=r.split(":").map(l=>Number.parseInt(l,10));if(o.some(l=>!Number.isFinite(l)))return;let s=0,i=0,a=0;if(o.length===3)[s,i,a]=o;else if(o.length===2)[i,a]=o;else if(o.length===1)[a]=o;else return;return n*86400+s*3600+i*60+a}import{existsSync as $K,readFileSync as DK}from"fs";import{createInterface as LK}from"readline";import ve from"chalk";W();G();var AE="https://api.telegram.org";function _E(e,t){if(!$K(e))return;let n=DK(e,"utf-8");for(let r of n.split(`
2371
+ `);return r.length>0&&r[r.length-1]===""&&r.pop(),r.slice(-t)}catch{return[]}}function MK(e){try{if(process.platform==="linux"){let t=`/proc/${e}/stat`;if(!Xo(t))return{};let r=Mc(t,"utf-8").split(" "),o=Number.parseInt(r[21]??"0",10),l=_K("/proc/1").mtimeMs/1e3+o/100,c=Math.floor(Date.now()/1e3-l),d=Mc(`/proc/${e}/status`,"utf-8").match(/VmRSS:\s+(\d+)\s+kB/),p=d?Math.round(Number.parseInt(d[1]??"0",10)/1024):void 0;return{uptimeSec:c,memoryMb:p}}if(process.platform==="darwin"){let t=RK("ps",["-p",String(e),"-o","etime=,rss="],{encoding:"utf-8",stdio:["ignore","pipe","ignore"]}).trim(),[n,r]=t.split(/\s+/);return{uptimeSec:OK(n??""),memoryMb:r?Math.round(Number.parseInt(r,10)/1024):void 0}}}catch{}return{}}function OK(e){if(!e)return;let t=e.split("-"),n=0,r=e;t.length===2&&(n=Number.parseInt(t[0]??"0",10),r=t[1]??"");let o=r.split(":").map(l=>Number.parseInt(l,10));if(o.some(l=>!Number.isFinite(l)))return;let s=0,i=0,a=0;if(o.length===3)[s,i,a]=o;else if(o.length===2)[i,a]=o;else if(o.length===1)[a]=o;else return;return n*86400+s*3600+i*60+a}import{existsSync as $K,readFileSync as DK}from"fs";import{createInterface as LK}from"readline";import ve from"chalk";W();G();var AE="https://api.telegram.org";function _E(e,t){if(!$K(e))return;let n=DK(e,"utf-8");for(let r of n.split(`
2372
2372
  `)){let o=r.trim();if(!o||o.startsWith("#"))continue;let s=o.indexOf("=");if(s===-1||o.slice(0,s).trim()!==t)continue;let a=o.slice(s+1).trim();return(a.startsWith('"')&&a.endsWith('"')||a.startsWith("'")&&a.endsWith("'"))&&(a=a.slice(1,-1)),a}}async function CE(e){let t=_E(e,"TELEGRAM_BOT_TOKEN");if(!t)return{set:!1,valid:!1,reason:"unset"};let n=await Zm(t);return n?{set:!0,valid:!0,botId:n.id,...n.username!==void 0?{username:n.username}:{}}:{set:!0,valid:!1,reason:"unauthorized"}}async function IE(e,t={}){let n=_E(e,"TELEGRAM_BOT_TOKEN");if(!n)return{found:!1,chats:[],reason:"unset"};let r=t.timeoutSec??60,o=2e3,s=Math.max(1,Math.ceil(r*1e3/o)),i=await PE(n,{maxAttempts:s,intervalMs:o});return i.length===0?{found:!1,chats:[],reason:"timeout"}:{found:!0,chats:i}}async function Zm(e){try{let t=await fetch(`${AE}/bot${e}/getMe`);if(!t.ok)return null;let n=await t.json();return!n.ok||!n.result?.id||!n.result?.first_name?null:{id:n.result.id,...n.result.username!==void 0?{username:n.result.username}:{},firstName:n.result.first_name}}catch{return null}}function FK(e){let t=new Map;for(let n of e){let o=(n.message??n.edited_message)?.chat;!o||typeof o.id!="number"||t.set(o.id,{chatId:o.id,type:o.type??"unknown",...o.username!==void 0?{username:o.username}:{},...o.first_name!==void 0?{firstName:o.first_name}:{}})}return[...t.values()].reverse()}async function NK(e){try{let t=await fetch(`${AE}/bot${e}/getUpdates`);if(!t.ok)return[];let n=await t.json();return!n.ok||!Array.isArray(n.result)?[]:n.result}catch{return[]}}async function PE(e,t={}){let n=t.maxAttempts??30,r=t.intervalMs??2e3;for(let o=0;o<n;o++){let s=await NK(e),i=FK(s);if(i.length>0)return i;o<n-1&&await new Promise(a=>setTimeout(a,r))}return[]}function RE(e){let t=LK({input:process.stdin,output:process.stdout});return new Promise(n=>{t.question(e,r=>{t.close(),n(r.trim())})})}function BK(e){return process.stdin.isTTY||(console.error(ve.red(`Cannot securely prompt for secret on a non-TTY stdin: "${e.trim()}"`)),console.error(ve.gray(" Supply TELEGRAM_BOT_TOKEN via environment variable or ~/.afk/config/afk.env instead.")),process.exit(1)),new Promise(t=>{process.stdout.write(e);let n=[];process.stdin.setRawMode(!0),process.stdin.resume(),process.stdin.setEncoding("utf-8");let r=o=>{o==="\r"||o===`
2373
2373
  `||o===""?(process.stdin.setRawMode(!1),process.stdin.pause(),process.stdin.removeListener("data",r),process.stdout.write(`
2374
2374
  `),t(n.join("").trim())):o===""?(process.stdin.setRawMode(!1),process.stdin.pause(),process.stdout.write(`
@@ -2380,21 +2380,21 @@ Multiple chats found:`)),o.forEach((l,c)=>{let u=l.username?`@${l.username}`:l.f
2380
2380
  `)}),t.command("start").description("Start the bot as a background daemon").action(async()=>{let n=await Vm();if(n.kind==="started"){console.log(Se.green(`\u2713 Bot started (PID ${n.pid})`)),console.log(Se.gray(` Logs: ${n.logFile}`)),console.log(Se.gray(" Tail with: afk telegram logs --follow"));return}if(n.kind==="already-running"&&(console.log(Se.yellow(`\u26A0 ${n.message}`)),process.exit(1)),n.kind==="exited-immediately"){if(console.error(Se.red(`\u2717 ${n.message}`)),n.logTail&&n.logTail.length>0){console.error(""),console.error(Se.bold("Last log entries:"));for(let r of n.logTail)console.error(Se.gray(` ${r}`))}process.exit(1)}console.error(Se.red(`\u2717 ${n.message}`)),process.exit(1)}),t.command("stop").description("Stop the bot (SIGTERM, then SIGKILL after 5s)").action(async()=>{let n=await Ym();if(n.kind==="not-running"){console.log(Se.yellow("\u26A0 Bot is not running"));return}if(n.kind==="stopped"){console.log(Se.green(`\u2713 Bot stopped (PID ${n.pid})`));return}console.log(Se.yellow(`\u26A0 Bot force-killed (PID ${n.pid}); graceful shutdown timed out`))}),t.command("restart").description("Stop and restart the bot").action(async()=>{let n=await Ym();(n.kind==="stopped"||n.kind==="force-killed")&&console.log(Se.gray(`Stopped (PID ${n.pid})`));let r=await Vm();if(r.kind==="started"){console.log(Se.green(`\u2713 Bot restarted (PID ${r.pid})`));return}console.error(Se.red(`\u2717 Restart failed: ${r.message}`)),process.exit(1)}),t.command("status").description("Show running state, uptime, memory, and recent log entries").action(()=>{let n=Xm();HK(n)}),t.command("logs").description("Show or follow the bot log").option("-f, --follow","Stream new log entries (like tail -f)",!1).option("-n, --lines <count>","Number of trailing lines to show","50").action(n=>{let{logFile:r}=Xm();if(!jK(r)){console.log(Se.yellow(`No log file at ${r}`)),console.log(Se.gray("Start the bot first: afk telegram start"));return}let o=Number.parseInt(n.lines??"50",10);if(n.follow){UK("tail",["-n",String(o),"-f",r],{stdio:"inherit"}).on("error",a=>{console.error(Se.red(`Failed to spawn tail: ${a.message}`))});return}let s=WK(r,"utf-8").split(`
2381
2381
  `).slice(-o-1);console.log(s.join(`
2382
2382
  `))})}function HK(e){if(console.log(Se.bold("\u{1F4CA} Telegram Bot Status")),console.log(""),e.running?(console.log(` ${Se.green("\u25CF")} Running (PID ${e.pid})`),e.uptimeSec!==void 0&&console.log(` Uptime: ${KK(e.uptimeSec)}`),e.memoryMb!==void 0&&console.log(` Memory: ${e.memoryMb} MB`)):console.log(` ${Se.red("\u25CF")} Stopped`),console.log(` PID: ${e.pidFile}`),console.log(` Logs: ${e.logFile}`),e.logTail&&e.logTail.length>0){console.log(""),console.log(Se.bold("Recent log entries:"));for(let t of e.logTail)console.log(Se.gray(` ${t}`))}}function KK(e){if(e<60)return`${e}s`;let t=Math.floor(e/60);if(t<60)return`${t}m ${e%60}s`;let n=Math.floor(t/60);return n<24?`${n}h ${t%60}m`:`${Math.floor(n/24)}d ${n%24}h`}G();import Q from"chalk";import{spawn as a2}from"child_process";import{existsSync as l2,readFileSync as c2}from"fs";W();import{execFileSync as GK,spawn as qK}from"child_process";import{existsSync as _r,mkdirSync as $E,readFileSync as $c,statSync as zK,unlinkSync as Dc,writeFileSync as JK,openSync as DE}from"fs";import{join as yi,dirname as ef}from"path";import{fileURLToPath as VK}from"url";var Qm=ef(VK(import.meta.url));function nf(){let e=yi(ye(),"threads");return{pidFile:yi(e,"poller.pid"),logFile:yi(kn(),"threads.log")}}function bi(e){if(!_r(e))return null;let t=$c(e,"utf-8").trim(),n=Number.parseInt(t,10);if(!Number.isFinite(n)||n<=0)return Dc(e),null;try{return process.kill(n,0),n}catch{return Dc(e),null}}function YK(){let e=yi(Qm,"..","threads.js");if(_r(e))return e;let t=yi(Qm,"..","threads.ts");if(_r(t))return t;throw new Error(`Threads entrypoint not found near ${Qm}`)}async function rf(){let{pidFile:e,logFile:t}=nf(),n=bi(e);if(n!==null)return{kind:"already-running",pid:n,message:`Poller already running (PID ${n}). Use 'afk threads stop' first.`};$E(ef(e),{recursive:!0}),$E(ef(t),{recursive:!0});let r=YK(),o=DE(t,"a"),s=DE(t,"a"),i;try{i=qK(process.execPath,[r],{detached:!0,stdio:["ignore",o,s],env:process.env})}catch(a){return{kind:"spawn-failed",message:`Failed to spawn poller: ${a.message}`}}return i.pid===void 0?{kind:"spawn-failed",message:"Spawned child has no PID"}:(JK(e,String(i.pid),{mode:420}),i.unref(),await new Promise(a=>setTimeout(a,1500)),bi(e)===null?{kind:"exited-immediately",logTail:tf(t,20),message:"Poller exited immediately after launch. Check the log for details."}:{kind:"started",pid:i.pid,logFile:t})}async function of(){let{pidFile:e}=nf(),t=bi(e);if(t===null)return{kind:"not-running"};try{process.kill(t,"SIGTERM")}catch{return _r(e)&&Dc(e),{kind:"stopped",pid:t}}for(let n=0;n<50;n++)if(await new Promise(r=>setTimeout(r,100)),bi(e)===null)return{kind:"stopped",pid:t};try{process.kill(t,"SIGKILL")}catch{}return _r(e)&&Dc(e),{kind:"force-killed",pid:t}}function sf(){let{pidFile:e,logFile:t}=nf(),n=bi(e),r={running:n!==null,pidFile:e,logFile:t};if(n===null)return{...r,logTail:tf(t,10)};let o=XK(n);return{...r,pid:n,...o,logTail:tf(t,10)}}function tf(e,t){if(!_r(e))return[];try{let r=$c(e,"utf-8").split(`
2383
- `);return r.length>0&&r[r.length-1]===""&&r.pop(),r.slice(-t)}catch{return[]}}function XK(e){try{if(process.platform==="linux"){let t=`/proc/${e}/stat`;if(!_r(t))return{};let r=$c(t,"utf-8").split(" "),o=Number.parseInt(r[21]??"0",10),l=zK("/proc/1").mtimeMs/1e3+o/100,c=Math.floor(Date.now()/1e3-l),d=$c(`/proc/${e}/status`,"utf-8").match(/VmRSS:\s+(\d+)\s+kB/),m=d?Math.round(Number.parseInt(d[1]??"0",10)/1024):void 0;return{uptimeSec:c,memoryMb:m}}if(process.platform==="darwin"){let t=GK("ps",["-p",String(e),"-o","etime=,rss="],{encoding:"utf-8",stdio:["ignore","pipe","ignore"]}).trim(),[n,r]=t.split(/\s+/);return{uptimeSec:ZK(n??""),memoryMb:r?Math.round(Number.parseInt(r,10)/1024):void 0}}}catch{}return{}}function ZK(e){if(!e)return;let t=e.split("-"),n=0,r=e;t.length===2&&(n=Number.parseInt(t[0]??"0",10),r=t[1]??"");let o=r.split(":").map(l=>Number.parseInt(l,10));if(o.some(l=>!Number.isFinite(l)))return;let s=0,i=0,a=0;if(o.length===3)[s,i,a]=o;else if(o.length===2)[i,a]=o;else if(o.length===1)[a]=o;else return;return n*86400+s*3600+i*60+a}import{existsSync as QK,readFileSync as e2}from"fs";import{homedir as t2}from"os";import{join as n2}from"path";function r2(){return n2(t2(),".config","threads-cli","config.json")}function LE(e,t){let r=(e??process.env).THREADS_ACCESS_TOKEN;if(r!==void 0&&r.trim().length>0)return{kind:"env",token:r.trim()};let o=t??r2();if(!QK(o))return{kind:"missing",path:o,reason:`No THREADS_ACCESS_TOKEN env var and no config file at ${o}. Run \`threads config set-token <token>\` or export THREADS_ACCESS_TOKEN.`};try{let s=e2(o,"utf-8"),a=JSON.parse(s).access_token;return typeof a!="string"||a.trim().length===0?{kind:"missing",path:o,reason:`Config file at ${o} has no .access_token string. Run \`threads config set-token <token>\`.`}:{kind:"file",token:a.trim(),path:o}}catch(s){return{kind:"missing",path:o,reason:`Config file at ${o} unreadable or not JSON: ${s.message}`}}}var Lc="https://graph.threads.net/v1.0",kt=class extends Error{constructor(n){super(n.message);this.detail=n;this.name="ThreadsApiError"}detail};function o2(e,t,n=Lc){let r=(e.fields??["id","username","text","timestamp","permalink","replied_to","root_post"]).join(","),o=new URLSearchParams;return o.set("fields",r),e.sinceSec!==void 0&&o.set("since",String(e.sinceSec)),e.limit!==void 0&&o.set("limit",String(e.limit)),o.set("access_token",t),`${n}/me/mentions?${o.toString()}`}function s2(e,t=Lc){let n=new URLSearchParams;return n.set("fields","id,username"),n.set("access_token",e),`${t}/me?${n.toString()}`}async function FE(e){let t="";try{t=await e.text()}catch{}let n=t||e.statusText||`HTTP ${e.status}`;if(e.status===401||e.status===403)return{kind:"auth",status:e.status,message:n};if(e.status===429){let r=e.headers.get("retry-after"),o=r?Number.parseInt(r,10):void 0;return{kind:"rate-limit",status:e.status,...Number.isFinite(o)?{retryAfterSec:o}:{},message:n}}return{kind:"http",status:e.status,message:n}}async function NE(e,t=fetch,n=Lc){let r;try{r=await t(s2(e,n))}catch(i){throw new kt({kind:"network",message:i.message})}if(!r.ok)throw new kt(await FE(r));let o;try{o=await r.json()}catch(i){throw new kt({kind:"parse",message:i.message})}if(typeof o!="object"||o===null||typeof o.id!="string"||typeof o.username!="string")throw new kt({kind:"parse",message:`/me did not return {id, username}: ${JSON.stringify(o).slice(0,200)}`});let s=o;return{id:s.id,username:s.username}}function i2(e){if(typeof e!="object"||e===null)return null;let t=e;if(typeof t.id!="string"||typeof t.username!="string")return null;let n={id:t.id,username:t.username,text:typeof t.text=="string"?t.text:"",timestamp:typeof t.timestamp=="string"?t.timestamp:"",permalink:typeof t.permalink=="string"?t.permalink:""},r=t.replied_to;if(typeof r=="object"&&r!==null){let s=r.id;typeof s=="string"&&(n.replyToId=s)}let o=t.root_post;if(typeof o=="object"&&o!==null){let s=o.id;typeof s=="string"&&(n.rootPostId=s)}return n}async function BE(e,t=fetch,n=Lc){let r=o2({...e.sinceSec!==void 0?{sinceSec:e.sinceSec}:{},...e.limit!==void 0?{limit:e.limit}:{}},e.accessToken,n),o;try{o=await t(r)}catch(l){throw new kt({kind:"network",message:l.message})}if(!o.ok)throw new kt(await FE(o));let s;try{s=await o.json()}catch(l){throw new kt({kind:"parse",message:l.message})}let i=Array.isArray(s.data)?s.data:[],a=[];for(let l of i){let c=i2(l);c!==null&&a.push(c)}return a}function UE(e,t=()=>{}){let n=new Set;if(!e)return n;for(let r of e.split(",")){let o=r.trim().replace(/^@/,"").toLowerCase();o&&(/^[a-z0-9._]{1,30}$/.test(o)||t("[threads-allowlist] Suspicious username (still added):",o),n.add(o))}return n}function jE(e,t){return e.has(t.trim().replace(/^@/,"").toLowerCase())}function WE(e){let t=e.command("threads").description("Manage the Agent AFK Threads mention poller (start, stop, status, test-fetch)");t.command("start").description("Start the poller as a background daemon").action(async()=>{let n=await rf();if(n.kind==="started"){console.log(Q.green(`\u2713 Poller started (PID ${n.pid})`)),console.log(Q.gray(` Logs: ${n.logFile}`)),console.log(Q.gray(" Tail with: afk threads logs --follow"));return}if(n.kind==="already-running"&&(console.log(Q.yellow(`\u26A0 ${n.message}`)),process.exit(1)),n.kind==="exited-immediately"){if(console.error(Q.red(`\u2717 ${n.message}`)),n.logTail&&n.logTail.length>0){console.error(""),console.error(Q.bold("Last log entries:"));for(let r of n.logTail)console.error(Q.gray(` ${r}`))}process.exit(1)}console.error(Q.red(`\u2717 ${n.message}`)),process.exit(1)}),t.command("stop").description("Stop the poller (SIGTERM, then SIGKILL after 5s)").action(async()=>{let n=await of();if(n.kind==="not-running"){console.log(Q.yellow("\u26A0 Poller is not running"));return}if(n.kind==="stopped"){console.log(Q.green(`\u2713 Poller stopped (PID ${n.pid})`));return}console.log(Q.yellow(`\u26A0 Poller force-killed (PID ${n.pid}); graceful shutdown timed out`))}),t.command("restart").description("Stop and restart the poller").action(async()=>{let n=await of();(n.kind==="stopped"||n.kind==="force-killed")&&console.log(Q.gray(`Stopped (PID ${n.pid})`));let r=await rf();if(r.kind==="started"){console.log(Q.green(`\u2713 Poller restarted (PID ${r.pid})`));return}console.error(Q.red(`\u2717 Restart failed: ${r.message}`)),process.exit(1)}),t.command("status").description("Show running state, uptime, memory, and recent log entries").action(()=>{let n=sf();u2(n)}),t.command("logs").description("Show or follow the poller log").option("-f, --follow","Stream new log entries (like tail -f)",!1).option("-n, --lines <count>","Number of trailing lines to show","50").action(n=>{let{logFile:r}=sf();if(!l2(r)){console.log(Q.yellow(`No log file at ${r}`)),console.log(Q.gray("Start the poller first: afk threads start"));return}let o=Number.parseInt(n.lines??"50",10);if(n.follow){a2("tail",["-n",String(o),"-f",r],{stdio:"inherit"}).on("error",a=>{console.error(Q.red(`Failed to spawn tail: ${a.message}`))});return}let s=c2(r,"utf-8").split(`
2383
+ `);return r.length>0&&r[r.length-1]===""&&r.pop(),r.slice(-t)}catch{return[]}}function XK(e){try{if(process.platform==="linux"){let t=`/proc/${e}/stat`;if(!_r(t))return{};let r=$c(t,"utf-8").split(" "),o=Number.parseInt(r[21]??"0",10),l=zK("/proc/1").mtimeMs/1e3+o/100,c=Math.floor(Date.now()/1e3-l),d=$c(`/proc/${e}/status`,"utf-8").match(/VmRSS:\s+(\d+)\s+kB/),p=d?Math.round(Number.parseInt(d[1]??"0",10)/1024):void 0;return{uptimeSec:c,memoryMb:p}}if(process.platform==="darwin"){let t=GK("ps",["-p",String(e),"-o","etime=,rss="],{encoding:"utf-8",stdio:["ignore","pipe","ignore"]}).trim(),[n,r]=t.split(/\s+/);return{uptimeSec:ZK(n??""),memoryMb:r?Math.round(Number.parseInt(r,10)/1024):void 0}}}catch{}return{}}function ZK(e){if(!e)return;let t=e.split("-"),n=0,r=e;t.length===2&&(n=Number.parseInt(t[0]??"0",10),r=t[1]??"");let o=r.split(":").map(l=>Number.parseInt(l,10));if(o.some(l=>!Number.isFinite(l)))return;let s=0,i=0,a=0;if(o.length===3)[s,i,a]=o;else if(o.length===2)[i,a]=o;else if(o.length===1)[a]=o;else return;return n*86400+s*3600+i*60+a}import{existsSync as QK,readFileSync as e2}from"fs";import{homedir as t2}from"os";import{join as n2}from"path";function r2(){return n2(t2(),".config","threads-cli","config.json")}function LE(e,t){let r=(e??process.env).THREADS_ACCESS_TOKEN;if(r!==void 0&&r.trim().length>0)return{kind:"env",token:r.trim()};let o=t??r2();if(!QK(o))return{kind:"missing",path:o,reason:`No THREADS_ACCESS_TOKEN env var and no config file at ${o}. Run \`threads config set-token <token>\` or export THREADS_ACCESS_TOKEN.`};try{let s=e2(o,"utf-8"),a=JSON.parse(s).access_token;return typeof a!="string"||a.trim().length===0?{kind:"missing",path:o,reason:`Config file at ${o} has no .access_token string. Run \`threads config set-token <token>\`.`}:{kind:"file",token:a.trim(),path:o}}catch(s){return{kind:"missing",path:o,reason:`Config file at ${o} unreadable or not JSON: ${s.message}`}}}var Lc="https://graph.threads.net/v1.0",kt=class extends Error{constructor(n){super(n.message);this.detail=n;this.name="ThreadsApiError"}detail};function o2(e,t,n=Lc){let r=(e.fields??["id","username","text","timestamp","permalink","replied_to","root_post"]).join(","),o=new URLSearchParams;return o.set("fields",r),e.sinceSec!==void 0&&o.set("since",String(e.sinceSec)),e.limit!==void 0&&o.set("limit",String(e.limit)),o.set("access_token",t),`${n}/me/mentions?${o.toString()}`}function s2(e,t=Lc){let n=new URLSearchParams;return n.set("fields","id,username"),n.set("access_token",e),`${t}/me?${n.toString()}`}async function FE(e){let t="";try{t=await e.text()}catch{}let n=t||e.statusText||`HTTP ${e.status}`;if(e.status===401||e.status===403)return{kind:"auth",status:e.status,message:n};if(e.status===429){let r=e.headers.get("retry-after"),o=r?Number.parseInt(r,10):void 0;return{kind:"rate-limit",status:e.status,...Number.isFinite(o)?{retryAfterSec:o}:{},message:n}}return{kind:"http",status:e.status,message:n}}async function NE(e,t=fetch,n=Lc){let r;try{r=await t(s2(e,n))}catch(i){throw new kt({kind:"network",message:i.message})}if(!r.ok)throw new kt(await FE(r));let o;try{o=await r.json()}catch(i){throw new kt({kind:"parse",message:i.message})}if(typeof o!="object"||o===null||typeof o.id!="string"||typeof o.username!="string")throw new kt({kind:"parse",message:`/me did not return {id, username}: ${JSON.stringify(o).slice(0,200)}`});let s=o;return{id:s.id,username:s.username}}function i2(e){if(typeof e!="object"||e===null)return null;let t=e;if(typeof t.id!="string"||typeof t.username!="string")return null;let n={id:t.id,username:t.username,text:typeof t.text=="string"?t.text:"",timestamp:typeof t.timestamp=="string"?t.timestamp:"",permalink:typeof t.permalink=="string"?t.permalink:""},r=t.replied_to;if(typeof r=="object"&&r!==null){let s=r.id;typeof s=="string"&&(n.replyToId=s)}let o=t.root_post;if(typeof o=="object"&&o!==null){let s=o.id;typeof s=="string"&&(n.rootPostId=s)}return n}async function BE(e,t=fetch,n=Lc){let r=o2({...e.sinceSec!==void 0?{sinceSec:e.sinceSec}:{},...e.limit!==void 0?{limit:e.limit}:{}},e.accessToken,n),o;try{o=await t(r)}catch(l){throw new kt({kind:"network",message:l.message})}if(!o.ok)throw new kt(await FE(o));let s;try{s=await o.json()}catch(l){throw new kt({kind:"parse",message:l.message})}let i=Array.isArray(s.data)?s.data:[],a=[];for(let l of i){let c=i2(l);c!==null&&a.push(c)}return a}function UE(e,t=()=>{}){let n=new Set;if(!e)return n;for(let r of e.split(",")){let o=r.trim().replace(/^@/,"").toLowerCase();o&&(/^[a-z0-9._]{1,30}$/.test(o)||t("[threads-allowlist] Suspicious username (still added):",o),n.add(o))}return n}function jE(e,t){return e.has(t.trim().replace(/^@/,"").toLowerCase())}function WE(e){let t=e.command("threads").description("Manage the Agent AFK Threads mention poller (start, stop, status, test-fetch)");t.command("start").description("Start the poller as a background daemon").action(async()=>{let n=await rf();if(n.kind==="started"){console.log(Q.green(`\u2713 Poller started (PID ${n.pid})`)),console.log(Q.gray(` Logs: ${n.logFile}`)),console.log(Q.gray(" Tail with: afk threads logs --follow"));return}if(n.kind==="already-running"&&(console.log(Q.yellow(`\u26A0 ${n.message}`)),process.exit(1)),n.kind==="exited-immediately"){if(console.error(Q.red(`\u2717 ${n.message}`)),n.logTail&&n.logTail.length>0){console.error(""),console.error(Q.bold("Last log entries:"));for(let r of n.logTail)console.error(Q.gray(` ${r}`))}process.exit(1)}console.error(Q.red(`\u2717 ${n.message}`)),process.exit(1)}),t.command("stop").description("Stop the poller (SIGTERM, then SIGKILL after 5s)").action(async()=>{let n=await of();if(n.kind==="not-running"){console.log(Q.yellow("\u26A0 Poller is not running"));return}if(n.kind==="stopped"){console.log(Q.green(`\u2713 Poller stopped (PID ${n.pid})`));return}console.log(Q.yellow(`\u26A0 Poller force-killed (PID ${n.pid}); graceful shutdown timed out`))}),t.command("restart").description("Stop and restart the poller").action(async()=>{let n=await of();(n.kind==="stopped"||n.kind==="force-killed")&&console.log(Q.gray(`Stopped (PID ${n.pid})`));let r=await rf();if(r.kind==="started"){console.log(Q.green(`\u2713 Poller restarted (PID ${r.pid})`));return}console.error(Q.red(`\u2717 Restart failed: ${r.message}`)),process.exit(1)}),t.command("status").description("Show running state, uptime, memory, and recent log entries").action(()=>{let n=sf();u2(n)}),t.command("logs").description("Show or follow the poller log").option("-f, --follow","Stream new log entries (like tail -f)",!1).option("-n, --lines <count>","Number of trailing lines to show","50").action(n=>{let{logFile:r}=sf();if(!l2(r)){console.log(Q.yellow(`No log file at ${r}`)),console.log(Q.gray("Start the poller first: afk threads start"));return}let o=Number.parseInt(n.lines??"50",10);if(n.follow){a2("tail",["-n",String(o),"-f",r],{stdio:"inherit"}).on("error",a=>{console.error(Q.red(`Failed to spawn tail: ${a.message}`))});return}let s=c2(r,"utf-8").split(`
2384
2384
  `).slice(-o-1);console.log(s.join(`
2385
- `))}),t.command("test-fetch").description("One-shot: GET /me/mentions, print payload (verifies token + filtering, no agent dispatch)").option("--limit <n>","Max events to fetch","5").option("--no-filter","Skip allowlist + self-loop filter (dump every event)").action(async n=>{let r=LE();r.kind==="missing"&&(console.error(Q.red("\u2717 "+r.reason)),process.exit(1));let o=r.token;console.log(Q.gray(`Token: ${r.kind==="file"?r.path:"THREADS_ACCESS_TOKEN env"}`));let s;try{s=await NE(o)}catch(m){m instanceof kt?console.error(Q.red(`\u2717 /me failed (${m.detail.kind}): ${m.detail.message}`)):console.error(Q.red(`\u2717 /me failed: ${m.message}`)),process.exit(1)}console.log(Q.gray(`Identity: @${s.username} (id ${s.id})`));let i=UE(T.AFK_THREADS_ALLOWED_USERNAMES),a=n.filter!==!1;a?i.size===0?(console.log(Q.yellow("\u26A0 AFK_THREADS_ALLOWED_USERNAMES is empty \u2014 all events will be filtered out.")),console.log(Q.gray(" Re-run with --no-filter to see raw payload."))):console.log(Q.gray(`Allowlist: ${[...i].map(m=>"@"+m).join(", ")}`)):console.log(Q.gray("Filter: OFF (dumping raw payload)")),console.log("");let l=Number.parseInt(n.limit??"5",10),c;try{c=await BE({accessToken:o,limit:l})}catch(m){m instanceof kt?console.error(Q.red(`\u2717 /me/mentions failed (${m.detail.kind}): ${m.detail.message}`)):console.error(Q.red(`\u2717 /me/mentions failed: ${m.message}`)),process.exit(1)}if(c.length===0){console.log(Q.gray("No mentions returned.")),console.log(Q.gray("Tag your account in a post (or have someone in the allowlist do it), then re-run."));return}let u=0,d=0;for(let m of c){let f=m.username.toLowerCase()===s.username.toLowerCase(),g=jE(i,m.username),h=!a||!f&&g,b=h?Q.green("\u2713 KEEP "):Q.gray("\u2717 FILTER "),y=h?"":f?" (self-loop)":g?"":" (not allowlisted)";console.log(`${b}@${m.username} ${Q.gray("\u2022")} ${m.timestamp}${Q.gray(y)}`),console.log(Q.gray(` id=${m.id}`)),console.log(` ${m.text.slice(0,200)}${m.text.length>200?"\u2026":""}`),m.permalink&&console.log(Q.gray(` ${m.permalink}`)),console.log(""),h?u++:d++}console.log(Q.bold(`Summary: ${u} kept, ${d} filtered (of ${c.length} fetched)`))})}function u2(e){if(console.log(Q.bold("\u{1F4CA} Threads Poller Status")),console.log(""),e.running?(console.log(` ${Q.green("\u25CF")} Running (PID ${e.pid})`),e.uptimeSec!==void 0&&console.log(` Uptime: ${d2(e.uptimeSec)}`),e.memoryMb!==void 0&&console.log(` Memory: ${e.memoryMb} MB`)):console.log(` ${Q.red("\u25CF")} Stopped`),console.log(` PID: ${e.pidFile}`),console.log(` Logs: ${e.logFile}`),e.logTail&&e.logTail.length>0){console.log(""),console.log(Q.bold("Recent log entries:"));for(let t of e.logTail)console.log(Q.gray(` ${t}`))}}function d2(e){if(e<60)return`${e}s`;let t=Math.floor(e/60);if(t<60)return`${t}m ${e%60}s`;let n=Math.floor(t/60);return n<24?`${n}h ${t%60}m`:`${Math.floor(n/24)}d ${n%24}h`}import{execFile as U2}from"node:child_process";import{promisify as j2}from"node:util";import pe from"chalk";W();import{execFile as p2}from"node:child_process";import{randomBytes as m2}from"node:crypto";import{promises as Zo}from"node:fs";import{join as Fc}from"node:path";import{promisify as f2}from"node:util";var g2=f2(p2),HE=16;var Qe=class extends Error{cause;code;constructor(t,n,r){super(t),this.name="WorktreeError",this.cause=n,this.code=r}};function af(e,t=40){return e.toLowerCase().trim().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"").slice(0,t).replace(/-+$/g,"")||"task"}function h2(){return m2(4).toString("hex").slice(0,4)}function y2(e){let t=n=>String(n).padStart(2,"0");return`${e.getUTCFullYear()}${t(e.getUTCMonth()+1)}${t(e.getUTCDate())}T${t(e.getUTCHours())}${t(e.getUTCMinutes())}${t(e.getUTCSeconds())}`}function b2(e,t={}){let n=(t.now??(()=>new Date))(),r=(t.randomSuffix??h2)();return`${y2(n)}-${af(e,32)}-${r}`}async function Cr(e,t){try{let n=await g2("git",t,{cwd:e,maxBuffer:4194304});return{stdout:n.stdout.trim(),stderr:n.stderr.trim()}}catch(n){let r=n,o=(r.stderr??r.stdout??r.message??"").toString().trim();throw new Qe(`git ${t.join(" ")} failed: ${o}`,n)}}async function w2(e){let{stdout:t}=await Cr(e,["rev-parse","--show-toplevel"]);if(!t)throw new Qe(`not a git repository: ${e}`);return t}async function S2(e,t){if(t){let{stdout:o}=await Cr(e,["rev-parse",t]);return{sha:o}}let{stdout:n}=await Cr(e,["rev-parse","HEAD"]),r;try{let{stdout:o}=await Cr(e,["symbolic-ref","--quiet","HEAD"]);o&&(r=o)}catch{}return{sha:n,branch:r}}function k2(e,t,n){let r=n?af(n,32):`branch-${t}`;return`afk/farm/${e}/${t}-${r}`}function v2(e,t){return Fc(e,`branch-${t}`)}async function T2(e,t){try{await Cr(e,["worktree","remove","--force",t])}catch{}}async function x2(e,t){try{await Cr(e,["branch","-D",t])}catch{}}async function KE(e){if(e.count<1||e.count>HE)throw new Qe(`count must be between 1 and ${HE}, got ${e.count}`);if(e.labels&&e.labels.length!==e.count)throw new Qe(`labels.length (${e.labels.length}) must equal count (${e.count})`);let t=e.cwd??process.cwd(),n=await w2(t),{sha:r,branch:o}=await S2(n,e.baseRef),s=(e.now??(()=>new Date))(),i=e.taskSlug??b2(e.taskName,{now:()=>s,randomSuffix:e.randomSuffix}),a=e.taskSlug??i,l=lu(i);try{throw await Zo.access(l),new Qe(`farm directory already exists: ${l}`)}catch(d){if(d.code!=="ENOENT")throw d instanceof Qe?d:new Qe(`failed to check farm dir ${l}`,d)}await Zo.mkdir(l,{recursive:!0});let c=[];try{for(let d=1;d<=e.count;d++){let m=e.labels?.[d-1],f=k2(i,d,m),g=v2(l,d);await Cr(n,["worktree","add","-b",f,g,r]),c.push({index:d,label:m?af(m,32):void 0,path:g,branch:f})}}catch(d){for(let m of c.slice().reverse())await T2(n,m.path),await x2(n,m.branch);throw await Zo.rm(l,{recursive:!0,force:!0}).catch(()=>{}),d instanceof Qe?d:new Qe("farm creation failed",d)}let u={schemaVersion:3,taskId:a,taskSlug:i,taskName:e.taskName,repoRoot:n,baseRef:r,baseBranch:o,farmDir:l,createdAt:s.toISOString(),branches:c};return await Zo.writeFile(Fc(l,"farm.json"),JSON.stringify(u,null,2)+`
2385
+ `))}),t.command("test-fetch").description("One-shot: GET /me/mentions, print payload (verifies token + filtering, no agent dispatch)").option("--limit <n>","Max events to fetch","5").option("--no-filter","Skip allowlist + self-loop filter (dump every event)").action(async n=>{let r=LE();r.kind==="missing"&&(console.error(Q.red("\u2717 "+r.reason)),process.exit(1));let o=r.token;console.log(Q.gray(`Token: ${r.kind==="file"?r.path:"THREADS_ACCESS_TOKEN env"}`));let s;try{s=await NE(o)}catch(p){p instanceof kt?console.error(Q.red(`\u2717 /me failed (${p.detail.kind}): ${p.detail.message}`)):console.error(Q.red(`\u2717 /me failed: ${p.message}`)),process.exit(1)}console.log(Q.gray(`Identity: @${s.username} (id ${s.id})`));let i=UE(T.AFK_THREADS_ALLOWED_USERNAMES),a=n.filter!==!1;a?i.size===0?(console.log(Q.yellow("\u26A0 AFK_THREADS_ALLOWED_USERNAMES is empty \u2014 all events will be filtered out.")),console.log(Q.gray(" Re-run with --no-filter to see raw payload."))):console.log(Q.gray(`Allowlist: ${[...i].map(p=>"@"+p).join(", ")}`)):console.log(Q.gray("Filter: OFF (dumping raw payload)")),console.log("");let l=Number.parseInt(n.limit??"5",10),c;try{c=await BE({accessToken:o,limit:l})}catch(p){p instanceof kt?console.error(Q.red(`\u2717 /me/mentions failed (${p.detail.kind}): ${p.detail.message}`)):console.error(Q.red(`\u2717 /me/mentions failed: ${p.message}`)),process.exit(1)}if(c.length===0){console.log(Q.gray("No mentions returned.")),console.log(Q.gray("Tag your account in a post (or have someone in the allowlist do it), then re-run."));return}let u=0,d=0;for(let p of c){let f=p.username.toLowerCase()===s.username.toLowerCase(),g=jE(i,p.username),h=!a||!f&&g,b=h?Q.green("\u2713 KEEP "):Q.gray("\u2717 FILTER "),y=h?"":f?" (self-loop)":g?"":" (not allowlisted)";console.log(`${b}@${p.username} ${Q.gray("\u2022")} ${p.timestamp}${Q.gray(y)}`),console.log(Q.gray(` id=${p.id}`)),console.log(` ${p.text.slice(0,200)}${p.text.length>200?"\u2026":""}`),p.permalink&&console.log(Q.gray(` ${p.permalink}`)),console.log(""),h?u++:d++}console.log(Q.bold(`Summary: ${u} kept, ${d} filtered (of ${c.length} fetched)`))})}function u2(e){if(console.log(Q.bold("\u{1F4CA} Threads Poller Status")),console.log(""),e.running?(console.log(` ${Q.green("\u25CF")} Running (PID ${e.pid})`),e.uptimeSec!==void 0&&console.log(` Uptime: ${d2(e.uptimeSec)}`),e.memoryMb!==void 0&&console.log(` Memory: ${e.memoryMb} MB`)):console.log(` ${Q.red("\u25CF")} Stopped`),console.log(` PID: ${e.pidFile}`),console.log(` Logs: ${e.logFile}`),e.logTail&&e.logTail.length>0){console.log(""),console.log(Q.bold("Recent log entries:"));for(let t of e.logTail)console.log(Q.gray(` ${t}`))}}function d2(e){if(e<60)return`${e}s`;let t=Math.floor(e/60);if(t<60)return`${t}m ${e%60}s`;let n=Math.floor(t/60);return n<24?`${n}h ${t%60}m`:`${Math.floor(n/24)}d ${n%24}h`}import{execFile as U2}from"node:child_process";import{promisify as j2}from"node:util";import pe from"chalk";W();import{execFile as p2}from"node:child_process";import{randomBytes as m2}from"node:crypto";import{promises as Zo}from"node:fs";import{join as Fc}from"node:path";import{promisify as f2}from"node:util";var g2=f2(p2),HE=16;var Qe=class extends Error{cause;code;constructor(t,n,r){super(t),this.name="WorktreeError",this.cause=n,this.code=r}};function af(e,t=40){return e.toLowerCase().trim().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"").slice(0,t).replace(/-+$/g,"")||"task"}function h2(){return m2(4).toString("hex").slice(0,4)}function y2(e){let t=n=>String(n).padStart(2,"0");return`${e.getUTCFullYear()}${t(e.getUTCMonth()+1)}${t(e.getUTCDate())}T${t(e.getUTCHours())}${t(e.getUTCMinutes())}${t(e.getUTCSeconds())}`}function b2(e,t={}){let n=(t.now??(()=>new Date))(),r=(t.randomSuffix??h2)();return`${y2(n)}-${af(e,32)}-${r}`}async function Cr(e,t){try{let n=await g2("git",t,{cwd:e,maxBuffer:4194304});return{stdout:n.stdout.trim(),stderr:n.stderr.trim()}}catch(n){let r=n,o=(r.stderr??r.stdout??r.message??"").toString().trim();throw new Qe(`git ${t.join(" ")} failed: ${o}`,n)}}async function w2(e){let{stdout:t}=await Cr(e,["rev-parse","--show-toplevel"]);if(!t)throw new Qe(`not a git repository: ${e}`);return t}async function S2(e,t){if(t){let{stdout:o}=await Cr(e,["rev-parse",t]);return{sha:o}}let{stdout:n}=await Cr(e,["rev-parse","HEAD"]),r;try{let{stdout:o}=await Cr(e,["symbolic-ref","--quiet","HEAD"]);o&&(r=o)}catch{}return{sha:n,branch:r}}function k2(e,t,n){let r=n?af(n,32):`branch-${t}`;return`afk/farm/${e}/${t}-${r}`}function v2(e,t){return Fc(e,`branch-${t}`)}async function T2(e,t){try{await Cr(e,["worktree","remove","--force",t])}catch{}}async function x2(e,t){try{await Cr(e,["branch","-D",t])}catch{}}async function KE(e){if(e.count<1||e.count>HE)throw new Qe(`count must be between 1 and ${HE}, got ${e.count}`);if(e.labels&&e.labels.length!==e.count)throw new Qe(`labels.length (${e.labels.length}) must equal count (${e.count})`);let t=e.cwd??process.cwd(),n=await w2(t),{sha:r,branch:o}=await S2(n,e.baseRef),s=(e.now??(()=>new Date))(),i=e.taskSlug??b2(e.taskName,{now:()=>s,randomSuffix:e.randomSuffix}),a=e.taskSlug??i,l=lu(i);try{throw await Zo.access(l),new Qe(`farm directory already exists: ${l}`)}catch(d){if(d.code!=="ENOENT")throw d instanceof Qe?d:new Qe(`failed to check farm dir ${l}`,d)}await Zo.mkdir(l,{recursive:!0});let c=[];try{for(let d=1;d<=e.count;d++){let p=e.labels?.[d-1],f=k2(i,d,p),g=v2(l,d);await Cr(n,["worktree","add","-b",f,g,r]),c.push({index:d,label:p?af(p,32):void 0,path:g,branch:f})}}catch(d){for(let p of c.slice().reverse())await T2(n,p.path),await x2(n,p.branch);throw await Zo.rm(l,{recursive:!0,force:!0}).catch(()=>{}),d instanceof Qe?d:new Qe("farm creation failed",d)}let u={schemaVersion:3,taskId:a,taskSlug:i,taskName:e.taskName,repoRoot:n,baseRef:r,baseBranch:o,farmDir:l,createdAt:s.toISOString(),branches:c};return await Zo.writeFile(Fc(l,"farm.json"),JSON.stringify(u,null,2)+`
2386
2386
  `,"utf8"),u}function E2(e){let t=e;return t.respawnedAt===void 0&&(t.respawnedAt=void 0),t.respawnedAs===void 0&&(t.respawnedAs=void 0),t.prUrl===void 0&&(t.prUrl=void 0),t.prCreatedAt===void 0&&(t.prCreatedAt=void 0),t}async function R2(e){let t=Fc(lu(e),"farm.json");try{let n=await Zo.readFile(t,"utf8"),r=JSON.parse(n);if(r.schemaVersion!==1&&r.schemaVersion!==2&&r.schemaVersion!==3)throw new Qe(`unsupported farm manifest schema: ${r.schemaVersion} (expected 1, 2, or 3)`,void 0,"unsupported-schema");return E2(r)}catch(n){if(n.code==="ENOENT")return null;throw n instanceof Qe?n:new Qe(`failed to load farm manifest ${t}`,n,"invalid")}}async function GE(e,t){let n=await R2(e);if(!n)throw new Qe(`farm not found: ${e}`);return n.memoryFactId=t,n.schemaVersion=3,await Zo.writeFile(Fc(n.farmDir,"farm.json"),JSON.stringify(n,null,2)+`
2387
- `,"utf8"),n}import{spawn as A2}from"child_process";import{promises as Nc}from"fs";import{join as wi,dirname as wSe}from"path";var _2=1,Bc=12e4;async function VE(e){let{branchPath:t,baseSha:n,testCmd:r,timeoutMs:o=Bc,_spawn:s=A2,_readPackageJson:i=C2,_now:a=Date.now,_nowIso:l=()=>new Date().toISOString()}=e,c=r??await I2(t,i),u=0,d=0,m=0,f;if(c){let y=await XE(c,t,o,s,a);m=y.durationMs,y.timedOut?(d=1,f=`tests timed out after ${o}ms`):y.crashed?(d=1,f=`test runner crashed: ${O2(y.stderr,200)}`):y.exitCode===0?u=1:d=1}else f="no test command found (no package.json scripts.test)";let g=await P2(t,o,s,a),h=await M2(t,n,s),b={schemaVersion:_2,pass:u,fail:d,loc_delta:h,lint_ok:g,duration_ms:m,branchPath:t,baseSha:n,scoredAt:l()};return f!==void 0&&(b.error=f),c!==void 0&&(b.testCmd=c),b}async function YE(e,t,n){let r=wi(e,"scores");await Nc.mkdir(r,{recursive:!0});let o=wi(r,`branch-${t}.json`);return await Nc.writeFile(o,JSON.stringify(n,null,2)+`
2388
- `,"utf8"),o}function Si(e){let t=e.filter(r=>r.score!==null),n=e.filter(r=>r.score===null).map(r=>r.index).sort((r,o)=>r-o);return t.sort((r,o)=>{let s=r.score,i=o.score,a=qE(s),l=qE(i);if(a!==l)return l-a;let c=zE(s.lint_ok),u=zE(i.lint_ok);return c!==u?u-c:s.loc_delta!==i.loc_delta?s.loc_delta-i.loc_delta:r.index-o.index}),[...t.map(r=>r.index),...n]}async function C2(e){try{let t=await Nc.readFile(e,"utf8");return JSON.parse(t)}catch{return null}}async function I2(e,t){let n=await t(wi(e,"package.json"));if(!JE(n))return;let r=n.scripts;return!JE(r)||typeof r.test!="string"?void 0:await ZE(wi(e,"pnpm-lock.yaml"))?"pnpm test":"npm test"}async function XE(e,t,n,r,o){let s=o();return new Promise(i=>{let a;try{a=r(e,{cwd:t,shell:!0,stdio:["ignore","pipe","pipe"],env:{...process.env,CI:"1"}})}catch(m){i({exitCode:null,durationMs:o()-s,stderr:m instanceof Error?m.message:String(m),timedOut:!1,crashed:!0});return}let l="",c=!1,u=!1,d=setTimeout(()=>{c=!0,a.kill("SIGKILL")},n);a.stderr?.on("data",m=>{l+=String(m),l.length>64e3&&(l=l.slice(-32e3))}),a.on("error",m=>{u||(u=!0,clearTimeout(d),i({exitCode:null,durationMs:o()-s,stderr:m.message,timedOut:!1,crashed:!0}))}),a.on("close",m=>{u||(u=!0,clearTimeout(d),i({exitCode:m,durationMs:o()-s,stderr:l,timedOut:c,crashed:!1}))})})}async function P2(e,t,n,r){if(!await ZE(wi(e,"tsconfig.json")))return null;let o=await XE("npx --no-install tsc --noEmit",e,t,n,r);return o.crashed||o.timedOut?null:o.exitCode===0}async function M2(e,t,n){return new Promise(r=>{let o;try{o=n("git",["diff","--shortstat",`${t}..HEAD`],{cwd:e,stdio:["ignore","pipe","pipe"]})}catch{r(0);return}let s="";o.stdout?.on("data",i=>{s+=String(i)}),o.on("error",()=>r(0)),o.on("close",()=>{let i=/(\d+) insertions?\(\+\)/.exec(s),a=/(\d+) deletions?\(-\)/.exec(s),l=i?Number(i[1]):0,c=a?Number(a[1]):0;r(l-c)})})}function qE(e){let t=e.pass+e.fail;return t===0?0:e.pass/t}function zE(e){return e===!0?2:e===!1?1:0}function O2(e,t){return e.length<=t?e:e.slice(0,t)+"\u2026"}function JE(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}async function ZE(e){try{return await Nc.access(e),!0}catch{return!1}}function QE(e,t){let n;try{n=t?._store??new We}catch(r){return{skipped:!0,reason:r instanceof Error?r.message:String(r)}}try{let r=$2(e);return{factId:n.storeFact({category:"learning",content:JSON.stringify(r),source_surface:"afk"})}}catch(r){return{skipped:!0,reason:r instanceof Error?r.message:String(r)}}}function $2(e){let t=D2(e.branches),n=e.winner??null,r=L2(e,t);return{type:"farm-run",task:e.taskName,taskSlug:e.taskSlug,winner:n,why:r,scores:t,human_decision:e.human_decision??null,baseSha:e.baseSha,completedAt:e.completedAt}}function D2(e){return e.map(t=>{let n=t.score??null;return{index:t.index,branch:t.branch,pass:n?.pass??0,fail:n?.fail??0,loc_delta:n?.loc_delta??0,lint_ok:n?.lint_ok??null,duration_ms:n?.duration_ms??0}})}function L2(e,t){let{winner:n,branches:r}=e;if(n==null)return r.some(m=>m.score!=null)?`no winner: all ${r.length} branches failed tests`:"no winner: scoring data unavailable";let o=t.find(d=>d.index===n);if(!o)return`branch-${n} wins`;let s=o.pass>0?"\u2713":"\u2717",i=o.lint_ok===!0?"\u2713":o.lint_ok===!1?"\u2717":"?",a=o.loc_delta>=0?`+${o.loc_delta}`:String(o.loc_delta),c=t.filter(d=>d.index!==n).map(d=>d.loc_delta>=0?`+${d.loc_delta}`:String(d.loc_delta)),u=c.length>0?` (vs ${c.join(", ")} LoC)`:"";return`branch-${n} wins: tests${s}, lint${i}, ${a} LoC${u}`}var F2="afk:f:";var N2=/^[a-z0-9T][a-z0-9T-]{0,62}$/;function ki(e,t){if(!N2.test(t))throw new Error(`buildFarmCallback: invalid taskSlug ${JSON.stringify(t)}`);let n=`${F2}${e}:${t}`,r=Buffer.byteLength(n,"utf8");if(r>64)throw new Error(`buildFarmCallback: payload ${r} bytes exceeds Telegram's 64-byte limit (slug=${t})`);return n}function eR(e){return{inline_keyboard:[[{text:"\u2705 Open PR",callback_data:ki("p",e)},{text:"\u{1F501} Respawn from winner",callback_data:ki("r",e)}],[{text:"\u{1F50D} Full diff",callback_data:ki("d",e)},{text:"\u274C Discard all",callback_data:ki("x",e)}]]}}function B2(e){let{taskName:t,taskSlug:n,baseSha:r,branches:o,winner:s}=e,i=o.filter(y=>y.ok),a=o.filter(y=>!y.ok),l=o.length,c=i.length,u=i.map(y=>({index:y.index,score:y.score??null})),d=Si(u),m=new Map(o.map(y=>[y.index,y])),f=[];f.push(`\u{1F331} Farm complete: ${c}/${l} branches \u2014 ${t}`),f.push("");let g=1;for(let y of d){let w=m.get(y);if(!w)continue;let x=s!==void 0&&s===w.index,R=w.label?` (${w.label})`:"",v=w.score??null,E=v===null?"\u2014":v.pass>0?"\u2713":"\u2717",I=v===null?"\u2014":v.lint_ok===!0?"\u2713":v.lint_ok===!1?"\u2717":"?",D=v===null?"?":v.loc_delta>0?`+${v.loc_delta}`:v.loc_delta<0?`${v.loc_delta}`:"0",_=x?" \u2190 winner":"";f.push(`#${g} ${w.branch}${R} tests${E} lint${I} ${D} LoC${_}`),g++}let h=[...a].sort((y,w)=>y.index-w.index);for(let y of h){let w=y.label?` (${y.label})`:"",x=y.error??"unknown error";f.push(`#${g} ${y.branch}${w} failed: ${x}`),g++}f.push(""),s===void 0&&(f.push("\u26A0 no branch won (no successful + scored branches)"),f.push(""));let b=r.slice(0,7);return f.push(`base: ${b}`),f.push(`farm: ~/.afk/farms/${n}/`),f.join(`
2387
+ `,"utf8"),n}import{spawn as A2}from"child_process";import{promises as Nc}from"fs";import{join as wi,dirname as wSe}from"path";var _2=1,Bc=12e4;async function VE(e){let{branchPath:t,baseSha:n,testCmd:r,timeoutMs:o=Bc,_spawn:s=A2,_readPackageJson:i=C2,_now:a=Date.now,_nowIso:l=()=>new Date().toISOString()}=e,c=r??await I2(t,i),u=0,d=0,p=0,f;if(c){let y=await XE(c,t,o,s,a);p=y.durationMs,y.timedOut?(d=1,f=`tests timed out after ${o}ms`):y.crashed?(d=1,f=`test runner crashed: ${O2(y.stderr,200)}`):y.exitCode===0?u=1:d=1}else f="no test command found (no package.json scripts.test)";let g=await P2(t,o,s,a),h=await M2(t,n,s),b={schemaVersion:_2,pass:u,fail:d,loc_delta:h,lint_ok:g,duration_ms:p,branchPath:t,baseSha:n,scoredAt:l()};return f!==void 0&&(b.error=f),c!==void 0&&(b.testCmd=c),b}async function YE(e,t,n){let r=wi(e,"scores");await Nc.mkdir(r,{recursive:!0});let o=wi(r,`branch-${t}.json`);return await Nc.writeFile(o,JSON.stringify(n,null,2)+`
2388
+ `,"utf8"),o}function Si(e){let t=e.filter(r=>r.score!==null),n=e.filter(r=>r.score===null).map(r=>r.index).sort((r,o)=>r-o);return t.sort((r,o)=>{let s=r.score,i=o.score,a=qE(s),l=qE(i);if(a!==l)return l-a;let c=zE(s.lint_ok),u=zE(i.lint_ok);return c!==u?u-c:s.loc_delta!==i.loc_delta?s.loc_delta-i.loc_delta:r.index-o.index}),[...t.map(r=>r.index),...n]}async function C2(e){try{let t=await Nc.readFile(e,"utf8");return JSON.parse(t)}catch{return null}}async function I2(e,t){let n=await t(wi(e,"package.json"));if(!JE(n))return;let r=n.scripts;return!JE(r)||typeof r.test!="string"?void 0:await ZE(wi(e,"pnpm-lock.yaml"))?"pnpm test":"npm test"}async function XE(e,t,n,r,o){let s=o();return new Promise(i=>{let a;try{a=r(e,{cwd:t,shell:!0,stdio:["ignore","pipe","pipe"],env:{...process.env,CI:"1"}})}catch(p){i({exitCode:null,durationMs:o()-s,stderr:p instanceof Error?p.message:String(p),timedOut:!1,crashed:!0});return}let l="",c=!1,u=!1,d=setTimeout(()=>{c=!0,a.kill("SIGKILL")},n);a.stderr?.on("data",p=>{l+=String(p),l.length>64e3&&(l=l.slice(-32e3))}),a.on("error",p=>{u||(u=!0,clearTimeout(d),i({exitCode:null,durationMs:o()-s,stderr:p.message,timedOut:!1,crashed:!0}))}),a.on("close",p=>{u||(u=!0,clearTimeout(d),i({exitCode:p,durationMs:o()-s,stderr:l,timedOut:c,crashed:!1}))})})}async function P2(e,t,n,r){if(!await ZE(wi(e,"tsconfig.json")))return null;let o=await XE("npx --no-install tsc --noEmit",e,t,n,r);return o.crashed||o.timedOut?null:o.exitCode===0}async function M2(e,t,n){return new Promise(r=>{let o;try{o=n("git",["diff","--shortstat",`${t}..HEAD`],{cwd:e,stdio:["ignore","pipe","pipe"]})}catch{r(0);return}let s="";o.stdout?.on("data",i=>{s+=String(i)}),o.on("error",()=>r(0)),o.on("close",()=>{let i=/(\d+) insertions?\(\+\)/.exec(s),a=/(\d+) deletions?\(-\)/.exec(s),l=i?Number(i[1]):0,c=a?Number(a[1]):0;r(l-c)})})}function qE(e){let t=e.pass+e.fail;return t===0?0:e.pass/t}function zE(e){return e===!0?2:e===!1?1:0}function O2(e,t){return e.length<=t?e:e.slice(0,t)+"\u2026"}function JE(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}async function ZE(e){try{return await Nc.access(e),!0}catch{return!1}}function QE(e,t){let n;try{n=t?._store??new We}catch(r){return{skipped:!0,reason:r instanceof Error?r.message:String(r)}}try{let r=$2(e);return{factId:n.storeFact({category:"learning",content:JSON.stringify(r),source_surface:"afk"})}}catch(r){return{skipped:!0,reason:r instanceof Error?r.message:String(r)}}}function $2(e){let t=D2(e.branches),n=e.winner??null,r=L2(e,t);return{type:"farm-run",task:e.taskName,taskSlug:e.taskSlug,winner:n,why:r,scores:t,human_decision:e.human_decision??null,baseSha:e.baseSha,completedAt:e.completedAt}}function D2(e){return e.map(t=>{let n=t.score??null;return{index:t.index,branch:t.branch,pass:n?.pass??0,fail:n?.fail??0,loc_delta:n?.loc_delta??0,lint_ok:n?.lint_ok??null,duration_ms:n?.duration_ms??0}})}function L2(e,t){let{winner:n,branches:r}=e;if(n==null)return r.some(p=>p.score!=null)?`no winner: all ${r.length} branches failed tests`:"no winner: scoring data unavailable";let o=t.find(d=>d.index===n);if(!o)return`branch-${n} wins`;let s=o.pass>0?"\u2713":"\u2717",i=o.lint_ok===!0?"\u2713":o.lint_ok===!1?"\u2717":"?",a=o.loc_delta>=0?`+${o.loc_delta}`:String(o.loc_delta),c=t.filter(d=>d.index!==n).map(d=>d.loc_delta>=0?`+${d.loc_delta}`:String(d.loc_delta)),u=c.length>0?` (vs ${c.join(", ")} LoC)`:"";return`branch-${n} wins: tests${s}, lint${i}, ${a} LoC${u}`}var F2="afk:f:";var N2=/^[a-z0-9T][a-z0-9T-]{0,62}$/;function ki(e,t){if(!N2.test(t))throw new Error(`buildFarmCallback: invalid taskSlug ${JSON.stringify(t)}`);let n=`${F2}${e}:${t}`,r=Buffer.byteLength(n,"utf8");if(r>64)throw new Error(`buildFarmCallback: payload ${r} bytes exceeds Telegram's 64-byte limit (slug=${t})`);return n}function eR(e){return{inline_keyboard:[[{text:"\u2705 Open PR",callback_data:ki("p",e)},{text:"\u{1F501} Respawn from winner",callback_data:ki("r",e)}],[{text:"\u{1F50D} Full diff",callback_data:ki("d",e)},{text:"\u274C Discard all",callback_data:ki("x",e)}]]}}function B2(e){let{taskName:t,taskSlug:n,baseSha:r,branches:o,winner:s}=e,i=o.filter(y=>y.ok),a=o.filter(y=>!y.ok),l=o.length,c=i.length,u=i.map(y=>({index:y.index,score:y.score??null})),d=Si(u),p=new Map(o.map(y=>[y.index,y])),f=[];f.push(`\u{1F331} Farm complete: ${c}/${l} branches \u2014 ${t}`),f.push("");let g=1;for(let y of d){let w=p.get(y);if(!w)continue;let x=s!==void 0&&s===w.index,R=w.label?` (${w.label})`:"",v=w.score??null,E=v===null?"\u2014":v.pass>0?"\u2713":"\u2717",I=v===null?"\u2014":v.lint_ok===!0?"\u2713":v.lint_ok===!1?"\u2717":"?",D=v===null?"?":v.loc_delta>0?`+${v.loc_delta}`:v.loc_delta<0?`${v.loc_delta}`:"0",_=x?" \u2190 winner":"";f.push(`#${g} ${w.branch}${R} tests${E} lint${I} ${D} LoC${_}`),g++}let h=[...a].sort((y,w)=>y.index-w.index);for(let y of h){let w=y.label?` (${y.label})`:"",x=y.error??"unknown error";f.push(`#${g} ${y.branch}${w} failed: ${x}`),g++}f.push(""),s===void 0&&(f.push("\u26A0 no branch won (no successful + scored branches)"),f.push(""));let b=r.slice(0,7);return f.push(`base: ${b}`),f.push(`farm: ~/.afk/farms/${n}/`),f.join(`
2389
2389
  `)}async function tR(e,t){let n=B2(e),r=eR(e.taskSlug),o=t?._push??await Promise.resolve().then(()=>(ws(),ju)).then(s=>s.pushIfConfigured);try{let s=await o(n,{replyMarkup:r});return s===null?{sent:!1,reason:"telegram unconfigured"}:{sent:!0,chatCount:s.length}}catch(s){return{sent:!1,reason:s instanceof Error?s.message:String(s)}}}var nR=j2(U2);async function W2(e,t){try{let{stdout:n}=await nR("git",["-C",e,"rev-list",`${t}..HEAD`,"--count"],{maxBuffer:4194304});return parseInt(n.trim(),10)||0}catch{return 0}}async function H2(e){try{let{stdout:t}=await nR("git",["-C",e,"status","--porcelain"],{maxBuffer:4194304});return t.trim()?t.trim().split(`
2390
2390
  `).filter(Boolean):[]}catch{return[]}}var lf=class extends Error{dirtyFiles;constructor(t){super(`Source repository has uncommitted changes after farm run. Dirty files:
2391
2391
  ${t.map(n=>` ${n}`).join(`
2392
- `)}`),this.name="FarmIsolationViolation",this.dirtyFiles=t}};function K2(e,t){return e.length>=t?e:e+" ".repeat(t-e.length)}function G2(e){if(e===void 0)return pe.dim("\u2014");if(e===null)return pe.dim("skipped");let t=e.fail===0&&e.pass>0?pe.green("tests\u2713"):pe.red("tests\u2717"),n=e.lint_ok===!0?pe.green("lint\u2713"):e.lint_ok===!1?pe.red("lint\u2717"):pe.dim("lint?"),r=e.loc_delta>0?"+":"",o=pe.dim(`${r}${e.loc_delta} LoC`);return`${t} ${n} ${o}`}function q2(e,t,n,r){let o="\u2500".repeat(45);console.log(pe.dim(o)),console.log(`farm: ${e}`),console.log(`slug: ${t}`),console.log("");let s=r.some(u=>u.score!=null),i=s?Si(r.map(u=>({index:u.index,score:u.score??null}))).map(u=>r.find(d=>d.index===u)):r;for(let u=0;u<i.length;u++){let d=i[u],m=n.find(w=>w.index===d.index),f=d.ok?pe.green("\u2713"):pe.red("\u2717"),g=K2(m.branch,40),h=d.ok?pe.dim(`(${d.commitCount} commit${d.commitCount===1?"":"s"})`):pe.red(`[error: ${d.error}]`),b=s?pe.cyan(`#${u+1} `):"",y=s?` ${G2(d.score)}`:"";console.log(`${b}branch-${d.index} ${f} ${g} ${h}${y}`),console.log(pe.dim(` worktree: ${m.path}`))}console.log(pe.dim(o));let a=r.filter(u=>u.ok).length,l=r.length;console.log(`${a}/${l} branches completed.`);let c=r.some(u=>u.score!=null&&u.score.pass>0);s&&!c&&console.log(pe.yellow("\u26A0 no branch passed tests \u2014 ranking falls back to lint + LoC"))}function z2(e,t,n){let r=t.map(a=>{let l=e.branches.find(u=>u.index===a.index),c={index:a.index,branch:l?.branch??`(unknown-${a.index})`,ok:a.ok,commitCount:a.commitCount};return l?.label!==void 0&&(c.label=l.label),a.error!==void 0&&(c.error=a.error),a.score!==void 0&&(c.score=a.score),c}),o=Si(t.map(a=>({index:a.index,score:a.score??null}))),s;for(let a of o){let l=t.find(c=>c.index===a);if(!(!l||!l.ok||!l.score)&&l.score.pass>0&&l.score.fail===0){s=a;break}}if(s===void 0)for(let a of o){let l=t.find(c=>c.index===a);if(l?.ok&&l.score){s=a;break}}let i={taskName:e.taskName,taskSlug:e.taskSlug,baseSha:e.baseRef,startedAt:n,completedAt:new Date().toISOString(),branches:r};return s!==void 0&&(i.winner=s),e.human_decision!==void 0&&(i.human_decision=e.human_decision),i}async function J2(e){let{task:t,branches:n,labels:r,model:o,baseRef:s,cwd:i=process.cwd(),failFast:a,taskSlug:l,score:c=!0,scoreTimeoutMs:u=Bc,memoryWrite:d=!0,digest:m=!0,_createFarm:f=KE,_runSubagentDAG:g=Ga,_getCommitCount:h=W2,_getSourceRepoDirtyFiles:b=H2,_scoreBranch:y=VE,_writeScore:w=YE,_writeFarmFact:x=QE,_sendFarmDigest:R=tR,_setFarmMemoryFactId:v=GE}=e,E=new Date().toISOString();(!Number.isInteger(n)||n<1||n>16)&&(console.error(pe.red(`--branches must be between 1 and 16 (got ${n})`)),process.exit(1)),r!==void 0&&r.length!==n&&(console.error(pe.red(`--labels count (${r.length}) must equal --branches (${n})`)),process.exit(1));let I;try{I=await f({taskName:t,count:n,labels:r,cwd:i,baseRef:s,taskSlug:l})}catch(B){console.error(pe.red(`Farm creation failed: ${B instanceof Error?B.message:String(B)}`)),process.exit(1)}let D=I.baseRef,_=o??Xe(),M=uo()??co()??"",P=I.branches.map(B=>({id:`branch-${B.index}`,agentType:`branch-${B.index}${B.label?` (${B.label})`:""}`,systemPrompt:M,promptBuilder:j=>(console.log(`[branch-${B.index}] started`),[`Task: ${t}`,"",`You are working in a dedicated git worktree. Your working directory has been set to: ${B.path}`,`Your branch is: ${B.branch}`,"","Complete the task. All file operations are restricted to this worktree by the runtime."].join(`
2393
- `)),model:_,idPrefix:`farm-${I.taskSlug}-branch-${B.index}`,cwd:B.path,readRoots:[B.path],writeRoots:[B.path]})),$=new AbortController,F=new X({parentAbortSignal:$.signal}),k={sessionId:`farm-${I.taskSlug}`,abortSignal:$.signal},A;try{A=await g({manager:F,parentSession:k,nodes:P,edges:[],failFast:a})}catch(B){throw console.error(pe.red(`Farm dispatch failed: ${B instanceof Error?B.message:String(B)}`)),B}finally{$.abort()}let N=[];for(let B of I.branches){let j=A.failed.find(L=>L.id===`branch-${B.index}`),ee=A.skipped.includes(`branch-${B.index}`);if(j||ee){let L=j?j.error.message:"skipped";console.log(`[branch-${B.index}] \u2717 failed: ${L}`),N.push({index:B.index,ok:!1,commitCount:0,error:L});continue}let q=await h(B.path,D);if(q===0){let L="no commits made";console.log(`[branch-${B.index}] \u2717 failed: ${L}`),N.push({index:B.index,ok:!1,commitCount:0,error:L})}else console.log(`[branch-${B.index}] \u2713 done`),N.push({index:B.index,ok:!0,commitCount:q})}let H=await b(i);if(c)for(let B of N){if(!B.ok){B.score=null;continue}let j=I.branches.find(q=>q.index===B.index);console.log(`[branch-${B.index}] scoring\u2026`);let ee=await y({branchPath:j.path,baseSha:D,timeoutMs:u});B.score=ee;try{await w(I.farmDir,B.index,ee)}catch(q){console.error(pe.yellow(`[branch-${B.index}] score.json write failed: ${q instanceof Error?q.message:String(q)}`))}}if(q2(t,I.taskSlug,I.branches,N),d||m){let B=z2(I,N,E);if(d){let j=x(B);if("skipped"in j)console.error(pe.yellow(`[memory] write skipped: ${j.reason}`));else{let{factId:ee}=j;try{await v(I.taskSlug,ee)}catch(q){console.error(pe.yellow(`[memory] setFarmMemoryFactId failed: ${q.message}`))}}}if(m){let j=await R(B);j.sent?console.log(pe.dim(`[telegram] digest sent (${j.chatCount} chat${j.chatCount===1?"":"s"})`)):j.reason&&j.reason!=="telegram unconfigured"&&console.error(pe.yellow(`[telegram] digest failed: ${j.reason}`))}}if(H.length>0){let B=new lf(H);console.error(pe.red(`
2394
- \u26A0 ISOLATION VIOLATION`)),console.error(pe.red(B.message)),process.exit(1)}let C=N.every(B=>B.ok);process.exit(C?0:1)}function rR(e){e.command("farm").description("Run a task across N speculative git worktree branches in parallel").argument("<task>","Task description to run on each branch").option("-n, --branches <number>","Number of branches to spawn (1-16)","3").option("--labels <labels>","Comma-separated branch labels (count must equal --branches)").option("-m, --model <model>","Model to use",Xe()).option("--base-ref <ref>","Base git ref (default: HEAD)").option("--cwd <path>","Source repo root (default: process.cwd())").option("--fail-fast","Abort remaining branches on first failure",!1).option("--task-slug <slug>","Deterministic task slug override (for tests)").option("--no-score","Skip the post-run scorer (tests + lint + LoC)").option("--score-timeout <ms>",`Per-branch test timeout in ms (default ${Bc})`).option("--no-memory","Skip writing the farm-run fact to cross-session memory").option("--no-digest","Skip pushing the Telegram digest on completion").action(async(t,n)=>{let r=parseInt(n.branches,10),o=n.labels?n.labels.split(",").map(i=>i.trim()).filter(Boolean):void 0,s=n.scoreTimeout?parseInt(n.scoreTimeout,10):void 0;s!==void 0&&(!Number.isFinite(s)||s<1)&&(console.error(pe.red(`--score-timeout must be a positive integer (got "${n.scoreTimeout}")`)),process.exit(1));try{await J2({task:t,branches:r,labels:o,model:n.model,baseRef:n.baseRef,cwd:n.cwd,failFast:n.failFast,taskSlug:n.taskSlug,score:n.score,memoryWrite:n.memory,digest:n.digest,...s!==void 0?{scoreTimeoutMs:s}:{}})}catch(i){console.error(i),process.exitCode=1}})}G();import Ot from"chalk";import{execFile as V2}from"node:child_process";import{promisify as Y2}from"node:util";var cf=Y2(V2);async function oR(){try{return(await cf("git",["rev-parse","--show-toplevel"])).stdout.trim()}catch{throw new Error("Not in a git repository.")}}function X2(e){return["empty","stale-clean","orphaned-dir","orphaned-registration","dead-owner"].includes(e)?Ot.red("yes"):e==="stale-dirty"?Ot.yellow("warn"):Ot.green("no")}var sR=["interactive","diagnose","all"];function Z2(e){if(sR.includes(e))return e;throw new Error(`Invalid --scope value: '${e}'. Allowed: ${sR.join(" | ")}.`)}function Q2(e){if(e<=0)return"-";let t=e/864e5;return t<1?`${Math.max(1,Math.round(e/36e5))}h`:`${Math.round(t)}d`}function iR(e){let t=e.command("worktree").description("Manage git worktrees created by afk");t.command("list").description("List all afk-managed worktrees and show prune candidates (dry-run only)").action(async()=>{let n;try{n=await oR()}catch(s){K(s)}let r;try{r=await zt({execFile:cf,repoRoot:n,dryRun:!0})}catch(s){K(new Error(`Sweep failed: ${s.message}`))}let o=["PATH".padEnd(45),"OWNER".padEnd(12),"AGE".padEnd(6),"STATUS".padEnd(22),"PRUNE?"].join(" | ");console.log(Ot.bold(o)),console.log("-".repeat(o.length));for(let s of r.candidates){let i=[s.path.slice(-44).padEnd(45),s.owner.padEnd(12),Q2(s.ageMs).padEnd(6),s.verdict.padEnd(22),X2(s.verdict)].join(" | ");console.log(i)}if(r.candidates.length===0&&console.log(Ot.dim(" (no afk-managed worktrees found)")),r.warnings.length>0){console.log("");for(let s of r.warnings)console.log(Ot.yellow(s))}}),t.command("prune").description("Remove stale, empty, and orphaned worktrees").option("--apply","Execute removals (default is dry-run)",!1).option("--max-age-days-clean <n>","Max age (days) for clean worktrees before removal").option("--max-age-days-dirty <n>","Max age (days) for dirty worktrees before warning").option("--scope <scope>","Scope: interactive | diagnose | all","all").action(async n=>{let r;try{r=await oR()}catch(w){K(w)}let s=nt().daemon?.worktreePrune,i=parseInt(T.AFK_WORKTREE_MAX_AGE_CLEAN??"",10),a=parseInt(T.AFK_WORKTREE_MAX_AGE_DIRTY??"",10),l=n.maxAgeDaysClean!==void 0?parseInt(n.maxAgeDaysClean,10):s?.maxAgeDaysClean??(Number.isNaN(i)?14:i),c=n.maxAgeDaysDirty!==void 0?parseInt(n.maxAgeDaysDirty,10):s?.maxAgeDaysDirty??(Number.isNaN(a)?30:a),u;try{u=Z2(n.scope)}catch(w){K(w)}let d={execFile:cf,repoRoot:r,dryRun:!n.apply,maxAgeDaysClean:l,maxAgeDaysDirty:c,scope:u},m;try{m=await zt(d)}catch(w){K(new Error(`Sweep failed: ${w.message}`))}m.dryRun&&console.log(Ot.yellow("\u{1F50D} Dry-run mode \u2014 no changes made."));let f={};for(let w of m.candidates)f[w.verdict]=(f[w.verdict]??0)+1;let g=m.warnings.filter(w=>w.startsWith("[WARN]")).length,h=m.warnings.filter(w=>w.startsWith("[ERROR]")).length,b=Object.entries(f).sort(([w],[x])=>w.localeCompare(x)).map(([w,x])=>`${w}=${x}`);console.log(`Removed: ${m.removed.length}, Warned: ${g}, Errors: ${h}`+(b.length>0?` [${b.join(" ")}]`:""));for(let w of m.candidates){let R=m.removed.includes(w.path)?Ot.red("\u2717"):Ot.green("\u2713");console.log(` ${R} [${w.verdict.padEnd(22)}] ${w.path}`)}if(m.warnings.length>0){console.log("");for(let w of m.warnings)w.startsWith("[ERROR]")?console.error(Ot.red(w)):console.log(Ot.yellow(w))}m.warnings.some(w=>w.startsWith("[ERROR]"))&&process.exit(1)})}import{spawn as eG}from"child_process";var tG=/^\d+\.\d+\.\d+(-[\da-z.]+)?$/i;function nG(e,t){let n=e.split(".").map(Number),r=t.split(".").map(Number),o=Math.max(n.length,r.length);for(let s=0;s<o;s++){let i=n[s]??0,a=r[s]??0;if(a>i)return!0;if(a<i)return!1}return!1}function aR(e){e.command("update").alias("upgrade").description("Update agent-afk to the latest published version").option("--check","Only check whether an update is available; do not install").option("--pin <version>","Install a specific version instead of latest (must be valid semver)").action(async t=>{let n=bn();if(t.check===!0){process.stderr.write(`Checking for updates\u2026
2395
- `);let i=await $m();if(i===void 0){console.log(p.warning("Could not reach the npm registry to check for updates.")),console.log(p.dim(` Current: ${n}`)),process.exitCode=1;return}if(nG(n,i)){console.log(`${p.bold("Update available:")} ${p.dim(n)} \u2192 ${p.bold(i)}`),console.log(p.dim(" Run `afk update` to install."));return}console.log(`agent-afk ${p.bold(n)} is up to date.`);return}if(t.pin!==void 0&&!tG.test(t.pin)){console.error(p.warning(`Invalid version: ${JSON.stringify(t.pin)}. Must be valid semver (e.g. 1.2.3 or 1.2.3-beta.1).`)),process.exitCode=1;return}let r=t.pin;if(r===void 0){if(process.stderr.write(`Fetching latest version\u2026
2396
- `),r=await $m(),r===void 0){console.error(p.warning("Could not reach the npm registry. Aborting.")),process.exitCode=1;return}if(r===n){console.log(`agent-afk ${p.bold(n)} is up to date.`);return}}console.log(`Updating agent-afk: ${p.dim(n)} \u2192 ${p.bold(r)}`),console.log(p.dim(` npm install -g agent-afk@${r}`));let{code:o,signal:s}=await rG(r);o===0?(Om(r),console.log(p.success(`\u2713 agent-afk@${r} installed.`))):s!==null?(console.error(p.warning(`npm install was killed by signal ${s}.`)),process.exitCode=1):(console.error(p.warning(`npm install exited with code ${o??1}.`)),process.exitCode=o??1)})}function rG(e){return new Promise(t=>{let n=eG("npm",["install","-g",`agent-afk@${e}`],{stdio:"inherit"});n.on("error",()=>t({code:1,signal:null})),n.on("exit",(r,o)=>t({code:r,signal:o}))})}import{existsSync as lR,readFileSync as cR}from"node:fs";import{join as oG}from"node:path";W();async function Uc(e,t,n){try{let r=oG(Nr("default"),"port");if(!lR(r))return;let o=cR(r,"utf-8").trim(),s=parseInt(o,10);if(Number.isNaN(s))return;await fetch(`http://localhost:${s}${t}`,{method:e,headers:{"Content-Type":"application/json"},body:n!==void 0?JSON.stringify(n):void 0,signal:AbortSignal.timeout(2e3)})}catch{}}function uR(e){let t=e.command("schedule").description("Manage scheduled daemon tasks");t.command("add").description("Add a new scheduled task").requiredOption("--name <name>","Human-readable label").requiredOption("--command <cmd>","Command to run").requiredOption("--cron <expr>","Cron expression (5-field)").option("--trigger <mode>","cron | sessionstart | both","cron").option("--notify <when>","failure | always | never","failure").option("--disabled","Add in disabled state",!1).action(async n=>{try{let r=la({name:n.name,command:n.command,cron:n.cron,trigger:n.trigger,notifyOn:n.notify,enabled:!n.disabled});await Uc("POST","/tasks",{taskId:r.id,command:r.command,cron:r.cron,trigger:r.trigger}),console.log(`\u2705 Added: ${r.id} \u2014 ${r.name}`)}catch(r){K(r)}}),t.command("list").description("List all scheduled tasks").action(()=>{try{let n=yt();if(n.length===0){console.log("No scheduled tasks.");return}let r="ID | NAME | CRON | ENABLED",o="-".repeat(r.length);console.log(r),console.log(o);for(let s of n)console.log([s.id.padEnd(20),s.name.padEnd(30),s.cron.padEnd(15),String(s.enabled)].join(" | "))}catch(n){K(n)}}),t.command("remove <id>").description("Permanently remove a scheduled task").action(async n=>{try{ca(n)||(console.error(`Task not found: ${n}`),process.exit(1)),await Uc("DELETE",`/tasks/${n}`),console.log(`\u2705 Removed: ${n}`)}catch(r){K(r)}}),t.command("enable <id>").description("Enable a scheduled task").action(async n=>{try{let r=Ss(n);r||(console.error(`Task not found: ${n}`),process.exit(1));let o=yt();sr(o.map(s=>s.id===n?{...s,enabled:!0,updatedAt:new Date().toISOString()}:s)),await Uc("POST","/tasks",{taskId:r.id,command:r.command,cron:r.cron,trigger:r.trigger}),console.log(`\u2705 Enabled: ${n}`)}catch(r){K(r)}}),t.command("disable <id>").description("Disable a scheduled task").action(async n=>{try{Ss(n)||(console.error(`Task not found: ${n}`),process.exit(1));let o=yt();sr(o.map(s=>s.id===n?{...s,enabled:!1,updatedAt:new Date().toISOString()}:s)),await Uc("DELETE",`/tasks/${n}`),console.log(`\u2705 Disabled: ${n}`)}catch(r){K(r)}}),t.command("logs <id>").description("Show recent execution history for a task").option("-n, --limit <n>","Number of records to show","10").action((n,r)=>{try{let o=Math.min(Math.max(1,parseInt(r.limit,10)||10),50),s=$t();if(!lR(s)){console.log(`No telemetry found for task: ${n}`);return}let i=cR(s),c=(i.length>1048576?i.subarray(i.length-1048576):i).toString("utf-8").split(`
2397
- `),u=[];for(let m=c.length-1;m>=0;m-=1){let f=c[m];if(f)try{let g=JSON.parse(f);if(g.taskId!==n)continue;if(u.push(g),u.length>=o)break}catch{continue}}let d=u.reverse();if(d.length===0){console.log(`No history found for task: ${n}`);return}console.log(JSON.stringify(d,null,2))}catch(o){K(o)}})}function dR(e){return new Date(e).toISOString().replace("T"," ").slice(0,19)}function Qo(e,t){return e.length>=t?e:e+" ".repeat(t-e.length)}var pR=9,jc=50,mR=22;function sG(e){let t=Qo(e.jobId,mR),n=Qo(e.status,pR),r=e.label.length>jc?`${e.label.slice(0,jc-1)}\u2026`:Qo(e.label,jc),o=dR(e.startedAt),s=e.endedAt!==void 0?dR(e.endedAt):"\u2014";return`${t} ${n} ${r} ${o} ${s}`}function iG(e){return e.type==="done"||e.type==="error"}function fR(e){let t=e.command("bg").description(`Inspect persisted background subagent job logs.
2392
+ `)}`),this.name="FarmIsolationViolation",this.dirtyFiles=t}};function K2(e,t){return e.length>=t?e:e+" ".repeat(t-e.length)}function G2(e){if(e===void 0)return pe.dim("\u2014");if(e===null)return pe.dim("skipped");let t=e.fail===0&&e.pass>0?pe.green("tests\u2713"):pe.red("tests\u2717"),n=e.lint_ok===!0?pe.green("lint\u2713"):e.lint_ok===!1?pe.red("lint\u2717"):pe.dim("lint?"),r=e.loc_delta>0?"+":"",o=pe.dim(`${r}${e.loc_delta} LoC`);return`${t} ${n} ${o}`}function q2(e,t,n,r){let o="\u2500".repeat(45);console.log(pe.dim(o)),console.log(`farm: ${e}`),console.log(`slug: ${t}`),console.log("");let s=r.some(u=>u.score!=null),i=s?Si(r.map(u=>({index:u.index,score:u.score??null}))).map(u=>r.find(d=>d.index===u)):r;for(let u=0;u<i.length;u++){let d=i[u],p=n.find(w=>w.index===d.index),f=d.ok?pe.green("\u2713"):pe.red("\u2717"),g=K2(p.branch,40),h=d.ok?pe.dim(`(${d.commitCount} commit${d.commitCount===1?"":"s"})`):pe.red(`[error: ${d.error}]`),b=s?pe.cyan(`#${u+1} `):"",y=s?` ${G2(d.score)}`:"";console.log(`${b}branch-${d.index} ${f} ${g} ${h}${y}`),console.log(pe.dim(` worktree: ${p.path}`))}console.log(pe.dim(o));let a=r.filter(u=>u.ok).length,l=r.length;console.log(`${a}/${l} branches completed.`);let c=r.some(u=>u.score!=null&&u.score.pass>0);s&&!c&&console.log(pe.yellow("\u26A0 no branch passed tests \u2014 ranking falls back to lint + LoC"))}function z2(e,t,n){let r=t.map(a=>{let l=e.branches.find(u=>u.index===a.index),c={index:a.index,branch:l?.branch??`(unknown-${a.index})`,ok:a.ok,commitCount:a.commitCount};return l?.label!==void 0&&(c.label=l.label),a.error!==void 0&&(c.error=a.error),a.score!==void 0&&(c.score=a.score),c}),o=Si(t.map(a=>({index:a.index,score:a.score??null}))),s;for(let a of o){let l=t.find(c=>c.index===a);if(!(!l||!l.ok||!l.score)&&l.score.pass>0&&l.score.fail===0){s=a;break}}if(s===void 0)for(let a of o){let l=t.find(c=>c.index===a);if(l?.ok&&l.score){s=a;break}}let i={taskName:e.taskName,taskSlug:e.taskSlug,baseSha:e.baseRef,startedAt:n,completedAt:new Date().toISOString(),branches:r};return s!==void 0&&(i.winner=s),e.human_decision!==void 0&&(i.human_decision=e.human_decision),i}async function J2(e){let{task:t,branches:n,labels:r,model:o,baseRef:s,cwd:i=process.cwd(),failFast:a,taskSlug:l,score:c=!0,scoreTimeoutMs:u=Bc,memoryWrite:d=!0,digest:p=!0,_createFarm:f=KE,_runSubagentDAG:g=Ga,_getCommitCount:h=W2,_getSourceRepoDirtyFiles:b=H2,_scoreBranch:y=VE,_writeScore:w=YE,_writeFarmFact:x=QE,_sendFarmDigest:R=tR,_setFarmMemoryFactId:v=GE}=e,E=new Date().toISOString();(!Number.isInteger(n)||n<1||n>16)&&(console.error(pe.red(`--branches must be between 1 and 16 (got ${n})`)),process.exit(1)),r!==void 0&&r.length!==n&&(console.error(pe.red(`--labels count (${r.length}) must equal --branches (${n})`)),process.exit(1));let I;try{I=await f({taskName:t,count:n,labels:r,cwd:i,baseRef:s,taskSlug:l})}catch(B){console.error(pe.red(`Farm creation failed: ${B instanceof Error?B.message:String(B)}`)),process.exit(1)}let D=I.baseRef,_=o??Xe(),M=uo()??co()??"",P=I.branches.map(B=>({id:`branch-${B.index}`,agentType:`branch-${B.index}${B.label?` (${B.label})`:""}`,systemPrompt:M,promptBuilder:j=>(console.log(`[branch-${B.index}] started`),[`Task: ${t}`,"",`You are working in a dedicated git worktree. Your working directory has been set to: ${B.path}`,`Your branch is: ${B.branch}`,"","Complete the task. All file operations are restricted to this worktree by the runtime."].join(`
2393
+ `)),model:_,idPrefix:`farm-${I.taskSlug}-branch-${B.index}`,cwd:B.path,readRoots:[B.path],writeRoots:[B.path]})),$=new AbortController,F=new X({parentAbortSignal:$.signal}),k={sessionId:`farm-${I.taskSlug}`,abortSignal:$.signal},A;try{A=await g({manager:F,parentSession:k,nodes:P,edges:[],failFast:a})}catch(B){throw console.error(pe.red(`Farm dispatch failed: ${B instanceof Error?B.message:String(B)}`)),B}finally{$.abort()}let N=[];for(let B of I.branches){let j=A.failed.find(L=>L.id===`branch-${B.index}`),ee=A.skipped.includes(`branch-${B.index}`);if(j||ee){let L=j?j.error.message:"skipped";console.log(`[branch-${B.index}] \u2717 failed: ${L}`),N.push({index:B.index,ok:!1,commitCount:0,error:L});continue}let q=await h(B.path,D);if(q===0){let L="no commits made";console.log(`[branch-${B.index}] \u2717 failed: ${L}`),N.push({index:B.index,ok:!1,commitCount:0,error:L})}else console.log(`[branch-${B.index}] \u2713 done`),N.push({index:B.index,ok:!0,commitCount:q})}let H=await b(i);if(c)for(let B of N){if(!B.ok){B.score=null;continue}let j=I.branches.find(q=>q.index===B.index);console.log(`[branch-${B.index}] scoring\u2026`);let ee=await y({branchPath:j.path,baseSha:D,timeoutMs:u});B.score=ee;try{await w(I.farmDir,B.index,ee)}catch(q){console.error(pe.yellow(`[branch-${B.index}] score.json write failed: ${q instanceof Error?q.message:String(q)}`))}}if(q2(t,I.taskSlug,I.branches,N),d||p){let B=z2(I,N,E);if(d){let j=x(B);if("skipped"in j)console.error(pe.yellow(`[memory] write skipped: ${j.reason}`));else{let{factId:ee}=j;try{await v(I.taskSlug,ee)}catch(q){console.error(pe.yellow(`[memory] setFarmMemoryFactId failed: ${q.message}`))}}}if(p){let j=await R(B);j.sent?console.log(pe.dim(`[telegram] digest sent (${j.chatCount} chat${j.chatCount===1?"":"s"})`)):j.reason&&j.reason!=="telegram unconfigured"&&console.error(pe.yellow(`[telegram] digest failed: ${j.reason}`))}}if(H.length>0){let B=new lf(H);console.error(pe.red(`
2394
+ \u26A0 ISOLATION VIOLATION`)),console.error(pe.red(B.message)),process.exit(1)}let C=N.every(B=>B.ok);process.exit(C?0:1)}function rR(e){e.command("farm").description("Run a task across N speculative git worktree branches in parallel").argument("<task>","Task description to run on each branch").option("-n, --branches <number>","Number of branches to spawn (1-16)","3").option("--labels <labels>","Comma-separated branch labels (count must equal --branches)").option("-m, --model <model>","Model to use",Xe()).option("--base-ref <ref>","Base git ref (default: HEAD)").option("--cwd <path>","Source repo root (default: process.cwd())").option("--fail-fast","Abort remaining branches on first failure",!1).option("--task-slug <slug>","Deterministic task slug override (for tests)").option("--no-score","Skip the post-run scorer (tests + lint + LoC)").option("--score-timeout <ms>",`Per-branch test timeout in ms (default ${Bc})`).option("--no-memory","Skip writing the farm-run fact to cross-session memory").option("--no-digest","Skip pushing the Telegram digest on completion").action(async(t,n)=>{let r=parseInt(n.branches,10),o=n.labels?n.labels.split(",").map(i=>i.trim()).filter(Boolean):void 0,s=n.scoreTimeout?parseInt(n.scoreTimeout,10):void 0;s!==void 0&&(!Number.isFinite(s)||s<1)&&(console.error(pe.red(`--score-timeout must be a positive integer (got "${n.scoreTimeout}")`)),process.exit(1));try{await J2({task:t,branches:r,labels:o,model:n.model,baseRef:n.baseRef,cwd:n.cwd,failFast:n.failFast,taskSlug:n.taskSlug,score:n.score,memoryWrite:n.memory,digest:n.digest,...s!==void 0?{scoreTimeoutMs:s}:{}})}catch(i){console.error(i),process.exitCode=1}})}G();import Ot from"chalk";import{execFile as V2}from"node:child_process";import{promisify as Y2}from"node:util";var cf=Y2(V2);async function oR(){try{return(await cf("git",["rev-parse","--show-toplevel"])).stdout.trim()}catch{throw new Error("Not in a git repository.")}}function X2(e){return["empty","stale-clean","orphaned-dir","orphaned-registration","dead-owner"].includes(e)?Ot.red("yes"):e==="stale-dirty"?Ot.yellow("warn"):Ot.green("no")}var sR=["interactive","diagnose","all"];function Z2(e){if(sR.includes(e))return e;throw new Error(`Invalid --scope value: '${e}'. Allowed: ${sR.join(" | ")}.`)}function Q2(e){if(e<=0)return"-";let t=e/864e5;return t<1?`${Math.max(1,Math.round(e/36e5))}h`:`${Math.round(t)}d`}function iR(e){let t=e.command("worktree").description("Manage git worktrees created by afk");t.command("list").description("List all afk-managed worktrees and show prune candidates (dry-run only)").action(async()=>{let n;try{n=await oR()}catch(s){K(s)}let r;try{r=await zt({execFile:cf,repoRoot:n,dryRun:!0})}catch(s){K(new Error(`Sweep failed: ${s.message}`))}let o=["PATH".padEnd(45),"OWNER".padEnd(12),"AGE".padEnd(6),"STATUS".padEnd(22),"PRUNE?"].join(" | ");console.log(Ot.bold(o)),console.log("-".repeat(o.length));for(let s of r.candidates){let i=[s.path.slice(-44).padEnd(45),s.owner.padEnd(12),Q2(s.ageMs).padEnd(6),s.verdict.padEnd(22),X2(s.verdict)].join(" | ");console.log(i)}if(r.candidates.length===0&&console.log(Ot.dim(" (no afk-managed worktrees found)")),r.warnings.length>0){console.log("");for(let s of r.warnings)console.log(Ot.yellow(s))}}),t.command("prune").description("Remove stale, empty, and orphaned worktrees").option("--apply","Execute removals (default is dry-run)",!1).option("--max-age-days-clean <n>","Max age (days) for clean worktrees before removal").option("--max-age-days-dirty <n>","Max age (days) for dirty worktrees before warning").option("--scope <scope>","Scope: interactive | diagnose | all","all").action(async n=>{let r;try{r=await oR()}catch(w){K(w)}let s=nt().daemon?.worktreePrune,i=parseInt(T.AFK_WORKTREE_MAX_AGE_CLEAN??"",10),a=parseInt(T.AFK_WORKTREE_MAX_AGE_DIRTY??"",10),l=n.maxAgeDaysClean!==void 0?parseInt(n.maxAgeDaysClean,10):s?.maxAgeDaysClean??(Number.isNaN(i)?14:i),c=n.maxAgeDaysDirty!==void 0?parseInt(n.maxAgeDaysDirty,10):s?.maxAgeDaysDirty??(Number.isNaN(a)?30:a),u;try{u=Z2(n.scope)}catch(w){K(w)}let d={execFile:cf,repoRoot:r,dryRun:!n.apply,maxAgeDaysClean:l,maxAgeDaysDirty:c,scope:u},p;try{p=await zt(d)}catch(w){K(new Error(`Sweep failed: ${w.message}`))}p.dryRun&&console.log(Ot.yellow("\u{1F50D} Dry-run mode \u2014 no changes made."));let f={};for(let w of p.candidates)f[w.verdict]=(f[w.verdict]??0)+1;let g=p.warnings.filter(w=>w.startsWith("[WARN]")).length,h=p.warnings.filter(w=>w.startsWith("[ERROR]")).length,b=Object.entries(f).sort(([w],[x])=>w.localeCompare(x)).map(([w,x])=>`${w}=${x}`);console.log(`Removed: ${p.removed.length}, Warned: ${g}, Errors: ${h}`+(b.length>0?` [${b.join(" ")}]`:""));for(let w of p.candidates){let R=p.removed.includes(w.path)?Ot.red("\u2717"):Ot.green("\u2713");console.log(` ${R} [${w.verdict.padEnd(22)}] ${w.path}`)}if(p.warnings.length>0){console.log("");for(let w of p.warnings)w.startsWith("[ERROR]")?console.error(Ot.red(w)):console.log(Ot.yellow(w))}p.warnings.some(w=>w.startsWith("[ERROR]"))&&process.exit(1)})}import{spawn as eG}from"child_process";var tG=/^\d+\.\d+\.\d+(-[\da-z.]+)?$/i;function nG(e,t){let n=e.split(".").map(Number),r=t.split(".").map(Number),o=Math.max(n.length,r.length);for(let s=0;s<o;s++){let i=n[s]??0,a=r[s]??0;if(a>i)return!0;if(a<i)return!1}return!1}function aR(e){e.command("update").alias("upgrade").description("Update agent-afk to the latest published version").option("--check","Only check whether an update is available; do not install").option("--pin <version>","Install a specific version instead of latest (must be valid semver)").action(async t=>{let n=bn();if(t.check===!0){process.stderr.write(`Checking for updates\u2026
2395
+ `);let i=await $m();if(i===void 0){console.log(m.warning("Could not reach the npm registry to check for updates.")),console.log(m.dim(` Current: ${n}`)),process.exitCode=1;return}if(nG(n,i)){console.log(`${m.bold("Update available:")} ${m.dim(n)} \u2192 ${m.bold(i)}`),console.log(m.dim(" Run `afk update` to install."));return}console.log(`agent-afk ${m.bold(n)} is up to date.`);return}if(t.pin!==void 0&&!tG.test(t.pin)){console.error(m.warning(`Invalid version: ${JSON.stringify(t.pin)}. Must be valid semver (e.g. 1.2.3 or 1.2.3-beta.1).`)),process.exitCode=1;return}let r=t.pin;if(r===void 0){if(process.stderr.write(`Fetching latest version\u2026
2396
+ `),r=await $m(),r===void 0){console.error(m.warning("Could not reach the npm registry. Aborting.")),process.exitCode=1;return}if(r===n){console.log(`agent-afk ${m.bold(n)} is up to date.`);return}}console.log(`Updating agent-afk: ${m.dim(n)} \u2192 ${m.bold(r)}`),console.log(m.dim(` npm install -g agent-afk@${r}`));let{code:o,signal:s}=await rG(r);o===0?(Om(r),console.log(m.success(`\u2713 agent-afk@${r} installed.`))):s!==null?(console.error(m.warning(`npm install was killed by signal ${s}.`)),process.exitCode=1):(console.error(m.warning(`npm install exited with code ${o??1}.`)),process.exitCode=o??1)})}function rG(e){return new Promise(t=>{let n=eG("npm",["install","-g",`agent-afk@${e}`],{stdio:"inherit"});n.on("error",()=>t({code:1,signal:null})),n.on("exit",(r,o)=>t({code:r,signal:o}))})}import{existsSync as lR,readFileSync as cR}from"node:fs";import{join as oG}from"node:path";W();async function Uc(e,t,n){try{let r=oG(Nr("default"),"port");if(!lR(r))return;let o=cR(r,"utf-8").trim(),s=parseInt(o,10);if(Number.isNaN(s))return;await fetch(`http://localhost:${s}${t}`,{method:e,headers:{"Content-Type":"application/json"},body:n!==void 0?JSON.stringify(n):void 0,signal:AbortSignal.timeout(2e3)})}catch{}}function uR(e){let t=e.command("schedule").description("Manage scheduled daemon tasks");t.command("add").description("Add a new scheduled task").requiredOption("--name <name>","Human-readable label").requiredOption("--command <cmd>","Command to run").requiredOption("--cron <expr>","Cron expression (5-field)").option("--trigger <mode>","cron | sessionstart | both","cron").option("--notify <when>","failure | always | never","failure").option("--disabled","Add in disabled state",!1).action(async n=>{try{let r=la({name:n.name,command:n.command,cron:n.cron,trigger:n.trigger,notifyOn:n.notify,enabled:!n.disabled});await Uc("POST","/tasks",{taskId:r.id,command:r.command,cron:r.cron,trigger:r.trigger}),console.log(`\u2705 Added: ${r.id} \u2014 ${r.name}`)}catch(r){K(r)}}),t.command("list").description("List all scheduled tasks").action(()=>{try{let n=yt();if(n.length===0){console.log("No scheduled tasks.");return}let r="ID | NAME | CRON | ENABLED",o="-".repeat(r.length);console.log(r),console.log(o);for(let s of n)console.log([s.id.padEnd(20),s.name.padEnd(30),s.cron.padEnd(15),String(s.enabled)].join(" | "))}catch(n){K(n)}}),t.command("remove <id>").description("Permanently remove a scheduled task").action(async n=>{try{ca(n)||(console.error(`Task not found: ${n}`),process.exit(1)),await Uc("DELETE",`/tasks/${n}`),console.log(`\u2705 Removed: ${n}`)}catch(r){K(r)}}),t.command("enable <id>").description("Enable a scheduled task").action(async n=>{try{let r=Ss(n);r||(console.error(`Task not found: ${n}`),process.exit(1));let o=yt();sr(o.map(s=>s.id===n?{...s,enabled:!0,updatedAt:new Date().toISOString()}:s)),await Uc("POST","/tasks",{taskId:r.id,command:r.command,cron:r.cron,trigger:r.trigger}),console.log(`\u2705 Enabled: ${n}`)}catch(r){K(r)}}),t.command("disable <id>").description("Disable a scheduled task").action(async n=>{try{Ss(n)||(console.error(`Task not found: ${n}`),process.exit(1));let o=yt();sr(o.map(s=>s.id===n?{...s,enabled:!1,updatedAt:new Date().toISOString()}:s)),await Uc("DELETE",`/tasks/${n}`),console.log(`\u2705 Disabled: ${n}`)}catch(r){K(r)}}),t.command("logs <id>").description("Show recent execution history for a task").option("-n, --limit <n>","Number of records to show","10").action((n,r)=>{try{let o=Math.min(Math.max(1,parseInt(r.limit,10)||10),50),s=$t();if(!lR(s)){console.log(`No telemetry found for task: ${n}`);return}let i=cR(s),c=(i.length>1048576?i.subarray(i.length-1048576):i).toString("utf-8").split(`
2397
+ `),u=[];for(let p=c.length-1;p>=0;p-=1){let f=c[p];if(f)try{let g=JSON.parse(f);if(g.taskId!==n)continue;if(u.push(g),u.length>=o)break}catch{continue}}let d=u.reverse();if(d.length===0){console.log(`No history found for task: ${n}`);return}console.log(JSON.stringify(d,null,2))}catch(o){K(o)}})}function dR(e){return new Date(e).toISOString().replace("T"," ").slice(0,19)}function Qo(e,t){return e.length>=t?e:e+" ".repeat(t-e.length)}var pR=9,jc=50,mR=22;function sG(e){let t=Qo(e.jobId,mR),n=Qo(e.status,pR),r=e.label.length>jc?`${e.label.slice(0,jc-1)}\u2026`:Qo(e.label,jc),o=dR(e.startedAt),s=e.endedAt!==void 0?dR(e.endedAt):"\u2014";return`${t} ${n} ${r} ${o} ${s}`}function iG(e){return e.type==="done"||e.type==="error"}function fR(e){let t=e.command("bg").description(`Inspect persisted background subagent job logs.
2398
2398
  Note: bg jobs are tied to the parent REPL process \u2014 if the REPL exits,
2399
2399
  the job dies. This command reads the persisted log.`);t.command("list").description("List background jobs from disk (most recent first)").option("-n, --max <number>","Maximum jobs to show","20").action(async n=>{try{let r=Math.min(100,Math.max(1,parseInt(n.max,10)||20)),s=(await jt.listJobs()).slice(0,r);if(s.length===0){process.stdout.write(`No background job logs found in ~/.afk/state/bg/
2400
2400
  `);return}let i=Qo("JOB ID",mR)+" "+Qo("STATUS",pR)+" "+Qo("LABEL",jc)+" STARTED AT ENDED AT";process.stdout.write(i+`
@@ -2410,20 +2410,20 @@ the job dies. This command reads the persisted log.`).action(async n=>{try{for a
2410
2410
  `)}catch(r){K(r)}})}import qe from"chalk";import{execFileSync as kG}from"child_process";import{existsSync as ER}from"fs";W();import{homedir as gR}from"os";import{join as uf}from"path";var es=["telegram","daemon"];function Ge(e){return`com.afk.${e}`}function df(e=gR()){return uf(e,"Library","LaunchAgents")}function Jn(e,t=gR()){return uf(df(t),`${Ge(e)}.plist`)}function ts(e){return uf(kn(),`service-${e}.log`)}function vi(){return`gui/${process.getuid?.()??501}`}var Ir=8e3;import{execFileSync as aG}from"child_process";import{existsSync as hR,realpathSync as yR}from"fs";import{homedir as lG}from"os";import{resolve as cG}from"path";function Vn(e){return e.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function bR(e){let t=[];t.push('<?xml version="1.0" encoding="UTF-8"?>'),t.push('<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">'),t.push('<plist version="1.0">'),t.push("<dict>"),t.push(" <key>Label</key>"),t.push(` <string>${Vn(e.label)}</string>`),t.push(" <key>ProgramArguments</key>"),t.push(" <array>");for(let n of e.programArguments)t.push(` <string>${Vn(n)}</string>`);if(t.push(" </array>"),t.push(" <key>WorkingDirectory</key>"),t.push(` <string>${Vn(e.workingDirectory)}</string>`),t.push(" <key>StandardOutPath</key>"),t.push(` <string>${Vn(e.standardOutPath)}</string>`),t.push(" <key>StandardErrorPath</key>"),t.push(` <string>${Vn(e.standardErrorPath)}</string>`),t.push(" <key>RunAtLoad</key>"),t.push(" <true/>"),t.push(" <key>KeepAlive</key>"),t.push(" <true/>"),t.push(" <key>ProcessType</key>"),t.push(" <string>Interactive</string>"),e.watchPaths&&e.watchPaths.length>0){t.push(" <key>WatchPaths</key>"),t.push(" <array>");for(let n of e.watchPaths)t.push(` <string>${Vn(n)}</string>`);t.push(" </array>")}if(e.environmentVariables&&Object.keys(e.environmentVariables).length>0){t.push(" <key>EnvironmentVariables</key>"),t.push(" <dict>");let n=Object.keys(e.environmentVariables).sort();for(let r of n){let o=e.environmentVariables[r]??"";t.push(` <key>${Vn(r)}</key>`),t.push(` <string>${Vn(o)}</string>`)}t.push(" </dict>")}return t.push("</dict>"),t.push("</plist>"),t.join(`
2411
2411
  `)+`
2412
2412
  `}function uG(e=["/usr/local/bin/afk","/opt/homebrew/bin/afk"],t=hR,n=dG,r=yR){let o=process.argv[1];if(o)try{let i=r(o);if(wR.some(a=>i.startsWith(a))&&t(i))return i}catch{}let s=n();if(s&&t(s))return s;for(let i of e)if(t(i))return i;throw new Error(`Could not locate the 'afk' binary. Searched: ${e.join(", ")}. Install it globally first (e.g. 'pnpm install -g agent-afk' or via Homebrew).`)}var wR=["/usr/local/bin/","/opt/homebrew/bin/","/usr/bin/","/opt/local/bin/"];function dG(){try{let e=aG("which",["afk"],{encoding:"utf-8",stdio:["ignore","pipe","ignore"]}).trim();if(!e)return;let t;try{t=yR(e)}catch{return}return wR.some(n=>t.startsWith(n))?t:void 0}catch{return}}function pf(e,t=hR){if(e==="telegram"){let r=Jm();if(r.endsWith(".ts"))throw new Error(`Refusing to install telegram service pointing at TypeScript source (${r}). Run 'pnpm build' first so the compiled entrypoint exists, or install agent-afk globally (e.g. 'pnpm install -g agent-afk').`);if(!t(r))throw new Error(`Telegram entrypoint does not exist on disk: ${r}. Run 'pnpm build' to compile it, or install agent-afk globally.`);return[process.execPath,r]}return[uG(),"daemon"]}function SR(e,t){let n=pf(e,t),r=e==="telegram"?n[1]:void 0;if(!r)return;let o=cG(r),s=lG();if(o.startsWith(s)&&!(o.includes("/node_modules/")||o.includes("/homebrew/")))return[o]}import{execFileSync as pG}from"child_process";import{existsSync as mG}from"fs";function fG(e,t){let n=e.trim();if(n.startsWith("[")||n.startsWith("{")){try{let r=JSON.parse(n),o=Array.isArray(r)?r:[r];for(let s of o){if(typeof s!="object"||s===null)continue;let i=s;if(i.Label!==t)continue;let a={};return typeof i.PID=="number"&&Number.isFinite(i.PID)&&(a.pid=i.PID),typeof i.LastExitStatus=="number"&&Number.isFinite(i.LastExitStatus)&&(a.lastExitStatus=i.LastExitStatus),a}}catch{}return}for(let r of n.split(`
2413
- `)){let o=r.split(" "),s=o.length>=3?o:r.trim().split(/\s+/);if(s.length<3||s[2]?.trim()!==t)continue;let i=s[0]?.trim()??"-",a=s[1]?.trim()??"0",l={};if(i!=="-"&&i!==""){let u=Number.parseInt(i,10);Number.isFinite(u)&&(l.pid=u)}let c=Number.parseInt(a,10);return Number.isFinite(c)&&(l.lastExitStatus=c),l}}function mf(e){let t=Jn(e),n={name:e,label:Ge(e),installed:mG(t),plistPath:t,logFile:ts(e)};if(!n.installed)return n;try{let r=pG("launchctl",["list"],{encoding:"utf-8",stdio:["ignore","pipe","ignore"],timeout:Ir}),o=fG(r,n.label);o&&(o.pid!==void 0&&(n.pid=o.pid),o.lastExitStatus!==void 0&&(n.lastExitStatus=o.lastExitStatus))}catch{}return n}import{execFileSync as Wc}from"child_process";import{existsSync as vR,mkdirSync as kR,readFileSync as xke,renameSync as gG,rmSync as hG,unlinkSync as yG,writeFileSync as bG}from"fs";import{homedir as wG}from"os";import{dirname as SG}from"path";function TR(e,t={}){let n=Jn(e);if(vR(n))return{kind:"already-installed",plistPath:n,label:Ge(e)};let r;try{r=pf(e,t._entrypointExistsCheck)}catch(c){return{kind:"failed",reason:c.message}}let o=t.noWatch?void 0:SR(e,t._entrypointExistsCheck),s=ts(e);kR(df(),{recursive:!0}),kR(SG(s),{recursive:!0});let i={label:Ge(e),programArguments:r,workingDirectory:wG(),standardOutPath:s,standardErrorPath:s,...o?{watchPaths:o}:{},...t.environment?{environmentVariables:t.environment}:{}},a=bR(i),l=`${n}.tmp`;try{bG(l,a,{encoding:"utf-8",flag:"wx",mode:384})}catch(c){return{kind:"failed",reason:`Failed to write plist (tmp ${l}): ${c.message}`}}try{gG(l,n)}catch(c){try{yG(l)}catch{}return{kind:"failed",reason:`Failed to install plist (rename ${l} \u2192 ${n}): ${c.message}`}}if(t.skipBootstrap)return{kind:"installed",plistPath:n,label:Ge(e),watchPathsActive:!!o};try{Wc("launchctl",["bootstrap",vi(),n],{stdio:["ignore","pipe","pipe"],timeout:Ir})}catch(c){let u=c.message;if(c.code==="EALREADY"||/\b37\b/.test(u))return{kind:"failed",reason:`Service already loaded \u2014 run 'afk service restart ${e}' to reload with the new plist.`};if(/already bootstrapped|already loaded/i.test(u)){let d="";try{Wc("launchctl",["bootout",`${vi()}/${Ge(e)}`],{stdio:["ignore","pipe","pipe"],timeout:Ir})}catch(m){d=m.message}try{Wc("launchctl",["bootstrap",vi(),n],{stdio:["ignore","pipe","pipe"],timeout:Ir})}catch(m){let f=m.message;return{kind:"failed",reason:`Bootstrap failed: ${d?`${f} (prior bootout: ${d})`:f}`}}}else return{kind:"failed",reason:`Bootstrap failed: ${u}`}}return{kind:"installed",plistPath:n,label:Ge(e),watchPathsActive:!!o}}function xR(e,t={}){let n=Jn(e);if(!vR(n))return{kind:"not-installed",plistPath:n};if(!t.skipBootout)try{Wc("launchctl",["bootout",`${vi()}/${Ge(e)}`],{stdio:"ignore",timeout:Ir})}catch{}try{hG(n,{force:!0})}catch(r){return{kind:"failed",reason:`Failed to remove plist: ${r.message}`}}return{kind:"uninstalled",plistPath:n}}function Ti(){if(process.platform!=="darwin")throw new Error(`'afk service' uses macOS launchd and is only supported on darwin. Detected: ${process.platform}.`)}function Hc(e){let t=e.toLowerCase();if(es.includes(t))return t;throw new Error(`Unknown service '${e}'. Supported: ${es.join(", ")}.`)}function AR(e){let t=e.command("service").description("Manage AFK background services via macOS launchd (always-on, auto-restart)");t.command("install <name>").description(`Install <${es.join("|")}> as a LaunchAgent that starts on login and relaunches on crash`).option("--no-watch","Disable WatchPaths (no auto-restart on rebuild)").option("--dry-run","Write the plist but do not call launchctl",!1).action((n,r)=>{try{Ti();let o=Hc(n),s=TR(o,{noWatch:r.watch===!1,skipBootstrap:!!r.dryRun});if(s.kind==="already-installed"&&(console.log(qe.yellow(`\u26A0 ${s.label} already installed at ${s.plistPath}`)),console.log(p.meta(` Run 'afk service uninstall ${o}' first to reinstall.`)),process.exit(1)),s.kind==="failed"&&(console.error(qe.red(`\u2717 Install failed: ${s.reason}`)),process.exit(1)),console.log(qe.green(`\u2713 Installed ${s.label}`)),console.log(p.meta(` Plist: ${s.plistPath}`)),console.log(p.meta(` Log: ${ts(o)}`)),s.watchPathsActive?console.log(p.meta(" WatchPaths: active \u2014 service auto-restarts on rebuild.")):console.log(p.meta(" WatchPaths: off \u2014 manual 'afk service restart' needed after updates.")),r.dryRun){let i=process.getuid?.()??501;console.log(p.info(" (dry-run) launchctl bootstrap was skipped; service is NOT yet running.")),console.log(p.meta(` Load manually: launchctl bootstrap gui/${i} ${s.plistPath}`))}else console.log(p.meta(` Status: afk service status ${o}`))}catch(o){K(o)}}),t.command("uninstall <name>").description("Stop the service and remove its LaunchAgent plist").action(n=>{try{Ti();let r=Hc(n),o=xR(r);if(o.kind==="not-installed"){console.log(qe.yellow(`\u26A0 ${Ge(r)} is not installed (no plist at ${o.plistPath})`));return}o.kind==="failed"&&(console.error(qe.red(`\u2717 Uninstall failed: ${o.reason}`)),process.exit(1)),console.log(qe.green(`\u2713 Uninstalled ${Ge(r)}`)),console.log(p.meta(` Removed: ${o.plistPath}`))}catch(r){K(r)}}),t.command("status [name]").description("Show running PID, last exit status, and log file for one or all services").action(n=>{try{if(Ti(),n){let r=mf(Hc(n));RR(r);return}for(let r of es)RR(mf(r)),console.log("")}catch(r){K(r)}}),t.command("list").description("List recognised service names and whether each is installed").action(()=>{try{Ti(),console.log(qe.bold("AFK services:"));for(let n of es){let r=Jn(n),o=ER(r),s=o?qe.green("\u25CF"):qe.dim("\u25CB"),i=o?p.meta("installed"):p.meta("not installed");console.log(` ${s} ${n.padEnd(10)} ${i} ${p.meta(r)}`)}}catch(n){K(n)}}),t.command("restart <name>").description("Restart the service (launchctl kickstart -k)").action(n=>{try{Ti();let r=Hc(n),o=Jn(r);if(ER(o)||(console.error(qe.red(`\u2717 ${Ge(r)} is not installed. Run 'afk service install ${r}' first.`)),process.exit(1)),typeof process.getuid!="function")throw new Error("process.getuid is unavailable \u2014 afk service restart requires a POSIX system.");let s=process.getuid();try{kG("launchctl",["kickstart","-k",`gui/${s}/${Ge(r)}`],{stdio:["ignore","pipe","pipe"],timeout:8e3}),console.log(qe.green(`\u2713 Restarted ${Ge(r)}`))}catch(i){console.error(qe.red(`\u2717 Restart failed: ${i.message}`)),process.exit(1)}}catch(r){K(r)}})}function RR(e){if(console.log(qe.bold(`${e.label}`)),!e.installed){console.log(` ${qe.dim("\u25CB")} Not installed`),console.log(p.meta(` Plist: ${e.plistPath}`)),console.log(p.meta(` Install: afk service install ${e.name}`));return}e.pid!==void 0?console.log(` ${qe.green("\u25CF")} Running (PID ${e.pid})`):(console.log(` ${qe.yellow("\u25CF")} Installed but not running`),e.lastExitStatus!==void 0&&e.lastExitStatus!==0&&console.log(p.meta(` Last exit status: ${e.lastExitStatus}`))),console.log(p.meta(` Plist: ${e.plistPath}`)),console.log(p.meta(` Log: ${e.logFile}`))}import{readFileSync as vG,readdirSync as TG,statSync as xG}from"fs";import{join as bf}from"path";W();import{join as et}from"path";function ff(){return et(Tt(),"improve")}function Pr(){return et(ff(),"failure-cards")}function _R(){return et(Pr(),".index.jsonl")}function xi(e){return et(Pr(),`${e}.json`)}function Kc(e){return et(Pr(),`${e}.md`)}function CR(){return et(ye(),"witness")}function Mr(){return et(ff(),"proposals")}function IR(){return et(Mr(),".index.jsonl")}function gf(e){return et(Mr(),`${e}.json`)}function PR(e){return et(Mr(),`${e}.md`)}function Yn(){return et(ff(),"eval-cases")}function MR(){return et(Yn(),".index.jsonl")}function hf(e){return et(Yn(),`${e}.json`)}function yf(e){return et(Yn(),`${e}.fixture.jsonl`)}function OR(e){return et(Yn(),`${e}.md`)}G();function $R(e){let t=e.trim(),n=/^(\d+)\s*([smhd])$/i.exec(t);if(!n)return;let r=Number.parseInt(n[1]??"0",10),o=(n[2]??"").toLowerCase();if(!(!Number.isFinite(r)||r<=0))switch(o){case"s":return r*1e3;case"m":return r*60*1e3;case"h":return r*60*60*1e3;case"d":return r*24*60*60*1e3;default:return}}function DR(e={}){let t=e.witnessRoot??CR(),n=e.afkHome??T.AFK_HOME??RG(),r=e.sinceMs,o={sessionsScanned:0,sessionsSkippedOld:0,sessionsSkippedEmpty:0,invalidLineCount:0,sessions:[]},s;try{s=TG(t)}catch{return o}for(let i of s){if(i.startsWith("."))continue;let a=bf(t,i),l;try{l=xG(a)}catch{continue}if(!l.isDirectory())continue;if(r!==void 0&&l.mtimeMs<r){o.sessionsSkippedOld+=1;continue}let c=bf(a,"trace.jsonl"),u;try{u=vG(c,"utf-8")}catch{o.sessionsSkippedEmpty+=1;continue}let d=AG(c,n),m=EG({sessionId:i,tracePath:c,relativeTracePath:d,content:u,sessionMtimeMs:l.mtimeMs});o.sessions.push(m),o.sessionsScanned+=1,o.invalidLineCount+=m.invalidLineCount}return o}function EG(e){let{sessionId:t,tracePath:n,relativeTracePath:r,content:o,sessionMtimeMs:s}=e,i=[],a=0,l=o.split(`
2414
- `);for(let c=0;c<l.length;c+=1){let u=l[c]??"";if(u.trim()==="")continue;let d;try{d=JSON.parse(u)}catch{a+=1;continue}let m=iS.safeParse(d);if(!m.success){a+=1;continue}i.push({sessionId:t,tracePath:n,relativeTracePath:r,lineNumber:c+1,rawLine:u,event:m.data})}return{sessionId:t,tracePath:n,relativeTracePath:r,sessionMtimeMs:s,events:i,invalidLineCount:a}}function RG(){let e=T.AFK_HOME;return e&&e.length>0?e:bf(T.HOME??"",".afk")}function AG(e,t){if(!t)return e;if(e.startsWith(t)){let n=e.slice(t.length);return n.startsWith("/")&&(n=n.slice(1)),n}return e}import{createHash as _G}from"crypto";var Or=4,CG=8,IG="v1-bytes-tuple";function LR(e,t={}){let n=t.minRepeats??Or;if(n<2)throw new Error(`minRepeats must be >= 2 (got ${n})`);let r=[];for(let o of e){let s=PG(o,n);r.push(...s)}return r}function PG(e,t){let n=MG(e.events),r=OG(n),o=[];for(let[s,i]of r.entries()){let a=$G(i,t);for(let l of a)o.push(LG(e,l,s))}return o}function MG(e){let t=new Map,n=[];for(let r of e){let o=r.event;if(o.kind!=="tool_call")continue;if(o.payload.phase==="started"){t.set(o.payload.toolUseId,{seq:o.seq,name:o.payload.name,inputBytes:o.payload.inputBytes,subagentId:o.payload.subagentId});continue}let s=t.get(o.payload.toolUseId);if(!s)continue;t.delete(o.payload.toolUseId);let i=DG({name:o.payload.name,inputBytes:s.inputBytes,resultBytes:o.payload.resultBytes,isError:o.payload.isError,subagentId:s.subagentId});n.push({toolUseId:o.payload.toolUseId,startedSeq:s.seq,completedSeq:o.seq,completedLineNumber:r.lineNumber,name:o.payload.name,inputBytes:s.inputBytes,resultBytes:o.payload.resultBytes,isError:o.payload.isError,subagentId:s.subagentId,rawLine:r.rawLine,fingerprint:i})}return n}function OG(e){let t=new Map;for(let n of e){let r=n.subagentId??"root",o=t.get(r);o?o.push(n):t.set(r,[n])}return t}function $G(e,t){let n=[],r=0;for(;r<e.length;){let o=e[r];if(!o){r+=1;continue}let s=r+1;for(;s<e.length;){let a=e[s];if(!a||a.fingerprint!==o.fingerprint)break;s+=1}s-r>=t&&n.push(e.slice(r,s)),r=s>r?s:r+1}return n}function DG(e){let t=[e.name,String(e.inputBytes),String(e.resultBytes),e.isError?"1":"0",e.subagentId??""].join("|");return _G("sha256").update(t).digest("hex")}function LG(e,t,n){let r=t[0];if(!r)throw new Error("repeated-tool-use: empty run");let o=jG(r.name,r.fingerprint),s=new Date().toISOString(),i=t.slice(0,CG),a=[{sessionId:e.sessionId,tracePath:e.relativeTracePath,eventIndices:i.map(l=>l.completedSeq),excerpt:FG(i),annotation:NG(t,n)}];return{slug:o,title:BG(r.name,t.length),pattern:"repeated-tool-use",severity:UG(t.length),observedAt:s,evidence:a,detail:{detector:"repeated-tool-use@v1",fingerprintAlgorithm:IG,fingerprint:r.fingerprint,toolName:r.name,runLength:t.length,agentContext:n,inputBytes:r.inputBytes,resultBytes:r.resultBytes,isError:r.isError,toolUseIds:t.map(l=>l.toolUseId),completedSeqs:t.map(l=>l.completedSeq)}}}function FG(e){let t=e.map(n=>n.rawLine).join(`
2415
- `);return t.length<=2e3?t:t.slice(0,1997)+"..."}function NG(e,t){let n=e[0],r=e[e.length-1];return!n||!r?"":[`${e.length}\xD7 consecutive '${n.name}' calls in ${t} context`,`(seq ${n.completedSeq}\u2026${r.completedSeq},`,`inputBytes=${n.inputBytes}, resultBytes=${n.resultBytes},`,`isError=${n.isError})`].join(" ")}function BG(e,t){return`'${e}' tool repeated ${t}\xD7 with identical fingerprint`}function UG(e){return e>=10?"high":e>=4?"medium":"low"}function jG(e,t){let n=e.toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,""),r=t.slice(0,12);return`repeated-tool-${n.length>0?n:"tool"}-${r}`}var WG=new Set(["budget_exceeded","timeout","hook_blocked","abort","iteration_cap","max_turns_exceeded"]);function NR(e,t={}){let n=t.minOccurrences??1;if(n<1)throw new Error(`minOccurrences must be >= 1 (got ${n})`);let r=new Map;for(let s of e)for(let i of s.events){let a=i.event;if(a.kind!=="closure")continue;let l=a.payload.reason;if(!WG.has(l))continue;let c={sessionId:s.sessionId,relativeTracePath:s.relativeTracePath,seq:a.seq,rawLine:i.rawLine,reason:l,finalCostUsd:a.payload.finalCostUsd,finalTurnCount:a.payload.finalTurnCount},u=r.get(l);u?u.push(c):r.set(l,[c])}let o=[];for(let[s,i]of r.entries())i.length<n||o.push(KG(s,i));return o}var HG=8;function KG(e,t){let n=qG(e),r=new Date().toISOString(),s=t.slice(0,HG).map(l=>({sessionId:l.sessionId,tracePath:l.relativeTracePath,eventIndices:[l.seq],excerpt:zG(l.rawLine),annotation:`closure.reason='${l.reason}' \xB7 cost=${JG(l.finalCostUsd)} \xB7 turns=${l.finalTurnCount}`})),i=t.reduce((l,c)=>l+c.finalCostUsd,0),a=t.reduce((l,c)=>l+c.finalTurnCount,0)/t.length;return{slug:n,title:`Session closure reason '${e}' across ${t.length} session${t.length===1?"":"s"}`,pattern:"closure-anomaly",severity:GG(e,t.length),observedAt:r,evidence:s,detail:{detector:"closure-anomaly@v1",closureReason:e,affectedSessions:t.length,totalCostUsd:FR(i),avgTurnCount:VG(a),maxCostUsd:FR(Math.max(...t.map(l=>l.finalCostUsd))),sessionIds:t.map(l=>l.sessionId),seqs:t.map(l=>l.seq)}}}function GG(e,t){switch(e){case"budget_exceeded":case"timeout":return"high";case"hook_blocked":case"iteration_cap":case"max_turns_exceeded":return t>=3?"high":"medium";case"abort":return t>=3?"medium":"low";default:return"low"}}function qG(e){let t=e.toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"");return`closure-anomaly-${t.length>0?t:"unknown"}`}function zG(e){return e.length<=2e3?e:e.slice(0,1997)+"..."}function JG(e){return`$${e.toFixed(4)}`}function FR(e){return Math.round(e*1e4)/1e4}function VG(e){return Math.round(e*100)/100}import{createHash as YG}from"crypto";var $r=2,XG="v1-hook-reason-tuple",ZG=8;function BR(e,t={}){let n=t.minOccurrences??$r;if(n<1)throw new Error(`minOccurrences must be >= 1 (got ${n})`);let r=new Map;for(let s of e)for(let i of s.events){let a=i.event;if(a.kind!=="hook_decision"||a.payload.hookEvent!=="SubagentStart"||a.payload.decision!=="block")continue;let l=a.payload.reason??"",c=a.payload.blockedTool,u=QG({hookEvent:a.payload.hookEvent,reason:l,blockedTool:c}),d={sessionId:s.sessionId,relativeTracePath:s.relativeTracePath,seq:a.seq,rawLine:i.rawLine,reason:l,blockedTool:c,injectedContextBytes:a.payload.injectedContextBytes},m=r.get(u);m?m.push(d):r.set(u,[d])}let o=[];for(let[s,i]of r.entries())i.length<n||o.push(tq(s,i));return o}function QG(e){let t=[e.hookEvent,e.reason,e.blockedTool??""].join("|");return YG("sha256").update(t).digest("hex")}function eq(e){return`subagent-block-${e.slice(0,12)}`}function tq(e,t){let n=t[0];if(!n)throw new Error("subagent-block: empty sighting bucket");let r=eq(e),o=new Date().toISOString(),i=t.slice(0,ZG).map(l=>({sessionId:l.sessionId,tracePath:l.relativeTracePath,eventIndices:[l.seq],excerpt:sq(l.rawLine),annotation:oq(l)})),a=new Set(t.map(l=>l.sessionId)).size;return{slug:r,title:rq(n.reason,t.length,a),pattern:"subagent-block",severity:nq(t.length,a),observedAt:o,evidence:i,detail:{detector:"subagent-block@v1",fingerprintAlgorithm:XG,fingerprint:e,hookEvent:"SubagentStart",reason:n.reason,blockedTool:n.blockedTool??null,blockCount:t.length,distinctSessions:a,sessionIds:t.map(l=>l.sessionId),seqs:t.map(l=>l.seq)}}}function nq(e,t){return e>=6||t>=3?"high":e>=3?"medium":"low"}function rq(e,t,n){let r=e.length>80?e.slice(0,77)+"...":e,o=r.length>0?`: "${r}"`:"";return`SubagentStart hook blocked ${t}\xD7 across ${n} session${n===1?"":"s"}${o}`}function oq(e){let t=[`seq ${e.seq}`];return e.reason&&t.push(`reason="${e.reason.slice(0,200)}"`),e.blockedTool&&t.push(`blockedTool=${e.blockedTool}`),typeof e.injectedContextBytes=="number"&&t.push(`injectedContextBytes=${e.injectedContextBytes}`),t.join(" \xB7 ")}function sq(e){return e.length<=2e3?e:e.slice(0,1997)+"..."}function UR(e,t={}){let n=t.minFailures??3,r=t.minFailureRate??.25;if(n<1)throw new Error(`minFailures must be >= 1 (got ${n})`);if(r<=0||r>1)throw new Error(`minFailureRate must be in (0, 1] (got ${r})`);let o=new Map;for(let i of e)for(let a of i.events){let l=a.event;if(l.kind!=="tool_call"||l.payload.phase!=="completed")continue;let c=iq(o,l.payload.name);c.totalCalls+=1,l.payload.isError&&(c.failures.push({sessionId:i.sessionId,relativeTracePath:i.relativeTracePath,seq:l.seq,rawLine:a.rawLine,resultBytes:l.payload.resultBytes,durationMs:l.payload.durationMs,truncated:l.payload.truncated}),c.affectedSessions.add(i.sessionId),l.payload.truncated&&(c.truncatedFailureCount+=1))}let s=[];for(let i of o.values()){if(i.failures.length<n)continue;let a=i.failures.length/i.totalCalls;a<r||s.push(aq(i,a))}return s.sort((i,a)=>i.slug.localeCompare(a.slug)),s}function iq(e,t){let n=e.get(t);return n||(n={toolName:t,totalCalls:0,failures:[],affectedSessions:new Set,truncatedFailureCount:0},e.set(t,n)),n}function aq(e,t){let n=uq(e.toolName),r=new Date().toISOString(),s=e.failures.slice(0,8).map(l=>({sessionId:l.sessionId,tracePath:l.relativeTracePath,eventIndices:[l.seq],excerpt:dq(l.rawLine),annotation:`isError=true \xB7 resultBytes=${l.resultBytes} \xB7 durationMs=${l.durationMs}${l.truncated?" \xB7 truncated":""}`})),a=e.failures.reduce((l,c)=>l+c.durationMs,0)/e.failures.length;return{slug:n,title:cq(e.toolName,e.failures.length,e.totalCalls,t),pattern:"tool-failure-density",severity:lq(e.failures.length,t),observedAt:r,evidence:s,detail:{detector:"tool-failure-density@v1",toolName:e.toolName,totalCalls:e.totalCalls,failureCount:e.failures.length,failureRate:pq(t),affectedSessionCount:e.affectedSessions.size,truncatedFailureCount:e.truncatedFailureCount,avgFailureDurationMs:mq(a),sessionIds:Array.from(e.affectedSessions),seqs:e.failures.map(l=>l.seq)}}}function lq(e,t){return t>=1||t>=.5?"high":t>=.25?e>=10?"high":"medium":e>=10?"medium":"low"}function cq(e,t,n,r){let o=(r*100).toFixed(1);return`'${e}' tool failed ${t}/${n} calls (${o}%)`}function uq(e){let t=e.toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"");return`tool-failure-${t.length>0?t:"unknown"}`}function dq(e){return e.length<=2e3?e:e.slice(0,1997)+"..."}function pq(e){return Math.round(e*1e4)/1e4}function mq(e){return Math.round(e*100)/100}var wf=Object.freeze([{name:"repeated-tool-use",description:`Tool fired \u2265N consecutive times with identical fingerprint (default ${Or})`,run:(e,t)=>LR(e,{minRepeats:t.minRepeats??Or})},{name:"closure-anomaly",description:`Session closure reason \u2208 {budget_exceeded,timeout,hook_blocked,abort,iteration_cap,max_turns_exceeded} (default \u2265${1})`,enabledByDefault:!1,run:(e,t)=>NR(e,{minOccurrences:t.closureAnomalyMinOccurrences??1})},{name:"subagent-block",description:`Same SubagentStart hook block reason recurring across \u2265N events (default ${$r})`,enabledByDefault:!1,run:(e,t)=>BR(e,{minOccurrences:t.subagentBlockMinOccurrences??$r})},{name:"tool-failure-density",description:`Tool with \u2265N failures (isError: true) AND failure rate \u2265R (defaults: ${3} failures, ${.25} rate)`,enabledByDefault:!1,run:(e,t)=>UR(e,{minFailures:t.toolFailureMinFailures??3,minFailureRate:t.toolFailureMinRate??.25})}]);function jR(e,t,n,r){let o=[];for(let s of wf){if(n!==void 0){if(!n.has(s.name))continue}else if(r!==!0&&s.enabledByDefault===!1)continue;let i=s.run(e,t);o.push(...i)}return o}function Gc(){return wf.map(e=>e.name)}function qc(){return wf.filter(e=>e.enabledByDefault===!1).map(e=>e.name)}import{existsSync as Yc,mkdirSync as JR,readFileSync as Aq,readdirSync as _q,renameSync as VR,writeFileSync as vf}from"fs";import{join as Cq}from"path";import{z as O}from"zod";var zc=O.enum(["repeated-tool-use","subagent-block","closure-anomaly","tool-failure-density"]),Jc=O.enum(["low","medium","high"]),fq=O.enum(["open","deferred","resolved"]),WR=O.object({sessionId:O.string().min(1),tracePath:O.string().min(1),eventIndices:O.array(O.number().int().nonnegative()).min(1),excerpt:O.string().max(2e3),annotation:O.string().optional()}),Sf=O.object({at:O.string().datetime(),text:O.string()}),_i=O.object({schemaVersion:O.literal(1),slug:O.string().regex(/^[a-z0-9][a-z0-9-]*$/,"slug must be lowercase alphanumeric with hyphens"),title:O.string().min(1).max(200),pattern:zc,severity:Jc,status:fq,firstSeen:O.string().datetime(),lastSeen:O.string().datetime(),occurrenceCount:O.number().int().nonnegative(),evidence:O.array(WR).min(1),detail:O.record(O.string(),O.unknown()),notes:O.array(Sf).default([])}),cve=O.object({slug:O.string().regex(/^[a-z0-9][a-z0-9-]*$/),title:O.string().min(1).max(200),pattern:zc,severity:Jc,observedAt:O.string().datetime(),evidence:O.array(WR).min(1),detail:O.record(O.string(),O.unknown())}),HR=O.object({timestamp:O.string().datetime(),event:O.enum(["created","updated","merged-noop"]),slug:O.string(),pattern:zc,occurrenceCount:O.number().int().nonnegative(),evidenceAdded:O.number().int().nonnegative()}),gq=O.enum(["prompt-defect","schema-too-strict","schema-too-loose","tool-output-shape","hook-overreach","retry-policy","timeout-too-low","cost-control","dispatcher-bug","detector-needs-tuning","unknown"]),hq=O.enum(["safe","moderate","high","forbidden"]),yq=O.enum(["low","medium","high"]),bq=O.object({cardSlug:O.string(),eventIndices:O.array(O.number().int().nonnegative()).min(1),annotation:O.string().optional()}),wq=O.object({path:O.string().min(1),rationale:O.string(),riskTier:hq,confidence:yq}),Sq=O.object({unitTests:O.array(O.string()),evalCases:O.array(O.string()),smokeChecks:O.array(O.string()),manualChecks:O.array(O.string())}),kq=O.object({forbiddenPaths:O.array(O.string()),requiresExplicitApproval:O.boolean()}),vq=O.enum(["draft","approved","rejected","superseded"]),kf=O.object({schemaVersion:O.literal(1),proposalId:O.string().regex(/^[a-z0-9][a-z0-9-]*$/),cardSlug:O.string().regex(/^[a-z0-9][a-z0-9-]*$/),title:O.string().min(1).max(200),hypothesis:O.string().min(1),rootCauseClass:gq,evidenceRefs:O.array(bq).min(1),fixSketch:O.string().min(1),likelyFiles:O.array(wq),riskLevel:Jc,validationPlan:Sq,scopeFreeze:kq,generatedBy:O.enum(["template","llm"]),createdAt:O.string().datetime(),status:vq,notes:O.array(Sf).default([])}),KR=O.object({timestamp:O.string().datetime(),event:O.enum(["created","triaged","superseded"]),proposalId:O.string(),cardSlug:O.string(),generatedBy:O.enum(["template","llm"]),riskLevel:Jc}),Tq=O.object({sourceSessionId:O.string().min(1),sourceTracePath:O.string().min(1),fixturePath:O.string().min(1),evidenceRowIndex:O.number().int().nonnegative(),evidenceEventIndices:O.array(O.number().int().nonnegative()).min(1),sliceLineRange:O.object({startLine:O.number().int().positive(),endLine:O.number().int().positive()}),sliceLineCount:O.number().int().positive(),sliceSha256:O.string().regex(/^[0-9a-f]{64}$/,"sliceSha256 must be 64 lowercase hex chars")}),xq=O.object({kind:O.literal("pattern-absent"),patternId:zc,detectorVersion:O.string().min(1),rationale:O.string().min(1)}),Eq=O.object({detectorAtGeneration:O.string().min(1),fingerprintAtGeneration:O.string().nullable(),cardOccurrenceCountAtGeneration:O.number().int().nonnegative(),cardLastSeenAtGeneration:O.string().datetime(),generatedBy:O.literal("replay-fixture")}),Rq=O.enum(["draft","approved","rejected","superseded"]),Vc=O.object({schemaVersion:O.literal(1),evalCaseId:O.string().regex(/^[a-z0-9][a-z0-9-]*$/,"evalCaseId must be lowercase alphanumeric with hyphens"),cardSlug:O.string().regex(/^[a-z0-9][a-z0-9-]*$/),proposalId:O.string().regex(/^[a-z0-9][a-z0-9-]*$/).nullable(),title:O.string().min(1).max(200),createdAt:O.string().datetime(),kind:O.literal("replay"),replay:Tq,assertion:xq,provenance:Eq,status:Rq,notes:O.array(Sf).default([])}),GR=O.object({timestamp:O.string().datetime(),event:O.enum(["created","triaged","superseded"]),evalCaseId:O.string(),cardSlug:O.string(),proposalId:O.string().nullable(),kind:O.literal("replay")}),qR=Object.freeze(["**/auth/**","**/billing/**","**/secrets/**","**/credentials*",".env",".env.*","pnpm-lock.yaml","package-lock.json","yarn.lock",".github/workflows/**","dist/**","build/**","node_modules/**",".git/**","**/.afk/config/**","**/.afk/state/**"]);function YR(e){let t=Pr();Yc(t)||JR(t,{recursive:!0});let n=xi(e.slug),r=Kc(e.slug),o=Ci(n),s=Iq(o,e),i=o===void 0,a=s.evidence.length-(o?.evidence.length??0),l=i?"created":a>0?"updated":"merged-noop",c=_i.parse(s);return Oq(n,c),$q(r,Ii(c)),Mq({timestamp:Dq(),event:l,slug:c.slug,pattern:c.pattern,occurrenceCount:c.occurrenceCount,evidenceAdded:Math.max(0,a)}),{slug:c.slug,event:l,occurrenceCount:c.occurrenceCount,evidenceAdded:Math.max(0,a),jsonPath:n,markdownPath:r}}function Ci(e){if(Yc(e))try{let t=Aq(e,"utf-8"),n=JSON.parse(t),r=_i.safeParse(n);return r.success?r.data:void 0}catch{return}}function Iq(e,t){if(!e)return{schemaVersion:1,slug:t.slug,title:t.title,pattern:t.pattern,severity:t.severity,status:"open",firstSeen:t.observedAt,lastSeen:t.observedAt,occurrenceCount:t.evidence.length,evidence:t.evidence,detail:t.detail,notes:[]};if(e.slug!==t.slug)throw new Error(`card-writer: slug mismatch on merge (existing='${e.slug}', detection='${t.slug}')`);let n=Pq(e.evidence,t.evidence),r=Lq(e.firstSeen,t.observedAt),o=Fq(e.lastSeen,t.observedAt),s=Nq(e.severity,t.severity);return{schemaVersion:1,slug:e.slug,title:t.title,pattern:e.pattern,severity:s,status:e.status,firstSeen:r,lastSeen:o,occurrenceCount:n.length,evidence:n,detail:t.detail,notes:e.notes}}function Pq(e,t){let n=s=>`${s.sessionId}::${s.eventIndices[0]??"NA"}`,r=new Set(e.map(n)),o=[...e];for(let s of t){let i=n(s);r.has(i)||(r.add(i),o.push(s))}return o}function Ii(e){let t=[];t.push(`# ${e.slug} \u2014 \`${e.severity}\` \u2014 \`${e.status}\``),t.push(""),t.push(e.title),t.push(""),t.push(`**Pattern:** \`${e.pattern}\` \xB7 **Occurrences:** ${e.occurrenceCount} \xB7 **First seen:** ${e.firstSeen} \xB7 **Last seen:** ${e.lastSeen}`),t.push(""),t.push("## Evidence"),t.push("");for(let n of e.evidence)t.push(`### Session \`${n.sessionId}\``),t.push(""),t.push(`- Trace: \`${n.tracePath}\``),t.push(`- Event seqs: ${n.eventIndices.join(", ")}`),n.annotation&&t.push(`- Note: ${n.annotation}`),t.push(""),t.push("```jsonl"),t.push(n.excerpt),t.push("```"),t.push("");if(t.push("## Detail"),t.push(""),t.push("```json"),t.push(JSON.stringify(e.detail,null,2)),t.push("```"),t.push(""),t.push("## Triage notes"),t.push(""),e.notes.length>0){for(let n of e.notes)t.push(`- _${n.at}_ \u2014 ${n.text}`);t.push("")}else t.push('_(none \u2014 add one with `afk improve cards triage <slug> --note "\u2026"`)_'),t.push("");return t.join(`
2413
+ `)){let o=r.split(" "),s=o.length>=3?o:r.trim().split(/\s+/);if(s.length<3||s[2]?.trim()!==t)continue;let i=s[0]?.trim()??"-",a=s[1]?.trim()??"0",l={};if(i!=="-"&&i!==""){let u=Number.parseInt(i,10);Number.isFinite(u)&&(l.pid=u)}let c=Number.parseInt(a,10);return Number.isFinite(c)&&(l.lastExitStatus=c),l}}function mf(e){let t=Jn(e),n={name:e,label:Ge(e),installed:mG(t),plistPath:t,logFile:ts(e)};if(!n.installed)return n;try{let r=pG("launchctl",["list"],{encoding:"utf-8",stdio:["ignore","pipe","ignore"],timeout:Ir}),o=fG(r,n.label);o&&(o.pid!==void 0&&(n.pid=o.pid),o.lastExitStatus!==void 0&&(n.lastExitStatus=o.lastExitStatus))}catch{}return n}import{execFileSync as Wc}from"child_process";import{existsSync as vR,mkdirSync as kR,readFileSync as xke,renameSync as gG,rmSync as hG,unlinkSync as yG,writeFileSync as bG}from"fs";import{homedir as wG}from"os";import{dirname as SG}from"path";function TR(e,t={}){let n=Jn(e);if(vR(n))return{kind:"already-installed",plistPath:n,label:Ge(e)};let r;try{r=pf(e,t._entrypointExistsCheck)}catch(c){return{kind:"failed",reason:c.message}}let o=t.noWatch?void 0:SR(e,t._entrypointExistsCheck),s=ts(e);kR(df(),{recursive:!0}),kR(SG(s),{recursive:!0});let i={label:Ge(e),programArguments:r,workingDirectory:wG(),standardOutPath:s,standardErrorPath:s,...o?{watchPaths:o}:{},...t.environment?{environmentVariables:t.environment}:{}},a=bR(i),l=`${n}.tmp`;try{bG(l,a,{encoding:"utf-8",flag:"wx",mode:384})}catch(c){return{kind:"failed",reason:`Failed to write plist (tmp ${l}): ${c.message}`}}try{gG(l,n)}catch(c){try{yG(l)}catch{}return{kind:"failed",reason:`Failed to install plist (rename ${l} \u2192 ${n}): ${c.message}`}}if(t.skipBootstrap)return{kind:"installed",plistPath:n,label:Ge(e),watchPathsActive:!!o};try{Wc("launchctl",["bootstrap",vi(),n],{stdio:["ignore","pipe","pipe"],timeout:Ir})}catch(c){let u=c.message;if(c.code==="EALREADY"||/\b37\b/.test(u))return{kind:"failed",reason:`Service already loaded \u2014 run 'afk service restart ${e}' to reload with the new plist.`};if(/already bootstrapped|already loaded/i.test(u)){let d="";try{Wc("launchctl",["bootout",`${vi()}/${Ge(e)}`],{stdio:["ignore","pipe","pipe"],timeout:Ir})}catch(p){d=p.message}try{Wc("launchctl",["bootstrap",vi(),n],{stdio:["ignore","pipe","pipe"],timeout:Ir})}catch(p){let f=p.message;return{kind:"failed",reason:`Bootstrap failed: ${d?`${f} (prior bootout: ${d})`:f}`}}}else return{kind:"failed",reason:`Bootstrap failed: ${u}`}}return{kind:"installed",plistPath:n,label:Ge(e),watchPathsActive:!!o}}function xR(e,t={}){let n=Jn(e);if(!vR(n))return{kind:"not-installed",plistPath:n};if(!t.skipBootout)try{Wc("launchctl",["bootout",`${vi()}/${Ge(e)}`],{stdio:"ignore",timeout:Ir})}catch{}try{hG(n,{force:!0})}catch(r){return{kind:"failed",reason:`Failed to remove plist: ${r.message}`}}return{kind:"uninstalled",plistPath:n}}function Ti(){if(process.platform!=="darwin")throw new Error(`'afk service' uses macOS launchd and is only supported on darwin. Detected: ${process.platform}.`)}function Hc(e){let t=e.toLowerCase();if(es.includes(t))return t;throw new Error(`Unknown service '${e}'. Supported: ${es.join(", ")}.`)}function AR(e){let t=e.command("service").description("Manage AFK background services via macOS launchd (always-on, auto-restart)");t.command("install <name>").description(`Install <${es.join("|")}> as a LaunchAgent that starts on login and relaunches on crash`).option("--no-watch","Disable WatchPaths (no auto-restart on rebuild)").option("--dry-run","Write the plist but do not call launchctl",!1).action((n,r)=>{try{Ti();let o=Hc(n),s=TR(o,{noWatch:r.watch===!1,skipBootstrap:!!r.dryRun});if(s.kind==="already-installed"&&(console.log(qe.yellow(`\u26A0 ${s.label} already installed at ${s.plistPath}`)),console.log(m.meta(` Run 'afk service uninstall ${o}' first to reinstall.`)),process.exit(1)),s.kind==="failed"&&(console.error(qe.red(`\u2717 Install failed: ${s.reason}`)),process.exit(1)),console.log(qe.green(`\u2713 Installed ${s.label}`)),console.log(m.meta(` Plist: ${s.plistPath}`)),console.log(m.meta(` Log: ${ts(o)}`)),s.watchPathsActive?console.log(m.meta(" WatchPaths: active \u2014 service auto-restarts on rebuild.")):console.log(m.meta(" WatchPaths: off \u2014 manual 'afk service restart' needed after updates.")),r.dryRun){let i=process.getuid?.()??501;console.log(m.info(" (dry-run) launchctl bootstrap was skipped; service is NOT yet running.")),console.log(m.meta(` Load manually: launchctl bootstrap gui/${i} ${s.plistPath}`))}else console.log(m.meta(` Status: afk service status ${o}`))}catch(o){K(o)}}),t.command("uninstall <name>").description("Stop the service and remove its LaunchAgent plist").action(n=>{try{Ti();let r=Hc(n),o=xR(r);if(o.kind==="not-installed"){console.log(qe.yellow(`\u26A0 ${Ge(r)} is not installed (no plist at ${o.plistPath})`));return}o.kind==="failed"&&(console.error(qe.red(`\u2717 Uninstall failed: ${o.reason}`)),process.exit(1)),console.log(qe.green(`\u2713 Uninstalled ${Ge(r)}`)),console.log(m.meta(` Removed: ${o.plistPath}`))}catch(r){K(r)}}),t.command("status [name]").description("Show running PID, last exit status, and log file for one or all services").action(n=>{try{if(Ti(),n){let r=mf(Hc(n));RR(r);return}for(let r of es)RR(mf(r)),console.log("")}catch(r){K(r)}}),t.command("list").description("List recognised service names and whether each is installed").action(()=>{try{Ti(),console.log(qe.bold("AFK services:"));for(let n of es){let r=Jn(n),o=ER(r),s=o?qe.green("\u25CF"):qe.dim("\u25CB"),i=o?m.meta("installed"):m.meta("not installed");console.log(` ${s} ${n.padEnd(10)} ${i} ${m.meta(r)}`)}}catch(n){K(n)}}),t.command("restart <name>").description("Restart the service (launchctl kickstart -k)").action(n=>{try{Ti();let r=Hc(n),o=Jn(r);if(ER(o)||(console.error(qe.red(`\u2717 ${Ge(r)} is not installed. Run 'afk service install ${r}' first.`)),process.exit(1)),typeof process.getuid!="function")throw new Error("process.getuid is unavailable \u2014 afk service restart requires a POSIX system.");let s=process.getuid();try{kG("launchctl",["kickstart","-k",`gui/${s}/${Ge(r)}`],{stdio:["ignore","pipe","pipe"],timeout:8e3}),console.log(qe.green(`\u2713 Restarted ${Ge(r)}`))}catch(i){console.error(qe.red(`\u2717 Restart failed: ${i.message}`)),process.exit(1)}}catch(r){K(r)}})}function RR(e){if(console.log(qe.bold(`${e.label}`)),!e.installed){console.log(` ${qe.dim("\u25CB")} Not installed`),console.log(m.meta(` Plist: ${e.plistPath}`)),console.log(m.meta(` Install: afk service install ${e.name}`));return}e.pid!==void 0?console.log(` ${qe.green("\u25CF")} Running (PID ${e.pid})`):(console.log(` ${qe.yellow("\u25CF")} Installed but not running`),e.lastExitStatus!==void 0&&e.lastExitStatus!==0&&console.log(m.meta(` Last exit status: ${e.lastExitStatus}`))),console.log(m.meta(` Plist: ${e.plistPath}`)),console.log(m.meta(` Log: ${e.logFile}`))}import{readFileSync as vG,readdirSync as TG,statSync as xG}from"fs";import{join as bf}from"path";W();import{join as et}from"path";function ff(){return et(Tt(),"improve")}function Pr(){return et(ff(),"failure-cards")}function _R(){return et(Pr(),".index.jsonl")}function xi(e){return et(Pr(),`${e}.json`)}function Kc(e){return et(Pr(),`${e}.md`)}function CR(){return et(ye(),"witness")}function Mr(){return et(ff(),"proposals")}function IR(){return et(Mr(),".index.jsonl")}function gf(e){return et(Mr(),`${e}.json`)}function PR(e){return et(Mr(),`${e}.md`)}function Yn(){return et(ff(),"eval-cases")}function MR(){return et(Yn(),".index.jsonl")}function hf(e){return et(Yn(),`${e}.json`)}function yf(e){return et(Yn(),`${e}.fixture.jsonl`)}function OR(e){return et(Yn(),`${e}.md`)}G();function $R(e){let t=e.trim(),n=/^(\d+)\s*([smhd])$/i.exec(t);if(!n)return;let r=Number.parseInt(n[1]??"0",10),o=(n[2]??"").toLowerCase();if(!(!Number.isFinite(r)||r<=0))switch(o){case"s":return r*1e3;case"m":return r*60*1e3;case"h":return r*60*60*1e3;case"d":return r*24*60*60*1e3;default:return}}function DR(e={}){let t=e.witnessRoot??CR(),n=e.afkHome??T.AFK_HOME??RG(),r=e.sinceMs,o={sessionsScanned:0,sessionsSkippedOld:0,sessionsSkippedEmpty:0,invalidLineCount:0,sessions:[]},s;try{s=TG(t)}catch{return o}for(let i of s){if(i.startsWith("."))continue;let a=bf(t,i),l;try{l=xG(a)}catch{continue}if(!l.isDirectory())continue;if(r!==void 0&&l.mtimeMs<r){o.sessionsSkippedOld+=1;continue}let c=bf(a,"trace.jsonl"),u;try{u=vG(c,"utf-8")}catch{o.sessionsSkippedEmpty+=1;continue}let d=AG(c,n),p=EG({sessionId:i,tracePath:c,relativeTracePath:d,content:u,sessionMtimeMs:l.mtimeMs});o.sessions.push(p),o.sessionsScanned+=1,o.invalidLineCount+=p.invalidLineCount}return o}function EG(e){let{sessionId:t,tracePath:n,relativeTracePath:r,content:o,sessionMtimeMs:s}=e,i=[],a=0,l=o.split(`
2414
+ `);for(let c=0;c<l.length;c+=1){let u=l[c]??"";if(u.trim()==="")continue;let d;try{d=JSON.parse(u)}catch{a+=1;continue}let p=iS.safeParse(d);if(!p.success){a+=1;continue}i.push({sessionId:t,tracePath:n,relativeTracePath:r,lineNumber:c+1,rawLine:u,event:p.data})}return{sessionId:t,tracePath:n,relativeTracePath:r,sessionMtimeMs:s,events:i,invalidLineCount:a}}function RG(){let e=T.AFK_HOME;return e&&e.length>0?e:bf(T.HOME??"",".afk")}function AG(e,t){if(!t)return e;if(e.startsWith(t)){let n=e.slice(t.length);return n.startsWith("/")&&(n=n.slice(1)),n}return e}import{createHash as _G}from"crypto";var Or=4,CG=8,IG="v1-bytes-tuple";function LR(e,t={}){let n=t.minRepeats??Or;if(n<2)throw new Error(`minRepeats must be >= 2 (got ${n})`);let r=[];for(let o of e){let s=PG(o,n);r.push(...s)}return r}function PG(e,t){let n=MG(e.events),r=OG(n),o=[];for(let[s,i]of r.entries()){let a=$G(i,t);for(let l of a)o.push(LG(e,l,s))}return o}function MG(e){let t=new Map,n=[];for(let r of e){let o=r.event;if(o.kind!=="tool_call")continue;if(o.payload.phase==="started"){t.set(o.payload.toolUseId,{seq:o.seq,name:o.payload.name,inputBytes:o.payload.inputBytes,subagentId:o.payload.subagentId});continue}let s=t.get(o.payload.toolUseId);if(!s)continue;t.delete(o.payload.toolUseId);let i=DG({name:o.payload.name,inputBytes:s.inputBytes,resultBytes:o.payload.resultBytes,isError:o.payload.isError,subagentId:s.subagentId});n.push({toolUseId:o.payload.toolUseId,startedSeq:s.seq,completedSeq:o.seq,completedLineNumber:r.lineNumber,name:o.payload.name,inputBytes:s.inputBytes,resultBytes:o.payload.resultBytes,isError:o.payload.isError,subagentId:s.subagentId,rawLine:r.rawLine,fingerprint:i})}return n}function OG(e){let t=new Map;for(let n of e){let r=n.subagentId??"root",o=t.get(r);o?o.push(n):t.set(r,[n])}return t}function $G(e,t){let n=[],r=0;for(;r<e.length;){let o=e[r];if(!o){r+=1;continue}let s=r+1;for(;s<e.length;){let a=e[s];if(!a||a.fingerprint!==o.fingerprint)break;s+=1}s-r>=t&&n.push(e.slice(r,s)),r=s>r?s:r+1}return n}function DG(e){let t=[e.name,String(e.inputBytes),String(e.resultBytes),e.isError?"1":"0",e.subagentId??""].join("|");return _G("sha256").update(t).digest("hex")}function LG(e,t,n){let r=t[0];if(!r)throw new Error("repeated-tool-use: empty run");let o=jG(r.name,r.fingerprint),s=new Date().toISOString(),i=t.slice(0,CG),a=[{sessionId:e.sessionId,tracePath:e.relativeTracePath,eventIndices:i.map(l=>l.completedSeq),excerpt:FG(i),annotation:NG(t,n)}];return{slug:o,title:BG(r.name,t.length),pattern:"repeated-tool-use",severity:UG(t.length),observedAt:s,evidence:a,detail:{detector:"repeated-tool-use@v1",fingerprintAlgorithm:IG,fingerprint:r.fingerprint,toolName:r.name,runLength:t.length,agentContext:n,inputBytes:r.inputBytes,resultBytes:r.resultBytes,isError:r.isError,toolUseIds:t.map(l=>l.toolUseId),completedSeqs:t.map(l=>l.completedSeq)}}}function FG(e){let t=e.map(n=>n.rawLine).join(`
2415
+ `);return t.length<=2e3?t:t.slice(0,1997)+"..."}function NG(e,t){let n=e[0],r=e[e.length-1];return!n||!r?"":[`${e.length}\xD7 consecutive '${n.name}' calls in ${t} context`,`(seq ${n.completedSeq}\u2026${r.completedSeq},`,`inputBytes=${n.inputBytes}, resultBytes=${n.resultBytes},`,`isError=${n.isError})`].join(" ")}function BG(e,t){return`'${e}' tool repeated ${t}\xD7 with identical fingerprint`}function UG(e){return e>=10?"high":e>=4?"medium":"low"}function jG(e,t){let n=e.toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,""),r=t.slice(0,12);return`repeated-tool-${n.length>0?n:"tool"}-${r}`}var WG=new Set(["budget_exceeded","timeout","hook_blocked","abort","iteration_cap","max_turns_exceeded"]);function NR(e,t={}){let n=t.minOccurrences??1;if(n<1)throw new Error(`minOccurrences must be >= 1 (got ${n})`);let r=new Map;for(let s of e)for(let i of s.events){let a=i.event;if(a.kind!=="closure")continue;let l=a.payload.reason;if(!WG.has(l))continue;let c={sessionId:s.sessionId,relativeTracePath:s.relativeTracePath,seq:a.seq,rawLine:i.rawLine,reason:l,finalCostUsd:a.payload.finalCostUsd,finalTurnCount:a.payload.finalTurnCount},u=r.get(l);u?u.push(c):r.set(l,[c])}let o=[];for(let[s,i]of r.entries())i.length<n||o.push(KG(s,i));return o}var HG=8;function KG(e,t){let n=qG(e),r=new Date().toISOString(),s=t.slice(0,HG).map(l=>({sessionId:l.sessionId,tracePath:l.relativeTracePath,eventIndices:[l.seq],excerpt:zG(l.rawLine),annotation:`closure.reason='${l.reason}' \xB7 cost=${JG(l.finalCostUsd)} \xB7 turns=${l.finalTurnCount}`})),i=t.reduce((l,c)=>l+c.finalCostUsd,0),a=t.reduce((l,c)=>l+c.finalTurnCount,0)/t.length;return{slug:n,title:`Session closure reason '${e}' across ${t.length} session${t.length===1?"":"s"}`,pattern:"closure-anomaly",severity:GG(e,t.length),observedAt:r,evidence:s,detail:{detector:"closure-anomaly@v1",closureReason:e,affectedSessions:t.length,totalCostUsd:FR(i),avgTurnCount:VG(a),maxCostUsd:FR(Math.max(...t.map(l=>l.finalCostUsd))),sessionIds:t.map(l=>l.sessionId),seqs:t.map(l=>l.seq)}}}function GG(e,t){switch(e){case"budget_exceeded":case"timeout":return"high";case"hook_blocked":case"iteration_cap":case"max_turns_exceeded":return t>=3?"high":"medium";case"abort":return t>=3?"medium":"low";default:return"low"}}function qG(e){let t=e.toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"");return`closure-anomaly-${t.length>0?t:"unknown"}`}function zG(e){return e.length<=2e3?e:e.slice(0,1997)+"..."}function JG(e){return`$${e.toFixed(4)}`}function FR(e){return Math.round(e*1e4)/1e4}function VG(e){return Math.round(e*100)/100}import{createHash as YG}from"crypto";var $r=2,XG="v1-hook-reason-tuple",ZG=8;function BR(e,t={}){let n=t.minOccurrences??$r;if(n<1)throw new Error(`minOccurrences must be >= 1 (got ${n})`);let r=new Map;for(let s of e)for(let i of s.events){let a=i.event;if(a.kind!=="hook_decision"||a.payload.hookEvent!=="SubagentStart"||a.payload.decision!=="block")continue;let l=a.payload.reason??"",c=a.payload.blockedTool,u=QG({hookEvent:a.payload.hookEvent,reason:l,blockedTool:c}),d={sessionId:s.sessionId,relativeTracePath:s.relativeTracePath,seq:a.seq,rawLine:i.rawLine,reason:l,blockedTool:c,injectedContextBytes:a.payload.injectedContextBytes},p=r.get(u);p?p.push(d):r.set(u,[d])}let o=[];for(let[s,i]of r.entries())i.length<n||o.push(tq(s,i));return o}function QG(e){let t=[e.hookEvent,e.reason,e.blockedTool??""].join("|");return YG("sha256").update(t).digest("hex")}function eq(e){return`subagent-block-${e.slice(0,12)}`}function tq(e,t){let n=t[0];if(!n)throw new Error("subagent-block: empty sighting bucket");let r=eq(e),o=new Date().toISOString(),i=t.slice(0,ZG).map(l=>({sessionId:l.sessionId,tracePath:l.relativeTracePath,eventIndices:[l.seq],excerpt:sq(l.rawLine),annotation:oq(l)})),a=new Set(t.map(l=>l.sessionId)).size;return{slug:r,title:rq(n.reason,t.length,a),pattern:"subagent-block",severity:nq(t.length,a),observedAt:o,evidence:i,detail:{detector:"subagent-block@v1",fingerprintAlgorithm:XG,fingerprint:e,hookEvent:"SubagentStart",reason:n.reason,blockedTool:n.blockedTool??null,blockCount:t.length,distinctSessions:a,sessionIds:t.map(l=>l.sessionId),seqs:t.map(l=>l.seq)}}}function nq(e,t){return e>=6||t>=3?"high":e>=3?"medium":"low"}function rq(e,t,n){let r=e.length>80?e.slice(0,77)+"...":e,o=r.length>0?`: "${r}"`:"";return`SubagentStart hook blocked ${t}\xD7 across ${n} session${n===1?"":"s"}${o}`}function oq(e){let t=[`seq ${e.seq}`];return e.reason&&t.push(`reason="${e.reason.slice(0,200)}"`),e.blockedTool&&t.push(`blockedTool=${e.blockedTool}`),typeof e.injectedContextBytes=="number"&&t.push(`injectedContextBytes=${e.injectedContextBytes}`),t.join(" \xB7 ")}function sq(e){return e.length<=2e3?e:e.slice(0,1997)+"..."}function UR(e,t={}){let n=t.minFailures??3,r=t.minFailureRate??.25;if(n<1)throw new Error(`minFailures must be >= 1 (got ${n})`);if(r<=0||r>1)throw new Error(`minFailureRate must be in (0, 1] (got ${r})`);let o=new Map;for(let i of e)for(let a of i.events){let l=a.event;if(l.kind!=="tool_call"||l.payload.phase!=="completed")continue;let c=iq(o,l.payload.name);c.totalCalls+=1,l.payload.isError&&(c.failures.push({sessionId:i.sessionId,relativeTracePath:i.relativeTracePath,seq:l.seq,rawLine:a.rawLine,resultBytes:l.payload.resultBytes,durationMs:l.payload.durationMs,truncated:l.payload.truncated}),c.affectedSessions.add(i.sessionId),l.payload.truncated&&(c.truncatedFailureCount+=1))}let s=[];for(let i of o.values()){if(i.failures.length<n)continue;let a=i.failures.length/i.totalCalls;a<r||s.push(aq(i,a))}return s.sort((i,a)=>i.slug.localeCompare(a.slug)),s}function iq(e,t){let n=e.get(t);return n||(n={toolName:t,totalCalls:0,failures:[],affectedSessions:new Set,truncatedFailureCount:0},e.set(t,n)),n}function aq(e,t){let n=uq(e.toolName),r=new Date().toISOString(),s=e.failures.slice(0,8).map(l=>({sessionId:l.sessionId,tracePath:l.relativeTracePath,eventIndices:[l.seq],excerpt:dq(l.rawLine),annotation:`isError=true \xB7 resultBytes=${l.resultBytes} \xB7 durationMs=${l.durationMs}${l.truncated?" \xB7 truncated":""}`})),a=e.failures.reduce((l,c)=>l+c.durationMs,0)/e.failures.length;return{slug:n,title:cq(e.toolName,e.failures.length,e.totalCalls,t),pattern:"tool-failure-density",severity:lq(e.failures.length,t),observedAt:r,evidence:s,detail:{detector:"tool-failure-density@v1",toolName:e.toolName,totalCalls:e.totalCalls,failureCount:e.failures.length,failureRate:pq(t),affectedSessionCount:e.affectedSessions.size,truncatedFailureCount:e.truncatedFailureCount,avgFailureDurationMs:mq(a),sessionIds:Array.from(e.affectedSessions),seqs:e.failures.map(l=>l.seq)}}}function lq(e,t){return t>=1||t>=.5?"high":t>=.25?e>=10?"high":"medium":e>=10?"medium":"low"}function cq(e,t,n,r){let o=(r*100).toFixed(1);return`'${e}' tool failed ${t}/${n} calls (${o}%)`}function uq(e){let t=e.toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"");return`tool-failure-${t.length>0?t:"unknown"}`}function dq(e){return e.length<=2e3?e:e.slice(0,1997)+"..."}function pq(e){return Math.round(e*1e4)/1e4}function mq(e){return Math.round(e*100)/100}var wf=Object.freeze([{name:"repeated-tool-use",description:`Tool fired \u2265N consecutive times with identical fingerprint (default ${Or})`,run:(e,t)=>LR(e,{minRepeats:t.minRepeats??Or})},{name:"closure-anomaly",description:`Session closure reason \u2208 {budget_exceeded,timeout,hook_blocked,abort,iteration_cap,max_turns_exceeded} (default \u2265${1})`,enabledByDefault:!1,run:(e,t)=>NR(e,{minOccurrences:t.closureAnomalyMinOccurrences??1})},{name:"subagent-block",description:`Same SubagentStart hook block reason recurring across \u2265N events (default ${$r})`,enabledByDefault:!1,run:(e,t)=>BR(e,{minOccurrences:t.subagentBlockMinOccurrences??$r})},{name:"tool-failure-density",description:`Tool with \u2265N failures (isError: true) AND failure rate \u2265R (defaults: ${3} failures, ${.25} rate)`,enabledByDefault:!1,run:(e,t)=>UR(e,{minFailures:t.toolFailureMinFailures??3,minFailureRate:t.toolFailureMinRate??.25})}]);function jR(e,t,n,r){let o=[];for(let s of wf){if(n!==void 0){if(!n.has(s.name))continue}else if(r!==!0&&s.enabledByDefault===!1)continue;let i=s.run(e,t);o.push(...i)}return o}function Gc(){return wf.map(e=>e.name)}function qc(){return wf.filter(e=>e.enabledByDefault===!1).map(e=>e.name)}import{existsSync as Yc,mkdirSync as JR,readFileSync as Aq,readdirSync as _q,renameSync as VR,writeFileSync as vf}from"fs";import{join as Cq}from"path";import{z as O}from"zod";var zc=O.enum(["repeated-tool-use","subagent-block","closure-anomaly","tool-failure-density"]),Jc=O.enum(["low","medium","high"]),fq=O.enum(["open","deferred","resolved"]),WR=O.object({sessionId:O.string().min(1),tracePath:O.string().min(1),eventIndices:O.array(O.number().int().nonnegative()).min(1),excerpt:O.string().max(2e3),annotation:O.string().optional()}),Sf=O.object({at:O.string().datetime(),text:O.string()}),_i=O.object({schemaVersion:O.literal(1),slug:O.string().regex(/^[a-z0-9][a-z0-9-]*$/,"slug must be lowercase alphanumeric with hyphens"),title:O.string().min(1).max(200),pattern:zc,severity:Jc,status:fq,firstSeen:O.string().datetime(),lastSeen:O.string().datetime(),occurrenceCount:O.number().int().nonnegative(),evidence:O.array(WR).min(1),detail:O.record(O.string(),O.unknown()),notes:O.array(Sf).default([])}),cve=O.object({slug:O.string().regex(/^[a-z0-9][a-z0-9-]*$/),title:O.string().min(1).max(200),pattern:zc,severity:Jc,observedAt:O.string().datetime(),evidence:O.array(WR).min(1),detail:O.record(O.string(),O.unknown())}),HR=O.object({timestamp:O.string().datetime(),event:O.enum(["created","updated","merged-noop"]),slug:O.string(),pattern:zc,occurrenceCount:O.number().int().nonnegative(),evidenceAdded:O.number().int().nonnegative()}),gq=O.enum(["prompt-defect","schema-too-strict","schema-too-loose","tool-output-shape","hook-overreach","retry-policy","timeout-too-low","cost-control","dispatcher-bug","detector-needs-tuning","unknown"]),hq=O.enum(["safe","moderate","high","forbidden"]),yq=O.enum(["low","medium","high"]),bq=O.object({cardSlug:O.string(),eventIndices:O.array(O.number().int().nonnegative()).min(1),annotation:O.string().optional()}),wq=O.object({path:O.string().min(1),rationale:O.string(),riskTier:hq,confidence:yq}),Sq=O.object({unitTests:O.array(O.string()),evalCases:O.array(O.string()),smokeChecks:O.array(O.string()),manualChecks:O.array(O.string())}),kq=O.object({forbiddenPaths:O.array(O.string()),requiresExplicitApproval:O.boolean()}),vq=O.enum(["draft","approved","rejected","superseded"]),kf=O.object({schemaVersion:O.literal(1),proposalId:O.string().regex(/^[a-z0-9][a-z0-9-]*$/),cardSlug:O.string().regex(/^[a-z0-9][a-z0-9-]*$/),title:O.string().min(1).max(200),hypothesis:O.string().min(1),rootCauseClass:gq,evidenceRefs:O.array(bq).min(1),fixSketch:O.string().min(1),likelyFiles:O.array(wq),riskLevel:Jc,validationPlan:Sq,scopeFreeze:kq,generatedBy:O.enum(["template","llm"]),createdAt:O.string().datetime(),status:vq,notes:O.array(Sf).default([])}),KR=O.object({timestamp:O.string().datetime(),event:O.enum(["created","triaged","superseded"]),proposalId:O.string(),cardSlug:O.string(),generatedBy:O.enum(["template","llm"]),riskLevel:Jc}),Tq=O.object({sourceSessionId:O.string().min(1),sourceTracePath:O.string().min(1),fixturePath:O.string().min(1),evidenceRowIndex:O.number().int().nonnegative(),evidenceEventIndices:O.array(O.number().int().nonnegative()).min(1),sliceLineRange:O.object({startLine:O.number().int().positive(),endLine:O.number().int().positive()}),sliceLineCount:O.number().int().positive(),sliceSha256:O.string().regex(/^[0-9a-f]{64}$/,"sliceSha256 must be 64 lowercase hex chars")}),xq=O.object({kind:O.literal("pattern-absent"),patternId:zc,detectorVersion:O.string().min(1),rationale:O.string().min(1)}),Eq=O.object({detectorAtGeneration:O.string().min(1),fingerprintAtGeneration:O.string().nullable(),cardOccurrenceCountAtGeneration:O.number().int().nonnegative(),cardLastSeenAtGeneration:O.string().datetime(),generatedBy:O.literal("replay-fixture")}),Rq=O.enum(["draft","approved","rejected","superseded"]),Vc=O.object({schemaVersion:O.literal(1),evalCaseId:O.string().regex(/^[a-z0-9][a-z0-9-]*$/,"evalCaseId must be lowercase alphanumeric with hyphens"),cardSlug:O.string().regex(/^[a-z0-9][a-z0-9-]*$/),proposalId:O.string().regex(/^[a-z0-9][a-z0-9-]*$/).nullable(),title:O.string().min(1).max(200),createdAt:O.string().datetime(),kind:O.literal("replay"),replay:Tq,assertion:xq,provenance:Eq,status:Rq,notes:O.array(Sf).default([])}),GR=O.object({timestamp:O.string().datetime(),event:O.enum(["created","triaged","superseded"]),evalCaseId:O.string(),cardSlug:O.string(),proposalId:O.string().nullable(),kind:O.literal("replay")}),qR=Object.freeze(["**/auth/**","**/billing/**","**/secrets/**","**/credentials*",".env",".env.*","pnpm-lock.yaml","package-lock.json","yarn.lock",".github/workflows/**","dist/**","build/**","node_modules/**",".git/**","**/.afk/config/**","**/.afk/state/**"]);function YR(e){let t=Pr();Yc(t)||JR(t,{recursive:!0});let n=xi(e.slug),r=Kc(e.slug),o=Ci(n),s=Iq(o,e),i=o===void 0,a=s.evidence.length-(o?.evidence.length??0),l=i?"created":a>0?"updated":"merged-noop",c=_i.parse(s);return Oq(n,c),$q(r,Ii(c)),Mq({timestamp:Dq(),event:l,slug:c.slug,pattern:c.pattern,occurrenceCount:c.occurrenceCount,evidenceAdded:Math.max(0,a)}),{slug:c.slug,event:l,occurrenceCount:c.occurrenceCount,evidenceAdded:Math.max(0,a),jsonPath:n,markdownPath:r}}function Ci(e){if(Yc(e))try{let t=Aq(e,"utf-8"),n=JSON.parse(t),r=_i.safeParse(n);return r.success?r.data:void 0}catch{return}}function Iq(e,t){if(!e)return{schemaVersion:1,slug:t.slug,title:t.title,pattern:t.pattern,severity:t.severity,status:"open",firstSeen:t.observedAt,lastSeen:t.observedAt,occurrenceCount:t.evidence.length,evidence:t.evidence,detail:t.detail,notes:[]};if(e.slug!==t.slug)throw new Error(`card-writer: slug mismatch on merge (existing='${e.slug}', detection='${t.slug}')`);let n=Pq(e.evidence,t.evidence),r=Lq(e.firstSeen,t.observedAt),o=Fq(e.lastSeen,t.observedAt),s=Nq(e.severity,t.severity);return{schemaVersion:1,slug:e.slug,title:t.title,pattern:e.pattern,severity:s,status:e.status,firstSeen:r,lastSeen:o,occurrenceCount:n.length,evidence:n,detail:t.detail,notes:e.notes}}function Pq(e,t){let n=s=>`${s.sessionId}::${s.eventIndices[0]??"NA"}`,r=new Set(e.map(n)),o=[...e];for(let s of t){let i=n(s);r.has(i)||(r.add(i),o.push(s))}return o}function Ii(e){let t=[];t.push(`# ${e.slug} \u2014 \`${e.severity}\` \u2014 \`${e.status}\``),t.push(""),t.push(e.title),t.push(""),t.push(`**Pattern:** \`${e.pattern}\` \xB7 **Occurrences:** ${e.occurrenceCount} \xB7 **First seen:** ${e.firstSeen} \xB7 **Last seen:** ${e.lastSeen}`),t.push(""),t.push("## Evidence"),t.push("");for(let n of e.evidence)t.push(`### Session \`${n.sessionId}\``),t.push(""),t.push(`- Trace: \`${n.tracePath}\``),t.push(`- Event seqs: ${n.eventIndices.join(", ")}`),n.annotation&&t.push(`- Note: ${n.annotation}`),t.push(""),t.push("```jsonl"),t.push(n.excerpt),t.push("```"),t.push("");if(t.push("## Detail"),t.push(""),t.push("```json"),t.push(JSON.stringify(e.detail,null,2)),t.push("```"),t.push(""),t.push("## Triage notes"),t.push(""),e.notes.length>0){for(let n of e.notes)t.push(`- _${n.at}_ \u2014 ${n.text}`);t.push("")}else t.push('_(none \u2014 add one with `afk improve cards triage <slug> --note "\u2026"`)_'),t.push("");return t.join(`
2416
2416
  `)}function Mq(e){let t=HR.parse(e),n=_R(),r=Pr();Yc(r)||JR(r,{recursive:!0});try{vf(n,JSON.stringify(t)+`
2417
- `,{flag:"a"})}catch{}}function Oq(e,t){let n=`${e}.tmp-${process.pid}-${Date.now()}`;vf(n,JSON.stringify(t,null,2)),VR(n,e)}function $q(e,t){let n=`${e}.tmp-${process.pid}-${Date.now()}`;vf(n,t),VR(n,e)}function Dq(){return new Date().toISOString()}function Lq(e,t){return e<=t?e:t}function Fq(e,t){return e>=t?e:t}var zR={low:0,medium:1,high:2};function Nq(e,t){return zR[e]>=zR[t]?e:t}function XR(){let e=Pr();if(!Yc(e))return[];let t=[];for(let n of _q(e)){if(!n.endsWith(".json")||n.startsWith("."))continue;let r=Ci(Cq(e,n));r&&t.push({slug:r.slug,title:r.title,pattern:r.pattern,severity:r.severity,status:r.status,occurrenceCount:r.occurrenceCount,firstSeen:r.firstSeen,lastSeen:r.lastSeen})}return t.sort((n,r)=>n.lastSeen!==r.lastSeen?n.lastSeen<r.lastSeen?1:-1:n.slug<r.slug?-1:1),t}function Xc(e){return Ci(xi(e))}import{existsSync as Bq,mkdirSync as Uq,renameSync as ZR,writeFileSync as QR}from"fs";import{dirname as jq}from"path";var Dr=class extends Error{constructor(n,r){super(r);this.code=n;this.name="TriageError"}code};function eA(e,t){let n=xi(e),r=Kc(e),o=Ci(n);if(!o)throw new Dr("card-not-found",`No failure card found for slug '${e}'`);if(t.note!==void 0&&t.note.trim().length===0)throw new Dr("invalid-note","triage note must be non-empty after trim");let s=t.note?.trim(),i=s!==void 0&&s.length>0,a=t.status!==void 0&&t.status!==o.status;if(!i&&!a)throw new Dr("no-change","triage requires at least --note or --status to differ from current");let l=(t.now??(()=>new Date))().toISOString(),c=i?[...o.notes,{at:l,text:s}]:o.notes,u=a?t.status:o.status,d={...o,status:u,notes:c},m=_i.parse(d);return Wq(n),Hq(n,m),Kq(r,Ii(m)),{slug:e,card:m,noteAdded:i,statusChanged:a?{from:o.status,to:u}:void 0,jsonPath:n,markdownPath:r}}function Wq(e){let t=jq(e);Bq(t)||Uq(t,{recursive:!0})}function Hq(e,t){let n=`${e}.tmp-${process.pid}-${Date.now()}`;QR(n,JSON.stringify(t,null,2)),ZR(n,e)}function Kq(e,t){let n=`${e}.tmp-${process.pid}-${Date.now()}`;QR(n,t),ZR(n,e)}var Gq={"repeated-tool-use":{rootCauseClass:"dispatcher-bug",hypothesis:e=>{let t=typeof e.detail.toolName=="string"?e.detail.toolName:"<unknown>",n=typeof e.detail.runLength=="number"?e.detail.runLength:"?";return`The '${t}' tool was dispatched ${n} times in a row with an identical input/output byte fingerprint. This is either (a) the model is stuck retrying the same call without responding to its result, (b) the tool's result shape is too uninformative for the model to make progress, or (c) a productive recursion that happens to share byte counts (rare; the fingerprint caveat is documented on the detector).`},fixSketch:e=>["## Candidate fixes (human picks)","",`**Option A \u2014 make the loop visible.** Surface a clear "no-progress" signal to the model when '${typeof e.detail.toolName=="string"?e.detail.toolName:"<the tool>"}' returns the same result N times in a row. Today the dispatcher just executes the call.`,"",`**Option B \u2014 improve the tool's result shape.** If the model can't distinguish "no results" from "same results," its result is information-poor. Inspect the tool's response and verify it carries enough signal for the model to change its query.`,"","**Option C \u2014 confirm productive recursion.** Open the source trace at the seq values listed in the evidence and inspect the model's reasoning between repeats. If each call's args genuinely differ (and the byte-count collision is the issue), no code change is needed; tune the detector instead.","","_Option C first \u2014 the byte-fingerprint detector has a documented collision caveat. Confirm there is a real loop before changing dispatcher behavior._"].join(`
2417
+ `,{flag:"a"})}catch{}}function Oq(e,t){let n=`${e}.tmp-${process.pid}-${Date.now()}`;vf(n,JSON.stringify(t,null,2)),VR(n,e)}function $q(e,t){let n=`${e}.tmp-${process.pid}-${Date.now()}`;vf(n,t),VR(n,e)}function Dq(){return new Date().toISOString()}function Lq(e,t){return e<=t?e:t}function Fq(e,t){return e>=t?e:t}var zR={low:0,medium:1,high:2};function Nq(e,t){return zR[e]>=zR[t]?e:t}function XR(){let e=Pr();if(!Yc(e))return[];let t=[];for(let n of _q(e)){if(!n.endsWith(".json")||n.startsWith("."))continue;let r=Ci(Cq(e,n));r&&t.push({slug:r.slug,title:r.title,pattern:r.pattern,severity:r.severity,status:r.status,occurrenceCount:r.occurrenceCount,firstSeen:r.firstSeen,lastSeen:r.lastSeen})}return t.sort((n,r)=>n.lastSeen!==r.lastSeen?n.lastSeen<r.lastSeen?1:-1:n.slug<r.slug?-1:1),t}function Xc(e){return Ci(xi(e))}import{existsSync as Bq,mkdirSync as Uq,renameSync as ZR,writeFileSync as QR}from"fs";import{dirname as jq}from"path";var Dr=class extends Error{constructor(n,r){super(r);this.code=n;this.name="TriageError"}code};function eA(e,t){let n=xi(e),r=Kc(e),o=Ci(n);if(!o)throw new Dr("card-not-found",`No failure card found for slug '${e}'`);if(t.note!==void 0&&t.note.trim().length===0)throw new Dr("invalid-note","triage note must be non-empty after trim");let s=t.note?.trim(),i=s!==void 0&&s.length>0,a=t.status!==void 0&&t.status!==o.status;if(!i&&!a)throw new Dr("no-change","triage requires at least --note or --status to differ from current");let l=(t.now??(()=>new Date))().toISOString(),c=i?[...o.notes,{at:l,text:s}]:o.notes,u=a?t.status:o.status,d={...o,status:u,notes:c},p=_i.parse(d);return Wq(n),Hq(n,p),Kq(r,Ii(p)),{slug:e,card:p,noteAdded:i,statusChanged:a?{from:o.status,to:u}:void 0,jsonPath:n,markdownPath:r}}function Wq(e){let t=jq(e);Bq(t)||Uq(t,{recursive:!0})}function Hq(e,t){let n=`${e}.tmp-${process.pid}-${Date.now()}`;QR(n,JSON.stringify(t,null,2)),ZR(n,e)}function Kq(e,t){let n=`${e}.tmp-${process.pid}-${Date.now()}`;QR(n,t),ZR(n,e)}var Gq={"repeated-tool-use":{rootCauseClass:"dispatcher-bug",hypothesis:e=>{let t=typeof e.detail.toolName=="string"?e.detail.toolName:"<unknown>",n=typeof e.detail.runLength=="number"?e.detail.runLength:"?";return`The '${t}' tool was dispatched ${n} times in a row with an identical input/output byte fingerprint. This is either (a) the model is stuck retrying the same call without responding to its result, (b) the tool's result shape is too uninformative for the model to make progress, or (c) a productive recursion that happens to share byte counts (rare; the fingerprint caveat is documented on the detector).`},fixSketch:e=>["## Candidate fixes (human picks)","",`**Option A \u2014 make the loop visible.** Surface a clear "no-progress" signal to the model when '${typeof e.detail.toolName=="string"?e.detail.toolName:"<the tool>"}' returns the same result N times in a row. Today the dispatcher just executes the call.`,"",`**Option B \u2014 improve the tool's result shape.** If the model can't distinguish "no results" from "same results," its result is information-poor. Inspect the tool's response and verify it carries enough signal for the model to change its query.`,"","**Option C \u2014 confirm productive recursion.** Open the source trace at the seq values listed in the evidence and inspect the model's reasoning between repeats. If each call's args genuinely differ (and the byte-count collision is the issue), no code change is needed; tune the detector instead.","","_Option C first \u2014 the byte-fingerprint detector has a documented collision caveat. Confirm there is a real loop before changing dispatcher behavior._"].join(`
2418
2418
  `),likelyFiles:[{path:"src/agent/providers/anthropic-direct/loop.ts",rationale:"Main tool dispatch loop. If a no-progress detector is added at the dispatch boundary, it lives here.",riskTier:"moderate",confidence:"medium"},{path:"src/agent/tools/",rationale:"Tool implementations. If the result shape is information-poor, the specific tool implementation needs the change.",riskTier:"safe",confidence:"low"},{path:"src/improve/scan/detectors/repeated-tool-use.ts",rationale:"If this turns out to be detector noise rather than a real bug, tune here.",riskTier:"safe",confidence:"medium"}],riskFloor:"medium",validationPlan:{unitTests:["pnpm test -- src/improve/scan/detectors/repeated-tool-use","pnpm test -- src/agent/providers/anthropic-direct"],evalCases:[],smokeChecks:["pnpm lint","afk improve scan --since 7d # after fix lands, this pattern should NOT recur"],manualChecks:["Open the trace at the evidence seqs and confirm the calls are truly identical (not just byte-coincident)."]}},"subagent-block":{rootCauseClass:"hook-overreach",hypothesis:e=>{let t=typeof e.detail.reason=="string"?e.detail.reason:"",n=typeof e.detail.blockCount=="number"?e.detail.blockCount:"?",r=typeof e.detail.distinctSessions=="number"?e.detail.distinctSessions:"?",o=t?` with reason "${t.slice(0,200)}"`:" (no reason field on the block events)";return`A SubagentStart hook returned decision:'block' ${n} times across ${r} session(s)${o}. Recurring blocks suggest either (a) the guard is over-broad and trips on legitimate dispatches, (b) the legitimate use case actually needs a refactor to satisfy the guard, or (c) the user has no signal explaining the block and keeps retrying.`},fixSketch:e=>{let t=typeof e.detail.reason=="string"?e.detail.reason:"<not in payload>";return["## Candidate fixes (human picks)","",`**Identify the hook owner first.** The trace's hook_decision event carries the \`reason\` field ("${t}"). Grep the codebase for that literal string \u2014 that locates the hook handler.`,"","```sh","# Replace the quoted string below with the actual reason text from the evidence.",`grep -rn -- "${t.slice(0,60).replace(/"/g,'\\"')}" src/`,"```","","**Option A \u2014 tighten the guard.** If the block fires on dispatches it should not, narrow the predicate. Confirm by adding a unit test that exercises the false-positive case.","","**Option B \u2014 make the refusal legible.** Instead of `decision: 'block'`, return a hook decision that injects a context message via `injectContext`. The parent session then sees a clear no-op message instead of a silent block.","",'**Option C \u2014 accept the block as correct.** If the guard is doing its job, mark the card resolved via `afk improve cards triage <slug> --status resolved --note "..."`. No code change.'].join(`
2419
2419
  `)},likelyFiles:[{path:"src/agent/hooks.ts",rationale:"Hook dispatch core. Only touched if the injectContext mechanism itself needs an extension.",riskTier:"high",confidence:"low"},{path:"src/agent/hook-registry.ts",rationale:"Hook registration. Same caveat \u2014 usually not the right spot.",riskTier:"high",confidence:"low"},{path:"src/agent/subagent-hooks.ts",rationale:"SubagentStart hook dispatch path. The reason text is set by whatever handler is registered here.",riskTier:"moderate",confidence:"medium"},{path:"src/skills/",rationale:"A skill is the typical owner of a SubagentStart hook. Grep for the block reason text to locate the specific handler.",riskTier:"safe",confidence:"medium"}],riskFloor:"medium",validationPlan:{unitTests:["pnpm test -- src/agent/hooks","pnpm test -- src/agent/subagent-hooks","pnpm test -- src/improve/scan/detectors/subagent-block"],evalCases:[],smokeChecks:["pnpm lint","afk improve scan --since 7d # after fix, blocks with same reason should not recur"],manualChecks:["Grep the codebase for the reason text from the evidence to find the hook handler.","Run a session that exercises the legitimate dispatch and confirm it is no longer blocked."]}},"tool-failure-density":{rootCauseClass:"unknown",hypothesis:e=>{let t=typeof e.detail.toolName=="string"?e.detail.toolName:"<unknown>",n=typeof e.detail.failureCount=="number"?e.detail.failureCount:"?",r=typeof e.detail.totalCalls=="number"?e.detail.totalCalls:"?",o=typeof e.detail.failureRate=="number"?`${(e.detail.failureRate*100).toFixed(1)}%`:"?%",s=typeof e.detail.truncatedFailureCount=="number"?e.detail.truncatedFailureCount:0,i=s>0?` ${s} of those failures were also truncated, which often indicates a separate output-shape problem.`:"";return`The '${t}' tool returned isError: true on ${n}/${r} calls (${o}).${i} Likely causes: (a) the tool's handler has a bug, (b) the model is calling the tool with malformed inputs the tool rejects, (c) a permission/hook guard is denying legitimate calls, or (d) the tool legitimately returns isError as a signal to the model and this detector is firing on normal behavior.`},fixSketch:e=>{let t=typeof e.detail.toolName=="string"?e.detail.toolName:"<the tool>";return["## Diagnostic steps (do these first)","",`1. Inspect a representative failure trace: \`cat ~/.afk/state/witness/${(Array.isArray(e.detail.sessionIds)?e.detail.sessionIds:[])[0]??"<session-id>"}/trace.jsonl | grep '"name":"${t}"' | tail -5\``,"2. Look at the events immediately BEFORE each failure \u2014 what did the model send as input?","3. The witness trace does not capture tool args verbatim. To see the actual input, check the session message history under `~/.afk/state/sessions/<sessionId>/`.","","## Candidate fixes (human picks)","","**Option A \u2014 handler bug.** Locate the tool implementation under `src/agent/tools/handlers/` and read its error paths. If a specific failure mode is reachable from common LLM inputs, fix the handler.","","**Option B \u2014 input shape too restrictive.** If the tool's input schema rejects inputs the model naturally produces, either loosen the schema or improve the schema's description so the model can comply.","","**Option C \u2014 permission/hook denial.** Check whether a PreToolUse hook or permission gate is rejecting the call. The dispatcher returns isError: true for hook blocks and permission denials (`src/agent/tools/dispatcher.ts:337\u2013352`).","","**Option D \u2014 accept as normal.** Some tools intentionally return isError as a signal (e.g., grep finding nothing). If this is the case, mark the card resolved with a note explaining why, or tune the detector threshold via `--tool-failure-min-rate`."].join(`
2420
2420
  `)},likelyFiles:[{path:"src/agent/tools/dispatcher.ts",rationale:"Tool dispatch core. Every isError: true path goes through here: hook block, permission denied, handler throw, unknown tool. Read this to understand which class each failure falls into.",riskTier:"high",confidence:"medium"},{path:"src/agent/tools/handlers/",rationale:"Tool handlers. If a specific handler is buggy, the fix lives in the handler file matching the tool name (e.g. handlers/bash.ts for the Bash tool).",riskTier:"moderate",confidence:"medium"},{path:"src/improve/scan/detectors/tool-failure-density.ts",rationale:"If the detector is flagging legitimate isError-as-signal behavior, tune the threshold here or document the tool as expected-failures.",riskTier:"safe",confidence:"low"}],riskFloor:"medium",validationPlan:{unitTests:["pnpm test -- src/improve/scan/detectors/tool-failure-density","pnpm test -- src/agent/tools/dispatcher"],evalCases:[],smokeChecks:["pnpm lint","afk improve scan --only tool-failure-density --since 7d # after fix, failure rate should drop"],manualChecks:["Open the trace at the evidence seqs and read the failure annotations (resultBytes, durationMs).","Inspect the session message history for the actual tool input that triggered the failure.","Decide which of the four root cause classes (handler bug / input shape / permission / detector noise) the failures belong to."]}},"closure-anomaly":{rootCauseClass:"unknown",hypothesis:e=>{let t=typeof e.detail.closureReason=="string"?e.detail.closureReason:"<unknown>",n=typeof e.detail.affectedSessions=="number"?e.detail.affectedSessions:"?",r=typeof e.detail.totalCostUsd=="number"?e.detail.totalCostUsd:null,o=r!==null?` totalling $${r.toFixed(4)}`:"";return`${n} session(s) closed with reason='${t}'${o}. Anomalous closure reasons signal one of: budget mis-configuration, timeout too tight, a hook returning block at the session edge, or an explicit/cascaded abort. The right fix depends on the reason value.`},fixSketch:e=>{let t=typeof e.detail.closureReason=="string"?e.detail.closureReason:"<unknown>",r=(Array.isArray(e.detail.sessionIds)?e.detail.sessionIds:[])[0]??"<session-id>",o=qq(t);return[`## Closure reason: \`${t}\``,"",o,"","## Diagnostic steps","",`1. Inspect the trace for one of the affected sessions: \`cat ~/.afk/state/witness/${r}/trace.jsonl | tail -20\``,"2. Check the events immediately before the closure \u2014 what was the runtime trying to do?","3. Cross-reference with `~/.afk/agent-framework/routing-decisions.jsonl` for any subagent activity at the same timestamp."].join(`
2421
2421
  `)},likelyFiles:[{path:"src/agent/session/agent-session.ts",rationale:"Closure-event emission lives here. Field meanings and the reason classification are owned by this module.",riskTier:"high",confidence:"medium"},{path:"src/agent/session/stream-consumer.ts",rationale:"Budget threshold detection / closure-reason routing. Touch only if the closure CAUSE is here.",riskTier:"high",confidence:"low"},{path:"src/agent/abort-graph.ts",rationale:"Origin tracking for abort-type closures.",riskTier:"moderate",confidence:"low"}],riskFloor:"medium",validationPlan:{unitTests:["pnpm test -- src/agent/session","pnpm test -- src/improve/scan/detectors/closure-anomaly"],evalCases:[],smokeChecks:["pnpm lint"],manualChecks:["Read the closure events at the seqs listed in the evidence.","Confirm the closure reason is correct semantically (not a misclassification)."]}}};function qq(e){switch(e){case"budget_exceeded":return"The monetary ceiling tripped. Confirm `AFK_MAX_BUDGET_USD` is set to a realistic value for the workload; if so, the LLM call shape (cache use, output cap, model choice) is the next place to look.";case"timeout":return"The wall-clock cap fired. Check whether the timeout is configured too tightly for the workload, or whether a tool call is hanging. Tool-call durations in the same trace will tell you which.";case"hook_blocked":return"A hook returned `decision: 'block'` at the session edge. Cross-reference with any `subagent-block` cards on this scan \u2014 the underlying cause is likely the same handler.";case"abort":return"An explicit or cascaded abort closed the session. If origin is `user_signal`, no action needed. If `cascade`/`budget`/`timeout`, the originating cause is the real issue.";case"iteration_cap":return"Loop iteration ceiling tripped. The model could not make progress in N turns. Either the task is genuinely impossible at that budget, or a tool is in an unproductive loop (cross-reference repeated-tool-use cards).";case"max_turns_exceeded":return"Turn ceiling tripped. Same diagnostic as iteration_cap.";default:return"Reason not in the known anomalous set. Inspect the trace and update the detector if this is a new closure variant."}}function nA(e,t){let n=Gq[e.pattern];if(!n)throw new Error(`template-engine: no template for pattern '${e.pattern}' \u2014 add one to TEMPLATES`);let r=(t.now??(()=>new Date))().toISOString(),o=n.hypothesis(e),s=n.fixSketch(e),i=n.likelyFiles.map(c=>({...c})),a=zq(n.riskFloor,i),l=e.evidence.map(c=>({cardSlug:e.slug,eventIndices:[...c.eventIndices],...c.annotation!==void 0?{annotation:c.annotation}:{}}));return{schemaVersion:1,proposalId:t.proposalId,cardSlug:e.slug,title:Yq(e),hypothesis:o,rootCauseClass:n.rootCauseClass,evidenceRefs:l,fixSketch:s,likelyFiles:i,riskLevel:a,validationPlan:Xq(n.validationPlan),scopeFreeze:{forbiddenPaths:[...qR],requiresExplicitApproval:a==="high"},generatedBy:"template",createdAt:r,status:"draft",notes:[]}}function zq(e,t){let n=Jq(t);return n==="forbidden"||n==="high"?"high":n==="moderate"?Vq(e,"medium"):e}function Jq(e){let t=["safe","moderate","high","forbidden"],n=0;for(let r of e){let o=t.indexOf(r.riskTier);o>n&&(n=o)}return t[n]}var tA={low:0,medium:1,high:2};function Vq(e,t){return tA[e]>=tA[t]?e:t}function Yq(e){return`Proposal: address ${e.pattern} \u2014 ${e.title}`.slice(0,200)}function Xq(e){return{unitTests:[...e.unitTests],evalCases:[...e.evalCases],smokeChecks:[...e.smokeChecks],manualChecks:[...e.manualChecks]}}import{randomBytes as Zq}from"crypto";import{existsSync as Zc,mkdirSync as rA,readFileSync as Qq,readdirSync as ez,renameSync as oA,writeFileSync as Tf}from"fs";import{join as tz}from"path";function sA(e){let t=kf.parse(e),n=Mr();Zc(n)||rA(n,{recursive:!0});let r=gf(t.proposalId),o=PR(t.proposalId);return oz(r,t),sz(o,Qc(t)),rz({timestamp:new Date().toISOString(),event:"created",proposalId:t.proposalId,cardSlug:t.cardSlug,generatedBy:t.generatedBy,riskLevel:t.riskLevel}),{proposalId:t.proposalId,jsonPath:r,markdownPath:o}}function iA(e,t={}){let n=(t.now??(()=>new Date))(),r=nz(n),o=t.randomSuffix!==void 0?t.randomSuffix():Zq(3).toString("hex");if(!/^[0-9a-f]{6}$/.test(o))throw new Error(`generateProposalId: randomSuffix must be 6 lowercase hex chars (got '${o}')`);return`${e}-${r}-${o}`}function nz(e){let t=e.getUTCFullYear(),n=String(e.getUTCMonth()+1).padStart(2,"0"),r=String(e.getUTCDate()).padStart(2,"0");return`${t}${n}${r}`}function Qc(e){let t=[];if(t.push(`# ${e.proposalId} \u2014 \`${e.riskLevel}\` \u2014 \`${e.status}\``),t.push(""),t.push(e.title),t.push(""),t.push(`**Card:** \`${e.cardSlug}\` \xB7 **Root cause class:** \`${e.rootCauseClass}\` \xB7 **Generated by:** \`${e.generatedBy}\` \xB7 **Created:** ${e.createdAt}`),t.push(""),t.push("## Hypothesis"),t.push(""),t.push(e.hypothesis),t.push(""),t.push(e.fixSketch),t.push(""),t.push("## Likely files"),t.push(""),e.likelyFiles.length===0)t.push("_(none suggested by the template)_");else{t.push("| Path | Risk | Confidence | Rationale |"),t.push("| --- | --- | --- | --- |");for(let n of e.likelyFiles){let r=n.path.replace(/\|/g,"\\|"),o=n.rationale.replace(/\n/g," ").replace(/\|/g,"\\|");t.push(`| \`${r}\` | ${n.riskTier} | ${n.confidence} | ${o} |`)}}t.push(""),t.push("## Evidence references"),t.push("");for(let n of e.evidenceRefs)t.push(`- card \`${n.cardSlug}\` \xB7 seqs ${n.eventIndices.join(", ")}${n.annotation?` \xB7 ${n.annotation}`:""}`);t.push(""),t.push("## Validation plan"),t.push(""),t.push("**Unit tests:**"),e.validationPlan.unitTests.length===0&&t.push("- _(none)_");for(let n of e.validationPlan.unitTests)t.push(`- \`${n}\``);if(t.push(""),t.push("**Eval cases:**"),e.validationPlan.evalCases.length===0)t.push(`- _(none \u2014 generate one with \`afk improve eval-gen ${e.cardSlug}\`)_`);else for(let n of e.validationPlan.evalCases)t.push(`- ${n}`);t.push(""),t.push("**Smoke checks:**"),e.validationPlan.smokeChecks.length===0&&t.push("- _(none)_");for(let n of e.validationPlan.smokeChecks)t.push(`- \`${n}\``);t.push(""),t.push("**Manual checks:**"),e.validationPlan.manualChecks.length===0&&t.push("- _(none)_");for(let n of e.validationPlan.manualChecks)t.push(`- ${n}`);t.push(""),t.push("## Scope freeze"),t.push(""),t.push(`**Requires explicit approval:** ${e.scopeFreeze.requiresExplicitApproval?"**yes**":"no"}`),t.push(""),t.push("**Forbidden paths** (never auto-edited by a future `apply`):");for(let n of e.scopeFreeze.forbiddenPaths)t.push(`- \`${n}\``);if(t.push(""),t.push("## Triage notes"),t.push(""),e.notes.length===0)t.push("_(none)_");else for(let n of e.notes)t.push(`- _${n.at}_ \u2014 ${n.text}`);return t.push(""),t.join(`
2422
2422
  `)}function aA(){let e=Mr();if(!Zc(e))return[];let t=[];for(let n of ez(e)){if(!n.endsWith(".json")||n.startsWith("."))continue;let r=lA(tz(e,n));r&&t.push({proposalId:r.proposalId,cardSlug:r.cardSlug,title:r.title,riskLevel:r.riskLevel,status:r.status,rootCauseClass:r.rootCauseClass,generatedBy:r.generatedBy,createdAt:r.createdAt})}return t.sort((n,r)=>n.createdAt!==r.createdAt?n.createdAt<r.createdAt?1:-1:n.proposalId<r.proposalId?-1:1),t}function xf(e){return lA(gf(e))}function lA(e){if(Zc(e))try{let t=Qq(e,"utf-8"),n=JSON.parse(t),r=kf.safeParse(n);return r.success?r.data:void 0}catch{return}}function rz(e){let t=KR.parse(e),n=IR(),r=Mr();Zc(r)||rA(r,{recursive:!0});try{Tf(n,JSON.stringify(t)+`
2423
- `,{flag:"a"})}catch{}}function oz(e,t){let n=`${e}.tmp-${process.pid}-${Date.now()}`;Tf(n,JSON.stringify(t,null,2)),oA(n,e)}function sz(e,t){let n=`${e}.tmp-${process.pid}-${Date.now()}`;Tf(n,t),oA(n,e)}import{createHash as cA}from"crypto";import{existsSync as iz,readFileSync as az}from"fs";var vt=class extends Error{code;constructor(t,n){super(t),this.name="EvalGenError",this.code=n}};function uA(e,t){if(!iz(e))throw new vt(`replay-fixture: source trace not found: ${e}`,"source-not-found");let n=t.startLine??1;if(n!==1)throw new vt(`replay-fixture: only startLine=1 (prefix slice) is supported in Sprint 3 (got ${n})`,"unsupported-window");let r=az(e);if(r.length===0)throw new vt(`replay-fixture: source trace is empty: ${e}`,"source-empty");let o=lz(r),s=o.length,i=-1;for(let d=0;d<o.length;d++){let m=o[d];if(m.contentEnd===m.start)continue;let f=r.subarray(m.start,m.contentEnd).toString("utf8"),g;try{g=JSON.parse(f)}catch{continue}if(g!==null&&typeof g=="object"&&"seq"in g&&typeof g.seq=="number"&&g.seq===t.endSeq){i=d+1;break}}if(i===-1)throw new vt(`replay-fixture: seq ${t.endSeq} not found in ${e} (scanned ${s} lines)`,"seq-not-found");let l=o[i-1].byteEnd,c=Buffer.from(r.subarray(0,l)),u=cA("sha256").update(c).digest("hex");return{bytes:c,startLine:1,endLine:i,sliceLineCount:i,sliceSha256:u,sourceLineCount:s}}function dA(e){return cA("sha256").update(e).digest("hex")}function lz(e){let t=[],n=0;for(let r=0;r<e.length;r++)e[r]===10&&(t.push({start:n,contentEnd:r,byteEnd:r+1}),n=r+1);return n<e.length&&t.push({start:n,contentEnd:e.length,byteEnd:e.length}),t}import{randomBytes as cz}from"crypto";import{existsSync as eu,mkdirSync as pA,readFileSync as mA,readdirSync as uz,renameSync as Ef,writeFileSync as tu}from"fs";import{join as fA,relative as dz}from"path";W();function gA(e,t={}){let n=(t.now??(()=>new Date))(),r=pz(n),o=t.randomSuffix!==void 0?t.randomSuffix():cz(3).toString("hex");if(!/^[0-9a-f]{6}$/.test(o))throw new Error(`generateEvalCaseId: randomSuffix must be 6 lowercase hex chars (got '${o}')`);return`${e}-eval-${r}-${o}`}function pz(e){let t=e.getUTCFullYear(),n=String(e.getUTCMonth()+1).padStart(2,"0"),r=String(e.getUTCDate()).padStart(2,"0");return`${t}${n}${r}`}function hA(e,t){if(t.evidenceRowIndex<0||t.evidenceRowIndex>=e.evidence.length)throw new vt(`buildEvalCase: evidence row ${t.evidenceRowIndex} out of range (card has ${e.evidence.length} row(s))`,"evidence-row-out-of-range");let n=e.evidence[t.evidenceRowIndex];if(n.eventIndices.length===0)throw new vt(`buildEvalCase: evidence row ${t.evidenceRowIndex} has no eventIndices`,"seq-not-found");let r=Math.max(...n.eventIndices),s=(t.resolveTraceAbsPath??mz)(n.tracePath),i=uA(s,{endSeq:r}),a=(t.now??(()=>new Date))().toISOString(),l=yf(t.evalCaseId),c=dz(Ce(),l),u=typeof e.detail.detector=="string"?e.detail.detector:e.pattern,d=typeof e.detail.fingerprint=="string"?e.detail.fingerprint:null,m=t.proposalId??null,f={schemaVersion:1,evalCaseId:t.evalCaseId,cardSlug:e.slug,proposalId:m,title:fz(e,r),createdAt:a,kind:"replay",replay:{sourceSessionId:n.sessionId,sourceTracePath:n.tracePath,fixturePath:c,evidenceRowIndex:t.evidenceRowIndex,evidenceEventIndices:[...n.eventIndices],sliceLineRange:{startLine:i.startLine,endLine:i.endLine},sliceLineCount:i.sliceLineCount,sliceSha256:i.sliceSha256},assertion:{kind:"pattern-absent",patternId:e.pattern,detectorVersion:u,rationale:gz({patternId:e.pattern,detectorVersion:u,endSeq:r,sliceLineCount:i.sliceLineCount,sessionId:n.sessionId})},provenance:{detectorAtGeneration:u,fingerprintAtGeneration:d,cardOccurrenceCountAtGeneration:e.occurrenceCount,cardLastSeenAtGeneration:e.lastSeen,generatedBy:"replay-fixture"},status:"draft",notes:[]};return Vc.parse(f),{evalCase:f,sliceBytes:i.bytes}}function mz(e){return fA(Ce(),e)}function fz(e,t){let n="Replay [pattern-absent]: ",r=` (through seq ${t})`,o=200-n.length-r.length,s=e.title.length>o?e.title.slice(0,o-1)+"\u2026":e.title;return`${n}${s}${r}`.slice(0,200)}function gz(e){let t=e.sessionId.slice(0,8);return`After the proposed fix lands, replaying the prefix [seq 0..${e.endSeq}] (${e.sliceLineCount} lines, session ${t}\u2026) through ${e.detectorVersion} must produce zero findings for '${e.patternId}' with the fingerprint at generation time. **Sprint 3 ships eval-case-as-contract; the runner that enforces this lands in a later sprint.**`}function yA(e,t){let n=Vc.parse(e),r=Yn();eu(r)||pA(r,{recursive:!0});let o=hf(n.evalCaseId),s=yf(n.evalCaseId),i=OR(n.evalCaseId);wz(s,t);let a=dA(mA(s));if(a!==n.replay.sliceSha256)throw new vt(`writeEvalCase: fixture sha256 mismatch after write (expected ${n.replay.sliceSha256}, got ${a}, path ${s})`,"fixture-mismatch");return yz(o,n),bz(i,nu(n)),hz({timestamp:new Date().toISOString(),event:"created",evalCaseId:n.evalCaseId,cardSlug:n.cardSlug,proposalId:n.proposalId,kind:n.kind}),{evalCaseId:n.evalCaseId,jsonPath:o,fixturePath:s,markdownPath:i}}function nu(e){let t=[];if(t.push(`# ${e.evalCaseId} \u2014 \`${e.kind}\` \u2014 \`${e.status}\``),t.push(""),t.push(e.title),t.push(""),t.push(`**Card:** \`${e.cardSlug}\` \xB7 **Proposal:** ${e.proposalId?`\`${e.proposalId}\``:"_(none)_"} \xB7 **Created:** ${e.createdAt}`),t.push(""),t.push("> **Sprint 3 disclaimer.** This file is a CONTRACT, not an"),t.push("> executable. No runner consumes it yet. A future sprint will"),t.push("> replay the fixture through the detector and assert the"),t.push("> pattern is absent. Until then this artifact captures intent."),t.push(""),t.push("## Replay fixture"),t.push(""),t.push(`- **Source session:** \`${e.replay.sourceSessionId}\``),t.push(`- **Source trace:** \`${e.replay.sourceTracePath}\``),t.push(`- **Fixture:** \`${e.replay.fixturePath}\``),t.push(`- **Evidence row:** index ${e.replay.evidenceRowIndex} (seqs ${e.replay.evidenceEventIndices.join(", ")})`),t.push(`- **Slice:** lines ${e.replay.sliceLineRange.startLine}\u2013${e.replay.sliceLineRange.endLine} (${e.replay.sliceLineCount} lines)`),t.push(`- **SHA-256:** \`${e.replay.sliceSha256}\``),t.push(""),t.push("## Assertion"),t.push(""),t.push(`- **Kind:** \`${e.assertion.kind}\``),t.push(`- **Pattern:** \`${e.assertion.patternId}\``),t.push(`- **Detector:** \`${e.assertion.detectorVersion}\``),t.push(""),t.push(e.assertion.rationale),t.push(""),t.push("## Provenance"),t.push(""),t.push(`- **Detector at generation:** \`${e.provenance.detectorAtGeneration}\``),t.push(`- **Fingerprint at generation:** ${e.provenance.fingerprintAtGeneration?`\`${e.provenance.fingerprintAtGeneration}\``:"_(none \u2014 detector has no fingerprint)_"}`),t.push(`- **Card occurrence count at generation:** ${e.provenance.cardOccurrenceCountAtGeneration}`),t.push(`- **Card lastSeen at generation:** ${e.provenance.cardLastSeenAtGeneration}`),t.push(`- **Generated by:** \`${e.provenance.generatedBy}\``),t.push(""),t.push("## Triage notes"),t.push(""),e.notes.length===0)t.push("_(none)_");else for(let n of e.notes)t.push(`- _${n.at}_ \u2014 ${n.text}`);return t.push(""),t.join(`
2423
+ `,{flag:"a"})}catch{}}function oz(e,t){let n=`${e}.tmp-${process.pid}-${Date.now()}`;Tf(n,JSON.stringify(t,null,2)),oA(n,e)}function sz(e,t){let n=`${e}.tmp-${process.pid}-${Date.now()}`;Tf(n,t),oA(n,e)}import{createHash as cA}from"crypto";import{existsSync as iz,readFileSync as az}from"fs";var vt=class extends Error{code;constructor(t,n){super(t),this.name="EvalGenError",this.code=n}};function uA(e,t){if(!iz(e))throw new vt(`replay-fixture: source trace not found: ${e}`,"source-not-found");let n=t.startLine??1;if(n!==1)throw new vt(`replay-fixture: only startLine=1 (prefix slice) is supported in Sprint 3 (got ${n})`,"unsupported-window");let r=az(e);if(r.length===0)throw new vt(`replay-fixture: source trace is empty: ${e}`,"source-empty");let o=lz(r),s=o.length,i=-1;for(let d=0;d<o.length;d++){let p=o[d];if(p.contentEnd===p.start)continue;let f=r.subarray(p.start,p.contentEnd).toString("utf8"),g;try{g=JSON.parse(f)}catch{continue}if(g!==null&&typeof g=="object"&&"seq"in g&&typeof g.seq=="number"&&g.seq===t.endSeq){i=d+1;break}}if(i===-1)throw new vt(`replay-fixture: seq ${t.endSeq} not found in ${e} (scanned ${s} lines)`,"seq-not-found");let l=o[i-1].byteEnd,c=Buffer.from(r.subarray(0,l)),u=cA("sha256").update(c).digest("hex");return{bytes:c,startLine:1,endLine:i,sliceLineCount:i,sliceSha256:u,sourceLineCount:s}}function dA(e){return cA("sha256").update(e).digest("hex")}function lz(e){let t=[],n=0;for(let r=0;r<e.length;r++)e[r]===10&&(t.push({start:n,contentEnd:r,byteEnd:r+1}),n=r+1);return n<e.length&&t.push({start:n,contentEnd:e.length,byteEnd:e.length}),t}import{randomBytes as cz}from"crypto";import{existsSync as eu,mkdirSync as pA,readFileSync as mA,readdirSync as uz,renameSync as Ef,writeFileSync as tu}from"fs";import{join as fA,relative as dz}from"path";W();function gA(e,t={}){let n=(t.now??(()=>new Date))(),r=pz(n),o=t.randomSuffix!==void 0?t.randomSuffix():cz(3).toString("hex");if(!/^[0-9a-f]{6}$/.test(o))throw new Error(`generateEvalCaseId: randomSuffix must be 6 lowercase hex chars (got '${o}')`);return`${e}-eval-${r}-${o}`}function pz(e){let t=e.getUTCFullYear(),n=String(e.getUTCMonth()+1).padStart(2,"0"),r=String(e.getUTCDate()).padStart(2,"0");return`${t}${n}${r}`}function hA(e,t){if(t.evidenceRowIndex<0||t.evidenceRowIndex>=e.evidence.length)throw new vt(`buildEvalCase: evidence row ${t.evidenceRowIndex} out of range (card has ${e.evidence.length} row(s))`,"evidence-row-out-of-range");let n=e.evidence[t.evidenceRowIndex];if(n.eventIndices.length===0)throw new vt(`buildEvalCase: evidence row ${t.evidenceRowIndex} has no eventIndices`,"seq-not-found");let r=Math.max(...n.eventIndices),s=(t.resolveTraceAbsPath??mz)(n.tracePath),i=uA(s,{endSeq:r}),a=(t.now??(()=>new Date))().toISOString(),l=yf(t.evalCaseId),c=dz(Ce(),l),u=typeof e.detail.detector=="string"?e.detail.detector:e.pattern,d=typeof e.detail.fingerprint=="string"?e.detail.fingerprint:null,p=t.proposalId??null,f={schemaVersion:1,evalCaseId:t.evalCaseId,cardSlug:e.slug,proposalId:p,title:fz(e,r),createdAt:a,kind:"replay",replay:{sourceSessionId:n.sessionId,sourceTracePath:n.tracePath,fixturePath:c,evidenceRowIndex:t.evidenceRowIndex,evidenceEventIndices:[...n.eventIndices],sliceLineRange:{startLine:i.startLine,endLine:i.endLine},sliceLineCount:i.sliceLineCount,sliceSha256:i.sliceSha256},assertion:{kind:"pattern-absent",patternId:e.pattern,detectorVersion:u,rationale:gz({patternId:e.pattern,detectorVersion:u,endSeq:r,sliceLineCount:i.sliceLineCount,sessionId:n.sessionId})},provenance:{detectorAtGeneration:u,fingerprintAtGeneration:d,cardOccurrenceCountAtGeneration:e.occurrenceCount,cardLastSeenAtGeneration:e.lastSeen,generatedBy:"replay-fixture"},status:"draft",notes:[]};return Vc.parse(f),{evalCase:f,sliceBytes:i.bytes}}function mz(e){return fA(Ce(),e)}function fz(e,t){let n="Replay [pattern-absent]: ",r=` (through seq ${t})`,o=200-n.length-r.length,s=e.title.length>o?e.title.slice(0,o-1)+"\u2026":e.title;return`${n}${s}${r}`.slice(0,200)}function gz(e){let t=e.sessionId.slice(0,8);return`After the proposed fix lands, replaying the prefix [seq 0..${e.endSeq}] (${e.sliceLineCount} lines, session ${t}\u2026) through ${e.detectorVersion} must produce zero findings for '${e.patternId}' with the fingerprint at generation time. **Sprint 3 ships eval-case-as-contract; the runner that enforces this lands in a later sprint.**`}function yA(e,t){let n=Vc.parse(e),r=Yn();eu(r)||pA(r,{recursive:!0});let o=hf(n.evalCaseId),s=yf(n.evalCaseId),i=OR(n.evalCaseId);wz(s,t);let a=dA(mA(s));if(a!==n.replay.sliceSha256)throw new vt(`writeEvalCase: fixture sha256 mismatch after write (expected ${n.replay.sliceSha256}, got ${a}, path ${s})`,"fixture-mismatch");return yz(o,n),bz(i,nu(n)),hz({timestamp:new Date().toISOString(),event:"created",evalCaseId:n.evalCaseId,cardSlug:n.cardSlug,proposalId:n.proposalId,kind:n.kind}),{evalCaseId:n.evalCaseId,jsonPath:o,fixturePath:s,markdownPath:i}}function nu(e){let t=[];if(t.push(`# ${e.evalCaseId} \u2014 \`${e.kind}\` \u2014 \`${e.status}\``),t.push(""),t.push(e.title),t.push(""),t.push(`**Card:** \`${e.cardSlug}\` \xB7 **Proposal:** ${e.proposalId?`\`${e.proposalId}\``:"_(none)_"} \xB7 **Created:** ${e.createdAt}`),t.push(""),t.push("> **Sprint 3 disclaimer.** This file is a CONTRACT, not an"),t.push("> executable. No runner consumes it yet. A future sprint will"),t.push("> replay the fixture through the detector and assert the"),t.push("> pattern is absent. Until then this artifact captures intent."),t.push(""),t.push("## Replay fixture"),t.push(""),t.push(`- **Source session:** \`${e.replay.sourceSessionId}\``),t.push(`- **Source trace:** \`${e.replay.sourceTracePath}\``),t.push(`- **Fixture:** \`${e.replay.fixturePath}\``),t.push(`- **Evidence row:** index ${e.replay.evidenceRowIndex} (seqs ${e.replay.evidenceEventIndices.join(", ")})`),t.push(`- **Slice:** lines ${e.replay.sliceLineRange.startLine}\u2013${e.replay.sliceLineRange.endLine} (${e.replay.sliceLineCount} lines)`),t.push(`- **SHA-256:** \`${e.replay.sliceSha256}\``),t.push(""),t.push("## Assertion"),t.push(""),t.push(`- **Kind:** \`${e.assertion.kind}\``),t.push(`- **Pattern:** \`${e.assertion.patternId}\``),t.push(`- **Detector:** \`${e.assertion.detectorVersion}\``),t.push(""),t.push(e.assertion.rationale),t.push(""),t.push("## Provenance"),t.push(""),t.push(`- **Detector at generation:** \`${e.provenance.detectorAtGeneration}\``),t.push(`- **Fingerprint at generation:** ${e.provenance.fingerprintAtGeneration?`\`${e.provenance.fingerprintAtGeneration}\``:"_(none \u2014 detector has no fingerprint)_"}`),t.push(`- **Card occurrence count at generation:** ${e.provenance.cardOccurrenceCountAtGeneration}`),t.push(`- **Card lastSeen at generation:** ${e.provenance.cardLastSeenAtGeneration}`),t.push(`- **Generated by:** \`${e.provenance.generatedBy}\``),t.push(""),t.push("## Triage notes"),t.push(""),e.notes.length===0)t.push("_(none)_");else for(let n of e.notes)t.push(`- _${n.at}_ \u2014 ${n.text}`);return t.push(""),t.join(`
2424
2424
  `)}function bA(){let e=Yn();if(!eu(e))return[];let t=[];for(let n of uz(e)){if(!n.endsWith(".json")||n.startsWith(".")||n.endsWith(".fixture.jsonl"))continue;let r=SA(fA(e,n));r&&t.push({evalCaseId:r.evalCaseId,cardSlug:r.cardSlug,proposalId:r.proposalId,title:r.title,kind:r.kind,status:r.status,patternId:r.assertion.patternId,createdAt:r.createdAt,sliceSha256:r.replay.sliceSha256})}return t.sort((n,r)=>n.createdAt!==r.createdAt?n.createdAt<r.createdAt?1:-1:n.evalCaseId<r.evalCaseId?-1:1),t}function wA(e){return SA(hf(e))}function SA(e){if(eu(e))try{let t=mA(e,"utf-8"),n=JSON.parse(t),r=Vc.safeParse(n);return r.success?r.data:void 0}catch{return}}function hz(e){let t=GR.parse(e),n=MR(),r=Yn();eu(r)||pA(r,{recursive:!0});try{tu(n,JSON.stringify(t)+`
2425
2425
  `,{flag:"a"})}catch{}}function yz(e,t){let n=`${e}.tmp-${process.pid}-${Date.now()}`;tu(n,JSON.stringify(t,null,2)),Ef(n,e)}function bz(e,t){let n=`${e}.tmp-${process.pid}-${Date.now()}`;tu(n,t),Ef(n,e)}function wz(e,t){let n=`${e}.tmp-${process.pid}-${Date.now()}`;tu(n,t),Ef(n,e)}var Rf=["open","deferred","resolved"],Af=["draft","approved","rejected","superseded"],_f=["repeated-tool-use","subagent-block","closure-anomaly"];function kA(e){let t=e.command("improve").description("Self-improvement pipeline: scan traces, triage cards, draft proposals, generate replay eval-cases.");Sz(t),kz(t),vz(t),Tz(t),xz(t),Ez(t)}function Sz(e){e.command("scan").description(`Run registered detectors against witness traces. Dry-run by default.
2426
- Some detectors are disabled by default (pass --include-disabled to enable): ${qc().join(", ")}.`).option("--since <duration>","Only scan sessions newer than this (e.g. 7d, 24h, all)","7d").option("--write","Persist failure cards to disk. Without this flag, scan is dry-run.",!1).option("--min-repeats <n>",`repeated-tool-use threshold (default ${Or})`,String(Or)).option("--closure-min-occurrences <n>",`closure-anomaly threshold (default ${1})`,String(1)).option("--block-min-occurrences <n>",`subagent-block threshold (default ${$r})`,String($r)).option("--tool-failure-min-failures <n>",`tool-failure-density absolute count threshold (default ${3})`,String(3)).option("--tool-failure-min-rate <rate>",`tool-failure-density rate threshold, 0\u20131 (default ${.25})`,String(.25)).option("--only <names>",`Comma-separated detector names to run (any of: ${Gc().join(", ")})`).option("--include-disabled",`Run detectors marked disabled-by-default (currently: ${qc().join(", ")})`,!1).action(t=>{try{let n=ru(t.minRepeats,"min-repeats",2),r=ru(t.closureMinOccurrences,"closure-min-occurrences",1),o=ru(t.blockMinOccurrences,"block-min-occurrences",1),s=ru(t.toolFailureMinFailures,"tool-failure-min-failures",1),i=Rz(t.toolFailureMinRate,"tool-failure-min-rate"),a;if(t.only){let y=t.only.split(",").map(R=>R.trim()).filter(R=>R.length>0),w=new Set(Gc()),x=y.filter(R=>!w.has(R));x.length>0&&(console.error(`Unknown detector(s): ${x.join(", ")}. Known: ${Gc().join(", ")}`),process.exit(2)),a=new Set(y)}let l;if(t.since&&t.since!=="all"){let y=$R(t.since);y===void 0&&(console.error(`Invalid --since: '${t.since}'. Use forms like '7d', '24h', '30m', '3600s', or 'all'.`),process.exit(2)),l=Date.now()-y}let c=DR({sinceMs:l}),u={minRepeats:n,closureAnomalyMinOccurrences:r,subagentBlockMinOccurrences:o,toolFailureMinFailures:s,toolFailureMinRate:i},d=jR(c.sessions,u,a,t.includeDisabled);console.log(`Scanned ${c.sessionsScanned} sessions`),c.sessionsSkippedOld>0&&console.log(` \u21B3 skipped ${c.sessionsSkippedOld} older than --since`),c.sessionsSkippedEmpty>0&&console.log(` \u21B3 skipped ${c.sessionsSkippedEmpty} with missing/unreadable trace.jsonl`),c.invalidLineCount>0&&console.log(` \u26A0 ${c.invalidLineCount} invalid JSONL lines skipped`);let m=qc();!t.only&&!t.includeDisabled&&m.length>0&&console.log(`Skipped ${m.length} detectors (disabled by default \u2014 pass --only or --include-disabled): ${m.join(", ")}`);let f=new Map;for(let y of d)f.set(y.pattern,(f.get(y.pattern)??0)+1);console.log(`Detections: ${d.length}`);for(let[y,w]of f.entries())console.log(` \u21B3 ${y}: ${w}`);if(d.length===0){t.write&&console.log("No cards written.");return}for(let y of d)console.log(` \u2022 ${y.slug} [${y.severity}] ${y.pattern} evidence=${y.evidence.length}`);if(!t.write){console.log(""),console.log("(dry-run \u2014 pass --write to persist cards)");return}let g=0,h=0,b=0;for(let y of d){let w=YR(y);w.event==="created"?g+=1:w.event==="updated"?h+=1:b+=1}console.log(""),console.log(`Wrote cards: ${g} created, ${h} updated, ${b} no-op merges.`)}catch(n){K(n)}})}function kz(e){let t=e.command("cards").description("Inspect and triage failure cards written by `afk improve scan`");t.command("list").description("List all failure cards, newest first").option("--pattern <name>","Filter by pattern name").option("--severity <level>","Filter by severity: low | medium | high").option("--status <state>","Filter by status: open | deferred | resolved").option("--json","Emit JSON instead of a table",!1).action(n=>{try{let r=XR();if(n.pattern&&(r=r.filter(i=>i.pattern===n.pattern)),n.severity&&(r=r.filter(i=>i.severity===n.severity)),n.status&&(r=r.filter(i=>i.status===n.status)),n.json){console.log(JSON.stringify(r,null,2));return}if(r.length===0){console.log("No failure cards found.");return}let o="SLUG | PATTERN | SEV | STATUS | N | LAST SEEN",s="-".repeat(o.length);console.log(o),console.log(s);for(let i of r)console.log([i.slug.padEnd(50).slice(0,50),i.pattern.padEnd(20),i.severity.padEnd(6),i.status.padEnd(9),String(i.occurrenceCount).padEnd(4),i.lastSeen].join(" | "))}catch(r){K(r)}}),t.command("show <slug>").description("Print a failure card by slug").option("--json","Emit raw JSON instead of rendered markdown",!1).action((n,r)=>{try{let o=Xc(n);if(o||(console.error(`Card not found: ${n}`),process.exit(1)),r.json){console.log(JSON.stringify(o,null,2));return}console.log(Ii(o))}catch(o){K(o)}}),t.command("triage <slug>").description("Append a human note and/or change status on a failure card").option("--note <text>","Note text to append (non-empty)").option("--status <state>",`New status (one of: ${Rf.join(", ")})`).option("--json","Emit the resulting card as JSON",!1).action((n,r)=>{try{let o;r.status!==void 0&&(Rf.includes(r.status)||(console.error(`Invalid --status: '${r.status}'. Must be one of: ${Rf.join(", ")}`),process.exit(2)),o=r.status);let s=eA(n,{...r.note!==void 0?{note:r.note}:{},...o!==void 0?{status:o}:{}});if(r.json){console.log(JSON.stringify(s.card,null,2));return}let i=[];s.noteAdded&&i.push("note appended"),s.statusChanged&&i.push(`status: ${s.statusChanged.from} \u2192 ${s.statusChanged.to}`),console.log(`Triaged ${n}: ${i.join(" \xB7 ")}`),console.log(` json: ${s.jsonPath}`),console.log(` md: ${s.markdownPath}`)}catch(o){o instanceof Dr&&(console.error(`triage failed [${o.code}]: ${o.message}`),process.exit(o.code==="card-not-found"?1:2)),K(o)}})}function vz(e){e.command("propose <slug>").description("Generate a template-mode improvement proposal for a failure card. No LLM calls.").option("--id <override>","Override the auto-generated proposal id").option("--json","Emit the proposal JSON to stdout (still writes to disk)",!1).option("--no-write","Render the proposal without persisting to disk (preview mode)").action((t,n)=>{try{let r=Xc(t);r||(console.error(`Card not found: ${t}`),process.exit(1));let o=n.id??iA(t),s=nA(r,{proposalId:o});if(n.write===!1){n.json?console.log(JSON.stringify(s,null,2)):(console.log("(preview \u2014 not persisted; remove --no-write to save)"),console.log(""),console.log(Qc(s)));return}let i=sA(s);if(n.json){console.log(JSON.stringify({...s,_paths:i},null,2));return}console.log(`Wrote proposal: ${i.proposalId}`),console.log(` json: ${i.jsonPath}`),console.log(` md: ${i.markdownPath}`),console.log(` risk: ${s.riskLevel} \xB7 root cause: ${s.rootCauseClass} \xB7 approval required: ${s.scopeFreeze.requiresExplicitApproval?"yes":"no"}`)}catch(r){K(r)}})}function Tz(e){let t=e.command("proposals").description("Inspect improvement proposals on disk");t.command("list").description("List all proposals, newest first").option("--card <slug>","Filter by card slug").option("--risk <level>","Filter by risk: low | medium | high").option("--json","Emit JSON instead of a table",!1).action(n=>{try{let r=aA();if(n.card&&(r=r.filter(i=>i.cardSlug===n.card)),n.risk&&(r=r.filter(i=>i.riskLevel===n.risk)),n.json){console.log(JSON.stringify(r,null,2));return}if(r.length===0){console.log("No proposals found.");return}let o="PROPOSAL ID | CARD | RISK | STATUS | CREATED",s="-".repeat(o.length);console.log(o),console.log(s);for(let i of r)console.log([i.proposalId.padEnd(64).slice(0,64),i.cardSlug.padEnd(44).slice(0,44),i.riskLevel.padEnd(6),i.status.padEnd(9),i.createdAt].join(" | "))}catch(r){K(r)}}),t.command("show <id>").description("Print a proposal by id").option("--json","Emit raw JSON instead of rendered markdown",!1).action((n,r)=>{try{let o=xf(n);if(o||(console.error(`Proposal not found: ${n}`),process.exit(1)),r.json){console.log(JSON.stringify(o,null,2));return}console.log(Qc(o))}catch(o){K(o)}})}function xz(e){e.command("eval-gen <cardSlug>").description("Generate a replay-mode eval-case from a failure card. Slices a byte-identical fixture from the source witness trace.").option("--proposal <id>","Back-reference to a proposal (validated to exist). Sprint 3 does NOT mutate the proposal artifact.").option("--evidence-row <index>","0-based index into the card's evidence array. Default: the most recent row (length - 1).").option("--id <override>","Override the auto-generated eval-case id").option("--json","Emit the eval-case JSON to stdout (still writes to disk)",!1).option("--no-write","Render the eval-case without persisting to disk (preview mode). Still reads the source trace.").action((t,n)=>{try{let r=Xc(t);if(r||(console.error(`Card not found: ${t}`),process.exit(1)),n.proposal!==void 0){let c=xf(n.proposal);c||(console.error(`Proposal not found: ${n.proposal}`),process.exit(1)),c.cardSlug!==t&&(console.error(`Proposal ${n.proposal} targets card '${c.cardSlug}', not '${t}'.`),process.exit(2))}let o=r.evidence.length-1;if(n.evidenceRow!==void 0){let c=Number.parseInt(n.evidenceRow,10);(!Number.isFinite(c)||c<0)&&(console.error(`Invalid --evidence-row: '${n.evidenceRow}' (must be non-negative integer)`),process.exit(2)),o=c}let s=n.id??gA(t),{evalCase:i,sliceBytes:a}=hA(r,{evalCaseId:s,evidenceRowIndex:o,proposalId:n.proposal??null});if(n.write===!1){n.json?console.log(JSON.stringify(i,null,2)):(console.log("(preview \u2014 not persisted; remove --no-write to save)"),console.log(""),console.log(nu(i)),console.log(""),console.log(`Fixture would be ${a.length} bytes, ${i.replay.sliceLineCount} lines.`));return}let l=yA(i,a);if(n.json){console.log(JSON.stringify({...i,_paths:l},null,2));return}console.log(`Wrote eval-case: ${l.evalCaseId}`),console.log(` json: ${l.jsonPath}`),console.log(` fixture: ${l.fixturePath}`),console.log(` md: ${l.markdownPath}`),console.log(` pattern: ${i.assertion.patternId} \xB7 slice: lines ${i.replay.sliceLineRange.startLine}\u2013${i.replay.sliceLineRange.endLine} (${i.replay.sliceLineCount} lines) \xB7 sha256 ${i.replay.sliceSha256.slice(0,12)}\u2026`),i.proposalId&&console.log(` proposal: ${i.proposalId} (back-reference only \u2014 Sprint 3 does not back-fill validationPlan.evalCases)`)}catch(r){if(r instanceof vt){console.error(`eval-gen failed [${r.code}]: ${r.message}`);let o=r.code==="evidence-row-out-of-range"||r.code==="unsupported-window"?2:1;process.exit(o)}K(r)}})}function Ez(e){let t=e.command("eval-cases").description("Inspect replay-mode eval-cases on disk");t.command("list").description("List all eval-cases, newest first").option("--card <slug>","Filter by card slug").option("--pattern <name>",`Filter by pattern (one of: ${_f.join(", ")})`).option("--status <state>",`Filter by status (one of: ${Af.join(", ")})`).option("--json","Emit JSON instead of a table",!1).action(n=>{try{n.pattern&&!_f.includes(n.pattern)&&(console.error(`Invalid --pattern: '${n.pattern}'. Must be one of: ${_f.join(", ")}`),process.exit(2)),n.status&&!Af.includes(n.status)&&(console.error(`Invalid --status: '${n.status}'. Must be one of: ${Af.join(", ")}`),process.exit(2));let r=bA();if(n.card&&(r=r.filter(i=>i.cardSlug===n.card)),n.pattern&&(r=r.filter(i=>i.patternId===n.pattern)),n.status&&(r=r.filter(i=>i.status===n.status)),n.json){console.log(JSON.stringify(r,null,2));return}if(r.length===0){console.log("No eval-cases found.");return}let o="EVAL CASE ID | CARD | PATTERN | STATUS | CREATED",s="-".repeat(o.length);console.log(o),console.log(s);for(let i of r)console.log([i.evalCaseId.padEnd(70).slice(0,70),i.cardSlug.padEnd(44).slice(0,44),i.patternId.padEnd(20),i.status.padEnd(9),i.createdAt].join(" | "))}catch(r){K(r)}}),t.command("show <id>").description("Print an eval-case by id").option("--json","Emit raw JSON instead of rendered markdown",!1).action((n,r)=>{try{let o=wA(n);if(o||(console.error(`Eval-case not found: ${n}`),process.exit(1)),r.json){console.log(JSON.stringify(o,null,2));return}console.log(nu(o))}catch(o){K(o)}})}function ru(e,t,n){let r=Number.parseInt(e,10);return(!Number.isFinite(r)||r<n)&&(console.error(`Invalid --${t}: '${e}' (must be integer >= ${n})`),process.exit(2)),r}function Rz(e,t){let n=Number.parseFloat(e);return(!Number.isFinite(n)||n<=0||n>1)&&(console.error(`Invalid --${t}: '${e}' (must be number in (0, 1])`),process.exit(2)),n}import{realpathSync as Cz}from"fs";Cf();Cf({path:ot(),override:!1});process.argv.includes("shell-init")||Gf();process.env.AFK_FRAMEWORK_DIR??=Tt();process.env.AGENT_SURFACE??="afk";Jf();var ge=new Az;ge.name("afk").description("AI agent CLI. Starts interactive REPL by default; use `afk chat` for one-shot.").version(bn()).option("--no-update-check","Skip update version check");wS(ge);Hx(ge);lE(ge);cE(ge);jx(ge);fE(ge);yE(ge);wE(ge);Wx(ge);kE(ge);vE(ge);TE(ge);OE(ge);WE(ge);rR(ge);iR(ge);aR(ge);uR(ge);fR(ge);AR(ge);kA(ge);Ab(ge);ge.commands.find(e=>e.name()==="chat")?.alias("c");ge.commands.find(e=>e.name()==="interactive")?.alias("i");ge.commands.find(e=>e.name()==="status")?.alias("s");ge.addHelpText("after",`
2426
+ Some detectors are disabled by default (pass --include-disabled to enable): ${qc().join(", ")}.`).option("--since <duration>","Only scan sessions newer than this (e.g. 7d, 24h, all)","7d").option("--write","Persist failure cards to disk. Without this flag, scan is dry-run.",!1).option("--min-repeats <n>",`repeated-tool-use threshold (default ${Or})`,String(Or)).option("--closure-min-occurrences <n>",`closure-anomaly threshold (default ${1})`,String(1)).option("--block-min-occurrences <n>",`subagent-block threshold (default ${$r})`,String($r)).option("--tool-failure-min-failures <n>",`tool-failure-density absolute count threshold (default ${3})`,String(3)).option("--tool-failure-min-rate <rate>",`tool-failure-density rate threshold, 0\u20131 (default ${.25})`,String(.25)).option("--only <names>",`Comma-separated detector names to run (any of: ${Gc().join(", ")})`).option("--include-disabled",`Run detectors marked disabled-by-default (currently: ${qc().join(", ")})`,!1).action(t=>{try{let n=ru(t.minRepeats,"min-repeats",2),r=ru(t.closureMinOccurrences,"closure-min-occurrences",1),o=ru(t.blockMinOccurrences,"block-min-occurrences",1),s=ru(t.toolFailureMinFailures,"tool-failure-min-failures",1),i=Rz(t.toolFailureMinRate,"tool-failure-min-rate"),a;if(t.only){let y=t.only.split(",").map(R=>R.trim()).filter(R=>R.length>0),w=new Set(Gc()),x=y.filter(R=>!w.has(R));x.length>0&&(console.error(`Unknown detector(s): ${x.join(", ")}. Known: ${Gc().join(", ")}`),process.exit(2)),a=new Set(y)}let l;if(t.since&&t.since!=="all"){let y=$R(t.since);y===void 0&&(console.error(`Invalid --since: '${t.since}'. Use forms like '7d', '24h', '30m', '3600s', or 'all'.`),process.exit(2)),l=Date.now()-y}let c=DR({sinceMs:l}),u={minRepeats:n,closureAnomalyMinOccurrences:r,subagentBlockMinOccurrences:o,toolFailureMinFailures:s,toolFailureMinRate:i},d=jR(c.sessions,u,a,t.includeDisabled);console.log(`Scanned ${c.sessionsScanned} sessions`),c.sessionsSkippedOld>0&&console.log(` \u21B3 skipped ${c.sessionsSkippedOld} older than --since`),c.sessionsSkippedEmpty>0&&console.log(` \u21B3 skipped ${c.sessionsSkippedEmpty} with missing/unreadable trace.jsonl`),c.invalidLineCount>0&&console.log(` \u26A0 ${c.invalidLineCount} invalid JSONL lines skipped`);let p=qc();!t.only&&!t.includeDisabled&&p.length>0&&console.log(`Skipped ${p.length} detectors (disabled by default \u2014 pass --only or --include-disabled): ${p.join(", ")}`);let f=new Map;for(let y of d)f.set(y.pattern,(f.get(y.pattern)??0)+1);console.log(`Detections: ${d.length}`);for(let[y,w]of f.entries())console.log(` \u21B3 ${y}: ${w}`);if(d.length===0){t.write&&console.log("No cards written.");return}for(let y of d)console.log(` \u2022 ${y.slug} [${y.severity}] ${y.pattern} evidence=${y.evidence.length}`);if(!t.write){console.log(""),console.log("(dry-run \u2014 pass --write to persist cards)");return}let g=0,h=0,b=0;for(let y of d){let w=YR(y);w.event==="created"?g+=1:w.event==="updated"?h+=1:b+=1}console.log(""),console.log(`Wrote cards: ${g} created, ${h} updated, ${b} no-op merges.`)}catch(n){K(n)}})}function kz(e){let t=e.command("cards").description("Inspect and triage failure cards written by `afk improve scan`");t.command("list").description("List all failure cards, newest first").option("--pattern <name>","Filter by pattern name").option("--severity <level>","Filter by severity: low | medium | high").option("--status <state>","Filter by status: open | deferred | resolved").option("--json","Emit JSON instead of a table",!1).action(n=>{try{let r=XR();if(n.pattern&&(r=r.filter(i=>i.pattern===n.pattern)),n.severity&&(r=r.filter(i=>i.severity===n.severity)),n.status&&(r=r.filter(i=>i.status===n.status)),n.json){console.log(JSON.stringify(r,null,2));return}if(r.length===0){console.log("No failure cards found.");return}let o="SLUG | PATTERN | SEV | STATUS | N | LAST SEEN",s="-".repeat(o.length);console.log(o),console.log(s);for(let i of r)console.log([i.slug.padEnd(50).slice(0,50),i.pattern.padEnd(20),i.severity.padEnd(6),i.status.padEnd(9),String(i.occurrenceCount).padEnd(4),i.lastSeen].join(" | "))}catch(r){K(r)}}),t.command("show <slug>").description("Print a failure card by slug").option("--json","Emit raw JSON instead of rendered markdown",!1).action((n,r)=>{try{let o=Xc(n);if(o||(console.error(`Card not found: ${n}`),process.exit(1)),r.json){console.log(JSON.stringify(o,null,2));return}console.log(Ii(o))}catch(o){K(o)}}),t.command("triage <slug>").description("Append a human note and/or change status on a failure card").option("--note <text>","Note text to append (non-empty)").option("--status <state>",`New status (one of: ${Rf.join(", ")})`).option("--json","Emit the resulting card as JSON",!1).action((n,r)=>{try{let o;r.status!==void 0&&(Rf.includes(r.status)||(console.error(`Invalid --status: '${r.status}'. Must be one of: ${Rf.join(", ")}`),process.exit(2)),o=r.status);let s=eA(n,{...r.note!==void 0?{note:r.note}:{},...o!==void 0?{status:o}:{}});if(r.json){console.log(JSON.stringify(s.card,null,2));return}let i=[];s.noteAdded&&i.push("note appended"),s.statusChanged&&i.push(`status: ${s.statusChanged.from} \u2192 ${s.statusChanged.to}`),console.log(`Triaged ${n}: ${i.join(" \xB7 ")}`),console.log(` json: ${s.jsonPath}`),console.log(` md: ${s.markdownPath}`)}catch(o){o instanceof Dr&&(console.error(`triage failed [${o.code}]: ${o.message}`),process.exit(o.code==="card-not-found"?1:2)),K(o)}})}function vz(e){e.command("propose <slug>").description("Generate a template-mode improvement proposal for a failure card. No LLM calls.").option("--id <override>","Override the auto-generated proposal id").option("--json","Emit the proposal JSON to stdout (still writes to disk)",!1).option("--no-write","Render the proposal without persisting to disk (preview mode)").action((t,n)=>{try{let r=Xc(t);r||(console.error(`Card not found: ${t}`),process.exit(1));let o=n.id??iA(t),s=nA(r,{proposalId:o});if(n.write===!1){n.json?console.log(JSON.stringify(s,null,2)):(console.log("(preview \u2014 not persisted; remove --no-write to save)"),console.log(""),console.log(Qc(s)));return}let i=sA(s);if(n.json){console.log(JSON.stringify({...s,_paths:i},null,2));return}console.log(`Wrote proposal: ${i.proposalId}`),console.log(` json: ${i.jsonPath}`),console.log(` md: ${i.markdownPath}`),console.log(` risk: ${s.riskLevel} \xB7 root cause: ${s.rootCauseClass} \xB7 approval required: ${s.scopeFreeze.requiresExplicitApproval?"yes":"no"}`)}catch(r){K(r)}})}function Tz(e){let t=e.command("proposals").description("Inspect improvement proposals on disk");t.command("list").description("List all proposals, newest first").option("--card <slug>","Filter by card slug").option("--risk <level>","Filter by risk: low | medium | high").option("--json","Emit JSON instead of a table",!1).action(n=>{try{let r=aA();if(n.card&&(r=r.filter(i=>i.cardSlug===n.card)),n.risk&&(r=r.filter(i=>i.riskLevel===n.risk)),n.json){console.log(JSON.stringify(r,null,2));return}if(r.length===0){console.log("No proposals found.");return}let o="PROPOSAL ID | CARD | RISK | STATUS | CREATED",s="-".repeat(o.length);console.log(o),console.log(s);for(let i of r)console.log([i.proposalId.padEnd(64).slice(0,64),i.cardSlug.padEnd(44).slice(0,44),i.riskLevel.padEnd(6),i.status.padEnd(9),i.createdAt].join(" | "))}catch(r){K(r)}}),t.command("show <id>").description("Print a proposal by id").option("--json","Emit raw JSON instead of rendered markdown",!1).action((n,r)=>{try{let o=xf(n);if(o||(console.error(`Proposal not found: ${n}`),process.exit(1)),r.json){console.log(JSON.stringify(o,null,2));return}console.log(Qc(o))}catch(o){K(o)}})}function xz(e){e.command("eval-gen <cardSlug>").description("Generate a replay-mode eval-case from a failure card. Slices a byte-identical fixture from the source witness trace.").option("--proposal <id>","Back-reference to a proposal (validated to exist). Sprint 3 does NOT mutate the proposal artifact.").option("--evidence-row <index>","0-based index into the card's evidence array. Default: the most recent row (length - 1).").option("--id <override>","Override the auto-generated eval-case id").option("--json","Emit the eval-case JSON to stdout (still writes to disk)",!1).option("--no-write","Render the eval-case without persisting to disk (preview mode). Still reads the source trace.").action((t,n)=>{try{let r=Xc(t);if(r||(console.error(`Card not found: ${t}`),process.exit(1)),n.proposal!==void 0){let c=xf(n.proposal);c||(console.error(`Proposal not found: ${n.proposal}`),process.exit(1)),c.cardSlug!==t&&(console.error(`Proposal ${n.proposal} targets card '${c.cardSlug}', not '${t}'.`),process.exit(2))}let o=r.evidence.length-1;if(n.evidenceRow!==void 0){let c=Number.parseInt(n.evidenceRow,10);(!Number.isFinite(c)||c<0)&&(console.error(`Invalid --evidence-row: '${n.evidenceRow}' (must be non-negative integer)`),process.exit(2)),o=c}let s=n.id??gA(t),{evalCase:i,sliceBytes:a}=hA(r,{evalCaseId:s,evidenceRowIndex:o,proposalId:n.proposal??null});if(n.write===!1){n.json?console.log(JSON.stringify(i,null,2)):(console.log("(preview \u2014 not persisted; remove --no-write to save)"),console.log(""),console.log(nu(i)),console.log(""),console.log(`Fixture would be ${a.length} bytes, ${i.replay.sliceLineCount} lines.`));return}let l=yA(i,a);if(n.json){console.log(JSON.stringify({...i,_paths:l},null,2));return}console.log(`Wrote eval-case: ${l.evalCaseId}`),console.log(` json: ${l.jsonPath}`),console.log(` fixture: ${l.fixturePath}`),console.log(` md: ${l.markdownPath}`),console.log(` pattern: ${i.assertion.patternId} \xB7 slice: lines ${i.replay.sliceLineRange.startLine}\u2013${i.replay.sliceLineRange.endLine} (${i.replay.sliceLineCount} lines) \xB7 sha256 ${i.replay.sliceSha256.slice(0,12)}\u2026`),i.proposalId&&console.log(` proposal: ${i.proposalId} (back-reference only \u2014 Sprint 3 does not back-fill validationPlan.evalCases)`)}catch(r){if(r instanceof vt){console.error(`eval-gen failed [${r.code}]: ${r.message}`);let o=r.code==="evidence-row-out-of-range"||r.code==="unsupported-window"?2:1;process.exit(o)}K(r)}})}function Ez(e){let t=e.command("eval-cases").description("Inspect replay-mode eval-cases on disk");t.command("list").description("List all eval-cases, newest first").option("--card <slug>","Filter by card slug").option("--pattern <name>",`Filter by pattern (one of: ${_f.join(", ")})`).option("--status <state>",`Filter by status (one of: ${Af.join(", ")})`).option("--json","Emit JSON instead of a table",!1).action(n=>{try{n.pattern&&!_f.includes(n.pattern)&&(console.error(`Invalid --pattern: '${n.pattern}'. Must be one of: ${_f.join(", ")}`),process.exit(2)),n.status&&!Af.includes(n.status)&&(console.error(`Invalid --status: '${n.status}'. Must be one of: ${Af.join(", ")}`),process.exit(2));let r=bA();if(n.card&&(r=r.filter(i=>i.cardSlug===n.card)),n.pattern&&(r=r.filter(i=>i.patternId===n.pattern)),n.status&&(r=r.filter(i=>i.status===n.status)),n.json){console.log(JSON.stringify(r,null,2));return}if(r.length===0){console.log("No eval-cases found.");return}let o="EVAL CASE ID | CARD | PATTERN | STATUS | CREATED",s="-".repeat(o.length);console.log(o),console.log(s);for(let i of r)console.log([i.evalCaseId.padEnd(70).slice(0,70),i.cardSlug.padEnd(44).slice(0,44),i.patternId.padEnd(20),i.status.padEnd(9),i.createdAt].join(" | "))}catch(r){K(r)}}),t.command("show <id>").description("Print an eval-case by id").option("--json","Emit raw JSON instead of rendered markdown",!1).action((n,r)=>{try{let o=wA(n);if(o||(console.error(`Eval-case not found: ${n}`),process.exit(1)),r.json){console.log(JSON.stringify(o,null,2));return}console.log(nu(o))}catch(o){K(o)}})}function ru(e,t,n){let r=Number.parseInt(e,10);return(!Number.isFinite(r)||r<n)&&(console.error(`Invalid --${t}: '${e}' (must be integer >= ${n})`),process.exit(2)),r}function Rz(e,t){let n=Number.parseFloat(e);return(!Number.isFinite(n)||n<=0||n>1)&&(console.error(`Invalid --${t}: '${e}' (must be number in (0, 1])`),process.exit(2)),n}import{realpathSync as Cz}from"fs";Cf();Cf({path:ot(),override:!1});process.argv.includes("shell-init")||Gf();process.env.AFK_FRAMEWORK_DIR??=Tt();process.env.AGENT_SURFACE??="afk";Jf();var ge=new Az;ge.name("afk").description("AI agent CLI. Starts interactive REPL by default; use `afk chat` for one-shot.").version(bn()).option("--no-update-check","Skip update version check");wS(ge);Hx(ge);lE(ge);cE(ge);jx(ge);fE(ge);yE(ge);wE(ge);Wx(ge);kE(ge);vE(ge);TE(ge);OE(ge);WE(ge);rR(ge);iR(ge);aR(ge);uR(ge);fR(ge);AR(ge);kA(ge);Ab(ge);ge.commands.find(e=>e.name()==="chat")?.alias("c");ge.commands.find(e=>e.name()==="interactive")?.alias("i");ge.commands.find(e=>e.name()==="status")?.alias("s");ge.addHelpText("after",`
2427
2427
  Examples:
2428
2428
  $ afk # start interactive REPL
2429
2429
  $ afk --model opus # REPL with specific model