@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.
- package/node_modules/@tomflow/chatloop-context/README.md +296 -0
- package/node_modules/@tomflow/chatloop-context/bin/cli.mjs +48 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/approval.mjs +151 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/context.mjs +376 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/document.mjs +290 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/evidence.mjs +134 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/presentation.mjs +82 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/project.mjs +349 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/sdk.mjs +72 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/start.mjs +73 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/task.mjs +507 -0
- package/node_modules/@tomflow/chatloop-context/capabilities.generated.mjs +11 -0
- package/node_modules/@tomflow/chatloop-context/docs/interfaces.md +261 -0
- package/node_modules/@tomflow/chatloop-context/docs/operator-and-migration.md +277 -0
- package/node_modules/@tomflow/chatloop-context/index.mjs +238 -0
- package/node_modules/@tomflow/chatloop-context/lib/api-log.mjs +86 -0
- package/node_modules/@tomflow/chatloop-context/lib/approval-policy.mjs +77 -0
- package/node_modules/@tomflow/chatloop-context/lib/capability-catalog.generated.json +2036 -0
- package/node_modules/@tomflow/chatloop-context/lib/capability-router.mjs +69 -0
- package/node_modules/@tomflow/chatloop-context/lib/cli-commands.mjs +338 -0
- package/node_modules/@tomflow/chatloop-context/lib/context-cli.mjs +298 -0
- package/node_modules/@tomflow/chatloop-context/lib/context-migration.mjs +286 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/context.mjs +117 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/evidence.mjs +51 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/health.mjs +134 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/migration.mjs +262 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/project.mjs +246 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/review.mjs +180 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/run-cancel.mjs +68 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/run-recovery.mjs +112 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/run.mjs +367 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/store.mjs +398 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/task.mjs +264 -0
- package/node_modules/@tomflow/chatloop-context/lib/line-review.mjs +13 -0
- package/node_modules/@tomflow/chatloop-context/lib/methods.mjs +111 -0
- package/node_modules/@tomflow/chatloop-context/lib/operations.mjs +107 -0
- package/node_modules/@tomflow/chatloop-context/lib/orphan-operator.mjs +118 -0
- package/node_modules/@tomflow/chatloop-context/lib/process-inventory.mjs +20 -0
- package/node_modules/@tomflow/chatloop-context/lib/project-branch.mjs +316 -0
- package/node_modules/@tomflow/chatloop-context/lib/project-diagnostic.mjs +153 -0
- package/node_modules/@tomflow/chatloop-context/lib/project-guards.mjs +19 -0
- package/node_modules/@tomflow/chatloop-context/lib/project-store.mjs +117 -0
- package/node_modules/@tomflow/chatloop-context/lib/trusted-run.mjs +142 -0
- package/node_modules/@tomflow/chatloop-context/lib/workspace-operator.mjs +169 -0
- package/node_modules/@tomflow/chatloop-context/package.json +38 -0
- package/node_modules/@tomflow/chatloop-context/scripts/build-capabilities.mjs +114 -0
- package/node_modules/@tomflow/chatloop-extension/README.md +190 -0
- package/node_modules/@tomflow/chatloop-extension/actions.generated.mjs +13 -0
- package/node_modules/@tomflow/chatloop-extension/bin/cli.mjs +168 -0
- package/node_modules/@tomflow/chatloop-extension/bin/native-host.mjs +246 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/chat_dom.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/chat_screenshot.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/chat_text.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/clean.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/continue.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/entry_test.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/handoff.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/reload.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/session.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/start.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/status.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/clean.js +123 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/common.js +183 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/continue.js +390 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/dom.js +2 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/entry_test.js +241 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/handoff-scoped.js +143 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/handoff.js +399 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/index.js +2 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/model-intent.js +60 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/read-mode.js +9 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/reload.js +20 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/screenshot.js +77 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/session.js +95 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/start.js +329 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/status.js +38 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/text.js +2 -0
- package/node_modules/@tomflow/chatloop-extension/extension/background.js +33 -0
- package/node_modules/@tomflow/chatloop-extension/extension/clean.html +5 -0
- package/node_modules/@tomflow/chatloop-extension/extension/content.js +40 -0
- package/node_modules/@tomflow/chatloop-extension/extension/continue.html +9 -0
- package/node_modules/@tomflow/chatloop-extension/extension/dom.html +17 -0
- package/node_modules/@tomflow/chatloop-extension/extension/entry_test.html +6 -0
- package/node_modules/@tomflow/chatloop-extension/extension/handoff.html +9 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/action-catalog.generated.js +2 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/api-aspect.generated.js +275 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/api-observation-queue.generated.js +84 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/core.js +107 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/index.js +36 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-approval.js +299 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-inspect.js +144 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-poll.js +172 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-recovery.js +109 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/observer.js +211 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/resource-maintenance.js +159 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/resources.js +399 -0
- package/node_modules/@tomflow/chatloop-extension/extension/manifest.json +43 -0
- package/node_modules/@tomflow/chatloop-extension/extension/popup.html +25 -0
- package/node_modules/@tomflow/chatloop-extension/extension/popup.js +99 -0
- package/node_modules/@tomflow/chatloop-extension/extension/reload.html +17 -0
- package/node_modules/@tomflow/chatloop-extension/extension/screenshot.html +17 -0
- package/node_modules/@tomflow/chatloop-extension/extension/session.html +7 -0
- package/node_modules/@tomflow/chatloop-extension/extension/start.html +6 -0
- package/node_modules/@tomflow/chatloop-extension/extension/status.html +6 -0
- package/node_modules/@tomflow/chatloop-extension/extension/text.html +17 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/action-result.js +136 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/action-take.js +218 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/admission.js +127 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/adopt-prepare.js +296 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/adopt-recovery.js +186 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/adopt-result.js +126 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/bootstrap.js +287 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/manual.js +163 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/readiness.js +345 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/router.js +209 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/scheduler.js +385 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/session-control.js +75 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-action-v2.js +278 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-cancel-recovery-v2.js +52 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-cancel-v2.js +93 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-continue-recovery-v2.js +78 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-handoff-recovery-v2.js +148 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-handoff-v2.js +325 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-hold-recovery-v2.js +109 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-intent-dispatch-v2.js +220 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-intent-v2.js +106 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-owner-resolver.js +110 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-recover-v2.js +209 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-routes-v2.js +357 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-run-store.js +400 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-scheduler.js +78 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-start-v2.js +361 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/task-terminal-v2.js +235 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/turn-failure.js +197 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/turn-finished.js +205 -0
- package/node_modules/@tomflow/chatloop-extension/index.mjs +78 -0
- package/node_modules/@tomflow/chatloop-extension/lib/action-routing.mjs +19 -0
- package/node_modules/@tomflow/chatloop-extension/lib/api-observation-queue.mjs +82 -0
- package/node_modules/@tomflow/chatloop-extension/lib/bridge.mjs +378 -0
- package/node_modules/@tomflow/chatloop-extension/lib/browser-launch.mjs +89 -0
- package/node_modules/@tomflow/chatloop-extension/lib/context-binding.mjs +45 -0
- package/node_modules/@tomflow/chatloop-extension/lib/context-ipc.mjs +122 -0
- package/node_modules/@tomflow/chatloop-extension/lib/owner-evidence.mjs +75 -0
- package/node_modules/@tomflow/chatloop-extension/lib/owner-kernel-lock.mjs +106 -0
- package/node_modules/@tomflow/chatloop-extension/lib/owner-ledger.mjs +316 -0
- package/node_modules/@tomflow/chatloop-extension/lib/owner-locked-write.mjs +30 -0
- package/node_modules/@tomflow/chatloop-extension/lib/scoped-cancel-v2.mjs +190 -0
- package/node_modules/@tomflow/chatloop-extension/lib/scoped-continue-v2.mjs +214 -0
- package/node_modules/@tomflow/chatloop-extension/lib/scoped-handoff-v2.mjs +338 -0
- package/node_modules/@tomflow/chatloop-extension/lib/scoped-native-control.mjs +398 -0
- package/node_modules/@tomflow/chatloop-extension/lib/scoped-recovery-v2.mjs +234 -0
- package/node_modules/@tomflow/chatloop-extension/lib/scoped-retire-v2.mjs +70 -0
- package/node_modules/@tomflow/chatloop-extension/lib/scoped-terminal-v2.mjs +143 -0
- package/node_modules/@tomflow/chatloop-extension/lib/session-file.mjs +228 -0
- package/node_modules/@tomflow/chatloop-extension/package.json +36 -0
- package/node_modules/@tomflow/chatloop-extension/scripts/build-actions.mjs +138 -0
- package/node_modules/@tomflow/chatloop-trace/README.md +118 -0
- package/node_modules/@tomflow/chatloop-trace/events/context.api.mjs +23 -0
- package/node_modules/@tomflow/chatloop-trace/events/typed.api.call.mjs +22 -0
- package/node_modules/@tomflow/chatloop-trace/events.generated.mjs +4 -0
- package/node_modules/@tomflow/chatloop-trace/index.mjs +59 -0
- package/node_modules/@tomflow/chatloop-trace/lib/api-aspect.mjs +273 -0
- package/node_modules/@tomflow/chatloop-trace/lib/trace-reader.mjs +308 -0
- package/node_modules/@tomflow/chatloop-trace/lib/trace-retention.mjs +140 -0
- package/node_modules/@tomflow/chatloop-trace/lib/trace.mjs +236 -0
- package/node_modules/@tomflow/chatloop-trace/package.json +28 -0
- package/node_modules/@tomflow/chatloop-trace/scripts/build-events.mjs +68 -0
- package/node_modules/ps-list/index.d.ts +76 -0
- package/node_modules/ps-list/index.js +366 -0
- package/node_modules/ps-list/license +9 -0
- package/node_modules/ps-list/package.json +44 -0
- package/node_modules/ps-list/readme.md +56 -0
- package/node_modules/ps-list/vendor/fastlist-0.3.0-x64.exe +0 -0
- package/node_modules/ps-list/vendor/fastlist-0.3.0-x86.exe +0 -0
- package/package.json +12 -4
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// Native-only terminal retention. Context is still the permanent Task truth.
|
|
2
|
+
// Never retire BLOCKED/UNKNOWN or infer safety from a closed Chrome tab.
|
|
3
|
+
export function createScopedRetireControl(ports){
|
|
4
|
+
const {readScopedOwner,retireScopedOwner,nativeContextControl}=ports;
|
|
5
|
+
const fail=code=>Object.assign(Error(code),{code});
|
|
6
|
+
const assert=(condition,code)=>{if(!condition)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
|
+
async function handle(m){
|
|
10
|
+
assert(m&&Object.keys(m).length===6
|
|
11
|
+
&&typeof retireScopedOwner==='function'
|
|
12
|
+
&&m.op==='task-retire-v2'
|
|
13
|
+
&&ID.test(m.projectId||'')&&ID.test(m.taskId||'')
|
|
14
|
+
&&UUID.test(m.sessionId||'')
|
|
15
|
+
&&Number.isSafeInteger(m.generation)&&m.generation>=0
|
|
16
|
+
&&Number.isSafeInteger(m.expectedSessionRevision)
|
|
17
|
+
&&m.expectedSessionRevision>=1,'SCOPED_RETIRE_SOURCE_INVALID');
|
|
18
|
+
const observed=readScopedOwner(m.projectId,m.taskId).session;
|
|
19
|
+
if(!observed){
|
|
20
|
+
// The original Context-approved Native deletion may have committed
|
|
21
|
+
// before its one-shot ACK was delivered. Read ONLY the exact Native
|
|
22
|
+
// retirement receipt; never infer a deletion from an absent Owner.
|
|
23
|
+
const recorded=retireScopedOwner(m.projectId,m.taskId,{
|
|
24
|
+
expectedSessionId:m.sessionId,
|
|
25
|
+
expectedGeneration:m.generation,
|
|
26
|
+
expectedSessionRevision:m.expectedSessionRevision});
|
|
27
|
+
assert(recorded?.status==='RETIRED'&&recorded.alreadyRetired===true,
|
|
28
|
+
'SCOPED_RETIRE_TOMBSTONE_UNVERIFIED');
|
|
29
|
+
return {status:'OK',scope:'TASK_SCOPED_RETIRED',
|
|
30
|
+
projectId:m.projectId,taskId:m.taskId,sessionId:m.sessionId,
|
|
31
|
+
generation:m.generation,nativeOwnerRevision:m.expectedSessionRevision,
|
|
32
|
+
terminal:recorded.terminal,alreadyRetired:true};
|
|
33
|
+
}
|
|
34
|
+
assert(observed&&observed.sessionId===m.sessionId
|
|
35
|
+
&&observed.generation===m.generation
|
|
36
|
+
&&observed.revision===m.expectedSessionRevision
|
|
37
|
+
&&['DONE','CANCELLED'].includes(observed.phase)
|
|
38
|
+
&&Date.now()-observed.updatedAt>=24*60*60*1000,
|
|
39
|
+
'SCOPED_RETIRE_NOT_READY');
|
|
40
|
+
const proof=Object.fromEntries(['projectId','taskId','sessionId',
|
|
41
|
+
'generation','revision','phase','activeChatId']
|
|
42
|
+
.map(k=>[k,observed[k]]));
|
|
43
|
+
const state=await nativeContextControl('run.terminalStatus',{
|
|
44
|
+
projectId:observed.projectId,taskId:observed.taskId,
|
|
45
|
+
sessionId:observed.sessionId,generation:observed.generation,
|
|
46
|
+
activeChatId:observed.activeChatId},{ownerProofV2:proof});
|
|
47
|
+
assert(state?.projectId===observed.projectId
|
|
48
|
+
&&state.taskId===observed.taskId&&state.runRef===null
|
|
49
|
+
&&state.pendingHandoff===false
|
|
50
|
+
&&state.pendingCheckpointId===null
|
|
51
|
+
&&state.unknownEffectCount===0
|
|
52
|
+
&&(observed.phase!=='DONE'
|
|
53
|
+
||state.executionOutcome==='DONE'
|
|
54
|
+
&&typeof state.deliveryRef==='string'&&state.deliveryRef)
|
|
55
|
+
&&(observed.phase!=='CANCELLED'
|
|
56
|
+
||state.sessionCancellation?.status==='CANCELLED_NOT_DELIVERED'
|
|
57
|
+
&&state.sessionCancellation.sessionId===observed.sessionId
|
|
58
|
+
&&state.sessionCancellation.generation===observed.generation),
|
|
59
|
+
'SCOPED_RETIRE_CONTEXT_UNVERIFIED');
|
|
60
|
+
const retired=retireScopedOwner(m.projectId,m.taskId,{
|
|
61
|
+
expectedSessionId:observed.sessionId,
|
|
62
|
+
expectedGeneration:observed.generation,
|
|
63
|
+
expectedSessionRevision:observed.revision});
|
|
64
|
+
return {status:'OK',scope:'TASK_SCOPED_RETIRED',
|
|
65
|
+
projectId:observed.projectId,taskId:observed.taskId,
|
|
66
|
+
sessionId:observed.sessionId,generation:observed.generation,
|
|
67
|
+
nativeOwnerRevision:observed.revision,terminal:retired.terminal};
|
|
68
|
+
}
|
|
69
|
+
return Object.freeze({handle});
|
|
70
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
// Model terminal marker -> independent Context terminal proof -> Native CAS.
|
|
2
|
+
// Task-specific, no Chat send, no Context identity from the browser message.
|
|
3
|
+
export function createScopedTerminalControl(ports){
|
|
4
|
+
const {readScopedOwner,writeScopedOwner,nativeContextControl}=ports;
|
|
5
|
+
const fail=code=>Object.assign(Error(code),{code});
|
|
6
|
+
const require=(yes,code)=>{if(!yes)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 proof=s=>Object.fromEntries(['projectId','taskId','sessionId',
|
|
10
|
+
'generation','revision','phase','activeChatId'].map(k=>[k,s[k]]));
|
|
11
|
+
async function readCommitted(m){
|
|
12
|
+
// A Native terminal CAS may have committed before the Worker saw its
|
|
13
|
+
// response. Reconcile ONLY the same Session/Chat and authoritative
|
|
14
|
+
// Context outcome; this function never writes to Context or Ledger.
|
|
15
|
+
require(m&&Object.keys(m).length===8
|
|
16
|
+
&&m.op==='task-terminal-read-v2'
|
|
17
|
+
&&ID.test(m.projectId||'')&&ID.test(m.taskId||'')
|
|
18
|
+
&&UUID.test(m.sessionId||'')
|
|
19
|
+
&&Number.isSafeInteger(m.generation)&&m.generation>=0
|
|
20
|
+
&&Number.isSafeInteger(m.expectedPriorSessionRevision)
|
|
21
|
+
&&m.expectedPriorSessionRevision>=1
|
|
22
|
+
&&['DONE','BLOCKED'].includes(m.terminal)
|
|
23
|
+
&&typeof m.activeChatUrl==='string'
|
|
24
|
+
&&/^https:\/\/chatgpt\.com\/(?:c\/[0-9a-f-]{12,}|g\/[^/]+\/c\/[0-9a-f-]{12,})$/i
|
|
25
|
+
.test(m.activeChatUrl),
|
|
26
|
+
'SCOPED_TERMINAL_READ_SCOPE_INVALID');
|
|
27
|
+
const scoped=readScopedOwner(m.projectId,m.taskId),s=scoped.session;
|
|
28
|
+
require(s&&s.sessionId===m.sessionId
|
|
29
|
+
&&s.generation===m.generation
|
|
30
|
+
&&s.revision===m.expectedPriorSessionRevision+1
|
|
31
|
+
&&s.phase===m.terminal
|
|
32
|
+
&&s.activeChatUrl===m.activeChatUrl,
|
|
33
|
+
'SCOPED_TERMINAL_READ_OWNER_CHANGED');
|
|
34
|
+
const state=await nativeContextControl('run.terminalStatus',{
|
|
35
|
+
projectId:s.projectId,taskId:s.taskId,sessionId:s.sessionId,
|
|
36
|
+
generation:s.generation,activeChatId:s.activeChatId},
|
|
37
|
+
{ownerProofV2:proof(s)});
|
|
38
|
+
require(state?.projectId===s.projectId&&state.taskId===s.taskId
|
|
39
|
+
&&state.pendingHandoff===false
|
|
40
|
+
&&state.pendingCheckpointId===null
|
|
41
|
+
&&state.unknownEffectCount===0
|
|
42
|
+
&&state.sessionCancellation===null
|
|
43
|
+
&&(m.terminal==='DONE'
|
|
44
|
+
?state.runRef===null&&state.executionOutcome==='DONE'
|
|
45
|
+
&&typeof state.deliveryRef==='string'&&state.deliveryRef
|
|
46
|
+
:state.runRef===s.sessionId+':'+s.taskId+':'+s.generation
|
|
47
|
+
&&state.executionOutcome!=='DONE'),
|
|
48
|
+
'SCOPED_TERMINAL_READ_CONTEXT_UNVERIFIED');
|
|
49
|
+
const after=readScopedOwner(m.projectId,m.taskId).session;
|
|
50
|
+
require(after?.revision===s.revision&&after.phase===s.phase
|
|
51
|
+
&&after.sessionId===s.sessionId
|
|
52
|
+
&&after.activeChatUrl===s.activeChatUrl,
|
|
53
|
+
'SCOPED_TERMINAL_READ_OWNER_RACED');
|
|
54
|
+
return {status:'CONFIRMED',scope:'TASK_SCOPED_TERMINAL',
|
|
55
|
+
terminal:s.phase,nativeOwnerRevision:s.revision,
|
|
56
|
+
readOnly:true};
|
|
57
|
+
}
|
|
58
|
+
async function handle(m){
|
|
59
|
+
require(m&&Object.keys(m).length===11
|
|
60
|
+
&&m.op==='task-terminal-v2'
|
|
61
|
+
&&ID.test(m.projectId||'')&&ID.test(m.taskId||'')
|
|
62
|
+
&&UUID.test(m.sessionId||'')
|
|
63
|
+
&&UUID.test(m.turnToken||'')
|
|
64
|
+
&&Number.isSafeInteger(m.generation)&&m.generation>=0
|
|
65
|
+
&&Number.isSafeInteger(m.expectedSessionRevision)
|
|
66
|
+
&&m.expectedSessionRevision>=1
|
|
67
|
+
&&['DONE','BLOCKED'].includes(m.terminal)
|
|
68
|
+
&&/^user:[^\s]{8,}$/.test(m.sourceUserKey||'')
|
|
69
|
+
&&/^assistant:[^\s]{8,}$/.test(m.sourceAssistantKey||'')
|
|
70
|
+
&&typeof m.page==='string'
|
|
71
|
+
&&/^\/(?:c\/[0-9a-f-]{12,}|g\/[^/]+\/c\/[0-9a-f-]{12,})$/i.test(m.page),
|
|
72
|
+
'SCOPED_TERMINAL_SOURCE_INVALID');
|
|
73
|
+
const before=readScopedOwner(m.projectId,m.taskId),s=before.session;
|
|
74
|
+
require(s&&s.sessionId===m.sessionId
|
|
75
|
+
&&s.generation===m.generation
|
|
76
|
+
&&s.revision===m.expectedSessionRevision
|
|
77
|
+
&&s.phase==='WAITING_REPLY'
|
|
78
|
+
&&s.activeChatUrl==='https://chatgpt.com'+m.page,
|
|
79
|
+
'SCOPED_TERMINAL_OWNER_CHANGED');
|
|
80
|
+
const context=async(command,args)=>nativeContextControl(command,{
|
|
81
|
+
projectId:s.projectId,taskId:s.taskId,sessionId:s.sessionId,
|
|
82
|
+
generation:s.generation,activeChatId:s.activeChatId,...args},
|
|
83
|
+
{ownerProofV2:proof(s)});
|
|
84
|
+
const state=await context('run.terminalStatus',{});
|
|
85
|
+
require(state?.projectId===s.projectId&&state.taskId===s.taskId
|
|
86
|
+
&&state.pendingHandoff===false
|
|
87
|
+
&&state.pendingCheckpointId===null
|
|
88
|
+
&&state.unknownEffectCount===0
|
|
89
|
+
&&state.sessionCancellation===null
|
|
90
|
+
&&state.projectRulesDrift===false
|
|
91
|
+
&&Number.isSafeInteger(state.bindingRevision)
|
|
92
|
+
&&state.bindingRevision>0,
|
|
93
|
+
'SCOPED_TERMINAL_CONTEXT_UNVERIFIED');
|
|
94
|
+
if(m.terminal==='DONE'){
|
|
95
|
+
require(state.executionOutcome==='DONE'
|
|
96
|
+
&&typeof state.deliveryRef==='string'&&state.deliveryRef
|
|
97
|
+
&&(state.runRef===null
|
|
98
|
+
||state.runRef===s.sessionId+':'+s.taskId+':'+s.generation),
|
|
99
|
+
'SCOPED_DONE_DELIVERY_NOT_COMMITTED');
|
|
100
|
+
if(state.runRef!==null){
|
|
101
|
+
const fenced=await context('run.fence.terminal',{
|
|
102
|
+
expectedStoreEpoch:state.storeEpoch,
|
|
103
|
+
expectedTaskRevision:state.taskRevision,
|
|
104
|
+
expectedBindingRevision:state.bindingRevision,
|
|
105
|
+
writeRequestId:'task-terminal:'+s.sessionId+':'+s.generation,
|
|
106
|
+
runRef:state.runRef});
|
|
107
|
+
require(fenced?.status==='TERMINAL_FENCED',
|
|
108
|
+
'SCOPED_TERMINAL_FENCE_UNKNOWN');
|
|
109
|
+
}
|
|
110
|
+
const after=await context('run.terminalStatus',{});
|
|
111
|
+
require(after?.runRef===null
|
|
112
|
+
&&after.executionOutcome==='DONE'
|
|
113
|
+
&&after.deliveryRef===state.deliveryRef
|
|
114
|
+
&&after.pendingHandoff===false
|
|
115
|
+
&&after.unknownEffectCount===0,
|
|
116
|
+
'SCOPED_TERMINAL_CONTEXT_FENCE_UNVERIFIED');
|
|
117
|
+
}else{
|
|
118
|
+
require(state.executionOutcome!== 'DONE'
|
|
119
|
+
&&state.runRef===s.sessionId+':'+s.taskId+':'+s.generation,
|
|
120
|
+
'SCOPED_BLOCKED_RUN_UNVERIFIED');
|
|
121
|
+
}
|
|
122
|
+
const fresh=readScopedOwner(s.projectId,s.taskId);
|
|
123
|
+
require(fresh.session?.sessionId===s.sessionId
|
|
124
|
+
&&fresh.session.generation===s.generation
|
|
125
|
+
&&fresh.session.revision===s.revision
|
|
126
|
+
&&fresh.session.phase==='WAITING_REPLY'
|
|
127
|
+
&&fresh.session.activeChatId===s.activeChatId,
|
|
128
|
+
'SCOPED_TERMINAL_OWNER_RACED');
|
|
129
|
+
const committed=writeScopedOwner({
|
|
130
|
+
projectId:s.projectId,taskId:s.taskId,sessionId:s.sessionId,
|
|
131
|
+
generation:s.generation,phase:m.terminal,
|
|
132
|
+
activeChatId:s.activeChatId,activeChatUrl:s.activeChatUrl,
|
|
133
|
+
updatedAt:Date.now()},{
|
|
134
|
+
expectedLedgerRevision:fresh.ledgerRevision,
|
|
135
|
+
expectedSessionRevision:s.revision});
|
|
136
|
+
require(committed.session.phase===m.terminal
|
|
137
|
+
&&committed.session.revision===s.revision+1,
|
|
138
|
+
'SCOPED_TERMINAL_NATIVE_UNKNOWN');
|
|
139
|
+
return {status:'CONFIRMED',scope:'TASK_SCOPED_TERMINAL',
|
|
140
|
+
terminal:m.terminal,nativeOwnerRevision:committed.session.revision};
|
|
141
|
+
}
|
|
142
|
+
return Object.freeze({handle,readCommitted});
|
|
143
|
+
}
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
// Single-Mac durable session pointer. Extension Worker is the only admission/sending authority.
|
|
2
|
+
// This file is a checked, minimal projection, not a second executor or task engine.
|
|
3
|
+
import {openSync,closeSync,readFileSync,writeFileSync,renameSync,
|
|
4
|
+
unlinkSync,lstatSync,mkdirSync} from 'node:fs';
|
|
5
|
+
import {homedir} from 'node:os';
|
|
6
|
+
import {join,dirname} from 'node:path';
|
|
7
|
+
import {randomUUID} from 'node:crypto';
|
|
8
|
+
import {hasActiveLedgerOwner,ownerLedgerLockPath} from './owner-ledger.mjs';
|
|
9
|
+
import {withOwnerKernelLock} from './owner-kernel-lock.mjs';
|
|
10
|
+
|
|
11
|
+
const ID=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
12
|
+
const CHAT=/^[0-9a-f-]{12,}$/i;
|
|
13
|
+
const PROJECT=/^[a-zA-Z0-9][a-zA-Z0-9._-]{0,127}$/;
|
|
14
|
+
const PHASE=new Set(['STARTING','RESERVED','PENDING','WAITING_REPLY','DONE','BLOCKED','CANCELLED']);
|
|
15
|
+
const fail=code=>Object.assign(Error(code),{code});
|
|
16
|
+
export const sessionPath=()=>join(homedir(),'.chat-loop','session','active-session.json');
|
|
17
|
+
// An absent path differs from a dangling symlink: existsSync follows the
|
|
18
|
+
// link and would otherwise silently erase the Native Owner projection.
|
|
19
|
+
const entryOrMissing=path=>{
|
|
20
|
+
try{return lstatSync(path)}
|
|
21
|
+
catch(error){if(error.code==='ENOENT')return null;throw error}
|
|
22
|
+
};
|
|
23
|
+
const verifyPrivateDirectory=dir=>{
|
|
24
|
+
const st=entryOrMissing(dir);
|
|
25
|
+
if(st&&(!st.isDirectory()||st.isSymbolicLink()
|
|
26
|
+
||(process.getuid&&st.uid!==process.getuid())
|
|
27
|
+
||(process.platform!=='win32'&&(st.mode&0o077))))
|
|
28
|
+
throw fail('SESSION_DIRECTORY_UNTRUSTED');
|
|
29
|
+
return Boolean(st);
|
|
30
|
+
};
|
|
31
|
+
const ensurePrivate=dir=>{
|
|
32
|
+
if(!verifyPrivateDirectory(dir))mkdirSync(dir,{recursive:true,mode:0o700});
|
|
33
|
+
if(!verifyPrivateDirectory(dir))throw fail('SESSION_DIRECTORY_UNTRUSTED');
|
|
34
|
+
};
|
|
35
|
+
const validate=r=>{
|
|
36
|
+
if(!r||typeof r!=='object'||Array.isArray(r)||
|
|
37
|
+
!ID.test(r.sessionId||'')||!PROJECT.test(r.projectId||'')||
|
|
38
|
+
(r.taskId!==undefined && !PROJECT.test(r.taskId))||
|
|
39
|
+
!PHASE.has(r.phase)||!Number.isSafeInteger(r.generation)||r.generation<0||
|
|
40
|
+
!Number.isSafeInteger(r.updatedAt)||r.updatedAt<=0)
|
|
41
|
+
throw fail('SESSION_INVALID');
|
|
42
|
+
if(r.activeChatId!==null&&(typeof r.activeChatId!=='string'||!CHAT.test(r.activeChatId)))
|
|
43
|
+
throw fail('SESSION_CHAT_INVALID');
|
|
44
|
+
if(r.activeChatUrl!==null&&(typeof r.activeChatUrl!=='string'
|
|
45
|
+
|| !/^https:\/\/chatgpt\.com\/(?:c\/[0-9a-f-]{12,}|g\/[^/]+\/c\/[0-9a-f-]{12,})$/i.test(r.activeChatUrl)))
|
|
46
|
+
throw fail('SESSION_CHAT_INVALID');
|
|
47
|
+
if((r.activeChatId===null)!==(r.activeChatUrl===null))
|
|
48
|
+
throw fail('SESSION_CHAT_INVALID');
|
|
49
|
+
if(r.activeChatUrl!==null&&!r.activeChatUrl.endsWith('/'+r.activeChatId))
|
|
50
|
+
throw fail('SESSION_CHAT_INVALID');
|
|
51
|
+
if(r.activeChatId===null&&r.phase==='WAITING_REPLY')throw fail('SESSION_CHAT_INVALID');
|
|
52
|
+
const keys=['sessionId','projectId','phase','generation','activeChatId','activeChatUrl','updatedAt','taskId'];
|
|
53
|
+
if(Object.keys(r).some(k=>!keys.includes(k)))throw fail('SESSION_EXTRA_FIELDS');
|
|
54
|
+
return Object.fromEntries(keys.filter(k=>Object.hasOwn(r,k)).map(k=>[k,r[k]]));
|
|
55
|
+
};
|
|
56
|
+
export function readSession(){
|
|
57
|
+
const file=sessionPath();
|
|
58
|
+
if(!verifyPrivateDirectory(dirname(file)))return null;
|
|
59
|
+
// Preserve the original V1 directory integrity error before examining V2.
|
|
60
|
+
// A V1 status must not misreport IDLE while V2 owns another Chat.
|
|
61
|
+
if(hasActiveLedgerOwner())throw fail('OWNER_LEDGER_ACTIVE_USE_SCOPED_QUERY');
|
|
62
|
+
const st=entryOrMissing(file);
|
|
63
|
+
if(!st)return null;
|
|
64
|
+
if(!st.isFile()||st.isSymbolicLink()||(process.getuid&&st.uid!==process.getuid())
|
|
65
|
+
||(process.platform!=='win32'&&(st.mode&0o077)))throw fail('SESSION_FILE_UNTRUSTED');
|
|
66
|
+
try{return validate(JSON.parse(readFileSync(file,'utf8')));}
|
|
67
|
+
catch(e){if(e.code)throw e;throw fail('SESSION_FILE_CORRUPT');}
|
|
68
|
+
}
|
|
69
|
+
function completeExistingLegacySession(args){
|
|
70
|
+
// During staged upgrade, an already-active V1 Session may still need
|
|
71
|
+
// its original terminal CAS before a permanent kernel marker can exist.
|
|
72
|
+
// Reuse both legacy O_EXCL locks, but NEVER admit a different V1 Session
|
|
73
|
+
// or allow this fallback once the new persistent marker has been written.
|
|
74
|
+
const file=sessionPath(),shared=ownerLedgerLockPath(),own=file+'.lock';
|
|
75
|
+
ensurePrivate(dirname(file));
|
|
76
|
+
let sharedFd,ownFd;
|
|
77
|
+
try{
|
|
78
|
+
try{sharedFd=openSync(shared,'wx',0o600)}
|
|
79
|
+
catch{throw fail('SESSION_CONCURRENT_UPDATE')}
|
|
80
|
+
try{ownFd=openSync(own,'wx',0o600)}
|
|
81
|
+
catch{throw fail('SESSION_CONCURRENT_UPDATE')}
|
|
82
|
+
const old=readSession(),next=validate(args[0]);
|
|
83
|
+
if(!old||['DONE','CANCELLED'].includes(old.phase)
|
|
84
|
+
||old.sessionId!==next.sessionId||old.projectId!==next.projectId
|
|
85
|
+
||old.taskId!==next.taskId)
|
|
86
|
+
throw fail('OWNER_LOCK_CUTOVER_LEGACY_REPLACEMENT_DENIED');
|
|
87
|
+
return writeSessionLocked(...args);
|
|
88
|
+
}finally{
|
|
89
|
+
if(ownFd!==undefined){closeSync(ownFd);unlinkSync(own)}
|
|
90
|
+
if(sharedFd!==undefined){closeSync(sharedFd);unlinkSync(shared)}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
export function writeSession(...args){
|
|
94
|
+
try{return withOwnerKernelLock('legacy-write',args)}
|
|
95
|
+
catch(e){
|
|
96
|
+
if(e?.code==='OWNER_LOCK_CUTOVER_ACTIVE')
|
|
97
|
+
return completeExistingLegacySession(args);
|
|
98
|
+
// Preserve the existing V1 lock-contention API; UNKNOWN remains
|
|
99
|
+
// distinct because its one-shot child may already have committed.
|
|
100
|
+
if(e?.code==='OWNER_LEDGER_LOCKED')throw fail('SESSION_CONCURRENT_UPDATE');
|
|
101
|
+
throw e;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
export function writeSessionLocked(input,replaceBlockedSessionId=null,resumeBlockedSessionId=null,resumeBlockedUpdatedAt=null,sameTaskPrePermitUpdatedAt=null,cancelBlockedUpdatedAt=null,
|
|
105
|
+
cancelFreezeExpectedAt=null){
|
|
106
|
+
const next=validate(input),file=sessionPath(),dir=dirname(file);
|
|
107
|
+
ensurePrivate(dir);
|
|
108
|
+
// The one-shot Native helper holds the same crash-released kernel lock
|
|
109
|
+
// as V2 for the entire legacy CAS; old O_EXCL writers cannot pass
|
|
110
|
+
// the persistent migration marker.
|
|
111
|
+
{
|
|
112
|
+
if(hasActiveLedgerOwner())throw fail('OWNER_LEDGER_ACTIVE');
|
|
113
|
+
const old=readSession();
|
|
114
|
+
// A canceled idle Adopt is terminal only after Context writer fencing.
|
|
115
|
+
// The Native Host independently verifies the authoritative Context
|
|
116
|
+
// cancellation receipt before passing this exact timestamp CAS.
|
|
117
|
+
const cancelCas=Number.isSafeInteger(cancelBlockedUpdatedAt)
|
|
118
|
+
&&cancelBlockedUpdatedAt>0&&old?.updatedAt===cancelBlockedUpdatedAt
|
|
119
|
+
&&old.phase==='BLOCKED'&&next.phase==='CANCELLED'
|
|
120
|
+
&&old.sessionId===next.sessionId&&old.projectId===next.projectId
|
|
121
|
+
&&old.taskId===next.taskId&&old.generation===next.generation
|
|
122
|
+
&&old.activeChatId===next.activeChatId
|
|
123
|
+
&&old.activeChatUrl===next.activeChatUrl;
|
|
124
|
+
if(cancelBlockedUpdatedAt!==null&&!cancelCas)
|
|
125
|
+
throw fail('SESSION_CANCEL_CAS_MISMATCH');
|
|
126
|
+
const freezeCas=Number.isSafeInteger(cancelFreezeExpectedAt)
|
|
127
|
+
&&cancelFreezeExpectedAt>0&&old?.updatedAt===cancelFreezeExpectedAt
|
|
128
|
+
&&old.phase==='WAITING_REPLY'&&next.phase==='BLOCKED'
|
|
129
|
+
&&old.sessionId===next.sessionId&&old.projectId===next.projectId
|
|
130
|
+
&&old.taskId===next.taskId&&old.generation===next.generation
|
|
131
|
+
&&old.activeChatId===next.activeChatId
|
|
132
|
+
&&old.activeChatUrl===next.activeChatUrl;
|
|
133
|
+
if(cancelFreezeExpectedAt!==null&&!freezeCas)
|
|
134
|
+
throw fail('SESSION_CANCEL_FREEZE_CAS_MISMATCH');
|
|
135
|
+
if(resumeBlockedSessionId){
|
|
136
|
+
// A BLOCKED session is terminal except for an exact-same-owner,
|
|
137
|
+
// same-Chat, same-generation, one-shot recovery CAS. No new Chat,
|
|
138
|
+
// task, project, approval or Handoff may be smuggled into recovery.
|
|
139
|
+
if(replaceBlockedSessionId || !old || old.phase!=='BLOCKED'
|
|
140
|
+
||!Number.isSafeInteger(resumeBlockedUpdatedAt)
|
|
141
|
+
||resumeBlockedUpdatedAt<=0
|
|
142
|
+
||old.updatedAt!==resumeBlockedUpdatedAt
|
|
143
|
+
||old.sessionId!==resumeBlockedSessionId
|
|
144
|
+
||next.sessionId!==old.sessionId
|
|
145
|
+
||next.projectId!==old.projectId
|
|
146
|
+
||next.taskId!==old.taskId
|
|
147
|
+
||next.generation!==old.generation
|
|
148
|
+
||next.activeChatId!==old.activeChatId
|
|
149
|
+
||next.activeChatUrl!==old.activeChatUrl
|
|
150
|
+
||next.phase!=='RESERVED')
|
|
151
|
+
throw fail('SESSION_RECOVERY_MISMATCH');
|
|
152
|
+
}
|
|
153
|
+
if(replaceBlockedSessionId && (!old||old.sessionId!==next.sessionId
|
|
154
|
+
&& (old.phase!=='BLOCKED'||old.sessionId!==replaceBlockedSessionId)))
|
|
155
|
+
throw fail('SESSION_REPLACEMENT_MISMATCH');
|
|
156
|
+
// The Worker alone identifies the pre-permit failure. Native requires
|
|
157
|
+
// an independent one-shot disk CAS before allowing the SAME scoped Task
|
|
158
|
+
// to replace its unsent BLOCKED session; other BLOCKED phases stay locked.
|
|
159
|
+
const prePermitCas=Number.isSafeInteger(sameTaskPrePermitUpdatedAt)
|
|
160
|
+
&&sameTaskPrePermitUpdatedAt>0&&old?.updatedAt===sameTaskPrePermitUpdatedAt
|
|
161
|
+
&&old.phase==='BLOCKED'&&old.sessionId===replaceBlockedSessionId
|
|
162
|
+
&&old.projectId===next.projectId&&typeof old.taskId==='string'
|
|
163
|
+
&&old.taskId===next.taskId&&next.sessionId!==old.sessionId
|
|
164
|
+
&&old.generation===0&&next.generation===0&&next.phase==='STARTING'
|
|
165
|
+
&&old.activeChatId===null&&old.activeChatUrl===null
|
|
166
|
+
&&next.activeChatId===null&&next.activeChatUrl===null;
|
|
167
|
+
if(sameTaskPrePermitUpdatedAt!==null&&!prePermitCas)
|
|
168
|
+
throw fail('SESSION_PRE_PERMIT_CAS_MISMATCH');
|
|
169
|
+
if(old&&old.sessionId!==next.sessionId
|
|
170
|
+
&&!['DONE','CANCELLED'].includes(old.phase)
|
|
171
|
+
&& !(old.phase==='BLOCKED' && next.phase==='STARTING'
|
|
172
|
+
&& replaceBlockedSessionId===old.sessionId
|
|
173
|
+
&& (next.projectId!==old.projectId
|
|
174
|
+
||next.taskId!==undefined&&next.taskId!==old.taskId
|
|
175
|
+
||prePermitCas)))
|
|
176
|
+
throw fail('SESSION_ALREADY_ACTIVE');
|
|
177
|
+
if(old&&old.sessionId===next.sessionId){
|
|
178
|
+
if(next.projectId!==old.projectId)throw fail('SESSION_PROJECT_CHANGED');
|
|
179
|
+
if(next.taskId!==old.taskId)throw fail('SESSION_TASK_CHANGED');
|
|
180
|
+
if(next.generation<old.generation)throw fail('SESSION_STALE_GENERATION');
|
|
181
|
+
// One verified Handoff advances exactly one generation. A skipped
|
|
182
|
+
// generation cannot become a durable Owner identity.
|
|
183
|
+
if(next.generation>old.generation+1)throw fail('SESSION_GENERATION_GAP');
|
|
184
|
+
if(next.generation===old.generation+1
|
|
185
|
+
&&(old.activeChatId===null||next.activeChatId===null
|
|
186
|
+
||next.activeChatId===old.activeChatId))
|
|
187
|
+
throw fail('SESSION_HANDOFF_IDENTITY_UNVERIFIED');
|
|
188
|
+
// A terminal session may be re-read or fenced, not reassigned to
|
|
189
|
+
// another Chat or generation under the same sessionId.
|
|
190
|
+
if(['DONE','BLOCKED','CANCELLED'].includes(old.phase)
|
|
191
|
+
&&(next.generation!==old.generation
|
|
192
|
+
||next.activeChatId!==old.activeChatId
|
|
193
|
+
||next.activeChatUrl!==old.activeChatUrl))
|
|
194
|
+
throw fail('SESSION_TERMINAL_IDENTITY_CHANGED');
|
|
195
|
+
if(old.phase==='DONE'&&next.phase!=='DONE'
|
|
196
|
+
||old.phase==='CANCELLED'&&next.phase!=='CANCELLED'
|
|
197
|
+
||old.phase==='BLOCKED'
|
|
198
|
+
&& !['BLOCKED','DONE'].includes(next.phase)
|
|
199
|
+
&&!(resumeBlockedSessionId===old.sessionId&&next.phase==='RESERVED')
|
|
200
|
+
&&!cancelCas)
|
|
201
|
+
throw fail('SESSION_TERMINAL_LOCKED');
|
|
202
|
+
if(old.activeChatId!==null&&next.generation===old.generation&&
|
|
203
|
+
next.activeChatId!==null&&old.activeChatId!==next.activeChatId)
|
|
204
|
+
throw fail('SESSION_CHAT_CHANGED_WITHOUT_HANDOFF');
|
|
205
|
+
}
|
|
206
|
+
// A read-only Status query and non-authoritative Worker diagnostics can
|
|
207
|
+
// re-sync the SAME durable projection. These are not a new Session
|
|
208
|
+
// transition: preserve Native's CAS timestamp and avoid rewriting disk.
|
|
209
|
+
// A genuine phase/identity/generation change still advances the record.
|
|
210
|
+
if(old && !replaceBlockedSessionId && !resumeBlockedSessionId
|
|
211
|
+
&& ['sessionId','projectId','taskId','phase','generation',
|
|
212
|
+
'activeChatId','activeChatUrl'].every(key=>old[key]===next[key]))
|
|
213
|
+
return old;
|
|
214
|
+
// updatedAt is the Native recovery CAS value, not merely display time.
|
|
215
|
+
// Two Worker transitions can occur inside one millisecond. Every actual
|
|
216
|
+
// write must advance that version; an identical projection returned above
|
|
217
|
+
// deliberately keeps the previous value.
|
|
218
|
+
const committed=old&&next.updatedAt<=old.updatedAt
|
|
219
|
+
?{...next,updatedAt:old.updatedAt+1}:next;
|
|
220
|
+
if(!Number.isSafeInteger(committed.updatedAt))
|
|
221
|
+
throw fail('SESSION_CLOCK_EXHAUSTED');
|
|
222
|
+
const temp=join(dir,'.active-session-'+randomUUID()+'.tmp');
|
|
223
|
+
try{writeFileSync(temp,JSON.stringify(committed,null,2)+'\n',{flag:'wx',mode:0o600});
|
|
224
|
+
renameSync(temp,file);
|
|
225
|
+
}catch(e){try{unlinkSync(temp);}catch{}throw e;}
|
|
226
|
+
return committed;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tomflow/chatloop-extension",
|
|
3
|
+
"version": "0.4.34",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Chat Loop Extension: reviewed formal Action pages, including TEST_ONLY native App entry",
|
|
6
|
+
"bin": {
|
|
7
|
+
"chatloop-extension": "bin/cli.mjs"
|
|
8
|
+
},
|
|
9
|
+
"engines": {
|
|
10
|
+
"node": ">=20"
|
|
11
|
+
},
|
|
12
|
+
"publishConfig": {
|
|
13
|
+
"access": "public"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"index.mjs",
|
|
17
|
+
"actions.generated.mjs",
|
|
18
|
+
"capabilities/",
|
|
19
|
+
"scripts/",
|
|
20
|
+
"extension/",
|
|
21
|
+
"bin/",
|
|
22
|
+
"lib/",
|
|
23
|
+
"README.md"
|
|
24
|
+
],
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build:actions": "node ./scripts/build-actions.mjs",
|
|
27
|
+
"build:actions:check": "node ./scripts/build-actions.mjs --check",
|
|
28
|
+
"prepack": "node ./scripts/build-actions.mjs --check"
|
|
29
|
+
},
|
|
30
|
+
"exports": {
|
|
31
|
+
".": "./index.mjs"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@tomflow/chatloop-trace": "0.4.6"
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// Extension action definitions are assembled at build time, never discovered
|
|
2
|
+
// by a privileged running Chrome Worker or Native Host.
|
|
3
|
+
import {readFileSync,readdirSync,writeFileSync,unlinkSync,existsSync} from 'node:fs';
|
|
4
|
+
import {dirname,join,resolve} from 'node:path';
|
|
5
|
+
import {fileURLToPath,pathToFileURL} from 'node:url';
|
|
6
|
+
import {createHash} from 'node:crypto';
|
|
7
|
+
const root=dirname(dirname(fileURLToPath(import.meta.url)));
|
|
8
|
+
const fail=code=>Object.assign(Error(code),{code});
|
|
9
|
+
const originals=['chat_text','chat_dom','chat_screenshot','reload',
|
|
10
|
+
'continue','handoff','clean','status','start','session'];
|
|
11
|
+
const privilegedKinds=Object.freeze({chat_text:'eye-read',chat_dom:'eye-read',
|
|
12
|
+
chat_screenshot:'eye-screenshot',reload:'eye-reload',
|
|
13
|
+
continue:'owner-intent',handoff:'owner-intent',clean:'owner-clean',
|
|
14
|
+
status:'owner-status',start:'owner-start',session:'owner-session'});
|
|
15
|
+
// Existing timeout and receipt projection are part of the public action
|
|
16
|
+
// behavior; a descriptor edit must not silently change side-effect handling.
|
|
17
|
+
const builtinBehavior=Object.freeze({
|
|
18
|
+
chat_text:[27000,true],chat_dom:[27000,true],
|
|
19
|
+
chat_screenshot:[27000,false],reload:[27000,false],
|
|
20
|
+
continue:[27000,true],handoff:[27000,true],clean:[27000,true],
|
|
21
|
+
status:[27000,true],start:[310000,true],session:[90000,true]
|
|
22
|
+
});
|
|
23
|
+
const builtinRoute=Object.freeze({
|
|
24
|
+
chat_text:['text.html','chat text'],chat_dom:['dom.html','chat dom'],
|
|
25
|
+
chat_screenshot:['screenshot.html','chat screenshot'],
|
|
26
|
+
reload:['reload.html','reload'],continue:['continue.html','continue'],
|
|
27
|
+
handoff:['handoff.html','handoff'],clean:['clean.html','clean'],
|
|
28
|
+
status:['status.html','status'],start:['start.html','start'],
|
|
29
|
+
session:['session.html','session']
|
|
30
|
+
});
|
|
31
|
+
const generatedMarker='<!-- GENERATED BY build-actions.mjs -->';
|
|
32
|
+
export async function compileActions(source=join(root,'capabilities'),
|
|
33
|
+
extension=join(root,'extension')){
|
|
34
|
+
const files=readdirSync(source).filter(f=>f.endsWith('.mjs')).sort();
|
|
35
|
+
const ids=new Set(),pages=new Set(),clis=new Set(),actions=[];
|
|
36
|
+
if(!files.length)throw fail('EXTENSION_ACTIONS_EMPTY');
|
|
37
|
+
for(const file of files){
|
|
38
|
+
if(!/^[a-z][a-z0-9_]*\.mjs$/.test(file))
|
|
39
|
+
throw fail('EXTENSION_CAPABILITY_FILENAME_INVALID');
|
|
40
|
+
const path=join(source,file);
|
|
41
|
+
const hash=createHash('sha256').update(readFileSync(path)).digest('hex');
|
|
42
|
+
const {capability:c}=await import(pathToFileURL(path).href+'?sha256='+hash);
|
|
43
|
+
if(!c||c.id!==file.slice(0,-4)||
|
|
44
|
+
!/^[a-z][a-z0-9_]*$/.test(c.id)||
|
|
45
|
+
!/^[a-z][a-z0-9_-]*\.html$/.test(c.page)||
|
|
46
|
+
!Array.isArray(c.cli)||!c.cli.length||
|
|
47
|
+
c.cli.some(t=>typeof t!=='string'||!/^[a-z][a-z0-9-]*$/.test(t))||
|
|
48
|
+
!Number.isSafeInteger(c.waitMs)||c.waitMs<1||c.waitMs>310000||
|
|
49
|
+
typeof c.textResult!=='boolean'||typeof c.description!=='string'||
|
|
50
|
+
!c.description.trim()||ids.has(c.id)||pages.has(c.page)||
|
|
51
|
+
clis.has(c.cli.join(' ')))
|
|
52
|
+
throw fail('EXTENSION_CAPABILITY_INVALID');
|
|
53
|
+
ids.add(c.id);pages.add(c.page);clis.add(c.cli.join(' '));
|
|
54
|
+
const newAction=!originals.includes(c.id);
|
|
55
|
+
if(!newAction && privilegedKinds[c.id]!==c.kind)
|
|
56
|
+
throw fail('EXTENSION_ACTION_AUTHORITY_CHANGED');
|
|
57
|
+
if(!newAction && (c.page!==builtinRoute[c.id][0]||
|
|
58
|
+
c.cli.join(' ')!==builtinRoute[c.id][1]))
|
|
59
|
+
throw fail('EXTENSION_BUILTIN_ROUTE_CHANGED');
|
|
60
|
+
if(!newAction && (c.waitMs!==builtinBehavior[c.id][0]||
|
|
61
|
+
c.textResult!==builtinBehavior[c.id][1]))
|
|
62
|
+
throw fail('EXTENSION_BUILTIN_BEHAVIOR_CHANGED');
|
|
63
|
+
if(newAction && !c.textResult)
|
|
64
|
+
throw fail('EXTENSION_READ_RESULT_REQUIRED');
|
|
65
|
+
if(c.kind==='eye-read'&&(!c.id.startsWith('chat_')||
|
|
66
|
+
!['text','dom'].includes(c.readMode)))
|
|
67
|
+
throw fail('EXTENSION_EYE_READ_MODE_INVALID');
|
|
68
|
+
const reviewedEntry=c.id==='entry_test' && c.kind==='entry-test'
|
|
69
|
+
&&c.page==='entry_test.html' &&c.cli.join(' ')==='entry test'
|
|
70
|
+
&&c.waitMs===310000 &&c.textResult===true;
|
|
71
|
+
if(newAction && c.kind!=='eye-read' && !reviewedEntry)
|
|
72
|
+
throw fail('EXTENSION_PRIVILEGED_ACTION_REVIEW_REQUIRED');
|
|
73
|
+
if((c.id==='chat_text'&&c.readMode!=='text')||
|
|
74
|
+
(c.id==='chat_dom'&&c.readMode!=='dom'))
|
|
75
|
+
throw fail('EXTENSION_BUILTIN_EYE_MODE_CHANGED');
|
|
76
|
+
const page=join(extension,c.page);
|
|
77
|
+
if(reviewedEntry&&!existsSync(page))throw fail('EXTENSION_ENTRY_ACTION_PAGE_MISSING');
|
|
78
|
+
const generated=existsSync(page)&&
|
|
79
|
+
readFileSync(page,'utf8').startsWith(generatedMarker);
|
|
80
|
+
if(!existsSync(page) && !newAction)
|
|
81
|
+
throw fail('EXTENSION_ACTION_PAGE_MISSING');
|
|
82
|
+
if(existsSync(page)&&!generated){
|
|
83
|
+
const expected='actions/'+(c.id.startsWith('chat_')?c.id.slice(5):c.id)+'.js';
|
|
84
|
+
if(!readFileSync(page,'utf8').includes(expected))
|
|
85
|
+
throw fail('EXTENSION_ACTION_EXECUTOR_MISMATCH');
|
|
86
|
+
}
|
|
87
|
+
actions.push({...c,file,generated:newAction && c.kind==='eye-read'});
|
|
88
|
+
}
|
|
89
|
+
actions.sort((a,b)=>(
|
|
90
|
+
(originals.indexOf(a.id)<0?99:originals.indexOf(a.id))-
|
|
91
|
+
(originals.indexOf(b.id)<0?99:originals.indexOf(b.id))
|
|
92
|
+
||a.id.localeCompare(b.id)));
|
|
93
|
+
const imports=actions.map((a,i)=>
|
|
94
|
+
"import {capability as a"+i+"} from './capabilities/"+a.file+"';").join('\n');
|
|
95
|
+
const node='// GENERATED by scripts/build-actions.mjs — do not edit.\n'+
|
|
96
|
+
imports+'\nexport const actionCapabilities=Object.freeze(['+
|
|
97
|
+
actions.map((_,i)=>'a'+i).join(',')+']);\n';
|
|
98
|
+
const eye=Object.fromEntries(actions.filter(a=>a.kind.startsWith('eye-'))
|
|
99
|
+
.map(a=>[a.id,{page:a.page,kind:a.kind,readMode:a.readMode??null}]));
|
|
100
|
+
const browser='// GENERATED by scripts/build-actions.mjs — do not edit.\n'+
|
|
101
|
+
'globalThis.ChatLoopEyeCatalogV1=Object.freeze('+JSON.stringify(eye)+');\n';
|
|
102
|
+
const aliases={};
|
|
103
|
+
for(const a of actions.filter(a=>a.generated)){
|
|
104
|
+
aliases[a.page]=generatedMarker+'\n<!doctype html><html><head><meta charset="utf-8">'+
|
|
105
|
+
'<title>Chat Loop read action</title></head><body>'+
|
|
106
|
+
'<p id="status"></p><p id="details"></p>'+
|
|
107
|
+
'<script src="actions/common.js"></script>'+
|
|
108
|
+
'<script src="actions/read-mode.js" data-action="'+a.id+
|
|
109
|
+
'" data-read-mode="'+a.readMode+'"></script></body></html>\n';
|
|
110
|
+
}
|
|
111
|
+
return {node,browser,aliases};
|
|
112
|
+
}
|
|
113
|
+
async function main(){
|
|
114
|
+
const ext=join(root,'extension'),expected=await compileActions();
|
|
115
|
+
const manifest=JSON.parse(readFileSync(join(ext,'manifest.json'),'utf8'));
|
|
116
|
+
const pkg=JSON.parse(readFileSync(join(root,'package.json'),'utf8'));
|
|
117
|
+
if(manifest.version!==pkg.version||manifest.manifest_version!==3)
|
|
118
|
+
throw fail('EXTENSION_PACKAGE_VERSION_MISMATCH');
|
|
119
|
+
const targets=[[join(root,'actions.generated.mjs'),expected.node],
|
|
120
|
+
[join(ext,'loop','action-catalog.generated.js'),expected.browser],
|
|
121
|
+
...Object.entries(expected.aliases).map(([page,html])=>[join(ext,page),html])];
|
|
122
|
+
const orphan=readdirSync(ext).filter(f=>f.endsWith('.html')&&
|
|
123
|
+
!Object.hasOwn(expected.aliases,f)&&
|
|
124
|
+
readFileSync(join(ext,f),'utf8').startsWith(generatedMarker));
|
|
125
|
+
if(process.argv.includes('--check')){
|
|
126
|
+
if(orphan.length||targets.some(([file,value])=>!existsSync(file)||
|
|
127
|
+
readFileSync(file,'utf8')!==value))
|
|
128
|
+
throw fail('EXTENSION_ACTIONS_STALE');
|
|
129
|
+
if(process.env.npm_lifecycle_event!=='prepack')
|
|
130
|
+
console.log('EXTENSION_ACTIONS_CURRENT');
|
|
131
|
+
}else{
|
|
132
|
+
for(const page of orphan)unlinkSync(join(ext,page));
|
|
133
|
+
for(const [file,value] of targets)writeFileSync(file,value);
|
|
134
|
+
console.log('EXTENSION_ACTIONS_BUILT');
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
if(process.argv[1]&&resolve(process.argv[1])===fileURLToPath(import.meta.url))
|
|
138
|
+
await main();
|