11import { mkdirSync , readFileSync , writeFileSync } from "node:fs" ;
2- import { join } from "node:path" ;
32import { networkInterfaces } from "node:os" ;
4- import { Database } from "../../src/shared/sqlite" ;
5- import { initializeDatabase } from "../../src/features/magic-context/storage-db" ;
6- import { insertMemory } from "../../src/features/magic-context/memory/storage-memory" ;
7- import { resolveProjectIdentityForSession } from "../../src/features/magic-context/memory/project-identity" ;
3+ import { join } from "node:path" ;
84import { memoryHistorySnapshot } from "../../src/features/magic-context/memory/history-applier" ;
5+ import { resolveProjectIdentityForSession } from "../../src/features/magic-context/memory/project-identity" ;
6+ import { insertMemory } from "../../src/features/magic-context/memory/storage-memory" ;
7+ import { HISTORIAN_FORMAT_INSTRUCTION } from "../../src/features/magic-context/memory/turn-one-publication" ;
98import { buildTurnOneAuthorizationRequest } from "../../src/features/magic-context/memory/turn-two" ;
9+ import { initializeDatabase } from "../../src/features/magic-context/storage-db" ;
1010import { historianSystemPrompt } from "../../src/hooks/magic-context/compartment-prompt" ;
11- import { HISTORIAN_FORMAT_INSTRUCTION } from "../../src/features/magic-context/memory/turn-one-publication" ;
12- import { CampaignLedger } from "../two-pass-live-replay/artifact-schema" ;
13- import { attestHost , disposableEnvironment } from "../two-pass-live-replay/host-drivers" ;
11+ import { Database } from "../../src/shared/sqlite" ;
1412import { callMeter , readPluginStore } from "../two-pass-activation-runs/activation-driver" ;
1513import { hostAdapter } from "../two-pass-activation-runs/host-adapters" ;
1614import { sumProviderSteps } from "../two-pass-activation-runs/provider-meter" ;
17- import { loadCases } from "./run" ;
15+ import { CampaignLedger } from "../two-pass-live-replay/artifact-schema" ;
16+ import { attestHost , disposableEnvironment } from "../two-pass-live-replay/host-drivers" ;
1817import { turnOne } from "./prompts" ;
18+ import { loadCases } from "./run" ;
1919
2020const ZEN_PROVIDER_ID = "opencode" ;
2121const ZEN_MODEL_ID = "deepseek-v4.1-flash-free" ;
@@ -25,7 +25,8 @@ const OUTPUT_CEILING = 32000;
2525if ( import . meta. main ) {
2626 const [ root , install , variantArg , outArg ] = process . argv . slice ( 2 ) ;
2727 const variant = variantArg === "high" ? "high" : variantArg === "max" ? "max" : null ;
28- if ( ! root || ! install ) throw new Error ( "usage: zen-host-trial.ts <root> <install> [high|max] [outDir]" ) ;
28+ if ( ! root || ! install )
29+ throw new Error ( "usage: zen-host-trial.ts <root> <install> [high|max] [outDir]" ) ;
2930 mkdirSync ( root , { recursive : true , mode : 0o700 } ) ;
3031 const outDir = outArg ?? join ( root , "zen-host-001" ) ;
3132 mkdirSync ( outDir , { recursive : true , mode : 0o700 } ) ;
@@ -99,7 +100,12 @@ if (import.meta.main) {
99100 two_pass : false ,
100101 maxTokens : OUTPUT_CEILING ,
101102 } ,
102- memory : { enabled : true , auto_promote : true , auto_search : { enabled : false } , git_commit_indexing : { enabled : false } } ,
103+ memory : {
104+ enabled : true ,
105+ auto_promote : true ,
106+ auto_search : { enabled : false } ,
107+ git_commit_indexing : { enabled : false } ,
108+ } ,
103109 embedding : { provider : "off" } ,
104110 } ,
105111 } ) ;
@@ -123,7 +129,11 @@ if (import.meta.main) {
123129 content : m . content ,
124130 sourceType : "historian" ,
125131 } ) ;
126- db . prepare ( "UPDATE memories SET id=?,revision=? WHERE id=?" ) . run ( m . id , m . revision , stored . id ) ;
132+ db . prepare ( "UPDATE memories SET id=?,revision=? WHERE id=?" ) . run (
133+ m . id ,
134+ m . revision ,
135+ stored . id ,
136+ ) ;
127137 }
128138 const seed = db . prepare ( "SELECT id FROM memories LIMIT 1" ) . get ( ) ;
129139 if ( c . memories . length && ! seed ) throw new Error ( "seed_insert_failed" ) ;
@@ -133,46 +143,119 @@ if (import.meta.main) {
133143 db . close ( ) ;
134144 }
135145 const firstRequest = turnOne ( c , "enabled" ) ;
136- writeFileSync ( join ( outDir , `case-${ c . id } -trial-first-request.json` ) , JSON . stringify ( firstRequest , null , 2 ) , { mode : 0o600 } ) ;
146+ writeFileSync (
147+ join ( outDir , `case-${ c . id } -trial-first-request.json` ) ,
148+ JSON . stringify ( firstRequest , null , 2 ) ,
149+ { mode : 0o600 } ,
150+ ) ;
137151 // For activation, send only the transcript (new_messages body), not the prebuilt historian prompt.
138152 // The plugin builds its own historian prompt (examples + references + transcript + format)
139153 // from session history; sending the prebuilt prompt would double-wrap examples/format
140154 // and fail validation (as observed: "no usable compartments").
141155 const newMessagesMatch = c . userPrompt . match ( / < n e w _ m e s s a g e s > [ \s \S ] * ?< \/ n e w _ m e s s a g e s > / ) ;
142- const transcriptOnly = newMessagesMatch ? newMessagesMatch [ 0 ] . slice ( 0 , 150_000 ) : ( firstRequest . messages [ 1 ] . content as string ) . slice ( 0 , 150_000 ) ;
156+ const transcriptOnly = newMessagesMatch
157+ ? newMessagesMatch [ 0 ] . slice ( 0 , 150_000 )
158+ : ( firstRequest . messages [ 1 ] . content as string ) . slice ( 0 , 150_000 ) ;
143159 let host : Awaited < ReturnType < typeof adapter . start > > | undefined ;
144160 try {
145- host = await adapter . start ( { installRoot : install , pluginEntrypoint, env : caseEnv , meter : callMeter ( ledger , profile as never ) } ) ;
161+ host = await adapter . start ( {
162+ installRoot : install ,
163+ pluginEntrypoint,
164+ env : caseEnv ,
165+ meter : callMeter ( ledger , profile as never ) ,
166+ } ) ;
146167 const before = await attestHost ( caseRoot , caseEnv , host . pid ) ;
147- if ( before . proof . tool !== "lsof -Fpn" ) throw new Error ( "proof_requires_lsof_not_proc_fallback" ) ;
148- writeFileSync ( join ( outDir , `case-${ c . id } -lsof-before.json` ) , JSON . stringify ( before , null , 2 ) , { mode : 0o600 } ) ;
168+ if ( before . proof . tool !== "lsof -Fpn" )
169+ throw new Error ( "proof_requires_lsof_not_proc_fallback" ) ;
170+ writeFileSync (
171+ join ( outDir , `case-${ c . id } -lsof-before.json` ) ,
172+ JSON . stringify ( before , null , 2 ) ,
173+ { mode : 0o600 } ,
174+ ) ;
149175 const promptText = transcriptOnly ;
150176 const session = ( await host . runSession ( promptText ) ) as {
151- requests : Array < { request : { path : string ; body : string ; providerHost ?: string } ; response : { status : number ; body : string ; contentType : string } ; steps ?: Array < { inputTokens : number ; outputTokens : number } > } > ;
177+ requests : Array < {
178+ request : { path : string ; body : string ; providerHost ?: string } ;
179+ response : { status : number ; body : string ; contentType : string } ;
180+ steps ?: Array < { inputTokens : number ; outputTokens : number } > ;
181+ } > ;
152182 carrier ?: string ;
153183 sessionId ?: string ;
154184 } ;
155- if ( host . carrier !== "opencode-session" ) throw new Error ( `observed_carrier_mismatch:${ host . carrier } ` ) ;
185+ if ( host . carrier !== "opencode-session" )
186+ throw new Error ( `observed_carrier_mismatch:${ host . carrier } ` ) ;
156187 const after = await attestHost ( caseRoot , caseEnv , host . pid ) ;
157- if ( after . proof . tool !== "lsof -Fpn" ) throw new Error ( "proof_requires_lsof_not_proc_fallback" ) ;
158- writeFileSync ( join ( outDir , `case-${ c . id } -lsof-after.json` ) , JSON . stringify ( after , null , 2 ) , { mode : 0o600 } ) ;
188+ if ( after . proof . tool !== "lsof -Fpn" )
189+ throw new Error ( "proof_requires_lsof_not_proc_fallback" ) ;
190+ writeFileSync (
191+ join ( outDir , `case-${ c . id } -lsof-after.json` ) ,
192+ JSON . stringify ( after , null , 2 ) ,
193+ { mode : 0o600 } ,
194+ ) ;
159195 if ( host . storePath === null ) throw new Error ( "plugin_store_unobservable" ) ;
160196 const store = readPluginStore ( caseRoot , host . storePath ) ;
161- const stages = ( store . stages as Array < Record < string , unknown > > ) . filter ( ( r ) => r . stage === "turn_one" || r . stage === "turn_two" ) ;
162- const turnOneStage = ( store . stages as Array < { stage : string ; state : string ; reply_json : string | null ; child_session_id : string | null } > ) . find ( ( r ) => r . stage === "turn_one" && r . state === "published" ) ;
163- const turnTwoStage = ( store . stages as Array < { stage : string ; state : string ; reply_json : string | null ; child_session_id : string | null } > ) . find ( ( r ) => r . stage === "turn_two" && r . state === "resolved" ) ;
164- const firstReply = turnOneStage ?. reply_json ? ( ( JSON . parse ( turnOneStage . reply_json ) as { text ?: string } ) . text ?? turnOneStage . reply_json ) : null ;
165- const secondReply = turnTwoStage ?. reply_json ? ( ( JSON . parse ( turnTwoStage . reply_json ) as { text ?: string } ) . text ?? turnTwoStage . reply_json ) : null ;
166- if ( firstReply ) writeFileSync ( join ( outDir , `case-${ c . id } -turn-one-reply.txt` ) , firstReply , { mode : 0o600 } ) ;
167- if ( secondReply ) writeFileSync ( join ( outDir , `case-${ c . id } -turn-two-reply.txt` ) , secondReply , { mode : 0o600 } ) ;
168- writeFileSync ( join ( outDir , `case-${ c . id } -provider-receipts.json` ) , JSON . stringify ( session . requests , null , 2 ) , { mode : 0o600 } ) ;
169- writeFileSync ( join ( outDir , `case-${ c . id } -stages.json` ) , JSON . stringify ( stages , null , 2 ) , { mode : 0o600 } ) ;
197+ const stages = ( store . stages as Array < Record < string , unknown > > ) . filter (
198+ ( r ) => r . stage === "turn_one" || r . stage === "turn_two" ,
199+ ) ;
200+ const turnOneStage = (
201+ store . stages as Array < {
202+ stage : string ;
203+ state : string ;
204+ reply_json : string | null ;
205+ child_session_id : string | null ;
206+ } >
207+ ) . find ( ( r ) => r . stage === "turn_one" && r . state === "published" ) ;
208+ const turnTwoStage = (
209+ store . stages as Array < {
210+ stage : string ;
211+ state : string ;
212+ reply_json : string | null ;
213+ child_session_id : string | null ;
214+ } >
215+ ) . find ( ( r ) => r . stage === "turn_two" && r . state === "resolved" ) ;
216+ const firstReply = turnOneStage ?. reply_json
217+ ? ( ( JSON . parse ( turnOneStage . reply_json ) as { text ?: string } ) . text ??
218+ turnOneStage . reply_json )
219+ : null ;
220+ const secondReply = turnTwoStage ?. reply_json
221+ ? ( ( JSON . parse ( turnTwoStage . reply_json ) as { text ?: string } ) . text ??
222+ turnTwoStage . reply_json )
223+ : null ;
224+ if ( firstReply )
225+ writeFileSync ( join ( outDir , `case-${ c . id } -turn-one-reply.txt` ) , firstReply , {
226+ mode : 0o600 ,
227+ } ) ;
228+ if ( secondReply )
229+ writeFileSync ( join ( outDir , `case-${ c . id } -turn-two-reply.txt` ) , secondReply , {
230+ mode : 0o600 ,
231+ } ) ;
232+ writeFileSync (
233+ join ( outDir , `case-${ c . id } -provider-receipts.json` ) ,
234+ JSON . stringify ( session . requests , null , 2 ) ,
235+ { mode : 0o600 } ,
236+ ) ;
237+ writeFileSync (
238+ join ( outDir , `case-${ c . id } -stages.json` ) ,
239+ JSON . stringify ( stages , null , 2 ) ,
240+ { mode : 0o600 } ,
241+ ) ;
170242 const historianRequests = session . requests . filter ( ( r ) => {
171243 try {
172- const body = JSON . parse ( r . request . body ) as { messages ?: Array < { role : string ; content : unknown } > ; system ?: unknown } ;
244+ const body = JSON . parse ( r . request . body ) as {
245+ messages ?: Array < { role : string ; content : unknown } > ;
246+ system ?: unknown ;
247+ } ;
173248 const text = ( v : unknown ) : string =>
174- typeof v === "string" ? v : Array . isArray ( v ) ? v . map ( text ) . join ( "\n" ) : typeof ( v as { text ?: unknown } ) ?. text === "string" ? ( v as { text : string } ) . text : "" ;
175- const sys = body . system ?? body . messages ?. filter ( ( m ) => m . role === "system" ) . map ( ( m ) => m . content ) ;
249+ typeof v === "string"
250+ ? v
251+ : Array . isArray ( v )
252+ ? v . map ( text ) . join ( "\n" )
253+ : typeof ( v as { text ?: unknown } ) ?. text === "string"
254+ ? ( v as { text : string } ) . text
255+ : "" ;
256+ const sys =
257+ body . system ??
258+ body . messages ?. filter ( ( m ) => m . role === "system" ) . map ( ( m ) => m . content ) ;
176259 return text ( sys ) . includes ( historianSystemPrompt ( true ) . slice ( 0 , 128 ) ) ;
177260 } catch {
178261 return false ;
@@ -186,12 +269,40 @@ if (import.meta.main) {
186269 providerRequestCount : session . requests . length ,
187270 turnOnePublished : ! ! turnOneStage ,
188271 turnTwoResolved : ! ! turnTwoStage ,
189- sameChild : ! ! turnOneStage && ! ! turnTwoStage && turnOneStage . child_session_id === turnTwoStage . child_session_id ,
272+ sameChild :
273+ ! ! turnOneStage &&
274+ ! ! turnTwoStage &&
275+ turnOneStage . child_session_id === turnTwoStage . child_session_id ,
190276 firstReplyChars : firstReply ?. length ?? 0 ,
191277 secondReplyChars : secondReply ?. length ?? 0 ,
192278 usage : sumProviderSteps ( session . requests . flatMap ( ( r ) => r . steps ?? [ ] ) ) ,
193- maxTokensObserved : [ ...new Set ( historianRequests . map ( ( r ) => { try { return ( JSON . parse ( r . request . body ) as { max_tokens ?: number } ) . max_tokens ; } catch { return undefined ; } } ) ) ] ,
194- reasoningFields : [ ...new Set ( historianRequests . map ( ( r ) => { try { const b = JSON . parse ( r . request . body ) as Record < string , unknown > ; return JSON . stringify ( { reasoning : b . reasoning ?? null , thinking : b . thinking ?? null } ) ; } catch { return "unparseable" ; } } ) ) ] ,
279+ maxTokensObserved : [
280+ ...new Set (
281+ historianRequests . map ( ( r ) => {
282+ try {
283+ return ( JSON . parse ( r . request . body ) as { max_tokens ?: number } )
284+ . max_tokens ;
285+ } catch {
286+ return undefined ;
287+ }
288+ } ) ,
289+ ) ,
290+ ] ,
291+ reasoningFields : [
292+ ...new Set (
293+ historianRequests . map ( ( r ) => {
294+ try {
295+ const b = JSON . parse ( r . request . body ) as Record < string , unknown > ;
296+ return JSON . stringify ( {
297+ reasoning : b . reasoning ?? null ,
298+ thinking : b . thinking ?? null ,
299+ } ) ;
300+ } catch {
301+ return "unparseable" ;
302+ }
303+ } ) ,
304+ ) ,
305+ ] ,
195306 } ) ;
196307 writeFileSync (
197308 join ( outDir , `transcript-case-${ c . id } .md` ) ,
@@ -204,8 +315,32 @@ if (import.meta.main) {
204315 }
205316 writeFileSync (
206317 join ( outDir , "zen-host-summary.json" ) ,
207- JSON . stringify ( { lane : "opencode-1" , carrier : "opencode-session" , provider : ZEN_PROVIDER_ID , modelId : ZEN_MODEL_ID , variant, outputCeiling : OUTPUT_CEILING , network : "bridge-for-zen-provider-call" , networkIsolation, ledger : ledger . rows , summary, actualUSDSpent : 0 , credentials : "none (Zen free tier)" } , null , 2 ) ,
318+ JSON . stringify (
319+ {
320+ lane : "opencode-1" ,
321+ carrier : "opencode-session" ,
322+ provider : ZEN_PROVIDER_ID ,
323+ modelId : ZEN_MODEL_ID ,
324+ variant,
325+ outputCeiling : OUTPUT_CEILING ,
326+ network : "bridge-for-zen-provider-call" ,
327+ networkIsolation,
328+ ledger : ledger . rows ,
329+ summary,
330+ actualUSDSpent : 0 ,
331+ credentials : "none (Zen free tier)" ,
332+ } ,
333+ null ,
334+ 2 ,
335+ ) ,
208336 { mode : 0o600 } ,
209337 ) ;
210- console . log ( JSON . stringify ( { lane : "opencode-1" , carrier : "opencode-session" , cases : summary . length , outDir } ) ) ;
338+ console . log (
339+ JSON . stringify ( {
340+ lane : "opencode-1" ,
341+ carrier : "opencode-session" ,
342+ cases : summary . length ,
343+ outDir,
344+ } ) ,
345+ ) ;
211346}
0 commit comments