@blackbelt-technology/pi-dashboard-session-distiller 0.5.4
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/NOTICE +10 -0
- package/README.md +63 -0
- package/bin/distill.mjs +12 -0
- package/package.json +35 -0
- package/src/AGENTS.md +16 -0
- package/src/__tests__/cluster.test.ts +70 -0
- package/src/__tests__/distill.test.ts +57 -0
- package/src/__tests__/fixtures/session-multi.jsonl +30 -0
- package/src/__tests__/jsonl-reader.test.ts +49 -0
- package/src/__tests__/main.test.ts +94 -0
- package/src/__tests__/route.test.ts +63 -0
- package/src/__tests__/segment.test.ts +73 -0
- package/src/__tests__/signals.test.ts +130 -0
- package/src/__tests__/trajectory.test.ts +53 -0
- package/src/__tests__/watermark.test.ts +66 -0
- package/src/cluster.ts +102 -0
- package/src/distill.ts +93 -0
- package/src/jsonl-reader.ts +43 -0
- package/src/main.ts +170 -0
- package/src/route.ts +118 -0
- package/src/segment.ts +77 -0
- package/src/signals.ts +193 -0
- package/src/trajectory.ts +122 -0
- package/src/types.ts +151 -0
- package/src/watermark.ts +98 -0
package/src/watermark.ts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Watermark store (task 2.3).
|
|
3
|
+
* Records the newest processed session timestamp per cwd so each run processes
|
|
4
|
+
* only newer sessions. Stored at
|
|
5
|
+
* ~/.pi/agent/distill-session-knowledge/<cwd-hash>/watermark.json.
|
|
6
|
+
*/
|
|
7
|
+
import { readFileSync, writeFileSync, existsSync, mkdirSync, readdirSync, statSync } from "node:fs";
|
|
8
|
+
import { homedir } from "node:os";
|
|
9
|
+
import { createHash } from "node:crypto";
|
|
10
|
+
import { dirname, join } from "node:path";
|
|
11
|
+
|
|
12
|
+
export interface Watermark {
|
|
13
|
+
lastTimestamp: string; // ISO; "" means never run
|
|
14
|
+
updatedAt: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const EMPTY: Watermark = { lastTimestamp: "", updatedAt: "" };
|
|
18
|
+
|
|
19
|
+
export function cwdHash(cwd: string): string {
|
|
20
|
+
return createHash("sha256").update(cwd).digest("hex").slice(0, 16);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function watermarkPath(cwd: string, root = defaultRoot()): string {
|
|
24
|
+
return join(root, cwdHash(cwd), "watermark.json");
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function defaultRoot(): string {
|
|
28
|
+
return join(homedir(), ".pi", "agent", "distill-session-knowledge");
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function readWatermark(cwd: string, root = defaultRoot()): Watermark {
|
|
32
|
+
const p = watermarkPath(cwd, root);
|
|
33
|
+
if (!existsSync(p)) return { ...EMPTY };
|
|
34
|
+
try {
|
|
35
|
+
return { ...EMPTY, ...JSON.parse(readFileSync(p, "utf-8")) };
|
|
36
|
+
} catch {
|
|
37
|
+
return { ...EMPTY };
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function writeWatermark(cwd: string, lastTimestamp: string, root = defaultRoot()): void {
|
|
42
|
+
const p = watermarkPath(cwd, root);
|
|
43
|
+
mkdirSync(dirname(p), { recursive: true });
|
|
44
|
+
const wm: Watermark = { lastTimestamp, updatedAt: new Date().toISOString() };
|
|
45
|
+
writeFileSync(p, JSON.stringify(wm, null, 2));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Encode a cwd to its pi session-directory name. */
|
|
49
|
+
export function sessionDirName(cwd: string): string {
|
|
50
|
+
// Handle both POSIX (/) and Windows (\) separators.
|
|
51
|
+
return "--" + cwd.replace(/^[/\\]/, "").replace(/[/\\]/g, "-") + "--";
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function sessionsRoot(cwd: string): string {
|
|
55
|
+
return join(homedir(), ".pi", "agent", "sessions", sessionDirName(cwd));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface SessionFileRef {
|
|
59
|
+
path: string;
|
|
60
|
+
/** session start timestamp parsed from the filename prefix */
|
|
61
|
+
timestamp: string;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** List .jsonl session files for a cwd whose timestamp is newer than the watermark. */
|
|
65
|
+
export function listNewerSessions(
|
|
66
|
+
cwd: string,
|
|
67
|
+
since: string,
|
|
68
|
+
dir = sessionsRoot(cwd),
|
|
69
|
+
): SessionFileRef[] {
|
|
70
|
+
if (!existsSync(dir)) return [];
|
|
71
|
+
const sinceMs = since ? Date.parse(since) : -Infinity;
|
|
72
|
+
if (since && Number.isNaN(sinceMs)) {
|
|
73
|
+
// A malformed watermark would make every `> sinceMs` false and silently
|
|
74
|
+
// process 0 sessions. Fail loud so the watermark can be repaired.
|
|
75
|
+
throw new Error(`Invalid watermark timestamp: ${since}`);
|
|
76
|
+
}
|
|
77
|
+
const refs: SessionFileRef[] = [];
|
|
78
|
+
for (const name of readdirSync(dir)) {
|
|
79
|
+
if (!name.endsWith(".jsonl")) continue;
|
|
80
|
+
const ts = timestampFromName(name) ?? isoFromMtime(join(dir, name));
|
|
81
|
+
const tsMs = Date.parse(ts);
|
|
82
|
+
if (Number.isNaN(tsMs)) continue; // skip unparseable timestamps rather than mis-compare
|
|
83
|
+
if (tsMs > sinceMs) refs.push({ path: join(dir, name), timestamp: ts });
|
|
84
|
+
}
|
|
85
|
+
refs.sort((a, b) => Date.parse(a.timestamp) - Date.parse(b.timestamp));
|
|
86
|
+
return refs;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Filenames look like 2026-06-23T22-25-00-849Z_<uuid>.jsonl */
|
|
90
|
+
export function timestampFromName(name: string): string | undefined {
|
|
91
|
+
const m = name.match(/^(\d{4}-\d{2}-\d{2})T(\d{2})-(\d{2})-(\d{2})-(\d{3})Z/);
|
|
92
|
+
if (!m) return undefined;
|
|
93
|
+
return `${m[1]}T${m[2]}:${m[3]}:${m[4]}.${m[5]}Z`;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function isoFromMtime(p: string): string {
|
|
97
|
+
return statSync(p).mtime.toISOString();
|
|
98
|
+
}
|