@tomflow/chatloop-extension 0.4.28 → 0.4.30

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.
@@ -277,6 +277,10 @@ if (message?.type === "CHAT_LOOP_APPROVAL_EVIDENCE_V1") {
277
277
  }
278
278
  reply({contract:CONTRACT,status:"VERIFIED",page:path,
279
279
  userKey,rVersion,pVersion,priorAssistantKey:messageId(pAssistant.el,"assistant"),
280
+ // Ephemeral approval source for Native/Context comparison at permit
281
+ // consumption. Never persisted in Chrome storage, logs or Owner state.
282
+ approvalTurns:{rUserKey:messageId(rUser.el,"user"),
283
+ rUserText:textOf(rUser),pUserText:textOf(pUser)},
280
284
  // Idle post-approval assistant was completed BEFORE adoption.
281
285
  // This is a historical baseline, never a fresh execution Turn.
282
286
  ...(scopedIdle?{postApprovalAssistantKey:snap.assistantKey}:{})});
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "manifest_version": 3,
3
3
  "name": "Chat Loop Extension",
4
- "version": "0.4.28",
4
+ "version": "0.4.30",
5
5
  "description": "Chat Loop browser companion: Eye actions, Turn observer, Continue and Handoff Actions",
6
6
  "permissions": [
7
7
  "activeTab",
@@ -27,7 +27,7 @@ async function approvedAdoptionTarget(chatUrl=null,taskId=null){
27
27
  const proof=await chrome.tabs.sendMessage(opened.id,
28
28
  {type:"CHAT_LOOP_APPROVAL_EVIDENCE_V1",
29
29
  explicitChatId:requestedId,
30
- ...(taskId?{expectedTaskId:taskId}:{})}).catch(()=>null);
30
+ ...(!requestedId&&taskId?{expectedTaskId:taskId}:{})}).catch(()=>null);
31
31
  if(proof?.contract==="chat-loop.turn-observer.v1"
32
32
  &&proof.status==="VERIFIED"
33
33
  &&chatIdOf('https://chatgpt.com'+proof.page)===requestedId){
@@ -62,8 +62,8 @@ async function sessionStartPrepare(message,sender){
62
62
  &&prior.contextStoreEpoch===intent.contextStoreEpoch
63
63
  &&prior.contextTaskRevision===intent.contextTaskRevision
64
64
  &&prior.contextApprovalRevision===intent.contextApprovalRevision
65
- &&prior.tabId===null&&prior.page==='/'&&!prior.newUserKey
66
- &&!prior.userKey&&!prior.attemptedNewTabId
65
+ &&prior.generation===0&&prior.tabId===null&&prior.page==='/'&&!prior.newUserKey
66
+ &&!prior.userKey&&!prior.assistantKey&&!prior.attemptedNewTabId&&!prior.lastStartActionTabId
67
67
  &&!!intent.chatUrl&&prior.adoptPage===pathOf(intent.chatUrl)
68
68
  &&Number.isInteger(prior.adoptTabId)
69
69
  &&typeof prior.adoptUserKey==='string');
@@ -71,7 +71,9 @@ async function sessionStartPrepare(message,sender){
71
71
  // rejected its CAS, Worker/Native disagree. No send was attempted, but this
72
72
  // is NOT a free retry: one exact native-BLOCKED repair with a fresh lease
73
73
  // is allowed; any managed writer, late send or second repair remains denied.
74
- const preNativeGhost=Boolean(prior?.phase==='STARTING'
74
+ const timedOutGhost=prior?.phase==='BLOCKED'
75
+ &&prior.reason==='ACTION_RESULT_TIMEOUT_UNVERIFIED';
76
+ const preNativeGhost=Boolean((prior?.phase==='STARTING'||timedOutGhost)
75
77
  &&prior.action==='START'&&prior.mode==='adopt'
76
78
  &&prior.prePermitRetryUsed===true&&!prior.preNativeRepairUsed
77
79
  &&prior.replacedBlockedSessionId===replace
@@ -82,8 +84,8 @@ async function sessionStartPrepare(message,sender){
82
84
  &&prior.contextStoreEpoch===intent.contextStoreEpoch
83
85
  &&prior.contextTaskRevision===intent.contextTaskRevision
84
86
  &&prior.contextApprovalRevision===intent.contextApprovalRevision
85
- &&prior.tabId===null&&prior.page==='/'&&!prior.newUserKey
86
- &&!prior.userKey&&!prior.attemptedNewTabId
87
+ &&prior.generation===0&&prior.tabId===null&&prior.page==='/'&&!prior.newUserKey
88
+ &&!prior.userKey&&!prior.assistantKey&&!prior.attemptedNewTabId&&!prior.lastStartActionTabId
87
89
  &&!!intent.chatUrl&&prior.adoptPage===pathOf(intent.chatUrl)
88
90
  &&Number.isInteger(prior.adoptTabId)
89
91
  &&typeof prior.adoptUserKey==='string');
@@ -114,8 +116,24 @@ async function sessionStartPrepare(message,sender){
114
116
  &&disk.session.updatedAt>0);
115
117
  if((matchingBlocked&&!(ownerMatches||(preNativeGhost&&sameTaskNative)
116
118
  ||driftReplacement))
117
- ||((prePermitRecovery||preNativeGhost)&&!sameTaskNative))
119
+ ||((prePermitRecovery||preNativeGhost)&&!sameTaskNative)
120
+ ||(preNativeGhost&&timedOutGhost&&!independentBlockedAdoptDrift(prior,disk.session)))
118
121
  return {status:'DENIED',code:'SESSION_REPLACEMENT_MISMATCH'};
122
+ if(prePermitRecovery||preNativeGhost){
123
+ // Native's durable old pointer scopes this read. A new lease or null
124
+ // runRef alone cannot prove the old START permit was never consumed.
125
+ const proof=await chrome.runtime.sendNativeMessage(SESSION_HOST,{
126
+ op:'context-control',command:'run.terminalStatus',
127
+ args:{startRecoveryLeaseRef:intent.contextLeaseRef}}).catch(()=>null);
128
+ const s=proof?.result;
129
+ if(proof?.status!=='OK'||s?.projectId!==intent.projectId||s?.taskId!==intent.taskId
130
+ ||s.storeEpoch!==intent.contextStoreEpoch||s.taskRevision!==intent.contextTaskRevision
131
+ ||s.approvalRevision!==intent.contextApprovalRevision||s.bindingRevision!==0
132
+ ||s.runRef!==null||s.unknownEffectCount!==0||s.executionOutcome!==null
133
+ ||s.startRecovery?.status!=='UNBOUND_NO_PERMIT'
134
+ ||s.startRecovery.leaseRef!==intent.contextLeaseRef)
135
+ return {status:'DENIED',code:'START_RECOVERY_PERMIT_UNVERIFIED'};
136
+ }
119
137
  // A terminal Worker record cannot authorize a new Start if a different
120
138
  // durable Native Owner exists under the same Browser installation.
121
139
  if(prior?.phase==='DONE'&&prior.sessionId&&disk.session&&!ownerMatches)
@@ -144,6 +162,8 @@ async function sessionStartPrepare(message,sender){
144
162
  :await approvedAdoptionTarget(intent.chatUrl||null,
145
163
  intent.contextLeaseRef?intent.taskId:null):null;
146
164
  if(adoption?.error)return {status:'DENIED',code:adoption.error};
165
+ if((prePermitRecovery||preNativeGhost)&&adoption?.target?.userKey!==prior.adoptUserKey)
166
+ return {status:'DENIED',code:'START_RECOVERY_APPROVAL_CHANGED'};
147
167
  if((matchingBlocked&&adoption?.target?.page===prior.page&&prior.page)
148
168
  ||(driftReplacement&&adoption?.target?.page===prior.adoptPage))
149
169
  return {status:'DENIED',code:'BLOCKED_REPLACEMENT_CHAT_REUSE'};
@@ -199,6 +219,36 @@ async function sessionStartTake(message,sender){
199
219
  if(!sessionPage(sender,'start',message.requestId)||run?.machineRequestId!==message.requestId
200
220
  ||run.machineActionTabId!==sender.tab.id||run.token!==message.token
201
221
  ||run.phase!=='STARTING')return {status:'DENIED',code:'START_OWNER_CHANGED'};
222
+ // Verify the real R/P source again immediately before asking Context to
223
+ // consume the Start lease. Never persist approval text in Worker storage.
224
+ let approvalEvidence=null;
225
+ if(run.contextLeaseRef&&run.mode==='adopt'
226
+ &&run.purpose!=='REPAIR_SAME_SCOPE'){
227
+ const proof=await chrome.tabs.sendMessage(run.adoptTabId,{
228
+ type:'CHAT_LOOP_APPROVAL_EVIDENCE_V1',
229
+ ...(run.adoptExplicitChatId?{explicitChatId:run.adoptExplicitChatId}:{}),
230
+ ...(run.adoptExpectedTaskId?{expectedTaskId:run.adoptExpectedTaskId}:{})
231
+ }).catch(()=>null);
232
+ const current=policy.normalizeAdoptionEvidence(proof,{page:run.adoptPage});
233
+ const original={page:run.adoptPage,userKey:run.adoptUserKey,
234
+ priorAssistantKey:run.adoptPriorAssistantKey||null,
235
+ postApprovalAssistantKey:run.adoptPostApprovalAssistantKey||null,
236
+ rVersion:run.adoptRVersion,pVersion:run.adoptPVersion};
237
+ const source=proof?.approvalTurns;
238
+ if(!policy.sameAdoptionEvidence(original,current)
239
+ ||!source||typeof source.rUserKey!=='string'
240
+ ||!/^user:[^\s]{8,}$/.test(source.rUserKey)
241
+ ||source.rUserKey===run.adoptUserKey
242
+ ||!['rUserText','pUserText'].every(k=>typeof source[k]==='string'
243
+ &&source[k].length>0&&source[k].length<=1000)){
244
+ await saveRun({...run,phase:'BLOCKED',
245
+ reason:'ADOPTION_APPROVAL_SOURCE_UNVERIFIED'});
246
+ return {status:'DENIED',code:'ADOPTION_APPROVAL_SOURCE_UNVERIFIED'};
247
+ }
248
+ approvalEvidence={rUserText:source.rUserText,rUserKey:source.rUserKey,
249
+ rVersion:run.adoptRVersion,pUserText:source.pUserText,
250
+ pUserKey:run.adoptUserKey,pVersion:run.adoptPVersion};
251
+ }
202
252
  await saveRun({...run,phase:'PENDING'});
203
253
  // Native validates current durable Session and signs the narrow Context IPC.
204
254
  // No Context adapter? Legacy Start behavior remains unchanged.
@@ -208,7 +258,9 @@ async function sessionStartTake(message,sender){
208
258
  args:{expectedStoreEpoch:run.contextStoreEpoch,writeRequestId:run.token,
209
259
  expectedTaskRevision:run.contextTaskRevision,
210
260
  expectedApprovalRevision:run.contextApprovalRevision,
211
- actionToken:run.token,kind:'START',leaseRef:run.contextLeaseRef}
261
+ actionToken:run.token,kind:'START',leaseRef:run.contextLeaseRef,
262
+ ...(run.mode==='adopt'&&run.purpose!=='REPAIR_SAME_SCOPE'
263
+ ?{adopt:true,approvalEvidence}:{})}
212
264
  }).catch(()=>null);
213
265
  if(allowed?.status!=='OK'||allowed.result?.status!=='PERMITTED'){
214
266
  await saveRun({...run,phase:'BLOCKED',reason:'CONTEXT_START_PERMIT_UNVERIFIED'});
@@ -93,6 +93,10 @@ async function saveRun(run) {
93
93
  if(recoveryCommit&&(!Number.isSafeInteger(next.recoveryExpectedNativeAt)
94
94
  ||next.recoveryExpectedNativeAt<=0))
95
95
  throw Error('SESSION_RECOVERY_CAS_MISSING');
96
+ // Native must commit first for the exact terminal CAS transitions.
97
+ const replacementCommit=Boolean(next.replacedBlockedSessionId
98
+ &&next.phase==='STARTING'&&stored?.sessionId!==next.sessionId);
99
+ const nativeFirst=recoveryCommit||replacementCommit;
96
100
  const syncNative=async()=>{
97
101
  if(!next.sessionId)return;
98
102
  const record=sessionProjection(next);
@@ -104,18 +108,26 @@ async function saveRun(run) {
104
108
  ?{sameTaskPrePermitUpdatedAt:next.sameTaskPrePermitUpdatedAt}:{})}:{}),
105
109
  ...(recoveryCommit?{resumeBlockedSessionId:next.sessionId,
106
110
  resumeBlockedUpdatedAt:next.recoveryExpectedNativeAt}:{})}).catch(()=>null);
111
+ // A fresh replacement Session ID identifies this exact CAS. If its
112
+ // transport reply was lost, confirm the durable projection once, without
113
+ // replaying the write. Same-ID recovery has no such unique commit proof.
114
+ if(!saved&&replacementCommit){
115
+ const observed=await chrome.runtime.sendNativeMessage(SESSION_HOST,
116
+ {op:'session-read'}).catch(()=>null);
117
+ if(observed?.status==='OK'&&matchesNativeProjection(record,observed.session))return;
118
+ }
107
119
  // Native acknowledgment must match the whole immutable Owner pointer,
108
120
  // not merely its sessionId and phase. updatedAt is CAS metadata, not
109
121
  // part of this identity projection.
110
122
  if(saved?.status!=="OK"||!matchesNativeProjection(record,saved.session))
111
123
  throw Error(saved?.code||"SESSION_PERSISTENCE_UNVERIFIED");
112
124
  };
113
- // A terminal BLOCKED session may resume only after native CAS first.
114
- // Do not leave a Chrome RESERVED owner behind if native rejects the
115
- // transition: that mismatch previously made all status calls unreadable.
116
- if(recoveryCommit)await syncNative();
125
+ // BLOCKED recovery and replacement both require a Native CAS first.
126
+ // Do not publish a new Worker reservation when Native rejects the CAS.
127
+ // An unknown acknowledgement stays fail-closed; never replay a Start.
128
+ if(nativeFirst)await syncNative();
117
129
  await chrome.storage.local.set({[RUN_KEY]:next});
118
- if(!recoveryCommit)await syncNative();
130
+ if(!nativeFirst)await syncNative();
119
131
  // The resource inventory is derived evidence; it never grants browser Owner.
120
132
  if (["DONE","BLOCKED"].includes(next.phase)
121
133
  && next.contextTerminalFence !== "PENDING"
@@ -150,7 +162,8 @@ const sessionProjectionDrift=(projected,native)=>SESSION_POINTER_KEYS.filter(
150
162
  key=>projected[key]!==native?.[key]);
151
163
  // Two distinct durable IDs may exist after an interrupted Adopt reservation.
152
164
  // This narrow read-only shape proves NEITHER a previous send nor authority to
153
- // replace it; only a DIFFERENT freshly approved Task can request Native CAS.
165
+ // replace it. Adopt admission separately verifies independent Task replacement
166
+ // or the exact same-Task pre-permit recovery contract before Native CAS.
154
167
  const independentBlockedAdoptDrift=(run,native)=>Boolean(
155
168
  run?.phase==='BLOCKED'&&run.reason==='ACTION_RESULT_TIMEOUT_UNVERIFIED'
156
169
  &&run.action==='START'&&run.mode==='adopt'
@@ -156,13 +156,24 @@ async function sessionStatus(message,sender){
156
156
  const projection=run?.sessionId?sessionProjection(run):null;
157
157
  if(projection&&!matchesNativeProjection(projection,disk.session)){
158
158
  const mismatchedFields=sessionProjectionDrift(projection,disk.session);
159
- return {status:'UNVERIFIED',code:'SESSION_FILE_MISMATCH',
160
- mismatchedFields,
161
- ...(mismatchedFields.length===1&&mismatchedFields[0]==='sessionId'
162
- &&independentBlockedAdoptDrift(run,disk.session)
163
- ?{independentTaskReplacementCandidate:{
164
- replaceBlockedSessionId:disk.session.sessionId,
165
- requires:'FRESH_APPROVED_DISTINCT_TASK_AND_NATIVE_CAS'}}:{})};
159
+ const blockedAdoptDrift=mismatchedFields.length===1&&mismatchedFields[0]==='sessionId'
160
+ &&independentBlockedAdoptDrift(run,disk.session);
161
+ // Only a locator for the exact old reservation, never a permit or Owner.
162
+ const prePermitCandidate=blockedAdoptDrift&&run.prePermitRetryUsed===true
163
+ &&!run.preNativeRepairUsed&&run.replacedBlockedSessionId===disk.session.sessionId
164
+ &&typeof run.contextStoreEpoch==='string'&&Number.isSafeInteger(run.contextTaskRevision)
165
+ &&Number.isSafeInteger(run.contextApprovalRevision)
166
+ &&pathOf('https://chatgpt.com'+run.adoptPage)===run.adoptPage;
167
+ return {status:'UNVERIFIED',code:'SESSION_FILE_MISMATCH',mismatchedFields,
168
+ diagnostics:{extensionVersion:chrome.runtime.getManifest?.().version||null},
169
+ ...(blockedAdoptDrift?{independentTaskReplacementCandidate:{
170
+ replaceBlockedSessionId:disk.session.sessionId,
171
+ requires:'FRESH_APPROVED_DISTINCT_TASK_AND_NATIVE_CAS'}}:{}),
172
+ ...(prePermitCandidate?{sameTaskPrePermitRecoveryCandidate:{
173
+ projectId:run.projectId,taskId:run.taskId,
174
+ replaceBlockedSessionId:disk.session.sessionId,
175
+ originalChatUrl:'https://chatgpt.com'+run.adoptPage,
176
+ requires:'FRESH_LEASE_NO_PRIOR_PERMIT_ORIGINAL_APPROVAL_AND_NATIVE_CAS'}}:{})};
166
177
  }
167
178
  if(!run?.sessionId&&disk.session&&disk.session.phase!=='DONE')
168
179
  return {status:'UNVERIFIED',code:'SESSION_OWNER_MISMATCH'};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tomflow/chatloop-extension",
3
- "version": "0.4.28",
3
+ "version": "0.4.30",
4
4
  "type": "module",
5
5
  "description": "Chat Loop Extension: reviewed formal Action pages, including TEST_ONLY native App entry",
6
6
  "bin": {