@dyyz1993/pi-coding-agent 0.70.6 → 0.74.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.
Files changed (275) hide show
  1. package/CHANGELOG.md +266 -80
  2. package/README.md +48 -20
  3. package/dist/bun/cli.d.ts.map +1 -1
  4. package/dist/bun/cli.js +4 -2
  5. package/dist/bun/cli.js.map +1 -1
  6. package/dist/bun/restore-sandbox-env.d.ts +13 -0
  7. package/dist/bun/restore-sandbox-env.d.ts.map +1 -0
  8. package/dist/bun/restore-sandbox-env.js +32 -0
  9. package/dist/bun/restore-sandbox-env.js.map +1 -0
  10. package/dist/cli/args.d.ts +2 -1
  11. package/dist/cli/args.d.ts.map +1 -1
  12. package/dist/cli/args.js +34 -22
  13. package/dist/cli/args.js.map +1 -1
  14. package/dist/cli/list-models.d.ts.map +1 -1
  15. package/dist/cli/list-models.js +2 -1
  16. package/dist/cli/list-models.js.map +1 -1
  17. package/dist/cli.d.ts.map +1 -1
  18. package/dist/cli.js +9 -4
  19. package/dist/cli.js.map +1 -1
  20. package/dist/config.d.ts +16 -8
  21. package/dist/config.d.ts.map +1 -1
  22. package/dist/config.js +238 -66
  23. package/dist/config.js.map +1 -1
  24. package/dist/core/agent-session-runtime.d.ts +10 -0
  25. package/dist/core/agent-session-runtime.d.ts.map +1 -1
  26. package/dist/core/agent-session-runtime.js +14 -0
  27. package/dist/core/agent-session-runtime.js.map +1 -1
  28. package/dist/core/agent-session-services.d.ts +2 -1
  29. package/dist/core/agent-session-services.d.ts.map +1 -1
  30. package/dist/core/agent-session-services.js +1 -0
  31. package/dist/core/agent-session-services.js.map +1 -1
  32. package/dist/core/agent-session.d.ts +37 -26
  33. package/dist/core/agent-session.d.ts.map +1 -1
  34. package/dist/core/agent-session.js +1068 -1116
  35. package/dist/core/agent-session.js.map +1 -1
  36. package/dist/core/auth-guidance.d.ts +5 -0
  37. package/dist/core/auth-guidance.d.ts.map +1 -0
  38. package/dist/core/auth-guidance.js +21 -0
  39. package/dist/core/auth-guidance.js.map +1 -0
  40. package/dist/core/auth-storage.d.ts +9 -0
  41. package/dist/core/auth-storage.d.ts.map +1 -1
  42. package/dist/core/auth-storage.js +20 -1
  43. package/dist/core/auth-storage.js.map +1 -1
  44. package/dist/core/bash-executor.d.ts.map +1 -1
  45. package/dist/core/bash-executor.js +9 -6
  46. package/dist/core/bash-executor.js.map +1 -1
  47. package/dist/core/compaction/compaction.d.ts +0 -1
  48. package/dist/core/compaction/compaction.d.ts.map +1 -1
  49. package/dist/core/compaction/compaction.js.map +1 -1
  50. package/dist/core/export-html/ansi-to-html.d.ts.map +1 -1
  51. package/dist/core/export-html/ansi-to-html.js +1 -1
  52. package/dist/core/export-html/ansi-to-html.js.map +1 -1
  53. package/dist/core/export-html/template.css +53 -4
  54. package/dist/core/export-html/template.js +84 -20
  55. package/dist/core/export-html/tool-renderer.d.ts +0 -6
  56. package/dist/core/export-html/tool-renderer.d.ts.map +1 -1
  57. package/dist/core/export-html/tool-renderer.js +15 -2
  58. package/dist/core/export-html/tool-renderer.js.map +1 -1
  59. package/dist/core/extensions/index.d.ts +1 -1
  60. package/dist/core/extensions/index.d.ts.map +1 -1
  61. package/dist/core/extensions/index.js.map +1 -1
  62. package/dist/core/extensions/loader.d.ts +0 -1
  63. package/dist/core/extensions/loader.d.ts.map +1 -1
  64. package/dist/core/extensions/loader.js +49 -137
  65. package/dist/core/extensions/loader.js.map +1 -1
  66. package/dist/core/extensions/runner.d.ts +24 -20
  67. package/dist/core/extensions/runner.d.ts.map +1 -1
  68. package/dist/core/extensions/runner.js +128 -253
  69. package/dist/core/extensions/runner.js.map +1 -1
  70. package/dist/core/extensions/types.d.ts +88 -60
  71. package/dist/core/extensions/types.d.ts.map +1 -1
  72. package/dist/core/extensions/types.js +10 -0
  73. package/dist/core/extensions/types.js.map +1 -1
  74. package/dist/core/file-store/file-snapshot-manager.d.ts +95 -0
  75. package/dist/core/file-store/file-snapshot-manager.d.ts.map +1 -0
  76. package/dist/core/file-store/file-snapshot-manager.js +508 -0
  77. package/dist/core/file-store/file-snapshot-manager.js.map +1 -0
  78. package/dist/core/file-store/index.d.ts +5 -0
  79. package/dist/core/file-store/index.d.ts.map +1 -0
  80. package/dist/core/file-store/index.js +3 -0
  81. package/dist/core/file-store/index.js.map +1 -0
  82. package/dist/core/messages.d.ts +10 -2
  83. package/dist/core/messages.d.ts.map +1 -1
  84. package/dist/core/messages.js +23 -6
  85. package/dist/core/messages.js.map +1 -1
  86. package/dist/core/model-registry.d.ts +19 -1
  87. package/dist/core/model-registry.d.ts.map +1 -1
  88. package/dist/core/model-registry.js +97 -16
  89. package/dist/core/model-registry.js.map +1 -1
  90. package/dist/core/model-resolver.d.ts.map +1 -1
  91. package/dist/core/model-resolver.js +24 -15
  92. package/dist/core/model-resolver.js.map +1 -1
  93. package/dist/core/package-manager.d.ts +1 -0
  94. package/dist/core/package-manager.d.ts.map +1 -1
  95. package/dist/core/package-manager.js +61 -35
  96. package/dist/core/package-manager.js.map +1 -1
  97. package/dist/core/provider-display-names.d.ts +2 -0
  98. package/dist/core/provider-display-names.d.ts.map +1 -0
  99. package/dist/core/provider-display-names.js +32 -0
  100. package/dist/core/provider-display-names.js.map +1 -0
  101. package/dist/core/resource-loader.d.ts.map +1 -1
  102. package/dist/core/resource-loader.js +9 -21
  103. package/dist/core/resource-loader.js.map +1 -1
  104. package/dist/core/sdk.d.ts +9 -1
  105. package/dist/core/sdk.d.ts.map +1 -1
  106. package/dist/core/sdk.js +39 -18
  107. package/dist/core/sdk.js.map +1 -1
  108. package/dist/core/session-manager.d.ts +27 -17
  109. package/dist/core/session-manager.d.ts.map +1 -1
  110. package/dist/core/session-manager.js +133 -47
  111. package/dist/core/session-manager.js.map +1 -1
  112. package/dist/core/settings-manager.d.ts +22 -3
  113. package/dist/core/settings-manager.d.ts.map +1 -1
  114. package/dist/core/settings-manager.js +54 -6
  115. package/dist/core/settings-manager.js.map +1 -1
  116. package/dist/core/skills.d.ts.map +1 -1
  117. package/dist/core/skills.js +3 -8
  118. package/dist/core/skills.js.map +1 -1
  119. package/dist/core/slash-commands.d.ts.map +1 -1
  120. package/dist/core/slash-commands.js +4 -3
  121. package/dist/core/slash-commands.js.map +1 -1
  122. package/dist/core/tools/bash.d.ts +0 -2
  123. package/dist/core/tools/bash.d.ts.map +1 -1
  124. package/dist/core/tools/bash.js +155 -110
  125. package/dist/core/tools/bash.js.map +1 -1
  126. package/dist/core/tools/edit-diff.d.ts.map +1 -1
  127. package/dist/core/tools/edit-diff.js +3 -2
  128. package/dist/core/tools/edit-diff.js.map +1 -1
  129. package/dist/core/tools/edit.d.ts.map +1 -1
  130. package/dist/core/tools/edit.js +4 -3
  131. package/dist/core/tools/edit.js.map +1 -1
  132. package/dist/core/tools/find.d.ts.map +1 -1
  133. package/dist/core/tools/find.js +1 -1
  134. package/dist/core/tools/find.js.map +1 -1
  135. package/dist/core/tools/grep.d.ts.map +1 -1
  136. package/dist/core/tools/grep.js +1 -1
  137. package/dist/core/tools/grep.js.map +1 -1
  138. package/dist/core/tools/output-accumulator.d.ts +50 -0
  139. package/dist/core/tools/output-accumulator.d.ts.map +1 -0
  140. package/dist/core/tools/output-accumulator.js +178 -0
  141. package/dist/core/tools/output-accumulator.js.map +1 -0
  142. package/dist/core/tools/read.d.ts.map +1 -1
  143. package/dist/core/tools/read.js +70 -13
  144. package/dist/core/tools/read.js.map +1 -1
  145. package/dist/index.d.ts +1 -1
  146. package/dist/index.d.ts.map +1 -1
  147. package/dist/index.js.map +1 -1
  148. package/dist/main.d.ts.map +1 -1
  149. package/dist/main.js +17 -39
  150. package/dist/main.js.map +1 -1
  151. package/dist/migrations.d.ts +1 -1
  152. package/dist/migrations.d.ts.map +1 -1
  153. package/dist/migrations.js +3 -3
  154. package/dist/migrations.js.map +1 -1
  155. package/dist/modes/interactive/components/config-selector.d.ts.map +1 -1
  156. package/dist/modes/interactive/components/config-selector.js +3 -1
  157. package/dist/modes/interactive/components/config-selector.js.map +1 -1
  158. package/dist/modes/interactive/components/extension-selector.d.ts +1 -4
  159. package/dist/modes/interactive/components/extension-selector.d.ts.map +1 -1
  160. package/dist/modes/interactive/components/extension-selector.js +14 -56
  161. package/dist/modes/interactive/components/extension-selector.js.map +1 -1
  162. package/dist/modes/interactive/components/login-dialog.d.ts +5 -1
  163. package/dist/modes/interactive/components/login-dialog.d.ts.map +1 -1
  164. package/dist/modes/interactive/components/login-dialog.js +19 -4
  165. package/dist/modes/interactive/components/login-dialog.js.map +1 -1
  166. package/dist/modes/interactive/components/model-selector.d.ts.map +1 -1
  167. package/dist/modes/interactive/components/model-selector.js +1 -1
  168. package/dist/modes/interactive/components/model-selector.js.map +1 -1
  169. package/dist/modes/interactive/components/oauth-selector.d.ts +18 -6
  170. package/dist/modes/interactive/components/oauth-selector.d.ts.map +1 -1
  171. package/dist/modes/interactive/components/oauth-selector.js +93 -25
  172. package/dist/modes/interactive/components/oauth-selector.js.map +1 -1
  173. package/dist/modes/interactive/components/scoped-models-selector.d.ts.map +1 -1
  174. package/dist/modes/interactive/components/scoped-models-selector.js +1 -1
  175. package/dist/modes/interactive/components/scoped-models-selector.js.map +1 -1
  176. package/dist/modes/interactive/components/session-selector.d.ts.map +1 -1
  177. package/dist/modes/interactive/components/session-selector.js +3 -7
  178. package/dist/modes/interactive/components/session-selector.js.map +1 -1
  179. package/dist/modes/interactive/components/settings-selector.d.ts +5 -0
  180. package/dist/modes/interactive/components/settings-selector.d.ts.map +1 -1
  181. package/dist/modes/interactive/components/settings-selector.js +53 -1
  182. package/dist/modes/interactive/components/settings-selector.js.map +1 -1
  183. package/dist/modes/interactive/interactive-mode.d.ts +20 -4
  184. package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
  185. package/dist/modes/interactive/interactive-mode.js +423 -186
  186. package/dist/modes/interactive/interactive-mode.js.map +1 -1
  187. package/dist/modes/interactive/theme/dark.json +1 -1
  188. package/dist/modes/interactive/theme/light.json +1 -1
  189. package/dist/modes/print-mode.d.ts +3 -0
  190. package/dist/modes/print-mode.d.ts.map +1 -1
  191. package/dist/modes/print-mode.js +62 -19
  192. package/dist/modes/print-mode.js.map +1 -1
  193. package/dist/modes/rpc/rpc-client.d.ts +80 -60
  194. package/dist/modes/rpc/rpc-client.d.ts.map +1 -1
  195. package/dist/modes/rpc/rpc-client.js +108 -93
  196. package/dist/modes/rpc/rpc-client.js.map +1 -1
  197. package/dist/modes/rpc/rpc-mode.d.ts.map +1 -1
  198. package/dist/modes/rpc/rpc-mode.js +106 -0
  199. package/dist/modes/rpc/rpc-mode.js.map +1 -1
  200. package/dist/modes/rpc/rpc-types.d.ts +115 -0
  201. package/dist/modes/rpc/rpc-types.d.ts.map +1 -1
  202. package/dist/modes/rpc/rpc-types.js.map +1 -1
  203. package/dist/package-manager-cli.d.ts.map +1 -1
  204. package/dist/package-manager-cli.js +238 -12
  205. package/dist/package-manager-cli.js.map +1 -1
  206. package/dist/utils/child-process.d.ts +1 -0
  207. package/dist/utils/child-process.d.ts.map +1 -1
  208. package/dist/utils/child-process.js +8 -0
  209. package/dist/utils/child-process.js.map +1 -1
  210. package/dist/utils/clipboard-image.d.ts.map +1 -1
  211. package/dist/utils/clipboard-image.js +2 -2
  212. package/dist/utils/clipboard-image.js.map +1 -1
  213. package/dist/utils/clipboard.d.ts.map +1 -1
  214. package/dist/utils/clipboard.js +84 -45
  215. package/dist/utils/clipboard.js.map +1 -1
  216. package/dist/utils/paths.d.ts +9 -0
  217. package/dist/utils/paths.d.ts.map +1 -1
  218. package/dist/utils/paths.js +31 -0
  219. package/dist/utils/paths.js.map +1 -1
  220. package/dist/utils/pi-user-agent.d.ts +2 -0
  221. package/dist/utils/pi-user-agent.d.ts.map +1 -0
  222. package/dist/utils/pi-user-agent.js +5 -0
  223. package/dist/utils/pi-user-agent.js.map +1 -0
  224. package/dist/utils/structured-output.d.ts +10 -0
  225. package/dist/utils/structured-output.d.ts.map +1 -0
  226. package/dist/utils/structured-output.js +57 -0
  227. package/dist/utils/structured-output.js.map +1 -0
  228. package/dist/utils/tools-manager.d.ts.map +1 -1
  229. package/dist/utils/tools-manager.js +6 -2
  230. package/dist/utils/tools-manager.js.map +1 -1
  231. package/dist/utils/version-check.d.ts +14 -0
  232. package/dist/utils/version-check.d.ts.map +1 -0
  233. package/dist/utils/version-check.js +77 -0
  234. package/dist/utils/version-check.js.map +1 -0
  235. package/docs/compaction.md +14 -14
  236. package/docs/custom-provider.md +40 -31
  237. package/docs/development.md +1 -1
  238. package/docs/docs.json +148 -0
  239. package/docs/extensions.md +116 -56
  240. package/docs/index.md +70 -0
  241. package/docs/json.md +4 -4
  242. package/docs/models.md +150 -3
  243. package/docs/packages.md +10 -5
  244. package/docs/providers.md +62 -17
  245. package/docs/quickstart.md +142 -0
  246. package/docs/rollback-architecture.md +693 -0
  247. package/docs/rollback-test-cases.md +412 -0
  248. package/docs/rpc.md +1 -1
  249. package/docs/sdk.md +26 -26
  250. package/docs/{session.md → session-format.md} +6 -6
  251. package/docs/sessions.md +137 -0
  252. package/docs/settings.md +52 -9
  253. package/docs/termux.md +1 -1
  254. package/docs/themes.md +2 -2
  255. package/docs/tui.md +20 -20
  256. package/docs/usage.md +277 -0
  257. package/examples/extensions/README.md +2 -4
  258. package/examples/extensions/border-status-editor.ts +150 -0
  259. package/examples/extensions/commands.ts +2 -2
  260. package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
  261. package/examples/extensions/custom-provider-anthropic/package.json +1 -1
  262. package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
  263. package/examples/extensions/dynamic-resources/dynamic.json +1 -1
  264. package/examples/extensions/git-checkpoint.ts +1 -1
  265. package/examples/extensions/handoff.ts +49 -11
  266. package/examples/extensions/plan-mode/index.ts +1 -1
  267. package/examples/extensions/sandbox/package-lock.json +5 -5
  268. package/examples/extensions/sandbox/package.json +1 -1
  269. package/examples/extensions/subagent/agents.ts +126 -0
  270. package/examples/extensions/with-deps/package-lock.json +2 -2
  271. package/examples/extensions/with-deps/package.json +1 -1
  272. package/examples/sdk/README.md +2 -2
  273. package/package.json +7 -16
  274. package/docs/tree.md +0 -233
  275. package/examples/extensions/antigravity-image-gen.ts +0 -418
@@ -0,0 +1,508 @@
1
+ import { existsSync, lstatSync, mkdirSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs";
2
+ import { dirname, join } from "node:path";
3
+ const FILE_SIZE_LIMIT = 1024 * 1024;
4
+ function findCanonicalGitRoot(cwd) {
5
+ let dir;
6
+ try {
7
+ dir = realpathSync(cwd);
8
+ }
9
+ catch {
10
+ return null;
11
+ }
12
+ for (;;) {
13
+ const gitPath = join(dir, ".git");
14
+ if (!existsSync(gitPath)) {
15
+ const parent = dirname(dir);
16
+ if (parent === dir)
17
+ return null;
18
+ dir = parent;
19
+ continue;
20
+ }
21
+ const stat = lstatSync(gitPath);
22
+ if (stat.isDirectory())
23
+ return dir;
24
+ if (stat.isFile()) {
25
+ const content = readFileSync(gitPath, "utf-8").trim();
26
+ const match = content.match(/^gitdir:\s*(.+)/);
27
+ if (!match)
28
+ return null;
29
+ const gitdir = match[1].trim();
30
+ if (gitdir.includes("/worktrees/")) {
31
+ const commonPrefix = gitdir.replace(/\/worktrees\/[^/]+\/?$/, "");
32
+ let rootDir = commonPrefix;
33
+ if (rootDir.endsWith("/.git"))
34
+ rootDir = rootDir.slice(0, -4);
35
+ if (!existsSync(join(rootDir, ".git")))
36
+ return null;
37
+ return realpathSync(rootDir);
38
+ }
39
+ const parent = dirname(gitdir);
40
+ if (!existsSync(parent))
41
+ return null;
42
+ return parent;
43
+ }
44
+ return null;
45
+ }
46
+ }
47
+ function readFilteredWorkingDir(git, cwd) {
48
+ const all = git.scanWorkingDir(cwd);
49
+ const filtered = new Map();
50
+ for (const [path, content] of all) {
51
+ if (content.length > FILE_SIZE_LIMIT)
52
+ continue;
53
+ filtered.set(path, content);
54
+ }
55
+ return filtered;
56
+ }
57
+ function generateUnifiedDiff(oldContent, newContent, filePath) {
58
+ const oldLines = oldContent === null ? [] : oldContent.split("\n");
59
+ const newLines = newContent === null ? [] : newContent.split("\n");
60
+ const lines = [];
61
+ lines.push(`--- ${filePath}`);
62
+ lines.push(`+++ ${filePath}`);
63
+ let i = 0;
64
+ let j = 0;
65
+ const hunks = [];
66
+ while (i < oldLines.length || j < newLines.length) {
67
+ if (i < oldLines.length && j < newLines.length && oldLines[i] === newLines[j]) {
68
+ i++;
69
+ j++;
70
+ }
71
+ else {
72
+ const hunkStart = Math.max(0, i - 3);
73
+ const hunkOldEnd = Math.min(oldLines.length, i + 3);
74
+ const hunkNewEnd = Math.min(newLines.length, j + 3);
75
+ const removed = [];
76
+ const added = [];
77
+ let oi = i;
78
+ let ni = j;
79
+ while (oi < hunkOldEnd || ni < hunkNewEnd) {
80
+ if (oi < hunkOldEnd && ni < hunkNewEnd && oldLines[oi] === newLines[ni]) {
81
+ break;
82
+ }
83
+ if (oi < hunkOldEnd) {
84
+ removed.push(oldLines[oi]);
85
+ oi++;
86
+ }
87
+ if (ni < hunkNewEnd && (oi >= hunkOldEnd || newLines[ni] !== oldLines[oi])) {
88
+ added.push(newLines[ni]);
89
+ ni++;
90
+ }
91
+ else if (ni < hunkNewEnd) {
92
+ break;
93
+ }
94
+ }
95
+ const contextStart = Math.max(0, i - 2);
96
+ const contextLines = [];
97
+ for (let c = contextStart; c < i; c++) {
98
+ contextLines.push(` ${oldLines[c]}`);
99
+ }
100
+ for (const r of removed) {
101
+ contextLines.push(`-${r}`);
102
+ }
103
+ for (const a of added) {
104
+ contextLines.push(`+${a}`);
105
+ }
106
+ const contextEnd = Math.min(newLines.length, ni + 2);
107
+ for (let c = ni; c < contextEnd; c++) {
108
+ contextLines.push(` ${newLines[c]}`);
109
+ }
110
+ hunks.push(`@@ -${i + 1},${oi - i} +${j + 1},${ni - j} @@`);
111
+ hunks.push(...contextLines);
112
+ i = oi;
113
+ j = ni;
114
+ }
115
+ }
116
+ if (hunks.length === 0)
117
+ return "";
118
+ lines.push(...hunks);
119
+ return lines.join("\n");
120
+ }
121
+ export class FileSnapshotManager {
122
+ git;
123
+ sessionStartTreeHash = null;
124
+ lastCommittedTreeHash = null;
125
+ turnIndex = 0;
126
+ snapshotIndex = new Map();
127
+ turnIndexMap = new Map();
128
+ constructor(git) {
129
+ this.git = git;
130
+ }
131
+ async initialize(cwd) {
132
+ this.sessionStartTreeHash = null;
133
+ this.lastCommittedTreeHash = null;
134
+ this.turnIndex = 0;
135
+ this.snapshotIndex.clear();
136
+ this.turnIndexMap.clear();
137
+ const files = readFilteredWorkingDir(this.git, cwd);
138
+ if (files.size > 0) {
139
+ this.sessionStartTreeHash = this.git.writeTree(files).treeHash;
140
+ }
141
+ }
142
+ onTurnEnd(cwd, turnIndex, appendEntry) {
143
+ const files = readFilteredWorkingDir(this.git, cwd);
144
+ const { treeHash: snapshotTreeHash, entries: newEntries } = this.git.writeTree(files);
145
+ const compareTo = this.lastCommittedTreeHash ?? this.sessionStartTreeHash;
146
+ const oldEntries = compareTo ? this.parseTreeEntriesFromHash(compareTo) : new Map();
147
+ const stepDiff = this.git.computeDiff(oldEntries, newEntries);
148
+ const hasChanges = stepDiff.added.length > 0 || stepDiff.modified.length > 0 || stepDiff.deleted.length > 0;
149
+ if (hasChanges) {
150
+ const entryId = appendEntry("step-snapshot", {
151
+ baselineTreeHash: compareTo,
152
+ snapshotTreeHash,
153
+ diff: stepDiff,
154
+ turnIndex,
155
+ });
156
+ if (entryId) {
157
+ this.snapshotIndex.set(entryId, {
158
+ baselineTreeHash: compareTo,
159
+ snapshotTreeHash,
160
+ diff: stepDiff,
161
+ turnIndex,
162
+ entryId,
163
+ });
164
+ this.turnIndexMap.set(turnIndex, entryId);
165
+ }
166
+ this.lastCommittedTreeHash = snapshotTreeHash;
167
+ }
168
+ this.turnIndex = turnIndex + 1;
169
+ }
170
+ rebuildIndex(entries) {
171
+ this.snapshotIndex.clear();
172
+ this.turnIndexMap.clear();
173
+ this.lastCommittedTreeHash = null;
174
+ this.sessionStartTreeHash = null;
175
+ this.turnIndex = 0;
176
+ for (const entry of entries) {
177
+ if (entry.type !== "custom")
178
+ continue;
179
+ const custom = entry;
180
+ if (custom.customType !== "step-snapshot")
181
+ continue;
182
+ const data = custom.data;
183
+ if (!data)
184
+ continue;
185
+ this.snapshotIndex.set(entry.id, {
186
+ ...data,
187
+ entryId: entry.id,
188
+ });
189
+ this.turnIndexMap.set(data.turnIndex, entry.id);
190
+ this.lastCommittedTreeHash = data.snapshotTreeHash;
191
+ this.turnIndex = Math.max(this.turnIndex, data.turnIndex + 1);
192
+ }
193
+ }
194
+ getLatestSnapshotOnPath(entries, leafId) {
195
+ if (!leafId)
196
+ return null;
197
+ const byId = new Map();
198
+ for (const entry of entries) {
199
+ byId.set(entry.id, entry);
200
+ }
201
+ const snapshots = [];
202
+ for (const entry of entries) {
203
+ if (entry.type !== "custom")
204
+ continue;
205
+ const custom = entry;
206
+ if (custom.customType !== "step-snapshot")
207
+ continue;
208
+ if (!isOnPathTo(byId, leafId, entry.id))
209
+ continue;
210
+ snapshots.push(custom.data);
211
+ }
212
+ return snapshots.length > 0 ? snapshots[snapshots.length - 1] : null;
213
+ }
214
+ getSnapshotAtTurn(turnIndex) {
215
+ const entryId = this.turnIndexMap.get(turnIndex);
216
+ if (!entryId)
217
+ return null;
218
+ const snap = this.snapshotIndex.get(entryId);
219
+ if (!snap)
220
+ return null;
221
+ return {
222
+ baselineTreeHash: snap.baselineTreeHash,
223
+ snapshotTreeHash: snap.snapshotTreeHash,
224
+ diff: snap.diff,
225
+ turnIndex: snap.turnIndex,
226
+ };
227
+ }
228
+ getSnapshotAtEntry(entryId) {
229
+ const snap = this.snapshotIndex.get(entryId);
230
+ if (!snap)
231
+ return null;
232
+ return {
233
+ baselineTreeHash: snap.baselineTreeHash,
234
+ snapshotTreeHash: snap.snapshotTreeHash,
235
+ diff: snap.diff,
236
+ turnIndex: snap.turnIndex,
237
+ };
238
+ }
239
+ getModifiedFiles(options) {
240
+ const snapshots = [...this.snapshotIndex.values()].sort((a, b) => a.turnIndex - b.turnIndex);
241
+ const fromIdx = options?.fromEntryId ? snapshots.findIndex((s) => s.entryId === options.fromEntryId) : -1;
242
+ const toIdx = options?.toEntryId ? snapshots.findIndex((s) => s.entryId === options.toEntryId) : -1;
243
+ const start = fromIdx === -1 ? 0 : fromIdx;
244
+ const end = toIdx === -1 ? snapshots.length - 1 : toIdx;
245
+ if (start > end || snapshots.length === 0)
246
+ return [];
247
+ const fileMap = new Map();
248
+ for (let i = start; i <= end; i++) {
249
+ const snap = snapshots[i];
250
+ if (!snap?.diff)
251
+ continue;
252
+ for (const path of snap.diff.added) {
253
+ if (!fileMap.has(path)) {
254
+ fileMap.set(path, { path, status: "added", turnIndex: snap.turnIndex, entryId: snap.entryId });
255
+ }
256
+ }
257
+ for (const path of snap.diff.modified) {
258
+ if (!fileMap.has(path)) {
259
+ fileMap.set(path, { path, status: "modified", turnIndex: snap.turnIndex, entryId: snap.entryId });
260
+ }
261
+ else {
262
+ const existing = fileMap.get(path);
263
+ if (existing.status !== "added") {
264
+ existing.status = "modified";
265
+ }
266
+ }
267
+ }
268
+ for (const path of snap.diff.deleted) {
269
+ if (!fileMap.has(path)) {
270
+ fileMap.set(path, { path, status: "deleted", turnIndex: snap.turnIndex, entryId: snap.entryId });
271
+ }
272
+ }
273
+ }
274
+ return [...fileMap.values()].sort((a, b) => a.path.localeCompare(b.path));
275
+ }
276
+ getFileDiff(options) {
277
+ const snapshots = [...this.snapshotIndex.values()].sort((a, b) => a.turnIndex - b.turnIndex);
278
+ const fromHash = options.fromEntryId
279
+ ? (snapshots.find((s) => s.entryId === options.fromEntryId)?.snapshotTreeHash ?? null)
280
+ : this.sessionStartTreeHash;
281
+ const toHash = options.toEntryId
282
+ ? (snapshots.find((s) => s.entryId === options.toEntryId)?.snapshotTreeHash ?? null)
283
+ : (this.lastCommittedTreeHash ?? this.sessionStartTreeHash);
284
+ if (!fromHash && !toHash)
285
+ return null;
286
+ const fromFiles = fromHash ? this.git.readTree(fromHash) : new Map();
287
+ const toFiles = toHash ? this.git.readTree(toHash) : new Map();
288
+ const oldContent = fromFiles.get(options.filePath) ?? null;
289
+ const newContent = toFiles.get(options.filePath) ?? null;
290
+ if (oldContent === null && newContent === null)
291
+ return null;
292
+ return {
293
+ path: options.filePath,
294
+ oldContent,
295
+ newContent,
296
+ oldHash: oldContent !== null ? this.git.hashContent(oldContent) : null,
297
+ newHash: newContent !== null ? this.git.hashContent(newContent) : null,
298
+ unifiedDiff: generateUnifiedDiff(oldContent, newContent, options.filePath),
299
+ };
300
+ }
301
+ getBatchDiffs(options) {
302
+ const files = this.getModifiedFiles({
303
+ fromEntryId: options?.fromEntryId,
304
+ toEntryId: options?.toEntryId,
305
+ });
306
+ let added = 0;
307
+ let modified = 0;
308
+ let deleted = 0;
309
+ const resultFiles = files.map((f) => {
310
+ if (f.status === "added")
311
+ added++;
312
+ else if (f.status === "modified")
313
+ modified++;
314
+ else
315
+ deleted++;
316
+ let diff = null;
317
+ try {
318
+ diff = this.getFileDiff({
319
+ filePath: f.path,
320
+ fromEntryId: options?.fromEntryId,
321
+ toEntryId: options?.toEntryId,
322
+ });
323
+ }
324
+ catch { }
325
+ return { path: f.path, status: f.status, diff };
326
+ });
327
+ return {
328
+ files: resultFiles,
329
+ summary: {
330
+ totalFiles: files.length,
331
+ added,
332
+ modified,
333
+ deleted,
334
+ },
335
+ };
336
+ }
337
+ getFileHistory(options) {
338
+ const snapshots = [...this.snapshotIndex.values()].sort((a, b) => a.turnIndex - b.turnIndex);
339
+ const history = [];
340
+ for (const snap of snapshots) {
341
+ if (!snap.diff)
342
+ continue;
343
+ let status = null;
344
+ if (snap.diff.added.includes(options.filePath)) {
345
+ status = "added";
346
+ }
347
+ else if (snap.diff.modified.includes(options.filePath)) {
348
+ status = "modified";
349
+ }
350
+ else if (snap.diff.deleted.includes(options.filePath)) {
351
+ status = "deleted";
352
+ }
353
+ if (status) {
354
+ const previousHash = snap.baselineTreeHash ?? null;
355
+ history.push({
356
+ entryId: snap.entryId,
357
+ turnIndex: snap.turnIndex,
358
+ timestamp: new Date(0).toISOString(),
359
+ status,
360
+ snapshotHash: snap.snapshotTreeHash,
361
+ previousHash,
362
+ });
363
+ }
364
+ }
365
+ return history;
366
+ }
367
+ async restoreFiles(cwd, options) {
368
+ const empty = { restored: [], deleted: [], skipped: [], dirty: [] };
369
+ let targetTreeHash;
370
+ if (options.snapshotHash) {
371
+ targetTreeHash = options.snapshotHash;
372
+ }
373
+ else if (options.targetEntryId) {
374
+ const snap = this.snapshotIndex.get(options.targetEntryId);
375
+ if (snap) {
376
+ targetTreeHash = snap.snapshotTreeHash;
377
+ }
378
+ else {
379
+ const pathSnap = this.getLatestSnapshotOnPath(options.entries, options.targetEntryId);
380
+ targetTreeHash = pathSnap?.snapshotTreeHash ?? null;
381
+ }
382
+ }
383
+ else {
384
+ targetTreeHash = this.sessionStartTreeHash ?? null;
385
+ }
386
+ let currentTreeHash;
387
+ if (options.currentLeafId !== undefined) {
388
+ const currentSnapshot = this.getLatestSnapshotOnPath(options.entries, options.currentLeafId);
389
+ currentTreeHash = currentSnapshot?.snapshotTreeHash ?? null;
390
+ }
391
+ else {
392
+ currentTreeHash = this.lastCommittedTreeHash ?? this.sessionStartTreeHash;
393
+ }
394
+ if (targetTreeHash === currentTreeHash)
395
+ return empty;
396
+ const targetFiles = targetTreeHash ? this.git.readTree(targetTreeHash) : new Map();
397
+ const currentFiles = currentTreeHash ? this.git.readTree(currentTreeHash) : new Map();
398
+ const toRestore = [];
399
+ for (const [path, content] of targetFiles) {
400
+ const current = currentFiles.get(path);
401
+ if (current !== content) {
402
+ toRestore.push(path);
403
+ }
404
+ }
405
+ const toDelete = [];
406
+ for (const path of currentFiles.keys()) {
407
+ if (!targetFiles.has(path)) {
408
+ toDelete.push(path);
409
+ }
410
+ }
411
+ let filteredRestore = toRestore;
412
+ let filteredDelete = toDelete;
413
+ if (options.files) {
414
+ const fileSet = new Set(options.files);
415
+ filteredRestore = toRestore.filter((p) => fileSet.has(p));
416
+ filteredDelete = toDelete.filter((p) => fileSet.has(p));
417
+ }
418
+ if (filteredRestore.length === 0 && filteredDelete.length === 0)
419
+ return empty;
420
+ const dirty = [];
421
+ for (const path of filteredRestore) {
422
+ const absPath = join(cwd, path);
423
+ if (existsSync(absPath)) {
424
+ const expectedContent = currentFiles.get(path);
425
+ const expectedHash = expectedContent !== undefined ? this.git.hashContent(expectedContent) : null;
426
+ let actualContent = null;
427
+ try {
428
+ const stat = lstatSync(absPath);
429
+ if (stat.size <= FILE_SIZE_LIMIT) {
430
+ actualContent = readFileSync(absPath, "utf-8");
431
+ }
432
+ }
433
+ catch {
434
+ continue;
435
+ }
436
+ const actualHash = actualContent !== null ? this.git.hashContent(actualContent) : null;
437
+ if (expectedHash !== null && actualHash !== expectedHash) {
438
+ dirty.push(path);
439
+ }
440
+ }
441
+ }
442
+ dirty.sort();
443
+ if (options.preview) {
444
+ return {
445
+ restored: filteredRestore.sort(),
446
+ deleted: filteredDelete.sort(),
447
+ skipped: [],
448
+ dirty,
449
+ };
450
+ }
451
+ const preRollbackFiles = readFilteredWorkingDir(this.git, cwd);
452
+ const preRollbackTreeHash = preRollbackFiles.size > 0 ? this.git.writeTree(preRollbackFiles).treeHash : null;
453
+ options.appendEntry("unrevert-point", {
454
+ preRollbackTreeHash,
455
+ rolledBackToLeaf: options.targetEntryId ?? "",
456
+ restoredFiles: filteredRestore,
457
+ });
458
+ for (const path of filteredRestore) {
459
+ const content = targetFiles.get(path);
460
+ if (content === undefined)
461
+ continue;
462
+ const absPath = join(cwd, path);
463
+ mkdirSync(dirname(absPath), { recursive: true });
464
+ writeFileSync(absPath, content, "utf-8");
465
+ }
466
+ for (const path of filteredDelete) {
467
+ const absPath = join(cwd, path);
468
+ if (existsSync(absPath)) {
469
+ rmSync(absPath, { force: true });
470
+ }
471
+ }
472
+ this.lastCommittedTreeHash = targetTreeHash;
473
+ return {
474
+ restored: filteredRestore.sort(),
475
+ deleted: filteredDelete.sort(),
476
+ skipped: [],
477
+ dirty,
478
+ };
479
+ }
480
+ parseTreeEntriesFromHash(treeHash) {
481
+ const treeData = this.git.readObject(treeHash);
482
+ const entries = new Map();
483
+ for (const line of treeData.split("\n")) {
484
+ if (!line)
485
+ continue;
486
+ const sep = line.indexOf("\0");
487
+ if (sep === -1)
488
+ continue;
489
+ const path = line.slice(0, sep);
490
+ const hash = line.slice(sep + 1);
491
+ entries.set(path, { path, hash });
492
+ }
493
+ return entries;
494
+ }
495
+ }
496
+ function isOnPathTo(byId, startId, targetId) {
497
+ let current = startId;
498
+ while (current !== null) {
499
+ if (current === targetId)
500
+ return true;
501
+ const entry = byId.get(current);
502
+ if (!entry)
503
+ break;
504
+ current = entry.parentId;
505
+ }
506
+ return false;
507
+ }
508
+ //# sourceMappingURL=file-snapshot-manager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file-snapshot-manager.js","sourceRoot":"","sources":["../../../src/core/file-store/file-snapshot-manager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,SAAS,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC9G,OAAO,EAAE,OAAO,EAAE,IAAI,EAAY,MAAM,WAAW,CAAC;AAkCpD,MAAM,eAAe,GAAG,IAAI,GAAG,IAAI,CAAC;AAEpC,SAAS,oBAAoB,CAAC,GAAW,EAAiB;IACzD,IAAI,GAAW,CAAC;IAChB,IAAI,CAAC;QACJ,GAAG,GAAG,YAAY,CAAC,GAAG,CAAC,CAAC;IACzB,CAAC;IAAC,MAAM,CAAC;QACR,OAAO,IAAI,CAAC;IACb,CAAC;IACD,SAAS,CAAC;QACT,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;QAClC,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE,CAAC;YAC1B,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;YAC5B,IAAI,MAA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I,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;QACnC,CAAC;QACD,OAAO,OAAO,CAAC;IAAA,CACf;CACD;AAED,SAAS,UAAU,CAAC,IAA+B,EAAE,OAAe,EAAE,QAAgB,EAAW;IAChG,IAAI,OAAO,GAAkB,OAAO,CAAC;IACrC,OAAO,OAAO,KAAK,IAAI,EAAE,CAAC;QACzB,IAAI,OAAO,KAAK,QAAQ;YAAE,OAAO,IAAI,CAAC;QACtC,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QAChC,IAAI,CAAC,KAAK;YAAE,MAAM;QAClB,OAAO,GAAG,KAAK,CAAC,QAAQ,CAAC;IAC1B,CAAC;IACD,OAAO,KAAK,CAAC;AAAA,CACb","sourcesContent":["import { existsSync, lstatSync, mkdirSync, readFileSync, realpathSync, rmSync, writeFileSync } from \"node:fs\";\nimport { dirname, join, relative } from \"node:path\";\nimport type { CustomEntry, SessionEntry } from \"../session-manager.js\";\nimport type { InternalGit, TreeEntry } from \"./internal-git.js\";\n\nexport interface StepSnapshotData {\n\tbaselineTreeHash: string | null;\n\tsnapshotTreeHash: string;\n\tdiff: { added: string[]; modified: string[]; deleted: string[] } | null;\n\tturnIndex: number;\n}\n\nexport interface ModifiedFileInfo {\n\tpath: string;\n\tstatus: \"added\" | \"modified\" | \"deleted\";\n\tturnIndex: number;\n\tentryId: string;\n}\n\nexport interface FileDiffInfo {\n\tpath: string;\n\toldContent: string | null;\n\tnewContent: string | null;\n\toldHash: string | null;\n\tnewHash: string | null;\n\tunifiedDiff: string;\n}\n\nexport interface RestoreResult {\n\trestored: string[];\n\tdeleted: string[];\n\tskipped: string[];\n\tdirty: string[];\n}\n\nconst FILE_SIZE_LIMIT = 1024 * 1024;\n\nfunction findCanonicalGitRoot(cwd: string): string | null {\n\tlet dir: string;\n\ttry {\n\t\tdir = realpathSync(cwd);\n\t} catch {\n\t\treturn null;\n\t}\n\tfor (;;) {\n\t\tconst gitPath = join(dir, \".git\");\n\t\tif (!existsSync(gitPath)) {\n\t\t\tconst parent = dirname(dir);\n\t\t\tif (parent === dir) return null;\n\t\t\tdir = parent;\n\t\t\tcontinue;\n\t\t}\n\t\tconst stat = lstatSync(gitPath);\n\t\tif (stat.isDirectory()) return dir;\n\t\tif (stat.isFile()) {\n\t\t\tconst content = readFileSync(gitPath, \"utf-8\").trim();\n\t\t\tconst match = content.match(/^gitdir:\\s*(.+)/);\n\t\t\tif (!match) return null;\n\t\t\tconst gitdir = match[1]!.trim();\n\t\t\tif (gitdir.includes(\"/worktrees/\")) {\n\t\t\t\tconst commonPrefix = gitdir.replace(/\\/worktrees\\/[^/]+\\/?$/, \"\");\n\t\t\t\tlet rootDir = commonPrefix;\n\t\t\t\tif (rootDir.endsWith(\"/.git\")) rootDir = rootDir.slice(0, -4);\n\t\t\t\tif (!existsSync(join(rootDir, \".git\"))) return null;\n\t\t\t\treturn realpathSync(rootDir);\n\t\t\t}\n\t\t\tconst parent = dirname(gitdir);\n\t\t\tif (!existsSync(parent)) return null;\n\t\t\treturn parent;\n\t\t}\n\t\treturn null;\n\t}\n}\n\nfunction readFilteredWorkingDir(git: InternalGit, cwd: string): Map<string, string> {\n\tconst all = git.scanWorkingDir(cwd);\n\tconst filtered = new Map<string, string>();\n\tfor (const [path, content] of all) {\n\t\tif (content.length > FILE_SIZE_LIMIT) continue;\n\t\tfiltered.set(path, content);\n\t}\n\treturn filtered;\n}\n\nfunction generateUnifiedDiff(oldContent: string | null, newContent: string | null, filePath: string): string {\n\tconst oldLines = oldContent === null ? [] : oldContent.split(\"\\n\");\n\tconst newLines = newContent === null ? [] : newContent.split(\"\\n\");\n\tconst lines: string[] = [];\n\tlines.push(`--- ${filePath}`);\n\tlines.push(`+++ ${filePath}`);\n\n\tlet i = 0;\n\tlet j = 0;\n\tconst hunks: string[] = [];\n\n\twhile (i < oldLines.length || j < newLines.length) {\n\t\tif (i < oldLines.length && j < newLines.length && oldLines[i] === newLines[j]) {\n\t\t\ti++;\n\t\t\tj++;\n\t\t} else {\n\t\t\tconst hunkStart = Math.max(0, i - 3);\n\t\t\tconst hunkOldEnd = Math.min(oldLines.length, i + 3);\n\t\t\tconst hunkNewEnd = Math.min(newLines.length, j + 3);\n\n\t\t\tconst removed: string[] = [];\n\t\t\tconst added: string[] = [];\n\t\t\tlet oi = i;\n\t\t\tlet ni = j;\n\n\t\t\twhile (oi < hunkOldEnd || ni < hunkNewEnd) {\n\t\t\t\tif (oi < hunkOldEnd && ni < hunkNewEnd && oldLines[oi] === newLines[ni]) {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tif (oi < hunkOldEnd) {\n\t\t\t\t\tremoved.push(oldLines[oi]);\n\t\t\t\t\toi++;\n\t\t\t\t}\n\t\t\t\tif (ni < hunkNewEnd && (oi >= hunkOldEnd || newLines[ni] !== oldLines[oi])) {\n\t\t\t\t\tadded.push(newLines[ni]);\n\t\t\t\t\tni++;\n\t\t\t\t} else if (ni < hunkNewEnd) {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tconst contextStart = Math.max(0, i - 2);\n\t\t\tconst contextLines: string[] = [];\n\t\t\tfor (let c = contextStart; c < i; c++) {\n\t\t\t\tcontextLines.push(` ${oldLines[c]}`);\n\t\t\t}\n\t\t\tfor (const r of removed) {\n\t\t\t\tcontextLines.push(`-${r}`);\n\t\t\t}\n\t\t\tfor (const a of added) {\n\t\t\t\tcontextLines.push(`+${a}`);\n\t\t\t}\n\t\t\tconst contextEnd = Math.min(newLines.length, ni + 2);\n\t\t\tfor (let c = ni; c < contextEnd; c++) {\n\t\t\t\tcontextLines.push(` ${newLines[c]}`);\n\t\t\t}\n\n\t\t\thunks.push(`@@ -${i + 1},${oi - i} +${j + 1},${ni - j} @@`);\n\t\t\thunks.push(...contextLines);\n\n\t\t\ti = oi;\n\t\t\tj = ni;\n\t\t}\n\t}\n\n\tif (hunks.length === 0) return \"\";\n\tlines.push(...hunks);\n\treturn lines.join(\"\\n\");\n}\n\nexport interface BatchDiffResult {\n\tfiles: Array<{\n\t\tpath: string;\n\t\tstatus: \"added\" | \"modified\" | \"deleted\";\n\t\tdiff: FileDiffInfo | null;\n\t}>;\n\tsummary: {\n\t\ttotalFiles: number;\n\t\tadded: number;\n\t\tmodified: number;\n\t\tdeleted: number;\n\t};\n}\n\nexport interface FileHistoryEntry {\n\tentryId: string;\n\tturnIndex: number;\n\ttimestamp: string;\n\tstatus: \"added\" | \"modified\" | \"deleted\";\n\tsnapshotHash: string;\n\tpreviousHash: string | null;\n}\n\ninterface SnapshotWithEntryId extends StepSnapshotData {\n\tentryId: string;\n}\n\nexport class FileSnapshotManager {\n\tprivate git: InternalGit;\n\tprivate sessionStartTreeHash: string | null = null;\n\tprivate lastCommittedTreeHash: string | null = null;\n\tprivate turnIndex = 0;\n\tprivate snapshotIndex: Map<string, SnapshotWithEntryId> = new Map();\n\tprivate turnIndexMap: Map<number, string> = new Map();\n\n\tconstructor(git: InternalGit) {\n\t\tthis.git = git;\n\t}\n\n\tasync initialize(cwd: string): Promise<void> {\n\t\tthis.sessionStartTreeHash = null;\n\t\tthis.lastCommittedTreeHash = null;\n\t\tthis.turnIndex = 0;\n\t\tthis.snapshotIndex.clear();\n\t\tthis.turnIndexMap.clear();\n\n\t\tconst files = readFilteredWorkingDir(this.git, cwd);\n\t\tif (files.size > 0) {\n\t\t\tthis.sessionStartTreeHash = this.git.writeTree(files).treeHash;\n\t\t}\n\t}\n\n\tonTurnEnd(cwd: string, turnIndex: number, appendEntry: (type: string, data: unknown) => string | undefined): void {\n\t\tconst files = readFilteredWorkingDir(this.git, cwd);\n\t\tconst { treeHash: snapshotTreeHash, entries: newEntries } = this.git.writeTree(files);\n\n\t\tconst compareTo = this.lastCommittedTreeHash ?? this.sessionStartTreeHash;\n\t\tconst oldEntries = compareTo ? this.parseTreeEntriesFromHash(compareTo) : new Map<string, TreeEntry>();\n\n\t\tconst stepDiff = this.git.computeDiff(oldEntries, newEntries);\n\t\tconst hasChanges = stepDiff.added.length > 0 || stepDiff.modified.length > 0 || stepDiff.deleted.length > 0;\n\n\t\tif (hasChanges) {\n\t\t\tconst entryId = appendEntry(\"step-snapshot\", {\n\t\t\t\tbaselineTreeHash: compareTo,\n\t\t\t\tsnapshotTreeHash,\n\t\t\t\tdiff: stepDiff,\n\t\t\t\tturnIndex,\n\t\t\t});\n\n\t\t\tif (entryId) {\n\t\t\t\tthis.snapshotIndex.set(entryId, {\n\t\t\t\t\tbaselineTreeHash: compareTo,\n\t\t\t\t\tsnapshotTreeHash,\n\t\t\t\t\tdiff: stepDiff,\n\t\t\t\t\tturnIndex,\n\t\t\t\t\tentryId,\n\t\t\t\t});\n\t\t\t\tthis.turnIndexMap.set(turnIndex, entryId);\n\t\t\t}\n\t\t\tthis.lastCommittedTreeHash = snapshotTreeHash;\n\t\t}\n\n\t\tthis.turnIndex = turnIndex + 1;\n\t}\n\n\trebuildIndex(entries: SessionEntry[]): void {\n\t\tthis.snapshotIndex.clear();\n\t\tthis.turnIndexMap.clear();\n\t\tthis.lastCommittedTreeHash = null;\n\t\tthis.sessionStartTreeHash = null;\n\t\tthis.turnIndex = 0;\n\n\t\tfor (const entry of entries) {\n\t\t\tif (entry.type !== \"custom\") continue;\n\t\t\tconst custom = entry as CustomEntry;\n\t\t\tif (custom.customType !== \"step-snapshot\") continue;\n\n\t\t\tconst data = custom.data as StepSnapshotData;\n\t\t\tif (!data) continue;\n\n\t\t\tthis.snapshotIndex.set(entry.id, {\n\t\t\t\t...data,\n\t\t\t\tentryId: entry.id,\n\t\t\t});\n\t\t\tthis.turnIndexMap.set(data.turnIndex, entry.id);\n\t\t\tthis.lastCommittedTreeHash = data.snapshotTreeHash;\n\t\t\tthis.turnIndex = Math.max(this.turnIndex, data.turnIndex + 1);\n\t\t}\n\t}\n\n\tgetLatestSnapshotOnPath(entries: SessionEntry[], leafId: string | null): StepSnapshotData | null {\n\t\tif (!leafId) return null;\n\n\t\tconst byId = new Map<string, SessionEntry>();\n\t\tfor (const entry of entries) {\n\t\t\tbyId.set(entry.id, entry);\n\t\t}\n\n\t\tconst snapshots: StepSnapshotData[] = [];\n\t\tfor (const entry of entries) {\n\t\t\tif (entry.type !== \"custom\") continue;\n\t\t\tconst custom = entry as CustomEntry;\n\t\t\tif (custom.customType !== \"step-snapshot\") continue;\n\t\t\tif (!isOnPathTo(byId, leafId, entry.id)) continue;\n\t\t\tsnapshots.push(custom.data as StepSnapshotData);\n\t\t}\n\n\t\treturn snapshots.length > 0 ? snapshots[snapshots.length - 1] : null;\n\t}\n\n\tgetSnapshotAtTurn(turnIndex: number): StepSnapshotData | null {\n\t\tconst entryId = this.turnIndexMap.get(turnIndex);\n\t\tif (!entryId) return null;\n\t\tconst snap = this.snapshotIndex.get(entryId);\n\t\tif (!snap) return null;\n\t\treturn {\n\t\t\tbaselineTreeHash: snap.baselineTreeHash,\n\t\t\tsnapshotTreeHash: snap.snapshotTreeHash,\n\t\t\tdiff: snap.diff,\n\t\t\tturnIndex: snap.turnIndex,\n\t\t};\n\t}\n\n\tgetSnapshotAtEntry(entryId: string): StepSnapshotData | null {\n\t\tconst snap = this.snapshotIndex.get(entryId);\n\t\tif (!snap) return null;\n\t\treturn {\n\t\t\tbaselineTreeHash: snap.baselineTreeHash,\n\t\t\tsnapshotTreeHash: snap.snapshotTreeHash,\n\t\t\tdiff: snap.diff,\n\t\t\tturnIndex: snap.turnIndex,\n\t\t};\n\t}\n\n\tgetModifiedFiles(options?: { fromEntryId?: string; toEntryId?: string }): ModifiedFileInfo[] {\n\t\tconst snapshots = [...this.snapshotIndex.values()].sort((a, b) => a.turnIndex - b.turnIndex);\n\n\t\tconst fromIdx = options?.fromEntryId ? snapshots.findIndex((s) => s.entryId === options.fromEntryId) : -1;\n\t\tconst toIdx = options?.toEntryId ? snapshots.findIndex((s) => s.entryId === options.toEntryId) : -1;\n\n\t\tconst start = fromIdx === -1 ? 0 : fromIdx;\n\t\tconst end = toIdx === -1 ? snapshots.length - 1 : toIdx;\n\n\t\tif (start > end || snapshots.length === 0) return [];\n\n\t\tconst fileMap = new Map<string, ModifiedFileInfo>();\n\t\tfor (let i = start; i <= end; i++) {\n\t\t\tconst snap = snapshots[i];\n\t\t\tif (!snap?.diff) continue;\n\n\t\t\tfor (const path of snap.diff.added) {\n\t\t\t\tif (!fileMap.has(path)) {\n\t\t\t\t\tfileMap.set(path, { path, status: \"added\", turnIndex: snap.turnIndex, entryId: snap.entryId });\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (const path of snap.diff.modified) {\n\t\t\t\tif (!fileMap.has(path)) {\n\t\t\t\t\tfileMap.set(path, { path, status: \"modified\", turnIndex: snap.turnIndex, entryId: snap.entryId });\n\t\t\t\t} else {\n\t\t\t\t\tconst existing = fileMap.get(path)!;\n\t\t\t\t\tif (existing.status !== \"added\") {\n\t\t\t\t\t\texisting.status = \"modified\";\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (const path of snap.diff.deleted) {\n\t\t\t\tif (!fileMap.has(path)) {\n\t\t\t\t\tfileMap.set(path, { path, status: \"deleted\", turnIndex: snap.turnIndex, entryId: snap.entryId });\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn [...fileMap.values()].sort((a, b) => a.path.localeCompare(b.path));\n\t}\n\n\tgetFileDiff(options: { filePath: string; fromEntryId?: string; toEntryId?: string }): FileDiffInfo | null {\n\t\tconst snapshots = [...this.snapshotIndex.values()].sort((a, b) => a.turnIndex - b.turnIndex);\n\n\t\tconst fromHash = options.fromEntryId\n\t\t\t? (snapshots.find((s) => s.entryId === options.fromEntryId)?.snapshotTreeHash ?? null)\n\t\t\t: this.sessionStartTreeHash;\n\t\tconst toHash = options.toEntryId\n\t\t\t? (snapshots.find((s) => s.entryId === options.toEntryId)?.snapshotTreeHash ?? null)\n\t\t\t: (this.lastCommittedTreeHash ?? this.sessionStartTreeHash);\n\n\t\tif (!fromHash && !toHash) return null;\n\n\t\tconst fromFiles = fromHash ? this.git.readTree(fromHash) : new Map<string, string>();\n\t\tconst toFiles = toHash ? this.git.readTree(toHash) : new Map<string, string>();\n\n\t\tconst oldContent = fromFiles.get(options.filePath) ?? null;\n\t\tconst newContent = toFiles.get(options.filePath) ?? null;\n\n\t\tif (oldContent === null && newContent === null) return null;\n\n\t\treturn {\n\t\t\tpath: options.filePath,\n\t\t\toldContent,\n\t\t\tnewContent,\n\t\t\toldHash: oldContent !== null ? this.git.hashContent(oldContent) : null,\n\t\t\tnewHash: newContent !== null ? this.git.hashContent(newContent) : null,\n\t\t\tunifiedDiff: generateUnifiedDiff(oldContent, newContent, options.filePath),\n\t\t};\n\t}\n\n\tgetBatchDiffs(options?: { fromEntryId?: string; toEntryId?: string }): BatchDiffResult {\n\t\tconst files = this.getModifiedFiles({\n\t\t\tfromEntryId: options?.fromEntryId,\n\t\t\ttoEntryId: options?.toEntryId,\n\t\t});\n\n\t\tlet added = 0;\n\t\tlet modified = 0;\n\t\tlet deleted = 0;\n\n\t\tconst resultFiles = files.map((f) => {\n\t\t\tif (f.status === \"added\") added++;\n\t\t\telse if (f.status === \"modified\") modified++;\n\t\t\telse deleted++;\n\n\t\t\tlet diff: FileDiffInfo | null = null;\n\t\t\ttry {\n\t\t\t\tdiff = this.getFileDiff({\n\t\t\t\t\tfilePath: f.path,\n\t\t\t\t\tfromEntryId: options?.fromEntryId,\n\t\t\t\t\ttoEntryId: options?.toEntryId,\n\t\t\t\t});\n\t\t\t} catch {}\n\n\t\t\treturn { path: f.path, status: f.status, diff };\n\t\t});\n\n\t\treturn {\n\t\t\tfiles: resultFiles,\n\t\t\tsummary: {\n\t\t\t\ttotalFiles: files.length,\n\t\t\t\tadded,\n\t\t\t\tmodified,\n\t\t\t\tdeleted,\n\t\t\t},\n\t\t};\n\t}\n\n\tgetFileHistory(options: { filePath: string }): FileHistoryEntry[] {\n\t\tconst snapshots = [...this.snapshotIndex.values()].sort((a, b) => a.turnIndex - b.turnIndex);\n\t\tconst history: FileHistoryEntry[] = [];\n\n\t\tfor (const snap of snapshots) {\n\t\t\tif (!snap.diff) continue;\n\n\t\t\tlet status: \"added\" | \"modified\" | \"deleted\" | null = null;\n\t\t\tif (snap.diff.added.includes(options.filePath)) {\n\t\t\t\tstatus = \"added\";\n\t\t\t} else if (snap.diff.modified.includes(options.filePath)) {\n\t\t\t\tstatus = \"modified\";\n\t\t\t} else if (snap.diff.deleted.includes(options.filePath)) {\n\t\t\t\tstatus = \"deleted\";\n\t\t\t}\n\n\t\t\tif (status) {\n\t\t\t\tconst previousHash = snap.baselineTreeHash ?? null;\n\t\t\t\thistory.push({\n\t\t\t\t\tentryId: snap.entryId,\n\t\t\t\t\tturnIndex: snap.turnIndex,\n\t\t\t\t\ttimestamp: new Date(0).toISOString(),\n\t\t\t\t\tstatus,\n\t\t\t\t\tsnapshotHash: snap.snapshotTreeHash,\n\t\t\t\t\tpreviousHash,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\treturn history;\n\t}\n\n\tasync restoreFiles(\n\t\tcwd: string,\n\t\toptions: {\n\t\t\ttargetEntryId?: string;\n\t\t\tsnapshotHash?: string;\n\t\t\tfiles?: string[];\n\t\t\tpreview?: boolean;\n\t\t\tcurrentLeafId?: string | null;\n\t\t\tentries: SessionEntry[];\n\t\t\tappendEntry: (type: string, data: unknown) => void;\n\t\t},\n\t): Promise<RestoreResult> {\n\t\tconst empty: RestoreResult = { restored: [], deleted: [], skipped: [], dirty: [] };\n\n\t\tlet targetTreeHash: string | null;\n\t\tif (options.snapshotHash) {\n\t\t\ttargetTreeHash = options.snapshotHash;\n\t\t} else if (options.targetEntryId) {\n\t\t\tconst snap = this.snapshotIndex.get(options.targetEntryId);\n\t\t\tif (snap) {\n\t\t\t\ttargetTreeHash = snap.snapshotTreeHash;\n\t\t\t} else {\n\t\t\t\tconst pathSnap = this.getLatestSnapshotOnPath(options.entries, options.targetEntryId);\n\t\t\t\ttargetTreeHash = pathSnap?.snapshotTreeHash ?? null;\n\t\t\t}\n\t\t} else {\n\t\t\ttargetTreeHash = this.sessionStartTreeHash ?? null;\n\t\t}\n\n\t\tlet currentTreeHash: string | null;\n\t\tif (options.currentLeafId !== undefined) {\n\t\t\tconst currentSnapshot = this.getLatestSnapshotOnPath(options.entries, options.currentLeafId);\n\t\t\tcurrentTreeHash = currentSnapshot?.snapshotTreeHash ?? null;\n\t\t} else {\n\t\t\tcurrentTreeHash = this.lastCommittedTreeHash ?? this.sessionStartTreeHash;\n\t\t}\n\n\t\tif (targetTreeHash === currentTreeHash) return empty;\n\n\t\tconst targetFiles = targetTreeHash ? this.git.readTree(targetTreeHash) : new Map<string, string>();\n\t\tconst currentFiles = currentTreeHash ? this.git.readTree(currentTreeHash) : new Map<string, string>();\n\n\t\tconst toRestore: string[] = [];\n\t\tfor (const [path, content] of targetFiles) {\n\t\t\tconst current = currentFiles.get(path);\n\t\t\tif (current !== content) {\n\t\t\t\ttoRestore.push(path);\n\t\t\t}\n\t\t}\n\n\t\tconst toDelete: string[] = [];\n\t\tfor (const path of currentFiles.keys()) {\n\t\t\tif (!targetFiles.has(path)) {\n\t\t\t\ttoDelete.push(path);\n\t\t\t}\n\t\t}\n\n\t\tlet filteredRestore = toRestore;\n\t\tlet filteredDelete = toDelete;\n\n\t\tif (options.files) {\n\t\t\tconst fileSet = new Set(options.files);\n\t\t\tfilteredRestore = toRestore.filter((p) => fileSet.has(p));\n\t\t\tfilteredDelete = toDelete.filter((p) => fileSet.has(p));\n\t\t}\n\n\t\tif (filteredRestore.length === 0 && filteredDelete.length === 0) return empty;\n\n\t\tconst dirty: string[] = [];\n\t\tfor (const path of filteredRestore) {\n\t\t\tconst absPath = join(cwd, path);\n\t\t\tif (existsSync(absPath)) {\n\t\t\t\tconst expectedContent = currentFiles.get(path);\n\t\t\t\tconst expectedHash = expectedContent !== undefined ? this.git.hashContent(expectedContent) : null;\n\t\t\t\tlet actualContent: string | null = null;\n\t\t\t\ttry {\n\t\t\t\t\tconst stat = lstatSync(absPath);\n\t\t\t\t\tif (stat.size <= FILE_SIZE_LIMIT) {\n\t\t\t\t\t\tactualContent = readFileSync(absPath, \"utf-8\");\n\t\t\t\t\t}\n\t\t\t\t} catch {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tconst actualHash = actualContent !== null ? this.git.hashContent(actualContent) : null;\n\t\t\t\tif (expectedHash !== null && actualHash !== expectedHash) {\n\t\t\t\t\tdirty.push(path);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tdirty.sort();\n\n\t\tif (options.preview) {\n\t\t\treturn {\n\t\t\t\trestored: filteredRestore.sort(),\n\t\t\t\tdeleted: filteredDelete.sort(),\n\t\t\t\tskipped: [],\n\t\t\t\tdirty,\n\t\t\t};\n\t\t}\n\n\t\tconst preRollbackFiles = readFilteredWorkingDir(this.git, cwd);\n\t\tconst preRollbackTreeHash = preRollbackFiles.size > 0 ? this.git.writeTree(preRollbackFiles).treeHash : null;\n\n\t\toptions.appendEntry(\"unrevert-point\", {\n\t\t\tpreRollbackTreeHash,\n\t\t\trolledBackToLeaf: options.targetEntryId ?? \"\",\n\t\t\trestoredFiles: filteredRestore,\n\t\t});\n\n\t\tfor (const path of filteredRestore) {\n\t\t\tconst content = targetFiles.get(path);\n\t\t\tif (content === undefined) continue;\n\t\t\tconst absPath = join(cwd, path);\n\t\t\tmkdirSync(dirname(absPath), { recursive: true });\n\t\t\twriteFileSync(absPath, content, \"utf-8\");\n\t\t}\n\n\t\tfor (const path of filteredDelete) {\n\t\t\tconst absPath = join(cwd, path);\n\t\t\tif (existsSync(absPath)) {\n\t\t\t\trmSync(absPath, { force: true });\n\t\t\t}\n\t\t}\n\n\t\tthis.lastCommittedTreeHash = targetTreeHash;\n\n\t\treturn {\n\t\t\trestored: filteredRestore.sort(),\n\t\t\tdeleted: filteredDelete.sort(),\n\t\t\tskipped: [],\n\t\t\tdirty,\n\t\t};\n\t}\n\n\tprivate parseTreeEntriesFromHash(treeHash: string): Map<string, TreeEntry> {\n\t\tconst treeData = this.git.readObject(treeHash);\n\t\tconst entries = new Map<string, TreeEntry>();\n\t\tfor (const line of treeData.split(\"\\n\")) {\n\t\t\tif (!line) continue;\n\t\t\tconst sep = line.indexOf(\"\\0\");\n\t\t\tif (sep === -1) continue;\n\t\t\tconst path = line.slice(0, sep);\n\t\t\tconst hash = line.slice(sep + 1);\n\t\t\tentries.set(path, { path, hash });\n\t\t}\n\t\treturn entries;\n\t}\n}\n\nfunction isOnPathTo(byId: Map<string, SessionEntry>, startId: string, targetId: string): boolean {\n\tlet current: string | null = startId;\n\twhile (current !== null) {\n\t\tif (current === targetId) return true;\n\t\tconst entry = byId.get(current);\n\t\tif (!entry) break;\n\t\tcurrent = entry.parentId;\n\t}\n\treturn false;\n}\n"]}
@@ -0,0 +1,5 @@
1
+ export type { FileDiffInfo, ModifiedFileInfo, RestoreResult, StepSnapshotData, } from "./file-snapshot-manager.js";
2
+ export { FileSnapshotManager } from "./file-snapshot-manager.js";
3
+ export type { StepDiff, TreeEntry, TreeSnapshot } from "./internal-git.js";
4
+ export { computeProjectHash, InternalGit } from "./internal-git.js";
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/core/file-store/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACX,YAAY,EACZ,gBAAgB,EAChB,aAAa,EACb,gBAAgB,GAChB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AACjE,YAAY,EAAE,QAAQ,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAC3E,OAAO,EAAE,kBAAkB,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC","sourcesContent":["export type {\n\tFileDiffInfo,\n\tModifiedFileInfo,\n\tRestoreResult,\n\tStepSnapshotData,\n} from \"./file-snapshot-manager.js\";\nexport { FileSnapshotManager } from \"./file-snapshot-manager.js\";\nexport type { StepDiff, TreeEntry, TreeSnapshot } from \"./internal-git.js\";\nexport { computeProjectHash, InternalGit } from \"./internal-git.js\";\n"]}
@@ -0,0 +1,3 @@
1
+ export { FileSnapshotManager } from "./file-snapshot-manager.js";
2
+ export { computeProjectHash, InternalGit } from "./internal-git.js";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/core/file-store/index.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AAEjE,OAAO,EAAE,kBAAkB,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC","sourcesContent":["export type {\n\tFileDiffInfo,\n\tModifiedFileInfo,\n\tRestoreResult,\n\tStepSnapshotData,\n} from \"./file-snapshot-manager.js\";\nexport { FileSnapshotManager } from \"./file-snapshot-manager.js\";\nexport type { StepDiff, TreeEntry, TreeSnapshot } from \"./internal-git.js\";\nexport { computeProjectHash, InternalGit } from \"./internal-git.js\";\n"]}
@@ -6,12 +6,14 @@
6
6
  */
7
7
  import type { AgentMessage } from "@dyyz1993/pi-agent-core";
8
8
  import type { ImageContent, Message, TextContent } from "@dyyz1993/pi-ai";
9
- export declare const FOLD_SUMMARY_PREFIX = "An earlier message in this conversation was folded into the following summary:\n\n<fold>\n";
10
- export declare const FOLD_SUMMARY_SUFFIX = "\n</fold>";
11
9
  export declare const COMPACTION_SUMMARY_PREFIX = "The conversation history before this point was compacted into the following summary:\n\n<summary>\n";
12
10
  export declare const COMPACTION_SUMMARY_SUFFIX = "\n</summary>";
13
11
  export declare const BRANCH_SUMMARY_PREFIX = "The following is a summary of a branch that this conversation came back from:\n\n<summary>\n";
14
12
  export declare const BRANCH_SUMMARY_SUFFIX = "</summary>";
13
+ export declare const FOLD_SUMMARY_PREFIX = "An earlier message in this conversation was folded into the following summary:\n\n<fold>\n";
14
+ export declare const FOLD_SUMMARY_SUFFIX = "\n</fold>";
15
+ export declare const SEGMENT_SUMMARY_PREFIX = "A segment of the conversation was compressed into this summary:\n\n<summary>\n";
16
+ export declare const SEGMENT_SUMMARY_SUFFIX = "\n</summary>";
15
17
  /**
16
18
  * Message type for bash executions via the ! command.
17
19
  */
@@ -57,6 +59,11 @@ export interface FoldSummaryMessage {
57
59
  originalTokens: number;
58
60
  timestamp: number;
59
61
  }
62
+ export interface SegmentSummaryMessage {
63
+ role: "segmentSummary";
64
+ summary: string;
65
+ timestamp: number;
66
+ }
60
67
  declare module "@dyyz1993/pi-agent-core" {
61
68
  interface CustomAgentMessages {
62
69
  bashExecution: BashExecutionMessage;
@@ -64,6 +71,7 @@ declare module "@dyyz1993/pi-agent-core" {
64
71
  branchSummary: BranchSummaryMessage;
65
72
  compactionSummary: CompactionSummaryMessage;
66
73
  foldSummary: FoldSummaryMessage;
74
+ segmentSummary: SegmentSummaryMessage;
67
75
  }
68
76
  }
69
77
  /**