pi-daddy 0.24.0 → 0.25.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +21 -0
- package/README.md +12 -4
- package/THIRD_PARTY_LICENSES/herdr-Apache-2.0.txt +201 -0
- package/THIRD_PARTY_NOTICES.md +7 -0
- package/contracts/ledger/v4/README.md +1 -1
- package/dist/daily-dashboard-host.d.ts +26 -2
- package/dist/daily-dashboard-host.d.ts.map +1 -1
- package/dist/daily-dashboard-host.js +78 -8
- package/dist/daily-dashboard-host.js.map +1 -1
- package/dist/dashboard-handshake.d.ts.map +1 -1
- package/dist/dashboard-handshake.js +13 -4
- package/dist/dashboard-handshake.js.map +1 -1
- package/dist/dashboard-harness.d.ts +1 -1
- package/dist/dashboard-harness.js +3 -3
- package/dist/dashboard-harness.js.map +1 -1
- package/dist/dashboard-herdr.d.ts +1 -0
- package/dist/dashboard-herdr.d.ts.map +1 -1
- package/dist/dashboard-herdr.js +10 -8
- package/dist/dashboard-herdr.js.map +1 -1
- package/dist/dashboard-host-contract.d.ts +10 -1
- package/dist/dashboard-host-contract.d.ts.map +1 -1
- package/dist/dashboard-host-contract.js +1 -1
- package/dist/dashboard-host-contract.js.map +1 -1
- package/dist/dashboard-host.d.ts +8 -0
- package/dist/dashboard-host.d.ts.map +1 -1
- package/dist/dashboard-host.js +24 -6
- package/dist/dashboard-host.js.map +1 -1
- package/dist/dashboard-observation.d.ts.map +1 -1
- package/dist/dashboard-observation.js +1 -0
- package/dist/dashboard-observation.js.map +1 -1
- package/dist/experiment.d.ts.map +1 -1
- package/dist/experiment.js +9 -4
- package/dist/experiment.js.map +1 -1
- package/dist/herdr-pi-lifecycle.d.ts +8 -0
- package/dist/herdr-pi-lifecycle.d.ts.map +1 -0
- package/dist/herdr-pi-lifecycle.js +22 -0
- package/dist/herdr-pi-lifecycle.js.map +1 -0
- package/dist/herdr-poll.d.ts +21 -0
- package/dist/herdr-poll.d.ts.map +1 -1
- package/dist/herdr-poll.js +52 -3
- package/dist/herdr-poll.js.map +1 -1
- package/dist/herdr-start.d.ts +9 -0
- package/dist/herdr-start.d.ts.map +1 -0
- package/dist/herdr-start.js +15 -0
- package/dist/herdr-start.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/intent-application.d.ts.map +1 -1
- package/dist/intent-application.js +3 -2
- package/dist/intent-application.js.map +1 -1
- package/dist/measured-order.d.ts +249 -0
- package/dist/measured-order.d.ts.map +1 -0
- package/dist/measured-order.js +251 -0
- package/dist/measured-order.js.map +1 -0
- package/dist/measured-session.d.ts +97 -0
- package/dist/measured-session.d.ts.map +1 -0
- package/dist/measured-session.js +134 -0
- package/dist/measured-session.js.map +1 -0
- package/dist/run-herdr.d.ts +3 -3
- package/dist/run-herdr.d.ts.map +1 -1
- package/dist/run-herdr.js +28 -33
- package/dist/run-herdr.js.map +1 -1
- package/dist/vendor/herdr-pi-lifecycle.d.ts +2 -0
- package/dist/vendor/herdr-pi-lifecycle.d.ts.map +1 -0
- package/dist/vendor/herdr-pi-lifecycle.js +223 -0
- package/dist/vendor/herdr-pi-lifecycle.js.map +1 -0
- package/dist/work-command.d.ts +2 -0
- package/dist/work-command.d.ts.map +1 -1
- package/dist/work-command.js +15 -2
- package/dist/work-command.js.map +1 -1
- package/extensions/daily-dashboard-session.ts +9 -8
- package/extensions/grants-command.ts +2 -0
- package/extensions/grants.ts +1 -1
- package/package.json +15 -1
- package/src/daily-dashboard-host.ts +49 -10
- package/src/dashboard-handshake.ts +14 -5
- package/src/dashboard-harness.ts +3 -3
- package/src/dashboard-herdr.ts +12 -9
- package/src/dashboard-host-contract.ts +5 -2
- package/src/dashboard-host.ts +14 -9
- package/src/dashboard-observation.ts +1 -0
- package/src/experiment.ts +8 -3
- package/src/herdr-pi-lifecycle.ts +21 -0
- package/src/herdr-poll.ts +52 -2
- package/src/herdr-start.ts +20 -0
- package/src/index.ts +17 -0
- package/src/intent-application.ts +3 -2
- package/src/measured-order.ts +67 -0
- package/src/measured-session.ts +70 -0
- package/src/run-herdr.ts +28 -40
- package/src/vendor/herdr-pi-lifecycle.ts +257 -0
- package/src/work-command.ts +8 -2
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import { constants } from "node:fs";
|
|
3
|
+
import { mkdir, open, readFile, realpath, lstat, writeFile } from "node:fs/promises";
|
|
4
|
+
import { isAbsolute, join, resolve } from "node:path";
|
|
5
|
+
import { withFileLock } from "./file-lock.ts";
|
|
6
|
+
import { intentKey } from "./intent-control.ts";
|
|
7
|
+
import { runMeasuredAgentSession, MeasuredSessionFailure, MeasuredSessionUnknownError, type MeasuredSessionHost, type MeasuredSessionInput } from "./measured-session.ts";
|
|
8
|
+
import { openResourceBudget, resourceBindingDigest, type GovernedBudgetBinding } from "./resource-budget.ts";
|
|
9
|
+
import { freezeWork } from "./work-ledger-json.ts";
|
|
10
|
+
|
|
11
|
+
const ID=/^[a-zA-Z0-9:_-]{1,128}$/,HEX=/^[a-f0-9]{64}$/;
|
|
12
|
+
const hash=(value:unknown)=>createHash("sha256").update(typeof value==="string"?value:intentKey(value)).digest("hex");
|
|
13
|
+
const exact=(value:object,keys:string[])=>Object.keys(value).sort().join()===keys.sort().join();
|
|
14
|
+
export type MeasuredOutputContract={version:"measured-output-exact-v1";expectedSha256:string}|{version:"measured-output-json-projection-v1";requiredKeys:string[];textFields:string[];arrayField:string;minItems:number;maxItems:number;itemRequiredFields:string[];projectionFields:string[];expectedProjectionSha256:string};
|
|
15
|
+
export interface MeasuredOrderAttempt extends Omit<MeasuredSessionInput,"budget"|"attempt"|"signal">{attemptId:string;includeDependencies:boolean;qualification:MeasuredOutputContract}
|
|
16
|
+
export interface MeasuredOrderNode {nodeId:string;dependencies:string[];attempts:MeasuredOrderAttempt[]}
|
|
17
|
+
export interface MeasuredOrder {version:"measured-order-v1";orderId:string;authorityDigest:string;budgetDigest:string;nodes:MeasuredOrderNode[]}
|
|
18
|
+
export interface MeasuredOrderBinding {version:"measured-order-binding-v1";directory:string;directoryDevice:string;directoryInode:string;journalDevice:string;journalInode:string;budget:GovernedBudgetBinding;order:MeasuredOrder;digest:string}
|
|
19
|
+
export interface MeasuredOrderAuthority {authorityDigest:string;orderDigests:string[];acknowledgementDigests:string[]}
|
|
20
|
+
export interface MeasuredOrderAcknowledgement {version:"measured-order-ack-v1";requestId:string;bindingDigest:string;attemptId:string;claimOwner:string;claimDigest:string}
|
|
21
|
+
type JournalEvent=
|
|
22
|
+
|{type:"claim";sequence:number;previousDigest:string|null;attemptId:string;owner:string;digest:string}
|
|
23
|
+
|{type:"result";sequence:number;previousDigest:string;attemptId:string;owner:string;state:"completed"|"failed"|"cancelled"|"unknown"|"mismatch";outputSha256:string|null;result:unknown;digest:string}
|
|
24
|
+
|{type:"acknowledge-unknown";sequence:number;previousDigest:string;request:MeasuredOrderAcknowledgement;digest:string};
|
|
25
|
+
export interface MeasuredOrderNodeView {nodeId:string;state:"eligible"|"dependency-blocked"|"running"|"unknown"|"exhausted"|"satisfied";action:"dispatch"|"wait"|"acknowledge"|"none";attemptId:string|null;outputSha256:string|null;unknownClaim:{owner:string;digest:string}|null}
|
|
26
|
+
function field(value:unknown):value is string{return typeof value==="string"&&/^[A-Za-z][A-Za-z0-9_-]{0,63}$/.test(value);}
|
|
27
|
+
function checkedContract(input:MeasuredOutputContract):MeasuredOutputContract{const c=JSON.parse(intentKey(input)) as MeasuredOutputContract;if(c.version==="measured-output-exact-v1"){if(!exact(c,["version","expectedSha256"])||!HEX.test(c.expectedSha256))throw Error("invalid exact output contract");return c;}if(c.version!=="measured-output-json-projection-v1"||!exact(c,["version","requiredKeys","textFields","arrayField","minItems","maxItems","itemRequiredFields","projectionFields","expectedProjectionSha256"])||![c.requiredKeys,c.textFields,c.itemRequiredFields,c.projectionFields].every(x=>Array.isArray(x)&&x.length<=32&&new Set(x).size===x.length&&x.every(field))||!field(c.arrayField)||!Number.isSafeInteger(c.minItems)||!Number.isSafeInteger(c.maxItems)||c.minItems<0||c.maxItems<c.minItems||c.maxItems>4096||!c.requiredKeys.includes(c.arrayField)||c.textFields.some(x=>!c.requiredKeys.includes(x))||c.projectionFields.some(x=>!c.itemRequiredFields.includes(x))||!HEX.test(c.expectedProjectionSha256))throw Error("invalid deterministic JSON output contract");return c;}
|
|
28
|
+
export function qualifyMeasuredOutput(output:string,input:MeasuredOutputContract){const contract=checkedContract(input),outputSha256=hash(output);if(contract.version==="measured-output-exact-v1")return{eligible:outputSha256===contract.expectedSha256,outputSha256,projectionSha256:null};let value:unknown;try{value=JSON.parse(output);}catch{return{eligible:false,outputSha256,projectionSha256:null};}if(!value||typeof value!=="object"||Array.isArray(value)||Object.keys(value).sort().join()!==[...contract.requiredKeys].sort().join())return{eligible:false,outputSha256,projectionSha256:null};const object=value as Record<string,unknown>,items=object[contract.arrayField];if(contract.textFields.some(x=>typeof object[x]!=="string"||!(object[x] as string).trim())||!Array.isArray(items)||items.length<contract.minItems||items.length>contract.maxItems||items.some(x=>!x||typeof x!=="object"||Array.isArray(x)||contract.itemRequiredFields.some(k=>!Object.hasOwn(x,k))))return{eligible:false,outputSha256,projectionSha256:null};const projection=(items as Record<string,unknown>[]).map(item=>Object.fromEntries(contract.projectionFields.map(k=>[k,item[k]]))),projectionSha256=hash(intentKey(projection));return{eligible:projectionSha256===contract.expectedProjectionSha256,outputSha256,projectionSha256};}
|
|
29
|
+
|
|
30
|
+
function checkedOrder(input:MeasuredOrder,budget:GovernedBudgetBinding):MeasuredOrder{
|
|
31
|
+
const order=JSON.parse(intentKey(input)) as MeasuredOrder;if(!exact(order,["version","orderId","authorityDigest","budgetDigest","nodes"])||order.version!=="measured-order-v1"||!ID.test(order.orderId)||!HEX.test(order.authorityDigest)||order.budgetDigest!==resourceBindingDigest(budget)||!Array.isArray(order.nodes)||!order.nodes.length||order.nodes.length>8)throw Error("invalid bounded measured order");
|
|
32
|
+
const ids=new Set<string>(),attempts=new Set<string>();let inputBytes=0,count=0;
|
|
33
|
+
for(const node of order.nodes){if(!exact(node,["nodeId","dependencies","attempts"])||!ID.test(node.nodeId)||ids.has(node.nodeId)||!Array.isArray(node.dependencies)||node.dependencies.length>8||new Set(node.dependencies).size!==node.dependencies.length||!Array.isArray(node.attempts)||!node.attempts.length||node.attempts.length>3)throw Error("invalid node/dependency/recovery policy");ids.add(node.nodeId);
|
|
34
|
+
for(const a of node.attempts){if(!exact(a,["attemptId","model","thinkingLevel","prompt","deadlineMs","terminationGraceMs","maxRetainedOutputBytes","maxOutputTokens","includeDependencies","qualification"])||!ID.test(a.attemptId)||attempts.has(a.attemptId)||typeof a.includeDependencies!=="boolean"||!["openai-codex/gpt-5.6-sol","openai-codex/gpt-5.6-terra"].includes(a.model)||typeof a.prompt!=="string"||!a.prompt.length||Buffer.byteLength(a.prompt)>16384||!Number.isSafeInteger(a.deadlineMs)||a.deadlineMs<1000||a.deadlineMs>300000||!Number.isSafeInteger(a.terminationGraceMs)||a.terminationGraceMs<100||a.terminationGraceMs>10000||!Number.isSafeInteger(a.maxRetainedOutputBytes)||a.maxRetainedOutputBytes<1||a.maxRetainedOutputBytes>1024*1024||!Number.isSafeInteger(a.maxOutputTokens)||a.maxOutputTokens<1||a.maxOutputTokens>16384)throw Error("invalid read-only model attempt");checkedContract(a.qualification);attempts.add(a.attemptId);inputBytes+=Buffer.byteLength(a.prompt);count++;}
|
|
35
|
+
}
|
|
36
|
+
for(const node of order.nodes)for(const attempt of node.attempts)if(attempt.includeDependencies){let extra=0;for(const dependency of node.dependencies){const prior=order.nodes.find(n=>n.nodeId===dependency);extra+=256+Math.max(...(prior?.attempts.map(a=>a.maxRetainedOutputBytes)??[0]));}if(Buffer.byteLength(attempt.prompt)+extra>16384)throw Error("sealed dependency prompt exceeds attempt bound");inputBytes+=extra;}
|
|
37
|
+
if(count>budget.limits.maxAttempts||inputBytes>budget.limits.maxInputBytes||budget.limits.maxConcurrent<count)throw Error("order exceeds original accounting allowance");
|
|
38
|
+
const visiting=new Set<string>(),done=new Set<string>();const visit=(id:string)=>{if(done.has(id))return;if(visiting.has(id))throw Error("dependency cycle");const node=order.nodes.find(n=>n.nodeId===id);if(!node)throw Error("missing dependency");visiting.add(id);node.dependencies.forEach(visit);visiting.delete(id);done.add(id);};order.nodes.forEach(n=>visit(n.nodeId));return order;
|
|
39
|
+
}
|
|
40
|
+
export const measuredOrderDigest=(order:MeasuredOrder)=>hash(order);
|
|
41
|
+
export function measuredOrderAcknowledgementDigest(input:MeasuredOrderAcknowledgement){const value=JSON.parse(intentKey(input)) as MeasuredOrderAcknowledgement;if(!exact(value,["version","requestId","bindingDigest","attemptId","claimOwner","claimDigest"])||value.version!=="measured-order-ack-v1"||![value.requestId,value.attemptId,value.claimOwner].every(x=>ID.test(x))||![value.bindingDigest,value.claimDigest].every(x=>HEX.test(x)))throw Error("invalid unknown acknowledgement");return hash(value);}
|
|
42
|
+
function paths(directory:string){return{binding:join(directory,"binding.json"),journal:join(directory,"journal.jsonl")};}
|
|
43
|
+
async function privateDirectory(directory:string){const st=await lstat(directory,{bigint:true});if(!st.isDirectory()||(st.mode&0o077n)||st.uid!==BigInt(process.getuid!())||await realpath(directory)!==directory)throw Error("owner-private measured order directory required");}
|
|
44
|
+
export async function createMeasuredOrder(input:{directory:string;budget:GovernedBudgetBinding;order:MeasuredOrder}):Promise<Readonly<MeasuredOrderBinding>>{
|
|
45
|
+
if(!isAbsolute(input.directory)||resolve(input.directory)!==input.directory)throw Error("absolute measured order directory required");const order=checkedOrder(input.order,input.budget);await mkdir(input.directory,{recursive:false,mode:0o700});await privateDirectory(input.directory);
|
|
46
|
+
const p=paths(input.directory);await writeFile(p.journal,"",{mode:0o600,flag:"wx"});const directoryStat=await lstat(input.directory,{bigint:true}),journalStat=await lstat(p.journal,{bigint:true}),identity={directory:input.directory,directoryDevice:String(directoryStat.dev),directoryInode:String(directoryStat.ino),journalDevice:String(journalStat.dev),journalInode:String(journalStat.ino)};
|
|
47
|
+
const binding={version:"measured-order-binding-v1" as const,...identity,budget:input.budget,order,digest:hash({...identity,budgetDigest:resourceBindingDigest(input.budget),orderDigest:measuredOrderDigest(order)})};await writeFile(p.binding,intentKey(binding)+"\n",{mode:0o600,flag:"wx"});return freezeWork(binding) as unknown as Readonly<MeasuredOrderBinding>;
|
|
48
|
+
}
|
|
49
|
+
function checkedBinding(input:MeasuredOrderBinding):MeasuredOrderBinding{const b=JSON.parse(intentKey(input)) as MeasuredOrderBinding,identity={directory:b.directory,directoryDevice:b.directoryDevice,directoryInode:b.directoryInode,journalDevice:b.journalDevice,journalInode:b.journalInode};if(!exact(b,["version","directory","directoryDevice","directoryInode","journalDevice","journalInode","budget","order","digest"])||b.version!=="measured-order-binding-v1"||!isAbsolute(b.directory)||resolve(b.directory)!==b.directory||![b.directoryDevice,b.directoryInode,b.journalDevice,b.journalInode].every(x=>/^\d+$/.test(x))||b.digest!==hash({...identity,budgetDigest:resourceBindingDigest(b.budget),orderDigest:measuredOrderDigest(checkedOrder(b.order,b.budget))}))throw Error("invalid measured order binding");return b;}
|
|
50
|
+
async function pinnedJournal(binding:MeasuredOrderBinding,writable=false){const path=paths(binding.directory).journal,handle=await open(path,(writable?constants.O_RDWR|constants.O_APPEND:constants.O_RDONLY)|constants.O_NOFOLLOW);try{const st=await handle.stat({bigint:true});if(!st.isFile()||st.nlink!==1n||(st.mode&0o077n)||String(st.dev)!==binding.journalDevice||String(st.ino)!==binding.journalInode)throw Error("measured order binding changed");return handle;}catch(error){await handle.close();throw error;}}
|
|
51
|
+
async function linked(binding:MeasuredOrderBinding){const st=await lstat(paths(binding.directory).journal,{bigint:true});if(!st.isFile()||st.nlink!==1n||String(st.dev)!==binding.journalDevice||String(st.ino)!==binding.journalInode||(await realpath(paths(binding.directory).journal))!==paths(binding.directory).journal)throw Error("measured order binding changed");}
|
|
52
|
+
function parseJournal(text:string):JournalEvent[]{if(text&&!text.endsWith("\n"))throw Error("incomplete measured order journal");const rows=text?text.trimEnd().split("\n").map(line=>{const row=JSON.parse(line) as JournalEvent;if(intentKey(row)!==line)throw Error("noncanonical measured order journal");return row;}):[];let prior:string|null=null;for(let i=0;i<rows.length;i++){const row=rows[i],copy={...row} as any,claimed=copy.digest;delete copy.digest;if(row.sequence!==i||row.previousDigest!==prior||claimed!==hash(copy))throw Error("invalid measured order journal");prior=claimed;}return rows;}
|
|
53
|
+
async function appendJournal(binding:MeasuredOrderBinding,event:Omit<JournalEvent,"sequence"|"previousDigest"|"digest">){const path=paths(binding.directory).journal;return withFileLock(path,"measured order",async()=>{const handle=await pinnedJournal(binding,true);try{const rows=parseJournal(await handle.readFile("utf8")),base={...event,sequence:rows.length,previousDigest:rows.at(-1)?.digest??null} as any,record={...base,digest:hash(base)} as JournalEvent;await linked(binding);await handle.writeFile(intentKey(record)+"\n");await handle.sync();return record;}finally{await handle.close();}});}
|
|
54
|
+
function project(order:MeasuredOrder,events:JournalEvent[],currentOwner:string):MeasuredOrderNodeView[]{const rows=new Map<string,MeasuredOrderNodeView>();const evaluate=(node:MeasuredOrderNode):MeasuredOrderNodeView=>{const prior=rows.get(node.nodeId);if(prior)return prior;const row:MeasuredOrderNodeView={nodeId:node.nodeId,state:"exhausted",action:"none",attemptId:null,outputSha256:null,unknownClaim:null};if(node.dependencies.some(id=>evaluate(order.nodes.find(n=>n.nodeId===id)!).state!=="satisfied")){row.state="dependency-blocked";row.action="wait";rows.set(node.nodeId,row);return row;}
|
|
55
|
+
for(const attempt of node.attempts){const claim=events.find(e=>e.type==="claim"&&e.attemptId===attempt.attemptId) as Extract<JournalEvent,{type:"claim"}>|undefined;if(!claim){row.state="eligible";row.action="dispatch";row.attemptId=attempt.attemptId;break;}const result=events.find(e=>e.type==="result"&&e.attemptId===attempt.attemptId) as Extract<JournalEvent,{type:"result"}>|undefined;const acknowledged=events.some(e=>e.type==="acknowledge-unknown"&&e.request.attemptId===attempt.attemptId&&e.request.claimDigest===claim.digest&&e.request.claimOwner===claim.owner);if(!result){if(claim.owner!==currentOwner){if(acknowledged)continue;row.state="unknown";row.action="acknowledge";row.unknownClaim={owner:claim.owner,digest:claim.digest};}else{row.state="running";row.action="wait";}row.attemptId=attempt.attemptId;break;}if(result.state==="unknown"){if(!acknowledged){row.state="unknown";row.action="acknowledge";row.attemptId=attempt.attemptId;row.unknownClaim={owner:claim.owner,digest:claim.digest};break;}continue;}if(result.state==="completed"){row.state="satisfied";row.outputSha256=result.outputSha256;row.attemptId=attempt.attemptId;break;}}
|
|
56
|
+
rows.set(node.nodeId,row);return row;};return order.nodes.map(evaluate);}
|
|
57
|
+
export function openMeasuredOrder(input:MeasuredOrderBinding,hostAuthority:MeasuredOrderAuthority|null,host:MeasuredSessionHost){const binding=checkedBinding(input),p=paths(binding.directory),owner=randomUUID(),authorized=()=>hostAuthority?.authorityDigest===binding.order.authorityDigest&&hostAuthority.orderDigests.includes(measuredOrderDigest(binding.order));
|
|
58
|
+
const read=async()=>{await privateDirectory(binding.directory);const directoryStat=await lstat(binding.directory,{bigint:true});if(String(directoryStat.dev)!==binding.directoryDevice||String(directoryStat.ino)!==binding.directoryInode||(await readFile(p.binding,"utf8")).trimEnd()!==intentKey(binding))throw Error("measured order binding changed");await linked(binding);const handle=await pinnedJournal(binding);try{return parseJournal(await handle.readFile("utf8"));}finally{await handle.close();}};
|
|
59
|
+
const inspect=async()=>{const events=await read(),accounting=await openResourceBudget(binding.budget).inspect(),diagnostics=events.filter((e):e is Extract<JournalEvent,{type:"result"}>=>e.type==="result"&&e.state!=="completed").map(e=>`${e.attemptId}:${e.state}`);return freezeWork({version:"measured-order-view-v1",orderId:binding.order.orderId,nodes:project(binding.order,events,owner),resources:accounting,diagnostics,acceptance:"not-assessed",freshness:"snapshot-unknown" as const});};
|
|
60
|
+
return Object.freeze({inspect,async advance(){if(!authorized())throw Error("independent exact order authority required");await read();let selected:MeasuredOrderAttempt|undefined,claim:Extract<JournalEvent,{type:"claim"}>|undefined;
|
|
61
|
+
await withFileLock(p.journal,"measured order advance",async()=>{const handle=await pinnedJournal(binding,true);try{const events=parseJournal(await handle.readFile("utf8")),eligible=project(binding.order,events,owner).find(n=>n.state==="eligible");if(!eligible)return;selected=binding.order.nodes.flatMap(n=>n.attempts).find(a=>a.attemptId===eligible.attemptId)!;const base={type:"claim",sequence:events.length,previousDigest:events.at(-1)?.digest??null,attemptId:selected.attemptId,owner} as const;claim={...base,digest:hash(base)};await linked(binding);await handle.writeFile(intentKey(claim)+"\n");await handle.sync();}finally{await handle.close();}});
|
|
62
|
+
if(!selected||!claim)return inspect();const node=binding.order.nodes.find(n=>n.attempts.some(a=>a.attemptId===selected!.attemptId))!,index=node.attempts.findIndex(a=>a.attemptId===selected!.attemptId),controller=new AbortController();let state:Extract<JournalEvent,{type:"result"}>["state"]="failed",result:unknown=null,outputSha256:string|null=null;
|
|
63
|
+
try{const accounting=await openResourceBudget(binding.budget).inspect(),parent=[...node.attempts.slice(0,index)].reverse().find(a=>accounting.reservations.some(r=>r.attemptId===a.attemptId))??null,events=await read();let prompt=selected.prompt;if(selected.includeDependencies)for(const dependency of node.dependencies){const dependencyNode=binding.order.nodes.find(n=>n.nodeId===dependency)!,prior=dependencyNode.attempts.map(a=>events.find(e=>e.type==="result"&&e.attemptId===a.attemptId)).find((e):e is Extract<JournalEvent,{type:"result"}>=>e?.type==="result"&&e.state==="completed"),output=(prior?.result as {output?:unknown})?.output;if(!prior||typeof output!=="string"||hash(output)!==prior.outputSha256)throw Error("sealed dependency output unavailable");prompt+=`\n\n<sealed-dependency node=${JSON.stringify(dependency)} sha256=${JSON.stringify(prior.outputSha256)}>\n${output}\n</sealed-dependency>`;}
|
|
64
|
+
result=await runMeasuredAgentSession({budget:binding.budget,attempt:{attemptId:selected.attemptId,orderId:binding.order.orderId,experimentId:`${binding.order.orderId}:${node.nodeId}`,kind:parent?"retry":"primary",parentAttemptId:parent?.attemptId??null},model:selected.model,thinkingLevel:selected.thinkingLevel,prompt,deadlineMs:selected.deadlineMs,terminationGraceMs:selected.terminationGraceMs,maxRetainedOutputBytes:selected.maxRetainedOutputBytes,maxOutputTokens:selected.maxOutputTokens,signal:controller.signal},host);const qualified=qualifyMeasuredOutput((result as {output:string}).output,selected.qualification);outputSha256=qualified.outputSha256;state=qualified.eligible?"completed":"mismatch";}catch(error){result=error instanceof MeasuredSessionFailure?error.evidence:{errorType:error instanceof Error?error.name:"unknown",errorCode:typeof error==="object"&&error&&"code" in error?String(error.code):null,message:error instanceof Error?error.message:"unknown"};state=error instanceof MeasuredSessionUnknownError?"unknown":error instanceof MeasuredSessionFailure&&(error.evidence as {state?:string})?.state==="cancelled"?"cancelled":"failed";}
|
|
65
|
+
await appendJournal(binding,{type:"result",attemptId:selected.attemptId,owner:claim.owner,state,outputSha256,result} as Omit<JournalEvent,"sequence"|"previousDigest"|"digest">);return inspect();},
|
|
66
|
+
async acknowledgeUnknown(request:MeasuredOrderAcknowledgement,authority:MeasuredOrderAuthority|null){const digest=measuredOrderAcknowledgementDigest(request);if(authority?.authorityDigest!==binding.order.authorityDigest||!authority.acknowledgementDigests.includes(digest)||request.bindingDigest!==binding.digest)throw Error("independent exact acknowledgement authority required");await read();await withFileLock(p.journal,"measured order acknowledgement",async()=>{const handle=await pinnedJournal(binding,true);try{const events=parseJournal(await handle.readFile("utf8")),claim=events.find(e=>e.type==="claim"&&e.attemptId===request.attemptId) as Extract<JournalEvent,{type:"claim"}>|undefined,result=events.find(e=>e.type==="result"&&e.attemptId===request.attemptId) as Extract<JournalEvent,{type:"result"}>|undefined;if(!claim||claim.owner!==request.claimOwner||claim.digest!==request.claimDigest||!(result?.state==="unknown"||!result&&claim.owner!==owner)||events.some(e=>e.type==="acknowledge-unknown"&&e.request.attemptId===request.attemptId))throw Error("exact current unknown claim required");const base={type:"acknowledge-unknown",sequence:events.length,previousDigest:events.at(-1)!.digest,request} as const,record={...base,digest:hash(base)};await linked(binding);await handle.writeFile(intentKey(record)+"\n");await handle.sync();}finally{await handle.close();}});return inspect();}});
|
|
67
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { mkdir, mkdtemp, rm } from 'node:fs/promises';
|
|
3
|
+
import { join, resolve } from 'node:path';
|
|
4
|
+
import { createAgentSession, DefaultResourceLoader, ModelRuntime, SessionManager, SettingsManager } from '@earendil-works/pi-coding-agent';
|
|
5
|
+
import { openResourceBudget, type AttemptDemand, type GovernedBudgetBinding } from './resource-budget.ts';
|
|
6
|
+
import { freezeWork } from './work-ledger-json.ts';
|
|
7
|
+
|
|
8
|
+
const MODELS=['openai-codex/gpt-5.6-sol','openai-codex/gpt-5.6-terra'] as const;
|
|
9
|
+
const THINKING=['off','minimal','low','medium','high','xhigh','max'] as const;
|
|
10
|
+
export interface ProviderUsage {input:number;output:number;cacheRead:number;cacheWrite:number;reasoning:number;totalTokens:number;cost:{input:number;output:number;cacheRead:number;cacheWrite:number;total:number}}
|
|
11
|
+
export interface MeasuredSessionHostInput {model:typeof MODELS[number];thinkingLevel:typeof THINKING[number];prompt:string;deadlineMs:number;maxOutputTokens:number;tools:readonly [];discovery:'factory-only';signal:AbortSignal}
|
|
12
|
+
export interface MeasuredSessionHostResult {output:string;usage:ProviderUsage;provider:'openai-codex';model:'gpt-5.6-sol'|'gpt-5.6-terra';stopReason:string}
|
|
13
|
+
export interface MeasuredSessionHost {run(input:MeasuredSessionHostInput):Promise<MeasuredSessionHostResult>}
|
|
14
|
+
export interface MeasuredSessionInput {budget:GovernedBudgetBinding;attempt:Omit<AttemptDemand,'inputBytes'|'inputDigest'>;model:typeof MODELS[number];thinkingLevel:typeof THINKING[number];prompt:string;deadlineMs:number;terminationGraceMs:number;maxRetainedOutputBytes:number;maxOutputTokens:number;signal?:AbortSignal}
|
|
15
|
+
export class MeasuredSessionFailure extends Error {readonly evidence:unknown;constructor(message:string,evidence:unknown){super(message);this.name='MeasuredSessionFailure';this.evidence=evidence;}}
|
|
16
|
+
export class MeasuredSessionUnknownError extends Error {constructor(){super('measured session did not settle after owned abort; reservation remains active');this.name='MeasuredSessionUnknownError';}}
|
|
17
|
+
const finite=(value:unknown)=>typeof value==='number'&&Number.isFinite(value)&&value>=0;
|
|
18
|
+
function checkedUsage(value:ProviderUsage):ProviderUsage {
|
|
19
|
+
if(!value||typeof value!=='object'||Object.keys(value).sort().join()!=='cacheRead,cacheWrite,cost,input,output,reasoning,totalTokens'||![value.input,value.output,value.cacheRead,value.cacheWrite,value.reasoning,value.totalTokens].every(finite)||!Number.isSafeInteger(value.totalTokens)||!value.cost||Object.keys(value.cost).sort().join()!=='cacheRead,cacheWrite,input,output,total'||!Object.values(value.cost).every(finite))throw Error('provider usage unavailable or malformed');
|
|
20
|
+
return JSON.parse(JSON.stringify(value));
|
|
21
|
+
}
|
|
22
|
+
function checked(input:MeasuredSessionInput):MeasuredSessionInput {
|
|
23
|
+
if(!input||typeof input!=='object'||!MODELS.includes(input.model)||!THINKING.includes(input.thinkingLevel))throw Error('measured sessions require exact Codex Sol or Terra');
|
|
24
|
+
if(typeof input.prompt!=='string'||!input.prompt.length||Buffer.byteLength(input.prompt)>65536||!Number.isSafeInteger(input.deadlineMs)||input.deadlineMs<1000||input.deadlineMs>300000||!Number.isSafeInteger(input.terminationGraceMs)||input.terminationGraceMs<100||input.terminationGraceMs>10000||!Number.isSafeInteger(input.maxRetainedOutputBytes)||input.maxRetainedOutputBytes<1||input.maxRetainedOutputBytes>1024*1024||!Number.isSafeInteger(input.maxOutputTokens)||input.maxOutputTokens<1||input.maxOutputTokens>16384)throw Error('invalid measured session bounds');
|
|
25
|
+
if(input.signal?.aborted)throw new MeasuredSessionFailure('measured session cancelled before reservation',{state:'cancelled-before-reservation',reason:'caller-signal'});
|
|
26
|
+
return input;
|
|
27
|
+
}
|
|
28
|
+
/** Opt-in one-prompt route. Existing resource budgets own attempts; byte retention is checked after generation. */
|
|
29
|
+
export async function runMeasuredAgentSession(raw:MeasuredSessionInput,host:MeasuredSessionHost){
|
|
30
|
+
const input=checked(raw),bytes=Buffer.from(input.prompt),budget=openResourceBudget(input.budget),permit=await budget.reserve({...input.attempt,inputBytes:bytes.length,inputDigest:createHash('sha256').update(bytes).digest('hex')});
|
|
31
|
+
const controller=new AbortController(),abort=()=>controller.abort(input.signal?.reason??'operator-cancellation');input.signal?.addEventListener('abort',abort,{once:true});if(input.signal?.aborted)abort();
|
|
32
|
+
const timer=setTimeout(()=>controller.abort('deadline'),input.deadlineMs);let outcome:'completed'|'failed'|'cancelled'|null='failed';
|
|
33
|
+
try{
|
|
34
|
+
const unknown=new Promise<never>((_,reject)=>controller.signal.addEventListener('abort',()=>setTimeout(()=>reject(new MeasuredSessionUnknownError()),input.terminationGraceMs),{once:true}));
|
|
35
|
+
if(controller.signal.aborted)throw new MeasuredSessionFailure('measured session cancelled after reservation',{state:'cancelled-before-host',reason:'caller-signal'});
|
|
36
|
+
const running=host.run({model:input.model,thinkingLevel:input.thinkingLevel,prompt:input.prompt,deadlineMs:input.deadlineMs,maxOutputTokens:input.maxOutputTokens,tools:[],discovery:'factory-only',signal:controller.signal});
|
|
37
|
+
const result=await Promise.race([running,unknown]);
|
|
38
|
+
const outputBytes=Buffer.from(result.output),outputEvidence={sha256:createHash('sha256').update(outputBytes).digest('hex'),bytes:outputBytes.length,retainedPrefix:outputBytes.subarray(0,input.maxRetainedOutputBytes).toString('utf8')};
|
|
39
|
+
let usage:ProviderUsage;try{usage=checkedUsage(result.usage);}catch(error){throw new MeasuredSessionFailure(String(error),{state:'failed',model:result.model,stopReason:result.stopReason,output:outputEvidence});}
|
|
40
|
+
if(result.provider!=='openai-codex'||`${result.provider}/${result.model}`!==input.model)throw new MeasuredSessionFailure('measured model identity mismatch',{state:'failed',observedProvider:result.provider,observedModel:result.model,stopReason:result.stopReason,output:outputEvidence,usage});
|
|
41
|
+
if(outputEvidence.bytes>input.maxRetainedOutputBytes)throw new MeasuredSessionFailure('measured retained-output bound exceeded',{state:'failed',model:input.model,stopReason:result.stopReason,output:outputEvidence,usage});
|
|
42
|
+
if(controller.signal.aborted||result.stopReason!=='stop')throw new MeasuredSessionFailure('measured assistant did not stop cleanly',{state:controller.signal.aborted?'cancelled':'failed',model:input.model,stopReason:result.stopReason,output:outputEvidence,usage});
|
|
43
|
+
outcome='completed';return freezeWork({version:'measured-session-result-v1',state:'completed',model:input.model,thinkingLevel:input.thinkingLevel,output:result.output,usage,stopReason:result.stopReason,acceptance:'not-assessed',grantExpansion:false});
|
|
44
|
+
}catch(error){
|
|
45
|
+
if(error instanceof MeasuredSessionUnknownError){outcome=null;throw error;}
|
|
46
|
+
if(controller.signal.aborted)outcome='cancelled';
|
|
47
|
+
if(error instanceof MeasuredSessionFailure)throw error;
|
|
48
|
+
throw new MeasuredSessionFailure('measured host failed',{state:controller.signal.aborted?'cancelled':'failed',errorType:error instanceof Error?error.name:'unknown'});
|
|
49
|
+
}
|
|
50
|
+
finally{clearTimeout(timer);input.signal?.removeEventListener('abort',abort);if(outcome)await permit.settle(outcome);}
|
|
51
|
+
}
|
|
52
|
+
export interface MeasuredAuthRuntime {isUsingOAuth(provider:string):boolean;isUsingSubscription(provider:string):boolean;checkAuth(provider:string,options?:{signal?:AbortSignal}):Promise<{type:string}|undefined>}
|
|
53
|
+
export async function verifyMeasuredSubscription(runtime:MeasuredAuthRuntime,signal?:AbortSignal){
|
|
54
|
+
signal?.throwIfAborted();const auth=await runtime.checkAuth('openai-codex',{signal});signal?.throwIfAborted();
|
|
55
|
+
if(auth?.type!=='oauth'||!runtime.isUsingOAuth('openai-codex')||!runtime.isUsingSubscription('openai-codex'))throw Error('exact OpenAI Codex OAuth subscription authorization required');
|
|
56
|
+
}
|
|
57
|
+
/** Concrete SDK host: in-memory session/settings, empty discovery roots, no tools, one factory-named inline extension. */
|
|
58
|
+
export function piSdkMeasuredSessionHost():MeasuredSessionHost{return{async run(input){
|
|
59
|
+
const root=await mkdtemp(join(resolve(process.env.TMPDIR??'/tmp'),'pi-daddy-measured-'));let session:Awaited<ReturnType<typeof createAgentSession>>['session']|undefined;
|
|
60
|
+
try{
|
|
61
|
+
const agentDir=join(root,'agent');await mkdir(agentDir,{mode:0o700});const settings=SettingsManager.inMemory({compaction:{enabled:false},retry:{enabled:false,maxRetries:0}});
|
|
62
|
+
const loader=new DefaultResourceLoader({cwd:root,agentDir,settingsManager:settings,systemPromptOverride:()=> 'Follow the supplied bounded task. Do not use tools or claim acceptance.',agentsFilesOverride:()=>({agentsFiles:[],diagnostics:[]}),skillsOverride:()=>({skills:[],diagnostics:[]}),promptsOverride:()=>({prompts:[],diagnostics:[]}),extensionFactories:[{name:'pi-daddy-measured-session',factory:()=>{}}]});await loader.reload();
|
|
63
|
+
input.signal.throwIfAborted();const runtime=await ModelRuntime.create({signal:input.signal});input.signal.throwIfAborted();await verifyMeasuredSubscription(runtime,input.signal);input.signal.throwIfAborted();
|
|
64
|
+
const [provider,modelId]=input.model.split('/'),found=runtime.getModel(provider,modelId);if(!found)throw Error('requested measured model unavailable');
|
|
65
|
+
const created=await createAgentSession({cwd:root,agentDir,model:{...found,maxTokens:input.maxOutputTokens},thinkingLevel:input.thinkingLevel,modelRuntime:runtime,resourceLoader:loader,sessionManager:SessionManager.inMemory(root),settingsManager:settings,noTools:'all'});session=created.session;if(created.extensionsResult.errors.length)throw Error('factory-only extension load failed');
|
|
66
|
+
const abort=()=>void session!.abort();input.signal.addEventListener('abort',abort,{once:true});try{if(input.signal.aborted){await session.abort();throw input.signal.reason;}await session.prompt(input.prompt,{expandPromptTemplates:false});}finally{input.signal.removeEventListener('abort',abort);}
|
|
67
|
+
const message=[...session.messages].reverse().find(m=>m.role==='assistant');if(!message)throw Error('measured assistant result unavailable');
|
|
68
|
+
return {output:message.content.filter(c=>c.type==='text').map(c=>c.text).join(''),usage:message.usage as ProviderUsage,provider:message.provider as MeasuredSessionHostResult['provider'],model:message.model as MeasuredSessionHostResult['model'],stopReason:message.stopReason};
|
|
69
|
+
}finally{session?.dispose();await rm(root,{recursive:true,force:true});}
|
|
70
|
+
}};}
|
package/src/run-herdr.ts
CHANGED
|
@@ -27,9 +27,11 @@ import type { ChildRunResult } from "./run-child.ts";
|
|
|
27
27
|
import { DEFAULT_MAX_OUTPUT_BYTES, DEFAULT_TIMEOUT_MS } from "./run-child.ts";
|
|
28
28
|
import { MAX_OPEN_PANES, markPaneSettled, trackPane, trimOpenPanes, untrackPane } from "./pane-reaper.ts";
|
|
29
29
|
import { defaultExec, parseReply, type HerdrExec } from "./herdr-cli.ts";
|
|
30
|
-
import { readPane,
|
|
30
|
+
import { FRESH_LIFECYCLE_TIMEOUT_MS, readPane, waitForLifecycleBaseline, waitForSettled, observeHerdrSession, type PollTarget } from "./herdr-poll.ts";
|
|
31
31
|
import { stageSystemPrompt } from "./herdr-stage.ts";
|
|
32
32
|
import { uniqueAgentName } from "./herdr-name.ts";
|
|
33
|
+
import { bundledHerdrPiLifecycleExtension } from "./herdr-pi-lifecycle.ts";
|
|
34
|
+
import { startHerdrAgent } from "./herdr-start.ts";
|
|
33
35
|
|
|
34
36
|
/**
|
|
35
37
|
* Re-exported so importers of the executor still reach the protocol at the name they always used.
|
|
@@ -42,13 +44,15 @@ export { type HerdrExec, parseReply } from "./herdr-cli.ts";
|
|
|
42
44
|
export { splitSystemPrompt, stageSystemPrompt } from "./herdr-stage.ts";
|
|
43
45
|
/** Re-exported: the name rules moved to `./herdr-name.ts` under the ceiling; tests import them here. */
|
|
44
46
|
export { uniqueAgentName } from "./herdr-name.ts";
|
|
47
|
+
/** Re-exported after startup retry extraction; existing executor importers keep this public constant. */
|
|
48
|
+
export { PANE_READY_POLL_MS } from "./herdr-start.ts";
|
|
45
49
|
/**
|
|
46
50
|
* Re-exported so `test/run-herdr.test.ts` and any importer keep reaching these where they always were.
|
|
47
51
|
*
|
|
48
52
|
* `POLL_INTERVAL_MS` and `newSuffix` moved to `./herdr-poll.ts` under the 400-line ceiling; the names are part
|
|
49
53
|
* of this module's surface and moving a file should not move an export.
|
|
50
54
|
*/
|
|
51
|
-
export { DEFAULT_SNAPSHOT_LINES, POLL_INTERVAL_MS, readPane, tailLines, waitForSettled, type PollTarget } from "./herdr-poll.ts";
|
|
55
|
+
export { DEFAULT_SNAPSHOT_LINES, POLL_INTERVAL_MS, readPane, tailLines, waitForLifecycleBaseline, waitForSettled, type PollTarget } from "./herdr-poll.ts";
|
|
52
56
|
|
|
53
57
|
export interface HerdrRunRequest {
|
|
54
58
|
/** `planSpawn` args **without** the prompt — see `prompt`. */
|
|
@@ -122,10 +126,6 @@ export class HerdrWriterCloseError extends Error {
|
|
|
122
126
|
}
|
|
123
127
|
}
|
|
124
128
|
|
|
125
|
-
/** How often to retry `agent start` while a freshly created pane is still reaching its shell prompt. */
|
|
126
|
-
export const PANE_READY_POLL_MS = 300;
|
|
127
|
-
|
|
128
|
-
|
|
129
129
|
/**
|
|
130
130
|
* Run one governed child in a pane and return its output.
|
|
131
131
|
*
|
|
@@ -162,7 +162,16 @@ export async function runHerdrPane(request: HerdrRunRequest): Promise<ChildRunRe
|
|
|
162
162
|
const staged = await stageSystemPrompt(request.args);
|
|
163
163
|
if (staged.error) return { ...empty, spawnError: staged.error };
|
|
164
164
|
const promptDir = staged.promptDir;
|
|
165
|
-
|
|
165
|
+
let lifecycleExtension: string;
|
|
166
|
+
try {
|
|
167
|
+
lifecycleExtension = bundledHerdrPiLifecycleExtension();
|
|
168
|
+
} catch (error) {
|
|
169
|
+
if (promptDir) await rm(promptDir, { recursive: true, force: true }).catch(() => undefined);
|
|
170
|
+
return { ...empty, spawnError: String(error) };
|
|
171
|
+
}
|
|
172
|
+
// Pi's `--no-extensions` disables discovery, not this explicit, package-pinned lifecycle reporter. It
|
|
173
|
+
// registers no tools; pi's existing `--tools` allowlist remains the governed child surface.
|
|
174
|
+
const effectiveArgs = [...staged.args, "-e", lifecycleExtension];
|
|
166
175
|
|
|
167
176
|
const create = ["tab", "create", "--label", request.name, "--cwd", request.cwd];
|
|
168
177
|
if (request.workspace) create.push("--workspace", request.workspace);
|
|
@@ -301,11 +310,19 @@ export async function runHerdrPane(request: HerdrRunRequest): Promise<ChildRunRe
|
|
|
301
310
|
let settledCleanly = false;
|
|
302
311
|
|
|
303
312
|
return runWithFinalizers(async () => {
|
|
304
|
-
const started = await
|
|
313
|
+
const started = await startHerdrAgent(exec, agentName, paneId, effectiveArgs, deadline);
|
|
305
314
|
if (started.error) return { ...empty, spawnError: `herdr agent start failed: ${started.error}` };
|
|
306
315
|
|
|
307
|
-
// The
|
|
308
|
-
|
|
316
|
+
// The bundled reporter queues its initial idle state asynchronously. Establish its native-authority
|
|
317
|
+
// baseline before prompting, otherwise that initial report could advance the counter after prompt and
|
|
318
|
+
// be mistaken for settlement.
|
|
319
|
+
const baseline = await waitForLifecycleBaseline(
|
|
320
|
+
exec,
|
|
321
|
+
{ ...request, name: agentName, nativePaneId: paneId },
|
|
322
|
+
Math.min(deadline, Date.now() + FRESH_LIFECYCLE_TIMEOUT_MS),
|
|
323
|
+
);
|
|
324
|
+
if (baseline.aborted) return { ...empty, aborted: true };
|
|
325
|
+
if (baseline.spawnError || baseline.before === undefined) return { ...empty, spawnError: baseline.spawnError ?? "Herdr lifecycle baseline is unavailable" };
|
|
309
326
|
observeHerdrSession(started.result?.agent, paneId, request.onSessionReference);
|
|
310
327
|
|
|
311
328
|
const prompted = parseReply(await exec(["agent", "prompt", agentName, request.prompt]));
|
|
@@ -318,7 +335,7 @@ export async function runHerdrPane(request: HerdrRunRequest): Promise<ChildRunRe
|
|
|
318
335
|
}
|
|
319
336
|
}
|
|
320
337
|
|
|
321
|
-
const settled = await waitForSettled(exec, { ...request, name: agentName, nativePaneId: paneId }, before, deadline, maxOutputBytes);
|
|
338
|
+
const settled = await waitForSettled(exec, { ...request, name: agentName, nativePaneId: paneId }, baseline.before, deadline, maxOutputBytes);
|
|
322
339
|
if (settled.aborted || settled.timedOut) {
|
|
323
340
|
// Still read: a timed-out child usually produced something, and a partial answer labelled partial is
|
|
324
341
|
// more useful than none. R-03's rule — a missing result must never look like an empty one.
|
|
@@ -367,32 +384,3 @@ export async function runHerdrPane(request: HerdrRunRequest): Promise<ChildRunRe
|
|
|
367
384
|
run: () => cleanup(settledCleanly),
|
|
368
385
|
}]);
|
|
369
386
|
}
|
|
370
|
-
|
|
371
|
-
/**
|
|
372
|
-
* Start the agent, retrying while the pane is still coming up.
|
|
373
|
-
*
|
|
374
|
-
* **Measured, and only visible once automated.** A pane created by `tab create` is not immediately at a
|
|
375
|
-
* shell prompt, and `herdr agent start` requires one — it fails with
|
|
376
|
-
* `agent_pane_busy: … is not an available shell`. Driving the two commands by hand hid this completely,
|
|
377
|
-
* because the think-time between them was longer than the shell took to start; the first scripted run hit
|
|
378
|
-
* it every time.
|
|
379
|
-
*
|
|
380
|
-
* Retried rather than preceded by a fixed sleep: a sleep long enough for a loaded machine is wasted on
|
|
381
|
-
* every spawn, and a fan-out pays it per child. Only the busy condition is retried — any other error is a
|
|
382
|
-
* real failure and returns immediately.
|
|
383
|
-
*/
|
|
384
|
-
async function startAgent(
|
|
385
|
-
exec: HerdrExec,
|
|
386
|
-
name: string,
|
|
387
|
-
paneId: string,
|
|
388
|
-
args: string[],
|
|
389
|
-
deadline: number,
|
|
390
|
-
): Promise<{ result?: Record<string, unknown>; error?: string }> {
|
|
391
|
-
for (;;) {
|
|
392
|
-
const reply = parseReply(await exec(["agent", "start", name, "--kind", "pi", "--pane", paneId, "--", ...args]));
|
|
393
|
-
if (!reply.error) return reply;
|
|
394
|
-
const busy = /not an available shell|agent_pane_busy/.test(reply.error);
|
|
395
|
-
if (!busy || Date.now() >= deadline) return reply;
|
|
396
|
-
await new Promise((r) => setTimeout(r, PANE_READY_POLL_MS));
|
|
397
|
-
}
|
|
398
|
-
}
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
// installed by herdr
|
|
2
|
+
// managed by herdr; reinstalling or updating the integration overwrites this file.
|
|
3
|
+
// add custom hooks/plugins beside this file instead of editing it.
|
|
4
|
+
// HERDR_INTEGRATION_ID=pi
|
|
5
|
+
// HERDR_INTEGRATION_VERSION=8
|
|
6
|
+
// @ts-nocheck
|
|
7
|
+
|
|
8
|
+
import net from "node:net";
|
|
9
|
+
|
|
10
|
+
const HERDR_ENV = process.env.HERDR_ENV;
|
|
11
|
+
const socketPath = process.env.HERDR_SOCKET_PATH;
|
|
12
|
+
const socketEndpoint =
|
|
13
|
+
process.platform === "win32" && socketPath ? `\\\\.\\pipe\\${socketPath}` : socketPath;
|
|
14
|
+
const paneId = process.env.HERDR_PANE_ID;
|
|
15
|
+
const source = "herdr:pi";
|
|
16
|
+
|
|
17
|
+
function enabled() {
|
|
18
|
+
return HERDR_ENV === "1" && !!socketPath && !!paneId;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function sendRequestAttempt(request: unknown, timeoutMs: number): Promise<boolean> {
|
|
22
|
+
if (!enabled()) {
|
|
23
|
+
return Promise.resolve(true);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return new Promise((resolve) => {
|
|
27
|
+
let done = false;
|
|
28
|
+
let timeout: ReturnType<typeof setTimeout> | undefined;
|
|
29
|
+
const finish = (delivered: boolean) => {
|
|
30
|
+
if (done) return;
|
|
31
|
+
done = true;
|
|
32
|
+
if (timeout) {
|
|
33
|
+
clearTimeout(timeout);
|
|
34
|
+
}
|
|
35
|
+
socket.destroy();
|
|
36
|
+
resolve(delivered);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const socket = net.createConnection(socketEndpoint!);
|
|
40
|
+
socket.on("error", () => finish(false));
|
|
41
|
+
socket.on("connect", () => socket.write(`${JSON.stringify(request)}\n`));
|
|
42
|
+
socket.on("data", () => finish(true));
|
|
43
|
+
socket.on("end", () => finish(false));
|
|
44
|
+
timeout = setTimeout(() => finish(false), timeoutMs);
|
|
45
|
+
timeout.unref?.();
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function sendRequest(request: unknown): Promise<void> {
|
|
50
|
+
if (await sendRequestAttempt(request, 500)) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
await sendRequestAttempt(request, 1500);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
type AgentState = "working" | "blocked" | "idle";
|
|
57
|
+
|
|
58
|
+
type QueuedState = {
|
|
59
|
+
state: AgentState;
|
|
60
|
+
message?: string;
|
|
61
|
+
seq: number;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
let reportSeq = Date.now() * 1000;
|
|
65
|
+
let currentAgentSessionId: string | undefined;
|
|
66
|
+
let currentAgentSessionPath: string | undefined;
|
|
67
|
+
|
|
68
|
+
function nextReportSeq(): number {
|
|
69
|
+
reportSeq += 1;
|
|
70
|
+
return reportSeq;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function updateSessionRef(ctx: any): void {
|
|
74
|
+
try {
|
|
75
|
+
const file = ctx?.sessionManager?.getSessionFile?.();
|
|
76
|
+
currentAgentSessionPath =
|
|
77
|
+
typeof file === "string" && file.startsWith("/") ? file : undefined;
|
|
78
|
+
} catch {
|
|
79
|
+
currentAgentSessionPath = undefined;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
try {
|
|
83
|
+
const id = ctx?.sessionManager?.getSessionId?.();
|
|
84
|
+
currentAgentSessionId = typeof id === "string" && id.length > 0 ? id : undefined;
|
|
85
|
+
} catch {
|
|
86
|
+
currentAgentSessionId = undefined;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function withSessionRef(params: Record<string, unknown>): Record<string, unknown> {
|
|
91
|
+
if (currentAgentSessionPath) {
|
|
92
|
+
return { ...params, agent_session_path: currentAgentSessionPath };
|
|
93
|
+
}
|
|
94
|
+
if (currentAgentSessionId) {
|
|
95
|
+
return { ...params, agent_session_id: currentAgentSessionId };
|
|
96
|
+
}
|
|
97
|
+
return params;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function currentSessionRef(): Record<string, unknown> | undefined {
|
|
101
|
+
if (currentAgentSessionPath) {
|
|
102
|
+
return { agent_session_path: currentAgentSessionPath };
|
|
103
|
+
}
|
|
104
|
+
if (currentAgentSessionId) {
|
|
105
|
+
return { agent_session_id: currentAgentSessionId };
|
|
106
|
+
}
|
|
107
|
+
return undefined;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function reportSession(sessionStartSource?: string): Promise<void> {
|
|
111
|
+
const sessionRef = currentSessionRef();
|
|
112
|
+
if (!sessionRef) {
|
|
113
|
+
return Promise.resolve();
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return sendRequest({
|
|
117
|
+
id: `${source}:session:${Date.now()}:${Math.random().toString(36).slice(2)}`,
|
|
118
|
+
method: "pane.report_agent_session",
|
|
119
|
+
params: {
|
|
120
|
+
pane_id: paneId,
|
|
121
|
+
source,
|
|
122
|
+
agent: "pi",
|
|
123
|
+
seq: nextReportSeq(),
|
|
124
|
+
session_start_source: sessionStartSource,
|
|
125
|
+
...sessionRef,
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function sendState(state: AgentState, message?: string, seq = nextReportSeq()): Promise<void> {
|
|
131
|
+
return sendRequest({
|
|
132
|
+
id: `${source}:${Date.now()}:${Math.random().toString(36).slice(2)}`,
|
|
133
|
+
method: "pane.report_agent",
|
|
134
|
+
params: withSessionRef({
|
|
135
|
+
pane_id: paneId,
|
|
136
|
+
source,
|
|
137
|
+
agent: "pi",
|
|
138
|
+
state,
|
|
139
|
+
message,
|
|
140
|
+
seq,
|
|
141
|
+
}),
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
let sendInFlight = false;
|
|
146
|
+
let queuedState: QueuedState | undefined;
|
|
147
|
+
|
|
148
|
+
function queueState(state: AgentState, message?: string): void {
|
|
149
|
+
queuedState = { state, message, seq: nextReportSeq() };
|
|
150
|
+
if (!sendInFlight) {
|
|
151
|
+
void drainStateQueue();
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
async function drainStateQueue(): Promise<void> {
|
|
156
|
+
if (sendInFlight) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
sendInFlight = true;
|
|
161
|
+
try {
|
|
162
|
+
while (queuedState) {
|
|
163
|
+
const next = queuedState;
|
|
164
|
+
queuedState = undefined;
|
|
165
|
+
await sendState(next.state, next.message, next.seq);
|
|
166
|
+
}
|
|
167
|
+
} finally {
|
|
168
|
+
sendInFlight = false;
|
|
169
|
+
if (queuedState) {
|
|
170
|
+
void drainStateQueue();
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export default function (pi) {
|
|
176
|
+
if (!enabled()) {
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
let agentActive = false;
|
|
181
|
+
let blockedCount = 0;
|
|
182
|
+
let blockedMessage: string | undefined;
|
|
183
|
+
let lastState: AgentState | undefined;
|
|
184
|
+
let lastMessage: string | undefined;
|
|
185
|
+
let rootSession = false;
|
|
186
|
+
|
|
187
|
+
function desiredState() {
|
|
188
|
+
if (blockedCount > 0) {
|
|
189
|
+
return { state: "blocked" as const, message: blockedMessage };
|
|
190
|
+
}
|
|
191
|
+
if (agentActive) {
|
|
192
|
+
return { state: "working" as const, message: undefined };
|
|
193
|
+
}
|
|
194
|
+
return { state: "idle" as const, message: undefined };
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function publishState(force = false) {
|
|
198
|
+
const next = desiredState();
|
|
199
|
+
if (!force && next.state === lastState && next.message === lastMessage) {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
lastState = next.state;
|
|
203
|
+
lastMessage = next.message;
|
|
204
|
+
queueState(next.state, next.message);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
pi.events.on("herdr:blocked", (data) => {
|
|
208
|
+
if (!rootSession) {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
if (!data?.active) {
|
|
212
|
+
blockedCount = Math.max(0, blockedCount - 1);
|
|
213
|
+
if (blockedCount === 0) {
|
|
214
|
+
blockedMessage = undefined;
|
|
215
|
+
}
|
|
216
|
+
publishState();
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
blockedCount += 1;
|
|
221
|
+
blockedMessage = data.label;
|
|
222
|
+
publishState();
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
pi.on("session_start", async (event, ctx) => {
|
|
226
|
+
// TUI only: RPC/JSON/print modes are headless (no PTY herdr can display),
|
|
227
|
+
// and RPC still reports hasUI=true, so mode is the reliable gate.
|
|
228
|
+
if (ctx?.mode !== "tui") {
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
rootSession = true;
|
|
232
|
+
updateSessionRef(ctx);
|
|
233
|
+
await reportSession(event?.reason);
|
|
234
|
+
// A reload can replace this extension mid-run without emitting another agent_start.
|
|
235
|
+
agentActive = ctx?.isIdle?.() === false;
|
|
236
|
+
publishState(true);
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
pi.on("agent_start", (_event, ctx) => {
|
|
240
|
+
if (!rootSession) {
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
updateSessionRef(ctx);
|
|
244
|
+
void reportSession();
|
|
245
|
+
agentActive = true;
|
|
246
|
+
publishState();
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
pi.on("agent_settled", (_event, ctx) => {
|
|
250
|
+
if (!rootSession || ctx?.isIdle?.() !== true) {
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
agentActive = false;
|
|
255
|
+
publishState();
|
|
256
|
+
});
|
|
257
|
+
}
|
package/src/work-command.ts
CHANGED
|
@@ -16,6 +16,7 @@ import {
|
|
|
16
16
|
type WorkRevision,
|
|
17
17
|
} from "./work-ledger.ts";
|
|
18
18
|
import { withFileLock } from "./file-lock.ts";
|
|
19
|
+
import { resolveWorkSnapshotText } from "./work-ledger-snapshot.ts";
|
|
19
20
|
|
|
20
21
|
const ID = /^[A-Za-z0-9][A-Za-z0-9._:/@+\-]{0,127}$/;
|
|
21
22
|
const DIGEST = /^[a-f0-9]{64}$/;
|
|
@@ -100,8 +101,8 @@ export async function declareWork(input: DeclareWorkInput): Promise<WorkFrozen<D
|
|
|
100
101
|
}
|
|
101
102
|
if (!ID.test(input.id) || typeof input.outcome !== "string" || !input.outcome.trim()) throw new TypeError("work declaration requires an identifier and non-empty outcome");
|
|
102
103
|
const resolved = paths(input), outcomeDigest = sha256(input.outcome.trim());
|
|
103
|
-
await mkdir(dirname(resolved.ledgerPath), { recursive: true });
|
|
104
|
-
await mkdir(dirname(resolved.statePath), { recursive: true });
|
|
104
|
+
await mkdir(dirname(resolved.ledgerPath), { recursive: true, mode: 0o700 });
|
|
105
|
+
await mkdir(dirname(resolved.statePath), { recursive: true, mode: 0o700 });
|
|
105
106
|
return withFileLock(resolved.statePath, "declared work", async () => {
|
|
106
107
|
const existing = await loadDeclaredWork(resolved.statePath);
|
|
107
108
|
if (existing) {
|
|
@@ -148,6 +149,11 @@ export async function declareWork(input: DeclareWorkInput): Promise<WorkFrozen<D
|
|
|
148
149
|
});
|
|
149
150
|
}
|
|
150
151
|
|
|
152
|
+
/** Persist an already-applied exact selection so the next governed dispatch cannot keep using the old cached declaration. */
|
|
153
|
+
export async function rebindDeclaredWork(state:WorkFrozen<DeclaredWorkState>,selectedSnapshot:NonNullable<WorkProjectionContext["selectedSnapshot"]>):Promise<WorkFrozen<DeclaredWorkState>>{
|
|
154
|
+
return withFileLock(state.statePath,"declared work rebind",async()=>{const current=await loadDeclaredWork(state.statePath);if(!current||JSON.stringify(current.selectedSnapshot)!==JSON.stringify(state.selectedSnapshot))throw Error("declared work rebind is stale; new dispatch remains unsupported");const text=await readFile(state.ledgerPath,"utf8"),resolved=resolveWorkSnapshotText(text,selectedSnapshot),binding=resolved.snapshot?.bindings[0];if(resolved.scopeState!=="valid"||!resolved.scope||!resolved.snapshot||!binding)throw Error("applied declared selection cannot bind future dispatch");const stored:StoredDeclaredWorkState={version:"pi-daddy-declared-work-v1",id:state.id,outcomeDigest:state.outcomeDigest,ledgerPath:state.ledgerPath,grantLedgerPath:state.grantLedgerPath,selectedSnapshot,scope:{kind:resolved.scope.kind,id:resolved.scope.id,revision:resolved.scope.revision,digest:resolved.scope.digest},intent:binding.intent,obligation:binding.obligation,policy:binding.policy};const temporary=`${state.statePath}.${process.pid}.${randomUUID()}.tmp`;try{await writeFile(temporary,`${JSON.stringify(stored,null,2)}\n`,{encoding:"utf8",mode:0o600,flag:"wx"});await rename(temporary,state.statePath);}finally{await rm(temporary,{force:true});}return Object.freeze({...stored,statePath:state.statePath});});
|
|
155
|
+
}
|
|
156
|
+
|
|
151
157
|
export type DeclaredOccurrenceIdentity = Pick<WorkOccurrencePayload["labels"], "toolCallId" | "taskId" | "workspaceId" | "definitionDigest" | "configurationDigest" | "modelId" | "effortId"> & {
|
|
152
158
|
executionId: string;
|
|
153
159
|
parentExecutionId: string | null;
|