pi-tuck 1.1.2 → 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 +65 -0
- package/README.md +7 -3
- 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 +29 -146
- 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 +28 -113
- 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
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Create private storage without changing any pre-existing directory's permissions.
|
|
2
|
+
* Callers must still verify existing storage before using it. */
|
|
3
|
+
export declare function createPrivateDirectory(directory: string): Promise<void>;
|
|
4
|
+
/** Only call for an empty file we just created exclusively inside verified private storage.
|
|
5
|
+
* The recorder retains its handle, protects the file, then verifies it before writing content. */
|
|
6
|
+
export declare function protectWindowsIncidentFile(path: string): Promise<void>;
|
|
7
|
+
/** Only the current user, SYSTEM and local administrators may receive access.
|
|
8
|
+
* No ACLs are changed: shared/custom storage fails closed before content is written. */
|
|
9
|
+
export declare function assertWindowsPrivateStorage(paths: readonly string[]): Promise<void>;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process';
|
|
2
|
+
import { mkdir } from 'node:fs/promises';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
// Node's Windows mode/uid fields cannot prove private storage. Check the native DACL instead.
|
|
5
|
+
// Pass paths as data, never PowerShell source; SID comparisons also work on non-English hosts.
|
|
6
|
+
const CHECK_ACL = `
|
|
7
|
+
$ErrorActionPreference = 'Stop'
|
|
8
|
+
try {
|
|
9
|
+
$user = [System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value
|
|
10
|
+
$trusted = @($user, 'S-1-5-18', 'S-1-5-32-544')
|
|
11
|
+
foreach ($path in (ConvertFrom-Json -InputObject $env:PI_TUCK_STORAGE_PATHS)) {
|
|
12
|
+
$item = Get-Item -LiteralPath $path -Force
|
|
13
|
+
if ($item.Attributes -band [System.IO.FileAttributes]::ReparsePoint) { throw 'Recorder reparse point' }
|
|
14
|
+
$acl = Get-Acl -LiteralPath $path
|
|
15
|
+
if ($trusted -notcontains $acl.GetOwner([System.Security.Principal.SecurityIdentifier]).Value) { throw 'Recorder owner' }
|
|
16
|
+
$rules = $acl.GetAccessRules($true, $true, [System.Security.Principal.SecurityIdentifier])
|
|
17
|
+
if ($rules.Count -eq 0) { throw 'Recorder missing DACL' }
|
|
18
|
+
foreach ($rule in $rules) {
|
|
19
|
+
if ($rule.AccessControlType -ne [System.Security.AccessControl.AccessControlType]::Allow) { continue }
|
|
20
|
+
$sid = $rule.IdentityReference.Value
|
|
21
|
+
if ($trusted -contains $sid) { continue }
|
|
22
|
+
if ($sid -eq 'S-1-3-0' -and ($rule.PropagationFlags -band [System.Security.AccessControl.PropagationFlags]::InheritOnly)) { continue }
|
|
23
|
+
throw 'Recorder non-private DACL'
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
} catch { [Console]::Error.WriteLine('Recorder requires private Windows storage'); exit 1 }
|
|
27
|
+
`;
|
|
28
|
+
// Build a fresh protected DACL, dropping both inherited and explicit foreign entries.
|
|
29
|
+
// Directories pass this security descriptor at creation, including missing ancestors;
|
|
30
|
+
// existing directories are returned unchanged by Directory.CreateDirectory.
|
|
31
|
+
const PRIVATE_DACL = `
|
|
32
|
+
$user = [System.Security.Principal.WindowsIdentity]::GetCurrent().User
|
|
33
|
+
$acl.SetOwner($user)
|
|
34
|
+
$acl.SetAccessRuleProtection($true, $false)
|
|
35
|
+
foreach ($sid in @($user.Value, 'S-1-5-18', 'S-1-5-32-544')) {
|
|
36
|
+
$identity = [System.Security.Principal.SecurityIdentifier]::new($sid)
|
|
37
|
+
$rule = [System.Security.AccessControl.FileSystemAccessRule]::new($identity, [System.Security.AccessControl.FileSystemRights]::FullControl, $inheritance, [System.Security.AccessControl.PropagationFlags]::None, [System.Security.AccessControl.AccessControlType]::Allow)
|
|
38
|
+
$acl.AddAccessRule($rule)
|
|
39
|
+
}
|
|
40
|
+
`;
|
|
41
|
+
const CREATE_DIRECTORY = `
|
|
42
|
+
$ErrorActionPreference = 'Stop'
|
|
43
|
+
try {
|
|
44
|
+
$acl = [System.Security.AccessControl.DirectorySecurity]::new()
|
|
45
|
+
$inheritance = [System.Security.AccessControl.InheritanceFlags]'ContainerInherit, ObjectInherit'
|
|
46
|
+
${PRIVATE_DACL}
|
|
47
|
+
foreach ($path in (ConvertFrom-Json -InputObject $env:PI_TUCK_STORAGE_PATHS)) {
|
|
48
|
+
[void][System.IO.Directory]::CreateDirectory($path, $acl)
|
|
49
|
+
}
|
|
50
|
+
} catch { [Console]::Error.WriteLine('Recorder could not create private Windows directory'); exit 1 }
|
|
51
|
+
`;
|
|
52
|
+
const PROTECT_FILE = `
|
|
53
|
+
$ErrorActionPreference = 'Stop'
|
|
54
|
+
try {
|
|
55
|
+
$acl = [System.Security.AccessControl.FileSecurity]::new()
|
|
56
|
+
$inheritance = [System.Security.AccessControl.InheritanceFlags]::None
|
|
57
|
+
${PRIVATE_DACL}
|
|
58
|
+
foreach ($path in (ConvertFrom-Json -InputObject $env:PI_TUCK_STORAGE_PATHS)) {
|
|
59
|
+
$item = Get-Item -LiteralPath $path -Force
|
|
60
|
+
if ($item.PSIsContainer -or ($item.Attributes -band [System.IO.FileAttributes]::ReparsePoint)) { throw 'Recorder file type' }
|
|
61
|
+
Set-Acl -LiteralPath $path -AclObject $acl
|
|
62
|
+
}
|
|
63
|
+
} catch { [Console]::Error.WriteLine('Recorder could not protect Windows file'); exit 1 }
|
|
64
|
+
`;
|
|
65
|
+
function runAclCommand(script, paths) {
|
|
66
|
+
const powershell = join(process.env.SystemRoot ?? 'C:\\Windows', 'System32', 'WindowsPowerShell', 'v1.0', 'powershell.exe');
|
|
67
|
+
return new Promise((resolve, reject) => {
|
|
68
|
+
execFile(powershell, ['-NoLogo', '-NoProfile', '-NonInteractive', '-Command', script], {
|
|
69
|
+
env: { ...process.env, PI_TUCK_STORAGE_PATHS: JSON.stringify(paths) }, windowsHide: true, timeout: 10_000, maxBuffer: 16_384,
|
|
70
|
+
}, error => error ? reject(error) : resolve());
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
/** Create private storage without changing any pre-existing directory's permissions.
|
|
74
|
+
* Callers must still verify existing storage before using it. */
|
|
75
|
+
export async function createPrivateDirectory(directory) {
|
|
76
|
+
if (process.platform === 'win32')
|
|
77
|
+
await runAclCommand(CREATE_DIRECTORY, [directory]);
|
|
78
|
+
else
|
|
79
|
+
await mkdir(directory, { recursive: true, mode: 0o700 });
|
|
80
|
+
}
|
|
81
|
+
/** Only call for an empty file we just created exclusively inside verified private storage.
|
|
82
|
+
* The recorder retains its handle, protects the file, then verifies it before writing content. */
|
|
83
|
+
export function protectWindowsIncidentFile(path) {
|
|
84
|
+
return runAclCommand(PROTECT_FILE, [path]);
|
|
85
|
+
}
|
|
86
|
+
/** Only the current user, SYSTEM and local administrators may receive access.
|
|
87
|
+
* No ACLs are changed: shared/custom storage fails closed before content is written. */
|
|
88
|
+
export function assertWindowsPrivateStorage(paths) {
|
|
89
|
+
return runAclCommand(CHECK_ACL, paths);
|
|
90
|
+
}
|
package/index.ts
CHANGED
|
@@ -1,22 +1,17 @@
|
|
|
1
|
-
import { VERSION, getAgentDir, type ExtensionAPI
|
|
2
|
-
import {
|
|
3
|
-
import { join
|
|
1
|
+
import { VERSION, getAgentDir, type ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
4
|
import { installCompaction } from "./src/compaction";
|
|
5
5
|
import { validCutAt } from "./src/budget";
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import { ensureSemanticRuntime } from "./src/setup";
|
|
6
|
+
import { notify } from "./src/notify";
|
|
7
|
+
import { cleanupLegacyFiles } from "./src/legacy-cleanup";
|
|
9
8
|
|
|
10
9
|
export type { CompactionOptions } from "./src/compaction";
|
|
11
10
|
export { installCompaction } from "./src/compaction";
|
|
12
11
|
export { inspectIncident, readableIncident } from "./src/diagnostic-replay.js";
|
|
13
|
-
export { createSemanticRecall, SemanticRecallController } from "./src/semantic-recall";
|
|
14
|
-
export type { SemanticRecallConfig, PriorResult, SemanticState, SemanticSetup } from "./src/semantic-recall";
|
|
15
|
-
export { ensureSemanticRuntime } from "./src/setup";
|
|
16
12
|
|
|
17
|
-
/** pi-tuck as a Pi extension: compaction and history recall
|
|
18
|
-
*
|
|
19
|
-
* (a number above 0 and at most 1) is the fraction of the input room a cut is triggered at, 0.6 by default. */
|
|
13
|
+
/** pi-tuck as a Pi extension: compaction and history recall. `"cutAt"` in `pi-tuck.json` in Pi's agent folder (a number above 0 and at most
|
|
14
|
+
* 1) fixes the cut point as a fraction of the input room; without it, the cut point adapts. */
|
|
20
15
|
/** The oldest Pi with every hook and API pi-tuck uses, and the oldest its tests pass on (Pi 1.2 added the custom message that starts a
|
|
21
16
|
* run to before_agent_start, which the prompt's cut counts). */
|
|
22
17
|
export const PI_FLOOR = "1.2.0";
|
|
@@ -39,84 +34,25 @@ export default async function (pi: ExtensionAPI) {
|
|
|
39
34
|
});
|
|
40
35
|
return;
|
|
41
36
|
}
|
|
42
|
-
const semanticRecall = createSemanticRecall();
|
|
43
|
-
// `/tuck-semantic off` holds for the rest of the session, including over a setup that finishes later.
|
|
44
37
|
const settings = readSettings(join(getAgentDir(), "pi-tuck.json"));
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
else if (semanticRecall.status().setup === "off-user") semanticRecall.setSetup("starting", { installed: existsSync(runtimeConfigPath(defaultRoot())) });
|
|
50
|
-
} });
|
|
51
|
-
if (settings.semanticRecall === false) {
|
|
52
|
-
semanticRecall.setSetup("off-settings");
|
|
53
|
-
pi.on("session_start", () => { semanticRecall.setSetup("off-settings"); });
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
let session = 0, said = "", announced = false, setup: Promise<unknown> | undefined;
|
|
57
|
-
// A context outlives its session (a setup can finish after /new); a notice to an ended session is dropped.
|
|
58
|
-
// With no UI (pi -p), Pi shows no notices, so a settings error or an off or failing semantic recall would go unsaid: write each to stderr
|
|
59
|
-
// instead, once per session. stderr is the operator's; it never enters the model's context.
|
|
60
|
-
const stderrSaid = new Set<string>();
|
|
61
|
-
const say = (ctx: ExtensionContext, message: string, level: "info" | "warning") => {
|
|
62
|
-
if (!ctx.hasUI) { if (!stderrSaid.has(message)) { stderrSaid.add(message); try { process.stderr.write(message + "\n"); } catch { /* closed */ } } return; }
|
|
63
|
-
try { ctx.ui.notify(message, level); } catch { /* ended */ }
|
|
64
|
-
};
|
|
65
|
-
const once = (ctx: ExtensionContext, message: string, level: "info" | "warning") => { if (said !== message) { said = message; say(ctx, message, level); } };
|
|
66
|
-
const announce = (ctx: ExtensionContext, state: Parameters<typeof semanticRecall.setSetup>[0], detail?: Parameters<typeof semanticRecall.setSetup>[1]) => {
|
|
67
|
-
semanticRecall.setSetup(state, detail);
|
|
68
|
-
const notice = semanticRecall.setupNotice();
|
|
69
|
-
if (notice) (state === "off-platform" || state === "off-project" ? once : say)(ctx, notice.message, notice.level);
|
|
70
|
-
};
|
|
71
|
-
pi.on("session_shutdown", () => { session++; });
|
|
72
|
-
pi.on("session_start", (_event, ctx) => {
|
|
73
|
-
const current = ++session;
|
|
74
|
-
stderrSaid.clear();
|
|
38
|
+
const handle = await installCompaction(pi, { cutAt: settings.cutAt });
|
|
39
|
+
let said = false;
|
|
40
|
+
pi.on("session_start", async (_event, ctx) => {
|
|
41
|
+
await cleanupLegacyFiles(ctx);
|
|
75
42
|
if (handle.state?.(ctx).kind === "standingDown") return;
|
|
76
|
-
if (settings.error
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
// Semantic recall keeps its runtime and indexes outside the project it serves, so not when Pi runs in a folder that holds its
|
|
80
|
-
// agent folder, such as the home folder.
|
|
81
|
-
if (within(real(getAgentDir()), real(ctx.cwd)))
|
|
82
|
-
return announce(ctx, "off-project");
|
|
83
|
-
// A run with no UI (pi -p, RPC, JSON) uses a runtime that is already installed: that needs no download. It does not start a first-time
|
|
84
|
-
// setup, which would outlast a one-shot run, and it says why, so tuck_search and /tuck name the real state.
|
|
85
|
-
const installed = existsSync(runtimeConfigPath(defaultRoot()));
|
|
86
|
-
if (!ctx.hasUI && !installed) return announce(ctx, "not-installed");
|
|
87
|
-
// The download's announcement outlives the session that made it: a session that starts while the shared setup is still running
|
|
88
|
-
// owes the "set up" notice once, as before the refactor.
|
|
89
|
-
semanticRecall.setSetup("starting", { installed, announced });
|
|
90
|
-
setup ??= ensureSemanticRuntime(defaultRoot(), message => {
|
|
91
|
-
announced = true;
|
|
92
|
-
if (current === session && semanticRecall.status().setup !== "off-user") announce(ctx, "starting", { installed, notice: message, announced: true });
|
|
93
|
-
});
|
|
94
|
-
setup.then(async () => {
|
|
95
|
-
if (current !== session || semanticRecall.status().setup === "off-user") return;
|
|
96
|
-
try {
|
|
97
|
-
await handle.setSemanticEnabled(true, ctx);
|
|
98
|
-
if (current === session && semanticRecall.status().setup !== "off-user") { announce(ctx, "ready", { announced }); announced = false; }
|
|
99
|
-
} catch (error) {
|
|
100
|
-
if (current === session && semanticRecall.status().setup !== "off-user") announce(ctx, "failed", { stage: "start", reason: reason(error) });
|
|
101
|
-
}
|
|
102
|
-
}, error => {
|
|
103
|
-
setup = undefined; // the next session start tries again
|
|
104
|
-
if (current === session && semanticRecall.status().setup !== "off-user") announce(ctx, "failed", { stage: "setup", reason: reason(error) });
|
|
105
|
-
});
|
|
43
|
+
if (settings.error && !said) {
|
|
44
|
+
said = notify(ctx, `pi-tuck: ${settings.error}; using the defaults.`, "warning");
|
|
45
|
+
}
|
|
106
46
|
});
|
|
107
47
|
}
|
|
108
48
|
|
|
109
|
-
function readSettings(path: string): {
|
|
49
|
+
function readSettings(path: string): { cutAt?: number; error?: string } {
|
|
110
50
|
let text: string;
|
|
111
51
|
try { text = readFileSync(path, "utf8"); } catch { return {}; }
|
|
112
52
|
try {
|
|
113
53
|
const value = JSON.parse(text);
|
|
114
|
-
if (value && typeof value === "object" &&
|
|
115
|
-
(value.cutAt === undefined || validCutAt(value.cutAt))) return value;
|
|
54
|
+
if (value && typeof value === "object" &&
|
|
55
|
+
(value.cutAt === undefined || validCutAt(value.cutAt))) return { cutAt: value.cutAt };
|
|
116
56
|
} catch { /* reported below */ }
|
|
117
|
-
return { error: `${path} should be a JSON object like {"
|
|
57
|
+
return { error: `${path} should be a JSON object like {"cutAt": 0.8}, with cutAt a number above 0 and at most 1` };
|
|
118
58
|
}
|
|
119
|
-
|
|
120
|
-
const reason = (error: unknown) => error instanceof Error ? error.message : String(error);
|
|
121
|
-
const within = (path: string, folder: string) => path === folder || path.startsWith(folder.endsWith(sep) ? folder : folder + sep);
|
|
122
|
-
const real = (path: string) => { try { return realpathSync(path); } catch { return path; } };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-tuck",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "Deterministic compaction for Pi with no model call, plus exact recall of earlier history.
|
|
3
|
+
"version": "1.2.0",
|
|
4
|
+
"description": "Deterministic compaction for Pi with no model call, plus exact recall of earlier history.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-package",
|
|
7
7
|
"pi",
|
|
@@ -9,7 +9,6 @@
|
|
|
9
9
|
"compaction",
|
|
10
10
|
"context",
|
|
11
11
|
"recall",
|
|
12
|
-
"semantic-search",
|
|
13
12
|
"pi-coding-agent",
|
|
14
13
|
"coding-agent",
|
|
15
14
|
"context-window",
|
|
@@ -26,7 +25,8 @@
|
|
|
26
25
|
"type": "module",
|
|
27
26
|
"os": [
|
|
28
27
|
"darwin",
|
|
29
|
-
"linux"
|
|
28
|
+
"linux",
|
|
29
|
+
"win32"
|
|
30
30
|
],
|
|
31
31
|
"engines": {
|
|
32
32
|
"node": ">=22.19.0"
|
|
@@ -46,14 +46,11 @@
|
|
|
46
46
|
"index.ts",
|
|
47
47
|
"src",
|
|
48
48
|
"dist",
|
|
49
|
-
"scripts/eg2-runtime.py",
|
|
50
|
-
"scripts/provision-eg2.py",
|
|
51
|
-
"scripts/semantic-worker.py",
|
|
52
49
|
"CHANGELOG.md"
|
|
53
50
|
],
|
|
54
51
|
"scripts": {
|
|
55
52
|
"check": "tsc -p tsconfig.tests.json",
|
|
56
|
-
"test": "vitest run --config vitest.config.ts
|
|
53
|
+
"test": "vitest run --config vitest.config.ts",
|
|
57
54
|
"build": "node scripts/build.mjs",
|
|
58
55
|
"prepack": "npm run build"
|
|
59
56
|
},
|
package/src/build-id.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const BUILD_ID = "
|
|
1
|
+
export const BUILD_ID = "f653b17921c5383797b70c8962152e6b4537a7769e05118290fc34d492a06ec1";
|