@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.
- package/LICENSE +58 -0
- package/LICENSE.en.md +94 -0
- package/README.md +88 -2
- package/bundle/ASSET_NOTICE.md +19 -0
- package/bundle/LICENSE +58 -0
- package/bundle/LICENSE.en.md +94 -0
- package/bundle/README.md +75 -0
- package/bundle/THIRD_PARTY_NOTICES.md +94 -0
- package/bundle/a2a-dispatches.js +38 -0
- package/bundle/a2a-endpoint.js +112 -0
- package/bundle/a2a-tools.js +11 -0
- package/bundle/a2a-worker.js +63 -0
- package/bundle/approval-auth.js +5 -0
- package/bundle/approval-chat.js +150 -0
- package/bundle/approval-feishu.js +52 -0
- package/bundle/approval-guidance.js +68 -0
- package/bundle/approval-http.js +8 -0
- package/bundle/approval-ledger.js +142 -0
- package/bundle/approval-local-relay.js +86 -0
- package/bundle/approval-receipts.js +62 -0
- package/bundle/approval-reference.js +33 -0
- package/bundle/approval-semantic-model.js +26 -0
- package/bundle/approval-semantics.js +51 -0
- package/bundle/approval-ui/app.js +1 -0
- package/bundle/approval-ui/index.html +1 -0
- package/bundle/approval-ui/style.css +1 -0
- package/bundle/approval-workbench.js +45 -0
- package/bundle/artifact-media.js +34 -0
- package/bundle/bounded-text.js +44 -0
- package/bundle/compatibility.json +980 -0
- package/bundle/cordis.patch.yml +5 -0
- package/bundle/delivery-approval.js +133 -0
- package/bundle/delivery-artifacts.js +52 -0
- package/bundle/delivery-commit.js +125 -0
- package/bundle/delivery-contract.js +42 -0
- package/bundle/delivery-git-read.js +217 -0
- package/bundle/delivery-index.js +23 -0
- package/bundle/delivery-journal.js +207 -0
- package/bundle/delivery-object-plan.js +138 -0
- package/bundle/delivery-preview.js +153 -0
- package/bundle/delivery-process-receipts.js +90 -0
- package/bundle/delivery-process.js +46 -0
- package/bundle/delivery-process.py +228 -0
- package/bundle/delivery-push-approval.js +89 -0
- package/bundle/delivery-push-plan.js +98 -0
- package/bundle/delivery-push-recovery.js +32 -0
- package/bundle/delivery-push.js +83 -0
- package/bundle/delivery-recovery.js +143 -0
- package/bundle/delivery-runtime.js +182 -0
- package/bundle/delivery-ssh.js +64 -0
- package/bundle/delivery-state.js +106 -0
- package/bundle/development-attention.js +35 -0
- package/bundle/development-bridge.js +136 -0
- package/bundle/development-command-review.js +110 -0
- package/bundle/development-container.js +110 -0
- package/bundle/development-contract.js +17 -0
- package/bundle/development-files.js +101 -0
- package/bundle/development-gate.js +236 -0
- package/bundle/development-model-budget.js +48 -0
- package/bundle/development-peer.js +155 -0
- package/bundle/development-plan.js +43 -0
- package/bundle/development-reconciliation.js +62 -0
- package/bundle/development-review.js +81 -0
- package/bundle/development-rules/AGENTS.md +51 -0
- package/bundle/development-runtime.js +68 -0
- package/bundle/development-source.js +48 -0
- package/bundle/development-test-runner.js +13 -0
- package/bundle/development-tools.js +45 -0
- package/bundle/development-worker-review.js +110 -0
- package/bundle/development-worker.js +68 -0
- package/bundle/development-workspace.js +54 -0
- package/bundle/diagnostics.js +78 -0
- package/bundle/distribution-lock.json +773 -0
- package/bundle/drained-process.js +26 -0
- package/bundle/durable-inbox.js +294 -0
- package/bundle/feishu-policy.js +24 -0
- package/bundle/gateway-maintenance.js +55 -0
- package/bundle/global-rules/AGENTS.md +83 -0
- package/bundle/im-delivery.js +60 -0
- package/bundle/index.js +445 -0
- package/bundle/intervention-actions.js +135 -0
- package/bundle/intervention-job-control.js +28 -0
- package/bundle/intervention-state.js +43 -0
- package/bundle/job-version.js +15 -0
- package/bundle/legacy-feishu-adapter.js +89 -0
- package/bundle/link-contract.js +80 -0
- package/bundle/maintenance-process-identity.js +93 -0
- package/bundle/model-selection.js +71 -0
- package/bundle/native-context.js +14 -0
- package/bundle/native-entry-admission.js +2 -0
- package/bundle/native-entry.js +58 -0
- package/bundle/native-file-reference.js +32 -0
- package/bundle/native-owner-origin.js +12 -0
- package/bundle/native-rules/AGENTS.md +199 -0
- package/bundle/native-tools.js +108 -0
- package/bundle/notification-artifacts.js +70 -0
- package/bundle/notification-contract.js +29 -0
- package/bundle/notification-defaults.js +81 -0
- package/bundle/notification-discovery.js +65 -0
- package/bundle/notification-endpoint.js +54 -0
- package/bundle/notification-feishu.js +51 -0
- package/bundle/notification-media-endpoint.js +94 -0
- package/bundle/notification-review.js +27 -0
- package/bundle/notification-targets.js +54 -0
- package/bundle/onboarding-activity.js +66 -0
- package/bundle/onboarding-agent-hold.js +94 -0
- package/bundle/onboarding-boundary.js +20 -0
- package/bundle/onboarding-cloud-contract.js +29 -0
- package/bundle/onboarding-cloud-file-review.js +58 -0
- package/bundle/onboarding-cloud-files.js +100 -0
- package/bundle/onboarding-cloud-im.js +34 -0
- package/bundle/onboarding-cloud-model-probe.js +53 -0
- package/bundle/onboarding-cloud-models.js +68 -0
- package/bundle/onboarding-cloud-operation.js +276 -0
- package/bundle/onboarding-cloud-package.js +38 -0
- package/bundle/onboarding-cloud-repositories.js +89 -0
- package/bundle/onboarding-cloud-retirement.js +59 -0
- package/bundle/onboarding-cloud-task-runtime.js +90 -0
- package/bundle/onboarding-cloud-web.js +85 -0
- package/bundle/onboarding-cloud.js +110 -0
- package/bundle/onboarding-coordinated-hold.js +94 -0
- package/bundle/onboarding-exclusive-rename.js +16 -0
- package/bundle/onboarding-existing-host.js +63 -0
- package/bundle/onboarding-existing-setup.js +123 -0
- package/bundle/onboarding-file-contract.js +31 -0
- package/bundle/onboarding-file-review-contract.js +38 -0
- package/bundle/onboarding-file-transfer.js +133 -0
- package/bundle/onboarding-handoff-remote.js +98 -0
- package/bundle/onboarding-handoff.js +112 -0
- package/bundle/onboarding-host-stop-observation.js +44 -0
- package/bundle/onboarding-host.js +254 -0
- package/bundle/onboarding-identity.js +9 -0
- package/bundle/onboarding-im-maintenance-native.js +30 -0
- package/bundle/onboarding-im-maintenance.js +70 -0
- package/bundle/onboarding-im.js +31 -0
- package/bundle/onboarding-inspection-guard.js +15 -0
- package/bundle/onboarding-install-fs.js +58 -0
- package/bundle/onboarding-installer.js +157 -0
- package/bundle/onboarding-local-pair-native.js +21 -0
- package/bundle/onboarding-local-pair.js +99 -0
- package/bundle/onboarding-maintenance-fs.js +69 -0
- package/bundle/onboarding-maintenance-plan.js +213 -0
- package/bundle/onboarding-maintenance-record.js +169 -0
- package/bundle/onboarding-maintenance.js +163 -0
- package/bundle/onboarding-model-probe.js +119 -0
- package/bundle/onboarding-models.js +107 -0
- package/bundle/onboarding-native-host.js +101 -0
- package/bundle/onboarding-native-probe.js +56 -0
- package/bundle/onboarding-native-runner.js +29 -0
- package/bundle/onboarding-node-bootstrap.py +678 -0
- package/bundle/onboarding-node.js +77 -0
- package/bundle/onboarding-notification-defaults.js +93 -0
- package/bundle/onboarding-package-source.js +33 -0
- package/bundle/onboarding-paths.js +58 -0
- package/bundle/onboarding-peer-binding.js +61 -0
- package/bundle/onboarding-peer-contract.js +12 -0
- package/bundle/onboarding-plugin-repair.js +116 -0
- package/bundle/onboarding-plugin-version.js +143 -0
- package/bundle/onboarding-remote-control.js +140 -0
- package/bundle/onboarding-remote-probe.js +40 -0
- package/bundle/onboarding-repositories.js +88 -0
- package/bundle/onboarding-repository.js +49 -0
- package/bundle/onboarding-runtime-activation.js +83 -0
- package/bundle/onboarding-runtime-artifacts-client.js +87 -0
- package/bundle/onboarding-runtime-artifacts.js +126 -0
- package/bundle/onboarding-runtime-extension.js +400 -0
- package/bundle/onboarding-runtime-inventory.js +88 -0
- package/bundle/onboarding-runtime-program.js +42 -0
- package/bundle/onboarding-runtime-reclaim-client.js +94 -0
- package/bundle/onboarding-runtime-reclaim-worker.js +63 -0
- package/bundle/onboarding-runtime-reclaim.js +132 -0
- package/bundle/onboarding-runtime-repair-client.js +50 -0
- package/bundle/onboarding-runtime-repair.js +88 -0
- package/bundle/onboarding-runtime-stop.js +90 -0
- package/bundle/onboarding-runtime-uninstall-client.js +88 -0
- package/bundle/onboarding-runtime-uninstall.js +167 -0
- package/bundle/onboarding-runtime-version-client.js +138 -0
- package/bundle/onboarding-runtime-version.js +145 -0
- package/bundle/onboarding-schema.js +42 -0
- package/bundle/onboarding-self-disable.js +3 -0
- package/bundle/onboarding-self-lifecycle.js +95 -0
- package/bundle/onboarding-self-maintenance.js +52 -0
- package/bundle/onboarding-self-remove.js +3 -0
- package/bundle/onboarding-self-stop.js +85 -0
- package/bundle/onboarding-ssh-forward.js +48 -0
- package/bundle/onboarding-ssh.js +105 -0
- package/bundle/onboarding-state.js +95 -0
- package/bundle/onboarding-task-runtime-contract.js +38 -0
- package/bundle/onboarding-task-runtime-native.js +80 -0
- package/bundle/onboarding-task-runtime.js +66 -0
- package/bundle/onboarding-token-plan.js +52 -0
- package/bundle/onboarding-version-activation-client.js +48 -0
- package/bundle/onboarding-version-candidate.js +96 -0
- package/bundle/onboarding-version-repair.js +128 -0
- package/bundle/package-lock.json +773 -0
- package/bundle/package.json +99 -0
- package/bundle/peer-file-fs.js +62 -0
- package/bundle/peer-file-migration.js +59 -0
- package/bundle/peer-file-review.js +107 -0
- package/bundle/peer-files.js +157 -0
- package/bundle/peer-mailbox.js +75 -0
- package/bundle/protocol.js +17 -0
- package/bundle/recovery.js +40 -0
- package/bundle/repository-catalog.js +85 -0
- package/bundle/repository-gitee.js +76 -0
- package/bundle/repository-managed.js +46 -0
- package/bundle/repository-writer.js +43 -0
- package/bundle/rules/AGENTS.md +22 -0
- package/bundle/runtime-im-lock.json +11577 -0
- package/bundle/runtime-im-package.json +18 -0
- package/bundle/runtime-lock.json +10152 -0
- package/bundle/runtime-package.json +17 -0
- package/bundle/scheduler-migration.js +45 -0
- package/bundle/search-contract.js +19 -0
- package/bundle/skills/dsh-dual-end/SKILL.md +12 -0
- package/bundle/skills/task-a2a/SKILL.md +55 -0
- package/bundle/skills/task-a2a-worker/SKILL.md +23 -0
- package/bundle/skills/task-approval/SKILL.md +128 -0
- package/bundle/skills/task-artifacts/SKILL.md +46 -0
- package/bundle/skills/task-development/SKILL.md +144 -0
- package/bundle/skills/task-diagnostics/SKILL.md +41 -0
- package/bundle/skills/task-direct-link/SKILL.md +143 -0
- package/bundle/skills/task-file-transfer/SKILL.md +107 -0
- package/bundle/skills/task-handoff/SKILL.md +23 -0
- package/bundle/skills/task-intake/SKILL.md +13 -0
- package/bundle/skills/task-notify/SKILL.md +102 -0
- package/bundle/skills/task-onboarding/SKILL.md +708 -0
- package/bundle/skills/task-recovery/SKILL.md +54 -0
- package/bundle/skills/task-repository/SKILL.md +145 -0
- package/bundle/skills/task-research/SKILL.md +43 -0
- package/bundle/skills/task-schedule/SKILL.md +79 -0
- package/bundle/skills/task-sidebar/SKILL.md +122 -0
- package/bundle/skills/task-workbench/SKILL.md +30 -0
- package/bundle/skills/task-worker/SKILL.md +88 -0
- package/bundle/source-search.js +89 -0
- package/bundle/source-web.js +17 -0
- package/bundle/ssh-connector.js +24 -0
- package/bundle/store.js +102 -0
- package/bundle/task-a2a.js +298 -0
- package/bundle/task-artifacts.js +179 -0
- package/bundle/task-board-a2a.js +55 -0
- package/bundle/task-board-development.js +83 -0
- package/bundle/task-board-planning.js +84 -0
- package/bundle/task-board.js +132 -0
- package/bundle/task-branch.js +2 -0
- package/bundle/task-catalog.js +77 -0
- package/bundle/task-context.js +40 -0
- package/bundle/task-envelope.js +96 -0
- package/bundle/task-interventions.js +54 -0
- package/bundle/task-jobs.js +156 -0
- package/bundle/task-notifications.js +132 -0
- package/bundle/task-results.js +16 -0
- package/bundle/task-scheduler.js +77 -0
- package/bundle/task-sources.js +101 -0
- package/bundle/task-trigger-time.js +46 -0
- package/bundle/task-triggers.js +157 -0
- package/bundle/task-worker.js +73 -0
- package/bundle/task-workflows.js +117 -0
- package/bundle/tools.js +13 -0
- package/bundle/workbench-assets/asset-provenance.json +26 -0
- package/bundle/workbench-assets/whale-ui-states.png +0 -0
- package/bundle/workbench-client.js +811 -0
- package/bundle/workbench-decisions.js +48 -0
- package/bundle/workbench-host.js +122 -0
- package/bundle/workbench-im-identity.js +51 -0
- package/bundle/workbench-proxy.js +127 -0
- package/bundle/worker-rules/AGENTS.md +84 -0
- package/bundle/worker-tools.js +13 -0
- package/compatibility.json +980 -0
- package/cordis.patch.yml +3 -0
- package/package.json +57 -4
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import {canonical} from './approval-ledger.js';
|
|
2
|
+
|
|
3
|
+
// Failure diagnostics are not success artifacts, approval, a refund, or a resume.
|
|
4
|
+
// Store after the native worker returns/throws; never infer drain from a callback.
|
|
5
|
+
export class DevelopmentAttention {
|
|
6
|
+
constructor(runtime){
|
|
7
|
+
this.r=runtime;this.db=runtime.gate.db;
|
|
8
|
+
this.db.exec(`CREATE TABLE IF NOT EXISTS development_attention(run_id TEXT PRIMARY KEY,request_id TEXT NOT NULL,job_id TEXT NOT NULL,snapshot TEXT NOT NULL,created INTEGER NOT NULL);`);
|
|
9
|
+
}
|
|
10
|
+
record(job,error){
|
|
11
|
+
const r=this.r.requestFor(job),gate=this.r.gate;
|
|
12
|
+
if(job.version!=='0.22.0'||r.task_id!==job.task_id)throw Error('DEVELOPMENT_ATTENTION_RUN_MISMATCH');
|
|
13
|
+
const run=this.r.g.scheduler.jobs.db.prepare('SELECT job_id,claim FROM runs WHERE id=?').get(job.run_id);
|
|
14
|
+
if(!run||run.job_id!==job.id||run.claim!==job.claim)throw Error('DEVELOPMENT_ATTENTION_RUN_MISMATCH');
|
|
15
|
+
const old=this.db.prepare('SELECT snapshot FROM development_attention WHERE run_id=?').get(job.run_id);
|
|
16
|
+
if(old)return JSON.parse(old.snapshot);
|
|
17
|
+
const progress=gate.progress(r.id),model=this.r.modelBudget.view(r.id),test=r.test_result?JSON.parse(r.test_result):null;
|
|
18
|
+
const passed=test?.passed===true&&test.file_hash===r.expected_hash;
|
|
19
|
+
const exhausted=[];
|
|
20
|
+
if(progress.write_budget.calls_used>=progress.write_budget.limit||progress.write_budget.intents_used>=progress.write_budget.limit)exhausted.push('write');
|
|
21
|
+
if(progress.command_budget.used>=progress.command_budget.limit)exhausted.push('command');
|
|
22
|
+
if(model.used>=model.limit)exhausted.push('model');
|
|
23
|
+
const code=/^DEVELOPMENT_[A-Z_]{1,80}$/.test(error?.message??'')?error.message:'DEVELOPMENT_WORKER_FAILED';
|
|
24
|
+
const labels={write:'写入',command:'命令',model:'模型'};
|
|
25
|
+
const summary='开发批次已停止,等待核对。'+(passed?'当前清单的必需命令已通过,但批次未完成,不能据此交付。':'当前清单的验收未通过或证据不完整。')+
|
|
26
|
+
(exhausted.length?'已用完'+exhausted.map(x=>labels[x]).join('、')+'预算;不自动追加或换任务重试。':'尚有额度也不会自动重启,须先核对已发生操作。');
|
|
27
|
+
const question=exhausted.length?'请查看执行会话中的失败输出,再决定调整方案并重新审批,或停止任务;原失败与消耗保留。':'请查看执行会话中的失败输出及当前文件;继续前由运营方核对原审批、效果和排空,再显式恢复。';
|
|
28
|
+
const snapshot={schema:1,run_id:job.run_id,request_id:r.id,job_id:job.id,code,summary,question,
|
|
29
|
+
test_passed_on_recorded_manifest:passed,recorded_manifest:r.expected_hash,exhausted,
|
|
30
|
+
...progress,model_budget:model,unclosed_operations:gate.unresolved(r.id),
|
|
31
|
+
grants_execution:false,success_artifact:false,budget_reset:false};
|
|
32
|
+
gate.store.tx(()=>{this.db.prepare('INSERT INTO development_attention VALUES (?,?,?,?,?)').run(job.run_id,r.id,job.id,canonical(snapshot),gate.now());gate.event(r.id,'attention_recorded',{run_id:job.run_id,code,exhausted});});
|
|
33
|
+
return snapshot;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import {DevelopmentReconciliation} from './development-reconciliation.js';
|
|
2
|
+
import {randomUUID,timingSafeEqual} from 'node:crypto';
|
|
3
|
+
import {canonical,digest} from './approval-ledger.js';
|
|
4
|
+
const object=x=>x&&typeof x==='object'&&!Array.isArray(x);
|
|
5
|
+
const id=x=>typeof x==='string'&&x.length>0&&x.length<=256;
|
|
6
|
+
const uuid=x=>typeof x==='string'&&/^[a-f0-9-]{36}$/.test(x);
|
|
7
|
+
const same=(a,b)=>typeof a==='string'&&typeof b==='string'&&a.length===b.length&&timingSafeEqual(Buffer.from(a),Buffer.from(b));
|
|
8
|
+
const fields=(a,n)=>{if(!object(a)||Object.keys(a).some(k=>!n.includes(k)))throw Error('DEVELOPMENT_BRIDGE_INVALID');};
|
|
9
|
+
const binding=['id','endpoint_id','ticket','epoch'];
|
|
10
|
+
// Explicit opt-in internal protocol for the shared CLOUD checkout. The authenticated
|
|
11
|
+
// caller may propose content; it does not receive raw Git/shell access or a local FS lease.
|
|
12
|
+
// All actual mutations and child processes stay behind the same DevelopmentGate.
|
|
13
|
+
export class DevelopmentBridge {
|
|
14
|
+
constructor(runtime){
|
|
15
|
+
this.runtime=runtime;this.g=runtime.g;this.gate=runtime.gate;this.db=this.gate.db;
|
|
16
|
+
this.db.exec(`CREATE TABLE IF NOT EXISTS development_bridge_claims(
|
|
17
|
+
request_id TEXT PRIMARY KEY,node TEXT NOT NULL,endpoint_id TEXT NOT NULL,ticket TEXT NOT NULL,epoch INTEGER NOT NULL);
|
|
18
|
+
CREATE TABLE IF NOT EXISTS development_bridge_tests(
|
|
19
|
+
id TEXT PRIMARY KEY,request_id TEXT NOT NULL,fingerprint TEXT NOT NULL,state TEXT NOT NULL,result TEXT);`);
|
|
20
|
+
this.db.exec('CREATE TABLE IF NOT EXISTS development_bridge_claim_receipts(request_id TEXT PRIMARY KEY,node TEXT NOT NULL,endpoint_id TEXT NOT NULL,recovery_hash TEXT NOT NULL,epoch INTEGER NOT NULL);');
|
|
21
|
+
this.reconciliation=new DevelopmentReconciliation(this);
|
|
22
|
+
}
|
|
23
|
+
authorization(r,node){
|
|
24
|
+
if(r.node!==node)throw Error('DEVELOPMENT_BRIDGE_ROLE_DENIED');
|
|
25
|
+
const a=this.gate.authority(r.task_id,r.approval_id,JSON.parse(r.policy));
|
|
26
|
+
if(a.spec.kind==='workspace-v1'&&!this.runtime.workspaceEnabled)throw Error('DEVELOPMENT_WORKSPACE_DISABLED');
|
|
27
|
+
// Old cloud-only proposals cannot silently grant a new cross-endpoint capability.
|
|
28
|
+
if(!a.spec.executors?.includes(node))throw Error('DEVELOPMENT_BRIDGE_EXECUTOR_REQUIRED');
|
|
29
|
+
if(a.hash!==r.authority_hash)throw Error('DEVELOPMENT_APPROVAL_CHANGED');
|
|
30
|
+
this.gate.assertWorkspace(r,a.workspace);return a;
|
|
31
|
+
}
|
|
32
|
+
bound(a,node){
|
|
33
|
+
if(!id(a.id)||!uuid(a.endpoint_id)||!uuid(a.ticket)||!Number.isSafeInteger(a.epoch)||a.epoch<1)throw Error('DEVELOPMENT_BRIDGE_INVALID');
|
|
34
|
+
const c=this.db.prepare('SELECT * FROM development_bridge_claims WHERE request_id=?').get(a.id);
|
|
35
|
+
if(!c||c.node!==node||c.endpoint_id!==a.endpoint_id||!same(c.ticket,a.ticket)||c.epoch!==a.epoch)throw Error('DEVELOPMENT_BRIDGE_STALE_FENCE');
|
|
36
|
+
const r=this.gate.row(a.id);if(r.node!==node||r.epoch!==a.epoch)throw Error('DEVELOPMENT_BRIDGE_STALE_FENCE');return r;
|
|
37
|
+
}
|
|
38
|
+
status(r){return {...this.gate.view(r.id),waiting_reason:this.gate.waiting(r.id),storage_host:'cloud',drain_scope:'managed_cloud_io_only',starts_worker:false,grants_execution:false};}
|
|
39
|
+
async call(method,a,node){
|
|
40
|
+
if(!['local','cloud'].includes(node))throw Error('DEVELOPMENT_BRIDGE_ROLE_DENIED');
|
|
41
|
+
// Separate wire method family: old clients cannot mutate or review the new contract.
|
|
42
|
+
const workspace=method.startsWith('workspace_bridge_');
|
|
43
|
+
if(workspace){if(!this.runtime.workspaceEnabled)throw Error('DEVELOPMENT_WORKSPACE_DISABLED');method=method.replace('workspace_bridge_','development_bridge_');if(['development_bridge_acquire','development_bridge_test'].includes(method))throw Error('METHOD_DENIED');}
|
|
44
|
+
if(method!=='development_bridge_enqueue'&&id(a?.id)){
|
|
45
|
+
const r=this.db.prepare('SELECT approval_id FROM development_requests WHERE id=?').get(a.id),spec=r&&this.gate.ledger.view(r.approval_id).proposal.development;
|
|
46
|
+
if(r&&(spec?.kind==='workspace-v1')!==workspace)throw Error('DEVELOPMENT_CONTRACT_METHOD_MISMATCH');
|
|
47
|
+
}
|
|
48
|
+
if(['development_bridge_evidence','development_bridge_review'].includes(method))return this.reconciliation.call(method,a,node);
|
|
49
|
+
if(method==='development_bridge_enqueue'){
|
|
50
|
+
fields(a,['task_id','approval_id','policy','event_id']);
|
|
51
|
+
if(!id(a.task_id)||!id(a.approval_id)||!id(a.event_id)||!object(a.policy))throw Error('DEVELOPMENT_BRIDGE_INVALID');
|
|
52
|
+
await this.g.repositories.refresh(a.task_id);
|
|
53
|
+
const auth=this.gate.authority(a.task_id,a.approval_id,a.policy);
|
|
54
|
+
if((auth.spec.kind==='workspace-v1')!==workspace)throw Error('DEVELOPMENT_CONTRACT_METHOD_MISMATCH');
|
|
55
|
+
if(!auth.spec.executors?.includes(node))throw Error('DEVELOPMENT_BRIDGE_EXECUTOR_REQUIRED');
|
|
56
|
+
const r=this.gate.enqueue(node,{...a,event_id:'bridge:'+node+':'+a.event_id});return this.status(r);
|
|
57
|
+
}
|
|
58
|
+
if(method==='development_bridge_cancel'){
|
|
59
|
+
fields(a,['id']);if(!id(a.id))throw Error('DEVELOPMENT_BRIDGE_INVALID');const r=this.gate.row(a.id);
|
|
60
|
+
if(r.node!==node)throw Error('DEVELOPMENT_BRIDGE_ROLE_DENIED');
|
|
61
|
+
if(r.state==='rejected'&&r.error==='DEVELOPMENT_QUEUE_CANCELLED')return {...this.status(r),replayed:true};
|
|
62
|
+
if(r.state!=='queued')throw Error('DEVELOPMENT_NOT_QUEUED');
|
|
63
|
+
this.gate.rejectQueued(r.id,'DEVELOPMENT_QUEUE_CANCELLED');return this.status(this.gate.row(r.id));
|
|
64
|
+
}
|
|
65
|
+
if(method==='development_bridge_status'){
|
|
66
|
+
fields(a,['id']);if(!id(a.id))throw Error('DEVELOPMENT_BRIDGE_INVALID');const r=this.gate.row(a.id);
|
|
67
|
+
if(r.node!==node)throw Error('DEVELOPMENT_BRIDGE_ROLE_DENIED');return this.status(r);
|
|
68
|
+
}
|
|
69
|
+
if(method==='development_bridge_claim_receipt'){
|
|
70
|
+
fields(a,['id','endpoint_id','recovery_key']);if(!id(a.id)||!uuid(a.endpoint_id)||!uuid(a.recovery_key))throw Error('DEVELOPMENT_BRIDGE_INVALID');
|
|
71
|
+
const receipt=this.db.prepare('SELECT * FROM development_bridge_claim_receipts WHERE request_id=?').get(a.id),c=this.db.prepare('SELECT * FROM development_bridge_claims WHERE request_id=?').get(a.id);
|
|
72
|
+
if(!receipt||!c||receipt.node!==node||c.node!==node||receipt.endpoint_id!==a.endpoint_id||c.endpoint_id!==a.endpoint_id||receipt.epoch!==c.epoch||!same(receipt.recovery_hash,digest(a.recovery_key)))throw Error('DEVELOPMENT_CLAIM_RECEIPT_UNAVAILABLE');
|
|
73
|
+
return {id:a.id,endpoint_id:c.endpoint_id,ticket:c.ticket,epoch:c.epoch,grants_execution:false,starts_worker:false};
|
|
74
|
+
}
|
|
75
|
+
if(['development_bridge_acquire','development_bridge_claim_acquire'].includes(method)){
|
|
76
|
+
const recoverable=method==='development_bridge_claim_acquire';
|
|
77
|
+
fields(a,recoverable?['id','endpoint_id','recovery_key']:['id','endpoint_id']);
|
|
78
|
+
if(recoverable&&!uuid(a.recovery_key))throw Error('DEVELOPMENT_BRIDGE_INVALID');if(!id(a.id)||!uuid(a.endpoint_id))throw Error('DEVELOPMENT_BRIDGE_INVALID');
|
|
79
|
+
const r=this.gate.row(a.id);if(r.node!==node)throw Error('DEVELOPMENT_BRIDGE_ROLE_DENIED');
|
|
80
|
+
await this.g.repositories.refresh(r.task_id);this.authorization(r,node);
|
|
81
|
+
const old=this.db.prepare('SELECT * FROM development_bridge_claims WHERE request_id=?').get(a.id);
|
|
82
|
+
if(old){
|
|
83
|
+
const receipt=this.db.prepare('SELECT * FROM development_bridge_claim_receipts WHERE request_id=?').get(a.id);
|
|
84
|
+
if((recoverable||receipt)&&(!recoverable||!receipt||!same(receipt.recovery_hash,digest(a.recovery_key))))throw Error('DEVELOPMENT_CLAIM_RECEIPT_UNAVAILABLE');
|
|
85
|
+
if(old.node!==node||old.endpoint_id!==a.endpoint_id||r.state!=='active'||r.epoch!==old.epoch)throw Error('DEVELOPMENT_BRIDGE_STALE_FENCE');
|
|
86
|
+
this.gate.assert(r.id,node,old.epoch);return {acquired:true,...this.status(r),grants_execution:true,execution_scope:'bounded_shared_cloud_checkout',ticket:old.ticket,endpoint_id:old.endpoint_id};
|
|
87
|
+
}
|
|
88
|
+
// Claim and ticket are committed in the SAME transaction as the resource fence.
|
|
89
|
+
const ticket=randomUUID(),claimed=this.gate.acquire(r.id,x=>{this.db.prepare('INSERT INTO development_bridge_claims VALUES (?,?,?,?,?)').run(x.id,node,a.endpoint_id,ticket,x.epoch);if(recoverable)this.db.prepare('INSERT INTO development_bridge_claim_receipts VALUES (?,?,?,?,?)').run(x.id,node,a.endpoint_id,digest(a.recovery_key),x.epoch);});
|
|
90
|
+
if(!claimed.acquired)this.gate.event(r.id,'bridge_waiting',{node,reason:claimed.reason});
|
|
91
|
+
return {...this.status(this.gate.row(r.id)),...claimed,...(claimed.acquired?{grants_execution:true,execution_scope:'bounded_shared_cloud_checkout',ticket,endpoint_id:a.endpoint_id}:{})};
|
|
92
|
+
}
|
|
93
|
+
const extra={development_bridge_read:workspace?['path']:[],development_bridge_write:['call_id','expected_hash','content',...(workspace?['path']:[])],...(workspace?{development_bridge_command:['call_id','expected_hash','command_id']}:{development_bridge_test:['call_id','expected_hash']}),development_bridge_finish:[],development_bridge_fail:[]}[method];
|
|
94
|
+
if(!extra)throw Error('METHOD_DENIED');fields(a,[...binding,...extra]);let r=this.bound(a,node);
|
|
95
|
+
if(method==='development_bridge_finish'&&r.state==='completed')return {...this.status(r),replayed:true};
|
|
96
|
+
if(method==='development_bridge_fail'){
|
|
97
|
+
if(!['active','needs_review'].includes(r.state))throw Error('DEVELOPMENT_BRIDGE_STALE_FENCE');
|
|
98
|
+
this.gate.block(r.id,'DEVELOPMENT_ENDPOINT_REVIEW');return this.status(this.gate.row(r.id));
|
|
99
|
+
}
|
|
100
|
+
this.authorization(r,node);
|
|
101
|
+
if(method==='development_bridge_read'){
|
|
102
|
+
const result=this.gate.read(r.id,node,a.epoch,a.path);
|
|
103
|
+
// A manifest hash is a CAS token; unrelated filenames are not approved read inputs.
|
|
104
|
+
if(workspace){const {manifest,...visible}=result;return {...visible,storage_host:'cloud'};}
|
|
105
|
+
return {...result,storage_host:'cloud'};
|
|
106
|
+
}
|
|
107
|
+
if(method==='development_bridge_write'){
|
|
108
|
+
if(!id(a.call_id))throw Error('DEVELOPMENT_BRIDGE_INVALID');
|
|
109
|
+
this.gate.admitWriteCall(r.id,node,a.epoch,a.call_id);
|
|
110
|
+
await this.g.repositories.refresh(r.task_id);r=this.bound(a,node);
|
|
111
|
+
this.authorization(r,node);return this.gate.write(r.id,node,a.epoch,a);
|
|
112
|
+
}
|
|
113
|
+
if(method==='development_bridge_command'){
|
|
114
|
+
if(!id(a.call_id)||!id(a.command_id))throw Error('DEVELOPMENT_BRIDGE_INVALID');
|
|
115
|
+
await this.g.repositories.refresh(r.task_id);r=this.bound(a,node);this.authorization(r,node);
|
|
116
|
+
if(a.expected_hash!==r.expected_hash)throw Error('DEVELOPMENT_FILE_CONFLICT');
|
|
117
|
+
// Gate owns the durable fingerprint, budget and drain barrier. A repeated HTTP
|
|
118
|
+
// request cannot create a second container for an unresolved command.
|
|
119
|
+
return this.gate.command(r.id,node,a.epoch,a);
|
|
120
|
+
}
|
|
121
|
+
if(method==='development_bridge_test'){
|
|
122
|
+
if(!id(a.call_id))throw Error('DEVELOPMENT_BRIDGE_INVALID');
|
|
123
|
+
this.gate.assert(r.id,node,a.epoch);if(a.expected_hash!==undefined&&a.expected_hash!==r.expected_hash)throw Error('DEVELOPMENT_FILE_CONFLICT');
|
|
124
|
+
const key=r.id+':'+a.call_id,fp=digest({epoch:a.epoch,expected_hash:r.expected_hash}),old=this.db.prepare('SELECT * FROM development_bridge_tests WHERE id=?').get(key);
|
|
125
|
+
if(old){if(old.fingerprint!==fp)throw Error('DEVELOPMENT_BRIDGE_CALL_CONFLICT');if(old.state!=='completed')throw Error('DEVELOPMENT_BRIDGE_TEST_UNCERTAIN');return {...JSON.parse(old.result),replayed:true};}
|
|
126
|
+
// A timed-out HTTP request never starts a second child on retransmission.
|
|
127
|
+
this.db.prepare("INSERT INTO development_bridge_tests VALUES (?,?,?,'running',NULL)").run(key,r.id,fp);
|
|
128
|
+
try{const result=await this.gate.test(r.id,node,a.epoch);this.db.prepare("UPDATE development_bridge_tests SET state='completed',result=? WHERE id=?").run(canonical(result),key);return result;}
|
|
129
|
+
catch(e){this.db.prepare("UPDATE development_bridge_tests SET state='needs_review' WHERE id=?").run(key);throw e;}
|
|
130
|
+
}
|
|
131
|
+
if(this.db.prepare("SELECT 1 FROM development_bridge_tests WHERE request_id=? AND state<>'completed'").get(r.id))throw Error('DEVELOPMENT_NOT_DRAINED');
|
|
132
|
+
// No caller flag can override the actual cloud write/test barriers. A completed
|
|
133
|
+
// fence revokes every old client operation, even if a local model is still idle/running.
|
|
134
|
+
this.gate.finish(r.id,node,a.epoch,{agentDrained:true});return {...this.status(this.gate.row(r.id)),committed:false,pushed:false,published:false};
|
|
135
|
+
}
|
|
136
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import {canonical,digest} from './approval-ledger.js';
|
|
2
|
+
import {workspacePlan} from './development-plan.js';
|
|
3
|
+
import {DevelopmentFiles} from './development-files.js';
|
|
4
|
+
|
|
5
|
+
// Internal operator API only. The exclusive Host owns its original scheduler
|
|
6
|
+
// socket; this does not imply the old Docker workload stopped. Never a model tool.
|
|
7
|
+
export class DevelopmentCommandReview {
|
|
8
|
+
constructor(gate,{assertExclusive}={}){
|
|
9
|
+
if(typeof assertExclusive!=='function')throw Error('DEVELOPMENT_REVIEW_EXCLUSIVE_REQUIRED');
|
|
10
|
+
this.gate=gate;this.db=gate.db;this.assertExclusive=assertExclusive;this.active=new Set();
|
|
11
|
+
this.db.exec(`CREATE TABLE IF NOT EXISTS development_command_reviews(
|
|
12
|
+
event_id TEXT PRIMARY KEY,fingerprint TEXT NOT NULL,operation_id TEXT UNIQUE NOT NULL,
|
|
13
|
+
request_id TEXT NOT NULL,state TEXT NOT NULL,record TEXT NOT NULL,result TEXT,created INTEGER NOT NULL);`);
|
|
14
|
+
}
|
|
15
|
+
context(id,operation){
|
|
16
|
+
const g=this.gate,r=g.row(id),t=g.store.task(r.task_id);this.assertExclusive(r.task_id);
|
|
17
|
+
if(!g.enabled||g.inflight.has(id)||g.busy(r.task_id)||r.state!=='needs_review'||
|
|
18
|
+
t.owner!==r.node||t.epoch!==r.epoch||!t.busy||!t.blocked)throw Error('DEVELOPMENT_REVIEW_STATE');
|
|
19
|
+
const c=this.db.prepare('SELECT * FROM development_commands WHERE request_id=? AND operation_id=?').all(id,operation);
|
|
20
|
+
const o=this.db.prepare('SELECT * FROM development_operations WHERE id=? AND request_id=?').get(operation,id);
|
|
21
|
+
if(c.length!==1||!['running','needs_review'].includes(c[0].state)||c[0].result!==null||
|
|
22
|
+
!o||o.kind!=='command'||o.epoch!==r.epoch||o.state==='closed')throw Error('DEVELOPMENT_REVIEW_STATE');
|
|
23
|
+
const events=type=>this.db.prepare('SELECT data FROM development_events WHERE request_id=? AND type=? ORDER BY seq').all(id,type).map(x=>JSON.parse(x.data)).filter(x=>x.operation===operation);
|
|
24
|
+
const intents=events('command_intent'),containers=events('command_container');
|
|
25
|
+
if(intents.length!==1||containers.length!==1||events('command_drained').length)throw Error('DEVELOPMENT_REVIEW_EVIDENCE_REQUIRED');
|
|
26
|
+
const intent=intents[0],container=containers[0],v=g.ledger.view(r.approval_id),spec=workspacePlan(v.proposal.development);
|
|
27
|
+
const hash=digest({approval:v.id,proposal_hash:v.proposal_hash,spec}),binding=g.bindings[r.task_id];
|
|
28
|
+
if(v.task_id!==r.task_id||v.proposal.scopes.length!==1||v.proposal.scopes[0]!=='develop'||!v.proposal.repository||
|
|
29
|
+
canonical(v.proposal.model_policy)!==canonical(JSON.parse(r.policy))||hash!==r.authority_hash||binding?.approval_hash!==hash)
|
|
30
|
+
throw Error('DEVELOPMENT_APPROVAL_CHANGED');
|
|
31
|
+
const command=spec.commands.find(x=>x.id===c[0].command_id);
|
|
32
|
+
if(!command||intent.command_id!==command.id||intent.epoch!==o.epoch||intent.file_hash!==r.expected_hash||
|
|
33
|
+
c[0].fingerprint!==digest({epoch:o.epoch,command_id:command.id,file_hash:intent.file_hash})||
|
|
34
|
+
!/^[a-f0-9]{64}$/.test(container.id??'')||container.name!==g.commands?.name(operation))throw Error('DEVELOPMENT_REVIEW_EVIDENCE_REQUIRED');
|
|
35
|
+
const workspace=new DevelopmentFiles(binding,spec,v.proposal.repository);g.assertWorkspace(r,workspace);workspace.check(r.expected_hash);
|
|
36
|
+
// Current approval validity is deliberately NOT an execution grant here:
|
|
37
|
+
// a revoked/expired task may still have its exact orphaned container stopped.
|
|
38
|
+
const evidence={schema:1,request_id:id,operation_id:operation,command_record:c[0],operation:o,
|
|
39
|
+
authority_hash:hash,epoch:r.epoch,expected_hash:r.expected_hash,workspace_identity:r.workspace_identity,
|
|
40
|
+
container_id:container.id,command_hash:digest(command),grants_execution:false};
|
|
41
|
+
return {evidence,context_hash:digest(evidence),target:{operation,container_id:container.id,command},r};
|
|
42
|
+
}
|
|
43
|
+
async evidence(id,operation){
|
|
44
|
+
const before=this.context(id,operation),observed=await this.gate.commands.observeForReview(before.target),after=this.context(id,operation);
|
|
45
|
+
if(before.context_hash!==after.context_hash)throw Error('DEVELOPMENT_REVIEW_STALE_EVIDENCE');
|
|
46
|
+
const evidence={...before.evidence,observed};return {evidence,proof_hash:digest(evidence)};
|
|
47
|
+
}
|
|
48
|
+
async review(a){
|
|
49
|
+
if(!a||Object.keys(a).some(k=>!['request_id','operation_id','event_id','proof_hash','note','stop_running'].includes(k))||
|
|
50
|
+
typeof a.event_id!=='string'||!a.event_id||a.event_id.length>256||typeof a.note!=='string'||!a.note.trim()||a.note.length>2000||
|
|
51
|
+
!/^[a-f0-9]{64}$/.test(a.proof_hash??'')||typeof a.stop_running!=='boolean')throw Error('DEVELOPMENT_REVIEW_INVALID');
|
|
52
|
+
const fp=digest(a),prior=this.db.prepare('SELECT * FROM development_command_reviews WHERE event_id=?').get(a.event_id);
|
|
53
|
+
if(prior&&prior.fingerprint!==fp)throw Error('DEVELOPMENT_REVIEW_CONFLICT');
|
|
54
|
+
if(prior?.state==='applied')return {...JSON.parse(prior.result),replayed:true};
|
|
55
|
+
if(this.active.has(a.operation_id))throw Error('DEVELOPMENT_REVIEW_IN_FLIGHT');
|
|
56
|
+
this.active.add(a.operation_id);
|
|
57
|
+
try{
|
|
58
|
+
if(!prior){
|
|
59
|
+
const snapshot=await this.evidence(a.request_id,a.operation_id);
|
|
60
|
+
if(snapshot.proof_hash!==a.proof_hash)throw Error('DEVELOPMENT_REVIEW_STALE_EVIDENCE');
|
|
61
|
+
if(snapshot.evidence.observed.status==='running'&&!a.stop_running)throw Error('DEVELOPMENT_CONTAINER_STOP_CONFIRMATION_REQUIRED');
|
|
62
|
+
const ctx=this.context(a.request_id,a.operation_id);
|
|
63
|
+
const {observed:ignored,...base}=snapshot.evidence;
|
|
64
|
+
if(ctx.context_hash!==digest(base))throw Error('DEVELOPMENT_REVIEW_STALE_EVIDENCE');
|
|
65
|
+
this.gate.store.tx(()=>{
|
|
66
|
+
if(this.db.prepare('SELECT 1 FROM development_command_reviews WHERE operation_id=?').get(a.operation_id))throw Error('DEVELOPMENT_REVIEW_CONFLICT');
|
|
67
|
+
const record={context_hash:ctx.context_hash,started_at:snapshot.evidence.observed.started_at,proof_hash:a.proof_hash,note:a.note};
|
|
68
|
+
this.db.prepare("INSERT INTO development_command_reviews VALUES (?,?,?,?,'prepared',?,NULL,?)").run(a.event_id,fp,a.operation_id,a.request_id,canonical(record),this.gate.now());
|
|
69
|
+
this.gate.event(a.request_id,'command_review_prepared',{event_id:a.event_id,operation:a.operation_id,proof_hash:a.proof_hash});
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
const row=this.db.prepare('SELECT * FROM development_command_reviews WHERE event_id=?').get(a.event_id),record=JSON.parse(row.record);
|
|
73
|
+
let ctx=this.context(a.request_id,a.operation_id);
|
|
74
|
+
if(ctx.context_hash!==record.context_hash)throw Error('DEVELOPMENT_REVIEW_STALE_EVIDENCE');
|
|
75
|
+
const observed=await this.gate.commands.stopForReview(ctx.target,{stop_running:a.stop_running,started_at:record.started_at});
|
|
76
|
+
// The adapter may have stopped the workload while the Host failed. Retrying
|
|
77
|
+
// this same prepared event re-inspects the same ID, never starts a new command.
|
|
78
|
+
ctx=this.context(a.request_id,a.operation_id);
|
|
79
|
+
if(ctx.context_hash!==record.context_hash)throw Error('DEVELOPMENT_REVIEW_STALE_EVIDENCE');
|
|
80
|
+
if(observed.status!=='exited'||observed.container_id!==ctx.target.container_id||observed.operation!==a.operation_id||
|
|
81
|
+
observed.command_hash!==digest(ctx.target.command)||observed.image!==ctx.target.command.image||observed.started_at!==record.started_at)
|
|
82
|
+
throw Error('DEVELOPMENT_CONTAINER_DRAIN_UNKNOWN');
|
|
83
|
+
return this.gate.store.tx(()=>{
|
|
84
|
+
if(this.context(a.request_id,a.operation_id).context_hash!==record.context_hash)throw Error('DEVELOPMENT_REVIEW_STALE_EVIDENCE');
|
|
85
|
+
const receipt={command_id:ctx.target.command.id,command_hash:observed.command_hash,image:observed.image,file_hash:ctx.r.expected_hash,
|
|
86
|
+
passed:false,drained:true,started:true,exit_code:observed.exit_code,code:'DEVELOPMENT_COMMAND_INTERRUPTED_REVIEWED',
|
|
87
|
+
output:'',stderr:'',container_id:observed.container_id,review_event:a.event_id};
|
|
88
|
+
this.db.prepare("UPDATE development_operations SET state='closed',closed=?,exit_code=?,signal='OPERATOR_REVIEW' WHERE id=?").run(this.gate.now(),observed.exit_code,a.operation_id);
|
|
89
|
+
this.db.prepare("UPDATE development_commands SET state='completed',result=? WHERE operation_id=?").run(canonical(receipt),a.operation_id);
|
|
90
|
+
this.db.prepare('UPDATE development_requests SET test_result=NULL WHERE id=?').run(a.request_id);
|
|
91
|
+
this.gate.event(a.request_id,'command_drained',{operation:a.operation_id,container_id:observed.container_id,code:observed.exit_code,signal:'OPERATOR_REVIEW',observed});
|
|
92
|
+
const result={request_id:a.request_id,operation_id:a.operation_id,event_id:a.event_id,drained:true,test_passed:false,
|
|
93
|
+
request_state:'needs_review',grants_execution:false,budget_reset:false,starts_worker:false,receipt};
|
|
94
|
+
this.db.prepare("UPDATE development_command_reviews SET state='applied',result=? WHERE event_id=?").run(canonical(result),a.event_id);
|
|
95
|
+
this.gate.event(a.request_id,'command_review_applied',result);return result;
|
|
96
|
+
});
|
|
97
|
+
}finally{this.active.delete(a.operation_id);}
|
|
98
|
+
}
|
|
99
|
+
async cleanup(event_id){
|
|
100
|
+
const row=this.db.prepare("SELECT * FROM development_command_reviews WHERE event_id=? AND state='applied'").get(event_id);
|
|
101
|
+
if(!row)throw Error('DEVELOPMENT_REVIEW_STATE');
|
|
102
|
+
const r=this.gate.row(row.request_id);this.assertExclusive(r.task_id);
|
|
103
|
+
const result=JSON.parse(row.result),record=JSON.parse(row.record),spec=workspacePlan(this.gate.ledger.view(r.approval_id).proposal.development);
|
|
104
|
+
const command=spec.commands.find(x=>digest(x)===result.receipt.command_hash);
|
|
105
|
+
if(!command)throw Error('DEVELOPMENT_APPROVAL_CHANGED');
|
|
106
|
+
await this.gate.commands.cleanupReviewed({operation:row.operation_id,container_id:result.receipt.container_id,command},record.started_at);
|
|
107
|
+
this.gate.event(r.id,'command_review_cleanup',{event_id,operation:row.operation_id});
|
|
108
|
+
return {cleaned:true,grants_execution:false};
|
|
109
|
+
}
|
|
110
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import {mkdirSync,realpathSync,lstatSync,rmSync,readFileSync} from 'node:fs';
|
|
2
|
+
import {execFile} from 'node:child_process';
|
|
3
|
+
import {join,resolve} from 'node:path';
|
|
4
|
+
import {execFileDrained} from './drained-process.js';
|
|
5
|
+
import {workspacePlan} from './development-plan.js';
|
|
6
|
+
import {digest} from './approval-ledger.js';
|
|
7
|
+
|
|
8
|
+
// An installed, operator-approved local daemon is a prerequisite, never auto-installed.
|
|
9
|
+
// The source checkout, .git, model credentials and daemon socket are NOT mounted.
|
|
10
|
+
export class DevelopmentContainer {
|
|
11
|
+
constructor({directory,docker='/usr/bin/docker'}){
|
|
12
|
+
if(process.platform!=='linux')throw Error('DEVELOPMENT_CONTAINER_UNAVAILABLE');
|
|
13
|
+
if(typeof directory!=='string'||!directory.startsWith('/')||/[:,\n\r]/.test(directory)||docker!=='/usr/bin/docker')throw Error('DEVELOPMENT_CONTAINER_CONFIG');
|
|
14
|
+
this.directory=resolve(directory);mkdirSync(this.directory,{recursive:true,mode:0o700});
|
|
15
|
+
if(realpathSync(this.directory)!==this.directory||!lstatSync(this.directory).isDirectory())throw Error('DEVELOPMENT_CONTAINER_CONFIG');
|
|
16
|
+
const allowed=/^Cpus_allowed_list:\s*(\d+)/m.exec(readFileSync('/proc/self/status','utf8'));
|
|
17
|
+
if(!allowed)throw Error('DEVELOPMENT_CONTAINER_CPU_UNAVAILABLE');this.cpu=allowed[1];
|
|
18
|
+
this.docker=docker;this.config=join(this.directory,'docker-config');mkdirSync(this.config,{recursive:true,mode:0o700});
|
|
19
|
+
}
|
|
20
|
+
async cli(args,options={}){
|
|
21
|
+
return execFileDrained(this.docker,['--host','unix:///var/run/docker.sock',...args],{
|
|
22
|
+
env:{PATH:'/usr/bin:/bin',HOME:this.directory,DOCKER_CONFIG:this.config},timeout:15000,maxBuffer:1048576,killSignal:'SIGKILL',...options},undefined,{execute:(file,args,opts,callback)=>execFile(file,args,opts,(error,out,err)=>callback(error?Object.assign(error,{stdout:out,stderr:err}):null,out))});
|
|
23
|
+
}
|
|
24
|
+
name(operation){if(!/^operation-[a-f0-9-]{36}$/.test(operation))throw Error('DEVELOPMENT_OPERATION_ID_INVALID');return 'dsh-build-'+operation.slice(10);}
|
|
25
|
+
async inspect(name,operation,commandHash){
|
|
26
|
+
const rows=JSON.parse(await this.cli(['inspect','--type','container',name]));const c=rows?.[0];
|
|
27
|
+
if(rows?.length!==1||!c?.Id||c.Config?.Labels?.['dsh.operation']!==operation||c.Config?.Labels?.['dsh.command']!==commandHash)throw Error('DEVELOPMENT_CONTAINER_IDENTITY_CHANGED');
|
|
28
|
+
return c;
|
|
29
|
+
}
|
|
30
|
+
// Operator review targets an already recorded immutable ID, never discovers by
|
|
31
|
+
// PID/name or treats a failed inspect as proof of absence/drain.
|
|
32
|
+
async observeForReview({operation,container_id,command}){
|
|
33
|
+
if(!/^[a-f0-9]{64}$/.test(container_id??''))throw Error('DEVELOPMENT_CONTAINER_IDENTITY_CHANGED');
|
|
34
|
+
const c=await this.inspect(container_id,operation,digest(command)),s=c.State;
|
|
35
|
+
if(c.Id!==container_id||c.Name!=='/'+this.name(operation)||c.Image!==command.image||
|
|
36
|
+
c.HostConfig?.RestartPolicy?.Name!=='no'||c.HostConfig?.AutoRemove!==false||c.RestartCount!==0)
|
|
37
|
+
throw Error('DEVELOPMENT_CONTAINER_IDENTITY_CHANGED');
|
|
38
|
+
if(!s||s.Dead!==false||s.Paused!==false||s.Restarting!==false||
|
|
39
|
+
!Number.isFinite(Date.parse(s.StartedAt))||s.StartedAt.startsWith('0001-')||
|
|
40
|
+
!(s.Status==='running'&&s.Running===true&&Number.isInteger(s.Pid)&&s.Pid>0||
|
|
41
|
+
s.Status==='exited'&&s.Running===false&&s.Pid===0&&Number.isInteger(s.ExitCode)&&Number.isFinite(Date.parse(s.FinishedAt))&&!s.FinishedAt.startsWith('0001-')))
|
|
42
|
+
throw Error('DEVELOPMENT_CONTAINER_DRAIN_UNKNOWN');
|
|
43
|
+
return {container_id:c.Id,operation,command_hash:digest(command),image:c.Image,status:s.Status,
|
|
44
|
+
started_at:s.StartedAt,finished_at:s.Status==='exited'?s.FinishedAt:null,exit_code:s.Status==='exited'?s.ExitCode:null};
|
|
45
|
+
}
|
|
46
|
+
async stopForReview(target,{stop_running=false,started_at}={}){
|
|
47
|
+
let observed=await this.observeForReview(target);
|
|
48
|
+
if(observed.started_at!==started_at)throw Error('DEVELOPMENT_CONTAINER_IDENTITY_CHANGED');
|
|
49
|
+
if(observed.status==='running'){
|
|
50
|
+
if(!stop_running)throw Error('DEVELOPMENT_CONTAINER_STOP_CONFIRMATION_REQUIRED');
|
|
51
|
+
await this.cli(['kill','--signal','KILL',target.container_id]);
|
|
52
|
+
await this.cli(['wait',target.container_id]);
|
|
53
|
+
observed=await this.observeForReview(target);
|
|
54
|
+
}
|
|
55
|
+
if(observed.status!=='exited'||observed.started_at!==started_at)throw Error('DEVELOPMENT_CONTAINER_DRAIN_UNKNOWN');
|
|
56
|
+
return observed;
|
|
57
|
+
}
|
|
58
|
+
async cleanupReviewed(target,started_at){
|
|
59
|
+
const observed=await this.observeForReview(target);
|
|
60
|
+
if(observed.status!=='exited'||observed.started_at!==started_at)throw Error('DEVELOPMENT_CONTAINER_DRAIN_UNKNOWN');
|
|
61
|
+
// No --force: a concurrently restarted container must not be silently removed.
|
|
62
|
+
await this.cli(['rm',target.container_id]);
|
|
63
|
+
rmSync(join(this.directory,this.name(target.operation)),{recursive:true,force:true});
|
|
64
|
+
}
|
|
65
|
+
async run(workspace,expectedHash,commandId,{operation,signal,onCreated=()=>{},onClosed=()=>{}}){
|
|
66
|
+
const spec=workspacePlan(workspace.spec),command=spec.commands.find(c=>c.id===commandId);
|
|
67
|
+
if(!command)throw Object.assign(Error('DEVELOPMENT_COMMAND_DENIED'),{drained:true});
|
|
68
|
+
if(signal?.aborted)throw Object.assign(Error('DEVELOPMENT_COMMAND_CANCELLED'),{drained:true});
|
|
69
|
+
const name=this.name(operation),commandHash=digest(command),dir=join(this.directory,name),snapshot=join(dir,'source');
|
|
70
|
+
let created=false,closed=false,container=null;
|
|
71
|
+
try{
|
|
72
|
+
const image=JSON.parse(await this.cli(['image','inspect',command.image]))?.[0];
|
|
73
|
+
if(image?.Id!==command.image||Object.keys(image.Config?.Volumes??{}).length)throw Error('DEVELOPMENT_IMAGE_UNAVAILABLE');
|
|
74
|
+
mkdirSync(dir,{recursive:false,mode:0o755});workspace.snapshot(expectedHash,snapshot);
|
|
75
|
+
const args=['create','--name',name,'--pull','never','--restart','no','--network','none','--read-only',
|
|
76
|
+
'--cap-drop','ALL','--security-opt','no-new-privileges:true','--user','65534:65534','--init','--no-healthcheck',
|
|
77
|
+
'--pids-limit','64','--memory','256m','--memory-swap','256m','--cpuset-cpus',this.cpu,'--shm-size','8m',
|
|
78
|
+
'--ulimit','nofile=128:128','--ulimit','core=0:0','--log-driver','local','--log-opt','max-size=1m','--log-opt','max-file=1','--log-opt','compress=false',
|
|
79
|
+
'--mount',`type=bind,src=${snapshot},dst=/work,readonly`,
|
|
80
|
+
'--tmpfs','/out:rw,nosuid,nodev,size=64m,mode=1777','--tmpfs','/tmp:rw,nosuid,nodev,noexec,size=16m,mode=1777',
|
|
81
|
+
'--env','HOME=/tmp','--env','TMPDIR=/tmp','--env','PYTHONDONTWRITEBYTECODE=1','--env','PYTHONPYCACHEPREFIX=/out/pycache',
|
|
82
|
+
'--workdir',command.cwd==='.'?'/work':'/work/'+command.cwd,
|
|
83
|
+
'--label','dsh.operation='+operation,'--label','dsh.command='+commandHash,
|
|
84
|
+
'--entrypoint',command.argv[0],command.image,...command.argv.slice(1)];
|
|
85
|
+
// Set before create: a failed client response may still have created the container.
|
|
86
|
+
created=true;await this.cli(args);container=await this.inspect(name,operation,commandHash);onCreated({id:container.Id,name});
|
|
87
|
+
let output='',stderr='',error=null,startAttempted=false;
|
|
88
|
+
if(!signal?.aborted){startAttempted=true;try{output=await this.cli(['start','--attach',container.Id],{timeout:command.timeout_ms,maxBuffer:65536,signal});}
|
|
89
|
+
catch(e){error=e;output=typeof e.stdout==='string'?e.stdout.slice(0,65536):'';stderr=typeof e.stderr==='string'?e.stderr.slice(0,65536):'';}}
|
|
90
|
+
else error=Error('DEVELOPMENT_COMMAND_CANCELLED');
|
|
91
|
+
// Closing the docker client is NOT proof the workload or its descendants stopped.
|
|
92
|
+
let state=await this.inspect(name,operation,commandHash);
|
|
93
|
+
if(state.State?.Running){await this.cli(['kill','--signal','KILL',state.Id]);await this.cli(['wait',state.Id]);state=await this.inspect(name,operation,commandHash);}
|
|
94
|
+
if((startAttempted&&state.State?.Status==='created')||state.State?.Running!==false||state.State?.Pid!==0||!['exited','created'].includes(state.State?.Status))throw Error('DEVELOPMENT_CONTAINER_DRAIN_UNKNOWN');
|
|
95
|
+
closed=true;onClosed({code:state.State.ExitCode??null,signal:signal?.aborted?'ABORT':error?'LIMIT':null,container_id:state.Id});
|
|
96
|
+
const passed=!error&&!signal?.aborted&&state.State.Status==='exited'&&state.State.ExitCode===0&&!state.State.OOMKilled;
|
|
97
|
+
workspace.check(expectedHash);
|
|
98
|
+
return {command_id:command.id,command_hash:commandHash,image:command.image,file_hash:expectedHash,passed,exit_code:state.State.ExitCode,
|
|
99
|
+
code:signal?.aborted?'DEVELOPMENT_COMMAND_CANCELLED':state.State.OOMKilled?'DEVELOPMENT_COMMAND_MEMORY_LIMIT':state.State.Error?'DEVELOPMENT_COMMAND_START_FAILED':error?.killed||error?.code==='ERR_CHILD_PROCESS_STDIO_MAXBUFFER'?'DEVELOPMENT_COMMAND_LIMIT':passed?'DEVELOPMENT_COMMAND_PASSED':'DEVELOPMENT_COMMAND_FAILED',
|
|
100
|
+
output,stderr,container_id:state.Id,started:!!state.State.StartedAt&&!state.State.StartedAt.startsWith('0001-'),drained:true};
|
|
101
|
+
}catch(e){
|
|
102
|
+
// Unknown daemon/start state stays fenced, and the exact named container is kept
|
|
103
|
+
// for explicit review. Never infer "not created" from an inspect/network failure.
|
|
104
|
+
throw Object.assign(Error(/^DEVELOPMENT_[A-Z_]+$/.test(e.message)?e.message:'DEVELOPMENT_CONTAINER_UNAVAILABLE'),{drained:!created||closed});
|
|
105
|
+
}finally{
|
|
106
|
+
if(closed&&container){try{await this.inspect(name,operation,commandHash);await this.cli(['rm',container.Id]);rmSync(dir,{recursive:true,force:true});}catch{/* Retain evidence; the verified stop is not invalidated by cleanup failure. */}}
|
|
107
|
+
else if(!created){try{rmSync(dir,{recursive:true,force:true});}catch{}}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import {workspacePlan} from './development-plan.js';
|
|
2
|
+
import {canonical,digest} from './approval-ledger.js';
|
|
3
|
+
|
|
4
|
+
// Deliberately one bounded code exercise, not an arbitrary shell/build adapter.
|
|
5
|
+
export function developmentSpec(value){
|
|
6
|
+
if(value?.kind==='workspace-v1')return workspacePlan(value);
|
|
7
|
+
if(!value||typeof value!=='object'||Array.isArray(value)||Object.keys(value).some(k=>!['kind','branch','file','validator','max_writes','executors'].includes(k)))throw Error('DEVELOPMENT_INVALID_SPEC');
|
|
8
|
+
if(value.kind!=='bounded-js-v1'||!/^dsh-task\/[a-z0-9-]{1,64}$/.test(value.branch??'')||!/^s[0-9]+-task-summary\.mjs$/.test(value.file??'')||value.validator!=='task-summary-v1'||!Number.isInteger(value.max_writes)||value.max_writes<1||value.max_writes>3)throw Error('DEVELOPMENT_INVALID_SPEC');
|
|
9
|
+
if(value.executors!==undefined&&(!Array.isArray(value.executors)||!value.executors.length||value.executors.length>2||new Set(value.executors).size!==value.executors.length||value.executors.some(x=>!['local','cloud'].includes(x))))throw Error('DEVELOPMENT_INVALID_SPEC');
|
|
10
|
+
return {...(value.executors?{executors:[...value.executors].sort()}:{}),kind:value.kind,branch:value.branch,file:value.file,validator:value.validator,max_writes:value.max_writes};
|
|
11
|
+
}
|
|
12
|
+
export function approvedDevelopment(ledger,task,approval,policy,{review=false}={}){
|
|
13
|
+
const v=ledger.view(approval),spec=developmentSpec(v.proposal.development);
|
|
14
|
+
if(review)v.state=ledger.validity(ledger.row(approval),{ignoreTaskBlock:true});
|
|
15
|
+
if(v.task_id!==task||v.state!=='approved'||v.proposal.scopes.length!==1||v.proposal.scopes[0]!=='develop'||!v.proposal.repository||canonical(v.proposal.model_policy)!==canonical(policy))throw Error('DEVELOPMENT_APPROVAL_REQUIRED');
|
|
16
|
+
return {approval:v,spec,hash:digest({approval:v.id,proposal_hash:v.proposal_hash,spec})};
|
|
17
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import {createHash,randomUUID} from 'node:crypto';
|
|
2
|
+
import {lstatSync,readdirSync,readFileSync,openSync,closeSync,writeFileSync,fsyncSync,renameSync,unlinkSync,mkdirSync,chmodSync,constants} from 'node:fs';
|
|
3
|
+
import {join,dirname} from 'node:path';
|
|
4
|
+
import {DevelopmentWorkspace,contentHash} from './development-workspace.js';
|
|
5
|
+
import {workspacePlan,developmentPath} from './development-plan.js';
|
|
6
|
+
import {digest} from './approval-ledger.js';
|
|
7
|
+
const absent=e=>{if(e.code==='ENOENT')return null;throw e;};
|
|
8
|
+
const MAX_FILE=1048576,MAX_TOTAL=33554432,MAX_FILES=4096;
|
|
9
|
+
export class DevelopmentFiles extends DevelopmentWorkspace {
|
|
10
|
+
constructor(binding,spec,snapshot){super(binding,{...spec,file:'unused'},snapshot);this.spec=workspacePlan(spec);}
|
|
11
|
+
// Hash every regular file, including ignored files. Git assume-unchanged/filter tricks
|
|
12
|
+
// must not hide an out-of-scope edit. The .git directory is never given to commands.
|
|
13
|
+
inventory(){
|
|
14
|
+
const out=Object.create(null);let bytes=0;
|
|
15
|
+
const walk=(dir,base='')=>{for(const name of readdirSync(dir).sort()){
|
|
16
|
+
if(!base&&name==='.git')continue;
|
|
17
|
+
const p=base?base+'/'+name:name;developmentPath(p);const path=join(dir,name),s=lstatSync(path);
|
|
18
|
+
if(s.isSymbolicLink())throw Error('DEVELOPMENT_FILE_UNSAFE');
|
|
19
|
+
if(s.isDirectory()){walk(path,p);continue;}
|
|
20
|
+
if(!s.isFile()||s.nlink!==1||s.size>8388608)throw Error('DEVELOPMENT_FILE_UNSAFE');
|
|
21
|
+
const data=readFileSync(path);bytes+=data.length;if(bytes>MAX_TOTAL||Object.keys(out).length>=MAX_FILES)throw Error('DEVELOPMENT_WORKSPACE_LIMIT');
|
|
22
|
+
out[p]={hash:contentHash(data),git_hash:createHash('sha1').update('blob '+data.length+'\0').update(data).digest('hex'),
|
|
23
|
+
git_sha256:createHash('sha256').update('blob '+data.length+'\0').update(data).digest('hex'),mode:s.mode&0o111?0o755:0o644};
|
|
24
|
+
}};walk(this.root);return out;
|
|
25
|
+
}
|
|
26
|
+
baseline(){
|
|
27
|
+
this.identity();
|
|
28
|
+
if(this.git(['rev-parse','--show-toplevel']).trim()!==this.root||this.git(['rev-parse','--absolute-git-dir']).trim()!==join(this.root,'.git'))throw Error('DEVELOPMENT_ROOT_CHANGED');
|
|
29
|
+
if(this.git(['symbolic-ref','--short','HEAD']).trim()!==this.spec.branch||this.git(['rev-parse','HEAD']).trim()!==this.b.base_sha)throw Error('DEVELOPMENT_BASELINE_CHANGED');
|
|
30
|
+
if(this.git(['remote','get-url','origin']).trim().replace(/\.git$/,'')!==this.b.url)throw Error('DEVELOPMENT_REMOTE_CHANGED');
|
|
31
|
+
if(this.git(['diff','--cached','--raw','--no-ext-diff','--no-textconv','HEAD']).trim())throw Error('DEVELOPMENT_STAGED_CHANGES');
|
|
32
|
+
const entries=Object.create(null);for(const raw of this.git(['ls-tree','-r','-z','HEAD']).split('\0').filter(Boolean)){
|
|
33
|
+
const m=/^(100644|100755) blob ([a-f0-9]{40}|[a-f0-9]{64})\t(.+)$/.exec(raw);
|
|
34
|
+
if(!m)throw Error('DEVELOPMENT_BASELINE_UNSUPPORTED');developmentPath(m[3]);entries[m[3]]={oid:m[2],mode:m[1]==='100755'?0o755:0o644};
|
|
35
|
+
}return entries;
|
|
36
|
+
}
|
|
37
|
+
check(expectedHash){
|
|
38
|
+
const base=this.baseline(),actual=this.inventory(),allowed=new Map(this.spec.files.map(f=>[f.path,f]));
|
|
39
|
+
for(const f of this.spec.files){
|
|
40
|
+
if(f.action==='create'&&base[f.path]||f.action!=='create'&&!base[f.path])throw Error('DEVELOPMENT_PLAN_BASELINE_MISMATCH');
|
|
41
|
+
if(f.action!=='create'){
|
|
42
|
+
// Binding to HEAD and to the approved sha256 prevents approval on another revision.
|
|
43
|
+
const bytes=this.git(['show',this.b.base_sha+':'+f.path]);
|
|
44
|
+
if(contentHash(Buffer.from(bytes))!==f.base_sha256)throw Error('DEVELOPMENT_PLAN_BASELINE_MISMATCH');
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
for(const f of this.spec.reads)if(!actual[f.path]||actual[f.path].hash!==f.sha256)throw Error('DEVELOPMENT_INPUT_CHANGED');
|
|
48
|
+
for(const p of new Set([...Object.keys(base),...Object.keys(actual)])){
|
|
49
|
+
const f=allowed.get(p),b=base[p],a=actual[p];
|
|
50
|
+
if(!f){if(!a||!b||(b.oid.length===40?a.git_hash:a.git_sha256)!==b.oid||a.mode!==b.mode)throw Error('DEVELOPMENT_OUT_OF_SCOPE_CHANGE');}
|
|
51
|
+
else{
|
|
52
|
+
if(f.action==='modify'&&!a)throw Error('DEVELOPMENT_FILE_CONFLICT');
|
|
53
|
+
if(a&&a.mode!==(b?.mode??0o644))throw Error('DEVELOPMENT_MODE_CHANGED');
|
|
54
|
+
if(expectedHash===null&&((a?.hash??null)!==f.base_sha256))throw Error('DEVELOPMENT_FILE_CONFLICT');
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const manifest=Object.fromEntries(Object.keys(actual).sort().map(p=>[p,{sha256:actual[p].hash,mode:actual[p].mode}]));
|
|
58
|
+
const hash=digest(manifest);if(expectedHash!==null&&hash!==expectedHash)throw Error('DEVELOPMENT_FILE_CONFLICT');
|
|
59
|
+
return {hash,manifest,files:this.spec.files.map(f=>({path:f.path,action:f.action,sha256:actual[f.path]?.hash??null})),reads:this.spec.reads};
|
|
60
|
+
}
|
|
61
|
+
read(expectedHash,path){
|
|
62
|
+
const state=this.check(expectedHash);if(path===undefined)return state;
|
|
63
|
+
if(![...this.spec.files,...this.spec.reads].some(f=>f.path===path))throw Error('DEVELOPMENT_READ_DENIED');
|
|
64
|
+
if(state.manifest[path]&&lstatSync(join(this.root,path)).size>MAX_FILE)throw Error('DEVELOPMENT_READ_LIMIT');
|
|
65
|
+
const content=state.manifest[path]?readFileSync(join(this.root,path),'utf8'):null;
|
|
66
|
+
if(content!==null&&content.includes('\0'))throw Error('DEVELOPMENT_BINARY_EDIT_UNSUPPORTED');
|
|
67
|
+
return {...state,path,content,base_sha256:state.manifest[path]?.sha256??null};
|
|
68
|
+
}
|
|
69
|
+
prepare(expectedHash,{path,content}){
|
|
70
|
+
developmentPath(path);const state=this.check(expectedHash),file=this.spec.files.find(f=>f.path===path);
|
|
71
|
+
if(!file)throw Error('DEVELOPMENT_WRITE_DENIED');
|
|
72
|
+
if(file.action==='delete'?content!==null:typeof content!=='string'||content.includes('\0')||Buffer.byteLength(content)>MAX_FILE)throw Error('DEVELOPMENT_CONTENT_INVALID');
|
|
73
|
+
const manifest=structuredClone(state.manifest);
|
|
74
|
+
if(content===null)delete manifest[path];else manifest[path]={sha256:contentHash(content),mode:state.manifest[path]?.mode??0o644};
|
|
75
|
+
return {hash:digest(manifest),manifest,path,content};
|
|
76
|
+
}
|
|
77
|
+
write(change,expectedHash){
|
|
78
|
+
const prepared=this.prepare(expectedHash,change),dest=join(this.root,prepared.path);
|
|
79
|
+
if(prepared.content===null){try{unlinkSync(dest);}catch(e){absent(e);}}
|
|
80
|
+
else{
|
|
81
|
+
mkdirSync(dirname(dest),{recursive:true});const temp=join(dirname(dest),'.dsh-dev-'+randomUUID());let fd;
|
|
82
|
+
try{fd=openSync(temp,constants.O_WRONLY|constants.O_CREAT|constants.O_EXCL|constants.O_NOFOLLOW,prepared.manifest[prepared.path].mode);
|
|
83
|
+
writeFileSync(fd,prepared.content);fsyncSync(fd);}finally{if(fd!==undefined)closeSync(fd);}
|
|
84
|
+
renameSync(temp,dest);
|
|
85
|
+
}
|
|
86
|
+
let fd=openSync(dirname(dest),'r');try{fsyncSync(fd);}finally{closeSync(fd);}
|
|
87
|
+
this.check(prepared.hash);return prepared.hash;
|
|
88
|
+
}
|
|
89
|
+
snapshot(expectedHash,destination){
|
|
90
|
+
const state=this.check(expectedHash);mkdirSync(destination,{recursive:false,mode:0o755});
|
|
91
|
+
for(const f of [...this.spec.files,...this.spec.reads]){
|
|
92
|
+
if(!state.manifest[f.path])continue;
|
|
93
|
+
const data=readFileSync(join(this.root,f.path));if(contentHash(data)!==state.manifest[f.path].sha256)throw Error('DEVELOPMENT_FILE_CONFLICT');
|
|
94
|
+
const dest=join(destination,f.path);mkdirSync(dirname(dest),{recursive:true});writeFileSync(dest,data,{flag:'wx',mode:0o444});chmodSync(dest,0o444);
|
|
95
|
+
}
|
|
96
|
+
// Host umask may be 077. Read access is limited to this generated snapshot,
|
|
97
|
+
// not granted on any source/credential directory. Container bind stays read-only.
|
|
98
|
+
const readable=dir=>{chmodSync(dir,0o755);for(const name of readdirSync(dir)){const p=join(dir,name);if(lstatSync(p).isDirectory())readable(p);}};readable(destination);
|
|
99
|
+
this.check(state.hash);return state;
|
|
100
|
+
}
|
|
101
|
+
}
|