Skip to content

fix(memory): budget the rendered view and publish merges at once - #23

Open
PermutationResearch wants to merge 3 commits into
ericjuta:mainfrom
PermutationResearch:main
Open

PermutationResearch wants to merge 3 commits into
ericjuta:mainfrom
PermutationResearch:main

Conversation

@PermutationResearch

Copy link
Copy Markdown

What this fixes

Memory.size() budgeted summary node bytes, but render() sends the <chat> wrapper plus
${start}+${2**level}|${text} per line. The view the model actually receives was therefore larger
than the configured viewBytes, and the batch gate never fired at the right moment.

The shared conformance fixture (docs/company-os/optchat.v1.conformance.json, generated from the
reference memory-core.mjs by scripts/optchat-conformance-fixture.mjs) pins the correct semantics.
Before this change optchat matched 49/95 scenario steps; pi-durable matched 95/95.

Behaviour changes

  1. Byte accounting is the rendered view. size() = CHAT_WRAPPER_BYTES + the rendered bytes of
    every line (including its trailing newline). lineBytes() is now the single place that answers
    "how many bytes does this part cost in the prompt".
  2. Merges publish immediately. A built parent replaces its two children in the published tiling at
    once, instead of being held until the whole batch reaches compactionViewBytes. The batch stays
    compacting while it is over target and a due sibling pair exists, which is what the reference
    implementation does.
  3. Eager publish costs prompt-cache fidelity. Holding merges back kept the published prefix stable
    for longer, so the provider cache hit more often. The contract requires immediate publish, so this
    trades some cache reuse for a view that is correct at every step. Worth knowing before merging.
  4. mostDuePair() is exported and used by coarsen(). The same ranking rule is duplicated in
    companyos-durable/memory-core.mjs and companyos-pi-durable/src/optchat/memory.ts; exporting it
    here keeps the three ports comparable and lets the conformance test pin the rule directly.

Tests

New tests/conformance.test.ts replays the shared fixture (95 append/build steps, byte-exact view
tiling, rendered bytes and target per step) and the 5 due-pair ranking cases. Existing memory,
memory-endpoint and store tests were updated to the rendered-byte contract — those assertions encoded
the old node-byte budgeting, so they were wrong rather than newly broken.

bun run typecheck            # clean
bun test tests/conformance.test.ts tests/memory.test.ts
# 113 pass, 0 fail, 940 expect()

Not covered here

The full bun test run still shows ~196 failures in this environment, all in suites that spawn a
fixture HTTP server on port 0 and fail with EADDRINUSE (tests/host/integration.test.ts and the
gates/evidence suites that share startCompressorServer) plus git init returning 128 in the
evidence isolation helpers. Those are environment failures, not regressions from this change, but
they mean the host-integration path is unverified on this machine.

Also included (separate commit, easy to drop)

scripts/oc: the wrapper I run OMP with — PI_OPENAI_STATEFUL=0 (no server-side carryover, OptChat
rebuilds the view each turn), memory tools on, only the rules judge gate on. It resolves symlinks so
it works installed as ~/.local/bin/oc, and finds omp via $OMP_BIN, PATH, then the Tern install
path. Say the word and I will split it into its own PR.

Verification beyond unit tests

Live runs with the fixture in place: oc --no-session -p "…" on a clean directory produces
optchat-dirs/<id>/{main,tree,view.json,metrics.jsonl}, and the rules gate writes
{"type":"rules","pRule":1,"added":true} plus a ledger.jsonl entry that a later fresh process can
quote back verbatim.

- size() counts the <chat> wrapper and every id+n|text line, not the node's own text.
- A batch starts only when every view line is built and the view is over budget; it merges the most due pairs down to half.
- A merged parent publishes immediately; compacting stays until the view fits the target with a sibling pair left.
- mostDuePair is exported and used by coarsen, so the ports share one ranking.
- Add the shared OptChat v1 conformance fixture as a test: 95 scenario steps plus 5 ranking cases.
- PI_OPENAI_STATEFUL=0: no server-side carryover, OptChat rebuilds the view each turn
- memory tools on; only the rules judge gate left enabled
- extension path resolves through symlinks, OMP_BIN/PATH fallback for the host binary
- A built parent now publishes its view inside the build, so the three
  summary crash stages kill with two acked records in the main log, not three.
- Follows the batch rule from the conformance fixture.
@PermutationResearch

Copy link
Copy Markdown
Author

CI is green at head 5bc1c07 on the fork: https://github.com/PermutationResearch/omp-optchat/actions/runs/46317411041 — all four jobs success (verify pinned + latest SDK, verify lease Linux model+real processes, verify lease macOS real+store suites).

The upstream run on this PR sits at action_required (a maintainer has to approve the first run for a fork).

Local replay of ci.yml on the fork head: typecheck clean, lease job green, 2609 tests pass. The only failures are 12 tests/status.test.ts cases asserting the status output does not contain /private/, which is macOS-only (they fail identically at 0a860d8, the current main), plus 3 tests/crash.test.ts summary-stage failures caused by this branch's own change — fixed in 5bc1c07.

I don't have write access to this repository (both accounts on my side are read-only), so I can't merge. Ready when you are.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants