@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,106 @@
1
+ // Task-first model proposals; this module NEVER creates or sends a Chat.
2
+ const scopedIntentFail=code=>Object.assign(Error(code),{code});
3
+ const scopedIntentAssert=(ok,code)=>{if(!ok)throw scopedIntentFail(code)};
4
+ async function scopedModelIntent(message,sender){
5
+ const id=message?.id,action=message?.action;
6
+ scopedIntentAssert(typeof id==='string'&&/^[a-f0-9]{32}$/.test(id)
7
+ &&['CONTINUE','HANDOFF'].includes(action)
8
+ &&Object.keys(message).length===3
9
+ &&sender?.id===chrome.runtime.id&&sender.frameId===0
10
+ &&sender?.url===chrome.runtime.getURL(action.toLowerCase()+'.html')
11
+ +'?requestId='+id
12
+ &&Number.isSafeInteger(sender.tab?.id)&&sender.tab.id>0,
13
+ 'SCOPED_MODEL_ACTION_SOURCE_INVALID');
14
+ const authorized=await chrome.runtime.sendNativeMessage(SESSION_HOST,{
15
+ op:'acquire',id,action:action.toLowerCase()});
16
+ const intent=authorized?.intent;
17
+ scopedIntentAssert(authorized?.status==='OK'&&authorized.id===id
18
+ &&authorized.action===action.toLowerCase()
19
+ &&intent?.taskFirst===true
20
+ &&typeof intent.turnToken==='string'
21
+ &&/^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i
22
+ .test(intent.turnToken),
23
+ 'SCOPED_MODEL_INTENT_UNVERIFIED');
24
+ const query=typeof intent.intentId==='string'||intent.lookup===true;
25
+ const current=await scopedTaskRuns.read();
26
+ const candidates=Object.values(current.items).filter(({run})=>
27
+ run.turnToken===intent.turnToken
28
+ ||run.lastIntentReceipt?.turnToken===intent.turnToken);
29
+ scopedIntentAssert(candidates.length===1,'SCOPED_MODEL_TURN_NOT_OWNED');
30
+ const target=candidates[0].run;
31
+ return scopedTaskScheduler.perTask(target.projectId,target.taskId,async()=>{
32
+ const snapshot=await scopedTaskRuns.get(target.projectId,target.taskId);
33
+ const run=snapshot.run;
34
+ scopedIntentAssert(run&&run.sessionId===target.sessionId
35
+ &&run.generation===target.generation
36
+ &&(run.turnToken===intent.turnToken
37
+ ||run.lastIntentReceipt?.turnToken===intent.turnToken),
38
+ 'SCOPED_MODEL_OWNER_CHANGED');
39
+ if(query){
40
+ const record=run.modelIntent?.turnToken===intent.turnToken
41
+ ?run.modelIntent:run.lastIntentReceipt;
42
+ scopedIntentAssert(record&&record.turnToken===intent.turnToken
43
+ &&record.action===action
44
+ &&(intent.lookup===true||record.id===intent.intentId),
45
+ 'SCOPED_MODEL_RECEIPT_NOT_FOUND');
46
+ // Same CLI/MCP receipt contract as V1: a query reports "phase",
47
+ // not the Worker-internal "state" field. No send/Owner side effect.
48
+ return {status:'STATUS',scope:'TASK_SCOPED_INTENT_STATUS',
49
+ intentId:record.id,action,
50
+ phase:record.state||record.status};
51
+ }
52
+ scopedIntentAssert(run.phase==='WAITING_REPLY'
53
+ &&run.turnToken===intent.turnToken,
54
+ 'SCOPED_MODEL_OWNER_NOT_READY');
55
+ scopedIntentAssert(!run.modelIntent,'SCOPED_MODEL_INTENT_ALREADY_QUEUED');
56
+ scopedIntentAssert(run.contextBindingRevision>0
57
+ &&run.contextStoreEpoch
58
+ &&Number.isSafeInteger(run.tabId)&&run.tabId>0
59
+ &&typeof run.page==='string',
60
+ 'SCOPED_MODEL_TASK_NOT_READY');
61
+ const tab=await chrome.tabs.get(run.tabId).catch(()=>null);
62
+ scopedIntentAssert(tab&&pathOf(tab.url)===run.page,
63
+ 'SCOPED_MODEL_TAB_CHANGED');
64
+ const route=await scopedTaskOwnerResolver.resolve({
65
+ id:chrome.runtime.id,frameId:0,
66
+ tab:{id:run.tabId},url:tab.url});
67
+ scopedIntentAssert(route.projectId===run.projectId
68
+ &&route.taskId===run.taskId
69
+ &&route.sessionId===run.sessionId
70
+ &&route.generation===run.generation
71
+ &&route.runRevision===snapshot.runRevision
72
+ &&route.nativeOwnerRevision===run.nativeOwnerRevision
73
+ &&route.contextStoreEpoch===run.contextStoreEpoch,
74
+ 'SCOPED_MODEL_WRITER_CHANGED');
75
+ // A model calls the tool WHILE its assistant Turn is running. The
76
+ // read-only Observer source identifies the current original User Turn;
77
+ // its assistant outcome is NOT yet known. Nothing opens or sends here.
78
+ const current=await chrome.tabs.sendMessage(run.tabId,{
79
+ type:'CHAT_LOOP_TURN_PROGRESS_V2'}).catch(()=>null);
80
+ scopedIntentAssert(current?.contract==='chat-loop.turn-observer.v1'
81
+ &&current.active===true
82
+ &&current.pendingConfirmation===false
83
+ &&current.page===run.page
84
+ &&/^user:[^\s]{8,}$/.test(current.userKey||'')
85
+ &&(!run.awaitingUserKey
86
+ ||run.awaitingUserKey===current.userKey),
87
+ 'SCOPED_MODEL_CURRENT_USER_NOT_ACTIVE');
88
+ const latest=await scopedTaskRuns.get(run.projectId,run.taskId);
89
+ scopedIntentAssert(latest.runRevision===snapshot.runRevision,
90
+ 'SCOPED_MODEL_TASK_STALE');
91
+ // Private Native source retains nextPrompt/reason. Worker saves only
92
+ // intent ID, Task token and exact original User Turn identity. The
93
+ // completed assistant Turn will decide whether to create an Action.
94
+ const committed=await scopedTaskRuns.put({...run,modelIntent:{
95
+ id,action,turnToken:intent.turnToken,requestedAt:Date.now(),
96
+ state:'QUEUED',actionTabId:null,
97
+ targetUserKey:current.userKey}},{
98
+ expectedStoreRevision:latest.storeRevision,
99
+ expectedRunRevision:latest.runRevision});
100
+ // No Action creation belongs in the model's own tool call. The exact
101
+ // completed Turn Observer (or the same Task's existing alarm) launches
102
+ // the formal Action later, after the model has finished speaking.
103
+ return {status:'QUEUED',scope:'TASK_SCOPED_INTENT_PENDING',
104
+ intentId:id,action};
105
+ });
106
+ }
@@ -0,0 +1,110 @@
1
+ // Exact Task Owner routing for future V2 Observer events.
2
+ // READ ONLY. This resolver never grants Context writes or sends a Chat turn.
3
+ (() => {
4
+ const fail=code=>Object.assign(Error(code),{code});
5
+ const assert=(condition,code)=>{if(!condition)throw fail(code)};
6
+ const CHAT=/^[0-9a-f-]{12,}$/i;
7
+ function create({store,getTab,readNative,checkWriter,pathOf,chatIdOf}){
8
+ assert(store&&typeof store.get==='function'&&
9
+ typeof store.resolveTab==='function'&&
10
+ typeof getTab==='function'&&typeof readNative==='function'&&
11
+ typeof checkWriter==='function'&&typeof pathOf==='function'&&typeof chatIdOf==='function',
12
+ 'SCOPED_RESOLVER_PORT_INVALID');
13
+ const resolve=async sender=>{
14
+ const tabId=sender?.tab?.id,url=sender?.url;
15
+ assert(Number.isSafeInteger(tabId)&&tabId>0&&
16
+ typeof url==='string'&&/^https:\/\/chatgpt\.com\//.test(url),
17
+ 'SCOPED_OBSERVER_SOURCE_INVALID');
18
+ const page=pathOf(url),chatId=chatIdOf(url);
19
+ assert(page&&CHAT.test(chatId||''),
20
+ 'SCOPED_OBSERVER_CHAT_INVALID');
21
+ const current=await getTab(tabId);
22
+ assert(current?.id===tabId&&current?.url===url,
23
+ 'SCOPED_OBSERVER_TAB_CHANGED');
24
+ const binding=await store.resolveTab(tabId,chatId);
25
+ assert(binding&&binding.projectId&&binding.taskId,
26
+ 'SCOPED_OBSERVER_TASK_NOT_FOUND');
27
+ const state=await store.get(binding.projectId,binding.taskId),run=state.run;
28
+ assert(run&&run.phase==='WAITING_REPLY'
29
+ &&run.projectId===binding.projectId
30
+ &&run.taskId===binding.taskId
31
+ &&run.sessionId===binding.sessionId
32
+ &&run.generation===binding.generation
33
+ &&run.nativeOwnerRevision===binding.nativeOwnerRevision
34
+ &&run.tabId===tabId&&run.ownerChatId===chatId
35
+ &&run.page===page,
36
+ 'SCOPED_WORKER_OWNER_DRIFT');
37
+ // A clock rollback must block only THIS Task's sender; rejecting
38
+ // this in Store-wide parsing would also make unrelated Task B fail.
39
+ assert(run.startedAt===undefined||run.startedAt<=Date.now(),
40
+ 'SCOPED_OWNER_CLOCK_UNTRUSTED');
41
+ // Do not ask Native to "find latest". It must read exactly the
42
+ // previously validated Store task/session/generation.
43
+ const native=await readNative({
44
+ projectId:binding.projectId,taskId:binding.taskId,
45
+ sessionId:binding.sessionId,generation:binding.generation});
46
+ const owner=native?.session;
47
+ assert(native?.status==='OK'
48
+ &&native.scope==='TASK_SCOPED_SNAPSHOT'
49
+ &&owner?.projectId===run.projectId
50
+ &&owner.taskId===run.taskId
51
+ &&owner.sessionId===run.sessionId
52
+ &&owner.generation===run.generation
53
+ &&owner.revision===run.nativeOwnerRevision
54
+ &&owner.phase==='WAITING_REPLY'
55
+ &&owner.activeChatId===chatId
56
+ &&owner.activeChatUrl==='https://chatgpt.com'+page,
57
+ 'SCOPED_NATIVE_OWNER_DRIFT');
58
+ // A current Chat claim alone is not a Context writer. The Native Host
59
+ // independently checks the Task's exact runRef and binding revision
60
+ // via the signed Supervisor/Context read-only contract.
61
+ const writer=await checkWriter({
62
+ projectId:run.projectId,taskId:run.taskId,
63
+ sessionId:run.sessionId,generation:run.generation,
64
+ expectedSessionRevision:owner.revision});
65
+ assert(writer?.status==='OK'
66
+ &&writer.scope==='TASK_SCOPED_WRITER_VERIFIED'
67
+ &&writer.projectId===run.projectId
68
+ &&writer.taskId===run.taskId
69
+ &&writer.sessionId===run.sessionId
70
+ &&writer.generation===run.generation
71
+ &&writer.nativeOwnerRevision===run.nativeOwnerRevision
72
+ &&Number.isSafeInteger(writer.contextBindingRevision)
73
+ &&writer.contextBindingRevision>0
74
+ &&run.contextBindingRevision===writer.contextBindingRevision
75
+ &&(run.contextStoreEpoch===undefined
76
+ ||run.contextStoreEpoch===writer.contextStoreEpoch),
77
+ 'SCOPED_CONTEXT_WRITER_UNVERIFIED');
78
+ // Context IPC can outlive a separate Worker event for THIS Task.
79
+ // Recheck its exact per-Task revision before returning the read-only
80
+ // routing candidate. Updates to another Task do not invalidate A.
81
+ const latestTab=await getTab(tabId);
82
+ assert(latestTab?.id===tabId&&latestTab.url===url,
83
+ 'SCOPED_OBSERVER_TAB_CHANGED');
84
+ const latest=await store.get(run.projectId,run.taskId);
85
+ assert(latest.runRevision===state.runRevision
86
+ &&latest.run?.sessionId===run.sessionId
87
+ &&latest.run.generation===run.generation
88
+ &&latest.run.nativeOwnerRevision===run.nativeOwnerRevision
89
+ &&latest.run.contextBindingRevision===run.contextBindingRevision
90
+ &&latest.run.ownerChatId===chatId
91
+ &&latest.run.tabId===tabId
92
+ &&latest.run.phase==='WAITING_REPLY',
93
+ 'SCOPED_WORKER_OWNER_CHANGED_DURING_VERIFY');
94
+ // Still not a permission to send: per-Turn intent/permit, Context CAS
95
+ // and DOM receipt require separate checks. A final send gate must
96
+ // independently reverify this candidate before any Action side effect.
97
+ return {projectId:run.projectId,taskId:run.taskId,
98
+ sessionId:run.sessionId,generation:run.generation,
99
+ tabId,chatId,page,nativeOwnerRevision:owner.revision,
100
+ runRevision:state.runRevision,
101
+ contextBindingRevision:writer.contextBindingRevision,
102
+ contextStoreEpoch:writer.contextStoreEpoch,
103
+ taskRevision:writer.taskRevision,
104
+ approvalRevision:writer.approvalRevision,
105
+ routeVerified:true};
106
+ };
107
+ return Object.freeze({resolve});
108
+ }
109
+ globalThis.ChatLoopTaskOwnerResolverV2=Object.freeze({create});
110
+ })();
@@ -0,0 +1,209 @@
1
+ // Stage 06: exact original Task recovery, never a Chat sender.
2
+ // The existing session.html Action is the only signed human-confirmed entry.
3
+ const scopedRecoveryResult=(run,credential)=>({
4
+ status:'OK',state:'WRITER_RESTORED_NO_SEND',
5
+ scope:'TASK_SCOPED_WRITER_RECOVERED',
6
+ projectId:run.projectId,taskId:run.taskId,sessionId:run.sessionId,
7
+ generation:run.generation,
8
+ runRef:run.sessionId+':'+run.taskId+':'+run.generation,
9
+ runCredential:credential,contextStoreEpoch:run.contextStoreEpoch,
10
+ bindingRevision:run.contextBindingRevision,turnToken:run.turnToken,
11
+ note:'原 Chat 已恢复 Context Writer,但没有发送任何消息。必须在原 Chat 的新的真实用户 Turn 中使用此凭据;旧 Handoff 消息绝不能重试。'
12
+ });
13
+ const scopedRecoveryReadNative=async(m,run,credential)=>{
14
+ const read=await chrome.runtime.sendNativeMessage(SESSION_HOST,{
15
+ ...m,op:'task-recover-read-v2'}).catch(()=>null);
16
+ if(read?.status!=='RECOVERED'||read.readOnly!==true
17
+ ||read.scope!=='TASK_SCOPED_WRITER_RECOVERED'
18
+ ||read.nativeOwnerRevision!==run.nativeOwnerRevision+2
19
+ ||read.contextBindingRevision!==run.contextBindingRevision+2)return null;
20
+ return {...read,projectId:run.projectId,taskId:run.taskId,
21
+ sessionId:run.sessionId,generation:run.generation,
22
+ contextStoreEpoch:run.contextStoreEpoch,
23
+ runRef:run.sessionId+':'+run.taskId+':'+run.generation,
24
+ newRunCredential:credential};
25
+ };
26
+ const scopedRecoveryWriterCurrent=async(run,revision,bindingRevision)=>{
27
+ const proof=await chrome.runtime.sendNativeMessage(SESSION_HOST,{
28
+ op:'context-writer-check-v2',projectId:run.projectId,
29
+ taskId:run.taskId,sessionId:run.sessionId,generation:run.generation,
30
+ expectedSessionRevision:revision
31
+ }).catch(()=>null);
32
+ return proof?.status==='OK'&&proof.scope==='TASK_SCOPED_WRITER_VERIFIED'
33
+ &&proof.projectId===run.projectId&&proof.taskId===run.taskId
34
+ &&proof.sessionId===run.sessionId&&proof.generation===run.generation
35
+ &&proof.nativeOwnerRevision===revision
36
+ &&proof.contextStoreEpoch===run.contextStoreEpoch
37
+ &&proof.contextBindingRevision===bindingRevision;
38
+ };
39
+ async function scopedTaskRecoverHandoff(message,sender){
40
+ const id=message?.requestId;
41
+ if(sender?.id!==chrome.runtime.id||sender.frameId!==0
42
+ ||!Number.isSafeInteger(sender.tab?.id)||sender.tab.id<=0
43
+ ||!/^[a-f0-9]{32}$/.test(id||'')
44
+ ||sender.url!==chrome.runtime.getURL('session.html')
45
+ +'?requestId='+id+'&operation=recover-handoff'
46
+ ||message.operation!=='recover-handoff'
47
+ ||Object.keys(message||{}).length!==3)
48
+ throw Object.assign(Error('Invalid recovery Action'),
49
+ {code:'SCOPED_RECOVERY_ACTION_SOURCE_INVALID'});
50
+ const source=await chrome.runtime.sendNativeMessage(SESSION_HOST,{
51
+ op:'acquire',id,action:'session'});
52
+ const intent=source?.intent;
53
+ if(source?.status!=='OK'||source.action!=='session'||source.id!==id
54
+ ||intent?.operation!=='recover-handoff'
55
+ ||intent.confirmed!==true||intent.taskFirst!==true
56
+ ||typeof intent.projectId!=='string'||typeof intent.taskId!=='string'
57
+ ||!/^[0-9a-f]{64}$/.test(intent.newRunCredential||''))
58
+ throw Object.assign(Error('Missing recovery authorization'),
59
+ {code:'SCOPED_RECOVERY_REQUEST_UNVERIFIED'});
60
+ return scopedTaskScheduler.perTask(intent.projectId,intent.taskId,async()=>{
61
+ const snapshot=await scopedTaskRuns.get(intent.projectId,intent.taskId);
62
+ let run=snapshot.run;
63
+ // Lost Action/Worker ACK after a complete projection: same signed
64
+ // request may read its credential, but cannot call recovery mutation.
65
+ if(run?.phase==='WAITING_REPLY'
66
+ &&run.reason==='HANDOFF_NOT_SENT_RECOVERED'
67
+ &&run.recoveryRequestId===id
68
+ &&run.lastIntentReceipt?.id===intent.originalAttemptId
69
+ &&run.sessionId===intent.sessionId&&run.generation===intent.generation
70
+ &&run.nativeOwnerRevision===intent.expectedNativeRevision+1){
71
+ if(!await scopedRecoveryWriterCurrent(run,run.nativeOwnerRevision,
72
+ run.contextBindingRevision))
73
+ throw Object.assign(Error('Recovered Writer status unknown'),
74
+ {code:'SCOPED_RECOVERY_READ_UNVERIFIED'});
75
+ return scopedRecoveryResult(run,intent.newRunCredential);
76
+ }
77
+ if(!run||run.phase!=='BLOCKED'
78
+ ||run.sessionId!==intent.sessionId||run.generation!==intent.generation
79
+ ||run.nativeOwnerRevision+1!==intent.expectedNativeRevision
80
+ ||run.modelIntent?.action!=='HANDOFF'
81
+ ||run.modelIntent.state!=='ACTION_UNVERIFIED'
82
+ ||run.preSendAbort?.id!==intent.originalAttemptId
83
+ ||run.preSendAbort.id!==run.modelIntent.id
84
+ ||run.nextAttempt?.id!==intent.originalAttemptId
85
+ ||run.nextAttempt.sendBeginAt!==null
86
+ ||run.lastIntentReceipt?.status!=='NOT_SENT'
87
+ ||run.lastIntentReceipt.id!==intent.originalAttemptId
88
+ ||run.lastIntentReceipt.turnToken!==run.turnToken
89
+ ||run.handoffFence?.attemptId!==intent.originalAttemptId
90
+ ||run.handoffFence.fencedBindingRevision!==run.contextBindingRevision+1)
91
+ throw Object.assign(Error('Unproven pre-send abort'),
92
+ {code:'SCOPED_RECOVERY_NOT_SENT_UNVERIFIED'});
93
+ const tab=await chrome.tabs.get(run.tabId).catch(()=>null);
94
+ if(!tab||!tab.url?.startsWith('https://chatgpt.com/')
95
+ ||pathOf(tab.url)!==run.page
96
+ ||chatIdOf(tab.url)!==run.ownerChatId)
97
+ throw Object.assign(Error('Original Chat changed'),
98
+ {code:'SCOPED_RECOVERY_OLD_CHAT_UNVERIFIED'});
99
+ const originalAction=run.preSendAbort.actionTabId;
100
+ const m={op:'task-recover-handoff-v2',sourceRequestId:id,
101
+ originalAttemptId:intent.originalAttemptId,
102
+ originalTurnToken:run.turnToken,
103
+ projectId:run.projectId,taskId:run.taskId,
104
+ sessionId:run.sessionId,generation:run.generation,
105
+ expectedSessionRevision:intent.expectedNativeRevision,
106
+ expectedStoreEpoch:run.contextStoreEpoch,
107
+ expectedTaskRevision:run.handoffFence.taskRevision,
108
+ expectedBindingRevision:run.handoffFence.fencedBindingRevision,
109
+ checkpointId:run.handoffFence.checkpointId};
110
+ if(run.recoveryRequestId&&run.recoveryRequestId!==id)
111
+ throw Object.assign(Error('Another recovery already attempted'),{
112
+ code:'SCOPED_RECOVERY_ALREADY_ATTEMPTED'});
113
+ let native=null;
114
+ if(run.recoveryRequestId===id){
115
+ // Never replay a possibly committed permit or Context binding. Only
116
+ // inspect the original receipt and finish its independently safe
117
+ // Native CAS if Context had already committed.
118
+ native=await scopedRecoveryReadNative(m,run,intent.newRunCredential);
119
+ if(!native)
120
+ native=await chrome.runtime.sendNativeMessage(SESSION_HOST,{
121
+ ...m,op:'task-recover-finalize-v2'}).catch(()=>null);
122
+ if(native?.status!=='RECOVERED')
123
+ native=await scopedRecoveryReadNative(m,run,intent.newRunCredential);
124
+ }else{
125
+ if(!await scopedVerifyAbortedHandoffFence(run))
126
+ throw Object.assign(Error('Original Native fence not proven'),{
127
+ code:'SCOPED_RECOVERY_ABORT_NOT_PROVEN'});
128
+ // A previous Alarm may have proved the exact original Action had
129
+ // no successor, released its hold and durably recorded settlement.
130
+ // Do not demand that the now-closed Action still exists. Without
131
+ // that receipt, repeat the strict live-Action/opener proof.
132
+ if(!run.resourceHoldSettledAt){
133
+ const released=await resources.releaseReservedChat({
134
+ ...run,token:run.turnToken},originalAction);
135
+ if(released?.released!==true)
136
+ throw Object.assign(Error('Possible successor Chat still exists'),{
137
+ code:released?.code||'SCOPED_RECOVERY_SUCCESSOR_UNVERIFIED'});
138
+ }
139
+ // Durable original request ID is the side-effect-start marker.
140
+ // No repeat permit.consume even if MV3/Native ACK disappears.
141
+ run={...run,resourceHoldSettledAt:run.resourceHoldSettledAt||Date.now(),
142
+ recoveryRequestId:id};
143
+ await scopedTaskRuns.put(run,{
144
+ expectedStoreRevision:snapshot.storeRevision,
145
+ expectedRunRevision:snapshot.runRevision});
146
+ native=await chrome.runtime.sendNativeMessage(SESSION_HOST,m)
147
+ .catch(()=>null);
148
+ if(native?.status!=='RECOVERED'){
149
+ native=await scopedRecoveryReadNative(m,run,intent.newRunCredential);
150
+ if(!native)
151
+ native=await chrome.runtime.sendNativeMessage(SESSION_HOST,{
152
+ ...m,op:'task-recover-finalize-v2'}).catch(()=>null);
153
+ if(native?.status!=='RECOVERED')
154
+ native=await scopedRecoveryReadNative(m,run,intent.newRunCredential);
155
+ }
156
+ }
157
+ if(native?.status!=='RECOVERED')
158
+ throw Object.assign(Error('Recovery commit unknown'),{
159
+ code:'SCOPED_RECOVERY_COMMIT_UNKNOWN'});
160
+ if(native.scope!=='TASK_SCOPED_WRITER_RECOVERED'
161
+ ||native.projectId!==run.projectId||native.taskId!==run.taskId
162
+ ||native.sessionId!==run.sessionId
163
+ ||native.generation!==run.generation
164
+ ||native.nativeOwnerRevision!==run.nativeOwnerRevision+2
165
+ ||native.contextBindingRevision!==run.contextBindingRevision+2
166
+ ||native.contextStoreEpoch!==run.contextStoreEpoch
167
+ ||native.newRunCredential!==intent.newRunCredential
168
+ ||native.runRef!==run.sessionId+':'+run.taskId+':'+run.generation)
169
+ throw Object.assign(Error('Recovery commit unknown'),
170
+ {code:'SCOPED_RECOVERY_COMMIT_UNKNOWN'});
171
+ if(!await scopedRecoveryWriterCurrent(run,native.nativeOwnerRevision,
172
+ native.contextBindingRevision))
173
+ throw Object.assign(Error('Recovered Context Writer drift'),{
174
+ code:'SCOPED_RECOVERY_WRITER_DRIFT'});
175
+ const sameTab=await chrome.tabs.get(run.tabId).catch(()=>null);
176
+ if(!sameTab?.url?.startsWith('https://chatgpt.com/')
177
+ ||pathOf(sameTab.url)!==run.page
178
+ ||chatIdOf(sameTab.url)!==run.ownerChatId)
179
+ throw Object.assign(Error('Recovered Chat navigated'),{
180
+ code:'SCOPED_RECOVERY_CHAT_CHANGED'});
181
+ const latest=await scopedTaskRuns.get(run.projectId,run.taskId);
182
+ if(latest.run?.phase!=='BLOCKED'
183
+ ||latest.run?.preSendAbort?.id!==intent.originalAttemptId
184
+ ||latest.run.nativeOwnerRevision!==run.nativeOwnerRevision)
185
+ throw Object.assign(Error('Recovery Worker projection unknown'),
186
+ {code:'SCOPED_RECOVERY_WORKER_DRIFT'});
187
+ const {modelIntent,nextAttempt,handoffFence,preSendAbort,
188
+ resourceHoldSettledAt,lastObservedTurn,lastFault,awaitingUserKey,
189
+ ...previous}=latest.run;
190
+ const startedAt=Date.now(),turnToken=crypto.randomUUID();
191
+ const restored={...previous,phase:'WAITING_REPLY',
192
+ reason:'HANDOFF_NOT_SENT_RECOVERED',
193
+ nativeOwnerRevision:native.nativeOwnerRevision,
194
+ contextBindingRevision:native.contextBindingRevision,
195
+ recoveryRequestId:id,
196
+ turnToken,startedAt,deadlineAt:startedAt+4*60*60*1000};
197
+ await scopedTaskRuns.put(restored,{
198
+ expectedStoreRevision:latest.storeRevision,
199
+ expectedRunRevision:latest.runRevision,
200
+ transitionKind:'CONTEXT_HANDOFF_RECOVERED'});
201
+ // Original Action cannot send now: Native revoked its PENDING permit.
202
+ // Leave uncertain tab close failures to the existing resource scanner.
203
+ const old=await chrome.tabs.get(originalAction).catch(()=>null);
204
+ if(old?.url===chrome.runtime.getURL('handoff.html')
205
+ +'?tabId='+run.tabId+'&token='+run.turnToken+'&taskFirst=1')
206
+ await chrome.tabs.remove(originalAction).catch(()=>{});
207
+ return scopedRecoveryResult(restored,intent.newRunCredential);
208
+ });
209
+ }