@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,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
+ }