Files
plugins/packages/agent-office/cold-scan.test.mjs
T
QingJ 0e3562b25f agent-office: frame only fragments unfinished at EOF in cold image scans (#559)
The incremental session scanner frames only fragments still unfinished at
EOF, instead of passing every 64 KiB fragment of a complete cross-chunk
record through the character framer. A framed-part index stops a later
tick from walking text it already framed. Adds regressions for the four
mutation gaps named on #551 and a WebKit CSP diagnostic script.

Reviewed at 5f564b034df26e12b60c40f683922ce0389abdd1 on main 7714b61.
Run on the Mac mini test box (mote, Bun 1.4.2):
- packages/agent-office: HOME=$(mktemp -d) bun test: 167 pass, 0 fail;
  bun scripts/stamp-page.mjs --check: exit 0.
- Full repo: HOME=$(mktemp -d) bun test: 4321 pass, 9 skip, 0 fail
  (338 files); bun scripts/check.mjs: exit 0.
- Broken by hand: eager framing restored -> both cold-scan tests red;
  framed index not advanced -> 2 red (cold-scan EOF, unfinished quoted
  brace never reparses); ExtendedTail disabled -> extended-EOF test red.
  Each restored -> 167 pass.
- bench-scan.mjs 100 1 1, initial scan: ~29-32 ms at this head vs
  ~294-319 ms at main.
- Host probe: office.hooks.mjs imported without
  MAGPIE_AGENT_OFFICE_PAGE in a sandbox HOME with one Claude session;
  snapshot() ok and page/live.js written.
Not tried: a real magpie install of the page.
2026-10-31 19:09:38 +08:00

56 lines
2.8 KiB
JavaScript

import { afterAll, expect, test } from "bun:test"
import { appendFileSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"
import { tmpdir } from "node:os"
import { join } from "node:path"
import { createScanner } from "./lib/scan.mjs"
const roots=[]
const tmp=()=>{const root=mkdtempSync(join(tmpdir(),"office-cold-"));roots.push(root);return root}
afterAll(()=>{for(const root of roots)rmSync(root,{recursive:true,force:true})})
const now=Date.now(),timestamp=new Date(now).toISOString()
const line=record=>JSON.stringify(record)+"\n"
const user=text=>({type:"user",timestamp,message:{role:"user",content:text}})
const scanner=root=>createScanner({sources:[{root,kind:"claude",agent:"claude",depth:0,match:name=>name.endsWith(".jsonl")}]})
// framePart walks characters with String's iterator. Count actual large
// string traversal, rather than asserting a machine-dependent time limit.
// JSON.parse handles completed lines natively and does not use this hook.
function traversal(run) {
const original=String.prototype[Symbol.iterator]
let chars=0
String.prototype[Symbol.iterator]=function(){const text=String(this);if(text.length>=512)chars+=text.length;return original.call(this)}
try{return {result:run(),get chars(){return chars}}}finally{String.prototype[Symbol.iterator]=original}
}
test("cold image records spanning read chunks skip lexical character traversal",()=>{
const root=tmp(),path=join(root,"cold.jsonl")
const image=line({type:"assistant",timestamp,message:{role:"assistant",content:[{type:"image",source:{type:"base64",data:"A".repeat(1024*1024)}}],stop_reason:"end_turn"}})
writeFileSync(path,line(user("cold image work"))+image.repeat(4))
const sc=scanner(root)
const measured=traversal(()=>sc.scan(now)[0])
expect(measured.result).toMatchObject({query:"cold image work",turns:1,phase:"done"})
expect(measured.chars).toBe(0)
expect(sc.files.get(path).cursor.parts).toHaveLength(0)
expect(sc.files.get(path).cursor.framedParts).toBe(0)
})
test("only the EOF fragment is framed and existing fragments are not walked on later ticks",()=>{
const root=tmp(),path=join(root,"tail.jsonl")
const image=line({type:"padding",data:"A".repeat(1024*1024)})
const tail='{"type":"padding","text":"'+"q".repeat(10000)
writeFileSync(path,image+tail)
const sc=scanner(root)
const first=traversal(()=>sc.scan(now))
expect(first.chars).toBe(tail.length)
const unchanged=traversal(()=>sc.scan(now))
expect(unchanged.chars).toBe(0)
const more="x".repeat(1000)+"}"
appendFileSync(path,more)
const appended=traversal(()=>sc.scan(now))
expect(appended.chars).toBe(more.length)
appendFileSync(path,'"}\n'+line(user("after image")))
const completed=traversal(()=>sc.scan(now)[0])
expect(completed.result).toMatchObject({query:"after image",turns:1})
expect(completed.chars).toBe(0)
expect(sc.files.get(path).cursor.parts).toHaveLength(0)
})