@tnunamak/dalan 0.8.1 → 0.8.3
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/CHANGELOG.md +61 -2
- package/CONTRIBUTING.md +2 -0
- package/README.md +8 -12
- package/bin/dalan.mjs +25 -4
- package/bin/dot-acp-agent.mjs +572 -76
- package/bin/dot-alert.mjs +9 -3
- package/bin/dot-deadman.mjs +3 -1
- package/bin/dot-heartbeat.mjs +14 -7
- package/bin/dot-pr-reviewer.mjs +3 -31
- package/bin/dot-worker.mjs +121 -34
- package/contracts/adapter-qualification.schema.json +94 -7
- package/docs/chatgpt-worker-onboarding.md +1 -1
- package/docs/codex-to-muse.md +2 -2
- package/docs/connect-chatgpt.md +1 -1
- package/docs/connection-longevity.md +2 -2
- package/docs/dalan-upgrade.md +8 -5
- package/docs/deadman.md +4 -0
- package/docs/design/authority-matrix.md +18 -0
- package/docs/design/failure-table.md +87 -0
- package/docs/design/identities.md +22 -0
- package/docs/design/pnpm-prepared-dependencies.md +64 -0
- package/docs/design/reuse-ledger.md +75 -0
- package/docs/design/target-architecture-v2.md +434 -0
- package/docs/local-agent-mcp.md +14 -0
- package/docs/local-agent-requester.md +2 -1
- package/docs/manual-setup.md +1 -1
- package/docs/operations.md +77 -14
- package/docs/pull-workers.md +11 -1
- package/docs/service-setup.md +10 -10
- package/docs/t3-integration.md +4 -2
- package/docs/t3-reverse-control.md +5 -5
- package/docs/test-suite-isolation.md +7 -6
- package/docs/trials/chatgpt-agent.md +47 -0
- package/docs/trials/claude-code-requester.md +68 -0
- package/docs/trials/muse.md +46 -0
- package/docs/usage.md +21 -7
- package/docs/worker-clients.md +1 -1
- package/integrations/t3/acp-kit/src/errors.mjs +6 -2
- package/integrations/t3/acp-kit/src/lock.mjs +46 -17
- package/integrations/t3/acp-kit/src/session-store.mjs +15 -3
- package/integrations/t3/acp-kit/src/t3-envelope.mjs +10 -2
- package/package.json +4 -3
- package/packages/job-core/protocol/canonical.mjs +45 -7
- package/packages/job-core/transitions.mjs +4 -2
- package/scripts/upgrade-drain.mjs +96 -8
- package/scripts/worker-bridge-setup.mjs +215 -120
- package/src/ack-ledger.mjs +7 -1
- package/src/acp-bridge.mjs +256 -28
- package/src/admin-socket.mjs +32 -3
- package/src/alerts.mjs +17 -8
- package/src/answer-journal-retention.mjs +82 -0
- package/src/answer-journal.mjs +6 -4
- package/src/as-store.mjs +51 -6
- package/src/as.mjs +27 -5
- package/src/catalog-switch-inventory.mjs +70 -0
- package/src/check-response.mjs +4 -1
- package/src/cli-help.mjs +14 -3
- package/src/cli-name.mjs +13 -1
- package/src/connect-cli.mjs +159 -4
- package/src/deadman.mjs +8 -5
- package/src/delivery.mjs +2 -19
- package/src/destinations/index.mjs +75 -14
- package/src/drain-runner.mjs +5 -1
- package/src/follow-current.mjs +3 -1
- package/src/get-summary.mjs +11 -2
- package/src/git-env.mjs +1 -1
- package/src/gozar-routes.mjs +313 -94
- package/src/host-budget-storage.py +56 -6
- package/src/host-budget.mjs +98 -15
- package/src/host-execution-policy.mjs +17 -6
- package/src/legacy-names.mjs +0 -2
- package/src/lifecycle.mjs +44 -9
- package/src/loopback-probe.mjs +46 -0
- package/src/mcp-outcome.mjs +3 -1
- package/src/mcp-sse.mjs +2 -1
- package/src/mcp-stdio.mjs +192 -30
- package/src/patch-apply.mjs +212 -7
- package/src/phase-trace.mjs +73 -0
- package/src/prepared-go-modules.mjs +127 -0
- package/src/prepared-node-modules.mjs +225 -38
- package/src/prepared-pnpm.mjs +276 -0
- package/src/prepared-store-prune.mjs +52 -0
- package/src/projects.mjs +59 -7
- package/src/protocol-artifact-commands.mjs +28 -0
- package/src/protocol-finish.mjs +3 -2
- package/src/publication-authority.mjs +22 -0
- package/src/publish-plan.mjs +2 -2
- package/src/pull-queue.mjs +55 -20
- package/src/read-api.mjs +127 -0
- package/src/redaction.mjs +173 -0
- package/src/rejections.mjs +19 -3
- package/src/route-custody.mjs +90 -0
- package/src/route-publication.mjs +50 -0
- package/src/route-setup.mjs +184 -0
- package/src/schema-subset.mjs +5 -3
- package/src/server.mjs +503 -98
- package/src/service-launchd.mjs +5 -1
- package/src/service-update-lock.mjs +7 -4
- package/src/service-update-runtime.mjs +136 -21
- package/src/service-update.mjs +207 -51
- package/src/service.mjs +276 -77
- package/src/setup.mjs +48 -25
- package/src/signal.mjs +1 -1
- package/src/status-evidence.mjs +18 -5
- package/src/store.mjs +876 -120
- package/src/submission-policy.mjs +257 -56
- package/src/t3-control.mjs +1 -1
- package/src/t3.mjs +14 -8
- package/src/terminal-safe.mjs +9 -7
- package/src/tool-registry.mjs +134 -41
- package/src/try-exposure.mjs +53 -86
- package/src/try-hosted.mjs +257 -70
- package/src/try.mjs +48 -22
- package/src/work-order.mjs +11 -6
- package/src/worker-instructions.mjs +26 -4
- package/src/worker-runs.mjs +35 -0
- package/src/workers.mjs +34 -7
- package/src/workspace.mjs +354 -54
- package/systemd/dot-deadman.service +3 -1
- package/docs/dot-pr-reviewer.config.example.json +0 -9
- package/docs/pr-head-reviewer.md +0 -70
- package/src/pr-head-reviewer.mjs +0 -596
- package/systemd/dot-pr-reviewer.service +0 -11
- package/systemd/dot-pr-reviewer.timer +0 -10
|
@@ -1,11 +1,49 @@
|
|
|
1
1
|
export function canonicalValue(value, errorCode = 'invalid_json') {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
const active = new WeakSet();
|
|
3
|
+
const invalid = () => Object.assign(new Error(errorCode), { code: errorCode });
|
|
4
|
+
|
|
5
|
+
const data = (item, key) => {
|
|
6
|
+
const descriptor = Object.getOwnPropertyDescriptor(item, key);
|
|
7
|
+
if (!descriptor || !Object.hasOwn(descriptor, 'value')) throw invalid();
|
|
8
|
+
return descriptor.value;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
function visit(item) {
|
|
12
|
+
if (item === null || typeof item === 'boolean' || typeof item === 'string') return item;
|
|
13
|
+
if (typeof item === 'number' && Number.isFinite(item)) return item;
|
|
14
|
+
const array = Array.isArray(item);
|
|
15
|
+
if (!array) {
|
|
16
|
+
if (!item || typeof item !== 'object') throw invalid();
|
|
17
|
+
const prototype = Object.getPrototypeOf(item);
|
|
18
|
+
if (prototype !== Object.prototype && prototype !== null) throw invalid();
|
|
19
|
+
}
|
|
20
|
+
if (active.has(item)) throw invalid();
|
|
21
|
+
active.add(item);
|
|
22
|
+
try {
|
|
23
|
+
if (array) {
|
|
24
|
+
const values = [];
|
|
25
|
+
for (let i = 0; i < item.length; i++) {
|
|
26
|
+
values.push(visit(data(item, i)));
|
|
27
|
+
}
|
|
28
|
+
return values;
|
|
29
|
+
}
|
|
30
|
+
return Object.fromEntries(Object.keys(item).sort().map(key => [key, visit(data(item, key))]));
|
|
31
|
+
} finally {
|
|
32
|
+
// Sibling branches may legitimately share the same object.
|
|
33
|
+
active.delete(item);
|
|
34
|
+
}
|
|
7
35
|
}
|
|
8
|
-
|
|
36
|
+
|
|
37
|
+
return visit(value);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function serializeCanonical(value) {
|
|
41
|
+
if (value === null || typeof value !== 'object') return JSON.stringify(value);
|
|
42
|
+
if (Array.isArray(value)) return '[' + value.map(serializeCanonical).join(',') + ']';
|
|
43
|
+
// JavaScript enumerates array-index keys numerically even on a sorted object.
|
|
44
|
+
// Write the properties in UTF-16 order rather than asking JSON.stringify to enumerate them.
|
|
45
|
+
return '{' + Object.keys(value).sort().map(key =>
|
|
46
|
+
JSON.stringify(key) + ':' + serializeCanonical(value[key])).join(',') + '}';
|
|
9
47
|
}
|
|
10
48
|
|
|
11
|
-
export const canonicalJson = (value, errorCode) =>
|
|
49
|
+
export const canonicalJson = (value, errorCode) => serializeCanonical(canonicalValue(value, errorCode));
|
|
@@ -5,7 +5,7 @@ export function assertClaimAuthority(command, authority, now) {
|
|
|
5
5
|
const { job, subject, worker, claim, epoch } = command;
|
|
6
6
|
if (!authority || authority.job !== job || authority.subject !== subject || authority.worker !== worker ||
|
|
7
7
|
authority.claim !== claim || authority.epoch !== epoch || authority.active !== true) fail('stale_authority');
|
|
8
|
-
if (!natural(authority.leaseUntil) || now >= authority.leaseUntil) fail('lease_expired');
|
|
8
|
+
if (!natural(now) || !natural(authority.leaseUntil) || now >= authority.leaseUntil) fail('lease_expired');
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
// Called only after authority and receipt replay have been checked by the caller.
|
|
@@ -14,7 +14,9 @@ export function decide(snapshot, command, context) {
|
|
|
14
14
|
if (snapshot.revision !== expectedRevision) fail('revision_conflict');
|
|
15
15
|
let leaseUntil;
|
|
16
16
|
if (operation === 'renew') {
|
|
17
|
-
if (!
|
|
17
|
+
if (!natural(context?.maxLeaseMs) || context.maxLeaseMs < 1 || !natural(context.now) ||
|
|
18
|
+
!args || typeof args !== 'object' || Array.isArray(args) || !Object.hasOwn(args, 'leaseMs') ||
|
|
19
|
+
Object.keys(args).some(key => key !== 'leaseMs') ||
|
|
18
20
|
!natural(args.leaseMs) || args.leaseMs < 1 || args.leaseMs > context.maxLeaseMs) fail('invalid_lease');
|
|
19
21
|
leaseUntil = context.now + args.leaseMs;
|
|
20
22
|
if (!natural(leaseUntil)) fail('invalid_lease');
|
|
@@ -2,6 +2,41 @@
|
|
|
2
2
|
import {spawnSync} from 'node:child_process';
|
|
3
3
|
import {setTimeout as delay} from 'node:timers/promises';
|
|
4
4
|
|
|
5
|
+
// Default: quiesce when the running service has the gate (admission refused for seconds, live leases survive the restart), else pause
|
|
6
|
+
// and wait for leases (a claim blackout as long as the longest lease: up to 20 minutes per deploy for a worker whose lease is 15).
|
|
7
|
+
// --until-quiescent insists on the gate and fails without it; --pause insists on the old pause-and-wait drain. Only a positive answer
|
|
8
|
+
// from the service picks a mode: counters mean the gate; an HTTP 404 for the route means a legacy service. Any other failure is
|
|
9
|
+
// retried briefly, then the drain stops without touching the service and asks for an explicit flag.
|
|
10
|
+
const args=process.argv.slice(2);
|
|
11
|
+
const extraArgs=args.filter(arg=>arg!=='--until-quiescent'&&arg!=='--pause');
|
|
12
|
+
if(extraArgs.length){console.error(`upgrade-drain: unknown argument ${JSON.stringify(extraArgs[0])}. The options are --until-quiescent and --pause.`);process.exit(2);}
|
|
13
|
+
if(args.includes('--until-quiescent')&&args.includes('--pause')){console.error('upgrade-drain: --until-quiescent and --pause exclude each other.');process.exit(2);}
|
|
14
|
+
let untilQuiescent=args.includes('--until-quiescent');
|
|
15
|
+
const forcePause=args.includes('--pause');
|
|
16
|
+
const gateCounters=['admitted_in_flight','delivery_in_flight','host_checks_unverified'];
|
|
17
|
+
const quiesceRecovery='If the old service is still quiesced, keep it running and run `dalan quiesce off` under the old HOME before retrying.';
|
|
18
|
+
// The gate must never stay closed after an aborted drain: a closed gate starves every worker with no signal. It stays closed only
|
|
19
|
+
// after the poll succeeds, because the restart that follows reopens it. The reopen call has its own timeout, not the drain deadline.
|
|
20
|
+
let gateClosed=false;
|
|
21
|
+
// A cleanup call counts only when its reply shows the state it asked for; exit 0 alone does not.
|
|
22
|
+
const jsonOf=text=>{try{return JSON.parse(text);}catch{return null;}};
|
|
23
|
+
function reopenGate(why) {
|
|
24
|
+
if(!gateClosed)return;
|
|
25
|
+
const result=spawnSync(process.execPath,['bin/dot-worker.mjs','quiesce','off','--json'],{encoding:'utf8',timeout:10000,killSignal:'SIGKILL'});
|
|
26
|
+
if(result.status===0&&jsonOf(result.stdout)?.on===false){gateClosed=false;console.error(`drain aborted (${why}); gate reopened`);}
|
|
27
|
+
else console.error(`drain aborted (${why}); the gate could NOT be confirmed reopened: ${(result.stderr||result.stdout||`exit ${result.status}`).trim()}`);
|
|
28
|
+
}
|
|
29
|
+
// A pause this run issued must not outlive an aborted run: claims would stay refused with no signal. A pause that was already there
|
|
30
|
+
// belongs to the operator and is never released here. After the drain completes, the pause is the documented handoff to the restart.
|
|
31
|
+
let pausedByRun=false;
|
|
32
|
+
function resumeOwnedPause(why) {
|
|
33
|
+
if(!pausedByRun)return;
|
|
34
|
+
const result=spawnSync(process.execPath,['bin/dot-worker.mjs','resume','--json'],{encoding:'utf8',timeout:10000,killSignal:'SIGKILL'});
|
|
35
|
+
if(result.status===0&&jsonOf(result.stdout)?.paused===false){pausedByRun=false;console.error(`drain aborted (${why}); claims resumed`);}
|
|
36
|
+
else console.error(`drain aborted (${why}); the pause could NOT be confirmed released: ${(result.stderr||result.stdout||`exit ${result.status}`).trim()}`);
|
|
37
|
+
}
|
|
38
|
+
const abortCleanup=why=>{reopenGate(why);resumeOwnedPause(why);};
|
|
39
|
+
for(const [name,code] of [['SIGINT',130],['SIGTERM',143],['SIGHUP',129]])process.on(name,()=>{abortCleanup(name);process.exit(code);});
|
|
5
40
|
const recovery='If the old service is paused, keep it running. Let workers finish or wait for their leases to expire, run `dalan resume` under the old HOME, then retry this drain.';
|
|
6
41
|
async function main() {
|
|
7
42
|
const timeoutMs=Number(process.env.DALAN_UPGRADE_DRAIN_TIMEOUT_MS??300000);
|
|
@@ -16,24 +51,77 @@ async function main() {
|
|
|
16
51
|
if(result.status!==0)fail(`dalan ${args[0]} failed: ${(result.stderr||result.stdout||`exit ${result.status}`).trim()}.`);
|
|
17
52
|
try { return JSON.parse(result.stdout); } catch { fail(`dalan ${args[0]} printed no JSON.`); }
|
|
18
53
|
};
|
|
19
|
-
|
|
20
|
-
|
|
54
|
+
if(!untilQuiescent&&!forcePause) {
|
|
55
|
+
// 'gate': the service answered with the in-flight counters. 'legacy': it answered HTTP 404 for the route, so it predates the gate.
|
|
56
|
+
// 'unknown': anything else (a transport error, a timeout, a bad body); that proves nothing about the service.
|
|
57
|
+
const probeOnce=()=>{
|
|
58
|
+
const probe=spawnSync(process.execPath,['bin/dot-worker.mjs','quiesce','status','--json'],{encoding:'utf8',timeout:Math.min(10000,Math.max(1,deadline-Date.now())),killSignal:'SIGKILL'});
|
|
59
|
+
if(probe.status===0) {
|
|
60
|
+
let status=null;
|
|
61
|
+
try { status=JSON.parse(probe.stdout); } catch {}
|
|
62
|
+
return status&&typeof status==='object'&&gateCounters.every(name=>Number.isSafeInteger(status[name])&&status[name]>=0)?'gate':'unknown';
|
|
63
|
+
}
|
|
64
|
+
let error=null;
|
|
65
|
+
try { error=JSON.parse(probe.stderr||probe.stdout); } catch {}
|
|
66
|
+
return error?.kind==='http'&&error.http_status===404?'legacy':'unknown';
|
|
67
|
+
};
|
|
68
|
+
let mode='unknown';
|
|
69
|
+
for(let attempt=1;attempt<=3&&mode==='unknown';attempt++) {
|
|
70
|
+
mode=probeOnce();
|
|
71
|
+
if(mode==='unknown'&&attempt<3&&Date.now()<deadline)await delay(1000);
|
|
72
|
+
}
|
|
73
|
+
if(mode==='unknown')fail('could not tell whether the running service has the quiesce gate (the status call failed three times). Nothing was changed. Retry, or pass --until-quiescent or --pause.');
|
|
74
|
+
untilQuiescent=mode==='gate';
|
|
75
|
+
console.log(untilQuiescent
|
|
76
|
+
?'The running service has the quiesce gate: draining without a pause.'
|
|
77
|
+
:'The running service has no quiesce gate: pausing claims and waiting for live leases (claims stay refused until the longest lease ends).');
|
|
78
|
+
}
|
|
79
|
+
if(untilQuiescent) {
|
|
80
|
+
// A gate that was already closed belongs to the operator (or another drain) and is never reopened here.
|
|
81
|
+
const alreadyClosed=cli('quiesce','status')?.on===true;
|
|
82
|
+
if(alreadyClosed)console.log('Admission was already quiesced; this drain leaves the gate as it is.');
|
|
83
|
+
else gateClosed=true;
|
|
84
|
+
if(cli('quiesce','on')?.on!==true)fail('dalan quiesce on did not report the gate as closed.');
|
|
85
|
+
console.log('Admission is quiesced. Live leases survive a restart using the same database and are not waited on.');
|
|
86
|
+
const names=gateCounters;
|
|
87
|
+
for(;;) {
|
|
88
|
+
const status=cli('quiesce','status');
|
|
89
|
+
if(!status||typeof status!=='object'||names.some(name=>!Number.isSafeInteger(status[name])||status[name]<0))
|
|
90
|
+
fail('dalan quiesce status returned invalid in-flight counters.');
|
|
91
|
+
if(status.on!==true)fail('dalan quiesce status says admission is open after quiesce on.');
|
|
92
|
+
if(names.every(name=>status[name]===0))break;
|
|
93
|
+
if(Date.now()>=deadline)fail('Upgrade drain timed out while work was still in flight.');
|
|
94
|
+
await delay(Math.min(1000,Math.max(0,deadline-Date.now())));
|
|
95
|
+
}
|
|
96
|
+
gateClosed=false;
|
|
97
|
+
console.log('No admitted calls, deliveries or unverified host checks remain. Restart with the same HOME and database; the new process starts with admission open. If you do not restart, run `dalan quiesce off` to reopen it.');
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const before=cli('status');
|
|
101
|
+
if(typeof before?.paused!=='boolean')fail('dalan status did not report whether claims are paused. Nothing was changed.');
|
|
102
|
+
if(before.paused)console.log(`Claims were already paused${before.pause_reason?` (${before.pause_reason})`:''}; this drain leaves that pause as it is.`);
|
|
103
|
+
else {
|
|
104
|
+
pausedByRun=true;
|
|
105
|
+
if(cli('pause','--reason','upgrade drain')?.paused!==true)fail('dalan pause did not report claims as paused.');
|
|
106
|
+
console.log('Claims paused. New submissions are still accepted while the drain waits.');
|
|
107
|
+
}
|
|
21
108
|
while(true) {
|
|
22
|
-
const {claims
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
});
|
|
109
|
+
const {claims}=cli('claims');
|
|
110
|
+
if(!Array.isArray(claims))fail('the claims list came back without a claims array. Nothing is known about live leases; retry.');
|
|
111
|
+
// Only the service can say its lease expired. Older servers without this field stay conservatively open.
|
|
112
|
+
const open=claims.filter(claim=>claim?.expired!==true);
|
|
27
113
|
if(!open.length)break;
|
|
28
114
|
for(const claim of open)console.log(`Waiting for ${claim.request_id}; lease expires ${claim.lease_expires_at}.`);
|
|
29
115
|
if(Date.now()>=deadline)fail('Upgrade drain timed out while claims were still active.');
|
|
30
116
|
await delay(Math.min(1000,Math.max(50,deadline-Date.now())));
|
|
31
117
|
}
|
|
32
118
|
console.log('No unexpired claims remain. An expired lease revokes its old claim ID but does not prove its process stopped; the new service will sweep the row back to the queue, where the claimant can resume the work only with a fresh claim epoch after stopping the old run.');
|
|
119
|
+
pausedByRun=false;
|
|
33
120
|
console.log('Stop the old service, update the code, start the new service with the same HOME and configuration, then run `dalan resume` to release the persisted pause.');
|
|
34
121
|
}
|
|
35
122
|
|
|
36
123
|
main().catch(error=>{
|
|
37
|
-
|
|
124
|
+
abortCleanup('failure');
|
|
125
|
+
console.error(`${error?.message??String(error)}\n${untilQuiescent?quiesceRecovery:recovery}`);
|
|
38
126
|
process.exitCode=1;
|
|
39
127
|
});
|