pi-tuck 1.1.3 → 1.2.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/CHANGELOG.md +46 -0
- package/dist/index.d.ts +2 -6
- package/dist/index.js +16 -111
- package/dist/src/build-id.d.ts +1 -1
- package/dist/src/build-id.js +1 -1
- package/dist/src/compaction.d.ts +2 -103
- package/dist/src/compaction.js +20 -140
- package/dist/src/diagnostic-replay.js +2 -2
- package/dist/src/diagnostics.d.ts +3 -2
- package/dist/src/diagnostics.js +30 -9
- package/dist/src/history-tools.js +3 -3
- package/dist/src/kept-reads.d.ts +2 -2
- package/dist/src/kept-reads.js +144 -8
- package/dist/src/ledger.d.ts +1 -1
- package/dist/src/ledger.js +5 -3
- package/dist/src/legacy-cleanup.d.ts +3 -0
- package/dist/src/legacy-cleanup.js +190 -0
- package/dist/src/notify.d.ts +3 -0
- package/dist/src/notify.js +13 -0
- package/dist/src/omissions.d.ts +1 -1
- package/dist/src/omissions.js +3 -3
- package/dist/src/path-key.d.ts +3 -0
- package/dist/src/path-key.js +8 -0
- package/dist/src/request-capacity.js +1 -1
- package/dist/src/run.d.ts +1 -3
- package/dist/src/run.js +3 -5
- package/dist/src/source-export.d.ts +1 -1
- package/dist/src/source-export.js +1 -1
- package/dist/src/status.d.ts +0 -6
- package/dist/src/status.js +1 -6
- package/dist/src/tuck/core/content.js +6 -9
- package/dist/src/tuck/core/keyword-query.d.ts +2 -2
- package/dist/src/tuck/core/keyword-query.js +1 -1
- package/dist/src/tuck/core/search-entries.js +5 -5
- package/dist/src/tuck/core/transcript.js +2 -1
- package/dist/src/tuck.js +390 -394
- package/dist/src/windows-private-storage.d.ts +9 -0
- package/dist/src/windows-private-storage.js +90 -0
- package/index.ts +18 -82
- package/package.json +5 -8
- package/src/build-id.ts +1 -1
- package/src/compaction.ts +19 -107
- package/src/diagnostic-replay.ts +2 -2
- package/src/diagnostics.ts +24 -9
- package/src/history-tools.ts +3 -3
- package/src/kept-reads.ts +78 -10
- package/src/ledger.ts +5 -3
- package/src/legacy-cleanup.ts +134 -0
- package/src/notify.ts +10 -0
- package/src/omissions.ts +3 -3
- package/src/path-key.ts +9 -0
- package/src/request-capacity.ts +1 -1
- package/src/run.ts +4 -6
- package/src/source-export.ts +1 -1
- package/src/status.ts +1 -10
- package/src/tuck/core/content.ts +5 -10
- package/src/tuck/core/keyword-query.ts +2 -2
- package/src/tuck/core/search-entries.ts +5 -5
- package/src/tuck/core/transcript.ts +2 -1
- package/src/tuck.ts +329 -335
- package/src/windows-private-storage.ts +94 -0
- package/dist/src/assets.d.ts +0 -58
- package/dist/src/assets.js +0 -80
- package/dist/src/eligibility-cache.d.ts +0 -19
- package/dist/src/eligibility-cache.js +0 -33
- package/dist/src/semantic-recall.d.ts +0 -422
- package/dist/src/semantic-recall.js +0 -1386
- package/dist/src/setup.d.ts +0 -3
- package/dist/src/setup.js +0 -191
- package/scripts/eg2-runtime.py +0 -307
- package/scripts/provision-eg2.py +0 -139
- package/scripts/semantic-worker.py +0 -234
- package/src/assets.ts +0 -92
- package/src/eligibility-cache.ts +0 -43
- package/src/semantic-recall.ts +0 -592
- package/src/setup.ts +0 -163
package/src/setup.ts
DELETED
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
// One-time setup of semantic recall's local runtime: download the pinned files (assets.ts), check each against its SHA-256, assemble
|
|
2
|
-
// them into the companion layout scripts/provision-eg2.py expects, and provision it. Everything lives under one folder, by default
|
|
3
|
-
// ~/.pi/agent/pi-tuck; deleting that folder removes all of it.
|
|
4
|
-
import { createHash, randomUUID } from "node:crypto";
|
|
5
|
-
import { constants, createWriteStream, existsSync } from "node:fs";
|
|
6
|
-
import { copyFile, mkdir, readdir, readFile, realpath, rename, rm, stat, statfs, writeFile } from "node:fs/promises";
|
|
7
|
-
import { execFile } from "node:child_process";
|
|
8
|
-
import { dirname, join } from "node:path";
|
|
9
|
-
import { Readable } from "node:stream";
|
|
10
|
-
import { pipeline } from "node:stream/promises";
|
|
11
|
-
import { fileURLToPath } from "node:url";
|
|
12
|
-
import { promisify } from "node:util";
|
|
13
|
-
import { LLAMA, MODEL, PINS, PYTHON, TOKENIZER, WHEELS, runtimeConfigPath, type Download } from "./assets";
|
|
14
|
-
|
|
15
|
-
const run = promisify(execFile);
|
|
16
|
-
const sha256 = (data: string | Uint8Array) => createHash("sha256").update(data).digest("hex");
|
|
17
|
-
const scripts = fileURLToPath(new URL(import.meta.url.endsWith(".ts") ? "../scripts/" : "../../scripts/", import.meta.url));
|
|
18
|
-
|
|
19
|
-
const DOWNLOAD_BYTES = [PYTHON, MODEL, TOKENIZER, LLAMA, ...Object.values(WHEELS)].reduce((n, d) => n + d.size, 0);
|
|
20
|
-
/** Provisioning keeps 5 GiB free after it copies the companion; the downloads and the staged copy come on top. */
|
|
21
|
-
const NEEDED_BYTES = 5 * 1024 ** 3 + 3 * DOWNLOAD_BYTES;
|
|
22
|
-
|
|
23
|
-
/** Returns the runtime configuration's path, setting the runtime up first if it is missing. Safe to call from several Pi processes at
|
|
24
|
-
* once: one sets up, the others wait for it. `notice` is called once, when a download actually starts. */
|
|
25
|
-
export async function ensureSemanticRuntime(root: string, notice: (message: string) => void = () => {}): Promise<string> {
|
|
26
|
-
if (existsSync(runtimeConfigPath(root))) return runtimeConfigPath(root);
|
|
27
|
-
await mkdir(join(root, "runtime"), { recursive: true });
|
|
28
|
-
root = await realpath(root); // provisioning refuses paths through symlinks
|
|
29
|
-
const config = runtimeConfigPath(root), runtime = dirname(config);
|
|
30
|
-
const release = await lock(runtime + ".lock");
|
|
31
|
-
const staging = join(root, `staging-${process.pid}`), lockFile = staging + ".lock.json";
|
|
32
|
-
try {
|
|
33
|
-
if (existsSync(config)) return config; // set up by another process while this one waited
|
|
34
|
-
// Remains of a setup killed before it could clean up; the lock means no other setup is using them.
|
|
35
|
-
await rm(runtime, { recursive: true, force: true });
|
|
36
|
-
for (const [folder, name] of [[root, /^staging-/], [join(root, "downloads"), /\.part$/], [join(root, "python"), /\.part$/]] as const)
|
|
37
|
-
for (const entry of await readdir(folder).catch(() => [])) if (name.test(entry)) await rm(join(folder, entry), { recursive: true, force: true });
|
|
38
|
-
const { bavail, bsize } = await statfs(root);
|
|
39
|
-
if (bavail * bsize < NEEDED_BYTES) throw new Error(`semantic recall needs ${Math.ceil(NEEDED_BYTES / 1e9)} GB of free disk space`);
|
|
40
|
-
notice(`pi-tuck: setting up semantic recall, a one-time download of about ${Math.round(DOWNLOAD_BYTES / 1e6)} MB. Word recall works meanwhile.`);
|
|
41
|
-
const downloads = join(root, "downloads");
|
|
42
|
-
const get = (download: Download) => fetchVerified(download, downloads);
|
|
43
|
-
const [python, llama, model, tokenizer, wheels] = await Promise.all([get(PYTHON), get(LLAMA), get(MODEL), get(TOKENIZER),
|
|
44
|
-
Promise.all(Object.entries(WHEELS).map(async ([name, download]) => [name, await get(download)] as [string, string]))]);
|
|
45
|
-
const interpreter = await installPython(root, python);
|
|
46
|
-
const files = await assemble(staging, { llama, model, tokenizer, wheels });
|
|
47
|
-
const wheel = Object.keys(WHEELS).find(name => name.startsWith("tokenizers-"))!;
|
|
48
|
-
await writeFile(lockFile, JSON.stringify({ format: 1, platform: "darwin-arm64", python: PYTHON.version, wheel: "wheels/" + wheel, files }));
|
|
49
|
-
await provision(interpreter, ["-I", "-B", join(scripts, "provision-eg2.py"), runtime, "--assets", staging, "--lock", lockFile,
|
|
50
|
-
"--python", interpreter, "--index-directory", join(root, "index")]);
|
|
51
|
-
await rm(downloads, { recursive: true, force: true });
|
|
52
|
-
return config;
|
|
53
|
-
} finally {
|
|
54
|
-
await rm(staging, { recursive: true, force: true });
|
|
55
|
-
await rm(lockFile, { force: true });
|
|
56
|
-
await release();
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/** Downloads once into a content-addressed cache; a file reaches its final name only after its size and SHA-256 match the pin. */
|
|
61
|
-
async function fetchVerified(download: Download, directory: string): Promise<string> {
|
|
62
|
-
const path = join(directory, download.sha256);
|
|
63
|
-
if (existsSync(path)) return path;
|
|
64
|
-
await mkdir(directory, { recursive: true });
|
|
65
|
-
const partial = `${path}.${randomUUID()}.part`; // a download abandoned by a failed sibling may still be writing its own
|
|
66
|
-
const response = await fetch(download.url);
|
|
67
|
-
if (!response.ok || !response.body) throw new Error(`download failed (HTTP ${response.status}): ${download.url}`);
|
|
68
|
-
const hash = createHash("sha256");
|
|
69
|
-
let size = 0;
|
|
70
|
-
await pipeline(Readable.fromWeb(response.body as import("node:stream/web").ReadableStream), async function* (chunks) {
|
|
71
|
-
for await (const chunk of chunks) { hash.update(chunk); size += chunk.length; yield chunk; }
|
|
72
|
-
}, createWriteStream(partial));
|
|
73
|
-
if (size !== download.size || hash.digest("hex") !== download.sha256) {
|
|
74
|
-
await rm(partial, { force: true });
|
|
75
|
-
throw new Error(`download does not match its pinned SHA-256: ${download.url}`);
|
|
76
|
-
}
|
|
77
|
-
await rename(partial, path);
|
|
78
|
-
return path;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/** The interpreter stays where it is unpacked: the runtime's virtual environment points at it. */
|
|
82
|
-
async function installPython(root: string, archive: string): Promise<string> {
|
|
83
|
-
const directory = join(root, "python", PYTHON.name), interpreter = join(directory, "python", "bin", "python3");
|
|
84
|
-
if (existsSync(interpreter)) return interpreter;
|
|
85
|
-
const partial = `${directory}.${process.pid}.part`;
|
|
86
|
-
await rm(partial, { recursive: true, force: true });
|
|
87
|
-
await mkdir(partial, { recursive: true });
|
|
88
|
-
await run("tar", ["-xzf", archive, "-C", partial]);
|
|
89
|
-
await rm(directory, { recursive: true, force: true });
|
|
90
|
-
await rename(partial, directory);
|
|
91
|
-
return interpreter;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/** Lays out the companion and returns its manifest: every file's path and SHA-256. */
|
|
95
|
-
async function assemble(staging: string, inputs: { llama: string; model: string; tokenizer: string; wheels: [string, string][] }) {
|
|
96
|
-
const files: Record<string, string> = {};
|
|
97
|
-
const put = async (path: string, from: string, sha: string) => {
|
|
98
|
-
await mkdir(join(staging, path, ".."), { recursive: true });
|
|
99
|
-
await copyFile(from, join(staging, path), constants.COPYFILE_FICLONE);
|
|
100
|
-
files[path] = sha;
|
|
101
|
-
};
|
|
102
|
-
const write = async (path: string, data: string | Uint8Array, sha = sha256(data)) => {
|
|
103
|
-
await mkdir(join(staging, path, ".."), { recursive: true });
|
|
104
|
-
await writeFile(join(staging, path), data);
|
|
105
|
-
files[path] = sha;
|
|
106
|
-
};
|
|
107
|
-
await put("model.gguf", inputs.model, MODEL.sha256);
|
|
108
|
-
await put("models/embedding/tokenizer.json", inputs.tokenizer, TOKENIZER.sha256);
|
|
109
|
-
for (const [name, path] of inputs.wheels) await put("wheels/" + name, path, WHEELS[name]!.sha256);
|
|
110
|
-
const unpacked = staging + ".llama";
|
|
111
|
-
await mkdir(unpacked, { recursive: true });
|
|
112
|
-
try {
|
|
113
|
-
await run("tar", ["-xzf", inputs.llama, "-C", unpacked]);
|
|
114
|
-
// Reading follows the archive's symlinks, so each copy is a plain file.
|
|
115
|
-
const member = async (name: string, sha: string, path: string) => {
|
|
116
|
-
const bytes = await readFile(join(unpacked, LLAMA.folder, name));
|
|
117
|
-
if (sha256(bytes) !== sha) throw new Error(`llama.cpp archive member does not match its pinned SHA-256: ${name}`);
|
|
118
|
-
await write(path, bytes, sha);
|
|
119
|
-
};
|
|
120
|
-
for (const [name, sha] of Object.entries(LLAMA.files)) await member(name, sha, "native/" + name);
|
|
121
|
-
await member("LICENSE", LLAMA.license, "notices/llama.cpp/LICENSE");
|
|
122
|
-
} finally { await rm(unpacked, { recursive: true, force: true }); }
|
|
123
|
-
// A wheel's filename starts with its distribution name and version.
|
|
124
|
-
await write("requirements.lock", Object.entries(WHEELS).map(([wheel, { sha256 }]) => {
|
|
125
|
-
const [name, version] = wheel.split("-");
|
|
126
|
-
return `${name}==${version} --hash=sha256:${sha256}\n`;
|
|
127
|
-
}).join(""));
|
|
128
|
-
await write("pins.json", JSON.stringify(PINS));
|
|
129
|
-
return files;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/** Runs provisioning so that it ends with Pi: an orphan would roll back its own folder under a later setup. Its error is the last
|
|
133
|
-
* line it wrote, not the whole command and log. */
|
|
134
|
-
async function provision(interpreter: string, args: string[]) {
|
|
135
|
-
const pending = run(interpreter, args, { maxBuffer: 16 * 1024 * 1024 }), stop = () => pending.child.kill("SIGTERM");
|
|
136
|
-
process.once("exit", stop);
|
|
137
|
-
try { await pending; }
|
|
138
|
-
catch (error) {
|
|
139
|
-
const lines = String((error as { stderr?: string }).stderr ?? "").trim().split("\n");
|
|
140
|
-
throw new Error(`provisioning failed: ${lines.at(-1) || (error as Error).message}`);
|
|
141
|
-
}
|
|
142
|
-
finally { process.off("exit", stop); }
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/** A directory lock: whoever creates it sets up. It is taken over when its holder has exited, or after six hours (a reused pid);
|
|
146
|
-
* the takeover renames the lock away first, so of several waiters only one takes it. */
|
|
147
|
-
async function lock(path: string): Promise<() => Promise<void>> {
|
|
148
|
-
for (;;) {
|
|
149
|
-
try {
|
|
150
|
-
await mkdir(path);
|
|
151
|
-
await writeFile(join(path, "pid"), String(process.pid));
|
|
152
|
-
return () => rm(path, { recursive: true, force: true });
|
|
153
|
-
} catch (error) { if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error; }
|
|
154
|
-
const holder = Number(await readFile(join(path, "pid"), "utf8").catch(() => ""));
|
|
155
|
-
const age = Date.now() - (await stat(path).catch(() => ({ mtimeMs: Date.now() }))).mtimeMs;
|
|
156
|
-
// A lock with no pid yet is a holder between its two steps, unless it has been like that for a while.
|
|
157
|
-
if (age > 6 * 3600_000 || (holder ? !alive(holder) : age > 30_000)) {
|
|
158
|
-
const taken = `${path}.stale-${randomUUID()}`;
|
|
159
|
-
if (await rename(path, taken).then(() => true, () => false)) await rm(taken, { recursive: true, force: true });
|
|
160
|
-
} else await new Promise(done => setTimeout(done, 2000));
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
const alive = (pid: number) => { try { process.kill(pid, 0); return true; } catch (error) { return (error as NodeJS.ErrnoException).code === "EPERM"; } };
|