@lyrumu/pi-diff 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/NOTICE.md +8 -0
- package/README.md +120 -2
- package/README.zh-CN.md +121 -0
- package/THIRD_PARTY_LICENSES.md +33 -0
- package/index.ts +153 -0
- package/package.json +45 -4
- package/src/diff-engine.ts +218 -0
- package/src/git.ts +378 -0
- package/src/language.ts +91 -0
- package/src/preferences.ts +47 -0
- package/src/snapshot.ts +114 -0
- package/src/store.ts +77 -0
- package/src/tracker.ts +508 -0
- package/src/tui/diff-screen.ts +310 -0
- package/src/tui/file-tree-utils.ts +174 -0
- package/src/tui/file-tree.ts +99 -0
- package/src/tui/patch-diff.ts +243 -0
- package/src/tui/scopes.ts +20 -0
- package/src/tui/text.ts +15 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { DATA_DIR } from "./snapshot.ts";
|
|
5
|
+
import type { DiffView } from "./tui/patch-diff.ts";
|
|
6
|
+
import type { Language } from "./language.ts";
|
|
7
|
+
|
|
8
|
+
export const VIEW_PREFERENCE_PATH = join(DATA_DIR, "view.json");
|
|
9
|
+
export const LANGUAGE_PREFERENCE_PATH = join(DATA_DIR, "language.json");
|
|
10
|
+
|
|
11
|
+
export async function loadViewPreference(file: string): Promise<DiffView | undefined> {
|
|
12
|
+
try {
|
|
13
|
+
const value: unknown = JSON.parse(await readFile(file, "utf8"));
|
|
14
|
+
if (typeof value !== "object" || value === null || !("view" in value)) return undefined;
|
|
15
|
+
return value.view === "unified" || value.view === "split" ? value.view : undefined;
|
|
16
|
+
} catch {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export async function saveViewPreference(file: string, view: DiffView): Promise<void> {
|
|
22
|
+
await savePreference(file, { view });
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export async function loadLanguagePreference(file: string): Promise<Language> {
|
|
26
|
+
try {
|
|
27
|
+
const value: unknown = JSON.parse(await readFile(file, "utf8"));
|
|
28
|
+
if (typeof value === "object" && value !== null && "language" in value && value.language === "zh-CN") return "zh-CN";
|
|
29
|
+
} catch {}
|
|
30
|
+
return "en";
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export async function saveLanguagePreference(file: string, language: Language): Promise<void> {
|
|
34
|
+
await savePreference(file, { language });
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Replace atomically so a failed write never destroys the previous choice. */
|
|
38
|
+
async function savePreference(file: string, value: { view: DiffView } | { language: Language }): Promise<void> {
|
|
39
|
+
const temporary = `${file}.${randomUUID()}.tmp`;
|
|
40
|
+
try {
|
|
41
|
+
await mkdir(dirname(file), { recursive: true });
|
|
42
|
+
await writeFile(temporary, JSON.stringify(value) + "\n");
|
|
43
|
+
await rename(temporary, file);
|
|
44
|
+
} finally {
|
|
45
|
+
await rm(temporary, { force: true }).catch(() => {});
|
|
46
|
+
}
|
|
47
|
+
}
|
package/src/snapshot.ts
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import { mkdir, readFile, readdir, rename, rm, rmdir, stat, unlink, utimes, writeFile } from "node:fs/promises";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { homedir } from "node:os";
|
|
5
|
+
|
|
6
|
+
export const DATA_DIR = join(homedir(), ".pi", "agent", "data", "pi-diff");
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Content-addressed snapshot cache under ~/.pi/agent/data/pi-diff/.
|
|
10
|
+
*
|
|
11
|
+
* Ported from opencode `packages/core/src/snapshot.ts` with the storage base
|
|
12
|
+
* swapped (PLAN.md deviations): upstream keeps git snapshot trees in a data
|
|
13
|
+
* directory; we store raw file blobs keyed by sha256. Patches are persisted
|
|
14
|
+
* self-contained in the session JSONL (see store.ts); this cache only
|
|
15
|
+
* accelerates incremental patch computation (turn old/new sides and the
|
|
16
|
+
* session-cumulative recompute). Losing it never breaks `/diff` — consumers
|
|
17
|
+
* fall back to the persisted per-turn patch texts.
|
|
18
|
+
*
|
|
19
|
+
* Cleanup: 30-day LRU by access time at session start. Note some filesystems
|
|
20
|
+
* are mounted noatime (common on macOS); we fall back to mtime there.
|
|
21
|
+
*/
|
|
22
|
+
export class SnapshotStore {
|
|
23
|
+
private readonly root: string;
|
|
24
|
+
|
|
25
|
+
constructor(root = DATA_DIR) {
|
|
26
|
+
this.root = root;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
static hash(content: Buffer): string {
|
|
30
|
+
return createHash("sha256").update(content).digest("hex");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Store content; returns its content hash (sha256). Idempotent. */
|
|
34
|
+
async put(content: Buffer): Promise<string> {
|
|
35
|
+
const hash = SnapshotStore.hash(content);
|
|
36
|
+
const file = this.objectPath(hash);
|
|
37
|
+
try {
|
|
38
|
+
await stat(file);
|
|
39
|
+
return hash; // already stored — keep the original (older) timestamps
|
|
40
|
+
} catch {
|
|
41
|
+
// not stored yet
|
|
42
|
+
}
|
|
43
|
+
await mkdir(this.shard(hash), { recursive: true });
|
|
44
|
+
const temporary = `${file}.${randomUUID()}.tmp`;
|
|
45
|
+
try {
|
|
46
|
+
await writeFile(temporary, content);
|
|
47
|
+
await rename(temporary, file);
|
|
48
|
+
} finally {
|
|
49
|
+
await rm(temporary, { force: true }).catch(() => {});
|
|
50
|
+
}
|
|
51
|
+
return hash;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Fetch content by hash; null when unknown or unreadable. Refreshes atime. */
|
|
55
|
+
async get(hash: string): Promise<Buffer | null> {
|
|
56
|
+
if (!/^[a-f0-9]{64}$/.test(hash)) return null;
|
|
57
|
+
const file = this.objectPath(hash);
|
|
58
|
+
try {
|
|
59
|
+
const content = await readFile(file);
|
|
60
|
+
if (SnapshotStore.hash(content) !== hash) return null;
|
|
61
|
+
// Best-effort atime refresh for the LRU (harmless if it fails).
|
|
62
|
+
await utimes(file, new Date(), new Date()).catch(() => {});
|
|
63
|
+
return content;
|
|
64
|
+
} catch {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Remove objects not accessed for `maxAgeDays` days. Objects are stored in
|
|
71
|
+
* shard dirs (objects/ab/cdef...); empty shards are removed too.
|
|
72
|
+
*/
|
|
73
|
+
async cleanup(maxAgeDays = 30): Promise<void> {
|
|
74
|
+
const objectsDir = join(this.root, "objects");
|
|
75
|
+
let shards: string[];
|
|
76
|
+
try {
|
|
77
|
+
shards = await readdir(objectsDir);
|
|
78
|
+
} catch {
|
|
79
|
+
return; // nothing cached yet
|
|
80
|
+
}
|
|
81
|
+
const cutoff = Date.now() - maxAgeDays * 24 * 60 * 60 * 1000;
|
|
82
|
+
for (const shard of shards) {
|
|
83
|
+
const shardDir = join(objectsDir, shard);
|
|
84
|
+
let entries: string[];
|
|
85
|
+
try {
|
|
86
|
+
entries = await readdir(shardDir);
|
|
87
|
+
} catch {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
let remaining = 0;
|
|
91
|
+
for (const entry of entries) {
|
|
92
|
+
const file = join(shardDir, entry);
|
|
93
|
+
try {
|
|
94
|
+
const s = await stat(file);
|
|
95
|
+
const lastUse = Math.max(s.atimeMs, s.mtimeMs);
|
|
96
|
+
if (lastUse < cutoff) await unlink(file);
|
|
97
|
+
else remaining++;
|
|
98
|
+
} catch {
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
// A concurrent capture may have added a fresh object since readdir.
|
|
103
|
+
if (remaining === 0) await rmdir(shardDir).catch(() => {});
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
private objectPath(hash: string): string {
|
|
108
|
+
return join(this.shard(hash), hash.slice(2));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
private shard(hash: string): string {
|
|
112
|
+
return join(this.root, "objects", hash.slice(0, 2));
|
|
113
|
+
}
|
|
114
|
+
}
|
package/src/store.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { SessionEntry } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import type { FilePatch } from "./diff-engine.ts";
|
|
3
|
+
|
|
4
|
+
/** Persisted shape of one turn's diff, stored as custom entry `pi-diff.turn`. */
|
|
5
|
+
export interface TurnDiffEntryData {
|
|
6
|
+
/** 1-based turn number on the current branch. */
|
|
7
|
+
turn: number;
|
|
8
|
+
/** ISO timestamp of turn end. */
|
|
9
|
+
endedAt: string;
|
|
10
|
+
/** Total changed files in this turn. */
|
|
11
|
+
fileCount: number;
|
|
12
|
+
/** Self-contained patches; the TUI renders from these alone. */
|
|
13
|
+
patches: FilePatch[];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const CUSTOM_TYPE = "pi-diff.turn" as const;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Append a turn diff to the session JSONL via pi.appendEntry (custom entry).
|
|
20
|
+
* Entries participate in branching: navigating a branch replays only the
|
|
21
|
+
* entries on that branch, so per-turn and session totals are branch-correct.
|
|
22
|
+
*/
|
|
23
|
+
export function appendTurnDiff(
|
|
24
|
+
appendEntry: (customType: string, data?: unknown) => void,
|
|
25
|
+
data: TurnDiffEntryData,
|
|
26
|
+
): void {
|
|
27
|
+
appendEntry(CUSTOM_TYPE, data);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function isTurnDiffData(value: unknown): value is TurnDiffEntryData {
|
|
31
|
+
if (typeof value !== "object" || value === null) return false;
|
|
32
|
+
const v = value as Record<string, unknown>;
|
|
33
|
+
return (
|
|
34
|
+
typeof v.turn === "number" && Number.isSafeInteger(v.turn) && v.turn > 0 &&
|
|
35
|
+
typeof v.endedAt === "string" && Number.isFinite(Date.parse(v.endedAt)) &&
|
|
36
|
+
typeof v.fileCount === "number" && Number.isSafeInteger(v.fileCount) && v.fileCount >= 0 &&
|
|
37
|
+
Array.isArray(v.patches) &&
|
|
38
|
+
v.fileCount === v.patches.length && v.patches.every(isFilePatch)
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function isFilePatch(value: unknown): value is FilePatch {
|
|
43
|
+
if (typeof value !== "object" || value === null) return false;
|
|
44
|
+
const p = value as Record<string, unknown>;
|
|
45
|
+
return typeof p.file === "string" && p.file.length > 0 && !p.file.includes("\0") &&
|
|
46
|
+
typeof p.patch === "string" &&
|
|
47
|
+
(p.status === "added" || p.status === "modified" || p.status === "deleted") &&
|
|
48
|
+
typeof p.additions === "number" && Number.isSafeInteger(p.additions) && p.additions >= 0 &&
|
|
49
|
+
typeof p.deletions === "number" && Number.isSafeInteger(p.deletions) && p.deletions >= 0 &&
|
|
50
|
+
[p.oldHash, p.newHash].every((hash) => hash === undefined || hash === null || (typeof hash === "string" && /^[a-f0-9]{64}$/.test(hash))) &&
|
|
51
|
+
[p.binary, p.tooLarge, p.baselineUnknown, p.unavailable, p.cumulativeFallback].every((flag) => flag === undefined || typeof flag === "boolean");
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Rebuild Tracker state from `pi-diff.turn` custom entries on the active
|
|
56
|
+
* branch (session reload / resume / branch switch). Corrupted entries from
|
|
57
|
+
* older versions are skipped instead of crashing.
|
|
58
|
+
*/
|
|
59
|
+
export function collectTurnDiffs(entries: readonly SessionEntry[]): TurnDiffEntryData[] {
|
|
60
|
+
const result: TurnDiffEntryData[] = [];
|
|
61
|
+
for (const entry of entries) {
|
|
62
|
+
if (entry.type !== "custom" || entry.customType !== CUSTOM_TYPE) continue;
|
|
63
|
+
if (!isTurnDiffData(entry.data)) continue;
|
|
64
|
+
result.push(entry.data);
|
|
65
|
+
}
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Recognize unusable old records without treating them as successful empty turns. */
|
|
70
|
+
export function hasUnavailableTurnDiffs(entries: readonly SessionEntry[]): boolean {
|
|
71
|
+
return entries.some((entry) => {
|
|
72
|
+
if (entry.type !== "custom") return false;
|
|
73
|
+
const type: unknown = entry.customType;
|
|
74
|
+
if (typeof type === "object" && type !== null && "customType" in type && type.customType === CUSTOM_TYPE) return true;
|
|
75
|
+
return type === CUSTOM_TYPE && !isTurnDiffData(entry.data);
|
|
76
|
+
});
|
|
77
|
+
}
|