pi-antigravity-bridge 0.1.9 → 0.1.10
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 +7 -0
- package/package.json +1 -1
- package/src/acp/process-manager.ts +1 -1
- package/src/session/checkpoint-sync.ts +261 -4
- package/src/session/turn-coordinator.ts +122 -56
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.1.10] - 2026-11-22
|
|
4
|
+
|
|
5
|
+
### Fixed
|
|
6
|
+
|
|
7
|
+
- 修复 `pi-tools` 模式下触发超长历史重建或辅助请求时,新建会话未保留禁用内置工具配置而意外启用 Antigravity 原生工具与内置工作流的问题。
|
|
8
|
+
- 修复从 `0.1.9` 受影响的旧会话恢复时继续沿用原生工具配置的问题,在下次对话时自动重建并恢复纯净 `pi-tools` 模式。
|
|
9
|
+
|
|
3
10
|
## [0.1.9] - 2026-11-22
|
|
4
11
|
|
|
5
12
|
### Changed
|
package/package.json
CHANGED
|
@@ -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.10";
|
|
27
27
|
|
|
28
28
|
export class AcpRpcError extends Error {
|
|
29
29
|
readonly code: number;
|
|
@@ -666,9 +666,14 @@ export async function forkSqliteConversationAtCheckpoint(
|
|
|
666
666
|
}
|
|
667
667
|
}
|
|
668
668
|
|
|
669
|
+
let origTrajectoryId: string | undefined;
|
|
669
670
|
let dstDb: import("node:sqlite").DatabaseSync | undefined;
|
|
670
671
|
try {
|
|
671
672
|
dstDb = openSqliteWithTimeout(sqlite, targetDbPath, false);
|
|
673
|
+
const origMeta = dstDb
|
|
674
|
+
.prepare("SELECT trajectory_id FROM trajectory_meta LIMIT 1")
|
|
675
|
+
.get() as { trajectory_id?: string } | undefined;
|
|
676
|
+
origTrajectoryId = origMeta?.trajectory_id;
|
|
672
677
|
dstDb.exec("BEGIN IMMEDIATE");
|
|
673
678
|
dstDb.prepare("DELETE FROM steps WHERE idx >= ?").run(checkpoint.steps);
|
|
674
679
|
dstDb.prepare("DELETE FROM parent_references WHERE idx >= ?").run(checkpoint.steps);
|
|
@@ -683,6 +688,7 @@ export async function forkSqliteConversationAtCheckpoint(
|
|
|
683
688
|
dstDb
|
|
684
689
|
.prepare("UPDATE trajectory_meta SET trajectory_id = ?, cascade_id = ?")
|
|
685
690
|
.run(forkedSessionId, forkedSessionId);
|
|
691
|
+
rewriteMetadataBlobUuids(dstDb, [sourceSessionId, origTrajectoryId], forkedSessionId);
|
|
686
692
|
dstDb.exec("COMMIT");
|
|
687
693
|
return forkedSessionId;
|
|
688
694
|
} catch {
|
|
@@ -762,11 +768,262 @@ export function encodePlannerResponseStepPayload(text: string): Buffer {
|
|
|
762
768
|
}
|
|
763
769
|
|
|
764
770
|
/**
|
|
765
|
-
*
|
|
766
|
-
*
|
|
767
|
-
|
|
771
|
+
* 校验已有本地会话库是否携带有效的 `trajectory_metadata_blob`(以及 `pi-tools` 模式下必需的 `pi-bridge` 标识)。
|
|
772
|
+
* 用于在恢复或分叉历史锚点前自动识别并隔离旧版无元数据裸库,避免 `session/resume` 回退启用 Antigravity 原生内置工具。
|
|
773
|
+
*/
|
|
774
|
+
export async function hasValidAgentMetadataBlob(
|
|
775
|
+
config: BridgeConfig,
|
|
776
|
+
agySessionId: string,
|
|
777
|
+
requiredAgentSubstring?: string,
|
|
778
|
+
): Promise<boolean> {
|
|
779
|
+
if (!agySessionId) return true;
|
|
780
|
+
const dbPath = path.join(resolveAgyConversationsDir(config), `${agySessionId}.db`);
|
|
781
|
+
if (!fs.existsSync(dbPath)) {
|
|
782
|
+
return true;
|
|
783
|
+
}
|
|
784
|
+
const sqlite = await loadNodeSqlite();
|
|
785
|
+
if (!sqlite) return true;
|
|
786
|
+
|
|
787
|
+
let db: import("node:sqlite").DatabaseSync | undefined;
|
|
788
|
+
try {
|
|
789
|
+
db = openSqliteWithTimeout(sqlite, dbPath, true, 500);
|
|
790
|
+
const hasBlobTable = db
|
|
791
|
+
.prepare(
|
|
792
|
+
"SELECT 1 AS ok FROM sqlite_master WHERE type = 'table' AND name = 'trajectory_metadata_blob'",
|
|
793
|
+
)
|
|
794
|
+
.get();
|
|
795
|
+
if (!hasBlobTable) {
|
|
796
|
+
return true;
|
|
797
|
+
}
|
|
798
|
+
const rows = db
|
|
799
|
+
.prepare("SELECT data FROM trajectory_metadata_blob WHERE data IS NOT NULL AND length(data) > 0")
|
|
800
|
+
.all() as Array<{ data: Uint8Array }>;
|
|
801
|
+
if (rows.length === 0) {
|
|
802
|
+
return false;
|
|
803
|
+
}
|
|
804
|
+
if (requiredAgentSubstring) {
|
|
805
|
+
return rows.some((row) =>
|
|
806
|
+
Buffer.from(row.data).toString("utf8").includes(requiredAgentSubstring),
|
|
807
|
+
);
|
|
808
|
+
}
|
|
809
|
+
return true;
|
|
810
|
+
} catch {
|
|
811
|
+
return true;
|
|
812
|
+
} finally {
|
|
813
|
+
try {
|
|
814
|
+
db?.close();
|
|
815
|
+
} catch {
|
|
816
|
+
// 忽略
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
/**
|
|
822
|
+
* 在克隆出的 `.db` 的 `trajectory_metadata_blob` 中将旧 UUID 等长替换为新 UUID。
|
|
823
|
+
* 仅对长度完全相等的 ASCII UUID 执行原位字节替换,不破坏 Protobuf 长度前缀编码。
|
|
824
|
+
*/
|
|
825
|
+
function rewriteMetadataBlobUuids(
|
|
826
|
+
db: import("node:sqlite").DatabaseSync,
|
|
827
|
+
oldIds: readonly (string | undefined)[],
|
|
828
|
+
newId: string,
|
|
829
|
+
): void {
|
|
830
|
+
const hasBlobTable = db
|
|
831
|
+
.prepare(
|
|
832
|
+
"SELECT 1 AS ok FROM sqlite_master WHERE type = 'table' AND name = 'trajectory_metadata_blob'",
|
|
833
|
+
)
|
|
834
|
+
.get();
|
|
835
|
+
if (!hasBlobTable) return;
|
|
836
|
+
|
|
837
|
+
const targets = Array.from(
|
|
838
|
+
new Set(
|
|
839
|
+
oldIds.filter(
|
|
840
|
+
(id): id is string =>
|
|
841
|
+
typeof id === "string" && id.length === newId.length && id !== newId,
|
|
842
|
+
),
|
|
843
|
+
),
|
|
844
|
+
);
|
|
845
|
+
if (targets.length === 0) return;
|
|
846
|
+
|
|
847
|
+
const newBytes = Buffer.from(newId, "ascii");
|
|
848
|
+
const rows = db
|
|
849
|
+
.prepare("SELECT id, data FROM trajectory_metadata_blob WHERE data IS NOT NULL")
|
|
850
|
+
.all() as Array<{ id: string; data: Uint8Array }>;
|
|
851
|
+
const updateStmt = db.prepare("UPDATE trajectory_metadata_blob SET data = ? WHERE id = ?");
|
|
852
|
+
|
|
853
|
+
for (const row of rows) {
|
|
854
|
+
const buf = Buffer.from(row.data);
|
|
855
|
+
let changed = false;
|
|
856
|
+
for (const oldId of targets) {
|
|
857
|
+
const oldBytes = Buffer.from(oldId, "ascii");
|
|
858
|
+
let offset = 0;
|
|
859
|
+
while ((offset = buf.indexOf(oldBytes, offset)) !== -1) {
|
|
860
|
+
newBytes.copy(buf, offset);
|
|
861
|
+
offset += newBytes.length;
|
|
862
|
+
changed = true;
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
if (changed) {
|
|
866
|
+
updateStmt.run(buf, row.id);
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
/**
|
|
872
|
+
* 基于刚刚由官方 `agy session/new` 创建的空白模板会话数据库,通过只读 `VACUUM INTO` 克隆出全新 UUID 的
|
|
873
|
+
* 会话库并写入多步预注入 `(USER_INPUT, PLANNER_RESPONSE)` 历史分片。
|
|
768
874
|
*
|
|
769
|
-
*
|
|
875
|
+
* 与从零建表不同,该方法完整保留 `agy` 原生写入 `trajectory_metadata_blob` 的 `CustomAgentSpec`
|
|
876
|
+
* (如 `pi-tools` 模式下的 `pi-bridge` 禁用内置工具与默认组件规格、挂载插件、工作区 URI 与权限策略),
|
|
877
|
+
* 防止 `session/resume` 找不到 Agent 元数据时回退启用 Antigravity 原生内置工具与工作流。
|
|
878
|
+
*/
|
|
879
|
+
export async function cloneTemplateSqliteConversationWithPreload(
|
|
880
|
+
config: BridgeConfig,
|
|
881
|
+
templateSessionId: string,
|
|
882
|
+
turns: readonly PreloadedSqliteTurn[],
|
|
883
|
+
options?: { signal?: AbortSignal },
|
|
884
|
+
): Promise<string | undefined> {
|
|
885
|
+
if (!config.enableSqliteFork || !templateSessionId || turns.length === 0) {
|
|
886
|
+
return undefined;
|
|
887
|
+
}
|
|
888
|
+
const sqlite = await loadNodeSqlite();
|
|
889
|
+
if (!sqlite) return undefined;
|
|
890
|
+
|
|
891
|
+
const convDir = resolveAgyConversationsDir(config);
|
|
892
|
+
const templateDbPath = path.join(convDir, `${templateSessionId}.db`);
|
|
893
|
+
|
|
894
|
+
// 等待刚刚关闭的模板会话完成 SQLite/WAL 落盘(最长 240ms,响应取消信号)
|
|
895
|
+
const deadline = Date.now() + 240;
|
|
896
|
+
let templateReady = false;
|
|
897
|
+
while (!options?.signal?.aborted) {
|
|
898
|
+
if (fs.existsSync(templateDbPath)) {
|
|
899
|
+
let probeDb: import("node:sqlite").DatabaseSync | undefined;
|
|
900
|
+
try {
|
|
901
|
+
probeDb = openSqliteWithTimeout(sqlite, templateDbPath, true, 300);
|
|
902
|
+
if (verifyRequiredTables(probeDb)) {
|
|
903
|
+
const hasBlobTable = probeDb
|
|
904
|
+
.prepare(
|
|
905
|
+
"SELECT 1 AS ok FROM sqlite_master WHERE type = 'table' AND name = 'trajectory_metadata_blob'",
|
|
906
|
+
)
|
|
907
|
+
.get();
|
|
908
|
+
if (hasBlobTable) {
|
|
909
|
+
const blobRow = probeDb
|
|
910
|
+
.prepare(
|
|
911
|
+
"SELECT count(*) AS c FROM trajectory_metadata_blob WHERE data IS NOT NULL AND length(data) > 0",
|
|
912
|
+
)
|
|
913
|
+
.get() as { c?: number } | undefined;
|
|
914
|
+
if (Number(blobRow?.c ?? 0) > 0) {
|
|
915
|
+
templateReady = true;
|
|
916
|
+
break;
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
} catch {
|
|
921
|
+
// 模板仍在 WAL 刷盘中,短等后重试
|
|
922
|
+
} finally {
|
|
923
|
+
try {
|
|
924
|
+
probeDb?.close();
|
|
925
|
+
} catch {
|
|
926
|
+
// 忽略
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
if (Date.now() >= deadline) {
|
|
931
|
+
break;
|
|
932
|
+
}
|
|
933
|
+
await sleepWithAbort(20, options?.signal);
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
if (!templateReady || options?.signal?.aborted) {
|
|
937
|
+
return undefined;
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
const clonedSessionId = crypto.randomUUID();
|
|
941
|
+
const targetDbPath = path.join(convDir, `${clonedSessionId}.db`);
|
|
942
|
+
const escapedTarget = targetDbPath.replace(/'/g, "''");
|
|
943
|
+
|
|
944
|
+
let srcDb: import("node:sqlite").DatabaseSync | undefined;
|
|
945
|
+
try {
|
|
946
|
+
srcDb = openSqliteWithTimeout(sqlite, templateDbPath, true);
|
|
947
|
+
srcDb.exec(`VACUUM INTO '${escapedTarget}'`);
|
|
948
|
+
} catch {
|
|
949
|
+
cleanupDbFilesSafe(targetDbPath);
|
|
950
|
+
return undefined;
|
|
951
|
+
} finally {
|
|
952
|
+
try {
|
|
953
|
+
srcDb?.close();
|
|
954
|
+
} catch {
|
|
955
|
+
// 忽略
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
let dstDb: import("node:sqlite").DatabaseSync | undefined;
|
|
960
|
+
try {
|
|
961
|
+
dstDb = openSqliteWithTimeout(sqlite, targetDbPath, false);
|
|
962
|
+
const origMeta = dstDb
|
|
963
|
+
.prepare("SELECT trajectory_id FROM trajectory_meta LIMIT 1")
|
|
964
|
+
.get() as { trajectory_id?: string } | undefined;
|
|
965
|
+
|
|
966
|
+
dstDb.exec("BEGIN IMMEDIATE");
|
|
967
|
+
dstDb.prepare("DELETE FROM steps").run();
|
|
968
|
+
dstDb.prepare("DELETE FROM parent_references").run();
|
|
969
|
+
const hasBattleTable = dstDb
|
|
970
|
+
.prepare("SELECT 1 AS ok FROM sqlite_master WHERE type = 'table' AND name = 'battle_mode_infos'")
|
|
971
|
+
.get();
|
|
972
|
+
if (hasBattleTable) {
|
|
973
|
+
dstDb.prepare("DELETE FROM battle_mode_infos").run();
|
|
974
|
+
}
|
|
975
|
+
dstDb.prepare("DELETE FROM gen_metadata").run();
|
|
976
|
+
dstDb.prepare("DELETE FROM executor_metadata").run();
|
|
977
|
+
dstDb
|
|
978
|
+
.prepare("UPDATE trajectory_meta SET trajectory_id = ?, cascade_id = ?")
|
|
979
|
+
.run(clonedSessionId, clonedSessionId);
|
|
980
|
+
rewriteMetadataBlobUuids(
|
|
981
|
+
dstDb,
|
|
982
|
+
[templateSessionId, origMeta?.trajectory_id],
|
|
983
|
+
clonedSessionId,
|
|
984
|
+
);
|
|
985
|
+
|
|
986
|
+
const insertStep = dstDb.prepare(
|
|
987
|
+
"INSERT INTO steps (idx, step_type, status, has_subtrajectory, step_payload, step_format) VALUES (?, ?, ?, 0, ?, 0)",
|
|
988
|
+
);
|
|
989
|
+
let idx = 0;
|
|
990
|
+
for (const turn of turns) {
|
|
991
|
+
insertStep.run(
|
|
992
|
+
idx++,
|
|
993
|
+
CORTEX_STEP_TYPE_USER_INPUT,
|
|
994
|
+
CORTEX_STEP_STATUS_DONE,
|
|
995
|
+
encodeUserInputStepPayload(turn.userText),
|
|
996
|
+
);
|
|
997
|
+
insertStep.run(
|
|
998
|
+
idx++,
|
|
999
|
+
CORTEX_STEP_TYPE_PLANNER_RESPONSE,
|
|
1000
|
+
CORTEX_STEP_STATUS_DONE,
|
|
1001
|
+
encodePlannerResponseStepPayload(turn.assistantAckText),
|
|
1002
|
+
);
|
|
1003
|
+
}
|
|
1004
|
+
dstDb.exec("COMMIT");
|
|
1005
|
+
return clonedSessionId;
|
|
1006
|
+
} catch {
|
|
1007
|
+
try {
|
|
1008
|
+
dstDb?.exec("ROLLBACK");
|
|
1009
|
+
} catch {
|
|
1010
|
+
// 忽略
|
|
1011
|
+
}
|
|
1012
|
+
cleanupDbFilesSafe(targetDbPath);
|
|
1013
|
+
return undefined;
|
|
1014
|
+
} finally {
|
|
1015
|
+
try {
|
|
1016
|
+
dstDb?.close();
|
|
1017
|
+
} catch {
|
|
1018
|
+
// 忽略
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
/**
|
|
1024
|
+
* 新建一个独立 UUID 的 AGY 本地 SQLite 会话数据库,并将超长历史的多个安全分片以
|
|
1025
|
+
* `(USER_INPUT, PLANNER_RESPONSE)` 交替步预注入 `steps` 表(底层最小建库实现;生产路径请优先使用
|
|
1026
|
+
* `cloneTemplateSqliteConversationWithPreload` 以保留 `agy` 原生 `trajectory_metadata_blob` Agent 规格)。
|
|
770
1027
|
*/
|
|
771
1028
|
export async function createPreloadedSqliteConversation(
|
|
772
1029
|
config: BridgeConfig,
|
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
type UserMessage,
|
|
14
14
|
} from "@earendil-works/pi-ai";
|
|
15
15
|
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
16
|
-
import type
|
|
16
|
+
import { BRIDGE_AGENT_NAME, type AcpClientConnection, type AcpProcessManager } from "../acp/process-manager.ts";
|
|
17
17
|
import type {
|
|
18
18
|
AcpAvailableModel,
|
|
19
19
|
AcpConfigOption,
|
|
@@ -60,10 +60,11 @@ import {
|
|
|
60
60
|
type AgySessionAnchor,
|
|
61
61
|
appendPreloadedTurnsToSqliteConversation,
|
|
62
62
|
captureSqliteCheckpoint,
|
|
63
|
-
|
|
63
|
+
cloneTemplateSqliteConversationWithPreload,
|
|
64
64
|
findBranchAnchorForHistory,
|
|
65
65
|
forkSqliteConversationAtCheckpoint,
|
|
66
66
|
hasLocalConversationDb,
|
|
67
|
+
hasValidAgentMetadataBlob,
|
|
67
68
|
readCurrentMaxSqliteStepIdx,
|
|
68
69
|
readSqliteGenerationUsagesSince,
|
|
69
70
|
readSqliteGenerationUsagesSinceWithRetry,
|
|
@@ -81,6 +82,7 @@ import {
|
|
|
81
82
|
extractVisibleMessages,
|
|
82
83
|
inspectTrafficOptProjectionState,
|
|
83
84
|
isAuxiliarySingleTurnRequest,
|
|
85
|
+
type PreloadedSqliteTurn,
|
|
84
86
|
type TrafficOptProjectionState,
|
|
85
87
|
} from "./reconcile.ts";
|
|
86
88
|
|
|
@@ -1043,9 +1045,80 @@ export class TurnCoordinator {
|
|
|
1043
1045
|
}
|
|
1044
1046
|
|
|
1045
1047
|
/**
|
|
1046
|
-
*
|
|
1047
|
-
*
|
|
1048
|
+
* 通过「官方 `session/new` 建空模板 -> `session/close` -> `VACUUM INTO` 克隆新 UUID 并写入预注入步 -> `session/resume`」
|
|
1049
|
+
* 创建带有完整 `trajectory_metadata_blob`(包含 `pi-bridge` 等 Agent 规格、挂载插件与工作区配置)的多步预注入会话。
|
|
1050
|
+
* 任意环节失败时立即清理模板与临时副本并返回 `undefined`,由调用方安全降级至单步重放,绝不生成无 Agent 规格的裸库。
|
|
1048
1051
|
*/
|
|
1052
|
+
private async createPreloadedSessionFromTemplate(params: {
|
|
1053
|
+
conn: AcpClientConnection;
|
|
1054
|
+
cwd: string;
|
|
1055
|
+
mcpServers: AcpMcpServerSpec[];
|
|
1056
|
+
additionalDirectories?: string[];
|
|
1057
|
+
preloadedTurns: readonly PreloadedSqliteTurn[];
|
|
1058
|
+
signal?: AbortSignal;
|
|
1059
|
+
}): Promise<AcpSessionResumeResult | undefined> {
|
|
1060
|
+
if (
|
|
1061
|
+
params.preloadedTurns.length === 0 ||
|
|
1062
|
+
!this.currentConfig.enableSqliteFork ||
|
|
1063
|
+
!params.conn.capabilities?.sessionCapabilities?.resume
|
|
1064
|
+
) {
|
|
1065
|
+
return undefined;
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
let templateSessionId: string | undefined;
|
|
1069
|
+
let clonedSessionId: string | undefined;
|
|
1070
|
+
try {
|
|
1071
|
+
const templateRes = await params.conn.request<AcpSessionNewResult>(
|
|
1072
|
+
"session/new",
|
|
1073
|
+
{
|
|
1074
|
+
cwd: params.cwd,
|
|
1075
|
+
mcpServers: [],
|
|
1076
|
+
additionalDirectories: params.additionalDirectories,
|
|
1077
|
+
},
|
|
1078
|
+
{ timeoutMs: 20_000, signal: params.signal },
|
|
1079
|
+
);
|
|
1080
|
+
const createdTemplateId = templateRes.sessionId;
|
|
1081
|
+
if (!createdTemplateId) {
|
|
1082
|
+
return undefined;
|
|
1083
|
+
}
|
|
1084
|
+
templateSessionId = createdTemplateId;
|
|
1085
|
+
await params.conn.closeSessionSafe(createdTemplateId);
|
|
1086
|
+
|
|
1087
|
+
clonedSessionId = await cloneTemplateSqliteConversationWithPreload(
|
|
1088
|
+
this.currentConfig,
|
|
1089
|
+
createdTemplateId,
|
|
1090
|
+
params.preloadedTurns,
|
|
1091
|
+
{ signal: params.signal },
|
|
1092
|
+
);
|
|
1093
|
+
if (!clonedSessionId) {
|
|
1094
|
+
return undefined;
|
|
1095
|
+
}
|
|
1096
|
+
|
|
1097
|
+
return await params.conn.request<AcpSessionResumeResult>(
|
|
1098
|
+
"session/resume",
|
|
1099
|
+
{
|
|
1100
|
+
sessionId: clonedSessionId,
|
|
1101
|
+
cwd: params.cwd,
|
|
1102
|
+
mcpServers: params.mcpServers,
|
|
1103
|
+
additionalDirectories: params.additionalDirectories,
|
|
1104
|
+
},
|
|
1105
|
+
{ timeoutMs: 25_000, signal: params.signal },
|
|
1106
|
+
);
|
|
1107
|
+
} catch (error) {
|
|
1108
|
+
if (clonedSessionId) {
|
|
1109
|
+
removeEmptyProbeSessionFiles(this.currentConfig, clonedSessionId);
|
|
1110
|
+
}
|
|
1111
|
+
if (params.signal?.aborted) {
|
|
1112
|
+
throw error;
|
|
1113
|
+
}
|
|
1114
|
+
return undefined;
|
|
1115
|
+
} finally {
|
|
1116
|
+
if (templateSessionId) {
|
|
1117
|
+
removeEmptyProbeSessionFiles(this.currentConfig, templateSessionId);
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
|
|
1049
1122
|
/**
|
|
1050
1123
|
* 热会话中遇到单条追问超过单步 64k 安全预算时,严格遵循只读原库原则:
|
|
1051
1124
|
* 通过 `VACUUM INTO` 克隆当前会话快照至新 UUID `.db`,追加前 N-1 个安全文本分片后切换 `session/resume`,
|
|
@@ -1749,7 +1822,7 @@ export class TurnCoordinator {
|
|
|
1749
1822
|
|
|
1750
1823
|
const priorProjection = inspectTrafficOptProjectionState(priorHistory);
|
|
1751
1824
|
const branchEntries = this.activeContext?.sessionManager?.getBranch?.() ?? [];
|
|
1752
|
-
|
|
1825
|
+
let anchorMatch =
|
|
1753
1826
|
!params.skipAnchorRestore && priorHistory.length > 0 && pendingUserMessages.length > 0
|
|
1754
1827
|
? findBranchAnchorForHistory({
|
|
1755
1828
|
branchEntries,
|
|
@@ -1758,7 +1831,24 @@ export class TurnCoordinator {
|
|
|
1758
1831
|
mountDigest: params.mountDigest,
|
|
1759
1832
|
config: this.currentConfig,
|
|
1760
1833
|
})
|
|
1761
|
-
: { kind: "none" as const
|
|
1834
|
+
: ({ kind: "none" } as const);
|
|
1835
|
+
|
|
1836
|
+
// 自愈校验:若历史锚点指向的本地库缺失 trajectory_metadata_blob 或 pi-bridge 规格(如旧版裸建库),
|
|
1837
|
+
// 跳过直接恢复/分叉,改由路径 3 通过官方模板重建,防止回退启用 Antigravity 原生内置工具与工作流。
|
|
1838
|
+
if (
|
|
1839
|
+
anchorMatch.kind !== "none" &&
|
|
1840
|
+
this.currentConfig.toolMode === "pi-tools" &&
|
|
1841
|
+
!anchorMatch.anchor.importedRaw
|
|
1842
|
+
) {
|
|
1843
|
+
const metadataValid = await hasValidAgentMetadataBlob(
|
|
1844
|
+
this.currentConfig,
|
|
1845
|
+
anchorMatch.anchor.agySessionId,
|
|
1846
|
+
BRIDGE_AGENT_NAME,
|
|
1847
|
+
);
|
|
1848
|
+
if (!metadataValid) {
|
|
1849
|
+
anchorMatch = { kind: "none" };
|
|
1850
|
+
}
|
|
1851
|
+
}
|
|
1762
1852
|
|
|
1763
1853
|
const addDirs =
|
|
1764
1854
|
params.additionalDirectories.length > 0 ? params.additionalDirectories : undefined;
|
|
@@ -1918,7 +2008,7 @@ export class TurnCoordinator {
|
|
|
1918
2008
|
}
|
|
1919
2009
|
}
|
|
1920
2010
|
|
|
1921
|
-
// 路径 3:新建会话并重放历史(超出单步 64k
|
|
2011
|
+
// 路径 3:新建会话并重放历史(超出单步 64k 安全线时优先经官方 session/new 模板克隆 + 多步预注入 + session/resume 零额外调用恢复,失败则平滑降级为单步安全重放)
|
|
1922
2012
|
if (!sessionInfo || !initialPromptBlocks) {
|
|
1923
2013
|
const replayPlan = buildFreshReplayPlan({
|
|
1924
2014
|
visibleMessages: params.visibleMessages,
|
|
@@ -1926,32 +2016,25 @@ export class TurnCoordinator {
|
|
|
1926
2016
|
maxContextWindowTokens: params.modelContextWindow,
|
|
1927
2017
|
});
|
|
1928
2018
|
|
|
1929
|
-
if (
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
sessionInfo =
|
|
1941
|
-
"session/resume",
|
|
1942
|
-
{
|
|
1943
|
-
sessionId: seededSessionId,
|
|
1944
|
-
cwd: params.cwd,
|
|
1945
|
-
mcpServers,
|
|
1946
|
-
additionalDirectories: addDirs,
|
|
1947
|
-
},
|
|
1948
|
-
{ timeoutMs: 25_000, signal: params.signal },
|
|
1949
|
-
);
|
|
2019
|
+
if (replayPlan.preloadedTurns.length > 0) {
|
|
2020
|
+
try {
|
|
2021
|
+
const preloadedRes = await this.createPreloadedSessionFromTemplate({
|
|
2022
|
+
conn,
|
|
2023
|
+
cwd: params.cwd,
|
|
2024
|
+
mcpServers,
|
|
2025
|
+
additionalDirectories: addDirs,
|
|
2026
|
+
preloadedTurns: replayPlan.preloadedTurns,
|
|
2027
|
+
signal: params.signal,
|
|
2028
|
+
});
|
|
2029
|
+
if (preloadedRes) {
|
|
2030
|
+
sessionInfo = preloadedRes;
|
|
1950
2031
|
initialPromptBlocks = replayPlan.finalPromptBlocks;
|
|
1951
|
-
} catch {
|
|
1952
|
-
sessionInfo = undefined;
|
|
1953
|
-
removeEmptyProbeSessionFiles(this.currentConfig, seededSessionId);
|
|
1954
2032
|
}
|
|
2033
|
+
} catch (error) {
|
|
2034
|
+
if (mcpToken) {
|
|
2035
|
+
this.httpMcpBridge.unregisterSessionBinding(mcpToken);
|
|
2036
|
+
}
|
|
2037
|
+
throw error;
|
|
1955
2038
|
}
|
|
1956
2039
|
}
|
|
1957
2040
|
|
|
@@ -2956,31 +3039,14 @@ export class TurnCoordinator {
|
|
|
2956
3039
|
let sessionRes: AcpSessionNewResult | undefined;
|
|
2957
3040
|
let promptBlocks: AcpPromptBlock[] = auxPlan.finalPromptBlocks;
|
|
2958
3041
|
|
|
2959
|
-
if (
|
|
2960
|
-
|
|
2961
|
-
|
|
2962
|
-
|
|
2963
|
-
|
|
2964
|
-
|
|
2965
|
-
|
|
2966
|
-
|
|
2967
|
-
);
|
|
2968
|
-
if (seededId) {
|
|
2969
|
-
try {
|
|
2970
|
-
sessionRes = await conn.request<AcpSessionResumeResult>(
|
|
2971
|
-
"session/resume",
|
|
2972
|
-
{
|
|
2973
|
-
sessionId: seededId,
|
|
2974
|
-
cwd,
|
|
2975
|
-
mcpServers: [],
|
|
2976
|
-
},
|
|
2977
|
-
{ timeoutMs: 25_000, signal: options?.signal },
|
|
2978
|
-
);
|
|
2979
|
-
} catch {
|
|
2980
|
-
sessionRes = undefined;
|
|
2981
|
-
removeEmptyProbeSessionFiles(this.currentConfig, seededId);
|
|
2982
|
-
}
|
|
2983
|
-
}
|
|
3042
|
+
if (auxPlan.preloadedTurns.length > 0) {
|
|
3043
|
+
sessionRes = await this.createPreloadedSessionFromTemplate({
|
|
3044
|
+
conn,
|
|
3045
|
+
cwd,
|
|
3046
|
+
mcpServers: [],
|
|
3047
|
+
preloadedTurns: auxPlan.preloadedTurns,
|
|
3048
|
+
signal: options?.signal,
|
|
3049
|
+
});
|
|
2984
3050
|
}
|
|
2985
3051
|
|
|
2986
3052
|
if (!sessionRes) {
|