fix(bus): keep the request route across setup waits and recheck association after admission (#1839)
This commit is contained in:
1 parent
3b071cac7b
commit
bec3583286
3 files changed
+189
-22
No files matched your search
@@ -16,11 +16,12 @@ export function createRequestLifecycle({ journal, services }) {
|
||||
const stateOf = (business, request) => journal.readRequest(business, request) ?? refuse('request-not-found');
|
||||
const actor = identity => ({ role: identity?.role ?? null, run: identity?.run ?? null });
|
||||
const authorize = (identity, state, operation) => services.authorize(identity, state, operation);
|
||||
const append = (state, kind, body, identity = null) => journal.appendRequest({ business: state.business, request: state.request,
|
||||
// `write` is journal.appendRequest outside a transaction and tx.appendRequest inside one.
|
||||
const append = (state, kind, body, identity = null, write = journal.appendRequest) => write({ business: state.business, request: state.request,
|
||||
expectedRevision: state.revision, kind, body, actor: actor(identity) });
|
||||
const wait = (state, code, identity = null, proposal = {}) => {
|
||||
const wait = (state, code, identity = null, proposal = {}, write = journal.appendRequest) => {
|
||||
if (state.wait?.kind === 'setup' && state.wait.code === code && !proposal.op) return state;
|
||||
return append(state, 'setup.wait', { code, subject: state.request, ...proposal }, identity);
|
||||
return append(state, 'setup.wait', { code, subject: state.request, ...proposal }, identity, write);
|
||||
};
|
||||
async function open(identity, args) {
|
||||
exact(args, ['request']); if (!id(args.request)) refuse('invalid-request');
|
||||
@@ -79,6 +80,20 @@ export function createRequestLifecycle({ journal, services }) {
|
||||
}
|
||||
return p;
|
||||
}
|
||||
// The host inventory and admission are asynchronous and neither bumps the request revision, so a
|
||||
// plan can go stale while a call is in flight. `staleCode` is the guard for that: callers read the
|
||||
// inventory as their last await and call it inside the synchronous journal transaction, so no
|
||||
// mapping or board change can land between this check and the append. It never confirms a stale plan.
|
||||
const associated = (rows, p) => Array.isArray(rows) && rows.some(m => object(m) && m.business === p.business && m.project === p.project &&
|
||||
m.name === p.name && m.tracker?.root === p.tracker.root && m.tracker?.project === p.tracker.project);
|
||||
function staleCode(plan, state, args, source, rows) {
|
||||
if (plan.existing && !associated(rows, plan)) return 'request-existing-project-changed';
|
||||
try { localPlan(plan, state, args, source); } catch (e) {
|
||||
if (!e.code?.startsWith('request-')) throw e;
|
||||
return e.code;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
async function admission(plan, state) {
|
||||
if (!services.admitPlan) return 'request-admission-unavailable';
|
||||
const result = await services.admitPlan(structuredClone(plan), structuredClone(state));
|
||||
@@ -98,7 +113,8 @@ export function createRequestLifecycle({ journal, services }) {
|
||||
if (state.phase !== 'selection' || state.wait?.kind === 'decision') refuse('request-phase-refused');
|
||||
const source = services.origin(identity, args.request);
|
||||
if (!services.project(state.business, args.source_project)) refuse('request-project-refused');
|
||||
const existing = state.wait?.code === 'request-existing-project';
|
||||
// The route is journaled when the owner stages it; no setup wait code can erase or imply it.
|
||||
const existing = state.route === 'existing';
|
||||
if (!existing && services.project(state.business, args.project)) refuse('request-project-collision');
|
||||
let candidate = null;
|
||||
if (existing) {
|
||||
@@ -111,7 +127,7 @@ export function createRequestLifecycle({ journal, services }) {
|
||||
if (current.revision !== args.revision) refuse('request-revision-conflict');
|
||||
return current;
|
||||
};
|
||||
const pendingWait = code => wait(recheck(), code, identity, { op: args.op, proposal_digest });
|
||||
const pendingWait = (code, write) => wait(recheck(), code, identity, { op: args.op, proposal_digest }, write);
|
||||
if (!prepare) return pendingWait('request-preparation-unavailable');
|
||||
const prepared = await prepare(structuredClone(state), structuredClone(args), structuredClone(source));
|
||||
recheck();
|
||||
@@ -124,9 +140,13 @@ export function createRequestLifecycle({ journal, services }) {
|
||||
}
|
||||
const plans = existing ? [prepared] : prepared;
|
||||
if (!Array.isArray(plans) || plans.length !== (existing ? 1 : 2)) refuse('invalid-request-plan');
|
||||
// Validate every plan before the first admission await. A change after this point is not a
|
||||
// malformed plan; the guard in the final transaction turns it into a stale wait.
|
||||
for (let i = 0; i < plans.length; i++) {
|
||||
const p = localPlan(plans[i], state, args, source);
|
||||
if (p.existing !== existing || (!existing && p.mode !== ['autonomous','guided'][i]) || (existing && p.tracker.root !== candidate.tracker.root)) refuse('request-plan-binding-refused');
|
||||
}
|
||||
for (const p of plans) {
|
||||
const missing = await admission(p, state); recheck(); if (missing) return pendingWait(missing);
|
||||
}
|
||||
// Both modes must display the same concrete resources and original intent.
|
||||
@@ -136,8 +156,15 @@ export function createRequestLifecycle({ journal, services }) {
|
||||
const question = `Confirm the resource plan for ${args.name}. ${existing ? `Existing project mode: ${mode}.` : 'Both new project choices use these resources with the selected mode.'} Original user intent and exact resources:\n${JSON.stringify(resources)}`;
|
||||
// Preserve the ordinary decision channel's existing display limit and all source bytes.
|
||||
if (question.length > 4096) return pendingWait('request-plan-display-too-large');
|
||||
// Last await. Preparation and admission may have outlived the association they bound, so read
|
||||
// it again and let the synchronous guard in the transaction decide before any decision exists.
|
||||
const rows = existing ? await projects(identity, { request: args.request }) : null;
|
||||
return journal.transaction(tx => {
|
||||
authorize(identity, state, 'propose');
|
||||
state = recheck();
|
||||
for (const plan of plans) {
|
||||
const late = staleCode(plan, state, args, source, rows);
|
||||
if (late) return pendingWait(late, tx.appendRequest);
|
||||
}
|
||||
const options = existing ? [{ key: 'confirm', text: 'Confirm this existing project association' }, { key: 'back', text: 'Choose another existing project' }]
|
||||
: [{ key: 'autonomous', text: 'New project — autonomous' }, { key: 'guided', text: 'New project — guided' }, { key: 'existing', text: 'Add to existing project' }];
|
||||
const decision = services.raiseDecision(identity, { action: 'project.bootstrap', question, options, recommendation: options[0].key,
|
||||
@@ -161,21 +188,22 @@ export function createRequestLifecycle({ journal, services }) {
|
||||
const binding = state.selection.bindings.find(b => b.choice === close.choice);
|
||||
if (!binding) refuse('request-selection-refused');
|
||||
const p = binding.plan;
|
||||
if (p.existing) {
|
||||
const mappings = await services.listProjects?.(structuredClone(state)) ?? [];
|
||||
if (!Array.isArray(mappings) || !mappings.some(m => m.business === business && m.project === p.project && m.name === p.name &&
|
||||
m.tracker?.root === p.tracker.root && m.tracker?.project === p.tracker.project)) return wait(state, 'request-existing-project-changed');
|
||||
}
|
||||
try {
|
||||
localPlan(p, state, { project: p.project, name: p.name, source_project: p.source.project, tracker_project: p.tracker.project },
|
||||
{ text: p.intent.text, source_message: state.source_message });
|
||||
} catch (e) {
|
||||
if (!e.code?.startsWith('request-')) throw e;
|
||||
return wait(state, e.code);
|
||||
}
|
||||
const missing = await admission(binding.plan, state);
|
||||
const planArgs = { project: p.project, name: p.name, source_project: p.source.project, tracker_project: p.tracker.project };
|
||||
const planSource = { text: p.intent.text, source_message: state.source_message };
|
||||
const inventory = async () => p.existing ? await services.listProjects?.(structuredClone(state)) ?? [] : null;
|
||||
// Check before asking admission, so a plan that is already stale never reaches it.
|
||||
const early = staleCode(p, state, planArgs, planSource, await inventory());
|
||||
if (early) return wait(state, early);
|
||||
const missing = await admission(p, state);
|
||||
if (missing) return wait(state, missing);
|
||||
return append(state, 'selection.confirmed', { decision: binding.decision, choice: binding.choice, digest: binding.digest });
|
||||
// Admission is the last slow call. Read the association once more and confirm only if it still
|
||||
// matches, in the same synchronous step as the append.
|
||||
const rows = await inventory();
|
||||
return journal.transaction(tx => {
|
||||
const late = staleCode(p, state, planArgs, planSource, rows);
|
||||
if (late) return wait(state, late, null, {}, tx.appendRequest);
|
||||
return append(state, 'selection.confirmed', { decision: binding.decision, choice: binding.choice, digest: binding.digest }, null, tx.appendRequest);
|
||||
});
|
||||
});
|
||||
}
|
||||
return Object.freeze({ open, get, projects, propose, resume,
|
||||
|
||||
@@ -55,7 +55,9 @@ function initial(business, request, b) {
|
||||
if (!text(b.intent.text) || digest(b.intent.text) !== b.intent.sha256 || b.intent.source_message !== b.source_message || b.intent.generated !== false) refuse('invalid-request-event');
|
||||
return { version: 1, request, business, revision: 1, source_message: b.source_message, thread: b.thread, session: b.session,
|
||||
owner: b.owner, pm: b.pm, phase: 'selection', project: null, chat_project: null, mode: null, mode_revision: 0,
|
||||
plan_digest: null, spec_commit: null, snapshot: null, tasks: [], delivery: null, question: null, candidate: null, selection: null, wait: null };
|
||||
plan_digest: null, spec_commit: null, snapshot: null, tasks: [], delivery: null, question: null, candidate: null, selection: null,
|
||||
// The owner's chosen route survives every setup wait; a wait code never says which route was chosen.
|
||||
route: null, wait: null };
|
||||
}
|
||||
function reduce(state, e) {
|
||||
const code = 'invalid-request-event', b = e.body;
|
||||
@@ -84,7 +86,7 @@ function reduce(state, e) {
|
||||
exact(b, ['decision','choice'], [], code);
|
||||
if (s.wait?.kind !== 'decision' || s.selection?.decision !== b.decision || !['existing','back'].includes(b.choice) ||
|
||||
(b.choice === 'existing') !== (s.wait.code === 'request-mode')) refuse(code);
|
||||
s.selection = null;
|
||||
s.selection = null; s.route = 'existing';
|
||||
s.wait = { kind: 'setup', code: 'request-existing-project', subject: s.request, resume_phase: 'selection', observed_revision: s.revision };
|
||||
} else if (e.kind === 'selection.confirmed') {
|
||||
exact(b, ['decision','choice','digest'], [], code);
|
||||
|
||||
Reference in new issue
Block a user