beeos-claw 0.6.44-staging.5 → 0.6.44-staging.7

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.
@@ -1 +1 @@
1
- import{randomUUID}from"node:crypto";import{mkdir,readFile}from"node:fs/promises";import path from"node:path";import{withOwnedDirectoryLock}from"./owned-directory-lock.js";import{MonotonicJournalSegments}from"./monotonic-journal-segments.js";const MAX_LOCAL_KEY_ALLOCATION_ATTEMPTS=128;function allocateUniqueRuntimeLocalKey(e,t){for(let r=0;r<128;r+=1){const r=t();if(!e.some(e=>e.runtimeLocalKey===r&&"tombstoned"!==e.state))return r}throw new Error("runtime agent local key allocation exhausted after 128 collisions")}function requireActiveRecord(e){if(!e)throw new Error("runtime agent identity allocation not found");if("active"!==e.state)throw new Error(`mutation lease cannot adopt ${e.state} identity`)}function resolveActiveLocalKey(e,t){const r=e.filter(e=>e.runtimeAgentUid===t&&"active"===e.state);if(1!==r.length)throw new Error(r.length?"runtime agent identity allocation is ambiguous":"runtime agent identity allocation not found");return r[0].observedRuntimeLocalKey??r[0].runtimeLocalKey}export class FileRuntimeAgentIdentityRegistry{filePath;createUid;createLocalKey;publication;lockPath;segments;constructor(e,t=randomUUID,r=()=>`agent-${randomUUID().replace(/-/g,"").slice(0,12)}`,a={}){this.filePath=e,this.createUid=t,this.createLocalKey=r,this.publication=a,this.lockPath=`${e}.lock`,this.segments=new MonotonicJournalSegments(e)}async allocateCreate(e,t,r){return await this.mutate(a=>{const o=a.records.find(t=>t.createOperation?.operationId===e.operationId)??a.records.find(t=>t.operationId===e.operationId);if(o){if(o.createOperation?.command!==t||o.createOperation.runtimeEpoch!==e.runtimeEpoch)throw new Error("create operation identity conflicts with existing allocation");return{...o}}const i=allocateUniqueRuntimeLocalKey(a.records,this.createLocalKey),n=r??this.createUid();if(a.records.some(e=>e.runtimeAgentUid===n))throw new Error("create operation UID conflicts with an existing identity");const s={runtimeAgentUid:n,runtimeLocalKey:i,operationId:e.operationId,leaseId:e.leaseId,runtimeEpoch:e.runtimeEpoch,command:t,createOperation:{operationId:e.operationId,command:t,runtimeEpoch:e.runtimeEpoch},state:"allocated",updatedAt:(new Date).toISOString()};return a.records.push(s),{...s}})}async resolveOrCreate(e){return await this.mutate(t=>{const r=[...t.records].reverse().find(t=>t.runtimeLocalKey===e&&"tombstoned"!==t.state);if(r)return r.runtimeAgentUid;const a=this.createUid();return t.records.push({runtimeAgentUid:a,runtimeLocalKey:e,operationId:`legacy:${randomUUID()}`,leaseId:"legacy",runtimeEpoch:"0",command:"legacy-migration",state:"active",updatedAt:(new Date).toISOString()}),a})}async observeEffectKey(e,t,r,a){await this.transition(e,t,a,e=>{e.observedRuntimeLocalKey=r})}async adoptMutationLease(e,t,r){await this.adoptLease(e,t,r,"active","mutation")}async adoptRecoveryLease(e,t,r){await this.adoptLease(e,t,r,"allocated","recovery")}async requireActive(e,t){requireActiveRecord((await this.read()).records.find(r=>r.runtimeAgentUid===e&&r.runtimeLocalKey===t))}async resolveActiveLocalKey(e){return resolveActiveLocalKey((await this.read()).records,e)}async adoptLease(e,t,r,a,o){await this.mutate(i=>{const n=i.records.find(r=>r.runtimeAgentUid===e&&r.runtimeLocalKey===t);if(!n)throw new Error("runtime agent identity allocation not found");if(n.state!==a)throw new Error(`${o} lease cannot adopt ${n.state} identity`);if(n.operationId!==r.operationId||n.leaseId!==r.leaseId||n.runtimeEpoch!==r.runtimeEpoch){if(BigInt(r.runtimeEpoch)<=BigInt(n.runtimeEpoch))throw new Error(`${o} lease is not a monotonic rollover`);n.operationId=r.operationId,n.leaseId=r.leaseId,n.runtimeEpoch=r.runtimeEpoch,n.updatedAt=(new Date).toISOString()}})}async activate(e,t,r){await this.transition(e,t,r,e=>{if("tombstoned"===e.state)throw new Error("cannot activate tombstoned identity");e.state="active"})}async tombstone(e,t,r){await this.transition(e,t,r,e=>{e.state="tombstoned"})}async pendingAllocations(){return(await this.read()).records.filter(e=>"allocated"===e.state).map(e=>({...e}))}async findCreateOperation(e){return(await this.read()).records.filter(t=>t.runtimeAgentUid===e.runtimeAgentUid&&t.createOperation?.operationId===e.operationId&&t.createOperation.command===e.command&&t.createOperation.runtimeEpoch===e.runtimeEpoch&&(void 0===t.createOperation.paramsHash||t.createOperation.paramsHash===e.paramsHash)).map(e=>({...e,createOperation:e.createOperation&&{...e.createOperation}}))}async bindCreateOperation(e,t,r){return await mkdir(path.dirname(this.filePath),{recursive:!0,mode:448}),await withOwnedDirectoryLock(this.lockPath,async a=>{const o=await this.segments.authorityFloor(),i=await this.read(),n=i.records.find(r=>r.runtimeAgentUid===e&&r.runtimeLocalKey===t);if(!n||!n.createOperation||n.createOperation.operationId!==r.operationId||n.createOperation.command!==r.command||n.createOperation.runtimeEpoch!==r.runtimeEpoch)throw new Error("create operation identity binding conflicts with allocation");if(void 0!==n.createOperation.paramsHash&&n.createOperation.paramsHash!==r.paramsHash)throw new Error("create operation params hash conflicts with allocation");if(n.createOperation.paramsHash===r.paramsHash)return structuredClone(n);if("allocated"!==n.state)throw new Error(`create operation hash is corrupt on ${n.state} identity`);if(!r.paramsHash)throw new Error("create operation params hash is required");return n.createOperation={...n.createOperation,paramsHash:r.paramsHash},n.updatedAt=(new Date).toISOString(),await(this.publication.beforePublish?.(a.epoch)),await a.assertOwned(),await this.segments.publish(a.epoch,[{key:"snapshot",logicalRevision:"0",value:structuredClone(i)}],o),await this.segments.prune(),structuredClone(n)},{...this.publication.lockOptions,epochFloor:()=>this.segments.authorityFloor()})}async inspectRecords(){return(await this.read()).records.map(e=>({...e}))}async transition(e,t,r,a){await this.mutate(o=>{const i=o.records.find(r=>r.runtimeAgentUid===e&&r.runtimeLocalKey===t);if(!i)throw new Error("runtime agent identity allocation not found");if(i.operationId!==r.operationId||i.leaseId!==r.leaseId||i.runtimeEpoch!==r.runtimeEpoch)throw new Error("runtime identity transition fenced");a(i),i.updatedAt=(new Date).toISOString()})}async mutate(e){return await mkdir(path.dirname(this.filePath),{recursive:!0,mode:448}),await withOwnedDirectoryLock(this.lockPath,async t=>{const r=await this.segments.authorityFloor(),a=await this.read(),o=e(a);return await(this.publication.beforePublish?.(t.epoch)),await t.assertOwned(),await this.segments.publish(t.epoch,[{key:"snapshot",logicalRevision:"0",value:structuredClone(a)}],r),await this.segments.prune(),o},{...this.publication.lockOptions,epochFloor:()=>this.segments.authorityFloor()})}async read(){const e=new Map;try{const t=JSON.parse(await readFile(this.filePath,"utf8"));if(1!==t.version||!Array.isArray(t.records))throw new Error("unsupported identity registry");e.set("snapshot",{value:t,logicalRevision:"0",publisherEpoch:"0"})}catch(e){if("ENOENT"!==e.code)throw e}const t=await this.segments.reduce(e);return structuredClone(t.get("snapshot")?.value??{version:1,records:[]})}}export class InMemoryRuntimeAgentIdentityRegistry{createUid;createLocalKey;records=[];constructor(e=randomUUID,t=()=>`agent-${randomUUID().replace(/-/g,"").slice(0,12)}`){this.createUid=e,this.createLocalKey=t}async allocateCreate(e,t,r){const a=this.records.find(t=>t.createOperation?.operationId===e.operationId)??this.records.find(t=>t.operationId===e.operationId);if(a){if(a.createOperation?.command!==t||a.createOperation.runtimeEpoch!==e.runtimeEpoch)throw new Error("create operation identity conflicts with existing allocation");return a}const o=r??this.createUid();if(this.records.some(e=>e.runtimeAgentUid===o))throw new Error("create operation UID conflicts with an existing identity");const i=allocateUniqueRuntimeLocalKey(this.records,this.createLocalKey),n={runtimeAgentUid:o,runtimeLocalKey:i,operationId:e.operationId,leaseId:e.leaseId,runtimeEpoch:e.runtimeEpoch,command:t,createOperation:{operationId:e.operationId,command:t,runtimeEpoch:e.runtimeEpoch},state:"allocated",updatedAt:"now"};return this.records.push(n),n}async resolveOrCreate(e){const t=[...this.records].reverse().find(t=>t.runtimeLocalKey===e&&"tombstoned"!==t.state);if(t)return t.runtimeAgentUid;const r=this.createUid();return this.records.push({runtimeAgentUid:r,runtimeLocalKey:e,operationId:`legacy:${r}`,leaseId:"legacy",runtimeEpoch:"0",command:"legacy-migration",state:"active",updatedAt:"now"}),r}async observeEffectKey(e,t,r,a){this.transition(e,t,a,e=>{e.observedRuntimeLocalKey=r})}async adoptMutationLease(e,t,r){this.adopt(e,t,r,"active","mutation")}async adoptRecoveryLease(e,t,r){this.adopt(e,t,r,"allocated","recovery")}async requireActive(e,t){requireActiveRecord(this.records.find(r=>r.runtimeAgentUid===e&&r.runtimeLocalKey===t))}async resolveActiveLocalKey(e){return resolveActiveLocalKey(this.records,e)}async activate(e,t,r){this.transition(e,t,r,e=>{if("tombstoned"===e.state)throw new Error("cannot activate tombstoned identity");e.state="active"})}async tombstone(e,t,r){this.transition(e,t,r,e=>{e.state="tombstoned"})}async pendingAllocations(){return this.records.filter(e=>"allocated"===e.state).map(e=>({...e}))}async findCreateOperation(e){return this.records.filter(t=>t.runtimeAgentUid===e.runtimeAgentUid&&t.createOperation?.operationId===e.operationId&&t.createOperation.command===e.command&&t.createOperation.runtimeEpoch===e.runtimeEpoch&&(void 0===t.createOperation.paramsHash||t.createOperation.paramsHash===e.paramsHash)).map(e=>({...e,createOperation:e.createOperation&&{...e.createOperation}}))}async bindCreateOperation(e,t,r){const a=this.require(e,t);if(!a.createOperation||a.createOperation.operationId!==r.operationId||a.createOperation.command!==r.command||a.createOperation.runtimeEpoch!==r.runtimeEpoch||void 0!==a.createOperation.paramsHash&&a.createOperation.paramsHash!==r.paramsHash)throw new Error("create operation identity binding conflicts with allocation");if(a.createOperation.paramsHash===r.paramsHash)return{...a,createOperation:{...a.createOperation}};if("allocated"!==a.state)throw new Error(`create operation hash is corrupt on ${a.state} identity`);if(!r.paramsHash)throw new Error("create operation params hash is required");return a.createOperation={...a.createOperation,paramsHash:r.paramsHash},a.updatedAt="now",{...a,createOperation:{...a.createOperation}}}async inspectRecords(){return this.records.map(e=>({...e,createOperation:e.createOperation&&{...e.createOperation}}))}require(e,t){const r=this.records.find(r=>r.runtimeAgentUid===e&&r.runtimeLocalKey===t);if(!r)throw new Error("runtime agent identity allocation not found");return r}adopt(e,t,r,a,o){const i=this.require(e,t);if(i.state!==a)throw new Error(`${o} lease cannot adopt ${i.state} identity`);if(i.operationId!==r.operationId||i.leaseId!==r.leaseId||i.runtimeEpoch!==r.runtimeEpoch){if(BigInt(r.runtimeEpoch)<=BigInt(i.runtimeEpoch))throw new Error(`${o} lease is not a monotonic rollover`);i.operationId=r.operationId,i.leaseId=r.leaseId,i.runtimeEpoch=r.runtimeEpoch,i.updatedAt="now"}}transition(e,t,r,a){const o=this.require(e,t);if(o.operationId!==r.operationId||o.leaseId!==r.leaseId||o.runtimeEpoch!==r.runtimeEpoch)throw new Error("runtime identity transition fenced");a(o),o.updatedAt="now"}}
1
+ import{randomUUID}from"node:crypto";import{mkdir,readFile}from"node:fs/promises";import path from"node:path";import{withOwnedDirectoryLock}from"./owned-directory-lock.js";import{MonotonicJournalSegments}from"./monotonic-journal-segments.js";const MAX_LOCAL_KEY_ALLOCATION_ATTEMPTS=128;function allocateUniqueRuntimeLocalKey(e,t){for(let r=0;r<128;r+=1){const r=t();if(!e.some(e=>e.runtimeLocalKey===r&&"tombstoned"!==e.state))return r}throw new Error("runtime agent local key allocation exhausted after 128 collisions")}function requireActiveRecord(e){if(!e)throw new Error("runtime agent identity allocation not found");if("active"!==e.state)throw new Error(`mutation lease cannot adopt ${e.state} identity`)}function requireDeleteTargetRecord(e){if(!e)throw new Error("runtime agent identity allocation not found");if("allocated"===e.state)throw new Error("delete cannot target allocated identity");return e.state}function resolveActiveLocalKey(e,t){const r=e.filter(e=>e.runtimeAgentUid===t&&"active"===e.state);if(1!==r.length)throw new Error(r.length?"runtime agent identity allocation is ambiguous":"runtime agent identity allocation not found");return r[0].observedRuntimeLocalKey??r[0].runtimeLocalKey}export class FileRuntimeAgentIdentityRegistry{filePath;createUid;createLocalKey;publication;lockPath;segments;constructor(e,t=randomUUID,r=()=>`agent-${randomUUID().replace(/-/g,"").slice(0,12)}`,a={}){this.filePath=e,this.createUid=t,this.createLocalKey=r,this.publication=a,this.lockPath=`${e}.lock`,this.segments=new MonotonicJournalSegments(e)}async allocateCreate(e,t,r){return await this.mutate(a=>{const o=a.records.find(t=>t.createOperation?.operationId===e.operationId)??a.records.find(t=>t.operationId===e.operationId);if(o){if(o.createOperation?.command!==t||o.createOperation.runtimeEpoch!==e.runtimeEpoch)throw new Error("create operation identity conflicts with existing allocation");return{...o}}const i=allocateUniqueRuntimeLocalKey(a.records,this.createLocalKey),n=r??this.createUid();if(a.records.some(e=>e.runtimeAgentUid===n))throw new Error("create operation UID conflicts with an existing identity");const s={runtimeAgentUid:n,runtimeLocalKey:i,operationId:e.operationId,leaseId:e.leaseId,runtimeEpoch:e.runtimeEpoch,command:t,createOperation:{operationId:e.operationId,command:t,runtimeEpoch:e.runtimeEpoch},state:"allocated",updatedAt:(new Date).toISOString()};return a.records.push(s),{...s}})}async resolveOrCreate(e){return await this.mutate(t=>{const r=[...t.records].reverse().find(t=>t.runtimeLocalKey===e&&"tombstoned"!==t.state);if(r)return r.runtimeAgentUid;const a=this.createUid();return t.records.push({runtimeAgentUid:a,runtimeLocalKey:e,operationId:`legacy:${randomUUID()}`,leaseId:"legacy",runtimeEpoch:"0",command:"legacy-migration",state:"active",updatedAt:(new Date).toISOString()}),a})}async observeEffectKey(e,t,r,a){await this.transition(e,t,a,e=>{e.observedRuntimeLocalKey=r})}async adoptMutationLease(e,t,r){await this.adoptLease(e,t,r,"active","mutation")}async adoptRecoveryLease(e,t,r){await this.adoptLease(e,t,r,"allocated","recovery")}async requireActive(e,t){requireActiveRecord((await this.read()).records.find(r=>r.runtimeAgentUid===e&&r.runtimeLocalKey===t))}async requireDeleteTarget(e,t){return requireDeleteTargetRecord((await this.read()).records.find(r=>r.runtimeAgentUid===e&&r.runtimeLocalKey===t))}async resolveActiveLocalKey(e){return resolveActiveLocalKey((await this.read()).records,e)}async adoptLease(e,t,r,a,o){await this.mutate(i=>{const n=i.records.find(r=>r.runtimeAgentUid===e&&r.runtimeLocalKey===t);if(!n)throw new Error("runtime agent identity allocation not found");if(n.state!==a)throw new Error(`${o} lease cannot adopt ${n.state} identity`);if(n.operationId!==r.operationId||n.leaseId!==r.leaseId||n.runtimeEpoch!==r.runtimeEpoch){if(BigInt(r.runtimeEpoch)<=BigInt(n.runtimeEpoch))throw new Error(`${o} lease is not a monotonic rollover`);n.operationId=r.operationId,n.leaseId=r.leaseId,n.runtimeEpoch=r.runtimeEpoch,n.updatedAt=(new Date).toISOString()}})}async activate(e,t,r){await this.transition(e,t,r,e=>{if("tombstoned"===e.state)throw new Error("cannot activate tombstoned identity");e.state="active"})}async tombstone(e,t,r){await this.transition(e,t,r,e=>{e.state="tombstoned"})}async commitDeleteTombstone(e,t,r){await this.mutate(a=>{const o=a.records.find(r=>r.runtimeAgentUid===e&&r.runtimeLocalKey===t);if(!o)throw new Error("runtime agent identity allocation not found");if("tombstoned"!==o.state){if("active"!==o.state)throw new Error(`delete cannot tombstone ${o.state} identity`);o.operationId=r.operationId,o.leaseId=r.leaseId,o.runtimeEpoch=r.runtimeEpoch,o.state="tombstoned",o.updatedAt=(new Date).toISOString()}})}async pendingAllocations(){return(await this.read()).records.filter(e=>"allocated"===e.state).map(e=>({...e}))}async findCreateOperation(e){return(await this.read()).records.filter(t=>t.runtimeAgentUid===e.runtimeAgentUid&&t.createOperation?.operationId===e.operationId&&t.createOperation.command===e.command&&t.createOperation.runtimeEpoch===e.runtimeEpoch&&(void 0===t.createOperation.paramsHash||t.createOperation.paramsHash===e.paramsHash)).map(e=>({...e,createOperation:e.createOperation&&{...e.createOperation}}))}async bindCreateOperation(e,t,r){return await mkdir(path.dirname(this.filePath),{recursive:!0,mode:448}),await withOwnedDirectoryLock(this.lockPath,async a=>{const o=await this.segments.authorityFloor(),i=await this.read(),n=i.records.find(r=>r.runtimeAgentUid===e&&r.runtimeLocalKey===t);if(!n||!n.createOperation||n.createOperation.operationId!==r.operationId||n.createOperation.command!==r.command||n.createOperation.runtimeEpoch!==r.runtimeEpoch)throw new Error("create operation identity binding conflicts with allocation");if(void 0!==n.createOperation.paramsHash&&n.createOperation.paramsHash!==r.paramsHash)throw new Error("create operation params hash conflicts with allocation");if(n.createOperation.paramsHash===r.paramsHash)return structuredClone(n);if("allocated"!==n.state)throw new Error(`create operation hash is corrupt on ${n.state} identity`);if(!r.paramsHash)throw new Error("create operation params hash is required");return n.createOperation={...n.createOperation,paramsHash:r.paramsHash},n.updatedAt=(new Date).toISOString(),await(this.publication.beforePublish?.(a.epoch)),await a.assertOwned(),await this.segments.publish(a.epoch,[{key:"snapshot",logicalRevision:"0",value:structuredClone(i)}],o),await this.segments.prune(),structuredClone(n)},{...this.publication.lockOptions,epochFloor:()=>this.segments.authorityFloor()})}async inspectRecords(){return(await this.read()).records.map(e=>({...e}))}async transition(e,t,r,a){await this.mutate(o=>{const i=o.records.find(r=>r.runtimeAgentUid===e&&r.runtimeLocalKey===t);if(!i)throw new Error("runtime agent identity allocation not found");if(i.operationId!==r.operationId||i.leaseId!==r.leaseId||i.runtimeEpoch!==r.runtimeEpoch)throw new Error("runtime identity transition fenced");a(i),i.updatedAt=(new Date).toISOString()})}async mutate(e){return await mkdir(path.dirname(this.filePath),{recursive:!0,mode:448}),await withOwnedDirectoryLock(this.lockPath,async t=>{const r=await this.segments.authorityFloor(),a=await this.read(),o=e(a);return await(this.publication.beforePublish?.(t.epoch)),await t.assertOwned(),await this.segments.publish(t.epoch,[{key:"snapshot",logicalRevision:"0",value:structuredClone(a)}],r),await this.segments.prune(),o},{...this.publication.lockOptions,epochFloor:()=>this.segments.authorityFloor()})}async read(){const e=new Map;try{const t=JSON.parse(await readFile(this.filePath,"utf8"));if(1!==t.version||!Array.isArray(t.records))throw new Error("unsupported identity registry");e.set("snapshot",{value:t,logicalRevision:"0",publisherEpoch:"0"})}catch(e){if("ENOENT"!==e.code)throw e}const t=await this.segments.reduce(e);return structuredClone(t.get("snapshot")?.value??{version:1,records:[]})}}export class InMemoryRuntimeAgentIdentityRegistry{createUid;createLocalKey;records=[];constructor(e=randomUUID,t=()=>`agent-${randomUUID().replace(/-/g,"").slice(0,12)}`){this.createUid=e,this.createLocalKey=t}async allocateCreate(e,t,r){const a=this.records.find(t=>t.createOperation?.operationId===e.operationId)??this.records.find(t=>t.operationId===e.operationId);if(a){if(a.createOperation?.command!==t||a.createOperation.runtimeEpoch!==e.runtimeEpoch)throw new Error("create operation identity conflicts with existing allocation");return a}const o=r??this.createUid();if(this.records.some(e=>e.runtimeAgentUid===o))throw new Error("create operation UID conflicts with an existing identity");const i=allocateUniqueRuntimeLocalKey(this.records,this.createLocalKey),n={runtimeAgentUid:o,runtimeLocalKey:i,operationId:e.operationId,leaseId:e.leaseId,runtimeEpoch:e.runtimeEpoch,command:t,createOperation:{operationId:e.operationId,command:t,runtimeEpoch:e.runtimeEpoch},state:"allocated",updatedAt:"now"};return this.records.push(n),n}async resolveOrCreate(e){const t=[...this.records].reverse().find(t=>t.runtimeLocalKey===e&&"tombstoned"!==t.state);if(t)return t.runtimeAgentUid;const r=this.createUid();return this.records.push({runtimeAgentUid:r,runtimeLocalKey:e,operationId:`legacy:${r}`,leaseId:"legacy",runtimeEpoch:"0",command:"legacy-migration",state:"active",updatedAt:"now"}),r}async observeEffectKey(e,t,r,a){this.transition(e,t,a,e=>{e.observedRuntimeLocalKey=r})}async adoptMutationLease(e,t,r){this.adopt(e,t,r,"active","mutation")}async adoptRecoveryLease(e,t,r){this.adopt(e,t,r,"allocated","recovery")}async requireActive(e,t){requireActiveRecord(this.records.find(r=>r.runtimeAgentUid===e&&r.runtimeLocalKey===t))}async requireDeleteTarget(e,t){return requireDeleteTargetRecord(this.records.find(r=>r.runtimeAgentUid===e&&r.runtimeLocalKey===t))}async resolveActiveLocalKey(e){return resolveActiveLocalKey(this.records,e)}async activate(e,t,r){this.transition(e,t,r,e=>{if("tombstoned"===e.state)throw new Error("cannot activate tombstoned identity");e.state="active"})}async tombstone(e,t,r){this.transition(e,t,r,e=>{e.state="tombstoned"})}async commitDeleteTombstone(e,t,r){const a=this.require(e,t);if("tombstoned"!==a.state){if("active"!==a.state)throw new Error(`delete cannot tombstone ${a.state} identity`);a.operationId=r.operationId,a.leaseId=r.leaseId,a.runtimeEpoch=r.runtimeEpoch,a.state="tombstoned",a.updatedAt="now"}}async pendingAllocations(){return this.records.filter(e=>"allocated"===e.state).map(e=>({...e}))}async findCreateOperation(e){return this.records.filter(t=>t.runtimeAgentUid===e.runtimeAgentUid&&t.createOperation?.operationId===e.operationId&&t.createOperation.command===e.command&&t.createOperation.runtimeEpoch===e.runtimeEpoch&&(void 0===t.createOperation.paramsHash||t.createOperation.paramsHash===e.paramsHash)).map(e=>({...e,createOperation:e.createOperation&&{...e.createOperation}}))}async bindCreateOperation(e,t,r){const a=this.require(e,t);if(!a.createOperation||a.createOperation.operationId!==r.operationId||a.createOperation.command!==r.command||a.createOperation.runtimeEpoch!==r.runtimeEpoch||void 0!==a.createOperation.paramsHash&&a.createOperation.paramsHash!==r.paramsHash)throw new Error("create operation identity binding conflicts with allocation");if(a.createOperation.paramsHash===r.paramsHash)return{...a,createOperation:{...a.createOperation}};if("allocated"!==a.state)throw new Error(`create operation hash is corrupt on ${a.state} identity`);if(!r.paramsHash)throw new Error("create operation params hash is required");return a.createOperation={...a.createOperation,paramsHash:r.paramsHash},a.updatedAt="now",{...a,createOperation:{...a.createOperation}}}async inspectRecords(){return this.records.map(e=>({...e,createOperation:e.createOperation&&{...e.createOperation}}))}require(e,t){const r=this.records.find(r=>r.runtimeAgentUid===e&&r.runtimeLocalKey===t);if(!r)throw new Error("runtime agent identity allocation not found");return r}adopt(e,t,r,a,o){const i=this.require(e,t);if(i.state!==a)throw new Error(`${o} lease cannot adopt ${i.state} identity`);if(i.operationId!==r.operationId||i.leaseId!==r.leaseId||i.runtimeEpoch!==r.runtimeEpoch){if(BigInt(r.runtimeEpoch)<=BigInt(i.runtimeEpoch))throw new Error(`${o} lease is not a monotonic rollover`);i.operationId=r.operationId,i.leaseId=r.leaseId,i.runtimeEpoch=r.runtimeEpoch,i.updatedAt="now"}}transition(e,t,r,a){const o=this.require(e,t);if(o.operationId!==r.operationId||o.leaseId!==r.leaseId||o.runtimeEpoch!==r.runtimeEpoch)throw new Error("runtime identity transition fenced");a(o),o.updatedAt="now"}}
@@ -1 +1 @@
1
- import{createHash,randomUUID}from"node:crypto";import{mkdir,writeFile}from"node:fs/promises";import{homedir}from"node:os";import path,{join,relative,resolve}from"node:path";import{RUNTIME_HASH_DOMAINS,RUNTIME_METHOD_PARITY_MANIFEST,runtimeHashPreimage}from"@beeos-ai/beeos-types/runtime";import{reduceRuntimeRecoveryPolicy}from"@beeos-ai/beeos-types/runtime/internal";import{assertTrustedCoreExecutionAttempt}from"./core-execution-attempt-context.js";import{InMemoryRuntimeCommandJournal,RuntimeCommandJournalConflictError,runtimeCommandIsReplayableQuery,runtimeCommandResultHash}from"./runtime-command-journal.js";import{RuntimeProjectionTransportError}from"./agent-projection-worker.js";const DEFAULT_AGENT_ID="main",AGENT_VALID_ID_RE=/^[a-z0-9][a-z0-9_-]{0,63}$/i,AGENT_INVALID_CHARS_RE=/[^a-z0-9_-]+/g;export function normalizeRuntimeAgentLocalKey(e){const t=(e??"").trim();return t?AGENT_VALID_ID_RE.test(t)?t.toLowerCase():t.toLowerCase().replace(AGENT_INVALID_CHARS_RE,"-").replace(/^-+/,"").replace(/-+$/,"").slice(0,64)||"main":"main"}export async function awaitRuntimeProjection(e){const t=e;return await(t.awaitProjection?.()??Promise.resolve(e.getSnapshot().projection))}export function hasEffectReceiptHandoff(e){const t=e;return t.effectCommitted instanceof Promise&&"function"==typeof t.acknowledgeEffectReceipt}export class RuntimeCommandError extends Error{code;param;constructor(e,t,r,i){super(t,i),this.code=e,this.param=r,this.name="RuntimeCommandError"}}const emptyProgress=async function*(){};function deferred(){let e;return{promise:new Promise(t=>{e=t}),resolve:e}}async function waitForEffectReceipt(e,t){if(!t)return await e;if(t.aborted)throw new RuntimeCommandError("OUTCOME_UNKNOWN","MCP effect receipt was not durably published before the Service attempt ended");await new Promise((r,i)=>{const n=()=>i(new RuntimeCommandError("OUTCOME_UNKNOWN","MCP effect receipt was not durably published before the Service attempt ended"));t.addEventListener("abort",n,{once:!0}),e.then(()=>{t.removeEventListener("abort",n),r()},e=>{t.removeEventListener("abort",n),i(e)})})}export class OpenClawRuntimeCommandDispatcher{deps;logger;getOpenclawHome;createAgentId;journal;capabilities;constructor(e){this.deps=e,this.logger=e.logger??{},this.getOpenclawHome=e.getOpenclawHome??(()=>process.env.OPENCLAW_HOME||join(homedir(),".openclaw")),this.createAgentId=e.createAgentId??(()=>`agent-${randomUUID().replace(/-/g,"").slice(0,12)}`),this.journal=e.journal??new InMemoryRuntimeCommandJournal,this.capabilities=e.capabilities}setCapabilities(e){this.capabilities=e}dispatch(e,t){const r=e;assertTrustedCoreExecutionAttempt(t,{operationId:r.context.operationId,runtimeEpoch:r.context.dispatchEpoch});let i,n,a,o="running";const s=deferred(),c=deferred();let m;const d="service"===t.origin&&("mcp.set"===r.name||"mcp.unset"===r.name),p=this.executeWithLease(r,t,(e,t)=>{o="committed",m={operationId:r.context.operationId,method:r.name,record:t},s.resolve({...m,result:e,committedAt:t.updatedAt})},async()=>{d?await waitForEffectReceipt(c.promise,t.abortSignal):"acp"!==t.origin||"mcp.set"!==r.name&&"mcp.unset"!==r.name||!m?.record.mcpDesiredStateCommit||(await this.journal.releaseMcpLiveEffect({operationId:r.context.operationId,source:"acp_local"}),await this.journal.promoteMcpDesiredStateActive({operationId:r.context.operationId,source:"acp_local"}))},d,e=>{i=e},e=>{n=e}).then(e=>(o="committed",e),e=>{throw o=e instanceof RuntimeCommandError&&("OUTCOME_UNKNOWN"===e.code||"INSTANCE_EPOCH_MISMATCH"===e.code)?"outcome_unknown":"failed",s.resolve(void 0),e});return{operationId:r.context.operationId,getSnapshot:()=>({effectState:o,...i?{projection:i}:{}}),completion:p,progress:emptyProgress(),effectCommitted:s.promise,acknowledgeEffectReceipt:async e=>{if(!m||e.operationId!==m.operationId||e.method!==m.method)throw new RuntimeCommandError("OUTCOME_UNKNOWN","MCP effect receipt does not match this operation");if(!/^[a-f0-9]{64}$/.test(e.effectRecordHash))throw new RuntimeCommandError("OUTCOME_UNKNOWN","MCP effect receipt hash is invalid");await this.journal.releaseMcpLiveEffect({operationId:e.operationId,source:"service",effectRecordHash:e.effectRecordHash}),c.resolve()},acknowledgeTerminalReceipt:async e=>{if(!(d&&m&&e.operationId===m.operationId&&e.method===m.method&&/^[a-f0-9]{64}$/.test(e.effectRecordHash)&&e.terminalMessageId))throw new RuntimeCommandError("OUTCOME_UNKNOWN","MCP terminal receipt does not match this operation");await this.journal.promoteMcpDesiredStateActive({operationId:e.operationId,source:"service",effectRecordHash:e.effectRecordHash,terminalMessageId:e.terminalMessageId});const a=await this.requireReleasedRecord(e.operationId);if(void 0===a.result)throw new RuntimeCommandError("OUTCOME_UNKNOWN","active MCP desired state lost its committed result");await this.afterCommittedEffect(r,a.result,a,t,e=>{i=e},e=>{n=e})},cancel:async()=>"running"===o?{status:"cancel_requested"===await(this.capabilities?.cancel?.(r.context.operationId))?"cancel_requested":"not_cancellable"}:{status:"already_terminal"},awaitProjection:async()=>n?(a??=n(),await a):i}}async executeWithLease(e,t,r,i,n,a,o){const s=await this.deps.leases.claim(e.context.operationId,e.context.dispatchEpoch);if(s.runtimeEpoch!==e.context.dispatchEpoch)throw await this.deps.leases.release(s),new RuntimeCommandError("INSTANCE_EPOCH_MISMATCH","runtime operation lease epoch mismatch");const c=this.deps.leases.startHeartbeat(s);try{let c;await this.recoverInterruptedCreates(e.context.operationId);try{c=await this.journal.prepare({operationId:e.context.operationId,command:e.name,paramsHash:e.context.paramsHash,runtimeEpoch:s.runtimeEpoch})}catch(e){if(e instanceof RuntimeCommandJournalConflictError)throw new RuntimeCommandError("OUTCOME_UNKNOWN",e.message,void 0,{cause:e});throw e}if(("effect_committed"===c.phase||"projection_committed"===c.phase)&&void 0!==c.result){if(r(c.result,c),await i(),c=await this.requireReleasedRecord(c.operationId),void 0===c.result)throw new RuntimeCommandError("OUTCOME_UNKNOWN","committed result disappeared from command journal");return n||await this.afterCommittedEffect(e,c.result,c,t,a,o),c.result}if(("effect_committed"===c.phase||"projection_committed"===c.phase)&&"hash_only"===c.resultReceipt?.policy&&runtimeCommandIsReplayableQuery(e.name)){const n=await this.execute(e,s,t);if(runtimeCommandResultHash(n)!==c.resultReceipt.resultHash)throw new RuntimeCommandError("OUTCOME_UNKNOWN","redacted committed query result changed");return r(n,c),await i(),n}if(("effect_committed"===c.phase||"projection_committed"===c.phase)&&"hash_only"===c.resultReceipt?.policy){if(r(void 0,c),await i(),c=await this.requireReleasedRecord(c.operationId),!n){if(isRuntimeAgentMutation(e)&&(c.agentProjectionIntent||c.agentProjectionPreparationFailure)){const r=agentProjectionRecoveryResult(c.agentProjectionIntent??c.agentProjectionPreparationFailure);return await this.afterCommittedEffect(e,r,c,t,a,o),r}await this.runAfterEffect(e,void 0,c,t,a)}throw new RuntimeCommandError("OUTCOME_UNKNOWN","committed mutation result is redacted")}if("effect_committed"===c.phase||"projection_committed"===c.phase)throw new RuntimeCommandError("OUTCOME_UNKNOWN","committed result receipt is unavailable");if("outcome_unknown"===c.phase)throw new RuntimeCommandError("OUTCOME_UNKNOWN","previous execution outcome is unknown");if("started"===c.phase&&isRuntimeAgentMutation(e)){let n;try{n=await this.recoverStartedAgentMutation(e,s,t)}catch(e){throw await this.markJournalUnknown(c),e instanceof RuntimeCommandError?e:new RuntimeCommandError("OUTCOME_UNKNOWN",`agent desired-state recovery could not prove its effect: ${errorText(e)}`,void 0,{cause:e})}const m=this.deps.projection?await this.prepareAgentProjectionCommit(e,n,t):void 0,d=await this.journal.transition({operationId:c.operationId,expectedRevision:c.revision,phase:"effect_committed",result:n,...m?{agentProjection:m}:{}});return r(n,d),await i(),await this.afterCommittedEffect(e,n,d,t,a,o),this.journal.compact().catch(()=>{}),n}if("started"===c.phase&&"outcome_unknown"===this.recoveryDecision(e.name,c.phase))throw await this.markJournalUnknown(c),new RuntimeCommandError("OUTCOME_UNKNOWN","imperative action crossed its execution boundary");if("failed"===c.phase)throw new RuntimeCommandError("EXECUTION_FAILED","previous execution failed");"prepared"===c.phase&&(c=await this.journal.transition({operationId:c.operationId,expectedRevision:c.revision,phase:"started"}));let m=!1;try{const d=await this.execute(e,s,t);m=!0;const p=isRuntimeAgentMutation(e)&&this.deps.projection?await this.prepareAgentProjectionCommit(e,d,t):void 0;let u;try{const e=await this.requireReleasedRecord(c.operationId);u=await this.journal.transition({operationId:e.operationId,expectedRevision:e.revision,phase:"effect_committed",result:d,...p?{agentProjection:p}:{}})}catch(e){throw new RuntimeCommandError("OUTCOME_UNKNOWN",`effect completed before its journal commit: ${errorText(e)}`,void 0,{cause:e})}return r(d,u),await i(),u=await this.requireReleasedRecord(u.operationId),n||await this.afterCommittedEffect(e,d,u,t,a,o),this.journal.compact().catch(()=>{}),d}catch(t){if(m)throw t;const r=t instanceof RuntimeCommandError&&("OUTCOME_UNKNOWN"===t.code||"INSTANCE_EPOCH_MISMATCH"===t.code);if(r&&"clear-saga"===recoveryMetadata(e.name).recoveryClass)throw t;try{await this.journal.transition({operationId:c.operationId,expectedRevision:c.revision,phase:r?"outcome_unknown":"failed",errorCode:r?"OUTCOME_UNKNOWN":"EXECUTION_FAILED"})}catch(e){throw new RuntimeCommandError("OUTCOME_UNKNOWN",`command and journal outcome unknown: ${errorText(e)}`,void 0,{cause:t})}throw t}}finally{try{await c.stop(),await this.deps.leases.release(s)}catch(e){throw new RuntimeCommandError("OUTCOME_UNKNOWN",`runtime operation lease finalization unknown: ${errorText(e)}`,void 0,{cause:e})}}}async execute(e,t,r){switch(e.name){case"agent.create":return await this.createAgent(e,t,r);case"agent.update":return await this.updateAgent(e);case"agent.delete":return await this.deleteAgent(e,t);case"agent.applyTemplate":return await this.applyTemplate(e,t,r)}if(!this.capabilities)throw new RuntimeCommandError("INVALID_PARAMS",`Unsupported runtime command: ${e.name}`);return await this.capabilities.execute(e,t,r)}async recoverStartedAgentMutation(e,t,r){if("agent.create"===e.name||"agent.applyTemplate"===e.name){let i=await this.bindCreateOperation(e,t,!1);if(i||(i=await this.findCreateRecoveryIdentity(e,!1),"allocated"===i.state&&void 0===i.createOperation?.paramsHash&&await this.bindCreateOperation(e,t,!1),i=await this.findCreateRecoveryIdentity(e)),"active"===i.state)return await this.recoverActiveCreateEffect(i);if(await this.runtimeAgentExists(i.observedRuntimeLocalKey??i.runtimeLocalKey)){const n={runtimeLocalKey:i.observedRuntimeLocalKey??i.runtimeLocalKey,workspacePath:join(this.getOpenclawHome(),`workspace-${i.runtimeLocalKey}`)};return"agent.create"===e.name?await this.completeCreatedAgent(e,t,r,n):await this.completeTemplateAgent(e,t,r,n)}return await this.execute(e,t,r)}const i=e.context.agent,n=await this.runtimeAgentExists(i.runtimeLocalKey);if("agent.delete"===e.name&&!n)return{runtimeAgentUid:i.runtimeAgentUid,runtimeLocalKey:i.runtimeLocalKey};if("agent.update"===e.name&&!n)throw new RuntimeCommandError("OUTCOME_UNKNOWN","update recovery target is absent");return await this.execute(e,t,r)}async runtimeAgentExists(e){const t=await this.requireGateway().request("agents.list",{});if(!Array.isArray(t?.agents))throw new RuntimeCommandError("OUTCOME_UNKNOWN","runtime agent directory query is incomplete");return t.agents.some(t=>null!==t&&"object"==typeof t&&(t.id===e||t.name===e))}async findCreateRecoveryIdentity(e,t=!0){const r={operationId:e.context.operationId,command:e.name,runtimeEpoch:e.context.dispatchEpoch,paramsHash:e.context.paramsHash,runtimeAgentUid:e.params.runtimeAgentUid},i=void 0!==this.deps.identities.findCreateOperation,n=i?await this.deps.identities.findCreateOperation(r):(await this.deps.identities.pendingAllocations()).filter(e=>e.operationId===r.operationId&&e.command===r.command&&e.runtimeEpoch===r.runtimeEpoch&&e.runtimeAgentUid===r.runtimeAgentUid);if(1!==n.length)throw new RuntimeCommandError("OUTCOME_UNKNOWN","create recovery identity binding is missing or ambiguous");const a=n[0];if(i&&(!a.createOperation||t&&a.createOperation.paramsHash!==r.paramsHash||a.createOperation.operationId!==r.operationId||a.createOperation.command!==r.command||a.createOperation.runtimeEpoch!==r.runtimeEpoch))throw new RuntimeCommandError("OUTCOME_UNKNOWN","create recovery identity hash binding conflicts");return a}async bindCreateOperation(e,t,r=!0){if(!this.deps.identities.findCreateOperation||!this.deps.identities.bindCreateOperation)return;const i={operationId:e.context.operationId,command:e.name,runtimeEpoch:e.context.dispatchEpoch,paramsHash:e.context.paramsHash,runtimeAgentUid:e.params.runtimeAgentUid};try{let n=await this.deps.identities.findCreateOperation(i);if(0===n.length){if(!r)throw new RuntimeCommandError("OUTCOME_UNKNOWN","create recovery identity binding is missing");if((await this.deps.identities.allocateCreate(t,e.name,e.params.runtimeAgentUid)).runtimeAgentUid!==e.params.runtimeAgentUid)throw new RuntimeCommandError("OUTCOME_UNKNOWN","create allocation UID does not match the canonical command");n=await this.deps.identities.findCreateOperation(i)}if(1!==n.length)throw new RuntimeCommandError("OUTCOME_UNKNOWN","create identity binding is missing or ambiguous");const a=n[0],o=await this.deps.identities.bindCreateOperation(a.runtimeAgentUid,a.runtimeLocalKey,{operationId:e.context.operationId,command:e.name,runtimeEpoch:e.context.dispatchEpoch,paramsHash:e.context.paramsHash});if(!o.createOperation||o.runtimeAgentUid!==e.params.runtimeAgentUid||o.createOperation.operationId!==i.operationId||o.createOperation.command!==i.command||o.createOperation.runtimeEpoch!==i.runtimeEpoch||o.createOperation.paramsHash!==i.paramsHash)throw new Error("create operation identity binding changed during compare-and-set");if("tombstoned"===o.state)throw new Error("create operation identity is tombstoned");return o}catch(e){if(e instanceof RuntimeCommandError)throw e;throw new RuntimeCommandError("OUTCOME_UNKNOWN",`create operation identity binding is corrupt: ${errorText(e)}`,void 0,{cause:e})}}async recoverActiveCreateEffect(e){const t=e.observedRuntimeLocalKey??e.runtimeLocalKey;if(!await this.runtimeAgentExists(t))throw new RuntimeCommandError("OUTCOME_UNKNOWN","active create identity is absent from the runtime directory");return{runtimeAgentUid:e.runtimeAgentUid,runtimeLocalKey:t,workspacePath:join(this.getOpenclawHome(),`workspace-${e.runtimeLocalKey}`)}}async markJournalUnknown(e){try{await this.journal.transition({operationId:e.operationId,expectedRevision:e.revision,phase:"outcome_unknown",errorCode:"OUTCOME_UNKNOWN"})}catch{}}async requireReleasedRecord(e){const t=await this.journal.get(e);if(!t)throw new RuntimeCommandError("OUTCOME_UNKNOWN","command journal record disappeared");return t}async afterCommittedEffect(e,t,r,i,n,a){if(!isRuntimeAgentMutation(e))return void await this.runAfterEffect(e,t,r,i,n);const o=await this.prepareAgentProjection(e,t,r,i,n);o&&(a(o),"acp"===i.origin&&o().catch(e=>this.logger.warn?.(`[runtime-core] deferred ACP agent projection failed: ${errorText(e)}`)))}async prepareAgentProjectionCommit(e,t,r){const i=agentProjectionRecoverySummary(e,t,r);try{const r=await this.deps.projection.prepareCommittedMutation({command:e,result:t,...i.expectedPlatformAgentId?{expectedPlatformAgentId:i.expectedPlatformAgentId}:{}});return Object.freeze({...r,...i})}catch(e){const t=!(e instanceof RuntimeProjectionTransportError)||e.retryable;return this.logger.warn?.(`[runtime-core] agent projection preparation ${t?"deferred":"blocked"}: ${errorText(e)}`),Object.freeze({...i,state:t?"pending":"blocked",errorKind:t?"snapshot_retryable":"snapshot_nonretryable"})}}async prepareAgentProjection(e,t,r,i,n){if("projection_committed"===r.phase)return n(r.projectionReceipt?.snapshot??Object.freeze({state:"synced",revision:`${Math.max(0,r.revision-1)}`})),r.agentProjectionIntent&&await this.compactAgentProjectionReference(r.agentProjectionIntent),void await this.runPostCommit(e,t);if("effect_committed"!==r.phase||!this.deps.projection)return;let a=r,o=a.agentProjectionIntent;const s=a.agentProjectionPreparationFailure;if(o&&"blocked"===a.agentProjectionTerminal?.state)return n(Object.freeze({state:"blocked",revision:a.agentProjectionTerminal.revision})),await this.compactAgentProjectionReference(o),void await this.runPostCommit(e,t);let c;return o&&n(Object.freeze({state:"pending",revision:o.projectionRevision})),async()=>(c??=(async()=>{if(!o){if(!s||"blocked"===s.state)return Object.freeze({state:"blocked",revision:`${Math.max(0,a.revision)}`});const t=await this.prepareAgentProjectionCommit(e,agentProjectionRecoveryResult(s),i);if(!isProjectionIntent(t))return"blocked"===t.state?Object.freeze({state:"blocked",revision:`${Math.max(0,a.revision)}`}):Object.freeze({state:"pending",revision:`${Math.max(0,a.revision)}`});o=t;try{a=await this.journal.recordAgentProjectionIntent({operationId:a.operationId,expectedRevision:a.revision,intent:o})}catch(e){return void this.logger.warn?.(`[runtime-core] agent projection reservation recovery deferred: ${errorText(e)}`)}n(Object.freeze({state:"pending",revision:o.projectionRevision}))}const r=Object.freeze({state:"pending",revision:o.projectionRevision});let c;try{c=await this.deps.projection.applyCommittedMutation(o)}catch(e){this.logger.warn?.(`[runtime-core] agent projection receipt deferred: ${errorText(e)}`),c=r}if(c.revision!==o.projectionRevision&&(c=Object.freeze({state:"blocked",revision:o.projectionRevision})),"synced"===c.state)try{a=await this.journal.transition({operationId:a.operationId,expectedRevision:a.revision,phase:"projection_committed",projectionSnapshot:c}),await this.compactAgentProjectionReference(o)}catch(e){this.logger.warn?.(`[runtime-core] projection phase commit deferred: ${errorText(e)}`),c=Object.freeze({state:"pending",revision:o.projectionRevision})}if("blocked"===c.state)try{a=await this.journal.recordAgentProjectionBlocked({operationId:a.operationId,expectedRevision:a.revision,revision:o.projectionRevision}),await this.compactAgentProjectionReference(o)}catch(e){this.logger.warn?.(`[runtime-core] blocked projection terminal commit deferred: ${errorText(e)}`),c=r}return"synced"===c.state&&c.platformAgentId&&"projection_committed"===a.phase&&this.reportTemplateOriginBestEffort(e,c.platformAgentId),n(c),await this.runPostCommit(e,t),c})(),await c)}async compactAgentProjectionReference(e){if(this.deps.projection)try{await this.deps.projection.compactCommittedMutation(e)}catch(e){this.logger.warn?.(`[runtime-core] projection reference compaction deferred: ${errorText(e)}`)}}reportTemplateOriginBestEffort(e,t){if("agent.applyTemplate"!==e.name||!this.deps.templateOriginReporter)return;const r=e.params.templateId;r&&this.deps.templateOriginReporter.report({platformAgentId:t,templateId:r}).catch(e=>{this.logger.warn?.(`[runtime-core] active template origin report failed, client fallback remains: ${errorText(e)}`)})}async runAfterEffect(e,t,r,i,n){if("projection_committed"===r.phase)return n(r.projectionReceipt?.snapshot??Object.freeze({state:"synced",revision:`${Math.max(0,r.revision-1)}`})),void await this.runPostCommit(e,t);const a="effect_committed"===r.phase&&await this.runProjection(e,t,i),o=!0===a?Object.freeze({state:"synced",revision:`${r.revision}`}):a&&"object"==typeof a?Object.freeze({...a,revision:`${r.revision}`}):"session.setModel"===e.name?Object.freeze({state:"pending",revision:`${r.revision}`}):void 0;if("synced"===o?.state)try{const e=o;await this.journal.transition({operationId:r.operationId,expectedRevision:r.revision,phase:"projection_committed",projectionSnapshot:e}),n(o)}catch(e){return this.logger.warn?.(`[runtime-core] projection phase commit deferred: ${errorText(e)}`),void n(Object.freeze({revision:o.revision,state:"pending"}))}else o&&n(o);await this.runPostCommit(e,t)}async runPostCommit(e,t){if(this.capabilities?.afterCommit)try{await this.capabilities.afterCommit(e,t)}catch(e){this.logger.warn?.(`[runtime-core] post-commit action failed: ${errorText(e)}`)}}async runProjection(e,t,r){if(!recoveryMetadata(e.name).projectionRequired)return!1;try{return!isRuntimeAgentMutation(e)&&(await(this.capabilities?.projectCommitted?.(e,t,r))??!1)}catch(e){return this.logger.warn?.(`[runtime-core] agent projection deferred: ${errorText(e)}`),!1}}recoveryDecision(e,t){const r=recoveryMetadata(e);return reduceRuntimeRecoveryPolicy({...r,messageServicePhase:"queued",executionBoundaryCrossed:"prepared"!==t,lineageContinuity:"continuous",journalState:"available",journalPhase:t})}requireGateway(){const e=this.deps.getGateway();if(!e?.isConnected)throw new RuntimeCommandError("GATEWAY_NOT_CONNECTED","Gateway not connected");return e}async createAgentShell(e,t,r,i,n,a){const o=this.requireGateway(),s=(await this.deps.identities.pendingAllocations()).find(t=>t.runtimeAgentUid===e),c=a??s?.runtimeLocalKey??this.createAgentId(),m=join(this.getOpenclawHome(),`workspace-${c}`);let d;try{d=await o.request("agents.create",{name:c,workspace:m,...n?{model:n}:{},...i?.emoji?{emoji:i.emoji}:{},...i?.avatar?{avatar:i.avatar}:{}})}catch(r){if(isDeterministicAgentsCreateParamsRejection(r)&&(void 0!==a||void 0!==s))try{await this.deps.identities.tombstone(e,c,t)}catch(e){this.logger.warn?.(`[runtime-core] rejected-create allocation cleanup deferred: ${errorText(e)}`)}throw r}const p=d?.agentId??c,u=d?.workspace??m;if(await this.deps.identities.observeEffectKey(e,c,p,t),p!==c)throw new RuntimeCommandError("OUTCOME_UNKNOWN",`Create agent outcome unknown: gateway changed reserved local key to ${p}`);try{await o.request("agents.update",{agentId:p,name:r})}catch(e){this.logger.warn?.(`[runtime-core] display-name update failed agent=${p}: ${errorText(e)}`)}return{runtimeLocalKey:p,workspacePath:u}}async createAgent(e,t,r){const i=e.params.name.trim();if(!i)throw new RuntimeCommandError("INVALID_PARAMS","Missing agent name");try{const n=await this.bindCreateOperation(e,t);if("active"===n?.state)return await this.recoverActiveCreateEffect(n);const a=await this.createAgentShell(e.params.runtimeAgentUid,t,i,e.params.identity,e.params.defaultModelId,n?.runtimeLocalKey);return await this.completeCreatedAgent(e,t,r,a)}catch(e){throw this.executionError("Create agent",e)}}async updateAgent(e){const{runtimeAgentUid:t,runtimeLocalKey:r}=e.context.agent;if(!r.trim())throw new RuntimeCommandError("MISSING_PARAM","Missing agentId","agentId");const i=e.params,n=i.name?.trim(),a=i.defaultModelId?.trim(),o=i.identity?.emoji?.trim(),s=i.identity?.avatar?.trim();if(!(n||a||o||s||void 0!==i.description||void 0!==i.version||void 0!==i.capabilities||void 0!==i.skills||void 0!==i.avatarUrl||void 0!==i.homepage||void 0!==i.status||void 0!==i.identity?.vibe))throw new RuntimeCommandError("INVALID_PARAMS","No updatable fields provided (name/model/identity)");try{return await this.deps.identities.requireActive(t,r),await this.requireGateway().request("agents.update",{agentId:r,...n?{name:n}:{},...a?{model:a}:{},...o?{emoji:o}:{},...s?{avatar:s}:{},...i.identity?.vibe?{vibe:i.identity.vibe}:{},...void 0!==i.description?{description:i.description}:{},...void 0!==i.version?{version:i.version}:{},...void 0!==i.capabilities?{capabilities:i.capabilities}:{},...void 0!==i.skills?{skills:i.skills}:{},...void 0!==i.avatarUrl?{avatarUrl:i.avatarUrl}:{},...void 0!==i.homepage?{homepage:i.homepage}:{},...void 0!==i.status?{status:i.status}:{}}),{runtimeAgentUid:t,runtimeLocalKey:r}}catch(e){throw this.executionError("Update agent",e)}}async deleteAgent(e,t){const{runtimeAgentUid:r,runtimeLocalKey:i}=e.context.agent;if(!i.trim())throw new RuntimeCommandError("MISSING_PARAM","Missing agentId","agentId");if("main"===normalizeRuntimeAgentLocalKey(i))throw new RuntimeCommandError("INVALID_PARAMS",'"main" cannot be deleted; delete the instance instead');try{await this.deps.identities.adoptMutationLease(r,i,t);const n=requireCommittedOpenClawDeleteAck(await this.requireGateway().request("agents.delete",{agentId:i,deleteFiles:e.params.deleteFiles??!0}),i);try{await this.deps.identities.tombstone(r,i,t)}catch(e){throw new RuntimeCommandError("OUTCOME_UNKNOWN",`Delete agent outcome unknown: identity tombstone failed: ${errorText(e)}`,void 0,{cause:e})}return{runtimeAgentUid:r,runtimeLocalKey:i,...void 0===n.removedBindings?{}:{removedBindings:n.removedBindings}}}catch(e){throw this.executionError("Delete agent",e)}}async applyTemplate(e,t,r){const i=e.params.name.trim();if(!i)throw new RuntimeCommandError("INVALID_PARAMS","Missing agent name");this.requireGateway();try{const n=await this.bindCreateOperation(e,t);if("active"===n?.state)return await this.recoverActiveCreateEffect(n);const a=await r.resources.readVerifiedArtifact(e.params.artifact),o=await this.createAgentShell(e.params.runtimeAgentUid,t,i,e.params.identity,e.params.defaultModelId,n?.runtimeLocalKey);return await this.completeTemplateAgent(e,t,r,o,a)}catch(e){throw this.executionError("Apply template",e)}}async completeCreatedAgent(e,t,r,i){for(const t of e.params.bootstrapFiles??[]){if("IDENTITY.md"===t.name){this.logger.warn?.("[runtime-core] agent.create ignoring IDENTITY.md; identity is structured");continue}const e=await r.resources.readContent(t.contentRef);try{await this.requireGateway().request("agents.files.set",{agentId:i.runtimeLocalKey,name:t.name,content:e})}catch(e){this.logger.warn?.(`[runtime-core] bootstrap write failed file=${t.name}: ${errorText(e)}`)}}try{await this.deps.identities.activate(e.params.runtimeAgentUid,i.runtimeLocalKey,t)}catch(r){throw await this.compensateCreatedAgent(e.params.runtimeAgentUid,i.runtimeLocalKey,t,r,"Create agent"),new RuntimeCommandError("EXECUTION_FAILED",`Create agent failed after compensation: ${errorText(r)}`)}return{runtimeAgentUid:e.params.runtimeAgentUid,runtimeLocalKey:i.runtimeLocalKey,workspacePath:i.workspacePath}}async completeTemplateAgent(e,t,r,i,n){const a=n??await r.resources.readVerifiedArtifact(e.params.artifact);try{for(const[e,t]of a.workspaceFiles){const r=resolve(i.workspacePath,e),n=relative(i.workspacePath,r);if(""===n||n.startsWith("..")||path.isAbsolute(n))throw new Error(`refusing to write outside workspace: ${e}`);await(this.deps.mkdirWorkspace??mkdir)(path.dirname(r),{recursive:!0}),await(this.deps.writeWorkspaceFile??writeFile)(r,t)}await this.deps.identities.activate(e.params.runtimeAgentUid,i.runtimeLocalKey,t)}catch(r){throw await this.compensateCreatedAgent(e.params.runtimeAgentUid,i.runtimeLocalKey,t,r,"Apply template"),new RuntimeCommandError("EXECUTION_FAILED",`Apply template failed after compensation: ${errorText(r)}`,void 0,{cause:r})}return{runtimeAgentUid:e.params.runtimeAgentUid,runtimeLocalKey:i.runtimeLocalKey,workspacePath:i.workspacePath}}executionError(e,t){return t instanceof RuntimeCommandError?t:(this.logger.error?.(`[runtime-core] ${e.toLowerCase()} failed: ${errorText(t)}`),new RuntimeCommandError("EXECUTION_FAILED",`${e} failed: ${errorText(t)}`,void 0,{cause:t}))}async compensateCreatedAgent(e,t,r,i,n){try{const i=this.requireGateway();requireCommittedOpenClawDeleteAck(await i.request("agents.delete",{agentId:t,deleteFiles:!0}),t),await this.deps.identities.tombstone(e,t,r)}catch(e){throw new RuntimeCommandError("OUTCOME_UNKNOWN",`${n} outcome unknown after ${errorText(i)}; compensation unproven: ${errorText(e)}`,void 0,{cause:e})}}async recoverInterruptedCreates(e){const t=await this.deps.identities.pendingAllocations();for(const r of t){if(r.operationId===e)continue;const t=await this.deps.leases.claimExpiredForRecovery(r.operationId);if(!t)continue;const i=this.deps.leases.startHeartbeat(t);try{await this.deps.identities.adoptRecoveryLease(r.runtimeAgentUid,r.runtimeLocalKey,t);const e=r.observedRuntimeLocalKey;if(!e){const e=r.createOperation?.operationId??r.operationId,i=await this.journal.get(e);if("failed"===i?.phase&&i.command===r.command&&!await this.runtimeAgentExists(r.runtimeLocalKey)){await this.deps.identities.tombstone(r.runtimeAgentUid,r.runtimeLocalKey,t);continue}throw new Error("no persisted gateway effect key; absence cannot be proven")}requireCommittedOpenClawDeleteAck(await this.requireGateway().request("agents.delete",{agentId:e,deleteFiles:!0}),e),await this.deps.identities.tombstone(r.runtimeAgentUid,r.runtimeLocalKey,t)}catch(e){throw new RuntimeCommandError("OUTCOME_UNKNOWN",`Interrupted ${r.command} recovery outcome unknown for operation ${r.operationId}: ${errorText(e)}`,void 0,{cause:e})}finally{let e;try{await i.stop()}catch(t){e=t}try{await this.deps.leases.release(t)}catch(t){e??=t}if(e)throw new RuntimeCommandError("OUTCOME_UNKNOWN",`Interrupted recovery lease finalization unknown: ${errorText(e)}`,void 0,{cause:e})}}}}function isRuntimeAgentMutation(e){return"agent.create"===e.name||"agent.update"===e.name||"agent.delete"===e.name||"agent.applyTemplate"===e.name}function servicePlatformAgentId(e){const t=e.verifiedMessageGrant?.claims;return"agent"===t?.scope?t.platformAgentId:void 0}function agentCoreResultHash(e,t){return createHash("sha256").update(runtimeHashPreimage(RUNTIME_HASH_DOMAINS.coreResult,{method:e.name.replace(".","/"),result:t})).digest("hex")}function agentProjectionRecoverySummary(e,t,r){const i="agent.update"===e.name||"agent.delete"===e.name?e.context.agent:createProjectionTarget(e,t),n=t;return Object.freeze({schemaVersion:1,operationId:e.context.operationId,method:e.name,runtimeEpoch:e.context.dispatchEpoch,runtimeAgentUid:i.runtimeAgentUid,runtimeLocalKey:i.runtimeLocalKey,coreResultHash:agentCoreResultHash(e,t),...servicePlatformAgentId(r)?{expectedPlatformAgentId:servicePlatformAgentId(r)}:{},..."agent.delete"===e.name&&Number.isSafeInteger(n?.removedBindings)?{removedBindings:n.removedBindings}:{}})}function createProjectionTarget(e,t){const r=t;if(!r||r.runtimeAgentUid!==e.params.runtimeAgentUid||"string"!=typeof r.runtimeLocalKey||!r.runtimeLocalKey)throw new RuntimeCommandError("OUTCOME_UNKNOWN","committed agent effect lacks a recoverable local identity");return{runtimeAgentUid:r.runtimeAgentUid,runtimeLocalKey:r.runtimeLocalKey}}function agentProjectionRecoveryResult(e){return Object.freeze({runtimeAgentUid:e.runtimeAgentUid,runtimeLocalKey:e.runtimeLocalKey,...void 0===e.removedBindings?{}:{removedBindings:e.removedBindings}})}function isProjectionIntent(e){return"projectionRevision"in e}function recoveryMetadata(e){const t=RUNTIME_METHOD_PARITY_MANIFEST.filter(t=>t.serviceAllowed&&t.canonicalCommand===e),r=t[0]?.recoveryClass;return r?{recoveryClass:r,projectionRequired:"mcp.set"===e||"mcp.unset"===e||t.some(e=>!0===e.projectionRequired)}:{recoveryClass:"imperative-ambiguous",projectionRequired:!1}}export function isDeterministicAgentsCreateParamsRejection(e){return e instanceof Error&&/^invalid agents\.create params\b/.test(e.message)}export function requireCommittedOpenClawDeleteAck(e,t){if(!e||"object"!=typeof e)throw new Error("gateway delete lacks the committed target acknowledgement");const r=e,i=Object.keys(r).every(e=>"ok"===e||"agentId"===e||"removedBindings"===e),n=void 0===r.removedBindings||Number.isSafeInteger(r.removedBindings)&&r.removedBindings>=0;if(!i||!0!==r.ok||r.agentId!==t||!n)throw new Error("gateway delete lacks the committed target acknowledgement");return{ok:!0,agentId:t,...void 0===r.removedBindings?{}:{removedBindings:r.removedBindings}}}function errorText(e){return e instanceof Error?e.message:String(e)}
1
+ import{createHash,randomUUID}from"node:crypto";import{mkdir,writeFile}from"node:fs/promises";import{homedir}from"node:os";import path,{join,relative,resolve}from"node:path";import{RUNTIME_HASH_DOMAINS,RUNTIME_METHOD_PARITY_MANIFEST,runtimeHashPreimage}from"@beeos-ai/beeos-types/runtime";import{reduceRuntimeRecoveryPolicy}from"@beeos-ai/beeos-types/runtime/internal";import{assertTrustedCoreExecutionAttempt}from"./core-execution-attempt-context.js";import{InMemoryRuntimeCommandJournal,RuntimeCommandJournalConflictError,runtimeCommandIsReplayableQuery,runtimeCommandResultHash}from"./runtime-command-journal.js";import{RuntimeProjectionTransportError}from"./agent-projection-worker.js";const DEFAULT_AGENT_ID="main",AGENT_VALID_ID_RE=/^[a-z0-9][a-z0-9_-]{0,63}$/i,AGENT_INVALID_CHARS_RE=/[^a-z0-9_-]+/g;export function normalizeRuntimeAgentLocalKey(e){const t=(e??"").trim();return t?AGENT_VALID_ID_RE.test(t)?t.toLowerCase():t.toLowerCase().replace(AGENT_INVALID_CHARS_RE,"-").replace(/^-+/,"").replace(/-+$/,"").slice(0,64)||"main":"main"}export async function awaitRuntimeProjection(e){const t=e;return await(t.awaitProjection?.()??Promise.resolve(e.getSnapshot().projection))}export function hasEffectReceiptHandoff(e){const t=e;return t.effectCommitted instanceof Promise&&"function"==typeof t.acknowledgeEffectReceipt}export class RuntimeCommandError extends Error{code;param;constructor(e,t,r,i){super(t,i),this.code=e,this.param=r,this.name="RuntimeCommandError"}}const emptyProgress=async function*(){};function deferred(){let e;return{promise:new Promise(t=>{e=t}),resolve:e}}async function waitForEffectReceipt(e,t){if(!t)return await e;if(t.aborted)throw new RuntimeCommandError("OUTCOME_UNKNOWN","MCP effect receipt was not durably published before the Service attempt ended");await new Promise((r,i)=>{const n=()=>i(new RuntimeCommandError("OUTCOME_UNKNOWN","MCP effect receipt was not durably published before the Service attempt ended"));t.addEventListener("abort",n,{once:!0}),e.then(()=>{t.removeEventListener("abort",n),r()},e=>{t.removeEventListener("abort",n),i(e)})})}export class OpenClawRuntimeCommandDispatcher{deps;logger;getOpenclawHome;createAgentId;journal;capabilities;constructor(e){this.deps=e,this.logger=e.logger??{},this.getOpenclawHome=e.getOpenclawHome??(()=>process.env.OPENCLAW_HOME||join(homedir(),".openclaw")),this.createAgentId=e.createAgentId??(()=>`agent-${randomUUID().replace(/-/g,"").slice(0,12)}`),this.journal=e.journal??new InMemoryRuntimeCommandJournal,this.capabilities=e.capabilities}setCapabilities(e){this.capabilities=e}dispatch(e,t){const r=e;assertTrustedCoreExecutionAttempt(t,{operationId:r.context.operationId,runtimeEpoch:r.context.dispatchEpoch});let i,n,a,o="running";const s=deferred(),c=deferred();let m;const d="service"===t.origin&&("mcp.set"===r.name||"mcp.unset"===r.name),p=this.executeWithLease(r,t,(e,t)=>{o="committed",m={operationId:r.context.operationId,method:r.name,record:t},s.resolve({...m,result:e,committedAt:t.updatedAt})},async()=>{d?await waitForEffectReceipt(c.promise,t.abortSignal):"acp"!==t.origin||"mcp.set"!==r.name&&"mcp.unset"!==r.name||!m?.record.mcpDesiredStateCommit||(await this.journal.releaseMcpLiveEffect({operationId:r.context.operationId,source:"acp_local"}),await this.journal.promoteMcpDesiredStateActive({operationId:r.context.operationId,source:"acp_local"}))},d,e=>{i=e},e=>{n=e}).then(e=>(o="committed",e),e=>{throw o=e instanceof RuntimeCommandError&&("OUTCOME_UNKNOWN"===e.code||"INSTANCE_EPOCH_MISMATCH"===e.code)?"outcome_unknown":"failed",s.resolve(void 0),e});return{operationId:r.context.operationId,getSnapshot:()=>({effectState:o,...i?{projection:i}:{}}),completion:p,progress:emptyProgress(),effectCommitted:s.promise,acknowledgeEffectReceipt:async e=>{if(!m||e.operationId!==m.operationId||e.method!==m.method)throw new RuntimeCommandError("OUTCOME_UNKNOWN","MCP effect receipt does not match this operation");if(!/^[a-f0-9]{64}$/.test(e.effectRecordHash))throw new RuntimeCommandError("OUTCOME_UNKNOWN","MCP effect receipt hash is invalid");await this.journal.releaseMcpLiveEffect({operationId:e.operationId,source:"service",effectRecordHash:e.effectRecordHash}),c.resolve()},acknowledgeTerminalReceipt:async e=>{if(!(d&&m&&e.operationId===m.operationId&&e.method===m.method&&/^[a-f0-9]{64}$/.test(e.effectRecordHash)&&e.terminalMessageId))throw new RuntimeCommandError("OUTCOME_UNKNOWN","MCP terminal receipt does not match this operation");await this.journal.promoteMcpDesiredStateActive({operationId:e.operationId,source:"service",effectRecordHash:e.effectRecordHash,terminalMessageId:e.terminalMessageId});const a=await this.requireReleasedRecord(e.operationId);if(void 0===a.result)throw new RuntimeCommandError("OUTCOME_UNKNOWN","active MCP desired state lost its committed result");await this.afterCommittedEffect(r,a.result,a,t,e=>{i=e},e=>{n=e})},cancel:async()=>"running"===o?{status:"cancel_requested"===await(this.capabilities?.cancel?.(r.context.operationId))?"cancel_requested":"not_cancellable"}:{status:"already_terminal"},awaitProjection:async()=>n?(a??=n(),await a):i}}async executeWithLease(e,t,r,i,n,a,o){const s=await this.deps.leases.claim(e.context.operationId,e.context.dispatchEpoch);if(s.runtimeEpoch!==e.context.dispatchEpoch)throw await this.deps.leases.release(s),new RuntimeCommandError("INSTANCE_EPOCH_MISMATCH","runtime operation lease epoch mismatch");const c=this.deps.leases.startHeartbeat(s);try{let c;await this.recoverInterruptedCreates(e.context.operationId);try{c=await this.journal.prepare({operationId:e.context.operationId,command:e.name,paramsHash:e.context.paramsHash,runtimeEpoch:s.runtimeEpoch})}catch(e){if(e instanceof RuntimeCommandJournalConflictError)throw new RuntimeCommandError("OUTCOME_UNKNOWN",e.message,void 0,{cause:e});throw e}if(("effect_committed"===c.phase||"projection_committed"===c.phase)&&void 0!==c.result){if(r(c.result,c),await i(),c=await this.requireReleasedRecord(c.operationId),void 0===c.result)throw new RuntimeCommandError("OUTCOME_UNKNOWN","committed result disappeared from command journal");return n||await this.afterCommittedEffect(e,c.result,c,t,a,o),c.result}if(("effect_committed"===c.phase||"projection_committed"===c.phase)&&"hash_only"===c.resultReceipt?.policy&&runtimeCommandIsReplayableQuery(e.name)){const n=await this.execute(e,s,t);if(runtimeCommandResultHash(n)!==c.resultReceipt.resultHash)throw new RuntimeCommandError("OUTCOME_UNKNOWN","redacted committed query result changed");return r(n,c),await i(),n}if(("effect_committed"===c.phase||"projection_committed"===c.phase)&&"hash_only"===c.resultReceipt?.policy){if(r(void 0,c),await i(),c=await this.requireReleasedRecord(c.operationId),!n){if(isRuntimeAgentMutation(e)&&(c.agentProjectionIntent||c.agentProjectionPreparationFailure)){const r=agentProjectionRecoveryResult(c.agentProjectionIntent??c.agentProjectionPreparationFailure);return await this.afterCommittedEffect(e,r,c,t,a,o),r}await this.runAfterEffect(e,void 0,c,t,a)}throw new RuntimeCommandError("OUTCOME_UNKNOWN","committed mutation result is redacted")}if("effect_committed"===c.phase||"projection_committed"===c.phase)throw new RuntimeCommandError("OUTCOME_UNKNOWN","committed result receipt is unavailable");if("outcome_unknown"===c.phase)throw new RuntimeCommandError("OUTCOME_UNKNOWN","previous execution outcome is unknown");if("started"===c.phase&&isRuntimeAgentMutation(e)){let n;try{n=await this.recoverStartedAgentMutation(e,s,t)}catch(e){throw await this.markJournalUnknown(c),e instanceof RuntimeCommandError?e:new RuntimeCommandError("OUTCOME_UNKNOWN",`agent desired-state recovery could not prove its effect: ${errorText(e)}`,void 0,{cause:e})}const m=this.deps.projection?await this.prepareAgentProjectionCommit(e,n,t):void 0,d=await this.journal.transition({operationId:c.operationId,expectedRevision:c.revision,phase:"effect_committed",result:n,...m?{agentProjection:m}:{}});return r(n,d),await i(),await this.afterCommittedEffect(e,n,d,t,a,o),this.journal.compact().catch(()=>{}),n}if("started"===c.phase&&"outcome_unknown"===this.recoveryDecision(e.name,c.phase))throw await this.markJournalUnknown(c),new RuntimeCommandError("OUTCOME_UNKNOWN","imperative action crossed its execution boundary");if("failed"===c.phase)throw new RuntimeCommandError("EXECUTION_FAILED","previous execution failed");"prepared"===c.phase&&(c=await this.journal.transition({operationId:c.operationId,expectedRevision:c.revision,phase:"started"}));let m=!1;try{const d=await this.execute(e,s,t);m=!0;const p=isRuntimeAgentMutation(e)&&this.deps.projection?await this.prepareAgentProjectionCommit(e,d,t):void 0;let u;try{const e=await this.requireReleasedRecord(c.operationId);u=await this.journal.transition({operationId:e.operationId,expectedRevision:e.revision,phase:"effect_committed",result:d,...p?{agentProjection:p}:{}})}catch(e){throw new RuntimeCommandError("OUTCOME_UNKNOWN",`effect completed before its journal commit: ${errorText(e)}`,void 0,{cause:e})}return r(d,u),await i(),u=await this.requireReleasedRecord(u.operationId),n||await this.afterCommittedEffect(e,d,u,t,a,o),this.journal.compact().catch(()=>{}),d}catch(t){if(m)throw t;const r=t instanceof RuntimeCommandError&&("OUTCOME_UNKNOWN"===t.code||"INSTANCE_EPOCH_MISMATCH"===t.code);if(r&&"clear-saga"===recoveryMetadata(e.name).recoveryClass)throw t;try{await this.journal.transition({operationId:c.operationId,expectedRevision:c.revision,phase:r?"outcome_unknown":"failed",errorCode:r?"OUTCOME_UNKNOWN":"EXECUTION_FAILED"})}catch(e){throw new RuntimeCommandError("OUTCOME_UNKNOWN",`command and journal outcome unknown: ${errorText(e)}`,void 0,{cause:t})}throw t}}finally{try{await c.stop(),await this.deps.leases.release(s)}catch(e){throw new RuntimeCommandError("OUTCOME_UNKNOWN",`runtime operation lease finalization unknown: ${errorText(e)}`,void 0,{cause:e})}}}async execute(e,t,r){switch(e.name){case"agent.create":return await this.createAgent(e,t,r);case"agent.update":return await this.updateAgent(e);case"agent.delete":return await this.deleteAgent(e,t);case"agent.applyTemplate":return await this.applyTemplate(e,t,r)}if(!this.capabilities)throw new RuntimeCommandError("INVALID_PARAMS",`Unsupported runtime command: ${e.name}`);return await this.capabilities.execute(e,t,r)}async recoverStartedAgentMutation(e,t,r){if("agent.create"===e.name||"agent.applyTemplate"===e.name){let i=await this.bindCreateOperation(e,t,!1);if(i||(i=await this.findCreateRecoveryIdentity(e,!1),"allocated"===i.state&&void 0===i.createOperation?.paramsHash&&await this.bindCreateOperation(e,t,!1),i=await this.findCreateRecoveryIdentity(e)),"active"===i.state)return await this.recoverActiveCreateEffect(i);if(await this.runtimeAgentExists(i.observedRuntimeLocalKey??i.runtimeLocalKey)){const n={runtimeLocalKey:i.observedRuntimeLocalKey??i.runtimeLocalKey,workspacePath:join(this.getOpenclawHome(),`workspace-${i.runtimeLocalKey}`)};return"agent.create"===e.name?await this.completeCreatedAgent(e,t,r,n):await this.completeTemplateAgent(e,t,r,n)}return await this.execute(e,t,r)}const i=e.context.agent,n=await this.runtimeAgentExists(i.runtimeLocalKey);if("agent.delete"===e.name&&!n)return await this.deps.identities.commitDeleteTombstone(i.runtimeAgentUid,i.runtimeLocalKey,t),{runtimeAgentUid:i.runtimeAgentUid,runtimeLocalKey:i.runtimeLocalKey};if("agent.update"===e.name&&!n)throw new RuntimeCommandError("OUTCOME_UNKNOWN","update recovery target is absent");return await this.execute(e,t,r)}async runtimeAgentExists(e){const t=await this.requireGateway().request("agents.list",{});if(!Array.isArray(t?.agents))throw new RuntimeCommandError("OUTCOME_UNKNOWN","runtime agent directory query is incomplete");return t.agents.some(t=>null!==t&&"object"==typeof t&&(t.id===e||t.name===e))}async findCreateRecoveryIdentity(e,t=!0){const r={operationId:e.context.operationId,command:e.name,runtimeEpoch:e.context.dispatchEpoch,paramsHash:e.context.paramsHash,runtimeAgentUid:e.params.runtimeAgentUid},i=void 0!==this.deps.identities.findCreateOperation,n=i?await this.deps.identities.findCreateOperation(r):(await this.deps.identities.pendingAllocations()).filter(e=>e.operationId===r.operationId&&e.command===r.command&&e.runtimeEpoch===r.runtimeEpoch&&e.runtimeAgentUid===r.runtimeAgentUid);if(1!==n.length)throw new RuntimeCommandError("OUTCOME_UNKNOWN","create recovery identity binding is missing or ambiguous");const a=n[0];if(i&&(!a.createOperation||t&&a.createOperation.paramsHash!==r.paramsHash||a.createOperation.operationId!==r.operationId||a.createOperation.command!==r.command||a.createOperation.runtimeEpoch!==r.runtimeEpoch))throw new RuntimeCommandError("OUTCOME_UNKNOWN","create recovery identity hash binding conflicts");return a}async bindCreateOperation(e,t,r=!0){if(!this.deps.identities.findCreateOperation||!this.deps.identities.bindCreateOperation)return;const i={operationId:e.context.operationId,command:e.name,runtimeEpoch:e.context.dispatchEpoch,paramsHash:e.context.paramsHash,runtimeAgentUid:e.params.runtimeAgentUid};try{let n=await this.deps.identities.findCreateOperation(i);if(0===n.length){if(!r)throw new RuntimeCommandError("OUTCOME_UNKNOWN","create recovery identity binding is missing");if((await this.deps.identities.allocateCreate(t,e.name,e.params.runtimeAgentUid)).runtimeAgentUid!==e.params.runtimeAgentUid)throw new RuntimeCommandError("OUTCOME_UNKNOWN","create allocation UID does not match the canonical command");n=await this.deps.identities.findCreateOperation(i)}if(1!==n.length)throw new RuntimeCommandError("OUTCOME_UNKNOWN","create identity binding is missing or ambiguous");const a=n[0],o=await this.deps.identities.bindCreateOperation(a.runtimeAgentUid,a.runtimeLocalKey,{operationId:e.context.operationId,command:e.name,runtimeEpoch:e.context.dispatchEpoch,paramsHash:e.context.paramsHash});if(!o.createOperation||o.runtimeAgentUid!==e.params.runtimeAgentUid||o.createOperation.operationId!==i.operationId||o.createOperation.command!==i.command||o.createOperation.runtimeEpoch!==i.runtimeEpoch||o.createOperation.paramsHash!==i.paramsHash)throw new Error("create operation identity binding changed during compare-and-set");if("tombstoned"===o.state)throw new Error("create operation identity is tombstoned");return o}catch(e){if(e instanceof RuntimeCommandError)throw e;throw new RuntimeCommandError("OUTCOME_UNKNOWN",`create operation identity binding is corrupt: ${errorText(e)}`,void 0,{cause:e})}}async recoverActiveCreateEffect(e){const t=e.observedRuntimeLocalKey??e.runtimeLocalKey;if(!await this.runtimeAgentExists(t))throw new RuntimeCommandError("OUTCOME_UNKNOWN","active create identity is absent from the runtime directory");return{runtimeAgentUid:e.runtimeAgentUid,runtimeLocalKey:t,workspacePath:join(this.getOpenclawHome(),`workspace-${e.runtimeLocalKey}`)}}async markJournalUnknown(e){try{await this.journal.transition({operationId:e.operationId,expectedRevision:e.revision,phase:"outcome_unknown",errorCode:"OUTCOME_UNKNOWN"})}catch{}}async requireReleasedRecord(e){const t=await this.journal.get(e);if(!t)throw new RuntimeCommandError("OUTCOME_UNKNOWN","command journal record disappeared");return t}async afterCommittedEffect(e,t,r,i,n,a){if(!isRuntimeAgentMutation(e))return void await this.runAfterEffect(e,t,r,i,n);const o=await this.prepareAgentProjection(e,t,r,i,n);o&&(a(o),"acp"===i.origin&&o().catch(e=>this.logger.warn?.(`[runtime-core] deferred ACP agent projection failed: ${errorText(e)}`)))}async prepareAgentProjectionCommit(e,t,r){const i=agentProjectionRecoverySummary(e,t,r);try{const r=await this.deps.projection.prepareCommittedMutation({command:e,result:t,...i.expectedPlatformAgentId?{expectedPlatformAgentId:i.expectedPlatformAgentId}:{}});return Object.freeze({...r,...i})}catch(e){const t=!(e instanceof RuntimeProjectionTransportError)||e.retryable;return this.logger.warn?.(`[runtime-core] agent projection preparation ${t?"deferred":"blocked"}: ${errorText(e)}`),Object.freeze({...i,state:t?"pending":"blocked",errorKind:t?"snapshot_retryable":"snapshot_nonretryable"})}}async prepareAgentProjection(e,t,r,i,n){if("projection_committed"===r.phase)return n(r.projectionReceipt?.snapshot??Object.freeze({state:"synced",revision:`${Math.max(0,r.revision-1)}`})),r.agentProjectionIntent&&await this.compactAgentProjectionReference(r.agentProjectionIntent),void await this.runPostCommit(e,t);if("effect_committed"!==r.phase||!this.deps.projection)return;let a=r,o=a.agentProjectionIntent;const s=a.agentProjectionPreparationFailure;if(o&&"blocked"===a.agentProjectionTerminal?.state)return n(Object.freeze({state:"blocked",revision:a.agentProjectionTerminal.revision})),await this.compactAgentProjectionReference(o),void await this.runPostCommit(e,t);let c;return o&&n(Object.freeze({state:"pending",revision:o.projectionRevision})),async()=>(c??=(async()=>{if(!o){if(!s||"blocked"===s.state)return Object.freeze({state:"blocked",revision:`${Math.max(0,a.revision)}`});const t=await this.prepareAgentProjectionCommit(e,agentProjectionRecoveryResult(s),i);if(!isProjectionIntent(t))return"blocked"===t.state?Object.freeze({state:"blocked",revision:`${Math.max(0,a.revision)}`}):Object.freeze({state:"pending",revision:`${Math.max(0,a.revision)}`});o=t;try{a=await this.journal.recordAgentProjectionIntent({operationId:a.operationId,expectedRevision:a.revision,intent:o})}catch(e){return void this.logger.warn?.(`[runtime-core] agent projection reservation recovery deferred: ${errorText(e)}`)}n(Object.freeze({state:"pending",revision:o.projectionRevision}))}const r=Object.freeze({state:"pending",revision:o.projectionRevision});let c;try{c=await this.deps.projection.applyCommittedMutation(o)}catch(e){this.logger.warn?.(`[runtime-core] agent projection receipt deferred: ${errorText(e)}`),c=r}if(c.revision!==o.projectionRevision&&(c=Object.freeze({state:"blocked",revision:o.projectionRevision})),"synced"===c.state)try{a=await this.journal.transition({operationId:a.operationId,expectedRevision:a.revision,phase:"projection_committed",projectionSnapshot:c}),await this.compactAgentProjectionReference(o)}catch(e){this.logger.warn?.(`[runtime-core] projection phase commit deferred: ${errorText(e)}`),c=Object.freeze({state:"pending",revision:o.projectionRevision})}if("blocked"===c.state)try{a=await this.journal.recordAgentProjectionBlocked({operationId:a.operationId,expectedRevision:a.revision,revision:o.projectionRevision}),await this.compactAgentProjectionReference(o)}catch(e){this.logger.warn?.(`[runtime-core] blocked projection terminal commit deferred: ${errorText(e)}`),c=r}return"synced"===c.state&&c.platformAgentId&&"projection_committed"===a.phase&&this.reportTemplateOriginBestEffort(e,c.platformAgentId),n(c),await this.runPostCommit(e,t),c})(),await c)}async compactAgentProjectionReference(e){if(this.deps.projection)try{await this.deps.projection.compactCommittedMutation(e)}catch(e){this.logger.warn?.(`[runtime-core] projection reference compaction deferred: ${errorText(e)}`)}}reportTemplateOriginBestEffort(e,t){if("agent.applyTemplate"!==e.name||!this.deps.templateOriginReporter)return;const r=e.params.templateId;r&&this.deps.templateOriginReporter.report({platformAgentId:t,templateId:r}).catch(e=>{this.logger.warn?.(`[runtime-core] active template origin report failed, client fallback remains: ${errorText(e)}`)})}async runAfterEffect(e,t,r,i,n){if("projection_committed"===r.phase)return n(r.projectionReceipt?.snapshot??Object.freeze({state:"synced",revision:`${Math.max(0,r.revision-1)}`})),void await this.runPostCommit(e,t);const a="effect_committed"===r.phase&&await this.runProjection(e,t,i),o=!0===a?Object.freeze({state:"synced",revision:`${r.revision}`}):a&&"object"==typeof a?Object.freeze({...a,revision:`${r.revision}`}):"session.setModel"===e.name?Object.freeze({state:"pending",revision:`${r.revision}`}):void 0;if("synced"===o?.state)try{const e=o;await this.journal.transition({operationId:r.operationId,expectedRevision:r.revision,phase:"projection_committed",projectionSnapshot:e}),n(o)}catch(e){return this.logger.warn?.(`[runtime-core] projection phase commit deferred: ${errorText(e)}`),void n(Object.freeze({revision:o.revision,state:"pending"}))}else o&&n(o);await this.runPostCommit(e,t)}async runPostCommit(e,t){if(this.capabilities?.afterCommit)try{await this.capabilities.afterCommit(e,t)}catch(e){this.logger.warn?.(`[runtime-core] post-commit action failed: ${errorText(e)}`)}}async runProjection(e,t,r){if(!recoveryMetadata(e.name).projectionRequired)return!1;try{return!isRuntimeAgentMutation(e)&&(await(this.capabilities?.projectCommitted?.(e,t,r))??!1)}catch(e){return this.logger.warn?.(`[runtime-core] agent projection deferred: ${errorText(e)}`),!1}}recoveryDecision(e,t){const r=recoveryMetadata(e);return reduceRuntimeRecoveryPolicy({...r,messageServicePhase:"queued",executionBoundaryCrossed:"prepared"!==t,lineageContinuity:"continuous",journalState:"available",journalPhase:t})}requireGateway(){const e=this.deps.getGateway();if(!e?.isConnected)throw new RuntimeCommandError("GATEWAY_NOT_CONNECTED","Gateway not connected");return e}async createAgentShell(e,t,r,i,n,a){const o=this.requireGateway(),s=(await this.deps.identities.pendingAllocations()).find(t=>t.runtimeAgentUid===e),c=a??s?.runtimeLocalKey??this.createAgentId(),m=join(this.getOpenclawHome(),`workspace-${c}`);let d;try{d=await o.request("agents.create",{name:c,workspace:m,...n?{model:n}:{},...i?.emoji?{emoji:i.emoji}:{},...i?.avatar?{avatar:i.avatar}:{}})}catch(r){if(isDeterministicAgentsCreateParamsRejection(r)&&(void 0!==a||void 0!==s))try{await this.deps.identities.tombstone(e,c,t)}catch(e){this.logger.warn?.(`[runtime-core] rejected-create allocation cleanup deferred: ${errorText(e)}`)}throw r}const p=d?.agentId??c,u=d?.workspace??m;if(await this.deps.identities.observeEffectKey(e,c,p,t),p!==c)throw new RuntimeCommandError("OUTCOME_UNKNOWN",`Create agent outcome unknown: gateway changed reserved local key to ${p}`);try{await o.request("agents.update",{agentId:p,name:r})}catch(e){this.logger.warn?.(`[runtime-core] display-name update failed agent=${p}: ${errorText(e)}`)}return{runtimeLocalKey:p,workspacePath:u}}async createAgent(e,t,r){const i=e.params.name.trim();if(!i)throw new RuntimeCommandError("INVALID_PARAMS","Missing agent name");try{const n=await this.bindCreateOperation(e,t);if("active"===n?.state)return await this.recoverActiveCreateEffect(n);const a=await this.createAgentShell(e.params.runtimeAgentUid,t,i,e.params.identity,e.params.defaultModelId,n?.runtimeLocalKey);return await this.completeCreatedAgent(e,t,r,a)}catch(e){throw this.executionError("Create agent",e)}}async updateAgent(e){const{runtimeAgentUid:t,runtimeLocalKey:r}=e.context.agent;if(!r.trim())throw new RuntimeCommandError("MISSING_PARAM","Missing agentId","agentId");const i=e.params,n=i.name?.trim(),a=i.defaultModelId?.trim(),o=i.identity?.emoji?.trim(),s=i.identity?.avatar?.trim();if(!(n||a||o||s||void 0!==i.description||void 0!==i.version||void 0!==i.capabilities||void 0!==i.skills||void 0!==i.avatarUrl||void 0!==i.homepage||void 0!==i.status||void 0!==i.identity?.vibe))throw new RuntimeCommandError("INVALID_PARAMS","No updatable fields provided (name/model/identity)");try{return await this.deps.identities.requireActive(t,r),await this.requireGateway().request("agents.update",{agentId:r,...n?{name:n}:{},...a?{model:a}:{},...o?{emoji:o}:{},...s?{avatar:s}:{},...i.identity?.vibe?{vibe:i.identity.vibe}:{},...void 0!==i.description?{description:i.description}:{},...void 0!==i.version?{version:i.version}:{},...void 0!==i.capabilities?{capabilities:i.capabilities}:{},...void 0!==i.skills?{skills:i.skills}:{},...void 0!==i.avatarUrl?{avatarUrl:i.avatarUrl}:{},...void 0!==i.homepage?{homepage:i.homepage}:{},...void 0!==i.status?{status:i.status}:{}}),{runtimeAgentUid:t,runtimeLocalKey:r}}catch(e){throw this.executionError("Update agent",e)}}async deleteAgent(e,t){const{runtimeAgentUid:r,runtimeLocalKey:i}=e.context.agent;if(!i.trim())throw new RuntimeCommandError("MISSING_PARAM","Missing agentId","agentId");if("main"===normalizeRuntimeAgentLocalKey(i))throw new RuntimeCommandError("INVALID_PARAMS",'"main" cannot be deleted; delete the instance instead');try{if("tombstoned"===await this.deps.identities.requireDeleteTarget(r,i))return{runtimeAgentUid:r,runtimeLocalKey:i};if(!await this.runtimeAgentExists(i))return await this.deps.identities.commitDeleteTombstone(r,i,t),{runtimeAgentUid:r,runtimeLocalKey:i};const n=requireCommittedOpenClawDeleteAck(await this.requireGateway().request("agents.delete",{agentId:i,deleteFiles:e.params.deleteFiles??!0}),i);try{await this.deps.identities.commitDeleteTombstone(r,i,t)}catch(e){throw new RuntimeCommandError("OUTCOME_UNKNOWN",`Delete agent outcome unknown: identity tombstone failed: ${errorText(e)}`,void 0,{cause:e})}return{runtimeAgentUid:r,runtimeLocalKey:i,...void 0===n.removedBindings?{}:{removedBindings:n.removedBindings}}}catch(e){throw this.executionError("Delete agent",e)}}async applyTemplate(e,t,r){const i=e.params.name.trim();if(!i)throw new RuntimeCommandError("INVALID_PARAMS","Missing agent name");this.requireGateway();try{const n=await this.bindCreateOperation(e,t);if("active"===n?.state)return await this.recoverActiveCreateEffect(n);const a=await r.resources.readVerifiedArtifact(e.params.artifact),o=await this.createAgentShell(e.params.runtimeAgentUid,t,i,e.params.identity,e.params.defaultModelId,n?.runtimeLocalKey);return await this.completeTemplateAgent(e,t,r,o,a)}catch(e){throw this.executionError("Apply template",e)}}async completeCreatedAgent(e,t,r,i){for(const t of e.params.bootstrapFiles??[]){if("IDENTITY.md"===t.name){this.logger.warn?.("[runtime-core] agent.create ignoring IDENTITY.md; identity is structured");continue}const e=await r.resources.readContent(t.contentRef);try{await this.requireGateway().request("agents.files.set",{agentId:i.runtimeLocalKey,name:t.name,content:e})}catch(e){this.logger.warn?.(`[runtime-core] bootstrap write failed file=${t.name}: ${errorText(e)}`)}}try{await this.deps.identities.activate(e.params.runtimeAgentUid,i.runtimeLocalKey,t)}catch(r){throw await this.compensateCreatedAgent(e.params.runtimeAgentUid,i.runtimeLocalKey,t,r,"Create agent"),new RuntimeCommandError("EXECUTION_FAILED",`Create agent failed after compensation: ${errorText(r)}`)}return{runtimeAgentUid:e.params.runtimeAgentUid,runtimeLocalKey:i.runtimeLocalKey,workspacePath:i.workspacePath}}async completeTemplateAgent(e,t,r,i,n){const a=n??await r.resources.readVerifiedArtifact(e.params.artifact);try{for(const[e,t]of a.workspaceFiles){const r=resolve(i.workspacePath,e),n=relative(i.workspacePath,r);if(""===n||n.startsWith("..")||path.isAbsolute(n))throw new Error(`refusing to write outside workspace: ${e}`);await(this.deps.mkdirWorkspace??mkdir)(path.dirname(r),{recursive:!0}),await(this.deps.writeWorkspaceFile??writeFile)(r,t)}await this.deps.identities.activate(e.params.runtimeAgentUid,i.runtimeLocalKey,t)}catch(r){throw await this.compensateCreatedAgent(e.params.runtimeAgentUid,i.runtimeLocalKey,t,r,"Apply template"),new RuntimeCommandError("EXECUTION_FAILED",`Apply template failed after compensation: ${errorText(r)}`,void 0,{cause:r})}return{runtimeAgentUid:e.params.runtimeAgentUid,runtimeLocalKey:i.runtimeLocalKey,workspacePath:i.workspacePath}}executionError(e,t){return t instanceof RuntimeCommandError?t:(this.logger.error?.(`[runtime-core] ${e.toLowerCase()} failed: ${errorText(t)}`),new RuntimeCommandError("EXECUTION_FAILED",`${e} failed: ${errorText(t)}`,void 0,{cause:t}))}async compensateCreatedAgent(e,t,r,i,n){try{const i=this.requireGateway();requireCommittedOpenClawDeleteAck(await i.request("agents.delete",{agentId:t,deleteFiles:!0}),t),await this.deps.identities.tombstone(e,t,r)}catch(e){throw new RuntimeCommandError("OUTCOME_UNKNOWN",`${n} outcome unknown after ${errorText(i)}; compensation unproven: ${errorText(e)}`,void 0,{cause:e})}}async recoverInterruptedCreates(e){const t=await this.deps.identities.pendingAllocations();for(const r of t){if(r.operationId===e)continue;const t=await this.deps.leases.claimExpiredForRecovery(r.operationId);if(!t)continue;const i=this.deps.leases.startHeartbeat(t);try{await this.deps.identities.adoptRecoveryLease(r.runtimeAgentUid,r.runtimeLocalKey,t);const e=r.observedRuntimeLocalKey;if(!e){const e=r.createOperation?.operationId??r.operationId,i=await this.journal.get(e);if("failed"===i?.phase&&i.command===r.command&&!await this.runtimeAgentExists(r.runtimeLocalKey)){await this.deps.identities.tombstone(r.runtimeAgentUid,r.runtimeLocalKey,t);continue}throw new Error("no persisted gateway effect key; absence cannot be proven")}requireCommittedOpenClawDeleteAck(await this.requireGateway().request("agents.delete",{agentId:e,deleteFiles:!0}),e),await this.deps.identities.tombstone(r.runtimeAgentUid,r.runtimeLocalKey,t)}catch(e){throw new RuntimeCommandError("OUTCOME_UNKNOWN",`Interrupted ${r.command} recovery outcome unknown for operation ${r.operationId}: ${errorText(e)}`,void 0,{cause:e})}finally{let e;try{await i.stop()}catch(t){e=t}try{await this.deps.leases.release(t)}catch(t){e??=t}if(e)throw new RuntimeCommandError("OUTCOME_UNKNOWN",`Interrupted recovery lease finalization unknown: ${errorText(e)}`,void 0,{cause:e})}}}}function isRuntimeAgentMutation(e){return"agent.create"===e.name||"agent.update"===e.name||"agent.delete"===e.name||"agent.applyTemplate"===e.name}function servicePlatformAgentId(e){const t=e.verifiedMessageGrant?.claims;return"agent"===t?.scope?t.platformAgentId:void 0}function agentCoreResultHash(e,t){return createHash("sha256").update(runtimeHashPreimage(RUNTIME_HASH_DOMAINS.coreResult,{method:e.name.replace(".","/"),result:t})).digest("hex")}function agentProjectionRecoverySummary(e,t,r){const i="agent.update"===e.name||"agent.delete"===e.name?e.context.agent:createProjectionTarget(e,t),n=t;return Object.freeze({schemaVersion:1,operationId:e.context.operationId,method:e.name,runtimeEpoch:e.context.dispatchEpoch,runtimeAgentUid:i.runtimeAgentUid,runtimeLocalKey:i.runtimeLocalKey,coreResultHash:agentCoreResultHash(e,t),...servicePlatformAgentId(r)?{expectedPlatformAgentId:servicePlatformAgentId(r)}:{},..."agent.delete"===e.name&&Number.isSafeInteger(n?.removedBindings)?{removedBindings:n.removedBindings}:{}})}function createProjectionTarget(e,t){const r=t;if(!r||r.runtimeAgentUid!==e.params.runtimeAgentUid||"string"!=typeof r.runtimeLocalKey||!r.runtimeLocalKey)throw new RuntimeCommandError("OUTCOME_UNKNOWN","committed agent effect lacks a recoverable local identity");return{runtimeAgentUid:r.runtimeAgentUid,runtimeLocalKey:r.runtimeLocalKey}}function agentProjectionRecoveryResult(e){return Object.freeze({runtimeAgentUid:e.runtimeAgentUid,runtimeLocalKey:e.runtimeLocalKey,...void 0===e.removedBindings?{}:{removedBindings:e.removedBindings}})}function isProjectionIntent(e){return"projectionRevision"in e}function recoveryMetadata(e){const t=RUNTIME_METHOD_PARITY_MANIFEST.filter(t=>t.serviceAllowed&&t.canonicalCommand===e),r=t[0]?.recoveryClass;return r?{recoveryClass:r,projectionRequired:"mcp.set"===e||"mcp.unset"===e||t.some(e=>!0===e.projectionRequired)}:{recoveryClass:"imperative-ambiguous",projectionRequired:!1}}export function isDeterministicAgentsCreateParamsRejection(e){return e instanceof Error&&/^invalid agents\.create params\b/.test(e.message)}export function requireCommittedOpenClawDeleteAck(e,t){if(!e||"object"!=typeof e)throw new Error("gateway delete lacks the committed target acknowledgement");const r=e,i=Object.keys(r).every(e=>"ok"===e||"agentId"===e||"removedBindings"===e),n=void 0===r.removedBindings||Number.isSafeInteger(r.removedBindings)&&r.removedBindings>=0;if(!i||!0!==r.ok||r.agentId!==t||!n)throw new Error("gateway delete lacks the committed target acknowledgement");return{ok:!0,agentId:t,...void 0===r.removedBindings?{}:{removedBindings:r.removedBindings}}}function errorText(e){return e instanceof Error?e.message:String(e)}
@@ -1 +1 @@
1
- import{randomUUID}from"node:crypto";import{mkdir,readFile,rename,stat,unlink,readdir}from"node:fs/promises";import{writeFile}from"node:fs/promises";import{homedir}from"node:os";import{join}from"node:path";const CACHE_TTL_MS=864e5,ENRICHMENT_CACHE_TTL_MS=2592e6,ENRICHMENT_PREFIX="enrich_",CACHE_VERSION=1;function cacheDir(){return join(process.env.OPENCLAW_HOME||join(homedir(),".openclaw"),"beeos-claw","upload_meta")}function safeToolCallId(e){return e.replace(/[^a-zA-Z0-9._-]/g,"_")}function cachePath(e){return join(cacheDir(),`${safeToolCallId(e)}.json`)}async function safeUnlink(e){try{await unlink(e)}catch{}}export async function writeUploadToolResult(e,t,a){await mkdir(cacheDir(),{recursive:!0,mode:448});const n={version:1,createdAt:Date.now(),toolCallId:e,toolName:t,result:a},r=async e=>{const t=`${e}.${randomUUID()}.tmp`;await writeFile(t,JSON.stringify(n),{encoding:"utf8",mode:384}),await rename(t,e)};await r(cachePath(e)),"beeos_upload_file"!==t&&"beeos_desktop_screenshot"!==t||await r(cachePath("latest_beeos_upload_file"))}export async function readUploadToolResult(e){const t=cachePath(e);let a,n,r;try{a=await stat(t)}catch{return}if(Date.now()-a.mtimeMs>=864e5)await safeUnlink(t);else{try{n=await readFile(t,"utf8")}catch{return}try{r=JSON.parse(n)}catch{return void await safeUnlink(t)}if(r&&1===r.version&&"number"==typeof r.createdAt&&!(Date.now()-r.createdAt>=864e5))return r.result;await safeUnlink(t)}}export function uploadCacheIdCandidates(e){const t=e.trim();if(!t)return[];const a=t.startsWith("tool:")?t.slice(5):t,n=[t,a,safeToolCallId(t),safeToolCallId(a)];return[...new Set(n.filter(e=>e.length>0))]}export async function resolveUploadToolResultForEnrichment(e,t="beeos_upload_file",a){const n=Math.max(1,a?.attempts??6),r=Math.max(0,a?.delayMs??40),o=Math.max(0,a?.latestMaxAgeMs??5e3),i=uploadCacheIdCandidates(e);for(let e=0;e<n;e++){for(const e of i){const t=await readUploadToolResult(e);if(null!=t)return t}if(e>=Math.max(1,n-2)&&o>0){const e=await readLatestUploadToolResult(t,o);if(null!=e)return e}e+1<n&&r>0&&await new Promise(t=>setTimeout(t,r*(e+1)))}}export async function readLatestUploadToolResult(e,t=12e4){let a;try{a=await readdir(cacheDir())}catch{return}const n=Date.now();let r;for(const o of a){if(!o.endsWith(".json")||o.startsWith("enrich_"))continue;const a=join(cacheDir(),o);let i,c;try{i=await readFile(a,"utf8")}catch{continue}try{c=JSON.parse(i)}catch{continue}!c||1!==c.version||c.toolName!==e||"number"!=typeof c.createdAt||n-c.createdAt>=t||n-c.createdAt>=864e5||(!r||c.createdAt>r.createdAt)&&(r={createdAt:c.createdAt,result:c.result})}return r?.result}function enrichmentCachePath(e){return join(cacheDir(),`enrich_${safeToolCallId(e)}.json`)}export async function writeEnrichmentCache(e,t){if(0===t.length)return;await mkdir(cacheDir(),{recursive:!0,mode:448});const a={version:1,createdAt:Date.now(),toolCallId:e,resourceLinks:t},n=enrichmentCachePath(e),r=`${n}.${randomUUID()}.tmp`;await writeFile(r,JSON.stringify(a),{encoding:"utf8",mode:384}),await rename(r,n)}export async function readEnrichmentCache(e){const t=enrichmentCachePath(e);let a,n,r;try{a=await stat(t)}catch{return}if(Date.now()-a.mtimeMs>=2592e6)await safeUnlink(t);else{try{n=await readFile(t,"utf8")}catch{return}try{r=JSON.parse(n)}catch{return void await safeUnlink(t)}if(r&&1===r.version&&"number"==typeof r.createdAt&&Array.isArray(r.resourceLinks)&&!(Date.now()-r.createdAt>=2592e6))return r.resourceLinks;await safeUnlink(t)}}export async function cleanupUploadToolResultCache(){let e;try{e=await readdir(cacheDir(),{withFileTypes:!0})}catch{return 0}const t=Date.now();let a=0;for(const n of e){if(!n.isFile())continue;if(!n.name.endsWith(".json")&&!n.name.endsWith(".tmp"))continue;const e=join(cacheDir(),n.name),r=n.name.startsWith("enrich_")?2592e6:864e5;try{t-(await stat(e)).mtimeMs>=r&&(await unlink(e),a++)}catch{}}return a}
1
+ import{randomUUID}from"node:crypto";import{mkdir,readFile,rename,stat,unlink,readdir}from"node:fs/promises";import{writeFile}from"node:fs/promises";import{homedir}from"node:os";import{join}from"node:path";const CACHE_TTL_MS=864e5,ENRICHMENT_CACHE_TTL_MS=2592e6,ENRICHMENT_PREFIX="enrich_",CACHE_VERSION=1;function cacheDir(){return join(process.env.OPENCLAW_HOME||join(homedir(),".openclaw"),"beeos-claw","upload_meta")}function safeToolCallId(e){return e.replace(/[^a-zA-Z0-9._-]/g,"_")}function cachePath(e){return join(cacheDir(),`${safeToolCallId(e)}.json`)}async function safeUnlink(e){try{await unlink(e)}catch{}}export async function writeUploadToolResult(e,t,a){await mkdir(cacheDir(),{recursive:!0,mode:448});const n={version:1,createdAt:Date.now(),toolCallId:e,toolName:t,result:a},r=async e=>{const t=`${e}.${randomUUID()}.tmp`;await writeFile(t,JSON.stringify(n),{encoding:"utf8",mode:384}),await rename(t,e)},o=uploadCacheIdCandidates(e);for(const t of o.length>0?o:[e])await r(cachePath(t))}export async function readUploadToolResult(e){const t=cachePath(e);let a,n,r;try{a=await stat(t)}catch{return}if(Date.now()-a.mtimeMs>=864e5)await safeUnlink(t);else{try{n=await readFile(t,"utf8")}catch{return}try{r=JSON.parse(n)}catch{return void await safeUnlink(t)}if(r&&1===r.version&&"number"==typeof r.createdAt&&!(Date.now()-r.createdAt>=864e5))return r.result;await safeUnlink(t)}}export function uploadCacheIdCandidates(e){const t=e.trim();if(!t)return[];const a=t.startsWith("tool:")?t.slice(5):t,n=[t,a,safeToolCallId(t),safeToolCallId(a)];return[...new Set(n.filter(e=>e.length>0))]}export async function resolveUploadToolResultForEnrichment(e,t="beeos_upload_file",a){const n=Math.max(1,a?.attempts??6),r=Math.max(0,a?.delayMs??40),o=uploadCacheIdCandidates(e);for(let e=0;e<n;e++){for(const e of o){const t=await readUploadToolResult(e);if(null!=t)return t}e+1<n&&r>0&&await new Promise(t=>setTimeout(t,r*(e+1)))}}function enrichmentCachePath(e){return join(cacheDir(),`enrich_${safeToolCallId(e)}.json`)}export async function writeEnrichmentCache(e,t){if(0===t.length)return;await mkdir(cacheDir(),{recursive:!0,mode:448});const a={version:1,createdAt:Date.now(),toolCallId:e,resourceLinks:t},n=enrichmentCachePath(e),r=`${n}.${randomUUID()}.tmp`;await writeFile(r,JSON.stringify(a),{encoding:"utf8",mode:384}),await rename(r,n)}export async function readEnrichmentCache(e){const t=enrichmentCachePath(e);let a,n,r;try{a=await stat(t)}catch{return}if(Date.now()-a.mtimeMs>=2592e6)await safeUnlink(t);else{try{n=await readFile(t,"utf8")}catch{return}try{r=JSON.parse(n)}catch{return void await safeUnlink(t)}if(r&&1===r.version&&"number"==typeof r.createdAt&&Array.isArray(r.resourceLinks)&&!(Date.now()-r.createdAt>=2592e6))return r.resourceLinks;await safeUnlink(t)}}export async function cleanupUploadToolResultCache(){let e;try{e=await readdir(cacheDir(),{withFileTypes:!0})}catch{return 0}const t=Date.now();let a=0;for(const n of e){if(!n.isFile())continue;if(!n.name.endsWith(".json")&&!n.name.endsWith(".tmp"))continue;const e=join(cacheDir(),n.name),r=n.name.startsWith("enrich_")?2592e6:864e5;try{t-(await stat(e)).mtimeMs>=r&&(await unlink(e),a++)}catch{}}return a}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "beeos-claw",
3
- "version": "0.6.44-staging.5",
3
+ "version": "0.6.44-staging.7",
4
4
  "description": "OpenClaw plugin that bridges the BeeOS platform with the local OpenClaw Gateway, providing file transfer, ACP bridge, and observability.",
5
5
  "license": "MIT",
6
6
  "author": "BeeOS <dev@beeos.ai>",