From 93a61c68a11202a0d9f99dd24e48cc7b424d6f2e Mon Sep 17 00:00:00 2001 From: chaodu-agent <274062505+chaodu-agent@users.noreply.github.com> Date: Sun, 22 Nov 2026 09:20:33 -0500 Subject: [PATCH 1/8] docs: design for async poll on the Muse write route Checked: 939 words (limit about 1000); grep for 32+ hex, tskey, 100.x addresses, ts.net, bot tokens found nothing. --- docs/design/muse-async-poll.md | 108 +++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 docs/design/muse-async-poll.md diff --git a/docs/design/muse-async-poll.md b/docs/design/muse-async-poll.md new file mode 100644 index 0000000..707c9aa --- /dev/null +++ b/docs/design/muse-async-poll.md @@ -0,0 +1,108 @@ +# Muse async poll: replacing the held connection with a poll token + +Status: design only, no code. Terms (hold, start claim, `forward_started`, the audit records) are those of +[muse-gated-passthrough.md](muse-gated-passthrough.md), called "the gated design" below. The key words MUST +and MUST NOT are normative. Every token, hash, chat id and address in this file is a placeholder. + +## 1. Scope + +Only the Muse route `POST /channels/*/messages` (the held route). Every other route, every allowed read and +the blocking mode are unchanged. Blocking stays selectable by configuration; async is chosen per route. + +## 2. POST: admit and return + +The proxy MUST, in this order: admit the request into the in-memory pending table (section 5), write the +`held` audit record, send the Telegram notification (the existing PR #41 path), and return `202` with a +JSON body `{"request_id": "", "poll_token": ""}`. The token is random, at least 128 bits, stored +only as a hash and compared in constant time. The response MUST NOT echo the request body or +`Authorization`. The request still passes every check of gated design 3.1 to 3.4 before admission. + +## 3. GET /poll/?wait=N + +The call waits up to N seconds, with N capped at 25, and returns one of: + +| Status | Meaning | +|---|---| +| `pending` | No decision yet. | +| `approved_and_sent` | Discord's status and a bounded body (cap set in configuration). | +| `denied`, `expired` | Owner denied, or `async_hold_secs` passed unapproved. Nothing was sent. | +| `send_unknown` | Section 7. | +| `send_failed` | Section 7. | +| `dropped` | Section 8. | + +The token is read-only. It MUST NOT approve, deny or change any state. An unknown, expired-window or +malformed token returns one uniform `404` with an identical body and timing class. Polls are rate limited +per token. + +## 4. State kept + +Pending entries, plus one fixed 60 s final window. During the window the final status stays readable and +the Idempotency-Key stays recorded. Afterwards the token returns the uniform 404 and the key is forgotten. +There is no retained-entry cap beyond this window and no per-identity retained count. + +## 5. Idempotency + +The Idempotency-Key is bound to the payload hash (method, route, body digest). Admission is atomic under +the table lock: + +- Same key, same payload, while pending or inside the 60 s final window: return the same `request_id` and + token. A retry MUST NOT become a newly admitted send. +- Same key, different payload: `409`. +- Same key after the window: a new request. + +## 6. Approval + +The owner decides on Telegram only (unchanged). Approve runs the existing start claim before any upstream +byte: policy, token-identity expiry and hash, and that the hold is still live (gated design 3.9, 4.5). +`forward_started` MUST be durable before the upstream connect. The audit records `held`, `approved`, +`forward_started`, `completed`, `expired`, `denied` keep their current shape and never carry a token, +`Authorization`, hash or body. + +## 7. Past forward_started + +A request that has passed `forward_started` MUST NOT end as `dropped`. If the process cannot confirm +Discord's result (connection reset, timeout), the status is `send_unknown` with the error class. The proxy +MUST NOT retry blindly. Crash recovery cannot apply, because memory is lost. A failure before any request +byte left is `send_failed`. Muse treats `send_unknown` as possibly delivered and submits a NEW request with +a NEW Idempotency-Key only after the owner decides, never as a blind retry. + +## 8. Restart, reload and expiry + +Pending entries are lost on restart. A token whose entry was lost returns the uniform 404. A `held` record +in the audit with no completion is the expected trace of such a loss, and needs no extra record. A policy +reload that removes the hold before the start claim ends the entry as `dropped` (gated design 3.9, +`dropped_on_reload`); this is possible only before `forward_started`. + +## 9. Limits + +A configured maximum of pending entries (a full table refuses with 503 `table_full`) and `async_hold_secs` +(default 120). A pending entry not approved by then ends as `expired`, nothing sent. Poll rate limit per +token (section 3). + +## 10. What this replaces + +This replaces requirement 6 and section 5 of the gated design ("the connection is the state") for this +route only. No longer guaranteed: a Muse disconnect no longer cancels the hold, so `abandoned` and +`abandoned_after_approve` do not occur on this route, and an approved request is sent although Muse has +stopped polling. Still holds: fail-closed, nothing is forwarded without an approval in the same process +lifetime, at most one forward per admitted request, and exactly-once is still not claimed. + +## 11. Owner confirmation: departures from the blocking design + +1. No cancel on Muse disconnect; an approved request is sent even if Muse never polls again. +2. Muse needs a poll loop and an Idempotency-Key; blocking clients are unaffected. +3. Final results are readable for 60 s only, then the 404. +4. `send_unknown` is a new outcome that needs the owner's decision. +5. A restart loses every pending request silently from Muse's view (404). +6. `async_hold_secs` (120) and the pending maximum are new limits. + +## 12. Tests + +1. Idempotency: same key and payload returns the same id and token while pending and inside 60 s, a new + request after, and 409 for a different payload. +2. Poll after each final status (`approved_and_sent`, `denied`, `expired`, `dropped`) inside and after the + window. +3. Unknown, malformed and expired tokens return the identical uniform 404. +4. `send_unknown` (reset after `forward_started`) and `send_failed` (before any byte); neither retries. +5. A restart loses pending entries; the old token returns 404 and the audit shows `held` only. +6. Audit shape: the six records are unchanged and no output holds a token, `Authorization`, hash or body. From b6d9559c828367733de92d909eab32732273de34 Mon Sep 17 00:00:00 2001 From: chaodu-agent <274062505+chaodu-agent@users.noreply.github.com> Date: Sun, 22 Nov 2026 09:21:20 -0500 Subject: [PATCH 2/8] docs: require a valid Idempotency-Key (400 otherwise) in async poll design Checked: word count 966; grep for hex 32+, tskey, tailnet addresses, ts.net: no matches. Fixes review MEDIUM only. --- docs/design/muse-async-poll.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/design/muse-async-poll.md b/docs/design/muse-async-poll.md index 707c9aa..9569ed3 100644 --- a/docs/design/muse-async-poll.md +++ b/docs/design/muse-async-poll.md @@ -42,8 +42,9 @@ There is no retained-entry cap beyond this window and no per-identity retained c ## 5. Idempotency -The Idempotency-Key is bound to the payload hash (method, route, body digest). Admission is atomic under -the table lock: +The Idempotency-Key is required. A POST with a missing, empty, over-long (more than 64 characters) or +malformed key (anything outside `[A-Za-z0-9_-]`) is refused with `400` before admission. The key is bound +to the payload hash (method, route, body digest). Admission is atomic under the table lock: - Same key, same payload, while pending or inside the 60 s final window: return the same `request_id` and token. A retry MUST NOT become a newly admitted send. From 9a95dc3a1f503bb4b11d7f85fec95d167508f064 Mon Sep 17 00:00:00 2001 From: chaodu-agent <274062505+chaodu-agent@users.noreply.github.com> Date: Sun, 22 Nov 2026 10:44:19 -0500 Subject: [PATCH 3/8] docs: fix 6 MEDIUM review findings in muse-async-poll design Checked: re-read each edit against review.md findings 1-6 and gated 3.4 step 5/6 and 4.5; no code, tests not applicable. Also applied LOW 7, 8, 9 and NIT 10. --- docs/design/muse-async-poll.md | 57 ++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/docs/design/muse-async-poll.md b/docs/design/muse-async-poll.md index 9569ed3..eb676d0 100644 --- a/docs/design/muse-async-poll.md +++ b/docs/design/muse-async-poll.md @@ -11,24 +11,29 @@ the blocking mode are unchanged. Blocking stays selectable by configuration; asy ## 2. POST: admit and return -The proxy MUST, in this order: admit the request into the in-memory pending table (section 5), write the -`held` audit record, send the Telegram notification (the existing PR #41 path), and return `202` with a -JSON body `{"request_id": "", "poll_token": ""}`. The token is random, at least 128 bits, stored +The proxy MUST, in this order: write the `held` audit record first (if that write fails, answer 503 +`audit_unavailable` before any entry or key record exists and before any notification, as gated design 3.4 +step 5); admit the request into the in-memory pending table (section 5); send the Telegram notification +(the existing PR #41 path); and return `202` with a JSON body `{"request_id": "", "poll_token": ""}`. The token is random, at least 128 bits, stored only as a hash and compared in constant time. The response MUST NOT echo the request body or -`Authorization`. The request still passes every check of gated design 3.1 to 3.4 before admission. +`Authorization`. The request still passes every check of gated design 3.1 to 3.3 and 3.4 steps 1 to 3 before admission. +Admission includes the existing cooldown, `max_held_per_identity` and `max_held_total` checks and their +refusals, unchanged. The Idempotency-Key replaces the fingerprint dedupe and `superseded` for this route. ## 3. GET /poll/?wait=N -The call waits up to N seconds, with N capped at 25, and returns one of: +The call waits up to N seconds (N capped at 25) and answers as follows: -| Status | Meaning | -|---|---| -| `pending` | No decision yet. | -| `approved_and_sent` | Discord's status and a bounded body (cap set in configuration). | -| `denied`, `expired` | Owner denied, or `async_hold_secs` passed unapproved. Nothing was sent. | -| `send_unknown` | Section 7. | -| `send_failed` | Section 7. | -| `dropped` | Section 8. | +| Status | HTTP | JSON fields | +|---|---|---| +| `pending` | 200 | `status`, `request_id` | +| `approved_and_sent` | 200 | `status`, `request_id`, `discord_status`, bounded `body`, `body_truncated` (bool; cap set in configuration) | +| `denied`, `expired`, `dropped` | 200 | `status`, `request_id`; nothing was sent (`expired`: `async_hold_secs` passed unapproved; `dropped`: section 8) | +| `send_unknown`, `send_failed` | 200 | `status`, `request_id`, `error_class` (`reset`, `timeout` or `write_failed`; section 7) | +| unknown, malformed or expired-window token | 404 | uniform body | +| bad `wait` | 400 | uniform error body; does not reveal whether the token exists | + +`wait` missing means 0; values above 25 are capped to 25; a non-numeric or negative value is 400. The token is read-only. It MUST NOT approve, deny or change any state. An unknown, expired-window or malformed token returns one uniform `404` with an identical body and timing class. Polls are rate limited @@ -36,7 +41,7 @@ per token. ## 4. State kept -Pending entries, plus one fixed 60 s final window. During the window the final status stays readable and +Pending entries, plus one fixed 60 s final window that starts when the entry reaches a final status. During the window the final status stays readable and the Idempotency-Key stays recorded. Afterwards the token returns the uniform 404 and the key is forgotten. There is no retained-entry cap beyond this window and no per-identity retained count. @@ -44,12 +49,13 @@ There is no retained-entry cap beyond this window and no per-identity retained c The Idempotency-Key is required. A POST with a missing, empty, over-long (more than 64 characters) or malformed key (anything outside `[A-Za-z0-9_-]`) is refused with `400` before admission. The key is bound -to the payload hash (method, route, body digest). Admission is atomic under the table lock: +to the payload hash over the method, the full route-relative path (including the channel id) and the +body digest, so the same key and body on a different channel gives `409`. Admission is atomic under the table lock: - Same key, same payload, while pending or inside the 60 s final window: return the same `request_id` and token. A retry MUST NOT become a newly admitted send. - Same key, different payload: `409`. -- Same key after the window: a new request. +- Same key after the window: a new request (this can be a second send of an `approved_and_sent` request). ## 6. Approval @@ -63,16 +69,18 @@ byte: policy, token-identity expiry and hash, and that the hold is still live (g A request that has passed `forward_started` MUST NOT end as `dropped`. If the process cannot confirm Discord's result (connection reset, timeout), the status is `send_unknown` with the error class. The proxy -MUST NOT retry blindly. Crash recovery cannot apply, because memory is lost. A failure before any request +MUST NOT retry blindly. A failure before any request byte left is `send_failed`. Muse treats `send_unknown` as possibly delivered and submits a NEW request with a NEW Idempotency-Key only after the owner decides, never as a blind retry. -## 8. Restart, reload and expiry +## 8. Restart, reload, expiry Pending entries are lost on restart. A token whose entry was lost returns the uniform 404. A `held` record -in the audit with no completion is the expected trace of such a loss, and needs no extra record. A policy -reload that removes the hold before the start claim ends the entry as `dropped` (gated design 3.9, -`dropped_on_reload`); this is possible only before `forward_started`. +in the audit with no completion is the expected trace of such a loss, and needs no extra record. Every +start-claim failure (policy reload, token-identity expiry or hash change) ends the entry as `dropped` +with the existing audit code `dropped_on_reload`, before any byte is sent (gated design 3.9, 4.5); this is +possible only before `forward_started`. Approve and expiry race on one mutex and the first transition wins +(gated design 3.4 step 6). ## 9. Limits @@ -82,8 +90,9 @@ token (section 3). ## 10. What this replaces -This replaces requirement 6 and section 5 of the gated design ("the connection is the state") for this -route only. No longer guaranteed: a Muse disconnect no longer cancels the hold, so `abandoned` and +This replaces requirement 6 and section 5 of the gated design ("the connection is the state"), requirement +4 (Discord's response returned verbatim with headers), requirement 5 (`X-AP-Decision` and 423/410 on this +route after admission) and 3.4 step 6 (the entry state machine with `Abandoned`) for this route only. No longer guaranteed: a Muse disconnect no longer cancels the hold, so `abandoned` and `abandoned_after_approve` do not occur on this route, and an approved request is sent although Muse has stopped polling. Still holds: fail-closed, nothing is forwarded without an approval in the same process lifetime, at most one forward per admitted request, and exactly-once is still not claimed. @@ -96,6 +105,8 @@ lifetime, at most one forward per admitted request, and exactly-once is still no 4. `send_unknown` is a new outcome that needs the owner's decision. 5. A restart loses every pending request silently from Muse's view (404). 6. `async_hold_secs` (120) and the pending maximum are new limits. +7. The response is no longer verbatim: status and a bounded body only, no Discord headers, and no + `X-AP-Decision` or 423/410 after admission (gated requirements 4 and 5, 3.4 step 6). ## 12. Tests From fd8682e1e18ba549efe5c037aa835d37bad6758b Mon Sep 17 00:00:00 2001 From: chaodu-agent <274062505+chaodu-agent@users.noreply.github.com> Date: Sun, 22 Nov 2026 10:45:09 -0500 Subject: [PATCH 4/8] docs: address design review (admission order, identity-scoped idempotency, 429 and send outcome audit mapping, replaced 3.4 steps 6-9) --- docs/design/muse-async-poll.md | 42 ++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/docs/design/muse-async-poll.md b/docs/design/muse-async-poll.md index eb676d0..4c63611 100644 --- a/docs/design/muse-async-poll.md +++ b/docs/design/muse-async-poll.md @@ -11,14 +11,19 @@ the blocking mode are unchanged. Blocking stays selectable by configuration; asy ## 2. POST: admit and return -The proxy MUST, in this order: write the `held` audit record first (if that write fails, answer 503 -`audit_unavailable` before any entry or key record exists and before any notification, as gated design 3.4 -step 5); admit the request into the in-memory pending table (section 5); send the Telegram notification -(the existing PR #41 path); and return `202` with a JSON body `{"request_id": "", "poll_token": ""}`. The token is random, at least 128 bits, stored +The proxy MUST, in this order: + +1. Validate the Idempotency-Key (section 5). +2. Under the table lock: look up (identity, key) (same key and payload returns the existing entry, so a retry + at a cap still gets the same token), then apply the existing cooldown, `max_held_per_identity` and + `max_held_total` checks and their refusals, unchanged. +3. Write the `held` audit record. If that write fails, answer 503 `audit_unavailable`; no entry or key record exists and nothing is notified (gated design 3.4 step 5). +4. Insert the entry and the key record (still under the same lock), send the Telegram notification (the existing PR #41 path), and return `202` with a JSON body `{"request_id": "", "poll_token": ""}`. + +A refused or repeated request therefore writes no `held` record. The token is random, at least 128 bits, stored only as a hash and compared in constant time. The response MUST NOT echo the request body or `Authorization`. The request still passes every check of gated design 3.1 to 3.3 and 3.4 steps 1 to 3 before admission. -Admission includes the existing cooldown, `max_held_per_identity` and `max_held_total` checks and their -refusals, unchanged. The Idempotency-Key replaces the fingerprint dedupe and `superseded` for this route. +The Idempotency-Key replaces the fingerprint dedupe and `superseded` for this route. ## 3. GET /poll/?wait=N @@ -31,13 +36,14 @@ The call waits up to N seconds (N capped at 25) and answers as follows: | `denied`, `expired`, `dropped` | 200 | `status`, `request_id`; nothing was sent (`expired`: `async_hold_secs` passed unapproved; `dropped`: section 8) | | `send_unknown`, `send_failed` | 200 | `status`, `request_id`, `error_class` (`reset`, `timeout` or `write_failed`; section 7) | | unknown, malformed or expired-window token | 404 | uniform body | +| poll rate exceeded | 429 | uniform error body, `Retry-After` header | | bad `wait` | 400 | uniform error body; does not reveal whether the token exists | -`wait` missing means 0; values above 25 are capped to 25; a non-numeric or negative value is 400. +`wait` missing means 0; the per-token poll limit is configured (default 2 per second, burst 5); values above 25 are capped to 25; a non-numeric or negative value is 400. The token is read-only. It MUST NOT approve, deny or change any state. An unknown, expired-window or malformed token returns one uniform `404` with an identical body and timing class. Polls are rate limited -per token. +per token (429 above). ## 4. State kept @@ -47,10 +53,12 @@ There is no retained-entry cap beyond this window and no per-identity retained c ## 5. Idempotency -The Idempotency-Key is required. A POST with a missing, empty, over-long (more than 64 characters) or +The Idempotency-Key is required and is scoped to the caller identity: the table is keyed on (identity, key), so +another identity never receives this identity's token. A POST with a missing, empty, over-long (more than 64 characters) or malformed key (anything outside `[A-Za-z0-9_-]`) is refused with `400` before admission. The key is bound -to the payload hash over the method, the full route-relative path (including the channel id) and the -body digest, so the same key and body on a different channel gives `409`. Admission is atomic under the table lock: +to the payload hash over the method, the raw route-relative path (including the channel id), the raw query +and the body digest. Forwarded headers and the credential are excluded: they are rewritten by the proxy and +the identity already scopes the key. So the same key and body on a different channel gives `409`. Admission is atomic under the table lock: - Same key, same payload, while pending or inside the 60 s final window: return the same `request_id` and token. A retry MUST NOT become a newly admitted send. @@ -70,7 +78,10 @@ byte: policy, token-identity expiry and hash, and that the hold is still live (g A request that has passed `forward_started` MUST NOT end as `dropped`. If the process cannot confirm Discord's result (connection reset, timeout), the status is `send_unknown` with the error class. The proxy MUST NOT retry blindly. A failure before any request -byte left is `send_failed`. Muse treats `send_unknown` as possibly delivered and submits a NEW request with +byte left is `send_failed`. Audit mapping: an unconfirmed result is `completed` with code `send_unknown` +(poll status `send_unknown`); a connect failure (`connect_failed`) or a failed `forward_started` write +(`audit_unavailable`, nothing sent) is `completed` with that code (poll status `send_failed`). A +`forward_started` with no `completed` is a crash trace only. Muse treats `send_unknown` as possibly delivered and submits a NEW request with a NEW Idempotency-Key only after the owner decides, never as a blind retry. ## 8. Restart, reload, expiry @@ -80,7 +91,7 @@ in the audit with no completion is the expected trace of such a loss, and needs start-claim failure (policy reload, token-identity expiry or hash change) ends the entry as `dropped` with the existing audit code `dropped_on_reload`, before any byte is sent (gated design 3.9, 4.5); this is possible only before `forward_started`. Approve and expiry race on one mutex and the first transition wins -(gated design 3.4 step 6). +(retained from gated design 3.4 step 6). ## 9. Limits @@ -92,7 +103,8 @@ token (section 3). This replaces requirement 6 and section 5 of the gated design ("the connection is the state"), requirement 4 (Discord's response returned verbatim with headers), requirement 5 (`X-AP-Decision` and 423/410 on this -route after admission) and 3.4 step 6 (the entry state machine with `Abandoned`) for this route only. No longer guaranteed: a Muse disconnect no longer cancels the hold, so `abandoned` and +route after admission) and, for this route only, 3.4 steps 7 to 9 (disconnect detection, `approved_but_abandoned` and its milestones, +the verbatim response) and the `Abandoned` and `Superseded` parts of step 6. First-transition-wins of step 6 is retained. No longer guaranteed: a Muse disconnect no longer cancels the hold, so `abandoned` and `abandoned_after_approve` do not occur on this route, and an approved request is sent although Muse has stopped polling. Still holds: fail-closed, nothing is forwarded without an approval in the same process lifetime, at most one forward per admitted request, and exactly-once is still not claimed. @@ -106,7 +118,7 @@ lifetime, at most one forward per admitted request, and exactly-once is still no 5. A restart loses every pending request silently from Muse's view (404). 6. `async_hold_secs` (120) and the pending maximum are new limits. 7. The response is no longer verbatim: status and a bounded body only, no Discord headers, and no - `X-AP-Decision` or 423/410 after admission (gated requirements 4 and 5, 3.4 step 6). + `X-AP-Decision` or 423/410 after admission (gated requirements 4 and 5, 3.4 steps 6 to 9: only `Abandoned`, `Superseded`, disconnect detection and `approved_but_abandoned` are dropped). ## 12. Tests From 6cbf17efa2f07871abd7607637e90d0eea9d5a65 Mon Sep 17 00:00:00 2001 From: chaodu-agent <274062505+chaodu-agent@users.noreply.github.com> Date: Sun, 22 Nov 2026 10:48:05 -0500 Subject: [PATCH 5/8] docs: replace async-poll design with one-page minimal version (87 lines) --- docs/design/muse-async-poll.md | 191 ++++++++++++--------------------- 1 file changed, 70 insertions(+), 121 deletions(-) diff --git a/docs/design/muse-async-poll.md b/docs/design/muse-async-poll.md index 4c63611..5665d22 100644 --- a/docs/design/muse-async-poll.md +++ b/docs/design/muse-async-poll.md @@ -1,132 +1,81 @@ -# Muse async poll: replacing the held connection with a poll token +# Muse async poll for held writes -Status: design only, no code. Terms (hold, start claim, `forward_started`, the audit records) are those of -[muse-gated-passthrough.md](muse-gated-passthrough.md), called "the gated design" below. The key words MUST -and MUST NOT are normative. Every token, hash, chat id and address in this file is a placeholder. +Status: draft, awaiting owner confirmation. Terms (held, start claim, `forward_started`, +`dropped_on_reload`) are as in `muse-gated-passthrough.md` 3.4 and 4.5. ## 1. Scope -Only the Muse route `POST /channels/*/messages` (the held route). Every other route, every allowed read and -the blocking mode are unchanged. Blocking stays selectable by configuration; async is chosen per route. +Only the Muse route `POST /channels/*/messages` (held). Blocking mode stays selectable by config. +Everything else is unchanged. -## 2. POST: admit and return +## 2. POST -The proxy MUST, in this order: - -1. Validate the Idempotency-Key (section 5). -2. Under the table lock: look up (identity, key) (same key and payload returns the existing entry, so a retry - at a cap still gets the same token), then apply the existing cooldown, `max_held_per_identity` and - `max_held_total` checks and their refusals, unchanged. -3. Write the `held` audit record. If that write fails, answer 503 `audit_unavailable`; no entry or key record exists and nothing is notified (gated design 3.4 step 5). -4. Insert the entry and the key record (still under the same lock), send the Telegram notification (the existing PR #41 path), and return `202` with a JSON body `{"request_id": "", "poll_token": ""}`. - -A refused or repeated request therefore writes no `held` record. The token is random, at least 128 bits, stored -only as a hash and compared in constant time. The response MUST NOT echo the request body or -`Authorization`. The request still passes every check of gated design 3.1 to 3.3 and 3.4 steps 1 to 3 before admission. -The Idempotency-Key replaces the fingerprint dedupe and `superseded` for this route. +1. Write the `held` audit record first. If the write fails, refuse 503 `audit_unavailable` before any + entry exists and before any notification. +2. Run the existing admission checks (cooldown, `max_held_per_identity`, `max_held_total`) with their + existing refusals. +3. Insert the entry in the in-memory pending table, send the existing Telegram notification. +4. Return 202 with `request_id` and a random poll token (at least 128 bits, stored only as a hash, + compared in constant time). The body and `Authorization` are never echoed. ## 3. GET /poll/?wait=N -The call waits up to N seconds (N capped at 25) and answers as follows: +`wait` missing means 0, capped at 25 s. Non-numeric or negative is 400, without revealing whether the +token exists. Response is `pending` or a final status: -| Status | HTTP | JSON fields | +| status | HTTP | fields | |---|---|---| -| `pending` | 200 | `status`, `request_id` | -| `approved_and_sent` | 200 | `status`, `request_id`, `discord_status`, bounded `body`, `body_truncated` (bool; cap set in configuration) | -| `denied`, `expired`, `dropped` | 200 | `status`, `request_id`; nothing was sent (`expired`: `async_hold_secs` passed unapproved; `dropped`: section 8) | -| `send_unknown`, `send_failed` | 200 | `status`, `request_id`, `error_class` (`reset`, `timeout` or `write_failed`; section 7) | -| unknown, malformed or expired-window token | 404 | uniform body | -| poll rate exceeded | 429 | uniform error body, `Retry-After` header | -| bad `wait` | 400 | uniform error body; does not reveal whether the token exists | - -`wait` missing means 0; the per-token poll limit is configured (default 2 per second, burst 5); values above 25 are capped to 25; a non-numeric or negative value is 400. - -The token is read-only. It MUST NOT approve, deny or change any state. An unknown, expired-window or -malformed token returns one uniform `404` with an identical body and timing class. Polls are rate limited -per token (429 above). - -## 4. State kept - -Pending entries, plus one fixed 60 s final window that starts when the entry reaches a final status. During the window the final status stays readable and -the Idempotency-Key stays recorded. Afterwards the token returns the uniform 404 and the key is forgotten. -There is no retained-entry cap beyond this window and no per-identity retained count. - -## 5. Idempotency - -The Idempotency-Key is required and is scoped to the caller identity: the table is keyed on (identity, key), so -another identity never receives this identity's token. A POST with a missing, empty, over-long (more than 64 characters) or -malformed key (anything outside `[A-Za-z0-9_-]`) is refused with `400` before admission. The key is bound -to the payload hash over the method, the raw route-relative path (including the channel id), the raw query -and the body digest. Forwarded headers and the credential are excluded: they are rewritten by the proxy and -the identity already scopes the key. So the same key and body on a different channel gives `409`. Admission is atomic under the table lock: - -- Same key, same payload, while pending or inside the 60 s final window: return the same `request_id` and - token. A retry MUST NOT become a newly admitted send. -- Same key, different payload: `409`. -- Same key after the window: a new request (this can be a second send of an `approved_and_sent` request). - -## 6. Approval - -The owner decides on Telegram only (unchanged). Approve runs the existing start claim before any upstream -byte: policy, token-identity expiry and hash, and that the hold is still live (gated design 3.9, 4.5). -`forward_started` MUST be durable before the upstream connect. The audit records `held`, `approved`, -`forward_started`, `completed`, `expired`, `denied` keep their current shape and never carry a token, -`Authorization`, hash or body. - -## 7. Past forward_started - -A request that has passed `forward_started` MUST NOT end as `dropped`. If the process cannot confirm -Discord's result (connection reset, timeout), the status is `send_unknown` with the error class. The proxy -MUST NOT retry blindly. A failure before any request -byte left is `send_failed`. Audit mapping: an unconfirmed result is `completed` with code `send_unknown` -(poll status `send_unknown`); a connect failure (`connect_failed`) or a failed `forward_started` write -(`audit_unavailable`, nothing sent) is `completed` with that code (poll status `send_failed`). A -`forward_started` with no `completed` is a crash trace only. Muse treats `send_unknown` as possibly delivered and submits a NEW request with -a NEW Idempotency-Key only after the owner decides, never as a blind retry. - -## 8. Restart, reload, expiry - -Pending entries are lost on restart. A token whose entry was lost returns the uniform 404. A `held` record -in the audit with no completion is the expected trace of such a loss, and needs no extra record. Every -start-claim failure (policy reload, token-identity expiry or hash change) ends the entry as `dropped` -with the existing audit code `dropped_on_reload`, before any byte is sent (gated design 3.9, 4.5); this is -possible only before `forward_started`. Approve and expiry race on one mutex and the first transition wins -(retained from gated design 3.4 step 6). - -## 9. Limits - -A configured maximum of pending entries (a full table refuses with 503 `table_full`) and `async_hold_secs` -(default 120). A pending entry not approved by then ends as `expired`, nothing sent. Poll rate limit per -token (section 3). - -## 10. What this replaces - -This replaces requirement 6 and section 5 of the gated design ("the connection is the state"), requirement -4 (Discord's response returned verbatim with headers), requirement 5 (`X-AP-Decision` and 423/410 on this -route after admission) and, for this route only, 3.4 steps 7 to 9 (disconnect detection, `approved_but_abandoned` and its milestones, -the verbatim response) and the `Abandoned` and `Superseded` parts of step 6. First-transition-wins of step 6 is retained. No longer guaranteed: a Muse disconnect no longer cancels the hold, so `abandoned` and -`abandoned_after_approve` do not occur on this route, and an approved request is sent although Muse has -stopped polling. Still holds: fail-closed, nothing is forwarded without an approval in the same process -lifetime, at most one forward per admitted request, and exactly-once is still not claimed. - -## 11. Owner confirmation: departures from the blocking design - -1. No cancel on Muse disconnect; an approved request is sent even if Muse never polls again. -2. Muse needs a poll loop and an Idempotency-Key; blocking clients are unaffected. -3. Final results are readable for 60 s only, then the 404. -4. `send_unknown` is a new outcome that needs the owner's decision. -5. A restart loses every pending request silently from Muse's view (404). -6. `async_hold_secs` (120) and the pending maximum are new limits. -7. The response is no longer verbatim: status and a bounded body only, no Discord headers, and no - `X-AP-Decision` or 423/410 after admission (gated requirements 4 and 5, 3.4 steps 6 to 9: only `Abandoned`, `Superseded`, disconnect detection and `approved_but_abandoned` are dropped). - -## 12. Tests - -1. Idempotency: same key and payload returns the same id and token while pending and inside 60 s, a new - request after, and 409 for a different payload. -2. Poll after each final status (`approved_and_sent`, `denied`, `expired`, `dropped`) inside and after the - window. -3. Unknown, malformed and expired tokens return the identical uniform 404. -4. `send_unknown` (reset after `forward_started`) and `send_failed` (before any byte); neither retries. -5. A restart loses pending entries; the old token returns 404 and the audit shows `held` only. -6. Audit shape: the six records are unchanged and no output holds a token, `Authorization`, hash or body. +| `approved_and_sent` | 200 | `status`, `request_id`, `discord_status`, bounded `body`, `truncated` | +| `denied`, `expired`, `dropped` | 200 | `status`, `request_id` | +| `send_failed` | 200 | `status`, `request_id` | +| `send_unknown` | 200 | `status`, `request_id`, `error_class` (`reset`, `timeout`, `write_failed`) | + +The token is read-only. It can never approve, deny or change state. + +## 4. Retention + +A final status stays readable for one fixed 60 s window. After that, and for any unknown token, the +response is a uniform 404. No retained-entry cap beyond this window. + +## 5. Retries + +No `Idempotency-Key` and no idempotency store. Muse never retries a POST blindly. After a lost or +unknown POST response it submits a NEW request. This is safe because every message needs its own +Telegram approval: a duplicate is a separate approval the owner can deny, so there is no unapproved +double send. + +## 6. States and races + +- A request that has passed `forward_started` never ends as `dropped`. An unconfirmed result is + `send_unknown` with an error class. +- `forward_started` is durable before the upstream connect. +- A start-claim failure before any byte maps to `dropped`, audit code `dropped_on_reload`. +- Approve versus expiry race on one mutex. The first transition wins. + +## 7. Restart + +Restart drops all pending entries. A poll then returns the uniform 404. An audit `held` record with no +completion is the expected trace. + +## 8. Approval and audit + +Approval stays on Telegram only, with the existing start-claim checks. Audit records keep their current +shape and never carry a token, `Authorization`, hash or body. `async_hold_secs` (default 120) bounds a +pending entry. Unapproved entries end as `expired`. + +## 9. Departures + +- Connection-as-state is replaced for this route. +- No cancel on Muse disconnect. +- Discord response headers are not returned verbatim. +- `X-AP-Decision` is not returned after admission. + +Owner confirmation is needed for these four departures before implementation. + +## 10. Tests + +- Poll after each final status. +- Uniform 404 after the window and for unknown tokens. +- `send_unknown` path. +- Restart loses pending entries. +- Audit shape, and no token in audit. From f090784e36d8f389854811f00cce31806e7b2636 Mon Sep 17 00:00:00 2001 From: chaodu-agent <274062505+chaodu-agent@users.noreply.github.com> Date: Sun, 22 Nov 2026 10:48:20 -0500 Subject: [PATCH 6/8] docs: trim async-poll design to 60 lines (previous commit was 81) --- docs/design/muse-async-poll.md | 79 +++++++++++++--------------------- 1 file changed, 29 insertions(+), 50 deletions(-) diff --git a/docs/design/muse-async-poll.md b/docs/design/muse-async-poll.md index 5665d22..77488cc 100644 --- a/docs/design/muse-async-poll.md +++ b/docs/design/muse-async-poll.md @@ -1,79 +1,58 @@ # Muse async poll for held writes +Draft. Terms follow `muse-gated-passthrough.md` 3.4 and 4.5. -Status: draft, awaiting owner confirmation. Terms (held, start claim, `forward_started`, -`dropped_on_reload`) are as in `muse-gated-passthrough.md` 3.4 and 4.5. - -## 1. Scope - +## Scope Only the Muse route `POST /channels/*/messages` (held). Blocking mode stays selectable by config. Everything else is unchanged. -## 2. POST - -1. Write the `held` audit record first. If the write fails, refuse 503 `audit_unavailable` before any - entry exists and before any notification. +## POST +1. Write the `held` audit record first. On failure refuse 503 `audit_unavailable`, before any entry + exists and before any notification. 2. Run the existing admission checks (cooldown, `max_held_per_identity`, `max_held_total`) with their existing refusals. -3. Insert the entry in the in-memory pending table, send the existing Telegram notification. +3. Insert into the in-memory pending table and send the existing Telegram notification. 4. Return 202 with `request_id` and a random poll token (at least 128 bits, stored only as a hash, - compared in constant time). The body and `Authorization` are never echoed. - -## 3. GET /poll/?wait=N + constant-time compare). Body and `Authorization` are never echoed. +## GET /poll/?wait=N `wait` missing means 0, capped at 25 s. Non-numeric or negative is 400, without revealing whether the -token exists. Response is `pending` or a final status: +token exists. The response is `pending` or a final status: | status | HTTP | fields | |---|---|---| | `approved_and_sent` | 200 | `status`, `request_id`, `discord_status`, bounded `body`, `truncated` | -| `denied`, `expired`, `dropped` | 200 | `status`, `request_id` | -| `send_failed` | 200 | `status`, `request_id` | +| `denied`, `expired`, `dropped`, `send_failed` | 200 | `status`, `request_id` | | `send_unknown` | 200 | `status`, `request_id`, `error_class` (`reset`, `timeout`, `write_failed`) | -The token is read-only. It can never approve, deny or change state. - -## 4. Retention - -A final status stays readable for one fixed 60 s window. After that, and for any unknown token, the -response is a uniform 404. No retained-entry cap beyond this window. - -## 5. Retries +The token is read-only and can never approve, deny or change state. A final status stays readable +for one fixed 60 s window; afterwards, and for any unknown token, the response is a uniform 404. No +retained-entry cap beyond this window. -No `Idempotency-Key` and no idempotency store. Muse never retries a POST blindly. After a lost or +## Retries +No `Idempotency-Key`, no idempotency store. Muse never retries a POST blindly; after a lost or unknown POST response it submits a NEW request. This is safe because every message needs its own Telegram approval: a duplicate is a separate approval the owner can deny, so there is no unapproved double send. -## 6. States and races - -- A request that has passed `forward_started` never ends as `dropped`. An unconfirmed result is - `send_unknown` with an error class. -- `forward_started` is durable before the upstream connect. +## States and races +- A request past `forward_started` never ends as `dropped`; an unconfirmed result is `send_unknown` + with an error class. `forward_started` is durable before the upstream connect. - A start-claim failure before any byte maps to `dropped`, audit code `dropped_on_reload`. - Approve versus expiry race on one mutex. The first transition wins. +- Restart drops all pending entries; a poll then returns the uniform 404. An audit `held` record with + no completion is the expected trace. -## 7. Restart - -Restart drops all pending entries. A poll then returns the uniform 404. An audit `held` record with no -completion is the expected trace. - -## 8. Approval and audit - -Approval stays on Telegram only, with the existing start-claim checks. Audit records keep their current -shape and never carry a token, `Authorization`, hash or body. `async_hold_secs` (default 120) bounds a -pending entry. Unapproved entries end as `expired`. - -## 9. Departures - -- Connection-as-state is replaced for this route. -- No cancel on Muse disconnect. -- Discord response headers are not returned verbatim. -- `X-AP-Decision` is not returned after admission. - -Owner confirmation is needed for these four departures before implementation. +## Approval and audit +Approval stays on Telegram only, with the existing start-claim checks. Audit records keep their +current shape and never carry a token, `Authorization`, hash or body. `async_hold_secs` (default 120) +bounds a pending entry; unapproved entries end as `expired`. -## 10. Tests +## Departures +Connection-as-state is replaced for this route. No cancel on Muse disconnect. Discord response +headers are not returned verbatim. `X-AP-Decision` is not returned after admission. +Owner confirmation of these four departures is needed before implementation. +## Tests - Poll after each final status. - Uniform 404 after the window and for unknown tokens. - `send_unknown` path. From 2e5c3273eacf687e26005d2c5d532c3b26e24a0b Mon Sep 17 00:00:00 2001 From: chaodu-agent <274062505+chaodu-agent@users.noreply.github.com> Date: Sun, 22 Nov 2026 10:49:10 -0500 Subject: [PATCH 7/8] docs: fix review findings in muse async poll design Review round 1 (6 MEDIUM) addressed; doc within 60 lines. Docs only, no tests. --- docs/design/muse-async-poll.md | 57 +++++++++++++++++----------------- 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/docs/design/muse-async-poll.md b/docs/design/muse-async-poll.md index 77488cc..0bf7f9f 100644 --- a/docs/design/muse-async-poll.md +++ b/docs/design/muse-async-poll.md @@ -1,60 +1,59 @@ # Muse async poll for held writes Draft. Terms follow `muse-gated-passthrough.md` 3.4 and 4.5. - ## Scope -Only the Muse route `POST /channels/*/messages` (held). Blocking mode stays selectable by config. -Everything else is unchanged. - +Only the Muse route `POST /channels/*/messages` (held), per route. Config key `muse.hold_mode = +blocking|async` (default `blocking`); `async_hold_secs` applies only in `async`. Else unchanged. ## POST 1. Write the `held` audit record first. On failure refuse 503 `audit_unavailable`, before any entry exists and before any notification. -2. Run the existing admission checks (cooldown, `max_held_per_identity`, `max_held_total`) with their - existing refusals. +2. Run the existing admission checks (cooldown, `max_held_per_identity`, `max_held_total`). A refusal + writes a terminal audit record with the existing code (`cooldown`, `identity_cap`, `table_full`) + after the `held` record and returns the existing 503. No token is issued. 3. Insert into the in-memory pending table and send the existing Telegram notification. 4. Return 202 with `request_id` and a random poll token (at least 128 bits, stored only as a hash, constant-time compare). Body and `Authorization` are never echoed. ## GET /poll/?wait=N -`wait` missing means 0, capped at 25 s. Non-numeric or negative is 400, without revealing whether the -token exists. The response is `pending` or a final status: +The token is the only credential. The poll needs no `Authorization` and returns the same 404 +whatever headers are sent. `wait` is an integer, missing means 0, values above 25 are capped at 25; +anything else is 400, without revealing whether the token exists. Response is `pending` or final: | status | HTTP | fields | |---|---|---| -| `approved_and_sent` | 200 | `status`, `request_id`, `discord_status`, bounded `body`, `truncated` | +| `approved_and_sent` | 200 | `status`, `request_id`, `discord_status`, `body`, `truncated` | | `denied`, `expired`, `dropped`, `send_failed` | 200 | `status`, `request_id` | | `send_unknown` | 200 | `status`, `request_id`, `error_class` (`reset`, `timeout`, `write_failed`) | -The token is read-only and can never approve, deny or change state. A final status stays readable -for one fixed 60 s window; afterwards, and for any unknown token, the response is a uniform 404. No -retained-entry cap beyond this window. +`body` is at most 64 KiB, `truncated=true` if cut. The token is read-only and never approves, denies +or changes state. A final status stays readable for a fixed 60 s window starting at the final +transition (for `approved_and_sent`, when the Discord response is fully read or cut at 64 KiB). +After that, and for any unknown token, the response is a uniform 404. No retained-entry cap. ## Retries -No `Idempotency-Key`, no idempotency store. Muse never retries a POST blindly; after a lost or -unknown POST response it submits a NEW request. This is safe because every message needs its own -Telegram approval: a duplicate is a separate approval the owner can deny, so there is no unapproved -double send. +No `Idempotency-Key`, no store. Muse never retries a POST blindly; after a lost or unknown POST +response it submits a NEW request. Safe because each message needs its own Telegram approval. ## States and races -- A request past `forward_started` never ends as `dropped`; an unconfirmed result is `send_unknown` - with an error class. `forward_started` is durable before the upstream connect. -- A start-claim failure before any byte maps to `dropped`, audit code `dropped_on_reload`. +- `forward_started` is written after the upstream connection is established and before the first + request byte. A connect failure writes no `forward_started` and ends as `send_failed`, audit code + `connect_failed`. +- Nothing sent before the first byte (start-claim failure, `connect_failed`, `audit_unavailable` on + the `forward_started` write, shutdown): start-claim failure (`dropped_on_reload`) and shutdown give + `dropped`, the others give `send_failed`. Anything after the first byte that is not a confirmed + Discord response gives `send_unknown`, never `dropped`. - Approve versus expiry race on one mutex. The first transition wins. -- Restart drops all pending entries; a poll then returns the uniform 404. An audit `held` record with - no completion is the expected trace. +- Restart drops all pending entries; a poll then returns the uniform 404. A `held` record with no + completion is the expected trace. ## Approval and audit Approval stays on Telegram only, with the existing start-claim checks. Audit records keep their -current shape and never carry a token, `Authorization`, hash or body. `async_hold_secs` (default 120) -bounds a pending entry; unapproved entries end as `expired`. +current shape and never carry a token, `Authorization`, hash or body. Unapproved entries end as +`expired` after `async_hold_secs` (default 120). ## Departures Connection-as-state is replaced for this route. No cancel on Muse disconnect. Discord response headers are not returned verbatim. `X-AP-Decision` is not returned after admission. Owner confirmation of these four departures is needed before implementation. - ## Tests -- Poll after each final status. -- Uniform 404 after the window and for unknown tokens. -- `send_unknown` path. -- Restart loses pending entries. -- Audit shape, and no token in audit. +Poll after each final status; uniform 404 after the window and for unknown tokens; `send_unknown` +path; restart loses pending entries; audit shape with no token in audit. From 785c993ee15f74ca0dec731258e3dcfd6dae48c9 Mon Sep 17 00:00:00 2001 From: chaodu-agent <274062505+chaodu-agent@users.noreply.github.com> Date: Sun, 22 Nov 2026 10:51:06 -0500 Subject: [PATCH 8/8] docs: async poll skips dedupe and supersede --- docs/design/muse-async-poll.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/design/muse-async-poll.md b/docs/design/muse-async-poll.md index 0bf7f9f..96463c2 100644 --- a/docs/design/muse-async-poll.md +++ b/docs/design/muse-async-poll.md @@ -8,7 +8,8 @@ blocking|async` (default `blocking`); `async_hold_secs` applies only in `async`. exists and before any notification. 2. Run the existing admission checks (cooldown, `max_held_per_identity`, `max_held_total`). A refusal writes a terminal audit record with the existing code (`cooldown`, `identity_cap`, `table_full`) - after the `held` record and returns the existing 503. No token is issued. + after the `held` record and returns the existing 503. No token is issued. In async mode the fingerprint dedupe and supersede are + not applied, so every POST is an independent pending entry and there is no `superseded` status. 3. Insert into the in-memory pending table and send the existing Telegram notification. 4. Return 202 with `request_id` and a random poll token (at least 128 bits, stored only as a hash, constant-time compare). Body and `Authorization` are never echoed. @@ -52,7 +53,7 @@ current shape and never carry a token, `Authorization`, hash or body. Unapproved ## Departures Connection-as-state is replaced for this route. No cancel on Muse disconnect. Discord response -headers are not returned verbatim. `X-AP-Decision` is not returned after admission. +headers are not returned verbatim. `X-AP-Decision` is not returned after admission. Fingerprint dedupe and supersede do not apply. Owner confirmation of these four departures is needed before implementation. ## Tests Poll after each final status; uniform 404 after the window and for unknown tokens; `send_unknown`