mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-25 12:11:49 +08:00
feat/https-compose-caddy
2
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
24acbe8f32 |
fix(openclaw): record turn advancements as one marker file per key (#4212)
## Description
OpenClaw 2026.9.9 started calling the context engine's `commitTurn`
after every accepted turn. While a commit is pending, it runs every
later turn of that session on its legacy context path, which means no
Headroom compression. The plugin's turn-commit store (from #3442) has
three ways to leave a commit pending, and this PR removes all three.
Each committed key becomes one small marker file, created with exclusive
creation (`O_CREAT | O_EXCL`). There's no lock and no shared file to
rewrite, and only the key is stored.
**What we saw.** We run the plugin on Kubernetes with a read-only root
filesystem. After upgrading OpenClaw to 2026.9.9, every turn logged:
```
[context-engine] durable turn advancement remains queued: <key>: ENOENT: no such file or directory, mkdir '/home/openclaw/.headroom'
```
On the session's next turn, OpenClaw retries the pending commit before
assembling context. When the retry fails, it degrades that turn to the
legacy engine (`degradeBeforeStart("pending durable turn advancement
could not be completed before the next turn")` in OpenClaw's
`context-engine-turn-attempt`). A failed commit only increments
`attempt_count`, so the row stays pending and every later turn of the
session degrades too.
**The three problems in `advancement-key-store.ts`:**
1. **Read-only home.** The default
`~/.headroom/openclaw/commit-log.json` can't be created on a read-only
root. Node reports the failed `mkdir` as `ENOENT`, which reads like a
bug rather than a setting. `commitLogPath` exists on the engine but not
in the manifest's `configSchema` (`additionalProperties: false`), so
`HEADROOM_WORKSPACE_DIR` is the only way to move it.
2. **Unbounded growth.** Every accepted turn's `messages` (OpenClaw caps
a turn at 8 MiB) are kept forever in one JSON file. Each commit
re-reads, parses, copies and rewrites the whole file on the gateway's
event loop, so the file and each commit's latency grow with every turn
ever committed. Nothing reads the messages back (`get` is test-only),
and OpenClaw's transcript already holds them.
3. **A lock nothing reclaims.** By design, the lock is never
force-broken. A gateway killed mid-commit (an OOM kill, a node drain)
leaves `commit-log.json.lock` behind, and every later commit waits 60 s
and then fails until someone deletes it. With the retry above, every
turn then starts 60 s late before degrading.
## Type of Change
- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [x] Documentation update
- [x] Performance improvement
- [ ] Code refactoring (no functional changes)
## Changes Made
- **One marker per committed key**, at
`commit-log.d/<sha256[0:2]>/<sha256(key)>` next to the old log path,
created with `open(path, "wx")`. The first creator in any process gets
`"committed"`; every later caller gets `EEXIST`, i.e. `"duplicate"`.
Only that exclusive create can mean `"duplicate"`, and only when what
exists at the path is a marker file. An `EEXIST` from creating the shard
directory (a file where the directory belongs), or anything but a file
at the marker's path, is a storage error. The marker holds
`{advancementKey, committedAt}`: 99 bytes for OpenClaw's UUID keys.
- **Only the key is recorded.** For this engine, the advancement *is*
the key: `assemble()` works from the messages OpenClaw passes in, and
the plugin keeps no turn state. The contract asks for "one atomic,
idempotent write keyed by `advancementKey`" (OpenClaw
`docs/concepts/context-engine.md`), and creating the marker is that
write.
- **Keys are still never evicted**, for the reason #3442 gives.
- **Migration.** Before every commit and lookup, a store checks for a
`commit-log.json` from an earlier version. With no log there, that check
is one failed `open()`. If a log is there and it's a version the store
hasn't migrated, the store moves its keys into markers and records which
version that was (size, mtime, inode) in
`commit-log.d/migrated-legacy-log.json`. That record only saves work, so
a store that can't write it carries on and reads the log again later.
The log's version and its contents come from one open handle. The log
itself is never moved or changed, so an older version still running
against the same workspace keeps its own record, and nothing has to
write to the log's directory (which may be read-only, with a writable
marker volume mounted beneath it). An unchanged log is skipped by its
metadata alone, even if it can no longer be read. A changed one is
migrated again, so a key an older version commits at any moment, even
while a store is migrating the log, is never accepted as new. Only keys
the store hasn't seen get markers, so a rewrite costs only the keys it
adds. Both the object and the older array format are read. Several
processes can migrate at once safely, since creating an existing marker
is a no-op. A corrupt log still fails loudly rather than being read as
empty, and the store recovers once it's fixed.
- **An actionable error.** When the marker directory can't be created,
the message says what to do: `Cannot record turn advancements under
<dir>: <error>. Point HEADROOM_WORKSPACE_DIR (or the plugin's
commitLogPath) at a writable directory.` A lookup in a store whose path
a file blocks fails the same way on every platform, although Windows
reports such a path as `ENOENT`, which is also what a missing marker
gives.
- **`commitLogPath` is settable.** It's declared in
`openclaw.plugin.json`, with a UI hint and a README row.
- **Tests.** The store suite is rewritten for the new design. It keeps
the existing guarantees: committed, then duplicate; survives a restart;
never evicts across 600 keys; exactly one winner in-process and across
two OS processes; a failed write is retried, not remembered. New tests
cover:
- a commit stores one small marker holding only the key;
- committing more keys never rewrites an earlier marker (same inode and
mtime);
- a leftover `.lock` from a crashed process doesn't block a commit;
- a file where the marker directory or a shard directory belongs, and a
directory where a marker belongs: each is an error, never `"duplicate"`,
and the same key commits once the path is cleared;
- a file blocking the store's directory or its marker directory,
reported the way Windows reports it (`ENOENT`, not `ENOTDIR`): a lookup
is a storage error, not an absent key;
- both legacy formats, including two stores migrating concurrently;
- a legacy log in a read-only directory: commits go through; a restart
doesn't read the unchanged log again; a migrated log that can no longer
be read doesn't block commits; and once the directory is writable, a key
an older version commits the old way (temp file + rename) is migrated
before a running store's next commit;
- a key an older version commits while a store is migrating the log: the
store's next lookup finds it, and its next commit, like a new store's,
is `"duplicate"`;
- an older version rewriting the log: only the key it added gets a
marker;
- a migration record that can't be written: commits go through, in that
store and in one started later;
- a corrupt legacy log.
In `engine.test.ts`, a commit carrying 1 MB of tool output stores under
1 KB. The lock-holder fixture is removed, since there is no lock.
## Testing
- [ ] Unit tests pass (`pytest`) (n/a, TypeScript plugin only)
- [ ] Linting passes (`ruff check .`) (n/a)
- [ ] Type checking passes (`mypy headroom`) (n/a; `npm run typecheck`
passes)
- [x] New tests added for new functionality
- [x] Manual testing performed (see Real Behavior Proof)
### Test Output
```text
$ cd plugins/openclaw && npm run typecheck # exit 0
$ npx vitest run # Node v24.21.0, macOS
Test Files 9 passed (9)
Tests 139 passed (139)
$ npx vitest run # Node v22.22.0, GitHub Actions windows-latest
Test Files 9 passed (9)
Tests 137 passed | 2 skipped (139)
```
## Real Behavior Proof
- Environment: macOS, Node v24.21.0, for the benchmark below: "old" is
`advancement-key-store.ts` from `main`, "new" is this PR, and the same
script drives both. In production: OpenClaw 2026.9.9 on Kubernetes with
a read-only root filesystem and `HEADROOM_WORKSPACE_DIR` on a writable
volume, running the plugin at v0.40.0 from a fork tag, with
`advancement-key-store.ts` identical to
|
||
|
|
a28dd4eab7 |
fix(openclaw): declare transcriptSemantics and implement commitTurn (#3442)
## Description Adds OpenClaw transcript-semantics declarations and an accepted-turn log for admitted-turn advancement. Fixes #3441. Messages and advancement keys are written together in `HEADROOM_WORKSPACE_DIR/openclaw/commit-log.json` (default workspace: `~/.headroom`). Each commit re-reads the log inside an exclusive sibling-file lock and replaces the log atomically. Keys are retained without eviction; prior key-only logs are recognized for migration. Failed writes do not create an in-memory phantom duplicate. ## Locking and recovery The lock serializes processes sharing the workspace and is never reclaimed merely because it is old. A waiting caller times out after 60 seconds instead of committing without ownership. Release checks the acquisition token. A crashed holder can leave an orphan `.lock` file. Recovery is operational: stop writers using that workspace, establish that the holder is no longer running, remove the orphan lock, then resume. Do not delete a live writer's lock. Automatic crash recovery and power-loss durability are not claimed. ## Testing - 99 plugin tests pass, including distinct-key and same-key child-process commits and an aged live-lock regression. - TypeScript checking passes. - The aged-lock regression fails against the prior age-reclaim implementation and passes against the current source. Test cleanup releases the child even on assertion failure. - Current-head CI is pending. ## Real Behavior Proof - Environment: macOS, two real Node child processes importing `DurableAdvancementKeyStore`. - Exact command / steps: A acquires the lock and reads the empty log; pause A while alive for 31 seconds. Start B committing another key and verify it remains pending, then resume A. - Observed result: B does not finish while A holds the lock. After release, both return `committed` and both message payloads remain in the log. The prior implementation acknowledged both but lost one turn. - Not tested: automatic recovery from crashed holders (requires the operator procedure above); power-loss durability. ## Runtime Rollout Safety - Rollout-managed feature(s): OpenClaw transcript-semantics declaration. - Minimum rollout channel: Host capability negotiation. - Stable/default behavior changed: Accepted messages and keys persist in a workspace-shared log. The log grows without eviction, and an orphan lock requires operator recovery. - Kill switch / disable path: Revert the plugin change or use the host's legacy context-engine path. - Unsafe override required: No. - Qualification impact: Admission uses atomic/idempotent commit behavior; contention fails closed. - Rollback path: Revert the plugin change. Preserve the log if retry history must remain available. ## Review Readiness - [x] Prior message persistence, retry, cross-process, and stale-lock findings resolved. - [x] Local tests and type checking pass. - [ ] Current-head CI complete. - Final human review is required. --------- Signed-off-by: Radhakrishnan P <gingeekrishna@gmail.com> Co-authored-by: Jerrett Davis <mxjerrett@gmail.com> |