@cortexkit/opencode-magic-context 0.49.83 → 0.49.85
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-xmyke353.js → context-bjfwzbts.js} +314 -107
- package/dist/{derive-budgets-2z6xxm7e.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/mural/render-trigger.d.ts +3 -3
- package/dist/features/magic-context/mural/render-trigger.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/inject-compartments.d.ts.map +1 -1
- package/dist/hooks/magic-context/lkg-recovery-fence.d.ts.map +1 -1
- package/dist/{index-zzvd4a9z.js → index-05bm2ndp.js} +1 -1
- package/dist/{index-93rezfvc.js → index-08jfpny9.js} +37 -6
- package/dist/{index-dg5jvh9r.js → index-0rge5rkh.js} +8 -8
- package/dist/{index-2xhzmwf6.js → index-1pf5qrx4.js} +5 -5
- package/dist/{index-e21n48yd.js → index-1y4y98r1.js} +23 -0
- 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-twmeey4h.js → index-70cv85b0.js} +2 -2
- package/dist/{index-rnyaa7cj.js → index-798s7c8s.js} +2 -2
- package/dist/{index-2q0t43fy.js → index-7j2zv9nm.js} +1 -1
- package/dist/{index-fpqdbb67.js → index-7svecf3m.js} +3 -3
- package/dist/{index-eptthgkw.js → index-9fsxmjs6.js} +5 -5
- package/dist/{v2/server-fvnzcnww.js → index-c5tmzym6.js} +192 -11
- package/dist/{index-w8dcjt7f.js → index-k0tb2z28.js} +1 -1
- package/dist/{index-zb4h9nrk.js → index-k50xhjw1.js} +202 -14
- package/dist/{index-hz1xdb15.js → index-kqnef2tg.js} +4 -4
- package/dist/{index-2ygw6q5s.js → index-nny4gm06.js} +2 -2
- package/dist/{index-fgy6hnr3.js → index-pw279hmt.js} +2 -2
- package/dist/{index-eyk7msgw.js → index-qvn3q9f1.js} +41 -41
- package/dist/{index-shpyv8gq.js → index-s8ks5nz4.js} +16 -9
- package/dist/{index-wxxcfpxh.js → index-sbr58jaf.js} +8 -8
- package/dist/{index-b2787fk6.js → index-tzbcb8yt.js} +36 -20
- package/dist/{index-x8qgqnym.js → index-w6v4dw5f.js} +25 -25
- package/dist/{index-zvane1rq.js → index-xccywqdj.js} +16 -16
- package/dist/{index-y208s7zx.js → index-xrnhxkz7.js} +6 -1
- package/dist/{index-tky3j43e.js → index-zmqx48vt.js} +58 -14
- package/dist/index.js +37 -37
- package/dist/{message-index-bounded-cfkeyf72.js → message-index-bounded-6fr624zf.js} +3 -3
- 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 +4 -4
- package/dist/{read-session-chunk-s3qac56g.js → read-session-chunk-6wedfths.js} +2 -2
- package/dist/{recomp-orchestrator-fjdp3t1d.js → recomp-orchestrator-xzs0a455.js} +19 -18
- package/dist/{render-c9fjwrad.js → render-yqp7q79f.js} +12 -11
- package/dist/{search-archive-s2jas2jb.js → search-archive-r4h96j2z.js} +4 -4
- package/dist/{search-1jejx5cx.js → search-ztgy3ya6.js} +15 -13
- package/dist/shared/models-dev-cache.d.ts +6 -1
- package/dist/shared/models-dev-cache.d.ts.map +1 -1
- 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/shared/test-git-env.d.ts +3 -0
- package/dist/shared/test-git-env.d.ts.map +1 -0
- package/dist/{storage-tags-6e83pvff.js → storage-tags-y2mrb8e6.js} +1 -1
- package/dist/{store-ggrq9e14.js → store-n4yz3n66.js} +2 -2
- package/dist/{store-reader-tr64nxyy.js → store-reader-nf8z86n9.js} +1 -1
- package/dist/{transform-postprocess-phase-p2egg02g.js → transform-postprocess-phase-39a5hm2a.js} +20 -19
- package/dist/v2/auto-search-worker.js +3 -3
- package/dist/v2/chunk-embedding-write-worker.js +6 -5
- package/dist/v2/{context-zn18k0yk.js → context-k2rrqzf4.js} +446 -248
- package/dist/v2/{derive-budgets-2eam671q.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/fold/restore.d.ts.map +1 -1
- package/dist/v2/hooks/context.d.ts.map +1 -1
- package/dist/v2/hooks/revert-message-state.d.ts +12 -0
- package/dist/v2/hooks/revert-message-state.d.ts.map +1 -0
- package/dist/v2/hooks/storage-notice.d.ts +12 -6
- package/dist/v2/hooks/storage-notice.d.ts.map +1 -1
- package/dist/v2/{message-index-bounded-0xy4dn04.js → message-index-bounded-jqy7283g.js} +4 -4
- package/dist/v2/migration-worker.js +5 -4
- package/dist/v2/{project-embedding-registry-mzsvyads.js → project-embedding-registry-2jp1antr.js} +6 -4
- package/dist/v2/raw-ordinal-warmup-worker.js +4 -4
- package/dist/v2/{read-session-chunk-6mt2t6dy.js → read-session-chunk-n3nhfvd7.js} +3 -3
- package/dist/v2/{recomp-orchestrator-jn39mxh0.js → recomp-orchestrator-jnbxbsc8.js} +21 -20
- package/dist/v2/{render-q2aj9bgy.js → render-fezgh7pa.js} +13 -12
- package/dist/v2/{search-archive-fj59gjv1.js → search-archive-zz1kza50.js} +5 -5
- package/dist/v2/{search-9xn6ds9x.js → search-h5w7510x.js} +15 -13
- package/dist/v2/{server-2gcfmd47.js → server-2k8tkbhz.js} +2 -2
- package/dist/v2/{server-4z964n1c.js → server-3p6b0qya.js} +1 -1
- package/dist/v2/{server-c8mqs0v5.js → server-4wwwm2ya.js} +32 -32
- package/dist/v2/{server-1cn3kryt.js → server-97w51d8n.js} +5 -5
- package/dist/v2/{server-gx1s06dx.js → server-98hsp35q.js} +2 -2
- package/dist/v2/{server-xepx8t43.js → server-a2f1bhyj.js} +1 -1
- package/dist/v2/{server-r00xyx66.js → server-ap81bk3c.js} +28 -28
- package/dist/v2/{server-d706s5xn.js → server-b93f0bqh.js} +205 -17
- package/dist/v2/{server-2w1rdttn.js → server-brgandk4.js} +2 -2
- package/dist/v2/{server-rp52f40v.js → server-cwtehwbb.js} +1 -1
- package/dist/v2/{server-bj779zmp.js → server-e2k9mt86.js} +1 -1
- 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-g1a0nhtk.js → server-n5zr7190.js} +18 -18
- package/dist/v2/{server-2ktxmtbd.js → server-ps6rz88g.js} +2 -2
- package/dist/v2/{server-5yfq912a.js → server-qyazxy92.js} +23 -0
- 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-xdh4r20h.js → server-s15knz0w.js} +642 -627
- package/dist/v2/{server-vt6nj2hn.js → server-s76ty4ws.js} +41 -25
- package/dist/v2/{server-0dmzr7f9.js → server-sxfbjdye.js} +2 -2
- package/dist/v2/{server-569he5mh.js → server-w7mpvkpj.js} +1 -1
- package/dist/v2/{server-53n8hv5q.js → server-x77hd658.js} +8 -8
- package/dist/v2/{server-nqdzv8b7.js → server-xa8fefhw.js} +58 -14
- package/dist/v2/{server-1efjj215.js → server-ytqps2xa.js} +37 -6
- package/dist/v2/server.js +6 -4
- package/dist/v2/{storage-tags-z8j8wq34.js → storage-tags-xq81zgt2.js} +2 -2
- package/dist/v2/{store-reader-83tka051.js → store-reader-2vhx33n6.js} +1 -1
- package/dist/v2/store-reader.d.ts +11 -0
- package/dist/v2/store-reader.d.ts.map +1 -1
- package/dist/v2/{store-qdgzd38f.js → store-y56y034s.js} +2 -2
- package/dist/v2/{v2-archive-reader-9z8z97db.js → v2-archive-reader-e2w39c3p.js} +1 -1
- package/dist/v2/{wrapup-orchestrator-vd5zfp3v.js → wrapup-orchestrator-mmje3etf.js} +20 -19
- package/dist/{v2-archive-reader-r0xpxsb1.js → v2-archive-reader-8dsngf2j.js} +1 -1
- package/dist/{wrapup-orchestrator-k29zqfp9.js → wrapup-orchestrator-sm6pwf3p.js} +20 -19
- package/package.json +1 -1
- package/src/shared/models-dev-cache.ts +90 -21
- package/src/shared/reusable-worker.test.ts +31 -29
- package/src/shared/sqlite.test.ts +19 -6
- 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/shared/test-db.test.ts +5 -3
- package/src/shared/test-git-env.test.ts +38 -0
- package/src/shared/test-git-env.ts +10 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { execFileSync } from "node:child_process";
|
|
2
2
|
import { createHash, randomUUID } from "node:crypto";
|
|
3
|
-
import { readdirSync, readFileSync, rmSync } from "node:fs";
|
|
3
|
+
import { readdirSync, readFileSync, rmdirSync, rmSync } from "node:fs";
|
|
4
4
|
import { join } from "node:path";
|
|
5
5
|
import { isPidAlive, readProcessStartTime } from "./rpc-utils";
|
|
6
6
|
import {
|
|
@@ -61,25 +61,29 @@ const pause = new Int32Array(new SharedArrayBuffer(4));
|
|
|
61
61
|
* Unique names mean reclaiming a dead ticket cannot unlink a successor's lock.
|
|
62
62
|
*/
|
|
63
63
|
export function withStorageFileMutex<T>(directory: string, work: () => T, timeoutMs = 1000): T {
|
|
64
|
-
|
|
64
|
+
const deadline = performance.now() + timeoutMs;
|
|
65
65
|
const name = `${process.pid}-${randomUUID()}.lock`;
|
|
66
66
|
const ownPath = join(directory, name);
|
|
67
67
|
const birth = processBirth(process.pid);
|
|
68
68
|
const ticket: Ticket = { choosing: true, number: 0, birth, checksum: "" };
|
|
69
69
|
const encodedTicket = () =>
|
|
70
70
|
JSON.stringify({ ...ticket, checksum: ticketChecksum(name, ticket) });
|
|
71
|
-
for (
|
|
71
|
+
for (;;) {
|
|
72
72
|
try {
|
|
73
|
+
ensurePrivateStorageDirectorySync(directory);
|
|
73
74
|
writeStorageFileSync(ownPath, encodedTicket(), { flag: "wx", forcePrivate: true });
|
|
74
75
|
break;
|
|
75
76
|
} catch (error) {
|
|
76
|
-
// A
|
|
77
|
-
//
|
|
78
|
-
|
|
79
|
-
|
|
77
|
+
// A peer can remove the empty directory before this process writes its ticket.
|
|
78
|
+
// On macOS, this race can also give EEXIST from mkdir or EINVAL from open.
|
|
79
|
+
const code = (error as NodeJS.ErrnoException).code;
|
|
80
|
+
if (
|
|
81
|
+
(code !== "ENOENT" && code !== "EEXIST" && code !== "EINVAL") ||
|
|
82
|
+
performance.now() >= deadline
|
|
83
|
+
)
|
|
84
|
+
throw error;
|
|
80
85
|
}
|
|
81
86
|
}
|
|
82
|
-
const deadline = performance.now() + timeoutMs;
|
|
83
87
|
const peers = (): [string, Ticket | null][] => {
|
|
84
88
|
const found: [string, Ticket | null][] = [];
|
|
85
89
|
for (const other of readdirSync(directory)) {
|
|
@@ -136,5 +140,10 @@ export function withStorageFileMutex<T>(directory: string, work: () => T, timeou
|
|
|
136
140
|
return result;
|
|
137
141
|
} finally {
|
|
138
142
|
rmSync(ownPath, { force: true });
|
|
143
|
+
try {
|
|
144
|
+
rmdirSync(directory);
|
|
145
|
+
} catch {
|
|
146
|
+
// Do not remove a live contender's ticket.
|
|
147
|
+
}
|
|
139
148
|
}
|
|
140
149
|
}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it } from "bun:test";
|
|
2
|
+
import { spawnSync } from "node:child_process";
|
|
3
|
+
import { lstatSync, readdirSync } from "node:fs";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { cleanupTestTempDir, createTestTempDir } from "./test-temp-dir";
|
|
6
|
+
|
|
7
|
+
const roots: string[] = [];
|
|
8
|
+
|
|
9
|
+
afterEach(() => {
|
|
10
|
+
for (const root of roots.splice(0)) cleanupTestTempDir(root);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
function child(root: string, script: string) {
|
|
14
|
+
return spawnSync(process.execPath, ["--eval", script], {
|
|
15
|
+
cwd: import.meta.dir,
|
|
16
|
+
encoding: "utf8",
|
|
17
|
+
timeout: 120_000,
|
|
18
|
+
windowsHide: true,
|
|
19
|
+
env: {
|
|
20
|
+
...process.env,
|
|
21
|
+
NODE_ENV: "test",
|
|
22
|
+
XDG_DATA_HOME: root,
|
|
23
|
+
XDG_CONFIG_HOME: root,
|
|
24
|
+
MAGIC_CONTEXT_TEST_DATA_DIR: root,
|
|
25
|
+
MAGIC_CONTEXT_STORAGE_DIR: root,
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
describe.skipIf(process.platform === "win32")("storage creation under umask 0", () => {
|
|
31
|
+
it("creates helper outputs and public storage records privately before any startup walk", () => {
|
|
32
|
+
const { dir: root } = createTestTempDir("mc-storage-creators-");
|
|
33
|
+
roots.push(root);
|
|
34
|
+
const result = child(
|
|
35
|
+
root,
|
|
36
|
+
`
|
|
37
|
+
import { closeSync, lstatSync, readdirSync } from 'node:fs';
|
|
38
|
+
import { once } from 'node:events';
|
|
39
|
+
import { join } from 'node:path';
|
|
40
|
+
import * as p from './storage-permissions.ts';
|
|
41
|
+
import { withStorageFileMutex } from './storage-file-mutex.ts';
|
|
42
|
+
import { markAnnouncementSeen } from './announcement.ts';
|
|
43
|
+
import { rememberGitIdentity } from '../features/magic-context/memory/project-identity-cache.ts';
|
|
44
|
+
import { refreshModelLimitsFromApi } from './models-dev-cache.ts';
|
|
45
|
+
import { appendWindowReport } from '../features/magic-context/window-report-ledger.ts';
|
|
46
|
+
import { markLkgRecoveryDirty } from '../hooks/magic-context/lkg-recovery-fence.ts';
|
|
47
|
+
import { MagicContextRpcServer } from './rpc-server.ts';
|
|
48
|
+
import { rpcPortDir } from './rpc-utils.ts';
|
|
49
|
+
import { emitStallReport } from './host-stall-profiler-common.ts';
|
|
50
|
+
const root = process.env.MAGIC_CONTEXT_STORAGE_DIR;
|
|
51
|
+
const check = (path) => {
|
|
52
|
+
const stat = lstatSync(path);
|
|
53
|
+
if (stat.isSymbolicLink()) return;
|
|
54
|
+
const expected = stat.isDirectory() ? 0o700 : 0o600;
|
|
55
|
+
if ((stat.mode & 0o777) !== expected) throw new Error(path + ' mode=' + (stat.mode & 0o777).toString(8));
|
|
56
|
+
if (stat.isDirectory()) for (const entry of readdirSync(path)) check(join(path, entry));
|
|
57
|
+
};
|
|
58
|
+
const previous = process.umask(0);
|
|
59
|
+
let rpc;
|
|
60
|
+
try {
|
|
61
|
+
p.ensureStorageDirectorySync(join(root, 'helpers', 'nested'));
|
|
62
|
+
p.ensurePrivateStorageDirectorySync(join(root, 'private'));
|
|
63
|
+
p.writeStorageFileSync(join(root, 'sync'), 'x');
|
|
64
|
+
p.writeStorageFileAtomicSync(join(root, 'atomic'), 'x');
|
|
65
|
+
p.writeStorageFileDurableAtomicSync(join(root, 'durable', 'record'), 'x');
|
|
66
|
+
p.copyStorageFileSync(join(root, 'sync'), join(root, 'copy'));
|
|
67
|
+
p.copyStorageTreeSync(join(root, 'durable'), join(root, 'copy-tree'));
|
|
68
|
+
closeSync(p.openStorageFileSync(join(root, 'open'), 'wx'));
|
|
69
|
+
await p.writeStorageFileAsync(join(root, 'async', 'file'), 'x');
|
|
70
|
+
const stream = p.createStorageWriteStream(join(root, 'stream'));
|
|
71
|
+
const finished = once(stream, 'close');
|
|
72
|
+
stream.end('x');
|
|
73
|
+
await finished;
|
|
74
|
+
const handle = await p.createStorageFileHandleAsync(join(root, 'handle'), 'wx');
|
|
75
|
+
await p.writeStorageFileHandleAsync(handle, 'x');
|
|
76
|
+
await handle.close();
|
|
77
|
+
withStorageFileMutex(join(root, 'mutex'), () => check(root));
|
|
78
|
+
markAnnouncementSeen('0.49.37');
|
|
79
|
+
rememberGitIdentity(join(root, 'project'), 'git:0123456789abcdef');
|
|
80
|
+
await refreshModelLimitsFromApi({ config: { providers: async () => ({ data: { providers: [
|
|
81
|
+
{ id: 'test', models: { model: { limit: { context: 100000, output: 10000 } } } }
|
|
82
|
+
] } }) } });
|
|
83
|
+
appendWindowReport({ access_path: 'api', geometry: 'unknown', observed_at_ms: 123 });
|
|
84
|
+
markLkgRecoveryDirty({ sessionId: 'private-creation', processLocalProof: false, slot: {
|
|
85
|
+
jsonPrefix: '[{"text":"old"}]', inputIdSeq: ['one'], inputContentDigests: ['digest'],
|
|
86
|
+
lastInputMessageId: 'one', modelKey: 'provider/model', providerKey: 'provider', capturedAt: 1,
|
|
87
|
+
} });
|
|
88
|
+
emitStallReport({ profilerDir: () => join(root, 'host-profiler'), wallNow: () => 1000,
|
|
89
|
+
now: () => 1000, pid: process.pid, bunVersion: process.versions.bun, log: () => {},
|
|
90
|
+
}, { startMs: 0, endMs: 1000, traces: [], windowed: [], windowFilter: 'all',
|
|
91
|
+
options: { thresholdMs: 500, sampleIntervalMs: 10 }, before: null,
|
|
92
|
+
after: { at: 'fixture', heap_size_bytes: null, jsc_memory: null, rss_bytes: null,
|
|
93
|
+
percent_available_memory_in_use: null }, backend: 'jsc' });
|
|
94
|
+
rpc = new MagicContextRpcServer(root, join(root, 'project'));
|
|
95
|
+
if (await rpc.start() <= 0) throw new Error('RPC server did not start');
|
|
96
|
+
if (!readdirSync(rpcPortDir(root, join(root, 'project'))).some(name => name.endsWith('.json')))
|
|
97
|
+
throw new Error('RPC discovery record was not published');
|
|
98
|
+
check(root);
|
|
99
|
+
console.log('PRIVATE_CREATORS_OK');
|
|
100
|
+
} finally {
|
|
101
|
+
rpc?.stop();
|
|
102
|
+
process.umask(previous);
|
|
103
|
+
}
|
|
104
|
+
`,
|
|
105
|
+
);
|
|
106
|
+
expect(result.status, result.stdout + result.stderr).toBe(0);
|
|
107
|
+
expect(result.stdout).toContain("PRIVATE_CREATORS_OK");
|
|
108
|
+
console.log(result.stdout.trim());
|
|
109
|
+
const storage = join(root, "cortexkit", "magic-context");
|
|
110
|
+
for (const name of ["project-identities", "lkg-recovery-fences"])
|
|
111
|
+
expect(lstatSync(join(storage, name)).mode & 0o777).toBe(0o700);
|
|
112
|
+
expect(lstatSync(join(root, "host-profiler")).mode & 0o777).toBe(0o700);
|
|
113
|
+
expect(readdirSync(storage)).toContain("window-reports.jsonl");
|
|
114
|
+
expect(readdirSync(storage)).toContain("model-context-limits-opencode.json");
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("keeps SQLite DB, WAL and SHM private and bounded on a later real open", () => {
|
|
118
|
+
const { dir: root } = createTestTempDir("mc-storage-sqlite-");
|
|
119
|
+
roots.push(root);
|
|
120
|
+
const result = child(
|
|
121
|
+
root,
|
|
122
|
+
`
|
|
123
|
+
import { expect, spyOn } from 'bun:test';
|
|
124
|
+
import * as fs from 'node:fs';
|
|
125
|
+
import { join } from 'node:path';
|
|
126
|
+
import { openDatabase, closeDatabase } from '../features/magic-context/storage-db.ts';
|
|
127
|
+
const root = process.env.MAGIC_CONTEXT_STORAGE_DIR;
|
|
128
|
+
const path = join(root, 'custom.sqlite');
|
|
129
|
+
const previous = process.umask(0);
|
|
130
|
+
try {
|
|
131
|
+
const db = openDatabase({ dbPath: path });
|
|
132
|
+
db.exec('CREATE TABLE permission_probe (id INTEGER); INSERT INTO permission_probe VALUES (1)');
|
|
133
|
+
for (const suffix of ['', '-wal', '-shm']) {
|
|
134
|
+
expect(fs.lstatSync(path + suffix).mode & 0o777).toBe(0o600);
|
|
135
|
+
}
|
|
136
|
+
closeDatabase();
|
|
137
|
+
fs.mkdirSync(join(root, 'lkg-recovery-fences'), { mode: 0o700 });
|
|
138
|
+
for (let i = 0; i < 2000; i++) fs.writeFileSync(join(root, 'lkg-recovery-fences', i + '.json'), '{}', { mode: 0o600 });
|
|
139
|
+
const lstat = spyOn(fs, 'lstatSync');
|
|
140
|
+
try {
|
|
141
|
+
expect(openDatabase({ dbPath: path })).not.toBeNull();
|
|
142
|
+
expect(lstat.mock.calls.length).toBeLessThanOrEqual(16);
|
|
143
|
+
console.log('SQLITE_PRIVATE_OK lstat=' + lstat.mock.calls.length);
|
|
144
|
+
} finally { lstat.mockRestore(); }
|
|
145
|
+
} finally {
|
|
146
|
+
closeDatabase();
|
|
147
|
+
process.umask(previous);
|
|
148
|
+
}
|
|
149
|
+
`,
|
|
150
|
+
);
|
|
151
|
+
expect(result.status, result.stdout + result.stderr).toBe(0);
|
|
152
|
+
expect(result.stdout).toContain("SQLITE_PRIVATE_OK");
|
|
153
|
+
console.log(result.stdout.trim());
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it("retains the full check for third-party model downloads under umask 0", () => {
|
|
157
|
+
const { dir: root } = createTestTempDir("mc-storage-models-");
|
|
158
|
+
roots.push(root);
|
|
159
|
+
const result = child(
|
|
160
|
+
root,
|
|
161
|
+
`
|
|
162
|
+
import { expect, spyOn } from 'bun:test';
|
|
163
|
+
import * as fs from 'node:fs';
|
|
164
|
+
import { join } from 'node:path';
|
|
165
|
+
import { FileCache } from '../../node_modules/@huggingface/transformers/src/utils/cache/FileCache.js';
|
|
166
|
+
import { tightenStorageTreeSync, writeStorageFileSync } from './storage-permissions.ts';
|
|
167
|
+
const root = process.env.MAGIC_CONTEXT_STORAGE_DIR;
|
|
168
|
+
const cache = join(root, 'models');
|
|
169
|
+
const file = join(cache, 'org/model/onnx/weights.bin');
|
|
170
|
+
const previous = process.umask(0);
|
|
171
|
+
try {
|
|
172
|
+
await new FileCache(cache).put('org/model/onnx/weights.bin', new Response('fixture'));
|
|
173
|
+
expect(fs.lstatSync(cache).mode & 0o777).toBe(0o777);
|
|
174
|
+
expect(fs.lstatSync(file).mode & 0o777).toBe(0o666);
|
|
175
|
+
writeStorageFileSync(join(root, '.storage-permissions-v1'), '1\\n');
|
|
176
|
+
const lstat = spyOn(fs, 'lstatSync');
|
|
177
|
+
try {
|
|
178
|
+
expect(tightenStorageTreeSync(root)).toEqual({ tightened: 5, failures: 0 });
|
|
179
|
+
// The bounded check (11 calls) plus one full walk of this small tree,
|
|
180
|
+
// because the bounded check tightened entries.
|
|
181
|
+
expect(lstat.mock.calls.length).toBeLessThanOrEqual(18);
|
|
182
|
+
} finally { lstat.mockRestore(); }
|
|
183
|
+
expect(fs.lstatSync(cache).mode & 0o777).toBe(0o700);
|
|
184
|
+
expect(fs.lstatSync(file).mode & 0o777).toBe(0o600);
|
|
185
|
+
expect(fs.existsSync(join(root, '.storage-permissions-v1'))).toBe(false);
|
|
186
|
+
console.log('MODELS_CHECK_OK entries=5 before=0777/0666 after=0700/0600');
|
|
187
|
+
} finally { process.umask(previous); }
|
|
188
|
+
`,
|
|
189
|
+
);
|
|
190
|
+
expect(result.status, result.stdout + result.stderr).toBe(0);
|
|
191
|
+
expect(result.stdout).toContain("MODELS_CHECK_OK entries=5");
|
|
192
|
+
console.log(result.stdout.trim());
|
|
193
|
+
});
|
|
194
|
+
});
|