@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,83 @@
|
|
|
1
|
+
// Private actual SSH push adapter. Default off; constructed only by the opt-in cloud runtime.
|
|
2
|
+
// Async remote observations are cached only for the journal's synchronous atomic
|
|
3
|
+
// gate. The journal checks freshness; CAS protects changes after that observation.
|
|
4
|
+
import {canonical,digest} from './approval-ledger.js';
|
|
5
|
+
import {validatePushIntent,validatePushReadiness,pushReadback} from './delivery-push-plan.js';
|
|
6
|
+
import {DeliveryGitRead} from './delivery-git-read.js';
|
|
7
|
+
import {DeliverySSH} from './delivery-ssh.js';
|
|
8
|
+
const fail=c=>{throw Error('DELIVERY_PUSH_ADAPTER_'+c);};
|
|
9
|
+
const same=(a,b)=>canonical(a)===canonical(b);
|
|
10
|
+
export class DeliveryPush {
|
|
11
|
+
constructor({journal,binding,provider,credentials,receipts,enabled=false,externalDrained=()=>false,now=Date.now},io={}){
|
|
12
|
+
this.j=journal;this.binding=structuredClone(binding);this.provider=provider;this.enabled=enabled;this.externalDrained=externalDrained;this.now=now;this.active=false;
|
|
13
|
+
this.receipts=receipts;
|
|
14
|
+
this.reader=io.reader??new DeliveryGitRead({binding,drained:()=>externalDrained()===true});
|
|
15
|
+
this.ssh=io.ssh??new DeliverySSH({binding,...credentials,enabled});this.preflightCache=null;this.readbackCache=null;
|
|
16
|
+
}
|
|
17
|
+
check(i){
|
|
18
|
+
if(this.enabled!==true)fail('DISABLED');validatePushIntent(i);
|
|
19
|
+
if(!same(this.binding,{root:i.workspace_identity.root,url:i.resource,branch:i.plan.branch,base_sha:i.plan.parent_oid}))fail('BINDING_CHANGED');
|
|
20
|
+
if(this.externalDrained()!==true)fail('DRAIN_REQUIRED');
|
|
21
|
+
const r=this.j.row(i.plan.source_action_id),source=this.j.intent(r);
|
|
22
|
+
if(r.state!=='completed'||source.fingerprint!==i.plan.source_intent_fingerprint||digest(JSON.parse(r.result??'null'))!==i.plan.source_result_fingerprint)fail('SOURCE_CHANGED');return source;
|
|
23
|
+
}
|
|
24
|
+
local(i){
|
|
25
|
+
const source=this.check(i);this.reader.observe(source);return this.reader.capture(source);
|
|
26
|
+
}
|
|
27
|
+
async identity(i){
|
|
28
|
+
const r=await this.provider.identity(i.plan.url);
|
|
29
|
+
if(r.provider!=='gitee'||r.url!==i.plan.url||r.repository_id!==i.plan.repository_id)fail('REMOTE_IDENTITY_CHANGED');return r;
|
|
30
|
+
}
|
|
31
|
+
async remoteBaseline(i,{signal}={}){
|
|
32
|
+
const before=this.local(i);await this.identity(i);const remote=await this.ssh.read(i.plan,{signal});await this.identity(i);
|
|
33
|
+
if(!same(before,this.local(i)))fail('LOCAL_CHANGED');return remote;
|
|
34
|
+
}
|
|
35
|
+
async preflight(i,{signal}={}){
|
|
36
|
+
const before=this.local(i);await this.identity(i);const remote=await this.ssh.read(i.plan,{signal}),observed_at=this.now();await this.identity(i);
|
|
37
|
+
if(remote!==i.plan.expected_remote_oid)fail('REMOTE_CHANGED');
|
|
38
|
+
const ff=remote===null?false:this.reader.ancestor(remote,i.plan.commit_oid,i.plan.format);
|
|
39
|
+
if(remote!==null&&ff!==true)fail('NON_FAST_FORWARD');
|
|
40
|
+
const after=this.local(i);if(!same(before,after))fail('LOCAL_CHANGED');
|
|
41
|
+
const b={kind:'push-state-v1',intent_fingerprint:i.fingerprint,workspace_identity:i.workspace_identity,repository_id:i.plan.repository_id,
|
|
42
|
+
url:i.plan.url,ref:i.plan.ref,remote_oid:remote,commit_oid:i.plan.commit_oid,tree_oid:i.plan.tree_oid,
|
|
43
|
+
manifest_sha256:i.plan.manifest_sha256,local_snapshot_sha256:after.fingerprint};
|
|
44
|
+
const proof={kind:'push-preflight-v1',intent_fingerprint:i.fingerprint,drained:true,local_commit_verified:true,
|
|
45
|
+
fast_forward_verified:ff,observed_at,baseline:{...b,fingerprint:digest(b)}};
|
|
46
|
+
validatePushReadiness(i,proof,this.now());this.preflightCache=proof;return structuredClone(proof);
|
|
47
|
+
}
|
|
48
|
+
cachedPreflight(i){if(this.preflightCache?.intent_fingerprint!==i.fingerprint)fail('PREFLIGHT_REQUIRED');validatePushReadiness(i,this.preflightCache,this.now());return structuredClone(this.preflightCache);}
|
|
49
|
+
async readback(i,{signal}={}){
|
|
50
|
+
const before=this.local(i);await this.identity(i);const remote=await this.ssh.read(i.plan,{signal}),observed_at=this.now();
|
|
51
|
+
await this.identity(i);if(!same(before,this.local(i)))fail('LOCAL_CHANGED');
|
|
52
|
+
const proof={kind:'push-readback-v1',intent_fingerprint:i.fingerprint,workspace_identity:i.workspace_identity,
|
|
53
|
+
repository_id:i.plan.repository_id,url:i.plan.url,ref:i.plan.ref,ref_oid:remote,drained:true,observed_at};
|
|
54
|
+
pushReadback(i,proof,this.now());this.readbackCache=proof;return structuredClone(proof);
|
|
55
|
+
}
|
|
56
|
+
cachedReadback(i){if(this.readbackCache?.intent_fingerprint!==i.fingerprint)fail('READBACK_REQUIRED');pushReadback(i,this.readbackCache,this.now());return structuredClone(this.readbackCache);}
|
|
57
|
+
guard(id,epoch,i){
|
|
58
|
+
this.j.effectAuthority(id,epoch);if(this.j.unfinished(id))fail('DRAIN_REQUIRED');
|
|
59
|
+
const local=this.local(i),proof=this.cachedPreflight(i);
|
|
60
|
+
if(local.fingerprint!==proof.baseline.local_snapshot_sha256)fail('LOCAL_CHANGED');
|
|
61
|
+
}
|
|
62
|
+
async execute(id,epoch,{signal}={}){
|
|
63
|
+
if(this.enabled!==true)fail('DISABLED');if(this.active)fail('IN_FLIGHT');if(signal?.aborted)fail('ABORTED');
|
|
64
|
+
this.active=true;let dispatched=false,operation;
|
|
65
|
+
try{
|
|
66
|
+
const r=this.j.fence(id,epoch),i=this.j.intent(r);if(i.kind!=='push-execution-intent-v1'||r.dispatched)fail('INVALID_ACTION');
|
|
67
|
+
await this.preflight(i,{signal});this.j.markDispatched(id,epoch);dispatched=true;
|
|
68
|
+
try{
|
|
69
|
+
await this.ssh.push(i.plan,{signal,beforeSpawn:()=>{
|
|
70
|
+
this.guard(id,epoch,i);operation=this.j.startOperation(id,epoch,'push',{plan_fingerprint:i.plan.fingerprint});
|
|
71
|
+
return this.receipts?.prepare(operation);
|
|
72
|
+
},onSpawn:({pid})=>this.j.spawned(operation,pid),onDrained:r=>this.j.closed(operation,{code:r.code,signal:r.signal})});
|
|
73
|
+
}catch{/* Never repeat a push. A closed process may still have updated the remote. */}
|
|
74
|
+
if(this.j.unfinished(id))fail('DRAIN_UNKNOWN');
|
|
75
|
+
// Only a fresh readback can settle uncertain acknowledgement; never retry.
|
|
76
|
+
await this.readback(i,{signal});return this.j.finish(id,epoch);
|
|
77
|
+
}catch(e){
|
|
78
|
+
if(!dispatched)throw e;
|
|
79
|
+
try{this.j.recordFailure(id,epoch);}catch{}
|
|
80
|
+
return {...this.j.view(id),review_required:true,error:'DELIVERY_PUSH_UNCERTAIN'};
|
|
81
|
+
}finally{this.active=false;}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
// Private, explicit operator reconciliation. Never resets, retries, repairs an index or closes an unknown child.
|
|
2
|
+
import {constants,lstatSync,fstatSync,realpathSync,openSync,readSync,closeSync,unlinkSync,fsyncSync} from 'node:fs';
|
|
3
|
+
import {join} from 'node:path';
|
|
4
|
+
import {createHash} from 'node:crypto';
|
|
5
|
+
import {canonical,digest} from './approval-ledger.js';
|
|
6
|
+
import {DeliveryGitRead,validateGitBaseline} from './delivery-git-read.js';
|
|
7
|
+
const same=(a,b)=>canonical(a)===canonical(b),sha=b=>createHash('sha256').update(b).digest('hex');
|
|
8
|
+
const fail=code=>{throw Error('DELIVERY_RECOVERY_'+code);};
|
|
9
|
+
const safe=e=>/^DELIVERY_(?:RECOVERY|GIT)_[A-Z_]+$/.test(e?.message)?e.message:'DELIVERY_RECOVERY_EVIDENCE_UNKNOWN';
|
|
10
|
+
const names=['config.lock','index.lock'];
|
|
11
|
+
const stamp=s=>[s.dev,s.ino,s.size,s.mtimeNs,s.ctimeNs].map(String).join(':');
|
|
12
|
+
const statId=s=>({device:String(s.dev),inode:String(s.ino)});
|
|
13
|
+
const absent=p=>{try{return lstatSync(p,{bigint:true});}catch(e){if(e.code==='ENOENT')return null;throw e;}};
|
|
14
|
+
export class DeliveryRecoveryLocks {
|
|
15
|
+
constructor(identity,tag,records,index=null){this.identity=structuredClone(identity);this.tag=structuredClone(tag);this.records=structuredClone(records);this.index=index;this.gd=join(identity.root,'.git');}
|
|
16
|
+
checkRoot(){for(const [p,id] of [[this.identity.root,this.identity.directory],[this.gd,this.identity.git_directory]]){const s=lstatSync(p,{bigint:true});if(!s.isDirectory()||s.isSymbolicLink()||realpathSync(p)!==p||!same(statId(s),id))fail('DIRECTORY_CHANGED');}}
|
|
17
|
+
read(name){
|
|
18
|
+
if(!names.includes(name))fail('LOCK_NAME_DENIED');this.checkRoot();const path=join(this.gd,name),before=absent(path);if(!before)return {name,state:'absent'};
|
|
19
|
+
const expected=this.records[name];if(!expected||!before.isFile()||before.isSymbolicLink()||before.nlink!==1n||before.size>8388608n||!same(statId(before),expected))fail('FOREIGN_LOCK');
|
|
20
|
+
const fd=openSync(path,constants.O_RDONLY|constants.O_NOFOLLOW|constants.O_NONBLOCK);
|
|
21
|
+
try{
|
|
22
|
+
if(stamp(fstatSync(fd,{bigint:true}))!==stamp(before))fail('LOCK_CHANGED');
|
|
23
|
+
const b=Buffer.alloc(Number(before.size)+1);let n=0,count;while(n<b.length&&(count=readSync(fd,b,n,b.length-n,null))>0)n+=count;
|
|
24
|
+
if(n!==Number(before.size)||stamp(fstatSync(fd,{bigint:true}))!==stamp(before)||stamp(lstatSync(path,{bigint:true}))!==stamp(before))fail('LOCK_CHANGED');
|
|
25
|
+
const data=b.subarray(0,n),hash=sha(data),marker=Buffer.from(canonical(this.tag)+'\n');
|
|
26
|
+
const kind=data.equals(marker)?'owner-marker':name==='index.lock'&&this.index?.sha256===hash&&this.index.bytes===n?'prepared-index':null;
|
|
27
|
+
if(!kind)fail('LOCK_CONTENT_UNKNOWN');
|
|
28
|
+
return {name,state:'owned',identity:expected,sha256:hash,bytes:n,stamp:stamp(before),kind};
|
|
29
|
+
}finally{closeSync(fd);}
|
|
30
|
+
}
|
|
31
|
+
scan(){this.checkRoot();return names.map(n=>this.read(n));}
|
|
32
|
+
release(expected,beforeEach=()=>{}){
|
|
33
|
+
if(!same(this.scan(),expected))fail('STALE_LOCK_EVIDENCE');
|
|
34
|
+
// No mkdir/open-for-write/rename. Delete only exact ledger-owned files, never another Git lock.
|
|
35
|
+
for(const e of [...expected].reverse()){
|
|
36
|
+
if(e.state!=='owned')continue;beforeEach(e.name);if(!same(this.read(e.name),e))fail('LOCK_CHANGED');this.checkRoot();
|
|
37
|
+
unlinkSync(join(this.gd,e.name));const fd=openSync(this.gd,'r');try{fsyncSync(fd);}finally{closeSync(fd);}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
export function classifyDelivery(intent,before,current,index=null){
|
|
42
|
+
validateGitBaseline(intent,before);const {fingerprint,...body}=current??{},p=intent.plan;
|
|
43
|
+
if(!fingerprint||digest(body)!==fingerprint||current.kind!=='git-state-v1'||current.intent_fingerprint!==intent.fingerprint||
|
|
44
|
+
!same(current.workspace_identity,intent.workspace_identity)||current.branch!==p.branch||current.manifest_sha256!==p.manifest_sha256||
|
|
45
|
+
current.parent_tree_oid!==before.parent_tree_oid||!Array.isArray(current.objects_present)||
|
|
46
|
+
new Set(current.objects_present).size!==current.objects_present.length||!same(current.objects_present,[...current.objects_present].sort())||current.objects_present.some(o=>!p.objects.some(x=>x.oid===o)))fail('SNAPSHOT_INVALID');
|
|
47
|
+
if(!current.metadata||!same(Object.keys(current.metadata).sort(),Object.keys(before.metadata).sort())||
|
|
48
|
+
Object.entries(current.metadata).some(([k,v])=>k==='index_sha256'&&v===null?false:typeof v!=='string'||!/^[a-f0-9]{64}$/.test(v)))fail('SNAPSHOT_INVALID');
|
|
49
|
+
if(current.metadata.config_sha256!==before.metadata.config_sha256||current.metadata.head_sha256!==before.metadata.head_sha256)return 'metadata_conflict';
|
|
50
|
+
if(before.objects_present.some(o=>!current.objects_present.includes(o)))return 'object_store_conflict';
|
|
51
|
+
if(current.head_oid!==current.ref_oid)return 'reference_conflict';
|
|
52
|
+
const original=current.head_oid===p.parent,committed=current.head_oid===p.computed_commit_oid,all=current.objects_present.length===p.objects.length;
|
|
53
|
+
if(!original&&!committed)return 'reference_conflict';
|
|
54
|
+
if(same(current,before))return 'no_effect';
|
|
55
|
+
const oldIndex=current.index_tree_oid===before.index_tree_oid&¤t.metadata.index_sha256===before.metadata.index_sha256;
|
|
56
|
+
if(committed&&all&¤t.index_tree_oid===p.tree_oid)return 'commit_complete';
|
|
57
|
+
if(committed&&all&&oldIndex)return 'reference_applied_index_pending';
|
|
58
|
+
if(original&&oldIndex&¤t.objects_present.length>before.objects_present.length)return 'objects_present_no_commit';
|
|
59
|
+
if(original&¤t.index_tree_oid===p.tree_oid&&index&¤t.metadata.index_sha256===index.sha256)return 'index_without_reference';
|
|
60
|
+
return 'state_conflict';
|
|
61
|
+
}
|
|
62
|
+
export class DeliveryRecovery {
|
|
63
|
+
constructor({journal,binding,enabled=false,quiescent=()=>false},io={}){
|
|
64
|
+
this.j=journal;this.db=journal.db;this.binding=structuredClone(binding);this.enabled=enabled;this.quiescent=quiescent;this.active=false;
|
|
65
|
+
this.makeLocks=io.makeLocks??((...a)=>new DeliveryRecoveryLocks(...a));
|
|
66
|
+
this.makeReader=io.makeReader??(proofs=>new DeliveryGitRead({binding:this.binding,drained:()=>this.quiescent()===true,ownedLocks:()=>proofs}));
|
|
67
|
+
this.db.exec(`CREATE TABLE IF NOT EXISTS delivery_recoveries(event_id TEXT PRIMARY KEY,fingerprint TEXT NOT NULL,action_id TEXT NOT NULL,epoch INTEGER NOT NULL,
|
|
68
|
+
decision TEXT NOT NULL,evidence_hash TEXT NOT NULL,state TEXT NOT NULL,created INTEGER NOT NULL,result TEXT);`);
|
|
69
|
+
}
|
|
70
|
+
guard(id,epoch){if(this.enabled!==true)fail('DISABLED');return this.j.fence(id,epoch,['needs_review']);}
|
|
71
|
+
evidenceRows(id,epoch){
|
|
72
|
+
const operations=this.db.prepare('SELECT * FROM delivery_operations WHERE action_id=? ORDER BY rowid').all(id);
|
|
73
|
+
if(operations.some(o=>o.epoch!==epoch))fail('OPERATION_EPOCH_CHANGED');
|
|
74
|
+
const records={},events=this.db.prepare("SELECT seq,type,data,created FROM delivery_events WHERE action_id=? AND type IN ('lock_owned','index_planned') ORDER BY seq").all(id);let index=null;
|
|
75
|
+
for(const e of events){const p=JSON.parse(e.data);if(p.epoch!==epoch)fail('LOCK_EPOCH_CHANGED');
|
|
76
|
+
if(e.type==='lock_owned'){
|
|
77
|
+
if(!names.includes(p.name)||records[p.name]||!p.identity||Object.keys(p.identity).sort().join(',')!=='device,inode'||Object.values(p.identity).some(x=>typeof x!=='string'||!/^[0-9]{1,30}$/.test(x)))fail('LOCK_RECORD_INVALID');
|
|
78
|
+
records[p.name]=p.identity;
|
|
79
|
+
}else{if(index)fail('INDEX_RECORD_INVALID');index=p;}
|
|
80
|
+
}
|
|
81
|
+
if(Object.keys(records).length&&operations.filter(o=>o.kind==='lock').length!==1)fail('LOCK_RECORD_INVALID');
|
|
82
|
+
return {operations,events,records,index};
|
|
83
|
+
}
|
|
84
|
+
inspect(id,epoch){
|
|
85
|
+
const row=this.guard(id,epoch),intent=this.j.intent(row),before=row.before_snapshot?JSON.parse(row.before_snapshot):null;
|
|
86
|
+
if(!before)fail('BASELINE_REQUIRED');validateGitBaseline(intent,before);
|
|
87
|
+
if(!same(this.binding,{root:intent.workspace_identity.root,url:intent.resource,branch:intent.plan.branch,base_sha:intent.plan.parent}))fail('BINDING_CHANGED');
|
|
88
|
+
const records=this.evidenceRows(id,epoch),task=this.j.store.task(row.task_id);if(task.revision!==intent.task_revision)fail('TASK_REVISION_CHANGED');
|
|
89
|
+
const base={schema:1,action_id:id,epoch,intent_fingerprint:intent.fingerprint,before_fingerprint:before.fingerprint,
|
|
90
|
+
task:{owner:task.owner,epoch:task.epoch,busy:!!task.busy,blocked:!!task.blocked,revision:task.revision},
|
|
91
|
+
dispatched:!!row.dispatched,state:row.state,operations:records.operations,ownership_events:records.events,
|
|
92
|
+
external_quiescent:this.quiescent()===true,classification:'drain_unknown',locks:null,current:null,error:null,allowed:[],grants_execution:false};
|
|
93
|
+
if(!base.external_quiescent||records.operations.some(o=>o.state!=='closed'))return {evidence:base,fingerprint:digest(base)};
|
|
94
|
+
try{
|
|
95
|
+
if(records.index&&(records.index.plan_fingerprint!==intent.plan.fingerprint||records.index.tree_oid!==intent.plan.tree_oid||
|
|
96
|
+
!/^[a-f0-9]{64}$/.test(records.index.sha256??'')||!Number.isSafeInteger(records.index.bytes)||records.index.bytes<1||records.index.bytes>8388608))fail('INDEX_RECORD_INVALID');
|
|
97
|
+
const set=this.makeLocks(intent.workspace_identity,{action_id:id,epoch,intent_fingerprint:intent.fingerprint},records.records,records.index),locks=set.scan();
|
|
98
|
+
const proofs=Object.fromEntries(locks.filter(l=>l.state==='owned').map(l=>[l.name,l.identity]));
|
|
99
|
+
const reader=this.makeReader(proofs),current=reader.capture(intent);
|
|
100
|
+
if(!same(set.scan(),locks)||this.quiescent()!==true)fail('EVIDENCE_CHANGED');this.guard(id,epoch);
|
|
101
|
+
if(!same(this.evidenceRows(id,epoch),records))fail('EVIDENCE_CHANGED');
|
|
102
|
+
base.classification=classifyDelivery(intent,before,current,records.index);base.locks=locks;base.current=current;
|
|
103
|
+
const owned=locks.some(l=>l.state==='owned');
|
|
104
|
+
if(owned&&['no_effect','objects_present_no_commit','reference_applied_index_pending','commit_complete'].includes(base.classification))base.allowed=['clean_owned_locks'];
|
|
105
|
+
if(!owned&&base.classification==='no_effect')base.allowed=['settle_not_applied'];
|
|
106
|
+
if(!owned&&base.classification==='commit_complete')base.allowed=['settle_completed'];
|
|
107
|
+
}catch(e){base.classification='evidence_unknown';base.error=safe(e);base.allowed=[];}
|
|
108
|
+
return {evidence:base,fingerprint:digest(base)};
|
|
109
|
+
}
|
|
110
|
+
result(row){return row.result?{...JSON.parse(row.result),replayed:true,checked_now:false}:{event_id:row.event_id,action_id:row.action_id,state:'needs_review',replayed:true,checked_now:false,retry_authorized:false};}
|
|
111
|
+
apply(a){
|
|
112
|
+
if(!a||Object.keys(a).sort().join(',')!=='decision,epoch,event_id,evidence_fingerprint,id'||typeof a.id!=='string'||!a.id||a.id.length>128||typeof a.event_id!=='string'||!a.event_id||a.event_id.length>256||
|
|
113
|
+
!Number.isSafeInteger(a.epoch)||a.epoch<1||!/^[a-f0-9]{64}$/.test(a.evidence_fingerprint??'')||!['clean_owned_locks','settle_not_applied','settle_completed'].includes(a.decision))fail('INVALID_REQUEST');
|
|
114
|
+
if(this.enabled!==true)fail('DISABLED');this.j.owner();const fingerprint=digest(a),old=this.db.prepare('SELECT * FROM delivery_recoveries WHERE event_id=?').get(a.event_id);
|
|
115
|
+
if(old){if(old.fingerprint!==fingerprint)fail('EVENT_CONFLICT');return this.result(old);}if(this.active)fail('IN_FLIGHT');this.active=true;
|
|
116
|
+
try{
|
|
117
|
+
const snapshot=this.inspect(a.id,a.epoch);if(snapshot.fingerprint!==a.evidence_fingerprint)fail('STALE_EVIDENCE');
|
|
118
|
+
if(!snapshot.evidence.allowed.includes(a.decision))fail('DECISION_DENIED');
|
|
119
|
+
this.db.prepare("INSERT INTO delivery_recoveries VALUES (?,?,?,?,?,?,'intent',?,NULL)").run(a.event_id,fingerprint,a.id,a.epoch,a.decision,a.evidence_fingerprint,this.j.now());
|
|
120
|
+
let value;
|
|
121
|
+
try{
|
|
122
|
+
if(a.decision==='clean_owned_locks'){
|
|
123
|
+
const row=this.guard(a.id,a.epoch),intent=this.j.intent(row),r=this.evidenceRows(a.id,a.epoch);
|
|
124
|
+
const set=this.makeLocks(intent.workspace_identity,{action_id:a.id,epoch:a.epoch,intent_fingerprint:intent.fingerprint},r.records,r.index);
|
|
125
|
+
const op=this.j.startOperation(a.id,a.epoch,'unlock',{review_event:a.event_id,evidence_hash:a.evidence_fingerprint});let code=1,async=false;
|
|
126
|
+
try{const result=set.release(snapshot.evidence.locks,name=>{this.guard(a.id,a.epoch);if(this.quiescent()!==true)fail('DRAIN_UNKNOWN');this.j.event(a.id,'recovery_lock_remove',{epoch:a.epoch,name,operation:op});});if(result&&typeof result.then==='function'){async=true;fail('ASYNC_CLEANUP_DENIED');}code=0;}
|
|
127
|
+
finally{if(!async)this.j.closed(op,{code});}
|
|
128
|
+
value={action:this.j.view(a.id),released_task:false,locks_cleaned:true,requires_fresh_review:true};
|
|
129
|
+
}else{
|
|
130
|
+
this.guard(a.id,a.epoch);if(this.quiescent()!==true)fail('DRAIN_UNKNOWN');
|
|
131
|
+
// Journal performs a separate fresh observer call, not a caller-provided success flag.
|
|
132
|
+
const action=a.decision==='settle_completed'?this.j.finish(a.id,a.epoch):this.j.settleUnapplied(a.id,a.epoch);
|
|
133
|
+
value={action,released_task:['completed','not_applied'].includes(action.state),locks_cleaned:false};
|
|
134
|
+
}
|
|
135
|
+
const result={event_id:a.event_id,action_id:a.id,decision:a.decision,state:value.action.state==='needs_review'&&!value.locks_cleaned?'needs_review':'recorded',...value,retry_authorized:false};
|
|
136
|
+
this.db.prepare('UPDATE delivery_recoveries SET state=?,result=? WHERE event_id=?').run(result.state,canonical(result),a.event_id);return result;
|
|
137
|
+
}catch(e){
|
|
138
|
+
const result={event_id:a.event_id,action_id:a.id,state:'needs_review',error:safe(e),retry_authorized:false,released_task:null,current_task_state:'query_ledger'};
|
|
139
|
+
this.db.prepare("UPDATE delivery_recoveries SET state='needs_review',result=? WHERE event_id=?").run(canonical(result),a.event_id);return result;
|
|
140
|
+
}
|
|
141
|
+
}finally{this.active=false;}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
// Opt-in cloud orchestration over the existing coordinator and approval ledger.
|
|
2
|
+
// Staging is metadata-only. Scheduling requires a separate operator execution
|
|
3
|
+
// switch AND a current exact trusted approval. No action is queued by approval.
|
|
4
|
+
import {DeliveryJournal} from './delivery-journal.js';
|
|
5
|
+
import {DeliveryApproval} from './delivery-approval.js';
|
|
6
|
+
import {DeliveryPushApproval} from './delivery-push-approval.js';
|
|
7
|
+
import {DeliveryPush} from './delivery-push.js';
|
|
8
|
+
import {DeliveryProcessReceipts} from './delivery-process-receipts.js';
|
|
9
|
+
import {DeliveryPushRecovery} from './delivery-push-recovery.js';
|
|
10
|
+
import {realpathSync} from 'node:fs';
|
|
11
|
+
import {join} from 'node:path';
|
|
12
|
+
import {compilePush} from './delivery-push-plan.js';
|
|
13
|
+
import {DeliveryGitRead} from './delivery-git-read.js';
|
|
14
|
+
import {DeliveryCommit} from './delivery-commit.js';
|
|
15
|
+
import {DeliveryRecovery} from './delivery-recovery.js';
|
|
16
|
+
import {canonical} from './approval-ledger.js';
|
|
17
|
+
const fail=c=>{throw Error('DELIVERY_RUNTIME_'+c);};
|
|
18
|
+
const has=(db,t)=>!!db.prepare("SELECT 1 FROM sqlite_master WHERE type='table' AND name=?").get(t);
|
|
19
|
+
const fields=(a,keys)=>a&&typeof a==='object'&&!Array.isArray(a)&&Object.keys(a).length===keys.length&&keys.every(k=>Object.hasOwn(a,k));
|
|
20
|
+
const event=x=>typeof x==='string'&&x.length>0&&x.length<=180&&!/[\x00-\x1f\x7f]/.test(x);
|
|
21
|
+
const identifier=x=>typeof x==='string'&&x.length>0&&x.length<=128&&!/[\x00-\x1f\x7f]/.test(x);
|
|
22
|
+
export class DeliveryRuntime {
|
|
23
|
+
constructor(g,c,io={}){
|
|
24
|
+
if(g.c.role!=='cloud'||!g.scheduler||!g.development||c.enabled!==true)fail('DISABLED');
|
|
25
|
+
if(Object.keys(c).some(k=>!['enabled','execute','author','committer','push'].includes(k))||c.execute!==undefined&&typeof c.execute!=='boolean')fail('CONFIG');
|
|
26
|
+
if(c.push!==undefined&&(!fields(c.push,['enabled','execute','keyFile','knownHostsFile'])||typeof c.push.enabled!=='boolean'||typeof c.push.execute!=='boolean'||c.push.execute&&!c.push.enabled||
|
|
27
|
+
[c.push.keyFile,c.push.knownHostsFile].some(p=>typeof p!=='string'||!p.startsWith('/')||p.length>2048||/[\x00-\x1f\x7f]/.test(p))))fail('PUSH_CONFIG');
|
|
28
|
+
if(c.push?.enabled&&process.platform!=='linux'&&!io.push)fail('PUSH_PLATFORM_UNSUPPORTED');
|
|
29
|
+
for(const v of [c.author,c.committer])if(!fields(v,['name','email'])||typeof v.name!=='string'||!v.name||v.name.length>256||typeof v.email!=='string'||!v.email||v.email.length>254||/[<>\x00-\x1f\x7f]/.test(v.name+v.email))fail('IDENTITY_REQUIRED');
|
|
30
|
+
this.g=g;this.c=structuredClone(c);this.io=io;this.executionEnabled=c.execute===true;this.closed=false;this.started=false;this.pending=null;this.scanAfter=0;
|
|
31
|
+
this.pushEnabled=c.push?.enabled===true;this.pushExecutionEnabled=this.pushEnabled&&c.push.execute===true;this.pushEngines=new Map();this.preparations=new Map();
|
|
32
|
+
this.journal=new DeliveryJournal({store:g.store,enabled:true,pushEnabled:this.pushEnabled,now:g.now,exclusive:()=>this.exclusive(),
|
|
33
|
+
authorize:a=>(this.pushApproval??this.approval).authorize(a),preflight:i=>i.kind==='push-execution-intent-v1'?this.push(i.task_id).cachedPreflight(i):this.reader(i.task_id).preflight(i),
|
|
34
|
+
observe:i=>i.kind==='push-execution-intent-v1'?this.push(i.task_id).cachedReadback(i):this.reader(i.task_id).observe(i),observeUnapplied:(i,b)=>this.reader(i.task_id).unchanged(i,b)});
|
|
35
|
+
this.approval=new DeliveryApproval({ledger:g.approvals,preview:g.delivery,journal:this.journal,enabled:true});
|
|
36
|
+
if(this.pushEnabled)this.pushApproval=new DeliveryPushApproval({commit:this.approval,journal:this.journal,enabled:true});
|
|
37
|
+
g.approvals.delivery=this.pushApproval??this.approval;
|
|
38
|
+
}
|
|
39
|
+
status(){return {staging_enabled:true,execution_enabled:this.executionEnabled,started:this.started&&!this.closed,active_runs:this.pending?1:0,push_staging_enabled:this.pushEnabled,push_enabled:this.pushExecutionEnabled,publish_enabled:false,grants_execution:false};}
|
|
40
|
+
busy(task){return !!this.g.store.db.prepare("SELECT 1 FROM delivery_actions WHERE task_id=? AND state IN ('queued','active','needs_review')").get(task);}
|
|
41
|
+
exclusive(){const s=this.g.scheduler;return s.started&&!s.closed&&s.lock?.listening===true;}
|
|
42
|
+
assertOpen(){if(this.closed||this.g.closed||!this.started||!this.exclusive())fail('EXCLUSIVE_OWNER_REQUIRED');}
|
|
43
|
+
binding(task){
|
|
44
|
+
const b=this.g.development.gate.bindings[task];if(!b)fail('BINDING_REQUIRED');
|
|
45
|
+
return {root:b.root,url:b.url,branch:b.branch,base_sha:b.base_sha};
|
|
46
|
+
}
|
|
47
|
+
drained(task){
|
|
48
|
+
if(!this.exclusive())return false;
|
|
49
|
+
const g=this.g,db=g.store.db,b=this.binding(task);
|
|
50
|
+
if(g.store.task(task).blocked||[...g.scheduler.active.values()].some(a=>a.task_id===task)||g.a2a?.busy(task))return false;
|
|
51
|
+
if(has(db,'development_operations')&&db.prepare("SELECT 1 FROM development_operations o JOIN development_requests r ON r.id=o.request_id WHERE r.resource=? AND o.state<>'closed'").get(b.url))return false;
|
|
52
|
+
for(const id of g.development.gate.inflight.keys())if(g.development.gate.row(id).resource===b.url)return false;
|
|
53
|
+
if(db.prepare("SELECT 1 FROM delivery_operations o JOIN delivery_actions r ON r.id=o.action_id WHERE r.resource=? AND o.state<>'closed'").get(b.url))return false;
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
reader(task){return this.io.reader?this.io.reader(task):new DeliveryGitRead({binding:this.binding(task),drained:()=>this.drained(task)});}
|
|
57
|
+
push(task){
|
|
58
|
+
if(!this.pushEnabled)fail('PUSH_DISABLED');
|
|
59
|
+
if(!this.pushEngines.has(task))this.pushEngines.set(task,this.io.push?this.io.push(task):new DeliveryPush({journal:this.journal,binding:this.binding(task),
|
|
60
|
+
provider:this.g.repositories.provider,credentials:this.c.push,receipts:this.processReceipts(),enabled:true,externalDrained:()=>this.drained(task),now:this.g.now}));
|
|
61
|
+
return this.pushEngines.get(task);
|
|
62
|
+
}
|
|
63
|
+
stagePush(node,a){
|
|
64
|
+
this.assertOpen();if(!this.pushEnabled)fail('PUSH_DISABLED');
|
|
65
|
+
if(!['local','cloud'].includes(node)||!fields(a,['task_id','commit_action_id','event_id'])||!event(a.event_id)||!identifier(a.task_id)||!/^delivery-[a-f0-9-]{36}$/.test(a.commit_action_id??''))fail('INVALID_PUSH_STAGE');
|
|
66
|
+
const event_id='runtime:'+node+':'+a.event_id,old=this.g.store.db.prepare('SELECT snapshot FROM delivery_push_plans WHERE event_id=?').get(event_id);
|
|
67
|
+
if(old){const value=JSON.parse(old.snapshot),s=this.pushApproval.stored(a.task_id,value.id,value.fingerprint);
|
|
68
|
+
if(s.intent.plan.source_action_id!==a.commit_action_id)fail('EVENT_CONFLICT');return {reference:this.pushApproval.reference(s),replayed:true,remote_checked_now:false,grants_execution:false};}
|
|
69
|
+
if(this.preparations.has(a.task_id))fail('PUSH_PREPARATION_BUSY');
|
|
70
|
+
const controller=new AbortController(),entry={controller,promise:null};this.preparations.set(a.task_id,entry);
|
|
71
|
+
entry.promise=Promise.resolve().then(async()=>{
|
|
72
|
+
this.assertOpen();const source=this.pushApproval.source(a.task_id,a.commit_action_id),engine=this.push(a.task_id);
|
|
73
|
+
const compile=expected_remote_oid=>compilePush({source_action_id:source.row.id,commit_intent:source.intent,commit_result:source.result,repository_id:source.repository.repository_id,expected_remote_oid});
|
|
74
|
+
const input={task_id:a.task_id,resource:source.intent.resource,workspace_identity:source.intent.workspace_identity,plan:compile(null)};
|
|
75
|
+
input.plan=compile(await engine.remoteBaseline(this.journal.prepare(input),{signal:controller.signal}));
|
|
76
|
+
await engine.preflight(this.journal.prepare(input),{signal:controller.signal});this.assertOpen();
|
|
77
|
+
return {...this.pushApproval.stage({...input,event_id}),remote_checked_now:true};
|
|
78
|
+
}).finally(()=>this.preparations.delete(a.task_id));return entry.promise;
|
|
79
|
+
}
|
|
80
|
+
schedulePush(node,a){
|
|
81
|
+
this.assertOpen();if(!this.pushExecutionEnabled)fail('PUSH_EXECUTION_DISABLED');
|
|
82
|
+
if(!['local','cloud'].includes(node)||!fields(a,['task_id','approval_id','event_id'])||!event(a.event_id)||!identifier(a.task_id)||!identifier(a.approval_id))fail('INVALID_PUSH_SCHEDULE');
|
|
83
|
+
const r=this.g.approvals.row(a.approval_id),plan=JSON.parse(r.proposal).delivery;
|
|
84
|
+
if(r.task_id!==a.task_id||plan?.kind!=='push-execution-v1')fail('PUSH_APPROVAL_REQUIRED');
|
|
85
|
+
const s=this.pushApproval.stored(a.task_id,plan.plan_id,plan.fingerprint),i=s.intent;
|
|
86
|
+
if(canonical(this.binding(a.task_id))!==canonical({root:i.workspace_identity.root,url:i.resource,branch:i.plan.branch,base_sha:i.plan.parent_oid}))fail('BINDING_CHANGED');
|
|
87
|
+
return {action:this.journal.enqueue({...i,approval_id:a.approval_id,event_id:'runtime:'+node+':'+a.event_id}),execution:'queued_or_recorded',grants_execution:false};
|
|
88
|
+
}
|
|
89
|
+
stage(node,a){
|
|
90
|
+
this.assertOpen();
|
|
91
|
+
if(!['local','cloud'].includes(node)||!fields(a,['task_id','preview_id','fingerprint','commit_message','event_id'])||!event(a.event_id)||
|
|
92
|
+
!identifier(a.task_id)||!/^delivery-[a-f0-9-]{36}$/.test(a.preview_id??'')||!/^[a-f0-9]{64}$/.test(a.fingerprint??'')||
|
|
93
|
+
typeof a.commit_message!=='string'||!a.commit_message.trim()||a.commit_message.length>2000||/[\x00-\x09\x0b-\x1f\x7f]/.test(a.commit_message))fail('INVALID_STAGE');
|
|
94
|
+
const event_id='runtime:'+node+':'+a.event_id,db=this.g.store.db;
|
|
95
|
+
const old=db.prepare('SELECT snapshot FROM delivery_approval_plans WHERE event_id=?').get(event_id);
|
|
96
|
+
if(old){
|
|
97
|
+
const value=JSON.parse(old.snapshot),s=this.approval.stored(a.task_id,value.id,value.fingerprint);
|
|
98
|
+
if(s.preview_id!==a.preview_id||s.preview_fingerprint!==a.fingerprint||s.intent.plan.message!==(a.commit_message.endsWith('\n')?a.commit_message:a.commit_message+'\n'))fail('EVENT_CONFLICT');
|
|
99
|
+
return {reference:this.approval.reference(s),replayed:true,filesystem_checked_now:false,grants_execution:false};
|
|
100
|
+
}
|
|
101
|
+
const p=this.g.delivery.stored(a.task_id,a.preview_id,a.fingerprint);this.g.delivery.checked(p);
|
|
102
|
+
const reader=this.reader(a.task_id),compiled=reader.compile({parent:p.baseline_commit,branch:p.workspace_branch,
|
|
103
|
+
changes:p.files.map(f=>({path:f.path,action:f.approved_action,before_sha256:f.before_sha256,after_sha256:f.after_sha256})),
|
|
104
|
+
tested_content:{kind:p.tested_content.kind,sha256:p.tested_content.sha256},author:this.c.author,committer:this.c.committer,
|
|
105
|
+
when:Math.floor(this.g.now()/1000),message:a.commit_message});
|
|
106
|
+
return {...this.approval.stage({event_id,task_id:a.task_id,preview_id:a.preview_id,fingerprint:a.fingerprint,resource:p.repository.url,...compiled}),filesystem_checked_now:true};
|
|
107
|
+
}
|
|
108
|
+
schedule(node,a){
|
|
109
|
+
this.assertOpen();if(!this.executionEnabled)fail('EXECUTION_DISABLED');
|
|
110
|
+
if(!['local','cloud'].includes(node)||!fields(a,['task_id','approval_id','event_id'])||!event(a.event_id)||!identifier(a.task_id)||!identifier(a.approval_id))fail('INVALID_SCHEDULE');
|
|
111
|
+
const r=this.g.approvals.row(a.approval_id),plan=JSON.parse(r.proposal).delivery;
|
|
112
|
+
if(r.task_id!==a.task_id||plan?.kind!=='commit-execution-v1')fail('APPROVAL_REQUIRED');
|
|
113
|
+
const s=this.approval.stored(a.task_id,plan.plan_id,plan.fingerprint),i=s.intent;
|
|
114
|
+
if(canonical(this.binding(a.task_id))!==canonical({root:i.workspace_identity.root,url:i.resource,branch:i.plan.branch,base_sha:i.plan.parent}))fail('BINDING_CHANGED');
|
|
115
|
+
// Exact stored objects, not a live workspace read: a busy task can safely queue.
|
|
116
|
+
return {action:this.journal.enqueue({task_id:a.task_id,resource:i.resource,workspace_identity:i.workspace_identity,plan:i.plan,
|
|
117
|
+
objects:this.approval.objects(s),approval_id:a.approval_id,event_id:'runtime:'+node+':'+a.event_id}),execution:'queued_or_recorded',grants_execution:false};
|
|
118
|
+
}
|
|
119
|
+
start(){
|
|
120
|
+
if(this.started)return;if(this.closed||!this.exclusive())fail('EXCLUSIVE_OWNER_REQUIRED');
|
|
121
|
+
this.journal.recover();this.started=true; // Active -> review only; no retry/unlock.
|
|
122
|
+
}
|
|
123
|
+
tick(){
|
|
124
|
+
if(this.g.maintenance?.blocked||this.closed||!this.started||(!this.executionEnabled&&!this.pushExecutionEnabled)||this.pending||!this.exclusive())return;
|
|
125
|
+
const select=this.g.store.db.prepare("SELECT id,seq FROM delivery_actions WHERE state='queued' AND seq>? ORDER BY seq LIMIT 50");
|
|
126
|
+
let rows=select.all(this.scanAfter);if(!rows.length){this.scanAfter=0;rows=select.all(0);}
|
|
127
|
+
for(const {id,seq} of rows){
|
|
128
|
+
this.scanAfter=seq; // Bounded scan without starving unrelated tasks behind 50 busy rows.
|
|
129
|
+
if(this.journal.waiting(id))continue;
|
|
130
|
+
let claimed=false;
|
|
131
|
+
try{
|
|
132
|
+
const r=this.journal.row(id);
|
|
133
|
+
const intent=this.journal.intent(r);
|
|
134
|
+
if(intent.kind==='push-execution-intent-v1'){
|
|
135
|
+
if(!this.pushExecutionEnabled||this.preparations.has(r.task_id)||!this.drained(r.task_id))continue;
|
|
136
|
+
this.controller=new AbortController();const signal=this.controller.signal;
|
|
137
|
+
this.pending=this.runPush(id,intent,signal).finally(()=>{this.pending=null;this.controller=null;});return;
|
|
138
|
+
}
|
|
139
|
+
if(!this.executionEnabled||this.preparations.has(r.task_id))continue;
|
|
140
|
+
if(!this.drained(r.task_id))continue;
|
|
141
|
+
const a=this.journal.acquire(id);if(!a.acquired)continue;claimed=true;
|
|
142
|
+
const request=this.g.approvals.row(r.approval_id),p=JSON.parse(request.proposal).delivery;
|
|
143
|
+
const s=this.approval.stored(r.task_id,p.plan_id,p.fingerprint),objects=this.approval.objects(s),binding=this.binding(r.task_id);
|
|
144
|
+
const executor=this.io.commit?this.io.commit(r.task_id):new DeliveryCommit({journal:this.journal,binding,enabled:true,externalDrained:()=>this.drained(r.task_id)});
|
|
145
|
+
this.controller=new AbortController();
|
|
146
|
+
this.pending=Promise.resolve().then(()=>executor.execute(id,a.epoch,objects,{signal:this.controller.signal})).catch(()=>{
|
|
147
|
+
this.journal.recordFailure(id,a.epoch);
|
|
148
|
+
}).catch(()=>{/* Lost ownership retains the old fence; never repair implicitly. */}).finally(()=>{this.pending=null;this.controller=null;});
|
|
149
|
+
return;
|
|
150
|
+
}catch{
|
|
151
|
+
if(claimed){try{this.journal.recordFailure(id,this.journal.row(id).epoch);}catch{}}
|
|
152
|
+
else this.journal.cancelQueued(id,'superseded'); // No silent retry after admission failure.
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
async runPush(id,intent,signal){
|
|
157
|
+
let epoch;
|
|
158
|
+
try{
|
|
159
|
+
const executor=this.push(intent.task_id);await executor.preflight(intent,{signal});
|
|
160
|
+
this.assertOpen();if(signal.aborted)fail('ABORTED');
|
|
161
|
+
const a=this.journal.acquire(id);if(!a.acquired)return;epoch=a.epoch;
|
|
162
|
+
await executor.execute(id,epoch,{signal});
|
|
163
|
+
}catch{
|
|
164
|
+
if(epoch!==undefined){try{this.journal.recordFailure(id,epoch);}catch{}}
|
|
165
|
+
else if(!this.closed){try{this.journal.cancelQueued(id,'preflight_failed');}catch{}} // No send; never loop an unavailable preflight.
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
recovery(task){
|
|
169
|
+
this.assertOpen();
|
|
170
|
+
return new DeliveryRecovery({journal:this.journal,binding:this.binding(task),enabled:true,quiescent:()=>!this.pending&&this.drained(task)});
|
|
171
|
+
}
|
|
172
|
+
processReceipts(){
|
|
173
|
+
if(!this.pushEnabled)fail('PUSH_DISABLED');
|
|
174
|
+
this.receipts??=new DeliveryProcessReceipts({journal:this.journal,directory:join(realpathSync(this.g.c.stateDir),'delivery-process-receipts'),enabled:true});return this.receipts;
|
|
175
|
+
}
|
|
176
|
+
pushRecovery(task){
|
|
177
|
+
this.assertOpen();
|
|
178
|
+
return new DeliveryPushRecovery({journal:this.journal,receipts:this.processReceipts(),executor:this.push(task),enabled:true,
|
|
179
|
+
quiescent:()=>!this.pending&&!this.preparations.size&&this.exclusive()&&!this.closed});
|
|
180
|
+
}
|
|
181
|
+
async close(){this.closed=true;this.controller?.abort();for(const p of this.preparations.values())p.controller.abort();await Promise.allSettled([this.pending,...[...this.preparations.values()].map(p=>p.promise)]);}
|
|
182
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// Fixed Gitee SSH Git transport. No model-supplied command, endpoint or credential.
|
|
2
|
+
// Executes only under a Linux descendant supervisor; macOS can stage/observe via RPC.
|
|
3
|
+
import {lstatSync,realpathSync} from 'node:fs';
|
|
4
|
+
import {join} from 'node:path';
|
|
5
|
+
import {canonical} from './approval-ledger.js';
|
|
6
|
+
import {validatePushPlan,pushCommand,remoteReadCommand,parseRemoteRef} from './delivery-push-plan.js';
|
|
7
|
+
import {runSupervised} from './delivery-process.js';
|
|
8
|
+
const fail=c=>{throw Error('DELIVERY_SSH_'+c);};
|
|
9
|
+
const quote=s=>"'"+s.replaceAll("'","'\\''")+"'";
|
|
10
|
+
function identity(path,secret){
|
|
11
|
+
if(typeof path!=='string'||!path.startsWith('/')||path.length>2048||/[\x00-\x1f\x7f]/.test(path))fail('CREDENTIAL_PATH');
|
|
12
|
+
const s=lstatSync(path,{bigint:true});
|
|
13
|
+
if(!s.isFile()||s.isSymbolicLink()||s.nlink!==1n||s.size<1n||s.size>1048576n||realpathSync(path)!==path||
|
|
14
|
+
(s.mode&BigInt(secret?0o077:0o022))!==0n)fail('CREDENTIAL_FILE');
|
|
15
|
+
return {device:String(s.dev),inode:String(s.ino),size:String(s.size),mtime:String(s.mtimeNs),ctime:String(s.ctimeNs)};
|
|
16
|
+
}
|
|
17
|
+
export function sshEnvironment({keyFile,knownHostsFile}){
|
|
18
|
+
const args=['/usr/bin/ssh','-F','/dev/null','-T','-p','22','-i',keyFile,
|
|
19
|
+
'-o','BatchMode=yes','-o','StrictHostKeyChecking=yes','-o','UpdateHostKeys=no','-o','IdentitiesOnly=yes',
|
|
20
|
+
'-o','IdentityAgent=none','-o','AddKeysToAgent=no','-o','ForwardAgent=no','-o','ClearAllForwardings=yes',
|
|
21
|
+
'-o','ControlMaster=no','-o','ControlPath=none','-o','ControlPersist=no','-o','ProxyCommand=none','-o','ProxyJump=none',
|
|
22
|
+
'-o','PermitLocalCommand=no','-o','KnownHostsCommand=none','-o','CertificateFile=none',
|
|
23
|
+
'-o','GlobalKnownHostsFile=/dev/null','-o','UserKnownHostsFile='+knownHostsFile,'-o','ConnectTimeout=8'];
|
|
24
|
+
if([keyFile,knownHostsFile].some(p=>typeof p!=='string'||!p.startsWith('/')||p.length>2048||/[\x00-\x1f\x7f]/.test(p)))fail('CREDENTIAL_PATH');
|
|
25
|
+
return {PATH:'/usr/bin:/bin',LC_ALL:'C',GIT_CONFIG_NOSYSTEM:'1',GIT_CONFIG_SYSTEM:'/dev/null',GIT_CONFIG_GLOBAL:'/dev/null',
|
|
26
|
+
GIT_OPTIONAL_LOCKS:'0',GIT_NO_REPLACE_OBJECTS:'1',GIT_TERMINAL_PROMPT:'0',GIT_LITERAL_PATHSPECS:'1',
|
|
27
|
+
GIT_SSH_VARIANT:'ssh',GIT_SSH_COMMAND:args.map(quote).join(' ')};
|
|
28
|
+
}
|
|
29
|
+
export function sshGitCommand(binding,plan,kind){
|
|
30
|
+
validatePushPlan(plan);
|
|
31
|
+
if(!binding||binding.url!==plan.url||binding.branch!==plan.branch||binding.base_sha!==plan.parent_oid||
|
|
32
|
+
typeof binding.root!=='string'||!binding.root.startsWith('/')||binding.root.endsWith('/')||/[\x00-\x1f\x7f]/.test(binding.root))fail('BINDING');
|
|
33
|
+
const tail=kind==='read'?remoteReadCommand(plan):kind==='push'?pushCommand(plan):null;if(!tail)fail('COMMAND_DENIED');
|
|
34
|
+
return ['/usr/bin/git','--git-dir='+join(binding.root,'.git'),'--work-tree='+binding.root,
|
|
35
|
+
'-c','core.hooksPath=/dev/null','-c','core.fsmonitor=false','-c','core.untrackedCache=false','-c','gc.auto=0','-c','maintenance.auto=false',
|
|
36
|
+
'-c','protocol.allow=never','-c','protocol.ssh.allow=always','-c','protocol.version=0','-c','push.followTags=false',
|
|
37
|
+
'-c','push.recurseSubmodules=no','-c','push.gpgSign=false','-c','credential.helper=',...tail];
|
|
38
|
+
}
|
|
39
|
+
export class DeliverySSH {
|
|
40
|
+
constructor({binding,keyFile,knownHostsFile,enabled=false},io={}){
|
|
41
|
+
this.b=structuredClone(binding);this.credentials={keyFile,knownHostsFile};this.enabled=enabled;this.platform=io.platform??process.platform;
|
|
42
|
+
this.fileIdentity=io.fileIdentity??identity;this.run=io.run??runSupervised;
|
|
43
|
+
this.secretIdentity=null;this.active=false;
|
|
44
|
+
}
|
|
45
|
+
check(){
|
|
46
|
+
if(this.enabled!==true)fail('DISABLED');if(this.platform!=='linux')fail('PLATFORM_UNSUPPORTED');
|
|
47
|
+
const current={key:this.fileIdentity(this.credentials.keyFile,true),hosts:this.fileIdentity(this.credentials.knownHostsFile,false)};
|
|
48
|
+
if(this.secretIdentity&&canonical(current)!==canonical(this.secretIdentity))fail('CREDENTIAL_CHANGED');this.secretIdentity??=current;
|
|
49
|
+
}
|
|
50
|
+
async invoke(plan,kind,{signal,onSpawn,onDrained,beforeSpawn}={}){
|
|
51
|
+
this.check();if(this.active)fail('IN_FLIGHT');if(signal?.aborted)fail('ABORTED');
|
|
52
|
+
const argv=sshGitCommand(this.b,plan,kind),env=sshEnvironment(this.credentials);this.active=true;
|
|
53
|
+
try{
|
|
54
|
+
const receipt=beforeSpawn?.();this.check();
|
|
55
|
+
const result=await this.run(argv,{cwd:this.b.root,env,timeout:20,limit:1048576,signal,onSpawn,onDrained,receipt});
|
|
56
|
+
if(result.drained!==true)fail('DRAIN_UNKNOWN');this.check();
|
|
57
|
+
if(result.cause!==null||result.signal!==null)fail('OPERATION_FAILED');
|
|
58
|
+
if(kind==='read')return parseRemoteRef(plan,{code:result.code,stdout:result.stdout.toString('ascii')});
|
|
59
|
+
if(result.code!==0)fail('PUSH_REJECTED');return {acknowledged:true,drained:true};
|
|
60
|
+
}catch{fail('OUTCOME_UNKNOWN');}finally{this.active=false;}
|
|
61
|
+
}
|
|
62
|
+
read(plan,options){return this.invoke(plan,'read',options);}
|
|
63
|
+
push(plan,options){return this.invoke(plan,'push',options);}
|
|
64
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// Read-only persisted projection. No Git, filesystem observation, migration,
|
|
2
|
+
// approval renewal, lease acquisition or execution callback is allowed here.
|
|
3
|
+
import {canonical,digest} from './approval-ledger.js';
|
|
4
|
+
import {validatePushIntent,pushReadback} from './delivery-push-plan.js';
|
|
5
|
+
const has=(db,t)=>!!db?.prepare("SELECT 1 FROM sqlite_master WHERE type='table' AND name=?").get(t);
|
|
6
|
+
const parse=s=>{try{return JSON.parse(s);}catch{return null;}};
|
|
7
|
+
const same=(a,b)=>canonical(a)===canonical(b);
|
|
8
|
+
const states=new Set(['queued','active','needs_review','completed','not_applied','cancelled']);
|
|
9
|
+
const types=new Set(['queued','acquired','dispatched','operation_intent','operation_spawned','operation_drained','needs_review','readback_completed','not_applied_confirmed','cancelled','recovery_applied','remote_readback_failed']);
|
|
10
|
+
const codes=new Set(['EFFECT_UNKNOWN','DRAIN_REQUIRED','READBACK_UNKNOWN','READBACK_MISMATCH','UNAPPLIED_READBACK_UNKNOWN','UNAPPLIED_READBACK_MISMATCH','HOST_RESTART_REVIEW','user_cancelled','superseded','preflight_failed']);
|
|
11
|
+
export function deliveryWaiting(db,r,t){
|
|
12
|
+
if(r.state==='needs_review'||t.blocked)return 'delivery_review';if(r.state!=='queued')return null;
|
|
13
|
+
if(t.busy)return 'task_busy';
|
|
14
|
+
const owner=db.prepare("SELECT state FROM delivery_actions WHERE resource=? AND state IN ('active','needs_review')").get(r.resource);
|
|
15
|
+
if(owner)return owner.state==='needs_review'?'delivery_resource_review':'delivery_resource_busy';
|
|
16
|
+
if(has(db,'development_requests')){
|
|
17
|
+
if(has(db,'development_operations')&&db.prepare("SELECT 1 FROM development_operations o JOIN development_requests d ON d.id=o.request_id WHERE d.resource=? AND o.state<>'closed'").get(r.resource))return 'development_resource_draining';
|
|
18
|
+
const d=db.prepare("SELECT state FROM development_requests WHERE resource=? AND state IN ('active','needs_review')").get(r.resource);
|
|
19
|
+
if(d)return d.state==='needs_review'?'development_resource_review':'development_resource_busy';
|
|
20
|
+
if(db.prepare("SELECT 1 FROM development_requests WHERE resource=? AND state='queued' AND seq<=?").get(r.resource,r.development_cutoff))return 'development_fifo';
|
|
21
|
+
}
|
|
22
|
+
if(db.prepare("SELECT 1 FROM delivery_actions WHERE resource=? AND state='queued' AND seq<?").get(r.resource,r.seq))return 'delivery_fifo';
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
function projectPush(db,r,task,i,base){
|
|
26
|
+
try{validatePushIntent(i);}catch{return base;}
|
|
27
|
+
if(!states.has(r.state)||r.state==='not_applied'||i.task_id!==r.task_id||i.resource!==r.resource||
|
|
28
|
+
digest({event_id:r.event_id,approval_id:r.approval_id,intent:i})!==r.fingerprint)return base;
|
|
29
|
+
const p=i.plan,result=parse(r.result),open=db.prepare("SELECT count(*) n FROM delivery_operations WHERE action_id=? AND state<>'closed'").get(r.id).n;
|
|
30
|
+
const e=db.prepare("SELECT data,created FROM delivery_events WHERE action_id=? AND type='readback_completed' ORDER BY seq DESC LIMIT 1").get(r.id),proof=parse(e?.data);
|
|
31
|
+
let completed=false;
|
|
32
|
+
if(r.state==='completed'&&r.dispatched===1&&Number.isSafeInteger(r.epoch)&&open===0&&proof?.epoch===r.epoch&&same(proof.result,result)&&
|
|
33
|
+
Number.isSafeInteger(result?.observed_at)&&result.observed_at<=e.created){
|
|
34
|
+
try{completed=same(pushReadback(i,{...result,ref_oid:result.commit_oid,workspace_identity:i.workspace_identity},result.observed_at),result);}catch{}
|
|
35
|
+
}
|
|
36
|
+
const state=r.state==='completed'&&!completed?'evidence_invalid':r.state;
|
|
37
|
+
return {...base,kind:'push',state,error:state==='evidence_invalid'?base.error:r.error?(codes.has(r.error)?r.error:'DELIVERY_REVIEW_REQUIRED'):null,
|
|
38
|
+
approval_id:r.approval_id,workspace_revision:i.task_revision,revision_matches:i.task_revision===task.revision,
|
|
39
|
+
branch:p.branch,source_commit_oid:p.commit_oid,commit_oid:null,repository_url:p.url,repository_id:p.repository_id,
|
|
40
|
+
expected_remote_oid:p.expected_remote_oid,remote_ref_oid:completed?result.commit_oid:null,
|
|
41
|
+
remote_acceptance:completed?'ref_observed':'not_observed',remote_observed_at:completed?result.observed_at:null,
|
|
42
|
+
dispatched:r.dispatched===1,unclosed_operations:open,waiting_reason:deliveryWaiting(db,r,task),external_drain_checked_now:false,
|
|
43
|
+
timeline:db.prepare('SELECT seq,type,created FROM delivery_events WHERE action_id=? ORDER BY seq DESC LIMIT 12').all(r.id).reverse().map(e=>({seq:e.seq,type:types.has(e.type)?e.type:'other',at:e.created}))};
|
|
44
|
+
}
|
|
45
|
+
function project(db,r,task){
|
|
46
|
+
const i=parse(r.intent),{fingerprint:ih,...ib}=i??{},p=i?.plan,{fingerprint:ph,...pb}=p??{};
|
|
47
|
+
const base={id:r.id,created:r.created,state:'evidence_invalid',error:'DELIVERY_EVIDENCE_INVALID',commit_oid:null,
|
|
48
|
+
computed_commit_oid:null,remote_acceptance:'not_observed',runtime_adoption:'not_observed',historical:true,timeline:[]};
|
|
49
|
+
if(i?.kind==='push-execution-intent-v1')return projectPush(db,r,task,i,base);
|
|
50
|
+
if(!states.has(r.state)||i?.kind!=='commit-execution-intent-v1'||i.task_id!==r.task_id||i.resource!==r.resource||
|
|
51
|
+
digest(ib)!==ih||p?.kind!=='commit-objects-v1'||p.persisted!==false||p.grants_execution!==false||digest(pb)!==ph||
|
|
52
|
+
digest({event_id:r.event_id,approval_id:r.approval_id,intent:i})!==r.fingerprint)return base;
|
|
53
|
+
const result=parse(r.result),open=db.prepare("SELECT count(*) n FROM delivery_operations WHERE action_id=? AND state<>'closed'").get(r.id).n;
|
|
54
|
+
const event=db.prepare("SELECT data,created FROM delivery_events WHERE action_id=? AND type='readback_completed' ORDER BY seq DESC LIMIT 1").get(r.id),proof=parse(event?.data);
|
|
55
|
+
const completed=r.state==='completed'&&r.dispatched===1&&Number.isSafeInteger(r.epoch)&&open===0&&proof?.epoch===r.epoch&&same(proof.result,result)&&
|
|
56
|
+
result?.kind==='commit-readback-v1'&&result.intent_fingerprint===i.fingerprint&&result.commit_oid===p.computed_commit_oid&&
|
|
57
|
+
result.tree_oid===p.tree_oid&&result.branch===p.branch&&result.manifest_sha256===p.manifest_sha256&&same(result.tested_content,p.tested_content)&&result.drained===true;
|
|
58
|
+
const unappliedEvent=r.state==='not_applied'?db.prepare("SELECT data FROM delivery_events WHERE action_id=? AND type='not_applied_confirmed' ORDER BY seq DESC LIMIT 1").get(r.id):null;
|
|
59
|
+
const unappliedProof=parse(unappliedEvent?.data),before=parse(r.before_snapshot),{fingerprint:beforeHash,...beforeBody}=before??{};
|
|
60
|
+
const unapplied=r.state==='not_applied'&&open===0&&Number.isSafeInteger(r.epoch)&&unappliedProof?.epoch===r.epoch&&same(unappliedProof.result,result)&&
|
|
61
|
+
result?.kind==='commit-not-applied-v1'&&result.intent_fingerprint===i.fingerprint&&beforeHash&&digest(beforeBody)===beforeHash&&
|
|
62
|
+
before.intent_fingerprint===i.fingerprint&&result.baseline_fingerprint===beforeHash&&result.drained===true&&result.unchanged===true&&result.object_presence_unchanged===true&&result.retry_authorized===false;
|
|
63
|
+
const state=r.state==='completed'&&!completed||r.state==='not_applied'&&!unapplied?'evidence_invalid':r.state;
|
|
64
|
+
return {...base,state,error:state==='evidence_invalid'?base.error:r.error?(codes.has(r.error)?r.error:'DELIVERY_REVIEW_REQUIRED'):null,
|
|
65
|
+
approval_id:r.approval_id,workspace_revision:i.task_revision,revision_matches:i.task_revision===task.revision,
|
|
66
|
+
branch:p.branch,computed_commit_oid:p.computed_commit_oid,commit_oid:completed?result.commit_oid:null,
|
|
67
|
+
commit_observed_at:completed?event.created:null,dispatched:r.dispatched===1,unclosed_operations:open,
|
|
68
|
+
waiting_reason:deliveryWaiting(db,r,task),external_drain_checked_now:false,
|
|
69
|
+
timeline:db.prepare('SELECT seq,type,created FROM delivery_events WHERE action_id=? ORDER BY seq DESC LIMIT 12').all(r.id).reverse().map(e=>({seq:e.seq,type:types.has(e.type)?e.type:'other',at:e.created}))};
|
|
70
|
+
}
|
|
71
|
+
export function deliveryActions(g,taskId){
|
|
72
|
+
const db=g.store?.db,base={schema:1,available:false,items:[],total:0,read_only:true,grants_execution:false,
|
|
73
|
+
execution_enabled:g.deliveryRuntime?.executionEnabled===true,push_execution_enabled:g.deliveryRuntime?.pushExecutionEnabled===true,filesystem_checked_now:false,remote_checked_now:false,automatic_retry:false};
|
|
74
|
+
if(!['delivery_actions','delivery_operations','delivery_events'].every(t=>has(db,t)))return base;
|
|
75
|
+
try{
|
|
76
|
+
const task=db.prepare('SELECT revision,busy,blocked FROM tasks WHERE id=?').get(taskId);if(!task)return base;
|
|
77
|
+
const total=db.prepare('SELECT count(*) n FROM delivery_actions WHERE task_id=?').get(taskId).n;
|
|
78
|
+
const rows=db.prepare("SELECT * FROM delivery_actions WHERE task_id=? ORDER BY CASE WHEN state='needs_review' THEN 0 WHEN state NOT IN ('queued','active','completed','not_applied','cancelled') THEN 1 WHEN state='active' THEN 2 WHEN state='queued' THEN 3 ELSE 4 END,seq DESC LIMIT 20").all(taskId);
|
|
79
|
+
return {...base,available:true,total,items:rows.map(r=>project(db,r,task))};
|
|
80
|
+
}catch{return {...base,error:'DELIVERY_STATE_UNAVAILABLE'};}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Exact historical action lookup for derived deliverables; independent of the
|
|
84
|
+
// twenty-row UI window and of whether the execution runtime is currently enabled.
|
|
85
|
+
export function deliveryAction(g,taskId,id){
|
|
86
|
+
const db=g.store?.db;
|
|
87
|
+
if(!['delivery_actions','delivery_operations','delivery_events'].every(t=>has(db,t)))return null;
|
|
88
|
+
try{const task=db.prepare('SELECT revision,busy,blocked FROM tasks WHERE id=?').get(taskId),r=db.prepare('SELECT * FROM delivery_actions WHERE id=? AND task_id=?').get(id,taskId);
|
|
89
|
+
return task&&r?project(db,r,task):null;
|
|
90
|
+
}catch{return null;}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Pending delivery belongs to the task even without a worker job. Completed
|
|
94
|
+
// delivery is historical evidence, never proof that the whole task is done.
|
|
95
|
+
export function deliveryDisplayState(fallback,execution){
|
|
96
|
+
if(execution?.error)return 'delivery_state_unavailable';
|
|
97
|
+
if(!execution?.available)return fallback;
|
|
98
|
+
const items=execution.items;
|
|
99
|
+
if(items.some(x=>['needs_review','evidence_invalid'].includes(x.state)))return 'delivery_review';
|
|
100
|
+
if(items.some(x=>x.unclosed_operations>0))return 'delivery_draining';
|
|
101
|
+
if(items.some(x=>x.state==='active'))return 'delivery_active';
|
|
102
|
+
// Do not hide another executing/blocked component with an ordinary queue.
|
|
103
|
+
if(!['registered','completed','cancelled','development_handed_off'].includes(fallback))return fallback;
|
|
104
|
+
if(items.some(x=>x.state==='queued'))return 'delivery_queued';
|
|
105
|
+
return fallback;
|
|
106
|
+
}
|