@tomflow/chatloop-context 0.6.21 → 0.6.23

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.
@@ -102,6 +102,14 @@ The Tools private handler passes a signed-IPC-verified Native projection into th
102
102
 
103
103
  The Browser Native Host now permits the Worker-used `run.reopenStatus`
104
104
  through the same signed private IPC path; it is not a public MCP method.
105
+ For first-Start recovery, `run.terminalStatus` accepts the private
106
+ `startRecoveryLeaseRef` only for a generation-zero Native BLOCKED pointer
107
+ without an active Chat. It returns `UNBOUND_NO_PERMIT` only when the exact
108
+ fresh lease is the sole valid reservation, no prior permit was consumed,
109
+ no writer/unknown effect exists, and current approvals remain valid.
110
+ `start.reserve` rejects a previously consumed initial permit and removes
111
+ expired unused initial leases transactionally; the read-only proof does not
112
+ grant a permit, bind a writer, or replace a Session.
105
113
 
106
114
  ## 5. Context → external dependencies (reverse direction)
107
115
 
@@ -1,6 +1,6 @@
1
1
  // Context Core health methods. Shared access and transactions are injected from store.mjs.
2
2
  export function createHealthMethods(values) {
3
- const {assert, parse, db, epoch, storeMode, hasApprovedPlan, required, checkIds, admin, readAuth, unresolvedEffects} = values;
3
+ const {assert, parse, db, epoch, storeMode, hasApprovedPlan, required, checkIds, admin, readAuth, unresolvedEffects, readSnapshot, projectDrift, ensureNoPendingIssue} = values;
4
4
  return {
5
5
  pragmas:()=>({
6
6
  journalMode:db.prepare('PRAGMA journal_mode').get().journal_mode,
@@ -28,14 +28,33 @@ export function createHealthMethods(values) {
28
28
  reopenUserTurn:c.reopenRequest?.userTurn??null,
29
29
  previousDeliveryRef:c.deliveryRef??null};
30
30
  },
31
- terminalStatus(projectId,taskId){
31
+ terminalStatus(projectId,taskId,startRecoveryLeaseRef=null){
32
32
  checkIds(projectId,taskId);readAuth(projectId,taskId,'run.terminalStatus');
33
- const r=required(projectId,taskId),c=parse(r.context_json);
34
- return {projectId,taskId,taskRevision:r.business_revision,
35
- bindingRevision:r.binding_revision,approvalRevision:r.approval_revision,
36
- unknownEffectCount:unresolvedEffects(c).length,
37
- runRef:r.writable_ref,executionOutcome:c.executionOutcome??null,
38
- deliveryRef:c.deliveryRef??null};
33
+ return readSnapshot(()=>{
34
+ const r=required(projectId,taskId),c=parse(r.context_json);
35
+ const state={projectId,taskId,taskRevision:r.business_revision,
36
+ bindingRevision:r.binding_revision,approvalRevision:r.approval_revision,
37
+ unknownEffectCount:unresolvedEffects(c).length,
38
+ runRef:r.writable_ref,executionOutcome:c.executionOutcome??null,
39
+ deliveryRef:c.deliveryRef??null};
40
+ if(startRecoveryLeaseRef===null)return state;
41
+ // Read-only, scoped to the fresh lease. The prior expired leases were
42
+ // removed transactionally by reserve; no prior permit may exist.
43
+ assert(r.binding_revision===0&&r.writable_ref===null&&r.pending_attempt===null
44
+ &&state.unknownEffectCount===0&&state.executionOutcome===null
45
+ &&!['ARCHIVED','DELIVERED','REOPENED'].includes(c.stage)
46
+ &&!projectDrift(r).projectDrift&&hasApprovedPlan(parse(r.approval_json)),
47
+ 'START_RECOVERY_UNSAFE');
48
+ ensureNoPendingIssue(projectId,taskId);
49
+ const leases=db.prepare('SELECT * FROM start_leases WHERE project_id=? AND task_id=?')
50
+ .all(projectId,taskId);
51
+ assert(leases.length===1&&leases[0].lease_ref===startRecoveryLeaseRef
52
+ &&leases[0].expires_at>Date.now()&&leases[0].business_revision===r.business_revision
53
+ &&leases[0].approval_revision===r.approval_revision,'START_RECOVERY_LEASE_UNVERIFIED');
54
+ assert(!db.prepare('SELECT 1 FROM permits WHERE project_id=? AND task_id=? LIMIT 1')
55
+ .get(projectId,taskId),'START_PERMIT_ALREADY_CONSUMED');
56
+ return {...state,storeEpoch:epoch,startRecovery:{status:'UNBOUND_NO_PERMIT',leaseRef:startRecoveryLeaseRef}};
57
+ });
39
58
  },
40
59
  latestCheckpoint(projectId,taskId){
41
60
  checkIds(projectId,taskId);readAuth(projectId,taskId,'context.latestCheckpoint');
package/lib/core/run.mjs CHANGED
@@ -25,6 +25,14 @@ export function createRunMethods(values) {
25
25
  assert(current.executionOutcome!=='DONE'&&current.stage!=='ARCHIVED',
26
26
  'TASK_ALREADY_DELIVERED');
27
27
  }
28
+ // Never re-reserve an initial Start whose execution permit may have
29
+ // been consumed. Expired, unconsumed leases cannot commit later.
30
+ if(r.binding_revision===0){
31
+ assert(!db.prepare('SELECT 1 FROM permits WHERE project_id=? AND task_id=? LIMIT 1')
32
+ .get(a.projectId,a.taskId),'START_PERMIT_ALREADY_CONSUMED');
33
+ db.prepare('DELETE FROM start_leases WHERE project_id=? AND task_id=? AND expires_at<=?')
34
+ .run(a.projectId,a.taskId,Date.now());
35
+ }
28
36
  const leaseRef=randomUUID();
29
37
  db.prepare('INSERT INTO start_leases VALUES(?,?,?,?,?,?)').run(leaseRef,
30
38
  a.projectId,a.taskId,r.approval_revision,r.business_revision,Date.now()+60000);
@@ -28,11 +28,17 @@ export function executeTrustedRun(command,args,native){
28
28
  assert(!['principal','role','internalAuthorized','ownerCommitted',
29
29
  'nativeCommitted'].some(k=>Object.hasOwn(args,k)),
30
30
  'CLIENT_AUTH_FIELD_FORBIDDEN');
31
+ const startRecovery=command==='run.terminalStatus'&&Object.hasOwn(args,'startRecoveryLeaseRef');
32
+ if(startRecovery)assert(native.phase==='BLOCKED'&&native.generation===0
33
+ &&native.activeChatId===null&&native.activeChatUrl===null
34
+ &&typeof args.startRecoveryLeaseRef==='string'
35
+ &&/^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i.test(args.startRecoveryLeaseRef),
36
+ 'START_RECOVERY_NATIVE_UNVERIFIED');
31
37
  assert(native.projectId===args.projectId && native.taskId===args.taskId
32
38
  && native.sessionId===args.sessionId
33
39
  && native.generation===args.generation
34
40
  && native.activeChatId===args.activeChatId
35
- && (native.activeChatId!==null || command==='permit.consume'&&args.kind==='START'),
41
+ && (native.activeChatId!==null || command==='permit.consume'&&args.kind==='START'||startRecovery),
36
42
  'CONTEXT_NATIVE_BINDING_MISMATCH');
37
43
  const allowed=allowedProjects();
38
44
  assert(allowed.has(native.projectId),'PROJECT_ACCESS_DENIED');
@@ -59,11 +65,12 @@ export function executeTrustedRun(command,args,native){
59
65
  const db=openContextProtocol(file,{principalId:'native-controlled',
60
66
  authorizeData:(_access,p)=>allowed.has(p),authorizeInternal:()=>true});
61
67
  try{
62
- if(!['run.terminalStatus','run.reopenStatus','run.snapshot'].includes(command)
68
+ if((startRecovery||!['run.terminalStatus','run.reopenStatus','run.snapshot'].includes(command))
63
69
  &&!db.projectBusinessAuthority(args.projectId).writable)
64
70
  throw fail('PROJECT_CUTOVER_NOT_VERIFIED');
65
71
  switch(command){
66
- case 'run.terminalStatus':return db.terminalStatus(args.projectId,args.taskId);
72
+ case 'run.terminalStatus':return db.terminalStatus(args.projectId,args.taskId,
73
+ startRecovery?args.startRecoveryLeaseRef:null);
67
74
  case 'run.reopenStatus':return db.reopenStatus(args.projectId,args.taskId);
68
75
  case 'run.snapshot':return db.latestCheckpoint(args.projectId,args.taskId);
69
76
  case 'permit.consume':return db.consumePermit(args);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tomflow/chatloop-context",
3
- "version": "0.6.21",
3
+ "version": "0.6.23",
4
4
  "type": "module",
5
5
  "description": "Independent local Context SDK and short-lived CLI for Chat Loop projects",
6
6
  "publishConfig": {
@@ -34,7 +34,7 @@
34
34
  },
35
35
  "dependencies": {
36
36
  "ps-list": "9.0.0",
37
- "@tomflow/chatloop-extension": "0.4.29",
37
+ "@tomflow/chatloop-extension": "0.4.31",
38
38
  "@tomflow/chatloop-trace": "0.4.6"
39
39
  }
40
40
  }