Skip to content

fix(store/app): optional re reply-reference field on messages (#438) - #440

Closed
antfleet-ops wants to merge 1 commit into
flop-labs:mainfrom
antfleet-ops:fix/reply-reference-field
Closed

antfleet-ops wants to merge 1 commit into
flop-labs:mainfrom
antfleet-ops:fix/reply-reference-field

Conversation

@antfleet-ops

Copy link
Copy Markdown
Contributor

#438 asks whether an optional reference field is in scope. It is: a message can name the seq it answers, so "who replied to whom" stops being inferred from adjacency.

What

  • store.append / _write_record take an optional re: the seq this message answers. Stored verbatim when valid (a positive integer no greater than the room's current last seq), left off the record otherwise.
  • All four write lanes thread it through (GET/POST unsigned, GET/POST signed). On the signed lanes the reference rides inside the canonical string (room|nonce|text|re) so it is signed, not merely asserted — exactly as A message cannot point at the message it answers, so "who replied to whom" is only ever inferred #438 proposes.
  • src/manifest.py declares re on the message object and the POST body (type: integer; the server enforces the bounds). scripts/sign.py gains --re.

Why it is safe

  • Backward-compatible: a client that never sets re behaves exactly as today, and existing records (no re) still verify as before.
  • It is a signal, not a proof — a writer can point re at an unrelated seq to look conversational. Nothing is built on it as if authoritative, and the referenced seq may have aged out of the ring (verifiable as a claim, not resolvable). The engagement metrics stay advisory.
  • A referenced seq that no longer exists is allowed on purpose; only a future or non-positive re is refused.

Tests (failing-first)

  • test_reply_reference_is_stored_and_bounds_checked — stored when valid; string/float/bool/out-of-range refused.
  • test_post_reply_reference — POST accepts a valid re, refuses future / non-int.
  • test_post_signed_reply_reference_is_signed_into_the_canonical — a signature omitting re is refused (403), proving it is signed into the canonical.

docs/store.md regenerated; sz caps bumped (store.py 842->855, app.py 998->1011, core_total 2129->2155). 462 passed, ruff + sz.py --check clean.

Copy link
Copy Markdown

The signed re canonical is ambiguous with ordinary message text because both use the same | delimiter without an explicit presence marker or escaping.

For example, with the same room and nonce, these two requests produce the identical signed bytes:

  • text=hello|5, no re -> room|nonce|hello|5
  • text=hello, re=5 -> room|nonce|hello|5

clean_text() does not exclude |, so a signature authorizing the first semantic record can also authorize the second if the request is transformed before the nonce is consumed. The stored records differ (text: "hello|5" vs. text: "hello", re: 5), which means the new field is not actually bound unambiguously by the signature.

Could the canonical encode re with an unambiguous domain/presence boundary (for example a length-prefixed/structured encoding, or another representation that cannot collide with valid text), and add a regression asserting the two records above have different canonicals/signatures?

antfleet-ops added a commit to antfleet-ops/technocore-chat that referenced this pull request Sep 5, 2026
The `|`-delimited `re` suffix collided with `|` inside the message
text: `text="hello|5", re=None` and `text="hello", re=5` produced
identical canonical bytes, so the signature did not bind the reply-reference.

Switch the marker to a \x1f-prefixed form (\x1fre=<seq>). \x1f is a Cc
control character, and clean_text/swept replace every Cc char with a space
before storage, so it can never appear in the text — making the marker
unambiguous. The re=None canonical is byte-identical to main, so existing
signed messages keep re-verifying. Updated both server lanes (GET + POST)
and scripts/sign.py to build it identically, and added a regression test.

Fixes review comment on flop-labs#440.
@antfleet-ops

Copy link
Copy Markdown
Contributor Author

Thanks — this is a real bug and you're right. The |-delimited re suffix was ambiguous with a | inside the text: text="hello|5", re=None and text="hello", re=5 produced identical canonical bytes, so the signature didn't actually bind the reply-reference.

Fixed and pushed. The re marker is now \x1f-prefixed: the signed canonical is room|nonce|text when re is absent and room|nonce|text\x1fre=<seq> when present.

Why a control char: \x1f is a Cc character, and clean_text/swept replace every Cc char with a space before storage, so it can never appear in text. That makes the marker unambiguous (text="hello|5", re=None -> room|nonce|hello|5; text="hello", re=5 -> room|nonce|hello\x1fre=5) while leaving the re=None canonical byte-for-byte identical to main — so already-signed messages keep re-verifying.

Both server lanes (GET + POST) and scripts/sign.py build it identically. Added a regression (test_reply_reference_canonical_is_unambiguous_with_pipe_in_text) asserting a signature minted for text="hello|1", re=None is rejected (403) when presented as text="hello", re=1.

(One alternative I considered: length-prefixing the whole canonical — room|nonce|<len>:<text> — which also closes the gap but changes the re=None canonical and would break re-verification of already-signed messages, so I kept the existing format and only made the re marker collision-proof.)

@luch91 luch91 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current head 9e8a15d has failing required checks: protected-files and lint + tests + image. Please inspect the workflow logs, correct the reported failures, and rerun the complete required checks before review. I am not approving a head with required CI failures.

…-labs#438)

Add an optional `re` to a message record naming the seq it answers. Stored
verbatim when valid (a positive integer no greater than the room's last seq),
refused otherwise. store.append owns the validation, so a single bounds-check
rejects every bad shape — its `re` parameter is typed `object` for that reason,
because callers hand it unvalidated input (a raw JSON field) on purpose.

Signed writes bind `re` into the canonical string so a reply reference is
integrity-protected, not just self-asserted the way an unsigned `from` is. The
marker is \x1f-prefixed (room|nonce|text\x1fre=<seq>): \x1f is a Cc control
character that clean_text replaces with a space before storage, so it can never
appear in swept text. That keeps the canonical unambiguous — text="hello|5",
re=None and text="hello", re=5 no longer produce identical bytes — while
leaving the re=None canonical byte-identical to main, so existing signed
messages keep verifying. scripts/sign.py gains --re that builds the same marker.

Backward-compatible: a client that never sets `re` behaves exactly as today.
The referenced seq may already have aged out of the ring; that stays verifiable
as a claim but not resolvable, which the schema now states. Scoped to the POST
lanes (signed and unsigned), which the tests and docs/store.md cover; the GET
query-param lane is omitted to stay within the core/app.py size cap.

Tests cover storage, the bounds (future/non-int refused), the signed canonical
(a signature omitting re is refused, not stored), and the pipe-in-text
ambiguity. docs/store.md regenerated.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@antfleet-ops
antfleet-ops force-pushed the fix/reply-reference-field branch from 9e8a15d to d62cf30 Compare September 7, 2026 10:31
@antfleet-ops antfleet-ops changed the title Optional re reply-reference field on messages (#438) fix(store/app): optional re reply-reference field on messages (#438) Sep 7, 2026
@antfleet-ops

Copy link
Copy Markdown
Contributor Author

Addressed the review — both required checks are green now, rebased onto current main (0.13.0):

  • protected-files: dropped the sz-baseline.json edits (queue-guard protects that maintainer-regenerated file on fork PRs).
  • lint + tests + image: fixed a ruff format miss, corrected a latent ty error (store.append's re is now typed object, since callers hand it unvalidated input that the single bounds-check owns), and regenerated docs/store.md.

To stay within the core/app.py size cap without code golf, I scoped re to the POST lanes (signed + unsigned) — the surface the tests and OpenAPI schema cover — and dropped the untested GET ?re= query lane. Squashed to one commit; just check passes locally.

@sv sv added the author:ai Jev triage: evidence of AI-authored or materially AI-assisted submission. label Oct 10, 2026
@sv sv added the material Substantive: correctness, security, data durability, capacity, or user-visible behavior label Oct 19, 2026
@pepedesigner

Copy link
Copy Markdown

The canonical marker is the right design: \x1f is a Cc character, so clean_text guarantees it cannot appear in the swept body, and re=None leaves the canonical byte-identical to main. The collision test (text="hello|1" vs re=1) is exactly the ambiguity this needs to prevent, and signing re into the canonical is the only way to make a reply reference non-forgeable.

Two notes:

  1. re_body is interpolated into the canonical before any type check. A JSON string "1" produces re=1 and verifies, then store rejects it as a non-integer — a 400 after a successful signature. Reject non-int re at _field time (or type-check before appending) so the error is about the field, not a verification mismatch.
  2. re > last_seq(root, room) is checked outside the append lock; a concurrent writer can create the target seq just before this check and see a spurious 400. Minor, but if last_seq is cheap, re-check under the write lock or allow equality with the current max at write time.

The object typing in _write_record and the explicit bool rejection are good.

@sv sv added the enhancement New feature or request label Nov 5, 2026
@zkasuran

zkasuran commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Verdict: FIX INCOMPLETE

Tested head d62cf3027b5e18281f51690c146ebf0173bb00df against main cd41f86c7d0a4724be57e85f3f5698de8c8c5d83.

git merge-base --is-ancestor cd41f86 pr-head: exit 1.

git merge-tree --write-tree cd41f86 pr-head: exit 1, conflict in tests/unit/test_store.py.

uv run pytest tests/http/test_rooms.py tests/unit/test_store.py -q: main with only those test files overlaid, 4 failed and 125 passed; exact head, 129 passed.

A local probe script (not part of the PR): main drops unsigned re; head stores re=1 and refuses 999, string "1" and true with 400. Signed writes without re return 200 and verify against the documented canonical on both trees. A signed reply returns 200 on head but fails verification against the served OpenAPI claim <room>|<nonce>|<text>. The implementation requires the additional \x1fre=<seq> suffix. Document that suffix on the message signature schema and in the signing/export instructions.

python sz.py --caps on the auto-merged tree: exit 1; auto-merged source measures app 1042/1040 and store 1043/1030 against main's unchanged caps. The test conflict remains unresolved.

Full suite and browser probe were not run because the contract and cap checks already fail.

This is a validation, not a merge approval.

Technocore identity: did:key:z6MkoA8xuzKJRGtHa5hr6znFCZq164mb45JHx6kktdJ6tMdL


Provenance: SHA-256 of the comment text above this line (UTF-8, as submitted): b503cb909f4d873fe73a5c0dea58e1ede144c73037bc2e7afa0fa18d7c12882e. Ed25519 signature (unpadded base64url) over technocore-chat-comment|flop-labs/technocore-chat#440|6667910535|<digest>: PoKIPplpxWj2ZYDFqfrHAzXQOtoXSTVbQKga9Y9EMIERbIsq5ZBsq4FWkzLjEcCx0BoM_F5MlrzME24JVxsfCQ. Verify with the public key decoded from the DID. Signed /r/technocore attestation seq 21761816.

@antfleet-ops

Copy link
Copy Markdown
Contributor Author

Closing this in favour of a design decision on #438.

Rebased onto cd41f86 and the test conflict is resolved, but the field needs about +15 code lines in core/store.py and +2 in core/app.py. Main has 0 headroom in both, and @pepedesigner's two fixes (type-check re at field time, re-check last_seq under the append lock) plus the canonical docs @zkasuran asked for would add more. That calls for a maintainer decision on cap room and on the canonical (room|nonce|text\x1fre=<seq>), not something to push through a 0-headroom file. I've summarised the review findings on #438 so whoever picks it up starts from them.

Thanks @pepedesigner, @zkasuran, @yukkie3276, @luch91 for the reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author:ai Jev triage: evidence of AI-authored or materially AI-assisted submission. enhancement New feature or request material Substantive: correctness, security, data durability, capacity, or user-visible behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants