Files
PaperClipAI/server/src/__tests__/heartbeat-timer-wake-session-reset-pf4.test.ts
T
santastabberandsantastabber 8774909361 fix(heartbeat): reuse sessions across execution handoffs (#9917)
## Thinking Path

> - Paperclip uses durable task sessions so local adapters can resume
work across sequential heartbeat runs.
> - `execution_review_requested` and `execution_changes_requested` are
issue-local execution-policy handoffs, not new task assignments.
> - The existing `agent_task_sessions` lookup, adapter session codec,
workspace resolution, and effective config freshness checks already
decide whether reuse is safe.
> - Treating those two handoff wake reasons as unconditional
fresh-session boundaries discards a valid saved task session before
adapter resume can be attempted.
> - This makes Dev → CodeReview → Dev loops repeatedly cold-start even
when task, issue, agent, adapter, workspace, and config identity are
unchanged.
> - The fix is to let normal review/change-request handoffs reach the
durable task-session path while preserving explicit fresh-session and
unsafe-boundary resets.

## Linked Issues or Issue Description

Fixes #8246.

cc @cryppadotta — this is the narrow handoff-session policy change
discussed there: normal `execution_review_requested` /
`execution_changes_requested` wakes no longer force a fresh task session
by wake reason alone, while assignment, approval, review-participant
recovery, timer wakes, explicit `forceFreshSession`, and
config/workspace/model/session freshness still keep their safety
boundaries.

## What Changed

- Removed normal `execution_review_requested` and
`execution_changes_requested` from the unconditional task-session reset
policy.
- Kept fresh-session boundaries for:
  - `issue_assigned`
  - `execution_approval_requested`
  - `execution_review_participant_recovery`
  - `heartbeat_timer`
  - explicit `forceFreshSession`
  - existing config/model/workspace/session freshness reset paths
- Updated heartbeat session-policy tests so execution handoffs are
resume-eligible by wake reason alone.
- Preserved PF-4 timer-wake behavior and its explicit reset reason.

## Verification

- `npx pnpm@9.15.4 exec vitest run
server/src/__tests__/heartbeat-workspace-session.test.ts
server/src/__tests__/heartbeat-timer-wake-session-reset-pf4.test.ts
server/src/__tests__/codex-local-execute.test.ts
server/src/__tests__/issue-comment-reopen-routes.test.ts
--reporter=verbose` — 220 tests passed.
- `npx pnpm@9.15.4 --filter @paperclipai/server typecheck` — passed.
- `git diff --check` — passed.
- `coderabbit review --agent -t committed --base origin/master` — 0
findings.

## Risks

- Moderate behavior change in session-boundary policy: normal
review/change-request handoffs may now reuse a saved per-task session
when the existing identity/freshness checks pass.
- Safety boundaries remain in place for new assignments, approval gates,
review-participant recovery, timer/discovery wakes, explicit
fresh-session requests, and config/model/workspace/session drift.
- If a saved session is stale or incompatible, existing freshness/resume
fallback behavior still handles reset/fresh execution.

> For core feature work, check ROADMAP.md first and discuss it in #dev
before opening the PR. Feature PRs that overlap with planned core work
may need to be redirected — check the roadmap first. See
CONTRIBUTING.md.

## Model Used

OpenAI GPT-5.5. Tool use and local verification were enabled.

## 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
- [x] My branch name describes the change and contains no internal
Paperclip ticket ID
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes — N/A,
server policy/test-only change
- [x] I have considered and documented any risks above
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

Co-authored-by: santastabber <184111696+santastabber@users.noreply.github.com>
2026-08-17 14:30:47 -07:00

145 lines
5.4 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
describeSessionResetReason,
shouldResetTaskSessionForWake,
} from "../services/heartbeat.ts";
// PF-4 keeps exploratory timer wakes fresh to avoid low-value context growth.
// Issue-scoped timer wakes are continuation work and should reuse the task
// session so orchestration does not repay session-start/re-orientation cost.
describe("PF-4 shouldResetTaskSessionForWake", () => {
it("resets the session for an unscoped heartbeat_timer wake", () => {
expect(
shouldResetTaskSessionForWake({
source: "scheduler",
reason: "interval_elapsed",
wakeReason: "heartbeat_timer",
}),
).toBe(true);
});
it("reuses the task session for an issue-scoped heartbeat_timer wake", () => {
expect(
shouldResetTaskSessionForWake({
source: "scheduler",
reason: "interval_elapsed",
wakeReason: "heartbeat_timer",
issueId: "issue-1",
}),
).toBe(false);
});
it("keeps assignment, approval, and review-participant recovery as fresh boundaries", () => {
for (const wakeReason of [
"issue_assigned",
"execution_approval_requested",
"execution_review_participant_recovery",
] as const) {
expect(shouldResetTaskSessionForWake({ wakeReason })).toBe(true);
}
});
it("does not reset execution handoffs by wake reason alone", () => {
for (const wakeReason of [
"execution_review_requested",
"execution_changes_requested",
] as const) {
expect(shouldResetTaskSessionForWake({ wakeReason })).toBe(false);
}
});
it("still respects forceFreshSession === true", () => {
expect(shouldResetTaskSessionForWake({ forceFreshSession: true })).toBe(true);
});
it("does not reset for issue_commented (preserve continuation context)", () => {
expect(shouldResetTaskSessionForWake({ wakeReason: "issue_commented" })).toBe(false);
});
it("does not reset for transient_failure_retry (resume in-flight work)", () => {
expect(shouldResetTaskSessionForWake({ wakeReason: "transient_failure_retry" })).toBe(false);
});
it("does not reset for unknown wake reasons", () => {
expect(shouldResetTaskSessionForWake({ wakeReason: "unknown_reason" })).toBe(false);
});
it("does not reset when context is null/undefined", () => {
expect(shouldResetTaskSessionForWake(null)).toBe(false);
expect(shouldResetTaskSessionForWake(undefined)).toBe(false);
});
});
describe("PF-4 describeSessionResetReason", () => {
it("describes unscoped heartbeat_timer wakes explicitly so run logs explain the reset", () => {
const reason = describeSessionResetReason({
wakeReason: "heartbeat_timer",
});
expect(reason).toBe("wake reason is heartbeat_timer (unscoped timer wake starts fresh)");
});
it("does not describe an issue-scoped heartbeat_timer wake as a reset", () => {
expect(
describeSessionResetReason({
wakeReason: "heartbeat_timer",
issueId: "issue-1",
}),
).toBeNull();
});
it("returns reasons for wake reasons that still force a fresh task session", () => {
expect(describeSessionResetReason({ wakeReason: "issue_assigned" })).toBe(
"wake reason is issue_assigned",
);
expect(describeSessionResetReason({ wakeReason: "execution_approval_requested" })).toBe(
"wake reason is execution_approval_requested",
);
expect(
describeSessionResetReason({ wakeReason: "execution_review_participant_recovery" }),
).toBe("wake reason is execution_review_participant_recovery");
});
it("does not report review/change-request handoffs as reset reasons", () => {
expect(describeSessionResetReason({ wakeReason: "execution_review_requested" })).toBeNull();
expect(describeSessionResetReason({ wakeReason: "execution_changes_requested" })).toBeNull();
});
it("returns the forceFreshSession message when explicitly requested", () => {
expect(describeSessionResetReason({ forceFreshSession: true })).toBe(
"forceFreshSession was requested",
);
});
it("returns null for non-resetting wake reasons", () => {
expect(describeSessionResetReason({ wakeReason: "issue_commented" })).toBeNull();
expect(describeSessionResetReason({ wakeReason: "transient_failure_retry" })).toBeNull();
expect(describeSessionResetReason({ wakeReason: "unknown_reason" })).toBeNull();
expect(describeSessionResetReason(null)).toBeNull();
expect(describeSessionResetReason(undefined)).toBeNull();
});
it("agrees with shouldResetTaskSessionForWake on every input — non-null reason iff should reset", () => {
const cases: Array<Record<string, unknown> | null | undefined> = [
{ wakeReason: "heartbeat_timer" },
{ wakeReason: "heartbeat_timer", issueId: "issue-1" },
{ wakeReason: "issue_assigned" },
{ wakeReason: "execution_review_requested" },
{ wakeReason: "execution_approval_requested" },
{ wakeReason: "execution_review_participant_recovery" },
{ wakeReason: "execution_changes_requested" },
{ forceFreshSession: true },
{ wakeReason: "issue_commented" },
{ wakeReason: "transient_failure_retry" },
{ wakeReason: "unknown_reason" },
null,
undefined,
];
for (const ctx of cases) {
const shouldReset = shouldResetTaskSessionForWake(ctx);
const reason = describeSessionResetReason(ctx);
expect(Boolean(reason)).toBe(shouldReset);
}
});
});