@estebanforge/pi-antigravity-bridge 1.10.9 → 1.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/README.md +10 -8
- package/extensions/index.ts +58 -14
- package/package.json +1 -1
- package/src/acp/connection.ts +12 -0
- package/src/acp/driver.ts +31 -1
- package/src/acp/events.ts +11 -0
- package/src/acp/home.ts +2 -1
- package/src/approval-detect.ts +25 -10
- package/src/approval-gate.ts +45 -20
- package/src/approval-hook.ts +57 -2
- package/src/ask-tool.ts +5 -6
- package/src/config.ts +44 -45
- package/src/diff-render.ts +3 -1
- package/src/handoff-toast.ts +21 -0
- package/src/mcp-registration.ts +6 -12
- package/src/mcp-server.ts +3 -5
- package/src/models.ts +10 -13
- package/src/patch-cleanup.ts +7 -3
- package/src/provider.ts +179 -39
- package/src/sessions.ts +22 -10
- package/src/skills.ts +27 -12
package/src/approval-hook.ts
CHANGED
|
@@ -65,9 +65,10 @@ function gateGroupPid(group: unknown): number | null {
|
|
|
65
65
|
return m ? Number(m[1]) : null;
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
/** agy native tools worth gating: everything that mutates the machine.
|
|
68
|
+
/** agy native tools worth gating: everything that mutates the machine.
|
|
69
|
+
* Includes both the legacy and the agy 1.5.11+ tool families (issue #18). */
|
|
69
70
|
export const GATED_AGY_TOOLS =
|
|
70
|
-
"create_file|write_to_file|replace_file_content|multi_replace_file_content|edit_file|run_command";
|
|
71
|
+
"create_file|write_to_file|write_file|replace_file_content|multi_replace_file_content|edit_file|run_command";
|
|
71
72
|
|
|
72
73
|
/** The same list as a set, for the bridge's POST /approval validation: a
|
|
73
74
|
* payload for anything else is answered with a direct deny (defense in
|
|
@@ -222,6 +223,60 @@ export function stageGateHooks(dir: string, opts: StageOptions): StageResult {
|
|
|
222
223
|
return { wrote: true };
|
|
223
224
|
}
|
|
224
225
|
|
|
226
|
+
/** Deliver this session's gate group into a staged ACP home's
|
|
227
|
+
* config/hooks.json. The staged home is private per connection (stageAgyHome),
|
|
228
|
+
* so foreign groups - the user's global hooks, copied in by stageAgyHome -
|
|
229
|
+
* are preserved and only this pid's group is added. An unparsable existing
|
|
230
|
+
* file is gate-only overwritten: agy could not have read it either. No-op
|
|
231
|
+
* only when the per-pid gate file is ABSENT (gate off at load); a staged
|
|
232
|
+
* file that exists but cannot be read, parsed, or does not carry this
|
|
233
|
+
* session's usable group throws, because running the spawn ungated while
|
|
234
|
+
* the gate is on would fail open (issue #18).
|
|
235
|
+
* mergeGateGroupIntoAcpHome's delivery is probed live on agy 1.5.12
|
|
236
|
+
* (scripts/probe-acp-hooks.mjs). */
|
|
237
|
+
export function mergeGateGroupIntoAcpHome(stagedHome: string, sourceDir: string = bridgeMcpConfigDir()): StageResult {
|
|
238
|
+
let staged: Record<string, unknown>;
|
|
239
|
+
try {
|
|
240
|
+
staged = JSON.parse(fs.readFileSync(gateHooksFile(sourceDir), "utf8")) as Record<string, unknown>;
|
|
241
|
+
} catch (err) {
|
|
242
|
+
if ((err as NodeJS.ErrnoException).code === "ENOENT") return { wrote: false, reason: "no staged gate" };
|
|
243
|
+
throw err;
|
|
244
|
+
}
|
|
245
|
+
// The source file is session-private (per-pid dir), so a parsable file
|
|
246
|
+
// without this session's usable group means something rewrote it between
|
|
247
|
+
// staging and merge - spawning ungated then would fail open.
|
|
248
|
+
const group: unknown = staged[gateGroupKey()];
|
|
249
|
+
if (!group || typeof group !== "object") {
|
|
250
|
+
throw new Error(`staged approval-gate file has no usable group for this session (${gateGroupKey()}); failing closed`);
|
|
251
|
+
}
|
|
252
|
+
const target = path.join(stagedHome, "config", "hooks.json");
|
|
253
|
+
let merged: Record<string, unknown> = {};
|
|
254
|
+
try {
|
|
255
|
+
const existing = JSON.parse(fs.readFileSync(target, "utf8")) as unknown;
|
|
256
|
+
if (existing && typeof existing === "object" && !Array.isArray(existing)) {
|
|
257
|
+
merged = existing as Record<string, unknown>;
|
|
258
|
+
}
|
|
259
|
+
} catch {
|
|
260
|
+
/* absent or unparsable: the merge result is gate-only */
|
|
261
|
+
}
|
|
262
|
+
merged[gateGroupKey()] = group;
|
|
263
|
+
const next = JSON.stringify(merged, null, 2) + "\n";
|
|
264
|
+
fs.mkdirSync(path.dirname(target), { recursive: true, mode: 0o700 });
|
|
265
|
+
const tmp = `${target}.${process.pid}.tmp`;
|
|
266
|
+
try {
|
|
267
|
+
fs.writeFileSync(tmp, next, { mode: 0o600 });
|
|
268
|
+
fs.renameSync(tmp, target);
|
|
269
|
+
} catch (err) {
|
|
270
|
+
try {
|
|
271
|
+
fs.unlinkSync(tmp);
|
|
272
|
+
} catch {
|
|
273
|
+
/* nothing */
|
|
274
|
+
}
|
|
275
|
+
throw err;
|
|
276
|
+
}
|
|
277
|
+
return { wrote: true };
|
|
278
|
+
}
|
|
279
|
+
|
|
225
280
|
/** Remove the private gate hooks file. The rest of the per-pid dir (the MCP
|
|
226
281
|
* config) is managed by the bridge server lifecycle and stays until close. */
|
|
227
282
|
export function removeGateHooks(dir: string = bridgeMcpConfigDir()): StageResult {
|
package/src/ask-tool.ts
CHANGED
|
@@ -16,7 +16,7 @@ import { randomBytes } from "node:crypto";
|
|
|
16
16
|
import * as fs from "node:fs";
|
|
17
17
|
import * as path from "node:path";
|
|
18
18
|
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
19
|
-
import { buildSessionContext
|
|
19
|
+
import { buildSessionContext } from "@earendil-works/pi-coding-agent";
|
|
20
20
|
import { Text } from "@earendil-works/pi-tui";
|
|
21
21
|
import { contentText, type ThinkingLevel } from "@earendil-works/pi-ai";
|
|
22
22
|
import { Type } from "typebox";
|
|
@@ -41,7 +41,7 @@ import {
|
|
|
41
41
|
type ModelEntry,
|
|
42
42
|
type Mode,
|
|
43
43
|
} from "@estebanforge/pi-ask-shared";
|
|
44
|
-
import { loadConfig, type AgyMode, type ThinkingTier } from "./config.js";
|
|
44
|
+
import { extensionsDataDir, loadConfig, type AgyMode, type ThinkingTier } from "./config.js";
|
|
45
45
|
import { redactText } from "./redact.js";
|
|
46
46
|
import { acquireBridgeSuppression } from "./mcp-registration.js";
|
|
47
47
|
import { AGY_EFFORT_ORDER, spawnAgyModelsRaw, toAgyEffort } from "./models.js";
|
|
@@ -1169,11 +1169,10 @@ function renderAgentMessagesMarkdown(messages: readonly unknown[]): string {
|
|
|
1169
1169
|
return lines.join("\n");
|
|
1170
1170
|
}
|
|
1171
1171
|
|
|
1172
|
-
/** Centralized scratch dir for full-context exports
|
|
1173
|
-
*
|
|
1174
|
-
* Derived from getAgentDir() so rebranded distros resolve correctly. */
|
|
1172
|
+
/** Centralized scratch dir for full-context exports (see src/config.ts
|
|
1173
|
+
* extensionsDataDir). */
|
|
1175
1174
|
function askContextDir(): string {
|
|
1176
|
-
return
|
|
1175
|
+
return extensionsDataDir();
|
|
1177
1176
|
}
|
|
1178
1177
|
|
|
1179
1178
|
interface AgyDetails {
|
package/src/config.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// Runtime config for the antigravity provider. Persisted at
|
|
2
|
-
//
|
|
2
|
+
// <agentDir>/antigravity-bridge/config.json so the /agy command can
|
|
3
3
|
// toggle settings that take effect on the next turn.
|
|
4
4
|
//
|
|
5
5
|
// Knobs today:
|
|
@@ -11,31 +11,32 @@
|
|
|
11
11
|
// and one-off runs can force a setting without editing the file.
|
|
12
12
|
|
|
13
13
|
import fs from "node:fs";
|
|
14
|
-
import os from "node:os";
|
|
15
14
|
import path from "node:path";
|
|
15
|
+
import { getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
16
16
|
import { normalizeAgyAgentName } from "./agents.js";
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
"antigravity-bridge"
|
|
23
|
-
|
|
24
|
-
|
|
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
|
+
}
|
|
25
35
|
|
|
26
36
|
/** Daily debug logs land here, sorted by day, for post-mortems and user
|
|
27
|
-
* support (see src/daily-log.ts).
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
export function logsDir(): string {
|
|
31
|
-
return path.join(
|
|
32
|
-
os.homedir(),
|
|
33
|
-
".pi",
|
|
34
|
-
"extensions-data",
|
|
35
|
-
"estebanforge",
|
|
36
|
-
"pi-antigravity-bridge",
|
|
37
|
-
"logs",
|
|
38
|
-
);
|
|
37
|
+
* support (see src/daily-log.ts). */
|
|
38
|
+
export function logsDir(agentDir: string = getAgentDir()): string {
|
|
39
|
+
return path.join(extensionsDataDir(agentDir), "logs");
|
|
39
40
|
}
|
|
40
41
|
|
|
41
42
|
/** Native `agy acp` is the default for new installs; stream-json remains
|
|
@@ -156,18 +157,18 @@ export interface AgyConfig {
|
|
|
156
157
|
* the file. Takes effect on the next bridge call. */
|
|
157
158
|
escalateLongTools?: string[];
|
|
158
159
|
/** Inject a delta digest of pi-side context (compaction summaries, turns
|
|
159
|
-
* handled by other providers or pi's own tools) into
|
|
160
|
+
* handled by other providers or pi's own tools) into agy prompts.
|
|
160
161
|
*
|
|
161
|
-
* Default
|
|
162
|
-
*
|
|
163
|
-
*
|
|
164
|
-
*
|
|
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.
|
|
165
168
|
*
|
|
166
|
-
*
|
|
167
|
-
*
|
|
168
|
-
*
|
|
169
|
-
* sessions gain nothing: agy already keeps its own history, and bridge
|
|
170
|
-
* round-trips deliver tool results through the bridge, not the digest. */
|
|
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. */
|
|
171
172
|
digest: boolean;
|
|
172
173
|
/** Prepend pi's composed system prompt (pi tool guidance + the global
|
|
173
174
|
* agent-dir AGENTS.md and ancestor AGENTS.md/CLAUDE.md) to the FIRST
|
|
@@ -216,7 +217,7 @@ const DEFAULTS: AgyConfig = {
|
|
|
216
217
|
askTool: true,
|
|
217
218
|
webTools: false,
|
|
218
219
|
bridgeTools: "all",
|
|
219
|
-
digest:
|
|
220
|
+
digest: true,
|
|
220
221
|
systemPrompt: true,
|
|
221
222
|
turnTimeoutMin: 0, // placeholder: the real default is resolved per loadConfig call (defaultTurnTimeoutMin)
|
|
222
223
|
inactivityTimeoutMin: 5,
|
|
@@ -278,21 +279,21 @@ function readConfigFile(configPath: string): Partial<AgyConfig> {
|
|
|
278
279
|
/** Strip the retired acp.bin key from the raw config file; returns true when
|
|
279
280
|
* removed. Raw passthrough on purpose (no defaults merge): users keep their
|
|
280
281
|
* minimal hand-edited shape instead of gaining the full default set. */
|
|
281
|
-
export function removeLegacyAcpBin(
|
|
282
|
-
const file = readConfigFile(
|
|
282
|
+
export function removeLegacyAcpBin(configFile: string = configPath()): boolean {
|
|
283
|
+
const file = readConfigFile(configFile) as Record<string, unknown>;
|
|
283
284
|
if (!file.acp || typeof file.acp !== "object" || Array.isArray(file.acp)) return false;
|
|
284
285
|
const acp = file.acp as Record<string, unknown>;
|
|
285
286
|
if (!("bin" in acp)) return false;
|
|
286
287
|
delete acp.bin;
|
|
287
288
|
// Rename would replace a dotfiles-managed symlink with a regular file;
|
|
288
289
|
// write through the resolved target so the link survives.
|
|
289
|
-
writeConfigJson(fs.realpathSync(
|
|
290
|
+
writeConfigJson(fs.realpathSync(configFile), file);
|
|
290
291
|
return true;
|
|
291
292
|
}
|
|
292
293
|
|
|
293
294
|
/** Load config merged over defaults. Env vars override the file when set. */
|
|
294
|
-
export function loadConfig(
|
|
295
|
-
let file: Partial<AgyConfig> = readConfigFile(
|
|
295
|
+
export function loadConfig(configFile: string = configPath()): AgyConfig {
|
|
296
|
+
let file: Partial<AgyConfig> = readConfigFile(configFile);
|
|
296
297
|
|
|
297
298
|
// Env overrides file (matches the skipPermissions pattern).
|
|
298
299
|
// The naive OR `env === "plan" || file.mode === "plan"` would ignore an
|
|
@@ -305,7 +306,7 @@ export function loadConfig(configPath: string = CONFIG_PATH): AgyConfig {
|
|
|
305
306
|
// the acp default is for new installs only - an existing install keeps
|
|
306
307
|
// the legacy stream-json engine. existsSync is false on EACCES too, which reads as fresh
|
|
307
308
|
// and picks acp; an unreadable config dir is a broken install either way.
|
|
308
|
-
const freshInstall = !fs.existsSync(
|
|
309
|
+
const freshInstall = !fs.existsSync(configFile);
|
|
309
310
|
const engineFallback = freshInstall || file.engine !== undefined ? DEFAULT_ENGINE : "stream-json";
|
|
310
311
|
const engine = narrowEngine(String(process.env.AGY_ENGINE ?? file.engine ?? engineFallback).toLowerCase());
|
|
311
312
|
|
|
@@ -375,7 +376,7 @@ export function loadConfig(configPath: string = CONFIG_PATH): AgyConfig {
|
|
|
375
376
|
|
|
376
377
|
const digest = process.env.AGY_DIGEST !== undefined
|
|
377
378
|
? ["1", "true", "on"].includes(process.env.AGY_DIGEST.toLowerCase())
|
|
378
|
-
: file.digest ??
|
|
379
|
+
: file.digest ?? true;
|
|
379
380
|
|
|
380
381
|
const envSys = process.env.AGY_SYSTEM_PROMPT;
|
|
381
382
|
const systemPrompt = envSys !== undefined
|
|
@@ -484,8 +485,8 @@ function writeConfigJson(configPath: string, value: unknown): void {
|
|
|
484
485
|
* legacy keys (acp.permissions) that loadConfig normalization drops. */
|
|
485
486
|
export type ConfigPatch = Partial<Omit<AgyConfig, "acp">> & { acp?: Partial<AcpConfig> };
|
|
486
487
|
|
|
487
|
-
export function saveConfig(patch: ConfigPatch,
|
|
488
|
-
const file = readConfigFile(
|
|
488
|
+
export function saveConfig(patch: ConfigPatch, configFile: string = configPath()): AgyConfig {
|
|
489
|
+
const file = readConfigFile(configFile);
|
|
489
490
|
// Null-safe on nested objects: a hand-edited or legacy file can carry
|
|
490
491
|
// "acp": null; loadConfig tolerates it, so must saveConfig. Key presence
|
|
491
492
|
// triggers normalization (a null becomes the default shape, never null).
|
|
@@ -497,7 +498,7 @@ export function saveConfig(patch: ConfigPatch, configPath: string = CONFIG_PATH)
|
|
|
497
498
|
// write the acp default into an existing install's file on any unrelated
|
|
498
499
|
// save, switching an existing install to ACP on the next start.
|
|
499
500
|
const persistedEngine = patch.engine ?? file.engine
|
|
500
|
-
?? (fs.existsSync(
|
|
501
|
+
?? (fs.existsSync(configFile) ? "stream-json" : DEFAULT_ENGINE);
|
|
501
502
|
const { acp: patchAcp, ...restPatch } = patch;
|
|
502
503
|
const mergedAcp: AcpConfig = {
|
|
503
504
|
usageEstimate: patchAcp?.usageEstimate ?? fileAcp.usageEstimate ?? DEFAULTS.acp.usageEstimate,
|
|
@@ -517,8 +518,6 @@ export function saveConfig(patch: ConfigPatch, configPath: string = CONFIG_PATH)
|
|
|
517
518
|
acp: mergedAcp,
|
|
518
519
|
engine: narrowEngine(String(persistedEngine).toLowerCase()),
|
|
519
520
|
};
|
|
520
|
-
writeConfigJson(
|
|
521
|
+
writeConfigJson(configFile, next);
|
|
521
522
|
return next;
|
|
522
523
|
}
|
|
523
|
-
|
|
524
|
-
export { CONFIG_PATH };
|
package/src/diff-render.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
// G8: surface agy's file edits as a diff in pi's thinking stream.
|
|
2
2
|
//
|
|
3
|
-
// agy edits with its OWN native
|
|
3
|
+
// agy edits with its OWN native full-file tools (write_to_file on legacy
|
|
4
|
+
// builds; write_file {Path, Content} on agy 1.5.11+, both parsed by
|
|
5
|
+
// parseEditToolInput's field-shape regexes, not by tool name), inside a
|
|
4
6
|
// closed tool loop that lands changes on disk. pi's native colored diff viewer
|
|
5
7
|
// is unreachable for agy turns (it is part of the tool-call lifecycle, and the
|
|
6
8
|
// provider emits no toolCall blocks by design), so we compute a line-numbered
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** Model-switch handoff toast (UI only): the message shown when the user
|
|
2
|
+
* lands on an antigravity bridge model that has no bound agy conversation,
|
|
3
|
+
* telling them pi-side context will re-deliver into the fresh conversation.
|
|
4
|
+
* Returns null when the toast must stay silent. */
|
|
5
|
+
|
|
6
|
+
const BRIDGE_API = "agy-bridge";
|
|
7
|
+
|
|
8
|
+
export function handoffToast(input: {
|
|
9
|
+
api: string | undefined;
|
|
10
|
+
digestEnabled: boolean;
|
|
11
|
+
bound: boolean;
|
|
12
|
+
hasContext: boolean;
|
|
13
|
+
source: "set" | "cycle" | "restore";
|
|
14
|
+
}): string | null {
|
|
15
|
+
if (input.source === "restore") return null;
|
|
16
|
+
if (input.api !== BRIDGE_API) return null;
|
|
17
|
+
if (!input.digestEnabled) return null;
|
|
18
|
+
if (input.bound) return null;
|
|
19
|
+
if (!input.hasContext) return null;
|
|
20
|
+
return "handoff context will ship to the new agy conversation";
|
|
21
|
+
}
|
package/src/mcp-registration.ts
CHANGED
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
import fs from "node:fs";
|
|
19
19
|
import os from "node:os";
|
|
20
20
|
import path from "node:path";
|
|
21
|
+
import { extensionsDataDir } from "./config.js";
|
|
21
22
|
|
|
22
23
|
/** Name convention for the bridge's per-pid server entries. */
|
|
23
24
|
export function bridgeServerName(pid: number): string {
|
|
@@ -174,18 +175,11 @@ export interface SuppressionMarker {
|
|
|
174
175
|
delegators: Record<string, { since: number }>;
|
|
175
176
|
}
|
|
176
177
|
|
|
177
|
-
/** Marker path,
|
|
178
|
-
*
|
|
179
|
-
* untouched. */
|
|
180
|
-
export function suppressionMarkerPath(
|
|
181
|
-
return path.join(
|
|
182
|
-
home,
|
|
183
|
-
".pi",
|
|
184
|
-
"extensions-data",
|
|
185
|
-
"estebanforge",
|
|
186
|
-
"pi-antigravity-bridge",
|
|
187
|
-
"suppression.json",
|
|
188
|
-
);
|
|
178
|
+
/** Marker path, under the bridge's extensions-data dir (src/config.ts
|
|
179
|
+
* extensionsDataDir). Lives OUTSIDE ~/.gemini so agy's watched config dir
|
|
180
|
+
* stays untouched. */
|
|
181
|
+
export function suppressionMarkerPath(agentDir?: string): string {
|
|
182
|
+
return path.join(extensionsDataDir(agentDir), "suppression.json");
|
|
189
183
|
}
|
|
190
184
|
|
|
191
185
|
/** A SIGKILLed pi can leave its marker entry behind, and pid reuse could
|
package/src/mcp-server.ts
CHANGED
|
@@ -20,7 +20,6 @@
|
|
|
20
20
|
|
|
21
21
|
import http from "node:http";
|
|
22
22
|
import fs from "node:fs";
|
|
23
|
-
import os from "node:os";
|
|
24
23
|
import path from "node:path";
|
|
25
24
|
import crypto from "node:crypto";
|
|
26
25
|
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
@@ -32,6 +31,7 @@ import {
|
|
|
32
31
|
SUPPORTED_PROTOCOL_VERSIONS,
|
|
33
32
|
} from "@modelcontextprotocol/sdk/types.js";
|
|
34
33
|
import { GATED_AGY_TOOL_SET } from "./approval-hook.js";
|
|
34
|
+
import { bridgeDir } from "./config.js";
|
|
35
35
|
|
|
36
36
|
/** Tools we do NOT expose to agy: it would just error (the provider is already
|
|
37
37
|
* antigravity, so the tool's own guard refuses; advertising it is noise). */
|
|
@@ -137,12 +137,10 @@ function clampProtocolVersionHeader(req: http.IncomingMessage): void {
|
|
|
137
137
|
}
|
|
138
138
|
}
|
|
139
139
|
|
|
140
|
-
const BRIDGE_BASE = path.join(os.homedir(), ".pi", "agent", "antigravity-bridge");
|
|
141
|
-
|
|
142
140
|
/** Per-process config dir. Each pi session owns its own file, so concurrent
|
|
143
141
|
* sessions never race on or cross-route through one shared config. */
|
|
144
142
|
export function bridgeMcpConfigDir(): string {
|
|
145
|
-
return path.join(
|
|
143
|
+
return path.join(bridgeDir(), `agy-mcp-${process.pid}`);
|
|
146
144
|
}
|
|
147
145
|
|
|
148
146
|
function bridgeMcpConfigPath(): string {
|
|
@@ -166,7 +164,7 @@ export function isPidAlive(pid: number): boolean {
|
|
|
166
164
|
}
|
|
167
165
|
|
|
168
166
|
/** Best-effort cleanup of stale per-pid dirs left by crashed sessions. */
|
|
169
|
-
export function sweepStaleBridgeDirs(baseDir: string =
|
|
167
|
+
export function sweepStaleBridgeDirs(baseDir: string = bridgeDir(), alive: (pid: number) => boolean = isPidAlive): void {
|
|
170
168
|
let entries: string[];
|
|
171
169
|
try {
|
|
172
170
|
entries = fs.readdirSync(baseDir);
|
package/src/models.ts
CHANGED
|
@@ -20,10 +20,11 @@
|
|
|
20
20
|
|
|
21
21
|
import { spawn } from "node:child_process";
|
|
22
22
|
import fs from "node:fs";
|
|
23
|
-
import os from "node:os";
|
|
24
23
|
import path from "node:path";
|
|
25
24
|
import type { Api, Model, ThinkingLevel, ThinkingLevelMap } from "@earendil-works/pi-ai";
|
|
26
25
|
|
|
26
|
+
import { bridgeDir } from "./config.js";
|
|
27
|
+
|
|
27
28
|
const DISCOVERY_TIMEOUT_MS = 8_000;
|
|
28
29
|
|
|
29
30
|
/** Stdout ceiling for `agy models`. Real output is a few KB of ASCII slugs;
|
|
@@ -192,20 +193,16 @@ export async function spawnAgyModelsRaw(binary: string): Promise<string> {
|
|
|
192
193
|
// --- catalog cache ----------------------------------------------------------
|
|
193
194
|
//
|
|
194
195
|
// `agy models` can take seconds (OAuth refresh, cold start) and its output
|
|
195
|
-
// rarely changes. We persist it to
|
|
196
|
+
// rarely changes. We persist it to <agentDir>/antigravity-bridge/models-
|
|
196
197
|
// cache.json with a short TTL so reloads serve instantly and only re-spawn in
|
|
197
198
|
// the background when stale. Only successful (non-empty) output is cached, so
|
|
198
199
|
// a broken agy is never sticky.
|
|
199
200
|
|
|
200
201
|
export const MODELS_CACHE_TTL_MS = 5 * 60_000;
|
|
201
202
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
"agent",
|
|
206
|
-
"antigravity-bridge",
|
|
207
|
-
"models-cache.json",
|
|
208
|
-
);
|
|
203
|
+
function modelsCachePath(): string {
|
|
204
|
+
return path.join(bridgeDir(), "models-cache.json");
|
|
205
|
+
}
|
|
209
206
|
|
|
210
207
|
interface ModelsCache {
|
|
211
208
|
raw: string;
|
|
@@ -213,7 +210,7 @@ interface ModelsCache {
|
|
|
213
210
|
}
|
|
214
211
|
|
|
215
212
|
/** Read and validate the models cache. Returns null when missing or corrupt. */
|
|
216
|
-
function readModelsCache(cachePath: string =
|
|
213
|
+
function readModelsCache(cachePath: string = modelsCachePath()): ModelsCache | null {
|
|
217
214
|
try {
|
|
218
215
|
const parsed = JSON.parse(fs.readFileSync(cachePath, "utf8")) as unknown;
|
|
219
216
|
if (
|
|
@@ -233,7 +230,7 @@ function readModelsCache(cachePath: string = MODELS_CACHE_PATH): ModelsCache | n
|
|
|
233
230
|
|
|
234
231
|
/** Atomically persist the raw catalog text (temp + rename, mode 0o600).
|
|
235
232
|
* Best-effort: a write failure just means the next load re-spawns. */
|
|
236
|
-
function writeModelsCache(raw: string, cachePath: string =
|
|
233
|
+
function writeModelsCache(raw: string, cachePath: string = modelsCachePath()): void {
|
|
237
234
|
fs.mkdirSync(path.dirname(cachePath), { recursive: true });
|
|
238
235
|
const tmp = `${cachePath}.${process.pid}.tmp`;
|
|
239
236
|
try {
|
|
@@ -254,7 +251,7 @@ function writeModelsCache(raw: string, cachePath: string = MODELS_CACHE_PATH): v
|
|
|
254
251
|
* the next load sees fresh data without this one having to wait on agy. */
|
|
255
252
|
export function refreshModelsInBackground(
|
|
256
253
|
binary: string,
|
|
257
|
-
cachePath: string =
|
|
254
|
+
cachePath: string = modelsCachePath(),
|
|
258
255
|
): void {
|
|
259
256
|
void spawnAgyModelsRaw(binary)
|
|
260
257
|
.then((raw) => {
|
|
@@ -277,7 +274,7 @@ export function refreshModelsInBackground(
|
|
|
277
274
|
* Gemini entries (agy missing/auth-failed). */
|
|
278
275
|
export async function loadModelCatalogRaw(
|
|
279
276
|
binary: string,
|
|
280
|
-
cachePath: string =
|
|
277
|
+
cachePath: string = modelsCachePath(),
|
|
281
278
|
): Promise<string> {
|
|
282
279
|
const cache = readModelsCache(cachePath);
|
|
283
280
|
if (cache) {
|
package/src/patch-cleanup.ts
CHANGED
|
@@ -41,9 +41,13 @@ import os from "node:os";
|
|
|
41
41
|
import { execSync } from "node:child_process";
|
|
42
42
|
import { fileURLToPath } from "node:url";
|
|
43
43
|
|
|
44
|
+
import { bridgeDir } from "./config.js";
|
|
45
|
+
|
|
44
46
|
const PACKAGE_NAME = "@earendil-works/pi-coding-agent";
|
|
45
|
-
|
|
46
|
-
export
|
|
47
|
+
|
|
48
|
+
export function patchBackupDir(): string {
|
|
49
|
+
return path.join(bridgeDir(), "pi-patch-backup");
|
|
50
|
+
}
|
|
47
51
|
|
|
48
52
|
/** One insertion site in one compiled file. */
|
|
49
53
|
interface PatchSite {
|
|
@@ -424,7 +428,7 @@ function resolveRoot(opts: { root?: string }): { root: string; version: string }
|
|
|
424
428
|
}
|
|
425
429
|
|
|
426
430
|
function backupBaseOf(opts: { backupBase?: string }): string {
|
|
427
|
-
return opts.backupBase ??
|
|
431
|
+
return opts.backupBase ?? patchBackupDir();
|
|
428
432
|
}
|
|
429
433
|
|
|
430
434
|
/** Report the current patch state without changing anything. */
|