@davideasden/pi-undo 0.2.11 → 0.2.14
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/README.md +1 -1
- package/extensions/pi-undo.ts +20 -3
- package/package.json +4 -3
- package/src/atomic-fs.ts +18 -0
- package/src/controller.ts +45 -4
- package/src/restore-engine.ts +4 -1
- package/src/root-discovery.ts +26 -1
- package/src/snapshot-store.ts +18 -7
package/README.md
CHANGED
|
@@ -19,7 +19,7 @@ Each completed agent run creates a checkpoint that captures both the Pi session
|
|
|
19
19
|
|
|
20
20
|
## Requirements
|
|
21
21
|
|
|
22
|
-
- Pi `0.80.10`
|
|
22
|
+
- Pi `0.80.10` 或更高版本(已在 0.84.4 上验证)。
|
|
23
23
|
- Node.js `22.19.0` or later.
|
|
24
24
|
- Git available on `PATH` (used internally for content-addressed snapshots).
|
|
25
25
|
|
package/extensions/pi-undo.ts
CHANGED
|
@@ -46,6 +46,7 @@ export function createPiUndoExtension(runtimeFactory: PiUndoRuntimeFactory): (pi
|
|
|
46
46
|
let acceptedReplay: DeferredPrompt | undefined;
|
|
47
47
|
let activeCommands = new Set<symbol>();
|
|
48
48
|
let activeAction: "undo" | "redo" | undefined;
|
|
49
|
+
let captureFailureNotified = false;
|
|
49
50
|
|
|
50
51
|
const initialize = async (context: ExtensionContext): Promise<void> => {
|
|
51
52
|
const currentGeneration = ++generation;
|
|
@@ -55,6 +56,7 @@ export function createPiUndoExtension(runtimeFactory: PiUndoRuntimeFactory): (pi
|
|
|
55
56
|
acceptedReplay = undefined;
|
|
56
57
|
activeCommands = new Set<symbol>();
|
|
57
58
|
activeAction = undefined;
|
|
59
|
+
captureFailureNotified = false;
|
|
58
60
|
try {
|
|
59
61
|
const next = await runtimeFactory(context, pi);
|
|
60
62
|
if (currentGeneration !== generation) return;
|
|
@@ -63,6 +65,8 @@ export function createPiUndoExtension(runtimeFactory: PiUndoRuntimeFactory): (pi
|
|
|
63
65
|
const history = next.controller.history();
|
|
64
66
|
if (history.locked) next.reporter.setRecoveryRequired("pending journal", next.recovery);
|
|
65
67
|
else next.reporter.setReady(history.undoCount, history.redoCount);
|
|
68
|
+
// 后台预热快照缓存,把新会话首次冷 capture 移出第一条 prompt 的关键路径。
|
|
69
|
+
next.controller.warmUp();
|
|
66
70
|
} catch (error) {
|
|
67
71
|
if (currentGeneration !== generation) return;
|
|
68
72
|
runtime = undefined;
|
|
@@ -213,7 +217,7 @@ export function createPiUndoExtension(runtimeFactory: PiUndoRuntimeFactory): (pi
|
|
|
213
217
|
pi.on("session_start", async (_event: unknown, context: ExtensionContext) => initialize(context));
|
|
214
218
|
pi.on("input", async (event: InputEvent, context: ExtensionContext) => {
|
|
215
219
|
const active = runtime;
|
|
216
|
-
if (active === undefined) return { action: "
|
|
220
|
+
if (active === undefined) return { action: "continue" as const };
|
|
217
221
|
const result = await active.controller.prepareInput(event.text, {
|
|
218
222
|
streaming: event.streamingBehavior !== undefined,
|
|
219
223
|
});
|
|
@@ -250,6 +254,14 @@ export function createPiUndoExtension(runtimeFactory: PiUndoRuntimeFactory): (pi
|
|
|
250
254
|
}
|
|
251
255
|
}
|
|
252
256
|
}
|
|
257
|
+
if (result.action === "continue" && active.controller.captureFailed() && !captureFailureNotified) {
|
|
258
|
+
captureFailureNotified = true;
|
|
259
|
+
const reason = active.controller.captureFailureReason();
|
|
260
|
+
context.ui.notify(
|
|
261
|
+
`pi-undo: pre-input snapshot failed${reason === undefined || reason.length === 0 ? "" : ` (${reason})`}; this run will not be undoable`,
|
|
262
|
+
"warning",
|
|
263
|
+
);
|
|
264
|
+
}
|
|
253
265
|
return result;
|
|
254
266
|
});
|
|
255
267
|
pi.on("before_agent_start", async () => {
|
|
@@ -286,11 +298,16 @@ export function createPiUndoExtension(runtimeFactory: PiUndoRuntimeFactory): (pi
|
|
|
286
298
|
return result;
|
|
287
299
|
});
|
|
288
300
|
pi.on("session_tree", async (event: PiSessionTreeEvent) => {
|
|
289
|
-
|
|
290
|
-
|
|
301
|
+
const active = runtime;
|
|
302
|
+
if (active?.isInternalNavigation?.()) return;
|
|
303
|
+
const treeGeneration = generation;
|
|
304
|
+
await active?.controller.afterTree({
|
|
291
305
|
newLeafId: event.newLeafId,
|
|
292
306
|
navigationTargetLeafId: event.summaryEntry?.parentId ?? event.newLeafId,
|
|
293
307
|
});
|
|
308
|
+
if (active !== undefined && runtime === active && generation === treeGeneration) {
|
|
309
|
+
resumeDeferredPrompts(active, treeGeneration, "session state ambiguous");
|
|
310
|
+
}
|
|
294
311
|
});
|
|
295
312
|
pi.on("session_shutdown", async () => {
|
|
296
313
|
generation += 1;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@davideasden/pi-undo",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.14",
|
|
4
4
|
"description": "Persistent workspace undo and redo for Pi",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"keywords": [
|
|
@@ -45,14 +45,15 @@
|
|
|
45
45
|
"pack:dry-run": "npm pack --dry-run"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
|
48
|
-
"@earendil-works/pi-coding-agent": "
|
|
48
|
+
"@earendil-works/pi-coding-agent": ">=0.80.10",
|
|
49
49
|
"@earendil-works/pi-tui": "*"
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
52
|
"proper-lockfile": "4.1.2"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
|
-
"@earendil-works/pi-coding-agent": "0.
|
|
55
|
+
"@earendil-works/pi-coding-agent": "0.84.4",
|
|
56
|
+
"@earendil-works/pi-tui": "0.84.4",
|
|
56
57
|
"@types/node": "24.12.4",
|
|
57
58
|
"@types/proper-lockfile": "4.1.4",
|
|
58
59
|
"typescript": "5.9.3",
|
package/src/atomic-fs.ts
CHANGED
|
@@ -86,10 +86,28 @@ export async function fsyncFile(file: string): Promise<void> {
|
|
|
86
86
|
}
|
|
87
87
|
}
|
|
88
88
|
|
|
89
|
+
/** 不支持目录 fsync 的平台/文件系统(Windows/NTFS、网络盘、exFAT、FUSE 等)返回的 errno。 */
|
|
90
|
+
const TOLERATED_DIR_FSYNC_CODES = new Set(["EPERM", "EINVAL", "ENOTSUP", "EOPNOTSUPP", "ENOSYS"]);
|
|
91
|
+
|
|
92
|
+
/** 目录 fsync 失败是否因平台/文件系统不支持而可以安全降级跳过。 */
|
|
93
|
+
export function isToleratedDirFsyncError(error: unknown): boolean {
|
|
94
|
+
return typeof error === "object" && error !== null && "code" in error &&
|
|
95
|
+
typeof (error as { code?: unknown }).code === "string" &&
|
|
96
|
+
TOLERATED_DIR_FSYNC_CODES.has((error as { code: string }).code);
|
|
97
|
+
}
|
|
98
|
+
|
|
89
99
|
export async function fsyncDirectory(directory: string): Promise<void> {
|
|
100
|
+
if (process.platform === "win32") {
|
|
101
|
+
// Windows 对目录句柄调用 fsync 必然返回 EPERM;rename 的持久性由文件系统自身保证。
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
90
104
|
const handle = await open(directory, "r");
|
|
91
105
|
try {
|
|
92
106
|
await handle.sync();
|
|
107
|
+
} catch (error) {
|
|
108
|
+
// 目录 fsync 只是崩溃持久性的 best-effort 强化,文件内容的 fsync 不受影响:
|
|
109
|
+
// 不支持目录 fsync 的文件系统降级跳过,其余错误照常传播。
|
|
110
|
+
if (!isToleratedDirFsyncError(error)) throw error;
|
|
93
111
|
} finally {
|
|
94
112
|
await handle.close();
|
|
95
113
|
}
|
package/src/controller.ts
CHANGED
|
@@ -145,6 +145,12 @@ export interface UndoController {
|
|
|
145
145
|
cancelTree?(): Promise<void>;
|
|
146
146
|
recover(): Promise<void>;
|
|
147
147
|
history(): HistoryState;
|
|
148
|
+
/** 后台预热快照缓存:立即返回,失败静默;后续 capture 会先等预热完成。 */
|
|
149
|
+
warmUp(): void;
|
|
150
|
+
/** 最近一次输入前快照是否失败(此时本次 run 不可 undo,但输入不受影响)。 */
|
|
151
|
+
captureFailed(): boolean;
|
|
152
|
+
/** 最近一次输入前快照失败的原因(截断后的错误消息);成功时为 undefined。 */
|
|
153
|
+
captureFailureReason(): string | undefined;
|
|
148
154
|
}
|
|
149
155
|
|
|
150
156
|
interface StagedRun {
|
|
@@ -192,6 +198,9 @@ export class UndoControllerImpl implements UndoController {
|
|
|
192
198
|
private operationProfiler: OperationProfiler | undefined;
|
|
193
199
|
private promptDeferralInFlight = false;
|
|
194
200
|
private lastSafetyManifestId: ManifestId | null = null;
|
|
201
|
+
private lastCaptureFailed = false;
|
|
202
|
+
private lastCaptureFailureMessage: string | undefined;
|
|
203
|
+
private warmUpInFlight: Promise<void> | undefined;
|
|
195
204
|
|
|
196
205
|
constructor(dependencies: ControllerDependencies, initialState: ControllerInitialState = {}) {
|
|
197
206
|
this.dependencies = dependencies;
|
|
@@ -205,22 +214,46 @@ export class UndoControllerImpl implements UndoController {
|
|
|
205
214
|
return { undoCount: this.undoStack.length, redoCount: this.redoStack.length, locked: this.locked };
|
|
206
215
|
}
|
|
207
216
|
|
|
217
|
+
captureFailed(): boolean {
|
|
218
|
+
return this.lastCaptureFailed;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
warmUp(): void {
|
|
222
|
+
if (this.locked || this.warmUpInFlight !== undefined) return;
|
|
223
|
+
this.warmUpInFlight = (async () => {
|
|
224
|
+
try {
|
|
225
|
+
await this.captureWithWorkspaceLock();
|
|
226
|
+
} catch {
|
|
227
|
+
// 预热是 best-effort:失败静默,正式 capture 会再次尝试并上报。
|
|
228
|
+
}
|
|
229
|
+
})();
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
captureFailureReason(): string | undefined {
|
|
233
|
+
return this.lastCaptureFailed ? this.lastCaptureFailureMessage : undefined;
|
|
234
|
+
}
|
|
235
|
+
|
|
208
236
|
listCheckpoints(): readonly CheckpointRecord[] {
|
|
209
237
|
return [...this.undoStack];
|
|
210
238
|
}
|
|
211
239
|
|
|
212
240
|
async prepareInput(text: string, context: InputContext): Promise<InputEventResult> {
|
|
213
241
|
if (this.promptDeferralInFlight) return { action: "defer" };
|
|
214
|
-
if (this.locked
|
|
242
|
+
if (this.locked) return { action: "continue" };
|
|
243
|
+
if (this.operationInFlight) return { action: "defer" };
|
|
215
244
|
if (context.streaming || text.length === 0) return { action: "continue" };
|
|
216
245
|
try {
|
|
217
246
|
const before = await this.captureWithWorkspaceLock();
|
|
247
|
+
this.lastCaptureFailed = false;
|
|
248
|
+
this.lastCaptureFailureMessage = undefined;
|
|
218
249
|
this.historyPaused = false;
|
|
219
250
|
this.staged = { rawPrompt: text, before, sourceLogicalLeaf: this.dependencies.getLogicalLeafId() };
|
|
220
251
|
return { action: "continue" };
|
|
221
|
-
} catch {
|
|
222
|
-
//
|
|
223
|
-
|
|
252
|
+
} catch (error) {
|
|
253
|
+
// 无法证明输入前状态:放弃记录本次历史,但绝不吞掉用户输入。
|
|
254
|
+
this.lastCaptureFailed = true;
|
|
255
|
+
this.lastCaptureFailureMessage = truncateReason(error instanceof Error ? error.message : String(error));
|
|
256
|
+
return { action: "continue" };
|
|
224
257
|
}
|
|
225
258
|
}
|
|
226
259
|
|
|
@@ -658,6 +691,10 @@ export class UndoControllerImpl implements UndoController {
|
|
|
658
691
|
}
|
|
659
692
|
|
|
660
693
|
private async captureWithWorkspaceLock(): Promise<SnapshotManifest> {
|
|
694
|
+
// 预热可能仍持有 workspace lock:先等它完成再 acquire,避免排队超时;
|
|
695
|
+
// 此时进程内缓存已暖,本次 capture 只需指纹校验。
|
|
696
|
+
const warmUp = this.warmUpInFlight;
|
|
697
|
+
if (warmUp !== undefined) await warmUp;
|
|
661
698
|
const lease = await this.dependencies.acquireWorkspaceLock();
|
|
662
699
|
try {
|
|
663
700
|
return await this.dependencies.capture();
|
|
@@ -844,3 +881,7 @@ class OperationProfiler {
|
|
|
844
881
|
function noop(): OperationResult {
|
|
845
882
|
return { code: "noop", changedFiles: 0 };
|
|
846
883
|
}
|
|
884
|
+
|
|
885
|
+
function truncateReason(reason: string): string {
|
|
886
|
+
return reason.replace(/[\u0000-\u001F\u007F]+/g, " ").trim().slice(0, 120);
|
|
887
|
+
}
|
package/src/restore-engine.ts
CHANGED
|
@@ -1661,7 +1661,10 @@ function assertCompatibleManifests(
|
|
|
1661
1661
|
throw new Error("restore manifest 不属于同一 workspace");
|
|
1662
1662
|
}
|
|
1663
1663
|
if (current.roots.some((root) => root.state === "broken") || target.roots.some((root) => root.state === "broken")) {
|
|
1664
|
-
|
|
1664
|
+
const brokenRoots = [...current.roots, ...target.roots]
|
|
1665
|
+
.filter((root, index, all) => root.state === "broken" && all.findIndex((candidate) => candidate.relativeRoot === root.relativeRoot) === index)
|
|
1666
|
+
.map((root) => root.relativeRoot);
|
|
1667
|
+
throw new Error(`broken root 不能用于 restore: ${brokenRoots.join(", ")}`);
|
|
1665
1668
|
}
|
|
1666
1669
|
const scopedCoverage = scope === undefined
|
|
1667
1670
|
? undefined
|
package/src/root-discovery.ts
CHANGED
|
@@ -333,10 +333,35 @@ async function isSafeDirectory(directory: string, workspaceIdentity: string): Pr
|
|
|
333
333
|
async function gitlinkState(absolutePath: string): Promise<DiscoveryRoot["state"]> {
|
|
334
334
|
try {
|
|
335
335
|
await lstat(absolutePath);
|
|
336
|
-
return "broken";
|
|
337
336
|
} catch {
|
|
338
337
|
return "uninitialized";
|
|
339
338
|
}
|
|
339
|
+
// git worktree / 部分 checkout 会为 gitlink 留下空目录骨架(可能含指向共享依赖的 symlink);
|
|
340
|
+
// 内容寻址快照只跟踪文件,不含任何文件的目录与未初始化等价,避免整个 workspace 无法快照。
|
|
341
|
+
return (await directoryTreeContainsFile(absolutePath)) ? "broken" : "uninitialized";
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
const SKELETON_NOISE_FILES = new Set([".DS_Store", "desktop.ini", "Thumbs.db"]);
|
|
345
|
+
|
|
346
|
+
async function directoryTreeContainsFile(directory: string): Promise<boolean> {
|
|
347
|
+
let entries;
|
|
348
|
+
try {
|
|
349
|
+
entries = await readdir(directory, { withFileTypes: true });
|
|
350
|
+
} catch {
|
|
351
|
+
return true;
|
|
352
|
+
}
|
|
353
|
+
for (const entry of entries) {
|
|
354
|
+
if (entry.isDirectory()) {
|
|
355
|
+
if (await directoryTreeContainsFile(join(directory, entry.name))) return true;
|
|
356
|
+
} else if (entry.isSymbolicLink() || SKELETON_NOISE_FILES.has(entry.name)) {
|
|
357
|
+
// symlink 不适随也不计内容:uninitialized 根下的内容不被捕获也不会被 restore 触碰,
|
|
358
|
+
// 原样保留不会丢失;OS 噪音文件不应让整个 workspace 无法快照。
|
|
359
|
+
continue;
|
|
360
|
+
} else {
|
|
361
|
+
return true;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
return false;
|
|
340
365
|
}
|
|
341
366
|
|
|
342
367
|
function cleanGitEnvironment(): Readonly<Record<string, string | undefined>> {
|
package/src/snapshot-store.ts
CHANGED
|
@@ -252,8 +252,9 @@ export class SnapshotStore {
|
|
|
252
252
|
const coverage = captureCoverage(topology.workspaceIdentity, scope);
|
|
253
253
|
const artifactExclusions = captureExclusions(topology.workspaceIdentity, options.excludePaths);
|
|
254
254
|
await this.assertTopology(topology, "捕获前 topology 已变化");
|
|
255
|
-
|
|
256
|
-
|
|
255
|
+
const brokenRoots = brokenRootPaths(topology);
|
|
256
|
+
if (brokenRoots.length > 0) {
|
|
257
|
+
throw new SnapshotStoreError("capture_failed", `broken root 不能静默进入快照: ${brokenRoots.join(", ")}`);
|
|
257
258
|
}
|
|
258
259
|
|
|
259
260
|
const storeDirectory = this.storeDirectory(topology);
|
|
@@ -347,8 +348,9 @@ export class SnapshotStore {
|
|
|
347
348
|
}
|
|
348
349
|
const artifactExclusions = captureExclusions(topology.workspaceIdentity, options.excludePaths);
|
|
349
350
|
await this.assertTopology(topology, "可见路径枚举前 topology 已变化");
|
|
350
|
-
|
|
351
|
-
|
|
351
|
+
const brokenRoots = brokenRootPaths(topology);
|
|
352
|
+
if (brokenRoots.length > 0) {
|
|
353
|
+
throw new SnapshotStoreError("capture_failed", `broken root 不能静默进入可见路径枚举: ${brokenRoots.join(", ")}`);
|
|
352
354
|
}
|
|
353
355
|
|
|
354
356
|
const storeDirectory = this.storeDirectory(topology);
|
|
@@ -1572,6 +1574,10 @@ function ignoredPresentProof(coverage: string, ignoredPresentPaths: readonly str
|
|
|
1572
1574
|
};
|
|
1573
1575
|
}
|
|
1574
1576
|
|
|
1577
|
+
function brokenRootPaths(topology: Pick<RootTopology, "roots">): string[] {
|
|
1578
|
+
return topology.roots.filter((root) => root.state === "broken").map((root) => root.relativeRoot);
|
|
1579
|
+
}
|
|
1580
|
+
|
|
1575
1581
|
function inactiveRootClosure(root: Pick<RootTopologyIdentity, "relativeRoot" | "state">): string {
|
|
1576
1582
|
return checksum(canonicalJson({
|
|
1577
1583
|
relativeRoot: root.relativeRoot,
|
|
@@ -1879,11 +1885,16 @@ function parseTreeEntries(output: Uint8Array): CapturedTreeEntry[] {
|
|
|
1879
1885
|
}
|
|
1880
1886
|
|
|
1881
1887
|
function parseNulPaths(output: Uint8Array): string[] {
|
|
1882
|
-
|
|
1888
|
+
const paths: string[] = [];
|
|
1889
|
+
for (const record of splitNulRecords(output)) {
|
|
1883
1890
|
const path = decodeUtf8(record);
|
|
1891
|
+
// git 在嵌套仓库边界会输出折叠的目录项(如 "dir/");该目录由 root discovery
|
|
1892
|
+
// 作为独立 root 捕获,不属于本仓库的路径枚举,直接跳过。
|
|
1893
|
+
if (path.endsWith("/")) continue;
|
|
1884
1894
|
relativeSafePath("/", path);
|
|
1885
|
-
|
|
1886
|
-
}
|
|
1895
|
+
paths.push(path);
|
|
1896
|
+
}
|
|
1897
|
+
return paths;
|
|
1887
1898
|
}
|
|
1888
1899
|
|
|
1889
1900
|
function splitNulRecords(output: Uint8Array): Uint8Array[] {
|