@tomflow/chatloop-tools 0.9.64 → 0.9.70
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/node_modules/@tomflow/chatloop-context/README.md +296 -0
- package/node_modules/@tomflow/chatloop-context/bin/cli.mjs +48 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/approval.mjs +151 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/context.mjs +376 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/document.mjs +290 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/evidence.mjs +134 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/presentation.mjs +82 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/project.mjs +349 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/sdk.mjs +72 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/start.mjs +73 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/task.mjs +507 -0
- package/node_modules/@tomflow/chatloop-context/capabilities.generated.mjs +11 -0
- package/node_modules/@tomflow/chatloop-context/docs/interfaces.md +261 -0
- package/node_modules/@tomflow/chatloop-context/docs/operator-and-migration.md +277 -0
- package/node_modules/@tomflow/chatloop-context/index.mjs +238 -0
- package/node_modules/@tomflow/chatloop-context/lib/api-log.mjs +86 -0
- package/node_modules/@tomflow/chatloop-context/lib/approval-policy.mjs +77 -0
- package/node_modules/@tomflow/chatloop-context/lib/capability-catalog.generated.json +2036 -0
- package/node_modules/@tomflow/chatloop-context/lib/capability-router.mjs +69 -0
- package/node_modules/@tomflow/chatloop-context/lib/cli-commands.mjs +338 -0
- package/node_modules/@tomflow/chatloop-context/lib/context-cli.mjs +298 -0
- package/node_modules/@tomflow/chatloop-context/lib/context-migration.mjs +286 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/context.mjs +117 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/evidence.mjs +51 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/health.mjs +134 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/migration.mjs +262 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/project.mjs +246 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/review.mjs +180 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/run-cancel.mjs +68 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/run-recovery.mjs +112 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/run.mjs +367 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/store.mjs +398 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/task.mjs +264 -0
- package/node_modules/@tomflow/chatloop-context/lib/line-review.mjs +13 -0
- package/node_modules/@tomflow/chatloop-context/lib/methods.mjs +111 -0
- package/node_modules/@tomflow/chatloop-context/lib/operations.mjs +107 -0
- package/node_modules/@tomflow/chatloop-context/lib/orphan-operator.mjs +118 -0
- package/node_modules/@tomflow/chatloop-context/lib/process-inventory.mjs +20 -0
- package/node_modules/@tomflow/chatloop-context/lib/project-branch.mjs +316 -0
- package/node_modules/@tomflow/chatloop-context/lib/project-diagnostic.mjs +153 -0
- package/node_modules/@tomflow/chatloop-context/lib/project-guards.mjs +19 -0
- package/node_modules/@tomflow/chatloop-context/lib/project-store.mjs +117 -0
- package/node_modules/@tomflow/chatloop-context/lib/trusted-run.mjs +142 -0
- package/node_modules/@tomflow/chatloop-context/lib/workspace-operator.mjs +169 -0
- package/node_modules/@tomflow/chatloop-context/package.json +38 -0
- package/node_modules/@tomflow/chatloop-context/scripts/build-capabilities.mjs +114 -0
- package/node_modules/@tomflow/chatloop-extension/README.md +190 -0
- package/node_modules/@tomflow/chatloop-extension/actions.generated.mjs +13 -0
- package/node_modules/@tomflow/chatloop-extension/bin/cli.mjs +168 -0
- package/node_modules/@tomflow/chatloop-extension/bin/native-host.mjs +246 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/chat_dom.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/chat_screenshot.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/chat_text.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/clean.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/continue.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/entry_test.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/handoff.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/reload.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/session.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/start.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/status.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/clean.js +123 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/common.js +183 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/continue.js +390 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/dom.js +2 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/entry_test.js +241 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/handoff-scoped.js +143 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/handoff.js +399 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/index.js +2 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/model-intent.js +60 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/read-mode.js +9 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/reload.js +20 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/screenshot.js +77 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/session.js +95 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/start.js +329 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/status.js +38 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/text.js +2 -0
- package/node_modules/@tomflow/chatloop-extension/extension/background.js +33 -0
- package/node_modules/@tomflow/chatloop-extension/extension/clean.html +5 -0
- package/node_modules/@tomflow/chatloop-extension/extension/content.js +40 -0
- package/node_modules/@tomflow/chatloop-extension/extension/continue.html +9 -0
- package/node_modules/@tomflow/chatloop-extension/extension/dom.html +17 -0
- package/node_modules/@tomflow/chatloop-extension/extension/entry_test.html +6 -0
- package/node_modules/@tomflow/chatloop-extension/extension/handoff.html +9 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/action-catalog.generated.js +2 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/api-aspect.generated.js +275 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/api-observation-queue.generated.js +84 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/core.js +107 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/index.js +36 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-approval.js +299 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-inspect.js +144 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-poll.js +172 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-recovery.js +109 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/observer.js +211 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/resource-maintenance.js +159 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/resources.js +399 -0
- package/node_modules/@tomflow/chatloop-extension/extension/manifest.json +43 -0
- package/node_modules/@tomflow/chatloop-extension/extension/popup.html +25 -0
- package/node_modules/@tomflow/chatloop-extension/extension/popup.js +99 -0
- package/node_modules/@tomflow/chatloop-extension/extension/reload.html +17 -0
- package/node_modules/@tomflow/chatloop-extension/extension/screenshot.html +17 -0
- package/node_modules/@tomflow/chatloop-extension/extension/session.html +7 -0
- package/node_modules/@tomflow/chatloop-extension/extension/start.html +6 -0
- package/node_modules/@tomflow/chatloop-extension/extension/status.html +6 -0
- package/node_modules/@tomflow/chatloop-extension/extension/text.html +17 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/action-result.js +136 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/action-take.js +218 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/admission.js +127 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/adopt-prepare.js +296 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/adopt-recovery.js +186 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/adopt-result.js +126 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/bootstrap.js +287 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/manual.js +163 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/readiness.js +345 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/router.js +209 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/scheduler.js +385 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/session-control.js +75 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-action-v2.js +278 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-cancel-recovery-v2.js +52 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-cancel-v2.js +93 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-continue-recovery-v2.js +78 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-handoff-recovery-v2.js +148 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-handoff-v2.js +325 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-hold-recovery-v2.js +109 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-intent-dispatch-v2.js +220 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-intent-v2.js +106 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-owner-resolver.js +110 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-recover-v2.js +209 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-routes-v2.js +357 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-run-store.js +400 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-scheduler.js +78 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-start-v2.js +361 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-terminal-v2.js +235 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/turn-failure.js +197 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/turn-finished.js +205 -0
- package/node_modules/@tomflow/chatloop-extension/index.mjs +78 -0
- package/node_modules/@tomflow/chatloop-extension/lib/action-routing.mjs +19 -0
- package/node_modules/@tomflow/chatloop-extension/lib/api-observation-queue.mjs +82 -0
- package/node_modules/@tomflow/chatloop-extension/lib/bridge.mjs +378 -0
- package/node_modules/@tomflow/chatloop-extension/lib/browser-launch.mjs +89 -0
- package/node_modules/@tomflow/chatloop-extension/lib/context-binding.mjs +45 -0
- package/node_modules/@tomflow/chatloop-extension/lib/context-ipc.mjs +122 -0
- package/node_modules/@tomflow/chatloop-extension/lib/owner-evidence.mjs +75 -0
- package/node_modules/@tomflow/chatloop-extension/lib/owner-kernel-lock.mjs +106 -0
- package/node_modules/@tomflow/chatloop-extension/lib/owner-ledger.mjs +316 -0
- package/node_modules/@tomflow/chatloop-extension/lib/owner-locked-write.mjs +30 -0
- package/node_modules/@tomflow/chatloop-extension/lib/scoped-cancel-v2.mjs +190 -0
- package/node_modules/@tomflow/chatloop-extension/lib/scoped-continue-v2.mjs +214 -0
- package/node_modules/@tomflow/chatloop-extension/lib/scoped-handoff-v2.mjs +338 -0
- package/node_modules/@tomflow/chatloop-extension/lib/scoped-native-control.mjs +398 -0
- package/node_modules/@tomflow/chatloop-extension/lib/scoped-recovery-v2.mjs +234 -0
- package/node_modules/@tomflow/chatloop-extension/lib/scoped-retire-v2.mjs +70 -0
- package/node_modules/@tomflow/chatloop-extension/lib/scoped-terminal-v2.mjs +143 -0
- package/node_modules/@tomflow/chatloop-extension/lib/session-file.mjs +228 -0
- package/node_modules/@tomflow/chatloop-extension/package.json +36 -0
- package/node_modules/@tomflow/chatloop-extension/scripts/build-actions.mjs +138 -0
- package/node_modules/@tomflow/chatloop-trace/README.md +118 -0
- package/node_modules/@tomflow/chatloop-trace/events/context.api.mjs +23 -0
- package/node_modules/@tomflow/chatloop-trace/events/typed.api.call.mjs +22 -0
- package/node_modules/@tomflow/chatloop-trace/events.generated.mjs +4 -0
- package/node_modules/@tomflow/chatloop-trace/index.mjs +59 -0
- package/node_modules/@tomflow/chatloop-trace/lib/api-aspect.mjs +273 -0
- package/node_modules/@tomflow/chatloop-trace/lib/trace-reader.mjs +308 -0
- package/node_modules/@tomflow/chatloop-trace/lib/trace-retention.mjs +140 -0
- package/node_modules/@tomflow/chatloop-trace/lib/trace.mjs +236 -0
- package/node_modules/@tomflow/chatloop-trace/package.json +28 -0
- package/node_modules/@tomflow/chatloop-trace/scripts/build-events.mjs +68 -0
- package/node_modules/ps-list/index.d.ts +76 -0
- package/node_modules/ps-list/index.js +366 -0
- package/node_modules/ps-list/license +9 -0
- package/node_modules/ps-list/package.json +44 -0
- package/node_modules/ps-list/readme.md +56 -0
- 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
- package/package.json +12 -4
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
// Chat Loop Worker — scheduler. Imported in order by background.js; no independent Browser Owner.
|
|
2
|
+
// Extension updates invalidate previous injected listeners. Reconnect only its own observers.
|
|
3
|
+
// A detached alarm/reconnect must settle its own failure, not create a
|
|
4
|
+
// second scheduler or implicitly repeat a browser Action.
|
|
5
|
+
const observeBackground=(work,scope)=>{
|
|
6
|
+
void work.catch(error=>console.warn('CHAT_LOOP_SCHEDULER_UNVERIFIED',
|
|
7
|
+
scope,safeWorkerErrorCode(error,'SCHEDULER_ERROR')));
|
|
8
|
+
};
|
|
9
|
+
async function reconnectScopedObservers(){
|
|
10
|
+
if(!scopedTaskRuns||!scopedTaskOwnerResolver||!scopedTaskScheduler)return;
|
|
11
|
+
const state=await scopedTaskRuns.read();
|
|
12
|
+
const tasks=Object.values(state.items).filter(({run})=>
|
|
13
|
+
run.phase==='WAITING_REPLY');
|
|
14
|
+
const jobs=tasks.map(({run,revision})=>scopedTaskScheduler.perTask(
|
|
15
|
+
run.projectId,run.taskId,async()=>{
|
|
16
|
+
const tab=Number.isSafeInteger(run.tabId)&&run.tabId>0
|
|
17
|
+
?await chrome.tabs.get(run.tabId).catch(()=>null):null;
|
|
18
|
+
if(!tab||pathOf(tab.url)!==run.page)return;
|
|
19
|
+
const route=await scopedTaskOwnerResolver.resolve({
|
|
20
|
+
id:chrome.runtime.id,frameId:0,tab:{id:tab.id},url:tab.url});
|
|
21
|
+
if(route.projectId!==run.projectId||route.taskId!==run.taskId
|
|
22
|
+
||route.sessionId!==run.sessionId
|
|
23
|
+
||route.generation!==run.generation
|
|
24
|
+
||route.nativeOwnerRevision!==run.nativeOwnerRevision
|
|
25
|
+
||route.runRevision!==revision)return;
|
|
26
|
+
const latest=await chrome.tabs.get(tab.id).catch(()=>null);
|
|
27
|
+
if(!latest||latest.url!==tab.url)return;
|
|
28
|
+
// Only install the existing Observer on its CURRENT owner. Its
|
|
29
|
+
// baseline is historical unless this Task has the exact user Turn
|
|
30
|
+
// from an already committed SEND_BEGIN+DOM+Native+Context receipt.
|
|
31
|
+
await chrome.scripting.executeScript({target:{tabId:tab.id},
|
|
32
|
+
files:["content.js","loop/core.js","loop/observer-inspect.js",
|
|
33
|
+
"loop/observer-poll.js","loop/observer-recovery.js",
|
|
34
|
+
"loop/observer-approval.js","loop/observer.js"]});
|
|
35
|
+
if(run.awaitingUserKey){
|
|
36
|
+
const owner=await scopedTaskRuns.get(run.projectId,run.taskId);
|
|
37
|
+
if(owner.runRevision!==revision
|
|
38
|
+
||owner.run?.awaitingUserKey!==run.awaitingUserKey)return;
|
|
39
|
+
const response=await chrome.tabs.sendMessage(tab.id,{
|
|
40
|
+
type:'CHAT_LOOP_TURN_RESUME_V1',page:run.page,
|
|
41
|
+
userKey:run.awaitingUserKey,assistantKey:null,
|
|
42
|
+
startedAt:run.startedAt||Date.now()}).catch(()=>null);
|
|
43
|
+
if(response?.status!=='RESUMED'
|
|
44
|
+
&&response?.status!=='ALREADY_MONITORING')
|
|
45
|
+
console.warn('TASK_SCOPED_AWAITED_OBSERVER_UNVERIFIED',
|
|
46
|
+
response?.reason||response?.status||'UNVERIFIED');
|
|
47
|
+
}
|
|
48
|
+
}));
|
|
49
|
+
const settled=await Promise.allSettled(jobs);
|
|
50
|
+
for(const item of settled)if(item.status==='rejected')
|
|
51
|
+
console.warn('CHAT_LOOP_SCOPED_RECONNECT_UNVERIFIED',
|
|
52
|
+
safeWorkerErrorCode(item.reason,'TASK_SCOPED_OWNER_UNVERIFIED'));
|
|
53
|
+
}
|
|
54
|
+
async function reconnectOpenChats() {
|
|
55
|
+
// Native and Worker must both prove V1 may reconnect. A scoped Task gets
|
|
56
|
+
// only its own read-only Observer, never legacy Turn RESUME.
|
|
57
|
+
if(await hasActiveScopedTask())return reconnectScopedObservers();
|
|
58
|
+
await assertLegacyWorkerWritable({native:true});
|
|
59
|
+
const tabs = await chrome.tabs.query({url: "https://chatgpt.com/*"});
|
|
60
|
+
for (const tab of tabs) {
|
|
61
|
+
await assertLegacyWorkerWritable({native:true});
|
|
62
|
+
if (!Number.isInteger(tab.id) || !pathOf(tab.url) && !/^https:\/\/chatgpt\.com\/$/.test(tab.url)) continue;
|
|
63
|
+
try {
|
|
64
|
+
await chrome.scripting.executeScript({target: {tabId: tab.id},
|
|
65
|
+
files: ["content.js", "loop/core.js", "loop/observer-inspect.js", "loop/observer-poll.js", "loop/observer-recovery.js", "loop/observer-approval.js", "loop/observer.js"]});
|
|
66
|
+
const owner = await readRun();
|
|
67
|
+
if (owner?.phase === "WAITING_REPLY" && owner.tabId === tab.id
|
|
68
|
+
&& owner.page === pathOf(tab.url) && owner.newUserKey) {
|
|
69
|
+
// Normal WAITING_REPLY uses its original zero-extra-read resume.
|
|
70
|
+
// Only a previously seen pending confirmation needs an extra DOM
|
|
71
|
+
// identity check for a human response that arrived during reload.
|
|
72
|
+
const proof=owner.reason==="PENDING_CONFIRMATION_DOM"
|
|
73
|
+
? await chrome.tabs.sendMessage(tab.id,
|
|
74
|
+
{type:"CHAT_LOOP_TURN_OBSERVE_V1",poll:true}).catch(()=>null)
|
|
75
|
+
:null;
|
|
76
|
+
const freshUser=proof?.page===owner.page
|
|
77
|
+
&& proof.userKey!==owner.newUserKey
|
|
78
|
+
&& proof.priorAssistantKey===owner.assistantKey
|
|
79
|
+
&& proof.priorAssistantUserKey===owner.userKey
|
|
80
|
+
&& proof.directReplyAfterPriorAssistant===true;
|
|
81
|
+
await assertLegacyWorkerWritable({native:true});
|
|
82
|
+
const res = await chrome.tabs.sendMessage(tab.id, {
|
|
83
|
+
type: "CHAT_LOOP_TURN_RESUME_V1", page: owner.page,
|
|
84
|
+
userKey:freshUser?proof.userKey:owner.newUserKey,
|
|
85
|
+
assistantKey: owner.assistantKey || null,
|
|
86
|
+
...(owner.reason==='ADOPTED_IDLE_BASELINE'?{baselineOnly:true}:{}),
|
|
87
|
+
startedAt: owner.startedAt
|
|
88
|
+
}).catch(() => null);
|
|
89
|
+
if (res?.status !== "RESUMED" && res?.status !== "ALREADY_MONITORING")
|
|
90
|
+
console.warn("CHAT_LOOP_TURN_RESUME_UNVERIFIED",
|
|
91
|
+
safeWorkerErrorCode({code:res?.reason||res?.status},
|
|
92
|
+
"OBSERVER_RESUME_UNVERIFIED"));
|
|
93
|
+
}
|
|
94
|
+
} catch (error) {
|
|
95
|
+
console.warn("CHAT_LOOP_OBSERVER_RECONNECT_UNVERIFIED",
|
|
96
|
+
safeWorkerErrorCode(error,"OBSERVER_RECONNECT_UNVERIFIED"));
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
// MV3 background service workers can suspend, and hidden-page setInterval
|
|
101
|
+
// may be throttled. An alarm asks the Observer to sample, never acts directly.
|
|
102
|
+
async function ensureAlarm() {
|
|
103
|
+
if (!chrome.alarms?.create) return;
|
|
104
|
+
const existing = await chrome.alarms.get?.(ALARM_NAME).catch(() => null);
|
|
105
|
+
if (!existing) await chrome.alarms.create(ALARM_NAME, {periodInMinutes: 1});
|
|
106
|
+
}
|
|
107
|
+
async function retireScopedFinished(){
|
|
108
|
+
if(!scopedTaskRuns||!scopedTaskScheduler)return;
|
|
109
|
+
const state=await scopedTaskRuns.read(),now=Date.now();
|
|
110
|
+
const candidates=Object.values(state.items).filter(({run})=>
|
|
111
|
+
['DONE','CANCELLED'].includes(run.phase)
|
|
112
|
+
&&!run.resourceTransfer
|
|
113
|
+
&&Number.isSafeInteger(run.terminalAt)
|
|
114
|
+
&&now-run.terminalAt>=24*60*60*1000);
|
|
115
|
+
const jobs=candidates.map(({run})=>scopedTaskScheduler.perTask(
|
|
116
|
+
run.projectId,run.taskId,async()=>{
|
|
117
|
+
const current=await scopedTaskRuns.get(run.projectId,run.taskId);
|
|
118
|
+
const exact=current.run;
|
|
119
|
+
if(!exact||exact.resourceTransfer||!['DONE','CANCELLED'].includes(exact.phase)
|
|
120
|
+
||exact.sessionId!==run.sessionId
|
|
121
|
+
||exact.generation!==run.generation
|
|
122
|
+
||exact.nativeOwnerRevision!==run.nativeOwnerRevision
|
|
123
|
+
||exact.terminalAt!==run.terminalAt
|
|
124
|
+
||!await scopedEnsureTerminalQuota(exact))return;
|
|
125
|
+
const receipt=await chrome.runtime.sendNativeMessage(SESSION_HOST,{
|
|
126
|
+
op:'task-retire-v2',projectId:run.projectId,
|
|
127
|
+
taskId:run.taskId,sessionId:run.sessionId,
|
|
128
|
+
generation:run.generation,
|
|
129
|
+
expectedSessionRevision:run.nativeOwnerRevision}).catch(()=>null);
|
|
130
|
+
if(receipt?.status!=='OK'
|
|
131
|
+
||receipt.scope!=='TASK_SCOPED_RETIRED')return;
|
|
132
|
+
// A lost deletion ACK never reclaims another Task slot. Context
|
|
133
|
+
// remains the permanent history; Native already reverified terminal.
|
|
134
|
+
await scopedTaskRuns.retire(run.projectId,run.taskId,{
|
|
135
|
+
expectedRunRevision:current.runRevision,receipt});
|
|
136
|
+
}));
|
|
137
|
+
const outcomes=await Promise.allSettled(jobs);
|
|
138
|
+
for(const item of outcomes)if(item.status==='rejected')
|
|
139
|
+
console.warn('CHAT_LOOP_RETIRE_UNVERIFIED',
|
|
140
|
+
safeWorkerErrorCode(item.reason,'SCOPED_RETIRE_UNVERIFIED'));
|
|
141
|
+
}
|
|
142
|
+
async function observeScopedDueTasks(){
|
|
143
|
+
if(!scopedTaskScheduler||!scopedTaskRuns)return;
|
|
144
|
+
// This is the SAME one-minute Worker alarm. Per-Task queues may overlap
|
|
145
|
+
// read-only observations; no new timer, browser sender or Task writer.
|
|
146
|
+
const candidates=await scopedTaskScheduler.due();
|
|
147
|
+
for(const candidate of candidates){
|
|
148
|
+
const observation=scopedTaskScheduler.dispatchDue(candidate,async()=>{
|
|
149
|
+
if(candidate.phase==='PENDING'){
|
|
150
|
+
const state=await scopedTaskRuns.get(
|
|
151
|
+
candidate.projectId,candidate.taskId);
|
|
152
|
+
if(state.run?.handoffReceipt)
|
|
153
|
+
await scopedTaskReconcileHandoffPending(state.run);
|
|
154
|
+
else if(state.run?.continueReceipt)
|
|
155
|
+
await scopedTaskReconcileContinuePending(state.run);
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
if(candidate.phase!=='WAITING_REPLY')return;
|
|
159
|
+
const state=await scopedTaskRuns.get(candidate.projectId,candidate.taskId);
|
|
160
|
+
const run=state.run;
|
|
161
|
+
if(!Number.isSafeInteger(run?.tabId)||run.tabId<=0
|
|
162
|
+
||typeof run.page!=='string')return;
|
|
163
|
+
const tab=await chrome.tabs.get(run.tabId).catch(()=>null);
|
|
164
|
+
if(!tab||pathOf(tab.url)!==run.page||!scopedTaskOwnerResolver)return;
|
|
165
|
+
// A DONE marker may predate Context delivery. Retry only the SAME
|
|
166
|
+
// stored Turn's read-only proof on the existing alarm; this queues
|
|
167
|
+
// behind the present Task without waiting on its own queue.
|
|
168
|
+
if(['DONE','BLOCKED'].includes(run.lastObservedTurn?.outcome)
|
|
169
|
+
&&!run.modelIntent&&!run.nextAttempt){
|
|
170
|
+
const proof=run.lastObservedTurn;
|
|
171
|
+
void scopedTaskTerminalObserved({
|
|
172
|
+
page:proof.page,userKey:proof.userKey,
|
|
173
|
+
assistantKey:proof.assistantKey,outcome:proof.outcome
|
|
174
|
+
},{id:chrome.runtime.id,frameId:0,
|
|
175
|
+
tab:{id:tab.id},url:tab.url}).catch(error=>{
|
|
176
|
+
if(error?.code!=='SCOPED_DONE_DELIVERY_NOT_COMMITTED')
|
|
177
|
+
console.warn('TASK_SCOPED_TERMINAL_RETRY_UNVERIFIED',
|
|
178
|
+
safeWorkerErrorCode(error,'SCOPED_TERMINAL_UNVERIFIED'));
|
|
179
|
+
});
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
// Even a read-only V2 timer must not inspect a stale or re-assigned
|
|
183
|
+
// Task Tab. The exact owner resolver reads Native and Context again;
|
|
184
|
+
// its result cannot become a Turn token or a send permit.
|
|
185
|
+
const route=await scopedTaskOwnerResolver.resolve({
|
|
186
|
+
id:chrome.runtime.id,frameId:0,tab:{id:tab.id},url:tab.url});
|
|
187
|
+
if(route.projectId!==candidate.projectId
|
|
188
|
+
||route.taskId!==candidate.taskId
|
|
189
|
+
||route.sessionId!==candidate.sessionId
|
|
190
|
+
||route.generation!==candidate.generation
|
|
191
|
+
||route.nativeOwnerRevision!==candidate.nativeOwnerRevision
|
|
192
|
+
||route.runRevision!==candidate.runRevision)return;
|
|
193
|
+
// Observe a page fault before considering any queued Action.
|
|
194
|
+
// This is the existing read-only snapshot, not a fresh DOM poll.
|
|
195
|
+
const sample=await chrome.tabs.sendMessage(tab.id,{
|
|
196
|
+
type:'CHAT_LOOP_TURN_OBSERVE_V1'}).catch(()=>null);
|
|
197
|
+
if(sample?.page!==route.page
|
|
198
|
+
||sample.contract!=='chat-loop.turn-observer.v1')return;
|
|
199
|
+
if(scopedReconcileObservedFault(run,tab,route,sample))return;
|
|
200
|
+
// Even an unrecognized visible failure never launches a queued sender.
|
|
201
|
+
if(sample.status==='BLOCKED')return;
|
|
202
|
+
// A tab created before a lost Chrome ACK is reconciled by its
|
|
203
|
+
// unique existing URL. Never create a second Action for LAUNCHING
|
|
204
|
+
// or ACTION_UNVERIFIED, and never infer a send from a tab alone.
|
|
205
|
+
if(['LAUNCHING','ACTION_UNVERIFIED'].includes(run.modelIntent?.state)
|
|
206
|
+
&&run.modelIntent.actionTabId===null){
|
|
207
|
+
await scopedRecoverLaunchedIntent(run.projectId,run.taskId);
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
// The model may have queued an intent DURING the assistant Turn,
|
|
211
|
+
// while its eventual Observer ACK was lost. A stored exact completed
|
|
212
|
+
// Turn permits only a once-fenced original Action creation.
|
|
213
|
+
if(['QUEUED','CAPACITY_BLOCKED'].includes(run.modelIntent?.state)
|
|
214
|
+
&&run.modelIntent.targetUserKey===run.lastObservedTurn?.userKey
|
|
215
|
+
&&run.lastObservedTurn.pendingConfirmation===false){
|
|
216
|
+
if(run.modelIntent.action===run.lastObservedTurn.outcome)
|
|
217
|
+
await scopedLaunchReadyIntent(run.projectId,run.taskId);
|
|
218
|
+
else await scopedSettleMismatchedIntent(
|
|
219
|
+
run.projectId,run.taskId);
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
// A Task whose *confirmed* send produced this precise User Turn
|
|
223
|
+
// cannot silently lose the subsequent assistant Turn on MV3 restart
|
|
224
|
+
// or a Native transfer race. Re-arm the existing observer only for
|
|
225
|
+
// the same ID; never infer a new sender, never retry a send.
|
|
226
|
+
const rearmUserKey=run.awaitingUserKey
|
|
227
|
+
||(run.modelIntent?.state==='QUEUED'
|
|
228
|
+
?run.modelIntent.targetUserKey:null);
|
|
229
|
+
if(rearmUserKey&&rearmUserKey===sample.userKey
|
|
230
|
+
&&['IDLE','UNKNOWN','TURN_FINISHED'].includes(sample.status)){
|
|
231
|
+
await chrome.tabs.sendMessage(tab.id,{
|
|
232
|
+
type:'CHAT_LOOP_TURN_RESUME_V1',page:run.page,
|
|
233
|
+
userKey:rearmUserKey,assistantKey:null,
|
|
234
|
+
startedAt:run.startedAt||Date.now()}).catch(()=>null);
|
|
235
|
+
}
|
|
236
|
+
// The sample and bounded re-arm are Observer-only. Context and
|
|
237
|
+
// the Chrome composer are not written by this Alarm.
|
|
238
|
+
});
|
|
239
|
+
observeBackground(observation,'TASK_SCOPED_READ_ONLY_POLL');
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
async function watchActiveTurn() {
|
|
243
|
+
// Exactly the existing Worker alarm. Reconcile the original Resource
|
|
244
|
+
// Ledger with ALL retained Task receipts; no second scheduler or sender.
|
|
245
|
+
observeBackground(reconcileScopedResourceInventory(),
|
|
246
|
+
'TASK_SCOPED_RESOURCE_RECONCILE');
|
|
247
|
+
// Archive terminal Task slots only after Native+Context prove finality.
|
|
248
|
+
observeBackground(retireScopedFinished(),'TASK_TERMINAL_RETIRE');
|
|
249
|
+
observeBackground(reconcileScopedAbortedHolds(),
|
|
250
|
+
'TASK_PRE_SEND_HOLD_RECONCILE');
|
|
251
|
+
observeBackground(reconcileScopedHandoffResources(),
|
|
252
|
+
'TASK_HANDOFF_RESOURCE_RECONCILE');
|
|
253
|
+
observeBackground(reconcileScopedCancelled(),
|
|
254
|
+
'TASK_CANCEL_ACK_RECONCILE');
|
|
255
|
+
if(await hasActiveScopedTask()){
|
|
256
|
+
await observeScopedDueTasks();
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
await assertLegacyWorkerWritable({native:true});
|
|
260
|
+
// One bounded reconciliation on the existing 1-minute Worker alarm.
|
|
261
|
+
await serial(async()=>{await assertLegacyWorkerWritable();
|
|
262
|
+
await expireStaleAction();
|
|
263
|
+
await resources.reconcile(await readRun()).catch(()=>{});});
|
|
264
|
+
// Normal periodic wakeups also reconcile a delayed one-shot deadline.
|
|
265
|
+
// All state changes share the same serial queue as Turn/Action receipts.
|
|
266
|
+
await serial(async()=>{await assertLegacyWorkerWritable();
|
|
267
|
+
await expireStaleModelIntent();await prepareHandoffIfDue();});
|
|
268
|
+
await assertLegacyWorkerWritable();
|
|
269
|
+
const run = await readRun();
|
|
270
|
+
// While an Action is pending, never dispatch competing operations.
|
|
271
|
+
if (run && ["RESERVED", "PENDING"].includes(run.phase)) return;
|
|
272
|
+
const tabs = run?.phase === "WAITING_REPLY"
|
|
273
|
+
? [await chrome.tabs.get(run.tabId).catch(() => null)]
|
|
274
|
+
: await chrome.tabs.query({url: "https://chatgpt.com/*"});
|
|
275
|
+
for (const tab of tabs) {
|
|
276
|
+
await assertLegacyWorkerWritable({native:true});
|
|
277
|
+
if (!tab?.id || !pathOf(tab.url)) continue;
|
|
278
|
+
try {
|
|
279
|
+
const proof = await chrome.tabs.sendMessage(tab.id,
|
|
280
|
+
{type: "CHAT_LOOP_TURN_OBSERVE_V1", poll: true});
|
|
281
|
+
// A reloaded Observer may have mistaken the in-flight user Turn for
|
|
282
|
+
// historical baseline. Its IDLE state is not proof of completion.
|
|
283
|
+
// Rebind the exact persisted owner using read-only verification.
|
|
284
|
+
const freshHuman=run?.phase==="WAITING_REPLY" && run.tabId===tab.id
|
|
285
|
+
&& proof?.page===run.page && proof.userKey!==run.newUserKey
|
|
286
|
+
&& proof.priorAssistantKey===run.assistantKey
|
|
287
|
+
&& proof.priorAssistantUserKey===run.userKey
|
|
288
|
+
&& proof.directReplyAfterPriorAssistant===true;
|
|
289
|
+
if (run?.phase === "WAITING_REPLY" && run.tabId === tab.id
|
|
290
|
+
&& proof?.page === run.page
|
|
291
|
+
&& (proof.userKey === run.newUserKey || freshHuman)
|
|
292
|
+
&& (proof.status === "IDLE"
|
|
293
|
+
|| proof.status === "UNKNOWN"
|
|
294
|
+
&& ["ATTACHING_CHAT", "WAITING_CHAT_HYDRATION"].includes(proof.reason))) {
|
|
295
|
+
await assertLegacyWorkerWritable({native:true});
|
|
296
|
+
await chrome.tabs.sendMessage(tab.id, {
|
|
297
|
+
type: "CHAT_LOOP_TURN_RESUME_V1", page: run.page,
|
|
298
|
+
userKey: freshHuman?proof.userKey:run.newUserKey,
|
|
299
|
+
assistantKey: run.assistantKey || null,
|
|
300
|
+
...(run.reason==='ADOPTED_IDLE_BASELINE'?{baselineOnly:true}:{}),
|
|
301
|
+
startedAt: run.startedAt
|
|
302
|
+
}).catch(() => null);
|
|
303
|
+
}
|
|
304
|
+
// A human response can fail after the previous question was kept in
|
|
305
|
+
// WAITING_REPLY. Do not let this become an indefinite silent wait.
|
|
306
|
+
if(freshHuman && proof.status==="BLOCKED") {
|
|
307
|
+
observeBackground(serial(async()=>{
|
|
308
|
+
await assertLegacyWorkerWritable({native:true});
|
|
309
|
+
return onTurnBlocked({page:run.page,userKey:proof.userKey,
|
|
310
|
+
reason:proof.reason||"PAGE_ERROR"},{tab,url:tab.url});
|
|
311
|
+
}),'HUMAN_TURN_RECOVERY');
|
|
312
|
+
}
|
|
313
|
+
// Alarm is still read-only. It may reconcile a verified page failure if
|
|
314
|
+
// the observer's original notification was lost while Worker slept.
|
|
315
|
+
if (run?.phase === "WAITING_REPLY" && run.tabId === tab.id
|
|
316
|
+
&& proof?.status === "BLOCKED" && ["PAGE_ERROR","CONTEXT_LIMIT","SECURITY_CHALLENGE"].includes(proof.reason)
|
|
317
|
+
&& proof.page === run.page && proof.userKey === run.newUserKey) {
|
|
318
|
+
observeBackground(serial(async()=>{
|
|
319
|
+
await assertLegacyWorkerWritable({native:true});
|
|
320
|
+
return onTurnBlocked({page:run.page,
|
|
321
|
+
userKey:run.newUserKey,reason:proof.reason},{tab,url:tab.url});
|
|
322
|
+
}),'TURN_FAILURE_RECOVERY');
|
|
323
|
+
}
|
|
324
|
+
} catch { /* Observer unavailable; never infer Turn completion. */ }
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
chrome.alarms?.onAlarm?.addListener(alarm => {
|
|
328
|
+
if (alarm?.name === ALARM_NAME)
|
|
329
|
+
observeBackground(watchActiveTurn(),'TURN_WATCH_ALARM');
|
|
330
|
+
if (alarm?.name === DEADLINE_ALARM_NAME)
|
|
331
|
+
observeBackground(serial(async()=>{
|
|
332
|
+
await assertLegacyWorkerWritable({native:true});
|
|
333
|
+
return prepareHandoffIfDue();
|
|
334
|
+
}),'HANDOFF_DEADLINE_ALARM');
|
|
335
|
+
});
|
|
336
|
+
chrome.runtime.onInstalled?.addListener(() => {
|
|
337
|
+
observeBackground(ensureAlarm(),'ALARM_SETUP');
|
|
338
|
+
observeBackground(serial(async()=>{
|
|
339
|
+
if(await hasActiveScopedTask())return observeScopedDueTasks();
|
|
340
|
+
await assertLegacyWorkerWritable({native:true});
|
|
341
|
+
await expireStaleAction();
|
|
342
|
+
await resources.reconcile(await readRun()).catch(()=>{});}),
|
|
343
|
+
'INSTALL_RECONCILIATION');
|
|
344
|
+
observeBackground(reconnectOpenChats(),'CHAT_RECONNECT');
|
|
345
|
+
});
|
|
346
|
+
chrome.runtime.onStartup?.addListener(() => {
|
|
347
|
+
observeBackground(ensureAlarm(),'ALARM_SETUP');
|
|
348
|
+
observeBackground(serial(async()=>{
|
|
349
|
+
if(await hasActiveScopedTask())return observeScopedDueTasks();
|
|
350
|
+
await assertLegacyWorkerWritable({native:true});
|
|
351
|
+
await expireStaleAction();
|
|
352
|
+
await resources.reconcile(await readRun()).catch(()=>{});}),
|
|
353
|
+
'INSTALL_RECONCILIATION');
|
|
354
|
+
observeBackground(reconnectOpenChats(),'CHAT_RECONNECT');
|
|
355
|
+
});
|
|
356
|
+
observeBackground(ensureAlarm(),'ALARM_SETUP');
|
|
357
|
+
// Reconcile an elapsed deadline after MV3 Worker suspension/reload.
|
|
358
|
+
observeBackground(serial(async()=>{
|
|
359
|
+
if(await hasActiveScopedTask())return observeScopedDueTasks();
|
|
360
|
+
await assertLegacyWorkerWritable({native:true});
|
|
361
|
+
await expireStaleAction();
|
|
362
|
+
await resources.reconcile(await readRun()).catch(()=>{});
|
|
363
|
+
await prepareHandoffIfDue();}),'STARTUP_RECONCILIATION');
|
|
364
|
+
// A manual extension Reload does not necessarily fire onInstalled/onStartup.
|
|
365
|
+
// Reattach the two idempotent read-only scripts to existing ChatGPT tabs.
|
|
366
|
+
observeBackground(reconnectOpenChats(),'CHAT_RECONNECT');
|
|
367
|
+
// User-clicked popup Clean must share the same Action quota as Worker actions.
|
|
368
|
+
// The popup cannot create extra Chrome tabs directly.
|
|
369
|
+
async function openPopupClean(message,sender) {
|
|
370
|
+
if(sender?.url!==chrome.runtime.getURL('popup.html')
|
|
371
|
+
||!Number.isSafeInteger(message?.tabId)||message.tabId<=0)
|
|
372
|
+
return {status:'DENIED',code:'POPUP_SOURCE_INVALID'};
|
|
373
|
+
const target=await chrome.tabs.get(message.tabId).catch(()=>null);
|
|
374
|
+
let valid=false;
|
|
375
|
+
try {valid=target?.active===true && Number.isSafeInteger(target.windowId)
|
|
376
|
+
&& new URL(target.url).origin==='https://chatgpt.com';} catch {}
|
|
377
|
+
if(!valid)return {status:'DENIED',code:'CLEAN_TARGET_UNVERIFIED'};
|
|
378
|
+
try {
|
|
379
|
+
const action=await createBoundedAction({url:chrome.runtime.getURL('clean.html')
|
|
380
|
+
+'?tabId='+target.id,windowId:target.windowId,active:true});
|
|
381
|
+
return {status:'OK',actionTabId:action.id};
|
|
382
|
+
} catch(error) {
|
|
383
|
+
return {status:'BLOCKED',code:safeWorkerErrorCode(error,'CLEAN_ACTION_UNVERIFIED')};
|
|
384
|
+
}
|
|
385
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// Single Worker Owner lifecycle operation. This does not send Chat messages,
|
|
2
|
+
// claim another Owner or finish the business Task.
|
|
3
|
+
async function sessionControl(message,sender){
|
|
4
|
+
if(!Number.isInteger(sender.tab?.id)
|
|
5
|
+
||sender.url!==chrome.runtime.getURL('session.html')
|
|
6
|
+
+'?requestId='+message.requestId+'&operation=cancel'
|
|
7
|
+
||!/^[a-f0-9]{32}$/.test(message.requestId||''))
|
|
8
|
+
return {status:'DENIED',code:'INVALID_SESSION_SOURCE'};
|
|
9
|
+
// URL parameters locate a private request, never authorize mutation.
|
|
10
|
+
const request=await chrome.runtime.sendNativeMessage(SESSION_HOST,{
|
|
11
|
+
op:'acquire',id:message.requestId,action:'session'
|
|
12
|
+
}).catch(()=>null);
|
|
13
|
+
const intent=request?.intent;
|
|
14
|
+
if(request?.status!=='OK'||request.id!==message.requestId
|
|
15
|
+
||request.action!=='session'||intent?.operation!=='cancel'
|
|
16
|
+
||message.operation!=='cancel'||intent.confirmed!==true)
|
|
17
|
+
return {status:'DENIED',code:'SESSION_INTENT_UNVERIFIED'};
|
|
18
|
+
const run=await readRun();
|
|
19
|
+
const id=run?.sessionId;
|
|
20
|
+
// V1 intentionally supports ONLY a started but never executing Adopt.
|
|
21
|
+
// A missing browser tab cannot be used to invent an Owner or cancel work.
|
|
22
|
+
if(!id||id!==intent.sessionId
|
|
23
|
+
||run.projectId!==intent.projectId||run.taskId!==intent.taskId
|
|
24
|
+
||run.generation!==intent.generation||run.phase!=='WAITING_REPLY'
|
|
25
|
+
||run.reason!=='ADOPTED_IDLE_BASELINE'||run.action!=='START'
|
|
26
|
+
||!run.contextManaged?.storeEpoch
|
|
27
|
+
||run.contextManaged.storeEpoch!==intent.contextStoreEpoch
|
|
28
|
+
||run.modelIntent?.action ||run.pendingAttempt ||run.contextHandoff)
|
|
29
|
+
return {status:'DENIED',code:'SESSION_CANCEL_NOT_IDLE'};
|
|
30
|
+
const before=await chrome.runtime.sendNativeMessage(SESSION_HOST,{
|
|
31
|
+
op:'session-read'
|
|
32
|
+
}).catch(()=>null);
|
|
33
|
+
if(before?.status!=='OK'
|
|
34
|
+
||!matchesNativeProjection(sessionProjection(run),before.session)
|
|
35
|
+
||before.session.updatedAt!==intent.expectedNativeUpdatedAt)
|
|
36
|
+
return {status:'DENIED',code:'SESSION_CANCEL_NATIVE_MISMATCH'};
|
|
37
|
+
// Stop scheduling FIRST. Do not release Native ID or Context writer here.
|
|
38
|
+
const stopped={...run,phase:'BLOCKED',
|
|
39
|
+
reason:'SESSION_CANCEL_PENDING',cancelRequestId:message.requestId,
|
|
40
|
+
cancelFreezeExpectedAt:intent.expectedNativeUpdatedAt};
|
|
41
|
+
await saveRun(stopped);
|
|
42
|
+
const disk=await chrome.runtime.sendNativeMessage(SESSION_HOST,{
|
|
43
|
+
op:'session-read'
|
|
44
|
+
}).catch(()=>null);
|
|
45
|
+
if(disk?.status!=='OK'||disk.session?.phase!=='BLOCKED'
|
|
46
|
+
||!matchesNativeProjection(sessionProjection(stopped),disk.session))
|
|
47
|
+
return {status:'BLOCKED',code:'SESSION_CANCEL_FREEZE_UNVERIFIED'};
|
|
48
|
+
const writeRequestId='cancel-'+message.requestId;
|
|
49
|
+
const fenced=await chrome.runtime.sendNativeMessage(SESSION_HOST,{
|
|
50
|
+
op:'context-control',command:'run.fence.cancel',
|
|
51
|
+
args:{expectedStoreEpoch:intent.contextStoreEpoch,
|
|
52
|
+
writeRequestId,expectedTaskRevision:intent.expectedTaskRevision,
|
|
53
|
+
expectedBindingRevision:intent.expectedBindingRevision,
|
|
54
|
+
actionToken:message.requestId,reason:'USER_CANCELLED'}
|
|
55
|
+
}).catch(()=>null);
|
|
56
|
+
if(fenced?.status!=='OK'||fenced.result?.status!=='CANCEL_FENCED')
|
|
57
|
+
return {status:'BLOCKED',code:fenced?.code||'CONTEXT_CANCEL_FENCE_UNVERIFIED'};
|
|
58
|
+
// A business receipt alone is not enough; read back the same durable state.
|
|
59
|
+
const observed=await chrome.runtime.sendNativeMessage(SESSION_HOST,{
|
|
60
|
+
op:'context-control',command:'run.terminalStatus',args:{}
|
|
61
|
+
}).catch(()=>null);
|
|
62
|
+
const c=observed?.result?.sessionCancellation;
|
|
63
|
+
if(observed?.status!=='OK'||observed.result.runRef!==null
|
|
64
|
+
||c?.status!=='CANCELLED_NOT_DELIVERED'
|
|
65
|
+
||c.requestId!==writeRequestId||c.sessionId!==id
|
|
66
|
+
||c.generation!==run.generation)
|
|
67
|
+
return {status:'BLOCKED',code:'CONTEXT_CANCEL_FENCE_UNVERIFIED'};
|
|
68
|
+
const next={...stopped,phase:'CANCELLED',reason:'SESSION_CANCELLED',
|
|
69
|
+
cancelBlockedUpdatedAt:disk.session.updatedAt,
|
|
70
|
+
contextCancelFence:'CONFIRMED'};
|
|
71
|
+
// The Native Host validates Context again, then exact BLOCKED CAS.
|
|
72
|
+
await saveRun(next);
|
|
73
|
+
return {status:'OK',state:'CANCELLED_NOT_DELIVERED',sessionId:id,
|
|
74
|
+
taskId:run.taskId,bindingRevision:observed.result.bindingRevision};
|
|
75
|
+
}
|