Skip to content

fix(store): preserve seq state across mixed-version splits - #708

Open
ShalyX wants to merge 2 commits into
flop-labs:mainfrom
ShalyX:fix/seqstate-merge-high-water-marks
Open

ShalyX wants to merge 2 commits into
flop-labs:mainfrom
ShalyX:fix/seqstate-merge-high-water-marks

Conversation

@ShalyX

@ShalyX ShalyX commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Summary

Why

During a rolling upgrade, an old worker can recreate/update .seqstate while a new worker has already updated the room's shard. Selecting either complete entry can preserve one transition while regressing the other, causing cursor starvation or a stale conversation generation.

Verification

  • Verified against main at 82d9429.
  • uv run ruff check .
  • uv run ruff format --check .
  • uv run ty check
  • uv run coverage run -m pytest tests -q: 665 passed, 1 skipped
  • uv run coverage report: 97.95% total coverage
  • uv run python sz.py --caps
  • git diff --check

No public API or new resource surface; no abuse-impact change. Does not modify changelog or protected size baseline.

@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown

Open pull requests citing the same issues:

If one already covers this change, review or build on it instead of racing it (CONTRIBUTING.md "Overlapping work").

Copy link
Copy Markdown

floor is not monotonic across room lifecycles, so merging it independently with max() can carry a stale floor into a newer generation. _set_seq_entry(..., floor=None) explicitly increments gen and clears floor on (re)create; e.g. merging {floor: 500, gen: 2} with a later {floor: 0, gen: 9} should preserve the recreate as {floor: 0, gen: 9}, but this patch produces {floor: 500, gen: 9}. That can make cursors in the new conversation appear to be behind an old-generation floor and skip valid messages. The merge needs to respect the coupling between generation and floor (and a regression should cover a newer generation with a cleared floor), rather than taking both maxima independently.

@ShalyX

ShalyX commented Sep 4, 2026

Copy link
Copy Markdown
Contributor Author

Fixed in commit 9cbe5db. Generation and floor now remain coupled: a newer generation wins as a whole, while floors are merged by high-water mark only within the same generation. Added regression coverage for a newer {floor: 0, gen: 9} recreate against an older {floor: 500, gen: 2}. Verification: 16 focused sequence-state tests passed, full suite 665 passed with 1 skipped, Ruff and format checks passed, and git diff --check passed.

@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.

Reviewed exact head 9cbe5db. The migration now treats generation and floor as coupled lifecycle state: the higher generation wins as a whole, while equal-generation floors merge by non-negative high-water maximum. This preserves recreate semantics without allowing an older floor to leak into a newer lifecycle. The added regression reproduces the mixed-version case, and the existing suite covers first split, shard races, downgrade backup, idempotence, and pre-split reads. The PR reports 665 tests, Ruff, format, type, size, diff, and coverage checks passing; GitHub CI is green for full gates. No blocker found.

@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.

Reviewed exact head 9cbe5db2ac6eb447cb4bc339900d87f6cb9cb491. During mixed-version seq-state migration, the patch merges lifecycle entries by generation and keeps floor monotonic only within the same generation; a newer generation carries its own coupled floor instead of inheriting an older lifecycle’s floor. This preserves cursor and recreation semantics across legacy-map/shard races. The added regression test exercises a newer generation with a lower floor, and required runtime/MCP/OpenAPI/lint/test/image checks are green. Approved.

@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 exact implementation and regression remain source-level sound, but the approval on this head is no longer sufficient for the current repository state. GitHub reports PR #708 BEHIND; it targets 82d942936050f1ab0fb9f34db17893b89f3e064b, while current main is 45921c3e3699e01a55cde391674815367e0cff6b. Please rebase onto current main, resolve any sequence-state overlap, and rerun the full required gates on the rebased commit. The previous approval should not be read as verification of the current base.

@almondous

Copy link
Copy Markdown
Contributor

Local revalidation of 9cbe5db on current main e4c4f73 found two integration gaps worth fixing before approval:

  1. The PR's added test_mixed_upgrade_keeps_newer_generation_floor_coupled also passes on unchanged main. An additional nine-case matrix fails five cases on main and passes on this implementation. It covers both migration directions, same-generation floors, different generations, backup preservation/idempotence, and a real append/read where main emits seq 11 instead of 51 and strands a since=50 reader.
  2. The combined tree has 1011 store code-lines against the 1010 cap. Reusing the existing _seq_value for both floor operands brings it to 1010 without changing validation semantics or protected files.

The merge docstring also still describes independent maxima, whereas the implementation selects the newer generation as a whole and merges floors only within the same generation.

The unmodified combined tree passes 783 tests; the small local follow-up with the additional cases passes 792, with one existing skip. Ruff, formatting, ty, and caps pass for the follow-up; coverage is 98.03%. Tested on macOS / Python 3.12.9, not Linux CI.

The follow-up also passes all 138 official Chromium checks.

I have a supplemental patch for this PR, not a competing PR.

@yukkie3276 yukkie3276 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

One fail-closed migration edge remains in the new merge branch. If the shard already has a valid dict entry but the recovered legacy map has a non-dict value for the same room, if not isinstance(current, dict) or not isinstance(entry, dict): merged[room] = entry lets the malformed legacy value overwrite the valid shard state. _read_seq_state() only validates the outer object, so this case is reachable from a partially malformed legacy state; subsequent _seq_field() reads then fall back to zero and can regress both generation/floor despite a valid durable shard entry. Please preserve the valid dict when only one side is structurally valid (and add the symmetric regression cases); only the both-valid case should enter lifecycle merging.

Copy link
Copy Markdown
Contributor

@ShalyX, following up on my supplemental patch and @yukkie3276’s malformed-state review: ba19f64 builds on your lifecycle fix and contains the integrated change on current main 0e47f770b13cc27e1e2e199d4cdf70a4778c97cc for this PR.

  • Whole snapshots are ordered by structural validity, generation, then floor. A malformed legacy value cannot overwrite valid shard state; a newer generation retains its own floor. Advisory metadata stays with the winning snapshot, with legacy winning exact ties.
  • Sharing existing nonnegative-field parsing keeps store.py at 1029/1030 code lines, unchanged from main, without changing protected files.
  • Added symmetric first/recovered migration tests, real append/read beyond since=50, lifecycle-state-machine coverage, and parsing/metadata checks. The 28-case regression matrix fails 8 cases on this PR’s current head and 14 on unchanged main.

Linux verification explicitly checked out that clean commit: 911 tests passed, 1 existing skip, 98.08% coverage, 881 API-contract cases and all 138 official Chromium checks passed, plus lint/types/caps.

Verified cherry-pick onto 0e47f770: git cherry-pick ba19f64baa5df3ba4f4018c603a3a048ed42b21c. This commit contains the full updated implementation; the fork-only workflow commit is excluded. Happy for you to fold it into this PR.

almondous added a commit to almondous/technocore-chat that referenced this pull request Oct 1, 2026
Current-main integration of japaneseml's PR flop-labs#311 lock-domain fix for flop-labs#302. Reuse the inode-validated locking primitive for sweeps, preserve shared/nonblocking semantics and explicit unlock, and recheck data under the acquired lock.

Add kernel, multiprocess, CAS/sequence, cleanup/counting, and stateful regressions. Keep the immutable store cap at 1030 code lines with shared cleanup contexts.

Place the stateful rule independently of the PR flop-labs#708 supplement; both patches apply cleanly in either order and produce the same combined tree.
@ShalyX
ShalyX force-pushed the fix/seqstate-merge-high-water-marks branch from 9cbe5db to d1c60f7 Compare October 2, 2026 00:08
@ShalyX

ShalyX commented Oct 2, 2026

Copy link
Copy Markdown
Contributor Author

Morning catch-up rebuild onto current main (0e47f77).

Tip: d1c60f7 on fix/seqstate-merge-high-water-marks (force-pushed; prior tip was monorepo-era).

Invariant: mixed-version .seqstate split merges by lifecycle, not whole-entry file age.

  • Higher gen wins the whole entry (recreate clears floor with the bump).
  • Same gen: floor is independently monotonic (max).
  • Stale post-split legacy map can no longer rewind a shard recreate.

Evidence

  • RED (main behavior, production fix stashed): test_mixed_upgrade_does_not_let_stale_map_clobber_shard_recreate → failed assert 2 == 3 (stale map rewound generation).
  • GREEN (with fix): tests/unit/test_seq_state.py → 21 passed; broader test_seq_state + test_store + test_stats → 115 passed.
  • sz.py --caps: store 1026/1030 (4 free) via net-zero packing of unrelated helpers.
  • ruff check / ruff format --check / ty check / git diff --check: clean.

Not merge approval from CI yet — waiting on fresh checks for this tip.

Copy link
Copy Markdown
Contributor

@ShalyX, thanks for rebuilding on current main. I rechecked d1c60f711e51b6f041e869a26c177c6129867d59; its CI and Chromium checks are now green.

One malformed-value case from the earlier review still reproduces: a valid shard {floor: 50, gen: 3} plus legacy {gone: null} becomes (floor, gen) = (0, 0) after the split; the next append writes seq 1 instead of 51.

I prepared f6032f6 directly on your new tip. It changes one production line to retain whichever side is a dict, preserving your lifecycle merge, and adds symmetric migration/cursor regressions plus a state-machine rule. Eight cases fail without the fix; all 52 focused/stateful tests pass with it.

Local uv run just check: 890 passed, 1 existing skip, 98.16% coverage; lint/types/format/caps pass and store stays 1026/1030. The published tree matches that tested tree.

Applicable directly to d1c60f7: git cherry-pick f6032f66001bb937bb3648dd238b9d8048f66f78.

@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 d1c60f7 is rebased onto current main and its reported CI is green, but the latest exact-head validation still reproduces a malformed migration case: a valid shard entry can be overwritten by a non-dict legacy value, regressing floor/generation to zero. The proposed supplemental fix f6032f6 is not part of this PR head. Please incorporate an equivalent guarded merge and regression tests, then rerun the full required gates.

ShalyX pushed a commit to ShalyX/technocore-chat that referenced this pull request Oct 18, 2026
Follow up on PR flop-labs#708 rebuilt at d1c60f7. Preserve the valid dict when only one migration entry is structurally valid, without changing lifecycle merging or cap usage. Add symmetric first/recovered migration regressions, append/cursor checks, and a lifecycle state-machine rule.
@ShalyX
ShalyX force-pushed the fix/seqstate-merge-high-water-marks branch from d1c60f7 to 497692b Compare October 18, 2026 00:06
@ShalyX

ShalyX commented Oct 18, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up update on exact head 497692b, rebuilt from upstream/main 0e47f77.

This incorporates the malformed-migration hardening from f6032f6:

  • retain the structurally valid lifecycle entry when the other side is malformed;
  • add symmetric first/recovered migration coverage;
  • add append/cursor assertions and the stateful migration rule.

Regression evidence:

  • RED on the prior d1c60f7 implementation with the new tests overlaid: 8 failed, 20 passed, 21 deselected. The failures are valid shard state being replaced by malformed legacy values.
  • GREEN on this head: full suite 890 passed, 1 skipped; coverage 98.08%.
  • ruff check, ruff format --check, ty check, git diff --check: passed.
  • sz.py --caps: store.py 1026/1030, app.py 1040/1040; sz.py --check: passed.

The existing PR branch was force-updated; no duplicate PR was opened and no merge was performed. This is implementation and regression evidence, not merge approval.

@sv sv added the material Substantive: correctness, security, data durability, capacity, or user-visible behavior label Oct 19, 2026
ShalyX pushed a commit to ShalyX/technocore-chat that referenced this pull request Oct 20, 2026
Follow up on PR flop-labs#708 rebuilt at d1c60f7. Preserve the valid dict when only one migration entry is structurally valid, without changing lifecycle merging or cap usage. Add symmetric first/recovered migration regressions, append/cursor checks, and a lifecycle state-machine rule.
@ShalyX
ShalyX force-pushed the fix/seqstate-merge-high-water-marks branch from 497692b to c9f665a Compare October 20, 2026 00:09
@ShalyX

ShalyX commented Oct 20, 2026

Copy link
Copy Markdown
Contributor Author

Rebuilt existing PR #708 onto current upstream/main 4a013e5 and force-pushed head c9f665a.

Review follow-up included:

  • preserve a structurally valid shard when the legacy entry is malformed, in either migration direction;
  • add symmetric first/recovered migration regressions;
  • add append/cursor assertions and a stateful migration rule.

Verification on the exact pushed tree:

  • uv sync --frozen: passed
  • focused seq-state/stateful tests: 52 passed
  • full suite: 923 passed, 1 skipped; coverage 98.16%
  • ruff check: passed
  • ruff format --check: passed
  • ty check: passed
  • sz.py --caps: passed (store.py 1026/1030, app.py 1040/1040)
  • sz.py --check: passed
  • git diff --check: passed

The first full run exposed one timing-sensitive stats test failure; the isolated test passed three consecutive reruns and the complete suite passed on the second run. This is implementation and regression evidence, not merge approval.

@pepedesigner

Copy link
Copy Markdown

Handling UnicodeDecodeError in the seq-state reader is the right hardening for a mixed-version/hand-edited shard, and coercing invalid counters to 0 keeps the reader total. The as_read/as_read_view split is also the correct place for the nonce-as-text change.

Two notes:

  1. fix(json): give a signed record's nonce back as canonical decimal text, not a JSON number #712 changes the JSON nonce to text; this PR's as_read is the seam that makes it consistent. Make sure /export, /r/<room>?format=json, and the posted object all use it, or one lane will still emit an int.
  2. A shard that fails to decode is silently treated as empty; log it at debug level at least, or an operator cannot tell corruption from absence.

@sv sv added the bug Something isn't working label Nov 5, 2026
ShalyX and others added 2 commits November 21, 2026 08:02
Merge legacy and shard .seqstate entries by generation lifecycle and
floor high-water mark instead of picking one whole entry from file age.

A post-split stale map can no longer rewind a shard recreate (gen+floor).
Same-gen independent reaps keep the higher floor. Net-zero packing keeps
store.py under the 1030 cap (1026/1030).

Closes flop-labs#652.
Follow up on PR flop-labs#708 rebuilt at d1c60f7. Preserve the valid dict when only one migration entry is structurally valid, without changing lifecycle merging or cap usage. Add symmetric first/recovered migration regressions, append/cursor checks, and a lifecycle state-machine rule.
@ShalyX
ShalyX force-pushed the fix/seqstate-merge-high-water-marks branch from c9f665a to d960702 Compare November 21, 2026 00:10
@ShalyX

ShalyX commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Rebuilt existing PR #708 onto current upstream main and force-pushed the existing ShalyX branch (not merge approval).

Provenance:

  • Current upstream main: 3cd868e
  • Exact PR head: d960702
  • PR base and merge base: 3cd868e
  • Scope: src/store.py, tests/unit/test_seq_state.py, tests/test_store_stateful.py

The rebuilt head preserves a structurally valid seq-state entry when the other side of a mixed-version migration is malformed, while retaining lifecycle-coupled generation/floor merging. It includes symmetric regressions and a stateful migration rule.

Exact-head local evidence:

  • Focused seq-state/stateful tests: 52 passed
  • Full suite: 953 passed, 1 skipped; coverage 98.09%
  • ruff check: passed
  • ruff format --check: passed (104 files)
  • ty check: passed
  • sz.py --caps: passed; store.py 1026/1030 and app.py 1040/1040
  • sz.py --check: passed; core code 2374 <= baseline 2378
  • git diff --check: passed
  • humans_ui_probe.mjs: first run hit timing failures in the live composer/out-of-order section; after restarting the isolated service, the complete rerun passed, including browser-engine IME and WebMCP sections. No source changed between runs.

The branch tip and PR metadata were read back after the force-push. Hosted checks for this new head remain authoritative and must rerun. This is reproducible implementation evidence, not merge approval.

@ShalyX

ShalyX commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Browser-gate follow-up for exact head d960702 (not merge approval).

The hosted humans job failed at the delegation display late-error wait after earlier sections passed:
https://github.com/flop-labs/technocore-chat/actions/runs/44841998915/job/136652418298

Fresh isolated comparison against current upstream main 3cd868e:

  • Base browser probe: passed, including delegation late-error and all later browser-engine IME checks.
  • Exact PR head browser probe: first local run hit timing failures in the live composer/out-of-order section; after restarting the isolated service, the complete probe passed, including delegation late-error and browser-engine IME checks.
  • No source changed between local attempts.

This does not prove the hosted failure is infrastructure-related, but the hosted failure is not reproduced on either current base or exact head locally. The PR remains blocked until GitHub reruns or resolves the hosted browser gate. Local browser evidence is separate from hosted CI and is not merge approval.

This branch has not been deployed

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

Labels

bug Something isn't working 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