pi-antigravity-bridge 0.1.8 → 0.1.9
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 +12 -0
- package/package.json +1 -1
- package/src/session/checkpoint-sync.ts +266 -1
- package/src/session/reconcile.ts +797 -39
- package/src/session/turn-coordinator.ts +394 -66
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.1.9] - 2026-11-22
|
|
4
|
+
|
|
5
|
+
### Changed
|
|
6
|
+
|
|
7
|
+
- 扩展超长历史重放、单次超长用户输入及 `/compact` 等长文本辅助请求的上下文容量,自动分段载入以突破单条 64k 输入限制,充分利用模型完整上下文窗口。
|
|
8
|
+
- 在降级至单步重放路径时动态扣除工作区上下文与末尾提问占用,优先保留最新对话与末尾用户提问。
|
|
9
|
+
|
|
10
|
+
### Fixed
|
|
11
|
+
|
|
12
|
+
- 修复长会话重建、分支恢复或超长输入场景下触发 `agy` 单步 64k 上限导致末尾最新提问被截断丢失的问题。
|
|
13
|
+
- 修复超长文本自动分段在 Emoji 等多字节字符边界处可能出现卡死的问题。
|
|
14
|
+
|
|
3
15
|
## [0.1.8] - 2026-11-22
|
|
4
16
|
|
|
5
17
|
### Added
|
package/package.json
CHANGED
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
type ToolMode,
|
|
9
9
|
resolveAgyConversationsDir,
|
|
10
10
|
} from "../config.ts";
|
|
11
|
-
import { computeTranscriptDigest } from "./reconcile.ts";
|
|
11
|
+
import { computeTranscriptDigest, type PreloadedSqliteTurn } from "./reconcile.ts";
|
|
12
12
|
|
|
13
13
|
export const AGY_SESSION_ANCHOR_CUSTOM_TYPE = "antigravity-acp-state";
|
|
14
14
|
|
|
@@ -702,6 +702,271 @@ export async function forkSqliteConversationAtCheckpoint(
|
|
|
702
702
|
}
|
|
703
703
|
}
|
|
704
704
|
|
|
705
|
+
const CORTEX_STEP_TYPE_USER_INPUT = 14;
|
|
706
|
+
const CORTEX_STEP_TYPE_PLANNER_RESPONSE = 15;
|
|
707
|
+
const CORTEX_STEP_STATUS_DONE = 3;
|
|
708
|
+
const CORTEX_TRAJECTORY_TYPE_INTERACTIVE_CASCADE = 4;
|
|
709
|
+
const CORTEX_TRAJECTORY_SOURCE_CLI = 17;
|
|
710
|
+
|
|
711
|
+
function encodeProtobufVarint(value: number | bigint): Buffer {
|
|
712
|
+
let v = typeof value === "bigint" ? value : BigInt(value);
|
|
713
|
+
const out: number[] = [];
|
|
714
|
+
while (v >= 0x80n) {
|
|
715
|
+
out.push(Number((v & 0x7fn) | 0x80n));
|
|
716
|
+
v >>= 7n;
|
|
717
|
+
}
|
|
718
|
+
out.push(Number(v));
|
|
719
|
+
return Buffer.from(out);
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
function encodeProtobufVarintField(fieldNo: number, value: number | bigint): Buffer {
|
|
723
|
+
return Buffer.concat([encodeProtobufVarint((fieldNo << 3) | 0), encodeProtobufVarint(value)]);
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
function encodeProtobufBytesField(fieldNo: number, payload: Buffer | string): Buffer {
|
|
727
|
+
const buf = Buffer.isBuffer(payload) ? payload : Buffer.from(payload, "utf8");
|
|
728
|
+
return Buffer.concat([
|
|
729
|
+
encodeProtobufVarint((fieldNo << 3) | 2),
|
|
730
|
+
encodeProtobufVarint(buf.length),
|
|
731
|
+
buf,
|
|
732
|
+
]);
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
/** 构造 AGY `CortexStep`(`USER_INPUT`)最小标准 Protobuf 载荷 */
|
|
736
|
+
export function encodeUserInputStepPayload(text: string): Buffer {
|
|
737
|
+
// CortexStepUserInput: field 2 = user_response, field 3 = items (TextOrScopeItem { field 1 = text })
|
|
738
|
+
const textItem = encodeProtobufBytesField(1, text);
|
|
739
|
+
const userInput = Buffer.concat([
|
|
740
|
+
encodeProtobufBytesField(2, text),
|
|
741
|
+
encodeProtobufBytesField(3, textItem),
|
|
742
|
+
]);
|
|
743
|
+
return Buffer.concat([
|
|
744
|
+
encodeProtobufVarintField(1, CORTEX_STEP_TYPE_USER_INPUT),
|
|
745
|
+
encodeProtobufVarintField(4, CORTEX_STEP_STATUS_DONE),
|
|
746
|
+
encodeProtobufBytesField(19, userInput),
|
|
747
|
+
]);
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
/** 构造 AGY `CortexStep`(`PLANNER_RESPONSE`)最小标准 Protobuf 载荷 */
|
|
751
|
+
export function encodePlannerResponseStepPayload(text: string): Buffer {
|
|
752
|
+
// CortexStepPlannerResponse: field 1 = response, field 8 = modified_response
|
|
753
|
+
const plannerResponse = Buffer.concat([
|
|
754
|
+
encodeProtobufBytesField(1, text),
|
|
755
|
+
encodeProtobufBytesField(8, text),
|
|
756
|
+
]);
|
|
757
|
+
return Buffer.concat([
|
|
758
|
+
encodeProtobufVarintField(1, CORTEX_STEP_TYPE_PLANNER_RESPONSE),
|
|
759
|
+
encodeProtobufVarintField(4, CORTEX_STEP_STATUS_DONE),
|
|
760
|
+
encodeProtobufBytesField(20, plannerResponse),
|
|
761
|
+
]);
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
/**
|
|
765
|
+
* 新建一个独立 UUID 的 AGY 本地 SQLite 会话数据库,并将超长历史的多个安全分片以
|
|
766
|
+
* `(USER_INPUT, PLANNER_RESPONSE)` 交替步预注入 `steps` 表,供随后通过 `session/resume`
|
|
767
|
+
* 零额外 LLM 调用直接恢复,规避 `agy` 单条 `USER_INPUT` 64,000 token 截断上限。
|
|
768
|
+
*
|
|
769
|
+
* 严格仅新建独立 UUID 文件,永不改动任何已有会话库;任何异常均立即清理临时文件并返回 `undefined`。
|
|
770
|
+
*/
|
|
771
|
+
export async function createPreloadedSqliteConversation(
|
|
772
|
+
config: BridgeConfig,
|
|
773
|
+
turns: readonly PreloadedSqliteTurn[],
|
|
774
|
+
): Promise<string | undefined> {
|
|
775
|
+
if (!config.enableSqliteFork || turns.length === 0) {
|
|
776
|
+
return undefined;
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
const sqlite = await loadNodeSqlite();
|
|
780
|
+
if (!sqlite) return undefined;
|
|
781
|
+
|
|
782
|
+
const convDir = resolveAgyConversationsDir(config);
|
|
783
|
+
try {
|
|
784
|
+
fs.mkdirSync(convDir, { recursive: true });
|
|
785
|
+
} catch {
|
|
786
|
+
return undefined;
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
const sessionId = crypto.randomUUID();
|
|
790
|
+
const dbPath = path.join(convDir, `${sessionId}.db`);
|
|
791
|
+
|
|
792
|
+
let db: import("node:sqlite").DatabaseSync | undefined;
|
|
793
|
+
try {
|
|
794
|
+
db = openSqliteWithTimeout(sqlite, dbPath, false);
|
|
795
|
+
db.exec(`
|
|
796
|
+
PRAGMA user_version = 1;
|
|
797
|
+
CREATE TABLE trajectory_meta (
|
|
798
|
+
trajectory_id text,
|
|
799
|
+
cascade_id text,
|
|
800
|
+
trajectory_type integer,
|
|
801
|
+
source integer,
|
|
802
|
+
PRIMARY KEY (trajectory_id)
|
|
803
|
+
);
|
|
804
|
+
CREATE TABLE steps (
|
|
805
|
+
idx integer,
|
|
806
|
+
step_type integer NOT NULL DEFAULT 0,
|
|
807
|
+
status integer NOT NULL DEFAULT 0,
|
|
808
|
+
has_subtrajectory numeric NOT NULL DEFAULT false,
|
|
809
|
+
metadata blob,
|
|
810
|
+
error_details blob,
|
|
811
|
+
permissions blob,
|
|
812
|
+
task_details blob,
|
|
813
|
+
render_info blob,
|
|
814
|
+
step_payload blob,
|
|
815
|
+
step_format integer NOT NULL DEFAULT 0,
|
|
816
|
+
PRIMARY KEY (idx)
|
|
817
|
+
);
|
|
818
|
+
CREATE INDEX idx_steps_status ON steps(status);
|
|
819
|
+
CREATE INDEX idx_steps_step_type ON steps(step_type);
|
|
820
|
+
CREATE TABLE gen_metadata (
|
|
821
|
+
idx integer,
|
|
822
|
+
data blob,
|
|
823
|
+
size integer NOT NULL DEFAULT 0,
|
|
824
|
+
PRIMARY KEY (idx)
|
|
825
|
+
);
|
|
826
|
+
CREATE TABLE executor_metadata (
|
|
827
|
+
idx integer,
|
|
828
|
+
data blob,
|
|
829
|
+
PRIMARY KEY (idx)
|
|
830
|
+
);
|
|
831
|
+
CREATE TABLE parent_references (
|
|
832
|
+
idx integer,
|
|
833
|
+
data blob,
|
|
834
|
+
PRIMARY KEY (idx)
|
|
835
|
+
);
|
|
836
|
+
CREATE TABLE trajectory_metadata_blob (
|
|
837
|
+
id text DEFAULT "main",
|
|
838
|
+
data blob,
|
|
839
|
+
PRIMARY KEY (id)
|
|
840
|
+
);
|
|
841
|
+
CREATE TABLE battle_mode_infos (
|
|
842
|
+
idx integer,
|
|
843
|
+
data blob,
|
|
844
|
+
PRIMARY KEY (idx)
|
|
845
|
+
);
|
|
846
|
+
`);
|
|
847
|
+
|
|
848
|
+
db.exec("BEGIN IMMEDIATE");
|
|
849
|
+
db.prepare(
|
|
850
|
+
"INSERT INTO trajectory_meta (trajectory_id, cascade_id, trajectory_type, source) VALUES (?, ?, ?, ?)",
|
|
851
|
+
).run(
|
|
852
|
+
sessionId,
|
|
853
|
+
sessionId,
|
|
854
|
+
CORTEX_TRAJECTORY_TYPE_INTERACTIVE_CASCADE,
|
|
855
|
+
CORTEX_TRAJECTORY_SOURCE_CLI,
|
|
856
|
+
);
|
|
857
|
+
|
|
858
|
+
const insertStep = db.prepare(
|
|
859
|
+
"INSERT INTO steps (idx, step_type, status, has_subtrajectory, step_payload, step_format) VALUES (?, ?, ?, 0, ?, 0)",
|
|
860
|
+
);
|
|
861
|
+
let idx = 0;
|
|
862
|
+
for (const turn of turns) {
|
|
863
|
+
insertStep.run(
|
|
864
|
+
idx++,
|
|
865
|
+
CORTEX_STEP_TYPE_USER_INPUT,
|
|
866
|
+
CORTEX_STEP_STATUS_DONE,
|
|
867
|
+
encodeUserInputStepPayload(turn.userText),
|
|
868
|
+
);
|
|
869
|
+
insertStep.run(
|
|
870
|
+
idx++,
|
|
871
|
+
CORTEX_STEP_TYPE_PLANNER_RESPONSE,
|
|
872
|
+
CORTEX_STEP_STATUS_DONE,
|
|
873
|
+
encodePlannerResponseStepPayload(turn.assistantAckText),
|
|
874
|
+
);
|
|
875
|
+
}
|
|
876
|
+
db.exec("COMMIT");
|
|
877
|
+
return sessionId;
|
|
878
|
+
} catch {
|
|
879
|
+
try {
|
|
880
|
+
db?.exec("ROLLBACK");
|
|
881
|
+
} catch {
|
|
882
|
+
// 忽略
|
|
883
|
+
}
|
|
884
|
+
cleanupDbFilesSafe(dbPath);
|
|
885
|
+
return undefined;
|
|
886
|
+
} finally {
|
|
887
|
+
try {
|
|
888
|
+
db?.close();
|
|
889
|
+
} catch {
|
|
890
|
+
// 忽略
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
/**
|
|
896
|
+
* 向刚通过 `forkSqliteConversationAtCheckpoint` 克隆出的新分叉 `.db` 末尾追加预注入分片步。
|
|
897
|
+
* 仅用于尚未被 `agy` 打开的新分叉副本,严禁对正在运行的原库直接调用。
|
|
898
|
+
*/
|
|
899
|
+
export async function appendPreloadedTurnsToSqliteConversation(
|
|
900
|
+
config: BridgeConfig,
|
|
901
|
+
forkedSessionId: string,
|
|
902
|
+
turns: readonly PreloadedSqliteTurn[],
|
|
903
|
+
): Promise<boolean> {
|
|
904
|
+
if (!config.enableSqliteFork || !forkedSessionId || turns.length === 0) {
|
|
905
|
+
return false;
|
|
906
|
+
}
|
|
907
|
+
const dbPath = path.join(resolveAgyConversationsDir(config), `${forkedSessionId}.db`);
|
|
908
|
+
if (!fs.existsSync(dbPath)) {
|
|
909
|
+
return false;
|
|
910
|
+
}
|
|
911
|
+
const sqlite = await loadNodeSqlite();
|
|
912
|
+
if (!sqlite) return false;
|
|
913
|
+
|
|
914
|
+
let db: import("node:sqlite").DatabaseSync | undefined;
|
|
915
|
+
try {
|
|
916
|
+
db = openSqliteWithTimeout(sqlite, dbPath, false);
|
|
917
|
+
if (!verifyRequiredTables(db)) {
|
|
918
|
+
return false;
|
|
919
|
+
}
|
|
920
|
+
const lastRow = db
|
|
921
|
+
.prepare("SELECT idx, step_type FROM steps ORDER BY idx DESC LIMIT 1")
|
|
922
|
+
.get() as { idx?: number; step_type?: number } | undefined;
|
|
923
|
+
let nextIdx = typeof lastRow?.idx === "number" ? lastRow.idx + 1 : 0;
|
|
924
|
+
|
|
925
|
+
db.exec("BEGIN IMMEDIATE");
|
|
926
|
+
const insertStep = db.prepare(
|
|
927
|
+
"INSERT INTO steps (idx, step_type, status, has_subtrajectory, step_payload, step_format) VALUES (?, ?, ?, 0, ?, 0)",
|
|
928
|
+
);
|
|
929
|
+
// 若原分叉尾部停在 USER_INPUT,先补一条中性 PLANNER_RESPONSE 保持严格交替角色合法性
|
|
930
|
+
if (lastRow?.step_type === CORTEX_STEP_TYPE_USER_INPUT) {
|
|
931
|
+
insertStep.run(
|
|
932
|
+
nextIdx++,
|
|
933
|
+
CORTEX_STEP_TYPE_PLANNER_RESPONSE,
|
|
934
|
+
CORTEX_STEP_STATUS_DONE,
|
|
935
|
+
encodePlannerResponseStepPayload("Continuing session context."),
|
|
936
|
+
);
|
|
937
|
+
}
|
|
938
|
+
for (const turn of turns) {
|
|
939
|
+
insertStep.run(
|
|
940
|
+
nextIdx++,
|
|
941
|
+
CORTEX_STEP_TYPE_USER_INPUT,
|
|
942
|
+
CORTEX_STEP_STATUS_DONE,
|
|
943
|
+
encodeUserInputStepPayload(turn.userText),
|
|
944
|
+
);
|
|
945
|
+
insertStep.run(
|
|
946
|
+
nextIdx++,
|
|
947
|
+
CORTEX_STEP_TYPE_PLANNER_RESPONSE,
|
|
948
|
+
CORTEX_STEP_STATUS_DONE,
|
|
949
|
+
encodePlannerResponseStepPayload(turn.assistantAckText),
|
|
950
|
+
);
|
|
951
|
+
}
|
|
952
|
+
db.exec("COMMIT");
|
|
953
|
+
return true;
|
|
954
|
+
} catch {
|
|
955
|
+
try {
|
|
956
|
+
db?.exec("ROLLBACK");
|
|
957
|
+
} catch {
|
|
958
|
+
// 忽略
|
|
959
|
+
}
|
|
960
|
+
return false;
|
|
961
|
+
} finally {
|
|
962
|
+
try {
|
|
963
|
+
db?.close();
|
|
964
|
+
} catch {
|
|
965
|
+
// 忽略
|
|
966
|
+
}
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
|
|
705
970
|
function cleanupDbFilesSafe(baseDbPath: string): void {
|
|
706
971
|
for (const ext of ["", "-wal", "-shm", "-journal"]) {
|
|
707
972
|
try {
|