@tomflow/chatloop-tools 0.9.63 → 0.9.64

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 (154) hide show
  1. package/README.md +13 -1
  2. package/index.mjs +7 -7
  3. package/lib/context-control-run.mjs +23 -5
  4. package/lib/extension-proxy.mjs +16 -7
  5. package/lib/native-owner-v2.mjs +128 -0
  6. package/lib/release-health-checks.mjs +29 -5
  7. package/lib/trace-gateway.mjs +3 -1
  8. package/package.json +2 -12
  9. package/providers/extension.mjs +59 -16
  10. package/node_modules/@tomflow/chatloop-context/README.md +0 -240
  11. package/node_modules/@tomflow/chatloop-context/bin/cli.mjs +0 -48
  12. package/node_modules/@tomflow/chatloop-context/capabilities/approval.mjs +0 -151
  13. package/node_modules/@tomflow/chatloop-context/capabilities/context.mjs +0 -376
  14. package/node_modules/@tomflow/chatloop-context/capabilities/document.mjs +0 -266
  15. package/node_modules/@tomflow/chatloop-context/capabilities/evidence.mjs +0 -134
  16. package/node_modules/@tomflow/chatloop-context/capabilities/presentation.mjs +0 -82
  17. package/node_modules/@tomflow/chatloop-context/capabilities/project.mjs +0 -349
  18. package/node_modules/@tomflow/chatloop-context/capabilities/sdk.mjs +0 -72
  19. package/node_modules/@tomflow/chatloop-context/capabilities/start.mjs +0 -73
  20. package/node_modules/@tomflow/chatloop-context/capabilities/task.mjs +0 -507
  21. package/node_modules/@tomflow/chatloop-context/capabilities.generated.mjs +0 -11
  22. package/node_modules/@tomflow/chatloop-context/docs/interfaces.md +0 -192
  23. package/node_modules/@tomflow/chatloop-context/docs/operator-and-migration.md +0 -277
  24. package/node_modules/@tomflow/chatloop-context/index.mjs +0 -236
  25. package/node_modules/@tomflow/chatloop-context/lib/api-log.mjs +0 -86
  26. package/node_modules/@tomflow/chatloop-context/lib/approval-policy.mjs +0 -77
  27. package/node_modules/@tomflow/chatloop-context/lib/capability-catalog.generated.json +0 -1989
  28. package/node_modules/@tomflow/chatloop-context/lib/capability-router.mjs +0 -69
  29. package/node_modules/@tomflow/chatloop-context/lib/cli-commands.mjs +0 -285
  30. package/node_modules/@tomflow/chatloop-context/lib/context-cli.mjs +0 -298
  31. package/node_modules/@tomflow/chatloop-context/lib/context-migration.mjs +0 -286
  32. package/node_modules/@tomflow/chatloop-context/lib/core/context.mjs +0 -115
  33. package/node_modules/@tomflow/chatloop-context/lib/core/evidence.mjs +0 -51
  34. package/node_modules/@tomflow/chatloop-context/lib/core/health.mjs +0 -75
  35. package/node_modules/@tomflow/chatloop-context/lib/core/migration.mjs +0 -262
  36. package/node_modules/@tomflow/chatloop-context/lib/core/project.mjs +0 -246
  37. package/node_modules/@tomflow/chatloop-context/lib/core/review.mjs +0 -147
  38. package/node_modules/@tomflow/chatloop-context/lib/core/run-cancel.mjs +0 -54
  39. package/node_modules/@tomflow/chatloop-context/lib/core/run.mjs +0 -375
  40. package/node_modules/@tomflow/chatloop-context/lib/core/store.mjs +0 -397
  41. package/node_modules/@tomflow/chatloop-context/lib/core/task.mjs +0 -183
  42. package/node_modules/@tomflow/chatloop-context/lib/line-review.mjs +0 -13
  43. package/node_modules/@tomflow/chatloop-context/lib/methods.mjs +0 -111
  44. package/node_modules/@tomflow/chatloop-context/lib/operations.mjs +0 -107
  45. package/node_modules/@tomflow/chatloop-context/lib/orphan-operator.mjs +0 -118
  46. package/node_modules/@tomflow/chatloop-context/lib/process-inventory.mjs +0 -20
  47. package/node_modules/@tomflow/chatloop-context/lib/project-branch.mjs +0 -316
  48. package/node_modules/@tomflow/chatloop-context/lib/project-diagnostic.mjs +0 -153
  49. package/node_modules/@tomflow/chatloop-context/lib/project-guards.mjs +0 -19
  50. package/node_modules/@tomflow/chatloop-context/lib/project-store.mjs +0 -117
  51. package/node_modules/@tomflow/chatloop-context/lib/trusted-run.mjs +0 -97
  52. package/node_modules/@tomflow/chatloop-context/lib/workspace-operator.mjs +0 -169
  53. package/node_modules/@tomflow/chatloop-context/package.json +0 -40
  54. package/node_modules/@tomflow/chatloop-context/scripts/build-capabilities.mjs +0 -114
  55. package/node_modules/@tomflow/chatloop-extension/README.md +0 -124
  56. package/node_modules/@tomflow/chatloop-extension/actions.generated.mjs +0 -13
  57. package/node_modules/@tomflow/chatloop-extension/bin/cli.mjs +0 -153
  58. package/node_modules/@tomflow/chatloop-extension/bin/native-host.mjs +0 -207
  59. package/node_modules/@tomflow/chatloop-extension/capabilities/chat_dom.mjs +0 -1
  60. package/node_modules/@tomflow/chatloop-extension/capabilities/chat_screenshot.mjs +0 -1
  61. package/node_modules/@tomflow/chatloop-extension/capabilities/chat_text.mjs +0 -1
  62. package/node_modules/@tomflow/chatloop-extension/capabilities/clean.mjs +0 -1
  63. package/node_modules/@tomflow/chatloop-extension/capabilities/continue.mjs +0 -1
  64. package/node_modules/@tomflow/chatloop-extension/capabilities/entry_test.mjs +0 -1
  65. package/node_modules/@tomflow/chatloop-extension/capabilities/handoff.mjs +0 -1
  66. package/node_modules/@tomflow/chatloop-extension/capabilities/reload.mjs +0 -1
  67. package/node_modules/@tomflow/chatloop-extension/capabilities/session.mjs +0 -1
  68. package/node_modules/@tomflow/chatloop-extension/capabilities/start.mjs +0 -1
  69. package/node_modules/@tomflow/chatloop-extension/capabilities/status.mjs +0 -1
  70. package/node_modules/@tomflow/chatloop-extension/extension/actions/clean.js +0 -123
  71. package/node_modules/@tomflow/chatloop-extension/extension/actions/common.js +0 -183
  72. package/node_modules/@tomflow/chatloop-extension/extension/actions/continue.js +0 -340
  73. package/node_modules/@tomflow/chatloop-extension/extension/actions/dom.js +0 -2
  74. package/node_modules/@tomflow/chatloop-extension/extension/actions/entry_test.js +0 -241
  75. package/node_modules/@tomflow/chatloop-extension/extension/actions/handoff.js +0 -393
  76. package/node_modules/@tomflow/chatloop-extension/extension/actions/index.js +0 -2
  77. package/node_modules/@tomflow/chatloop-extension/extension/actions/model-intent.js +0 -56
  78. package/node_modules/@tomflow/chatloop-extension/extension/actions/read-mode.js +0 -9
  79. package/node_modules/@tomflow/chatloop-extension/extension/actions/reload.js +0 -20
  80. package/node_modules/@tomflow/chatloop-extension/extension/actions/screenshot.js +0 -77
  81. package/node_modules/@tomflow/chatloop-extension/extension/actions/session.js +0 -52
  82. package/node_modules/@tomflow/chatloop-extension/extension/actions/start.js +0 -295
  83. package/node_modules/@tomflow/chatloop-extension/extension/actions/status.js +0 -24
  84. package/node_modules/@tomflow/chatloop-extension/extension/actions/text.js +0 -2
  85. package/node_modules/@tomflow/chatloop-extension/extension/background.js +0 -17
  86. package/node_modules/@tomflow/chatloop-extension/extension/clean.html +0 -5
  87. package/node_modules/@tomflow/chatloop-extension/extension/content.js +0 -40
  88. package/node_modules/@tomflow/chatloop-extension/extension/continue.html +0 -9
  89. package/node_modules/@tomflow/chatloop-extension/extension/dom.html +0 -17
  90. package/node_modules/@tomflow/chatloop-extension/extension/entry_test.html +0 -6
  91. package/node_modules/@tomflow/chatloop-extension/extension/handoff.html +0 -9
  92. package/node_modules/@tomflow/chatloop-extension/extension/loop/action-catalog.generated.js +0 -2
  93. package/node_modules/@tomflow/chatloop-extension/extension/loop/api-aspect.generated.js +0 -275
  94. package/node_modules/@tomflow/chatloop-extension/extension/loop/api-observation-queue.generated.js +0 -84
  95. package/node_modules/@tomflow/chatloop-extension/extension/loop/core.js +0 -107
  96. package/node_modules/@tomflow/chatloop-extension/extension/loop/index.js +0 -36
  97. package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-approval.js +0 -299
  98. package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-inspect.js +0 -144
  99. package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-poll.js +0 -171
  100. package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-recovery.js +0 -109
  101. package/node_modules/@tomflow/chatloop-extension/extension/loop/observer.js +0 -154
  102. package/node_modules/@tomflow/chatloop-extension/extension/loop/resources.js +0 -259
  103. package/node_modules/@tomflow/chatloop-extension/extension/manifest.json +0 -42
  104. package/node_modules/@tomflow/chatloop-extension/extension/popup.html +0 -25
  105. package/node_modules/@tomflow/chatloop-extension/extension/popup.js +0 -99
  106. package/node_modules/@tomflow/chatloop-extension/extension/reload.html +0 -17
  107. package/node_modules/@tomflow/chatloop-extension/extension/screenshot.html +0 -17
  108. package/node_modules/@tomflow/chatloop-extension/extension/session.html +0 -6
  109. package/node_modules/@tomflow/chatloop-extension/extension/start.html +0 -6
  110. package/node_modules/@tomflow/chatloop-extension/extension/status.html +0 -6
  111. package/node_modules/@tomflow/chatloop-extension/extension/text.html +0 -17
  112. package/node_modules/@tomflow/chatloop-extension/extension/worker/action-result.js +0 -136
  113. package/node_modules/@tomflow/chatloop-extension/extension/worker/action-take.js +0 -218
  114. package/node_modules/@tomflow/chatloop-extension/extension/worker/admission.js +0 -124
  115. package/node_modules/@tomflow/chatloop-extension/extension/worker/adopt-prepare.js +0 -296
  116. package/node_modules/@tomflow/chatloop-extension/extension/worker/adopt-recovery.js +0 -186
  117. package/node_modules/@tomflow/chatloop-extension/extension/worker/adopt-result.js +0 -126
  118. package/node_modules/@tomflow/chatloop-extension/extension/worker/bootstrap.js +0 -230
  119. package/node_modules/@tomflow/chatloop-extension/extension/worker/manual.js +0 -163
  120. package/node_modules/@tomflow/chatloop-extension/extension/worker/readiness.js +0 -345
  121. package/node_modules/@tomflow/chatloop-extension/extension/worker/router.js +0 -192
  122. package/node_modules/@tomflow/chatloop-extension/extension/worker/scheduler.js +0 -161
  123. package/node_modules/@tomflow/chatloop-extension/extension/worker/session-control.js +0 -75
  124. package/node_modules/@tomflow/chatloop-extension/extension/worker/turn-failure.js +0 -197
  125. package/node_modules/@tomflow/chatloop-extension/extension/worker/turn-finished.js +0 -205
  126. package/node_modules/@tomflow/chatloop-extension/index.mjs +0 -71
  127. package/node_modules/@tomflow/chatloop-extension/lib/action-routing.mjs +0 -19
  128. package/node_modules/@tomflow/chatloop-extension/lib/api-observation-queue.mjs +0 -82
  129. package/node_modules/@tomflow/chatloop-extension/lib/bridge.mjs +0 -303
  130. package/node_modules/@tomflow/chatloop-extension/lib/browser-launch.mjs +0 -89
  131. package/node_modules/@tomflow/chatloop-extension/lib/context-binding.mjs +0 -45
  132. package/node_modules/@tomflow/chatloop-extension/lib/context-ipc.mjs +0 -115
  133. package/node_modules/@tomflow/chatloop-extension/lib/owner-evidence.mjs +0 -75
  134. package/node_modules/@tomflow/chatloop-extension/lib/session-file.mjs +0 -185
  135. package/node_modules/@tomflow/chatloop-extension/package.json +0 -38
  136. package/node_modules/@tomflow/chatloop-extension/scripts/build-actions.mjs +0 -138
  137. package/node_modules/@tomflow/chatloop-trace/README.md +0 -118
  138. package/node_modules/@tomflow/chatloop-trace/events/context.api.mjs +0 -23
  139. package/node_modules/@tomflow/chatloop-trace/events/typed.api.call.mjs +0 -22
  140. package/node_modules/@tomflow/chatloop-trace/events.generated.mjs +0 -4
  141. package/node_modules/@tomflow/chatloop-trace/index.mjs +0 -59
  142. package/node_modules/@tomflow/chatloop-trace/lib/api-aspect.mjs +0 -273
  143. package/node_modules/@tomflow/chatloop-trace/lib/trace-reader.mjs +0 -308
  144. package/node_modules/@tomflow/chatloop-trace/lib/trace-retention.mjs +0 -140
  145. package/node_modules/@tomflow/chatloop-trace/lib/trace.mjs +0 -236
  146. package/node_modules/@tomflow/chatloop-trace/package.json +0 -30
  147. package/node_modules/@tomflow/chatloop-trace/scripts/build-events.mjs +0 -68
  148. package/node_modules/ps-list/index.d.ts +0 -76
  149. package/node_modules/ps-list/index.js +0 -366
  150. package/node_modules/ps-list/license +0 -9
  151. package/node_modules/ps-list/package.json +0 -44
  152. package/node_modules/ps-list/readme.md +0 -56
  153. package/node_modules/ps-list/vendor/fastlist-0.3.0-x64.exe +0 -0
  154. package/node_modules/ps-list/vendor/fastlist-0.3.0-x86.exe +0 -0
@@ -1,147 +0,0 @@
1
- // Context Core review methods. Shared access and transactions are injected from store.mjs.
2
- export function createReviewMethods(values) {
3
- const {assert, ID, digest, parse, db, hasRequirementApproval, randomUUID, checkIds, receipt, readAuth, ensureNoPendingIssue, expectedBusiness, scope} = values;
4
- return {
5
- documentCreate(a){
6
- return receipt(a,'document.createDraft',()=>{
7
- const r=scope(a);expectedBusiness(r,a.expectedTaskRevision);
8
- // The public Method Catalog also advertises immutable task-scoped
9
- // reference and historical-evidence drafts. They are storage only:
10
- // document.prepareIssue must never turn them into R/P approval issues.
11
- assert(['R','P','INSTRUCTIONS','HISTORY','LEGACY_EVIDENCE','REFERENCE']
12
- .includes(a.kind),'INVALID_DOCUMENT_KIND');
13
- assert(ID.test(a.documentId||'')&&Number.isInteger(a.version)&&a.version>=1,
14
- 'INVALID_DOCUMENT_REF');
15
- assert(typeof a.body==='string'&&Buffer.byteLength(a.body,'utf8')>=1
16
- &&Buffer.byteLength(a.body,'utf8')<=4*1024*1024,'INVALID_DOCUMENT_BODY');
17
- if(a.kind!=='P')assert(a.requirementRef===undefined,
18
- 'REQUIREMENT_REF_FOR_NON_PLAN');
19
- if(a.kind==='P'){
20
- assert(a.requirementRef&&typeof a.requirementRef==='object','REQUIREMENT_REF_REQUIRED');
21
- const req=db.prepare("SELECT content_hash FROM documents WHERE project_id=? AND task_id=? AND document_id=? AND version=? AND kind='R'")
22
- .get(a.projectId,a.taskId,a.requirementRef.documentId,a.requirementRef.version);
23
- assert(req&&req.content_hash===a.requirementRef.contentHash,'REQUIREMENT_REF_MISMATCH');
24
- }
25
- assert(!db.prepare('SELECT 1 FROM documents WHERE project_id=? AND task_id=? AND document_id=? AND version=?')
26
- .get(a.projectId,a.taskId,a.documentId,a.version),'DOCUMENT_EXISTS');
27
- const contentHash=digest(a.body);
28
- db.prepare('INSERT INTO documents VALUES(?,?,?,?,?,?,?,?)')
29
- .run(a.projectId,a.taskId,a.documentId,a.version,a.kind,a.body,contentHash,
30
- a.requirementRef?JSON.stringify(a.requirementRef):null);
31
- db.prepare('UPDATE tasks SET business_revision=business_revision+1 WHERE project_id=? AND task_id=?')
32
- .run(a.projectId,a.taskId);
33
- return {status:'COMMITTED',documentId:a.documentId,version:a.version,
34
- kind:a.kind,contentHash,taskRevision:r.business_revision+1};
35
- });
36
- },
37
- documentGet(projectId,taskId,documentId,version){
38
- checkIds(projectId,taskId);readAuth(projectId,taskId,'document.get');
39
- const r=db.prepare('SELECT * FROM documents WHERE project_id=? AND task_id=? AND document_id=? AND version=?')
40
- .get(projectId,taskId,documentId,version);
41
- assert(r,'DOCUMENT_NOT_FOUND');
42
- return {documentId:r.document_id,version:r.version,kind:r.kind,body:r.body,
43
- contentHash:r.content_hash,requirementRef:r.requirement_ref?parse(r.requirement_ref):null};
44
- },
45
- documentList(projectId,taskId){
46
- checkIds(projectId,taskId);readAuth(projectId,taskId,'document.list');
47
- return db.prepare('SELECT document_id,version,kind,content_hash FROM documents WHERE project_id=? AND task_id=? ORDER BY kind,version')
48
- .all(projectId,taskId).map(r=>({documentId:r.document_id,version:r.version,kind:r.kind,contentHash:r.content_hash}));
49
- },
50
- presentationResolve(a){
51
- return receipt(a,'presentation.resolve',()=>{
52
- const r=scope(a);expectedBusiness(r,a.expectedTaskRevision);
53
- const issue=db.prepare('SELECT * FROM issues WHERE project_id=? AND task_id=? AND issue_id=?')
54
- .get(a.projectId,a.taskId,a.issueId);
55
- assert(issue&&issue.state==='PENDING','ISSUE_NOT_PENDING');
56
- assert(['CANCELLED','FAILED','REJECTED'].includes(a.outcome),'INVALID_PRESENTATION_OUTCOME');
57
- assert(typeof a.sourceRef==='string'&&a.sourceRef.length>=8,
58
- 'PRESENTATION_EVIDENCE_REQUIRED');
59
- db.prepare('UPDATE issues SET state=?,presented_ref=? WHERE project_id=? AND task_id=? AND issue_id=?')
60
- .run(a.outcome,a.sourceRef,a.projectId,a.taskId,a.issueId);
61
- db.prepare('UPDATE tasks SET business_revision=business_revision+1 WHERE project_id=? AND task_id=?')
62
- .run(a.projectId,a.taskId);
63
- return {status:a.outcome,taskRevision:r.business_revision+1,
64
- // prior approvals were invalidated on prepareIssue; never revived
65
- approvalsRestored:false};
66
- });
67
- },
68
- prepareIssue(a){
69
- return receipt(a,'document.prepareIssue',()=>{
70
- const r=scope(a);expectedBusiness(r,a.expectedTaskRevision);
71
- ensureNoPendingIssue(a.projectId,a.taskId);
72
- const doc=db.prepare('SELECT * FROM documents WHERE project_id=? AND task_id=? AND document_id=? AND version=?')
73
- .get(a.projectId,a.taskId,a.documentId,a.version);
74
- assert(doc&&doc.content_hash===a.contentHash,'DOCUMENT_VERSION_MISMATCH');
75
- assert(['R','P'].includes(doc.kind),'ISSUE_DOCUMENT_KIND_INVALID');
76
- const approvals=parse(r.approval_json);
77
- // Publishing a NEW version invalidates the affected previous approval.
78
- // A draft alone never revokes it. A cancelled issue will not revive it.
79
- const kept=doc.kind==='P'?approvals.filter(x=>x.kind==='R'):[];
80
- const id=randomUUID();
81
- db.prepare('INSERT INTO issues(project_id,task_id,issue_id,document_id,version,content_hash) VALUES(?,?,?,?,?,?)')
82
- .run(a.projectId,a.taskId,id,a.documentId,a.version,a.contentHash);
83
- db.prepare('UPDATE tasks SET approval_json=?,approval_revision=approval_revision+1,business_revision=business_revision+1 WHERE project_id=? AND task_id=?')
84
- .run(JSON.stringify(kept),a.projectId,a.taskId);
85
- return {status:'ISSUE_PENDING',issueId:id,taskRevision:r.business_revision+1,
86
- approvalRevision:r.approval_revision+1};
87
- });
88
- },
89
- observeApproval(a){
90
- return receipt(a,'approval.observe',()=>{
91
- const r=scope(a);expectedBusiness(r,a.expectedTaskRevision);
92
- assert(['APPROVE','REJECT','REQUEST_CHANGE','CUSTOM'].includes(a.decision)
93
- &&a.sourceRole==='user'&&typeof a.userTurn==='string'
94
- &&a.userTurn.length>=8,'APPROVAL_SOURCE_INVALID');
95
- const issue=db.prepare('SELECT * FROM issues WHERE project_id=? AND task_id=? AND issue_id=?')
96
- .get(a.projectId,a.taskId,a.issueId);
97
- assert(issue&&issue.state==='PENDING','ISSUE_NOT_PENDING');
98
- assert(typeof a.presentedAssistantRef==='string'&&a.presentedAssistantRef.length>=8,
99
- 'PRESENTATION_SOURCE_REQUIRED');
100
- const doc=db.prepare('SELECT kind,content_hash,requirement_ref FROM documents WHERE project_id=? AND task_id=? AND document_id=? AND version=?')
101
- .get(a.projectId,a.taskId,issue.document_id,issue.version);
102
- assert(doc&&doc.content_hash===issue.content_hash,'DOCUMENT_CHANGED');
103
- assert(['R','P'].includes(doc.kind),'ISSUE_DOCUMENT_KIND_INVALID');
104
- if(a.decision!=='APPROVE'){
105
- // The legacy CUSTOM decision has the same meaning as a change request.
106
- const issueStatus=a.decision==='REJECT'?'REJECTED':'CHANGE_REQUESTED';
107
- db.prepare('UPDATE issues SET state=?,presented_ref=? WHERE project_id=? AND task_id=? AND issue_id=?')
108
- .run(issueStatus,a.presentedAssistantRef,a.projectId,a.taskId,a.issueId);
109
- db.prepare('UPDATE tasks SET business_revision=business_revision+1 WHERE project_id=? AND task_id=?')
110
- .run(a.projectId,a.taskId);
111
- return {status:issueStatus,kind:doc.kind,taskRevision:r.business_revision+1};
112
- }
113
- const approvals=parse(r.approval_json);
114
- const record={kind:doc.kind,documentId:issue.document_id,version:issue.version,
115
- contentHash:issue.content_hash,presentedAssistantRef:a.presentedAssistantRef,
116
- userTurn:a.userTurn,sourceRole:'user',proofLevel:'SOFT_USER_TURN'};
117
- if(doc.kind==='R'){
118
- approvals.length=0;approvals.push(record);
119
- }else{
120
- assert(hasRequirementApproval(approvals),'REQUIREMENT_APPROVAL_REQUIRED');
121
- const req=doc.requirement_ref?parse(doc.requirement_ref):null;
122
- assert(req&&req.documentId===approvals[0].documentId
123
- &&req.version===approvals[0].version
124
- &&req.contentHash===approvals[0].contentHash,'REQUIREMENT_APPROVAL_MISMATCH');
125
- assert(a.userTurn!==approvals[0].userTurn,'USER_TURN_REUSE');
126
- approvals.push(record);
127
- }
128
- db.prepare('UPDATE tasks SET approval_json=?,approval_revision=approval_revision+1,business_revision=business_revision+1 WHERE project_id=? AND task_id=?')
129
- .run(JSON.stringify(approvals),a.projectId,a.taskId);
130
- db.prepare("UPDATE issues SET state='PRESENTED',presented_ref=? WHERE project_id=? AND task_id=? AND issue_id=?")
131
- .run(a.presentedAssistantRef,a.projectId,a.taskId,a.issueId);
132
- return {status:'APPROVED_SOFT',kind:doc.kind,taskRevision:r.business_revision+1,
133
- approvalRevision:r.approval_revision+1};
134
- });
135
- },
136
- revokeApproval(a){
137
- return receipt(a,'approval.revoke',()=>{
138
- const r=scope(a);
139
- expectedBusiness(r,a.expectedTaskRevision);
140
- assert(r.approval_revision===a.expectedApprovalRevision,'APPROVAL_CONFLICT');
141
- db.prepare("UPDATE tasks SET approval_json='[]', approval_revision=approval_revision+1, business_revision=business_revision+1 WHERE project_id=? AND task_id=?")
142
- .run(a.projectId,a.taskId);
143
- return {status:'REVOKED',approvalRevision:r.approval_revision+1,taskRevision:r.business_revision+1};
144
- });
145
- }
146
- };
147
- }
@@ -1,54 +0,0 @@
1
- // Trusted Context Core transaction: idle Adopt cancellation is never Task delivery.
2
- export function createCancelMethods(values){
3
- const {internal,receipt,scope,expectedBusiness,expectedBinding,assert,
4
- projectDrift,ensureNoPendingIssue,parse,unresolvedEffects,db}=values;
5
- return {
6
- fenceCancelledIdle(a){
7
- // Formal Native/Worker-controlled idle Adopt exit, NOT task delivery.
8
- // Native verifies the exact old blocked Owner before this transaction.
9
- internal('run.fence.cancel',a);
10
- return receipt(a,'run.fence.cancel',()=>{
11
- const r=scope(a);
12
- expectedBusiness(r,a.expectedTaskRevision);
13
- expectedBinding(r,a.expectedBindingRevision);
14
- assert(r.writable_ref!==null&&r.writable_ref===a.runRef
15
- &&a.runRef===a.sessionId+':'+a.taskId+':'+a.generation,
16
- 'RUN_REF_MISMATCH');
17
- assert(!r.pending_attempt,'HANDOFF_PENDING');
18
- assert(r.writer_approval_revision===r.approval_revision,
19
- 'ACTIVE_RUN_APPROVAL_STALE');
20
- assert(!projectDrift(r).projectDrift,'PROJECT_RULES_DRIFT');
21
- ensureNoPendingIssue(a.projectId,a.taskId);
22
- assert(a.reason==='USER_CANCELLED'
23
- &&typeof a.actionToken==='string'&&a.actionToken.length>=8,
24
- 'CANCEL_INTENT_REQUIRED');
25
- const context=parse(r.context_json);
26
- assert(context.stage==='DRAFT'&&context.executionOutcome!=='DONE'
27
- &&!context.deliveryRef&&!context.sessionCancellation
28
- &&Array.isArray(context.progress)&&context.progress.length===0
29
- &&Array.isArray(context.blockers)
30
- &&unresolvedEffects(context).length===0,
31
- 'IDLE_CANCEL_UNSAFE');
32
- const cancelled={status:'CANCELLED_NOT_DELIVERED',
33
- oldRunRef:a.runRef,sessionId:a.sessionId,generation:a.generation,
34
- requestId:a.writeRequestId,reason:'USER_CANCELLED',at:Date.now()};
35
- const next={...context,sessionCancellation:cancelled,
36
- blockers:[...(context.blockers||[]).filter(x=>x!=='SESSION_CANCELLED'),
37
- 'SESSION_CANCELLED']};
38
- db.prepare(`UPDATE tasks SET writable_ref=NULL,credential_digest=NULL,
39
- writer_approval_revision=NULL,binding_revision=binding_revision+1,
40
- business_revision=business_revision+1,context_json=?
41
- WHERE project_id=? AND task_id=?`)
42
- .run(JSON.stringify(next),a.projectId,a.taskId);
43
- // Old permits and start leases cannot be recycled as authority.
44
- db.prepare("UPDATE permits SET kind='USED' WHERE project_id=? AND task_id=?")
45
- .run(a.projectId,a.taskId);
46
- db.prepare('DELETE FROM start_leases WHERE project_id=? AND task_id=?')
47
- .run(a.projectId,a.taskId);
48
- return {status:'CANCEL_FENCED',taskRevision:r.business_revision+1,
49
- bindingRevision:r.binding_revision+1,executionOutcome:null,
50
- oldRunRef:a.runRef};
51
- });
52
- },
53
- };
54
- }
@@ -1,375 +0,0 @@
1
- // Context Core run methods. Shared access and transactions are injected from store.mjs.
2
- export function createRunMethods(values) {
3
- const {fail, assert, ID, runDigest, parse, db, epoch, hasApprovedPlan, approvedTurnSourceMatches, randomUUID, projectDrift, receipt, internal, admin, unresolvedEffects, ensureNoPendingIssue, expectedBusiness, expectedBinding, assertPinnedHandoffCurrent, scope} = values;
4
- return {
5
- reserve(a){
6
- return receipt(a,'start.reserve',()=>{
7
- const r=scope(a);expectedBusiness(r,a.expectedTaskRevision);
8
- assert(!r.pending_attempt,'HANDOFF_PENDING');
9
- assert(!r.writable_ref,'RUN_ALREADY_BOUND');
10
- assert(!projectDrift(r).projectDrift,'PROJECT_RULES_DRIFT');
11
- ensureNoPendingIssue(a.projectId,a.taskId);
12
- const approvals=parse(r.approval_json);
13
- assert(hasApprovedPlan(approvals),'APPROVAL_REQUIRED');
14
- assert(r.approval_revision===a.expectedApprovalRevision,'APPROVAL_CONFLICT');
15
- const current=parse(r.context_json);
16
- if(a.purpose==='REPAIR_SAME_SCOPE'){
17
- assert(current.stage==='REOPENED'&&current.reopenReason==='REPAIR_SAME_SCOPE'
18
- &&current.reopenRequest?.sourceRole==='user'
19
- &&typeof current.reopenRequest?.userTurn==='string'
20
- &&current.reopenRequest.userTurn.length>=8,'REOPEN_INTENT_REQUIRED');
21
- }else {
22
- assert(a.purpose===undefined || a.purpose==='NEW_EXECUTION','START_PURPOSE_INVALID');
23
- // Terminal DONE cannot be silently restarted with the old R/P permit.
24
- // Only a user-requested same-scope reopen may issue a repair lease.
25
- assert(current.executionOutcome!=='DONE'&&current.stage!=='ARCHIVED',
26
- 'TASK_ALREADY_DELIVERED');
27
- assert(!current.sessionCancellation,'SESSION_CANCELLED_NEW_TASK_REQUIRED');
28
- }
29
- // Never re-reserve an initial Start whose execution permit may have
30
- // been consumed. Expired, unconsumed leases cannot commit later.
31
- if(r.binding_revision===0){
32
- assert(!db.prepare('SELECT 1 FROM permits WHERE project_id=? AND task_id=? LIMIT 1')
33
- .get(a.projectId,a.taskId),'START_PERMIT_ALREADY_CONSUMED');
34
- db.prepare('DELETE FROM start_leases WHERE project_id=? AND task_id=? AND expires_at<=?')
35
- .run(a.projectId,a.taskId,Date.now());
36
- }
37
- const leaseRef=randomUUID();
38
- db.prepare('INSERT INTO start_leases VALUES(?,?,?,?,?,?)').run(leaseRef,
39
- a.projectId,a.taskId,r.approval_revision,r.business_revision,Date.now()+60000);
40
- return {status:'RESERVED',leaseRef,approvalRevision:r.approval_revision,
41
- taskRevision:r.business_revision};
42
- });
43
- },
44
- consumePermit(a){
45
- internal('permit.consume',a);
46
- return receipt(a,'permit.consume',()=>{
47
- const r=scope(a);expectedBusiness(r,a.expectedTaskRevision);
48
- assert(r.approval_revision===a.expectedApprovalRevision,'APPROVAL_CONFLICT');
49
- assert(!projectDrift(r).projectDrift,'PROJECT_RULES_DRIFT');
50
- ensureNoPendingIssue(a.projectId,a.taskId);
51
- const approvals=parse(r.approval_json);
52
- assert(hasApprovedPlan(approvals),'APPROVAL_REVOKED');
53
- // Managed original-Chat Adopt is not authorized by a valid lease
54
- // alone. The observed R/P user turns must also be those approved for
55
- // THIS Task. The browser proof is soft; Native still owns the lease,
56
- // CAS and one-use permit. No new approval or second fact store.
57
- if(a.adopt!==undefined||a.approvalEvidence!==undefined){
58
- assert(a.kind==='START'&&a.adopt===true,
59
- 'ADOPTION_APPROVAL_EVIDENCE_REQUIRED');
60
- const e=a.approvalEvidence;
61
- const validTurn=(kind,record)=>approvedTurnSourceMatches(record,{
62
- userText:e?.[kind+'UserText'],userKey:e?.[kind+'UserKey'],
63
- version:e?.[kind+'Version']},kind.toUpperCase());
64
- assert(e&&typeof e==='object'&&!Array.isArray(e)
65
- &&Object.keys(e).length===6
66
- &&['rUserText','rUserKey','rVersion','pUserText',
67
- 'pUserKey','pVersion'].every(k=>Object.hasOwn(e,k)),
68
- 'ADOPTION_APPROVAL_EVIDENCE_REQUIRED');
69
- assert(validTurn('r',approvals[0])&&validTurn('p',approvals[1])
70
- &&e.rUserKey!==e.pUserKey,
71
- 'ADOPTION_APPROVAL_SOURCE_MISMATCH');
72
- }
73
- if(['CONTINUE','HANDOFF'].includes(a.kind)){
74
- // A terminal Task with no holder is NOT eligible for more work.
75
- // Only a named pending Handoff may reserve the single NOT_SENT
76
- // Continue recovery; ordinary HANDOFF requires the old holder.
77
- assert(r.writable_ref!==null
78
- ||a.kind==='CONTINUE'&&r.pending_attempt!==null,
79
- 'WRITER_FENCED');
80
- if(a.kind==='HANDOFF')assert(r.pending_attempt===null,'HANDOFF_PENDING');
81
- if(r.writable_ref!==null)
82
- assert(r.writer_approval_revision===r.approval_revision,
83
- 'ACTIVE_RUN_APPROVAL_STALE');
84
- }
85
- if((a.kind??'START')==='START'){
86
- assert(r.writable_ref===null&&r.pending_attempt===null,'RUN_ALREADY_BOUND');
87
- const lease=db.prepare('SELECT * FROM start_leases WHERE lease_ref=?').get(a.leaseRef);
88
- assert(lease&&lease.project_id===a.projectId&&lease.task_id===a.taskId,
89
- 'START_LEASE_REQUIRED');
90
- assert(lease.expires_at>Date.now(),'START_LEASE_EXPIRED');
91
- assert(lease.approval_revision===r.approval_revision &&
92
- lease.business_revision===r.business_revision,'START_LEASE_STALE');
93
- }
94
- assert(typeof a.actionToken==='string'&&a.actionToken.length>=8,'ACTION_TOKEN_REQUIRED');
95
- const existing=db.prepare('SELECT * FROM permits WHERE project_id=? AND task_id=? AND action_token=?')
96
- .get(a.projectId,a.taskId,a.actionToken);
97
- if(existing)throw fail('ACTION_TOKEN_ALREADY_USED');
98
- // Bind each START permit to its exact lease, not just an approval
99
- // revision; the lease survives until the initial bind transaction.
100
- const permitKind=(a.kind??'START')==='START'?'START:'+a.leaseRef:a.kind;
101
- db.prepare('INSERT INTO permits VALUES(?,?,?,?,?)')
102
- .run(a.projectId,a.taskId,a.actionToken,r.approval_revision,permitKind);
103
- return {status:'PERMITTED',actionToken:a.actionToken,approvalRevision:r.approval_revision};
104
- });
105
- },
106
- bindInitial(a){
107
- internal('run.bind.initial',a);
108
- return receipt(a,'run.bind.initial',()=>{
109
- assert(a.ownerCommitted===true,'OWNER_UNVERIFIED');
110
- const r=scope(a);
111
- expectedBusiness(r,a.expectedTaskRevision);expectedBinding(r,a.expectedBindingRevision);
112
- assert(r.writable_ref===null && r.pending_attempt===null,'BINDING_CONFLICT');
113
- assert(typeof a.runCredential==='string'&&a.runCredential.length===64,'WRITE_CREDENTIAL_INVALID');
114
- assert(typeof a.runRef==='string'&&a.runRef.length>=8,'RUN_REF_MISSING');
115
- const permit=db.prepare('SELECT * FROM permits WHERE project_id=? AND task_id=? AND action_token=?')
116
- .get(a.projectId,a.taskId,a.actionToken);
117
- assert(permit&&permit.kind==='START:'+a.leaseRef,'START_PERMIT_REQUIRED');
118
- const lease=db.prepare('SELECT * FROM start_leases WHERE lease_ref=?')
119
- .get(a.leaseRef);
120
- assert(lease&&lease.project_id===a.projectId&&lease.task_id===a.taskId,
121
- 'START_LEASE_REQUIRED');
122
- // A committed permit is not authority to bind a later business
123
- // revision. The browser send is in flight: fail closed, never resend.
124
- assert(lease.business_revision===r.business_revision
125
- &&lease.approval_revision===r.approval_revision,'START_LEASE_STALE');
126
- assert(permit.approval_revision===r.approval_revision &&
127
- hasApprovedPlan(parse(r.approval_json)),'APPROVAL_REVOKED');
128
- const initialContext=parse(r.context_json);
129
- assert(initialContext.stage==='REOPENED' &&
130
- initialContext.reopenReason==='REPAIR_SAME_SCOPE' ||
131
- initialContext.executionOutcome!=='DONE' &&
132
- !['DELIVERED','ARCHIVED','REOPENED'].includes(initialContext.stage),
133
- 'START_REOPEN_REQUIRED');
134
- db.prepare(`UPDATE tasks SET writable_ref=?,credential_digest=?,
135
- writer_approval_revision=?,binding_revision=binding_revision+1 WHERE project_id=? AND task_id=?`)
136
- .run(a.runRef,runDigest(epoch,a.projectId,a.taskId,a.runRef,a.runCredential),
137
- r.approval_revision,a.projectId,a.taskId);
138
- // Retain a durable one-use tombstone, so the same token cannot be reissued.
139
- db.prepare("UPDATE permits SET kind='USED' WHERE project_id=? AND task_id=? AND action_token=?")
140
- .run(a.projectId,a.taskId,a.actionToken);
141
- db.prepare('DELETE FROM start_leases WHERE lease_ref=?').run(a.leaseRef);
142
- return {status:'BOUND',runRef:a.runRef,bindingRevision:r.binding_revision+1};
143
- });
144
- },
145
- rotateCredential(a){
146
- // Native/Worker-only recovery of an Adopt owner's lost DELIVERY
147
- // capability. This is NOT an MCP method or a second Owner binding.
148
- internal('run.credential.rotate',a);
149
- return receipt(a,'run.credential.rotate',()=>{
150
- const r=scope(a);expectedBusiness(r,a.expectedTaskRevision);
151
- expectedBinding(r,a.expectedBindingRevision);
152
- assert(r.writable_ref!==null&&r.pending_attempt===null
153
- &&r.writable_ref===a.runRef,'WRITER_FENCED');
154
- assert(hasApprovedPlan(parse(r.approval_json))
155
- &&r.writer_approval_revision===r.approval_revision
156
- &&r.approval_revision===a.expectedApprovalRevision,
157
- 'ACTIVE_RUN_APPROVAL_STALE');
158
- assert(!projectDrift(r).projectDrift,'PROJECT_RULES_DRIFT');
159
- ensureNoPendingIssue(a.projectId,a.taskId);
160
- const context=parse(r.context_json);
161
- assert(context.executionOutcome!=='DONE'
162
- &&unresolvedEffects(context).length===0,'RUN_RECOVERY_UNSAFE');
163
- assert(typeof a.newRunCredential==='string'
164
- &&/^[0-9a-f]{64}$/.test(a.newRunCredential),
165
- 'WRITE_CREDENTIAL_INVALID');
166
- assert(typeof a.actionToken==='string'&&a.actionToken.length>=8,
167
- 'ACTION_TOKEN_REQUIRED');
168
- const permit=db.prepare('SELECT kind,approval_revision FROM permits WHERE project_id=? AND task_id=? AND action_token=?')
169
- .get(a.projectId,a.taskId,a.actionToken);
170
- assert(permit?.kind==='CONTINUE'
171
- &&permit.approval_revision===r.approval_revision,
172
- 'RECOVERY_PERMIT_MISSING');
173
- db.prepare('UPDATE tasks SET credential_digest=?,binding_revision=binding_revision+1 WHERE project_id=? AND task_id=?')
174
- .run(runDigest(epoch,a.projectId,a.taskId,a.runRef,a.newRunCredential),
175
- a.projectId,a.taskId);
176
- db.prepare("UPDATE permits SET kind='USED' WHERE project_id=? AND task_id=? AND action_token=?")
177
- .run(a.projectId,a.taskId,a.actionToken);
178
- return {status:'CREDENTIAL_ROTATED',runRef:r.writable_ref,
179
- taskRevision:r.business_revision,bindingRevision:r.binding_revision+1};
180
- });
181
- },
182
- fenceOrphan(a){
183
- // LOCAL OPERATOR ONLY. Recover a stale writer whose Native Owner was
184
- // replaced by another Project. This explicitly does NOT deliver Task
185
- // work or manufacture a missing same-Session terminal fence.
186
- admin('run.orphan.fence',a.projectId);
187
- admin('run.orphan.fence.reviewed',a.projectId);
188
- return receipt(a,'run.orphan.fence',()=>{
189
- const r=scope(a);
190
- expectedBusiness(r,a.expectedTaskRevision);
191
- expectedBinding(r,a.expectedBindingRevision);
192
- assert(r.writable_ref===a.expectedRunRef&&!!a.expectedRunRef
193
- &&r.pending_attempt===null,'ORPHAN_RUN_NOT_BOUND');
194
- assert(typeof a.oldSessionId==='string'
195
- &&a.expectedRunRef.startsWith(a.oldSessionId+':'+a.taskId+':'),
196
- 'ORPHAN_SOURCE_MISMATCH');
197
- assert(typeof a.nativeSessionId==='string'
198
- &&a.nativeSessionId!==a.oldSessionId
199
- &&typeof a.nativeProjectId==='string'
200
- &&(a.nativeProjectId!==a.projectId
201
- ||typeof a.nativeTaskId==='string'&&a.nativeTaskId!==a.taskId)
202
- &&a.nativePhase==='DONE'
203
- &&Number.isSafeInteger(a.nativeUpdatedAt),
204
- 'ORPHAN_NATIVE_NOT_TERMINAL');
205
- assert(!projectDrift(r).projectDrift,'PROJECT_RULES_DRIFT');
206
- assert(this.projectBusinessAuthority(a.projectId).writable,
207
- 'PROJECT_CUTOVER_NOT_VERIFIED');
208
- const current=parse(r.context_json);
209
- assert(current.executionOutcome!=='DONE'&&!current.deliveryRef
210
- &&current.stage!=='DELIVERED','ORPHAN_ALREADY_DELIVERED');
211
- assert(unresolvedEffects(current).length===0,'UNKNOWN_EFFECTS_UNRESOLVED');
212
- assert(typeof a.evidenceDocumentId==='string'
213
- &&ID.test(a.evidenceDocumentId)
214
- &&Number.isInteger(a.evidenceVersion)&&a.evidenceVersion>0
215
- &&/^[0-9a-f]{64}$/.test(a.evidenceHash||''),
216
- 'ORPHAN_EVIDENCE_INVALID');
217
- const proof=db.prepare(
218
- 'SELECT kind,body,content_hash FROM documents WHERE project_id=? AND task_id=? AND document_id=? AND version=?')
219
- .get(a.projectId,a.taskId,a.evidenceDocumentId,a.evidenceVersion);
220
- assert(proof&&proof.kind==='REFERENCE'
221
- &&proof.content_hash===a.evidenceHash
222
- &&proof.body.includes(a.expectedRunRef),
223
- 'ORPHAN_EVIDENCE_UNVERIFIED');
224
- const blockers=Array.isArray(current.blockers)?current.blockers:[];
225
- const orphanRef={runRef:a.expectedRunRef,oldSessionId:a.oldSessionId,
226
- currentNativeSessionId:a.nativeSessionId,
227
- currentNativeProjectId:a.nativeProjectId,
228
- currentNativeTaskId:a.nativeTaskId??null,
229
- currentNativeUpdatedAt:a.nativeUpdatedAt,
230
- evidenceDocumentId:a.evidenceDocumentId,
231
- evidenceVersion:a.evidenceVersion,evidenceHash:a.evidenceHash,
232
- status:'FENCED_NOT_DELIVERED',operator:'USER_DELEGATED_AGENT',
233
- at:Date.now()};
234
- const next={...current,
235
- blockers:[...blockers.filter(x=>x!=='ORPHAN_RUN_FENCED'),
236
- 'ORPHAN_RUN_FENCED'],
237
- orphanRunRecovery:orphanRef,
238
- nextStep:'Obtain a new verified R/P-authorized Owner before delivery; no old send replay.'};
239
- db.prepare(`UPDATE tasks SET writable_ref=NULL,credential_digest=NULL,
240
- writer_approval_revision=NULL,binding_revision=binding_revision+1,
241
- business_revision=business_revision+1,context_json=?
242
- WHERE project_id=? AND task_id=?`)
243
- .run(JSON.stringify(next),a.projectId,a.taskId);
244
- db.prepare("UPDATE permits SET kind='USED' WHERE project_id=? AND task_id=?")
245
- .run(a.projectId,a.taskId);
246
- db.prepare('DELETE FROM start_leases WHERE project_id=? AND task_id=?')
247
- .run(a.projectId,a.taskId);
248
- return {status:'ORPHAN_FENCED',projectId:a.projectId,taskId:a.taskId,
249
- oldRunRef:a.expectedRunRef,
250
- taskRevision:r.business_revision+1,bindingRevision:r.binding_revision+1,
251
- executionOutcome:null,deliveryRef:null};
252
- });
253
- },
254
- fence(a){
255
- internal('run.fence',a);
256
- return receipt(a,'run.fence',()=>{
257
- const r=scope(a);
258
- expectedBusiness(r,a.expectedTaskRevision);expectedBinding(r,a.expectedBindingRevision);
259
- assert(r.writable_ref===a.runRef&&!!a.runRef,'WRITER_FENCED');
260
- assert(r.writer_approval_revision===r.approval_revision,
261
- 'ACTIVE_RUN_APPROVAL_STALE');
262
- assert(!projectDrift(r).projectDrift,'PROJECT_RULES_DRIFT');
263
- ensureNoPendingIssue(a.projectId,a.taskId);
264
- assert(!r.pending_attempt,'HANDOFF_PENDING');
265
- const cp=db.prepare('SELECT * FROM checkpoints WHERE project_id=? AND task_id=? AND checkpoint_id=?')
266
- .get(a.projectId,a.taskId,a.checkpointId);
267
- assert(cp&&cp.business_revision===r.business_revision,'CHECKPOINT_STALE');
268
- assert(typeof a.attemptId==='string'&&a.attemptId.length>=8,'ATTEMPT_MISSING');
269
- const permit=db.prepare('SELECT * FROM permits WHERE project_id=? AND task_id=? AND action_token=?')
270
- .get(a.projectId,a.taskId,a.actionToken);
271
- assert(permit&&permit.kind==='HANDOFF','HANDOFF_PERMIT_REQUIRED');
272
- assert(typeof a.nextRunRef==='string'&&a.nextRunRef.length>=8,'NEXT_RUN_MISSING');
273
- assert(typeof a.nextRunCredential==='string'&&a.nextRunCredential.length===64,'NEXT_CREDENTIAL_INVALID');
274
- db.prepare(`UPDATE tasks SET writable_ref=NULL,credential_digest=NULL,
275
- writer_approval_revision=NULL,pending_attempt=?,pending_checkpoint=?,pending_target_generation=?,
276
- pending_credential_digest=?,pending_next_ref=?,binding_revision=binding_revision+1
277
- WHERE project_id=? AND task_id=?`).run(a.attemptId,a.checkpointId,a.targetGeneration,
278
- runDigest(epoch,a.projectId,a.taskId,a.nextRunRef,a.nextRunCredential),
279
- a.nextRunRef,a.projectId,a.taskId);
280
- // Retain a durable one-use tombstone, so the same token cannot be reissued.
281
- db.prepare("UPDATE permits SET kind='USED' WHERE project_id=? AND task_id=? AND action_token=?")
282
- .run(a.projectId,a.taskId,a.actionToken);
283
- return {status:'FENCED',attemptId:a.attemptId,checkpointId:a.checkpointId,
284
- taskRevision:r.business_revision,bindingRevision:r.binding_revision+1};
285
- });
286
- },
287
- bindHandoff(a){
288
- internal('run.bind.handoff',a);
289
- return receipt(a,'run.bind.handoff',()=>{
290
- assert(a.ownerCommitted===true&&a.nativeCommitted===true,'OWNER_UNVERIFIED');
291
- const r=scope(a);
292
- expectedBusiness(r,a.expectedTaskRevision);expectedBinding(r,a.expectedBindingRevision);
293
- assert(r.writable_ref===null&&r.pending_attempt===a.attemptId
294
- &&r.pending_checkpoint===a.checkpointId
295
- &&r.pending_target_generation===a.targetGeneration
296
- &&r.pending_next_ref===a.runRef,'HANDOFF_MISMATCH');
297
- assert(runDigest(epoch,a.projectId,a.taskId,a.runRef,a.runCredential)===r.pending_credential_digest,
298
- 'WRITE_CREDENTIAL_INVALID');
299
- assertPinnedHandoffCurrent(r,a.checkpointId);
300
- assert(hasApprovedPlan(parse(r.approval_json)),'APPROVAL_REVOKED');
301
- db.prepare(`UPDATE tasks SET writable_ref=?,credential_digest=pending_credential_digest,
302
- writer_approval_revision=?,
303
- pending_attempt=NULL,pending_checkpoint=NULL,pending_target_generation=NULL,
304
- pending_credential_digest=NULL,pending_next_ref=NULL,binding_revision=binding_revision+1
305
- WHERE project_id=? AND task_id=?`).run(a.runRef,r.approval_revision,
306
- a.projectId,a.taskId);
307
- return {status:'BOUND',runRef:a.runRef,bindingRevision:r.binding_revision+1,
308
- taskRevision:r.business_revision};
309
- });
310
- },
311
- fenceTerminal(a){
312
- internal('run.fence.terminal',a);
313
- return receipt(a,'run.fence.terminal',()=>{
314
- const r=scope(a);
315
- expectedBusiness(r,a.expectedTaskRevision);
316
- expectedBinding(r,a.expectedBindingRevision);
317
- assert(r.writable_ref!==null&&r.pending_attempt===null,'WRITER_FENCED');
318
- assert(r.writable_ref===a.runRef,'RUN_REF_MISMATCH');
319
- const ctx=parse(r.context_json);
320
- assert(ctx.executionOutcome==='DONE'&&ctx.deliveryRef,
321
- 'DELIVERY_NOT_COMMITTED');
322
- db.prepare(`UPDATE tasks SET writable_ref=NULL,credential_digest=NULL,
323
- writer_approval_revision=NULL,binding_revision=binding_revision+1 WHERE project_id=? AND task_id=?`)
324
- .run(a.projectId,a.taskId);
325
- // A terminal run invalidates every unused permit and old START lease.
326
- // Keep used-token tombstones so later replay cannot mint a fresh action.
327
- db.prepare("UPDATE permits SET kind='USED' WHERE project_id=? AND task_id=?")
328
- .run(a.projectId,a.taskId);
329
- db.prepare('DELETE FROM start_leases WHERE project_id=? AND task_id=?')
330
- .run(a.projectId,a.taskId);
331
- return {status:'TERMINAL_FENCED',taskRevision:r.business_revision,
332
- bindingRevision:r.binding_revision+1};
333
- });
334
- },
335
- restoreSameOwner(a){
336
- internal('run.bind.restore-same-owner',a);
337
- return receipt(a,'run.bind.restore-same-owner',()=>{
338
- // These proofs are adapter assertions in W01 only. Real Worker/Native
339
- // provenance MUST be verified by the transport before W07 enables this.
340
- assert(a.sendState==='NOT_SENT' && a.oldActionTokenRevoked===true,
341
- 'SEND_NOT_PROVEN_ABSENT');
342
- assert(a.workerRestored===true && a.nativeRestored===true &&
343
- a.ownerStillSame===true,'OWNER_UNVERIFIED');
344
- const r=scope(a);expectedBusiness(r,a.expectedTaskRevision);
345
- expectedBinding(r,a.expectedBindingRevision);
346
- assert(r.writable_ref===null && r.pending_attempt===a.attemptId
347
- && r.pending_checkpoint===a.checkpointId,'HANDOFF_MISMATCH');
348
- assertPinnedHandoffCurrent(r,a.checkpointId);
349
- assert(typeof a.recoveryCredential==='string'&&a.recoveryCredential.length===64,
350
- 'WRITE_CREDENTIAL_INVALID');
351
- assert(typeof a.recoveryActionToken==='string'&&a.recoveryActionToken.length>=8,
352
- 'ACTION_TOKEN_REQUIRED');
353
- assert(typeof a.sourceRunRef==='string'&&a.sourceRunRef.length>=8,
354
- 'RUN_REF_MISSING');
355
- const permit=db.prepare('SELECT * FROM permits WHERE project_id=? AND task_id=? AND action_token=?')
356
- .get(a.projectId,a.taskId,a.recoveryActionToken);
357
- assert(permit&&permit.kind==='CONTINUE'
358
- &&permit.approval_revision===r.approval_revision,'RECOVERY_PERMIT_MISSING');
359
- assert(hasApprovedPlan(parse(r.approval_json)),'APPROVAL_REVOKED');
360
- db.prepare(`UPDATE tasks SET writable_ref=?,credential_digest=?,
361
- writer_approval_revision=?,pending_attempt=NULL,pending_checkpoint=NULL,
362
- pending_target_generation=NULL,pending_credential_digest=NULL,
363
- pending_next_ref=NULL,binding_revision=binding_revision+1
364
- WHERE project_id=? AND task_id=?`).run(a.sourceRunRef,
365
- runDigest(epoch,a.projectId,a.taskId,a.sourceRunRef,a.recoveryCredential),
366
- r.approval_revision,a.projectId,a.taskId);
367
- // Retain a durable one-use tombstone, so the same token cannot be reissued.
368
- db.prepare("UPDATE permits SET kind='USED' WHERE project_id=? AND task_id=? AND action_token=?")
369
- .run(a.projectId,a.taskId,a.recoveryActionToken);
370
- return {status:'RESTORED_SAME_OWNER',runRef:a.sourceRunRef,
371
- bindingRevision:r.binding_revision+1,recoveryActionToken:a.recoveryActionToken};
372
- });
373
- }
374
- };
375
- }