@phnx-labs/agents-cli 1.20.64 → 1.20.65

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 (155) hide show
  1. package/CHANGELOG.md +39 -3
  2. package/README.md +37 -2
  3. package/dist/bin/agents +0 -0
  4. package/dist/commands/apply.d.ts +12 -0
  5. package/dist/commands/apply.js +274 -0
  6. package/dist/commands/browser.js +2 -2
  7. package/dist/commands/cloud.js +32 -2
  8. package/dist/commands/doctor.js +4 -1
  9. package/dist/commands/exec.js +94 -49
  10. package/dist/commands/feed.js +25 -11
  11. package/dist/commands/hosts.js +44 -6
  12. package/dist/commands/mcp.js +55 -5
  13. package/dist/commands/monitors.d.ts +12 -0
  14. package/dist/commands/monitors.js +740 -0
  15. package/dist/commands/output.js +2 -2
  16. package/dist/commands/routines.js +23 -2
  17. package/dist/commands/secrets.d.ts +16 -0
  18. package/dist/commands/secrets.js +215 -64
  19. package/dist/commands/serve.js +31 -0
  20. package/dist/commands/sessions-export.js +8 -3
  21. package/dist/commands/sessions.d.ts +16 -0
  22. package/dist/commands/sessions.js +36 -6
  23. package/dist/commands/ssh.js +45 -2
  24. package/dist/commands/versions.js +7 -3
  25. package/dist/commands/view.d.ts +26 -0
  26. package/dist/commands/view.js +32 -9
  27. package/dist/commands/webhook.js +10 -2
  28. package/dist/index.js +34 -14
  29. package/dist/lib/agents.d.ts +18 -0
  30. package/dist/lib/agents.js +53 -3
  31. package/dist/lib/auto-dispatch-provider.js +7 -2
  32. package/dist/lib/auto-dispatch.d.ts +3 -0
  33. package/dist/lib/auto-dispatch.js +3 -0
  34. package/dist/lib/browser/chrome.js +2 -2
  35. package/dist/lib/cloud/antigravity.js +2 -2
  36. package/dist/lib/cloud/host.d.ts +59 -0
  37. package/dist/lib/cloud/host.js +224 -0
  38. package/dist/lib/cloud/registry.js +4 -0
  39. package/dist/lib/cloud/types.d.ts +6 -4
  40. package/dist/lib/computer-rpc.js +3 -1
  41. package/dist/lib/crabbox/cli.js +5 -1
  42. package/dist/lib/crabbox/runtimes.js +11 -2
  43. package/dist/lib/daemon.d.ts +20 -4
  44. package/dist/lib/daemon.js +62 -19
  45. package/dist/lib/devices/fleet.d.ts +3 -2
  46. package/dist/lib/devices/fleet.js +9 -0
  47. package/dist/lib/devices/registry.d.ts +15 -0
  48. package/dist/lib/devices/registry.js +9 -0
  49. package/dist/lib/exec.d.ts +19 -2
  50. package/dist/lib/exec.js +41 -13
  51. package/dist/lib/fleet/apply.d.ts +63 -0
  52. package/dist/lib/fleet/apply.js +214 -0
  53. package/dist/lib/fleet/auth-sync.d.ts +67 -0
  54. package/dist/lib/fleet/auth-sync.js +142 -0
  55. package/dist/lib/fleet/manifest.d.ts +29 -0
  56. package/dist/lib/fleet/manifest.js +127 -0
  57. package/dist/lib/fleet/types.d.ts +129 -0
  58. package/dist/lib/fleet/types.js +13 -0
  59. package/dist/lib/git.d.ts +27 -0
  60. package/dist/lib/git.js +34 -2
  61. package/dist/lib/hosts/dispatch.d.ts +29 -8
  62. package/dist/lib/hosts/dispatch.js +46 -18
  63. package/dist/lib/hosts/passthrough.js +2 -0
  64. package/dist/lib/hosts/providers/devices.d.ts +27 -0
  65. package/dist/lib/hosts/providers/devices.js +98 -0
  66. package/dist/lib/hosts/registry.d.ts +10 -16
  67. package/dist/lib/hosts/registry.js +17 -50
  68. package/dist/lib/hosts/remote-cmd.d.ts +23 -0
  69. package/dist/lib/hosts/remote-cmd.js +71 -0
  70. package/dist/lib/hosts/run-target.d.ts +84 -0
  71. package/dist/lib/hosts/run-target.js +99 -0
  72. package/dist/lib/hosts/types.d.ts +23 -5
  73. package/dist/lib/hosts/types.js +22 -4
  74. package/dist/lib/linear-autoclose.d.ts +30 -0
  75. package/dist/lib/linear-autoclose.js +22 -0
  76. package/dist/lib/mcp.d.ts +27 -1
  77. package/dist/lib/mcp.js +126 -12
  78. package/dist/lib/monitors/config.d.ts +161 -0
  79. package/dist/lib/monitors/config.js +372 -0
  80. package/dist/lib/monitors/dispatch.d.ts +28 -0
  81. package/dist/lib/monitors/dispatch.js +91 -0
  82. package/dist/lib/monitors/engine.d.ts +61 -0
  83. package/dist/lib/monitors/engine.js +201 -0
  84. package/dist/lib/monitors/sources/command.d.ts +11 -0
  85. package/dist/lib/monitors/sources/command.js +31 -0
  86. package/dist/lib/monitors/sources/device.d.ts +13 -0
  87. package/dist/lib/monitors/sources/device.js +35 -0
  88. package/dist/lib/monitors/sources/file.d.ts +14 -0
  89. package/dist/lib/monitors/sources/file.js +57 -0
  90. package/dist/lib/monitors/sources/http.d.ts +10 -0
  91. package/dist/lib/monitors/sources/http.js +34 -0
  92. package/dist/lib/monitors/sources/index.d.ts +14 -0
  93. package/dist/lib/monitors/sources/index.js +31 -0
  94. package/dist/lib/monitors/sources/poll.d.ts +9 -0
  95. package/dist/lib/monitors/sources/poll.js +9 -0
  96. package/dist/lib/monitors/sources/types.d.ts +18 -0
  97. package/dist/lib/monitors/sources/types.js +9 -0
  98. package/dist/lib/monitors/sources/webhook.d.ts +23 -0
  99. package/dist/lib/monitors/sources/webhook.js +47 -0
  100. package/dist/lib/monitors/sources/ws.d.ts +14 -0
  101. package/dist/lib/monitors/sources/ws.js +45 -0
  102. package/dist/lib/monitors/state.d.ts +69 -0
  103. package/dist/lib/monitors/state.js +144 -0
  104. package/dist/lib/platform/exec.d.ts +16 -0
  105. package/dist/lib/platform/exec.js +17 -0
  106. package/dist/lib/plugins.js +101 -2
  107. package/dist/lib/redact.d.ts +14 -1
  108. package/dist/lib/redact.js +47 -1
  109. package/dist/lib/remote-agents-json.js +7 -1
  110. package/dist/lib/rotate.d.ts +6 -3
  111. package/dist/lib/rotate.js +0 -1
  112. package/dist/lib/routines.d.ts +16 -0
  113. package/dist/lib/routines.js +19 -0
  114. package/dist/lib/runner.d.ts +1 -0
  115. package/dist/lib/runner.js +102 -9
  116. package/dist/lib/secrets/agent.d.ts +48 -10
  117. package/dist/lib/secrets/agent.js +123 -15
  118. package/dist/lib/secrets/bundles.d.ts +26 -0
  119. package/dist/lib/secrets/bundles.js +59 -8
  120. package/dist/lib/secrets/mcp.js +4 -2
  121. package/dist/lib/secrets/remote.d.ts +17 -0
  122. package/dist/lib/secrets/remote.js +40 -0
  123. package/dist/lib/self-update.d.ts +20 -0
  124. package/dist/lib/self-update.js +54 -1
  125. package/dist/lib/serve/control.d.ts +95 -0
  126. package/dist/lib/serve/control.js +260 -0
  127. package/dist/lib/serve/server.d.ts +35 -1
  128. package/dist/lib/serve/server.js +106 -76
  129. package/dist/lib/serve/stream.d.ts +43 -0
  130. package/dist/lib/serve/stream.js +116 -0
  131. package/dist/lib/serve/token.d.ts +35 -0
  132. package/dist/lib/serve/token.js +85 -0
  133. package/dist/lib/session/bundle.d.ts +14 -0
  134. package/dist/lib/session/bundle.js +12 -1
  135. package/dist/lib/session/remote-list.js +5 -1
  136. package/dist/lib/session/state.d.ts +7 -25
  137. package/dist/lib/session/state.js +16 -6
  138. package/dist/lib/session/sync/config.js +8 -2
  139. package/dist/lib/session/types.d.ts +30 -0
  140. package/dist/lib/ssh-tunnel.d.ts +19 -1
  141. package/dist/lib/ssh-tunnel.js +86 -7
  142. package/dist/lib/startup/command-registry.d.ts +2 -0
  143. package/dist/lib/startup/command-registry.js +4 -0
  144. package/dist/lib/state.d.ts +5 -0
  145. package/dist/lib/state.js +12 -0
  146. package/dist/lib/tmux/session.d.ts +7 -0
  147. package/dist/lib/tmux/session.js +3 -1
  148. package/dist/lib/triggers/webhook.d.ts +18 -0
  149. package/dist/lib/triggers/webhook.js +105 -0
  150. package/dist/lib/types.d.ts +26 -1
  151. package/dist/lib/usage.js +7 -5
  152. package/dist/lib/versions.js +14 -11
  153. package/dist/lib/workflows.d.ts +20 -0
  154. package/dist/lib/workflows.js +24 -0
  155. package/package.json +2 -1
@@ -0,0 +1,99 @@
1
+ /**
2
+ * Shared host-run dispatch — the one path every surface uses to run an agent on
3
+ * another machine: `agents run --host` (commands/exec.ts), the `host` cloud
4
+ * provider (`agents cloud run --provider host`), and host-placed routines.
5
+ *
6
+ * Wraps the two steps every caller needs and previously lived inline in
7
+ * exec.ts's `--host` branch:
8
+ * 1. resolution — name → capability tag → error, with the same fall-through
9
+ * semantics as `agents run --host` (only "Multiple hosts tagged…" is a
10
+ * resolution verdict; "no host tagged" degrades to unknown-host), and
11
+ * 2. headless dispatch — session-id mint (Claude only), detached SSH launch,
12
+ * and LOCAL session-index registration so the run shows in `agents sessions`.
13
+ *
14
+ * Interactive dispatch stays in exec.ts: it is inherently tied to the caller's
15
+ * TTY and has no other consumers.
16
+ */
17
+ import { randomUUID } from 'crypto';
18
+ import { resolveHost, resolveHostByCap } from './registry.js';
19
+ import { dispatchToHost } from './dispatch.js';
20
+ import { registerHostSession } from './session-index.js';
21
+ /**
22
+ * Resolution failed with a user-actionable message the caller should print
23
+ * verbatim. Distinct from `DeviceOffloadUnsupportedError` (which propagates to
24
+ * the top-level catch) so callers can tell "bad name" from "bad auth method".
25
+ */
26
+ export class HostResolutionError extends Error {
27
+ constructor(message) {
28
+ super(message);
29
+ this.name = 'HostResolutionError';
30
+ }
31
+ }
32
+ /**
33
+ * Resolve a `--host` value the way `agents run` does: exact name (providers →
34
+ * devices → `user@host`), then capability tag. Throws `HostResolutionError`
35
+ * for an ambiguous tag or an unknown name; lets `DeviceOffloadUnsupportedError`
36
+ * (password-auth device) propagate untouched for the top-level catch.
37
+ */
38
+ export async function resolveHostRunTarget(name, opts = {}) {
39
+ let host = await resolveHost(name);
40
+ if (!host) {
41
+ try {
42
+ host = await resolveHostByCap(name, opts.any);
43
+ }
44
+ catch (e) {
45
+ const msg = e.message ?? '';
46
+ // Ambiguity is a verdict, not a miss — surface it. "No host tagged"
47
+ // falls through to the generic unknown-host error below.
48
+ if (msg.startsWith('Multiple hosts'))
49
+ throw new HostResolutionError(msg);
50
+ }
51
+ }
52
+ if (!host)
53
+ throw new HostResolutionError(`Unknown host "${name}". List hosts: agents hosts list`);
54
+ return host;
55
+ }
56
+ /**
57
+ * Dispatch a headless prompt run onto a resolved host: mint the forced session
58
+ * id (Claude is the only agent that accepts `--session-id`; on resume the
59
+ * remote session keeps its id), launch detached over SSH, and register the run
60
+ * in the LOCAL session index. Returns the task record and the remote exit code
61
+ * (`-1` = follow window closed while the run continues).
62
+ */
63
+ export async function dispatchPromptToHost(host, opts) {
64
+ const sessionId = opts.agent === 'claude' && !opts.resume ? randomUUID() : undefined;
65
+ const result = await dispatchToHost(host, {
66
+ agent: opts.agent,
67
+ prompt: opts.prompt,
68
+ version: opts.version,
69
+ mode: opts.mode,
70
+ model: opts.model,
71
+ remoteCwd: opts.remoteCwd,
72
+ sessionId,
73
+ name: opts.name,
74
+ resume: opts.resume,
75
+ follow: opts.follow !== false,
76
+ timeoutMs: opts.timeoutMs,
77
+ effort: opts.effort,
78
+ env: opts.env,
79
+ addDir: opts.addDir,
80
+ timeout: opts.timeout,
81
+ strategy: opts.strategy,
82
+ balanced: opts.balanced,
83
+ fallback: opts.fallback,
84
+ loop: opts.loop,
85
+ maxIterations: opts.maxIterations,
86
+ budget: opts.budget,
87
+ until: opts.until,
88
+ interval: opts.interval,
89
+ json: opts.json,
90
+ verbose: opts.verbose,
91
+ yes: opts.yes,
92
+ acp: opts.acp,
93
+ autoSecrets: opts.autoSecrets,
94
+ passthroughArgs: opts.passthroughArgs,
95
+ copyCreds: opts.copyCreds,
96
+ });
97
+ registerHostSession(result.task, { cwd: opts.cwd ?? process.cwd(), prompt: opts.prompt });
98
+ return result;
99
+ }
@@ -2,13 +2,14 @@
2
2
  * Agent-host provider contract.
3
3
  *
4
4
  * A `HostProvider` answers "what are my hosts, and how do I reach them?" — the
5
- * pluggable directory/metadata/reachability layer. v1 ships only the `local`
6
- * provider (ssh-config ∪ inline registry); `rush`/`tailscale`/`crabbox` are
7
- * additive fast-follows behind this same contract. Capability-gated so partial
8
- * providers are first-class (mirrors the cloud provider registry).
5
+ * pluggable directory/metadata/reachability layer. Shipped providers: `local`
6
+ * (ssh-config ∪ inline registry) and `devices` (the Tailscale fleet from
7
+ * `agents devices`); `rush`/`crabbox` remain additive fast-follows behind this
8
+ * same contract. Capability-gated so partial providers are first-class
9
+ * (mirrors the cloud provider registry).
9
10
  */
10
11
  import type { HostEntry } from '../types.js';
11
- export type HostProviderId = 'local';
12
+ export type HostProviderId = 'local' | 'devices';
12
13
  export type HostStatus = 'online' | 'offline' | 'unknown';
13
14
  /** A host as seen at runtime: its persisted entry plus name/provider/status. */
14
15
  export interface Host extends HostEntry {
@@ -17,6 +18,23 @@ export interface Host extends HostEntry {
17
18
  /** True when the host has an explicit overlay/inline entry in the registry. */
18
19
  enrolled?: boolean;
19
20
  status?: HostStatus;
21
+ /**
22
+ * False when the host is listed for honesty but can't carry a `--host` run
23
+ * (today: password-auth devices — offload rides BatchMode=yes ssh). Absent
24
+ * means dispatchable. Cap routing and target pickers filter on this.
25
+ */
26
+ dispatchable?: boolean;
27
+ }
28
+ /**
29
+ * Thrown when a device resolves but can't be used as an offload target because
30
+ * it authenticates with a password. The offload path runs over `sshExec`, whose
31
+ * `SSH_OPTS` force `BatchMode=yes` (no password prompts), so only key / ssh-config
32
+ * auth can carry a `--host` run. Named so the top-level catch prints the message
33
+ * cleanly instead of a stack trace. (Lives here, not registry.ts, so providers
34
+ * can throw it without a circular import; registry.ts re-exports it.)
35
+ */
36
+ export declare class DeviceOffloadUnsupportedError extends Error {
37
+ constructor(name: string);
20
38
  }
21
39
  export interface HostProviderCapabilities {
22
40
  /** Can list/track hosts. */
@@ -2,11 +2,29 @@
2
2
  * Agent-host provider contract.
3
3
  *
4
4
  * A `HostProvider` answers "what are my hosts, and how do I reach them?" — the
5
- * pluggable directory/metadata/reachability layer. v1 ships only the `local`
6
- * provider (ssh-config ∪ inline registry); `rush`/`tailscale`/`crabbox` are
7
- * additive fast-follows behind this same contract. Capability-gated so partial
8
- * providers are first-class (mirrors the cloud provider registry).
5
+ * pluggable directory/metadata/reachability layer. Shipped providers: `local`
6
+ * (ssh-config ∪ inline registry) and `devices` (the Tailscale fleet from
7
+ * `agents devices`); `rush`/`crabbox` remain additive fast-follows behind this
8
+ * same contract. Capability-gated so partial providers are first-class
9
+ * (mirrors the cloud provider registry).
9
10
  */
11
+ /**
12
+ * Thrown when a device resolves but can't be used as an offload target because
13
+ * it authenticates with a password. The offload path runs over `sshExec`, whose
14
+ * `SSH_OPTS` force `BatchMode=yes` (no password prompts), so only key / ssh-config
15
+ * auth can carry a `--host` run. Named so the top-level catch prints the message
16
+ * cleanly instead of a stack trace. (Lives here, not registry.ts, so providers
17
+ * can throw it without a circular import; registry.ts re-exports it.)
18
+ */
19
+ export class DeviceOffloadUnsupportedError extends Error {
20
+ constructor(name) {
21
+ super(`Device "${name}" uses password auth, which --host offload can't use yet ` +
22
+ `(runs go over ssh with BatchMode=yes). Switch it to key auth with ` +
23
+ `\`agents devices set ${name} --auth key\`, or enroll it as a host with ` +
24
+ `\`agents hosts add ${name}\`.`);
25
+ this.name = 'DeviceOffloadUnsupportedError';
26
+ }
27
+ }
10
28
  /**
11
29
  * The ssh target string for a host: the bare name for ssh-config hosts (ssh
12
30
  * resolves HostName/User/Port/Identity), else `user@address` (or `address`).
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Linear auto-close: close Linear issues whose linked GitHub PRs have merged.
3
+ *
4
+ * The pure decision function `shouldCloseIssue` is the only logic here —
5
+ * it is unit-tested and shared with the command routine's equivalent
6
+ * shell check. The rest (Linear GraphQL, gh invocations) lives in the
7
+ * routine YAML's `command:` shell script so the routine stays self-contained
8
+ * and independent of the CLI build.
9
+ *
10
+ * @see apps/cli/routines/linear-autoclose.yml
11
+ */
12
+ /**
13
+ * Subset of `gh pr view --json state,mergedAt` output that drives the
14
+ * close decision. State is one of: OPEN | CLOSED | MERGED (gh GraphQL).
15
+ */
16
+ export interface PrInfo {
17
+ /** gh GraphQL PR state: 'OPEN' | 'CLOSED' | 'MERGED' */
18
+ state: string;
19
+ /** ISO-8601 merge timestamp, or null when the PR was not merged. */
20
+ mergedAt: string | null;
21
+ }
22
+ /**
23
+ * Pure decision: returns true when the PR has been merged and its linked
24
+ * Linear issue should therefore be closed.
25
+ *
26
+ * A PR is considered merged only when both conditions hold:
27
+ * 1. `state === 'MERGED'` — gh marks CLOSED (rejected) separately from MERGED
28
+ * 2. `mergedAt !== null` — defensive guard; a merged PR always carries a timestamp
29
+ */
30
+ export declare function shouldCloseIssue(pr: PrInfo): boolean;
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Linear auto-close: close Linear issues whose linked GitHub PRs have merged.
3
+ *
4
+ * The pure decision function `shouldCloseIssue` is the only logic here —
5
+ * it is unit-tested and shared with the command routine's equivalent
6
+ * shell check. The rest (Linear GraphQL, gh invocations) lives in the
7
+ * routine YAML's `command:` shell script so the routine stays self-contained
8
+ * and independent of the CLI build.
9
+ *
10
+ * @see apps/cli/routines/linear-autoclose.yml
11
+ */
12
+ /**
13
+ * Pure decision: returns true when the PR has been merged and its linked
14
+ * Linear issue should therefore be closed.
15
+ *
16
+ * A PR is considered merged only when both conditions hold:
17
+ * 1. `state === 'MERGED'` — gh marks CLOSED (rejected) separately from MERGED
18
+ * 2. `mergedAt !== null` — defensive guard; a merged PR always carries a timestamp
19
+ */
20
+ export function shouldCloseIssue(pr) {
21
+ return pr.state === 'MERGED' && pr.mergedAt !== null;
22
+ }
package/dist/lib/mcp.d.ts CHANGED
@@ -44,10 +44,35 @@ export declare function parseMcpServerConfig(filePath: string): McpYamlConfig |
44
44
  * command-line options or that contain characters unsafe for argv/identifier use.
45
45
  */
46
46
  export declare function validateMcpServerName(name: string): void;
47
+ /** Path to the user-owned project-trust store (never inside a repo). */
48
+ export declare function getMcpTrustStorePath(): string;
49
+ /**
50
+ * Whether the project that owns `projectAgentsDir` has been explicitly trusted
51
+ * for MCP auto-apply. Untrusted by default (fail closed).
52
+ */
53
+ export declare function isProjectMcpTrusted(projectAgentsDir: string): boolean;
54
+ /**
55
+ * Record explicit trust for the project containing `cwd` so its project-scoped
56
+ * MCP servers may be registered/spawned. Returns the trusted project root, or
57
+ * null when `cwd` is not inside a project (no `.agents/` to trust).
58
+ */
59
+ export declare function trustProjectMcp(cwd?: string): string | null;
60
+ /** Revoke MCP trust for the project containing `cwd`. Returns true if it was trusted. */
61
+ export declare function untrustProjectMcp(cwd?: string): boolean;
47
62
  /**
48
63
  * List all MCP server configs from ~/.agents/mcp/.
64
+ *
65
+ * When `enforceProjectTrust` is set, project-scoped configs are included only
66
+ * for a project the user has explicitly trusted (see `isProjectMcpTrusted`) —
67
+ * this is the choke point that keeps an untrusted cloned repo's MCP servers out
68
+ * of the register/spawn path. It ALSO fixes name-collision shadowing: an
69
+ * untrusted project entry is dropped before dedup, so it can never mask a
70
+ * same-named user entry. Display callers omit the flag to surface project
71
+ * entries (command+args and all) regardless of trust.
49
72
  */
50
- export declare function listMcpServerConfigs(cwd?: string): InstalledMcpServer[];
73
+ export declare function listMcpServerConfigs(cwd?: string, options?: {
74
+ enforceProjectTrust?: boolean;
75
+ }): InstalledMcpServer[];
51
76
  /**
52
77
  * Scan a repository for MCP server YAML configs.
53
78
  * Looks under <repoPath>/mcp/*.yaml — same on-disk layout as ~/.agents/mcp/.
@@ -70,6 +95,7 @@ export declare function installMcpConfigCentrally(sourcePath: string): {
70
95
  */
71
96
  export declare function getMcpServersByName(names?: string[], options?: {
72
97
  cwd?: string;
98
+ enforceProjectTrust?: boolean;
73
99
  }): InstalledMcpServer[];
74
100
  /**
75
101
  * Assemble the JSON payload Claude's `--mcp-config` flag expects from a set of
package/dist/lib/mcp.js CHANGED
@@ -12,9 +12,9 @@ import * as path from 'path';
12
12
  import * as yaml from 'yaml';
13
13
  import { execFileSync } from 'child_process';
14
14
  import * as os from 'os';
15
- import { getMcpDir, getUserMcpDir, getProjectAgentsDir, getVersionsDir } from './state.js';
15
+ import { getMcpDir, getUserMcpDir, getProjectAgentsDir, getVersionsDir, getUserAgentsDir } from './state.js';
16
16
  import { getBinaryPath, getVersionHomePath } from './versions.js';
17
- import { IS_WINDOWS, needsWindowsShell } from './platform/index.js';
17
+ import { IS_WINDOWS, execFileShellSpec } from './platform/index.js';
18
18
  import { AGENTS } from './agents.js';
19
19
  import { isCapable } from './capabilities.js';
20
20
  import { setGeminiAutoUpdateDisabled, updateGeminiSettings } from './gemini-settings.js';
@@ -93,13 +93,116 @@ function isStringRecord(value) {
93
93
  return false;
94
94
  return Object.values(value).every((item) => typeof item === 'string');
95
95
  }
96
+ // ─── Project MCP trust (RUSH-1776) ───────────────────────────────────────────
97
+ // Project-scoped MCP configs (<repo>/.agents/mcp/*.yaml) are UNTRUSTED by
98
+ // default. An MCP server is an arbitrary command spawned under the agent's
99
+ // authority, so merely cloning a hostile repo must never auto-register or run
100
+ // it. A project's MCP servers enter the register/spawn path only after the user
101
+ // explicitly trusts that project (`agents mcp trust`), recorded in a user-owned
102
+ // store OUTSIDE any repo so a cloned repo can't grant itself trust. User- and
103
+ // system-scoped MCPs (~/.agents/mcp/*) are always trusted.
104
+ /** Path to the user-owned project-trust store (never inside a repo). */
105
+ export function getMcpTrustStorePath() {
106
+ return path.join(getUserAgentsDir(), 'mcp-trust.yaml');
107
+ }
108
+ /**
109
+ * Key a project by its ROOT (parent of `.agents/`), resolved through symlinks
110
+ * so the key is stable no matter how the cwd was spelled.
111
+ */
112
+ function normalizeProjectKey(projectAgentsDir) {
113
+ const root = path.dirname(projectAgentsDir);
114
+ try {
115
+ return fs.realpathSync(root);
116
+ }
117
+ catch {
118
+ return path.resolve(root);
119
+ }
120
+ }
121
+ function readTrustedProjects() {
122
+ const storePath = getMcpTrustStorePath();
123
+ if (!fs.existsSync(storePath))
124
+ return new Set();
125
+ let parsed;
126
+ try {
127
+ parsed = yaml.parse(fs.readFileSync(storePath, 'utf-8'));
128
+ }
129
+ catch {
130
+ return new Set();
131
+ }
132
+ const list = parsed && typeof parsed === 'object' && Array.isArray(parsed.trustedProjects)
133
+ ? parsed.trustedProjects
134
+ : [];
135
+ const out = new Set();
136
+ for (const entry of list) {
137
+ if (typeof entry !== 'string' || entry.length === 0)
138
+ continue;
139
+ try {
140
+ out.add(fs.realpathSync(entry));
141
+ }
142
+ catch {
143
+ out.add(path.resolve(entry));
144
+ }
145
+ }
146
+ return out;
147
+ }
148
+ function writeTrustedProjects(trusted) {
149
+ const storePath = getMcpTrustStorePath();
150
+ fs.mkdirSync(path.dirname(storePath), { recursive: true });
151
+ fs.writeFileSync(storePath, yaml.stringify({ trustedProjects: Array.from(trusted).sort() }), 'utf-8');
152
+ }
153
+ /**
154
+ * Whether the project that owns `projectAgentsDir` has been explicitly trusted
155
+ * for MCP auto-apply. Untrusted by default (fail closed).
156
+ */
157
+ export function isProjectMcpTrusted(projectAgentsDir) {
158
+ return readTrustedProjects().has(normalizeProjectKey(projectAgentsDir));
159
+ }
160
+ /**
161
+ * Record explicit trust for the project containing `cwd` so its project-scoped
162
+ * MCP servers may be registered/spawned. Returns the trusted project root, or
163
+ * null when `cwd` is not inside a project (no `.agents/` to trust).
164
+ */
165
+ export function trustProjectMcp(cwd = process.cwd()) {
166
+ const projectAgentsDir = getProjectAgentsDir(cwd);
167
+ if (!projectAgentsDir)
168
+ return null;
169
+ const key = normalizeProjectKey(projectAgentsDir);
170
+ const trusted = readTrustedProjects();
171
+ if (!trusted.has(key)) {
172
+ trusted.add(key);
173
+ writeTrustedProjects(trusted);
174
+ }
175
+ return key;
176
+ }
177
+ /** Revoke MCP trust for the project containing `cwd`. Returns true if it was trusted. */
178
+ export function untrustProjectMcp(cwd = process.cwd()) {
179
+ const projectAgentsDir = getProjectAgentsDir(cwd);
180
+ if (!projectAgentsDir)
181
+ return false;
182
+ const key = normalizeProjectKey(projectAgentsDir);
183
+ const trusted = readTrustedProjects();
184
+ if (!trusted.delete(key))
185
+ return false;
186
+ writeTrustedProjects(trusted);
187
+ return true;
188
+ }
96
189
  /**
97
190
  * List all MCP server configs from ~/.agents/mcp/.
191
+ *
192
+ * When `enforceProjectTrust` is set, project-scoped configs are included only
193
+ * for a project the user has explicitly trusted (see `isProjectMcpTrusted`) —
194
+ * this is the choke point that keeps an untrusted cloned repo's MCP servers out
195
+ * of the register/spawn path. It ALSO fixes name-collision shadowing: an
196
+ * untrusted project entry is dropped before dedup, so it can never mask a
197
+ * same-named user entry. Display callers omit the flag to surface project
198
+ * entries (command+args and all) regardless of trust.
98
199
  */
99
- export function listMcpServerConfigs(cwd = process.cwd()) {
200
+ export function listMcpServerConfigs(cwd = process.cwd(), options = {}) {
100
201
  const dirs = [];
101
202
  const projectAgentsDir = getProjectAgentsDir(cwd);
102
- if (projectAgentsDir) {
203
+ const includeProject = projectAgentsDir !== null
204
+ && (!options.enforceProjectTrust || isProjectMcpTrusted(projectAgentsDir));
205
+ if (projectAgentsDir && includeProject) {
103
206
  dirs.push({ scope: 'project', dir: path.join(projectAgentsDir, 'mcp') });
104
207
  }
105
208
  // User dir first (wins on name collision), then system
@@ -175,7 +278,10 @@ export function installMcpConfigCentrally(sourcePath) {
175
278
  * Otherwise returns all servers.
176
279
  */
177
280
  export function getMcpServersByName(names, options = {}) {
178
- const allServers = listMcpServerConfigs(options.cwd);
281
+ // This feeds the register/spawn path (installMcpServers, workflow assembly),
282
+ // so untrusted project-scoped servers are excluded by default (fail closed).
283
+ const enforceProjectTrust = options.enforceProjectTrust ?? true;
284
+ const allServers = listMcpServerConfigs(options.cwd, { enforceProjectTrust });
179
285
  if (!names || names.length === 0) {
180
286
  return allServers;
181
287
  }
@@ -228,20 +334,24 @@ function installMcpViaClaude(binaryPath, server, versionHome) {
228
334
  server.config.command,
229
335
  ...(server.config.args || [])
230
336
  ];
231
- execFileSync(binaryPath, args, {
337
+ // RUSH-1752: user-controlled MCP command/args must not reach cmd.exe unquoted.
338
+ const spec = execFileShellSpec(binaryPath, args);
339
+ execFileSync(spec.command, spec.args, {
232
340
  stdio: 'pipe',
233
341
  timeout: 30000,
234
342
  env: execEnv,
235
- shell: needsWindowsShell(binaryPath),
343
+ shell: spec.shell,
236
344
  });
237
345
  }
238
346
  else {
239
347
  // claude mcp add --scope user --transport http -- <name> <url>
240
- execFileSync(binaryPath, ['mcp', 'add', '--scope', 'user', '--transport', 'http', '--', server.name, server.config.url], {
348
+ const httpArgs = ['mcp', 'add', '--scope', 'user', '--transport', 'http', '--', server.name, server.config.url];
349
+ const spec = execFileShellSpec(binaryPath, httpArgs);
350
+ execFileSync(spec.command, spec.args, {
241
351
  stdio: 'pipe',
242
352
  timeout: 30000,
243
353
  env: execEnv,
244
- shell: needsWindowsShell(binaryPath),
354
+ shell: spec.shell,
245
355
  });
246
356
  }
247
357
  }
@@ -259,11 +369,13 @@ function installMcpViaCodex(binaryPath, server, versionHome) {
259
369
  server.config.command,
260
370
  ...(server.config.args || [])
261
371
  ];
262
- execFileSync(binaryPath, args, {
372
+ // RUSH-1752: user-controlled MCP command/args must not reach cmd.exe unquoted.
373
+ const spec = execFileShellSpec(binaryPath, args);
374
+ execFileSync(spec.command, spec.args, {
263
375
  stdio: 'pipe',
264
376
  timeout: 30000,
265
377
  env: { ...process.env, HOME: versionHome },
266
- shell: needsWindowsShell(binaryPath),
378
+ shell: spec.shell,
267
379
  });
268
380
  }
269
381
  // Note: Codex may not support HTTP MCPs
@@ -314,7 +426,9 @@ function registerMcpCommand(agentId, name, commandSpec, scope, transport, option
314
426
  ? ['mcp', 'add', '--transport', transport, '--scope', scope, '--', name, ...commandArgs]
315
427
  : ['mcp', 'add', '--', name, ...commandArgs];
316
428
  const env = options.home ? { ...process.env, HOME: options.home } : process.env;
317
- execFileSync(bin, args, { stdio: 'pipe', timeout: 30000, env, shell: needsWindowsShell(bin) });
429
+ // RUSH-1752: user-controlled MCP command/args must not reach cmd.exe unquoted.
430
+ const spec = execFileShellSpec(bin, args);
431
+ execFileSync(spec.command, spec.args, { stdio: 'pipe', timeout: 30000, env, shell: spec.shell });
318
432
  return { success: true };
319
433
  }
320
434
  catch (err) {
@@ -0,0 +1,161 @@
1
+ /**
2
+ * Monitor (event-triggered watcher) configuration, validation, and CRUD.
3
+ *
4
+ * A monitor is a routine whose trigger is a *watched source* instead of a
5
+ * *clock*. It watches a SOURCE, detects a CONDITION change, and fires an ACTION —
6
+ * reusing the routines daemon, dispatch engine, device model, and notify path.
7
+ *
8
+ * Monitors are YAML files in ~/.agents/monitors/. This module owns the on-disk
9
+ * shape (mirroring lib/routines.ts read/write helpers), hand-rolled validation
10
+ * (mirroring validateJob — no zod), and the device-owner eligibility gate.
11
+ */
12
+ import type { AgentId } from '../types.js';
13
+ /** Source types a monitor can watch. */
14
+ export type MonitorSourceType = 'command' | 'poll' | 'poll-http' | 'webhook' | 'ws' | 'file' | 'device';
15
+ /** Webhook source filters — reuse the github/linear matcher shape from triggers/webhook.ts. */
16
+ export interface MonitorWebhookSource {
17
+ source: 'github' | 'linear';
18
+ event: string;
19
+ repo?: string;
20
+ branch?: string;
21
+ action?: string;
22
+ teamKey?: string;
23
+ label?: string;
24
+ }
25
+ /**
26
+ * What a monitor watches. Exactly one source-payload field is populated, keyed by
27
+ * `type`: `command`/`interval` for command/poll, `url`/`interval` for poll-http,
28
+ * `wsUrl` for ws, `path` for file, `device` for device, `webhook` for webhook.
29
+ */
30
+ export interface MonitorSource {
31
+ type: MonitorSourceType;
32
+ /** Shell command whose stdout is the observation (command, poll). */
33
+ command?: string;
34
+ /** Re-evaluation interval (poll, poll-http; optional for command/file/device). e.g. `30s`, `15m`, `8h`. */
35
+ interval?: string;
36
+ /** URL to GET (poll-http). */
37
+ url?: string;
38
+ /** WebSocket URL; each frame is an observation (ws). */
39
+ wsUrl?: string;
40
+ /** File or directory to watch (file). */
41
+ path?: string;
42
+ /** A registered fleet device whose health/reachability is the observation (device). */
43
+ device?: string;
44
+ /** Webhook trigger filters (webhook). */
45
+ webhook?: MonitorWebhookSource;
46
+ }
47
+ /** How an observation becomes a fire. */
48
+ export type MonitorConditionMode = 'on-change' | 'match' | 'every';
49
+ export interface MonitorCondition {
50
+ mode: MonitorConditionMode;
51
+ /** Regex (required for `match` mode) — fire when the observation matches. */
52
+ match?: string;
53
+ /**
54
+ * What counts as "the same event" for de-duplication. When set, the dedupe
55
+ * signature is the first regex match of this expression against the
56
+ * observation (so re-observing the same token is silent); when omitted, the
57
+ * full observation is the signature.
58
+ */
59
+ dedupeKey?: string;
60
+ }
61
+ /** Action types a monitor can fire. */
62
+ export type MonitorActionType = 'run' | 'routine' | 'notify' | 'webhook-out';
63
+ /**
64
+ * What a monitor does on a fire. Shares the run-shaped fields (agent, prompt,
65
+ * mode, effort, timeout) conceptually with JobConfig (lib/routines.ts) so
66
+ * dispatch reuses the routines path (executeJobDetached). The fired event is
67
+ * injected into the prompt as `{event}`.
68
+ */
69
+ export interface ActionConfig {
70
+ type: MonitorActionType;
71
+ /** run: which agent to spawn. */
72
+ agent?: AgentId;
73
+ /** run: the prompt; `{event}` is replaced with the fired event summary. */
74
+ prompt?: string;
75
+ /** run: execution mode (shared with routines). */
76
+ mode?: 'plan' | 'edit' | 'auto' | 'skip' | 'full';
77
+ /** run: reasoning effort (shared with routines). */
78
+ effort?: 'low' | 'medium' | 'high' | 'xhigh' | 'max' | 'auto';
79
+ /** run: kill the action if it runs longer than this (e.g. `10m`). */
80
+ timeout?: string;
81
+ /** routine: name of an existing routine to fire. */
82
+ routine?: string;
83
+ /** notify: channel for the notification (default `telegram`). */
84
+ notifyChannel?: string;
85
+ /** webhook-out: URL to POST the event to. */
86
+ url?: string;
87
+ }
88
+ /** Full monitor configuration (persisted as YAML in ~/.agents/monitors/). */
89
+ export interface MonitorConfig {
90
+ name: string;
91
+ enabled: boolean;
92
+ source: MonitorSource;
93
+ condition: MonitorCondition;
94
+ action: ActionConfig;
95
+ /**
96
+ * Pin-to-one OWNER device — the single machine whose daemon evaluates the
97
+ * source and fires. Exactly-once for v1 (no distributed lock). When set, only
98
+ * that machine is eligible; everywhere else the monitor is inert.
99
+ */
100
+ device?: string;
101
+ /**
102
+ * Fleet allowlist (advanced) — each listed device evaluates and fires
103
+ * independently, like routines' `devices`. Mutually exclusive with `device`.
104
+ */
105
+ devices?: string[];
106
+ /** Execute the ACTION on this machine over SSH (placement), distinct from the owner that fires it. */
107
+ runOn?: string;
108
+ /** Firehose guard: auto-pause the monitor if it fires more than `max` times per `per`. */
109
+ rateLimit?: {
110
+ max: number;
111
+ per: string;
112
+ };
113
+ /** User-defined prompt variables (expanded like routines' variables). */
114
+ variables?: Record<string, string>;
115
+ /** Pin the agent version for `run` actions (omit to use the run strategy). */
116
+ version?: string;
117
+ }
118
+ /**
119
+ * A fired event. `summary` is injected into the action prompt as `{event}`;
120
+ * `payload` carries the structured observation for `webhook-out` and inspection.
121
+ */
122
+ export interface MonitorEvent {
123
+ monitorName: string;
124
+ firedAt: string;
125
+ summary: string;
126
+ payload: Record<string, unknown>;
127
+ }
128
+ /**
129
+ * Parse a human interval string (e.g. `30s`, `15m`, `8h`, `1d`, `1h30m`) into
130
+ * milliseconds. Unlike routines' parseTimeout, seconds are supported (polls tick
131
+ * in seconds). Returns null on empty/unparseable/zero input.
132
+ */
133
+ export declare function parseInterval(interval: string): number | null;
134
+ /**
135
+ * True when the monitor may evaluate + fire on this machine. Owner semantics:
136
+ * `device` (single owner, exactly-once) → only that machine; else `devices`
137
+ * (allowlist) → any listed machine; else unrestricted. Both sides normalize so
138
+ * `Yosemite-S0` and `yosemite-s0.tailnet.ts.net` agree with `yosemite-s0`.
139
+ */
140
+ export declare function monitorRunsOnThisDevice(config: Pick<MonitorConfig, 'device' | 'devices'>): boolean;
141
+ /**
142
+ * Validate a partial monitor config, returning a list of human-readable errors.
143
+ * Hand-rolled like validateJob (lib/routines.ts) — no zod. Rejects: no source,
144
+ * two sources, no action, two actions, match-mode without `match`, plus the
145
+ * per-type field/shape checks.
146
+ */
147
+ export declare function validateMonitor(config: Partial<MonitorConfig>): string[];
148
+ /** List all monitor configs in ~/.agents/monitors/. */
149
+ export declare function listMonitors(): MonitorConfig[];
150
+ /** Read a single monitor config by name. Returns null if not found or corrupt. */
151
+ export declare function readMonitor(name: string): MonitorConfig | null;
152
+ /** Get the filesystem path of a monitor's YAML config, or null if not found. */
153
+ export declare function getMonitorPath(name: string): string | null;
154
+ /** Write a monitor config to disk atomically, omitting fields that match defaults. */
155
+ export declare function writeMonitor(config: MonitorConfig): void;
156
+ /** Delete a monitor config file by name. Returns true if the file existed. */
157
+ export declare function deleteMonitor(name: string): boolean;
158
+ /** Enable or disable a monitor by name. */
159
+ export declare function setMonitorEnabled(name: string, enabled: boolean): void;
160
+ /** Check whether a monitor with the given name exists on disk. */
161
+ export declare function monitorExists(name: string): boolean;