@tomflow/chatloop-extension 0.4.38 → 0.4.40

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.
@@ -51,7 +51,7 @@ process.stdin.on('data',async chunk=>{
51
51
  try{projectContext=session?registeredContext(session.projectId):null;
52
52
  if(projectContext)contextStatus='VERIFIED';}catch{contextStatus='UNVERIFIED';}
53
53
  write({status:'OK',session,contextStatus,...(projectContext?{projectContext}:{})});
54
- }else if(['session-read-v2','session-sync-v2','session-snapshot-v2',
54
+ }else if(['session-read-v2','session-sync-v2','start-prepermit-verify-v2','session-snapshot-v2',
55
55
  'context-status-v2','context-writer-check-v2',
56
56
  'context-control-v2','continue-permit-v2',
57
57
  'continue-reconcile-v2','continue-send-check-v2',
@@ -8,6 +8,7 @@ importScripts("worker/bootstrap.js");
8
8
  importScripts("worker/admission.js");
9
9
  importScripts("worker/adopt-prepare.js");
10
10
  importScripts("worker/adopt-result.js");
11
+ importScripts("worker/task-start-evidence-v2.js");
11
12
  importScripts("worker/task-start-v2.js");
12
13
  importScripts("worker/task-intent-v2.js");
13
14
  importScripts("worker/task-intent-dispatch-v2.js");
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "manifest_version": 3,
3
3
  "name": "Chat Loop Extension",
4
- "version": "0.4.38",
4
+ "version": "0.4.40",
5
5
  "description": "Chat Loop browser companion: Eye actions, Turn observer, Continue and Handoff Actions",
6
6
  "permissions": [
7
7
  "activeTab",
@@ -0,0 +1,59 @@
1
+ // Task-first Start only: read-only approval evidence and bounded Observer reconnect.
2
+ async function scopedStartEvidence(intent){
3
+ const page=pathOf(intent.chatUrl),chatId=chatIdOf(intent.chatUrl);
4
+ scopedStartAssert(page&&chatId,'SCOPED_ADOPTION_URL_INVALID');
5
+ // Never implicitly open/select the latest Chat, including across Projects.
6
+ const tabs=await chrome.tabs.query({url:'https://chatgpt.com/*'});
7
+ const matches=tabs.filter(t=>Number.isSafeInteger(t.id)
8
+ &&pathOf(t.url)===page);
9
+ scopedStartAssert(matches.length===1,'SCOPED_ADOPTION_CHAT_UNVERIFIED');
10
+ const tab=matches[0];
11
+ const observe=()=>chrome.tabs.sendMessage(tab.id,{
12
+ type:'CHAT_LOOP_APPROVAL_EVIDENCE_V1',explicitChatId:chatId
13
+ }).catch(()=>null);
14
+ let first=await observe();
15
+ if(!first){
16
+ // Reload invalidates the old content-script connection. Reinject only
17
+ // the existing read-only Observer in the exact, already-open Chat.
18
+ const current=await chrome.tabs.get(tab.id).catch(()=>null);
19
+ scopedStartAssert(current&&pathOf(current.url)===page,
20
+ 'SCOPED_ADOPTION_TAB_CHANGED');
21
+ const files=['content.js','loop/core.js','loop/observer-inspect.js',
22
+ 'loop/observer-poll.js','loop/observer-recovery.js',
23
+ 'loop/observer-approval.js','loop/observer.js'];
24
+ const loaded=await chrome.scripting.executeScript({
25
+ target:{tabId:tab.id,allFrames:false},files
26
+ }).catch(()=>null);
27
+ scopedStartAssert(Array.isArray(loaded)&&loaded.length===1
28
+ &&loaded[0].frameId===0,
29
+ 'SCOPED_ADOPTION_OBSERVER_RECONNECT_FAILED');
30
+ const after=await chrome.tabs.get(tab.id).catch(()=>null);
31
+ scopedStartAssert(after&&pathOf(after.url)===page,
32
+ 'SCOPED_ADOPTION_TAB_CHANGED');
33
+ first=await observe();
34
+ }
35
+ const candidate=policy.normalizeAdoptionEvidence(first,{page});
36
+ scopedStartAssert(candidate,'SCOPED_ADOPTION_APPROVAL_UNVERIFIED');
37
+ const second=await observe();
38
+ scopedStartAssert(policy.sameAdoptionEvidence(candidate,
39
+ policy.normalizeAdoptionEvidence(second,{page})),
40
+ 'SCOPED_ADOPTION_APPROVAL_CHANGED');
41
+ const source=second?.approvalTurns;
42
+ scopedStartAssert(source&&typeof source==='object'
43
+ &&typeof source.rUserText==='string'&&source.rUserText.length>0
44
+ &&source.rUserText.length<=1000
45
+ &&typeof source.pUserText==='string'&&source.pUserText.length>0
46
+ &&source.pUserText.length<=1000
47
+ &&/^user:[^\s]{8,}$/.test(source.rUserKey||'')
48
+ &&source.rUserKey!==candidate.userKey,
49
+ 'SCOPED_ADOPTION_SOURCE_UNVERIFIED');
50
+ const fresh=await chrome.tabs.get(tab.id).catch(()=>null);
51
+ scopedStartAssert(fresh&&pathOf(fresh.url)===page,
52
+ 'SCOPED_ADOPTION_TAB_CHANGED');
53
+ return {tabId:tab.id,chatId,page,
54
+ evidence:candidate,approvalEvidence:{
55
+ rUserText:source.rUserText,rUserKey:source.rUserKey,
56
+ rVersion:candidate.rVersion,
57
+ pUserText:source.pUserText,pUserKey:candidate.userKey,
58
+ pVersion:candidate.pVersion}};
59
+ }
@@ -41,44 +41,6 @@ async function scopedStartIntent(message,sender){
41
41
  'SCOPED_START_INTENT_UNVERIFIED');
42
42
  return {requestId:id,intent,actionTabId:sender.tab.id};
43
43
  }
44
- async function scopedStartEvidence(intent){
45
- const page=pathOf(intent.chatUrl),chatId=chatIdOf(intent.chatUrl);
46
- scopedStartAssert(page&&chatId,'SCOPED_ADOPTION_URL_INVALID');
47
- // Never implicitly open/select the latest Chat, including across Projects.
48
- const tabs=await chrome.tabs.query({url:'https://chatgpt.com/*'});
49
- const matches=tabs.filter(t=>Number.isSafeInteger(t.id)
50
- &&pathOf(t.url)===page);
51
- scopedStartAssert(matches.length===1,'SCOPED_ADOPTION_CHAT_UNVERIFIED');
52
- const tab=matches[0];
53
- const observe=()=>chrome.tabs.sendMessage(tab.id,{
54
- type:'CHAT_LOOP_APPROVAL_EVIDENCE_V1',explicitChatId:chatId
55
- }).catch(()=>null);
56
- const first=await observe();
57
- const candidate=policy.normalizeAdoptionEvidence(first,{page});
58
- scopedStartAssert(candidate,'SCOPED_ADOPTION_APPROVAL_UNVERIFIED');
59
- const second=await observe();
60
- scopedStartAssert(policy.sameAdoptionEvidence(candidate,
61
- policy.normalizeAdoptionEvidence(second,{page})),
62
- 'SCOPED_ADOPTION_APPROVAL_CHANGED');
63
- const source=second?.approvalTurns;
64
- scopedStartAssert(source&&typeof source==='object'
65
- &&typeof source.rUserText==='string'&&source.rUserText.length>0
66
- &&source.rUserText.length<=1000
67
- &&typeof source.pUserText==='string'&&source.pUserText.length>0
68
- &&source.pUserText.length<=1000
69
- &&/^user:[^\s]{8,}$/.test(source.rUserKey||'')
70
- &&source.rUserKey!==candidate.userKey,
71
- 'SCOPED_ADOPTION_SOURCE_UNVERIFIED');
72
- const fresh=await chrome.tabs.get(tab.id).catch(()=>null);
73
- scopedStartAssert(fresh&&pathOf(fresh.url)===page,
74
- 'SCOPED_ADOPTION_TAB_CHANGED');
75
- return {tabId:tab.id,chatId,page,
76
- evidence:candidate,approvalEvidence:{
77
- rUserText:source.rUserText,rUserKey:source.rUserKey,
78
- rVersion:candidate.rVersion,
79
- pUserText:source.pUserText,pUserKey:candidate.userKey,
80
- pVersion:candidate.pVersion}};
81
- }
82
44
  const scopedStartRecord=(scope,phase,chatId=null)=>({
83
45
  projectId:scope.projectId,taskId:scope.taskId,sessionId:scope.sessionId,
84
46
  generation:0,phase,activeChatId:chatId,
@@ -111,9 +73,45 @@ async function scopedTaskStartPrepare(message,sender,source){
111
73
  const native=await scopedStartNative({
112
74
  op:'session-read-v2',sourceRequestId:requestId});
113
75
  if(existing.run){
114
- // Only resume the SAME private Start request before any Permit is
115
- // consumed. The original random Session is never replaced or guessed.
116
76
  const prior=existing.run,s=native?.session;
77
+ if(prior.startRequestId!==requestId){
78
+ // One fresh signed request may reclaim the SAME unbound reservation.
79
+ // Native+Context must prove a unique fresh lease and zero permits.
80
+ scopedStartAssert(prior.phase==='RESERVED'&&s?.phase==='RESERVED'
81
+ &&prior.prePermitRecoveryUsed!==true
82
+ &&intent.replaceBlockedSessionId===prior.sessionId
83
+ &&s.sessionId===prior.sessionId
84
+ &&s.projectId===intent.projectId&&s.taskId===intent.taskId
85
+ &&s.revision===prior.nativeOwnerRevision&&s.generation===0
86
+ &&s.activeChatId===null&&s.activeChatUrl===null,
87
+ 'SCOPED_START_PREPERMIT_RECOVERY_DENIED');
88
+ const approved=await scopedStartEvidence(intent);
89
+ scopedStartAssert(approved.tabId===prior.adoptTabId
90
+ &&approved.page===prior.adoptPage
91
+ &&approved.evidence.userKey===prior.adoptUserKey
92
+ &&approved.evidence.rVersion===prior.adoptRVersion
93
+ &&approved.evidence.pVersion===prior.adoptPVersion,
94
+ 'SCOPED_START_PREPERMIT_APPROVAL_CHANGED');
95
+ const checked=await scopedStartNative({op:'start-prepermit-verify-v2',
96
+ sourceRequestId:requestId,expectedSessionId:prior.sessionId,
97
+ expectedNativeRevision:s.revision});
98
+ scopedStartAssert(checked?.status==='OK'
99
+ &&checked.scope==='TASK_SCOPED_PREPERMIT_VERIFIED'
100
+ &&checked.projectId===intent.projectId&&checked.taskId===intent.taskId
101
+ &&checked.sessionId===prior.sessionId
102
+ &&checked.nativeOwnerRevision===s.revision,
103
+ 'SCOPED_START_PREPERMIT_CONTEXT_UNVERIFIED');
104
+ const repaired=await scopedTaskRuns.put({...prior,
105
+ startRequestId:requestId,actionTabId,prePermitRecoveryUsed:true},{
106
+ expectedStoreRevision:existing.storeRevision,
107
+ expectedRunRevision:existing.runRevision});
108
+ scopedStartAssert(repaired.runRevision>existing.runRevision,
109
+ 'SCOPED_START_PREPERMIT_ACK_UNKNOWN');
110
+ return {status:'PREPARED',scope:'TASK_SCOPED_ADOPT_NO_SEND',
111
+ sessionId:prior.sessionId,tabId:approved.tabId,
112
+ page:approved.page,userKey:approved.evidence.userKey};
113
+ }
114
+ // Same private request only; never reconsume a Permit.
117
115
  scopedStartAssert(prior.startRequestId===requestId
118
116
  &&prior.actionTabId===actionTabId
119
117
  &&['STARTING','RESERVED'].includes(prior.phase),
package/lib/bridge.mjs CHANGED
@@ -168,8 +168,7 @@ function validateIntent(action, intent, fromStoredRequest=false) {
168
168
  ||(intent.taskFirst!==undefined
169
169
  &&(intent.taskFirst!==true||intent.mode!=='adopt'
170
170
  ||!intent.taskId||!intent.chatUrl||!intent.contextLeaseRef
171
- ||intent.purpose!==undefined
172
- ||intent.replaceBlockedSessionId!==undefined))
171
+ ||intent.purpose!==undefined))
173
172
  ||intent.confirmed!==true || (intent.mode!==undefined && intent.mode!=='adopt')
174
173
  || (intent.replaceBlockedSessionId!==undefined
175
174
  && !/^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i.test(intent.replaceBlockedSessionId))
@@ -18,6 +18,7 @@ import {createScopedTerminalControl} from './scoped-terminal-v2.mjs';
18
18
  import {createScopedCancelControl} from './scoped-cancel-v2.mjs';
19
19
  import {createScopedRetireControl} from './scoped-retire-v2.mjs';
20
20
  import {createScopedRecoveryControl} from './scoped-recovery-v2.mjs';
21
+ import {createScopedStartRecoveryControl} from './scoped-start-recovery.mjs';
21
22
  // Mutating Start operations require a private request and exact Ledger CAS.
22
23
  // Exact-identity snapshot and writer checks are read-only, never sender permits.
23
24
  export function createScopedNativeControl(ports){
@@ -27,12 +28,13 @@ export function createScopedNativeControl(ports){
27
28
  const handoffControl=createScopedHandoffControl(ports);
28
29
  const recoveryControl=createScopedRecoveryControl({
29
30
  ...ports,verifyOriginalAbort:m=>handoffControl.handle(m)});
30
- const terminalControl=createScopedTerminalControl(ports);
31
- const cancelControl=createScopedCancelControl(ports);
31
+ const terminalControl=createScopedTerminalControl(ports),cancelControl=createScopedCancelControl(ports);
32
32
  const retireControl=createScopedRetireControl(ports);
33
+ const startRecoveryControl=createScopedStartRecoveryControl(ports);
33
34
  if([readRequest,readScopedOwner,writeScopedOwner,nativeContextControl]
34
35
  .some(port=>typeof port!=='function'))throw Error('SCOPED_NATIVE_PORTS_MISSING');
35
36
  return async function handleScopedNativeMessage(m){
37
+ if(m?.op==='start-prepermit-verify-v2')return startRecoveryControl(m);
36
38
  let result=null;
37
39
  const write=value=>{result=value};
38
40
  if(['continue-permit-v2','continue-reconcile-v2',
@@ -247,7 +249,7 @@ export function createScopedNativeControl(ports){
247
249
  // ONLY by the same private Start request. No guessed Session ID,
248
250
  // task-only fallback, or replay of a Context START Permit.
249
251
  if(!s||s.projectId!==intent.projectId||s.taskId!==intent.taskId
250
- ||s.sessionId!==startSessionForRequest(m.sourceRequestId)
252
+ ||!(s.sessionId===startSessionForRequest(m.sourceRequestId) ||committing&&intent.replaceBlockedSessionId===s.sessionId)
251
253
  ||s.generation!==0||!(prepermit||committing)
252
254
  ||!Number.isSafeInteger(m.expectedLedgerRevision)
253
255
  ||m.expectedLedgerRevision<0)
@@ -0,0 +1,53 @@
1
+ // Read-only same-Task pre-Permit recovery proof; never changes Owner or sends.
2
+ export function createScopedStartRecoveryControl(ports){
3
+ const {readRequest,readScopedOwner,nativeContextControl}=ports;
4
+ const fail=code=>Object.assign(Error(code),{code});
5
+ return async m=>{
6
+ if(m?.op!=='start-prepermit-verify-v2'
7
+ ||Object.keys(m).length!==4
8
+ ||!/^[a-f0-9]{32}$/.test(m.sourceRequestId||'')
9
+ ||!/^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i.test(m.expectedSessionId||'')
10
+ ||!Number.isSafeInteger(m.expectedNativeRevision)
11
+ ||m.expectedNativeRevision<1)
12
+ throw fail('SCOPED_PREPERMIT_REQUEST_INVALID');
13
+ const request=readRequest(m.sourceRequestId),i=request?.intent;
14
+ if(request?.action!=='start'||i?.taskFirst!==true
15
+ ||i.mode!=='adopt'||i.confirmed!==true
16
+ ||i.replaceBlockedSessionId!==m.expectedSessionId
17
+ ||typeof i.contextLeaseRef!=='string'
18
+ ||typeof i.contextStoreEpoch!=='string'
19
+ ||!Number.isSafeInteger(i.contextTaskRevision)
20
+ ||!Number.isSafeInteger(i.contextApprovalRevision))
21
+ throw fail('SCOPED_PREPERMIT_SOURCE_INVALID');
22
+ const prior=readScopedOwner(i.projectId,i.taskId).session;
23
+ if(!prior||prior.sessionId!==m.expectedSessionId
24
+ ||prior.revision!==m.expectedNativeRevision
25
+ ||prior.phase!=='RESERVED'||prior.generation!==0
26
+ ||prior.activeChatId!==null||prior.activeChatUrl!==null)
27
+ throw fail('SCOPED_PREPERMIT_OWNER_CHANGED');
28
+ const proof=Object.fromEntries(['projectId','taskId','sessionId',
29
+ 'generation','revision','phase','activeChatId']
30
+ .map(k=>[k,prior[k]]));
31
+ const state=await nativeContextControl('run.terminalStatus',{
32
+ projectId:prior.projectId,taskId:prior.taskId,
33
+ sessionId:prior.sessionId,generation:0,activeChatId:null,
34
+ startRecoveryLeaseRef:i.contextLeaseRef},{ownerProofV2:proof});
35
+ if(state?.startRecovery?.status!=='UNBOUND_NO_PERMIT'
36
+ ||state.startRecovery.leaseRef!==i.contextLeaseRef
37
+ ||state.storeEpoch!==i.contextStoreEpoch
38
+ ||state.taskRevision!==i.contextTaskRevision
39
+ ||state.approvalRevision!==i.contextApprovalRevision
40
+ ||state.bindingRevision!==0||state.runRef!==null
41
+ ||state.unknownEffectCount!==0||state.pendingHandoff!==false
42
+ ||state.pendingCheckpointId!==null||state.executionOutcome!==null
43
+ ||state.sessionCancellation!==null||state.projectRulesDrift!==false)
44
+ throw fail('SCOPED_PREPERMIT_CONTEXT_UNVERIFIED');
45
+ const fresh=readScopedOwner(i.projectId,i.taskId).session;
46
+ if(!fresh||fresh.sessionId!==prior.sessionId
47
+ ||fresh.revision!==prior.revision||fresh.phase!=='RESERVED')
48
+ throw fail('SCOPED_PREPERMIT_OWNER_CHANGED');
49
+ return {status:'OK',scope:'TASK_SCOPED_PREPERMIT_VERIFIED',
50
+ projectId:i.projectId,taskId:i.taskId,
51
+ sessionId:prior.sessionId,nativeOwnerRevision:prior.revision};
52
+ };
53
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tomflow/chatloop-extension",
3
- "version": "0.4.38",
3
+ "version": "0.4.40",
4
4
  "type": "module",
5
5
  "description": "Chat Loop Extension: reviewed formal Action pages, including TEST_ONLY native App entry",
6
6
  "bin": {