@herbertgao/pi-extensions 2026.11.18 → 2026.11.20

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 (177) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +5 -5
  3. package/THIRD_PARTY_NOTICES.md +3 -24
  4. package/node_modules/@estebanforge/pi-antigravity-bridge/CHANGELOG.md +956 -0
  5. package/node_modules/{pi-antigravity → @estebanforge/pi-antigravity-bridge}/LICENSE +1 -1
  6. package/node_modules/@estebanforge/pi-antigravity-bridge/README.md +240 -0
  7. package/node_modules/@estebanforge/pi-antigravity-bridge/extensions/index.ts +2117 -0
  8. package/node_modules/@estebanforge/pi-antigravity-bridge/package.json +61 -0
  9. package/node_modules/@estebanforge/pi-antigravity-bridge/src/acp/connection.ts +644 -0
  10. package/node_modules/@estebanforge/pi-antigravity-bridge/src/acp/driver.ts +1043 -0
  11. package/node_modules/@estebanforge/pi-antigravity-bridge/src/acp/events.ts +323 -0
  12. package/node_modules/@estebanforge/pi-antigravity-bridge/src/acp/home.ts +145 -0
  13. package/node_modules/@estebanforge/pi-antigravity-bridge/src/acp/jsonrpc.ts +243 -0
  14. package/node_modules/@estebanforge/pi-antigravity-bridge/src/acp/setup.ts +107 -0
  15. package/node_modules/@estebanforge/pi-antigravity-bridge/src/acp/usage-estimate.ts +61 -0
  16. package/node_modules/@estebanforge/pi-antigravity-bridge/src/agents.ts +64 -0
  17. package/node_modules/@estebanforge/pi-antigravity-bridge/src/agy-install.ts +93 -0
  18. package/node_modules/@estebanforge/pi-antigravity-bridge/src/agy-paths.ts +30 -0
  19. package/node_modules/@estebanforge/pi-antigravity-bridge/src/agy-setup.ts +81 -0
  20. package/node_modules/@estebanforge/pi-antigravity-bridge/src/agy-version.ts +289 -0
  21. package/node_modules/@estebanforge/pi-antigravity-bridge/src/approval-detect.ts +161 -0
  22. package/node_modules/@estebanforge/pi-antigravity-bridge/src/approval-gate.ts +233 -0
  23. package/node_modules/@estebanforge/pi-antigravity-bridge/src/approval-hook.ts +325 -0
  24. package/node_modules/@estebanforge/pi-antigravity-bridge/src/artifacts-ui.ts +88 -0
  25. package/node_modules/@estebanforge/pi-antigravity-bridge/src/artifacts.ts +144 -0
  26. package/node_modules/@estebanforge/pi-antigravity-bridge/src/ask-tool.ts +1221 -0
  27. package/node_modules/@estebanforge/pi-antigravity-bridge/src/background-watch.ts +225 -0
  28. package/node_modules/@estebanforge/pi-antigravity-bridge/src/bridge-catalog.ts +197 -0
  29. package/node_modules/@estebanforge/pi-antigravity-bridge/src/config.ts +523 -0
  30. package/node_modules/@estebanforge/pi-antigravity-bridge/src/daily-log.ts +196 -0
  31. package/node_modules/@estebanforge/pi-antigravity-bridge/src/dialog-lock.ts +48 -0
  32. package/node_modules/@estebanforge/pi-antigravity-bridge/src/diff-render.ts +207 -0
  33. package/node_modules/@estebanforge/pi-antigravity-bridge/src/driver-types.ts +168 -0
  34. package/node_modules/@estebanforge/pi-antigravity-bridge/src/driver.ts +907 -0
  35. package/node_modules/@estebanforge/pi-antigravity-bridge/src/engine-picker.ts +160 -0
  36. package/node_modules/@estebanforge/pi-antigravity-bridge/src/frame-guard.ts +38 -0
  37. package/node_modules/@estebanforge/pi-antigravity-bridge/src/handoff-toast.ts +21 -0
  38. package/node_modules/@estebanforge/pi-antigravity-bridge/src/mcp-registration.ts +376 -0
  39. package/node_modules/@estebanforge/pi-antigravity-bridge/src/mcp-server.ts +618 -0
  40. package/node_modules/@estebanforge/pi-antigravity-bridge/src/models.ts +404 -0
  41. package/node_modules/@estebanforge/pi-antigravity-bridge/src/native-tools.ts +104 -0
  42. package/node_modules/@estebanforge/pi-antigravity-bridge/src/parked-turn.ts +125 -0
  43. package/node_modules/@estebanforge/pi-antigravity-bridge/src/patch-cleanup.ts +523 -0
  44. package/node_modules/@estebanforge/pi-antigravity-bridge/src/poll-fold.ts +55 -0
  45. package/node_modules/@estebanforge/pi-antigravity-bridge/src/prompt-images.ts +92 -0
  46. package/node_modules/@estebanforge/pi-antigravity-bridge/src/provider.ts +2214 -0
  47. package/node_modules/@estebanforge/pi-antigravity-bridge/src/queued-messages.ts +152 -0
  48. package/node_modules/@estebanforge/pi-antigravity-bridge/src/quota-notice.ts +40 -0
  49. package/node_modules/@estebanforge/pi-antigravity-bridge/src/redact.ts +27 -0
  50. package/node_modules/@estebanforge/pi-antigravity-bridge/src/sessions.ts +197 -0
  51. package/node_modules/@estebanforge/pi-antigravity-bridge/src/skills.ts +207 -0
  52. package/node_modules/@estebanforge/pi-antigravity-bridge/src/stream-events.ts +161 -0
  53. package/node_modules/@estebanforge/pi-antigravity-bridge/src/stream-sanitizer.ts +180 -0
  54. package/node_modules/@estebanforge/pi-antigravity-bridge/src/subagent-roster.ts +245 -0
  55. package/node_modules/@estebanforge/pi-antigravity-bridge/src/sysprompt-agent.ts +130 -0
  56. package/node_modules/@estebanforge/pi-antigravity-bridge/src/tasks-ui.ts +90 -0
  57. package/node_modules/@estebanforge/pi-antigravity-bridge/src/tasks.ts +174 -0
  58. package/node_modules/@estebanforge/pi-antigravity-bridge/src/usage.ts +174 -0
  59. package/node_modules/@estebanforge/pi-antigravity-bridge/src/web-tools.ts +320 -0
  60. package/node_modules/@herbertgao/pi-cc-extensions/CHANGELOG.md +6 -0
  61. package/node_modules/@herbertgao/pi-cc-extensions/extensions/config/panel.ts +1 -1
  62. package/node_modules/@herbertgao/pi-cc-extensions/extensions/feature/shell/prompt-editor.ts +3 -1
  63. package/node_modules/@herbertgao/pi-cc-extensions/package.json +1 -1
  64. package/node_modules/@herbertgao/pi-subagents/CHANGELOG.md +317 -105
  65. package/node_modules/@herbertgao/pi-subagents/SECURITY.md +2 -2
  66. package/node_modules/@herbertgao/pi-subagents/docs/rpc.md +240 -36
  67. package/node_modules/@herbertgao/pi-subagents/docs/translations.md +63 -0
  68. package/node_modules/@herbertgao/pi-subagents/docs/workflows.md +1 -1
  69. package/node_modules/@herbertgao/pi-subagents/package.json +4 -4
  70. package/node_modules/@herbertgao/pi-subagents/src/agent-file-toggle.ts +2 -0
  71. package/node_modules/@herbertgao/pi-subagents/src/agent-manager.ts +89 -6
  72. package/node_modules/@herbertgao/pi-subagents/src/agent-runner.ts +6 -2
  73. package/node_modules/@herbertgao/pi-subagents/src/agent-types.ts +20 -11
  74. package/node_modules/@herbertgao/pi-subagents/src/cache-retention.ts +39 -0
  75. package/node_modules/@herbertgao/pi-subagents/src/completion-outbox.ts +5 -0
  76. package/node_modules/@herbertgao/pi-subagents/src/cross-extension-rpc.ts +185 -44
  77. package/node_modules/@herbertgao/pi-subagents/src/custom-agents.ts +7 -0
  78. package/node_modules/@herbertgao/pi-subagents/src/group-join.ts +9 -0
  79. package/node_modules/@herbertgao/pi-subagents/src/index.ts +126 -116
  80. package/node_modules/@herbertgao/pi-subagents/src/mention-clone.ts +3 -2
  81. package/node_modules/@herbertgao/pi-subagents/src/settings.ts +21 -0
  82. package/node_modules/@herbertgao/pi-subagents/src/subagent-info.ts +115 -0
  83. package/node_modules/@herbertgao/pi-subagents/src/types.ts +11 -7
  84. package/node_modules/@herbertgao/pi-subagents/src/ui/agent-widget.ts +2 -3
  85. package/node_modules/@herbertgao/pi-subagents/src/ui/conversation-viewer.ts +1 -1
  86. package/node_modules/@herbertgao/pi-subagents/src/workflow/meta.ts +2 -2
  87. package/node_modules/@herbertgao/pi-subagents/src/workflow-info.ts +184 -0
  88. package/node_modules/pi-commandcode-provider/CHANGELOG.md +10 -0
  89. package/node_modules/pi-commandcode-provider/README.md +3 -2
  90. package/node_modules/pi-commandcode-provider/index.ts +120 -13
  91. package/node_modules/pi-commandcode-provider/package.json +4 -3
  92. package/node_modules/pi-commandcode-provider/src/commandcode-catalog.ts +11 -2
  93. package/node_modules/pi-commandcode-provider/src/pricing.ts +17 -1
  94. package/node_modules/pi-commandcode-provider/src/quota-command.ts +3 -2
  95. package/node_modules/pi-commandcode-provider/src/zdr.ts +163 -0
  96. package/node_modules/pi-typesafe/README.md +1 -1
  97. package/node_modules/pi-typesafe/dist/ask.js +6 -3
  98. package/node_modules/pi-typesafe/dist/auth.d.ts +31 -7
  99. package/node_modules/pi-typesafe/dist/auth.js +159 -55
  100. package/node_modules/pi-typesafe/dist/batch.d.ts +16 -2
  101. package/node_modules/pi-typesafe/dist/batch.js +62 -22
  102. package/node_modules/pi-typesafe/dist/calibrate.d.ts +38 -11
  103. package/node_modules/pi-typesafe/dist/calibrate.js +56 -24
  104. package/node_modules/pi-typesafe/dist/client.d.ts +13 -0
  105. package/node_modules/pi-typesafe/dist/client.js +67 -27
  106. package/node_modules/pi-typesafe/dist/errors.d.ts +20 -0
  107. package/node_modules/pi-typesafe/dist/errors.js +105 -2
  108. package/node_modules/pi-typesafe/dist/extension.d.ts +106 -0
  109. package/node_modules/pi-typesafe/dist/extension.js +206 -106
  110. package/node_modules/pi-typesafe/dist/index.d.ts +8 -8
  111. package/node_modules/pi-typesafe/dist/index.js +3 -3
  112. package/node_modules/pi-typesafe/dist/key-prompt.d.ts +20 -0
  113. package/node_modules/pi-typesafe/dist/key-prompt.js +23 -3
  114. package/node_modules/pi-typesafe/dist/login.d.ts +58 -1
  115. package/node_modules/pi-typesafe/dist/login.js +167 -13
  116. package/node_modules/pi-typesafe/dist/schema.d.ts +3 -0
  117. package/node_modules/pi-typesafe/dist/schema.js +47 -13
  118. package/node_modules/pi-typesafe/dist/ui.d.ts +2 -1
  119. package/node_modules/pi-typesafe/dist/usage.d.ts +53 -0
  120. package/node_modules/pi-typesafe/dist/usage.js +79 -40
  121. package/node_modules/pi-typesafe/package.json +3 -3
  122. package/package.json +12 -15
  123. package/node_modules/pi-antigravity/README.md +0 -298
  124. package/node_modules/pi-antigravity/docs/reader-guarantees.md +0 -35
  125. package/node_modules/pi-antigravity/package.json +0 -87
  126. package/node_modules/pi-antigravity/src/auth/accounts.ts +0 -327
  127. package/node_modules/pi-antigravity/src/auth/index.ts +0 -23
  128. package/node_modules/pi-antigravity/src/auth/oauth.ts +0 -445
  129. package/node_modules/pi-antigravity/src/client/client.ts +0 -602
  130. package/node_modules/pi-antigravity/src/client/enterprise.ts +0 -150
  131. package/node_modules/pi-antigravity/src/client/index.ts +0 -2
  132. package/node_modules/pi-antigravity/src/diagnostics/diagnostics.ts +0 -104
  133. package/node_modules/pi-antigravity/src/diagnostics/index.ts +0 -1
  134. package/node_modules/pi-antigravity/src/features/generate-image.ts +0 -132
  135. package/node_modules/pi-antigravity/src/features/google-search.ts +0 -72
  136. package/node_modules/pi-antigravity/src/features/read-url-content.ts +0 -124
  137. package/node_modules/pi-antigravity/src/image/image.ts +0 -427
  138. package/node_modules/pi-antigravity/src/image/index.ts +0 -1
  139. package/node_modules/pi-antigravity/src/image/native.ts +0 -69
  140. package/node_modules/pi-antigravity/src/index.ts +0 -547
  141. package/node_modules/pi-antigravity/src/models/discovery.ts +0 -176
  142. package/node_modules/pi-antigravity/src/models/grouping.ts +0 -487
  143. package/node_modules/pi-antigravity/src/models/index.ts +0 -3
  144. package/node_modules/pi-antigravity/src/models/models.ts +0 -647
  145. package/node_modules/pi-antigravity/src/search/index.ts +0 -1
  146. package/node_modules/pi-antigravity/src/search/search.ts +0 -967
  147. package/node_modules/pi-antigravity/src/stream/index.ts +0 -1
  148. package/node_modules/pi-antigravity/src/stream/stream.ts +0 -1961
  149. package/node_modules/pi-antigravity/src/types/enums.ts +0 -52
  150. package/node_modules/pi-antigravity/src/types/index.ts +0 -2
  151. package/node_modules/pi-antigravity/src/types/types.ts +0 -329
  152. package/node_modules/pi-antigravity/src/usage/index.ts +0 -1
  153. package/node_modules/pi-antigravity/src/usage/usage.ts +0 -437
  154. package/node_modules/pi-antigravity/src/utils/address.ts +0 -180
  155. package/node_modules/pi-antigravity/src/utils/credits.ts +0 -149
  156. package/node_modules/pi-antigravity/src/utils/html.ts +0 -31
  157. package/node_modules/pi-antigravity/src/utils/http.ts +0 -96
  158. package/node_modules/pi-antigravity/src/utils/index.ts +0 -5
  159. package/node_modules/pi-antigravity/src/utils/security.ts +0 -278
  160. package/node_modules/pi-antigravity/src/utils/text.ts +0 -66
  161. package/node_modules/pi-antigravity/src/utils/util.ts +0 -217
  162. package/node_modules/pi-antigravity/src/web-fetch/document.ts +0 -83
  163. package/node_modules/pi-antigravity/src/web-fetch/fences.ts +0 -24
  164. package/node_modules/pi-antigravity/src/web-fetch/header.ts +0 -97
  165. package/node_modules/pi-antigravity/src/web-fetch/index.ts +0 -3
  166. package/node_modules/pi-antigravity/src/web-fetch/inspect.ts +0 -328
  167. package/node_modules/pi-antigravity/src/web-fetch/limits.ts +0 -4
  168. package/node_modules/pi-antigravity/src/web-fetch/markdown.ts +0 -744
  169. package/node_modules/pi-antigravity/src/web-fetch/mdn.ts +0 -164
  170. package/node_modules/pi-antigravity/src/web-fetch/media.ts +0 -11
  171. package/node_modules/pi-antigravity/src/web-fetch/metadata.ts +0 -9
  172. package/node_modules/pi-antigravity/src/web-fetch/representation.ts +0 -93
  173. package/node_modules/pi-antigravity/src/web-fetch/save.ts +0 -71
  174. package/node_modules/pi-antigravity/src/web-fetch/sites.ts +0 -138
  175. package/node_modules/pi-antigravity/src/web-fetch/vendor.d.ts +0 -9
  176. package/node_modules/pi-antigravity/src/web-fetch/web-fetch.ts +0 -957
  177. package/node_modules/pi-antigravity/tsconfig.json +0 -21
@@ -0,0 +1,523 @@
1
+ // Runtime config for the antigravity provider. Persisted at
2
+ // <agentDir>/antigravity-bridge/config.json so the /agy command can
3
+ // toggle settings that take effect on the next turn.
4
+ //
5
+ // Knobs today:
6
+ // mode "accept-edits" (default) or "plan". Drives agy's --mode.
7
+ // skipPermissions true (default). Passes --dangerously-skip-permissions so
8
+ // commands don't hang on an unanswerable prompt in -p mode.
9
+ //
10
+ // Env overrides (AGY_MODE, AGY_SKIP_PERMISSIONS) win over the file so tests
11
+ // and one-off runs can force a setting without editing the file.
12
+
13
+ import fs from "node:fs";
14
+ import path from "node:path";
15
+ import { getAgentDir } from "@earendil-works/pi-coding-agent";
16
+ import { normalizeAgyAgentName } from "./agents.js";
17
+
18
+ /** pi's agent dir (env override PI_CODING_AGENT_DIR, else ~/.pi/agent); every
19
+ * bridge-owned path derives from it so isolated or rebranded pi homes keep
20
+ * their state together (issue #17). */
21
+ export function bridgeDir(agentDir: string = getAgentDir()): string {
22
+ return path.join(agentDir, "antigravity-bridge");
23
+ }
24
+
25
+ export function configPath(agentDir: string = getAgentDir()): string {
26
+ return path.join(bridgeDir(agentDir), "config.json");
27
+ }
28
+
29
+ /** Scoped extensions-data convention shared with other EstebanForge
30
+ * extensions: <configRoot>/extensions-data/<author>/<name>, anchored at
31
+ * dirname(getAgentDir()) so it relocates with the agent dir too. */
32
+ export function extensionsDataDir(agentDir: string = getAgentDir()): string {
33
+ return path.join(path.dirname(agentDir), "extensions-data", "estebanforge", "pi-antigravity-bridge");
34
+ }
35
+
36
+ /** Daily debug logs land here, sorted by day, for post-mortems and user
37
+ * support (see src/daily-log.ts). */
38
+ export function logsDir(agentDir: string = getAgentDir()): string {
39
+ return path.join(extensionsDataDir(agentDir), "logs");
40
+ }
41
+
42
+ /** Native `agy acp` is the default for new installs; stream-json remains
43
+ * a fully supported fallback. */
44
+ export type Engine = "stream-json" | "acp";
45
+
46
+ /** The engine a fresh install runs when nothing is configured (no file key,
47
+ * no AGY_ENGINE): the official ACP server. Unknown/stale stored values also
48
+ * fall back here. Startup checks that the required agy CLI is available. */
49
+ export const DEFAULT_ENGINE: Engine = "acp";
50
+ export type AgyMode = "accept-edits" | "plan";
51
+ export type ThinkingTier = "low" | "medium" | "high";
52
+ export type BridgeTools = "none" | "mcp" | "all";
53
+
54
+ /** How ACP turns report token usage while Gate B stands (agy sends none).
55
+ * "estimate" (default): word-boundary regex over prompt/response/thought
56
+ * text (pi-token-speed's mechanism). "direct": 1 token per streamed delta.
57
+ * "off": keep zero-usage. Real server usage (usageSeen latch) always wins. */
58
+ export type UsageEstimate = "estimate" | "direct" | "off";
59
+
60
+ /** How the approval gate activates (docs/TODO.md section 2.5).
61
+ *
62
+ * "auto" (default): OFF until a third-party pi permission extension is
63
+ * detected (src/approval-detect.ts). When one is found, the gate runs in
64
+ * "shadow" mode so that extension gates agy's native tool calls with zero
65
+ * configuration on its side.
66
+ *
67
+ * "shadow" / "dedicated": force the gate on in that shape. "off": never
68
+ * gate, even when a permission extension is installed. NOTE: "dedicated"
69
+ * currently stages the same shadow tools as "shadow" (warn-logged remap);
70
+ * the explicit antigravity_approve variant is planned - see docs/TODO.md. */
71
+ export type GateMode = "auto" | "shadow" | "dedicated" | "off";
72
+
73
+ /** Fallback decision when no extension blocked a gated call: "ask" uses
74
+ * ctx.ui.confirm (headless = deny, fail-closed), "allow" auto-approves,
75
+ * "deny" auto-rejects. */
76
+ export type GateAskMode = "ask" | "allow" | "deny";
77
+
78
+ export interface GateConfig {
79
+ gateMode: GateMode;
80
+ mode: GateAskMode;
81
+ }
82
+
83
+ export interface AcpConfig {
84
+ /** Gate B stopgap: client-side token estimates for ACP turns so pi's
85
+ * usage surfaces show nonzero numbers. Estimates are labeled as such in
86
+ * /agy doctor and auto-disable when the server sends real usage. */
87
+ usageEstimate: UsageEstimate;
88
+ /** Timeout in ms for ACP session operations (session/new, session/load,
89
+ * session/set_config_option, session/close). Default 20000 (20s); valid
90
+ * range 1000..300000. Env AGY_ACP_SESSION_TIMEOUT_MS wins over the file. */
91
+ sessionTimeoutMs: number;
92
+ /** Advanced: transparently retry session/new once when it times out while
93
+ * the ACP server is still alive. Default false. Warning: doubles the wait
94
+ * on genuinely dead networks (e.g. 40s at default 20s timeout) and may not
95
+ * recover if the server process itself has a wedged socket.
96
+ * Env AGY_ACP_SESSION_RETRY wins over the file. */
97
+ sessionRetryOnTimeout: boolean;
98
+ }
99
+
100
+ export interface AgyConfig {
101
+ /** Engine. Switching requires a pi restart (drivers wire at load). */
102
+ engine: Engine;
103
+ /** Official-server ACP engine options (used when engine = "acp"). */
104
+ acp: AcpConfig;
105
+ mode: AgyMode;
106
+ /** Fail-closed kill switch for provider turns. ON = every provider turn
107
+ * is refused visibly: the agy CLI has no review-only enforcement on any
108
+ * path (probed 2026-10-07, upstream #1181: plan mode stages a plan and
109
+ * executes the write in the same turn on -p and stream-json alike; the
110
+ * provider plan turns are refused on ACP too). Built for callers like
111
+ * `pi -p --no-tools` health checks that must never mutate a workspace
112
+ * and prefer a visible refusal over a write-capable turn. Wins over
113
+ * `mode`; the AskAntigravity delegation tool is refused while this is on
114
+ * (the CLI has no review-only enforcement, so nothing here can run
115
+ * read-only). Env AGY_READONLY wins over the file.
116
+ * Default false. */
117
+ readOnly: boolean;
118
+ /** Auto-approve all agy tool permission requests (--dangerously-skip-permissions).
119
+ * Required for non-interactive use: without it, any `run_command` triggers an
120
+ * interactive y/n prompt that hangs forever in `-p` mode. Defaults true.
121
+ * DANGEROUS: lets agy run arbitrary commands (including destructive ones)
122
+ * without review. The ask tool withholds the flag on plan runs;
123
+ * accept-edits runs need it to avoid hanging on the unanswerable prompt. */
124
+ skipPermissions: boolean;
125
+ /** AskAntigravity tool: default model alias (flash/pro/gemini or exact). */
126
+ defaultModel: string;
127
+ /** AskAntigravity tool: default thinking tier when the alias names none. */
128
+ defaultThinking: ThinkingTier;
129
+ /** Register the AskAntigravity one-shot delegation tool. Default on.
130
+ * off removes the tool entirely for users who want only the provider
131
+ * and models (no delegation tool in the model's window context).
132
+ * Takes effect at pi start (or /reload). */
133
+ askTool: boolean;
134
+ /** Register the agy_web_search + agy_read_url Pi tools. Default OFF:
135
+ * inside Antigravity sessions the model already has native web tools;
136
+ * these exist for NON-Antigravity provider sessions and every call
137
+ * spends Antigravity quota. Takes effect at pi start (or /reload). */
138
+ webTools: boolean;
139
+ /** Set after the one-time notice about a leftover legacy invokeTool patch
140
+ * on the installed pi. The notice never repeats; /agy patch-cleanup is
141
+ * always available. */
142
+ patchCleanupNotified?: boolean;
143
+ /** Custom agy agent for stream-json turns (`--agent`, /agy agent).
144
+ * Absent = agy's default agent. ACP turns ignore it (no protocol slot). */
145
+ agent?: string;
146
+ /** Which pi tools the MCP bridge exposes to agy: "none" (bridge off),
147
+ * "all" (every registered non-builtin tool incl. other Ask* delegations;
148
+ * default - users expect the bridge working out of the box, and the
149
+ * "mcp" surface serves an empty catalog on installs without
150
+ * pi-mcp-adapter), "mcp" (pi-mcp-adapter tools + skills bridge only). */
151
+ bridgeTools: BridgeTools;
152
+ /** Extra pi tool names that get the long (120s) synchronous escalation
153
+ * window, in addition to delegation-shaped names (subagent_run,
154
+ * agent_spawn/agent_wait, *delegate*; issue #6). For custom tools that
155
+ * legitimately run minutes-long but must return a plain result to an
156
+ * orchestrator. Env AGY_ESCALATE_LONG_TOOLS (comma-separated) wins over
157
+ * the file. Takes effect on the next bridge call. */
158
+ escalateLongTools?: string[];
159
+ /** Inject a delta digest of pi-side context (compaction summaries, turns
160
+ * handled by other providers or pi's own tools) into agy prompts.
161
+ *
162
+ * Default ON. The digest is self-gating: it ships only when the delta
163
+ * window is non-empty (a fresh agy conversation from a mid-session model
164
+ * switch or engine switch, another provider's turns, pi-side tool runs,
165
+ * or a compaction summary), and each piece ships once. Pure antigravity
166
+ * sessions get an empty delta, byte-stable prompts, and an intact
167
+ * server-side prompt cache.
168
+ *
169
+ * Turn off for agy-native behavior: prompts then contain only your
170
+ * message, and a conversation opened mid-session starts without prior
171
+ * pi-side context. */
172
+ digest: boolean;
173
+ /** Prepend pi's composed system prompt (pi tool guidance + the global
174
+ * agent-dir AGENTS.md and ancestor AGENTS.md/CLAUDE.md) to the FIRST
175
+ * prompt of each fresh agy conversation.
176
+ *
177
+ * Default ON: agy keeps its own history, so the prefix is sent once per
178
+ * conversation and stays byte-identical afterwards - agy's server-side
179
+ * prompt cache keeps hitting. This is why it is safe here while the G1
180
+ * digest (per-turn) is not. Turn off for agy-native behavior (agy's own
181
+ * system prompt only). */
182
+ systemPrompt: boolean;
183
+ /** Overall cap on ONE agy turn in minutes, both engines. When it fires, the
184
+ * bridge kills the agy process mid-task ("ACP turn exceeded the Xm
185
+ * deadline" / "agy exceeded the Xm turn timeout"). The ACP server binary
186
+ * exposes no timeout flag of its own (--helpfull: debug/notices only), so
187
+ * this bridge cap is the only knob.
188
+ *
189
+ * Default is TTY-aware: 0 (no cap) on an interactive pi - the user aborts
190
+ * with Esc and is the better backstop - and 20 on headless runs, where
191
+ * nobody can abort and one runaway turn blocks the drivers' serialized
192
+ * turn queue. Explicit values opt into the gate: 1..1440 valid, 0
193
+ * disables, anything else (garbage, negative, >1440) falls back to the
194
+ * TTY-aware default. The inactivity stall guard (5m silence) still bounds
195
+ * a hung server either way. Next-turn effect. Env AGY_TURN_TIMEOUT_MIN
196
+ * wins over the file. */
197
+ turnTimeoutMin: number;
198
+ /** Silence cap in minutes, both engines: no stream-json stdout / no ACP
199
+ * session/update for this long fails the turn as a stall. Default 5;
200
+ * 0 disables the guard. Next-turn effect.
201
+ * Env AGY_INACTIVITY_TIMEOUT_MIN wins over the file. */
202
+ inactivityTimeoutMin: number;
203
+ /** Approval gate over agy NATIVE tool calls (create_file, run_command,
204
+ * ...). pi tools agy calls already pass through pi's gates via the G9
205
+ * round-trip; this covers the rest. Default auto (off until a
206
+ * third-party permission extension is detected). See docs/TODO.md 2.5. */
207
+ approvals: GateConfig;
208
+ }
209
+
210
+ const DEFAULTS: AgyConfig = {
211
+ engine: DEFAULT_ENGINE,
212
+ mode: "accept-edits",
213
+ readOnly: false,
214
+ skipPermissions: true,
215
+ defaultModel: "gemini-4-hydrogen",
216
+ defaultThinking: "medium",
217
+ askTool: true,
218
+ webTools: false,
219
+ bridgeTools: "all",
220
+ digest: true,
221
+ systemPrompt: true,
222
+ turnTimeoutMin: 0, // placeholder: the real default is resolved per loadConfig call (defaultTurnTimeoutMin)
223
+ inactivityTimeoutMin: 5,
224
+ approvals: { gateMode: "auto", mode: "ask" },
225
+ acp: {
226
+ usageEstimate: "estimate",
227
+ sessionTimeoutMs: 20_000,
228
+ sessionRetryOnTimeout: false,
229
+ },
230
+ };
231
+
232
+ const MIN_ACP_SESSION_TIMEOUT_MS = 1_000;
233
+ const MAX_ACP_SESSION_TIMEOUT_MS = 300_000;
234
+
235
+ function parseSessionTimeoutMs(raw: unknown, fallback: number): number {
236
+ if (raw === undefined || raw === null || String(raw).trim() === "") return fallback;
237
+ const n = typeof raw === "number" ? raw : Number(String(raw).trim());
238
+ if (!Number.isSafeInteger(n)) return fallback;
239
+ return n >= MIN_ACP_SESSION_TIMEOUT_MS && n <= MAX_ACP_SESSION_TIMEOUT_MS ? n : fallback;
240
+ }
241
+
242
+ /** Hard ceiling for a free-typed turn cap: one day, in minutes. */
243
+ export const MAX_TURN_CAP_MIN = 1440;
244
+
245
+ /** TTY-aware default for the overall turn cap. An interactive user aborts
246
+ * with Esc and is the better backstop; the wall clock only earns its keep
247
+ * where nobody can abort (headless runs: one runaway turn also blocks the
248
+ * drivers' serialized turn queue). */
249
+ export function defaultTurnTimeoutMin(): number {
250
+ return process.stdout.isTTY || process.stdin.isTTY ? 0 : 20;
251
+ }
252
+
253
+ /** Explicit cap minutes: 0 keeps (disable), 1..MAX_TURN_CAP_MIN keeps;
254
+ * garbage, negative, or out-of-range falls back to the caller's default.
255
+ * Pass Number.NaN as the fallback to detect rejection via isNaN. */
256
+ export function parseCapMinutes(raw: string | number | undefined, fallback: number): number {
257
+ if (raw === undefined || String(raw).trim() === "") return fallback;
258
+ const n = typeof raw === "number" ? raw : Number(String(raw).trim());
259
+ if (!Number.isFinite(n)) return fallback;
260
+ if (n === 0) return 0;
261
+ return n >= 1 && n <= MAX_TURN_CAP_MIN ? n : fallback;
262
+ }
263
+
264
+ /** Read the raw config file (no defaults, no env). Shared by loadConfig and
265
+ * saveConfig so persisted writes can never bake env overrides into the file. */
266
+ function readConfigFile(configPath: string): Partial<AgyConfig> {
267
+ try {
268
+ const raw = fs.readFileSync(configPath, "utf8");
269
+ const parsed = JSON.parse(raw);
270
+ if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
271
+ return parsed as Partial<AgyConfig>;
272
+ }
273
+ } catch {
274
+ /* missing or corrupt - fall back to empty */
275
+ }
276
+ return {};
277
+ }
278
+
279
+ /** Strip the retired acp.bin key from the raw config file; returns true when
280
+ * removed. Raw passthrough on purpose (no defaults merge): users keep their
281
+ * minimal hand-edited shape instead of gaining the full default set. */
282
+ export function removeLegacyAcpBin(configFile: string = configPath()): boolean {
283
+ const file = readConfigFile(configFile) as Record<string, unknown>;
284
+ if (!file.acp || typeof file.acp !== "object" || Array.isArray(file.acp)) return false;
285
+ const acp = file.acp as Record<string, unknown>;
286
+ if (!("bin" in acp)) return false;
287
+ delete acp.bin;
288
+ // Rename would replace a dotfiles-managed symlink with a regular file;
289
+ // write through the resolved target so the link survives.
290
+ writeConfigJson(fs.realpathSync(configFile), file);
291
+ return true;
292
+ }
293
+
294
+ /** Load config merged over defaults. Env vars override the file when set. */
295
+ export function loadConfig(configFile: string = configPath()): AgyConfig {
296
+ let file: Partial<AgyConfig> = readConfigFile(configFile);
297
+
298
+ // Env overrides file (matches the skipPermissions pattern).
299
+ // The naive OR `env === "plan" || file.mode === "plan"` would ignore an
300
+ // explicit AGY_MODE=accept-edits when the file says plan, violating the
301
+ // documented precedence. Check env first.
302
+ // Engine: narrow to the known set; anything else (incl. the pre-1.3.2
303
+ // "sqlite" value) falls back to the default engine, same as an absent key.
304
+ // One absence exception: a config file WITHOUT an engine key belongs to an
305
+ // existing install (saveConfig persists the resolved legacy engine), and
306
+ // the acp default is for new installs only - an existing install keeps
307
+ // the legacy stream-json engine. existsSync is false on EACCES too, which reads as fresh
308
+ // and picks acp; an unreadable config dir is a broken install either way.
309
+ const freshInstall = !fs.existsSync(configFile);
310
+ const engineFallback = freshInstall || file.engine !== undefined ? DEFAULT_ENGINE : "stream-json";
311
+ const engine = narrowEngine(String(process.env.AGY_ENGINE ?? file.engine ?? engineFallback).toLowerCase());
312
+
313
+ const mode: AgyMode =
314
+ process.env.AGY_MODE !== undefined
315
+ ? process.env.AGY_MODE === "plan"
316
+ ? "plan"
317
+ : "accept-edits"
318
+ : file.mode === "plan"
319
+ ? "plan"
320
+ : "accept-edits";
321
+
322
+ const envPerm = process.env.AGY_SKIP_PERMISSIONS;
323
+ const skipPermissions =
324
+ envPerm !== undefined
325
+ ? envPerm === "1" || envPerm.toLowerCase() === "true"
326
+ : file.skipPermissions ?? DEFAULTS.skipPermissions;
327
+
328
+ const envReadOnly = process.env.AGY_READONLY;
329
+ // Fail-closed parse for a safety knob: empty string means the user
330
+ // explicitly blanked it (off), but ANY other set value that is not a
331
+ // spelled-out falsity counts as ON. " 1", "yes", typos: all refuse.
332
+ const readOnly =
333
+ envReadOnly !== undefined
334
+ ? envReadOnly === ""
335
+ ? false
336
+ : !["0", "false", "off"].includes(envReadOnly.trim().toLowerCase())
337
+ : file.readOnly ?? DEFAULTS.readOnly;
338
+
339
+ const defaultModelRaw =
340
+ process.env.AGY_DEFAULT_MODEL ?? file.defaultModel ?? DEFAULTS.defaultModel;
341
+ const defaultModel =
342
+ typeof defaultModelRaw === "string" ? defaultModelRaw.trim() || DEFAULTS.defaultModel : DEFAULTS.defaultModel;
343
+
344
+ const envThink = process.env.AGY_DEFAULT_THINKING;
345
+ const thinkRaw = (envThink ?? file.defaultThinking ?? DEFAULTS.defaultThinking).toLowerCase();
346
+ const defaultThinking: ThinkingTier =
347
+ thinkRaw === "low" || thinkRaw === "high" ? thinkRaw : "medium";
348
+
349
+ const askTool = process.env.AGY_ASK_TOOL !== undefined
350
+ ? ["1", "true", "on"].includes(process.env.AGY_ASK_TOOL.toLowerCase())
351
+ : file.askTool ?? DEFAULTS.askTool;
352
+
353
+ const webTools = process.env.AGY_WEB_TOOLS !== undefined
354
+ ? ["1", "true", "on"].includes(process.env.AGY_WEB_TOOLS.toLowerCase())
355
+ : file.webTools ?? DEFAULTS.webTools;
356
+
357
+ const bridgeRaw = (process.env.AGY_BRIDGE_TOOLS ?? file.bridgeTools ?? DEFAULTS.bridgeTools).toLowerCase();
358
+ const bridgeTools: BridgeTools =
359
+ bridgeRaw === "none" || bridgeRaw === "all" || bridgeRaw === "mcp"
360
+ ? bridgeRaw
361
+ : DEFAULTS.bridgeTools;
362
+
363
+ // Extra long-window tool names for the bridge escalation (issue #6).
364
+ // Comma-separated in the env; junk entries and junk shapes are dropped,
365
+ // not fatal - loadConfig runs per bridge call and must never throw.
366
+ const longToolsRaw: unknown =
367
+ process.env.AGY_ESCALATE_LONG_TOOLS ?? file.escalateLongTools;
368
+ const longToolsParts: unknown[] = typeof longToolsRaw === "string"
369
+ ? longToolsRaw.split(",")
370
+ : Array.isArray(longToolsRaw) ? longToolsRaw : [];
371
+ const escalateLongTools = longToolsParts
372
+ .filter((s): s is string => typeof s === "string")
373
+ .map((s) => s.trim())
374
+ .filter((s) => s.length > 0)
375
+ .slice(0, 32);
376
+
377
+ const digest = process.env.AGY_DIGEST !== undefined
378
+ ? ["1", "true", "on"].includes(process.env.AGY_DIGEST.toLowerCase())
379
+ : file.digest ?? true;
380
+
381
+ const envSys = process.env.AGY_SYSTEM_PROMPT;
382
+ const systemPrompt = envSys !== undefined
383
+ ? ["1", "true", "on"].includes(envSys.toLowerCase())
384
+ : file.systemPrompt ?? DEFAULTS.systemPrompt;
385
+
386
+ // Turn caps, minutes. Env wins over the file (same pattern as mode).
387
+ const turnTimeoutMin = parseCapMinutes(
388
+ process.env.AGY_TURN_TIMEOUT_MIN ?? file.turnTimeoutMin,
389
+ defaultTurnTimeoutMin(),
390
+ );
391
+ const inactivityTimeoutMin = parseCapMinutes(
392
+ process.env.AGY_INACTIVITY_TIMEOUT_MIN ?? file.inactivityTimeoutMin,
393
+ DEFAULTS.inactivityTimeoutMin,
394
+ );
395
+
396
+ // Approval gate (docs/TODO.md 2.5). Unknown values fall back to "auto"
397
+ // so a typo can never silently force the gate on.
398
+ const gateRaw = (process.env.AGY_APPROVALS ?? file.approvals?.gateMode ?? DEFAULTS.approvals.gateMode).toLowerCase();
399
+ const gateMode: GateMode =
400
+ gateRaw === "shadow" || gateRaw === "dedicated" || gateRaw === "off"
401
+ ? gateRaw
402
+ : "auto";
403
+ const askRaw = (process.env.AGY_APPROVALS_MODE ?? file.approvals?.mode ?? DEFAULTS.approvals.mode).toLowerCase();
404
+ const gateAskMode: GateAskMode =
405
+ askRaw === "allow" || askRaw === "deny"
406
+ ? askRaw
407
+ : "ask";
408
+
409
+ const fileAcp = (typeof file.acp === "object" && file.acp !== null ? file.acp : {}) as Partial<AcpConfig>;
410
+ // Unknown values fall back to "estimate" (same narrow-parse pattern as
411
+ // gateMode: a typo must never silently change behavior).
412
+ const usageRaw = String(
413
+ process.env.AGY_USAGE_ESTIMATE ?? fileAcp.usageEstimate ?? DEFAULTS.acp.usageEstimate,
414
+ ).toLowerCase();
415
+ const usageEstimate: UsageEstimate =
416
+ usageRaw === "direct" || usageRaw === "off" ? usageRaw : "estimate";
417
+ const sessionTimeoutMs = parseSessionTimeoutMs(
418
+ process.env.AGY_ACP_SESSION_TIMEOUT_MS ?? fileAcp.sessionTimeoutMs,
419
+ DEFAULTS.acp.sessionTimeoutMs,
420
+ );
421
+ const envSessionRetry = process.env.AGY_ACP_SESSION_RETRY;
422
+ const sessionRetryOnTimeout = envSessionRetry !== undefined
423
+ ? ["1", "true", "on"].includes(envSessionRetry.toLowerCase())
424
+ : fileAcp.sessionRetryOnTimeout === true;
425
+ const acp: AcpConfig = { usageEstimate, sessionTimeoutMs, sessionRetryOnTimeout };
426
+
427
+ // Custom agent for stream-json turns (--agent). Env wins over the file
428
+ // (same pattern as mode). Normalized (trim/cap/control-strip); an empty
429
+ // result means agy's default agent.
430
+ const agent = normalizeAgyAgentName(process.env.AGY_AGENT ?? file.agent);
431
+
432
+ return {
433
+ engine,
434
+ acp,
435
+ mode,
436
+ readOnly,
437
+ skipPermissions,
438
+ defaultModel,
439
+ defaultThinking,
440
+ askTool,
441
+ webTools,
442
+ bridgeTools,
443
+ escalateLongTools,
444
+ digest,
445
+ systemPrompt,
446
+ turnTimeoutMin,
447
+ inactivityTimeoutMin,
448
+ agent,
449
+ approvals: { gateMode, mode: gateAskMode },
450
+ patchCleanupNotified: file.patchCleanupNotified === true,
451
+ };
452
+ }
453
+
454
+ /** Narrow a raw engine string to the known set; anything unknown (incl. the
455
+ * pre-1.3.2 "sqlite" value) falls to the default engine. Shared by
456
+ * loadConfig's effective-engine resolution and saveConfig's persistence so
457
+ * the two can never drift. */
458
+ function narrowEngine(raw: string): Engine {
459
+ return raw === "acp" || raw === "stream-json" ? raw : DEFAULT_ENGINE;
460
+ }
461
+
462
+ /** Atomic JSON write (temp + rename, 0600) shared by every config writer. */
463
+ function writeConfigJson(configPath: string, value: unknown): void {
464
+ const dir = path.dirname(configPath);
465
+ fs.mkdirSync(dir, { recursive: true });
466
+ const tmp = `${configPath}.${process.pid}.tmp`;
467
+ try {
468
+ fs.writeFileSync(tmp, JSON.stringify(value, null, 2) + "\n", { mode: 0o600 });
469
+ fs.renameSync(tmp, configPath);
470
+ } catch (err) {
471
+ try {
472
+ fs.unlinkSync(tmp);
473
+ } catch {
474
+ /* nothing to clean */
475
+ }
476
+ throw err;
477
+ }
478
+ }
479
+
480
+ /** Atomically persist a config patch (temp + rename). Merges onto the RAW
481
+ * FILE values, never loadConfig(): env overrides must not be baked into the
482
+ * file (AGY_READONLY=1 in the env would otherwise persist readOnly: true to
483
+ * disk on any unrelated save, outliving the env var). Nested objects are
484
+ * re-normalized to their known keys: a raw passthrough would resurrect
485
+ * legacy keys (acp.permissions) that loadConfig normalization drops. */
486
+ export type ConfigPatch = Partial<Omit<AgyConfig, "acp">> & { acp?: Partial<AcpConfig> };
487
+
488
+ export function saveConfig(patch: ConfigPatch, configFile: string = configPath()): AgyConfig {
489
+ const file = readConfigFile(configFile);
490
+ // Null-safe on nested objects: a hand-edited or legacy file can carry
491
+ // "acp": null; loadConfig tolerates it, so must saveConfig. Key presence
492
+ // triggers normalization (a null becomes the default shape, never null).
493
+ const fileAcp: Partial<AcpConfig> = file.acp && typeof file.acp === "object" ? file.acp : {};
494
+ const fileApprovals: Partial<GateConfig> = file.approvals && typeof file.approvals === "object" ? file.approvals : {};
495
+ // Engine persistence follows loadConfig's continuity rule minus env (env
496
+ // must never be baked into the file). A legacy file without an engine key
497
+ // persists "stream-json": the bare DEFAULTS spread below would otherwise
498
+ // write the acp default into an existing install's file on any unrelated
499
+ // save, switching an existing install to ACP on the next start.
500
+ const persistedEngine = patch.engine ?? file.engine
501
+ ?? (fs.existsSync(configFile) ? "stream-json" : DEFAULT_ENGINE);
502
+ const { acp: patchAcp, ...restPatch } = patch;
503
+ const mergedAcp: AcpConfig = {
504
+ usageEstimate: patchAcp?.usageEstimate ?? fileAcp.usageEstimate ?? DEFAULTS.acp.usageEstimate,
505
+ sessionTimeoutMs: parseSessionTimeoutMs(
506
+ patchAcp?.sessionTimeoutMs ?? fileAcp.sessionTimeoutMs,
507
+ DEFAULTS.acp.sessionTimeoutMs,
508
+ ),
509
+ sessionRetryOnTimeout: patchAcp?.sessionRetryOnTimeout ?? (fileAcp.sessionRetryOnTimeout === true),
510
+ };
511
+ const next: AgyConfig = {
512
+ ...DEFAULTS,
513
+ ...file,
514
+ ...(file.approvals !== undefined
515
+ ? { approvals: { gateMode: fileApprovals.gateMode ?? DEFAULTS.approvals.gateMode, mode: fileApprovals.mode ?? DEFAULTS.approvals.mode } }
516
+ : {}),
517
+ ...restPatch,
518
+ acp: mergedAcp,
519
+ engine: narrowEngine(String(persistedEngine).toLowerCase()),
520
+ };
521
+ writeConfigJson(configFile, next);
522
+ return next;
523
+ }