@tomflow/chatloop-extension 0.4.34 → 0.4.35
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 +66 -0
- package/bin/cli.mjs +19 -4
- package/bin/native-host.mjs +40 -1
- package/capabilities/session.mjs +1 -1
- package/extension/actions/continue.js +54 -4
- package/extension/actions/handoff-scoped.js +143 -0
- package/extension/actions/handoff.js +9 -3
- package/extension/actions/model-intent.js +9 -5
- package/extension/actions/session.js +53 -10
- package/extension/actions/start.js +34 -0
- package/extension/actions/status.js +16 -2
- package/extension/background.js +16 -0
- package/extension/loop/index.js +1 -1
- package/extension/loop/observer-approval.js +19 -10
- package/extension/loop/observer-poll.js +7 -6
- package/extension/loop/observer.js +64 -7
- package/extension/loop/resource-maintenance.js +159 -0
- package/extension/loop/resources.js +235 -95
- package/extension/manifest.json +2 -1
- package/extension/session.html +1 -0
- package/extension/worker/admission.js +3 -0
- package/extension/worker/bootstrap.js +73 -16
- package/extension/worker/router.js +23 -6
- package/extension/worker/scheduler.js +237 -13
- package/extension/worker/task-action-v2.js +278 -0
- package/extension/worker/task-cancel-recovery-v2.js +52 -0
- package/extension/worker/task-cancel-v2.js +93 -0
- package/extension/worker/task-continue-recovery-v2.js +78 -0
- package/extension/worker/task-handoff-recovery-v2.js +148 -0
- package/extension/worker/task-handoff-v2.js +325 -0
- package/extension/worker/task-hold-recovery-v2.js +109 -0
- package/extension/worker/task-intent-dispatch-v2.js +220 -0
- package/extension/worker/task-intent-v2.js +106 -0
- package/extension/worker/task-owner-resolver.js +110 -0
- package/extension/worker/task-recover-v2.js +209 -0
- package/extension/worker/task-routes-v2.js +357 -0
- package/extension/worker/task-run-store.js +400 -0
- package/extension/worker/task-scheduler.js +78 -0
- package/extension/worker/task-start-v2.js +361 -0
- package/extension/worker/task-terminal-v2.js +235 -0
- package/index.mjs +7 -0
- package/lib/bridge.mjs +94 -19
- package/lib/browser-launch.mjs +3 -3
- package/lib/context-ipc.mjs +9 -2
- package/lib/owner-kernel-lock.mjs +106 -0
- package/lib/owner-ledger.mjs +316 -0
- package/lib/owner-locked-write.mjs +30 -0
- package/lib/scoped-cancel-v2.mjs +190 -0
- package/lib/scoped-continue-v2.mjs +214 -0
- package/lib/scoped-handoff-v2.mjs +338 -0
- package/lib/scoped-native-control.mjs +398 -0
- package/lib/scoped-recovery-v2.mjs +234 -0
- package/lib/scoped-retire-v2.mjs +70 -0
- package/lib/scoped-terminal-v2.mjs +143 -0
- package/lib/session-file.mjs +50 -7
- package/package.json +2 -4
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
// Task-scoped Continue: private Native source + Context Permit + Native CAS.
|
|
2
|
+
// No Chrome/DOM operation occurs here. PENDING is not a replayable send grant.
|
|
3
|
+
export function createScopedContinueControl(ports){
|
|
4
|
+
const {readRequest,readScopedOwner,writeScopedOwner,nativeContextControl}=ports;
|
|
5
|
+
const fail=code=>Object.assign(Error(code),{code});
|
|
6
|
+
const yes=(value,code)=>{if(!value)throw fail(code)};
|
|
7
|
+
const ID=/^[a-zA-Z0-9][a-zA-Z0-9._-]{0,127}$/;
|
|
8
|
+
const UUID=/^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i;
|
|
9
|
+
const keyNames=['op','sourceRequestId','projectId','taskId',
|
|
10
|
+
'sessionId','generation','expectedSessionRevision',
|
|
11
|
+
'expectedStoreEpoch','expectedBindingRevision','turnToken'];
|
|
12
|
+
const proof=s=>Object.fromEntries(['projectId','taskId','sessionId',
|
|
13
|
+
'generation','revision','phase','activeChatId'].map(k=>[k,s[k]]));
|
|
14
|
+
function source(m){
|
|
15
|
+
yes(m&&typeof m==='object'&&!Array.isArray(m)
|
|
16
|
+
&&['continue-permit-v2','continue-reconcile-v2',
|
|
17
|
+
'continue-send-check-v2','continue-result-v2',
|
|
18
|
+
'continue-result-read-v2'].includes(m.op),'SCOPED_CONTINUE_SOURCE_INVALID');
|
|
19
|
+
const names=['continue-result-v2','continue-result-read-v2']
|
|
20
|
+
.includes(m.op)
|
|
21
|
+
?[...keyNames,'sourceUserKey','newUserKey','newPage']:keyNames;
|
|
22
|
+
yes(Object.keys(m).length===names.length
|
|
23
|
+
&&names.every(k=>Object.hasOwn(m,k))
|
|
24
|
+
&&typeof m.sourceRequestId==='string'
|
|
25
|
+
&&/^[a-f0-9]{32}$/.test(m.sourceRequestId)
|
|
26
|
+
&&ID.test(m.projectId||'')&&ID.test(m.taskId||'')
|
|
27
|
+
&&UUID.test(m.sessionId||'')
|
|
28
|
+
&&Number.isSafeInteger(m.generation)&&m.generation>=0
|
|
29
|
+
&&Number.isSafeInteger(m.expectedSessionRevision)
|
|
30
|
+
&&m.expectedSessionRevision>=1
|
|
31
|
+
&&typeof m.expectedStoreEpoch==='string'
|
|
32
|
+
&&m.expectedStoreEpoch.length>=10
|
|
33
|
+
&&Number.isSafeInteger(m.expectedBindingRevision)
|
|
34
|
+
&&m.expectedBindingRevision>=1
|
|
35
|
+
&&UUID.test(m.turnToken||''),
|
|
36
|
+
'SCOPED_CONTINUE_SOURCE_INVALID');
|
|
37
|
+
const request=readRequest(m.sourceRequestId),intent=request?.intent;
|
|
38
|
+
yes(request?.action==='continue'&&intent?.taskFirst===true
|
|
39
|
+
&&intent.turnToken===m.turnToken
|
|
40
|
+
&&typeof intent.nextPrompt==='string'
|
|
41
|
+
&&intent.nextPrompt.trim().length>0
|
|
42
|
+
&&intent.nextPrompt.length<=4000,
|
|
43
|
+
'SCOPED_CONTINUE_INTENT_UNVERIFIED');
|
|
44
|
+
if(['continue-result-v2','continue-result-read-v2'].includes(m.op))
|
|
45
|
+
yes(typeof m.sourceUserKey==='string'
|
|
46
|
+
&&/^user:[^\s]{8,}$/.test(m.sourceUserKey)
|
|
47
|
+
&&typeof m.newUserKey==='string'
|
|
48
|
+
&&/^user:[^\s]{8,}$/.test(m.newUserKey)
|
|
49
|
+
&&m.sourceUserKey!==m.newUserKey
|
|
50
|
+
&&typeof m.newPage==='string'
|
|
51
|
+
&&m.newPage.length<=512,
|
|
52
|
+
'SCOPED_CONTINUE_RECEIPT_INVALID');
|
|
53
|
+
return intent;
|
|
54
|
+
}
|
|
55
|
+
function owner(m,phase){
|
|
56
|
+
const current=readScopedOwner(m.projectId,m.taskId);
|
|
57
|
+
const s=current.session;
|
|
58
|
+
yes(s&&s.projectId===m.projectId&&s.taskId===m.taskId
|
|
59
|
+
&&s.sessionId===m.sessionId&&s.generation===m.generation
|
|
60
|
+
&&s.revision===m.expectedSessionRevision
|
|
61
|
+
&&s.phase===phase&&typeof s.activeChatId==='string'
|
|
62
|
+
&&/^https:\/\/chatgpt\.com\/(?:c\/[0-9a-f-]{12,}|g\/[^/]+\/c\/[0-9a-f-]{12,})$/i
|
|
63
|
+
.test(s.activeChatUrl||'')
|
|
64
|
+
&&s.activeChatUrl.endsWith('/'+s.activeChatId),
|
|
65
|
+
'SCOPED_CONTINUE_OWNER_CHANGED');
|
|
66
|
+
return current;
|
|
67
|
+
}
|
|
68
|
+
async function writer(m,s){
|
|
69
|
+
const result=await nativeContextControl('run.terminalStatus',{
|
|
70
|
+
projectId:s.projectId,taskId:s.taskId,sessionId:s.sessionId,
|
|
71
|
+
generation:s.generation,activeChatId:s.activeChatId},
|
|
72
|
+
{ownerProofV2:proof(s)});
|
|
73
|
+
const wanted=s.sessionId+':'+s.taskId+':'+s.generation;
|
|
74
|
+
yes(result?.projectId===s.projectId&&result.taskId===s.taskId
|
|
75
|
+
&&result.runRef===wanted
|
|
76
|
+
&&result.storeEpoch===m.expectedStoreEpoch
|
|
77
|
+
&&result.bindingRevision===m.expectedBindingRevision
|
|
78
|
+
&&result.pendingHandoff===false
|
|
79
|
+
&&result.pendingCheckpointId===null
|
|
80
|
+
&&result.writerApprovalCurrent===true
|
|
81
|
+
&&result.projectRulesDrift===false
|
|
82
|
+
&&result.unknownEffectCount===0
|
|
83
|
+
&&result.sessionCancellation===null
|
|
84
|
+
&&result.executionOutcome===null
|
|
85
|
+
&&Number.isSafeInteger(result.taskRevision)
|
|
86
|
+
&&Number.isSafeInteger(result.approvalRevision),
|
|
87
|
+
'SCOPED_CONTINUE_WRITER_UNVERIFIED');
|
|
88
|
+
const after=readScopedOwner(s.projectId,s.taskId).session;
|
|
89
|
+
yes(after?.revision===s.revision&&after.phase===s.phase
|
|
90
|
+
&&after.sessionId===s.sessionId&&after.activeChatId===s.activeChatId,
|
|
91
|
+
'SCOPED_CONTINUE_OWNER_CHANGED_DURING_READ');
|
|
92
|
+
return result;
|
|
93
|
+
}
|
|
94
|
+
async function recorded(m,s,approvalRevision){
|
|
95
|
+
const receipt=await nativeContextControl('permit.status',{
|
|
96
|
+
projectId:s.projectId,taskId:s.taskId,sessionId:s.sessionId,
|
|
97
|
+
generation:s.generation,activeChatId:s.activeChatId,
|
|
98
|
+
actionToken:m.sourceRequestId},{ownerProofV2:proof(s)});
|
|
99
|
+
yes(receipt?.status==='RECORDED'
|
|
100
|
+
&&receipt.kind==='CONTINUE'
|
|
101
|
+
&&receipt.projectId===s.projectId
|
|
102
|
+
&&receipt.taskId===s.taskId
|
|
103
|
+
&&receipt.actionToken===m.sourceRequestId
|
|
104
|
+
&&receipt.approvalRevision===approvalRevision,
|
|
105
|
+
'SCOPED_CONTINUE_PERMIT_UNVERIFIED');
|
|
106
|
+
}
|
|
107
|
+
async function handle(m){
|
|
108
|
+
source(m);
|
|
109
|
+
if(m.op==='continue-result-read-v2'){
|
|
110
|
+
// Lost result ACK: the original Native transition MUST already exist.
|
|
111
|
+
// A read of the exact original Permit/Writer cannot send or replay.
|
|
112
|
+
const current=readScopedOwner(m.projectId,m.taskId),s=current.session;
|
|
113
|
+
yes(s&&s.sessionId===m.sessionId
|
|
114
|
+
&&s.generation===m.generation
|
|
115
|
+
&&s.phase==='WAITING_REPLY'
|
|
116
|
+
&&s.revision===m.expectedSessionRevision+1
|
|
117
|
+
&&typeof s.activeChatId==='string'
|
|
118
|
+
&&m.newPage===s.activeChatUrl?.slice('https://chatgpt.com'.length),
|
|
119
|
+
'SCOPED_CONTINUE_RESULT_NOT_COMMITTED');
|
|
120
|
+
const state=await writer(m,s);
|
|
121
|
+
await recorded(m,s,state.approvalRevision);
|
|
122
|
+
return {status:'CONFIRMED',
|
|
123
|
+
scope:'TASK_SCOPED_CONTINUE_RECEIPT',
|
|
124
|
+
nativeOwnerRevision:s.revision,readOnly:true};
|
|
125
|
+
}
|
|
126
|
+
if(m.op==='continue-reconcile-v2'){
|
|
127
|
+
// The original permit+Native PENDING CAS may have committed while
|
|
128
|
+
// the Worker lost its ACK before persisting SEND_BEGIN. Read exact
|
|
129
|
+
// original token and Writer; do NOT consume another permit.
|
|
130
|
+
const current=readScopedOwner(m.projectId,m.taskId),s=current.session;
|
|
131
|
+
yes(s&&s.projectId===m.projectId&&s.taskId===m.taskId
|
|
132
|
+
&&s.sessionId===m.sessionId&&s.generation===m.generation
|
|
133
|
+
&&s.phase==='PENDING'
|
|
134
|
+
&&s.revision===m.expectedSessionRevision+1
|
|
135
|
+
&&typeof s.activeChatId==='string'
|
|
136
|
+
&&/^https:\/\/chatgpt\.com\/(?:c\/[0-9a-f-]{12,}|g\/[^/]+\/c\/[0-9a-f-]{12,})$/i
|
|
137
|
+
.test(s.activeChatUrl||''),
|
|
138
|
+
'SCOPED_CONTINUE_PRIOR_CAS_UNVERIFIED');
|
|
139
|
+
const state=await writer(m,s);
|
|
140
|
+
await recorded(m,s,state.approvalRevision);
|
|
141
|
+
return {status:'PERMITTED',scope:'TASK_SCOPED_CONTINUE_PENDING',
|
|
142
|
+
nativeOwnerRevision:s.revision,
|
|
143
|
+
contextBindingRevision:state.bindingRevision,
|
|
144
|
+
contextStoreEpoch:state.storeEpoch,
|
|
145
|
+
taskRevision:state.taskRevision,readOnlyReconciled:true};
|
|
146
|
+
}
|
|
147
|
+
if(m.op==='continue-permit-v2'){
|
|
148
|
+
const before=owner(m,'WAITING_REPLY');
|
|
149
|
+
const context=await writer(m,before.session);
|
|
150
|
+
// Exactly one Context permit per signed model request. If the ACK is
|
|
151
|
+
// lost or Native CAS fails, DO NOT retry permit.consume.
|
|
152
|
+
const permit=await nativeContextControl('permit.consume',{
|
|
153
|
+
projectId:m.projectId,taskId:m.taskId,
|
|
154
|
+
sessionId:m.sessionId,generation:m.generation,
|
|
155
|
+
activeChatId:before.session.activeChatId,
|
|
156
|
+
expectedStoreEpoch:context.storeEpoch,
|
|
157
|
+
expectedTaskRevision:context.taskRevision,
|
|
158
|
+
expectedApprovalRevision:context.approvalRevision,
|
|
159
|
+
actionToken:m.sourceRequestId,kind:'CONTINUE',
|
|
160
|
+
writeRequestId:m.sourceRequestId+':permit'},
|
|
161
|
+
{ownerProofV2:proof(before.session)});
|
|
162
|
+
yes(permit?.status==='PERMITTED'
|
|
163
|
+
&&permit.actionToken===m.sourceRequestId
|
|
164
|
+
&&permit.approvalRevision===context.approvalRevision,
|
|
165
|
+
'SCOPED_CONTINUE_PERMIT_UNKNOWN');
|
|
166
|
+
const fresh=owner(m,'WAITING_REPLY');
|
|
167
|
+
const s=fresh.session;
|
|
168
|
+
const committed=writeScopedOwner({
|
|
169
|
+
projectId:s.projectId,taskId:s.taskId,sessionId:s.sessionId,
|
|
170
|
+
generation:s.generation,phase:'PENDING',
|
|
171
|
+
activeChatId:s.activeChatId,activeChatUrl:s.activeChatUrl,
|
|
172
|
+
updatedAt:Date.now()},{
|
|
173
|
+
expectedLedgerRevision:fresh.ledgerRevision,
|
|
174
|
+
expectedSessionRevision:s.revision,
|
|
175
|
+
transitionKind:'CONTEXT_CONTINUE_PERMITTED'});
|
|
176
|
+
yes(committed.session.phase==='PENDING'
|
|
177
|
+
&&committed.session.revision===s.revision+1,
|
|
178
|
+
'SCOPED_CONTINUE_NATIVE_COMMIT_UNKNOWN');
|
|
179
|
+
return {status:'PERMITTED',scope:'TASK_SCOPED_CONTINUE_PENDING',
|
|
180
|
+
nativeOwnerRevision:committed.session.revision,
|
|
181
|
+
contextBindingRevision:context.bindingRevision,
|
|
182
|
+
contextStoreEpoch:context.storeEpoch,
|
|
183
|
+
taskRevision:context.taskRevision};
|
|
184
|
+
}
|
|
185
|
+
const phase=owner(m,'PENDING');
|
|
186
|
+
const state=await writer(m,phase.session);
|
|
187
|
+
await recorded(m,phase.session,state.approvalRevision);
|
|
188
|
+
// The exact original Action may now proceed to ONE irreversible send.
|
|
189
|
+
if(m.op==='continue-send-check-v2'){
|
|
190
|
+
const fresh=owner(m,'PENDING');
|
|
191
|
+
return {status:'SEND_ALLOWED',scope:'TASK_SCOPED_CONTINUE_ONCE',
|
|
192
|
+
nativeOwnerRevision:fresh.session.revision};
|
|
193
|
+
}
|
|
194
|
+
// Only the Worker (not this Native process) can verify the DOM receipt.
|
|
195
|
+
// Here a signed source and exact Native/Context permit are additionally
|
|
196
|
+
// required before the Owner resumes waiting for a NEW assistant turn.
|
|
197
|
+
yes(m.newPage===phase.session.activeChatUrl.slice('https://chatgpt.com'.length),
|
|
198
|
+
'SCOPED_CONTINUE_PAGE_CHANGED');
|
|
199
|
+
const fresh=owner(m,'PENDING'),s=fresh.session;
|
|
200
|
+
const committed=writeScopedOwner({
|
|
201
|
+
projectId:s.projectId,taskId:s.taskId,sessionId:s.sessionId,
|
|
202
|
+
generation:s.generation,phase:'WAITING_REPLY',
|
|
203
|
+
activeChatId:s.activeChatId,activeChatUrl:s.activeChatUrl,
|
|
204
|
+
updatedAt:Date.now()},{
|
|
205
|
+
expectedLedgerRevision:fresh.ledgerRevision,
|
|
206
|
+
expectedSessionRevision:s.revision});
|
|
207
|
+
yes(committed.session.phase==='WAITING_REPLY'
|
|
208
|
+
&&committed.session.revision===s.revision+1,
|
|
209
|
+
'SCOPED_CONTINUE_RESULT_UNKNOWN');
|
|
210
|
+
return {status:'CONFIRMED',scope:'TASK_SCOPED_CONTINUE_RECEIPT',
|
|
211
|
+
nativeOwnerRevision:committed.session.revision};
|
|
212
|
+
}
|
|
213
|
+
return Object.freeze({handle});
|
|
214
|
+
}
|
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
// Handoff is one Context checkpoint and one Native Owner generation transfer.
|
|
2
|
+
// This module never opens Chrome or sends a Chat. UNKNOWN is NOT replayable.
|
|
3
|
+
import {randomBytes} from 'node:crypto';
|
|
4
|
+
export function createScopedHandoffControl(ports){
|
|
5
|
+
const {readRequest,readScopedOwner,writeScopedOwner,nativeContextControl}=ports;
|
|
6
|
+
const fail=code=>Object.assign(Error(code),{code});
|
|
7
|
+
const ok=(x,code)=>{if(!x)throw fail(code)};
|
|
8
|
+
const ID=/^[a-zA-Z0-9][a-zA-Z0-9._-]{0,127}$/;
|
|
9
|
+
const UUID=/^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i;
|
|
10
|
+
const names=['op','sourceRequestId','projectId','taskId','sessionId',
|
|
11
|
+
'generation','expectedSessionRevision','expectedStoreEpoch',
|
|
12
|
+
'expectedBindingRevision','turnToken'];
|
|
13
|
+
const ownerProof=s=>Object.fromEntries(['projectId','taskId',
|
|
14
|
+
'sessionId','generation','revision','phase','activeChatId']
|
|
15
|
+
.map(k=>[k,s[k]]));
|
|
16
|
+
const wantedRef=s=>s.sessionId+':'+s.taskId+':'+s.generation;
|
|
17
|
+
function verifySource(m){
|
|
18
|
+
ok(m&&typeof m==='object'&&!Array.isArray(m)
|
|
19
|
+
&&['handoff-permit-v2','handoff-send-check-v2',
|
|
20
|
+
'handoff-abort-fence-v2',
|
|
21
|
+
'handoff-abort-fence-read-v2','handoff-result-v2',
|
|
22
|
+
'handoff-result-read-v2'].includes(m.op),
|
|
23
|
+
'SCOPED_HANDOFF_SOURCE_INVALID');
|
|
24
|
+
const fields=m.op==='handoff-permit-v2'?names
|
|
25
|
+
:['handoff-send-check-v2','handoff-abort-fence-v2',
|
|
26
|
+
'handoff-abort-fence-read-v2']
|
|
27
|
+
.includes(m.op)
|
|
28
|
+
?[...names,'checkpointId','fencedBindingRevision']
|
|
29
|
+
:[...names,'checkpointId','fencedBindingRevision',
|
|
30
|
+
...(m.op==='handoff-result-v2'?['nextRunCredential']:[]),
|
|
31
|
+
'newChatUrl','newUserKey','sourceUserKey'];
|
|
32
|
+
ok(Object.keys(m).length===fields.length
|
|
33
|
+
&&fields.every(k=>Object.hasOwn(m,k))
|
|
34
|
+
&&/^[a-f0-9]{32}$/.test(m.sourceRequestId||'')
|
|
35
|
+
&&ID.test(m.projectId||'')&&ID.test(m.taskId||'')
|
|
36
|
+
&&UUID.test(m.sessionId||'')
|
|
37
|
+
&&Number.isSafeInteger(m.generation)&&m.generation>=0
|
|
38
|
+
&&Number.isSafeInteger(m.expectedSessionRevision)
|
|
39
|
+
&&m.expectedSessionRevision>=1
|
|
40
|
+
&&typeof m.expectedStoreEpoch==='string'
|
|
41
|
+
&&m.expectedStoreEpoch.length>=10
|
|
42
|
+
&&Number.isSafeInteger(m.expectedBindingRevision)
|
|
43
|
+
&&m.expectedBindingRevision>=1
|
|
44
|
+
&&UUID.test(m.turnToken||''),
|
|
45
|
+
'SCOPED_HANDOFF_SOURCE_INVALID');
|
|
46
|
+
// Only the revoked-abort READ may outlive the four-hour signed source.
|
|
47
|
+
// Its Native BLOCKED CAS, exact Context pending attempt, Checkpoint and
|
|
48
|
+
// USED original Permit are verified below. Every send/write still needs
|
|
49
|
+
// the unexpired signed Handoff request and original Turn token.
|
|
50
|
+
if(m.op!=='handoff-abort-fence-read-v2'){
|
|
51
|
+
const request=readRequest(m.sourceRequestId),intent=request?.intent;
|
|
52
|
+
ok(request?.action==='handoff'&&intent?.taskFirst===true
|
|
53
|
+
&&intent.turnToken===m.turnToken,
|
|
54
|
+
'SCOPED_HANDOFF_REQUEST_UNVERIFIED');
|
|
55
|
+
}
|
|
56
|
+
if(m.op!=='handoff-permit-v2')
|
|
57
|
+
ok(typeof m.checkpointId==='string'&&ID.test(m.checkpointId)
|
|
58
|
+
&&Number.isSafeInteger(m.fencedBindingRevision)
|
|
59
|
+
&&m.fencedBindingRevision===m.expectedBindingRevision+1,
|
|
60
|
+
'SCOPED_HANDOFF_FENCE_SOURCE_INVALID');
|
|
61
|
+
if(['handoff-result-v2','handoff-result-read-v2'].includes(m.op))
|
|
62
|
+
ok((m.op!=='handoff-result-v2'
|
|
63
|
+
||typeof m.nextRunCredential==='string'
|
|
64
|
+
&&/^[0-9a-f]{64}$/.test(m.nextRunCredential))
|
|
65
|
+
&&typeof m.newChatUrl==='string'
|
|
66
|
+
&&/^https:\/\/chatgpt\.com\/c\/[0-9a-f-]{12,}$/i
|
|
67
|
+
.test(m.newChatUrl)
|
|
68
|
+
&&/^user:[^\s]{8,}$/.test(m.sourceUserKey||'')
|
|
69
|
+
&&/^user:[^\s]{8,}$/.test(m.newUserKey||'')
|
|
70
|
+
&&m.newUserKey!==m.sourceUserKey,
|
|
71
|
+
'SCOPED_HANDOFF_RECEIPT_INVALID');
|
|
72
|
+
}
|
|
73
|
+
function current(m,phase){
|
|
74
|
+
const scope=readScopedOwner(m.projectId,m.taskId),s=scope.session;
|
|
75
|
+
ok(s&&s.sessionId===m.sessionId&&s.projectId===m.projectId
|
|
76
|
+
&&s.taskId===m.taskId&&s.generation===m.generation
|
|
77
|
+
&&s.revision===m.expectedSessionRevision
|
|
78
|
+
&&s.phase===phase&&typeof s.activeChatId==='string'
|
|
79
|
+
&&/^https:\/\/chatgpt\.com\/(?:c\/[0-9a-f-]{12,}|g\/[^/]+\/c\/[0-9a-f-]{12,})$/i
|
|
80
|
+
.test(s.activeChatUrl||''),
|
|
81
|
+
'SCOPED_HANDOFF_OWNER_CHANGED');
|
|
82
|
+
return scope;
|
|
83
|
+
}
|
|
84
|
+
const context=(command,args,s)=>nativeContextControl(command,args,
|
|
85
|
+
{ownerProofV2:ownerProof(s)});
|
|
86
|
+
async function health(m,s,phase){
|
|
87
|
+
const state=await context('run.terminalStatus',{
|
|
88
|
+
projectId:s.projectId,taskId:s.taskId,sessionId:s.sessionId,
|
|
89
|
+
generation:s.generation,activeChatId:s.activeChatId,
|
|
90
|
+
...(phase==='PENDING'
|
|
91
|
+
?{expectedHandoffAttemptId:m.sourceRequestId}:{})},s);
|
|
92
|
+
ok(state?.projectId===s.projectId&&state.taskId===s.taskId
|
|
93
|
+
&&state.storeEpoch===m.expectedStoreEpoch
|
|
94
|
+
&&state.bindingRevision===(phase==='WAITING_REPLY'
|
|
95
|
+
?m.expectedBindingRevision:m.fencedBindingRevision)
|
|
96
|
+
&&state.pendingHandoff===(phase==='PENDING')
|
|
97
|
+
&&(phase!=='PENDING'||state.pendingAttemptMatches===true)
|
|
98
|
+
&&state.unknownEffectCount===0
|
|
99
|
+
&&state.executionOutcome===null
|
|
100
|
+
&&state.sessionCancellation===null
|
|
101
|
+
&&state.projectRulesDrift===false
|
|
102
|
+
&&Number.isSafeInteger(state.taskRevision)
|
|
103
|
+
&&Number.isSafeInteger(state.approvalRevision)
|
|
104
|
+
&&(phase==='WAITING_REPLY'
|
|
105
|
+
?state.runRef===wantedRef(s)
|
|
106
|
+
&&state.pendingCheckpointId===null
|
|
107
|
+
&&state.writerApprovalCurrent===true
|
|
108
|
+
:state.runRef===null
|
|
109
|
+
&&state.pendingCheckpointId===m.checkpointId),
|
|
110
|
+
'SCOPED_HANDOFF_CONTEXT_UNVERIFIED');
|
|
111
|
+
ok(readScopedOwner(s.projectId,s.taskId).session?.revision===s.revision,
|
|
112
|
+
'SCOPED_HANDOFF_OWNER_CHANGED_DURING_READ');
|
|
113
|
+
return state;
|
|
114
|
+
}
|
|
115
|
+
async function checkUsed(m,s,approvalRevision){
|
|
116
|
+
const permit=await context('permit.status',{
|
|
117
|
+
projectId:s.projectId,taskId:s.taskId,sessionId:s.sessionId,
|
|
118
|
+
generation:s.generation,activeChatId:s.activeChatId,
|
|
119
|
+
actionToken:m.sourceRequestId},s);
|
|
120
|
+
ok(permit?.status==='RECORDED'&&permit.kind==='USED'
|
|
121
|
+
&&permit.actionToken===m.sourceRequestId
|
|
122
|
+
&&permit.approvalRevision===approvalRevision,
|
|
123
|
+
'SCOPED_HANDOFF_PERMIT_NOT_FENCED');
|
|
124
|
+
}
|
|
125
|
+
async function handle(m){
|
|
126
|
+
verifySource(m);
|
|
127
|
+
if(m.op==='handoff-permit-v2'){
|
|
128
|
+
const before=current(m,'WAITING_REPLY'),s=before.session;
|
|
129
|
+
const state=await health(m,s,'WAITING_REPLY');
|
|
130
|
+
const cp=await context('run.snapshot',{
|
|
131
|
+
projectId:s.projectId,taskId:s.taskId,sessionId:s.sessionId,
|
|
132
|
+
generation:s.generation,activeChatId:s.activeChatId},s);
|
|
133
|
+
ok(cp?.projectId===s.projectId&&cp.taskId===s.taskId
|
|
134
|
+
&&cp.runRef===wantedRef(s)
|
|
135
|
+
&&cp.checkpointCurrent===true
|
|
136
|
+
&&cp.taskRevision===state.taskRevision
|
|
137
|
+
&&cp.bindingRevision===state.bindingRevision
|
|
138
|
+
&&cp.unknownEffectCount===0
|
|
139
|
+
&&ID.test(cp.checkpointId||''),
|
|
140
|
+
'SCOPED_HANDOFF_CHECKPOINT_UNVERIFIED');
|
|
141
|
+
const permit=await context('permit.consume',{
|
|
142
|
+
projectId:s.projectId,taskId:s.taskId,sessionId:s.sessionId,
|
|
143
|
+
generation:s.generation,activeChatId:s.activeChatId,
|
|
144
|
+
expectedStoreEpoch:state.storeEpoch,
|
|
145
|
+
expectedTaskRevision:state.taskRevision,
|
|
146
|
+
expectedApprovalRevision:state.approvalRevision,
|
|
147
|
+
kind:'HANDOFF',actionToken:m.sourceRequestId,
|
|
148
|
+
writeRequestId:m.sourceRequestId+':permit'},s);
|
|
149
|
+
ok(permit?.status==='PERMITTED'
|
|
150
|
+
&&permit.actionToken===m.sourceRequestId,
|
|
151
|
+
'SCOPED_HANDOFF_PERMIT_UNKNOWN');
|
|
152
|
+
const fresh=current(m,'WAITING_REPLY'),f=fresh.session;
|
|
153
|
+
const reserved=writeScopedOwner({
|
|
154
|
+
projectId:f.projectId,taskId:f.taskId,sessionId:f.sessionId,
|
|
155
|
+
generation:f.generation,phase:'PENDING',
|
|
156
|
+
activeChatId:f.activeChatId,activeChatUrl:f.activeChatUrl,
|
|
157
|
+
updatedAt:Date.now()},{
|
|
158
|
+
expectedLedgerRevision:fresh.ledgerRevision,
|
|
159
|
+
expectedSessionRevision:f.revision,
|
|
160
|
+
transitionKind:'CONTEXT_HANDOFF_PERMITTED'});
|
|
161
|
+
ok(reserved.session.phase==='PENDING'
|
|
162
|
+
&&reserved.session.revision===f.revision+1,
|
|
163
|
+
'SCOPED_HANDOFF_NATIVE_PENDING_UNKNOWN');
|
|
164
|
+
const newCredential=randomBytes(32).toString('hex');
|
|
165
|
+
const nextRef=s.sessionId+':'+s.taskId+':'+(s.generation+1);
|
|
166
|
+
const fenced=await context('run.fence',{
|
|
167
|
+
projectId:s.projectId,taskId:s.taskId,sessionId:s.sessionId,
|
|
168
|
+
generation:s.generation,activeChatId:s.activeChatId,
|
|
169
|
+
expectedStoreEpoch:state.storeEpoch,
|
|
170
|
+
expectedTaskRevision:state.taskRevision,
|
|
171
|
+
expectedBindingRevision:state.bindingRevision,
|
|
172
|
+
writeRequestId:m.sourceRequestId+':fence',
|
|
173
|
+
runRef:wantedRef(s),attemptId:m.sourceRequestId,
|
|
174
|
+
actionToken:m.sourceRequestId,checkpointId:cp.checkpointId,
|
|
175
|
+
targetGeneration:s.generation+1,nextRunRef:nextRef,
|
|
176
|
+
nextRunCredential:newCredential
|
|
177
|
+
},reserved.session);
|
|
178
|
+
ok(fenced?.status==='FENCED'
|
|
179
|
+
&&fenced.attemptId===m.sourceRequestId
|
|
180
|
+
&&fenced.checkpointId===cp.checkpointId
|
|
181
|
+
&&fenced.bindingRevision===state.bindingRevision+1,
|
|
182
|
+
'SCOPED_HANDOFF_CONTEXT_FENCE_UNKNOWN');
|
|
183
|
+
return {status:'PERMITTED',scope:'TASK_SCOPED_HANDOFF_PENDING',
|
|
184
|
+
nativeOwnerRevision:reserved.session.revision,
|
|
185
|
+
contextStoreEpoch:state.storeEpoch,
|
|
186
|
+
taskRevision:state.taskRevision,
|
|
187
|
+
fencedBindingRevision:fenced.bindingRevision,
|
|
188
|
+
checkpointId:cp.checkpointId,
|
|
189
|
+
targetGeneration:s.generation+1,nextRunRef:nextRef,
|
|
190
|
+
nextRunCredential:newCredential};
|
|
191
|
+
}
|
|
192
|
+
if(m.op==='handoff-abort-fence-v2'){
|
|
193
|
+
// Worker must have durably stored the original pre-SEND_BEGIN abort
|
|
194
|
+
// before requesting this one-way Native revocation. A later Action
|
|
195
|
+
// cannot pass send-check because the Native Owner leaves PENDING.
|
|
196
|
+
const scope=current(m,'PENDING'),s=scope.session;
|
|
197
|
+
const state=await health(m,s,'PENDING');
|
|
198
|
+
await checkUsed(m,s,state.approvalRevision);
|
|
199
|
+
const blocked=writeScopedOwner({
|
|
200
|
+
projectId:s.projectId,taskId:s.taskId,sessionId:s.sessionId,
|
|
201
|
+
generation:s.generation,phase:'BLOCKED',
|
|
202
|
+
activeChatId:s.activeChatId,activeChatUrl:s.activeChatUrl,
|
|
203
|
+
updatedAt:Date.now()
|
|
204
|
+
},{expectedLedgerRevision:scope.ledgerRevision,
|
|
205
|
+
expectedSessionRevision:s.revision});
|
|
206
|
+
ok(blocked?.session.phase==='BLOCKED'
|
|
207
|
+
&&blocked.session.revision===s.revision+1,
|
|
208
|
+
'SCOPED_HANDOFF_ABORT_NATIVE_UNKNOWN');
|
|
209
|
+
return {status:'REVOKED',scope:'TASK_SCOPED_HANDOFF_ABORT_REVOKED',
|
|
210
|
+
projectId:s.projectId,taskId:s.taskId,sessionId:s.sessionId,
|
|
211
|
+
generation:s.generation,
|
|
212
|
+
nativeOwnerRevision:blocked.session.revision,
|
|
213
|
+
contextBindingRevision:state.bindingRevision};
|
|
214
|
+
}
|
|
215
|
+
if(m.op==='handoff-abort-fence-read-v2'){
|
|
216
|
+
// The original revoke ACK can be lost. This is strictly a
|
|
217
|
+
// source-bound read, never another PENDING->BLOCKED CAS.
|
|
218
|
+
const s=readScopedOwner(m.projectId,m.taskId).session;
|
|
219
|
+
ok(s&&s.projectId===m.projectId&&s.taskId===m.taskId
|
|
220
|
+
&&s.sessionId===m.sessionId&&s.generation===m.generation
|
|
221
|
+
&&s.revision===m.expectedSessionRevision+1
|
|
222
|
+
&&s.phase==='BLOCKED'&&s.activeChatId!==null,
|
|
223
|
+
'SCOPED_HANDOFF_ABORT_NOT_REVOKED');
|
|
224
|
+
const state=await health(m,s,'PENDING');
|
|
225
|
+
await checkUsed(m,s,state.approvalRevision);
|
|
226
|
+
const again=readScopedOwner(s.projectId,s.taskId).session;
|
|
227
|
+
ok(again?.sessionId===s.sessionId
|
|
228
|
+
&&again.generation===s.generation&&again.revision===s.revision
|
|
229
|
+
&&again.phase==='BLOCKED'&&again.activeChatUrl===s.activeChatUrl,
|
|
230
|
+
'SCOPED_HANDOFF_ABORT_OWNER_RACED');
|
|
231
|
+
return {status:'REVOKED',scope:'TASK_SCOPED_HANDOFF_ABORT_REVOKED',
|
|
232
|
+
projectId:s.projectId,taskId:s.taskId,sessionId:s.sessionId,
|
|
233
|
+
generation:s.generation,nativeOwnerRevision:s.revision,
|
|
234
|
+
contextBindingRevision:state.bindingRevision,readOnly:true};
|
|
235
|
+
}
|
|
236
|
+
if(m.op==='handoff-result-read-v2'){
|
|
237
|
+
// Original result's Native generation switch AND Context bind must
|
|
238
|
+
// already be committed; lost ACK cannot be repaired by replaying the
|
|
239
|
+
// Handoff send, Native transition or run.bind.handoff mutation.
|
|
240
|
+
const s=readScopedOwner(m.projectId,m.taskId).session;
|
|
241
|
+
const newId=m.newChatUrl.split('/').at(-1);
|
|
242
|
+
ok(s&&s.projectId===m.projectId&&s.taskId===m.taskId
|
|
243
|
+
&&s.sessionId===m.sessionId
|
|
244
|
+
&&s.generation===m.generation+1
|
|
245
|
+
&&s.revision===m.expectedSessionRevision+1
|
|
246
|
+
&&s.phase==='WAITING_REPLY'
|
|
247
|
+
&&s.activeChatUrl===m.newChatUrl
|
|
248
|
+
&&s.activeChatId===newId,
|
|
249
|
+
'SCOPED_HANDOFF_RESULT_NOT_COMMITTED');
|
|
250
|
+
const args={projectId:s.projectId,taskId:s.taskId,
|
|
251
|
+
sessionId:s.sessionId,generation:s.generation,
|
|
252
|
+
activeChatId:s.activeChatId};
|
|
253
|
+
const state=await context('run.terminalStatus',args,s);
|
|
254
|
+
ok(state?.projectId===s.projectId&&state.taskId===s.taskId
|
|
255
|
+
&&state.storeEpoch===m.expectedStoreEpoch
|
|
256
|
+
&&state.runRef===wantedRef(s)
|
|
257
|
+
&&state.bindingRevision===m.fencedBindingRevision+1
|
|
258
|
+
&&state.pendingHandoff===false
|
|
259
|
+
&&state.pendingCheckpointId===null
|
|
260
|
+
&&state.writerApprovalCurrent===true
|
|
261
|
+
&&state.projectRulesDrift===false
|
|
262
|
+
&&state.unknownEffectCount===0
|
|
263
|
+
&&state.sessionCancellation===null
|
|
264
|
+
&&state.executionOutcome===null,
|
|
265
|
+
'SCOPED_HANDOFF_RESULT_CONTEXT_NOT_BOUND');
|
|
266
|
+
const checkpoint=await context('run.snapshot',args,s);
|
|
267
|
+
ok(checkpoint?.projectId===s.projectId
|
|
268
|
+
&&checkpoint.taskId===s.taskId
|
|
269
|
+
&&checkpoint.checkpointId===m.checkpointId
|
|
270
|
+
&&checkpoint.checkpointCurrent===true
|
|
271
|
+
&&checkpoint.runRef===wantedRef(s)
|
|
272
|
+
&&checkpoint.bindingRevision===state.bindingRevision
|
|
273
|
+
&&checkpoint.unknownEffectCount===0,
|
|
274
|
+
'SCOPED_HANDOFF_RESULT_CHECKPOINT_UNVERIFIED');
|
|
275
|
+
const permit=await context('permit.status',{
|
|
276
|
+
...args,actionToken:m.sourceRequestId},s);
|
|
277
|
+
ok(permit?.status==='RECORDED'&&permit.kind==='USED'
|
|
278
|
+
&&permit.projectId===s.projectId
|
|
279
|
+
&&permit.taskId===s.taskId
|
|
280
|
+
&&permit.actionToken===m.sourceRequestId
|
|
281
|
+
&&permit.approvalRevision===state.approvalRevision,
|
|
282
|
+
'SCOPED_HANDOFF_RESULT_PERMIT_UNVERIFIED');
|
|
283
|
+
const again=readScopedOwner(s.projectId,s.taskId).session;
|
|
284
|
+
ok(again?.sessionId===s.sessionId
|
|
285
|
+
&&again.generation===s.generation
|
|
286
|
+
&&again.revision===s.revision
|
|
287
|
+
&&again.phase===s.phase&&again.activeChatUrl===s.activeChatUrl,
|
|
288
|
+
'SCOPED_HANDOFF_RESULT_OWNER_RACED');
|
|
289
|
+
return {status:'CONFIRMED',
|
|
290
|
+
scope:'TASK_SCOPED_HANDOFF_RESULT_READ_ONLY',
|
|
291
|
+
projectId:s.projectId,taskId:s.taskId,sessionId:s.sessionId,
|
|
292
|
+
generation:s.generation,nativeOwnerRevision:s.revision,
|
|
293
|
+
contextBindingRevision:state.bindingRevision,
|
|
294
|
+
contextStoreEpoch:state.storeEpoch,
|
|
295
|
+
taskRevision:state.taskRevision,readOnly:true};
|
|
296
|
+
}
|
|
297
|
+
const scoped=current(m,'PENDING'),s=scoped.session;
|
|
298
|
+
const state=await health(m,s,'PENDING');
|
|
299
|
+
await checkUsed(m,s,state.approvalRevision);
|
|
300
|
+
if(m.op==='handoff-send-check-v2')
|
|
301
|
+
return {status:'SEND_ALLOWED',scope:'TASK_SCOPED_HANDOFF_ONCE',
|
|
302
|
+
nativeOwnerRevision:s.revision};
|
|
303
|
+
const newChatId=m.newChatUrl.split('/').at(-1);
|
|
304
|
+
ok(newChatId!==s.activeChatId,'SCOPED_HANDOFF_NEW_CHAT_INVALID');
|
|
305
|
+
const next=writeScopedOwner({
|
|
306
|
+
projectId:s.projectId,taskId:s.taskId,sessionId:s.sessionId,
|
|
307
|
+
generation:s.generation+1,phase:'WAITING_REPLY',
|
|
308
|
+
activeChatId:newChatId,activeChatUrl:m.newChatUrl,
|
|
309
|
+
updatedAt:Date.now()},{
|
|
310
|
+
expectedLedgerRevision:scoped.ledgerRevision,
|
|
311
|
+
expectedSessionRevision:s.revision});
|
|
312
|
+
ok(next.session.phase==='WAITING_REPLY'
|
|
313
|
+
&&next.session.generation===s.generation+1
|
|
314
|
+
&&next.session.revision===s.revision+1,
|
|
315
|
+
'SCOPED_HANDOFF_OWNER_COMMIT_UNKNOWN');
|
|
316
|
+
const bound=await context('run.bind.handoff',{
|
|
317
|
+
projectId:s.projectId,taskId:s.taskId,sessionId:s.sessionId,
|
|
318
|
+
generation:s.generation+1,activeChatId:newChatId,
|
|
319
|
+
expectedStoreEpoch:state.storeEpoch,
|
|
320
|
+
expectedTaskRevision:state.taskRevision,
|
|
321
|
+
expectedBindingRevision:state.bindingRevision,
|
|
322
|
+
writeRequestId:m.sourceRequestId+':bind',
|
|
323
|
+
attemptId:m.sourceRequestId,checkpointId:m.checkpointId,
|
|
324
|
+
targetGeneration:s.generation+1,
|
|
325
|
+
runCredential:m.nextRunCredential,
|
|
326
|
+
runRef:s.sessionId+':'+s.taskId+':'+(s.generation+1)
|
|
327
|
+
},next.session);
|
|
328
|
+
ok(bound?.status==='BOUND'
|
|
329
|
+
&&bound.runRef===s.sessionId+':'+s.taskId+':'+(s.generation+1)
|
|
330
|
+
&&bound.bindingRevision===state.bindingRevision+1,
|
|
331
|
+
'SCOPED_HANDOFF_BIND_UNKNOWN');
|
|
332
|
+
return {status:'CONFIRMED',scope:'TASK_SCOPED_HANDOFF_RECEIPT',
|
|
333
|
+
nativeOwnerRevision:next.session.revision,
|
|
334
|
+
generation:next.session.generation,
|
|
335
|
+
bindingRevision:bound.bindingRevision};
|
|
336
|
+
}
|
|
337
|
+
return Object.freeze({handle});
|
|
338
|
+
}
|