Files
PaperClipAI/packages/adapter-utils/src
d1cdd217a2 fix(adapter-utils): give hermes_local real session-rotation thresholds (#17199)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - It keeps a session for each agent. Some agents use the same session
for many runs.
> - The session compaction feature rotates a session when it grows too
large.
> - The rotation function asks the adapter registry for a limit for each
adapter type.
> - The registry gave hermes_local a policy where all limits were 0.
> - The function read those 0 values and decided that no limit exists.
It never rotates.
> - So a new hermes agent starts with no rotation limit unless someone
adds a manual override.
> - hermes does not compress its own context well on large sessions. Its
compression stops.
> - A stalled compression uses half of the run budget and the run then
fails.
> - This pull request gives hermes_local its own rotation policy with
real limits.
> - The benefit is that every new hermes agent is protected by default.

## Linked Issues or Issue Description

No public GitHub issue exists for this defect. This is the defect
report.

**What happened?**

The registry marks `hermes_local` as `nativeContextManagement:
"confirmed"`. It gives the type `ADAPTER_MANAGED_SESSION_POLICY`. Every
limit in that policy is 0. The function
`hasSessionCompactionThresholds()` returns false for it. The server then
never rotates the session.

**Expected behavior**

A new `hermes_local` agent should start with working rotation limits. A
developer should not need to add
`runtimeConfig.heartbeat.sessionCompaction` to each agent by hand.

**Steps to reproduce**

1. Create an agent with adapter `hermes_local`. Do not set any session
policy.
2. Call `resolveSessionCompactionPolicy("hermes_local", undefined)`.
3. Read `hasSessionCompactionThresholds(result.policy)`.
4. The result is `false`. The server returns `rotate: false` on every
wake.

**Agent adapter(s) involved**

`hermes_local`. The change does not affect `claude_local`,
`codex_local`, or `acpx_local`.

**Relevant logs or output**

hermes reports this in its own log:

```
run_agent: Context compression made no progress for 300.0s (total wait 300.0s, ceiling 600.0s); continuing without compression
```

The session row records the same event:

```
compression_failure_error: backoff:stall_interrupted:strategy=lean:msgs=1297:tokens=346650
```

On one measured fleet, 52 unarchived hermes sessions reached 400 or more
messages. They hold 33,894 messages and 854,572,069 cumulative input
tokens.

**Additional context**

On that fleet, agents that have a manual override rotate correctly
today. This change only protects agents that are created after it lands.

## What Changed

- Add `HERMES_SESSION_COMPACTION_POLICY` to
`packages/adapter-utils/src/session-compaction.ts`.
- Point
`ADAPTER_SESSION_MANAGEMENT.hermes_local.defaultSessionCompaction` at
that policy.
- Add a test file with four cases. It covers the default, the other
adapters, an agent override, and a full opt out.

The new policy is:

```ts
const HERMES_SESSION_COMPACTION_POLICY: SessionCompactionPolicy = {
  enabled: true,
  maxSessionRuns: 12,
  maxRawInputTokens: 500_000,
  maxSessionAgeHours: 168,
};
```

`maxSessionRuns` and `maxSessionAgeHours` work together across cadences.
An agent that runs about four times a day reaches 12 runs in about three
days. A daily agent reaches the 168 hour limit first.

`maxRawInputTokens` compares only the latest run. For hermes it is a
backstop. `maxSessionRuns` is the limit that acts first.

## Verification

Run these commands from the repository root.

```bash
pnpm install --frozen-lockfile
pnpm --filter @paperclipai/plugin-sdk build
npx tsc --noEmit -p packages/adapter-utils
npx vitest run packages/adapter-utils/src/__tests__/session-compaction.test.ts \
  packages/shared/src/validators/adapter-registry.test.ts \
  server/src/__tests__/adapter-registry.test.ts
```

Results on master at commit `043f2514e`:

| Check | Result |
| --- | --- |
| `tsc --noEmit` in `packages/adapter-utils` | clean |
| `packages/adapter-utils/src/__tests__/session-compaction.test.ts` | 4
passed |
| `server/src/__tests__/adapter-registry.test.ts` | 33 passed |
| `packages/shared/src/validators/adapter-registry.test.ts` | 4 passed |

CI runs the full suite. It uses `pnpm test:run:general`, the serialized
server shards, `typecheck:build-gaps`, `build`, and the end to end
tests.

One test fails in the sandbox. It is
`heartbeat-workspace-session.test.ts` →
`assertGitWorktreeBaseWorkspaceReady` → `marks a proven non-Git local
path as an owner configuration conflict without changing the failure`.
The same test fails on unmodified master. It does not relate to this
change.

## Risks

The change is low risk. It does not alter a shared default.

- Only `hermes_local` changes. The shared
`ADAPTER_MANAGED_SESSION_POLICY` is unchanged.
- A hermes agent that runs about four times a day rotates after about
three days. Its context is then rebuilt from a handoff note.
- Any agent can still set `runtimeConfig.heartbeat.sessionCompaction`
and override the policy.
- Any agent can set `enabled: false` and stop rotation.
- There is no migration. No stored data changes.

Reviewers should compare this change with PR 3905. That PR is open. It
sets limits for `codex_local`. It edits the same file. The two changes
touch different adapter keys.

## Model Used

The original fix was written with Anthropic Claude Opus 4.8. It used a
1M context window.

The change was later moved onto current master by an OpenClaw agent
runtime. That runtime used the model `minimax-portal/MiniMax-M3`. It
used shell access, git, the GitHub API, and the test runner. It did not
use a separate reasoning mode.

## Related and Prior Work

- PR 3905 is open. It sets limits for `codex_local`. This change does
the same job for `hermes_local`.
- PR 7902 is closed. It tried to stop unbounded session growth in the
shared policy. That approach would rotate adapters that compress
correctly. This change gives hermes its own policy instead.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [ ] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [ ] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: ruai-engineering-lead <engineering-lead@ruah.innovations>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-authored-by: Head of Engineering <engineering@ruah-innovations.local>
2026-12-05 00:06:22 -08:00
..