pi-antigravity-bridge 0.1.4 → 0.1.5
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 +16 -0
- package/package.json +1 -1
- package/src/acp/binary-resolver.ts +46 -2
- package/src/acp/process-manager.ts +121 -26
- package/src/catalog/model-discovery.ts +115 -36
- package/src/index.ts +40 -25
- package/src/mcp/stdio-shim.ts +59 -3
- package/src/session/checkpoint-sync.ts +189 -0
- package/src/session/customization-mounter.ts +14 -0
- package/src/session/reconcile.ts +144 -61
- package/src/session/turn-coordinator.ts +215 -49
- package/src/stream/event-mapper.ts +51 -26
- package/src/tools/agy-mirror-tools.ts +16 -8
- package/src/ui/commands.ts +71 -32
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.1.5] - 2026-11-21
|
|
4
|
+
|
|
5
|
+
### Changed
|
|
6
|
+
|
|
7
|
+
- 升级单步与累计 Token、缓存命中率及输出速率统计口径,对齐上游模型真实单次生成数据并消除同轮多工具拆分造成的重复计费与命中率失真。
|
|
8
|
+
- 为历史重放补充字节预算保护,优先保留最近对话与末尾最新提问,避免长会话回退时尾部内容被截断。
|
|
9
|
+
- 在 `pi-tools` 模式下补充宿主 MCP 服务器索引上下文,并在 `processPerSession: false` 下支持工具列表动态变更推送。
|
|
10
|
+
|
|
11
|
+
### Fixed
|
|
12
|
+
|
|
13
|
+
- 修复安装上下文投影或图片压缩类扩展时会话摘要比对失配导致热缓存会话被误销毁的问题。
|
|
14
|
+
- 修复启动阶段并发模型刷新产生多余后台子进程及残留空会话数据库文件的问题。
|
|
15
|
+
- 修复按 `Esc` 中断轮次后仍写入会话锚点以及 `/agy sessions` 导入会话后恢复锚点未生效的问题。
|
|
16
|
+
- 修复 `agy-tools` 与 `hybrid` 模式下 MCP 工具暴露范围、`tool_search` 动态工具保留、`/agy bridge` 即时同步及原生工具分类准确性问题。
|
|
17
|
+
- 修复异常重建会话丢失规则上下文、分叉恢复失败残留临时数据库文件、Windows 包装脚本含空格路径参数转义及流式配额错误行首判定等边界问题。
|
|
18
|
+
|
|
3
19
|
## [0.1.4] - 2026-11-21
|
|
4
20
|
|
|
5
21
|
### Fixed
|
package/package.json
CHANGED
|
@@ -142,13 +142,57 @@ export function resolveAgyBinary(options: ResolveBinaryOptions = {}): ResolvedBi
|
|
|
142
142
|
return undefined;
|
|
143
143
|
}
|
|
144
144
|
|
|
145
|
+
export interface AgySpawnSpec {
|
|
146
|
+
command: string;
|
|
147
|
+
args: string[];
|
|
148
|
+
windowsVerbatimArguments?: boolean;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* 为 Windows `cmd.exe /d /s /c` 安全引用单个命令行参数(处理含空格路径与双引号,且无需设置 `shell: true`,彻底规避 Node 22+ DEP0190)。
|
|
153
|
+
*/
|
|
154
|
+
export function quoteWindowsCmdArg(arg: string): string {
|
|
155
|
+
if (arg.length === 0) return '""';
|
|
156
|
+
if (!/[\s"&|<>^()]/.test(arg)) return arg;
|
|
157
|
+
return `"${arg.replace(/"/g, '""')}"`;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* 构造跨平台安全的 `spawn` / `spawnSync` 调用参数:
|
|
162
|
+
* - 原生可执行文件(`.exe` 或 POSIX 二进制):直接传参数数组,`shell: false`;
|
|
163
|
+
* - Windows `.cmd` / `.bat` 包装脚本:通过 `cmd.exe /d /s /c "..."` + `windowsVerbatimArguments: true` 调用,完整保护 `binPath` 与 `--gemini-dir` 等含空格路径。
|
|
164
|
+
*/
|
|
165
|
+
export function buildAgySpawnSpec(
|
|
166
|
+
binary: ResolvedBinary,
|
|
167
|
+
args: readonly string[],
|
|
168
|
+
env: NodeJS.ProcessEnv = process.env,
|
|
169
|
+
): AgySpawnSpec {
|
|
170
|
+
if (!binary.isWindowsWrapperScript) {
|
|
171
|
+
return {
|
|
172
|
+
command: binary.binPath,
|
|
173
|
+
args: [...args],
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const comspec = env.ComSpec || env.COMSPEC || "cmd.exe";
|
|
178
|
+
const innerCommand = [quoteWindowsCmdArg(binary.binPath), ...args.map(quoteWindowsCmdArg)].join(
|
|
179
|
+
" ",
|
|
180
|
+
);
|
|
181
|
+
return {
|
|
182
|
+
command: comspec,
|
|
183
|
+
args: ["/d", "/s", "/c", `"${innerCommand}"`],
|
|
184
|
+
windowsVerbatimArguments: true,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
145
188
|
/** 读取本机 `agy --version` 版本字符串 */
|
|
146
189
|
export function probeAgyVersion(binary: ResolvedBinary, timeoutMs = 4_000): string | undefined {
|
|
147
190
|
try {
|
|
148
|
-
const
|
|
191
|
+
const spec = buildAgySpawnSpec(binary, ["--version"]);
|
|
192
|
+
const result = spawnSync(spec.command, spec.args, {
|
|
149
193
|
encoding: "utf8",
|
|
150
194
|
timeout: timeoutMs,
|
|
151
|
-
|
|
195
|
+
windowsVerbatimArguments: spec.windowsVerbatimArguments,
|
|
152
196
|
windowsHide: true,
|
|
153
197
|
});
|
|
154
198
|
if (result.status === 0 && result.stdout) {
|
|
@@ -6,7 +6,7 @@ import { StringDecoder } from "node:string_decoder";
|
|
|
6
6
|
import { fileURLToPath } from "node:url";
|
|
7
7
|
import type { BridgeConfig } from "../config.ts";
|
|
8
8
|
import { resolveEffectiveFlags } from "../config.ts";
|
|
9
|
-
import { type ResolvedBinary, resolveAgyBinary } from "./binary-resolver.ts";
|
|
9
|
+
import { buildAgySpawnSpec, type ResolvedBinary, resolveAgyBinary } from "./binary-resolver.ts";
|
|
10
10
|
import {
|
|
11
11
|
type AcpElicitationCreateParams,
|
|
12
12
|
type AcpElicitationCreateResult,
|
|
@@ -23,7 +23,7 @@ import {
|
|
|
23
23
|
|
|
24
24
|
export const BRIDGE_AGENT_NAME = "pi-bridge";
|
|
25
25
|
export const CLIENT_INFO_NAME = "pi-antigravity-bridge";
|
|
26
|
-
export const CLIENT_INFO_VERSION = "0.1.
|
|
26
|
+
export const CLIENT_INFO_VERSION = "0.1.5";
|
|
27
27
|
|
|
28
28
|
export class AcpRpcError extends Error {
|
|
29
29
|
readonly code: number;
|
|
@@ -251,6 +251,52 @@ function appendNoProxyLoopback(env: NodeJS.ProcessEnv): NodeJS.ProcessEnv {
|
|
|
251
251
|
return result;
|
|
252
252
|
}
|
|
253
253
|
|
|
254
|
+
function racePromiseWithSignal<T>(promise: Promise<T>, signal?: AbortSignal): Promise<T> {
|
|
255
|
+
if (!signal) return promise;
|
|
256
|
+
if (signal.aborted) {
|
|
257
|
+
return Promise.reject(signal.reason ?? new Error("请求已取消"));
|
|
258
|
+
}
|
|
259
|
+
return new Promise<T>((resolve, reject) => {
|
|
260
|
+
const onAbort = () => {
|
|
261
|
+
reject(signal.reason ?? new Error("请求已取消"));
|
|
262
|
+
};
|
|
263
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
264
|
+
promise.then(
|
|
265
|
+
(val) => {
|
|
266
|
+
signal.removeEventListener("abort", onAbort);
|
|
267
|
+
resolve(val);
|
|
268
|
+
},
|
|
269
|
+
(err) => {
|
|
270
|
+
signal.removeEventListener("abort", onAbort);
|
|
271
|
+
reject(err);
|
|
272
|
+
},
|
|
273
|
+
);
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
function scheduleOrphanedSessionDbCleanup(
|
|
278
|
+
geminiDir: string,
|
|
279
|
+
appDataDirName: string,
|
|
280
|
+
sessionId: string,
|
|
281
|
+
): void {
|
|
282
|
+
const convDir = path.join(geminiDir, appDataDirName || "antigravity-cli", "conversations");
|
|
283
|
+
const tryUnlink = () => {
|
|
284
|
+
for (const suffix of [".db", ".db-wal", ".db-shm", ".db-journal"]) {
|
|
285
|
+
try {
|
|
286
|
+
const target = path.join(convDir, `${sessionId}${suffix}`);
|
|
287
|
+
if (fs.existsSync(target)) {
|
|
288
|
+
fs.unlinkSync(target);
|
|
289
|
+
}
|
|
290
|
+
} catch {
|
|
291
|
+
// Windows 下 worker 退出前可能短暂持有文件句柄,由后续延时重试兜底
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
tryUnlink();
|
|
296
|
+
setTimeout(tryUnlink, 700).unref();
|
|
297
|
+
setTimeout(tryUnlink, 1_800).unref();
|
|
298
|
+
}
|
|
299
|
+
|
|
254
300
|
/** 封装单个已握手 `agy acp` 子进程连接 */
|
|
255
301
|
export class AcpClientConnection {
|
|
256
302
|
readonly binary: ResolvedBinary;
|
|
@@ -258,6 +304,8 @@ export class AcpClientConnection {
|
|
|
258
304
|
private readonly proc: ChildProcessWithoutNullStreams;
|
|
259
305
|
private readonly managedAgentDir?: string;
|
|
260
306
|
private readonly pending = new Map<JsonRpcId, PendingRpc>();
|
|
307
|
+
/** 记录调用方提前 abort 或超时的 `session/new` / `session/resume` 请求,在后端迟到回包时立即关闭孤儿 worker */
|
|
308
|
+
private readonly orphanedSessionOpenRpcs = new Map<JsonRpcId, string>();
|
|
261
309
|
private readonly activeSessions = new Set<string>();
|
|
262
310
|
private readonly stderrTail: string[] = [];
|
|
263
311
|
private nextRpcId = 1;
|
|
@@ -300,15 +348,12 @@ export class AcpClientConnection {
|
|
|
300
348
|
args.push("--app-data-dir", profile.appDataDirName);
|
|
301
349
|
}
|
|
302
350
|
|
|
303
|
-
const
|
|
304
|
-
binary.isWindowsWrapperScript && binary.binPath.includes(" ")
|
|
305
|
-
? `"${binary.binPath}"`
|
|
306
|
-
: binary.binPath;
|
|
351
|
+
const spawnSpec = buildAgySpawnSpec(binary, args, env);
|
|
307
352
|
|
|
308
|
-
this.proc = spawn(
|
|
353
|
+
this.proc = spawn(spawnSpec.command, spawnSpec.args, {
|
|
309
354
|
env: appendNoProxyLoopback(env),
|
|
310
355
|
stdio: ["pipe", "pipe", "pipe"],
|
|
311
|
-
|
|
356
|
+
windowsVerbatimArguments: spawnSpec.windowsVerbatimArguments,
|
|
312
357
|
detached: process.platform !== "win32",
|
|
313
358
|
windowsHide: true,
|
|
314
359
|
});
|
|
@@ -383,6 +428,9 @@ export class AcpClientConnection {
|
|
|
383
428
|
if (this.initResult) {
|
|
384
429
|
return this.initResult;
|
|
385
430
|
}
|
|
431
|
+
if (signal?.aborted) {
|
|
432
|
+
throw signal.reason ?? new Error("请求已取消");
|
|
433
|
+
}
|
|
386
434
|
if (!this.initPromise) {
|
|
387
435
|
this.initPromise = this.request<AcpInitializeResult>(
|
|
388
436
|
"initialize",
|
|
@@ -396,13 +444,18 @@ export class AcpClientConnection {
|
|
|
396
444
|
version: CLIENT_INFO_VERSION,
|
|
397
445
|
},
|
|
398
446
|
},
|
|
399
|
-
{ timeoutMs: 20_000
|
|
400
|
-
)
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
447
|
+
{ timeoutMs: 20_000 },
|
|
448
|
+
)
|
|
449
|
+
.then((res) => {
|
|
450
|
+
this.initResult = res;
|
|
451
|
+
return res;
|
|
452
|
+
})
|
|
453
|
+
.catch((err) => {
|
|
454
|
+
this.initPromise = undefined;
|
|
455
|
+
throw err;
|
|
456
|
+
});
|
|
404
457
|
}
|
|
405
|
-
return this.initPromise;
|
|
458
|
+
return racePromiseWithSignal(this.initPromise, signal);
|
|
406
459
|
}
|
|
407
460
|
|
|
408
461
|
request<TResult = unknown>(
|
|
@@ -418,6 +471,8 @@ export class AcpClientConnection {
|
|
|
418
471
|
}
|
|
419
472
|
|
|
420
473
|
const id = this.nextRpcId++;
|
|
474
|
+
const tracksSessionOpen = method === "session/new" || method === "session/resume";
|
|
475
|
+
|
|
421
476
|
return new Promise<TResult>((resolve, reject) => {
|
|
422
477
|
const entry: PendingRpc = {
|
|
423
478
|
method,
|
|
@@ -428,6 +483,9 @@ export class AcpClientConnection {
|
|
|
428
483
|
if (options.timeoutMs && options.timeoutMs > 0) {
|
|
429
484
|
entry.timer = setTimeout(() => {
|
|
430
485
|
this.pending.delete(id);
|
|
486
|
+
if (tracksSessionOpen) {
|
|
487
|
+
this.orphanedSessionOpenRpcs.set(id, method);
|
|
488
|
+
}
|
|
431
489
|
entry.cleanupAbort?.();
|
|
432
490
|
reject(new Error(`ACP 请求 ${method} 超时 (${options.timeoutMs}ms)`));
|
|
433
491
|
}, options.timeoutMs);
|
|
@@ -437,6 +495,9 @@ export class AcpClientConnection {
|
|
|
437
495
|
const onAbort = () => {
|
|
438
496
|
if (!this.pending.has(id)) return;
|
|
439
497
|
this.pending.delete(id);
|
|
498
|
+
if (tracksSessionOpen) {
|
|
499
|
+
this.orphanedSessionOpenRpcs.set(id, method);
|
|
500
|
+
}
|
|
440
501
|
if (entry.timer) clearTimeout(entry.timer);
|
|
441
502
|
reject(options.signal?.reason ?? new Error(`ACP 请求 ${method} 已中止`));
|
|
442
503
|
};
|
|
@@ -580,7 +641,28 @@ export class AcpClientConnection {
|
|
|
580
641
|
if ("id" in msg && ("result" in msg || "error" in msg) && !("method" in msg)) {
|
|
581
642
|
if (msg.id === null) return;
|
|
582
643
|
const entry = this.pending.get(msg.id);
|
|
583
|
-
if (!entry)
|
|
644
|
+
if (!entry) {
|
|
645
|
+
// 回收调用方已被 abort/timeout 放弃但后端仍创建成功的孤儿会话,防止泄漏 agy acp --worker 子进程与空 .db
|
|
646
|
+
const orphanedMethod = this.orphanedSessionOpenRpcs.get(msg.id);
|
|
647
|
+
if (orphanedMethod) {
|
|
648
|
+
this.orphanedSessionOpenRpcs.delete(msg.id);
|
|
649
|
+
if ("result" in msg && msg.result && typeof msg.result === "object") {
|
|
650
|
+
const sid = (msg.result as { sessionId?: unknown }).sessionId;
|
|
651
|
+
if (typeof sid === "string" && sid.length > 0) {
|
|
652
|
+
void this.closeSessionSafe(sid).finally(() => {
|
|
653
|
+
if (orphanedMethod === "session/new") {
|
|
654
|
+
scheduleOrphanedSessionDbCleanup(
|
|
655
|
+
this.profile.geminiDir,
|
|
656
|
+
this.profile.appDataDirName,
|
|
657
|
+
sid,
|
|
658
|
+
);
|
|
659
|
+
}
|
|
660
|
+
});
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
return;
|
|
665
|
+
}
|
|
584
666
|
this.pending.delete(msg.id);
|
|
585
667
|
if (entry.timer) clearTimeout(entry.timer);
|
|
586
668
|
entry.cleanupAbort?.();
|
|
@@ -707,6 +789,7 @@ export function resolveProcessProfileForConfig(
|
|
|
707
789
|
/** 管理不同 Profile 的常驻 `agy acp` 进程池与统一释放 */
|
|
708
790
|
export class AcpProcessManager {
|
|
709
791
|
private readonly pool = new Map<string, AcpClientConnection>();
|
|
792
|
+
private readonly initializing = new Map<string, Promise<AcpClientConnection>>();
|
|
710
793
|
private sharedHandlers: AcpServerHandlers = {};
|
|
711
794
|
|
|
712
795
|
setHandlers(handlers: AcpServerHandlers): void {
|
|
@@ -734,6 +817,10 @@ export class AcpProcessManager {
|
|
|
734
817
|
profileOverride?: Partial<AcpProcessProfile>,
|
|
735
818
|
signal?: AbortSignal,
|
|
736
819
|
): Promise<AcpClientConnection> {
|
|
820
|
+
if (signal?.aborted) {
|
|
821
|
+
throw signal.reason ?? new Error("请求已取消");
|
|
822
|
+
}
|
|
823
|
+
|
|
737
824
|
const baseProfile = resolveProcessProfileForConfig(config);
|
|
738
825
|
const profile: AcpProcessProfile = { ...baseProfile, ...profileOverride };
|
|
739
826
|
const key = buildProfileKey(profile);
|
|
@@ -744,18 +831,26 @@ export class AcpProcessManager {
|
|
|
744
831
|
return existing;
|
|
745
832
|
}
|
|
746
833
|
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
834
|
+
let initTask = this.initializing.get(key);
|
|
835
|
+
if (!initTask) {
|
|
836
|
+
initTask = (async () => {
|
|
837
|
+
const binary = this.resolveBinary(config);
|
|
838
|
+
const conn = new AcpClientConnection(binary, profile, this.sharedHandlers);
|
|
839
|
+
try {
|
|
840
|
+
await conn.ensureInitialized();
|
|
841
|
+
this.pool.set(key, conn);
|
|
842
|
+
return conn;
|
|
843
|
+
} catch (error) {
|
|
844
|
+
await conn.dispose();
|
|
845
|
+
throw error;
|
|
846
|
+
} finally {
|
|
847
|
+
this.initializing.delete(key);
|
|
848
|
+
}
|
|
849
|
+
})();
|
|
850
|
+
this.initializing.set(key, initTask);
|
|
758
851
|
}
|
|
852
|
+
|
|
853
|
+
return racePromiseWithSignal(initTask, signal);
|
|
759
854
|
}
|
|
760
855
|
|
|
761
856
|
/** 从连接池驱逐并强制销毁已卡死或状态异常的单个子进程连接 */
|
|
@@ -316,60 +316,139 @@ export function writeCachedCatalog(
|
|
|
316
316
|
}
|
|
317
317
|
}
|
|
318
318
|
|
|
319
|
+
const CATALOG_FRESH_TTL_MS = 15_000;
|
|
320
|
+
let inFlightCatalogProbe: Promise<DiscoveredCatalogSnapshot> | undefined;
|
|
321
|
+
let lastLiveProbeSnapshot: DiscoveredCatalogSnapshot | undefined;
|
|
322
|
+
let lastLiveProbeAt = 0;
|
|
323
|
+
|
|
324
|
+
function raceCatalogProbeWithSignal<T>(promise: Promise<T>, signal?: AbortSignal): Promise<T> {
|
|
325
|
+
if (!signal) return promise;
|
|
326
|
+
if (signal.aborted) {
|
|
327
|
+
return Promise.reject(signal.reason ?? new Error("模型探测已取消"));
|
|
328
|
+
}
|
|
329
|
+
return new Promise<T>((resolve, reject) => {
|
|
330
|
+
const onAbort = () => {
|
|
331
|
+
reject(signal.reason ?? new Error("模型探测已取消"));
|
|
332
|
+
};
|
|
333
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
334
|
+
promise.then(
|
|
335
|
+
(v) => {
|
|
336
|
+
signal.removeEventListener("abort", onAbort);
|
|
337
|
+
resolve(v);
|
|
338
|
+
},
|
|
339
|
+
(err) => {
|
|
340
|
+
signal.removeEventListener("abort", onAbort);
|
|
341
|
+
reject(err);
|
|
342
|
+
},
|
|
343
|
+
);
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* 清理指定会话(如零消息探针或分叉失败副本)的 SQLite 数据库文件。
|
|
349
|
+
* 支持延时异步重试,兼容 Windows 下 `agy acp --worker` 退出前短暂持有文件句柄的场景。
|
|
350
|
+
*/
|
|
319
351
|
export function removeEmptyProbeSessionFiles(config: BridgeConfig, sessionId: string): void {
|
|
352
|
+
if (!sessionId) return;
|
|
320
353
|
const convDir = resolveAgyConversationsDir(config);
|
|
321
|
-
|
|
322
|
-
|
|
354
|
+
const tryRemove = (): boolean => {
|
|
355
|
+
let allGone = true;
|
|
356
|
+
for (const suffix of [".db", ".db-wal", ".db-shm", ".db-journal"]) {
|
|
323
357
|
const target = path.join(convDir, `${sessionId}${suffix}`);
|
|
324
|
-
|
|
325
|
-
fs.
|
|
358
|
+
try {
|
|
359
|
+
if (fs.existsSync(target)) {
|
|
360
|
+
fs.unlinkSync(target);
|
|
361
|
+
}
|
|
362
|
+
} catch {
|
|
363
|
+
allGone = false;
|
|
326
364
|
}
|
|
327
|
-
} catch {
|
|
328
|
-
// 忽略文件清理异常
|
|
329
365
|
}
|
|
366
|
+
return allGone;
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
if (!tryRemove()) {
|
|
370
|
+
setTimeout(() => {
|
|
371
|
+
if (!tryRemove()) {
|
|
372
|
+
setTimeout(() => {
|
|
373
|
+
tryRemove();
|
|
374
|
+
}, 1_200).unref();
|
|
375
|
+
}
|
|
376
|
+
}, 600).unref();
|
|
330
377
|
}
|
|
331
378
|
}
|
|
332
379
|
|
|
380
|
+
export interface ProbeCatalogOptions {
|
|
381
|
+
signal?: AbortSignal;
|
|
382
|
+
force?: boolean;
|
|
383
|
+
}
|
|
384
|
+
|
|
333
385
|
/**
|
|
334
386
|
* 在线零配额探测当前账号可用的 AGY 模型与订阅层级。
|
|
335
|
-
*
|
|
387
|
+
* 内置 Singleflight 并发合并与短时新鲜度缓存,彻底杜绝并发刷新产生多个孤儿 worker 与空数据库文件。
|
|
336
388
|
*/
|
|
337
389
|
export async function probeLiveAntigravityCatalog(
|
|
338
390
|
processManager: AcpProcessManager,
|
|
339
391
|
config: BridgeConfig,
|
|
340
392
|
cwd: string,
|
|
341
|
-
|
|
393
|
+
signalOrOptions?: AbortSignal | ProbeCatalogOptions,
|
|
342
394
|
): Promise<DiscoveredCatalogSnapshot> {
|
|
343
|
-
const
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
cwd,
|
|
348
|
-
mcpServers: [],
|
|
349
|
-
},
|
|
350
|
-
{ timeoutMs: 20_000, signal },
|
|
351
|
-
);
|
|
395
|
+
const opts: ProbeCatalogOptions =
|
|
396
|
+
signalOrOptions && "aborted" in signalOrOptions
|
|
397
|
+
? { signal: signalOrOptions }
|
|
398
|
+
: (signalOrOptions ?? {});
|
|
352
399
|
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
modelEfforts,
|
|
364
|
-
};
|
|
365
|
-
writeCachedCatalog(snapshot);
|
|
366
|
-
return snapshot;
|
|
367
|
-
} finally {
|
|
368
|
-
if (sessionRes.sessionId) {
|
|
369
|
-
await conn.closeSessionSafe(sessionRes.sessionId);
|
|
370
|
-
removeEmptyProbeSessionFiles(config, sessionRes.sessionId);
|
|
371
|
-
}
|
|
400
|
+
if (opts.signal?.aborted) {
|
|
401
|
+
throw opts.signal.reason ?? new Error("模型探测已取消");
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
if (
|
|
405
|
+
!opts.force &&
|
|
406
|
+
lastLiveProbeSnapshot &&
|
|
407
|
+
Date.now() - lastLiveProbeAt < CATALOG_FRESH_TTL_MS
|
|
408
|
+
) {
|
|
409
|
+
return lastLiveProbeSnapshot;
|
|
372
410
|
}
|
|
411
|
+
|
|
412
|
+
if (!inFlightCatalogProbe) {
|
|
413
|
+
inFlightCatalogProbe = (async () => {
|
|
414
|
+
const conn = await processManager.acquire(config, undefined);
|
|
415
|
+
const sessionRes = await conn.request<AcpSessionNewResult>(
|
|
416
|
+
"session/new",
|
|
417
|
+
{
|
|
418
|
+
cwd,
|
|
419
|
+
mcpServers: [],
|
|
420
|
+
},
|
|
421
|
+
{ timeoutMs: 20_000 },
|
|
422
|
+
);
|
|
423
|
+
|
|
424
|
+
try {
|
|
425
|
+
const { models, modelEfforts } = aggregateAcpModels(
|
|
426
|
+
sessionRes.models?.availableModels ?? [],
|
|
427
|
+
sessionRes.configOptions ?? [],
|
|
428
|
+
);
|
|
429
|
+
const snapshot: DiscoveredCatalogSnapshot = {
|
|
430
|
+
version: 1,
|
|
431
|
+
updatedAt: Date.now(),
|
|
432
|
+
tier: sessionRes._meta?.antigravity?.tier,
|
|
433
|
+
models: models.length > 0 ? models : getSeedModels(),
|
|
434
|
+
modelEfforts,
|
|
435
|
+
};
|
|
436
|
+
writeCachedCatalog(snapshot);
|
|
437
|
+
lastLiveProbeSnapshot = snapshot;
|
|
438
|
+
lastLiveProbeAt = Date.now();
|
|
439
|
+
return snapshot;
|
|
440
|
+
} finally {
|
|
441
|
+
if (sessionRes.sessionId) {
|
|
442
|
+
await conn.closeSessionSafe(sessionRes.sessionId);
|
|
443
|
+
removeEmptyProbeSessionFiles(config, sessionRes.sessionId);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
})().finally(() => {
|
|
447
|
+
inFlightCatalogProbe = undefined;
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
return raceCatalogProbeWithSignal(inFlightCatalogProbe, opts.signal);
|
|
373
452
|
}
|
|
374
453
|
|
|
375
454
|
export interface ResolvedModelSelection {
|
package/src/index.ts
CHANGED
|
@@ -13,7 +13,7 @@ import type {
|
|
|
13
13
|
ProviderConfig,
|
|
14
14
|
TurnEndEvent,
|
|
15
15
|
} from "@earendil-works/pi-coding-agent";
|
|
16
|
-
import { resolveAgyBinary } from "./acp/binary-resolver.ts";
|
|
16
|
+
import { buildAgySpawnSpec, resolveAgyBinary } from "./acp/binary-resolver.ts";
|
|
17
17
|
import { AcpProcessManager } from "./acp/process-manager.ts";
|
|
18
18
|
import {
|
|
19
19
|
ANTIGRAVITY_API_ID,
|
|
@@ -109,11 +109,21 @@ export default function antigravityBridgeExtension(pi: ExtensionAPI): void {
|
|
|
109
109
|
models,
|
|
110
110
|
streamSimple: (model, context, options) => coordinator.streamSimple(model, context, options),
|
|
111
111
|
async refreshModels(refreshCtx) {
|
|
112
|
+
if (!refreshCtx.allowNetwork) {
|
|
113
|
+
const disk = readCachedCatalog();
|
|
114
|
+
if (disk?.tier) {
|
|
115
|
+
coordinator.setLatestTier(disk.tier);
|
|
116
|
+
}
|
|
117
|
+
if (disk?.models && disk.models.length > 0) {
|
|
118
|
+
currentModels = disk.models;
|
|
119
|
+
}
|
|
120
|
+
return currentModels;
|
|
121
|
+
}
|
|
112
122
|
const snapshot = await probeLiveAntigravityCatalog(
|
|
113
123
|
processManager,
|
|
114
124
|
coordinator.config,
|
|
115
125
|
process.cwd(),
|
|
116
|
-
refreshCtx.signal,
|
|
126
|
+
{ signal: refreshCtx.signal, force: refreshCtx.force },
|
|
117
127
|
);
|
|
118
128
|
if (snapshot.tier) {
|
|
119
129
|
coordinator.setLatestTier(snapshot.tier);
|
|
@@ -164,18 +174,16 @@ export default function antigravityBridgeExtension(pi: ExtensionAPI): void {
|
|
|
164
174
|
);
|
|
165
175
|
}
|
|
166
176
|
|
|
167
|
-
const
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
},
|
|
178
|
-
);
|
|
177
|
+
const loginSpec = buildAgySpawnSpec(bin, [
|
|
178
|
+
"acp",
|
|
179
|
+
"--login",
|
|
180
|
+
"--gemini-dir",
|
|
181
|
+
coordinator.config.geminiDir,
|
|
182
|
+
]);
|
|
183
|
+
const res = spawnSync(loginSpec.command, loginSpec.args, {
|
|
184
|
+
stdio: "inherit",
|
|
185
|
+
windowsVerbatimArguments: loginSpec.windowsVerbatimArguments,
|
|
186
|
+
});
|
|
179
187
|
if (res.status !== 0) {
|
|
180
188
|
throw new Error("官方 agy 登录未完成,可在独立终端运行 `agy acp --login` 后重试。");
|
|
181
189
|
}
|
|
@@ -217,7 +225,7 @@ export default function antigravityBridgeExtension(pi: ExtensionAPI): void {
|
|
|
217
225
|
processManager,
|
|
218
226
|
coordinator.config,
|
|
219
227
|
ctx.cwd,
|
|
220
|
-
ctx.signal,
|
|
228
|
+
{ signal: ctx.signal, force: true },
|
|
221
229
|
);
|
|
222
230
|
if (snapshot.tier) {
|
|
223
231
|
coordinator.setLatestTier(snapshot.tier);
|
|
@@ -260,7 +268,7 @@ export default function antigravityBridgeExtension(pi: ExtensionAPI): void {
|
|
|
260
268
|
coordinator.setActiveContext(ctx);
|
|
261
269
|
toolActivation.syncForContext(ctx, currentConfig);
|
|
262
270
|
|
|
263
|
-
//
|
|
271
|
+
// 若首次加载尚无本地缓存快照,复用内置 Singleflight 静默探测一次模型目录
|
|
264
272
|
if (!cachedCatalog) {
|
|
265
273
|
void refreshAndReRegisterModels(ctx).catch(() => {});
|
|
266
274
|
}
|
|
@@ -279,10 +287,11 @@ export default function antigravityBridgeExtension(pi: ExtensionAPI): void {
|
|
|
279
287
|
|
|
280
288
|
// 6. ACP 单会话单活跃轮次协议约束:运行中按 Enter 发送的新消息统一转入 Follow-up 排队队列,
|
|
281
289
|
// 保持原任务完整执行完毕后再自动开启下一轮真实用户请求(同时支持 Alt+Up 编辑队列与 Esc 撤回)。
|
|
282
|
-
pi.on("input",
|
|
290
|
+
pi.on("input", (event, ctx) => {
|
|
283
291
|
if (
|
|
284
292
|
ctx.model?.provider === ANTIGRAVITY_PROVIDER_ID &&
|
|
285
293
|
event.streamingBehavior === "steer" &&
|
|
294
|
+
event.source !== "extension" &&
|
|
286
295
|
!ctx.isIdle()
|
|
287
296
|
) {
|
|
288
297
|
const parts: Array<TextContent | ImageContent> = [];
|
|
@@ -292,12 +301,10 @@ export default function antigravityBridgeExtension(pi: ExtensionAPI): void {
|
|
|
292
301
|
if (event.images && event.images.length > 0) {
|
|
293
302
|
parts.push(...event.images);
|
|
294
303
|
}
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
}),
|
|
300
|
-
);
|
|
304
|
+
pi.sendUserMessage(parts.length > 0 ? parts : event.text, {
|
|
305
|
+
deliverAs: "followUp",
|
|
306
|
+
expandPromptTemplates: event.source === "interactive",
|
|
307
|
+
});
|
|
301
308
|
return { action: "handled" };
|
|
302
309
|
}
|
|
303
310
|
return { action: "continue" };
|
|
@@ -310,8 +317,16 @@ export default function antigravityBridgeExtension(pi: ExtensionAPI): void {
|
|
|
310
317
|
updateAntigravityStatusBar(ctx, coordinator.config, coordinator.getTelemetrySnapshot());
|
|
311
318
|
return;
|
|
312
319
|
}
|
|
313
|
-
if (
|
|
314
|
-
|
|
320
|
+
if (
|
|
321
|
+
stopReason === "aborted" ||
|
|
322
|
+
stopReason === "error" ||
|
|
323
|
+
event.outcome !== "completed"
|
|
324
|
+
) {
|
|
325
|
+
if (stopReason === "aborted" || event.outcome === "aborted") {
|
|
326
|
+
coordinator.cancelActiveTurnForPiSession(ctx.sessionManager.getSessionId());
|
|
327
|
+
}
|
|
328
|
+
updateAntigravityStatusBar(ctx, coordinator.config, coordinator.getTelemetrySnapshot());
|
|
329
|
+
return;
|
|
315
330
|
}
|
|
316
331
|
const visible = extractVisibleMessages(event.context.llmMessages);
|
|
317
332
|
await coordinator.persistTurnAnchor(visible, ctx.sessionManager.getSessionId());
|