diff --git a/CHANGELOG.md b/CHANGELOG.md index 46e2178..ebd3ef0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -138,7 +138,17 @@ ### Fixed -1. **A direct `python extensions/mcp-binding/mcp_binding.py` now connects its servers.** The binding attached its boot +1. **The agent-question app keeps the first accepted decision when two devices decide one card at the same moment** + (RFC-0014 step 5, the live drive of 2026-11-19). Both clicks pass the open check and reach the engine, which resumes at + most once; the device that lost got `nothing waiting`, and the row took whichever outcome landed last - so a late 410 + could overwrite the accepted decision and its operator. A decision's write now applies when the row is still open or + when the engine accepted the decision (`state = 'answered'`), in the memory inbox and in `SQL_DECIDED` alike; a + refused write answers the losing device with the row as it stands, so it sees who decided, and nothing is broadcast + for it. The shared vectors are version 2 with the two-devices transition case (`tests/vectors/agent-question-vectors.json`, + 31 cases); a race test with a scripted reply endpoint pins both orderings. **Upgrade note:** none - the schema is + unchanged. + +2. **A direct `python extensions/mcp-binding/mcp_binding.py` now connects its servers.** The binding attached its boot and shutdown hooks to the platform only when loaded by `mercury-serve`; run directly, the Event API started with no server connected and every bound route answered 503. Both entry points attach the hooks now, so a bad server file stops the direct run by name as it stops the served one. diff --git a/extensions/agent-question/README.md b/extensions/agent-question/README.md index 755bd90..f7cf6e3 100644 --- a/extensions/agent-question/README.md +++ b/extensions/agent-question/README.md @@ -21,7 +21,9 @@ application is one of those routes. It is a standalone application on the packag (`expired`); a refused decision or an endpoint that did not answer leaves it `open` with the outcome shown. An advice is `open` until the person marks it `seen`. Open waits past their expiry are marked `expired` by a sweeper and before every replay. The engine stays the authority: a second device on the same card gets `nothing waiting`, - and the row records the first. Rows are never deleted here; `schema/purge.sql` is the installation's. + and the row records the first: an acceptance always wins the row, and a `nothing waiting` that lands after one + never overwrites it (the live drive of 2026-11-19). Rows are never deleted here; `schema/purge.sql` is the + installation's. The Node.js twin (mercury-nodejs `extensions/agent-question`) serves the same route, the same page and the same schema, pinned by the same vector file. diff --git a/extensions/agent-question/agent_question.py b/extensions/agent-question/agent_question.py index 0907101..13e9462 100644 --- a/extensions/agent-question/agent_question.py +++ b/extensions/agent-question/agent_question.py @@ -428,6 +428,8 @@ async def decided(self, row_id: int, decisions: list[dict[str, Any]], who: str, row = self._rows.get(row_id) if row is None: return None + if row.state != "open" and state != "answered": + return row # decided first by another device: a later nothing-waiting never overwrites it row = replace(row, seq=self._bump(), state=state, decisions=decisions, outcome=outcome, answered_by=who, answered_at=now, updated_at=now) self._rows[row_id] = row @@ -484,7 +486,7 @@ async def counts(self) -> dict[str, int]: SQL_DECIDED = ( "UPDATE question_inbox SET seq = nextval('question_inbox_seq'), state = %s, decisions = %s::jsonb, " "outcome = %s::jsonb, answered_by = %s, answered_at = %s::timestamptz, updated_at = %s::timestamptz " - f"WHERE id = %s RETURNING {COLUMNS}" + f"WHERE id = %s AND (state = 'open' OR %s::text = 'answered') RETURNING {COLUMNS}" ) SQL_SEEN = ( "UPDATE question_inbox SET seq = nextval('question_inbox_seq'), state = 'seen', updated_at = %s::timestamptz " @@ -583,7 +585,9 @@ async def since(self, seq: int, limit: int) -> list[Row]: async def decided(self, row_id: int, decisions: list[dict[str, Any]], who: str, state: str, outcome: dict[str, Any], now: str) -> Row | None: - return await self._one(SQL_DECIDED, (state, _compact(decisions), _compact(outcome), who, now, now, row_id)) + row = await self._one(SQL_DECIDED, (state, _compact(decisions), _compact(outcome), who, now, now, row_id, state)) + # no row: the guard kept a row another device decided first (or the row is gone) - answer with it as stored + return row if row is not None else await self.get(row_id) async def seen(self, row_id: int, now: str) -> Row | None: return await self._one(SQL_SEEN, (now, row_id)) @@ -910,7 +914,8 @@ async def _forward(url: str, body: dict[str, Any], settings: Settings) -> tuple[ async def decide(request: web.Request) -> web.Response: """``POST /questions/{id}/decide`` with ``{decisions, decided_by?}``: the click, automated - the decisions go to the link's reply endpoint under this app's bearer, the outcome lands on the row, every device is - told, and the invoker gets the row with the outcome.""" + told, and the invoker gets the row with the outcome. The first accepted decision stands: an acceptance always + wins the row, and a nothing-waiting that lands after one never overwrites it.""" settings = current_settings() try: body = await request.json() @@ -952,6 +957,12 @@ async def decide(request: web.Request) -> web.Response: updated = await inbox().decided(row_id, [dict(d) for d in decisions], identity.who, state, outcome, now) if updated is None: return _error(404, f"row {row_id} is not in the inbox") + if updated.answered_by != identity.who or updated.answered_at != now: + # two devices on one card: the other one's decision was accepted while this one was in flight, so the engine + # answered nothing-waiting here and the row stands as the first decision left it - this device sees who + log.info("Row %d: %s's decision met a row %s by %s - the row stands", row_id, identity.who, updated.state, + updated.answered_by) + return web.json_response({"row": updated.public(), "outcome": outcome}) _wakeups.publish([updated]) log.info("Row %d decided by %s via %s - %s -> %s", row_id, identity.who, identity.via, outcome["status"], state) return web.json_response({"row": updated.public(), "outcome": outcome}) diff --git a/extensions/ask-person/README.md b/extensions/ask-person/README.md index 41b6e44..34e2d89 100644 --- a/extensions/ask-person/README.md +++ b/extensions/ask-person/README.md @@ -95,7 +95,8 @@ engine; a malformed one or another version is a 400, one no key signs a 403. renders as expired; an engine that has moved on shows its reply. - `POST /ask/reply` takes `{"ticket": "...", "decisions": [...], "decided_by": "..."}`: `{id, approved, note?}` for a tool call, `{id, answer}` for a question - the shapes the loop accepts. Each tool call's note is prefixed - `[ at