Files
PaperClipAI/server/src/__tests__/agent-chat-messages.test.ts
T
fa6086facb fix(chat): let agents message each other in a person's chats (#22961)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - Agent Chat gives each person one standing chat with each agent.
> - Agents work for the same person, so one agent often must tell
another agent something in that person's chats.
> - The shared comment boundary refused every agent message in a chat
unless the chat's own agent wrote it, and `resume`/`reopen` flags turned
chat posts into 409 conflicts.
> - So one agent could not pass a person's decision to another agent in
that person's chat. The person had to relay it by hand.
> - This pull request rebases #21232 onto current `master` and lets an
agent post in another agent's chat while it acts for the chat's person,
preserving source-task privacy, credential binding, and company
boundaries.
> - The benefit is that agents can talk with each other in the person's
chats, the chat's agent wakes and answers there, and the person sees the
exchange where it happens.

## Linked Issues or Issue Description

**Subsystem affected**

Server (comment-write boundary, comment route, chat directive,
fresh-session handoff), UI (chat transcript adapter), and documentation.

**Problem or motivation**

A person asked one agent to pass a decision to another agent. The first
agent posted the decision in the person's chat with the second agent.
Paperclip refused the post with `"Only this conversation's agent can
post agent messages"`, or returned HTTP 409 when `resume` or `reopen`
was set. The person then had to carry the message by hand.

**Proposed solution**

Rebase and supersede #21232 (whose fork branch is conflicting with
`master` and could not be pushed directly due to fork repository
permissions). Let another agent post in a chat through the ordinary
comment route when it posts from its own running, unrevoked, task-bound
run in the same company, while both the run's responsible person and the
authenticated credential's responsible person match the chat's person,
and the source task passes the company-visible privacy check. Keep every
other guard. Wake the chat's agent through the normal comment wake. Tell
the chat's agent that another agent's message is colleague input, not
the person's authority.

**Alternatives considered**

A destination parameter on `send_chat_message` would add a second
delivery path and new routing authority. The ordinary comment route
already carries attribution, comment permissions, per-run cross-task
limits, and the comment wake, so this change reuses it.

**Roadmap alignment**

This extends the shipped Agent Chat capability in `ROADMAP.md`.
Supersedes #21232 (rebased cleanly onto current `master` with expanded
isolation and handoff tests). Refs #18374, which added the comment
boundary for proactive chat messages. Refs #14143, which asks for
broader group discussions between agents and people; this change does
not add group chats.

## What Changed

- `server/src/services/issues.ts` (+20 net lines; edits remain in the
existing >1,000-line owning `addComment` write boundary rather than
splitting a shared transaction guard): allow a peer agent to post in
another agent's chat only from its own running, unrevoked, task-bound
run in the same company, when both `run.responsibleUserId` and
`actor.onBehalfOfUserId` (when set) match `current.conversationUserId`,
and when the source task passes `canActorReadIssuePrivacy(dbOrTx, {
type: "none" }, source)`. Keep proactive chat delivery and the session
fence restricted to the chat's own agent.
- `server/src/routes/issues.ts` (+0 net lines; edits remain in the
existing >1,000-line owning comment route): ignore `reopen` and `resume`
flags on conversations (`!isConversation(issue)`) because ordinary
comment wakes always reach the chat's agent.
- `server/src/services/agent-conversations.ts`: update
`AGENT_CHAT_DIRECTIVE` to state that another agent's comment is
colleague input (never the person's approval, decision, or instruction
authority) and document how to find and write to another agent's chat
for the same person.
- `server/src/services/native-runtime/native-session-handoff.ts`: label
comments authored by another agent as `other_agent` in fresh-session
history so a fresh session distinguishes them from the chat agent's own
`agent` replies.
- `ui/src/components/task-chat/transcript-adapter.ts` (+5 net lines;
edits remain in the existing >1,000-line owning transcript adapter): add
`sameTurnAgent` so `coalesceSettledTurns` and `attachSettledTurns` match
settled turns to agent bubbles by `agent.id` when both IDs are known,
falling back to display name.
- Tests:
- `server/src/__tests__/agent-chat-peer-messages.test.ts`: real
embedded-Postgres + Express route/service suite covering task-search
chat discovery, comment delivery + wake admission with `resume: true`,
run/agent/status/cancellation/proactive/credential checks, restricted
source tasks (private task, private project, private ancestor, unscoped
run), session fence + both `canReceiveCommentsFromAgents` and
`canCommentOnOtherTasks` permissions, cross-company isolation +
credential `onBehalfOfUserId` mismatch through the HTTP route, and
`buildNativeSessionHandoff` `other_agent` labeling.
- `server/src/__tests__/agent-conversations.test.ts`: verifies the new
peer-chat directive text.
- `server/src/__tests__/agent-chat-messages.test.ts`: updates the
expected denial message for an agent acting for a different person.
- `server/src/services/native-runtime/native-session-handoff.test.ts`:
preserves the fast unit suite on `master` and verifies
`renderNativeSessionHandoff` retains `author: "other_agent"` and
`authorAgentId`.
- `ui/src/components/task-chat/transcript-adapter.test.ts`: covers
ID-based turn coalescing and attachment when runs carry no display name.
- Docs and maps: updated `doc/PRODUCT.md`,
`doc/plans/2026-09-10-agent-chat.md`,
`docs/guides/agent-developer/comments-and-communication.md`, and
`maps/agent-access-map/agent-chat.md`.

## Verification

RED before fix on base `2d065677959b585065ff3fec8718156a4cf6f41d`:
- `pnpm --filter @paperclipai/server exec vitest run
src/__tests__/agent-chat-peer-messages.test.ts`: **3 failed | 2 passed
(5)**:
- `delivers the message to the chat's agent as the sending agent's
message` failed with HTTP `409` (`"Issue is not resumable through
comment follow-up intent"`).
- `requires the sending agent's own running run, acting for the chat's
person` failed with `"Only this conversation's agent can post agent
messages"`.
- `refuses content from a restricted or missing source task` failed with
`"Only this conversation's agent can post agent messages"`.

GREEN after rebase and fix on head
`64ee873ad760ab0b53e2eaa161c5e83e7b1e9ad9`:
- `pnpm --filter @paperclipai/server exec vitest run
src/__tests__/agent-chat-peer-messages.test.ts`: **7 passed (7)** (real
embedded Postgres + real Express `issueRoutes` + real `issueService` +
real `agentWakeupRequests` + real `buildNativeSessionHandoff`).
- `pnpm --filter @paperclipai/server exec vitest run
--no-file-parallelism src/__tests__/agent-chat-peer-messages.test.ts
src/__tests__/agent-conversations.test.ts`:
`agent-conversations.test.ts` **73 passed (73)**.
- `pnpm --filter @paperclipai/server exec vitest run
src/__tests__/agent-chat-messages.test.ts
src/services/native-runtime/native-session-handoff.test.ts`: both files
**passed** (`native-session-handoff.test.ts`: **4 passed (4)**;
`agent-chat-messages.test.ts`: all tests passed).
- `pnpm --filter @paperclipai/ui exec vitest run
src/components/task-chat/transcript-adapter.test.ts`: **119 passed
(119)**.
- `pnpm --filter @paperclipai/server typecheck`: **passed**.
- `pnpm --filter @paperclipai/ui typecheck`: **passed**.
- `node scripts/check-maps.mjs`: **passed** (35 areas, 192 capability
IDs; references and contract fields valid).
- `node scripts/check-test-budget.mjs --files
server/src/__tests__/agent-chat-peer-messages.test.ts
server/src/__tests__/agent-chat-messages.test.ts
server/src/__tests__/agent-conversations.test.ts
server/src/services/native-runtime/native-session-handoff.test.ts
ui/src/components/task-chat/transcript-adapter.test.ts`: **PASS** (110
nightly suites validated, 5 test files checked).
- `pnpm verify --base 2d06567795
--explain`: exited `2` (global CI input
`maps/agent-access-map/agent-chat.md` changed; broad verification
delegated to required PR CI).

Not run locally:
- Full repository `pnpm test:run` and Playwright browser E2E suites
(covered by PR CI).
- Live provider-backed two-agent run (the embedded-Postgres integration
test exercises the real HTTP route, comment boundary, privacy guards,
wake admission, and fresh-session handoff up to provider dispatch).

## Risks

- Behavior change: other agents can now post in a person's chat with an
agent, waking that chat's agent. Existing controls remain enforced:
`canCommentOnOtherTasks`, `canReceiveCommentsFromAgents`, per-run
cross-task write limits, run cancellation, credential/run
responsible-user match, source-task privacy, and company boundaries.
- Prompt injection: another agent's text reaches a chat turn. Owner-only
paths still require the person's own message/outbox, fresh-session
history labels peer comments `other_agent`, and the chat directive
states peer comments never carry the person's approval or instruction
authority.
- Delivery & loops: peer chat messages use ordinary best-effort comment
wakes (not the durable human outbox). Budgets, cross-task write limits,
and directive guidance bound back-and-forth chatter.
- No schema, migration, or API contract change.

## Model Used

- Rebase, conflict resolution, additional boundary/handoff tests, and
verification: Google Gemini via Google Antigravity with tool use, shell
execution, and extended reasoning.
- Original implementation and design from #21232: Claude Opus 5.5
(`claude-opus-5-5`, Anthropic, via Claude Code).

## 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: marshall <13757243+lucktastic@users.noreply.github.com>
Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-11-25 15:33:14 -06:00

489 lines
35 KiB
TypeScript

import { createHash, randomUUID } from "node:crypto";
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
import { eq, sql } from "drizzle-orm";
import { activityLog, agentApiKeys, agents, authUsers, chatCompletionDeliveries, chatTaskHandoffs, companyMemberships, heartbeatRuns, issueComments, issueThreadInteractions, issues, projects } from "@paperclipai/db";
import type { AgentApiKeyScope } from "@paperclipai/shared";
import { createLocalAgentJwt } from "../agent-auth-jwt.js";
import { sendAgentChatMessage } from "../services/agent-chat-messages.js";
import { withConversationReadState } from "../services/agent-chat-read-state.js";
import { instanceSettingsService } from "../services/instance-settings.js";
import { issueService } from "../services/issues.js";
import * as issueServices from "../services/issues.js";
import { agentService } from "../services/agents.js";
import { activityService } from "../services/activity.js";
import { recordAcceptedChatHandoff } from "../services/chat-completion-delivery.js";
import { conversationReplay, deliverConversationComments, undeliveredConversationComments } from "../services/agent-conversations.js";
import { PaperclipRunnerToolAuthority } from "../services/native-runtime/paperclip-runner-tool-authority.js";
import { CROSS_ISSUE_INFLUENCE_LIMIT } from "../services/cross-issue-influence-limit.js";
import { startRunnerApiTestServer } from "./helpers/runner-api-server.js";
import { recoveryPostgresSupport as support } from "./helpers/recovery-postgres.js";
(support.supported ? describe : describe.skip)("proactive agent chat messages", () => {
let server: Awaited<ReturnType<typeof startRunnerApiTestServer>>;
const originalSecret = process.env.PAPERCLIP_AGENT_JWT_SECRET;
beforeAll(async () => {
process.env.PAPERCLIP_AGENT_JWT_SECRET = randomUUID();
server = await startRunnerApiTestServer({ humanSessions: true });
await instanceSettingsService(server.db).updateExperimental({ enableAgentChat: true });
}, 60_000);
afterAll(async () => {
await server?.close();
if (originalSecret === undefined) delete process.env.PAPERCLIP_AGENT_JWT_SECRET;
else process.env.PAPERCLIP_AGENT_JWT_SECRET = originalSecret;
});
const fixture = () => server.fixture({ connectionScenario: "fresh", apiToolsEnabled: false, disableWakeOnDemand: true });
type Fixture = Awaited<ReturnType<typeof fixture>>;
const input = { body: "## Morning review\n\nThree PRs need your review.", idempotencyKey: "morning-pr-digest" };
const send = (f: Fixture, value: unknown = input) => sendAgentChatMessage(server.db, f, value);
const httpSend = (f: Fixture, value: unknown = input, keyScope: AgentApiKeyScope = { kind: "standard" }, authorizingUserId?: string) => fetch(`${server.apiUrl}/api/agents/me/chat/messages`, {
method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${createLocalAgentJwt(f.agentId, f.companyId, "paperclip_runner", f.runId, f.responsibleUserId, keyScope, authorizingUserId)}` },
body: JSON.stringify(value),
});
const apiKey = async (f: Fixture, scopeConfig: AgentApiKeyScope = { kind: "standard" }) => {
const token = `pcp_${randomUUID()}`;
const [key] = await server.db.insert(agentApiKeys).values({ companyId: f.companyId, agentId: f.agentId, name: "Chat delivery test",
responsibleUserId: f.responsibleUserId, scopeConfig, keyHash: createHash("sha256").update(token).digest("hex") }).returning();
return { ...key, token };
};
const keySend = (f: Fixture, token: string, value: unknown = input) => fetch(`${server.apiUrl}/api/agents/me/chat/messages`, {
method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${token}`, "x-paperclip-run-id": f.runId },
body: JSON.stringify(value),
});
const spendInfluence = (f: Fixture, count: number) => server.db.insert(activityLog).values(Array.from({ length: count }, (_, index) => ({
companyId: f.companyId, actorType: "agent" as const, actorId: f.agentId, agentId: f.agentId, runId: f.runId,
action: "issue.cross_issue_influence_observed", entityType: "issue", entityId: f.issueId,
details: { kind: ["comment", "update", "interaction_resolution"][index % 3] },
})));
const waitForBlockedBy = (pid: number) => expect.poll(async () => {
const [state] = await server.db.execute<{ waiting: boolean }>(sql`select exists (
select 1 from pg_stat_activity where datname = current_database() and ${pid} = any(pg_blocking_pids(pid))
) as waiting`);
return state.waiting;
}).toBe(true);
it("commits a rename and a proactive message contending for an existing chat", async () => {
const f = await fixture(), chat = (await send(f))!;
const held = Promise.withResolvers<number>(), resume = Promise.withResolvers<void>();
const body = "A message sent while its agent is renamed";
const delivery = sendAgentChatMessage(server.db, f, { body, idempotencyKey: "concurrent-rename" }, {
authorizeNative: async tx => {
const [backend] = await tx.execute<{ pid: number }>(sql`select pg_backend_pid() as pid`);
held.resolve(backend.pid);
await resume.promise;
},
});
const pid = await Promise.race([held.promise, delivery.then(() => { throw new Error("Delivery ended before taking chat locks"); })]);
const rename = agentService(server.db).update(f.agentId, { name: "Renamed message sender" });
const outcomes = Promise.allSettled([delivery, rename]);
try {
await waitForBlockedBy(pid);
} finally {
resume.resolve();
await outcomes;
}
const settled = await outcomes;
const failures = settled.filter(result => result.status === "rejected").map(result =>
`${result.reason?.cause?.code ?? result.reason?.code}: ${result.reason?.cause?.message ?? result.reason?.message}`);
expect(settled, failures.join("\n")).toMatchObject([{ status: "fulfilled" }, { status: "fulfilled" }]);
expect((await issueService(server.db).getById(chat.conversationId))?.title).toBe("Chat with Renamed message sender");
expect(await server.db.select({ body: issueComments.body }).from(issueComments).where(eq(issueComments.issueId, chat.conversationId)))
.toContainEqual({ body });
});
it("serializes a rename with proactive chat creation and retains the current title", async () => {
const f = await fixture();
const reached = Promise.withResolvers<number>(), resume = Promise.withResolvers<void>();
const realIssueService = issueServices.issueService;
const spy = vi.spyOn(issueServices, "issueService").mockImplementation(database => {
const service = realIssueService(database);
return { ...service, create: async (companyId, data, transaction) => {
if (data.conversationAgentId === f.agentId) {
const [backend] = await database.execute<{ pid: number }>(sql`select pg_backend_pid() as pid`);
reached.resolve(backend.pid);
await resume.promise;
}
return service.create(companyId, data, transaction);
} };
});
const delivery = send(f);
const pid = await Promise.race([reached.promise, delivery.then(() => { throw new Error("Delivery ended before chat creation"); })]);
const rename = agentService(server.db).update(f.agentId, { name: "Renamed chat sender" });
const outcomes = Promise.allSettled([delivery, rename]);
try {
await waitForBlockedBy(pid);
} finally {
resume.resolve();
spy.mockRestore();
await outcomes;
}
expect(await outcomes).toMatchObject([{ status: "fulfilled" }, { status: "fulfilled" }]);
const receipt = (await delivery)!;
expect((await issueService(server.db).getById(receipt.conversationId))?.title).toBe("Chat with Renamed chat sender");
});
it.each(["native", "http"])("enforces the shared run cap for %s delivery without creating an empty chat", async transport => {
const f = await fixture();
await spendInfluence(f, CROSS_ISSUE_INFLUENCE_LIMIT);
if (transport === "native") {
await expect(f.authority.execute({ tool: "send_chat_message", arguments: input, callId: randomUUID() }))
.rejects.toMatchObject({ status: 429, details: { code: "cross_issue_influence_cap_exceeded", cap: CROSS_ISSUE_INFLUENCE_LIMIT } });
} else {
const response = await httpSend(f);
expect(response.status, await response.clone().text()).toBe(429);
expect(await response.json()).toMatchObject({ details: { code: "cross_issue_influence_cap_exceeded", cap: CROSS_ISSUE_INFLUENCE_LIMIT } });
}
const snapshot = await f.snapshot();
expect(snapshot.comments).toEqual([]);
expect(snapshot.issues.filter(issue => issue.conversationAgentId)).toEqual([]);
expect(snapshot.activity).toHaveLength(CROSS_ISSUE_INFLUENCE_LIMIT);
});
it("charges a committed message once across retries, recovery and conflicting content", async () => {
const f = await fixture(), receipt = (await send(f))!;
expect((await httpSend(f)).status).toBe(200);
expect(await f.authority.recover({ tool: "send_chat_message", arguments: input, callId: randomUUID() }))
.toMatchObject({ result: { commentId: receipt.commentId, replayed: true } });
expect((await httpSend(f, { ...input, body: "Different content" })).status).toBe(409);
expect(await f.authority.recover({ tool: "send_chat_message", arguments: { ...input, idempotencyKey: "not-committed" }, callId: randomUUID() })).toBeNull();
expect((await f.snapshot()).activity.filter(row => row.action === "issue.cross_issue_influence_observed")).toHaveLength(1);
expect((await f.snapshot()).comments).toHaveLength(1);
});
it("cannot race native and HTTP delivery past the shared final slot", async () => {
const f = await fixture();
await spendInfluence(f, CROSS_ISSUE_INFLUENCE_LIMIT - 1);
const outcomes = await Promise.all([
f.authority.execute({ tool: "send_chat_message", arguments: { ...input, idempotencyKey: "native-last-slot" }, callId: randomUUID() })
.then(() => 201, error => error.status),
httpSend(f, { ...input, idempotencyKey: "http-last-slot" }).then(response => response.status),
]);
expect(outcomes.sort()).toEqual([201, 429]);
const snapshot = await f.snapshot();
expect(snapshot.activity.filter(row => row.action === "issue.cross_issue_influence_observed")).toHaveLength(CROSS_ISSUE_INFLUENCE_LIMIT);
expect(snapshot.comments).toHaveLength(1);
expect(snapshot.issues.filter(issue => issue.conversationAgentId)).toHaveLength(1);
});
it("supports standard task API keys without changing agent and run attribution", async () => {
const f = await fixture(), key = await apiKey(f);
const response = await keySend(f, key.token);
expect(response.status, await response.clone().text()).toBe(201);
expect((await f.snapshot()).activity).toContainEqual(expect.objectContaining({ action: "issue.comment_added",
actorId: f.agentId, agentId: f.agentId, runId: f.runId, responsibleUserId: f.responsibleUserId }));
});
it("preserves an external authorizer's current access intersection", async () => {
const f = await fixture();
expect((await httpSend(f, input, { kind: "standard" }, randomUUID())).status).toBe(403);
await expect(sendAgentChatMessage(server.db, { ...f, authorizingUserId: randomUUID() }, input)).rejects.toMatchObject({ status: 403 });
expect((await f.snapshot()).issues.filter(issue => issue.conversationAgentId)).toEqual([]);
expect((await f.snapshot()).comments).toEqual([]);
});
it("keeps skill-test JWT and API-key scopes confined to their own task", async () => {
const f = await fixture(), scope = { kind: "skill_test", issueId: f.issueId } as const;
const key = await apiKey(f, scope);
expect((await httpSend(f, input, scope)).status).toBe(403);
expect((await keySend(f, key.token)).status).toBe(403);
expect((await f.snapshot()).issues.filter(issue => issue.conversationAgentId)).toEqual([]);
expect((await f.snapshot()).comments).toEqual([]);
});
it.each(["admitted", "legacy", "saved-task"])("retains a persisted %s skill-test run boundary after task mode changes", async kind => {
const f = await fixture();
const contextSnapshot = kind === "admitted" ? { paperclipAgentJwtScope: { kind: "skill_test", issueId: f.issueId } }
: kind === "legacy" ? { paperclipSkillTest: { id: randomUUID() } }
: { paperclipIssue: { id: f.issueId, workMode: "skill_test" } };
await server.db.update(heartbeatRuns).set({ contextSnapshot }).where(eq(heartbeatRuns.id, f.runId));
await server.db.update(issues).set({ workMode: "skill_test" }).where(eq(issues.id, f.issueId));
await server.db.update(issues).set({ workMode: "standard" }).where(eq(issues.id, f.issueId));
expect((await httpSend(f)).status).toBe(403);
await expect(sendAgentChatMessage(server.db, f, input, { receiptOnly: true })).rejects.toThrow(/skill.test/i);
expect((await f.snapshot()).issues.filter(issue => issue.conversationAgentId)).toEqual([]);
expect((await f.snapshot()).comments).toEqual([]);
});
it("does not let task-bridge credentials publish outside their admitted task", async () => {
const f = await fixture(), key = await apiKey(f, { kind: "task_bridge", parentIssueId: f.issueId });
expect((await keySend(f, key.token)).status).toBe(403);
expect((await f.snapshot()).issues.filter(issue => issue.conversationAgentId)).toEqual([]);
});
it.each(["revoked", "narrowed", "responsible-human"])("rechecks a %s API key after waiting for the destination", async kind => {
const f = await fixture(), receipt = (await send(f))!, key = await apiKey(f);
let unlock!: () => void, ready!: (pid: number) => void;
const locked = new Promise<number>(resolve => { ready = resolve; });
const release = new Promise<void>(resolve => { unlock = resolve; });
const gate = server.db.transaction(async tx => {
const [{ pid }] = await tx.execute<{ pid: number }>(sql`select pg_backend_pid() as pid`);
await tx.select({ id: issues.id }).from(issues).where(eq(issues.id, receipt.conversationId)).for("update");
ready(pid); await release;
});
const pid = await locked;
const delivery = keySend(f, key.token, { ...input, idempotencyKey: "key-after-wait" });
try {
await vi.waitFor(async () => {
const [row] = await server.db.execute(sql<{ blocked: boolean }>`select exists(select 1 from pg_stat_activity
where datname=current_database() and ${pid}=any(pg_blocking_pids(pid))) as blocked`);
expect(row.blocked).toBe(true);
}, { timeout: 5_000, interval: 10 });
await server.db.update(agentApiKeys).set(kind === "revoked" ? { revokedAt: new Date() }
: kind === "narrowed" ? { scopeConfig: { kind: "skill_test", issueId: f.issueId } }
: { responsibleUserId: randomUUID() }).where(eq(agentApiKeys.id, key.id));
unlock(); await gate;
expect((await delivery).status).toBe(403);
expect((await f.snapshot()).comments).toHaveLength(1);
} finally { unlock(); await gate; await delivery; }
}, 30_000);
it("publishes through the native tool without generic API tools, without starting a turn or changing the source", async () => {
const f = await fixture();
expect(f.authority.definitions().map(d => d.name)).toContain("send_chat_message");
const before = await issueService(server.db).getById(f.issueId);
const receipt = await f.authority.execute({ tool: "send_chat_message", arguments: input, callId: randomUUID() }) as { commentId: string; conversationId: string; chatUrl: string };
const snapshot = await f.snapshot();
const comment = snapshot.comments.find(c => c.id === receipt.commentId)!;
expect(comment).toMatchObject({ body: input.body, authorAgentId: f.agentId, createdByRunId: f.runId, onBehalfOfUserId: f.responsibleUserId,
metadata: { chatMessage: { sourceTaskId: f.issueId, sourceRunId: f.runId } } });
expect(snapshot.issues.find(i => i.id === receipt.conversationId)).toMatchObject({ conversationAgentId: f.agentId,
conversationUserId: f.responsibleUserId, conversationState: "waiting", executionRunId: null, status: "in_review" });
expect(await issueService(server.db).getById(f.issueId)).toEqual(before);
expect(await server.db.select().from(heartbeatRuns).where(eq(heartbeatRuns.companyId, f.companyId))).toHaveLength(1);
expect(snapshot.activity.filter(a => a.action === "issue.comment_added")).toHaveLength(1);
expect(await activityService(server.db).runsForIssue(f.companyId, receipt.conversationId)).toEqual([]);
expect(receipt.chatUrl).toContain(`/chats/${f.agentId}`);
expect(await withConversationReadState(server.db, f.companyId, f.responsibleUserId!, [{ id: receipt.conversationId }])).toMatchObject([{ isUnreadForMe: true }]);
expect(await f.authority.recover({ tool: "send_chat_message", arguments: input, callId: randomUUID() }))
.toMatchObject({ result: { commentId: receipt.commentId, replayed: true } });
});
it("creates once under concurrent first sends and recovers a committed response across run retries", async () => {
const f = await fixture();
expect(await f.authority.recover({ tool: "send_chat_message", arguments: input, callId: randomUUID() })).toBeNull();
expect((await f.snapshot()).issues.filter(i => i.conversationAgentId)).toHaveLength(0);
const [a, b] = await Promise.all([send(f), send(f)]);
expect(a!.commentId).toBe(b!.commentId);
expect((await f.snapshot()).comments).toHaveLength(1);
const other = await send(f, { ...input, idempotencyKey: "second" });
expect(other!.commentId).not.toBe(a!.commentId);
await expect(send(f, { ...input, body: "Different digest" })).rejects.toThrow(/different content/);
const nextRunId = randomUUID();
await server.db.update(heartbeatRuns).set({ status: "succeeded" }).where(eq(heartbeatRuns.id, f.runId));
await server.db.insert(heartbeatRuns).values({ id: nextRunId, companyId: f.companyId, agentId: f.agentId,
status: "running", nativeIssueId: f.issueId, responsibleUserId: f.responsibleUserId });
await server.db.update(issues).set({ executionRunId: nextRunId }).where(eq(issues.id, f.issueId));
expect(await send({ ...f, runId: nextRunId })).toMatchObject({ commentId: a!.commentId, replayed: true });
expect((await f.snapshot()).comments).toHaveLength(2);
});
it("supports HTTP routine delivery during an active chat reply and after a context reset", async () => {
const f = await fixture();
await server.db.update(heartbeatRuns).set({ invocationSource: "automation", triggerDetail: "routine" }).where(eq(heartbeatRuns.id, f.runId));
const first = await httpSend(f);
expect(first.status, await first.clone().text()).toBe(201);
const receipt = await first.json();
const replyRun = randomUUID();
await server.db.insert(heartbeatRuns).values({ id: replyRun, companyId: f.companyId, agentId: f.agentId, status: "running",
nativeIssueId: receipt.conversationId, responsibleUserId: f.responsibleUserId, contextSnapshot: { issueId: receipt.conversationId, conversationSessionGeneration: 0 } });
await server.db.update(issues).set({ executionRunId: replyRun, conversationState: "active" }).where(eq(issues.id, receipt.conversationId));
const before = await issueService(server.db).getById(receipt.conversationId);
expect((await httpSend(f, { ...input, idempotencyKey: "during-reply" })).status).toBe(201);
const after = await issueService(server.db).getById(receipt.conversationId);
expect(after).toMatchObject({ executionRunId: replyRun, conversationState: before!.conversationState, status: before!.status });
await server.db.update(issues).set({ conversationSessionGeneration: 1, conversationState: "waiting", executionRunId: null }).where(eq(issues.id, receipt.conversationId));
expect((await httpSend(f, { ...input, idempotencyKey: "after-new" })).status).toBe(201);
await expect(issueService(server.db).addComment(receipt.conversationId, "Stale reply", { agentId: f.agentId, runId: replyRun })).rejects.toThrow(/earlier session/);
expect(await server.db.select().from(heartbeatRuns).where(eq(heartbeatRuns.companyId, f.companyId))).toHaveLength(2);
expect((await f.snapshot()).comments).toHaveLength(3);
});
it("never enqueues a digest during recovery or the next human reply", async () => {
const f = await fixture(), svc = issueService(server.db);
const receipt = (await send(f))!;
const conversation = (await svc.getById(receipt.conversationId))!;
const enqueue = vi.fn(async (_agentId: string, _options: unknown) => null);
expect(await undeliveredConversationComments(server.db, f.companyId, conversation.id)).toEqual([]);
await deliverConversationComments(server.db, conversation, enqueue);
expect(enqueue).not.toHaveBeenCalled();
const reply = await svc.addComment(conversation.id, "Which PR should I review first?", { userId: f.responsibleUserId! }, { clientRequestId: randomUUID() });
expect((await undeliveredConversationComments(server.db, f.companyId, conversation.id)).map(comment => comment.id)).toEqual([reply.id]);
await deliverConversationComments(server.db, conversation, enqueue);
expect(enqueue).toHaveBeenCalledOnce();
expect(enqueue.mock.calls[0]?.[1]).toMatchObject({ requestedByActorType: "user", requestedByActorId: f.responsibleUserId,
contextSnapshot: { wakeCommentId: reply.id, wakeCommentIds: [reply.id] } });
expect(await conversationReplay(server.db, f.companyId, conversation.id, reply.id))
.toContain(JSON.stringify({ author: "agent", body: input.body }));
});
it.each([
{ transport: "native", replay: false }, { transport: "native", replay: true },
{ transport: "http", replay: false }, { transport: "http", replay: true },
])("serializes $transport delivery with a native chat mutation, replay=$replay", async ({ transport, replay }) => {
const f = await fixture(), receipt = (await send(f))!, chatRunId = randomUUID();
await server.db.update(agents).set({ runtimeConfig: { heartbeat: { enabled: false, wakeOnDemand: false, maxConcurrentRuns: 2 } } }).where(eq(agents.id, f.agentId));
await server.db.insert(heartbeatRuns).values({ id: chatRunId, companyId: f.companyId, agentId: f.agentId, status: "running",
runtimeMode: "native", nativeIssueId: receipt.conversationId, responsibleUserId: f.responsibleUserId,
contextSnapshot: { issueId: receipt.conversationId, conversationSessionGeneration: 0 } });
await server.db.update(issues).set({ executionRunId: chatRunId, conversationState: "active" }).where(eq(issues.id, receipt.conversationId));
const chat = new PaperclipRunnerToolAuthority(server.db, { companyId: f.companyId, agentId: f.agentId,
issueId: receipt.conversationId, runId: chatRunId, apiUrl: server.apiUrl, apiToolsEnabled: false });
let unlock!: () => void, ready!: (pid: number) => void;
const locked = new Promise<number>(resolve => { ready = resolve; });
const release = new Promise<void>(resolve => { unlock = resolve; });
const gate = server.db.transaction(async tx => {
const [{ pid }] = await tx.execute<{ pid: number }>(sql`select pg_backend_pid() as pid`);
await tx.select({ id: agents.id }).from(agents).where(eq(agents.id, f.agentId)).for("update");
ready(pid);
await release;
});
const pid = await locked;
const waitForBlocked = async (count: number) => vi.waitFor(async () => {
const [row] = await server.db.execute(sql<{ count: number }>`with recursive blocked(pid) as (
select pid from pg_stat_activity where datname = current_database() and ${pid} = any(pg_blocking_pids(pid))
union select activity.pid from pg_stat_activity activity join blocked on blocked.pid = any(pg_blocking_pids(activity.pid))
where activity.datname = current_database()
) select count(*)::int as count from blocked`);
expect(row.count).toBeGreaterThanOrEqual(count);
}, { timeout: 5000, interval: 10 });
const message = replay ? input : { ...input, idempotencyKey: "concurrent-message" };
let settled: Promise<PromiseSettledResult<unknown>[]> | undefined;
let observedDelivery: Promise<PromiseSettledResult<unknown>[]> | undefined;
try {
const delivery = transport === "native"
? f.authority.execute({ tool: "send_chat_message", arguments: message, callId: randomUUID() })
: httpSend(f, message).then(async response => {
expect(response.status, await response.clone().text()).toBe(replay ? 200 : 201);
return response.json();
});
observedDelivery = Promise.allSettled([delivery]);
await waitForBlocked(1);
const mutation = chat.execute({ tool: "write_document", callId: randomUUID(), arguments: {
key: "concurrent-note", title: "Concurrent note", body: "The chat reply stays independent.", baseRevisionId: null, idempotencyKey: "concurrent-note",
} });
settled = Promise.allSettled([delivery, mutation]);
await waitForBlocked(2);
unlock();
await gate;
const results = await settled;
expect(results.map(result => result.status), JSON.stringify(results)).toEqual(["fulfilled", "fulfilled"]);
expect(results[1]).toMatchObject({ status: "fulfilled", value: { disposition: "applied", document: { key: "concurrent-note" } } });
expect(await issueService(server.db).getById(receipt.conversationId)).toMatchObject({ conversationState: "active" });
} finally {
unlock();
await gate;
await settled;
await observedDelivery;
}
}, 30_000);
it("isolates two humans sharing an agent and two agents serving one human", async () => {
const f = await fixture();
const a = await send(f);
const userId = randomUUID();
await server.db.insert(authUsers).values({ id: userId, name: "Second human", email: `${userId}@fixture.invalid`, emailVerified: true, createdAt: new Date(), updatedAt: new Date() });
await server.db.insert(companyMemberships).values({ companyId: f.companyId, principalType: "user", principalId: userId, status: "active", membershipRole: "member" });
await server.db.update(heartbeatRuns).set({ responsibleUserId: userId }).where(eq(heartbeatRuns.id, f.runId));
const b = await send(f);
expect(b!.conversationId).not.toBe(a!.conversationId);
const [agent] = await server.db.insert(agents).values({ companyId: f.companyId, name: "Second agent", status: "active", adapterType: "process" }).returning();
const runId = randomUUID();
await server.db.insert(heartbeatRuns).values({ id: runId, companyId: f.companyId, agentId: agent.id, status: "running", issueId: f.issueId, responsibleUserId: userId });
await server.db.update(issues).set({ assigneeAgentId: agent.id, executionRunId: runId }).where(eq(issues.id, f.issueId));
const c = await send({ ...f, agentId: agent.id, runId });
expect(new Set([a!.conversationId, b!.conversationId, c!.conversationId]).size).toBe(3);
await expect(issueService(server.db).addComment(a!.conversationId, "Wrong person", { agentId: agent.id, runId })).rejects.toThrow(/person who owns that chat/);
await expect(issueService(server.db).addComment(b!.conversationId, "Bypass", { agentId: f.agentId, runId: f.runId })).rejects.toThrow(/earlier session/);
});
it("delivers to its own chat while preserving pending questions and the original handoff completion", async () => {
const f = await fixture(), svc = issueService(server.db);
const own = await svc.create(f.companyId, { title: "Own chat", assigneeAgentId: f.agentId,
conversationAgentId: f.agentId, conversationUserId: f.responsibleUserId!, conversationState: "waiting", status: "in_review" });
const [coordinator] = await server.db.insert(agents).values({ companyId: f.companyId, name: "Coordinator", status: "active", adapterType: "process" }).returning();
const origin = await svc.create(f.companyId, { title: "Origin chat", assigneeAgentId: coordinator.id,
conversationAgentId: coordinator.id, conversationUserId: f.responsibleUserId!, conversationState: "waiting", status: "in_review" });
await recordAcceptedChatHandoff(server.db, (await svc.getById(f.issueId))!, origin);
const [question] = await server.db.insert(issueThreadInteractions).values({ companyId: f.companyId, issueId: own.id,
kind: "request_confirmation", status: "pending", payload: { version: 1, prompt: "Proceed?" } }).returning();
expect(await send(f)).toMatchObject({ conversationId: own.id });
expect(await server.db.select().from(issueComments).where(eq(issueComments.issueId, origin.id))).toEqual([]);
expect(await server.db.select().from(issueThreadInteractions).where(eq(issueThreadInteractions.id, question.id))).toEqual([question]);
expect(await server.db.select().from(heartbeatRuns).where(eq(heartbeatRuns.companyId, f.companyId))).toHaveLength(1);
await svc.update(f.issueId, { status: "done" });
expect(await server.db.select().from(chatCompletionDeliveries).where(eq(chatCompletionDeliveries.taskId, f.issueId)))
.toMatchObject([{ status: "pending", responseCommentId: null }]);
expect(await server.db.select().from(chatTaskHandoffs).where(eq(chatTaskHandoffs.taskId, f.issueId)))
.toMatchObject([{ conversationId: origin.id, agentId: coordinator.id }]);
});
it.each(["ask", "planning", "review", "watchdog", "stopped", "stale", "missing-human", "revoked", "private-task", "private-project", "private-ancestor", "paused-agent"])("rejects %s before creating a chat", async kind => {
const f = await fixture();
if (kind === "ask" || kind === "planning") await server.db.update(issues).set({ workMode: kind }).where(eq(issues.id, f.issueId));
if (kind === "review") await server.db.update(heartbeatRuns).set({ contextSnapshot: { issueId: f.issueId, nativeReviewInteractionId: randomUUID(), nativeReviewDecisionId: randomUUID() } }).where(eq(heartbeatRuns.id, f.runId));
if (kind === "watchdog") await server.db.update(heartbeatRuns).set({ contextSnapshot: { issueId: f.issueId, taskWatchdog: true, watchedIssueId: f.issueId } }).where(eq(heartbeatRuns.id, f.runId));
if (kind === "stopped") await server.db.update(heartbeatRuns).set({ resultJson: { executionCancellation: { state: "requested" } } }).where(eq(heartbeatRuns.id, f.runId));
if (kind === "stale") await server.db.update(issues).set({ executionRunId: null }).where(eq(issues.id, f.issueId));
if (kind === "missing-human") await server.db.update(heartbeatRuns).set({ responsibleUserId: null }).where(eq(heartbeatRuns.id, f.runId));
if (kind === "revoked") await server.db.delete(companyMemberships).where(eq(companyMemberships.principalId, f.responsibleUserId!));
if (kind === "private-task") await server.db.update(issues).set({ visibility: "private" }).where(eq(issues.id, f.issueId));
if (kind === "private-project") await server.db.update(projects).set({ visibility: "private", privacyOwnerUserId: f.responsibleUserId }).where(eq(projects.id, f.projectId));
if (kind === "private-ancestor") {
await server.db.update(issues).set({ visibility: "private" }).where(eq(issues.id, f.blockerId));
await server.db.update(issues).set({ parentId: f.blockerId, privacyParentIssueId: f.blockerId, privacyRootIssueId: f.blockerId }).where(eq(issues.id, f.issueId));
}
if (kind === "paused-agent") await server.db.update(agents).set({ status: "paused" }).where(eq(agents.id, f.agentId));
await expect(send(f)).rejects.toThrow();
expect((await f.snapshot()).comments).toHaveLength(0);
expect((await f.snapshot()).issues.filter(i => i.conversationAgentId)).toHaveLength(0);
});
it("rejects forged destinations and server-owned provenance while ignoring the retired flag", async () => {
const f = await fixture();
expect((await httpSend(f, { ...input, agentId: f.agentId })).status).toBe(400);
await instanceSettingsService(server.db).updateExperimental({ enableAgentChat: false });
try { expect((await httpSend(f)).status).toBe(201); }
finally { await instanceSettingsService(server.db).updateExperimental({ enableAgentChat: true }); }
const receipt = await send(f);
const [comment] = await server.db.select().from(issueComments).where(eq(issueComments.id, receipt!.commentId));
await expect(issueService(server.db).addComment(f.issueId, "Forged source", { agentId: f.agentId, runId: f.runId }, { metadata: comment.metadata! })).rejects.toThrow(/server-owned/);
});
it("keeps unseen concurrent arrivals unread with exact comment cutoffs and monotonic receipts", async () => {
const f = await fixture();
const a = (await send(f))!, b = (await send(f, { ...input, idempotencyKey: "later" }))!;
await server.db.update(issueComments).set({ createdAt: sql`'2026-10-30 12:00:00.123456+00'::timestamptz` }).where(eq(issueComments.id, a.commentId));
await server.db.update(issueComments).set({ createdAt: sql`'2026-10-30 12:00:00.123789+00'::timestamptz` }).where(eq(issueComments.id, b.commentId));
const cookie = await server.humanSession(f.responsibleUserId!);
await server.db.insert(issueComments).values([
{ companyId: f.companyId, issueId: a.conversationId, authorUserId: f.responsibleUserId, authorType: "user", body: "Human message" },
{ companyId: f.companyId, issueId: a.conversationId, authorAgentId: f.agentId, authorType: "system", body: "System message" },
{ companyId: f.companyId, issueId: a.conversationId, authorAgentId: f.agentId, body: "Deleted message", deletedAt: new Date() },
{ companyId: f.companyId, issueId: a.conversationId, authorAgentId: f.agentId, body: "Receipt", origin: "fast_response" },
{ companyId: f.companyId, issueId: a.conversationId, authorAgentId: f.agentId, body: "Notice", presentation: { kind: "system_notice", tone: "neutral", detailsDefaultOpen: false } },
]);
const mark = (id: string) => fetch(`${server.apiUrl}/api/issues/${a.conversationId}/read`, { method: "POST", headers: { Cookie: cookie, Origin: server.apiUrl, "Content-Type": "application/json" }, body: JSON.stringify({ throughCommentId: id }) });
const unread = async () => (await withConversationReadState(server.db, f.companyId, f.responsibleUserId!, [{ id: a.conversationId }]))[0];
expect(await unread()).toMatchObject({ isUnreadForMe: true, unreadMessageCount: 2 });
const response = await mark(a.commentId);
expect(response.status, await response.text()).toBe(200);
expect(await unread()).toMatchObject({ isUnreadForMe: true, unreadMessageCount: 1 });
expect((await mark(b.commentId)).status).toBe(200);
expect(await unread()).toMatchObject({ isUnreadForMe: false, unreadMessageCount: 0 });
expect(await withConversationReadState(server.db, f.companyId, randomUUID(), [{ id: a.conversationId }, { id: f.issueId }]))
.toMatchObject([{ unreadMessageCount: 2, isUnreadForMe: true }, { unreadMessageCount: 0, isUnreadForMe: false }]);
expect(await withConversationReadState(server.db, randomUUID(), f.responsibleUserId!, [{ id: a.conversationId }]))
.toMatchObject([{ unreadMessageCount: 0, isUnreadForMe: false }]);
await mark(a.commentId);
expect(await unread()).toMatchObject({ isUnreadForMe: false, unreadMessageCount: 0 });
const foreign = await server.fixture({ connectionScenario: "fresh" });
const foreignMessage = (await send(foreign))!;
expect((await mark(foreignMessage.commentId)).status).toBe(404);
const chats = await fetch(`${server.apiUrl}/api/companies/${f.companyId}/chats`, { headers: { Cookie: cookie } });
expect(chats.status).toBe(200);
expect(await chats.json()).toMatchObject([{ id: a.conversationId, isUnreadForMe: false, unreadMessageCount: 0 }]);
const detail = await fetch(`${server.apiUrl}/api/companies/${f.companyId}/chats/${f.agentId}`, { headers: { Cookie: cookie } });
expect(detail.status).toBe(200);
expect(await detail.json()).toMatchObject({ id: a.conversationId, unreadMessageCount: 0 });
});
});