Skip to content

Commit a0b0f03

Browse files
acn-ericlawclaude
andcommitted
fix(agent-question): the first accepted decision stands when two devices decide one card - RFC-0014 step 5 drive
In lockstep with the Python pack: a decision's write applies when the row is still open or the engine accepted it, in the memory inbox and SQL_DECIDED alike; the losing device gets the row as it stands. Vectors version 2 byte for byte, SHA re-pinned; the ask-person README's audit claim narrowed to a refused call's note. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
1 parent 9eb428c commit a0b0f03

8 files changed

Lines changed: 224 additions & 12 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,17 @@
112112
`test/agent-question.test.mjs` (48 tests; a live PostgreSQL gated on `AGENT_QUESTION_TEST_URL`). **Upgrade note:**
113113
none - a new application.
114114

115+
### Fixed
116+
117+
1. **The agent-question app keeps the first accepted decision when two devices decide one card at the same moment**
118+
(RFC-0014 step 5, the live drive of 2026-11-19), in lockstep with the Python pack. Both clicks pass the open check and
119+
reach the engine, which resumes at most once; the device that lost got `nothing waiting`, and the row took whichever
120+
outcome landed last - so a late 410 could overwrite the accepted decision and its operator. A decision's write now
121+
applies when the row is still open or when the engine accepted the decision (`state = 'answered'`), in the memory inbox
122+
and in `SQL_DECIDED` alike; a refused write answers the losing device with the row as it stands, and nothing is
123+
broadcast for it. The shared vectors are version 2 with the two-devices transition case, byte-identical with the Python
124+
pack and SHA-pinned. **Upgrade note:** none - the schema is unchanged.
125+
115126
### Changed
116127

117128
1. **`web.crawl` accepts a relative `include`**, in lockstep with the Python pack: a prefix that does not start with `/` matches

‎extensions/agent-question/README.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ application is one of those routes. It is a standalone application on the packag
2121
(`expired`); a refused decision or an endpoint that did not answer leaves it `open` with the outcome shown. An
2222
advice is `open` until the person marks it `seen`. Open waits past their expiry are marked `expired` by a sweeper
2323
and before every replay. The engine stays the authority: a second device on the same card gets `nothing waiting`,
24-
and the row records the first. Rows are never deleted here; `schema/purge.sql` is the installation's.
24+
and the row records the first: an acceptance always wins the row, and a `nothing waiting` that lands after one
25+
never overwrites it (the live drive of 2026-11-19). Rows are never deleted here; `schema/purge.sql` is the
26+
installation's.
2527

2628
The Python twin (mercury-python `extensions/agent-question`) serves the same route, the same page and the same schema,
2729
pinned by the same vector file (`test/vectors/agent-question-vectors.json`, byte-identical in both packs).

‎extensions/agent-question/agent-question.mjs‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ export class MemoryInbox {
306306
async decided(id, decisions, who, state, outcome, now) {
307307
const row = this.rows.get(id);
308308
if (!row) return null;
309+
if (row.state !== 'open' && state !== 'answered') return row; // decided first by another device: a later nothing-waiting never overwrites it
309310
const updated = { ...row, seq: this.bump(), state, decisions, outcome, answered_by: who, answered_at: now, updated_at: now };
310311
this.rows.set(id, updated);
311312
return updated;
@@ -357,7 +358,7 @@ export const SQL_GET = `SELECT ${COLUMNS} FROM question_inbox WHERE id = $1`;
357358
export const SQL_SINCE = `SELECT ${COLUMNS} FROM question_inbox WHERE seq > $1 ORDER BY seq LIMIT $2`;
358359
export const SQL_DECIDED = "UPDATE question_inbox SET seq = nextval('question_inbox_seq'), state = $1, decisions = $2::jsonb, "
359360
+ 'outcome = $3::jsonb, answered_by = $4, answered_at = $5::timestamptz, updated_at = $6::timestamptz '
360-
+ `WHERE id = $7 RETURNING ${COLUMNS}`;
361+
+ `WHERE id = $7 AND (state = 'open' OR $8::text = 'answered') RETURNING ${COLUMNS}`;
361362
export const SQL_SEEN = "UPDATE question_inbox SET seq = nextval('question_inbox_seq'), state = 'seen', updated_at = $1::timestamptz "
362363
+ `WHERE id = $2 AND kind = 'advice' RETURNING ${COLUMNS}`;
363364
export const SQL_EXPIRE = "UPDATE question_inbox SET seq = nextval('question_inbox_seq'), state = 'expired', updated_at = $1::timestamptz "
@@ -436,7 +437,9 @@ export class PostgresInbox {
436437
}
437438

438439
async decided(id, decisions, who, state, outcome, now) {
439-
return this.one(SQL_DECIDED, [state, compact(decisions), compact(outcome), who, now, now, id]);
440+
const row = await this.one(SQL_DECIDED, [state, compact(decisions), compact(outcome), who, now, now, id, state]);
441+
// no row: the guard kept a row another device decided first (or the row is gone) - answer with it as stored
442+
return row ?? await this.get(id);
440443
}
441444

442445
async seen(id, now) {
@@ -775,7 +778,8 @@ async function forward(url, body, config) {
775778
}
776779
}
777780

778-
/** POST /questions/{id}/decide with {decisions, decided_by?}: the click, automated. */
781+
/** POST /questions/{id}/decide with {decisions, decided_by?}: the click, automated. The first accepted decision
782+
* stands: an acceptance always wins the row, and a nothing-waiting that lands after one never overwrites it. */
779783
export async function decide(req, res, id) {
780784
const config = currentSettings();
781785
let body;
@@ -814,8 +818,15 @@ export async function decide(req, res, id) {
814818
? { status: 502, body: { type: 'error', message: `the reply endpoint did not answer - ${error}` } }
815819
: { status, body: answer };
816820
const state = stateAfter(outcome.status, outcome.body);
817-
const updated = await inbox().decided(id, decisions.map((d) => ({ ...d })), identity.who, state, outcome, isoUtc());
821+
const now = isoUtc();
822+
const updated = await inbox().decided(id, decisions.map((d) => ({ ...d })), identity.who, state, outcome, now);
818823
if (!updated) return sendError(res, 404, `row ${id} is not in the inbox`);
824+
if (updated.answered_by !== identity.who || updated.answered_at !== now) {
825+
// two devices on one card: the other one's decision was accepted while this one was in flight, so the engine
826+
// answered nothing-waiting here and the row stands as the first decision left it - this device sees who
827+
log.info(`Row ${id}: ${identity.who}'s decision met a row ${updated.state} by ${updated.answered_by} - the row stands`);
828+
return sendJson(res, 200, { row: publicRow(updated), outcome });
829+
}
819830
wakeups.publish([updated]);
820831
log.info(`Row ${id} decided by ${identity.who} via ${identity.via} - ${outcome.status} -> ${state}`);
821832
return sendJson(res, 200, { row: publicRow(updated), outcome });

‎extensions/ask-person/README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ engine; a malformed one or another version is a 400, one no key signs a 403.
9595
renders as expired; an engine that has moved on shows its reply.
9696
- `POST /ask/reply` takes `{"ticket": "...", "decisions": [...], "decided_by": "..."}`: `{id, approved, note?}` for a
9797
tool call, `{id, answer}` for a question - the shapes the loop accepts. Each tool call's note is prefixed
98-
`[<who> at <time>]`, so the audit store carries who decided without a new field; an answer travels unchanged. The
98+
`[<who> at <time>]`; the audit store carries it when the note reaches the model, which the loop does for a refused
99+
call - an approved call's decision stays on the channel's record and in this log; an answer travels unchanged. The
99100
engine's status and JSON body come back as they are; a graph that does not answer or answers outside the contract is a
100101
502 `{"type": "error", "message": ...}`; an expired ticket is a 410 `{"type": "expired"}`.
101102
- **Who may answer** (`ask.reply.auth`): `none` in development - no sign-in, `decided_by` is taken as given or the

‎memory/continuity.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,11 @@
249249
`GET /questions/events` with `Last-Event-ID`, the automated click `POST /questions/{id}/decide` to the link's own base under
250250
the app's bearer, `seen`; states open, answered, expired, seen; a sweeper for expiry; identity as the main flow's. Pinned by
251251
`test/vectors/agent-question-vectors.json` (30 cases, byte-identical, SHA-pinned - the reference inbox replayed on the scripted
252-
sequence lands on the same rows, ids and change sequence) and 48 tests. **How to apply:** the app never talks to the engine,
252+
sequence lands on the same rows, ids and change sequence) and 48 tests. **Driven live 2026-11-19 (RFC-0014 step 5, in the
253+
engines' `docs/test-reports/ask-a-person-live-drive.md`):** the Node twins carried a question, a decision, the advices and
254+
two devices on one card end to end behind the Java Playground; the first-decision rule found by the drive is in both
255+
packs - a decision's write applies when the row is still open or when the engine accepted it, so `answered` always wins
256+
and the losing device gets the row as it stands (vectors version 2, 31 cases). **How to apply:** the app never talks to the engine,
253257
only to the reply endpoint the link names; a row is never deleted by the app; the runtime's differences - `$n` placeholders,
254258
int8 as text, `jose` - stay inside the module, never in the contract.
255259
<!-- id: agent-question-app-node | created: 2026-11-19 | last_used: 2026-11-19 | uses: 1 | tier: working | origin: 2026-11-19-074617 -->
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Session (2026-11-19T08:48:22.000Z)
2+
3+
**Agent:** Claude Code
4+
**Title:** RFC-0014 step 5, the live drive - the Node twins end to end; the agent-question app keeps the first accepted decision
5+
**User:** Eric - "please proceed with step 5 of the build plan" (the drive is recorded in the engines' repository).
6+
7+
## What We Did
8+
9+
- **The Node twins driven live behind the Java Playground** (the record is the engines' `docs/test-reports/ask-a-person-live-drive.md`):
10+
`extensions/ask-person` on 8094 as the Playground's `person.ask` peer, its channel `extensions/agent-question` on
11+
8093 with the inbox on the same PostgreSQL the Python twin used. A `plan-assistant` question through the Node flow to
12+
the Node app, answered from a device on the Node stream with the acknowledgement and the outcome advices following;
13+
two devices on one card (both reached the engine, one resumed, the row answered by the accepted one, a third device
14+
refused by the app); a dead wait on the Node reply page (410, the card expired). Rows the Python app wrote rendered the
15+
same through the Node app.
16+
- **The first-decision rule, in lockstep with mercury-python:** the inbox's decision write was unconditional, so two
17+
devices deciding one card at once left the row to whichever outcome landed last. `MemoryInbox.decided` and
18+
`SQL_DECIDED` now apply a write only when the row is still open or the state is `answered`; `PostgresInbox.decided`
19+
answers with the row as stored when the guard kept it; the handler returns that row with the invoker's outcome and
20+
broadcasts nothing for it. `test/vectors/agent-question-vectors.json` is version 2 (31 cases), copied byte for byte
21+
and re-pinned by SHA-256; the statement test's parameters follow; the ask-person README's audit claim is narrowed to a
22+
refused call's note, as in the Python pack.
23+
- **Gates:** `npm test` - build, then 713 tests, 638 passed, 75 skipped (the gated live tests), 0 failed. CHANGELOG
24+
Unreleased/Fixed 1. Diff scanned for [[conv-compartment-downstream-secrets]]: nothing of a downstream adopter.
25+
26+
## Memory References
27+
28+
- Referenced: agent-question-app-node (edited: driven live, the first-decision rule), ask-person-main-flow-node (the
29+
reply endpoint's behaviour shaped the scenarios), conv-compartment-downstream-secrets (the diff scan)
30+
- Created: (none)
31+
- Superseded: (none)

‎test/agent-question.test.mjs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import * as aq from '../extensions/agent-question/agent-question.mjs';
2020

2121
const VECTORS_FILE = new URL('./vectors/agent-question-vectors.json', import.meta.url);
2222
// the Python twin runs the same file: change it in both packs or in neither
23-
const VECTORS_SHA256 = '65acc6da940618b53e74ed22ddff102df4fb0d4961688cea0aca2c16c3a5f735';
23+
const VECTORS_SHA256 = '4ccf076692bbc87b656cefd874f4474a5141fb8c7882f2833267c1c6ddb93844';
2424
const VECTORS = JSON.parse(readFileSync(VECTORS_FILE, 'utf8'));
2525
const SCHEMA = new URL('../extensions/agent-question/schema/postgres.sql', import.meta.url);
2626

@@ -65,7 +65,7 @@ const postJson = (url, body, headers = {}) => fetch(url, { method: 'POST', heade
6565
test('the vector file is the one the Python twin runs', () => {
6666
assert.equal(createHash('sha256').update(readFileSync(VECTORS_FILE)).digest('hex'), VECTORS_SHA256);
6767
assert.equal(VECTORS.contract, 'agent-question');
68-
assert.equal(VECTORS.version, 1);
68+
assert.equal(VECTORS.version, 2);
6969
});
7070

7171
for (const c of VECTORS.row_cases) {
@@ -456,7 +456,7 @@ test('the postgres inbox runs the statements and maps the records', async () =>
456456
await box.decided(7, [{ id: 'toolu_01A', approved: true }], 'operator-17', 'answered', { status: 200, body: {} }, now);
457457
[sql, params] = pool.log.at(-1);
458458
assert.equal(sql, aq.SQL_DECIDED);
459-
assert.deepEqual(params, ['answered', '[{"id":"toolu_01A","approved":true}]', '{"status":200,"body":{}}', 'operator-17', now, now, 7]);
459+
assert.deepEqual(params, ['answered', '[{"id":"toolu_01A","approved":true}]', '{"status":200,"body":{}}', 'operator-17', now, now, 7, 'answered']);
460460
await box.seen(8, now);
461461
assert.deepEqual(pool.log.at(-1), [aq.SQL_SEEN, [now, 8]]);
462462
await box.expire(now);

‎test/vectors/agent-question-vectors.json‎

Lines changed: 154 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"contract": "agent-question",
3-
"version": 1,
4-
"description": "The agent-question app's shared vectors (RFC-0014, build plan step 3): the pure half of extensions/agent-question, run by mercury-python and mercury-nodejs against the same file. row_cases pin the row a message event becomes (every field a replay carries; the upsert key of a wait; an advice has none) or the exact 400 that refuses the event. outcome_cases pin the state a decision's outcome leaves: the engine accepted it (answered), nothing was waiting (expired), a refusal or an endpoint that did not answer (open). click_cases pin the request the automated click sends - the link's own base plus /ask/reply, the ticket, the decisions and the person as decided_by. transition_cases pin the reference inbox on a scripted sequence - takes, a decision, a seen, an expiry sweep, two replays - as the rows each step leaves and the final rows, ids and change sequence included: a wait upserts on (graph_id, correlation_id, turn), an advice appends, every write bumps the sequence a replay reads from. No HTTP is exchanged by these cases; the routes are tested by each pack in-process.",
3+
"version": 2,
4+
"description": "The agent-question app's shared vectors (RFC-0014, build plan step 3): the pure half of extensions/agent-question, run by mercury-python and mercury-nodejs against the same file. row_cases pin the row a message event becomes (every field a replay carries; the upsert key of a wait; an advice has none) or the exact 400 that refuses the event. outcome_cases pin the state a decision's outcome leaves: the engine accepted it (answered), nothing was waiting (expired), a refusal or an endpoint that did not answer (open). click_cases pin the request the automated click sends - the link's own base plus /ask/reply, the ticket, the decisions and the person as decided_by. transition_cases pin the reference inbox on a scripted sequence - takes, a decision, a seen, an expiry sweep, two replays - as the rows each step leaves and the final rows, ids and change sequence included: a wait upserts on (graph_id, correlation_id, turn), an advice appends, every write bumps the sequence a replay reads from. Version 2 (the live drive of step 5) adds the rule of two devices on one card: a decision's write applies when the row is still open or when the engine accepted the decision (answered), so an acceptance always wins the row and a nothing-waiting that lands after one never overwrites it - the losing device gets the row as it stands. No HTTP is exchanged by these cases; the routes are tested by each pack in-process.",
55
"row_cases": [
66
{
77
"name": "an approval",
@@ -1042,6 +1042,158 @@
10421042
"outcome": null
10431043
}
10441044
]
1045+
},
1046+
{
1047+
"name": "two devices on one card - the first accepted decision stands",
1048+
"steps": [
1049+
{
1050+
"op": "take",
1051+
"event": {
1052+
"kind": "approval",
1053+
"graph_id": "order-assistant",
1054+
"description": "Customer-support assistant: a refund waits for a supervisor",
1055+
"correlation_id": "order-1004-case",
1056+
"turn": 2,
1057+
"node": "assist",
1058+
"summary": "A refund waits for the supervisor's decision",
1059+
"calls": [
1060+
{
1061+
"id": "toolu_01A",
1062+
"name": "issue_refund",
1063+
"route": "demo.refund.issue",
1064+
"wait": true,
1065+
"input": {
1066+
"order_id": "ORD-1004",
1067+
"amount": 240.0,
1068+
"reason": "Shipment lost in transit"
1069+
}
1070+
}
1071+
],
1072+
"ttl": "2d",
1073+
"requested_at": "2026-11-19T10:00:00.000Z",
1074+
"expires_at": "2026-11-21T10:00:00.000Z",
1075+
"ticket": "v1.two.mac",
1076+
"link": "https://ask.example.test/ask/v1.two.mac"
1077+
},
1078+
"now": "2026-11-19T10:00:00.200Z"
1079+
},
1080+
{
1081+
"op": "decided",
1082+
"id": 1,
1083+
"decisions": [
1084+
{
1085+
"id": "toolu_01A",
1086+
"approved": true,
1087+
"note": "from device-2"
1088+
}
1089+
],
1090+
"who": "device-2",
1091+
"status": 410,
1092+
"body": {
1093+
"type": "nothing-waiting",
1094+
"run": "fresh"
1095+
},
1096+
"now": "2026-11-19T10:00:01.000Z",
1097+
"note": "two devices clicked at once; the engine took the other one's decision, so this one got nothing-waiting, and it lands first: the open row turns expired for a moment"
1098+
},
1099+
{
1100+
"op": "decided",
1101+
"id": 1,
1102+
"decisions": [
1103+
{
1104+
"id": "toolu_01A",
1105+
"approved": true,
1106+
"note": "from device-1"
1107+
}
1108+
],
1109+
"who": "device-1",
1110+
"status": 200,
1111+
"body": {
1112+
"type": "answer",
1113+
"turn": 3
1114+
},
1115+
"now": "2026-11-19T10:00:02.000Z",
1116+
"note": "the accepted decision always wins the row"
1117+
},
1118+
{
1119+
"op": "decided",
1120+
"id": 1,
1121+
"decisions": [
1122+
{
1123+
"id": "toolu_01A",
1124+
"approved": false
1125+
}
1126+
],
1127+
"who": "device-3",
1128+
"status": 410,
1129+
"body": {
1130+
"type": "nothing-waiting",
1131+
"run": "fresh"
1132+
},
1133+
"now": "2026-11-19T10:00:03.000Z",
1134+
"note": "a nothing-waiting after the acceptance changes nothing: the row stands as device-1 left it and the sequence does not move"
1135+
},
1136+
{
1137+
"op": "since",
1138+
"seq": 0,
1139+
"limit": 10
1140+
}
1141+
],
1142+
"results": [
1143+
{
1144+
"id": 1,
1145+
"seq": 1,
1146+
"state": "open",
1147+
"ticket": "v1.two.mac"
1148+
},
1149+
{
1150+
"id": 1,
1151+
"seq": 2,
1152+
"state": "expired",
1153+
"answered_by": "device-2"
1154+
},
1155+
{
1156+
"id": 1,
1157+
"seq": 3,
1158+
"state": "answered",
1159+
"answered_by": "device-1"
1160+
},
1161+
{
1162+
"id": 1,
1163+
"seq": 3,
1164+
"state": "answered",
1165+
"answered_by": "device-1"
1166+
},
1167+
{
1168+
"ids": [
1169+
1
1170+
],
1171+
"seqs": [
1172+
3
1173+
]
1174+
}
1175+
],
1176+
"final": [
1177+
{
1178+
"id": 1,
1179+
"seq": 3,
1180+
"kind": "approval",
1181+
"state": "answered",
1182+
"ticket": "v1.two.mac",
1183+
"turn": 2,
1184+
"requested_at": "2026-11-19T10:00:00.000Z",
1185+
"expires_at": "2026-11-21T10:00:00.000Z",
1186+
"updated_at": "2026-11-19T10:00:02.000Z",
1187+
"answered_by": "device-1",
1188+
"outcome": {
1189+
"status": 200,
1190+
"body": {
1191+
"type": "answer",
1192+
"turn": 3
1193+
}
1194+
}
1195+
}
1196+
]
10451197
}
10461198
]
10471199
}

0 commit comments

Comments
 (0)