Files
PaperClipAI/packages/adapter-utils
Alex Castillo González 5fb030f59f fix(heartbeat): keep the newest comments when a wake window is cut (#19969)
Fixes #19198

## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - A heartbeat builds a wake payload so the agent can read the latest
issue comments
> - The inline window holds at most 8 comments and 12000 characters of
comment text
> - The builder walked comments from the oldest and stopped when a cap
was reached
> - The newest comment, which the prompt asks the agent to acknowledge,
was left out
> - This pull request keeps the newest comments that fit and names the
older ones that were cut
> - The benefit is that a busy agent answers the comment that woke it

## Linked Issues or Issue Description

Fixes: #19198

No open pull request mentions this issue.

## What Changed

- `buildPaperclipWakePayload` fills the inline comment window from the
newest comment back. The caps stay the same: 8 comments, 4000 characters
per body, and 12000 characters in total. The kept comments still render
oldest first.
- The payload records `commentWindow.cutCount`, `cutCommentIds` (at most
20, oldest first), and `cutAttachmentCount`.
- Attachments keep the newest 20. When nothing is cut, their order stays
oldest first. A count query runs only when more than 20 attachments
match.
- The wake prompt prints one `cut comments` line, and one `cut
attachments` line when needed, after `latest comment id`. A wake that
cuts nothing renders the same prompt as before.
- A generic wake still tells the agent to read the cut comments with
`GET /api/issues/<id>/comments`. A native external-chat turn that
already requires `read_current_wake_comments` and zero other Paperclip
API calls names that reader in the cut line instead. That reader pages
every comment id accepted for the run. The full-thread GET stays out of
that prompt.
- The redaction test that checks an exact comment window now expects the
new fields at zero when nothing is cut.

## Verification

Run this command in `server`:

`pnpm exec vitest run
src/services/heartbeat/run-preparation.wake-window.test.ts --testTimeout
60000`

Before this change, all 7 tests failed. An 11-comment wake listed
comments 1–8 and omitted comment 11. Five comments of 4000 characters
kept the first three and dropped the two newest. After this change, all
7 tests pass. The seventh checks that a native external-chat cut names
`read_current_wake_comments` and does not name a `GET`, while the same
payload without the native reader still names the thread `GET`.

The test fixture now derives its row type from the builder input and
uses the schema-valid `comment` origin. Attachment assertions validate
the array shape before reading ids.

`pnpm exec tsc --noEmit -p .` in `server` reports no errors in the new
test file or the changed source files.

## Risks

A wake that already fits in the caps keeps the same prompt text.

A wake that used to cut now shows the newest comments instead of the
oldest ones. The cut line names the omitted comment ids. A generic wake
tells the agent to read `GET /api/issues/<id>/comments`. A native
external-chat wake tells the agent to read those same ids with
`read_current_wake_comments`.

An attachment list longer than 20 now keeps the newest 20 instead of the
oldest 20.

## Model Used

xAI Grok 4.7, with tool use, prepared the original change and test
evidence. OpenAI GPT-6 (Codex), with repository and shell tools,
corrected the test fixture types. Context window size was not exposed to
the agent.

## 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 `#<number>` /
`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

This change was prepared with the assistance of an AI coding agent.
2026-12-05 03:46:55 -08:00
..

@paperclipai/adapter-utils

Shared utilities for Paperclip adapters: process spawning, environment injection, sandbox/SSH transport, workspace sync, and the round-trip helpers that move code between the local execution-workspace cwd and wherever the agent actually runs.

For the adapter-author guide see docs/adapters/creating-an-adapter.md and the in-repo notes at packages/adapters/AUTHORING.md.

Sandbox bridge deadlines

Command-managed bridge control operations (queue reads, input delivery, setup, and cleanup) have a host-enforced deadline of at most 30 seconds per shell command. A shorter configured timeout still applies. These operations cannot inherit the agent run's hours-long lifetime or wait forever on a provider that ignores its timeout. Long-lived agent session commands keep their own limits.

A control timeout reports a transport failure. It does not prove that the remote command stopped and does not replay an uncertain write. The process session closes failed input delivery and uses its existing shutdown path; normal execution settlement must still verify termination.

Native archive transfer deadlines

Native syncIn and syncOut calls accept an optional minimumTimeoutMs budget. The server validates this budget and extends the worker RPC deadline. Providers can honor this field for file transfers without changing their configuration. Daytona uses the larger of this minimum and its configured timeout. Other providers keep their existing behavior. The minimum is limited to 30 minutes and does not change sandbox lifetimes, stored settings or the timeout for other commands.

The remote-workspace archive path uses the verified capture size for downloads and the local archive size for uploads. Large archives get one second per MiB plus 60 seconds, capped at 30 minutes. Small archives keep the existing provider timeout when that calculation is no greater than five minutes. Archive size, checksum, confinement and host space checks still apply.

An exact retained-sandbox resume keeps its recorded provider timeout and RPC overhead. It uses the normal one-minute fallback only when no timeout was recorded. A five-minute cold restore must not be cut off by that fallback. Resume still targets the recorded allocation and does not acquire a replacement.

No-remote-git contract

The local execution-workspace cwd is the only persistence boundary across runs. No adapter may depend on a git remote for cross-run state.

Adapters that run the agent on a different host should use the SSH round-trip helpers in src/ssh.ts:

  • prepareWorkspaceForSshExecution({ spec, localDir, remoteDir }) — bundles the local cwd (tracked files, dirty edits, untracked additions, and the git history needed to reconstruct it) to remoteDir before the run starts. Runs with no git remote configured.
  • restoreWorkspaceFromSshExecution({ spec, localDir, remoteDir, ... }) — syncs the remote cwd back into localDir after the run, including any new commits the agent created. Also runs with no git remote configured.

prepareRemoteManagedRuntime in src/remote-managed-runtime.ts wraps both calls for adapters that want a per-run remote workspace and an automatic restoreWorkspace() finally hook.

The invariant is pinned by the no-remote-git contract case in src/ssh-fixture.test.ts, which asserts that a remote-only commit propagates to the local worktree through the prepare → restore round-trip with no git remote configured at any point. Do not regress that test.