salvage 2026-11-06: uncommitted work from scratch/darkwing-scratch/r137/wt (disk cleanup #824)

This commit is contained in:
mos committed 2026-11-06 10:03:20 -06:00
1 parent 7a2987cf51
commit ace2cb9e20
27 files changed
+1229 -255

No files matched your search

+36
View File
@@ -106,6 +106,42 @@ as `action.refused` on the task's trail.
addressed to (`request-not-found` otherwise). Returns the request's
`{project,commit,ref}`, which the host bundles for the reviewer.
Row 137 (#1631) adds four ops for a landing on a working repository's
`main` through the same git socket. The decision checks are land-work's,
in `src/landing.mjs` (`readLanding`), which `scripts/land-work/read.mjs`
also calls. Each caller names its branch, `refactor` for land-work and
the task ref for an agent's landing, and the decision's target has to
match it. So a decision raised for one can't land the other. Both callers
accept only a decision the human resolved through the CLI (`human/cli`).
- `{op:'checkLand',cap,args}`: `args` is `{task_ref,decision}`. The caller
must hold its claim and the decision: `git.merge.protected`, raised by
this role in this thread with the task ref as its target, resolved with
its approval option and not yet used (`decision-consumed` otherwise).
Then `readLanding`'s checks, and the verdict's commit must be the task's
latest push (`ref-moved`). A landing with no subject refuses
`no-subject`, since an agent has no `--subject` to pass. Returns a
ticket with what the host needs: the project, the work ref, the commit,
the verdict and the resolution. It records nothing unless it refuses.
- `{op:'recordLand',ticket,land}`: `land` is `{commit,previous,reviewed,
base,patch_id}` once `main` moved. The checks run again inside the
transaction. It writes the `action.allowed` for `git.merge.protected`
on the task's trail, which spends the decision, and sends the role a
RESULT in the decision's thread. When it refuses, the host moves `main`
back.
- `{op:'landRefused',ticket,args}`: `args` is `{code,detail,notify}`. It
writes `action.refused` with the code on the task's trail and ends the
landing. With `notify` the role gets a RESULT too, since the git socket
had already answered `started`.
- `{op:'landHeld',record}`: `record` is `{business,role,thread}`. True when
the role, in that thread (null for a role run), holds a merge decision a
landing could use. The launcher binds the git socket's land op only
then; `checkLand` still checks everything.
A ticket lives in the broker's memory from `checkLand` until `recordLand`
or `landRefused` ends it, so either may run after the run ended. An
unknown ticket refuses `land-unknown`.
`{op:'close'}`, SIGTERM and SIGINT close the socket and DB and remove the owned
writer lock. Losing the host IPC channel closes with exit 2. Startup or host
protocol failures return a static error code and exit 2, never a raw exception.
+169 -18
View File
@@ -1,5 +1,6 @@
import { randomUUID, randomBytes } from 'node:crypto';
import { loadRequirements } from '../../tasks/src/requirements.mjs';
import { MERGE, LandingRefusal, readLanding } from './landing.mjs';
// Kept closed while S1 lands; its vocabulary is the integration authority.
export const ACTIONS = Object.freeze([
@@ -136,6 +137,8 @@ export class Broker {
#sessions = new Map();
#runs = new Map();
#ended = new Set();
// Row 137 (#1631): landings checkLand let start, by ticket, until recordLand or landRefused ends them.
#landings = new Map();
#secretCheck;
#credentialStatus;
#requirements;
@@ -607,8 +610,13 @@ export class Broker {
const tasks = rest.slice(0, limit);
return { tasks, total: matched.length, next: rest.length > limit ? tasks.at(-1).task_ref : null };
}
#checkAuthority(s, action, { decision = null, target = null } = {}) {
#checkAuthority(s, action, context = {}) {
this.#agent(s);
return this.#authorityFor(s, action, context);
}
// #checkAuthority less the holder check. A landing's record and refusal use it alone: the run
// that asked held its claim at checkLand, and may have ended while the gate ran (row 137).
#authorityFor(s, action, { decision = null, target = null } = {}) {
if (
action === 'role.launch' &&
this.#store.get('SELECT state FROM launch_state WHERE business=?', s.business)?.state === 'revoked'
@@ -619,12 +627,7 @@ export class Broker {
if (!decision) fail('decision-required');
const d = this.#decision(s, decision),
r = this.#closed(d.id);
const evidence = this.#store.get(
"SELECT body FROM events WHERE business=? AND json_extract(body,'$.decision')=? AND json_extract(body,'$.operation')='decision.raise' AND kind='action.allowed' ORDER BY seq LIMIT 1",
s.business,
d.id,
);
const context = evidence ? JSON.parse(evidence.body) : null;
const context = this.#raised(s.business, d.id);
// Row 136 (#1630): bound to the role instance and its thread, not the run, so an approval
// outlives a restart. Single use is #consumeAuthority's.
if (
@@ -641,20 +644,29 @@ export class Broker {
fail('decision-mismatch');
return { class: cls, decision: d.id };
}
// The raise's context: its target and approval option, from the decision.raise action.allowed.
#raised(business, decision) {
const evidence = this.#store.get(
"SELECT body FROM events WHERE business=? AND json_extract(body,'$.decision')=? AND json_extract(body,'$.operation')='decision.raise' AND kind='action.allowed' ORDER BY seq LIMIT 1",
business,
decision,
);
return evidence ? JSON.parse(evidence.body) : null;
}
// Whether an executor already used the decision. decision.raise records context, not permission
// consumed by an executor.
#spent(business, decision) {
return !!this.#store.get(
"SELECT 1 FROM events WHERE business=? AND kind='action.allowed' AND json_extract(body,'$.decision')=? AND json_extract(body,'$.operation') IS NOT 'decision.raise' LIMIT 1",
business,
decision,
);
}
// Caller owns the transaction: authority, consumption and effect commit together.
// detail adds to the action.allowed body: a push's commit (row 93).
#consumeAuthority(s, action, context = {}, detail = {}) {
const result = this.#checkAuthority(s, action, context);
// decision.raise records context, not permission consumed by an executor.
if (
result.decision &&
this.#store.get(
"SELECT 1 FROM events WHERE business=? AND kind='action.allowed' AND json_extract(body,'$.decision')=? AND json_extract(body,'$.operation') IS NOT 'decision.raise' LIMIT 1",
s.business,
result.decision,
)
)
fail('decision-consumed');
if (result.decision && this.#spent(s.business, result.decision)) fail('decision-consumed');
this.#event(
s,
'action.allowed',
@@ -732,6 +744,145 @@ export class Broker {
return { task_ref, commit };
});
}
// Row 137 (#1631): the bus host's git socket lands a task on its working repository's main.
// checkLand runs before the host touches the repository. The run must hold its claim and the
// decision: git.merge.protected, raised by this role in this thread with the task ref as its
// target, resolved by the human with its approval option and not yet used. Then land-work's
// checks (landing.mjs, into 'main'), and the verdict's commit must be the task's latest push,
// whose project the landing uses. A refusal is a code; nothing is recorded unless it refuses.
// The ticket names the landing to recordLand and landRefused, which may run after the run ended.
checkLand(cap, args) {
const s = this.#session(cap);
return this.#store.transaction(() => {
keys(args, ['task_ref', 'decision'], ['task_ref', 'decision']);
if (!taskRef(args.task_ref) || !id(args.decision)) fail('invalid-request');
const { task_ref, decision } = args;
this.#checkAuthority(s, MERGE, { decision, target: task_ref });
const facts = this.#landFacts(s, task_ref, decision);
const ticket = randomUUID();
this.#landings.set(ticket, { s, task_ref, decision });
return { ticket, ...facts };
});
}
// What a landing needs, checked again at recordLand. The decision's own project, when it names
// one, must be the push's.
#landFacts(s, task_ref, decision) {
if (this.#spent(s.business, decision)) fail('decision-consumed');
let l;
try {
l = readLanding(this.#store, this.#business(s.business), decision, { into: 'main' });
} catch (e) {
if (e instanceof LandingRefusal) fail(e.reason);
throw e;
}
if (l.task_ref !== task_ref) fail('work-ref');
const push = this.#latestPush(s.business, task_ref);
if (!push || push.commit !== l.commit) fail('ref-moved');
if (l.project && l.project !== push.project) fail('work-ref');
// land-work refuses a landing with no subject; an agent has no --subject to pass.
if (!l.subject) fail('no-subject');
return {
task_ref,
work_ref: l.work_ref,
decision,
project: push.project,
ref: push.ref,
commit: l.commit,
verdict: l.verdict,
subject: l.subject,
choice: l.choice,
by: l.by,
resolved_at: l.resolved_at,
};
}
#landing(ticket) {
return (typeof ticket === 'string' && this.#landings.get(ticket)) || fail('land-unknown');
}
// Records a landing the git socket made (working.mjs finishLand) as the action.allowed for
// git.merge.protected that spends the decision, on the task's trail, and tells the role. The
// checks run again inside the transaction; a refusal here moves main back. Trusted host only.
recordLand(ticket, land) {
const t = this.#landing(ticket);
const { s, task_ref, decision } = t;
const done = this.#store.transaction(() => {
const all = ['commit', 'previous', 'reviewed', 'base', 'patch_id'];
keys(land, all, all);
if (!all.every((k) => oid(land[k]))) fail('invalid-request');
const result = this.#authorityFor(s, MERGE, { decision, target: task_ref });
const facts = this.#landFacts(s, task_ref, decision);
if (facts.commit !== land.reviewed) fail('ref-moved');
const { commit, previous, reviewed, base, patch_id } = land;
this.#event(
s,
'action.allowed',
{ project: facts.project, ref: 'refs/heads/main', commit, previous, reviewed, base, patch_id, verdict: facts.verdict, action: MERGE, ...result, target: task_ref },
task_ref,
);
this.#landMessage(s, decision, `Landed ${task_ref} on ${facts.project} main under decision ${decision}: main moved from ${previous} to ${commit}, the squash of reviewed ${reviewed}.`);
return { task_ref, commit };
});
this.#landings.delete(ticket);
return done;
}
// Ends a landing that didn't land: action.refused with the code on the task's trail, as a
// refusal of the run's own request is filed. With notify, the role is told too: the git socket
// had already answered "started" and the run has no other way to learn it. Trusted host only.
landRefused(ticket, { code, detail = null, notify = false } = {}) {
const t = this.#landing(ticket);
if (!/^[a-z-]{1,64}$/.test(code ?? '') || (detail !== null && !text(detail, 1000)) || typeof notify !== 'boolean')
fail('invalid-request');
this.#secretCheck({ detail });
this.#store.transaction(() => {
this.#event(t.s, 'action.refused', { code }, t.task_ref);
if (notify)
this.#landMessage(t.s, t.decision, `Landing of ${t.task_ref} under decision ${t.decision} refused: ${code}.${detail === null ? '' : `\n${detail}`}`);
});
this.#landings.delete(ticket);
return code;
}
// The outcome as a RESULT from the bus, as #notice sends: the runner takes a turn on it and sends
// no automatic reply. It goes to the role in the thread the decision was raised in.
#landMessage(s, decision, body) {
const mid = randomUUID();
this.#store.run(
'INSERT INTO messages(id,at,business,from_role,from_run,to_role,class,in_reply_to,decision,corrects,body) VALUES(?,?,?,?,?,?,?,?,?,?,?)',
mid,
this.#now(),
s.business,
NOTICE_ROLE,
NOTICE_RUN,
s.role,
'RESULT',
null,
decision,
null,
body,
);
this.#route(s.business, mid, s.role, s.thread);
}
// Trusted launcher API: whether the role, in the thread (null for a role run), holds a merge
// decision a landing could use: raised there with a task ref as its target, resolved with its
// approval option, not yet used. The launcher binds the git socket's land op only then; checkLand
// still checks everything.
landHeld(record) {
keys(record, ['business', 'role', 'thread'], ['business', 'role', 'thread']);
this.#business(identifier(record.business));
identifier(record.role);
if (record.thread !== null) identifier(record.thread);
for (const d of this.#store.all(
'SELECT * FROM decisions WHERE business=? AND action=? AND raised_by_role=? ORDER BY seq',
record.business,
MERGE,
record.role,
)) {
if (this.#raisedThread(d) !== record.thread) continue;
const context = this.#raised(d.business, d.id);
const r = this.#closed(d.id);
if (context && taskRef(context.target) && r?.op === 'resolved' && r.choice === context.approvalChoice && !this.#spent(d.business, d.id))
return true;
}
return false;
}
// Row 93: what the git socket's fetch for a reviewer may send, from the review.requested event the
// reviewer cites. The caller must hold review.verdict and be the request's reviewer. Trusted host only.
reviewFetch(cap, { task_ref, request } = {}) {
@@ -762,7 +913,7 @@ export class Broker {
return this.#store.transaction(() => {
this.#agent(s);
if (['human.input', 'launch.revoked', 'launch.restored', ...REVIEW_KINDS].includes(kind)) fail('reserved-event');
if (kind === 'action.allowed' && body?.action === 'git.push.working') fail('reserved-event');
if (kind === 'action.allowed' && ['git.push.working', MERGE].includes(body?.action)) fail('reserved-event');
return this.#event(s, kind, body, subject);
});
}
+9
View File
@@ -11,6 +11,10 @@ const LAUNCH_OPS = new Set([
'checkPush',
'recordPush',
'reviewFetch',
'checkLand',
'recordLand',
'landRefused',
'landHeld',
'threadsState',
'threadIdle',
]);
@@ -23,6 +27,11 @@ function launchOp(m) {
if (m.op === 'checkPush') return runtime.checkPush(m.cap, m.task_ref);
if (m.op === 'recordPush') return runtime.recordPush(m.cap, m.push);
if (m.op === 'reviewFetch') return runtime.reviewFetch(m.cap, m.args);
// Row 137 (#1631): the git socket's land op, and the launcher's question whether to bind it.
if (m.op === 'checkLand') return runtime.checkLand(m.cap, m.args);
if (m.op === 'recordLand') return runtime.recordLand(m.ticket, m.land);
if (m.op === 'landRefused') return runtime.landRefused(m.ticket, m.args);
if (m.op === 'landHeld') return runtime.landHeld(m.record);
// Row 121 (#1615): the launcher's view of the business's threads, and its idle release.
if (m.op === 'threadsState') return runtime.threadsState(m.business);
if (m.op === 'threadIdle') return runtime.threadIdle(m.record);
+7
View File
@@ -141,6 +141,9 @@ export async function startBroker({
// and recorded once the ref moved; a reviewer's fetch is checked against its review.requested.
const checkPush = (cap, task_ref) => refusing(cap, () => broker.checkPush(cap, task_ref), task_ref);
const reviewFetch = (cap, args) => refusing(cap, () => broker.reviewFetch(cap, args), args?.task_ref);
// Row 137 (#1631): the git socket's land op. checkLand's refusal is the run's, like checkPush's.
// A landing then ends in recordLand or landRefused under its ticket, which outlive the run's cap.
const checkLand = (cap, args) => refusing(cap, () => broker.checkLand(cap, args), args?.task_ref);
// Metadata only (service, date, state); the token stays in Credentials.
function credentialStatus(business, role) {
return credentials
@@ -161,6 +164,10 @@ export async function startBroker({
checkPush,
recordPush: (cap, push) => refusing(cap, () => broker.recordPush(cap, push), push?.task_ref),
reviewFetch,
checkLand,
recordLand: (ticket, land) => broker.recordLand(ticket, land),
landRefused: (ticket, args) => broker.landRefused(ticket, args),
landHeld: (record) => broker.landHeld(record),
refuse: (cap, code, subject) => broker.refuse(cap, code, subject),
credentialStatus,
launches: bound,
@@ -14,8 +14,11 @@ test('Broker keeps the existing sandbox client import closure: no new imports or
assert.deepEqual([...source.matchAll(/^import .*;$/gm)].map(m => m[0]), [
"import { randomUUID, randomBytes } from 'node:crypto';",
"import { loadRequirements } from '../../tasks/src/requirements.mjs';",
// Row 137 (#1631): land-work's checks, in RUNNER_FILES beside the broker. They import nothing.
"import { MERGE, LandingRefusal, readLanding } from './landing.mjs';",
]);
assert.doesNotMatch(source, /\bimport\s*\(/);
assert.doesNotMatch(readFileSync(new URL('../src/landing.mjs', import.meta.url), 'utf8'), /\bimport\b/);
});
test('without a trusted project adapter all three async paths refuse project-not-configured', async t => {