[PR #6158] [MERGED] fix(environment): bound state reads and retain cleanup ownership #7171

Closed
opened 2026-11-01 21:45:54 +00:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/morluto/rea/pull/6158
Author: @morluto
Created: 10/21/2026
Status: ✅ Merged
Merged: 10/21/2026
Merged by: @morluto

Base: main ← Head: codex/fix-5351-state-cleanup


📝 Commits (10+)

  • ad439bd feat(environment): extract the initial state a capture's reads observed
  • 7a668b7 Merge main into feat/environment-state-view
  • f2c9a2e fix(environment): bound state at redacted writes; infer identity only from items
  • 2999128 fix(environment): keep first sightings, one list pointer and linear field unions
  • a5c7b92 fix(environment): keep post-write values and partial lists out of the initial state
  • 4e93547 docs(environment): scope item-shaped and deleted written paths out of documents
  • aba9cb1 fix(environment): count writes in flight when a value changes between reads
  • 856c8c0 Merge main into feat/environment-state-view
  • 55480bd chore: merge main (5fa4defc) into environment state view
  • 9fee36d fix(environment): catch cross-tool changes across writes; read only item totals

📊 Changes

8 files changed (+517 additions, -106 deletions)

View changed files

📝 src/application/environment/environmentState.test.ts (+57 -0)
📝 src/application/environment/environmentStateDocument.ts (+110 -46)
📝 src/application/environment/environmentStateStore.ts (+14 -2)
📝 src/domain/jsonValue.test.ts (+3 -1)
📝 src/domain/jsonValue.ts (+116 -54)
📝 src/server/environment/environmentDataPlane.ts (+1 -1)
📝 src/server/environment/serveEnvironment.ts (+99 -2)
📝 src/server/environment/serveEnvironmentState.test.ts (+117 -0)

📄 Description

Summary

Fixes the remaining state-document resource problems from #5351, whose fork rejected maintainer pushes. Original contributor commits and attribution are preserved. After merging main f23066f0, the residual diff is eight state/JSON implementation and test files; it no longer introduces the already-landed state-serving feature.

HTTP shutdown now attempts every owned finalizer, preserves primary/secondary failure order and retains failed descriptor ownership for cleanup retry. Wide JSON traversal uses active-depth cursors rather than allocating one temporary child record per array element. Server-decoded state transfers through an explicitly owned path without another whole-tree copy; ordinary public replace still makes defensive copies.

Within one RPC request/batch, unchanged data/get results reuse exact encoded-size facts keyed by installed member-array identities. Repeated refused replies no longer rescan the entire snapshot. Replacement/reset identities invalidate or safely reuse those facts, and response accounting retains the existing byte budget.

Validation and limits

Node 24.18.0/npm 11.16.0: 71 focused source tests and 3 compiled CLI/MCP boundary tests passed, with npm run check:fast. The 100-read batch regression verifies refusals, actual response size and repeated traversal work. Cleanup coverage includes independent finalizer failures and exact descriptor retry; caller-alias mutation remains isolated by the ordinary replacement path.

After the final main merge, the only source conflict was the JSON validator. Its 22 focused tests and check:fast passed again; the resolution retains both bounded traversal and main's complete depth-limit diagnostic.

An original one-million-scalar defensive-replacement probe exhausted V8's heap with a 128 MiB old-space setting. The final implementation passed that fixture with the same setting (5.1 MiB observed heap delta; 339.1 MiB process max RSS). An eight-million-zero, 16,002,867-byte fixture passed the owned path with 256 MiB old-space (8.8 MiB observed heap delta; 533.2 MiB process max RSS). These Node/tsx source probes include runtime/native overhead in RSS; they are not portable memory guarantees or complete file/network task measurements. Agent tokens, cold/warm whole workflows and provider costs were not measured. No real provider or full PR suite was rerun.

Current hosted checks and human review remain pending; this draft does not attest to human readiness. Original #5351 remains unchanged on its fork.

Author readiness

Human owner: @morluto

  • I reviewed the final diff and relevant existing code, understand the change and its edge cases, and take responsibility for the complete PR, including AI-assisted work.
  • I checked the evidence above, confirmed it supports the current change, and recorded meaningful validation gaps.
  • I declare this PR ready for initial maintainer review.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/morluto/rea/pull/6158 **Author:** [@morluto](https://github.com/morluto) **Created:** 10/21/2026 **Status:** ✅ Merged **Merged:** 10/21/2026 **Merged by:** [@morluto](https://github.com/morluto) **Base:** `main` ← **Head:** `codex/fix-5351-state-cleanup` --- ### 📝 Commits (10+) - [`ad439bd`](https://github.com/morluto/rea/commit/ad439bd2f26c61b442dcf758c2bb149e49657929) feat(environment): extract the initial state a capture's reads observed - [`7a668b7`](https://github.com/morluto/rea/commit/7a668b73f4783b42eb0e8ce1eeff079f86d53e22) Merge main into feat/environment-state-view - [`f2c9a2e`](https://github.com/morluto/rea/commit/f2c9a2e4462a7162224bec209f21dfa2622abe39) fix(environment): bound state at redacted writes; infer identity only from items - [`2999128`](https://github.com/morluto/rea/commit/2999128baedceabc738e9a5aa69bfcfec39a55f1) fix(environment): keep first sightings, one list pointer and linear field unions - [`a5c7b92`](https://github.com/morluto/rea/commit/a5c7b9236c48094bc4af24ae055e8d67e3968477) fix(environment): keep post-write values and partial lists out of the initial state - [`4e93547`](https://github.com/morluto/rea/commit/4e93547c846444e891e2a2267425c316e6ee5218) docs(environment): scope item-shaped and deleted written paths out of documents - [`aba9cb1`](https://github.com/morluto/rea/commit/aba9cb15c8836b7592a51743d79bceab4dfd99af) fix(environment): count writes in flight when a value changes between reads - [`856c8c0`](https://github.com/morluto/rea/commit/856c8c0b50ec11e4ba96cb6172b7cfe01ef6829f) Merge main into feat/environment-state-view - [`55480bd`](https://github.com/morluto/rea/commit/55480bdf7e3275e3be8903e53baabe28be81a9a9) chore: merge main (5fa4defc) into environment state view - [`9fee36d`](https://github.com/morluto/rea/commit/9fee36d3155145fa8e568b34713e0e68c7675216) fix(environment): catch cross-tool changes across writes; read only item totals ### 📊 Changes **8 files changed** (+517 additions, -106 deletions) <details> <summary>View changed files</summary> 📝 `src/application/environment/environmentState.test.ts` (+57 -0) 📝 `src/application/environment/environmentStateDocument.ts` (+110 -46) 📝 `src/application/environment/environmentStateStore.ts` (+14 -2) 📝 `src/domain/jsonValue.test.ts` (+3 -1) 📝 `src/domain/jsonValue.ts` (+116 -54) 📝 `src/server/environment/environmentDataPlane.ts` (+1 -1) 📝 `src/server/environment/serveEnvironment.ts` (+99 -2) 📝 `src/server/environment/serveEnvironmentState.test.ts` (+117 -0) </details> ### 📄 Description ## Summary Fixes the remaining state-document resource problems from #5351, whose fork rejected maintainer pushes. Original contributor commits and attribution are preserved. After merging main `f23066f0`, the residual diff is eight state/JSON implementation and test files; it no longer introduces the already-landed state-serving feature. HTTP shutdown now attempts every owned finalizer, preserves primary/secondary failure order and retains failed descriptor ownership for cleanup retry. Wide JSON traversal uses active-depth cursors rather than allocating one temporary child record per array element. Server-decoded state transfers through an explicitly owned path without another whole-tree copy; ordinary public `replace` still makes defensive copies. Within one RPC request/batch, unchanged `data/get` results reuse exact encoded-size facts keyed by installed member-array identities. Repeated refused replies no longer rescan the entire snapshot. Replacement/reset identities invalidate or safely reuse those facts, and response accounting retains the existing byte budget. ## Validation and limits Node 24.18.0/npm 11.16.0: 71 focused source tests and 3 compiled CLI/MCP boundary tests passed, with `npm run check:fast`. The 100-read batch regression verifies refusals, actual response size and repeated traversal work. Cleanup coverage includes independent finalizer failures and exact descriptor retry; caller-alias mutation remains isolated by the ordinary replacement path. After the final main merge, the only source conflict was the JSON validator. Its 22 focused tests and `check:fast` passed again; the resolution retains both bounded traversal and main's complete depth-limit diagnostic. An original one-million-scalar defensive-replacement probe exhausted V8's heap with a 128 MiB old-space setting. The final implementation passed that fixture with the same setting (5.1 MiB observed heap delta; 339.1 MiB process max RSS). An eight-million-zero, 16,002,867-byte fixture passed the owned path with 256 MiB old-space (8.8 MiB observed heap delta; 533.2 MiB process max RSS). These Node/tsx source probes include runtime/native overhead in RSS; they are not portable memory guarantees or complete file/network task measurements. Agent tokens, cold/warm whole workflows and provider costs were not measured. No real provider or full PR suite was rerun. Current hosted checks and human review remain pending; this draft does not attest to human readiness. Original #5351 remains unchanged on its fork. ## Author readiness Human owner: @morluto - [ ] I reviewed the final diff and relevant existing code, understand the change and its edge cases, and take responsibility for the complete PR, including AI-assisted work. - [ ] I checked the evidence above, confirmed it supports the current change, and recorded meaningful validation gaps. - [ ] I declare this PR ready for initial maintainer review. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-01 21:45:54 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
morluto/rea#7171
No description provided.