okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-0213-fixux (82d3a0345): user screenshots - a data folder on an external drive under /run/media is no longer refused as a system folder (real system folders still are; the doubled sentence is gone); one fit label everywhere (owner 11-08): 'fits' when the model files without the n-gram/PLE/Engram table fit graphics memory plus RAM, else 'may run slower' (the Unsloth sets were measured against RAM only; DeepSeek Needs shows the whole size; no-card PCs and small Macs say 'may run slower'; NVFP4-QAD sized by its 76.8 GB pack)b7bcbcde9

main
8.1 KiB104 linesraw
1// The chat's own folder and opening its files by path.
2import test from 'node:test';
3import assert from 'node:assert/strict';
4import { mkdtempSync, mkdirSync, rmSync, writeFileSync, symlinkSync, existsSync, readFileSync, realpathSync } from 'node:fs';
5import { tmpdir as osTmpdir } from 'node:os';
6// the canonical scratch root: on a Mac the temp folder is /var/folders/..., which the folder policy refuses as a system folder and which
7// is /private/var/... once resolved; the tests make their folders under the resolved name (the policy itself is unchanged)
8const tmpdir = () => realpathSync(osTmpdir());
9import { join } from 'node:path';
10import { createChatFolders, openFile, openCommand, resolveChatFile, refusedFolder } from '../server/chat-files.mjs';
11import { startScriptedWorker } from './helpers/scripted-worker.mjs';
12import { createAgentBridge } from '../server/agent/agent-bridge.mjs';
13import { resolveHarness } from '../server/agent/harness-runtime.mjs';
14const BS = String.fromCharCode(92);
15const HARNESS =process.env.STRATA_HARNESS_DIR;
16const skip = !HARNESS || !resolveHarness({ env: process.env }).ok ? 'STRATA_HARNESS_DIR is not set to a built Harness' : false;
17
18test('a chat folder is remembered per chat, must exist, and is never a system folder or a whole drive', () => {
19  const dir = mkdtempSync(join(tmpdir(), 'strata-cf-'));
20  try {
21    const mine = join(dir, 'pagoda'); mkdirSync(mine);
22    const file = join(dir, 'folders.json');
23    const a = createChatFolders({ file });
24    assert.equal(a.get('c1'), null);
25    assert.equal(a.set('c1', mine).name, 'pagoda');
26    assert.equal(createChatFolders({ file }).get('c1'), mine, 'it survives a restart');
27    assert.equal(createChatFolders({ file }).get('c2'), null, 'per chat');
28    assert.throws(() => a.set('c1', join(dir, 'nope')), /doesn't exist/);
29    writeFileSync(join(dir, 'f.txt'), 'x'); assert.throws(() => a.set('c1', join(dir, 'f.txt')), /not a folder/);
30    assert.throws(() => a.set('c1', ''), /Choose a folder/);
31    assert.throws(() => a.set('c1', process.platform === 'win32' ? 'C:' + BS : '/'), /whole drive/);
32    assert.ok(refusedFolder(process.platform === 'win32' ? 'C:' + BS + 'Windows' + BS + 'System32' : '/etc'));
33    rmSync(mine, { recursive: true }); assert.equal(a.get('c1'), null, 'a deleted folder falls back to the default'); assert.equal(a.info('c1').exists, false);
34    a.clear('c1'); assert.equal(a.info('c1'), null);
35  } finally { rmSync(dir, { recursive: true, force: true }); }
36});
37
38test('a drive the desktop mounted (/run/media/<user>/<drive>) is a fine chat folder; the rest of /run is still system', { skip: process.platform === 'win32' && 'POSIX paths' }, () => {
39  for (const p of ['/run/media/ann/Backup', '/run/media/ann/Backup/projects/pagoda', '/media/ann/Backup/x', '/mnt/data/x']) assert.equal(refusedFolder(p), null, p);
40  for (const p of ['/run', '/run/media', '/run/media/ann', '/run/user/1000/x', '/run/systemd', '/usr/share', '/etc', '/var/lib/x']) assert.match(refusedFolder(p), /system folder/, p);
41});
42
43test('files open by path from inside the folder only; the browser gets a file:// address', () => {
44  const dir = mkdtempSync(join(tmpdir(), 'strata-cf2-'));
45  try {
46    const root = join(dir, 'proj'); mkdirSync(root); writeFileSync(join(root, 'pagoda.html'), '<p>hi</p>'); writeFileSync(join(dir, 'secret.txt'), 's');
47    const rec = [];
48    const r = openFile(root, 'pagoda.html', 'browser', { recorder: rec, platform: 'win32' });
49    assert.match(r.url, /^file:\/\/\/.*pagoda\.html$/); assert.deepEqual(rec[0].slice(0, 2), ['rundll32.exe', 'url.dll,FileProtocolHandler']); assert.match(rec[0][2], /^file:\/\//);
50    assert.ok(!/127\.0\.0\.1|localhost/.test(rec[0].join(' ')));
51    const WIN = 'C:' + BS + 'a' + BS + 'b.html';
52    assert.deepEqual(openCommand('folder', WIN, 'win32'), ['explorer.exe', ['/select,' + WIN]]);
53    assert.equal(openCommand('folder', '/a/b.html', 'darwin')[1][0], '-R');
54    assert.equal(rec.length, 1);
55    assert.throws(() => openFile(root, '../secret.txt', 'app', { recorder: rec }), /outside this chat's folder/);
56    assert.throws(() => openFile(root, join(dir, 'secret.txt'), 'app', { recorder: rec }), /outside/);
57    assert.throws(() => openFile(root, 'missing.html', 'app', { recorder: rec }), /no longer there/);
58    assert.throws(() => openFile(null, 'pagoda.html', 'app', { recorder: rec }), /no folder/);
59    assert.throws(() => openFile(root, 'pagoda.html', 'weird', { recorder: rec }), /not known/);
60    try { symlinkSync(join(dir, 'secret.txt'), join(root, 'link.txt')); assert.throws(() => resolveChatFile(root, 'link.txt'), /outside/, 'a link out of the folder is followed and refused'); } catch { /* no symlink right on this system */ }
61    assert.equal(rec.length, 1, 'nothing was started for a refused request');
62  } finally { rmSync(dir, { recursive: true, force: true }); }
63});
64
65test('a chat that moves to another folder gets a session there: the agent writes into the new folder', { skip }, async (t) => {
66  const worker = await startScriptedWorker({});
67  const dir = mkdtempSync(join(tmpdir(), 'strata-cf3-')); const a = join(dir, 'a'), b = join(dir, 'b'); mkdirSync(a); mkdirSync(b);
68  const bridge = createAgentBridge({ dataDir: dir, localInstances: () => [{ id: 'i', label: 'S', baseUrl: worker.url, modelId: 'scripted-model', context: 32768, slots: 2 }], connections: () => [], env: process.env, startTimeoutMs: 90_000 });
69  t.after(async () => { await bridge.close(); await worker.close(); try { rmSync(dir, { recursive: true, force: true, maxRetries: 3 }); } catch { /* best effort */ } });
70  const fwd = p => p.replaceAll(BS, '/');
71  const run = (ws, name) => bridge.runTurn({ chatId: 'one', message: `SCRIPT:call write ${JSON.stringify({ file_path: fwd(join(ws, name)), content: name })}`, workspacePath: ws, mode: 'manual', onFrame: f => { if (f.type === 'agent-tool' && f.tool.state === 'proposed') void bridge.approve({ chatId: 'one', callId: f.tool.id, decision: 'approve' }); } });
72  await run(a, 'in-a.txt');
73  const first = bridge.sessionOf('one');
74  await run(b, 'in-b.txt');
75  assert.notEqual(bridge.sessionOf('one'), first, 'a new session for the new folder');
76  assert.ok(existsSync(join(a, 'in-a.txt')) && existsSync(join(b, 'in-b.txt')));
77  // the sandbox follows the folder: Plan mode shows the old folder is not the working folder any more
78  await run(b, 'again.txt');
79  assert.equal(readFileSync(join(b, 'again.txt'), 'utf8').trim(), 'again.txt');
80});
81
82import { chatRootOf, previewFile } from '../server/chat-files.mjs';
83test('a chat with no chosen folder still has a folder: the one its agent worked in, else the default workspace; files there open, preview and reveal', () => {
84  const dir = mkdtempSync(join(tmpdir(), 'strata-root-'));
85  try {
86    const def = join(dir, 'Documents', 'Strata', 'Workspace'); mkdirSync(def, { recursive: true });
87    const chosen = join(dir, 'mine'); mkdirSync(chosen);
88    const used = join(dir, 'other'); mkdirSync(used);
89    assert.equal(chatRootOf({ chosen, used, fallback: def }), chosen, 'the chosen folder wins');
90    assert.equal(chatRootOf({ chosen: null, used, fallback: def }), used, 'the folder the agent really wrote to');
91    assert.equal(chatRootOf({ chosen: null, used: join(dir, 'gone'), fallback: def }), def, 'a folder that is gone is skipped');
92    const root = chatRootOf({ chosen: null, used: null, fallback: def });
93    assert.equal(root, def, 'no chosen folder: the default workspace');
94    writeFileSync(join(def, 'pagoda.html'), '<h1>hi</h1>');
95    assert.equal(previewFile(root, 'pagoda.html', 'c1').kind, 'html');
96    const rec = [];
97    const r = openFile(root, join(def, 'pagoda.html'), 'folder', { platform: 'darwin', recorder: rec });
98    assert.equal(r.ok, true); assert.deepEqual(rec[0].slice(0, 2), ['open', '-R']);
99    assert.equal(openFile(root, 'pagoda.html', 'app', { platform: 'linux', recorder: rec }).ok, true);
100    assert.equal(openFile(root, 'pagoda.html', 'app', { platform: 'win32', recorder: rec }).ok, true);
101    assert.throws(() => resolveChatFile(root, join(dir, 'secret.txt')), /no longer there|outside/, 'the default folder is still a boundary');
102    assert.equal(chatRootOf({}), null);
103  } finally { rmSync(dir, { recursive: true, force: true }); }
104});