@mogragsigma/dsh-dual-host-plugin 0.0.0-stage → 0.33.2

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 (271) hide show
  1. package/LICENSE +58 -0
  2. package/LICENSE.en.md +94 -0
  3. package/README.md +88 -2
  4. package/bundle/ASSET_NOTICE.md +19 -0
  5. package/bundle/LICENSE +58 -0
  6. package/bundle/LICENSE.en.md +94 -0
  7. package/bundle/README.md +75 -0
  8. package/bundle/THIRD_PARTY_NOTICES.md +94 -0
  9. package/bundle/a2a-dispatches.js +38 -0
  10. package/bundle/a2a-endpoint.js +112 -0
  11. package/bundle/a2a-tools.js +11 -0
  12. package/bundle/a2a-worker.js +63 -0
  13. package/bundle/approval-auth.js +5 -0
  14. package/bundle/approval-chat.js +150 -0
  15. package/bundle/approval-feishu.js +52 -0
  16. package/bundle/approval-guidance.js +68 -0
  17. package/bundle/approval-http.js +8 -0
  18. package/bundle/approval-ledger.js +142 -0
  19. package/bundle/approval-local-relay.js +86 -0
  20. package/bundle/approval-receipts.js +62 -0
  21. package/bundle/approval-reference.js +33 -0
  22. package/bundle/approval-semantic-model.js +26 -0
  23. package/bundle/approval-semantics.js +51 -0
  24. package/bundle/approval-ui/app.js +1 -0
  25. package/bundle/approval-ui/index.html +1 -0
  26. package/bundle/approval-ui/style.css +1 -0
  27. package/bundle/approval-workbench.js +45 -0
  28. package/bundle/artifact-media.js +34 -0
  29. package/bundle/bounded-text.js +44 -0
  30. package/bundle/compatibility.json +980 -0
  31. package/bundle/cordis.patch.yml +5 -0
  32. package/bundle/delivery-approval.js +133 -0
  33. package/bundle/delivery-artifacts.js +52 -0
  34. package/bundle/delivery-commit.js +125 -0
  35. package/bundle/delivery-contract.js +42 -0
  36. package/bundle/delivery-git-read.js +217 -0
  37. package/bundle/delivery-index.js +23 -0
  38. package/bundle/delivery-journal.js +207 -0
  39. package/bundle/delivery-object-plan.js +138 -0
  40. package/bundle/delivery-preview.js +153 -0
  41. package/bundle/delivery-process-receipts.js +90 -0
  42. package/bundle/delivery-process.js +46 -0
  43. package/bundle/delivery-process.py +228 -0
  44. package/bundle/delivery-push-approval.js +89 -0
  45. package/bundle/delivery-push-plan.js +98 -0
  46. package/bundle/delivery-push-recovery.js +32 -0
  47. package/bundle/delivery-push.js +83 -0
  48. package/bundle/delivery-recovery.js +143 -0
  49. package/bundle/delivery-runtime.js +182 -0
  50. package/bundle/delivery-ssh.js +64 -0
  51. package/bundle/delivery-state.js +106 -0
  52. package/bundle/development-attention.js +35 -0
  53. package/bundle/development-bridge.js +136 -0
  54. package/bundle/development-command-review.js +110 -0
  55. package/bundle/development-container.js +110 -0
  56. package/bundle/development-contract.js +17 -0
  57. package/bundle/development-files.js +101 -0
  58. package/bundle/development-gate.js +236 -0
  59. package/bundle/development-model-budget.js +48 -0
  60. package/bundle/development-peer.js +155 -0
  61. package/bundle/development-plan.js +43 -0
  62. package/bundle/development-reconciliation.js +62 -0
  63. package/bundle/development-review.js +81 -0
  64. package/bundle/development-rules/AGENTS.md +51 -0
  65. package/bundle/development-runtime.js +68 -0
  66. package/bundle/development-source.js +48 -0
  67. package/bundle/development-test-runner.js +13 -0
  68. package/bundle/development-tools.js +45 -0
  69. package/bundle/development-worker-review.js +110 -0
  70. package/bundle/development-worker.js +68 -0
  71. package/bundle/development-workspace.js +54 -0
  72. package/bundle/diagnostics.js +78 -0
  73. package/bundle/distribution-lock.json +773 -0
  74. package/bundle/drained-process.js +26 -0
  75. package/bundle/durable-inbox.js +294 -0
  76. package/bundle/feishu-policy.js +24 -0
  77. package/bundle/gateway-maintenance.js +55 -0
  78. package/bundle/global-rules/AGENTS.md +83 -0
  79. package/bundle/im-delivery.js +60 -0
  80. package/bundle/index.js +445 -0
  81. package/bundle/intervention-actions.js +135 -0
  82. package/bundle/intervention-job-control.js +28 -0
  83. package/bundle/intervention-state.js +43 -0
  84. package/bundle/job-version.js +15 -0
  85. package/bundle/legacy-feishu-adapter.js +89 -0
  86. package/bundle/link-contract.js +80 -0
  87. package/bundle/maintenance-process-identity.js +93 -0
  88. package/bundle/model-selection.js +71 -0
  89. package/bundle/native-context.js +14 -0
  90. package/bundle/native-entry-admission.js +2 -0
  91. package/bundle/native-entry.js +58 -0
  92. package/bundle/native-file-reference.js +32 -0
  93. package/bundle/native-owner-origin.js +12 -0
  94. package/bundle/native-rules/AGENTS.md +199 -0
  95. package/bundle/native-tools.js +108 -0
  96. package/bundle/notification-artifacts.js +70 -0
  97. package/bundle/notification-contract.js +29 -0
  98. package/bundle/notification-defaults.js +81 -0
  99. package/bundle/notification-discovery.js +65 -0
  100. package/bundle/notification-endpoint.js +54 -0
  101. package/bundle/notification-feishu.js +51 -0
  102. package/bundle/notification-media-endpoint.js +94 -0
  103. package/bundle/notification-review.js +27 -0
  104. package/bundle/notification-targets.js +54 -0
  105. package/bundle/onboarding-activity.js +66 -0
  106. package/bundle/onboarding-agent-hold.js +94 -0
  107. package/bundle/onboarding-boundary.js +20 -0
  108. package/bundle/onboarding-cloud-contract.js +29 -0
  109. package/bundle/onboarding-cloud-file-review.js +58 -0
  110. package/bundle/onboarding-cloud-files.js +100 -0
  111. package/bundle/onboarding-cloud-im.js +34 -0
  112. package/bundle/onboarding-cloud-model-probe.js +53 -0
  113. package/bundle/onboarding-cloud-models.js +68 -0
  114. package/bundle/onboarding-cloud-operation.js +276 -0
  115. package/bundle/onboarding-cloud-package.js +38 -0
  116. package/bundle/onboarding-cloud-repositories.js +89 -0
  117. package/bundle/onboarding-cloud-retirement.js +59 -0
  118. package/bundle/onboarding-cloud-task-runtime.js +90 -0
  119. package/bundle/onboarding-cloud-web.js +85 -0
  120. package/bundle/onboarding-cloud.js +110 -0
  121. package/bundle/onboarding-coordinated-hold.js +94 -0
  122. package/bundle/onboarding-exclusive-rename.js +16 -0
  123. package/bundle/onboarding-existing-host.js +63 -0
  124. package/bundle/onboarding-existing-setup.js +123 -0
  125. package/bundle/onboarding-file-contract.js +31 -0
  126. package/bundle/onboarding-file-review-contract.js +38 -0
  127. package/bundle/onboarding-file-transfer.js +133 -0
  128. package/bundle/onboarding-handoff-remote.js +98 -0
  129. package/bundle/onboarding-handoff.js +112 -0
  130. package/bundle/onboarding-host-stop-observation.js +44 -0
  131. package/bundle/onboarding-host.js +254 -0
  132. package/bundle/onboarding-identity.js +9 -0
  133. package/bundle/onboarding-im-maintenance-native.js +30 -0
  134. package/bundle/onboarding-im-maintenance.js +70 -0
  135. package/bundle/onboarding-im.js +31 -0
  136. package/bundle/onboarding-inspection-guard.js +15 -0
  137. package/bundle/onboarding-install-fs.js +58 -0
  138. package/bundle/onboarding-installer.js +157 -0
  139. package/bundle/onboarding-local-pair-native.js +21 -0
  140. package/bundle/onboarding-local-pair.js +99 -0
  141. package/bundle/onboarding-maintenance-fs.js +69 -0
  142. package/bundle/onboarding-maintenance-plan.js +213 -0
  143. package/bundle/onboarding-maintenance-record.js +169 -0
  144. package/bundle/onboarding-maintenance.js +163 -0
  145. package/bundle/onboarding-model-probe.js +119 -0
  146. package/bundle/onboarding-models.js +107 -0
  147. package/bundle/onboarding-native-host.js +101 -0
  148. package/bundle/onboarding-native-probe.js +56 -0
  149. package/bundle/onboarding-native-runner.js +29 -0
  150. package/bundle/onboarding-node-bootstrap.py +678 -0
  151. package/bundle/onboarding-node.js +77 -0
  152. package/bundle/onboarding-notification-defaults.js +93 -0
  153. package/bundle/onboarding-package-source.js +33 -0
  154. package/bundle/onboarding-paths.js +58 -0
  155. package/bundle/onboarding-peer-binding.js +61 -0
  156. package/bundle/onboarding-peer-contract.js +12 -0
  157. package/bundle/onboarding-plugin-repair.js +116 -0
  158. package/bundle/onboarding-plugin-version.js +143 -0
  159. package/bundle/onboarding-remote-control.js +140 -0
  160. package/bundle/onboarding-remote-probe.js +40 -0
  161. package/bundle/onboarding-repositories.js +88 -0
  162. package/bundle/onboarding-repository.js +49 -0
  163. package/bundle/onboarding-runtime-activation.js +83 -0
  164. package/bundle/onboarding-runtime-artifacts-client.js +87 -0
  165. package/bundle/onboarding-runtime-artifacts.js +126 -0
  166. package/bundle/onboarding-runtime-extension.js +400 -0
  167. package/bundle/onboarding-runtime-inventory.js +88 -0
  168. package/bundle/onboarding-runtime-program.js +42 -0
  169. package/bundle/onboarding-runtime-reclaim-client.js +94 -0
  170. package/bundle/onboarding-runtime-reclaim-worker.js +63 -0
  171. package/bundle/onboarding-runtime-reclaim.js +132 -0
  172. package/bundle/onboarding-runtime-repair-client.js +50 -0
  173. package/bundle/onboarding-runtime-repair.js +88 -0
  174. package/bundle/onboarding-runtime-stop.js +90 -0
  175. package/bundle/onboarding-runtime-uninstall-client.js +88 -0
  176. package/bundle/onboarding-runtime-uninstall.js +167 -0
  177. package/bundle/onboarding-runtime-version-client.js +138 -0
  178. package/bundle/onboarding-runtime-version.js +145 -0
  179. package/bundle/onboarding-schema.js +42 -0
  180. package/bundle/onboarding-self-disable.js +3 -0
  181. package/bundle/onboarding-self-lifecycle.js +95 -0
  182. package/bundle/onboarding-self-maintenance.js +52 -0
  183. package/bundle/onboarding-self-remove.js +3 -0
  184. package/bundle/onboarding-self-stop.js +85 -0
  185. package/bundle/onboarding-ssh-forward.js +48 -0
  186. package/bundle/onboarding-ssh.js +105 -0
  187. package/bundle/onboarding-state.js +95 -0
  188. package/bundle/onboarding-task-runtime-contract.js +38 -0
  189. package/bundle/onboarding-task-runtime-native.js +80 -0
  190. package/bundle/onboarding-task-runtime.js +66 -0
  191. package/bundle/onboarding-token-plan.js +52 -0
  192. package/bundle/onboarding-version-activation-client.js +48 -0
  193. package/bundle/onboarding-version-candidate.js +96 -0
  194. package/bundle/onboarding-version-repair.js +128 -0
  195. package/bundle/package-lock.json +773 -0
  196. package/bundle/package.json +99 -0
  197. package/bundle/peer-file-fs.js +62 -0
  198. package/bundle/peer-file-migration.js +59 -0
  199. package/bundle/peer-file-review.js +107 -0
  200. package/bundle/peer-files.js +157 -0
  201. package/bundle/peer-mailbox.js +75 -0
  202. package/bundle/protocol.js +17 -0
  203. package/bundle/recovery.js +40 -0
  204. package/bundle/repository-catalog.js +85 -0
  205. package/bundle/repository-gitee.js +76 -0
  206. package/bundle/repository-managed.js +46 -0
  207. package/bundle/repository-writer.js +43 -0
  208. package/bundle/rules/AGENTS.md +22 -0
  209. package/bundle/runtime-im-lock.json +11577 -0
  210. package/bundle/runtime-im-package.json +18 -0
  211. package/bundle/runtime-lock.json +10152 -0
  212. package/bundle/runtime-package.json +17 -0
  213. package/bundle/scheduler-migration.js +45 -0
  214. package/bundle/search-contract.js +19 -0
  215. package/bundle/skills/dsh-dual-end/SKILL.md +12 -0
  216. package/bundle/skills/task-a2a/SKILL.md +55 -0
  217. package/bundle/skills/task-a2a-worker/SKILL.md +23 -0
  218. package/bundle/skills/task-approval/SKILL.md +128 -0
  219. package/bundle/skills/task-artifacts/SKILL.md +46 -0
  220. package/bundle/skills/task-development/SKILL.md +144 -0
  221. package/bundle/skills/task-diagnostics/SKILL.md +41 -0
  222. package/bundle/skills/task-direct-link/SKILL.md +143 -0
  223. package/bundle/skills/task-file-transfer/SKILL.md +107 -0
  224. package/bundle/skills/task-handoff/SKILL.md +23 -0
  225. package/bundle/skills/task-intake/SKILL.md +13 -0
  226. package/bundle/skills/task-notify/SKILL.md +102 -0
  227. package/bundle/skills/task-onboarding/SKILL.md +708 -0
  228. package/bundle/skills/task-recovery/SKILL.md +54 -0
  229. package/bundle/skills/task-repository/SKILL.md +145 -0
  230. package/bundle/skills/task-research/SKILL.md +43 -0
  231. package/bundle/skills/task-schedule/SKILL.md +79 -0
  232. package/bundle/skills/task-sidebar/SKILL.md +122 -0
  233. package/bundle/skills/task-workbench/SKILL.md +30 -0
  234. package/bundle/skills/task-worker/SKILL.md +88 -0
  235. package/bundle/source-search.js +89 -0
  236. package/bundle/source-web.js +17 -0
  237. package/bundle/ssh-connector.js +24 -0
  238. package/bundle/store.js +102 -0
  239. package/bundle/task-a2a.js +298 -0
  240. package/bundle/task-artifacts.js +179 -0
  241. package/bundle/task-board-a2a.js +55 -0
  242. package/bundle/task-board-development.js +83 -0
  243. package/bundle/task-board-planning.js +84 -0
  244. package/bundle/task-board.js +132 -0
  245. package/bundle/task-branch.js +2 -0
  246. package/bundle/task-catalog.js +77 -0
  247. package/bundle/task-context.js +40 -0
  248. package/bundle/task-envelope.js +96 -0
  249. package/bundle/task-interventions.js +54 -0
  250. package/bundle/task-jobs.js +156 -0
  251. package/bundle/task-notifications.js +132 -0
  252. package/bundle/task-results.js +16 -0
  253. package/bundle/task-scheduler.js +77 -0
  254. package/bundle/task-sources.js +101 -0
  255. package/bundle/task-trigger-time.js +46 -0
  256. package/bundle/task-triggers.js +157 -0
  257. package/bundle/task-worker.js +73 -0
  258. package/bundle/task-workflows.js +117 -0
  259. package/bundle/tools.js +13 -0
  260. package/bundle/workbench-assets/asset-provenance.json +26 -0
  261. package/bundle/workbench-assets/whale-ui-states.png +0 -0
  262. package/bundle/workbench-client.js +811 -0
  263. package/bundle/workbench-decisions.js +48 -0
  264. package/bundle/workbench-host.js +122 -0
  265. package/bundle/workbench-im-identity.js +51 -0
  266. package/bundle/workbench-proxy.js +127 -0
  267. package/bundle/worker-rules/AGENTS.md +84 -0
  268. package/bundle/worker-tools.js +13 -0
  269. package/compatibility.json +980 -0
  270. package/cordis.patch.yml +3 -0
  271. package/package.json +57 -4
@@ -0,0 +1,87 @@
1
+ // Local operator ledger. Reopening reads history only; only explicit actions use SSH.
2
+ import {createHash} from 'node:crypto';
3
+ import {credentialKey} from '@deepseek-ai/dsh-credentials';
4
+ import {runtimeProgram,runtimeCommand} from './onboarding-runtime-program.js';
5
+ const hash=x=>createHash('sha256').update(JSON.stringify(x)).digest('hex');
6
+ const fail=c=>{throw Error('ONBOARDING_CLOUD_ARTIFACTS_'+c);};
7
+ const exact=(v,k)=>v&&typeof v==='object'&&!Array.isArray(v)&&Object.keys(v).sort().join()===k.slice().sort().join();
8
+ const uuid=x=>typeof x==='string'&&/^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/.test(x);
9
+ const hex=x=>typeof x==='string'&&/^[a-f0-9]{64}$/.test(x),version=x=>typeof x==='string'&&/^\d+\.\d+\.\d+(?:-[A-Za-z0-9.-]+)?$/.test(x)&&x.length<100;
10
+ const terminal=s=>['committed','rolled_back','refused'].includes(s),scope='stopped_owned_rules_and_skills_only';
11
+ export const artifactsRecordKey=id=>credentialKey('dsh-task-gateway','onboarding-runtime-artifacts-v1-'+id);
12
+ export function artifactsPending(d){const last=new Map((d?.requests??[]).filter(r=>r.receipt?.state!=='refused').map(r=>[r.targetId,r]));return [...last.values()].some(r=>r.state==='requested'||!terminal(r.receipt?.state));}
13
+ export function parseArtifactsRequest(record,operationId){
14
+ if(!record)return null;const d=record.payload;
15
+ if(record.kind!=='grant'||!exact(d,['schema','operationId','runtimeId','binding','revision','plans','requests'])||d.schema!==1||d.operationId!==operationId||!uuid(d.runtimeId)||!hex(d.binding)||!Number.isSafeInteger(d.revision)||d.revision<1||!Array.isArray(d.plans)||d.plans.length>128||!Array.isArray(d.requests)||d.requests.length>128||Buffer.byteLength(JSON.stringify(d))>262144)fail('LOCAL_LEDGER_REVIEW');
16
+ const ids=new Set();for(const p of d.plans){if(!exact(p,['id','kind','targetId','fingerprint','expiresAt','version'])||!uuid(p.id)||ids.has(p.id)||!['apply','recover'].includes(p.kind)||!uuid(p.targetId)||!hex(p.fingerprint)||!Number.isSafeInteger(p.expiresAt)||!version(p.version))fail('LOCAL_LEDGER_REVIEW');ids.add(p.id);}
17
+ for(const p of d.plans)if(p.kind==='apply'?p.id!==p.targetId:!d.plans.some(x=>x.kind==='apply'&&x.id===p.targetId&&x.version===p.version))fail('LOCAL_LEDGER_REVIEW');
18
+ const requests=new Set();for(const r of d.requests){
19
+ const p=d.plans.find(p=>p.id===r?.planId&&p.kind===r?.action);
20
+ if(!exact(r,['key','action','planId','targetId','fingerprint','state','receipt'])||!p||r.key!==r.action+':'+r.planId||requests.has(r.key)||r.targetId!==p.targetId||r.fingerprint!==p.fingerprint||!['requested','observed'].includes(r.state)||r.state==='requested'&&r.receipt!==null)fail('LOCAL_LEDGER_REVIEW');requests.add(r.key);
21
+ if(r.state==='observed'){if(!exact(r.receipt,['contract','operationId','runtimeId','id','version','state','recoveryRequired','scope','hostStarted','activationVerified','initialization','pluginBinaryChanged','credentialsChanged']))fail('LOCAL_LEDGER_REVIEW');try{checkReceipt(r.receipt,d,p.targetId,p.version);}catch{fail('LOCAL_LEDGER_REVIEW');}}
22
+ }return d;
23
+ }
24
+ function checkReceipt(v,d,id,ver){
25
+ if(!v||v.operationId!==d.operationId||v.runtimeId!==d.runtimeId||v.contract!==1||v.id!==id||v.version!==ver||v.scope!==scope||!['requested','needs_review','committed','rolled_back','refused'].includes(v.state)||v.recoveryRequired!==!terminal(v.state)||v.hostStarted!==false||v.activationVerified!==false||v.initialization!==false||v.pluginBinaryChanged!==false||v.credentialsChanged!==false)fail('RESPONSE_REVIEW');
26
+ }
27
+ export async function runtimeArtifactsClient({args,p,a,b,runtimeId,credentials,ssh,now=Date.now}){
28
+ const action=args?.action,fields={rules_record:[],rules_preview:[],rules_apply:['id','fingerprint','confirm'],rules_status:['id'],rules_recover_preview:['id'],rules_recover:['id','fingerprint','confirm']};
29
+ const mutation=['rules_apply','rules_recover'].includes(action),kind=action?.slice(6);
30
+ if(!Object.hasOwn(fields,action)||!exact(args,['action','operationId',...fields[action]])||args.operationId!==p.id||Object.hasOwn(args,'id')&&!uuid(args.id)||mutation&&(args.confirm!==true||!hex(args.fingerprint)))fail('CONFIRMATION_REQUIRED');
31
+ const key=artifactsRecordKey(p.id),c=credentials(),binding=hash({operationId:p.id,archiveHash:p.archiveHash,bindingFingerprint:p.bindingFingerprint});
32
+ let ledger=parseArtifactsRequest(await c.readRecord(key),p.id);
33
+ if(ledger&&(ledger.binding!==binding||runtimeId&&ledger.runtimeId!==runtimeId))fail('BINDING_CHANGED');
34
+ const decode=n=>{const f=a.files.find(x=>x.path==='plugin/'+n);if(!f)fail('ARCHIVE_REVIEW');return Buffer.from(f.data,'base64').toString();};
35
+ const compat=JSON.parse(decode('compatibility.json')),supported=compat.onboardingPluginVersion===1&&compat.onboardingPluginVersionRequests===1&&compat.onboardingPluginVersionArtifacts===1;
36
+ const cached=r=>({...r.receipt??{contract:1,id:r.targetId,state:'artifacts_outcome_unknown',recoveryRequired:true,scope,operationId:p.id,runtimeId},cached:true,historical:true,notResent:true,requestState:r.state,activationVerified:false,hostStarted:false,initialization:false});
37
+ if(action==='rules_record')return {supported,operationId:p.id,runtimeId:ledger?.runtimeId??runtimeId??null,cached:true,historical:true,pending:artifactsPending(ledger),plans:ledger?.plans??[],requests:ledger?.requests.map(r=>({...r,receipt:r.receipt?{...r.receipt,cached:true,historical:true}:null}))??[],initialization:false,hostStarted:false,activationVerified:false};
38
+ if(!supported)fail('UNSUPPORTED');if(!uuid(runtimeId))fail('RUNTIME_RECORD_REQUIRED');
39
+ const empty={schema:1,operationId:p.id,runtimeId,binding,revision:0,plans:[],requests:[]},current=()=>ledger??empty;
40
+ async function save(next){const old=ledger,value={...next,revision:(ledger?.revision??0)+1};parseArtifactsRequest({kind:'grant',payload:value},p.id);await c.modifyRecord(key,r=>{if(JSON.stringify(parseArtifactsRequest(r,p.id))!==JSON.stringify(old))fail('LOCAL_CONFLICT');return {kind:'grant',payload:value};});ledger=value;}
41
+ const previous=mutation?current().requests.find(r=>r.key===kind+':'+args.id):null;
42
+ if(previous){if(previous.fingerprint!==args.fingerprint)fail('PLAN_DRIFT');return cached(previous);}
43
+ const last=id=>current().requests.filter(r=>r.targetId===id&&r.receipt?.state!=='refused').at(-1);
44
+ if(artifactsPending(ledger)&&!['rules_status','rules_recover_preview','rules_recover'].includes(action))fail('RECOVERY_REQUIRED');
45
+ if(['rules_recover_preview','rules_recover'].includes(action)&&current().requests.some(r=>r.state==='requested'))fail('OUTCOME_UNKNOWN');
46
+ let target=args.id,plan,remoteArgs={};
47
+ if(mutation){
48
+ plan=current().plans.find(x=>x.id===args.id&&x.kind===kind);
49
+ if(!plan||plan.fingerprint!==args.fingerprint||plan.expiresAt<=now())fail('PLAN_EXPIRED');target=plan.targetId;
50
+ if(kind==='recover'&&last(target)?.receipt?.recoveryRequired!==true)fail('RECOVERY_REQUIRED');
51
+ remoteArgs={id:plan.id,fingerprint:plan.fingerprint,confirm:true};
52
+ }else if(action!=='rules_preview'){
53
+ if(!current().plans.some(x=>x.kind==='apply'&&x.id===args.id))fail('TRANSACTION_UNKNOWN');remoteArgs={id:args.id};
54
+ if(action==='rules_recover_preview'&&last(args.id)?.receipt?.recoveryRequired!==true)fail('RECOVERY_REQUIRED');
55
+ }
56
+ const input=JSON.stringify({action:'runtime_version',root:p.root,operationId:p.id,installationId:p.installationId,proof:p.proof,planHash:p.planHash,versionAction:action==='rules_recover_preview'?'rules_recoveryPreview':action,args:remoteArgs});
57
+ const command=runtimeCommand(a.baseline.node.path,runtimeProgram(decode('onboarding-remote-control.js'),decode('onboarding-runtime-extension.js')),'runtimeVersionCLI');
58
+ let request;if(mutation){if(current().requests.length>=128)fail('HISTORY_FULL');request={key:kind+':'+args.id,action:kind,planId:args.id,targetId:target,fingerprint:args.fingerprint,state:'requested',receipt:null};await save({...current(),requests:[...current().requests,request]});}
59
+ let result;try{result=JSON.parse(await ssh.execute(b,command,input,{timeoutMs:120000}));}catch{fail('OUTCOME_UNKNOWN');}
60
+ if(result?.ok!==true){
61
+ if(request&&['ONBOARDING_CLOUD_RUNTIME_VERSION_PREFLIGHT_REFUSED','ONBOARDING_CLOUD_RUNTIME_ARTIFACTS_PLAN_DRIFT','ONBOARDING_CLOUD_RUNTIME_ARTIFACTS_CONFIRMATION_REQUIRED'].includes(result?.error)){
62
+ const receipt={contract:1,operationId:p.id,runtimeId,id:target,version:plan.version,state:'refused',recoveryRequired:false,scope,hostStarted:false,activationVerified:false,initialization:false,pluginBinaryChanged:false,credentialsChanged:false};
63
+ await save({...current(),requests:current().requests.map(r=>r.key===request.key?{...r,state:'observed',receipt}:r)});
64
+ }
65
+ throw Error(typeof result?.error==='string'&&/^ONBOARDING_CLOUD_[A-Z_]{1,120}$/.test(result.error)?result.error:'ONBOARDING_CLOUD_ARTIFACTS_RESPONSE_REVIEW');
66
+ }
67
+ const v=result.value;
68
+ if(!v||v.operationId!==p.id||v.runtimeId!==runtimeId||v.scope!==scope||v.contract!==1||v.initialization!==false||v.hostStarted!==false||v.activationVerified!==false||v.pluginBinaryChanged!==false||v.credentialsChanged!==false)fail('RESPONSE_REVIEW');
69
+ if(['rules_preview','rules_recover_preview'].includes(action)){
70
+ const recover=action==='rules_recover_preview',ver=recover?current().plans.find(x=>x.kind==='apply'&&x.id===args.id)?.version:v.targetVersion;
71
+ if(!recover&&(typeof v.canApply!=='boolean'||typeof v.planReady!=='boolean'||!Array.isArray(v.conflicts)||!Array.isArray(v.changes)||v.conflicts.length>256||v.changes.length>65))fail('RESPONSE_REVIEW');
72
+ if(!recover&&!v.canApply){if(v.id!==null||v.fingerprint!==null||v.expiresAt!==null)fail('RESPONSE_REVIEW');return {...v,cached:false};}
73
+ if(!uuid(v.id)||!hex(v.fingerprint)||!Number.isSafeInteger(v.expiresAt)||v.expiresAt<=now()||v.expiresAt>now()+301000||!version(ver)||recover&&(v.transactionId!==args.id||!['finalize_receipt','rollback_incomplete_changes'].includes(v.action))||!recover&&(v.planReady!==true||v.conflicts.length))fail('RESPONSE_REVIEW');
74
+ if(current().plans.length>=128||current().plans.some(p=>p.id===v.id))fail('HISTORY_FULL');
75
+ const next={id:v.id,kind:recover?'recover':'apply',targetId:recover?args.id:v.id,fingerprint:v.fingerprint,expiresAt:v.expiresAt,version:ver};await save({...current(),plans:[...current().plans,next]});return {...v,cached:false};
76
+ }
77
+ const base=current().plans.find(p=>p.kind==='apply'&&p.id===target);checkReceipt(v,current(),target,base?.version);
78
+ // Whitelist persisted receipt fields: no target-supplied free text, user bytes or credentials.
79
+ const receipt={contract:1,operationId:p.id,runtimeId,id:target,version:v.version,state:v.state,recoveryRequired:v.recoveryRequired,scope,hostStarted:false,activationVerified:false,initialization:false,pluginBinaryChanged:false,credentialsChanged:false};
80
+ const relevant=request??last(target);
81
+ if(relevant){
82
+ // An in-progress observation cannot settle whether our recovery was delivered.
83
+ if(relevant.state==='requested'&&relevant.action==='recover'&&!terminal(receipt.state)&&request!==relevant)return {...cached(relevant),observedState:receipt.state};
84
+ await save({...current(),requests:current().requests.map(r=>r.key===relevant.key?{...r,state:'observed',receipt}:r)});
85
+ }
86
+ return {...receipt,cached:false,historical:action==='rules_status'};
87
+ }
@@ -0,0 +1,126 @@
1
+ // Executed from the original archive, never from the candidate. Candidate rules
2
+ // and Skills are data for the existing protective installer/maintenance engine.
3
+ import {randomUUID} from 'node:crypto';
4
+ import {createOnboardingMaintenance} from './onboarding-maintenance.js';
5
+ import {createMaintenancePlanner} from './onboarding-maintenance-plan.js';
6
+ import {snapshot,bytes} from './onboarding-install-fs.js';
7
+ const uuid=x=>typeof x==='string'&&/^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/.test(x);
8
+ const hex=x=>typeof x==='string'&&/^[a-f0-9]{64}$/.test(x);
9
+ const exact=(x,k)=>x&&typeof x==='object'&&!Array.isArray(x)&&Object.keys(x).sort().join()===k.slice().sort().join();
10
+ export function createRuntimeArtifacts(api,q,j,{original,effective,guard}){
11
+ const {path,hash,fail,regular,signed,verified,atomicJSON}=api,now=api.versionNow??Date.now;
12
+ const base=path.join(q.root,'runtime'),home=path.join(base,'native-home'),dsh=path.join(home,'dsh'),agents=path.join(home,'agents');
13
+ const state=path.join(dsh,'dual-host-onboarding'),control=path.join(q.root,'.control/runtime');
14
+ const ledgerFile=path.join(control,'plugin-artifacts.json'),planFile=path.join(control,'plugin-artifacts-plan.json');
15
+ const binding=hash({operationId:q.operationId,runtimeId:j.id,inventoryHash:j.inventoryHash,installationId:j.rules?.transactionId??null});
16
+ const env={HOME:home,DSH_HOME:dsh,DSH_AGENTS_HOME:agents,DSH_ONBOARDING_ISOLATED_ROOT:base};
17
+ const config={onboarding:{role:'cloud',installation:{enabled:true,isolatedRoot:base}}};
18
+ const options={env,home,source:path.join(base,'plugin'),maintenanceHook:api.artifactsHook};
19
+ const service=()=>createOnboardingMaintenance(config,options),planner=()=>createMaintenancePlanner(config,options);
20
+ const terminal=r=>['committed','rolled_back'].includes(r.state);
21
+ const view=r=>({contract:1,scope:'stopped_owned_rules_and_skills_only',operationId:q.operationId,runtimeId:j.id,id:r?.id??null,
22
+ state:r?.state??'not_requested',version:r?.version??null,recoveryRequired:!!r&&!terminal(r),historical:true,
23
+ hostStarted:false,activationVerified:false,initialization:false,pluginBinaryChanged:false,credentialsChanged:false});
24
+ async function json(file){const f=await regular(file,2097152);if(!f)return null;if(f.identity.uid!==process.getuid()||f.identity.mode!==0o600)fail('RUNTIME_ARTIFACTS_RECORD_REVIEW');return verified(JSON.parse(f.data),q.proof);}
25
+ async function save(file,d){await atomicJSON(file,signed(d,q.proof));}
26
+ async function read(){
27
+ const d=await json(ledgerFile);if(!d)return {schema:1,binding,records:[]};
28
+ if(!exact(d,['schema','binding','records'])||d.schema!==1||d.binding!==binding||!Array.isArray(d.records)||d.records.length>128)fail('RUNTIME_ARTIFACTS_RECORD_REVIEW');
29
+ const seen=new Set();for(const r of d.records){
30
+ if(!exact(r,['id','maintenanceId','fingerprint','innerFingerprint','imageHash','version','beforeHash','beforeContentHash','afterHash','state','updatedAt'])||!uuid(r.id)||!uuid(r.maintenanceId)||seen.has(r.id)||![r.fingerprint,r.innerFingerprint,r.imageHash,r.beforeHash,r.beforeContentHash].every(hex)||r.afterHash!==null&&!hex(r.afterHash)||typeof r.version!=='string'||r.version.length>100||!['requested','committed','rolled_back','needs_review'].includes(r.state)||!Number.isSafeInteger(r.updatedAt)||terminal(r)&&!hex(r.afterHash))fail('RUNTIME_ARTIFACTS_RECORD_REVIEW');seen.add(r.id);
31
+ }return d;
32
+ }
33
+ function paths(){
34
+ // A candidate/user cannot choose a different HOME through an RPC argument.
35
+ if(j.nativeReceipt?.dshHome!==dsh||j.nativeReceipt?.agentsHome!==agents||j.nativeReceipt?.workspace!==path.join(base,'workspace')||!uuid(j.rules?.transactionId))fail('RUNTIME_ARTIFACTS_PATH_REVIEW');
36
+ }
37
+ async function metadata(){
38
+ paths();const installed=await snapshot(path.join(state,'installed.json'),2097152),latest=await snapshot(path.join(state,'latest.json'),2097152);
39
+ if(installed.missing||latest.missing)fail('RUNTIME_ARTIFACTS_OWNERSHIP_REVIEW');
40
+ const m=JSON.parse(bytes(installed));if(![1,2].includes(m.schema)||!uuid(m.id))fail('RUNTIME_ARTIFACTS_OWNERSHIP_REVIEW');
41
+ const name=path.join(state,m.schema===1?'transactions':'maintenance-transactions',m.id,'journal.json'),journal=await snapshot(name,2097152);
42
+ if(journal.missing)fail('RUNTIME_ARTIFACTS_OWNERSHIP_REVIEW');
43
+ return {hash:hash({installed,latest,journal}),contentHash:hash([installed,latest,journal].map(s=>({sha256:s.sha256,mode:s.mode}))),rows:[[path.join(state,'installed.json'),installed],[path.join(state,'latest.json'),latest],[name,journal]]};
44
+ }
45
+ async function owned(d){
46
+ const m=await metadata(),last=d.records.at(-1),restored=await api.uninstallRestored?.();
47
+ if(d.records.some(r=>!terminal(r)))fail('RUNTIME_ARTIFACTS_RECOVERY_REQUIRED');
48
+ // Only a signed, rolled-back uninstall may update the metadata inode binding.
49
+ // Once the artifacts ledger changes this bridge no longer applies.
50
+ if(restored&&restored.artifactLedgerHash===hash(d)&&restored.metadataHash===m.hash)return m;
51
+ if(last){if(!terminal(last))fail('RUNTIME_ARTIFACTS_RECOVERY_REQUIRED');if(m.hash!==last.afterHash)fail('RUNTIME_ARTIFACTS_OWNERSHIP_REVIEW');}
52
+ else for(const [name,s] of m.rows){const row=original.items.find(x=>x.path===path.relative(q.root,name));if(!row||row.kind!=='file'||row.sha256!==s.sha256||row.bytes!==s.size||row.dev!==s.dev||row.ino!==s.ino||row.mode!==s.mode)fail('RUNTIME_ARTIFACTS_OWNERSHIP_REVIEW');}
53
+ return m;
54
+ }
55
+ async function content(){
56
+ const image=await effective(),p=await planner().prepare('upgrade');
57
+ if(p.view.targetVersion!==image.version)fail('RUNTIME_ARTIFACTS_SOURCE_REVIEW');
58
+ return {image,imageHash:hash(image),plan:p};
59
+ }
60
+ async function noPending(){if((await read()).records.some(r=>!terminal(r)))fail('RUNTIME_ARTIFACTS_RECOVERY_REQUIRED');}
61
+ async function ready(){
62
+ try{const d=await read();await owned(d);const c=await content();return c.plan.view.planReady&&c.plan.operations.length===0&&c.plan.view.installedVersion===c.image.version;}
63
+ catch{return false;}
64
+ }
65
+ function publicPlan(p,v){return {...v,id:p.id,fingerprint:p.fingerprint,expiresAt:p.expiresAt,contract:1,scope:'stopped_owned_rules_and_skills_only',operationId:q.operationId,runtimeId:j.id,hostStarted:false,activationVerified:false,initialization:false,pluginBinaryChanged:false,credentialsChanged:false};}
66
+ async function preview(){
67
+ await guard();const d=await read(),m=await owned(d),c=await content();
68
+ const v=c.plan.view;if(!v.planReady)return {...publicPlan({id:null,fingerprint:null,expiresAt:null},v),canApply:false};
69
+ if(!c.plan.operations.length&&v.installedVersion===c.image.version)return {...publicPlan({id:null,fingerprint:null,expiresAt:null},v),canApply:false,artifactsMatched:true};
70
+ const p={schema:1,binding,id:randomUUID(),kind:'upgrade',targetId:null,imageHash:c.imageHash,version:c.image.version,beforeHash:m.hash,innerFingerprint:v.fingerprint,expiresAt:now()+300000};p.fingerprint=hash(p);await save(planFile,p);
71
+ return publicPlan(p,{...v,canApply:true,artifactsMatched:false});
72
+ }
73
+ async function plan(args,kind){
74
+ if(!exact(args,['id','fingerprint','confirm'])||args.confirm!==true||!uuid(args.id)||!hex(args.fingerprint))fail('RUNTIME_ARTIFACTS_CONFIRMATION_REQUIRED');
75
+ const p=await json(planFile);if(!p||p.schema!==1||p.binding!==binding||p.kind!==kind||p.id!==args.id||p.fingerprint!==args.fingerprint||p.expiresAt<=now())fail('RUNTIME_ARTIFACTS_PLAN_DRIFT');
76
+ const {fingerprint,...body}=p;if(hash(body)!==fingerprint)fail('RUNTIME_ARTIFACTS_PLAN_DRIFT');return p;
77
+ }
78
+ async function innerJournal(r){
79
+ const f=await snapshot(path.join(state,'maintenance-transactions',r.maintenanceId,'journal.json'),2097152);
80
+ if(f.missing)fail('RUNTIME_ARTIFACTS_JOURNAL_REVIEW');const x=JSON.parse(bytes(f));
81
+ if(x.id!==r.maintenanceId||x.fingerprint!==r.innerFingerprint||x.schema!==2||x.action!=='upgrade'||x.afterManifest?.version!==r.version||x.beforeManifest?.missing||x.beforeLatest?.missing)fail('RUNTIME_ARTIFACTS_JOURNAL_REVIEW');
82
+ const old=JSON.parse(bytes(x.beforeManifest));if(![1,2].includes(old.schema)||!uuid(old.id))fail('RUNTIME_ARTIFACTS_JOURNAL_REVIEW');
83
+ const prior=await snapshot(path.join(state,old.schema===1?'transactions':'maintenance-transactions',old.id,'journal.json'),2097152);
84
+ if(prior.missing||hash({installed:x.beforeManifest,latest:x.beforeLatest,journal:prior})!==r.beforeHash)fail('RUNTIME_ARTIFACTS_JOURNAL_REVIEW');
85
+ }
86
+ async function settle(d,r){
87
+ await innerJournal(r);
88
+ const s=await service().status({id:r.maintenanceId});
89
+ if(s.state==='committed'){
90
+ const current=await service().status();if(current.canRecover||current.state!=='inspected')return {...view(r),state:'needs_review',recoveryRequired:true};
91
+ const c=await content();if(c.imageHash!==r.imageHash||!c.plan.view.planReady||c.plan.operations.length||c.plan.view.installedVersion!==r.version)fail('RUNTIME_ARTIFACTS_CONTENT_REVIEW');
92
+ }else if(s.state!=='rolled_back')return {...view(r),state:'needs_review',recoveryRequired:true};
93
+ const m=await metadata();if(s.state==='rolled_back'&&m.contentHash!==r.beforeContentHash)fail('RUNTIME_ARTIFACTS_RESTORE_REVIEW');
94
+ r.state=s.state;r.afterHash=m.hash;r.updatedAt=now();await save(ledgerFile,d);return {...view(r),historical:false};
95
+ }
96
+ async function apply(args){
97
+ await guard();const d=await read(),prior=d.records.find(r=>r.id===args.id);
98
+ if(prior){if(args.confirm!==true||args.fingerprint!==prior.fingerprint)fail('RUNTIME_ARTIFACTS_CONFIRMATION_REQUIRED');return {...view(prior),notResent:true};}
99
+ const p=await plan(args,'upgrade'),m=await owned(d),c=await content();
100
+ if(d.records.length>=128)fail('RUNTIME_ARTIFACTS_HISTORY_FULL');
101
+ if(c.imageHash!==p.imageHash||m.hash!==p.beforeHash||c.plan.view.fingerprint!==p.innerFingerprint||!c.plan.view.planReady)fail('RUNTIME_ARTIFACTS_PLAN_DRIFT');
102
+ const svc=service(),inner=await svc.preview({action:'upgrade'});if(inner.fingerprint!==p.innerFingerprint||!inner.canApply)fail('RUNTIME_ARTIFACTS_PLAN_DRIFT');
103
+ const r={id:p.id,maintenanceId:inner.id,fingerprint:p.fingerprint,innerFingerprint:p.innerFingerprint,imageHash:p.imageHash,version:p.version,beforeHash:m.hash,beforeContentHash:m.contentHash,afterHash:null,state:'requested',updatedAt:now()};d.records.push(r);await save(ledgerFile,d);
104
+ await api.artifactsHook?.('intent_saved',-1,null);await guard();
105
+ await svc.apply({id:inner.id,fingerprint:inner.fingerprint,confirm:true});
106
+ await api.artifactsHook?.('maintenance_returned',-1,null);return settle(d,r);
107
+ }
108
+ async function status(args){
109
+ if(!exact(args,['id'])||!uuid(args.id))fail('ARGUMENTS');const d=await read(),r=d.records.find(x=>x.id===args.id);if(!r)fail('RUNTIME_ARTIFACTS_NOT_FOUND');
110
+ if(terminal(r))return view(r);await guard();return settle(d,r);
111
+ }
112
+ async function recoveryPreview(args){
113
+ if(!exact(args,['id'])||!uuid(args.id))fail('ARGUMENTS');await guard();const d=await read(),r=d.records.at(-1);
114
+ if(!r||r.id!==args.id||terminal(r))fail('RUNTIME_ARTIFACTS_RECOVERY_REQUIRED');
115
+ await innerJournal(r);const v=await service().recoveryPreview({id:r.maintenanceId}),image=await effective();if(hash(image)!==r.imageHash)fail('RUNTIME_ARTIFACTS_SOURCE_REVIEW');
116
+ const p={schema:1,binding,id:randomUUID(),kind:'recover',targetId:r.id,imageHash:r.imageHash,version:r.version,beforeHash:hash(r),innerFingerprint:v.fingerprint,expiresAt:now()+300000};p.fingerprint=hash(p);await save(planFile,p);
117
+ return publicPlan(p,{action:v.action,state:v.state,transactionId:r.id,confirmRequired:true});
118
+ }
119
+ async function recover(args){
120
+ await guard();const p=await plan(args,'recover'),d=await read(),r=d.records.at(-1);
121
+ if(!r||r.id!==p.targetId||hash(r)!==p.beforeHash||hash(await effective())!==p.imageHash)fail('RUNTIME_ARTIFACTS_PLAN_DRIFT');
122
+ await innerJournal(r);const svc=service(),v=await svc.recoveryPreview({id:r.maintenanceId});if(v.fingerprint!==p.innerFingerprint)fail('RUNTIME_ARTIFACTS_PLAN_DRIFT');
123
+ await svc.recover({id:r.maintenanceId,fingerprint:v.fingerprint,confirm:true});return settle(d,r);
124
+ }
125
+ return {preview,apply,status,recoveryPreview,recover,ready,noPending,metadata,ownership:async()=>{const d=await read();return {metadata:await owned(d),ledgerHash:hash(d)};}};
126
+ }