@tomflow/chatloop-tools 0.9.62 → 0.9.64
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -1
- package/index.mjs +7 -7
- package/lib/context-control-run.mjs +23 -5
- package/lib/extension-proxy.mjs +29 -10
- package/lib/native-owner-v2.mjs +128 -0
- package/lib/release-health-checks.mjs +29 -5
- package/lib/trace-gateway.mjs +3 -1
- package/package.json +3 -13
- package/providers/extension.mjs +59 -16
- package/node_modules/@tomflow/chatloop-context/README.md +0 -240
- package/node_modules/@tomflow/chatloop-context/bin/cli.mjs +0 -48
- package/node_modules/@tomflow/chatloop-context/capabilities/approval.mjs +0 -151
- package/node_modules/@tomflow/chatloop-context/capabilities/context.mjs +0 -376
- package/node_modules/@tomflow/chatloop-context/capabilities/document.mjs +0 -266
- package/node_modules/@tomflow/chatloop-context/capabilities/evidence.mjs +0 -134
- package/node_modules/@tomflow/chatloop-context/capabilities/presentation.mjs +0 -82
- package/node_modules/@tomflow/chatloop-context/capabilities/project.mjs +0 -349
- package/node_modules/@tomflow/chatloop-context/capabilities/sdk.mjs +0 -72
- package/node_modules/@tomflow/chatloop-context/capabilities/start.mjs +0 -73
- package/node_modules/@tomflow/chatloop-context/capabilities/task.mjs +0 -507
- package/node_modules/@tomflow/chatloop-context/capabilities.generated.mjs +0 -11
- package/node_modules/@tomflow/chatloop-context/docs/interfaces.md +0 -192
- package/node_modules/@tomflow/chatloop-context/docs/operator-and-migration.md +0 -277
- package/node_modules/@tomflow/chatloop-context/index.mjs +0 -236
- package/node_modules/@tomflow/chatloop-context/lib/api-log.mjs +0 -86
- package/node_modules/@tomflow/chatloop-context/lib/approval-policy.mjs +0 -77
- package/node_modules/@tomflow/chatloop-context/lib/capability-catalog.generated.json +0 -1989
- package/node_modules/@tomflow/chatloop-context/lib/capability-router.mjs +0 -69
- package/node_modules/@tomflow/chatloop-context/lib/cli-commands.mjs +0 -285
- package/node_modules/@tomflow/chatloop-context/lib/context-cli.mjs +0 -298
- package/node_modules/@tomflow/chatloop-context/lib/context-migration.mjs +0 -286
- package/node_modules/@tomflow/chatloop-context/lib/core/context.mjs +0 -115
- package/node_modules/@tomflow/chatloop-context/lib/core/evidence.mjs +0 -51
- package/node_modules/@tomflow/chatloop-context/lib/core/health.mjs +0 -75
- package/node_modules/@tomflow/chatloop-context/lib/core/migration.mjs +0 -262
- package/node_modules/@tomflow/chatloop-context/lib/core/project.mjs +0 -246
- package/node_modules/@tomflow/chatloop-context/lib/core/review.mjs +0 -147
- package/node_modules/@tomflow/chatloop-context/lib/core/run-cancel.mjs +0 -54
- package/node_modules/@tomflow/chatloop-context/lib/core/run.mjs +0 -375
- package/node_modules/@tomflow/chatloop-context/lib/core/store.mjs +0 -397
- package/node_modules/@tomflow/chatloop-context/lib/core/task.mjs +0 -183
- package/node_modules/@tomflow/chatloop-context/lib/line-review.mjs +0 -13
- package/node_modules/@tomflow/chatloop-context/lib/methods.mjs +0 -111
- package/node_modules/@tomflow/chatloop-context/lib/operations.mjs +0 -107
- package/node_modules/@tomflow/chatloop-context/lib/orphan-operator.mjs +0 -118
- package/node_modules/@tomflow/chatloop-context/lib/process-inventory.mjs +0 -20
- package/node_modules/@tomflow/chatloop-context/lib/project-branch.mjs +0 -316
- package/node_modules/@tomflow/chatloop-context/lib/project-diagnostic.mjs +0 -153
- package/node_modules/@tomflow/chatloop-context/lib/project-guards.mjs +0 -19
- package/node_modules/@tomflow/chatloop-context/lib/project-store.mjs +0 -117
- package/node_modules/@tomflow/chatloop-context/lib/trusted-run.mjs +0 -97
- package/node_modules/@tomflow/chatloop-context/lib/workspace-operator.mjs +0 -169
- package/node_modules/@tomflow/chatloop-context/package.json +0 -40
- package/node_modules/@tomflow/chatloop-context/scripts/build-capabilities.mjs +0 -114
- package/node_modules/@tomflow/chatloop-extension/README.md +0 -124
- package/node_modules/@tomflow/chatloop-extension/actions.generated.mjs +0 -13
- package/node_modules/@tomflow/chatloop-extension/bin/cli.mjs +0 -153
- package/node_modules/@tomflow/chatloop-extension/bin/native-host.mjs +0 -207
- package/node_modules/@tomflow/chatloop-extension/capabilities/chat_dom.mjs +0 -1
- package/node_modules/@tomflow/chatloop-extension/capabilities/chat_screenshot.mjs +0 -1
- package/node_modules/@tomflow/chatloop-extension/capabilities/chat_text.mjs +0 -1
- package/node_modules/@tomflow/chatloop-extension/capabilities/clean.mjs +0 -1
- package/node_modules/@tomflow/chatloop-extension/capabilities/continue.mjs +0 -1
- package/node_modules/@tomflow/chatloop-extension/capabilities/entry_test.mjs +0 -1
- package/node_modules/@tomflow/chatloop-extension/capabilities/handoff.mjs +0 -1
- package/node_modules/@tomflow/chatloop-extension/capabilities/reload.mjs +0 -1
- package/node_modules/@tomflow/chatloop-extension/capabilities/session.mjs +0 -1
- package/node_modules/@tomflow/chatloop-extension/capabilities/start.mjs +0 -1
- package/node_modules/@tomflow/chatloop-extension/capabilities/status.mjs +0 -1
- package/node_modules/@tomflow/chatloop-extension/extension/actions/clean.js +0 -123
- package/node_modules/@tomflow/chatloop-extension/extension/actions/common.js +0 -183
- package/node_modules/@tomflow/chatloop-extension/extension/actions/continue.js +0 -340
- package/node_modules/@tomflow/chatloop-extension/extension/actions/dom.js +0 -2
- package/node_modules/@tomflow/chatloop-extension/extension/actions/entry_test.js +0 -241
- package/node_modules/@tomflow/chatloop-extension/extension/actions/handoff.js +0 -393
- package/node_modules/@tomflow/chatloop-extension/extension/actions/index.js +0 -2
- package/node_modules/@tomflow/chatloop-extension/extension/actions/model-intent.js +0 -56
- package/node_modules/@tomflow/chatloop-extension/extension/actions/read-mode.js +0 -9
- package/node_modules/@tomflow/chatloop-extension/extension/actions/reload.js +0 -20
- package/node_modules/@tomflow/chatloop-extension/extension/actions/screenshot.js +0 -77
- package/node_modules/@tomflow/chatloop-extension/extension/actions/session.js +0 -52
- package/node_modules/@tomflow/chatloop-extension/extension/actions/start.js +0 -295
- package/node_modules/@tomflow/chatloop-extension/extension/actions/status.js +0 -24
- package/node_modules/@tomflow/chatloop-extension/extension/actions/text.js +0 -2
- package/node_modules/@tomflow/chatloop-extension/extension/background.js +0 -17
- package/node_modules/@tomflow/chatloop-extension/extension/clean.html +0 -5
- package/node_modules/@tomflow/chatloop-extension/extension/content.js +0 -40
- package/node_modules/@tomflow/chatloop-extension/extension/continue.html +0 -9
- package/node_modules/@tomflow/chatloop-extension/extension/dom.html +0 -17
- package/node_modules/@tomflow/chatloop-extension/extension/entry_test.html +0 -6
- package/node_modules/@tomflow/chatloop-extension/extension/handoff.html +0 -9
- package/node_modules/@tomflow/chatloop-extension/extension/loop/action-catalog.generated.js +0 -2
- package/node_modules/@tomflow/chatloop-extension/extension/loop/api-aspect.generated.js +0 -275
- package/node_modules/@tomflow/chatloop-extension/extension/loop/api-observation-queue.generated.js +0 -84
- package/node_modules/@tomflow/chatloop-extension/extension/loop/core.js +0 -107
- package/node_modules/@tomflow/chatloop-extension/extension/loop/index.js +0 -36
- package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-approval.js +0 -299
- package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-inspect.js +0 -144
- package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-poll.js +0 -171
- package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-recovery.js +0 -109
- package/node_modules/@tomflow/chatloop-extension/extension/loop/observer.js +0 -154
- package/node_modules/@tomflow/chatloop-extension/extension/loop/resources.js +0 -259
- package/node_modules/@tomflow/chatloop-extension/extension/manifest.json +0 -42
- package/node_modules/@tomflow/chatloop-extension/extension/popup.html +0 -25
- package/node_modules/@tomflow/chatloop-extension/extension/popup.js +0 -99
- package/node_modules/@tomflow/chatloop-extension/extension/reload.html +0 -17
- package/node_modules/@tomflow/chatloop-extension/extension/screenshot.html +0 -17
- package/node_modules/@tomflow/chatloop-extension/extension/session.html +0 -6
- package/node_modules/@tomflow/chatloop-extension/extension/start.html +0 -6
- package/node_modules/@tomflow/chatloop-extension/extension/status.html +0 -6
- package/node_modules/@tomflow/chatloop-extension/extension/text.html +0 -17
- package/node_modules/@tomflow/chatloop-extension/extension/worker/action-result.js +0 -136
- package/node_modules/@tomflow/chatloop-extension/extension/worker/action-take.js +0 -218
- package/node_modules/@tomflow/chatloop-extension/extension/worker/admission.js +0 -124
- package/node_modules/@tomflow/chatloop-extension/extension/worker/adopt-prepare.js +0 -296
- package/node_modules/@tomflow/chatloop-extension/extension/worker/adopt-recovery.js +0 -186
- package/node_modules/@tomflow/chatloop-extension/extension/worker/adopt-result.js +0 -126
- package/node_modules/@tomflow/chatloop-extension/extension/worker/bootstrap.js +0 -230
- package/node_modules/@tomflow/chatloop-extension/extension/worker/manual.js +0 -163
- package/node_modules/@tomflow/chatloop-extension/extension/worker/readiness.js +0 -345
- package/node_modules/@tomflow/chatloop-extension/extension/worker/router.js +0 -192
- package/node_modules/@tomflow/chatloop-extension/extension/worker/scheduler.js +0 -161
- package/node_modules/@tomflow/chatloop-extension/extension/worker/session-control.js +0 -75
- package/node_modules/@tomflow/chatloop-extension/extension/worker/turn-failure.js +0 -197
- package/node_modules/@tomflow/chatloop-extension/extension/worker/turn-finished.js +0 -205
- package/node_modules/@tomflow/chatloop-extension/index.mjs +0 -71
- package/node_modules/@tomflow/chatloop-extension/lib/action-routing.mjs +0 -19
- package/node_modules/@tomflow/chatloop-extension/lib/api-observation-queue.mjs +0 -82
- package/node_modules/@tomflow/chatloop-extension/lib/bridge.mjs +0 -303
- package/node_modules/@tomflow/chatloop-extension/lib/browser-launch.mjs +0 -83
- package/node_modules/@tomflow/chatloop-extension/lib/context-binding.mjs +0 -45
- package/node_modules/@tomflow/chatloop-extension/lib/context-ipc.mjs +0 -115
- package/node_modules/@tomflow/chatloop-extension/lib/owner-evidence.mjs +0 -75
- package/node_modules/@tomflow/chatloop-extension/lib/session-file.mjs +0 -185
- package/node_modules/@tomflow/chatloop-extension/package.json +0 -38
- package/node_modules/@tomflow/chatloop-extension/scripts/build-actions.mjs +0 -138
- package/node_modules/@tomflow/chatloop-trace/README.md +0 -118
- package/node_modules/@tomflow/chatloop-trace/events/context.api.mjs +0 -23
- package/node_modules/@tomflow/chatloop-trace/events/typed.api.call.mjs +0 -22
- package/node_modules/@tomflow/chatloop-trace/events.generated.mjs +0 -4
- package/node_modules/@tomflow/chatloop-trace/index.mjs +0 -59
- package/node_modules/@tomflow/chatloop-trace/lib/api-aspect.mjs +0 -273
- package/node_modules/@tomflow/chatloop-trace/lib/trace-reader.mjs +0 -308
- package/node_modules/@tomflow/chatloop-trace/lib/trace-retention.mjs +0 -140
- package/node_modules/@tomflow/chatloop-trace/lib/trace.mjs +0 -236
- package/node_modules/@tomflow/chatloop-trace/package.json +0 -30
- package/node_modules/@tomflow/chatloop-trace/scripts/build-events.mjs +0 -68
- package/node_modules/ps-list/index.d.ts +0 -76
- package/node_modules/ps-list/index.js +0 -366
- package/node_modules/ps-list/license +0 -9
- package/node_modules/ps-list/package.json +0 -44
- package/node_modules/ps-list/readme.md +0 -56
- package/node_modules/ps-list/vendor/fastlist-0.3.0-x64.exe +0 -0
- package/node_modules/ps-list/vendor/fastlist-0.3.0-x86.exe +0 -0
|
@@ -1,345 +0,0 @@
|
|
|
1
|
-
// Chat Loop Worker — readiness. Imported in order by background.js; no independent Browser Owner.
|
|
2
|
-
// Read the existing resource index without reconciliation, tab mutation or
|
|
3
|
-
// Owner changes. A registered blank successor proves only tab creation,
|
|
4
|
-
// NEVER a click, user message or successful Handoff.
|
|
5
|
-
async function handoffTabEvidence(run){
|
|
6
|
-
if(run?.phase!=="BLOCKED"||run.action!=="HANDOFF"||!run.sessionId)
|
|
7
|
-
return null;
|
|
8
|
-
const saved=(await chrome.storage.local.get(resources.KEY).catch(()=>null))
|
|
9
|
-
?.[resources.KEY];
|
|
10
|
-
const candidates=Array.isArray(saved?.entries)?saved.entries.filter(entry=>
|
|
11
|
-
entry?.kind==="chat"&&entry.confirmed===false
|
|
12
|
-
&&entry.sessionId===run.sessionId&&Number.isInteger(entry.id)
|
|
13
|
-
&&entry.id!==run.tabId):[];
|
|
14
|
-
if(candidates.length!==1)return null;
|
|
15
|
-
const entry=candidates[0];
|
|
16
|
-
const tab=await chrome.tabs.get(entry.id).catch(()=>null);
|
|
17
|
-
if(!tab||typeof tab.url!=="string"||!tab.url.startsWith("https://chatgpt.com/"))
|
|
18
|
-
return null;
|
|
19
|
-
return {tabId:entry.id,recorded:true,confirmed:false,
|
|
20
|
-
pageState:tab.url==="https://chatgpt.com/"?"BLANK_HOME":"CHANGED_UNVERIFIED",
|
|
21
|
-
sendReceipt:"UNVERIFIED"};
|
|
22
|
-
}
|
|
23
|
-
// Read-only diagnosis for a START that created an unconfirmed Chat home.
|
|
24
|
-
// Never infers a send receipt, creates a tab, reads message text, or changes Owner.
|
|
25
|
-
async function startReadinessEvidence(run){
|
|
26
|
-
if(run?.phase!=='BLOCKED'||run.action!=='START'||!run.sessionId
|
|
27
|
-
||!Number.isSafeInteger(run.attemptedNewTabId))return null;
|
|
28
|
-
const saved=(await chrome.storage.local.get(resources.KEY).catch(()=>null))
|
|
29
|
-
?.[resources.KEY];
|
|
30
|
-
const candidates=Array.isArray(saved?.entries)?saved.entries.filter(entry=>
|
|
31
|
-
entry?.kind==='chat'&&entry.confirmed===false
|
|
32
|
-
&&entry.sessionId===run.sessionId&&entry.id===run.attemptedNewTabId):[];
|
|
33
|
-
if(candidates.length!==1)return null;
|
|
34
|
-
const tab=await Promise.race([
|
|
35
|
-
chrome.tabs.get(run.attemptedNewTabId).catch(()=>null),
|
|
36
|
-
new Promise(resolve=>setTimeout(()=>resolve(null),1500))
|
|
37
|
-
]);
|
|
38
|
-
if(!tab||typeof tab.url!=='string'||!tab.url.startsWith('https://chatgpt.com/'))
|
|
39
|
-
return null;
|
|
40
|
-
const result=await Promise.race([
|
|
41
|
-
chrome.scripting.executeScript({target:{tabId:tab.id},func:async expectedDigest=>{
|
|
42
|
-
const visible=e=>Boolean(e?.getClientRects?.().length);
|
|
43
|
-
const mains=[...document.querySelectorAll('main')].filter(visible);
|
|
44
|
-
const editors=[...document.querySelectorAll(
|
|
45
|
-
'[contenteditable="true"][data-composer-markdown],#prompt-textarea[contenteditable="true"]')];
|
|
46
|
-
const editor=editors.find(visible);
|
|
47
|
-
const messages=mains.flatMap(m=>[...m.querySelectorAll(
|
|
48
|
-
'[data-chatgpt-search-message-ids],[data-message-author-role]')]);
|
|
49
|
-
const modern=mains.flatMap(m=>[...m.querySelectorAll(
|
|
50
|
-
'[data-chatgpt-search-unit-key][data-chatgpt-search-message-ids]')]);
|
|
51
|
-
const units=modern.length?modern:messages;
|
|
52
|
-
const role=n=>n.getAttribute('data-message-author-role')
|
|
53
|
-
||n.getAttribute('data-chatgpt-search-unit-key')?.split(':').at(-1);
|
|
54
|
-
const messageId=n=>{
|
|
55
|
-
const id=n.getAttribute('data-message-id')
|
|
56
|
-
||n.getAttribute('data-chatgpt-search-message-ids')?.trim().split(/\s+/)[0];
|
|
57
|
-
return typeof id==='string'&&/^[A-Za-z0-9][A-Za-z0-9._:-]{0,115}$/.test(id)
|
|
58
|
-
?id:null;
|
|
59
|
-
};
|
|
60
|
-
const canonical=v=>String(v||'').replace(/\s+/g,'');
|
|
61
|
-
const users=units.filter(n=>role(n)==='user');
|
|
62
|
-
const assistants=units.filter(n=>role(n)==='assistant');
|
|
63
|
-
const comparable=typeof expectedDigest==='string'&&/^[a-f0-9]{64}$/.test(expectedDigest)
|
|
64
|
-
&&Boolean(globalThis.crypto?.subtle)&&typeof TextEncoder==='function';
|
|
65
|
-
const matched=[];
|
|
66
|
-
if(comparable)for(const user of users){
|
|
67
|
-
if(!messageId(user))continue;
|
|
68
|
-
const bubble=user.querySelector('[data-search-result-target]')
|
|
69
|
-
||user.querySelector('[data-user-message-bubble="true"]')||user;
|
|
70
|
-
const normalized=canonical(bubble.innerText||bubble.textContent);
|
|
71
|
-
const bytes=new TextEncoder().encode(normalized);
|
|
72
|
-
const hash=await crypto.subtle.digest('SHA-256',bytes);
|
|
73
|
-
const digest=Array.from(new Uint8Array(hash),v=>v.toString(16).padStart(2,'0')).join('');
|
|
74
|
-
if(digest===expectedDigest)matched.push(user);
|
|
75
|
-
}
|
|
76
|
-
const key=(prefix,node)=>node&&messageId(node)?prefix+messageId(node):null;
|
|
77
|
-
// Diagnostic only: an account-switch error can leave a local-chatgpt
|
|
78
|
-
// user bubble visible without any server-confirmed first send.
|
|
79
|
-
// Never infer NOT_SENT or a retry permit from this UI text.
|
|
80
|
-
const switched=/你的账户已切换[。.!!\s]*请重新发送消息|your account (?:has been |was )?switched[.!\s]*please (?:re-?send|send again) (?:your )?message/i;
|
|
81
|
-
const inUserMessage=users.some(n=>switched.test(n.innerText||n.textContent||''));
|
|
82
|
-
const accountSwitchVisible=!inUserMessage&&mains.some(m=>
|
|
83
|
-
switched.test(m.innerText||m.textContent||''));
|
|
84
|
-
return {path:location.pathname,readyState:document.readyState,
|
|
85
|
-
uiErrorCode:accountSwitchVisible?'ACCOUNT_SWITCHED_RETRY_REQUIRED':null,
|
|
86
|
-
mainCount:mains.length,editorCount:editors.length,
|
|
87
|
-
editorVisible:Boolean(editor),editorEditable:Boolean(editor?.isContentEditable),
|
|
88
|
-
editorEmpty:editor?!(editor.innerText||editor.textContent||'').trim():null,
|
|
89
|
-
messageNodeCount:messages.length,
|
|
90
|
-
// Read-only identity and equality evidence. No message text or send permit.
|
|
91
|
-
userMessageCount:users.length,assistantMessageCount:assistants.length,
|
|
92
|
-
startPromptComparison:comparable?'AVAILABLE':'UNAVAILABLE',
|
|
93
|
-
matchedStartPromptCount:comparable?new Set(matched.map(messageId)).size:null,
|
|
94
|
-
lastUserKey:key('user:',users.at(-1)),
|
|
95
|
-
lastMatchedUserKey:key('user:',matched.at(-1)),
|
|
96
|
-
lastAssistantKey:key('assistant:',assistants.at(-1))};
|
|
97
|
-
},args:[typeof run.startPromptDigest==='string'?run.startPromptDigest:null]
|
|
98
|
-
}).then(rows=>rows?.[0]?.result||null).catch(()=>null),
|
|
99
|
-
new Promise(resolve=>setTimeout(()=>resolve(null),1500))
|
|
100
|
-
]);
|
|
101
|
-
return {tabId:tab.id,confirmed:false,sendReceipt:'UNVERIFIED',
|
|
102
|
-
pageState:tab.url==='https://chatgpt.com/'?'BLANK_HOME':'CHANGED_UNVERIFIED',
|
|
103
|
-
...(result&&typeof result.mainCount==='number'?{readiness:result}:{})};
|
|
104
|
-
}
|
|
105
|
-
// TEST_ONLY is an independent native @ App first-turn, never a managed Owner.
|
|
106
|
-
// A frozen BLOCKED run with a lost Owner tab must not disable ordinary first-
|
|
107
|
-
// turn testing forever. Keep its Native pointer untouched: this read-only
|
|
108
|
-
// gate checks both authorities and denies any live/unknown Owner transition.
|
|
109
|
-
async function entryTestAdmission(message,sender){
|
|
110
|
-
if(!sessionPage(sender,'entry_test',message.requestId))
|
|
111
|
-
return {status:'DENIED',code:'ENTRY_TEST_SOURCE_UNVERIFIED'};
|
|
112
|
-
const disk=await chrome.runtime.sendNativeMessage(SESSION_HOST,
|
|
113
|
-
{op:'session-read'}).catch(()=>null);
|
|
114
|
-
if(disk?.status!=='OK')return {status:'DENIED',code:'ENTRY_TEST_NATIVE_UNVERIFIED'};
|
|
115
|
-
const run=await readRun(),record=disk.session;
|
|
116
|
-
if(!run?.sessionId){
|
|
117
|
-
return !record||record.phase==='DONE'
|
|
118
|
-
?{status:'OK',scope:'TEST_ONLY'}
|
|
119
|
-
:{status:'DENIED',code:'ENTRY_TEST_OWNER_UNVERIFIED'};
|
|
120
|
-
}
|
|
121
|
-
if(!matchesNativeProjection(sessionProjection(run),record))
|
|
122
|
-
return {status:'DENIED',code:'ENTRY_TEST_OWNER_MISMATCH'};
|
|
123
|
-
if(run.phase==='DONE')
|
|
124
|
-
return !run.contextManaged||run.taskId&&terminalFenceVerified(run)
|
|
125
|
-
?{status:'OK',scope:'TEST_ONLY'}
|
|
126
|
-
:{status:'DENIED',code:'ENTRY_TEST_UNFENCED'};
|
|
127
|
-
if(run.phase==='CANCELLED'){
|
|
128
|
-
const checked=await chrome.runtime.sendNativeMessage(SESSION_HOST,{
|
|
129
|
-
op:'context-control',command:'run.terminalStatus',args:{}
|
|
130
|
-
}).catch(()=>null);
|
|
131
|
-
const cancelled=checked?.result?.sessionCancellation;
|
|
132
|
-
return record.phase==='CANCELLED'&&checked?.status==='OK'
|
|
133
|
-
&&checked.result.runRef===null
|
|
134
|
-
&&cancelled?.status==='CANCELLED_NOT_DELIVERED'
|
|
135
|
-
&&cancelled.sessionId===run.sessionId
|
|
136
|
-
&&cancelled.generation===run.generation
|
|
137
|
-
?{status:'OK',scope:'TEST_ONLY'}
|
|
138
|
-
:{status:'DENIED',code:'ENTRY_TEST_CANCEL_FENCE_UNVERIFIED'};
|
|
139
|
-
}
|
|
140
|
-
if(run.phase!=='BLOCKED'||run.contextManaged&&(!run.taskId||!terminalFenceVerified(run))
|
|
141
|
-
||!Number.isSafeInteger(run.tabId)||!pathOf('https://chatgpt.com'+run.page))
|
|
142
|
-
return {status:'DENIED',code:'ENTRY_TEST_ACTIVE_OWNER'};
|
|
143
|
-
// Chrome may reuse the old numeric tab ID. Even a changed tab is not a
|
|
144
|
-
// verified absence. A new tab showing the same Chat is also a live risk.
|
|
145
|
-
if(await chrome.tabs.get(run.tabId).catch(()=>null))
|
|
146
|
-
return {status:'DENIED',code:'ENTRY_TEST_OWNER_TAB_PRESENT'};
|
|
147
|
-
const existing=await chrome.tabs.query({url:'https://chatgpt.com/*'}).catch(()=>null);
|
|
148
|
-
if(!Array.isArray(existing)
|
|
149
|
-
||existing.some(tab=>chatIdOf(tab.url)===chatIdOf('https://chatgpt.com'+run.page)))
|
|
150
|
-
return {status:'DENIED',code:'ENTRY_TEST_OWNER_REOPENED'};
|
|
151
|
-
const again=await readRun();
|
|
152
|
-
const second=await chrome.runtime.sendNativeMessage(SESSION_HOST,
|
|
153
|
-
{op:'session-read'}).catch(()=>null);
|
|
154
|
-
if(!again||JSON.stringify(again)!==JSON.stringify(run)
|
|
155
|
-
||second?.status!=='OK'||!second.session
|
|
156
|
-
||second.session.updatedAt!==record.updatedAt
|
|
157
|
-
||!matchesNativeProjection(sessionProjection(again),second.session))
|
|
158
|
-
return {status:'DENIED',code:'ENTRY_TEST_OWNER_DRIFT'};
|
|
159
|
-
return {status:'OK',scope:'TEST_ONLY',mode:'FROZEN_BLOCKED_NO_OWNER'};
|
|
160
|
-
}
|
|
161
|
-
async function sessionStatus(message,sender){
|
|
162
|
-
if(!sessionPage(sender,'status',message.requestId))
|
|
163
|
-
return {status:'DENIED',code:'INVALID_STATUS_SOURCE'};
|
|
164
|
-
const run=await readRun();
|
|
165
|
-
const disk=await chrome.runtime.sendNativeMessage(SESSION_HOST,{op:'session-read',
|
|
166
|
-
...(message.auditDispatchParent?{sourceRequestId:message.requestId,
|
|
167
|
-
traceParent:message.auditDispatchParent}:{})}).catch(()=>null);
|
|
168
|
-
if(disk?.status!=='OK')return {status:'UNVERIFIED',code:'SESSION_STATE_UNVERIFIED'};
|
|
169
|
-
const projection=run?.sessionId?sessionProjection(run):null;
|
|
170
|
-
if(projection&&!matchesNativeProjection(projection,disk.session)){
|
|
171
|
-
const mismatchedFields=sessionProjectionDrift(projection,disk.session);
|
|
172
|
-
const blockedAdoptDrift=mismatchedFields.length===1&&mismatchedFields[0]==='sessionId'
|
|
173
|
-
&&independentBlockedAdoptDrift(run,disk.session);
|
|
174
|
-
// Only a locator for the exact old reservation, never a permit or Owner.
|
|
175
|
-
const prePermitCandidate=blockedAdoptDrift&&run.prePermitRetryUsed===true
|
|
176
|
-
&&!run.preNativeRepairUsed&&run.replacedBlockedSessionId===disk.session.sessionId
|
|
177
|
-
&&typeof run.contextStoreEpoch==='string'&&Number.isSafeInteger(run.contextTaskRevision)
|
|
178
|
-
&&Number.isSafeInteger(run.contextApprovalRevision)
|
|
179
|
-
&&pathOf('https://chatgpt.com'+run.adoptPage)===run.adoptPage;
|
|
180
|
-
return {status:'UNVERIFIED',code:'SESSION_FILE_MISMATCH',mismatchedFields,
|
|
181
|
-
diagnostics:{extensionVersion:chrome.runtime.getManifest?.().version||null},
|
|
182
|
-
...(blockedAdoptDrift?{independentTaskReplacementCandidate:{
|
|
183
|
-
replaceBlockedSessionId:disk.session.sessionId,
|
|
184
|
-
requires:'FRESH_APPROVED_DISTINCT_TASK_AND_NATIVE_CAS'}}:{}),
|
|
185
|
-
...(prePermitCandidate?{sameTaskPrePermitRecoveryCandidate:{
|
|
186
|
-
projectId:run.projectId,taskId:run.taskId,
|
|
187
|
-
replaceBlockedSessionId:disk.session.sessionId,
|
|
188
|
-
originalChatUrl:'https://chatgpt.com'+run.adoptPage,
|
|
189
|
-
requires:'FRESH_LEASE_NO_PRIOR_PERMIT_ORIGINAL_APPROVAL_AND_NATIVE_CAS'}}:{})};
|
|
190
|
-
}
|
|
191
|
-
if(!run?.sessionId&&disk.session&&disk.session.phase!=='DONE')
|
|
192
|
-
return {status:'UNVERIFIED',code:'SESSION_OWNER_MISMATCH'};
|
|
193
|
-
if(run&&run.phase!=='DONE'&&!run.sessionId)
|
|
194
|
-
return {status:'OK',state:'LEGACY_OWNER',phase:run.phase};
|
|
195
|
-
if(run?.phase==='DONE'&&!terminalFenceVerified(run))
|
|
196
|
-
return {status:'UNVERIFIED',code:'CONTEXT_TERMINAL_FENCE_UNVERIFIED'};
|
|
197
|
-
if(run?.phase==='CANCELLED'){
|
|
198
|
-
const proof=await chrome.runtime.sendNativeMessage(SESSION_HOST,{
|
|
199
|
-
op:'context-control',command:'run.terminalStatus',args:{}}).catch(()=>null);
|
|
200
|
-
const canceled=proof?.result?.sessionCancellation;
|
|
201
|
-
if(proof?.status!=='OK'||proof.result.runRef!==null
|
|
202
|
-
||canceled?.status!=='CANCELLED_NOT_DELIVERED'
|
|
203
|
-
||canceled.sessionId!==run.sessionId
|
|
204
|
-
||canceled.generation!==run.generation
|
|
205
|
-
||disk.session?.phase!=='CANCELLED')
|
|
206
|
-
return {status:'UNVERIFIED',code:'CONTEXT_CANCEL_FENCE_UNVERIFIED'};
|
|
207
|
-
return {status:'OK',state:'IDLE',lastSession:disk.session,
|
|
208
|
-
terminalReason:'CANCELLED_NOT_DELIVERED'};
|
|
209
|
-
}
|
|
210
|
-
if(!run||run.phase==='DONE')return {status:'OK',state:'IDLE',
|
|
211
|
-
...(disk.session?{lastSession:disk.session}:{} )};
|
|
212
|
-
const record=sessionProjection(run);
|
|
213
|
-
// Native validates the private project binding; Status only forwards its
|
|
214
|
-
// read-only verified reference for the SAME active owner.
|
|
215
|
-
const projectContext=disk.contextStatus==='VERIFIED'
|
|
216
|
-
&&disk.projectContext?.projectId===run.projectId
|
|
217
|
-
?disk.projectContext:null;
|
|
218
|
-
let blockedOwnerTabMissing=false;
|
|
219
|
-
if(record.activeChatId&&['WAITING_REPLY','BLOCKED'].includes(run.phase)){
|
|
220
|
-
const tab=await chrome.tabs.get(run.tabId).catch(()=>null);
|
|
221
|
-
if(!tab||pathOf(tab.url)!==run.page){
|
|
222
|
-
// A missing tab for the OLD, already-BLOCKED Session is a locator
|
|
223
|
-
// diagnostic, not a conclusion about a different newly approved Task.
|
|
224
|
-
// Native/Worker identity above must still match; the independent new
|
|
225
|
-
// Start must separately prove the exact replacement and Context fence.
|
|
226
|
-
if(run.phase!=='BLOCKED')
|
|
227
|
-
return {status:'UNVERIFIED',code:'OWNER_TAB_MISSING',session:disk.session,
|
|
228
|
-
diagnostics:{extensionVersion:chrome.runtime.getManifest?.().version||null,
|
|
229
|
-
ownerTabId:null,ownerLocatorState:tab?'URL_MISMATCH':'TAB_LOOKUP_UNVERIFIED'}};
|
|
230
|
-
blockedOwnerTabMissing=true;
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
// Identity comes from the verified Worker projection, but updatedAt is
|
|
234
|
-
// Native's durable CAS revision. Never report a synthetic timestamp as a
|
|
235
|
-
// usable recovery credential.
|
|
236
|
-
const tabEvidence=await handoffTabEvidence(run);
|
|
237
|
-
const startEvidence=await startReadinessEvidence(run);
|
|
238
|
-
return {status:'OK',state:run.phase==='BLOCKED'?'BLOCKED':'ACTIVE',session:disk.session,
|
|
239
|
-
diagnostics:{extensionVersion:chrome.runtime.getManifest?.().version||null,
|
|
240
|
-
...(tabEvidence?{handoffTabEvidence:tabEvidence}:{}),
|
|
241
|
-
...(startEvidence?{startReadinessEvidence:startEvidence}:{}),
|
|
242
|
-
...(run.lastStartFailureCode?{lastStartFailureCode:run.lastStartFailureCode}:{}),
|
|
243
|
-
// The numeric tab ID is a locator, never a send capability. Expose only
|
|
244
|
-
// for an exactly verified terminal/awaiting Owner, not a guessed tab.
|
|
245
|
-
ownerTabId:blockedOwnerTabMissing?null
|
|
246
|
-
:['WAITING_REPLY','BLOCKED'].includes(run.phase)?run.tabId:null,
|
|
247
|
-
...(blockedOwnerTabMissing
|
|
248
|
-
?{oldOwnerTabStatus:'MISSING_UNVERIFIED'}:{}),
|
|
249
|
-
reason:run.reason||null,action:run.action||null,
|
|
250
|
-
userKey:run.userKey||null,assistantKey:run.assistantKey||null,
|
|
251
|
-
newUserKey:run.newUserKey||null,recoveryAttempts:run.recoveryAttempts||0,
|
|
252
|
-
...(projectContext?{projectContext}:{}),
|
|
253
|
-
recoveryMode:run.recoveryMode||null,recoveryProtocol:run.recoveryProtocol||null,
|
|
254
|
-
recoverySourceAssistantKey:run.recoverySourceAssistantKey||null,
|
|
255
|
-
startedAt:run.startedAt||null,
|
|
256
|
-
deadlineAt:deadlineOf(run),lastManualFeedback:run.lastManualFeedback||null}};
|
|
257
|
-
}
|
|
258
|
-
async function expireStaleAction() {
|
|
259
|
-
const run=await readRun();
|
|
260
|
-
if(!['STARTING','RESERVED','PENDING'].includes(run?.phase)
|
|
261
|
-
|| !Number.isSafeInteger(run.actionPendingAt)
|
|
262
|
-
|| Date.now()-run.actionPendingAt<((run.action==='START'||run.action==='HANDOFF')
|
|
263
|
-
?330000:180000)) return;
|
|
264
|
-
// No action may be recreated or re-sent on a missing receipt. Keep the Chat.
|
|
265
|
-
await saveRun({...run,phase:'BLOCKED',reason:'ACTION_RESULT_TIMEOUT_UNVERIFIED'});
|
|
266
|
-
}
|
|
267
|
-
async function prepareHandoffIfDue() {
|
|
268
|
-
const run = await readRun();
|
|
269
|
-
const deadlineAt = deadlineOf(run);
|
|
270
|
-
if (!clockActive(run) || run.handoffPending || deadlineAt === null
|
|
271
|
-
|| Date.now() < deadlineAt) return;
|
|
272
|
-
await saveRun({...run, handoffPending: true,
|
|
273
|
-
handoffPendingAt: Date.now()});
|
|
274
|
-
}
|
|
275
|
-
// Chrome preserves a content script's original document sender.url across SPA
|
|
276
|
-
// / → /c/... navigation. Authenticate its origin, then read the CURRENT tab URL.
|
|
277
|
-
const eligible = async (sender, page) => {
|
|
278
|
-
if (!Number.isInteger(sender.tab?.id) || typeof page !== "string"
|
|
279
|
-
|| pathOf("https://chatgpt.com" + page) !== page) return false;
|
|
280
|
-
try {
|
|
281
|
-
if (new URL(sender.url).origin !== "https://chatgpt.com") return false;
|
|
282
|
-
const tab = await chrome.tabs.get(sender.tab.id);
|
|
283
|
-
return pathOf(tab.url) === page;
|
|
284
|
-
} catch { return false; }
|
|
285
|
-
};
|
|
286
|
-
|
|
287
|
-
// Reuse the existing Observer read channel. This checks ONLY the latest
|
|
288
|
-
// assistant Turn in the current owned Chat, not historical forms or composer
|
|
289
|
-
// fields. An unknown read is never evidence of approval.
|
|
290
|
-
async function currentConfirmation(tabId,page,userKey,assistantKey) {
|
|
291
|
-
const tab=await chrome.tabs.get(tabId).catch(()=>null);
|
|
292
|
-
if(pathOf(tab?.url)!==page)return null;
|
|
293
|
-
const proof=await chrome.tabs.sendMessage(tabId,
|
|
294
|
-
{type:"CHAT_LOOP_TURN_OBSERVE_V1",poll:true}).catch(()=>null);
|
|
295
|
-
if(proof?.contract!=="chat-loop.turn-observer.v1"
|
|
296
|
-
||proof.page!==page||proof.userKey!==userKey
|
|
297
|
-
||proof.assistantKey!==assistantKey
|
|
298
|
-
||proof.reason==="WAITING_CHAT_HYDRATION"
|
|
299
|
-
||typeof proof.pendingConfirmation!=="boolean")return null;
|
|
300
|
-
return proof.pendingConfirmation;
|
|
301
|
-
}
|
|
302
|
-
// A transient partial UI snapshot may end with UNKNOWN even when the same
|
|
303
|
-
// already-confirmed reply later settles on a valid final status. Only resample
|
|
304
|
-
// the exact in-flight user/assistant turn, read-only, before failing closed.
|
|
305
|
-
async function recheckUnknownTurn(message, previous, tabId) {
|
|
306
|
-
if (previous?.phase !== "WAITING_REPLY" || previous.tabId !== tabId
|
|
307
|
-
|| previous.page !== message.page || previous.newUserKey !== message.userKey
|
|
308
|
-
|| previous.assistantKey === message.assistantKey) return null;
|
|
309
|
-
const read = async () => {
|
|
310
|
-
const tab = await chrome.tabs.get(tabId).catch(() => null);
|
|
311
|
-
if (pathOf(tab?.url) !== message.page) return null;
|
|
312
|
-
const proof = await chrome.tabs.sendMessage(tabId,
|
|
313
|
-
{type: "CHAT_LOOP_TURN_VERIFY_V1"}).catch(() => null);
|
|
314
|
-
if (proof?.contract !== "chat-loop.turn-observer.v1" || proof.status !== "READY"
|
|
315
|
-
|| proof.page !== message.page || proof.userKey !== message.userKey
|
|
316
|
-
|| proof.assistantKey !== message.assistantKey
|
|
317
|
-
|| typeof proof.replyText !== "string") return null;
|
|
318
|
-
return proof.replyText;
|
|
319
|
-
};
|
|
320
|
-
for (let attempt = 0; attempt < 8; attempt++) {
|
|
321
|
-
const first = await read();
|
|
322
|
-
if (first === null) return null;
|
|
323
|
-
const parsed = workerApi.parseStatus(first);
|
|
324
|
-
if (parsed !== "UNKNOWN") {
|
|
325
|
-
await new Promise(resolve => setTimeout(resolve, 2300));
|
|
326
|
-
const second = await read();
|
|
327
|
-
if (second === first) return {status: parsed, replyText: second};
|
|
328
|
-
} else {
|
|
329
|
-
await new Promise(resolve => setTimeout(resolve, 1500));
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
return null;
|
|
333
|
-
}
|
|
334
|
-
// A suspended MV3 Worker may lose the Action receipt. Never retry a send
|
|
335
|
-
// from a stale RESERVED/PENDING phase; scrub the queued task text and block.
|
|
336
|
-
async function expireStaleModelIntent() {
|
|
337
|
-
const run=await readRun();
|
|
338
|
-
if(!run?.modelIntent || !Number.isSafeInteger(run.modelIntent.queuedAt)
|
|
339
|
-
||Date.now()-run.modelIntent.queuedAt<=MODEL_RECEIPT_TTL_MS)return;
|
|
340
|
-
const pendingSend=["RESERVED","PENDING"].includes(run.phase);
|
|
341
|
-
const next=settleModelIntent(run,"STALE_UNVERIFIED","INTENT_TTL_EXPIRED");
|
|
342
|
-
await saveRun(pendingSend
|
|
343
|
-
?{...next,phase:"BLOCKED",reason:"STALE_INTENT_UNKNOWN_SEND"}
|
|
344
|
-
:next);
|
|
345
|
-
}
|
|
@@ -1,192 +0,0 @@
|
|
|
1
|
-
// Chat Loop Worker — router. Imported in order by background.js; no independent Browser Owner.
|
|
2
|
-
// Read-only target resolver for a CLI-created formal Eye Action.
|
|
3
|
-
// This never starts a Loop, changes Owner, or sends a chat message.
|
|
4
|
-
async function onExtensionCliTarget(message,sender) {
|
|
5
|
-
const entry=workerApi.eyeCatalog?.[message?.action];
|
|
6
|
-
const page=entry?.page;
|
|
7
|
-
// Catalog only grants the existing read-only Eye permission category. It
|
|
8
|
-
// does not register new send/Owner/Native control message handlers.
|
|
9
|
-
if(!['eye-read','eye-screenshot','eye-reload'].includes(entry?.kind))
|
|
10
|
-
return {status:'DENIED',code:'INVALID_EXTENSION_ACTION_SOURCE'};
|
|
11
|
-
if(!page || !/^[a-f0-9]{32}$/.test(message?.id||"")
|
|
12
|
-
|| sender.url !== chrome.runtime.getURL(page)+"?requestId="+message.id)
|
|
13
|
-
return {status:"DENIED",code:"INVALID_EXTENSION_ACTION_SOURCE"};
|
|
14
|
-
if(message.action==="reload")return {status:"OK"};
|
|
15
|
-
const run=await readRun();
|
|
16
|
-
// A failed first send has no Chat Owner. Eye may inspect only the exact
|
|
17
|
-
// unconfirmed tab created by this START, and only after verifying its
|
|
18
|
-
// original prompt fingerprint. No ownership, recovery or send is granted.
|
|
19
|
-
if(run?.phase==='BLOCKED'&&run.action==='START'
|
|
20
|
-
&&run.lastStartFailureCode==='FIRST_SEND_RECEIPT_UNKNOWN'){
|
|
21
|
-
const evidence=await startReadinessEvidence(run);
|
|
22
|
-
const observed=evidence?.readiness;
|
|
23
|
-
if(evidence?.sendReceipt==='UNVERIFIED'
|
|
24
|
-
&&observed?.startPromptComparison==='AVAILABLE'
|
|
25
|
-
&&observed.matchedStartPromptCount===1
|
|
26
|
-
&&observed.userMessageCount>=1
|
|
27
|
-
&&typeof observed.path==='string'&&observed.path.startsWith('/c/')){
|
|
28
|
-
const candidate=await chrome.tabs.get(evidence.tabId).catch(()=>null);
|
|
29
|
-
// pathOf deliberately rejects provisional local-chatgpt paths for
|
|
30
|
-
// Ownership. Eye instead checks the exact observed HTTPS path only.
|
|
31
|
-
try {
|
|
32
|
-
const url=new URL(candidate?.url);
|
|
33
|
-
if(url.origin==='https://chatgpt.com'&&url.pathname===observed.path)
|
|
34
|
-
return {status:'OK',tabId:candidate.id,page:observed.path};
|
|
35
|
-
} catch { /* A changed or malformed tab is not a read target. */ }
|
|
36
|
-
}
|
|
37
|
-
return {status:'DENIED',code:'START_EYE_EVIDENCE_UNVERIFIED'};
|
|
38
|
-
}
|
|
39
|
-
// Read-only Eye may inspect the last confirmed terminal Owner while the same
|
|
40
|
-
// Chat URL and exact tab still match. This does not reactivate the Loop,
|
|
41
|
-
// grant Continue/Handoff permission or select another Chat tab.
|
|
42
|
-
const finishedOwned = ["DONE","BLOCKED"].includes(run?.phase)
|
|
43
|
-
&& /^user:[^\s]{8,}$/.test(run.userKey || "")
|
|
44
|
-
&& /^assistant:[^\s]{8,}$/.test(run.assistantKey || "");
|
|
45
|
-
if(!(run?.phase === "WAITING_REPLY" || finishedOwned) || !Number.isInteger(run.tabId)
|
|
46
|
-
|| typeof run.page!=="string")
|
|
47
|
-
return {status:"DENIED",code:"NO_ACTIVE_CHAT_OWNER"};
|
|
48
|
-
const tab=await chrome.tabs.get(run.tabId).catch(()=>null);
|
|
49
|
-
if(!tab || pathOf(tab.url)!==run.page)
|
|
50
|
-
return {status:"DENIED",code:"OWNER_CHANGED"};
|
|
51
|
-
return {status:"OK",tabId:run.tabId,page:run.page};
|
|
52
|
-
}
|
|
53
|
-
// One explicit router for authenticated, serialized state changes.
|
|
54
|
-
// The registered handler still verifies source, token, Owner and Turn identity.
|
|
55
|
-
const SERIAL_ROUTES=new Map([
|
|
56
|
-
['CHAT_LOOP_TURN_FINISHED_V1',onTurnFinished],
|
|
57
|
-
['CHAT_LOOP_TURN_BLOCKED_V1',onTurnBlocked],
|
|
58
|
-
['CHAT_LOOP_ACTION_TAKE_V1',onTake],
|
|
59
|
-
['CHAT_LOOP_ACTION_RESULT_V1',onResult],
|
|
60
|
-
['CHAT_LOOP_HANDOFF_SEND_BEGIN_V1',onHandoffSendBegin],
|
|
61
|
-
['CHAT_LOOP_ADOPT_KICKOFF_PREPARE_V1',onAdoptKickoffPrepare],
|
|
62
|
-
['CHAT_LOOP_ADOPT_BASELINE_REPAIR_V1',onAdoptBaselineRepair],
|
|
63
|
-
['CHAT_LOOP_ACTION_MANUAL_REVEAL_V1',onManualReveal],
|
|
64
|
-
['CHAT_LOOP_ACTION_MANUAL_PREPARE_V1',onManualPrepare],
|
|
65
|
-
['CHAT_LOOP_MODEL_INTENT_V1',onModelIntent],
|
|
66
|
-
['CHAT_LOOP_MODEL_INTENT_STATUS_V1',onModelIntentStatus],
|
|
67
|
-
['CHAT_LOOP_SESSION_START_PREPARE_V1',sessionStartPrepare],
|
|
68
|
-
['CHAT_LOOP_SESSION_START_TAKE_V1',sessionStartTake],
|
|
69
|
-
['CHAT_LOOP_SESSION_START_RESULT_V1',sessionStartResult],
|
|
70
|
-
['CHAT_LOOP_SESSION_STATUS_V1',sessionStatus],
|
|
71
|
-
['CHAT_LOOP_SESSION_CONTROL_V1',sessionControl],
|
|
72
|
-
['CHAT_LOOP_ENTRY_TEST_ADMISSION_V1',entryTestAdmission],
|
|
73
|
-
['CHAT_LOOP_RESOURCE_CHAT_V1',resourceChatGate]
|
|
74
|
-
|
|
75
|
-
]);
|
|
76
|
-
const onBrowserMessage=(message, sender, reply, dispatchParent=null) => {
|
|
77
|
-
const type=message?.type;
|
|
78
|
-
const handle=SERIAL_ROUTES.get(type);
|
|
79
|
-
if(handle){
|
|
80
|
-
void serial(async()=>{
|
|
81
|
-
let result;
|
|
82
|
-
try{result=await handle(type==='CHAT_LOOP_SESSION_STATUS_V1'
|
|
83
|
-
?{...message,auditDispatchParent:dispatchParent}:message,sender);}
|
|
84
|
-
catch(error){result={status:'BLOCKED',code:safeWorkerErrorCode(error,'SCHEDULER_ERROR')};}
|
|
85
|
-
// A bounded diagnostic receipt, never permission or a second state store.
|
|
86
|
-
// In particular a rejected manual recovery must be observable remotely.
|
|
87
|
-
if(type==='CHAT_LOOP_ACTION_MANUAL_PREPARE_V1'){
|
|
88
|
-
const current=await readRun();
|
|
89
|
-
if(current)await chrome.storage.local.set({[RUN_KEY]:{...current,
|
|
90
|
-
lastManualFeedback:{status:result.status,
|
|
91
|
-
code:/^[A-Z0-9_]{1,100}$/.test(result.code||'')?result.code:null,at:Date.now()}}});
|
|
92
|
-
}
|
|
93
|
-
return result;
|
|
94
|
-
}).then(reply,error=>reply({status:'BLOCKED',code:safeWorkerErrorCode(error,'SCHEDULER_ERROR')}));
|
|
95
|
-
return true;
|
|
96
|
-
}
|
|
97
|
-
if (type === 'CHAT_LOOP_POPUP_CLEAN_OPEN_V1') {
|
|
98
|
-
void serial(()=>openPopupClean(message,sender)).then(reply,
|
|
99
|
-
()=>reply({status:'BLOCKED',code:'CLEAN_ACTION_UNVERIFIED'}));
|
|
100
|
-
return true;
|
|
101
|
-
}
|
|
102
|
-
if (type === "CHAT_LOOP_EXTENSION_TARGET_V1") {
|
|
103
|
-
void onExtensionCliTarget(message,sender).then(reply,
|
|
104
|
-
()=>reply({status:"DENIED",code:"OWNER_UNVERIFIED"}));
|
|
105
|
-
return true;
|
|
106
|
-
}
|
|
107
|
-
if (type !== "CHAT_LOOP_EYE_RELOAD_V1") return false;
|
|
108
|
-
const id = sender.tab?.id;
|
|
109
|
-
let acknowledged=false;
|
|
110
|
-
void (async () => {
|
|
111
|
-
try {
|
|
112
|
-
if (!Number.isInteger(id)) throw Error("NO_ACTION_TAB");
|
|
113
|
-
const tab = await chrome.tabs.get(id);
|
|
114
|
-
const reloadBase=chrome.runtime.getURL("reload.html");
|
|
115
|
-
if (!tab.url.startsWith(reloadBase) ||
|
|
116
|
-
(tab.url!==reloadBase &&
|
|
117
|
-
!/^\?requestId=[a-f0-9]{32}$/.test(tab.url.slice(reloadBase.length))))
|
|
118
|
-
throw Error("INVALID_RELOAD_SOURCE");
|
|
119
|
-
// Acknowledgement is deliberately not a verified reload result.
|
|
120
|
-
// Once sent, never issue a second contradictory callback.
|
|
121
|
-
acknowledged=true;
|
|
122
|
-
reply({status: "ACCEPTED"});
|
|
123
|
-
await chrome.tabs.remove(id);
|
|
124
|
-
// Reload terminates pending MV3 Native Messaging Promises immediately.
|
|
125
|
-
// Each span's terminal record follows its BEGIN across a one-shot
|
|
126
|
-
// Native process, which can take longer than a single 300-ms drain.
|
|
127
|
-
// The business receipt is already sent; only soft audit transport
|
|
128
|
-
// may run until this bounded teardown deadline (never retry Action).
|
|
129
|
-
try{
|
|
130
|
-
const deadline=Date.now()+2300;
|
|
131
|
-
while(Date.now()<deadline){
|
|
132
|
-
if(await workerApi.audit.drain(Math.min(700,deadline-Date.now())))break;
|
|
133
|
-
const health=workerApi.audit.health();
|
|
134
|
-
if(health.pending===0&&health.inFlight===0)break;
|
|
135
|
-
}
|
|
136
|
-
}catch{/* Audit cannot change the accepted business result. */}
|
|
137
|
-
chrome.runtime.reload();
|
|
138
|
-
} catch (error) {
|
|
139
|
-
if(!acknowledged)
|
|
140
|
-
reply({status: "FAILED", code: safeWorkerErrorCode(error,'RELOAD_UNVERIFIED')});
|
|
141
|
-
// After ACCEPTED, the caller retains ACCEPTED_UNVERIFIED and must
|
|
142
|
-
// recheck the same extension instance; no automatic replay.
|
|
143
|
-
}
|
|
144
|
-
})();
|
|
145
|
-
return true;
|
|
146
|
-
};
|
|
147
|
-
chrome.runtime.onMessage.addListener((message,sender,reply)=>{
|
|
148
|
-
const type=typeof message?.type==='string'&&
|
|
149
|
-
/^CHAT_LOOP_[A-Z0-9_]{2,90}$/.test(message.type)
|
|
150
|
-
?message.type:'unknown';
|
|
151
|
-
// Dispatch is a synchronous acceptance marker, not an Action result.
|
|
152
|
-
// Capture its safe span ID before invoking the original handler so that
|
|
153
|
-
// a later async reply can be independently audited as its child.
|
|
154
|
-
let dispatchSpan=null;
|
|
155
|
-
const respond=result=>{
|
|
156
|
-
try{return reply(result);}
|
|
157
|
-
finally{
|
|
158
|
-
try{
|
|
159
|
-
const rejected=['DENIED','BLOCKED','FAILED','UNVERIFIED']
|
|
160
|
-
.includes(result?.status);
|
|
161
|
-
const code=typeof result?.code==='string'&&
|
|
162
|
-
/^[A-Z][A-Z0-9_]{1,95}$/.test(result.code)
|
|
163
|
-
?result.code:'BROWSER_REPLY_REJECTED';
|
|
164
|
-
workerApi.audit.observe('browser.reply.'+type,()=>{
|
|
165
|
-
if(rejected)throw Object.assign(Error('Reply rejected'),{code});
|
|
166
|
-
return true;
|
|
167
|
-
},'browser',{parent:dispatchSpan})();
|
|
168
|
-
}catch{/* Reply telemetry cannot change the browser callback. */}
|
|
169
|
-
}
|
|
170
|
-
};
|
|
171
|
-
// Only the known, exact status Action may offer an AOP parent. The
|
|
172
|
-
// existing sessionPage/Owner gate still independently checks the request.
|
|
173
|
-
const sourceId=message?.requestId||message?.id;
|
|
174
|
-
const actionSource=/^[a-f0-9]{32}$/.test(sourceId||'')&&(
|
|
175
|
-
type==='CHAT_LOOP_SESSION_STATUS_V1'&&
|
|
176
|
-
sender?.url===chrome.runtime.getURL('status.html')+'?requestId='+sourceId||
|
|
177
|
-
['CHAT_LOOP_MODEL_INTENT_V1','CHAT_LOOP_MODEL_INTENT_STATUS_V1']
|
|
178
|
-
.includes(type)&&['CONTINUE','HANDOFF'].includes(message?.action)&&
|
|
179
|
-
sender?.url===chrome.runtime.getURL(message.action.toLowerCase()+'.html')+
|
|
180
|
-
'?requestId='+sourceId||
|
|
181
|
-
['CHAT_LOOP_SESSION_START_PREPARE_V1','CHAT_LOOP_SESSION_START_TAKE_V1',
|
|
182
|
-
'CHAT_LOOP_SESSION_START_RESULT_V1'].includes(type)&&
|
|
183
|
-
sender?.url===chrome.runtime.getURL('start.html')+'?requestId='+sourceId);
|
|
184
|
-
return workerApi.audit.observe('browser.dispatch.'+type,
|
|
185
|
-
// Read dispatchSpan only when the AOP invokes its target: onBegin has
|
|
186
|
-
// already allocated the child Span. Passing it as an eager argument
|
|
187
|
-
// captures undefined before onBegin and breaks Native parent linkage.
|
|
188
|
-
(msg,src,cb)=>onBrowserMessage(msg,src,cb,dispatchSpan),'browser',{
|
|
189
|
-
...(actionSource&&message.traceParent?{parent:message.traceParent}:{}),
|
|
190
|
-
onBegin:ids=>{dispatchSpan=ids;}
|
|
191
|
-
})(message,sender,respond);
|
|
192
|
-
});
|