@tomflow/chatloop-extension 0.4.41 → 0.4.43
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 +2 -2
- package/bin/cli.mjs +1 -1
- package/bin/native-host.mjs +2 -1
- package/extension/actions/session.js +24 -13
- package/extension/background.js +2 -0
- package/extension/manifest.json +1 -1
- package/extension/worker/entry-admission-v2.js +53 -0
- package/extension/worker/readiness.js +2 -0
- package/extension/worker/task-cancel-recover-v2.js +98 -0
- package/extension/worker/task-cancel-recovery-v2.js +21 -9
- package/extension/worker/task-cancel-v2.js +21 -0
- package/extension/worker/task-routes-v2.js +6 -0
- package/lib/bridge.mjs +3 -0
- package/lib/cancel-recovery-intent.mjs +33 -0
- package/lib/scoped-cancel-recovery.mjs +161 -0
- package/lib/scoped-cancel-v2.mjs +64 -2
- package/lib/scoped-native-control.mjs +8 -15
- package/lib/start-session-id.mjs +13 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -60,7 +60,7 @@ The Context and Extension package updates are untested and unreleased.
|
|
|
60
60
|
| `extension status` + `projectId/taskId` | `actions/status.js` → `CHAT_LOOP_TASK_STATUS_V2` → Native `context-status-v2` → Context 权威只读 `run.terminalStatus`,不选择 latest Task | 仅状态;无 Chat/无 Writer 的 V2 原始 Adopt `STARTING/RESERVED/PENDING` 统一显示只读 `PRE_ADOPT_UNBOUND`(不等于发送或启动 PASS),若该阶段 Context 出现绑定/待决副作用则显示 `FENCED_UNCERTAIN`,而不是误报 `PENDING_SEND_UNRESOLVED/VERIFIED`;原取消确已 Native `CANCELLED +2` 且 Context 已取消才显示 `CANCEL_ACK_REVIEW`,若只达到 Native `BLOCKED +1` 则显示 `FENCED_UNCERTAIN`;Handoff 新代 Native 已切换但 Context 未绑定亦显示 `FENCED_UNCERTAIN`,只有新代 Revision 精确 `+1`、Context Writer/审批/项目规则和零待决副作用一致时才显示 `HANDOFF_ACK_REVIEW`;即使 Worker/Native Phase 一致,Context 中非空的 Writer RunRef 也必须属于当前 Session/Task/generation(Native 只暴露 `writerMatchesOwner` 布尔证明,不暴露凭据),否则仅显示 `FENCED_UNCERTAIN` 而非 `VERIFIED`;Context Writer 失权或终态交付未结算亦同;Native 只暴露经 Context 核验的 `deliveryCommitted` 布尔值及精确匹配当前 Session/Generation 的 `sessionCancelled`,不泄露交付正文、取消证据或 Writer 凭据(均不是新许可);Handoff 预发送中止须原 Worker `NOT_SENT` 与 Native 的新只读 `handoff-abort-fence-read-v2` 同时证实原签发请求、USED Permit、Checkpoint、BLOCKED 撤销修订 +1 与 Context 仍 pending,才显示 `HANDOFF_PRE_SEND_FENCED`,否则仍为 FENCED_UNCERTAIN;两者均不代表恢复许可。无参数时仍旧 V1 Status |
|
|
61
61
|
| `extension continue` + `taskFirst:true/turnToken` | `task-intent-v2.js` → 同一个 `continue.html` → `task-action-v2.js` → Native `continue-{permit,send-check,result}-v2` → Context `permit.consume/permit.status` | 原 Start/Model Intent/Continue/Handoff/Cancel Action 均校验签发 URL + 扩展自身顶层 Frame,且真实动作继续独立核 Native/Context 原请求;原 Action 唯一 `sendOnce`;持久 `SEND_BEGIN` 后至多点一次;新 Turn Token 只能在精确回执后更换,UNKNOWN 不重发 |
|
|
62
62
|
| `extension handoff` + `taskFirst:true/turnToken` | 原 `handoff.html` 动态装配 `handoff-scoped.js`,复用 `verifyOldChat/waitForNewChat/sendHandoffOnce`;Native `handoff-{permit,send-check,result}-v2` 使用 Context 原 `run.fence/run.bind.handoff` | 旧 Writer 围栏后新建一次 Chat;原 Context Checkpoint / 代际 / 真实 DOM / Native / Context 双回执才转 Owner;Handoff Permit 的 Native PENDING CAS 与 Context Fence Binding 都须精确推进 `+1`(而非仅大于旧值)。新 Credential 仅留在一次性 Action,不写 Worker Store |
|
|
63
|
-
| `extension session` + `taskFirst:true/expectedNativeRevision` | `task-cancel-v2.js` 先核扩展自身顶层 Action Tab/正式 session.html 签发 URL → `task-cancel-v2` Native → Context `run.idleStartPermitStatus` 与 `run.fence.cancel` 同事务复核历史 Permit → Native+Worker `CANCELLED` | 只取消未执行业务的初始 Adopt;任何已消耗 Continue/Handoff Permit 均不可冒充空闲;不修改 Task DONE |
|
|
63
|
+
| `extension session` + `taskFirst:true/expectedNativeRevision` | `task-cancel-v2.js` 先核扩展自身顶层 Action Tab/正式 session.html 签发 URL → `task-cancel-preflight-v2` Native/Context 只读资格核验 **在 Worker 冻结前** → `task-cancel-v2` Native → Context `run.idleStartPermitStatus` 与 `run.fence.cancel` 同事务复核历史 Permit → Native+Worker `CANCELLED` | 只取消未执行业务的初始 Adopt;Stage 03 审计 progress 原样保留,不等同于执行 Permit;任何已消耗 Continue/Handoff Permit 均不可冒充空闲;不修改 Task DONE |
|
|
64
64
|
| `extension session` + `operation:recover-handoff` / `taskFirst:true` | 原签发请求→同一 `session.html` Action 显示原 Project/Task,要求浏览器中 60 秒内的可信用户点击(`confirmed:true` 参数不能代替;未点击不进入 Worker)→`task-recover-v2.js` 核原 Worker `NOT_SENT`、`sendBeginAt=null`、原 Action Tab/Chat/已结算 Resource Hold(若有先前 Alarm 的持久结算回执则不重复要求已关闭的 Action;否则必须重新核对实时 Action/opener)→Native `task-recover-{handoff,read,finalize}-v2` 原撤销/许可证明→Context 唯一 `RECOVER_HANDOFF` Permit 与 `run.restore-fenced-handoff`→Native `BLOCKED→WAITING_REPLY` 精确 CAS→Worker 独立投影 CAS;所有调用仍走私有签名 IPC | 恢复**原 Task、原 Chat、原 generation** 的 Writer;不新建 Chat、不自动发送;一次新凭据仅存原 0600 私有请求和正式回执,不入 Worker Store。IPC 丢 ACK 只查原请求及其原 Permit/Writer/CAS;Native 先提交但 Worker 未落盘,只读修复同 Task 投影;未知不生成第二个恢复请求。源码 **NOT_RUN**,不是已安装功能 |
|
|
65
65
|
| Observer `DONE/BLOCKED` | `task-terminal-v2.js` → Native `task-terminal-v2` → Context 交付/终态 Writer Fence;定时器只重核原 Turn,不能再发送 | 仅 Context 已确认 DONE 才释放已完成任务;普通 BLOCKED 锁住原任务,不影响 B |
|
|
66
66
|
| 终态保留 | 原唯一分钟 Alarm → `task-retire-v2` → Context 再证 DONE 或 `CANCELLED_NOT_DELIVERED` → Native 单 Task CAS 删除 + **同一 Ledger 内的精确归档回执** → Worker 同 Task Revision 删除 | 至少保留 24h;BLOCKED/UNKNOWN 绝不 GC。丢失首次 Native ACK 只能查询本 Ledger 的原 `TaskKey+Session+generation+revision` 归档回执;同一回执返回完整 `projectId/taskId/sessionId/generation/nativeOwnerRevision/terminal`,Worker Task Store 删除前必须逐一与当前槽位匹配,不能由 Native Owner 缺席或不完整回执猜测已经归档。回执有界保留,过期/耗尽时 `UNVERIFIED`,不可误删 Worker |
|
|
@@ -73,7 +73,7 @@ V2 Continue/Handoff 的原 `intentId` 或 `lookup:true` **只读查询**仍复
|
|
|
73
73
|
|
|
74
74
|
**私有请求时间窗**:旧版 CLI/Native Action 请求继续 20 分钟过期和清理;仅确实包含 `taskFirst:true` 的 V2 Continue/Handoff 模型意图,其原私有 sourceRequestId 必须跨越当前助手轮次,因此将 TTL 有界提高到四小时,清理仅保留未到期的这类签发记录。过期或失去原 Context/Native/Turn 身份仍拒绝,不凭保留请求文件赋予发送许可。不得把 TTL 提升当作聊天无限续接。
|
|
75
75
|
|
|
76
|
-
**取消终态的丢 ACK 对账**:正常原签发取消同只读 ACK 恢复均要求 Native `WAITING_REPLY→BLOCKED→CANCELLED` 精确各 `+1`(总 `+2`)及 Context Binding Revision `+1`,不接受仅“修订变大”的回执。正式 `session taskFirst cancel` 先用原 Worker Task CAS 进入 `SESSION_CANCEL_PENDING`,Native 将旧 Owner 围栏、Context `run.fence.cancel` 和 Native `CANCELLED` 依次提交。若一次性 ACK 在后两者全部提交后丢失,`task-cancel-read-v2` **只读**核查同 Task/Session/generation、原 requestId 对应的 Context `cancel-<requestId>`、Native 两步 Revision、Context BindingRevision/空 Writer,再由原 Alarm 的 `task-cancel-recovery-v2.js` 用 Worker CAS 写入 `CANCELLED`;Task Status 表示为 `CANCEL_ACK_REVIEW` 而非自动 PASS。若 Native 仍是 BLOCKED、Context 已取消但 Native 终态未提交,不能通过这条只读路径猜测成功或重跑取消;保留 Stage06 的阻断待权威恢复。
|
|
76
|
+
**Stage 06 已冻结取消恢复(新源码,未部署)**:当旧版 `session cancel` 已使原 Task 的 Worker/Native 冻结为 `BLOCKED`、Context `run.fence.cancel` 因 Stage 03 审计进度误判拒绝且 Writer 尚绑定时,不重复原 Cancel、不绕过 Owner。通过原 `session.html` 的独立 `recover-cancel` Action 接收可信点击,验证旧 `cancelRequestId` 与同 Task/Session/generation、Native BLOCKED rev、Context Writer 与唯一 START Permit;仅在全链证明未开始业务执行时按新的幂等请求围栏原 Writer、提交 Native/Worker CANCELLED,永不制造 Task DONE。恢复请求入原 Worker Task Store;ACK 丢失只读对账原请求,部分提交保持 BLOCKED 供另一个受控恢复节点处理。与普通 Handoff 恢复、原始 Cancel 的 ACK 补偿不可混用。此条是候选合同,**不代表生产扩展已加载或本次阻断已解决**。\n\n**取消终态的丢 ACK 对账**:正常原签发取消同只读 ACK 恢复均要求 Native `WAITING_REPLY→BLOCKED→CANCELLED` 精确各 `+1`(总 `+2`)及 Context Binding Revision `+1`,不接受仅“修订变大”的回执。正式 `session taskFirst cancel` 先用原 Worker Task CAS 进入 `SESSION_CANCEL_PENDING`,Native 将旧 Owner 围栏、Context `run.fence.cancel` 和 Native `CANCELLED` 依次提交。若一次性 ACK 在后两者全部提交后丢失,`task-cancel-read-v2` **只读**核查同 Task/Session/generation、原 requestId 对应的 Context `cancel-<requestId>`、Native 两步 Revision、Context BindingRevision/空 Writer,再由原 Alarm 的 `task-cancel-recovery-v2.js` 用 Worker CAS 写入 `CANCELLED`;Task Status 表示为 `CANCEL_ACK_REVIEW` 而非自动 PASS。若 Native 仍是 BLOCKED、Context 已取消但 Native 终态未提交,不能通过这条只读路径猜测成功或重跑取消;保留 Stage06 的阻断待权威恢复。
|
|
77
77
|
|
|
78
78
|
**Observer 恢复**:成功 Continue/Handoff 的已确认新 User Turn ID 写在同 Task 的 `awaitingUserKey`,重启/周期 Alarm 只能给这个 Turn 重新挂接原只读 Observer,不重发;完成观察后清除等待标识。误匹配其他人的新 User Turn 一律拒绝。以上均不解除未解决的 Handoff NOT_SENT、跨 Profile 及混版切流门禁。
|
|
79
79
|
|
package/bin/cli.mjs
CHANGED
|
@@ -21,7 +21,7 @@ const HELP={
|
|
|
21
21
|
'extension handoff':'Usage: chatloop-extension extension handoff (JSON intent on stdin)\nRequest early handoff or query receipt; does not transfer directly.',
|
|
22
22
|
'extension status':'Usage: chatloop-extension extension status [optional JSON {projectId,taskId} on stdin]\nWithout input reads legacy V1 state. Explicit Task status never guesses the latest Task.',
|
|
23
23
|
'extension start':'Usage: chatloop-extension extension start (JSON on stdin)\nStart a new task with prompt, or use mode=adopt (no prompt) to bind the only R/P-approved existing Chat without sending.',
|
|
24
|
-
'extension session':'Usage: chatloop-extension extension session (JSON intent on stdin)\nCancel an
|
|
24
|
+
'extension session':'Usage: chatloop-extension extension session (JSON intent on stdin)\nCancel an idle Adopt, complete a verified frozen cancel, or recover a proven unsent Handoff. Recovery requires a real click in the signed session Action; never sends Chat messages.',
|
|
25
25
|
'extension entry test':'Usage: chatloop-extension extension entry test (JSON intent on stdin)\nTEST_ONLY: use the built-in Extension Action to create a new normal Chat, choose the actual native @ App, and send exactly once without a Browser Owner.'
|
|
26
26
|
};
|
|
27
27
|
function helpFor(args){
|
package/bin/native-host.mjs
CHANGED
|
@@ -61,7 +61,8 @@ process.stdin.on('data',async chunk=>{
|
|
|
61
61
|
'handoff-abort-fence-read-v2','handoff-result-v2',
|
|
62
62
|
'handoff-result-read-v2','task-terminal-v2',
|
|
63
63
|
'task-terminal-read-v2','task-cancel-v2',
|
|
64
|
-
'task-cancel-read-v2','task-retire-v2',
|
|
64
|
+
'task-cancel-preflight-v2','task-cancel-read-v2','task-retire-v2',
|
|
65
|
+
'task-cancel-recover-v2','task-cancel-recover-read-v2',
|
|
65
66
|
'task-recover-handoff-v2','task-recover-read-v2',
|
|
66
67
|
'task-recover-finalize-v2'].includes(m.op)){
|
|
67
68
|
write(await handleScopedNativeMessage(m));
|
|
@@ -10,9 +10,16 @@ const show=(status,text)=>{statusNode.textContent=status;
|
|
|
10
10
|
async function confirmRecoveryClick(intent){
|
|
11
11
|
const button=document.getElementById('confirm-recovery');
|
|
12
12
|
if(!button)throw Error('SCOPED_RECOVERY_CONFIRM_UI_MISSING');
|
|
13
|
+
const recoveringCancel=intent.operation==='recover-cancel';
|
|
14
|
+
button.textContent=recoveringCancel
|
|
15
|
+
?'确认完成原 Session 的安全取消(不交付、不发送)'
|
|
16
|
+
:'确认恢复原 Task 写权(不会发送 Chat 消息)';
|
|
13
17
|
show('CONFIRMATION_REQUIRED',
|
|
14
18
|
'项目:'+intent.projectId+'\n任务:'+intent.taskId
|
|
15
|
-
+
|
|
19
|
+
+(recoveringCancel
|
|
20
|
+
?'\n继续同一条已冻结 Session 的取消;先核对 Context、Native 和原请求,不发送也不交付。'
|
|
21
|
+
:'\n仅恢复原 Chat 的写权;不会创建 Chat 或发送消息。')
|
|
22
|
+
+'\n请在本页确认。');
|
|
16
23
|
button.hidden=false;
|
|
17
24
|
// Bound the real-click window below the CLI's 90-second wait.
|
|
18
25
|
// A later Native/Context ACK can still be UNKNOWN: reconcile by request ID.
|
|
@@ -34,7 +41,7 @@ async function run(){
|
|
|
34
41
|
const own=await chrome.tabs.getCurrent(),params=new URLSearchParams(location.search);
|
|
35
42
|
const id=params.get('requestId'),operation=params.get('operation');
|
|
36
43
|
if(!own?.id||own.url!==location.href||[...params.keys()].join(',')!=='requestId,operation'
|
|
37
|
-
||!['cancel','recover-handoff'].includes(operation)
|
|
44
|
+
||!['cancel','recover-handoff','recover-cancel'].includes(operation)
|
|
38
45
|
||!/^[a-f0-9]{32}$/.test(id||''))
|
|
39
46
|
throw Error('INVALID_SESSION_ACTION');
|
|
40
47
|
const acquired=await chrome.runtime.sendNativeMessage(host,{
|
|
@@ -46,19 +53,23 @@ async function run(){
|
|
|
46
53
|
throw Error(acquired?.code||'SESSION_INTENT_UNVERIFIED');
|
|
47
54
|
let result;
|
|
48
55
|
try{
|
|
49
|
-
if(operation
|
|
56
|
+
if(operation!=='cancel')await confirmRecoveryClick(acquired.intent);
|
|
50
57
|
show('RUNNING',operation==='cancel'
|
|
51
58
|
?'核验 Session、停止调度并撤销 Context 写权…'
|
|
52
|
-
:'
|
|
59
|
+
:operation==='recover-cancel'
|
|
60
|
+
?'核验旧取消请求和已冻结 Owner,完成 Context 围栏;不会发送消息…'
|
|
61
|
+
:'核验原 Handoff 未发送证据及一次性恢复许可;不会发送消息…');
|
|
53
62
|
result=await chrome.runtime.sendMessage({
|
|
54
|
-
type:operation==='recover-
|
|
55
|
-
?'
|
|
56
|
-
:
|
|
63
|
+
type:operation==='recover-cancel'
|
|
64
|
+
?'CHAT_LOOP_TASK_CANCEL_RECOVER_V2'
|
|
65
|
+
:operation==='recover-handoff'
|
|
66
|
+
?'CHAT_LOOP_TASK_HANDOFF_RECOVER_V2'
|
|
67
|
+
:acquired.intent?.taskFirst===true
|
|
57
68
|
?'CHAT_LOOP_TASK_SESSION_CANCEL_V2':'CHAT_LOOP_SESSION_CONTROL_V1',
|
|
58
69
|
requestId:id,operation
|
|
59
70
|
});
|
|
60
|
-
if(result?.status!=='OK'||result.state!==(operation==='
|
|
61
|
-
?'
|
|
71
|
+
if(result?.status!=='OK'||result.state!==(operation==='recover-handoff'
|
|
72
|
+
?'WRITER_RESTORED_NO_SEND':'CANCELLED_NOT_DELIVERED'))
|
|
62
73
|
throw Error(result?.code||'SESSION_OPERATION_UNVERIFIED');
|
|
63
74
|
const receipt=await chrome.runtime.sendNativeMessage(host,{
|
|
64
75
|
op:'complete',id,nonce:acquired.nonce,action:'session',
|
|
@@ -66,16 +77,16 @@ async function run(){
|
|
|
66
77
|
});
|
|
67
78
|
if(receipt?.status!=='SAVED'||receipt.id!==id)
|
|
68
79
|
throw Error(receipt?.code||'SESSION_RESULT_UNVERIFIED');
|
|
69
|
-
show('SUCCESS',operation==='
|
|
70
|
-
?'
|
|
71
|
-
:'
|
|
80
|
+
show('SUCCESS',operation==='recover-handoff'
|
|
81
|
+
?'原 Task Writer 已恢复;本次没有发送 Chat 消息。'
|
|
82
|
+
:'Session 已安全取消;业务任务未交付。');
|
|
72
83
|
await chrome.tabs.remove(own.id);
|
|
73
84
|
}catch(error){
|
|
74
85
|
// The Worker may have fenced Context before a transport failure. Never
|
|
75
86
|
// retry a mutation; the original request and persisted receipt are proof.
|
|
76
87
|
const raw=error?.message;
|
|
77
88
|
const code=/^[A-Z][A-Z0-9_]{1,95}$/.test(raw||'')?raw:'SESSION_ACTION_UNVERIFIED';
|
|
78
|
-
if(operation
|
|
89
|
+
if(operation!=='cancel'){
|
|
79
90
|
// A permit/bind/Native CAS may already have committed. Never seal
|
|
80
91
|
// an ambiguous recovery as a definitive FAILED Action receipt:
|
|
81
92
|
// preserve this exact signed request for read-only reconciliation.
|
package/extension/background.js
CHANGED
|
@@ -19,9 +19,11 @@ importScripts("worker/task-handoff-recovery-v2.js");
|
|
|
19
19
|
importScripts("worker/task-hold-recovery-v2.js");
|
|
20
20
|
importScripts("worker/task-terminal-v2.js");
|
|
21
21
|
importScripts("worker/task-cancel-v2.js");
|
|
22
|
+
importScripts("worker/task-cancel-recover-v2.js");
|
|
22
23
|
importScripts("worker/task-recover-v2.js");
|
|
23
24
|
importScripts("worker/task-cancel-recovery-v2.js");
|
|
24
25
|
importScripts("worker/session-control.js");
|
|
26
|
+
importScripts("worker/entry-admission-v2.js");
|
|
25
27
|
importScripts("worker/readiness.js");
|
|
26
28
|
importScripts("worker/turn-failure.js");
|
|
27
29
|
importScripts("worker/turn-finished.js");
|
package/extension/manifest.json
CHANGED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// A live V2 Task is NOT a global blocker for an independent TEST_ONLY Chat.
|
|
2
|
+
// V1 Native session-read deliberately rejects active V2 Ledger state; retain
|
|
3
|
+
// that fence and verify each current scoped Worker/Native/Context writer instead.
|
|
4
|
+
// No Task is adopted, fenced, resumed, or sent through the Owner path here.
|
|
5
|
+
async function verifiedScopedEntryAdmission(){
|
|
6
|
+
if(!scopedTaskRuns)return {status:'DENIED',code:'ENTRY_TEST_SCOPED_STORE_MISSING'};
|
|
7
|
+
const legacy=await readRun();
|
|
8
|
+
if(legacy&&(!['DONE','CANCELLED','BLOCKED'].includes(legacy.phase)
|
|
9
|
+
||legacy.handoffPending))
|
|
10
|
+
return {status:'DENIED',code:'ENTRY_TEST_LEGACY_RUN_ACTIVE'};
|
|
11
|
+
const snapshot=await scopedTaskRuns.read();
|
|
12
|
+
const active=Object.values(snapshot.items).map(x=>x.run)
|
|
13
|
+
.filter(run=>!['DONE','CANCELLED'].includes(run.phase));
|
|
14
|
+
if(!active.length||active.length>32)
|
|
15
|
+
return {status:'DENIED',code:'ENTRY_TEST_SCOPED_OWNER_UNVERIFIED'};
|
|
16
|
+
for(const run of active){
|
|
17
|
+
if(run.phase!=='WAITING_REPLY'||typeof run.projectId!=='string'
|
|
18
|
+
||typeof run.taskId!=='string'||typeof run.sessionId!=='string'
|
|
19
|
+
||!Number.isSafeInteger(run.generation)||run.generation<0
|
|
20
|
+
||!Number.isSafeInteger(run.nativeOwnerRevision)
|
|
21
|
+
||run.nativeOwnerRevision<1)
|
|
22
|
+
return {status:'DENIED',code:'ENTRY_TEST_SCOPED_OWNER_UNVERIFIED'};
|
|
23
|
+
const args={projectId:run.projectId,taskId:run.taskId,
|
|
24
|
+
sessionId:run.sessionId,generation:run.generation};
|
|
25
|
+
const native=await chrome.runtime.sendNativeMessage(SESSION_HOST,{
|
|
26
|
+
op:'session-snapshot-v2',...args}).catch(()=>null);
|
|
27
|
+
const s=native?.session;
|
|
28
|
+
if(native?.status!=='OK'||native.scope!=='TASK_SCOPED_SNAPSHOT'
|
|
29
|
+
||s?.projectId!==run.projectId||s.taskId!==run.taskId
|
|
30
|
+
||s.sessionId!==run.sessionId||s.generation!==run.generation
|
|
31
|
+
||s.phase!=='WAITING_REPLY'||s.revision!==run.nativeOwnerRevision
|
|
32
|
+
||typeof s.activeChatId!=='string'
|
|
33
|
+
||s.activeChatId!==run.ownerChatId)
|
|
34
|
+
return {status:'DENIED',code:'ENTRY_TEST_SCOPED_OWNER_MISMATCH'};
|
|
35
|
+
const writer=await chrome.runtime.sendNativeMessage(SESSION_HOST,{
|
|
36
|
+
op:'context-writer-check-v2',...args,
|
|
37
|
+
expectedSessionRevision:run.nativeOwnerRevision}).catch(()=>null);
|
|
38
|
+
if(writer?.status!=='OK'||writer.scope!=='TASK_SCOPED_WRITER_VERIFIED'
|
|
39
|
+
||writer.projectId!==run.projectId||writer.taskId!==run.taskId
|
|
40
|
+
||writer.sessionId!==run.sessionId
|
|
41
|
+
||writer.nativeOwnerRevision!==run.nativeOwnerRevision)
|
|
42
|
+
return {status:'DENIED',code:'ENTRY_TEST_SCOPED_WRITER_UNVERIFIED'};
|
|
43
|
+
}
|
|
44
|
+
const reread=await scopedTaskRuns.read();
|
|
45
|
+
const current=Object.values(reread.items).map(x=>x.run)
|
|
46
|
+
.filter(run=>!['DONE','CANCELLED'].includes(run.phase));
|
|
47
|
+
const identity=run=>[run.projectId,run.taskId,run.sessionId,
|
|
48
|
+
run.generation,run.nativeOwnerRevision,run.phase,run.ownerChatId].join('|');
|
|
49
|
+
if(current.length!==active.length
|
|
50
|
+
||current.some((run,i)=>identity(run)!==identity(active[i])))
|
|
51
|
+
return {status:'DENIED',code:'ENTRY_TEST_SCOPED_OWNER_CHANGED'};
|
|
52
|
+
return {status:'OK',scope:'TEST_ONLY',mode:'VERIFIED_SCOPED_OWNER_DISTINCT_CHAT'};
|
|
53
|
+
}
|
|
@@ -126,6 +126,8 @@ async function entryTestAdmission(message,sender){
|
|
|
126
126
|
return {status:'DENIED',code:'ENTRY_TEST_SOURCE_UNVERIFIED'};
|
|
127
127
|
const disk=await chrome.runtime.sendNativeMessage(SESSION_HOST,
|
|
128
128
|
{op:'session-read'}).catch(()=>null);
|
|
129
|
+
if(disk?.code==='OWNER_LEDGER_ACTIVE_USE_SCOPED_QUERY')
|
|
130
|
+
return verifiedScopedEntryAdmission();
|
|
129
131
|
if(disk?.status!=='OK')return {status:'DENIED',code:'ENTRY_TEST_NATIVE_UNVERIFIED'};
|
|
130
132
|
const run=await readRun(),record=disk.session;
|
|
131
133
|
if(!run?.sessionId){
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// Stage 06: explicit confirmation of a frozen, still-bound cancel.
|
|
2
|
+
// Only the existing signed session.html Action may enter; no Chat send.
|
|
3
|
+
const cancelRecoveryFail=code=>Object.assign(Error(code),{code});
|
|
4
|
+
async function scopedTaskRecoverCancel(message,sender){
|
|
5
|
+
const id=message?.requestId;
|
|
6
|
+
if(sender?.id!==chrome.runtime.id||sender.frameId!==0
|
|
7
|
+
||!Number.isSafeInteger(sender.tab?.id)||sender.tab.id<=0
|
|
8
|
+
||!/^[a-f0-9]{32}$/.test(id||'')
|
|
9
|
+
||sender.url!==chrome.runtime.getURL('session.html')
|
|
10
|
+
+'?requestId='+id+'&operation=recover-cancel'
|
|
11
|
+
||message.operation!=='recover-cancel'
|
|
12
|
+
||Object.keys(message||{}).length!==3)
|
|
13
|
+
throw cancelRecoveryFail('SCOPED_CANCEL_RECOVER_ACTION_INVALID');
|
|
14
|
+
const source=await chrome.runtime.sendNativeMessage(SESSION_HOST,{
|
|
15
|
+
op:'acquire',id,action:'session'});
|
|
16
|
+
const i=source?.intent;
|
|
17
|
+
if(source?.status!=='OK'||source.action!=='session'||source.id!==id
|
|
18
|
+
||i?.operation!=='recover-cancel'
|
|
19
|
+
||i.taskFirst!==true||i.confirmed!==true)
|
|
20
|
+
throw cancelRecoveryFail('SCOPED_CANCEL_RECOVER_SOURCE_UNVERIFIED');
|
|
21
|
+
return scopedTaskScheduler.perTask(i.projectId,i.taskId,async()=>{
|
|
22
|
+
const snap=await scopedTaskRuns.get(i.projectId,i.taskId);
|
|
23
|
+
const run=snap.run;
|
|
24
|
+
if(!run||run.sessionId!==i.sessionId
|
|
25
|
+
||run.generation!==i.generation||run.projectId!==i.projectId
|
|
26
|
+
||run.taskId!==i.taskId
|
|
27
|
+
||run.contextStoreEpoch!==i.contextStoreEpoch
|
|
28
|
+
||(run.phase==='CANCELLED'
|
|
29
|
+
?run.contextBindingRevision!==i.expectedBindingRevision+1
|
|
30
|
+
:run.contextBindingRevision!==i.expectedBindingRevision)
|
|
31
|
+
||(run.phase==='CANCELLED'
|
|
32
|
+
?run.nativeOwnerRevision!==i.expectedNativeRevision+1
|
|
33
|
+
:run.nativeOwnerRevision+1!==i.expectedNativeRevision))
|
|
34
|
+
throw cancelRecoveryFail('SCOPED_CANCEL_RECOVER_IDENTITY_DRIFT');
|
|
35
|
+
const base={sourceRequestId:id,
|
|
36
|
+
originalCancelRequestId:i.originalCancelRequestId,
|
|
37
|
+
projectId:run.projectId,taskId:run.taskId,
|
|
38
|
+
sessionId:run.sessionId,generation:run.generation,
|
|
39
|
+
expectedSessionRevision:i.expectedNativeRevision,
|
|
40
|
+
expectedStoreEpoch:run.contextStoreEpoch,
|
|
41
|
+
expectedTaskRevision:i.expectedTaskRevision,
|
|
42
|
+
expectedBindingRevision:i.expectedBindingRevision,
|
|
43
|
+
startRequestId:run.startRequestId};
|
|
44
|
+
if(run.phase==='CANCELLED'&&run.reason==='SESSION_CANCELLED'
|
|
45
|
+
&&run.recoveryRequestId===id){
|
|
46
|
+
const r=await chrome.runtime.sendNativeMessage(SESSION_HOST,{
|
|
47
|
+
op:'task-cancel-recover-read-v2',...base}).catch(()=>null);
|
|
48
|
+
if(r?.status!=='CONFIRMED'||r.readOnly!==true
|
|
49
|
+
||r.nativeOwnerRevision!==run.nativeOwnerRevision)
|
|
50
|
+
throw cancelRecoveryFail('SCOPED_CANCEL_RECOVER_RECEIPT_MISSING');
|
|
51
|
+
return {status:'OK',state:'CANCELLED_NOT_DELIVERED',
|
|
52
|
+
projectId:run.projectId,taskId:run.taskId,sessionId:run.sessionId};
|
|
53
|
+
}
|
|
54
|
+
if(run.phase!=='BLOCKED'||run.reason!=='SESSION_CANCEL_PENDING'
|
|
55
|
+
||run.cancelRequestId!==i.originalCancelRequestId
|
|
56
|
+
||!run.startRequestId||run.modelIntent||run.nextAttempt
|
|
57
|
+
||run.lastIntentReceipt||run.handoffFence
|
|
58
|
+
||run.recoveryRequestId&&run.recoveryRequestId!==id)
|
|
59
|
+
throw cancelRecoveryFail('SCOPED_CANCEL_RECOVER_NOT_FROZEN');
|
|
60
|
+
const tab=await chrome.tabs.get(run.tabId).catch(()=>null);
|
|
61
|
+
if(!tab||pathOf(tab.url)!==run.page
|
|
62
|
+
||chatIdOf(tab.url)!==run.ownerChatId)
|
|
63
|
+
throw cancelRecoveryFail('SCOPED_CANCEL_RECOVER_CHAT_CHANGED');
|
|
64
|
+
const first=!run.recoveryRequestId;
|
|
65
|
+
let stamped=snap;
|
|
66
|
+
if(first){
|
|
67
|
+
stamped=await scopedTaskRuns.put({
|
|
68
|
+
...run,recoveryRequestId:id},{expectedStoreRevision:snap.storeRevision,
|
|
69
|
+
expectedRunRevision:snap.runRevision});
|
|
70
|
+
}
|
|
71
|
+
// Never reissue a possibly committed Context cancellation for the same
|
|
72
|
+
// recovery request. Later attempts/Alarm can only read its receipt.
|
|
73
|
+
const result=await chrome.runtime.sendNativeMessage(SESSION_HOST,{
|
|
74
|
+
op:first?'task-cancel-recover-v2':'task-cancel-recover-read-v2',
|
|
75
|
+
...base}).catch(()=>null);
|
|
76
|
+
if(result?.status!=='CONFIRMED'
|
|
77
|
+
||result.scope!=='TASK_SCOPED_CANCELLED_NOT_DELIVERED'
|
|
78
|
+
||result.nativeOwnerRevision!==run.nativeOwnerRevision+2
|
|
79
|
+
||result.contextBindingRevision!==run.contextBindingRevision+1
|
|
80
|
+
||(!first&&result.readOnly!==true))
|
|
81
|
+
throw cancelRecoveryFail('SCOPED_CANCEL_RECOVER_RESULT_UNKNOWN');
|
|
82
|
+
const fresh=await scopedTaskRuns.get(run.projectId,run.taskId);
|
|
83
|
+
if(fresh.runRevision!==stamped.runRevision
|
|
84
|
+
||fresh.run?.recoveryRequestId!==id)
|
|
85
|
+
throw cancelRecoveryFail('SCOPED_CANCEL_RECOVER_WORKER_CHANGED');
|
|
86
|
+
const final={...fresh.run,phase:'CANCELLED',
|
|
87
|
+
reason:'SESSION_CANCELLED',terminalAt:Date.now(),
|
|
88
|
+
nativeOwnerRevision:result.nativeOwnerRevision,
|
|
89
|
+
contextBindingRevision:result.contextBindingRevision};
|
|
90
|
+
await scopedTaskRuns.put(final,{
|
|
91
|
+
expectedStoreRevision:fresh.storeRevision,
|
|
92
|
+
expectedRunRevision:fresh.runRevision,
|
|
93
|
+
transitionKind:'CONTEXT_IDLE_CANCELLED'});
|
|
94
|
+
await resources.terminal(final).catch(()=>{});
|
|
95
|
+
return {status:'OK',state:'CANCELLED_NOT_DELIVERED',
|
|
96
|
+
projectId:run.projectId,taskId:run.taskId,sessionId:run.sessionId};
|
|
97
|
+
});
|
|
98
|
+
}
|
|
@@ -17,15 +17,27 @@ async function reconcileScopedCancelled(){
|
|
|
17
17
|
||s.cancelRequestId!==run.cancelRequestId
|
|
18
18
|
||s.sessionId!==run.sessionId
|
|
19
19
|
||s.generation!==run.generation)return;
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
20
|
+
const recovering=typeof s.recoveryRequestId==='string';
|
|
21
|
+
const verified=await chrome.runtime.sendNativeMessage(SESSION_HOST,
|
|
22
|
+
recovering?{
|
|
23
|
+
op:'task-cancel-recover-read-v2',
|
|
24
|
+
sourceRequestId:s.recoveryRequestId,
|
|
25
|
+
originalCancelRequestId:s.cancelRequestId,
|
|
26
|
+
projectId:s.projectId,taskId:s.taskId,
|
|
27
|
+
sessionId:s.sessionId,generation:s.generation,
|
|
28
|
+
expectedSessionRevision:s.nativeOwnerRevision+1,
|
|
29
|
+
expectedStoreEpoch:s.contextStoreEpoch,
|
|
30
|
+
expectedBindingRevision:s.contextBindingRevision,
|
|
31
|
+
startRequestId:s.startRequestId
|
|
32
|
+
}:{
|
|
33
|
+
op:'task-cancel-read-v2',
|
|
34
|
+
sourceRequestId:s.cancelRequestId,
|
|
35
|
+
projectId:s.projectId,taskId:s.taskId,
|
|
36
|
+
sessionId:s.sessionId,generation:s.generation,
|
|
37
|
+
expectedPriorSessionRevision:s.nativeOwnerRevision,
|
|
38
|
+
expectedBindingRevision:s.contextBindingRevision,
|
|
39
|
+
expectedActiveChatUrl:'https://chatgpt.com'+s.page
|
|
40
|
+
}).catch(()=>null);
|
|
29
41
|
if(verified?.status!=='CONFIRMED'
|
|
30
42
|
||verified.scope!=='TASK_SCOPED_CANCELLED_NOT_DELIVERED'
|
|
31
43
|
||verified.readOnly!==true
|
|
@@ -51,6 +51,27 @@ async function scopedTaskCancel(message,sender){
|
|
|
51
51
|
||route.taskRevision!==intent.expectedTaskRevision)
|
|
52
52
|
throw Object.assign(Error('Task Cancel writer drift'),
|
|
53
53
|
{code:'SCOPED_CANCEL_WRITER_CHANGED'});
|
|
54
|
+
// Refuse known Context/permit/Owner conflicts BEFORE freezing Worker.
|
|
55
|
+
// This read-only probe grants no send; Native repeats all conditions
|
|
56
|
+
// under its own CAS, since an out-of-band Context writer can race it.
|
|
57
|
+
const preflight=await chrome.runtime.sendNativeMessage(SESSION_HOST,{
|
|
58
|
+
op:'task-cancel-preflight-v2',sourceRequestId:message.requestId,
|
|
59
|
+
projectId:run.projectId,taskId:run.taskId,
|
|
60
|
+
sessionId:run.sessionId,generation:run.generation,
|
|
61
|
+
expectedSessionRevision:run.nativeOwnerRevision,
|
|
62
|
+
expectedStoreEpoch:run.contextStoreEpoch,
|
|
63
|
+
expectedTaskRevision:intent.expectedTaskRevision,
|
|
64
|
+
expectedBindingRevision:run.contextBindingRevision,
|
|
65
|
+
startRequestId:run.startRequestId
|
|
66
|
+
}).catch(()=>null);
|
|
67
|
+
if(preflight?.status!=='OK'
|
|
68
|
+
||preflight.scope!=='TASK_SCOPED_CANCEL_PREFLIGHT_READ_ONLY'
|
|
69
|
+
||preflight.sessionId!==run.sessionId
|
|
70
|
+
||preflight.nativeOwnerRevision!==run.nativeOwnerRevision
|
|
71
|
+
||preflight.taskRevision!==intent.expectedTaskRevision
|
|
72
|
+
||preflight.bindingRevision!==run.contextBindingRevision)
|
|
73
|
+
throw Object.assign(Error('Cancel not eligible before freeze'),
|
|
74
|
+
{code:preflight?.code||'SCOPED_CANCEL_PREFLIGHT_UNVERIFIED'});
|
|
54
75
|
// Freeze this Task's Worker queue BEFORE the Native Context write.
|
|
55
76
|
// If the IPC ACK disappears, no other Task is changed or resumed.
|
|
56
77
|
const stopped=await scopedTaskRuns.put({...run,phase:'BLOCKED',
|
|
@@ -32,6 +32,12 @@ const onScopedBrowserMessage=(message,sender,reply)=>{
|
|
|
32
32
|
code:safeWorkerErrorCode(error,'SCOPED_RECOVERY_UNVERIFIED')}));
|
|
33
33
|
return true;
|
|
34
34
|
}
|
|
35
|
+
if(type==='CHAT_LOOP_TASK_CANCEL_RECOVER_V2'){
|
|
36
|
+
void Promise.resolve().then(()=>scopedTaskRecoverCancel(message,sender))
|
|
37
|
+
.then(reply,error=>reply({status:'BLOCKED',
|
|
38
|
+
code:safeWorkerErrorCode(error,'SCOPED_CANCEL_RECOVER_UNVERIFIED')}));
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
35
41
|
if(type==='CHAT_LOOP_TASK_SESSION_CANCEL_V2'){
|
|
36
42
|
void Promise.resolve().then(()=>scopedTaskCancel(message,sender))
|
|
37
43
|
.then(reply,error=>reply({status:'BLOCKED',
|
package/lib/bridge.mjs
CHANGED
|
@@ -2,6 +2,7 @@ import {randomBytes,randomUUID} from 'node:crypto';
|
|
|
2
2
|
import {existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, writeFileSync} from 'node:fs';
|
|
3
3
|
import {homedir, tmpdir} from 'node:os';
|
|
4
4
|
import {join} from 'node:path';
|
|
5
|
+
import {validateCancelRecoveryIntent} from './cancel-recovery-intent.mjs';
|
|
5
6
|
|
|
6
7
|
export const HOST_NAME = 'com.tomflow.chatloop.extension';
|
|
7
8
|
export let ACTIONS=Object.freeze([]);
|
|
@@ -80,6 +81,8 @@ function validateIntent(action, intent, fromStoredRequest=false) {
|
|
|
80
81
|
}
|
|
81
82
|
if(action==='session'){
|
|
82
83
|
const ID=/^[a-zA-Z0-9][a-zA-Z0-9._-]{0,127}$/;
|
|
84
|
+
if(intent?.operation==='recover-cancel')
|
|
85
|
+
return validateCancelRecoveryIntent(intent);
|
|
83
86
|
if(intent?.operation==='recover-handoff'){
|
|
84
87
|
const names=['operation','projectId','taskId','sessionId',
|
|
85
88
|
'generation','expectedNativeRevision','originalAttemptId',
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// Signed Session Action input for repairing an already-frozen idle cancel.
|
|
2
|
+
// The existing cancellation request remains bound to the Worker Task slot;
|
|
3
|
+
// this new user-approved recovery creates no Chat and consumes no new Permit.
|
|
4
|
+
export function validateCancelRecoveryIntent(intent){
|
|
5
|
+
const id=/^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/;
|
|
6
|
+
const uuid=/^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i;
|
|
7
|
+
const hex=/^[a-f0-9]{32}$/;
|
|
8
|
+
const names=['operation','projectId','taskId','sessionId','generation',
|
|
9
|
+
'expectedNativeRevision','originalCancelRequestId',
|
|
10
|
+
'expectedTaskRevision','expectedBindingRevision','contextStoreEpoch',
|
|
11
|
+
'confirmed','taskFirst'];
|
|
12
|
+
const valid=intent&&typeof intent==='object'&&!Array.isArray(intent)
|
|
13
|
+
&&Object.keys(intent).length===names.length
|
|
14
|
+
&&names.every(k=>Object.hasOwn(intent,k))
|
|
15
|
+
&&intent.operation==='recover-cancel'
|
|
16
|
+
&&id.test(intent.projectId||'')&&id.test(intent.taskId||'')
|
|
17
|
+
&&uuid.test(intent.sessionId||'')
|
|
18
|
+
&&Number.isSafeInteger(intent.generation)&&intent.generation>=0
|
|
19
|
+
&&Number.isSafeInteger(intent.expectedNativeRevision)
|
|
20
|
+
&&intent.expectedNativeRevision>=2
|
|
21
|
+
&&hex.test(intent.originalCancelRequestId||'')
|
|
22
|
+
&&Number.isSafeInteger(intent.expectedTaskRevision)
|
|
23
|
+
&&intent.expectedTaskRevision>=1
|
|
24
|
+
&&Number.isSafeInteger(intent.expectedBindingRevision)
|
|
25
|
+
&&intent.expectedBindingRevision>=1
|
|
26
|
+
&&typeof intent.contextStoreEpoch==='string'
|
|
27
|
+
&&intent.contextStoreEpoch.length>=10
|
|
28
|
+
&&intent.contextStoreEpoch.length<=128
|
|
29
|
+
&&intent.confirmed===true&&intent.taskFirst===true;
|
|
30
|
+
if(!valid)throw Object.assign(Error('Invalid cancel recovery'),{
|
|
31
|
+
code:'INVALID_CANCEL_RECOVERY_INTENT'});
|
|
32
|
+
return {...intent};
|
|
33
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
// Stage 06: explicit user-confirmed completion of a partly frozen idle cancel.
|
|
2
|
+
// The original cancel is recorded in the SAME Worker Task slot. A new, signed
|
|
3
|
+
// recovery Action is required; this module never sends a Chat or creates a Task.
|
|
4
|
+
export function createScopedCancelRecoveryControl(ports){
|
|
5
|
+
const {readRequest,readScopedOwner,writeScopedOwner,nativeContextControl}=ports;
|
|
6
|
+
const fail=code=>Object.assign(Error(code),{code});
|
|
7
|
+
const yes=(test,code)=>{if(!test)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 token=/^[a-f0-9]{32}$/;
|
|
11
|
+
const proof=s=>Object.fromEntries(['projectId','taskId','sessionId',
|
|
12
|
+
'generation','revision','phase','activeChatId'].map(k=>[k,s[k]]));
|
|
13
|
+
const context=(command,s,args)=>nativeContextControl(command,{
|
|
14
|
+
projectId:s.projectId,taskId:s.taskId,sessionId:s.sessionId,
|
|
15
|
+
generation:s.generation,activeChatId:s.activeChatId,...args
|
|
16
|
+
},{ownerProofV2:proof(s)});
|
|
17
|
+
const originalRun=s=>s.sessionId+':'+s.taskId+':'+s.generation;
|
|
18
|
+
const cancellationMatches=(state,m)=> {
|
|
19
|
+
const c=state?.sessionCancellation;
|
|
20
|
+
return state?.projectId===m.projectId
|
|
21
|
+
&&state.taskId===m.taskId
|
|
22
|
+
&&state.bindingRevision===m.expectedBindingRevision+1
|
|
23
|
+
&&state.runRef===null&&state.executionOutcome===null
|
|
24
|
+
&&state.unknownEffectCount===0
|
|
25
|
+
&&state.pendingHandoff===false&&state.pendingCheckpointId===null
|
|
26
|
+
&&c?.status==='CANCELLED_NOT_DELIVERED'
|
|
27
|
+
&&c.sessionId===m.sessionId&&c.generation===m.generation
|
|
28
|
+
&&c.oldRunRef===originalRun(m)
|
|
29
|
+
&&c.reason==='USER_CANCELLED'
|
|
30
|
+
&&['cancel-'+m.originalCancelRequestId,
|
|
31
|
+
'cancel-'+m.sourceRequestId].includes(c.requestId);
|
|
32
|
+
};
|
|
33
|
+
const source=m=>{
|
|
34
|
+
yes(m&&Object.keys(m).length===12
|
|
35
|
+
&&['task-cancel-recover-v2','task-cancel-recover-read-v2'].includes(m.op)
|
|
36
|
+
&&token.test(m.sourceRequestId||'')
|
|
37
|
+
&&token.test(m.originalCancelRequestId||'')
|
|
38
|
+
&&m.sourceRequestId!==m.originalCancelRequestId
|
|
39
|
+
&&ID.test(m.projectId||'')&&ID.test(m.taskId||'')
|
|
40
|
+
&&UUID.test(m.sessionId||'')
|
|
41
|
+
&&Number.isSafeInteger(m.generation)&&m.generation>=0
|
|
42
|
+
&&Number.isSafeInteger(m.expectedSessionRevision)
|
|
43
|
+
&&m.expectedSessionRevision>=2
|
|
44
|
+
&&typeof m.expectedStoreEpoch==='string'
|
|
45
|
+
&&m.expectedStoreEpoch.length>=10
|
|
46
|
+
&&Number.isSafeInteger(m.expectedTaskRevision)
|
|
47
|
+
&&m.expectedTaskRevision>=1
|
|
48
|
+
&&Number.isSafeInteger(m.expectedBindingRevision)
|
|
49
|
+
&&m.expectedBindingRevision>=1
|
|
50
|
+
&&token.test(m.startRequestId||''),
|
|
51
|
+
'SCOPED_CANCEL_RECOVER_SOURCE_INVALID');
|
|
52
|
+
const request=readRequest(m.sourceRequestId),i=request?.intent;
|
|
53
|
+
yes(request?.action==='session'&&i?.operation==='recover-cancel'
|
|
54
|
+
&&i.confirmed===true&&i.taskFirst===true
|
|
55
|
+
&&i.projectId===m.projectId&&i.taskId===m.taskId
|
|
56
|
+
&&i.sessionId===m.sessionId&&i.generation===m.generation
|
|
57
|
+
&&i.expectedNativeRevision===m.expectedSessionRevision
|
|
58
|
+
&&i.originalCancelRequestId===m.originalCancelRequestId
|
|
59
|
+
&&i.expectedTaskRevision===m.expectedTaskRevision
|
|
60
|
+
&&i.expectedBindingRevision===m.expectedBindingRevision
|
|
61
|
+
&&i.contextStoreEpoch===m.expectedStoreEpoch,
|
|
62
|
+
'SCOPED_CANCEL_RECOVER_INTENT_UNVERIFIED');
|
|
63
|
+
};
|
|
64
|
+
const outcome=(m,s,readOnly=false)=>({
|
|
65
|
+
status:'CONFIRMED',scope:'TASK_SCOPED_CANCELLED_NOT_DELIVERED',
|
|
66
|
+
taskId:m.taskId,sessionId:m.sessionId,generation:m.generation,
|
|
67
|
+
nativeOwnerRevision:s.revision,
|
|
68
|
+
contextBindingRevision:m.expectedBindingRevision+1,
|
|
69
|
+
...(readOnly?{readOnly:true}:{})
|
|
70
|
+
});
|
|
71
|
+
async function readCommitted(m){
|
|
72
|
+
// The minute Alarm knows the durable Worker Task slot but must not
|
|
73
|
+
// duplicate the business revision from the old signed recovery Action.
|
|
74
|
+
if(m?.op==='task-cancel-recover-read-v2'
|
|
75
|
+
&&!Object.hasOwn(m,'expectedTaskRevision')){
|
|
76
|
+
const original=readRequest(m.sourceRequestId);
|
|
77
|
+
m={...m,expectedTaskRevision:original?.intent?.expectedTaskRevision};
|
|
78
|
+
}
|
|
79
|
+
source(m);
|
|
80
|
+
const s=readScopedOwner(m.projectId,m.taskId).session;
|
|
81
|
+
yes(s&&s.sessionId===m.sessionId&&s.generation===m.generation
|
|
82
|
+
&&s.phase==='CANCELLED'
|
|
83
|
+
&&s.revision===m.expectedSessionRevision+1,
|
|
84
|
+
'SCOPED_CANCEL_RECOVER_NOT_COMMITTED');
|
|
85
|
+
const state=await context('run.terminalStatus',s,{});
|
|
86
|
+
yes(state?.storeEpoch===m.expectedStoreEpoch
|
|
87
|
+
&&state.taskRevision===m.expectedTaskRevision+1
|
|
88
|
+
&&cancellationMatches(state,m),
|
|
89
|
+
'SCOPED_CANCEL_RECOVER_CONTEXT_UNVERIFIED');
|
|
90
|
+
const after=readScopedOwner(m.projectId,m.taskId).session;
|
|
91
|
+
yes(after?.revision===s.revision&&after?.phase===s.phase
|
|
92
|
+
&&after.sessionId===s.sessionId,
|
|
93
|
+
'SCOPED_CANCEL_RECOVER_OWNER_RACED');
|
|
94
|
+
return outcome(m,s,true);
|
|
95
|
+
}
|
|
96
|
+
async function handle(m){
|
|
97
|
+
source(m);
|
|
98
|
+
const located=readScopedOwner(m.projectId,m.taskId),s=located.session;
|
|
99
|
+
yes(s&&s.sessionId===m.sessionId&&s.generation===m.generation,
|
|
100
|
+
'SCOPED_CANCEL_RECOVER_OWNER_MISSING');
|
|
101
|
+
if(s.phase==='CANCELLED'&&s.revision===m.expectedSessionRevision+1)
|
|
102
|
+
return readCommitted({...m,op:'task-cancel-recover-read-v2'});
|
|
103
|
+
yes(s.phase==='BLOCKED'&&s.revision===m.expectedSessionRevision
|
|
104
|
+
&&s.activeChatId&&s.activeChatUrl,
|
|
105
|
+
'SCOPED_CANCEL_RECOVER_OWNER_CHANGED');
|
|
106
|
+
let state=await context('run.terminalStatus',s,{});
|
|
107
|
+
yes(state?.storeEpoch===m.expectedStoreEpoch,
|
|
108
|
+
'SCOPED_CANCEL_RECOVER_STORE_CHANGED');
|
|
109
|
+
if(!cancellationMatches(state,m)){
|
|
110
|
+
yes(state?.taskRevision===m.expectedTaskRevision
|
|
111
|
+
&&state.bindingRevision===m.expectedBindingRevision
|
|
112
|
+
&&state.runRef===originalRun(s)
|
|
113
|
+
&&state.pendingHandoff===false
|
|
114
|
+
&&state.pendingCheckpointId===null
|
|
115
|
+
&&state.unknownEffectCount===0
|
|
116
|
+
&&state.executionOutcome===null
|
|
117
|
+
&&state.sessionCancellation===null
|
|
118
|
+
&&state.writerApprovalCurrent===true
|
|
119
|
+
&&state.projectRulesDrift===false,
|
|
120
|
+
'SCOPED_CANCEL_RECOVER_CONTEXT_NOT_IDLE');
|
|
121
|
+
const history=await context('run.idleStartPermitStatus',s,{
|
|
122
|
+
startActionToken:m.startRequestId});
|
|
123
|
+
yes(history?.startOnly===true&&history.cancelEligible===true,
|
|
124
|
+
'SCOPED_CANCEL_RECOVER_EXECUTION_PERMIT_EXISTS');
|
|
125
|
+
const done=await context('run.fence.cancel',s,{
|
|
126
|
+
expectedStoreEpoch:m.expectedStoreEpoch,
|
|
127
|
+
expectedTaskRevision:m.expectedTaskRevision,
|
|
128
|
+
expectedBindingRevision:m.expectedBindingRevision,
|
|
129
|
+
writeRequestId:'cancel-'+m.sourceRequestId,
|
|
130
|
+
actionToken:m.sourceRequestId,
|
|
131
|
+
scopedStartActionToken:m.startRequestId,
|
|
132
|
+
reason:'USER_CANCELLED',runRef:originalRun(s)});
|
|
133
|
+
yes(done?.status==='CANCEL_FENCED',
|
|
134
|
+
'SCOPED_CANCEL_RECOVER_FENCE_UNKNOWN');
|
|
135
|
+
state=await context('run.terminalStatus',s,{});
|
|
136
|
+
}
|
|
137
|
+
yes(state?.storeEpoch===m.expectedStoreEpoch
|
|
138
|
+
&&state.taskRevision===m.expectedTaskRevision+1
|
|
139
|
+
&&cancellationMatches(state,m),
|
|
140
|
+
'SCOPED_CANCEL_RECOVER_FENCE_NOT_VERIFIED');
|
|
141
|
+
const latest=readScopedOwner(m.projectId,m.taskId);
|
|
142
|
+
yes(latest.session?.phase==='BLOCKED'
|
|
143
|
+
&&latest.session.revision===s.revision
|
|
144
|
+
&&latest.session.sessionId===s.sessionId
|
|
145
|
+
&&latest.session.generation===s.generation,
|
|
146
|
+
'SCOPED_CANCEL_RECOVER_OWNER_RACED');
|
|
147
|
+
const committed=writeScopedOwner({
|
|
148
|
+
projectId:s.projectId,taskId:s.taskId,sessionId:s.sessionId,
|
|
149
|
+
generation:s.generation,phase:'CANCELLED',
|
|
150
|
+
activeChatId:s.activeChatId,activeChatUrl:s.activeChatUrl,
|
|
151
|
+
updatedAt:Date.now()
|
|
152
|
+
},{expectedLedgerRevision:latest.ledgerRevision,
|
|
153
|
+
expectedSessionRevision:s.revision,
|
|
154
|
+
transitionKind:'CONTEXT_IDLE_CANCELLED'});
|
|
155
|
+
yes(committed.session?.phase==='CANCELLED'
|
|
156
|
+
&&committed.session.revision===m.expectedSessionRevision+1,
|
|
157
|
+
'SCOPED_CANCEL_RECOVER_NATIVE_UNKNOWN');
|
|
158
|
+
return outcome(m,committed.session);
|
|
159
|
+
}
|
|
160
|
+
return Object.freeze({handle,readCommitted});
|
|
161
|
+
}
|
package/lib/scoped-cancel-v2.mjs
CHANGED
|
@@ -16,6 +16,67 @@ export function createScopedCancelControl(ports){
|
|
|
16
16
|
activeChatId:s.activeChatId,...args
|
|
17
17
|
},{ownerProofV2:proof(s)});
|
|
18
18
|
}
|
|
19
|
+
// Read-only admission occurs before Worker changes its Task projection.
|
|
20
|
+
// Native repeats the same hard checks in handle() after the freeze CAS:
|
|
21
|
+
// this probe never consumes a permit, changes Owner or grants send.
|
|
22
|
+
async function preflight(m){
|
|
23
|
+
yes(m&&Object.keys(m).length===11
|
|
24
|
+
&&m.op==='task-cancel-preflight-v2'
|
|
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.expectedTaskRevision)
|
|
34
|
+
&&m.expectedTaskRevision>=1
|
|
35
|
+
&&Number.isSafeInteger(m.expectedBindingRevision)
|
|
36
|
+
&&m.expectedBindingRevision>=1
|
|
37
|
+
&&/^[a-f0-9]{32}$/.test(m.startRequestId||''),
|
|
38
|
+
'SCOPED_CANCEL_PREFLIGHT_SOURCE_INVALID');
|
|
39
|
+
const source=readRequest(m.sourceRequestId),intent=source?.intent;
|
|
40
|
+
yes(source?.action==='session'&&intent?.taskFirst===true
|
|
41
|
+
&&intent.operation==='cancel'&&intent.confirmed===true
|
|
42
|
+
&&intent.projectId===m.projectId&&intent.taskId===m.taskId
|
|
43
|
+
&&intent.sessionId===m.sessionId&&intent.generation===m.generation
|
|
44
|
+
&&intent.expectedNativeRevision===m.expectedSessionRevision
|
|
45
|
+
&&intent.contextStoreEpoch===m.expectedStoreEpoch
|
|
46
|
+
&&intent.expectedTaskRevision===m.expectedTaskRevision
|
|
47
|
+
&&intent.expectedBindingRevision===m.expectedBindingRevision,
|
|
48
|
+
'SCOPED_CANCEL_PREFLIGHT_INTENT_UNVERIFIED');
|
|
49
|
+
const before=readScopedOwner(m.projectId,m.taskId),s=before.session;
|
|
50
|
+
yes(s&&s.sessionId===m.sessionId&&s.generation===m.generation
|
|
51
|
+
&&s.revision===m.expectedSessionRevision
|
|
52
|
+
&&s.phase==='WAITING_REPLY'&&s.activeChatId,
|
|
53
|
+
'SCOPED_CANCEL_PREFLIGHT_OWNER_CHANGED');
|
|
54
|
+
const history=await context('run.idleStartPermitStatus',s,{
|
|
55
|
+
startActionToken:m.startRequestId});
|
|
56
|
+
yes(history?.projectId===s.projectId&&history.taskId===s.taskId
|
|
57
|
+
&&history.startOnly===true&&history.cancelEligible===true,
|
|
58
|
+
'SCOPED_CANCEL_PREFLIGHT_NOT_IDLE');
|
|
59
|
+
const state=await context('run.terminalStatus',s,{});
|
|
60
|
+
yes(state?.projectId===s.projectId&&state.taskId===s.taskId
|
|
61
|
+
&&state.storeEpoch===m.expectedStoreEpoch
|
|
62
|
+
&&state.taskRevision===m.expectedTaskRevision
|
|
63
|
+
&&state.bindingRevision===m.expectedBindingRevision
|
|
64
|
+
&&state.runRef===s.sessionId+':'+s.taskId+':'+s.generation
|
|
65
|
+
&&state.pendingHandoff===false&&state.pendingCheckpointId===null
|
|
66
|
+
&&state.unknownEffectCount===0&&state.executionOutcome===null
|
|
67
|
+
&&state.sessionCancellation===null
|
|
68
|
+
&&state.writerApprovalCurrent===true
|
|
69
|
+
&&state.projectRulesDrift===false,
|
|
70
|
+
'SCOPED_CANCEL_PREFLIGHT_CONTEXT_CHANGED');
|
|
71
|
+
const after=readScopedOwner(s.projectId,s.taskId).session;
|
|
72
|
+
yes(after&&after.sessionId===s.sessionId
|
|
73
|
+
&&after.generation===s.generation&&after.revision===s.revision
|
|
74
|
+
&&after.phase===s.phase&&after.activeChatId===s.activeChatId,
|
|
75
|
+
'SCOPED_CANCEL_PREFLIGHT_OWNER_RACED');
|
|
76
|
+
return {status:'OK',scope:'TASK_SCOPED_CANCEL_PREFLIGHT_READ_ONLY',
|
|
77
|
+
sessionId:s.sessionId,nativeOwnerRevision:s.revision,
|
|
78
|
+
taskRevision:state.taskRevision,bindingRevision:state.bindingRevision};
|
|
79
|
+
}
|
|
19
80
|
async function handle(m){
|
|
20
81
|
yes(m&&Object.keys(m).length===11&&m.op==='task-cancel-v2'
|
|
21
82
|
&&/^[a-f0-9]{32}$/.test(m.sourceRequestId||'')
|
|
@@ -55,7 +116,8 @@ export function createScopedCancelControl(ports){
|
|
|
55
116
|
const history=await context('run.idleStartPermitStatus',s,{
|
|
56
117
|
startActionToken:m.startRequestId});
|
|
57
118
|
yes(history?.projectId===s.projectId&&history.taskId===s.taskId
|
|
58
|
-
&&history.startOnly===true
|
|
119
|
+
&&history.startOnly===true
|
|
120
|
+
&&history.cancelEligible===true,'SCOPED_CANCEL_EXECUTION_PERMIT_EXISTS');
|
|
59
121
|
const state=await context('run.terminalStatus',s,{});
|
|
60
122
|
yes(state?.projectId===s.projectId&&state.taskId===s.taskId
|
|
61
123
|
&&state.storeEpoch===m.expectedStoreEpoch
|
|
@@ -186,5 +248,5 @@ export function createScopedCancelControl(ports){
|
|
|
186
248
|
contextBindingRevision:state.bindingRevision,
|
|
187
249
|
taskRevision:state.taskRevision,readOnly:true};
|
|
188
250
|
}
|
|
189
|
-
return Object.freeze({handle,readCommitted});
|
|
251
|
+
return Object.freeze({preflight,handle,readCommitted});
|
|
190
252
|
}
|
|
@@ -1,21 +1,10 @@
|
|
|
1
1
|
// Task-scoped Native protocol. A single Owner authority remains in Native.
|
|
2
|
-
import {
|
|
3
|
-
const startSessionForRequest=requestId=>{
|
|
4
|
-
if(typeof requestId!=='string'||!/^[a-f0-9]{32}$/.test(requestId))
|
|
5
|
-
throw Object.assign(Error('Invalid Start request'),{
|
|
6
|
-
code:'SCOPED_START_REQUEST_ID_INVALID'});
|
|
7
|
-
const bytes=createHash('sha256')
|
|
8
|
-
.update('chatloop-start-v2:'+requestId).digest();
|
|
9
|
-
bytes[6]=(bytes[6]&0x0f)|0x50;
|
|
10
|
-
bytes[8]=(bytes[8]&0x3f)|0x80;
|
|
11
|
-
const hex=bytes.subarray(0,16).toString('hex');
|
|
12
|
-
return [hex.slice(0,8),hex.slice(8,12),hex.slice(12,16),
|
|
13
|
-
hex.slice(16,20),hex.slice(20)].join('-');
|
|
14
|
-
};
|
|
2
|
+
import {startSessionForRequest} from './start-session-id.mjs';
|
|
15
3
|
import {createScopedContinueControl} from './scoped-continue-v2.mjs';
|
|
16
4
|
import {createScopedHandoffControl} from './scoped-handoff-v2.mjs';
|
|
17
5
|
import {createScopedTerminalControl} from './scoped-terminal-v2.mjs';
|
|
18
6
|
import {createScopedCancelControl} from './scoped-cancel-v2.mjs';
|
|
7
|
+
import {createScopedCancelRecoveryControl} from './scoped-cancel-recovery.mjs';
|
|
19
8
|
import {createScopedRetireControl} from './scoped-retire-v2.mjs';
|
|
20
9
|
import {createScopedRecoveryControl} from './scoped-recovery-v2.mjs';
|
|
21
10
|
import {createScopedStartRecoveryControl} from './scoped-start-recovery.mjs';
|
|
@@ -29,6 +18,7 @@ export function createScopedNativeControl(ports){
|
|
|
29
18
|
const recoveryControl=createScopedRecoveryControl({
|
|
30
19
|
...ports,verifyOriginalAbort:m=>handoffControl.handle(m)});
|
|
31
20
|
const terminalControl=createScopedTerminalControl(ports),cancelControl=createScopedCancelControl(ports);
|
|
21
|
+
const cancelRecovery=createScopedCancelRecoveryControl(ports);
|
|
32
22
|
const retireControl=createScopedRetireControl(ports);
|
|
33
23
|
const startRecoveryControl=createScopedStartRecoveryControl(ports);
|
|
34
24
|
if([readRequest,readScopedOwner,writeScopedOwner,nativeContextControl]
|
|
@@ -56,10 +46,13 @@ export function createScopedNativeControl(ports){
|
|
|
56
46
|
return terminalControl.handle(m);
|
|
57
47
|
if(m?.op==='task-terminal-read-v2')
|
|
58
48
|
return terminalControl.readCommitted(m);
|
|
59
|
-
if(
|
|
60
|
-
return cancelControl.handle(m);
|
|
49
|
+
if(['task-cancel-preflight-v2','task-cancel-v2'].includes(m?.op))
|
|
50
|
+
return m.op==='task-cancel-v2'?cancelControl.handle(m):cancelControl.preflight(m);
|
|
61
51
|
if(m?.op==='task-cancel-read-v2')
|
|
62
52
|
return cancelControl.readCommitted(m);
|
|
53
|
+
if(['task-cancel-recover-v2','task-cancel-recover-read-v2'].includes(m?.op))
|
|
54
|
+
return m.op==='task-cancel-recover-v2'
|
|
55
|
+
?cancelRecovery.handle(m):cancelRecovery.readCommitted(m);
|
|
63
56
|
if(m?.op==='task-retire-v2')
|
|
64
57
|
return retireControl.handle(m);
|
|
65
58
|
if(m?.op==='context-status-v2'){
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import {createHash} from 'node:crypto';
|
|
2
|
+
export const startSessionForRequest=requestId=>{
|
|
3
|
+
if(typeof requestId!=='string'||!/^[a-f0-9]{32}$/.test(requestId))
|
|
4
|
+
throw Object.assign(Error('Invalid Start request'),{
|
|
5
|
+
code:'SCOPED_START_REQUEST_ID_INVALID'});
|
|
6
|
+
const bytes=createHash('sha256')
|
|
7
|
+
.update('chatloop-start-v2:'+requestId).digest();
|
|
8
|
+
bytes[6]=(bytes[6]&0x0f)|0x50;
|
|
9
|
+
bytes[8]=(bytes[8]&0x3f)|0x80;
|
|
10
|
+
const hex=bytes.subarray(0,16).toString('hex');
|
|
11
|
+
return [hex.slice(0,8),hex.slice(8,12),hex.slice(12,16),
|
|
12
|
+
hex.slice(16,20),hex.slice(20)].join('-');
|
|
13
|
+
};
|