@tomflow/chatloop-tools 0.9.64 → 0.9.70

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (175) hide show
  1. package/node_modules/@tomflow/chatloop-context/README.md +296 -0
  2. package/node_modules/@tomflow/chatloop-context/bin/cli.mjs +48 -0
  3. package/node_modules/@tomflow/chatloop-context/capabilities/approval.mjs +151 -0
  4. package/node_modules/@tomflow/chatloop-context/capabilities/context.mjs +376 -0
  5. package/node_modules/@tomflow/chatloop-context/capabilities/document.mjs +290 -0
  6. package/node_modules/@tomflow/chatloop-context/capabilities/evidence.mjs +134 -0
  7. package/node_modules/@tomflow/chatloop-context/capabilities/presentation.mjs +82 -0
  8. package/node_modules/@tomflow/chatloop-context/capabilities/project.mjs +349 -0
  9. package/node_modules/@tomflow/chatloop-context/capabilities/sdk.mjs +72 -0
  10. package/node_modules/@tomflow/chatloop-context/capabilities/start.mjs +73 -0
  11. package/node_modules/@tomflow/chatloop-context/capabilities/task.mjs +507 -0
  12. package/node_modules/@tomflow/chatloop-context/capabilities.generated.mjs +11 -0
  13. package/node_modules/@tomflow/chatloop-context/docs/interfaces.md +261 -0
  14. package/node_modules/@tomflow/chatloop-context/docs/operator-and-migration.md +277 -0
  15. package/node_modules/@tomflow/chatloop-context/index.mjs +238 -0
  16. package/node_modules/@tomflow/chatloop-context/lib/api-log.mjs +86 -0
  17. package/node_modules/@tomflow/chatloop-context/lib/approval-policy.mjs +77 -0
  18. package/node_modules/@tomflow/chatloop-context/lib/capability-catalog.generated.json +2036 -0
  19. package/node_modules/@tomflow/chatloop-context/lib/capability-router.mjs +69 -0
  20. package/node_modules/@tomflow/chatloop-context/lib/cli-commands.mjs +338 -0
  21. package/node_modules/@tomflow/chatloop-context/lib/context-cli.mjs +298 -0
  22. package/node_modules/@tomflow/chatloop-context/lib/context-migration.mjs +286 -0
  23. package/node_modules/@tomflow/chatloop-context/lib/core/context.mjs +117 -0
  24. package/node_modules/@tomflow/chatloop-context/lib/core/evidence.mjs +51 -0
  25. package/node_modules/@tomflow/chatloop-context/lib/core/health.mjs +134 -0
  26. package/node_modules/@tomflow/chatloop-context/lib/core/migration.mjs +262 -0
  27. package/node_modules/@tomflow/chatloop-context/lib/core/project.mjs +246 -0
  28. package/node_modules/@tomflow/chatloop-context/lib/core/review.mjs +180 -0
  29. package/node_modules/@tomflow/chatloop-context/lib/core/run-cancel.mjs +68 -0
  30. package/node_modules/@tomflow/chatloop-context/lib/core/run-recovery.mjs +112 -0
  31. package/node_modules/@tomflow/chatloop-context/lib/core/run.mjs +367 -0
  32. package/node_modules/@tomflow/chatloop-context/lib/core/store.mjs +398 -0
  33. package/node_modules/@tomflow/chatloop-context/lib/core/task.mjs +264 -0
  34. package/node_modules/@tomflow/chatloop-context/lib/line-review.mjs +13 -0
  35. package/node_modules/@tomflow/chatloop-context/lib/methods.mjs +111 -0
  36. package/node_modules/@tomflow/chatloop-context/lib/operations.mjs +107 -0
  37. package/node_modules/@tomflow/chatloop-context/lib/orphan-operator.mjs +118 -0
  38. package/node_modules/@tomflow/chatloop-context/lib/process-inventory.mjs +20 -0
  39. package/node_modules/@tomflow/chatloop-context/lib/project-branch.mjs +316 -0
  40. package/node_modules/@tomflow/chatloop-context/lib/project-diagnostic.mjs +153 -0
  41. package/node_modules/@tomflow/chatloop-context/lib/project-guards.mjs +19 -0
  42. package/node_modules/@tomflow/chatloop-context/lib/project-store.mjs +117 -0
  43. package/node_modules/@tomflow/chatloop-context/lib/trusted-run.mjs +142 -0
  44. package/node_modules/@tomflow/chatloop-context/lib/workspace-operator.mjs +169 -0
  45. package/node_modules/@tomflow/chatloop-context/package.json +38 -0
  46. package/node_modules/@tomflow/chatloop-context/scripts/build-capabilities.mjs +114 -0
  47. package/node_modules/@tomflow/chatloop-extension/README.md +190 -0
  48. package/node_modules/@tomflow/chatloop-extension/actions.generated.mjs +13 -0
  49. package/node_modules/@tomflow/chatloop-extension/bin/cli.mjs +168 -0
  50. package/node_modules/@tomflow/chatloop-extension/bin/native-host.mjs +246 -0
  51. package/node_modules/@tomflow/chatloop-extension/capabilities/chat_dom.mjs +1 -0
  52. package/node_modules/@tomflow/chatloop-extension/capabilities/chat_screenshot.mjs +1 -0
  53. package/node_modules/@tomflow/chatloop-extension/capabilities/chat_text.mjs +1 -0
  54. package/node_modules/@tomflow/chatloop-extension/capabilities/clean.mjs +1 -0
  55. package/node_modules/@tomflow/chatloop-extension/capabilities/continue.mjs +1 -0
  56. package/node_modules/@tomflow/chatloop-extension/capabilities/entry_test.mjs +1 -0
  57. package/node_modules/@tomflow/chatloop-extension/capabilities/handoff.mjs +1 -0
  58. package/node_modules/@tomflow/chatloop-extension/capabilities/reload.mjs +1 -0
  59. package/node_modules/@tomflow/chatloop-extension/capabilities/session.mjs +1 -0
  60. package/node_modules/@tomflow/chatloop-extension/capabilities/start.mjs +1 -0
  61. package/node_modules/@tomflow/chatloop-extension/capabilities/status.mjs +1 -0
  62. package/node_modules/@tomflow/chatloop-extension/extension/actions/clean.js +123 -0
  63. package/node_modules/@tomflow/chatloop-extension/extension/actions/common.js +183 -0
  64. package/node_modules/@tomflow/chatloop-extension/extension/actions/continue.js +390 -0
  65. package/node_modules/@tomflow/chatloop-extension/extension/actions/dom.js +2 -0
  66. package/node_modules/@tomflow/chatloop-extension/extension/actions/entry_test.js +241 -0
  67. package/node_modules/@tomflow/chatloop-extension/extension/actions/handoff-scoped.js +143 -0
  68. package/node_modules/@tomflow/chatloop-extension/extension/actions/handoff.js +399 -0
  69. package/node_modules/@tomflow/chatloop-extension/extension/actions/index.js +2 -0
  70. package/node_modules/@tomflow/chatloop-extension/extension/actions/model-intent.js +60 -0
  71. package/node_modules/@tomflow/chatloop-extension/extension/actions/read-mode.js +9 -0
  72. package/node_modules/@tomflow/chatloop-extension/extension/actions/reload.js +20 -0
  73. package/node_modules/@tomflow/chatloop-extension/extension/actions/screenshot.js +77 -0
  74. package/node_modules/@tomflow/chatloop-extension/extension/actions/session.js +95 -0
  75. package/node_modules/@tomflow/chatloop-extension/extension/actions/start.js +329 -0
  76. package/node_modules/@tomflow/chatloop-extension/extension/actions/status.js +38 -0
  77. package/node_modules/@tomflow/chatloop-extension/extension/actions/text.js +2 -0
  78. package/node_modules/@tomflow/chatloop-extension/extension/background.js +33 -0
  79. package/node_modules/@tomflow/chatloop-extension/extension/clean.html +5 -0
  80. package/node_modules/@tomflow/chatloop-extension/extension/content.js +40 -0
  81. package/node_modules/@tomflow/chatloop-extension/extension/continue.html +9 -0
  82. package/node_modules/@tomflow/chatloop-extension/extension/dom.html +17 -0
  83. package/node_modules/@tomflow/chatloop-extension/extension/entry_test.html +6 -0
  84. package/node_modules/@tomflow/chatloop-extension/extension/handoff.html +9 -0
  85. package/node_modules/@tomflow/chatloop-extension/extension/loop/action-catalog.generated.js +2 -0
  86. package/node_modules/@tomflow/chatloop-extension/extension/loop/api-aspect.generated.js +275 -0
  87. package/node_modules/@tomflow/chatloop-extension/extension/loop/api-observation-queue.generated.js +84 -0
  88. package/node_modules/@tomflow/chatloop-extension/extension/loop/core.js +107 -0
  89. package/node_modules/@tomflow/chatloop-extension/extension/loop/index.js +36 -0
  90. package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-approval.js +299 -0
  91. package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-inspect.js +144 -0
  92. package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-poll.js +172 -0
  93. package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-recovery.js +109 -0
  94. package/node_modules/@tomflow/chatloop-extension/extension/loop/observer.js +211 -0
  95. package/node_modules/@tomflow/chatloop-extension/extension/loop/resource-maintenance.js +159 -0
  96. package/node_modules/@tomflow/chatloop-extension/extension/loop/resources.js +399 -0
  97. package/node_modules/@tomflow/chatloop-extension/extension/manifest.json +43 -0
  98. package/node_modules/@tomflow/chatloop-extension/extension/popup.html +25 -0
  99. package/node_modules/@tomflow/chatloop-extension/extension/popup.js +99 -0
  100. package/node_modules/@tomflow/chatloop-extension/extension/reload.html +17 -0
  101. package/node_modules/@tomflow/chatloop-extension/extension/screenshot.html +17 -0
  102. package/node_modules/@tomflow/chatloop-extension/extension/session.html +7 -0
  103. package/node_modules/@tomflow/chatloop-extension/extension/start.html +6 -0
  104. package/node_modules/@tomflow/chatloop-extension/extension/status.html +6 -0
  105. package/node_modules/@tomflow/chatloop-extension/extension/text.html +17 -0
  106. package/node_modules/@tomflow/chatloop-extension/extension/worker/action-result.js +136 -0
  107. package/node_modules/@tomflow/chatloop-extension/extension/worker/action-take.js +218 -0
  108. package/node_modules/@tomflow/chatloop-extension/extension/worker/admission.js +127 -0
  109. package/node_modules/@tomflow/chatloop-extension/extension/worker/adopt-prepare.js +296 -0
  110. package/node_modules/@tomflow/chatloop-extension/extension/worker/adopt-recovery.js +186 -0
  111. package/node_modules/@tomflow/chatloop-extension/extension/worker/adopt-result.js +126 -0
  112. package/node_modules/@tomflow/chatloop-extension/extension/worker/bootstrap.js +287 -0
  113. package/node_modules/@tomflow/chatloop-extension/extension/worker/manual.js +163 -0
  114. package/node_modules/@tomflow/chatloop-extension/extension/worker/readiness.js +345 -0
  115. package/node_modules/@tomflow/chatloop-extension/extension/worker/router.js +209 -0
  116. package/node_modules/@tomflow/chatloop-extension/extension/worker/scheduler.js +385 -0
  117. package/node_modules/@tomflow/chatloop-extension/extension/worker/session-control.js +75 -0
  118. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-action-v2.js +278 -0
  119. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-cancel-recovery-v2.js +52 -0
  120. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-cancel-v2.js +93 -0
  121. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-continue-recovery-v2.js +78 -0
  122. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-handoff-recovery-v2.js +148 -0
  123. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-handoff-v2.js +325 -0
  124. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-hold-recovery-v2.js +109 -0
  125. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-intent-dispatch-v2.js +220 -0
  126. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-intent-v2.js +106 -0
  127. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-owner-resolver.js +110 -0
  128. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-recover-v2.js +209 -0
  129. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-routes-v2.js +357 -0
  130. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-run-store.js +400 -0
  131. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-scheduler.js +78 -0
  132. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-start-v2.js +361 -0
  133. package/node_modules/@tomflow/chatloop-extension/extension/worker/task-terminal-v2.js +235 -0
  134. package/node_modules/@tomflow/chatloop-extension/extension/worker/turn-failure.js +197 -0
  135. package/node_modules/@tomflow/chatloop-extension/extension/worker/turn-finished.js +205 -0
  136. package/node_modules/@tomflow/chatloop-extension/index.mjs +78 -0
  137. package/node_modules/@tomflow/chatloop-extension/lib/action-routing.mjs +19 -0
  138. package/node_modules/@tomflow/chatloop-extension/lib/api-observation-queue.mjs +82 -0
  139. package/node_modules/@tomflow/chatloop-extension/lib/bridge.mjs +378 -0
  140. package/node_modules/@tomflow/chatloop-extension/lib/browser-launch.mjs +89 -0
  141. package/node_modules/@tomflow/chatloop-extension/lib/context-binding.mjs +45 -0
  142. package/node_modules/@tomflow/chatloop-extension/lib/context-ipc.mjs +122 -0
  143. package/node_modules/@tomflow/chatloop-extension/lib/owner-evidence.mjs +75 -0
  144. package/node_modules/@tomflow/chatloop-extension/lib/owner-kernel-lock.mjs +106 -0
  145. package/node_modules/@tomflow/chatloop-extension/lib/owner-ledger.mjs +316 -0
  146. package/node_modules/@tomflow/chatloop-extension/lib/owner-locked-write.mjs +30 -0
  147. package/node_modules/@tomflow/chatloop-extension/lib/scoped-cancel-v2.mjs +190 -0
  148. package/node_modules/@tomflow/chatloop-extension/lib/scoped-continue-v2.mjs +214 -0
  149. package/node_modules/@tomflow/chatloop-extension/lib/scoped-handoff-v2.mjs +338 -0
  150. package/node_modules/@tomflow/chatloop-extension/lib/scoped-native-control.mjs +398 -0
  151. package/node_modules/@tomflow/chatloop-extension/lib/scoped-recovery-v2.mjs +234 -0
  152. package/node_modules/@tomflow/chatloop-extension/lib/scoped-retire-v2.mjs +70 -0
  153. package/node_modules/@tomflow/chatloop-extension/lib/scoped-terminal-v2.mjs +143 -0
  154. package/node_modules/@tomflow/chatloop-extension/lib/session-file.mjs +228 -0
  155. package/node_modules/@tomflow/chatloop-extension/package.json +36 -0
  156. package/node_modules/@tomflow/chatloop-extension/scripts/build-actions.mjs +138 -0
  157. package/node_modules/@tomflow/chatloop-trace/README.md +118 -0
  158. package/node_modules/@tomflow/chatloop-trace/events/context.api.mjs +23 -0
  159. package/node_modules/@tomflow/chatloop-trace/events/typed.api.call.mjs +22 -0
  160. package/node_modules/@tomflow/chatloop-trace/events.generated.mjs +4 -0
  161. package/node_modules/@tomflow/chatloop-trace/index.mjs +59 -0
  162. package/node_modules/@tomflow/chatloop-trace/lib/api-aspect.mjs +273 -0
  163. package/node_modules/@tomflow/chatloop-trace/lib/trace-reader.mjs +308 -0
  164. package/node_modules/@tomflow/chatloop-trace/lib/trace-retention.mjs +140 -0
  165. package/node_modules/@tomflow/chatloop-trace/lib/trace.mjs +236 -0
  166. package/node_modules/@tomflow/chatloop-trace/package.json +28 -0
  167. package/node_modules/@tomflow/chatloop-trace/scripts/build-events.mjs +68 -0
  168. package/node_modules/ps-list/index.d.ts +76 -0
  169. package/node_modules/ps-list/index.js +366 -0
  170. package/node_modules/ps-list/license +9 -0
  171. package/node_modules/ps-list/package.json +44 -0
  172. package/node_modules/ps-list/readme.md +56 -0
  173. package/node_modules/ps-list/vendor/fastlist-0.3.0-x64.exe +0 -0
  174. package/node_modules/ps-list/vendor/fastlist-0.3.0-x86.exe +0 -0
  175. package/package.json +12 -4
@@ -0,0 +1,190 @@
1
+ // Original approved Task/Session idle Adopt cancellation: Native Owner CAS,
2
+ // existing Context run.fence.cancel and exact post-fence terminal verification.
3
+ // No browser send, no Task DONE, no automatic recovery from UNKNOWN.
4
+ export function createScopedCancelControl(ports){
5
+ const {readRequest,readScopedOwner,writeScopedOwner,nativeContextControl}=ports;
6
+ const fail=code=>Object.assign(Error(code),{code});
7
+ const yes=(v,code)=>{if(!v)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 proof=s=>Object.fromEntries(['projectId','taskId','sessionId',
11
+ 'generation','revision','phase','activeChatId'].map(k=>[k,s[k]]));
12
+ async function context(command,s,args){
13
+ return nativeContextControl(command,{
14
+ projectId:s.projectId,taskId:s.taskId,
15
+ sessionId:s.sessionId,generation:s.generation,
16
+ activeChatId:s.activeChatId,...args
17
+ },{ownerProofV2:proof(s)});
18
+ }
19
+ async function handle(m){
20
+ yes(m&&Object.keys(m).length===11&&m.op==='task-cancel-v2'
21
+ &&/^[a-f0-9]{32}$/.test(m.sourceRequestId||'')
22
+ &&ID.test(m.projectId||'')&&ID.test(m.taskId||'')
23
+ &&UUID.test(m.sessionId||'')
24
+ &&Number.isSafeInteger(m.generation)&&m.generation>=0
25
+ &&Number.isSafeInteger(m.expectedSessionRevision)
26
+ &&m.expectedSessionRevision>=1
27
+ &&typeof m.expectedStoreEpoch==='string'
28
+ &&m.expectedStoreEpoch.length>=10
29
+ &&Number.isSafeInteger(m.expectedTaskRevision)
30
+ &&m.expectedTaskRevision>=1
31
+ &&Number.isSafeInteger(m.expectedBindingRevision)
32
+ &&m.expectedBindingRevision>=1
33
+ &&/^[a-f0-9]{32}$/.test(m.startRequestId||''),
34
+ 'SCOPED_CANCEL_SOURCE_INVALID');
35
+ const request=readRequest(m.sourceRequestId),intent=request?.intent;
36
+ yes(request?.action==='session'&&intent?.taskFirst===true
37
+ &&intent.operation==='cancel'&&intent.confirmed===true
38
+ &&intent.projectId===m.projectId&&intent.taskId===m.taskId
39
+ &&intent.sessionId===m.sessionId
40
+ &&intent.generation===m.generation
41
+ &&intent.expectedNativeRevision===m.expectedSessionRevision
42
+ &&intent.contextStoreEpoch===m.expectedStoreEpoch
43
+ &&intent.expectedTaskRevision===m.expectedTaskRevision
44
+ &&intent.expectedBindingRevision===m.expectedBindingRevision,
45
+ 'SCOPED_CANCEL_INTENT_UNVERIFIED');
46
+ const previous=readScopedOwner(m.projectId,m.taskId),s=previous.session;
47
+ yes(s&&s.sessionId===m.sessionId&&s.generation===m.generation
48
+ &&s.revision===m.expectedSessionRevision
49
+ &&s.phase==='WAITING_REPLY'
50
+ &&typeof s.activeChatId==='string'
51
+ &&typeof s.activeChatUrl==='string',
52
+ 'SCOPED_CANCEL_OWNER_CHANGED');
53
+ // The authoritative Task must show ONLY its consumed START permit.
54
+ // Any CONTINUE/HANDOFF/other permit makes idle cancellation unsafe.
55
+ const history=await context('run.idleStartPermitStatus',s,{
56
+ startActionToken:m.startRequestId});
57
+ yes(history?.projectId===s.projectId&&history.taskId===s.taskId
58
+ &&history.startOnly===true,'SCOPED_CANCEL_EXECUTION_PERMIT_EXISTS');
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
66
+ &&state.pendingCheckpointId===null
67
+ &&state.unknownEffectCount===0
68
+ &&state.executionOutcome===null
69
+ &&state.sessionCancellation===null
70
+ &&state.writerApprovalCurrent===true
71
+ &&state.projectRulesDrift===false,
72
+ 'SCOPED_CANCEL_CONTEXT_NOT_IDLE');
73
+ const fresh=readScopedOwner(s.projectId,s.taskId);
74
+ yes(fresh.session?.revision===s.revision
75
+ &&fresh.session.phase==='WAITING_REPLY',
76
+ 'SCOPED_CANCEL_OWNER_RACED');
77
+ // Fence all sends FIRST. If Context fails, the old Owner stays BLOCKED.
78
+ const stopped=writeScopedOwner({
79
+ projectId:s.projectId,taskId:s.taskId,sessionId:s.sessionId,
80
+ generation:s.generation,phase:'BLOCKED',
81
+ activeChatId:s.activeChatId,activeChatUrl:s.activeChatUrl,
82
+ updatedAt:Date.now()},{
83
+ expectedLedgerRevision:fresh.ledgerRevision,
84
+ expectedSessionRevision:s.revision});
85
+ yes(stopped.session.phase==='BLOCKED'
86
+ &&stopped.session.revision===s.revision+1,
87
+ 'SCOPED_CANCEL_FREEZE_UNKNOWN');
88
+ const writeRequestId='cancel-'+m.sourceRequestId;
89
+ const cancelled=await context('run.fence.cancel',stopped.session,{
90
+ expectedStoreEpoch:m.expectedStoreEpoch,
91
+ expectedTaskRevision:m.expectedTaskRevision,
92
+ expectedBindingRevision:m.expectedBindingRevision,
93
+ writeRequestId,actionToken:m.sourceRequestId,
94
+ scopedStartActionToken:m.startRequestId,
95
+ reason:'USER_CANCELLED',runRef:state.runRef});
96
+ yes(cancelled?.status==='CANCEL_FENCED',
97
+ 'SCOPED_CANCEL_CONTEXT_FENCE_UNKNOWN');
98
+ const after=await context('run.terminalStatus',stopped.session,{});
99
+ const c=after?.sessionCancellation;
100
+ yes(after?.runRef===null&&after.executionOutcome===null
101
+ &&after.unknownEffectCount===0
102
+ &&c?.status==='CANCELLED_NOT_DELIVERED'
103
+ &&c.requestId===writeRequestId
104
+ &&c.sessionId===s.sessionId&&c.generation===s.generation,
105
+ 'SCOPED_CANCEL_CONTEXT_RECEIPT_UNVERIFIED');
106
+ const final=readScopedOwner(s.projectId,s.taskId);
107
+ yes(final.session?.revision===stopped.session.revision
108
+ &&final.session.phase==='BLOCKED',
109
+ 'SCOPED_CANCEL_FINAL_OWNER_RACED');
110
+ const committed=writeScopedOwner({
111
+ projectId:s.projectId,taskId:s.taskId,sessionId:s.sessionId,
112
+ generation:s.generation,phase:'CANCELLED',
113
+ activeChatId:s.activeChatId,activeChatUrl:s.activeChatUrl,
114
+ updatedAt:Date.now()},{
115
+ expectedLedgerRevision:final.ledgerRevision,
116
+ expectedSessionRevision:stopped.session.revision,
117
+ transitionKind:'CONTEXT_IDLE_CANCELLED'});
118
+ yes(committed.session.phase==='CANCELLED'
119
+ &&committed.session.revision===stopped.session.revision+1
120
+ &&after.bindingRevision===m.expectedBindingRevision+1,
121
+ 'SCOPED_CANCEL_NATIVE_UNKNOWN');
122
+ return {status:'CONFIRMED',scope:'TASK_SCOPED_CANCELLED_NOT_DELIVERED',
123
+ nativeOwnerRevision:committed.session.revision,
124
+ contextBindingRevision:after.bindingRevision,
125
+ taskRevision:after.taskRevision};
126
+ }
127
+ async function readCommitted(m){
128
+ // Recover ONLY an already-committed cancellation. The original
129
+ // signed user decision must be present in durable Context evidence;
130
+ // this branch never calls run.fence.cancel or mutates Native Owner.
131
+ yes(m&&Object.keys(m).length===9
132
+ &&m.op==='task-cancel-read-v2'
133
+ &&/^[a-f0-9]{32}$/.test(m.sourceRequestId||'')
134
+ &&ID.test(m.projectId||'')&&ID.test(m.taskId||'')
135
+ &&UUID.test(m.sessionId||'')
136
+ &&Number.isSafeInteger(m.generation)&&m.generation>=0
137
+ &&Number.isSafeInteger(m.expectedPriorSessionRevision)
138
+ &&m.expectedPriorSessionRevision>=1
139
+ &&Number.isSafeInteger(m.expectedBindingRevision)
140
+ &&m.expectedBindingRevision>=1
141
+ &&typeof m.expectedActiveChatUrl==='string'
142
+ &&/^https:\/\/chatgpt\.com\/(?:c\/[0-9a-f-]{12,}|g\/[^/]+\/c\/[0-9a-f-]{12,})$/i
143
+ .test(m.expectedActiveChatUrl),
144
+ 'SCOPED_CANCEL_READ_SCOPE_INVALID');
145
+ // A read-only reconciliation is not an unscoped status endpoint:
146
+ // require the SAME private, explicitly confirmed cancel request.
147
+ // No other Task's request ID may query this Owner's terminal proof.
148
+ const request=readRequest(m.sourceRequestId),intent=request?.intent;
149
+ yes(request?.action==='session'&&intent?.taskFirst===true
150
+ &&intent.operation==='cancel'&&intent.confirmed===true
151
+ &&intent.projectId===m.projectId&&intent.taskId===m.taskId
152
+ &&intent.sessionId===m.sessionId
153
+ &&intent.generation===m.generation
154
+ &&intent.expectedNativeRevision===m.expectedPriorSessionRevision
155
+ &&intent.expectedBindingRevision===m.expectedBindingRevision,
156
+ 'SCOPED_CANCEL_READ_INTENT_UNVERIFIED');
157
+ const s=readScopedOwner(m.projectId,m.taskId).session;
158
+ yes(s&&s.projectId===m.projectId&&s.taskId===m.taskId
159
+ &&s.sessionId===m.sessionId&&s.generation===m.generation
160
+ &&s.phase==='CANCELLED'
161
+ &&s.activeChatUrl===m.expectedActiveChatUrl
162
+ &&s.revision===m.expectedPriorSessionRevision+2,
163
+ 'SCOPED_CANCEL_READ_OWNER_NOT_TERMINAL');
164
+ const state=await context('run.terminalStatus',s,{});
165
+ const c=state?.sessionCancellation;
166
+ yes(state?.projectId===m.projectId&&state.taskId===m.taskId
167
+ &&state.runRef===null
168
+ &&state.executionOutcome===null
169
+ &&state.unknownEffectCount===0
170
+ &&state.pendingHandoff===false
171
+ &&state.pendingCheckpointId===null
172
+ &&state.bindingRevision===m.expectedBindingRevision+1
173
+ &&c?.status==='CANCELLED_NOT_DELIVERED'
174
+ &&c.requestId==='cancel-'+m.sourceRequestId
175
+ &&c.reason==='USER_CANCELLED'
176
+ &&c.sessionId===m.sessionId&&c.generation===m.generation,
177
+ 'SCOPED_CANCEL_READ_CONTEXT_NOT_COMMITTED');
178
+ const after=readScopedOwner(m.projectId,m.taskId).session;
179
+ yes(after?.sessionId===s.sessionId
180
+ &&after.generation===s.generation
181
+ &&after.phase===s.phase&&after.revision===s.revision,
182
+ 'SCOPED_CANCEL_READ_OWNER_RACED');
183
+ return {status:'CONFIRMED',
184
+ scope:'TASK_SCOPED_CANCELLED_NOT_DELIVERED',
185
+ nativeOwnerRevision:s.revision,
186
+ contextBindingRevision:state.bindingRevision,
187
+ taskRevision:state.taskRevision,readOnly:true};
188
+ }
189
+ return Object.freeze({handle,readCommitted});
190
+ }
@@ -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
+ }