Files
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

37 lines
2.8 KiB
JavaScript

// Synthetic, local-only scan benchmark; no real agent logs or model calls.
// bun scripts/bench-scan.mjs [size-in-MiB=100] [session-count=1] [image-MiB-per-30-lines=0]
import { appendFileSync, closeSync, mkdtempSync, openSync, readSync, rmSync, statSync, writeSync } from "node:fs"
import { tmpdir } from "node:os"
import { join, resolve } from "node:path"
import { performance } from "node:perf_hooks"
import { createScanner } from "../lib/scan.mjs"
const mib=Number(process.argv[2]||100),count=Number(process.argv[3]||1),imageMiB=Number(process.argv[4]||0)
if(!Number.isInteger(mib)||mib<1||mib>512||!Number.isInteger(count)||count<1||count>8||!Number.isInteger(imageMiB)||imageMiB<0||imageMiB>16)throw new Error("Use 1..512 MiB, 1..8 sessions and 0..16 MiB images")
const root=mkdtempSync(join(tmpdir(),"office-scan-bench-")),paths=[]
let bytes=0,calls=0
const iso=new Date().toISOString(),json=x=>JSON.stringify(x)+"\n"
const body=json({type:"assistant",timestamp:iso,message:{role:"assistant",content:[{type:"text",text:"x".repeat(3950)}],stop_reason:"end_turn"}})
const image=imageMiB?json({type:"assistant",timestamp:iso,message:{role:"assistant",content:[{type:"image",source:{type:"base64",data:"A".repeat(imageMiB*1024*1024)}}],stop_reason:"end_turn"}}):""
const block=Buffer.from(imageMiB?body.repeat(30)+image:body.repeat(64))
try {
for(let i=0;i<count;i++) {
const path=join(root,`session-${i}.jsonl`),fd=openSync(path,"w")
try {for(let size=0;size<mib*1024*1024;size+=writeSync(fd,block)) {}}finally{closeSync(fd)}
paths.push(path)
}
const scanner=createScanner({sources:[{agent:"claude",kind:"claude",root,depth:0,match:n=>n.endsWith(".jsonl")}],readSync(fd,b,o,l,p){const n=readSync(fd,b,o,l,p);bytes+=n;calls++;return n}})
const sample=()=>{bytes=0;calls=0;const t=performance.now();const sessions=scanner.scan();return {ms:+(performance.now()-t).toFixed(3),bytes,calls,sessions:sessions.length}}
const initial=sample(),unchanged=sample(),appends=[]
for(let i=0;i<5;i++) {
let expectedBytes=0
for(const path of paths){const next=json({type:"user",timestamp:iso,message:{role:"user",content:`append-${i} `+"q".repeat(3900)}});appendFileSync(path,next);expectedBytes+=Buffer.byteLength(next)}
const observed=sample();if(observed.bytes<expectedBytes||observed.bytes>expectedBytes+128*count)throw new Error(`Append read ${observed.bytes}, expected ${expectedBytes} + bounded guards`)
appends.push({...observed,appendedBytes:expectedBytes,guardBytes:observed.bytes-expectedBytes})
}
console.log(JSON.stringify({sizeMiB:mib,sessionCount:count,imageMiB,totalBytes:paths.reduce((n,p)=>n+statSync(p).size,0),initial,unchanged,appends},null,2))
} finally {
const target=resolve(root),prefix=resolve(tmpdir())+"/office-scan-bench-"
if(!target.startsWith(prefix))throw new Error("Unexpected benchmark cleanup path")
rmSync(target,{recursive:true,force:true})
}