@tomflow/chatloop-tools 0.9.63 → 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/README.md +13 -1
- package/index.mjs +7 -7
- package/lib/context-control-run.mjs +23 -5
- package/lib/extension-proxy.mjs +16 -7
- package/lib/native-owner-v2.mjs +128 -0
- package/lib/release-health-checks.mjs +29 -5
- package/lib/trace-gateway.mjs +3 -1
- package/node_modules/@tomflow/chatloop-context/README.md +56 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/document.mjs +24 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/start.mjs +1 -1
- package/node_modules/@tomflow/chatloop-context/capabilities/task.mjs +2 -2
- package/node_modules/@tomflow/chatloop-context/docs/interfaces.md +69 -0
- package/node_modules/@tomflow/chatloop-context/index.mjs +3 -1
- package/node_modules/@tomflow/chatloop-context/lib/capability-catalog.generated.json +50 -3
- package/node_modules/@tomflow/chatloop-context/lib/cli-commands.mjs +54 -1
- package/node_modules/@tomflow/chatloop-context/lib/core/context.mjs +2 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/health.mjs +61 -2
- package/node_modules/@tomflow/chatloop-context/lib/core/review.mjs +33 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/run-cancel.mjs +14 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/run-recovery.mjs +112 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/run.mjs +31 -39
- package/node_modules/@tomflow/chatloop-context/lib/core/store.mjs +6 -5
- package/node_modules/@tomflow/chatloop-context/lib/core/task.mjs +84 -3
- package/node_modules/@tomflow/chatloop-context/lib/trusted-run.mjs +52 -7
- package/node_modules/@tomflow/chatloop-context/package.json +2 -4
- package/node_modules/@tomflow/chatloop-extension/README.md +66 -0
- package/node_modules/@tomflow/chatloop-extension/bin/cli.mjs +19 -4
- package/node_modules/@tomflow/chatloop-extension/bin/native-host.mjs +40 -1
- package/node_modules/@tomflow/chatloop-extension/capabilities/session.mjs +1 -1
- package/node_modules/@tomflow/chatloop-extension/extension/actions/continue.js +54 -4
- package/node_modules/@tomflow/chatloop-extension/extension/actions/handoff-scoped.js +143 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/handoff.js +9 -3
- package/node_modules/@tomflow/chatloop-extension/extension/actions/model-intent.js +9 -5
- package/node_modules/@tomflow/chatloop-extension/extension/actions/session.js +53 -10
- package/node_modules/@tomflow/chatloop-extension/extension/actions/start.js +34 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/status.js +16 -2
- package/node_modules/@tomflow/chatloop-extension/extension/background.js +16 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/index.js +1 -1
- package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-poll.js +7 -6
- package/node_modules/@tomflow/chatloop-extension/extension/loop/observer.js +64 -7
- package/node_modules/@tomflow/chatloop-extension/extension/loop/resource-maintenance.js +159 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/resources.js +235 -95
- package/node_modules/@tomflow/chatloop-extension/extension/manifest.json +1 -0
- package/node_modules/@tomflow/chatloop-extension/extension/session.html +1 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/admission.js +3 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/bootstrap.js +73 -16
- package/node_modules/@tomflow/chatloop-extension/extension/worker/router.js +23 -6
- package/node_modules/@tomflow/chatloop-extension/extension/worker/scheduler.js +237 -13
- 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/index.mjs +7 -0
- package/node_modules/@tomflow/chatloop-extension/lib/bridge.mjs +94 -19
- package/node_modules/@tomflow/chatloop-extension/lib/browser-launch.mjs +3 -3
- package/node_modules/@tomflow/chatloop-extension/lib/context-ipc.mjs +9 -2
- 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 +50 -7
- package/node_modules/@tomflow/chatloop-extension/package.json +1 -3
- package/node_modules/@tomflow/chatloop-trace/package.json +1 -3
- package/package.json +9 -11
- package/providers/extension.mjs +59 -16
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
// Task-scoped routes in the SAME MV3 Worker. No listener or Owner.
|
|
2
|
+
const onScopedBrowserMessage=(message,sender,reply)=>{
|
|
3
|
+
const type=message?.type;
|
|
4
|
+
if(['CHAT_LOOP_TASK_HANDOFF_TAKE_V2',
|
|
5
|
+
'CHAT_LOOP_TASK_HANDOFF_CHAT_V2',
|
|
6
|
+
'CHAT_LOOP_TASK_HANDOFF_ABORT_V2',
|
|
7
|
+
'CHAT_LOOP_TASK_HANDOFF_SEND_BEGIN_V2',
|
|
8
|
+
'CHAT_LOOP_TASK_HANDOFF_RESULT_V2'].includes(type)){
|
|
9
|
+
void Promise.resolve().then(()=>scopedTaskHandoffAction(message,sender))
|
|
10
|
+
.then(reply,error=>reply({status:'DENIED',
|
|
11
|
+
code:safeWorkerErrorCode(error,'TASK_SCOPED_HANDOFF_UNVERIFIED')}));
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
14
|
+
if(['CHAT_LOOP_TASK_ACTION_TAKE_V2','CHAT_LOOP_TASK_SEND_BEGIN_V2',
|
|
15
|
+
'CHAT_LOOP_TASK_ACTION_RESULT_V2'].includes(type)){
|
|
16
|
+
void Promise.resolve().then(()=>scopedTaskContinueAction(message,sender))
|
|
17
|
+
.then(reply,error=>reply({status:'DENIED',
|
|
18
|
+
code:safeWorkerErrorCode(error,'TASK_SCOPED_ACTION_UNVERIFIED')}));
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
if(type==='CHAT_LOOP_TASK_MODEL_INTENT_V2'){
|
|
22
|
+
// Same installed Continue/Handoff Action pages, one Task-specific
|
|
23
|
+
// queue, private Native intent source, no implicit latest-Run routing.
|
|
24
|
+
void Promise.resolve().then(()=>scopedModelIntent(message,sender))
|
|
25
|
+
.then(reply,error=>reply({status:'DENIED',
|
|
26
|
+
code:safeWorkerErrorCode(error,'TASK_SCOPED_INTENT_UNVERIFIED')}));
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
if(type==='CHAT_LOOP_TASK_HANDOFF_RECOVER_V2'){
|
|
30
|
+
void Promise.resolve().then(()=>scopedTaskRecoverHandoff(message,sender))
|
|
31
|
+
.then(reply,error=>reply({status:'BLOCKED',
|
|
32
|
+
code:safeWorkerErrorCode(error,'SCOPED_RECOVERY_UNVERIFIED')}));
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
if(type==='CHAT_LOOP_TASK_SESSION_CANCEL_V2'){
|
|
36
|
+
void Promise.resolve().then(()=>scopedTaskCancel(message,sender))
|
|
37
|
+
.then(reply,error=>reply({status:'BLOCKED',
|
|
38
|
+
code:safeWorkerErrorCode(error,'SCOPED_CANCEL_UNVERIFIED')}));
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
if(type==='CHAT_LOOP_TASK_STATUS_V2'){
|
|
42
|
+
// The selected Project/Task exists ONLY in the signed private Status
|
|
43
|
+
// Request, not in a caller-provided URL or a guessed current Session.
|
|
44
|
+
void (async()=>{
|
|
45
|
+
if(sender?.id!==chrome.runtime.id||sender.frameId!==0
|
|
46
|
+
||!sessionPage(sender,'status',message?.requestId)
|
|
47
|
+
||Object.keys(message||{}).length!==2
|
|
48
|
+
||!scopedTaskRuns)throw Error('SCOPED_STATUS_ACTION_SOURCE_INVALID');
|
|
49
|
+
const proof=await chrome.runtime.sendNativeMessage(SESSION_HOST,{
|
|
50
|
+
op:'acquire',id:message.requestId,action:'status'});
|
|
51
|
+
const intent=proof?.intent;
|
|
52
|
+
if(proof?.status!=='OK'||proof?.action!=='status'
|
|
53
|
+
||proof.id!==message.requestId
|
|
54
|
+
||!intent||typeof intent.projectId!=='string'
|
|
55
|
+
||typeof intent.taskId!=='string')
|
|
56
|
+
throw Error('SCOPED_STATUS_REQUEST_UNVERIFIED');
|
|
57
|
+
// Native revalidates the same source, exact owner and Context
|
|
58
|
+
// Project grant before returning even bounded status metadata.
|
|
59
|
+
const native=await chrome.runtime.sendNativeMessage(SESSION_HOST,{
|
|
60
|
+
op:'context-status-v2',sourceRequestId:message.requestId});
|
|
61
|
+
if(native?.status!=='OK'
|
|
62
|
+
||native.scope!=='TASK_SCOPED_STATUS_READ_ONLY'
|
|
63
|
+
||native.projectId!==intent.projectId
|
|
64
|
+
||native.taskId!==intent.taskId)
|
|
65
|
+
throw Error(native?.code||'SCOPED_STATUS_CONTEXT_UNVERIFIED');
|
|
66
|
+
const worker=await scopedTaskRuns.get(intent.projectId,intent.taskId);
|
|
67
|
+
const run=worker.run;
|
|
68
|
+
const same=Boolean(run&&run.sessionId===native.sessionId
|
|
69
|
+
&&run.generation===native.generation
|
|
70
|
+
&&run.nativeOwnerRevision===native.nativeOwnerRevision);
|
|
71
|
+
const related=Boolean(run&&run.sessionId===native.sessionId);
|
|
72
|
+
const phaseAligned=same&&run.phase===native.phase;
|
|
73
|
+
const clockUntrusted=run?.startedAt!==undefined
|
|
74
|
+
&&run.startedAt>Date.now();
|
|
75
|
+
const context=native.context;
|
|
76
|
+
// The Native phase and Worker projection can agree while Context
|
|
77
|
+
// has lost its Writer, approval or delivery. "VERIFIED" must never
|
|
78
|
+
// be a stronger claim than the authoritative Context snapshot.
|
|
79
|
+
const contextQuiet=context?.pendingHandoff===false
|
|
80
|
+
&&context?.pendingCheckpoint===false
|
|
81
|
+
&&context?.unknownEffectCount===0
|
|
82
|
+
&&context?.projectRulesDrift===false;
|
|
83
|
+
const writerReady=contextQuiet&&context?.runBound===true
|
|
84
|
+
&&context?.writerMatchesOwner===true
|
|
85
|
+
&&context?.writerApprovalCurrent===true
|
|
86
|
+
&&context?.sessionCancelled===false;
|
|
87
|
+
const terminalReady=contextQuiet&&context?.runBound===false
|
|
88
|
+
&&(run?.phase==='DONE'&&context?.executionOutcome==='DONE'
|
|
89
|
+
&&context?.deliveryCommitted===true
|
|
90
|
+
||run?.phase==='CANCELLED'&&context?.sessionCancelled===true);
|
|
91
|
+
// Initial original-Chat Adopt has no sender, including its first
|
|
92
|
+
// Native PENDING Permit. It must not masquerade as PENDING_SEND.
|
|
93
|
+
const preAdopt=phaseAligned&&run.generation===0
|
|
94
|
+
&&run.ownerChatId===null
|
|
95
|
+
&&['STARTING','RESERVED','PENDING'].includes(run.phase);
|
|
96
|
+
const preAdoptUnbound=preAdopt&&contextQuiet
|
|
97
|
+
&&context?.runBound===false&&context?.sessionCancelled===false
|
|
98
|
+
&&context?.executionOutcome===null;
|
|
99
|
+
// An interrupted cancel can stop after its first Native BLOCKED CAS
|
|
100
|
+
// (Worker retained the original revision). A Handoff can advance
|
|
101
|
+
// Native while Context remains unbound. Both are fenced uncertainty,
|
|
102
|
+
// not proof of a completed action or an excuse to send again.
|
|
103
|
+
const cancelHalfCommit=related&&run.phase==='BLOCKED'
|
|
104
|
+
&&run.reason==='SESSION_CANCEL_PENDING'
|
|
105
|
+
&&native.generation===run.generation
|
|
106
|
+
&&native.phase==='BLOCKED'
|
|
107
|
+
&&native.nativeOwnerRevision===run.nativeOwnerRevision+1;
|
|
108
|
+
const handoffHalfCommit=related&&run.phase==='PENDING'
|
|
109
|
+
&&run.modelIntent?.action==='HANDOFF'
|
|
110
|
+
&&native.generation===run.generation+1
|
|
111
|
+
&&native.nativeOwnerRevision===run.nativeOwnerRevision+1
|
|
112
|
+
&&native.phase==='WAITING_REPLY'
|
|
113
|
+
&&!writerReady;
|
|
114
|
+
const fencedUncertain=same&&run.phase==='BLOCKED'
|
|
115
|
+
&&['PENDING','WAITING_REPLY'].includes(native.phase)
|
|
116
|
+
||cancelHalfCommit||handoffHalfCommit;
|
|
117
|
+
// This is only a diagnostic: the Alarm still must prove the exact
|
|
118
|
+
// successor DOM, Native Owner and Context Writer before reconciling.
|
|
119
|
+
const handoffAckReview=related&&run.phase==='PENDING'
|
|
120
|
+
&&run.modelIntent?.action==='HANDOFF'
|
|
121
|
+
&&run.modelIntent.state==='SEND_BEGIN'
|
|
122
|
+
&&Boolean(run.handoffReceipt)
|
|
123
|
+
&&run.generation+1===native.generation
|
|
124
|
+
&&native.nativeOwnerRevision===run.nativeOwnerRevision+1
|
|
125
|
+
&&native.phase==='WAITING_REPLY'
|
|
126
|
+
&&writerReady;
|
|
127
|
+
const cancelAckReview=related&&run.phase==='BLOCKED'
|
|
128
|
+
&&run.reason==='SESSION_CANCEL_PENDING'
|
|
129
|
+
&&/^[a-f0-9]{32}$/.test(run.cancelRequestId||'')
|
|
130
|
+
&&native.phase==='CANCELLED'
|
|
131
|
+
&&native.generation===run.generation
|
|
132
|
+
&&native.nativeOwnerRevision===run.nativeOwnerRevision+2
|
|
133
|
+
&&native.context?.sessionCancelled===true
|
|
134
|
+
&&native.context?.runBound===false
|
|
135
|
+
&&native.context?.unknownEffectCount===0;
|
|
136
|
+
// A physical hold release is NOT Context Writer recovery. Surface the
|
|
137
|
+
// exact unambiguous pre-send fence as a diagnostic only: Stage 06
|
|
138
|
+
// still needs its own source-bound recovery and permit.
|
|
139
|
+
const handoffPreSendFenced=related&&run.phase==='BLOCKED'
|
|
140
|
+
&&run.preSendAbort?.id===run.modelIntent?.id
|
|
141
|
+
&&run.nextAttempt?.sendBeginAt===null
|
|
142
|
+
&&native.generation===run.generation
|
|
143
|
+
&&native.nativeOwnerRevision===run.nativeOwnerRevision+1
|
|
144
|
+
&&native.phase==='BLOCKED'
|
|
145
|
+
&&native.context?.pendingHandoff===true
|
|
146
|
+
&&native.context?.runBound===false
|
|
147
|
+
&&native.context?.unknownEffectCount===0;
|
|
148
|
+
const preSendEvidence=handoffPreSendFenced
|
|
149
|
+
&&await scopedVerifyAbortedHandoffFence(run);
|
|
150
|
+
const abortFenceUncertain=related&&run.phase==='BLOCKED'
|
|
151
|
+
&&Boolean(run.preSendAbort)&&native.phase==='BLOCKED'
|
|
152
|
+
&&native.generation===run.generation
|
|
153
|
+
&&native.nativeOwnerRevision===run.nativeOwnerRevision+1;
|
|
154
|
+
const pageFaultReview=phaseAligned&&run.phase==='WAITING_REPLY'
|
|
155
|
+
&&writerReady
|
|
156
|
+
&&['PAGE_ERROR','CONTEXT_LIMIT','SECURITY_CHALLENGE']
|
|
157
|
+
.includes(run.lastFault?.reason)
|
|
158
|
+
&&(!run.awaitingUserKey||run.awaitingUserKey===run.lastFault.userKey);
|
|
159
|
+
return {status:'OK',scope:'TASK_SCOPED_STATUS_READ_ONLY',
|
|
160
|
+
projectId:intent.projectId,taskId:intent.taskId,
|
|
161
|
+
native:{phase:native.phase,sessionId:native.sessionId,
|
|
162
|
+
generation:native.generation,revision:native.nativeOwnerRevision},
|
|
163
|
+
context:native.context,
|
|
164
|
+
worker:{state:!run?'NOT_IN_THIS_PROFILE'
|
|
165
|
+
:handoffAckReview?'HANDOFF_ACK_REVIEW'
|
|
166
|
+
:cancelAckReview?'CANCEL_ACK_REVIEW'
|
|
167
|
+
:preSendEvidence?'HANDOFF_PRE_SEND_FENCED'
|
|
168
|
+
:pageFaultReview?'PAGE_FAULT_REVIEW'
|
|
169
|
+
:fencedUncertain||abortFenceUncertain||clockUntrusted?'FENCED_UNCERTAIN'
|
|
170
|
+
:preAdoptUnbound?'PRE_ADOPT_UNBOUND'
|
|
171
|
+
:preAdopt?'FENCED_UNCERTAIN'
|
|
172
|
+
:phaseAligned&&run.phase==='PENDING'
|
|
173
|
+
?'PENDING_SEND_UNRESOLVED'
|
|
174
|
+
:phaseAligned&&run.phase==='BLOCKED'
|
|
175
|
+
?contextQuiet?'BLOCKED_FENCED':'FENCED_UNCERTAIN'
|
|
176
|
+
:phaseAligned&&run.phase==='WAITING_REPLY'&&!writerReady
|
|
177
|
+
?'FENCED_UNCERTAIN'
|
|
178
|
+
:phaseAligned&&['DONE','CANCELLED'].includes(run.phase)
|
|
179
|
+
&&!terminalReady?'FENCED_UNCERTAIN'
|
|
180
|
+
:phaseAligned?'VERIFIED':'DRIFT',
|
|
181
|
+
phase:related?run.phase:null,
|
|
182
|
+
runRevision:related?worker.runRevision:null,
|
|
183
|
+
recoveryAttemptId:preSendEvidence?run.preSendAbort.id:null,
|
|
184
|
+
// Recovery is always a query about THIS original attempt. A
|
|
185
|
+
// Status request never surfaces prompts, credentials, raw DOM,
|
|
186
|
+
// a reusable Turn token or permission to re-open an Action.
|
|
187
|
+
pageFault:pageFaultReview
|
|
188
|
+
?{reason:run.lastFault.reason,
|
|
189
|
+
retryAvailable:run.lastFault.retryAvailable,
|
|
190
|
+
observedAt:run.lastFault.at}:null,
|
|
191
|
+
pendingIntent:related&&run.modelIntent
|
|
192
|
+
?{intentId:run.modelIntent.id,action:run.modelIntent.action,
|
|
193
|
+
state:run.modelIntent.state,
|
|
194
|
+
sendBegin:run.nextAttempt?.sendBeginAt!==null
|
|
195
|
+
&&Number.isSafeInteger(run.nextAttempt?.sendBeginAt)}
|
|
196
|
+
:null,
|
|
197
|
+
lastReceipt:related&&run.lastIntentReceipt
|
|
198
|
+
?{intentId:run.lastIntentReceipt.id,
|
|
199
|
+
action:run.lastIntentReceipt.action,
|
|
200
|
+
status:run.lastIntentReceipt.status}
|
|
201
|
+
:null}};
|
|
202
|
+
})().then(reply,error=>reply({status:'UNVERIFIED',
|
|
203
|
+
code:safeWorkerErrorCode(error,'SCOPED_STATUS_UNVERIFIED')}));
|
|
204
|
+
return true;
|
|
205
|
+
}
|
|
206
|
+
if(type==='CHAT_LOOP_TASK_START_PREPARE_V2'
|
|
207
|
+
||type==='CHAT_LOOP_TASK_START_COMMIT_V2'){
|
|
208
|
+
// Derive the TaskKey from the private Native start request, NEVER from
|
|
209
|
+
// an Action URL/query or user-controlled message. Queue only this Task;
|
|
210
|
+
// a long A Start cannot serialize unrelated approved Task B.
|
|
211
|
+
void (async()=>{
|
|
212
|
+
if(!scopedTaskRuns||!scopedTaskScheduler)
|
|
213
|
+
throw Error('TASK_SCOPED_ROUTER_NOT_READY');
|
|
214
|
+
if(!message||Object.keys(message).length!==2
|
|
215
|
+
||!/^[a-f0-9]{32}$/.test(message.requestId||''))
|
|
216
|
+
throw Error('SCOPED_START_MESSAGE_INVALID');
|
|
217
|
+
const source=await scopedStartIntent(message,sender);
|
|
218
|
+
return scopedTaskScheduler.perTask(source.intent.projectId,
|
|
219
|
+
source.intent.taskId,()=>type==='CHAT_LOOP_TASK_START_PREPARE_V2'
|
|
220
|
+
?scopedTaskStartPrepare(message,sender,source)
|
|
221
|
+
:scopedTaskStartCommit(message,sender,source));
|
|
222
|
+
})().then(reply,error=>reply({status:'BLOCKED',
|
|
223
|
+
code:safeWorkerErrorCode(error,'SCOPED_START_UNVERIFIED')}));
|
|
224
|
+
return true;
|
|
225
|
+
}
|
|
226
|
+
if(type==='CHAT_LOOP_TASK_TURN_BLOCKED_V2'){
|
|
227
|
+
// A visible page fault is bounded evidence, not an assistant terminal
|
|
228
|
+
// marker or a new automatic Handoff/sender authorization.
|
|
229
|
+
void Promise.resolve().then(()=>scopedTaskFaultObserved(message,sender))
|
|
230
|
+
.then(reply,error=>reply({status:'BLOCKED',
|
|
231
|
+
code:safeWorkerErrorCode(error,'SCOPED_FAULT_UNVERIFIED')}));
|
|
232
|
+
return true;
|
|
233
|
+
}
|
|
234
|
+
// V2 is an Observer-only, read-only route. It never invokes any V1
|
|
235
|
+
// action, serial mutation handler, Context write, or page send.
|
|
236
|
+
if(type==='CHAT_LOOP_TASK_TURN_OBSERVED_V2'){
|
|
237
|
+
void (async()=>{
|
|
238
|
+
if(!scopedTaskOwnerResolver)return {
|
|
239
|
+
status:'BLOCKED',code:'TASK_SCOPED_ROUTER_NOT_READY'};
|
|
240
|
+
// Ordinary V1 chats have no scoped Worker owner. Do not burden their
|
|
241
|
+
// established observer with V2-only field validation; the V1 handler
|
|
242
|
+
// independently checks Native before every executable transition.
|
|
243
|
+
if(!await hasActiveScopedTask())return {
|
|
244
|
+
status:'UNSCOPED',code:'SCOPED_OBSERVER_TASK_NOT_FOUND'};
|
|
245
|
+
if(sender?.id!==chrome.runtime.id||sender.frameId!==0
|
|
246
|
+
||!Number.isSafeInteger(sender.tab?.id)
|
|
247
|
+
||typeof message?.page!=='string'
|
|
248
|
+
||message.page!==pathOf(sender.url)
|
|
249
|
+
||typeof message.userKey!=='string'||message.userKey.length>1024
|
|
250
|
+
||!/^user:[^\s]{8,}$/.test(message.userKey)
|
|
251
|
+
||typeof message.assistantKey!=='string'
|
|
252
|
+
||message.assistantKey.length>1024
|
|
253
|
+
||!/^assistant:[^\s]{8,}$/.test(message.assistantKey)
|
|
254
|
+
||!['CONTINUE','DONE','BLOCKED','HANDOFF','UNKNOWN']
|
|
255
|
+
.includes(message.outcome)
|
|
256
|
+
||typeof message.pendingConfirmation!=='boolean'
|
|
257
|
+
||Object.keys(message).some(k=>!['type','page','userKey',
|
|
258
|
+
'assistantKey','outcome','pendingConfirmation'].includes(k)))
|
|
259
|
+
return {status:'DENIED',code:'TASK_SCOPED_OBSERVER_SOURCE_INVALID'};
|
|
260
|
+
if(['DONE','BLOCKED'].includes(message.outcome))
|
|
261
|
+
return scopedTaskQueueTerminalObserver(message,sender);
|
|
262
|
+
try {
|
|
263
|
+
const initial=await scopedTaskOwnerResolver.resolve(sender);
|
|
264
|
+
return scopedTaskScheduler.perTask(initial.projectId,
|
|
265
|
+
initial.taskId,async()=>{
|
|
266
|
+
// Resolve AGAIN inside the one Task queue. The model's signed
|
|
267
|
+
// intent may have been registered after the first read; another
|
|
268
|
+
// Task's unrelated StoreRevision does not invalidate this Turn.
|
|
269
|
+
const route=await scopedTaskOwnerResolver.resolve(sender);
|
|
270
|
+
if(route.projectId!==initial.projectId
|
|
271
|
+
||route.taskId!==initial.taskId
|
|
272
|
+
||route.page!==message.page)return {
|
|
273
|
+
status:'DENIED',code:'TASK_SCOPED_OBSERVER_OWNER_CHANGED'};
|
|
274
|
+
// Retain only the latest verified pair of DOM Turn identifiers
|
|
275
|
+
// inside THIS Task slot; never retain reply text or infer DONE.
|
|
276
|
+
// A repeated Observer event is idempotent, not a second Turn.
|
|
277
|
+
const snapshot=await scopedTaskRuns.get(route.projectId,route.taskId);
|
|
278
|
+
if(snapshot.runRevision!==route.runRevision
|
|
279
|
+
||snapshot.run?.sessionId!==route.sessionId
|
|
280
|
+
||snapshot.run?.generation!==route.generation
|
|
281
|
+
||snapshot.run?.phase!=='WAITING_REPLY'
|
|
282
|
+
||snapshot.run?.tabId!==route.tabId)
|
|
283
|
+
return {status:'BLOCKED',code:'TASK_SCOPED_OBSERVER_STALE'};
|
|
284
|
+
// After one confirmed Continue/Handoff send, the *exact* new
|
|
285
|
+
// User Turn is the only eligible completion. An unrelated manual
|
|
286
|
+
// user message cannot consume the old Task's next-turn authority.
|
|
287
|
+
const expected=snapshot.run.awaitingUserKey;
|
|
288
|
+
if(expected&&expected!==message.userKey)
|
|
289
|
+
return {status:'BLOCKED',code:'TASK_SCOPED_AWAITED_TURN_CHANGED'};
|
|
290
|
+
const previous=snapshot.run.lastObservedTurn;
|
|
291
|
+
if(expected
|
|
292
|
+
||previous?.userKey!==message.userKey
|
|
293
|
+
||previous.assistantKey!==message.assistantKey
|
|
294
|
+
||previous.page!==route.page
|
|
295
|
+
||previous.outcome!==message.outcome
|
|
296
|
+
||previous.pendingConfirmation!==message.pendingConfirmation){
|
|
297
|
+
const next={...snapshot.run,lastObservedTurn:{
|
|
298
|
+
page:route.page,userKey:message.userKey,
|
|
299
|
+
assistantKey:message.assistantKey,
|
|
300
|
+
outcome:message.outcome,
|
|
301
|
+
pendingConfirmation:message.pendingConfirmation,
|
|
302
|
+
observedAt:Date.now()}};
|
|
303
|
+
// A stable assistant reply that still awaits a real human
|
|
304
|
+
// confirmation is NOT a completed managed next Turn. Keep its
|
|
305
|
+
// exact expected User key; a later unrelated User message cannot
|
|
306
|
+
// silently inherit this Task's continuation authority.
|
|
307
|
+
if(expected&&message.pendingConfirmation===false)
|
|
308
|
+
delete next.awaitingUserKey;
|
|
309
|
+
delete next.lastFault;
|
|
310
|
+
await scopedTaskRuns.put(next,{
|
|
311
|
+
expectedStoreRevision:snapshot.storeRevision,
|
|
312
|
+
expectedRunRevision:route.runRevision});
|
|
313
|
+
}
|
|
314
|
+
// Only a signed intent whose original User Turn and final DOM
|
|
315
|
+
// status match can now create the existing formal Action. No
|
|
316
|
+
// Context Permit or Chat send happens in this observation.
|
|
317
|
+
await scopedLaunchReadyIntent(route.projectId,route.taskId);
|
|
318
|
+
await scopedSettleMismatchedIntent(route.projectId,route.taskId);
|
|
319
|
+
if(message.outcome==='UNKNOWN'
|
|
320
|
+
&&message.pendingConfirmation===false){
|
|
321
|
+
// An actually finished reply lacking a valid final status is NOT
|
|
322
|
+
// approval to queue a later automatic sender. Freeze only this
|
|
323
|
+
// Worker Task slot. Native/Context Writer remains untouched:
|
|
324
|
+
// explicit Stage 06 recovery must inspect that drift separately.
|
|
325
|
+
const frozen=await scopedTaskRuns.get(
|
|
326
|
+
route.projectId,route.taskId);
|
|
327
|
+
if(frozen.run?.phase==='WAITING_REPLY'){
|
|
328
|
+
const blocked={...frozen.run,phase:'BLOCKED',
|
|
329
|
+
reason:'SCOPED_FINAL_STATUS_UNVERIFIED'};
|
|
330
|
+
delete blocked.awaitingUserKey;
|
|
331
|
+
await scopedTaskRuns.put(blocked,{
|
|
332
|
+
expectedStoreRevision:frozen.storeRevision,
|
|
333
|
+
expectedRunRevision:frozen.runRevision});
|
|
334
|
+
}
|
|
335
|
+
return {status:'BLOCKED',
|
|
336
|
+
code:'SCOPED_FINAL_STATUS_UNVERIFIED'};
|
|
337
|
+
}
|
|
338
|
+
return {status:'OBSERVED',code:'TASK_SCOPED_OBSERVER_VERIFIED',
|
|
339
|
+
scope:'TASK_SCOPED_OBSERVATION_ONLY'};
|
|
340
|
+
});
|
|
341
|
+
} catch(error) {
|
|
342
|
+
const code=safeWorkerErrorCode(error,'TASK_SCOPED_OWNER_UNVERIFIED');
|
|
343
|
+
// Only an exact unowned Tab may use the old V1 path. All other
|
|
344
|
+
// Native/Context/Tab drift stays fail-closed.
|
|
345
|
+
return code==='SCOPED_OBSERVER_TASK_NOT_FOUND'
|
|
346
|
+
?{status:'UNSCOPED',code}
|
|
347
|
+
:{status:'BLOCKED',code};
|
|
348
|
+
}
|
|
349
|
+
})().then(reply,error=>reply({status:'BLOCKED',
|
|
350
|
+
code:safeWorkerErrorCode(error,'TASK_SCOPED_OWNER_UNVERIFIED')}));
|
|
351
|
+
return true;
|
|
352
|
+
}
|
|
353
|
+
// Unknown V2 message names must fail synchronously; never fall back
|
|
354
|
+
// to V1 serial handlers, another Chat's Owner or implicit latest Task.
|
|
355
|
+
reply({status:'DENIED',code:'TASK_SCOPED_ROUTE_UNKNOWN'});
|
|
356
|
+
return false;
|
|
357
|
+
};
|