okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b7bcbcde9
main
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' , () => { 19const dir = mkdtempSync ( join ( tmpdir (), 'strata-cf-' )); 20try { 21const mine = join ( dir , 'pagoda' ); mkdirSync ( mine ); 22const file = join ( dir , 'folders.json' ); 23const a = createChatFolders ({ file}); 24assert . equal ( a . get ( 'c1' ), null ); 25assert . equal ( a . set ( 'c1' , mine ). name , 'pagoda' ); 26assert . equal ( createChatFolders ({ file}). get ( 'c1' ), mine , 'it survives a restart' ); 27assert . equal ( createChatFolders ({ file}). get ( 'c2' ), null , 'per chat' ); 28assert . throws (() => a . set ( 'c1' , join ( dir , 'nope' )), / doesn't exist / ); 29writeFileSync ( join ( dir , 'f.txt' ), 'x' ); assert . throws (() => a . set ( 'c1' , join ( dir , 'f.txt' )), / not a folder / ); 30assert . throws (() => a . set ( 'c1' , '' ), / Choose a folder / ); 31assert . throws (() => a . set ( 'c1' , process . platform === 'win32' ?'C:' + BS :'/' ), / whole drive / ); 32assert . ok ( refusedFolder ( process . platform === 'win32' ?'C:' + BS + 'Windows' + BS + 'System32' :'/etc' )); 33rmSync ( mine , { recursive :true }); assert . equal ( a . get ( 'c1' ), null , 'a deleted folder falls back to the default' ); assert . equal ( a . info ( 'c1' ). exists , false ); 34a . 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' }, () => { 39for ( 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 ); 40for ( 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' , () => { 44const dir = mkdtempSync ( join ( tmpdir (), 'strata-cf2-' )); 45try { 46const root = join ( dir , 'proj' ); mkdirSync ( root ); writeFileSync ( join ( root , 'pagoda.html' ), '<p>hi</p>' ); writeFileSync ( join ( dir , 'secret.txt' ), 's' ); 47const rec = []; 48const r = openFile ( root , 'pagoda.html' , 'browser' , { recorder :rec , platform :'win32' }); 49assert . match ( r . url , / ^file:\/\/\/. * pagoda\.html$ / ); assert . deepEqual ( rec [ 0 ]. slice ( 0 , 2 ), [ 'rundll32.exe' , 'url.dll,FileProtocolHandler' ]); assert . match ( rec [ 0 ][ 2 ], / ^file:\/\/ / ); 50assert . ok ( ! /127\.0\.0\.1 | localhost / . test ( rec [ 0 ]. join ( ' ' ))); 51const WIN = 'C:' + BS + 'a' + BS + 'b.html' ; 52assert . deepEqual ( openCommand ( 'folder' , WIN , 'win32' ), [ 'explorer.exe' , [ '/select,' + WIN ]]); 53assert . equal ( openCommand ( 'folder' , '/a/b.html' , 'darwin' )[ 1 ][ 0 ], '-R' ); 54assert . equal ( rec . length , 1 ); 55assert . throws (() => openFile ( root , '../secret.txt' , 'app' , { recorder :rec }), / outside this chat's folder / ); 56assert . throws (() => openFile ( root , join ( dir , 'secret.txt' ), 'app' , { recorder :rec }), / outside / ); 57assert . throws (() => openFile ( root , 'missing.html' , 'app' , { recorder :rec }), / no longer there / ); 58assert . throws (() => openFile ( null , 'pagoda.html' , 'app' , { recorder :rec }), / no folder / ); 59assert . throws (() => openFile ( root , 'pagoda.html' , 'weird' , { recorder :rec }), / not known / ); 60try { 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 */ } 61assert . 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 ) => { 66const worker = await startScriptedWorker ({}); 67const dir = mkdtempSync ( join ( tmpdir (), 'strata-cf3-' )); const a = join ( dir , 'a' ), b = join ( dir , 'b' ); mkdirSync ( a ); mkdirSync ( b ); 68const 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 }); 69t . after ( async () => { await bridge . close (); await worker . close (); try { rmSync ( dir , { recursive :true , force :true , maxRetries :3 }); } catch { /* best effort */ } }); 70const fwd = p => p . replaceAll ( BS , '/' ); 71const 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' }); } }); 72await run ( a , 'in-a.txt' ); 73const first = bridge . sessionOf ( 'one' ); 74await run ( b , 'in-b.txt' ); 75assert . notEqual ( bridge . sessionOf ( 'one' ), first , 'a new session for the new folder' ); 76assert . 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 78await run ( b , 'again.txt' ); 79assert . 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' , () => { 84const dir = mkdtempSync ( join ( tmpdir (), 'strata-root-' )); 85try { 86const def = join ( dir , 'Documents' , 'Strata' , 'Workspace' ); mkdirSync ( def , { recursive :true }); 87const chosen = join ( dir , 'mine' ); mkdirSync ( chosen ); 88const used = join ( dir , 'other' ); mkdirSync ( used ); 89assert . equal ( chatRootOf ({ chosen, used, fallback :def }), chosen , 'the chosen folder wins' ); 90assert . equal ( chatRootOf ({ chosen :null , used, fallback :def }), used , 'the folder the agent really wrote to' ); 91assert . equal ( chatRootOf ({ chosen :null , used :join ( dir , 'gone' ), fallback :def }), def , 'a folder that is gone is skipped' ); 92const root = chatRootOf ({ chosen :null , used :null , fallback :def }); 93assert . equal ( root , def , 'no chosen folder: the default workspace' ); 94writeFileSync ( join ( def , 'pagoda.html' ), '<h1>hi</h1>' ); 95assert . equal ( previewFile ( root , 'pagoda.html' , 'c1' ). kind , 'html' ); 96const rec = []; 97const r = openFile ( root , join ( def , 'pagoda.html' ), 'folder' , { platform :'darwin' , recorder :rec }); 98assert . equal ( r . ok , true ); assert . deepEqual ( rec [ 0 ]. slice ( 0 , 2 ), [ 'open' , '-R' ]); 99assert . equal ( openFile ( root , 'pagoda.html' , 'app' , { platform :'linux' , recorder :rec }). ok , true ); 100assert . equal ( openFile ( root , 'pagoda.html' , 'app' , { platform :'win32' , recorder :rec }). ok , true ); 101assert . throws (() => resolveChatFile ( root , join ( dir , 'secret.txt' )), / no longer there | outside / , 'the default folder is still a boundary' ); 102assert . equal ( chatRootOf ({}), null ); 103} finally { rmSync ( dir , { recursive :true , force :true }); } 104});