salvage: uncommitted work from dewey-r156-wt (disk cleanup #824, 2026-11-06)

This commit is contained in:
mos committed 2026-11-06 10:00:52 -06:00
1 parent de74dd4735
commit 0ee3365717
14 files changed
+757 -90

No files matched your search

+59 -11
View File
@@ -966,19 +966,59 @@ export class Broker {
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',
return !this.#held(record.business, record.role, record.thread).next().done;
}
// Row 156 (#1651): every merge decision a non-thread instance holds as landHeld counts it, in
// decision order, with its task ref and resolution time. The launcher's landing pass starts the
// holder with these in the session file's landing. Trusted launcher API, not a socket verb.
landHolders(business) {
this.#business(identifier(business));
return [...this.#held(business, null, null)].map(({ d, context, r }) => ({
instance: d.raised_by_role,
decision: d.id,
task_ref: context.target,
resolved_at: r.at,
}));
}
// Row 156 (#1651): the landing pass's stop for a role run started before its decision was
// resolved, so it has no land_work for it. As threadIdle, it rechecks and releases in one
// synchronous call. Messages delivered to the run and not read are a turn in progress and keep
// it; messages still waiting stay undelivered and go to the next holder. by='land-relaunch'.
landRelease(record) {
keys(record, ['business', 'run', 'decision'], ['business', 'run', 'decision']);
this.#business(identifier(record.business));
const key = record.business + ':' + identifier(record.run);
const session = this.#runs.get(key) ?? fail('unknown-run');
if (this.#ended.has(key)) fail('run-ended');
if (session.thread) fail('thread-not-allowed');
const h = this.#holder(record.business, session.role);
if (h?.op !== 'claim' || h.holder_run !== record.run) fail('not-holder');
if (!this.landHolders(record.business).some((x) => x.instance === session.role && x.decision === record.decision))
fail('decision-not-held');
const { unread } = this.#store.get(
"SELECT count(*) AS unread FROM messages m JOIN deliveries d ON d.message=m.id AND d.op='delivered' WHERE m.business=? AND m.to_role=? AND d.holder_run=? AND NOT EXISTS(SELECT 1 FROM deliveries r WHERE r.message=m.id AND r.op='read')",
record.business,
MERGE,
record.role,
)) {
if (d.class !== 'cross-role' || this.#raisedThread(d) !== record.thread) continue;
session.role,
record.run,
);
if (unread) return { released: false, unread };
this.#store.transaction(() => this.#claimEnd(session, h, 'release', null, null, 'land-relaunch'));
return { released: true };
}
// The merge decisions landHeld would count, each with its raised context and closing event. A
// null role is every non-thread instance's (row 156): raised outside a thread by a role that
// isn't a thread instance.
*#held(business, role, thread) {
for (const d of role === null
? this.#store.all('SELECT * FROM decisions WHERE business=? AND action=? ORDER BY seq', business, MERGE)
: this.#store.all('SELECT * FROM decisions WHERE business=? AND action=? AND raised_by_role=? ORDER BY seq', business, MERGE, role)) {
if (d.class !== 'cross-role' || this.#raisedThread(d) !== thread) continue;
if (role === null && this.#threaded(business, d.raised_by_role)) 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;
yield { d, context, r };
}
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.
@@ -1190,17 +1230,25 @@ export class Broker {
}
}
// Only the runner's bound session and delivered message identify a turn; no host path crosses the wire.
// Row 156 (#1651, sage-0808): a role run's landing turn is a merge decision id instead. Its begin
// needs the decision held for the run's role; its end needs that begin, since the landing may
// have spent the decision by then.
async #requestUsage(s, verb, args) {
try {
keys(args, ['turn'], ['turn']);
identifier(args.turn);
const phase = verb === 'usage.begin' ? 'begin' : 'end';
this.#store.transaction(() => {
this.#agent(s);
if (!this.#store.get("SELECT 1 FROM messages m JOIN deliveries d ON d.message=m.id WHERE m.id=? AND m.business=? AND m.to_role=? AND d.op='delivered' AND d.holder_run=?", args.turn, s.business, s.role, s.run))
fail('not-recipient');
if (this.#store.get("SELECT 1 FROM messages m JOIN deliveries d ON d.message=m.id WHERE m.id=? AND m.business=? AND m.to_role=? AND d.op='delivered' AND d.holder_run=?", args.turn, s.business, s.role, s.run))
return;
if (!s.thread && (phase === 'begin'
? this.landHolders(s.business).some((h) => h.instance === s.role && h.decision === args.turn)
: this.#store.get("SELECT 1 FROM usage_records WHERE business=? AND run=? AND turn=? AND phase='begin'", s.business, s.run, args.turn)))
return;
fail('not-recipient');
});
if (!this.#usage) fail('usage-not-configured');
const phase = verb === 'usage.begin' ? 'begin' : 'end';
try {
return await this.#usage.collect(s, phase, args.turn);
} finally {
+6
View File
@@ -18,6 +18,8 @@ const LAUNCH_OPS = new Set([
'threadsState',
'threadIdle',
'mayLaunch',
'landHolders',
'landRelease',
]);
function launchOp(m) {
if (m.op === 'identity') return runtime.identity(m.cap);
@@ -38,6 +40,10 @@ function launchOp(m) {
if (m.op === 'threadIdle') return runtime.threadIdle(m.record);
// Row 125 (#1619, REQ-LIMIT-4): the limit checks' answer before a launch (async).
if (m.op === 'mayLaunch') return runtime.mayLaunch(m);
// Row 156 (#1651): the launcher's landing pass: who holds a merge decision, and the stop of a
// role run started before its decision was resolved.
if (m.op === 'landHolders') return runtime.landHolders(m.business);
if (m.op === 'landRelease') return runtime.landRelease(m.record);
return runtime.credentialStatus(m.business, m.role);
}
// The host's request id comes back on the reply, so a late reply can't
+3
View File
@@ -194,6 +194,9 @@ export async function startBroker({
recordLand: (ticket, land) => broker.recordLand(ticket, land),
landRefused: (ticket, args) => broker.landRefused(ticket, args),
landHeld: (record) => broker.landHeld(record),
// Row 156 (#1651): the launcher's landing pass.
landHolders: (business) => broker.landHolders(business),
landRelease: (r) => broker.landRelease(r),
refuse: (cap, code, subject) => broker.refuse(cap, code, subject),
credentialStatus,
launches: bound,
+125 -3
View File
@@ -16,6 +16,7 @@ import { Store } from '../src/store.mjs';
import { Broker } from '../src/broker.mjs';
import { readLanding } from '../src/landing.mjs';
import { fixture } from './spec-fixtures.mjs';
import { createUsage, turnRecords } from '../src/usage.mjs';
const TASK = 'vikunja:1/2';
const MERGE = 'git.merge.protected';
@@ -41,11 +42,12 @@ const options = [
];
const merge = { action: MERGE, target: TASK, question: 'Squash-merge work/vikunja-1-2 into main?', options, recommendation: 'yes', blocking: false };
function setup(t, { arbiters, roles: r } = {}) {
// `extra(store, root, businesses)` adds Broker options, as the usage collector and limit checks.
function setup(t, { arbiters, roles: r, extra = () => ({}) } = {}) {
const root = mkdtempSync(join(tmpdir(), 'bus-land-'));
const store = new Store(root);
const businesses = { demo: demo(arbiters, r) };
const b = new Broker({ store, businesses });
const b = new Broker({ store, businesses, ...extra(store, root, businesses) });
t.after(() => {
store.close();
rmSync(root, { recursive: true, force: true });
@@ -84,7 +86,7 @@ function setup(t, { arbiters, roles: r } = {}) {
const held = (role = 'merge-gate', thread = null) => b.landHeld({ business: 'demo', role, thread });
const check = (args) => b.checkLand(caps['merge-gate'], args);
const routed = (d) => ({ ...store.get('SELECT class, route_to FROM decisions WHERE id=?', d.id) });
return { b, store, businesses, caps, call, push, approve, raise, resolve, ready, land, events, merges, results, held, check, routed };
return { b, store, root, businesses, caps, call, push, approve, raise, resolve, ready, land, events, merges, results, held, check, routed };
}
test('REQ-DEC-2: a merge-gate raise its arbiter resolved lands through the host op: checkLand answers the facts and a ticket, records nothing, and recordLand spends the decision once', (t) => {
@@ -353,6 +355,126 @@ test('landHeld is true only for an approved, unspent cross-role merge decision t
assert.throws(() => w.b.landHeld({ business: 'nope', role: 'merge-gate', thread: null }));
});
// Row 156 (#1651): landHolders is landHeld's list for the launcher's landing pass. At each step
// of the landHeld test above, the two agree.
test('landHolders lists a merge decision exactly while landHeld is true for its non-thread instance, in decision order', (t) => {
const w = setup(t);
const holders = () => w.b.landHolders('demo');
const agree = (msg) => assert.equal(holders().some((h) => h.instance === 'merge-gate'), w.held(), msg);
assert.deepEqual(holders(), []);
w.push(A);
w.approve(A);
const d = w.raise();
agree('open');
w.resolve(d, 'no');
agree('declined');
const notTask = w.raise('merge-gate', { target: 'main' });
w.resolve(notTask);
agree('a target that is not a task ref');
const gated = w.raise('coder');
w.resolve(gated, 'yes', 'human');
assert.deepEqual(holders(), [], 'gated');
const first = w.raise();
w.resolve(first);
const second = w.raise();
w.resolve(second);
agree('approved');
const at = (x) => w.store.get("SELECT at FROM decision_events WHERE decision=? AND op='resolved'", x.id).at;
const entry = (x) => ({ instance: 'merge-gate', decision: x.id, task_ref: TASK, resolved_at: at(x) });
assert.deepEqual(holders(), [entry(first), entry(second)]);
// The landing spends the first; the second is still listed.
w.b.recordLand(w.check({ task_ref: TASK, decision: first.id }).ticket, w.land);
assert.deepEqual(holders(), [entry(second)], 'spent');
assert.throws(() => w.b.landHolders('nope'), /unknown-business/);
assert.throws(() => w.b.landHolders('no such'), /invalid-request/);
});
test("landHolders leaves out a thread instance's decision, even one raised before its role became one", (t) => {
const w = setup(t);
w.ready();
assert.equal(w.b.landHolders('demo').length, 1);
// A restart with merge-gate now a thread instance: landHeld still answers for the role and
// thread asked, but the landing pass starts no non-thread run for it.
const roles2 = { ...roles, 'merge-gate': { ...roles['merge-gate'], definition: 'assistant' } };
const b2 = new Broker({ store: w.store, businesses: { demo: demo(undefined, roles2) } });
assert.equal(b2.landHeld({ business: 'demo', role: 'merge-gate', thread: null }), true);
assert.deepEqual(b2.landHolders('demo'), []);
});
// Row 156: the landing pass stops a role run started before its decision was resolved. Unread
// delivered messages keep it; waiting ones stay undelivered for the next holder.
test('landRelease releases the holding run by land-relaunch once nothing delivered to it is unread, and waiting messages go to the next holder', (t) => {
const w = setup(t);
const { d } = w.ready();
const release = (extra = {}) => w.b.landRelease({ business: 'demo', run: 'merge-gate-run', decision: d.id, ...extra });
const sent = w.call('human', 'message.send', { to: 'merge-gate', body: 'one' });
assert.equal(w.call('merge-gate', 'message.receive').length, 1);
assert.deepEqual(release(), { released: false, unread: 1 });
assert.equal(w.store.get("SELECT op FROM role_claims WHERE role='merge-gate' ORDER BY seq DESC LIMIT 1").op, 'claim', 'kept');
w.call('merge-gate', 'message.read', { id: sent.id });
const waiting = w.call('human', 'message.send', { to: 'merge-gate', body: 'two' });
assert.throws(() => release({ decision: 'nope' }), /decision-not-held/);
assert.throws(() => release({ extra: 1 }), /invalid-request/);
assert.throws(() => w.b.landRelease({ business: 'demo', run: 'no-run', decision: d.id }), /unknown-run/);
assert.throws(() => w.b.landRelease({ business: 'demo', run: 'coder-run', decision: d.id }), /decision-not-held/);
// A run of the same instance that doesn't hold the claim releases nothing.
w.caps.other = w.b.bindLaunch({ business: 'demo', role: 'merge-gate', run: 'merge-gate-0', harness: 'pi' });
assert.throws(() => w.b.landRelease({ business: 'demo', run: 'merge-gate-0', decision: d.id }), /not-holder/);
assert.deepEqual(release(), { released: true });
assert.deepEqual(
{ ...w.store.get("SELECT op, holder_run, \"by\", decision, reason FROM role_claims WHERE role='merge-gate' ORDER BY seq DESC LIMIT 1") },
{ op: 'release', holder_run: 'merge-gate-run', by: 'land-relaunch', decision: null, reason: null },
);
assert.throws(() => release(), /not-holder/, 'released once');
assert.throws(() => w.call('merge-gate', 'message.receive'), /not-holder/);
w.b.endLaunch({ business: 'demo', run: 'merge-gate-run', reason: 'exited' });
assert.throws(() => release(), /run-ended/);
assert.equal(w.store.get("SELECT count(*) AS n FROM deliveries WHERE message=? AND op='delivered'", waiting.id).n, 0, 'still waiting');
// The decision is still held, and the next run gets the waiting message.
assert.equal(w.held(), true);
w.caps.next = w.b.bindLaunch({ business: 'demo', role: 'merge-gate', run: 'merge-gate-2', harness: 'pi' });
w.call('next', 'role.claim');
assert.deepEqual(w.call('next', 'message.receive').map((m) => m.id), [waiting.id]);
assert.equal(w.store.get("SELECT holder_run FROM deliveries WHERE message=? AND op='delivered'", waiting.id).holder_run, 'merge-gate-2');
});
// Row 156 (sage-0808): a role run's landing turn brackets its usage with the decision id. The
// begin needs the decision held for the run's instance; the end needs that begin.
test('usage.begin with a decision id needs it held for the run\'s instance, usage.end needs the begin, and the limit checks see the landing turn', async (t) => {
const asked = [];
const w = setup(t, {
extra: (store, root, businesses) => {
const usage = createUsage({ store, dataRoot: root, businesses, log: () => {} });
return {
usage: { collect: (...a) => usage.then((u) => u.collect(...a)), view: () => ({}) },
limits: { mustStop: async (business, subject) => { asked.push({ subject, turns: turnRecords(store, business) }); return { stop: false }; } },
};
},
});
const usage = (who, phase, turn) => w.call(who, `usage.${phase}`, { turn });
const open = w.raise('merge-gate');
await assert.rejects(usage('merge-gate', 'begin', open.id), { code: 'not-recipient' }, 'open');
w.push(A);
w.approve(A);
w.resolve(open);
await assert.rejects(usage('pm', 'begin', open.id), { code: 'not-recipient' }, "another instance's");
await assert.rejects(usage('merge-gate', 'end', open.id), { code: 'not-recipient' }, 'no begin');
assert.deepEqual(await usage('merge-gate', 'begin', open.id), { recorded: true });
// The landing spends the decision inside the turn; the end still records.
w.b.recordLand(w.check({ task_ref: TASK, decision: open.id }).ticket, w.land);
await assert.rejects(usage('pm', 'end', open.id), { code: 'not-recipient' }, "another run's begin");
assert.deepEqual(await usage('merge-gate', 'end', open.id), { recorded: true });
assert.deepEqual(
w.store.all('SELECT run, instance, turn, phase FROM usage_records ORDER BY seq').map((r) => ({ ...r })),
['begin', 'end'].map((phase) => ({ run: 'merge-gate-run', instance: 'merge-gate', turn: open.id, phase })),
);
assert.equal(asked.length, 1);
assert.deepEqual(asked[0].subject, { instance: 'merge-gate', provider: null, model: null });
assert.deepEqual(asked[0].turns.map((r) => [r.run, r.turn, r.instance]), [['merge-gate-run', open.id, 'merge-gate']]);
// Spent, a second landing turn's begin refuses.
await assert.rejects(usage('merge-gate', 'begin', open.id), { code: 'not-recipient' }, 'spent');
});
test('no run may write the action.allowed for git.merge.protected itself', (t) => {
const w = setup(t);
assert.throws(