mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-26 01:39:51 +01:00
## Thinking Path
> - Paperclip agents take work from assigned tasks and from user
comments.
> - A fresh task run can include saved comments when the current agent
or a confirmed former assignee owns them.
> - Connected chat messages and resolved interactions have separate
authority and session requirements.
> - Heartbeat previously decided which receipts to adopt between the
three calls that saved a new run.
> - One wake-queue use case now selects the receipts and commits the
same run and comment lineage.
## Linked Issues or Issue Description
**What existing behavior does this improve?**
Assignment and comment continuation when a task needs a fresh run.
**Subsystem affected**
Server task admission.
**Current behavior**
Heartbeat chooses the comment receipts to consume, reads their source,
and builds the final run context.
**Proposed behavior**
One application operation admits the run. Its Postgres adapter loads
eligible company and agent receipts under the issue lock. The caller
supplies the responsible user and records the specialized continuation.
**Additional context**
Builds on #22266. The existing release and queued-comment boundaries are
in #13136 and #13145.
## What Changed
- Keep exact resumed receipts separate from ordinary board comments.
Adopt a prior assignee’s comments only when the interrupted run proves a
handoff.
- Use the same canonical comment ID codecs for admission and dispatch.
- Test the assignment path with one or two saved comments and
independent chat and interaction receipts.
## Verification
On c957f8529c, 260 tests passed across the four admission, liveness and
responsible-user suites. The saved Grok interrupt route passed its
exactly-once continuation test. A separate Postgres run passed the
ordered comment and rollback checks, both reassignment cases, and the
company isolation test. Server typecheck and module boundaries passed.
Check the fresh admission and handoff cases in
`server/src/services/heartbeat/queue.test.ts`. Run `pnpm exec vitest run
server/src/__tests__/heartbeat-comment-wake-batching.test.ts
server/src/services/issue-queued-comment-queue.test.ts`, `pnpm --filter
@paperclipai/server typecheck`, and `node
scripts/check-module-boundaries.mjs`.
## Risks
Receipt selection controls which user input reaches the next agent. The
store requires the same company, issue and executor or a verified
handoff. Chat and interaction receipts preserve their independent
delivery.
## Model Used
OpenAI GPT-6.2 Sol (`gpt-6.2-sol`) implemented the change. OpenAI Codex
directed the work, reviewed the diff, and ran qualification.
## 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
- [x] I have updated relevant documentation to reflect my changes
- [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: Paperclip <noreply@paperclip.ing>
145 lines
6.0 KiB
TypeScript
145 lines
6.0 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
import type { IssueComment, IssueQueuedCommentQueue } from "@paperclipai/shared";
|
|
|
|
export {
|
|
queuedCommentIdsFromRunContext,
|
|
queuedCommentIdsFromWakePayload,
|
|
withQueuedCommentIdsInRunContext,
|
|
withQueuedCommentIdsInWakePayload,
|
|
} from "../lib/queued-comment-context.js";
|
|
|
|
function record(value: unknown): Record<string, unknown> {
|
|
return typeof value === "object" && value !== null && !Array.isArray(value)
|
|
? value as Record<string, unknown>
|
|
: {};
|
|
}
|
|
|
|
/**
|
|
* Fingerprints one queued-comment queue: the wake id plus every comment's id
|
|
* and last-updated time. A mutation that changes the queue changes this
|
|
* value, so a caller can echo it back to detect a queue it no longer holds
|
|
* the latest view of. The read path (`GET /queued-comments`) and every queue
|
|
* mutation must call this same function, so a client's fingerprint always
|
|
* compares against the same computation.
|
|
*/
|
|
export function queuedCommentQueueRevision(input: {
|
|
queueId: string | null;
|
|
comments: Array<{ id: string; updatedAt: Date }>;
|
|
}): string {
|
|
return createHash("sha256")
|
|
.update(JSON.stringify({
|
|
queueId: input.queueId,
|
|
comments: input.comments.map((comment) => [comment.id, comment.updatedAt.toISOString()]),
|
|
}))
|
|
.digest("hex")
|
|
.slice(0, 32);
|
|
}
|
|
|
|
export type QueuedCommentQueueProtocol = "paperclip_runner_v1" | "legacy";
|
|
|
|
export type QueuedCommentQueueSteeringDecision =
|
|
| { protocol: QueuedCommentQueueProtocol; kind: "unsupported" }
|
|
| { protocol: QueuedCommentQueueProtocol; kind: "temporarily_unavailable" }
|
|
/** Only the caller can probe the live runner. `steeringRunId` names the run to probe. */
|
|
| { protocol: "paperclip_runner_v1"; kind: "probe"; steeringRunId: string };
|
|
|
|
/**
|
|
* Decides the queue protocol and the steering answer for one queued-comment
|
|
* queue, from plain facts. This is the one place that resolves the
|
|
* `paperclip_runner_v1`/`legacy` protocol and the steering answer; every
|
|
* caller that builds a queue response must call this function instead of
|
|
* repeating the rule.
|
|
*
|
|
* When the decision is `"probe"`, only the caller can answer the question:
|
|
* it must ask the live runner (through a call such as
|
|
* `getNativeSessionSteeringState`) and fall back to
|
|
* `"temporarily_unavailable"` on failure. A caller that never probes the
|
|
* live runner must answer `"temporarily_unavailable"` for a `"probe"`
|
|
* decision instead.
|
|
*/
|
|
export function decideQueuedCommentQueueSteering(facts: {
|
|
state: "deferred" | "queued" | null;
|
|
/** The queued run's own runtime mode. Read only when `state` is `"queued"`. */
|
|
queueRunRuntimeMode: string | null;
|
|
/** The currently running turn, if any. Read only when `state` is `"deferred"`. */
|
|
activeRun: { id: string; runtimeMode: string | null; runtimeModeResolvedAt?: Date | null; runnerProfileJson?: Record<string, unknown> | null } | null;
|
|
assignedAgentAdapterType: string | null;
|
|
queuedCommentCount: number;
|
|
}): QueuedCommentQueueSteeringDecision {
|
|
// The default legacy value is not a selection receipt during preparation.
|
|
// Use the run's immutable dispatch choice, never the agent's mutable settings.
|
|
const preparingNative = facts.state === "deferred" && facts.activeRun?.runtimeMode === "legacy" &&
|
|
facts.activeRun.runtimeModeResolvedAt === null &&
|
|
record(facts.activeRun.runnerProfileJson?.adapterDispatch).adapterType === "paperclip_runner";
|
|
const persistedRuntimeMode = preparingNative ? null :
|
|
facts.state === "queued"
|
|
? facts.queueRunRuntimeMode
|
|
: facts.state === "deferred"
|
|
? facts.activeRun?.runtimeMode ?? null
|
|
: null;
|
|
|
|
const protocol: QueuedCommentQueueProtocol =
|
|
preparingNative || persistedRuntimeMode === "native"
|
|
|| (persistedRuntimeMode === null && facts.assignedAgentAdapterType === "paperclip_runner")
|
|
? "paperclip_runner_v1"
|
|
: "legacy";
|
|
|
|
if (protocol !== "paperclip_runner_v1") {
|
|
return { protocol, kind: "unsupported" };
|
|
}
|
|
|
|
const steeringRun = facts.state === "deferred" ? facts.activeRun : null;
|
|
if (!steeringRun || preparingNative || facts.queuedCommentCount === 0) {
|
|
return { protocol, kind: "temporarily_unavailable" };
|
|
}
|
|
|
|
return { protocol, kind: "probe", steeringRunId: steeringRun.id };
|
|
}
|
|
|
|
type QueuedCommentQueueEntryFacts = {
|
|
id: string;
|
|
updatedAt: Date;
|
|
authorUserId: string | null;
|
|
authorAgentId: string | null;
|
|
};
|
|
|
|
/**
|
|
* Assembles the shared `IssueQueuedCommentQueue` response from the already
|
|
* resolved facts: the queue identity, the already-decided protocol and
|
|
* steering answer, the live comment rows, and the actor who reads the
|
|
* queue. This is the one place that builds the response shape; every
|
|
* caller that builds a queue response must call this function instead of
|
|
* repeating the rule.
|
|
*/
|
|
export function buildQueuedCommentQueueSnapshot<TComment extends QueuedCommentQueueEntryFacts>(facts: {
|
|
issueId: string;
|
|
queueId: string | null;
|
|
state: "deferred" | "queued" | null;
|
|
/** The currently running turn's id. Read only when `state` is `"deferred"`. */
|
|
activeRunId: string | null;
|
|
protocol: QueuedCommentQueueProtocol;
|
|
steeringDisposition: IssueQueuedCommentQueue["steeringDisposition"];
|
|
comments: TComment[];
|
|
executionWait?: IssueQueuedCommentQueue["executionWait"];
|
|
actorType: "agent" | "user";
|
|
actorId: string;
|
|
}): IssueQueuedCommentQueue {
|
|
return {
|
|
issueId: facts.issueId,
|
|
queueId: facts.queueId,
|
|
state: facts.state,
|
|
targetRunId: facts.state === "deferred" ? facts.activeRunId : null,
|
|
revision: queuedCommentQueueRevision({ queueId: facts.queueId, comments: facts.comments }),
|
|
protocol: facts.protocol,
|
|
steeringDisposition: facts.steeringDisposition,
|
|
...(facts.executionWait ? { executionWait: facts.executionWait } : {}),
|
|
entries: facts.comments.map((comment, position) => ({
|
|
comment: comment as unknown as IssueComment,
|
|
position,
|
|
canEdit: facts.actorType === "user" && comment.authorUserId === facts.actorId,
|
|
canDiscard: facts.actorType === "user" &&
|
|
(comment.authorUserId === facts.actorId || Boolean(comment.authorAgentId)),
|
|
})),
|
|
};
|
|
}
|