22// through an SDK Client over an in-memory transport (#4854): the JSONL file
33// format, how unreadable lines are loaded and then kept on write, read and write
44// failures as tool errors, the atomic temp-file save, and two servers sharing
5- // one file. Tests that pin a known bug cite its issue (#4827),
6- // so the PR that fixes it has a test to change.
5+ // one file.
76import { describe , it , expect , beforeEach , afterEach , vi } from "vitest" ;
87import { promises as fs } from "fs" ;
98import path from "path" ;
@@ -268,11 +267,10 @@ describe("memory persistence over the protocol", () => {
268267 } ) ;
269268 } ) ;
270269
271- // KNOWN BUG #4827: pins current (wrong) behavior; the fix changes this assertion.
272- // Characterizes #4827: saveGraph writes a new temp file and renames it over
273- // the graph file, so the graph file takes a new file's mode. An operator's
274- // 0600 is lost, and a read-only file is silently replaced. The fix for #4827
275- // changes this test. POSIX only: Windows has no mode bits to lose.
270+ // #4827: saveGraph writes a new temp file and renames it over the graph
271+ // file, which replaces the file's inode. The save keeps the graph file's
272+ // own mode rather than a new file's, and refuses a read-only graph file as
273+ // a plain write would. POSIX only: Windows has no mode bits to lose.
276274 describe . skipIf ( process . platform === "win32" ) ( "file mode (#4827)" , ( ) => {
277275 // The mode a file created now gets, which is what the temp file gets.
278276 async function newFileMode ( ) : Promise < number > {
@@ -287,37 +285,50 @@ describe("memory persistence over the protocol", () => {
287285 return ( await fs . stat ( file ) ) . mode & 0o777 ;
288286 }
289287
290- it ( "replaces a hardened graph file's mode with a new file's mode " , async ( ) => {
288+ it ( "keeps a hardened graph file's mode across a save " , async ( ) => {
291289 const { client } = await open ( ) ;
292290 await call ( client , "create_entities" , { entities : [ alice ] } ) ;
293291 // 0600 as in the issue, unless the umask already makes new files 0600;
294- // the original mode must differ from a new file's for the loss to show.
295- const expected = await newFileMode ( ) ;
296- const original = expected === 0o600 ? 0o640 : 0o600 ;
292+ // the original mode must differ from a new file's for a loss to show.
293+ const original = ( await newFileMode ( ) ) === 0o600 ? 0o640 : 0o600 ;
297294 await fs . chmod ( filePath , original ) ;
295+ const writeFile = vi . spyOn ( fs , "writeFile" ) ;
298296
299297 await call ( client , "create_entities" , { entities : [ bob ] } ) ;
300298
301- expect ( await modeOf ( filePath ) ) . toBe ( expected ) ;
302- } ) ;
303-
304- it ( "silently overwrites a read-only graph file" , async ( ) => {
305- const { client } = await open ( ) ;
306- await call ( client , "create_entities" , { entities : [ alice ] } ) ;
307- await fs . chmod ( filePath , 0o444 ) ;
308- const expected = await newFileMode ( ) ;
309-
310- const result = await call ( client , "create_entities" , {
311- entities : [ bob ] ,
312- } ) ;
313-
314- expect ( result . isError ) . toBeUndefined ( ) ;
315- expect ( await modeOf ( filePath ) ) . toBe ( expected ) ;
299+ // The temp file is created with that mode, never a wider one first.
300+ expect ( writeFile ) . toHaveBeenCalledWith (
301+ expect . stringMatching ( / \. t m p $ / ) ,
302+ expect . any ( String ) ,
303+ { mode : original } ,
304+ ) ;
305+ expect ( await modeOf ( filePath ) ) . toBe ( original ) ;
316306 expect ( ( await call ( client , "read_graph" ) ) . structuredContent ) . toEqual ( {
317307 entities : [ alice , bob ] ,
318308 relations : [ ] ,
319309 } ) ;
320310 } ) ;
311+
312+ // root may write a read-only file, so the refusal shows only without it.
313+ it . skipIf ( process . getuid ?.( ) === 0 ) (
314+ "refuses to overwrite a read-only graph file" ,
315+ async ( ) => {
316+ const { client } = await open ( ) ;
317+ await call ( client , "create_entities" , { entities : [ alice ] } ) ;
318+ await fs . chmod ( filePath , 0o444 ) ;
319+ const before = await readFileText ( filePath ) ;
320+
321+ const result = await call ( client , "create_entities" , {
322+ entities : [ bob ] ,
323+ } ) ;
324+
325+ expect ( result . isError ) . toBe ( true ) ;
326+ expect ( textOf ( result ) ) . toMatch ( / E A C C E S / ) ;
327+ expect ( await modeOf ( filePath ) ) . toBe ( 0o444 ) ;
328+ expect ( await readFileText ( filePath ) ) . toBe ( before ) ;
329+ expect ( await fs . readdir ( dir ) ) . toEqual ( [ "memory.jsonl" ] ) ;
330+ } ,
331+ ) ;
321332 } ) ;
322333
323334 // #4797: two servers on one file (two client processes in practice) used to
0 commit comments