@cortexkit/opencode-magic-context 0.49.84 → 0.49.86
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/dist/{announcement-p41dde4h.js → announcement-fnyxaahs.js} +3 -1
- package/dist/auto-search-worker.js +3 -3
- package/dist/chunk-embedding-write-worker.js +5 -4
- package/dist/{context-3sajdysh.js → context-xesm7rj3.js} +27 -26
- package/dist/{derive-budgets-0kcek5ex.js → derive-budgets-90869322.js} +3 -1
- package/dist/{embedding-backfill-k994xdas.js → embedding-backfill-v6sw58m8.js} +7 -5
- package/dist/embedding-worker.js +15 -27
- package/dist/features/magic-context/memory/embedding-worker-boundary.test-support.d.ts +24 -0
- package/dist/features/magic-context/memory/embedding-worker-boundary.test-support.d.ts.map +1 -0
- package/dist/features/magic-context/memory/embedding-worker-client.d.ts +35 -1
- package/dist/features/magic-context/memory/embedding-worker-client.d.ts.map +1 -1
- package/dist/features/magic-context/memory/embedding-worker-loop.d.ts +57 -0
- package/dist/features/magic-context/memory/embedding-worker-loop.d.ts.map +1 -0
- package/dist/features/magic-context/memory/verification-paths.d.ts.map +1 -1
- package/dist/features/magic-context/project-embedding-registry.d.ts +6 -1
- package/dist/features/magic-context/project-embedding-registry.d.ts.map +1 -1
- package/dist/features/magic-context/storage-db.d.ts.map +1 -1
- package/dist/hooks/magic-context/lkg-recovery-fence.d.ts.map +1 -1
- package/dist/{index-xjfan0za.js → index-022w6314.js} +37 -37
- package/dist/{index-tvbkemec.js → index-08jfpny9.js} +1 -1
- package/dist/{index-fgy6hnr3.js → index-1bqkqde0.js} +5 -4
- package/dist/{index-0wehkw71.js → index-1pf5qrx4.js} +4 -4
- package/dist/{index-tqnvn9pa.js → index-25nktq8n.js} +3 -3
- package/dist/{index-98qy103c.js → index-2kf73btc.js} +1 -1
- package/dist/{index-vpw46557.js → index-3dtzyred.js} +1 -1
- package/dist/{index-bd34njzg.js → index-4ntnt8ee.js} +1 -1
- package/dist/{index-w2wpx7t5.js → index-4w5rx7de.js} +1 -1
- package/dist/{index-xd0ww0ns.js → index-798s7c8s.js} +1 -1
- package/dist/{index-fpqdbb67.js → index-7svecf3m.js} +3 -3
- package/dist/{v2/server-fvnzcnww.js → index-c5tmzym6.js} +192 -11
- package/dist/{index-k0jnsjcp.js → index-d0rtbv0f.js} +6 -6
- package/dist/{index-qybp04ff.js → index-e32b3x4n.js} +3 -3
- package/dist/{index-cfy5k1pa.js → index-gxdc95cp.js} +5 -5
- package/dist/{index-w8dcjt7f.js → index-k0tb2z28.js} +1 -1
- package/dist/{index-zb4h9nrk.js → index-k50xhjw1.js} +202 -14
- package/dist/{index-szwrpyac.js → index-nny4gm06.js} +1 -1
- package/dist/{index-shpyv8gq.js → index-s8ks5nz4.js} +16 -9
- package/dist/{index-d028gmgp.js → index-ttk3trse.js} +14 -14
- package/dist/{index-skwwbcnv.js → index-w6v4dw5f.js} +24 -24
- package/dist/{index-5wjv4c41.js → index-xccywqdj.js} +15 -15
- package/dist/{index-kthfyf45.js → index-zmqx48vt.js} +3 -3
- package/dist/index.js +33 -33
- package/dist/migration-worker.js +4 -3
- package/dist/{project-embedding-registry-y3z36mjy.js → project-embedding-registry-q80scexh.js} +6 -4
- package/dist/raw-ordinal-warmup-worker.js +2 -2
- package/dist/{recomp-orchestrator-m2s3jpvj.js → recomp-orchestrator-7ecg8ryx.js} +16 -15
- package/dist/{render-p2j7rn9a.js → render-yqp7q79f.js} +8 -7
- package/dist/{search-archive-hz9xh7nh.js → search-archive-r4h96j2z.js} +2 -2
- package/dist/{search-ksx0f1qd.js → search-bw22d7zc.js} +12 -10
- package/dist/shared/storage-file-mutex.d.ts.map +1 -1
- package/dist/shared/storage-permissions.d.ts +5 -3
- package/dist/shared/storage-permissions.d.ts.map +1 -1
- package/dist/{store-x0swqb5c.js → store-n4yz3n66.js} +2 -2
- package/dist/{store-reader-tsv1w4vt.js → store-reader-nf8z86n9.js} +1 -1
- package/dist/{transform-postprocess-phase-1wb2rrr1.js → transform-postprocess-phase-t99yj9vt.js} +17 -16
- package/dist/v2/auto-search-worker.js +3 -3
- package/dist/v2/chunk-embedding-write-worker.js +5 -4
- package/dist/v2/{context-at9nje0j.js → context-k8sx1nyy.js} +39 -39
- package/dist/v2/{derive-budgets-xbwcap8k.js → derive-budgets-ecjy1aqh.js} +4 -2
- package/dist/v2/{embedding-backfill-hjvjxp7d.js → embedding-backfill-k8kqnwfq.js} +7 -5
- package/dist/v2/embedding-worker.js +15 -27
- package/dist/v2/migration-worker.js +4 -3
- package/dist/v2/{project-embedding-registry-mzsvyads.js → project-embedding-registry-2jp1antr.js} +6 -4
- package/dist/v2/raw-ordinal-warmup-worker.js +2 -2
- package/dist/v2/{recomp-orchestrator-nhef90gc.js → recomp-orchestrator-ssde9ft1.js} +17 -16
- package/dist/v2/{render-c9r2g2jv.js → render-fezgh7pa.js} +8 -7
- package/dist/v2/{search-archive-80ckv9kq.js → search-archive-zz1kza50.js} +2 -2
- package/dist/v2/{search-t06js5dc.js → search-m251amra.js} +11 -9
- package/dist/v2/{server-4z964n1c.js → server-3p6b0qya.js} +1 -1
- package/dist/v2/{server-3cndh146.js → server-4wwwm2ya.js} +24 -24
- package/dist/v2/{server-mr9rqn0y.js → server-97w51d8n.js} +4 -4
- package/dist/v2/{server-d706s5xn.js → server-b93f0bqh.js} +205 -17
- package/dist/v2/{server-dfesqfv1.js → server-brgandk4.js} +1 -1
- package/dist/v2/{server-w1jfaxas.js → server-cwtehwbb.js} +1 -1
- package/dist/v2/{server-y4favn4d.js → server-e2k9mt86.js} +1 -1
- package/dist/v2/{server-3hhakg5a.js → server-fsw8kbng.js} +13 -13
- package/dist/v2/{server-68q80y27.js → server-hn8b7sfe.js} +16 -9
- package/dist/v2/{server-pejjj0nv.js → server-mbkwcwgw.js} +1 -1
- package/dist/v2/{server-ry6rpspz.js → server-n5zr7190.js} +15 -15
- package/dist/v2/{server-210rc0z9.js → server-ps6rz88g.js} +1 -1
- package/dist/{index-fvnzcnww.js → v2/server-r2es6zt7.js} +192 -11
- package/dist/v2/{server-bt546axy.js → server-rz4snd3x.js} +3 -3
- package/dist/v2/{server-863bwkga.js → server-ssg29wjg.js} +6 -6
- package/dist/v2/{server-hhw6rzsn.js → server-w7mpvkpj.js} +1 -1
- package/dist/v2/{server-sy98vksf.js → server-xa8fefhw.js} +3 -3
- package/dist/v2/{server-gx1s06dx.js → server-y31y6gws.js} +5 -4
- package/dist/v2/{server-cce6m31c.js → server-ytqps2xa.js} +1 -1
- package/dist/v2/server.js +6 -4
- package/dist/v2/{store-reader-paamf0ac.js → store-reader-2vhx33n6.js} +1 -1
- package/dist/v2/{store-h3s95ywe.js → store-y56y034s.js} +2 -2
- package/dist/v2/{v2-archive-reader-1zzteaa5.js → v2-archive-reader-e2w39c3p.js} +1 -1
- package/dist/v2/{wrapup-orchestrator-9y98qcqz.js → wrapup-orchestrator-eg5b1p89.js} +16 -15
- package/dist/{v2-archive-reader-3yjxaj5z.js → v2-archive-reader-8dsngf2j.js} +1 -1
- package/dist/{wrapup-orchestrator-sa3a075a.js → wrapup-orchestrator-nmk6yf7f.js} +17 -16
- package/package.json +1 -1
- package/src/shared/storage-file-mutex-review.test.ts +125 -2
- package/src/shared/storage-file-mutex.ts +17 -8
- package/src/shared/storage-permissions-creators.test.ts +194 -0
- package/src/shared/storage-permissions-startup.test.ts +476 -0
- package/src/shared/storage-permissions.ts +227 -12
- package/src/tui/compaction-off.ts +2 -2
- package/src/tui/slots/sidebar-content-render.test.ts +309 -0
- package/src/tui/slots/sidebar-content.tsx +134 -99
- package/src/tui-compiled/compaction-off.ts +2 -2
- package/src/tui-compiled/slots/sidebar-content.tsx +321 -247
- /package/dist/{index-rdh63md0.js → index-1y4y98r1.js} +0 -0
- /package/dist/v2/{server-9nk5h6w6.js → server-qyazxy92.js} +0 -0
|
@@ -5,11 +5,13 @@
|
|
|
5
5
|
* applying POSIX modes. The default preserves the historical owner-only policy.
|
|
6
6
|
*/
|
|
7
7
|
import { randomBytes } from "node:crypto";
|
|
8
|
-
import type { WriteStream } from "node:fs";
|
|
8
|
+
import type { Stats, WriteStream } from "node:fs";
|
|
9
9
|
import {
|
|
10
10
|
chmodSync,
|
|
11
11
|
closeSync,
|
|
12
|
+
constants,
|
|
12
13
|
createWriteStream,
|
|
14
|
+
fstatSync,
|
|
13
15
|
fsyncSync,
|
|
14
16
|
lstatSync,
|
|
15
17
|
mkdirSync,
|
|
@@ -18,6 +20,7 @@ import {
|
|
|
18
20
|
readlinkSync,
|
|
19
21
|
readSync,
|
|
20
22
|
renameSync,
|
|
23
|
+
rmdirSync,
|
|
21
24
|
rmSync,
|
|
22
25
|
symlinkSync,
|
|
23
26
|
writeFileSync,
|
|
@@ -25,12 +28,55 @@ import {
|
|
|
25
28
|
} from "node:fs";
|
|
26
29
|
import type { FileHandle } from "node:fs/promises";
|
|
27
30
|
import { open as openAsync, writeFile as writeFileAsync } from "node:fs/promises";
|
|
28
|
-
import { dirname, join } from "node:path";
|
|
31
|
+
import { basename, dirname, join, resolve } from "node:path";
|
|
32
|
+
import { isPidAlive } from "./rpc-utils";
|
|
29
33
|
|
|
30
34
|
let enforcePrivateStoragePermissions = true;
|
|
35
|
+
const STORAGE_PERMISSIONS_POLICY_VERSION = 1;
|
|
36
|
+
const STORAGE_PERMISSIONS_MARKER = `.storage-permissions-v${STORAGE_PERMISSIONS_POLICY_VERSION}`;
|
|
37
|
+
const STORAGE_PERMISSIONS_MARKER_BYTES = Buffer.from(`${STORAGE_PERMISSIONS_POLICY_VERSION}\n`);
|
|
38
|
+
const STORAGE_PERMISSIONS_MARKER_MAX_AGE_MS = 24 * 60 * 60_000;
|
|
39
|
+
const LEGACY_FENCE_TEMP_MIN_AGE_MS = 10 * 60_000;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Storage roots that this process checked with tightenStorageTreeSync. Only a
|
|
43
|
+
* root has a marker. Each database open checks its root, so a process that
|
|
44
|
+
* writes storage files knows its root.
|
|
45
|
+
*/
|
|
46
|
+
const storagePermissionRoots = new Set<string>();
|
|
47
|
+
|
|
48
|
+
function invalidateStoragePermissionMarkers(directory: string): void {
|
|
49
|
+
// Stop at the storage root: an ancestor of the root has no applicable marker.
|
|
50
|
+
// A directory that is not in a known root continues to the file system
|
|
51
|
+
// root, because a marker that stays can keep the approval of a loose tree.
|
|
52
|
+
for (let current = resolve(directory); ; current = dirname(current)) {
|
|
53
|
+
const marker = join(current, STORAGE_PERMISSIONS_MARKER);
|
|
54
|
+
try {
|
|
55
|
+
rmSync(marker, { force: true });
|
|
56
|
+
} catch (error) {
|
|
57
|
+
const stat = lstatSync(marker, { throwIfNoEntry: false });
|
|
58
|
+
// Only a regular file can be a certificate.
|
|
59
|
+
// A read-only mount can reject an unlink even if the marker is absent.
|
|
60
|
+
if (stat?.isFile() || (!stat && (error as NodeJS.ErrnoException).code !== "EROFS")) {
|
|
61
|
+
throw error;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (storagePermissionRoots.has(current) || dirname(current) === current) return;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
31
67
|
|
|
32
68
|
export function setStoragePrivatePermissionEnforcement(enforce: boolean): void {
|
|
33
69
|
enforcePrivateStoragePermissions = enforce;
|
|
70
|
+
if (enforce || process.platform === "win32") return;
|
|
71
|
+
// A host can keep its database open across a reload, so the next open does not
|
|
72
|
+
// run the startup walk. Files written while enforcement is off can be loose.
|
|
73
|
+
for (const root of storagePermissionRoots) {
|
|
74
|
+
try {
|
|
75
|
+
rmSync(join(root, STORAGE_PERMISSIONS_MARKER), { force: true });
|
|
76
|
+
} catch {
|
|
77
|
+
// Do not stop the configuration change. The startup walk reports this failure.
|
|
78
|
+
}
|
|
79
|
+
}
|
|
34
80
|
}
|
|
35
81
|
|
|
36
82
|
export function shouldEnforcePrivateStoragePermissions(): boolean {
|
|
@@ -61,7 +107,10 @@ export function ensurePrivateStorageDirectorySync(directory: string): void {
|
|
|
61
107
|
const stat = lstatSync(directory);
|
|
62
108
|
if (!stat.isDirectory() || stat.isSymbolicLink())
|
|
63
109
|
throw new Error("Private storage directory is not a real directory");
|
|
64
|
-
if (process.platform !== "win32")
|
|
110
|
+
if (process.platform !== "win32") {
|
|
111
|
+
if ((stat.mode & 0o777) !== 0o700) invalidateStoragePermissionMarkers(directory);
|
|
112
|
+
chmodSync(directory, 0o700);
|
|
113
|
+
}
|
|
65
114
|
}
|
|
66
115
|
|
|
67
116
|
/** Create or replace a storage file using a private mode from the first byte. */
|
|
@@ -105,15 +154,17 @@ export function syncStorageDirectorySync(directory: string): void {
|
|
|
105
154
|
}
|
|
106
155
|
}
|
|
107
156
|
|
|
108
|
-
/** Publish a safety record only after both its bytes and directory entry are durable.
|
|
157
|
+
/** Publish a safety record only after both its bytes and directory entry are durable.
|
|
158
|
+
* Hold the mutex through this call to use a constant tempPath.
|
|
159
|
+
*/
|
|
109
160
|
export function writeStorageFileDurableAtomicSync(
|
|
110
161
|
filePath: string,
|
|
111
162
|
content: string,
|
|
112
163
|
afterTempSync?: () => void,
|
|
164
|
+
tempPath = `${filePath}.${process.pid}.${randomBytes(12).toString("hex")}.tmp`,
|
|
113
165
|
): void {
|
|
114
166
|
const directory = dirname(filePath);
|
|
115
167
|
ensurePrivateStorageDirectorySync(directory);
|
|
116
|
-
const tempPath = `${filePath}.${process.pid}.${randomBytes(12).toString("hex")}.tmp`;
|
|
117
168
|
let fd: number | undefined;
|
|
118
169
|
try {
|
|
119
170
|
fd = openStorageFileSync(tempPath, "wx", true);
|
|
@@ -218,24 +269,140 @@ export async function createStorageFileHandleAsync(
|
|
|
218
269
|
return openAsync(filePath, flags, privateMode ? 0o600 : undefined);
|
|
219
270
|
}
|
|
220
271
|
|
|
272
|
+
function readStoragePermissionCertificate(marker: string): Stats | undefined {
|
|
273
|
+
const fd = openSync(marker, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
274
|
+
try {
|
|
275
|
+
const stat = fstatSync(fd);
|
|
276
|
+
// File times can have fractions of a millisecond. Use the precision of Date.now().
|
|
277
|
+
const age = Date.now() - Math.floor(stat.mtimeMs);
|
|
278
|
+
if (
|
|
279
|
+
!stat.isFile() ||
|
|
280
|
+
(stat.mode & 0o7777) !== 0o600 ||
|
|
281
|
+
stat.uid !== process.getuid?.() ||
|
|
282
|
+
stat.size !== STORAGE_PERMISSIONS_MARKER_BYTES.length ||
|
|
283
|
+
age < 0 ||
|
|
284
|
+
age >= STORAGE_PERMISSIONS_MARKER_MAX_AGE_MS
|
|
285
|
+
) {
|
|
286
|
+
return undefined;
|
|
287
|
+
}
|
|
288
|
+
// Read one more byte in case the file grew after fstat.
|
|
289
|
+
const buffer = Buffer.alloc(STORAGE_PERMISSIONS_MARKER_BYTES.length + 1);
|
|
290
|
+
const bytesRead = readSync(fd, buffer, 0, buffer.length, 0);
|
|
291
|
+
if (
|
|
292
|
+
bytesRead === STORAGE_PERMISSIONS_MARKER_BYTES.length &&
|
|
293
|
+
buffer.subarray(0, bytesRead).equals(STORAGE_PERMISSIONS_MARKER_BYTES)
|
|
294
|
+
) {
|
|
295
|
+
return stat;
|
|
296
|
+
}
|
|
297
|
+
} finally {
|
|
298
|
+
closeSync(fd);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
221
302
|
/** Tighten existing entries during storage startup; symlinks are never followed. */
|
|
222
|
-
export function tightenStorageTreeSync(
|
|
223
|
-
|
|
303
|
+
export function tightenStorageTreeSync(
|
|
304
|
+
directory: string,
|
|
305
|
+
dbPath = join(directory, "context.db"),
|
|
306
|
+
): { tightened: number; failures: number } {
|
|
307
|
+
if (process.platform === "win32") {
|
|
224
308
|
return { tightened: 0, failures: 0 };
|
|
225
309
|
}
|
|
310
|
+
const root = resolve(directory);
|
|
311
|
+
const marker = join(root, STORAGE_PERMISSIONS_MARKER);
|
|
226
312
|
let tightened = 0;
|
|
227
313
|
let failures = 0;
|
|
228
|
-
|
|
229
|
-
|
|
314
|
+
let keptLegacyTemp = false;
|
|
315
|
+
const removeMarker = (): boolean => {
|
|
230
316
|
try {
|
|
231
|
-
|
|
317
|
+
rmSync(marker, { force: true });
|
|
318
|
+
return true;
|
|
232
319
|
} catch {
|
|
320
|
+
return false;
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
if (!shouldEnforcePrivateStoragePermissions()) {
|
|
324
|
+
storagePermissionRoots.add(root);
|
|
325
|
+
if (!removeMarker()) failures++;
|
|
326
|
+
return { tightened, failures };
|
|
327
|
+
}
|
|
328
|
+
let rootStat: Stats;
|
|
329
|
+
try {
|
|
330
|
+
rootStat = lstatSync(root);
|
|
331
|
+
if (!rootStat.isDirectory()) return { tightened, failures: 1 };
|
|
332
|
+
} catch {
|
|
333
|
+
return { tightened, failures: 1 };
|
|
334
|
+
}
|
|
335
|
+
storagePermissionRoots.add(root);
|
|
336
|
+
let certificate: Stats | undefined;
|
|
337
|
+
let discardMarker = false;
|
|
338
|
+
try {
|
|
339
|
+
certificate = readStoragePermissionCertificate(marker);
|
|
340
|
+
discardMarker = certificate === undefined;
|
|
341
|
+
} catch (error) {
|
|
342
|
+
discardMarker = (error as NodeJS.ErrnoException).code !== "ENOENT";
|
|
343
|
+
}
|
|
344
|
+
// undefined: no loose entry. false: loose entry, marker stays. true: marker removed.
|
|
345
|
+
let markerRemoved: boolean | undefined;
|
|
346
|
+
const fenceDirectory = join(root, "lkg-recovery-fences");
|
|
347
|
+
const visit = (entryPath: string, recursive = true, optional = false, stat?: Stats): void => {
|
|
348
|
+
try {
|
|
349
|
+
stat ??= lstatSync(entryPath);
|
|
350
|
+
} catch (error) {
|
|
351
|
+
if (!optional || (error as NodeJS.ErrnoException).code !== "ENOENT") failures++;
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
if (entryPath === root && !stat.isDirectory()) {
|
|
233
355
|
failures++;
|
|
234
356
|
return;
|
|
235
357
|
}
|
|
236
358
|
if (stat.isSymbolicLink()) return;
|
|
359
|
+
// A mode change must not turn an invalid marker into a certificate.
|
|
360
|
+
if (entryPath === marker && !stat.isDirectory()) return;
|
|
361
|
+
if (!certificate && dirname(entryPath) === fenceDirectory) {
|
|
362
|
+
const name = basename(entryPath);
|
|
363
|
+
const legacyTemp = stat.isFile() && /^.+\.json\..+\.tmp$/.test(name);
|
|
364
|
+
if (legacyTemp) {
|
|
365
|
+
const publisherPid = /^.+\.json\.([1-9]\d*)\.[a-f0-9]{24}\.tmp$/.exec(name)?.[1];
|
|
366
|
+
// The legacy writer puts its PID in the name. Age does not prove that it stopped.
|
|
367
|
+
if (
|
|
368
|
+
Date.now() - stat.mtimeMs <= LEGACY_FENCE_TEMP_MIN_AGE_MS ||
|
|
369
|
+
(publisherPid && isPidAlive(Number(publisherPid)) !== "dead")
|
|
370
|
+
) {
|
|
371
|
+
// The certificate can prevent a check of these files at the next start.
|
|
372
|
+
keptLegacyTemp = true;
|
|
373
|
+
} else {
|
|
374
|
+
try {
|
|
375
|
+
rmSync(entryPath, { force: true });
|
|
376
|
+
} catch {
|
|
377
|
+
failures++;
|
|
378
|
+
}
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
if (stat.isDirectory() && name.endsWith(".json.locks")) {
|
|
383
|
+
try {
|
|
384
|
+
lstatSync(entryPath.slice(0, -".locks".length));
|
|
385
|
+
} catch (error) {
|
|
386
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") {
|
|
387
|
+
try {
|
|
388
|
+
// A live ticket prevents removal of its directory.
|
|
389
|
+
rmdirSync(entryPath);
|
|
390
|
+
return;
|
|
391
|
+
} catch (cleanupError) {
|
|
392
|
+
const code = (cleanupError as NodeJS.ErrnoException).code;
|
|
393
|
+
if (code === "ENOENT") return;
|
|
394
|
+
if (code !== "ENOTEMPTY" && code !== "EEXIST") failures++;
|
|
395
|
+
}
|
|
396
|
+
} else failures++;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
}
|
|
237
400
|
const mode = stat.isDirectory() ? 0o700 : 0o600;
|
|
238
401
|
if ((stat.mode & 0o077) !== 0 || (stat.mode & 0o777) !== mode) {
|
|
402
|
+
if (certificate) {
|
|
403
|
+
// Invalidate before the mode change so a peer can see that the tree changed.
|
|
404
|
+
markerRemoved ??= removeMarker();
|
|
405
|
+
}
|
|
239
406
|
try {
|
|
240
407
|
chmodSync(entryPath, mode);
|
|
241
408
|
tightened++;
|
|
@@ -243,7 +410,7 @@ export function tightenStorageTreeSync(root: string): { tightened: number; failu
|
|
|
243
410
|
failures++;
|
|
244
411
|
}
|
|
245
412
|
}
|
|
246
|
-
if (!stat.isDirectory()) return;
|
|
413
|
+
if (!recursive || !stat.isDirectory()) return;
|
|
247
414
|
let entries: string[];
|
|
248
415
|
try {
|
|
249
416
|
entries = readdirSync(entryPath);
|
|
@@ -253,6 +420,54 @@ export function tightenStorageTreeSync(root: string): { tightened: number; failu
|
|
|
253
420
|
}
|
|
254
421
|
for (const entry of entries) visit(join(entryPath, entry));
|
|
255
422
|
};
|
|
256
|
-
|
|
423
|
+
if (certificate) {
|
|
424
|
+
visit(root, false, false, rootStat);
|
|
425
|
+
visit(dbPath, false, true);
|
|
426
|
+
visit(`${dbPath}-wal`, false, true);
|
|
427
|
+
visit(`${dbPath}-shm`, false, true);
|
|
428
|
+
// Transformers does not set cache entry modes. Keep this subtree checked.
|
|
429
|
+
visit(join(root, "models"), true, true);
|
|
430
|
+
// The Rust logger does not use the storage helpers.
|
|
431
|
+
visit(join(root, "logs"), true, true);
|
|
432
|
+
// A loose entry shows the tree changed after certification. The root's mode does not
|
|
433
|
+
// stop a process with a working directory in the tree. Walk the full tree at this start.
|
|
434
|
+
if (markerRemoved !== undefined) {
|
|
435
|
+
certificate = undefined;
|
|
436
|
+
visit(root);
|
|
437
|
+
if (!markerRemoved && !removeMarker()) failures++;
|
|
438
|
+
} else if (failures > 0) {
|
|
439
|
+
if (!removeMarker()) failures++;
|
|
440
|
+
} else {
|
|
441
|
+
// A peer can remove the certificate during the mode checks.
|
|
442
|
+
let markerChanged: boolean;
|
|
443
|
+
try {
|
|
444
|
+
const current = lstatSync(marker);
|
|
445
|
+
markerChanged =
|
|
446
|
+
current.dev !== certificate.dev ||
|
|
447
|
+
current.ino !== certificate.ino ||
|
|
448
|
+
current.ctimeMs !== certificate.ctimeMs;
|
|
449
|
+
} catch (error) {
|
|
450
|
+
markerChanged = true;
|
|
451
|
+
discardMarker = (error as NodeJS.ErrnoException).code !== "ENOENT";
|
|
452
|
+
}
|
|
453
|
+
if (markerChanged) {
|
|
454
|
+
certificate = undefined;
|
|
455
|
+
visit(root);
|
|
456
|
+
if (discardMarker && !removeMarker()) failures++;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
} else {
|
|
460
|
+
visit(root, true, false, rootStat);
|
|
461
|
+
if (discardMarker && !removeMarker()) failures++;
|
|
462
|
+
if (failures === 0 && !keptLegacyTemp) {
|
|
463
|
+
try {
|
|
464
|
+
writeStorageFileSync(marker, STORAGE_PERMISSIONS_MARKER_BYTES, {
|
|
465
|
+
flag: "wx",
|
|
466
|
+
});
|
|
467
|
+
} catch (error) {
|
|
468
|
+
if ((error as NodeJS.ErrnoException).code !== "EEXIST") failures++;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}
|
|
257
472
|
return { tightened, failures };
|
|
258
473
|
}
|
|
@@ -18,8 +18,8 @@ export function nativeCompactionContextLabel(snapshot: SidebarSnapshot): string
|
|
|
18
18
|
|
|
19
19
|
export function compactionOffSidebarRows(snapshot: SidebarSnapshot): CompactionOffSidebarRow[] {
|
|
20
20
|
const rows: CompactionOffSidebarRow[] = [
|
|
21
|
-
{ label: "Memories", value: String(snapshot.memoryCount) },
|
|
22
|
-
{ label: "Notes", value: String(snapshot.sessionNoteCount) },
|
|
21
|
+
{ label: "Memories", value: String(snapshot.memoryCount ?? "") },
|
|
22
|
+
{ label: "Notes", value: String(snapshot.sessionNoteCount ?? "") },
|
|
23
23
|
];
|
|
24
24
|
const archivedCount = snapshot.archivedCompartmentCount ?? 0;
|
|
25
25
|
if (archivedCount > 0) {
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import { beforeAll, describe, expect, test } from "bun:test";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import type { RenderCase, RenderResult } from "../../../scripts/render-compiled-sidebar";
|
|
4
|
+
import type { SidebarSnapshot } from "../data/context-db";
|
|
5
|
+
|
|
6
|
+
const BASE: SidebarSnapshot = {
|
|
7
|
+
sessionId: "ses_sidebar",
|
|
8
|
+
usagePercentage: 10,
|
|
9
|
+
inputTokens: 1000,
|
|
10
|
+
contextLimit: 10_000,
|
|
11
|
+
systemPromptTokens: 500,
|
|
12
|
+
compartmentCount: 0,
|
|
13
|
+
memoryCount: 7,
|
|
14
|
+
memoryBlockCount: 0,
|
|
15
|
+
pendingOpsCount: 0,
|
|
16
|
+
historianRunning: false,
|
|
17
|
+
compartmentInProgress: false,
|
|
18
|
+
sessionNoteCount: 0,
|
|
19
|
+
readySmartNoteCount: 0,
|
|
20
|
+
cacheTtl: "5m",
|
|
21
|
+
lastTransformError: null,
|
|
22
|
+
lastDreamerRunAt: null,
|
|
23
|
+
projectIdentity: null,
|
|
24
|
+
compartmentTokens: 0,
|
|
25
|
+
factTokens: 0,
|
|
26
|
+
memoryTokens: 0,
|
|
27
|
+
docsTokens: 0,
|
|
28
|
+
profileTokens: 0,
|
|
29
|
+
conversationTokens: 500,
|
|
30
|
+
toolCallTokens: 0,
|
|
31
|
+
toolDefinitionTokens: 0,
|
|
32
|
+
executeThreshold: 65,
|
|
33
|
+
};
|
|
34
|
+
const REFRESHED = {
|
|
35
|
+
...BASE,
|
|
36
|
+
usagePercentage: 20,
|
|
37
|
+
inputTokens: 2000,
|
|
38
|
+
conversationTokens: 1500,
|
|
39
|
+
memoryCount: 8,
|
|
40
|
+
};
|
|
41
|
+
const tailHygiene = {
|
|
42
|
+
u: 250,
|
|
43
|
+
t: 500,
|
|
44
|
+
severity: 0.5,
|
|
45
|
+
evaluable: true,
|
|
46
|
+
generationInvalidated: false,
|
|
47
|
+
baselineGeneration: 1,
|
|
48
|
+
computedAt: 1,
|
|
49
|
+
};
|
|
50
|
+
const dreamerProgress = { task: "verify", processed: 2, total: 5, startedAt: 1 } as const;
|
|
51
|
+
|
|
52
|
+
function without(
|
|
53
|
+
...fields: Exclude<keyof SidebarSnapshot, "sessionId">[]
|
|
54
|
+
): RenderCase["snapshots"][0] {
|
|
55
|
+
const reply: RenderCase["snapshots"][0] = { ...REFRESHED };
|
|
56
|
+
for (const field of fields) delete reply[field];
|
|
57
|
+
return reply;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
type Removal = {
|
|
61
|
+
name: string;
|
|
62
|
+
render: RenderCase;
|
|
63
|
+
present: RegExp;
|
|
64
|
+
absent: RegExp;
|
|
65
|
+
fresh?: RegExp;
|
|
66
|
+
};
|
|
67
|
+
const removals: Removal[] = [
|
|
68
|
+
{
|
|
69
|
+
name: "removes Hygiene without throwing when the third snapshot omits tailHygiene",
|
|
70
|
+
// A second snapshot with Hygiene keeps the row active before the field goes away.
|
|
71
|
+
render: {
|
|
72
|
+
snapshots: [
|
|
73
|
+
{ ...BASE, tailHygiene },
|
|
74
|
+
{ ...BASE, usagePercentage: 15, tailHygiene },
|
|
75
|
+
REFRESHED,
|
|
76
|
+
],
|
|
77
|
+
},
|
|
78
|
+
present: /Hygiene +50\.0% · 250\/500/,
|
|
79
|
+
absent: /Hygiene/,
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
name: "guards Dreamer progress removal when dreamerProgress is omitted",
|
|
83
|
+
render: {
|
|
84
|
+
snapshots: [
|
|
85
|
+
{ ...BASE, dreamerProgress },
|
|
86
|
+
{ ...BASE, usagePercentage: 15, dreamerProgress },
|
|
87
|
+
REFRESHED,
|
|
88
|
+
],
|
|
89
|
+
},
|
|
90
|
+
present: /Dreamer verify: 2\/5 processed/,
|
|
91
|
+
absent: /Dreamer/,
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
name: "guards warning removal when lastTransformError is omitted",
|
|
95
|
+
render: {
|
|
96
|
+
snapshots: [
|
|
97
|
+
{ ...BASE, lastTransformError: "Transform failed" },
|
|
98
|
+
without("lastTransformError"),
|
|
99
|
+
],
|
|
100
|
+
},
|
|
101
|
+
present: /Transform failed/,
|
|
102
|
+
absent: /Transform failed/,
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
name: "guards marker removal when executeThresholdClamped is omitted",
|
|
106
|
+
render: { snapshots: [{ ...BASE, executeThresholdClamped: true }, REFRESHED] },
|
|
107
|
+
present: /10\.0% \/ 65%\*/,
|
|
108
|
+
absent: /65%\*/,
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
name: "guards Stats removal when totalInputTokens is omitted",
|
|
112
|
+
render: { snapshots: [{ ...BASE, totalInputTokens: 0 }, REFRESHED] },
|
|
113
|
+
present: /Total tokens +0\b/,
|
|
114
|
+
absent: /Total tokens/,
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
name: "guards renderer mode when compaction_enabled is omitted",
|
|
118
|
+
render: { snapshots: [{ ...BASE, compaction_enabled: false }, REFRESHED] },
|
|
119
|
+
present: /native compaction/,
|
|
120
|
+
absent: /native compaction/,
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
name: "guards archived compartments removal when archivedCompartmentCount is omitted",
|
|
124
|
+
render: {
|
|
125
|
+
snapshots: [
|
|
126
|
+
{ ...BASE, compaction_enabled: false, archivedCompartmentCount: 3 },
|
|
127
|
+
{ ...REFRESHED, compaction_enabled: false },
|
|
128
|
+
],
|
|
129
|
+
},
|
|
130
|
+
present: /Archived compartments +3\b/,
|
|
131
|
+
absent: /Archived compartments/,
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
name: "guards the token breakdown when inputTokens is omitted",
|
|
135
|
+
render: { snapshots: [BASE, without("inputTokens")] },
|
|
136
|
+
present: /Conversation/,
|
|
137
|
+
absent: /Conversation/,
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
name: "guards the context summary when contextLimit is omitted",
|
|
141
|
+
render: { snapshots: [BASE, without("contextLimit")] },
|
|
142
|
+
present: /10\.0% \/ 65%/,
|
|
143
|
+
absent: /65%/,
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
name: "omits the usage percentage without throwing when usagePercentage is omitted",
|
|
147
|
+
render: { snapshots: [BASE, without("usagePercentage")] },
|
|
148
|
+
present: /10\.0% \/ 65%/,
|
|
149
|
+
absent: /10\.0%|undefined|NaN/,
|
|
150
|
+
},
|
|
151
|
+
{
|
|
152
|
+
name: "guards the threshold when executeThreshold is omitted",
|
|
153
|
+
render: { snapshots: [BASE, without("executeThreshold")] },
|
|
154
|
+
present: /65%/,
|
|
155
|
+
absent: /65%|undefined|NaN/,
|
|
156
|
+
},
|
|
157
|
+
{
|
|
158
|
+
name: "omits the Conversation row when conversationTokens is omitted",
|
|
159
|
+
render: { snapshots: [BASE, without("conversationTokens")] },
|
|
160
|
+
present: /Conversation +500 \(50%\)/,
|
|
161
|
+
absent: /Conversation|NaN/,
|
|
162
|
+
},
|
|
163
|
+
{
|
|
164
|
+
name: "leaves native-compaction counts empty when the counts are omitted",
|
|
165
|
+
render: {
|
|
166
|
+
snapshots: [
|
|
167
|
+
{ ...BASE, compaction_enabled: false, sessionNoteCount: 6 },
|
|
168
|
+
{ ...without("memoryCount", "sessionNoteCount"), compaction_enabled: false },
|
|
169
|
+
],
|
|
170
|
+
},
|
|
171
|
+
present: /Notes +6\b/,
|
|
172
|
+
absent: /Notes +6\b|Memories +7\b|undefined|NaN/,
|
|
173
|
+
fresh: /Context: 20\.0% · native compaction/,
|
|
174
|
+
},
|
|
175
|
+
{
|
|
176
|
+
name: "guards Injected removal when memoryBlockCount is omitted",
|
|
177
|
+
render: { snapshots: [{ ...BASE, memoryBlockCount: 3 }, without("memoryBlockCount")] },
|
|
178
|
+
present: /Injected +3\b/,
|
|
179
|
+
absent: /Injected/,
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
name: "guards the collapsed Memories count when memoryBlockCount is omitted",
|
|
183
|
+
render: {
|
|
184
|
+
collapsed: true,
|
|
185
|
+
snapshots: [{ ...BASE, memoryBlockCount: 3 }, without("memoryBlockCount")],
|
|
186
|
+
},
|
|
187
|
+
present: /Memories +3\/7\b/,
|
|
188
|
+
absent: /3\/7|undefined|NaN/,
|
|
189
|
+
},
|
|
190
|
+
{
|
|
191
|
+
name: "guards Queue removal when pendingOpsCount is omitted",
|
|
192
|
+
render: {
|
|
193
|
+
snapshots: [
|
|
194
|
+
{ ...BASE, pendingOpsCount: 3, sessionNoteCount: 1 },
|
|
195
|
+
{ ...without("pendingOpsCount"), sessionNoteCount: 1 },
|
|
196
|
+
],
|
|
197
|
+
},
|
|
198
|
+
present: /Queue +3 pending/,
|
|
199
|
+
absent: /Queue/,
|
|
200
|
+
},
|
|
201
|
+
{
|
|
202
|
+
name: "guards Notes removal when sessionNoteCount is omitted",
|
|
203
|
+
render: {
|
|
204
|
+
snapshots: [
|
|
205
|
+
{ ...BASE, sessionNoteCount: 4, pendingOpsCount: 1 },
|
|
206
|
+
{ ...without("sessionNoteCount"), pendingOpsCount: 1 },
|
|
207
|
+
],
|
|
208
|
+
},
|
|
209
|
+
present: /Notes +4\b/,
|
|
210
|
+
absent: /Notes/,
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
name: "guards Smart Notes removal when readySmartNoteCount is omitted",
|
|
214
|
+
render: {
|
|
215
|
+
snapshots: [
|
|
216
|
+
{ ...BASE, readySmartNoteCount: 4, pendingOpsCount: 1 },
|
|
217
|
+
{ ...without("readySmartNoteCount"), pendingOpsCount: 1 },
|
|
218
|
+
],
|
|
219
|
+
},
|
|
220
|
+
present: /Smart Notes +4 ready/,
|
|
221
|
+
absent: /Smart Notes/,
|
|
222
|
+
},
|
|
223
|
+
];
|
|
224
|
+
|
|
225
|
+
const updated: RenderCase = {
|
|
226
|
+
snapshots: [
|
|
227
|
+
{ ...BASE, tailHygiene, dreamerProgress },
|
|
228
|
+
{
|
|
229
|
+
...REFRESHED,
|
|
230
|
+
tailHygiene: { ...tailHygiene, u: 125, severity: 0.25, evaluable: false },
|
|
231
|
+
dreamerProgress: { ...dreamerProgress, processed: 4 },
|
|
232
|
+
},
|
|
233
|
+
],
|
|
234
|
+
};
|
|
235
|
+
const forcedError: RenderCase = {
|
|
236
|
+
snapshots: [
|
|
237
|
+
{ ...BASE, tailHygiene },
|
|
238
|
+
{ ...REFRESHED, tailHygiene },
|
|
239
|
+
],
|
|
240
|
+
throwOnEvaluable: true,
|
|
241
|
+
};
|
|
242
|
+
const equalReply = { ...BASE, tailHygiene, dreamerProgress };
|
|
243
|
+
const unchanged = {
|
|
244
|
+
snapshots: [equalReply, equalReply, equalReply],
|
|
245
|
+
trackText: ["10.0% / 65%", "1K / 10K", "Dreamer verify: 2/5 processed", "Hygiene"],
|
|
246
|
+
} satisfies RenderCase;
|
|
247
|
+
let results: RenderResult[];
|
|
248
|
+
|
|
249
|
+
beforeAll(() => {
|
|
250
|
+
const cases = [...removals.map(({ render }) => render), updated, forcedError, unchanged];
|
|
251
|
+
const script = join(import.meta.dir, "../../../scripts/render-compiled-sidebar.ts");
|
|
252
|
+
const child = Bun.spawnSync(["bun", "--conditions=browser", script], {
|
|
253
|
+
stdin: new TextEncoder().encode(JSON.stringify(cases)),
|
|
254
|
+
stdout: "pipe",
|
|
255
|
+
stderr: "pipe",
|
|
256
|
+
windowsHide: true,
|
|
257
|
+
});
|
|
258
|
+
if (child.exitCode !== 0)
|
|
259
|
+
throw new Error(`sidebar render script failed: ${child.stderr.toString()}`);
|
|
260
|
+
results = JSON.parse(child.stdout.toString());
|
|
261
|
+
}, 120_000);
|
|
262
|
+
|
|
263
|
+
describe("compiled sidebar refresh", () => {
|
|
264
|
+
for (const [index, { name, render, present, absent, fresh }] of removals.entries()) {
|
|
265
|
+
test(name, () => {
|
|
266
|
+
const { frames, error } = results[index];
|
|
267
|
+
expect(frames[0]).toMatch(present);
|
|
268
|
+
if (render.snapshots.length === 3) {
|
|
269
|
+
expect(frames[1]).toContain("15.0% / 65%");
|
|
270
|
+
expect(frames[1]).toMatch(present);
|
|
271
|
+
}
|
|
272
|
+
expect(error).toBeUndefined();
|
|
273
|
+
expect(frames).toHaveLength(render.snapshots.length);
|
|
274
|
+
const after = frames[frames.length - 1];
|
|
275
|
+
expect(after).not.toMatch(absent);
|
|
276
|
+
expect(after).toMatch(fresh ?? /Memories +8\b/);
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
test("guards the new Hygiene and Dreamer readings when both fields stay present", () => {
|
|
281
|
+
const {
|
|
282
|
+
frames: [before, after],
|
|
283
|
+
error,
|
|
284
|
+
} = results[removals.length];
|
|
285
|
+
expect(error).toBeUndefined();
|
|
286
|
+
expect(before).toMatch(/Hygiene +50\.0% · 250\/500/);
|
|
287
|
+
expect(before).toContain("Dreamer verify: 2/5 processed");
|
|
288
|
+
expect(after).toMatch(/Hygiene +25\.0% · 125\/500/);
|
|
289
|
+
expect(after).toContain("Dreamer verify: 4/5 processed");
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
test("reports a forced slot error on refresh after a valid Hygiene frame", () => {
|
|
293
|
+
const { frames, error } = results[removals.length + 1];
|
|
294
|
+
expect(frames).toHaveLength(1);
|
|
295
|
+
expect(frames[0]).toMatch(/Hygiene +50\.0% · 250\/500/);
|
|
296
|
+
expect(error).toBe("forced error reading tailHygiene.evaluable during refresh");
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
test("retains the Context, Dreamer and Hygiene elements across equal refreshes", () => {
|
|
300
|
+
const { frames, error, textIds } = results[removals.length + 2];
|
|
301
|
+
expect(error).toBeUndefined();
|
|
302
|
+
expect(frames).toHaveLength(3);
|
|
303
|
+
expect(textIds).toHaveLength(3);
|
|
304
|
+
expect(textIds?.map((frame) => frame.map((ids) => ids.length))).toEqual(
|
|
305
|
+
frames.map(() => unchanged.trackText.map(() => 1)),
|
|
306
|
+
);
|
|
307
|
+
expect(textIds?.slice(1)).toEqual([textIds?.[0], textIds?.[0]]);
|
|
308
|
+
});
|
|
309
|
+
});
|