mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-08 21:35:36 +01:00
## Thinking Path > - Paperclip helps people manage AI agents at work. > - Native Runner owns execution and task authority. > - Operators need an explicit native Cursor Cloud choice. > - Tools/files need company, task, session and run scope. > - This connects setup and lifecycle to Runner Core. > - Legacy agents retain their selected runtime/history. ## Linked Issues or Issue Description Depends on #18068. Managed-only API placement and API-owned Stop containment are in #18310. Refs #5664, #11803, #17299, and #17471. No duplicate native integration was found. ## What Changed - Add explicit native setup, Test, saved settings and dispatch with no-prompt account/catalog/callback checks. - Scope MCP to company, agent, task, session and run through authenticated HTTPS. Register verified artifacts/work products; preserve unpriced accounting, completion and budgets. - Connect Question/Plan continuation, cold handoff and admitted fresh sessions; recheck waiting admission under the task lock. - Present audited Stop intent while preserving physical containment proof; bound typed Stop metadata. Preserve title edits through preference/navigation readiness. - Select one live/history representation per run to prevent duplicate settled cards. Integrate upstream task/read-state/retry/schema contracts and repair two test fixtures. ## Verification Core #18068 merged normally as `3910352c923a`. Product head `5a60f541c982` has passing repository CI/build/browser checks, including required `ci / verify` and `ci / e2e`, plus a fresh exact-head Greptile 5/5 review with zero unresolved threads. Two non-required external app checks remain pending: Paperclip Review is queued waiting for its assigned agent; Superagent Security Scan is reviewing the diff. These are not claimed completed. The normal merge endpoint enforces the repository rules; no policy or status override is used. The dependent Top PR is addressing its fresh deferred-Stop review finding. Product `5a60f541c982` passed 270 UI and 20 selected Cloud/native/disk-full tests, UI/server typechecks, tokens and maps. Top’s clean normal merge passed tokens/maps/diff checks; its matrix is pending. Earlier receipts retain Core’s 47 Rust, 4 compiled-native retirement and 19 settlement controls, normal Runner build, and runtime 9af build/recursive typecheck. Reproduced loading/shutdown CI fixture failures passed 7/7 browser and 121/121 unit tests, including a broken-unsubscribe negative control. No timeout/oracle was relaxed. The actual-parent restack adopts 75 mainline paths, including schema/permission and generic runtime changes, beyond the prior two fixture repairs and client projection fix. The full difference from hosted `9af1a5de42a2` is therefore larger than four files; backend source also differs. The merged config-read checks match Core391, and the Cursor placement/runtime/relay/selection modules match frozen9af; selected affected regressions pass. Paid proof keeps its actual 9af source/image identity. The separately tested live/history card fix is not claimed deployed on that host. On 9af, original saved settings survived reopening and fresh no-prompt Test authenticated catalog/model/binding; no new Save is claimed. Fresh Linux admission passed. With `claude-sonnet-4-5`, Artifact downloaded exact 27 bytes and matched 121 canonical identities in complete S3-served history. First Stop’s strict quiet FAILED on a 10.373-second gap despite independent cancellation/no late artifact. One targeted unchanged-sampler retry retained all 24 samples: 35.076 seconds quiet / maximum gap 4.584 seconds, 67 ordered canonical identities, and separate CANCELLED/artifact-absent status after the original 180-second deadline. The pre-reload duplicate settled card is disclosed. Exact-ID cleanup for the three new provider agents is awaiting authorization, so full grades and the fresh Artifact’s post-cleanup download remain blocked. A historical deleted-provider artifact was newly downloaded on 9af; original F226 Question/Plan/reconnect and cleanup proof keep their identities. Retained failures: stale fixture 132/133; E8 48/135 with 87 reset-hook failures; Mac 17 SQL failures/skips; earlier title/startup/custody/Stop/deletion failures with unknown causes/bounded recoveries; `dea023` Stop’s warning/50 duplicate identities; pre-repair ACK and original Core CI failures. WSP’s 251-assertion, 7/8-suite attempt failed physical PostgreSQL teardown; the unchanged wrapper rerun passed 128. Retry alone establishes no cause or full local-suite pass. All paid attempts remain recorded; vendor dollar cost is UNKNOWN. ## Risks Experimental: Cursor independently acknowledges neither the effective model nor instruction digest before the first prompt. Explicit values are submitted and checked through behavior; unpriced provider dollar cost stays UNKNOWN. Callbacks need authenticated reachable HTTPS. Scope, revocation, budgets, completion and cleanup remain authoritative. Operator Stop intent is separate from physical containment. Saved legacy agents retain their selected runtime. Prior shared title-save/deletion failures remain disclosed. ## Model Used OpenAI Codex, GPT-6 family, assisted with reasoning, repository tools, code execution, browser verification, and delegated code review. The serving model’s exact API ID and context window are not exposed. No value is inferred from the model catalog. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used and the limits of available model metadata - [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 linked public issues or described the issue in this PR - [x] I have not referenced internal or instance-local Paperclip issues or links - [x] My branch describes the change and contains no internal ticket ID - [x] I have run the scoped local tests reported above; pending gates are stated separately - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation - [x] I have considered and documented the risks - [x] All required CI gates pass on the current head - [x] Fresh Greptile is 5/5 with no unresolved findings on the current head - [x] I will address all reviewer comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
1879 lines
58 KiB
TypeScript
1879 lines
58 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import type { Agent } from "@paperclipai/shared";
|
|
import {
|
|
buildAssistantPartsFromTranscript,
|
|
buildIssueChatMessages,
|
|
isCoTSegmentActive,
|
|
isRedundantAiRecoveryNotice,
|
|
preserveReadableStreamingRetraction,
|
|
stabilizeThreadMessages,
|
|
type IssueChatComment,
|
|
type IssueChatLinkedRun,
|
|
} from "./issue-chat-messages";
|
|
import type {
|
|
AskUserQuestionsInteraction,
|
|
AskUserQuestionsQuestion,
|
|
ConnectionIntentInteraction,
|
|
RequestConfirmationInteraction,
|
|
SuggestTasksInteraction,
|
|
} from "./issue-thread-interactions";
|
|
import { pendingConnectionIntentInteraction } from "../fixtures/issueThreadInteractionFixtures";
|
|
import type { IssueTimelineEvent } from "./issue-timeline-events";
|
|
import type { ActiveRunForIssue, LiveRunForIssue } from "../api/heartbeats";
|
|
import { registerUIAdapter, unregisterUIAdapter } from "../adapters/registry";
|
|
|
|
describe("connection intents in the task feed", () => {
|
|
it("orders the card chronologically and collapses live/polling snapshots into one updated message", () => {
|
|
const pending = {
|
|
...pendingConnectionIntentInteraction,
|
|
createdAt: new Date("2026-08-26T12:02:00.000Z"),
|
|
updatedAt: new Date("2026-08-26T12:02:00.000Z"),
|
|
};
|
|
const connected: ConnectionIntentInteraction = {
|
|
...pending,
|
|
status: "accepted",
|
|
updatedAt: new Date("2026-08-26T12:04:00.000Z"),
|
|
resolvedAt: new Date("2026-08-26T12:04:00.000Z"),
|
|
result: {
|
|
version: 1,
|
|
outcome: "connected",
|
|
connectionId: "connection-1",
|
|
},
|
|
};
|
|
const messages = buildIssueChatMessages({
|
|
comments: [
|
|
createComment({
|
|
id: "before",
|
|
body: "Before",
|
|
createdAt: new Date("2026-08-26T12:01:00.000Z"),
|
|
}),
|
|
createComment({
|
|
id: "after",
|
|
body: "After",
|
|
createdAt: new Date("2026-08-26T12:03:00.000Z"),
|
|
}),
|
|
],
|
|
// Models the brief overlap where live invalidation and polling each
|
|
// supplied a snapshot of the same interaction id.
|
|
interactions: [pending, connected],
|
|
timelineEvents: [],
|
|
linkedRuns: [],
|
|
liveRuns: [],
|
|
});
|
|
|
|
expect(messages.map((message) => message.id)).toEqual([
|
|
"before",
|
|
`interaction:${pending.id}`,
|
|
"after",
|
|
]);
|
|
const interactionMessages = messages.filter(
|
|
(message) => message.id === `interaction:${pending.id}`,
|
|
);
|
|
expect(interactionMessages).toHaveLength(1);
|
|
const custom = interactionMessages[0]?.metadata?.custom as {
|
|
interaction?: ConnectionIntentInteraction;
|
|
};
|
|
expect(custom.interaction?.status).toBe("accepted");
|
|
expect(custom.interaction?.result?.outcome).toBe("connected");
|
|
});
|
|
});
|
|
|
|
function createAgent(id: string, name: string): Agent {
|
|
return {
|
|
id,
|
|
companyId: "company-1",
|
|
name,
|
|
role: "engineer",
|
|
title: null,
|
|
icon: "code",
|
|
status: "active",
|
|
reportsTo: null,
|
|
capabilities: null,
|
|
adapterType: "codex_local",
|
|
adapterConfig: {},
|
|
runtimeConfig: {},
|
|
budgetMonthlyCents: 0,
|
|
spentMonthlyCents: 0,
|
|
lastHeartbeatAt: null,
|
|
metadata: {},
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
pauseReason: null,
|
|
pausedAt: null,
|
|
urlKey: "codexcoder",
|
|
permissions: { canCreateAgents: false },
|
|
} as Agent;
|
|
}
|
|
|
|
function createComment(
|
|
overrides: Partial<IssueChatComment> = {},
|
|
): IssueChatComment {
|
|
const authorAgentId = overrides.authorAgentId ?? null;
|
|
return {
|
|
id: "comment-1",
|
|
companyId: "company-1",
|
|
issueId: "issue-1",
|
|
authorAgentId: null,
|
|
authorUserId: "user-1",
|
|
body: "Hello",
|
|
authorType: authorAgentId ? "agent" : "user",
|
|
presentation: null,
|
|
metadata: null,
|
|
sourceTrust: null,
|
|
createdAt: new Date("2026-04-06T12:00:00.000Z"),
|
|
updatedAt: new Date("2026-04-06T12:00:00.000Z"),
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
function createInteraction(
|
|
overrides: Partial<SuggestTasksInteraction> = {},
|
|
): SuggestTasksInteraction {
|
|
return {
|
|
id: "interaction-1",
|
|
companyId: "company-1",
|
|
issueId: "issue-1",
|
|
kind: "suggest_tasks",
|
|
title: "Suggested follow-up work",
|
|
summary: "Preview the next issue tree before accepting it.",
|
|
status: "pending",
|
|
continuationPolicy: "wake_assignee",
|
|
createdByAgentId: "agent-1",
|
|
createdByUserId: null,
|
|
resolvedByAgentId: null,
|
|
resolvedByUserId: null,
|
|
createdAt: new Date("2026-04-06T12:02:00.000Z"),
|
|
updatedAt: new Date("2026-04-06T12:02:00.000Z"),
|
|
resolvedAt: null,
|
|
payload: {
|
|
version: 1,
|
|
tasks: [
|
|
{
|
|
clientKey: "task-1",
|
|
title: "Prototype the card",
|
|
},
|
|
],
|
|
},
|
|
result: null,
|
|
...overrides,
|
|
resolverPolicy: overrides.resolverPolicy ?? "anyone",
|
|
requestedResolverPolicy: overrides.requestedResolverPolicy ?? "anyone",
|
|
effectiveResolverPolicy: overrides.effectiveResolverPolicy ?? "anyone",
|
|
resolverPolicyProvenance: overrides.resolverPolicyProvenance ?? "inherited",
|
|
effectiveResolverPolicySource:
|
|
overrides.effectiveResolverPolicySource ?? "requested",
|
|
legacyResolverPolicyAliases: overrides.legacyResolverPolicyAliases ?? {
|
|
requested: "board_or_agents",
|
|
effective: "board_or_agents",
|
|
},
|
|
};
|
|
}
|
|
|
|
function createRequestConfirmation(
|
|
overrides: Partial<RequestConfirmationInteraction> = {},
|
|
): RequestConfirmationInteraction {
|
|
return {
|
|
id: "confirmation-1",
|
|
companyId: "company-1",
|
|
issueId: "issue-1",
|
|
kind: "request_confirmation",
|
|
title: "Approve the plan",
|
|
summary: "Review and approve the latest plan.",
|
|
status: "pending",
|
|
continuationPolicy: "wake_assignee",
|
|
createdByAgentId: "agent-1",
|
|
createdByUserId: null,
|
|
resolvedByAgentId: null,
|
|
resolvedByUserId: null,
|
|
createdAt: new Date("2026-04-06T12:01:00.000Z"),
|
|
updatedAt: new Date("2026-04-06T12:01:00.000Z"),
|
|
resolvedAt: null,
|
|
payload: {
|
|
version: 1,
|
|
prompt: "Approve the plan?",
|
|
},
|
|
result: null,
|
|
...overrides,
|
|
resolverPolicy: overrides.resolverPolicy ?? "anyone",
|
|
requestedResolverPolicy: overrides.requestedResolverPolicy ?? "anyone",
|
|
effectiveResolverPolicy: overrides.effectiveResolverPolicy ?? "anyone",
|
|
resolverPolicyProvenance: overrides.resolverPolicyProvenance ?? "inherited",
|
|
effectiveResolverPolicySource:
|
|
overrides.effectiveResolverPolicySource ?? "requested",
|
|
legacyResolverPolicyAliases: overrides.legacyResolverPolicyAliases ?? {
|
|
requested: "board_or_agents",
|
|
effective: "board_or_agents",
|
|
},
|
|
};
|
|
}
|
|
|
|
describe("buildAssistantPartsFromTranscript", () => {
|
|
it("keeps canonical provider activity structured for the task-thread widget", () => {
|
|
const result = buildAssistantPartsFromTranscript([{
|
|
kind: "provider_activity",
|
|
ts: "2026-08-21T12:00:00.000Z",
|
|
family: "plan",
|
|
eventType: "plan.updated",
|
|
status: "completed",
|
|
title: "Plan",
|
|
summary: "Plan synchronized",
|
|
payload: { steps: [{ stepId: "s1", body: "Validate schemas", status: "completed" }] },
|
|
}]);
|
|
expect(result.parts).toHaveLength(1);
|
|
expect(result.parts[0]).toMatchObject({
|
|
type: "tool-call",
|
|
toolName: "paperclip_provider_activity",
|
|
args: {
|
|
family: "plan",
|
|
eventType: "plan.updated",
|
|
payload: { steps: [{ body: "Validate schemas" }] },
|
|
},
|
|
result: { status: "completed" },
|
|
});
|
|
expect(JSON.stringify(result.parts)).not.toContain("reasoning");
|
|
});
|
|
|
|
it("maps assistant text, reasoning, and tool activity while omitting noisy stderr", () => {
|
|
const result = buildAssistantPartsFromTranscript([
|
|
{
|
|
kind: "assistant",
|
|
ts: "2026-04-06T12:00:00.000Z",
|
|
text: "Working on it. ",
|
|
},
|
|
{ kind: "assistant", ts: "2026-04-06T12:00:01.000Z", text: "Done." },
|
|
{
|
|
kind: "thinking",
|
|
ts: "2026-04-06T12:00:02.000Z",
|
|
text: "Need to inspect files.",
|
|
},
|
|
{
|
|
kind: "tool_call",
|
|
ts: "2026-04-06T12:00:03.000Z",
|
|
name: "read_file",
|
|
toolUseId: "tool-1",
|
|
input: { path: "ui/src/pages/IssueDetail.tsx" },
|
|
},
|
|
{
|
|
kind: "tool_result",
|
|
ts: "2026-04-06T12:00:04.000Z",
|
|
toolUseId: "tool-1",
|
|
content: "file contents",
|
|
isError: false,
|
|
},
|
|
{
|
|
kind: "stderr",
|
|
ts: "2026-04-06T12:00:05.000Z",
|
|
text: "warn: noisy setup output",
|
|
},
|
|
]);
|
|
|
|
expect(result.parts).toHaveLength(3);
|
|
expect(result.parts[0]).toMatchObject({
|
|
type: "text",
|
|
text: "Working on it. Done.",
|
|
});
|
|
expect(result.parts[1]).toMatchObject({
|
|
type: "reasoning",
|
|
text: "Need to inspect files.",
|
|
});
|
|
expect(result.parts[2]).toMatchObject({
|
|
type: "tool-call",
|
|
toolCallId: "tool-1",
|
|
toolName: "read_file",
|
|
result: "file contents",
|
|
isError: false,
|
|
});
|
|
expect(result.notices).toEqual([]);
|
|
});
|
|
|
|
it("accumulates streamed tool output before the completed result", () => {
|
|
const result = buildAssistantPartsFromTranscript([
|
|
{ kind: "tool_call", ts: "2026-04-06T12:00:00.000Z", name: "shell", toolUseId: "tool-1", input: {} },
|
|
{ kind: "tool_result", ts: "2026-04-06T12:00:01.000Z", toolUseId: "tool-1", content: "first\n", delta: true },
|
|
{ kind: "tool_result", ts: "2026-04-06T12:00:02.000Z", toolUseId: "tool-1", content: "second\n", delta: true },
|
|
{ kind: "tool_result", ts: "2026-04-06T12:00:03.000Z", toolUseId: "tool-1", content: "", isError: false },
|
|
]);
|
|
|
|
expect(result.parts).toMatchObject([
|
|
{ type: "tool-call", toolCallId: "tool-1", result: "first\nsecond\n", isError: false },
|
|
]);
|
|
});
|
|
|
|
it("preserves transcript ordering when text and tool activity are interleaved", () => {
|
|
const result = buildAssistantPartsFromTranscript([
|
|
{ kind: "assistant", ts: "2026-04-06T12:00:00.000Z", text: "First." },
|
|
{
|
|
kind: "tool_call",
|
|
ts: "2026-04-06T12:00:01.000Z",
|
|
name: "read_file",
|
|
toolUseId: "tool-1",
|
|
input: { path: "ui/src/components/IssueChatThread.tsx" },
|
|
},
|
|
{ kind: "assistant", ts: "2026-04-06T12:00:02.000Z", text: "Second." },
|
|
{
|
|
kind: "tool_result",
|
|
ts: "2026-04-06T12:00:03.000Z",
|
|
toolUseId: "tool-1",
|
|
content: "ok",
|
|
isError: false,
|
|
},
|
|
{
|
|
kind: "thinking",
|
|
ts: "2026-04-06T12:00:04.000Z",
|
|
text: "Need one more check.",
|
|
},
|
|
{
|
|
kind: "tool_call",
|
|
ts: "2026-04-06T12:00:05.000Z",
|
|
name: "write_file",
|
|
toolUseId: "tool-2",
|
|
input: { path: "ui/src/lib/issue-chat-messages.ts" },
|
|
},
|
|
{
|
|
kind: "tool_result",
|
|
ts: "2026-04-06T12:00:06.000Z",
|
|
toolUseId: "tool-2",
|
|
content: "saved",
|
|
isError: false,
|
|
},
|
|
]);
|
|
|
|
expect(result.parts).toMatchObject([
|
|
{ type: "text", text: "First." },
|
|
{
|
|
type: "tool-call",
|
|
toolCallId: "tool-1",
|
|
toolName: "read_file",
|
|
result: "ok",
|
|
},
|
|
{ type: "text", text: "Second." },
|
|
{ type: "reasoning", text: "Need one more check." },
|
|
{
|
|
type: "tool-call",
|
|
toolCallId: "tool-2",
|
|
toolName: "write_file",
|
|
result: "saved",
|
|
},
|
|
]);
|
|
});
|
|
|
|
it("treats a completed tool-only segment as resolved once a tool_result arrives", () => {
|
|
const result = buildAssistantPartsFromTranscript([
|
|
{
|
|
kind: "thinking",
|
|
ts: "2026-04-06T12:00:00.000Z",
|
|
text: "Checking the task.",
|
|
},
|
|
{
|
|
kind: "tool_call",
|
|
ts: "2026-04-06T12:00:01.000Z",
|
|
name: "search",
|
|
toolUseId: "tool-1",
|
|
input: { query: "paperclip" },
|
|
},
|
|
{
|
|
kind: "tool_result",
|
|
ts: "2026-04-06T12:00:02.000Z",
|
|
toolUseId: "tool-1",
|
|
content: "search completed",
|
|
isError: false,
|
|
},
|
|
{
|
|
kind: "assistant",
|
|
ts: "2026-04-06T12:00:03.000Z",
|
|
text: "Found the relevant code.",
|
|
},
|
|
]);
|
|
|
|
expect(result.parts).toMatchObject([
|
|
{ type: "reasoning", text: "Checking the task." },
|
|
{
|
|
type: "tool-call",
|
|
toolCallId: "tool-1",
|
|
toolName: "search",
|
|
result: "search completed",
|
|
isError: false,
|
|
},
|
|
{ type: "text", text: "Found the relevant code." },
|
|
]);
|
|
expect(result.segments).toEqual([
|
|
{
|
|
startMs: new Date("2026-04-06T12:00:00.000Z").getTime(),
|
|
endMs: new Date("2026-04-06T12:00:02.000Z").getTime(),
|
|
},
|
|
]);
|
|
});
|
|
|
|
it("marks only the latest chain-of-thought segment active while a run is live", () => {
|
|
expect(
|
|
isCoTSegmentActive({
|
|
isMessageRunning: true,
|
|
segmentIndex: 0,
|
|
segmentCount: 2,
|
|
}),
|
|
).toBe(false);
|
|
expect(
|
|
isCoTSegmentActive({
|
|
isMessageRunning: true,
|
|
segmentIndex: 1,
|
|
segmentCount: 2,
|
|
}),
|
|
).toBe(true);
|
|
expect(
|
|
isCoTSegmentActive({
|
|
isMessageRunning: false,
|
|
segmentIndex: 1,
|
|
segmentCount: 2,
|
|
}),
|
|
).toBe(false);
|
|
});
|
|
|
|
it("keeps run errors while suppressing init and system transcript noise", () => {
|
|
const result = buildAssistantPartsFromTranscript([
|
|
{
|
|
kind: "init",
|
|
ts: "2026-04-06T12:00:00.000Z",
|
|
model: "gpt-5.4",
|
|
sessionId: "session-123",
|
|
},
|
|
{
|
|
kind: "system",
|
|
ts: "2026-04-06T12:00:01.000Z",
|
|
text: "item started: planning_step (id=step-1)",
|
|
},
|
|
{
|
|
kind: "system",
|
|
ts: "2026-04-06T12:00:02.000Z",
|
|
text: "item completed: planning_step (id=step-1)",
|
|
},
|
|
{
|
|
kind: "result",
|
|
ts: "2026-04-06T12:00:03.000Z",
|
|
text: "Tool crashed during execution",
|
|
inputTokens: 0,
|
|
outputTokens: 0,
|
|
cachedTokens: 0,
|
|
costUsd: 0,
|
|
subtype: "error",
|
|
isError: true,
|
|
errors: ["ENOENT: missing file"],
|
|
},
|
|
]);
|
|
|
|
expect(result.parts).toMatchObject([
|
|
{
|
|
type: "reasoning",
|
|
text: "Run error: ENOENT: missing file",
|
|
},
|
|
]);
|
|
expect(result.notices).toEqual([]);
|
|
});
|
|
|
|
it("preserves diff transcript output as a fenced diff block", () => {
|
|
const result = buildAssistantPartsFromTranscript([
|
|
{
|
|
kind: "assistant",
|
|
ts: "2026-04-06T12:00:00.000Z",
|
|
text: "Applied the patch.",
|
|
},
|
|
{
|
|
kind: "diff",
|
|
ts: "2026-04-06T12:00:01.000Z",
|
|
changeType: "file_header",
|
|
text: "ui/src/lib/issue-chat-messages.ts",
|
|
},
|
|
{
|
|
kind: "diff",
|
|
ts: "2026-04-06T12:00:02.000Z",
|
|
changeType: "add",
|
|
text: "+function formatDiffBlock(lines: string[]) {",
|
|
},
|
|
{
|
|
kind: "diff",
|
|
ts: "2026-04-06T12:00:03.000Z",
|
|
changeType: "add",
|
|
text: "+ return ````diff`;",
|
|
},
|
|
]);
|
|
|
|
expect(result.parts).toMatchObject([
|
|
{ type: "text", text: "Applied the patch." },
|
|
{
|
|
type: "text",
|
|
text: [
|
|
"```diff",
|
|
"ui/src/lib/issue-chat-messages.ts",
|
|
"+function formatDiffBlock(lines: string[]) {",
|
|
"+ return ````diff`;",
|
|
"```",
|
|
].join("\n"),
|
|
},
|
|
]);
|
|
});
|
|
});
|
|
|
|
describe("buildIssueChatMessages", () => {
|
|
it("uses the company user label for current-user comments instead of collapsing to You", () => {
|
|
const messages = buildIssueChatMessages({
|
|
comments: [createComment({ authorUserId: "user-1" })],
|
|
timelineEvents: [],
|
|
linkedRuns: [],
|
|
liveRuns: [],
|
|
currentUserId: "user-1",
|
|
userLabelMap: new Map([["user-1", "Dotta"]]),
|
|
});
|
|
|
|
expect(messages[0]).toMatchObject({
|
|
role: "user",
|
|
metadata: {
|
|
custom: {
|
|
authorName: "Dotta",
|
|
authorUserId: "user-1",
|
|
},
|
|
},
|
|
});
|
|
});
|
|
|
|
it("flags an operator-interrupted historical run so the timeline can read 'interrupted'", () => {
|
|
const messages = buildIssueChatMessages({
|
|
comments: [],
|
|
timelineEvents: [],
|
|
linkedRuns: [
|
|
{
|
|
runId: "run-int",
|
|
status: "cancelled",
|
|
agentId: "agent-1",
|
|
createdAt: new Date("2026-04-06T12:01:00.000Z"),
|
|
startedAt: new Date("2026-04-06T12:01:00.000Z"),
|
|
finishedAt: new Date("2026-04-06T12:02:00.000Z"),
|
|
resultJson: {
|
|
operatorInterrupted: true,
|
|
interruptionSource: "issue_comment_interrupt",
|
|
},
|
|
},
|
|
{
|
|
runId: "run-plain",
|
|
status: "cancelled",
|
|
agentId: "agent-1",
|
|
createdAt: new Date("2026-04-06T12:03:00.000Z"),
|
|
startedAt: new Date("2026-04-06T12:03:00.000Z"),
|
|
finishedAt: new Date("2026-04-06T12:04:00.000Z"),
|
|
resultJson: null,
|
|
},
|
|
],
|
|
liveRuns: [],
|
|
});
|
|
|
|
const interrupted = messages.find(
|
|
(message) => message.id === "run-assistant:run-int",
|
|
);
|
|
const plain = messages.find(
|
|
(message) => message.id === "run-assistant:run-plain",
|
|
);
|
|
expect(interrupted?.metadata?.custom).toMatchObject({
|
|
runOperatorInterrupted: true,
|
|
});
|
|
expect(plain?.metadata?.custom).toMatchObject({
|
|
runOperatorInterrupted: false,
|
|
});
|
|
});
|
|
|
|
it("redacts deleted comment bodies while preserving tombstone metadata", () => {
|
|
const messages = buildIssueChatMessages({
|
|
comments: [
|
|
createComment({
|
|
body: "Sensitive deleted body",
|
|
deletedAt: new Date("2026-04-06T12:05:00.000Z"),
|
|
deletedByType: "user",
|
|
deletedByUserId: "user-1",
|
|
}),
|
|
],
|
|
timelineEvents: [],
|
|
linkedRuns: [],
|
|
liveRuns: [],
|
|
currentUserId: "user-1",
|
|
userLabelMap: new Map([["user-1", "Dotta"]]),
|
|
});
|
|
|
|
expect(messages[0]?.content).toEqual([{ type: "text", text: "" }]);
|
|
expect(messages[0]?.metadata.custom).toMatchObject({
|
|
deletedAt: "2026-04-06T12:05:00.000Z",
|
|
deletedByType: "user",
|
|
deletedByUserId: "user-1",
|
|
});
|
|
expect(JSON.stringify(messages[0])).not.toContain("Sensitive deleted body");
|
|
});
|
|
|
|
it("preserves low-trust source metadata on comment messages", () => {
|
|
const messages = buildIssueChatMessages({
|
|
comments: [
|
|
createComment({
|
|
authorAgentId: "agent-1",
|
|
authorUserId: null,
|
|
sourceTrust: {
|
|
preset: "low_trust_review",
|
|
disposition: "quarantined",
|
|
sourceAgentId: "agent-1",
|
|
},
|
|
}),
|
|
],
|
|
timelineEvents: [],
|
|
linkedRuns: [],
|
|
liveRuns: [],
|
|
agentMap: new Map([
|
|
["agent-1", createAgent("agent-1", "Low Trust Reviewer")],
|
|
]),
|
|
});
|
|
|
|
expect(messages[0]?.metadata.custom.sourceTrust).toMatchObject({
|
|
preset: "low_trust_review",
|
|
disposition: "quarantined",
|
|
sourceAgentId: "agent-1",
|
|
});
|
|
});
|
|
|
|
it("prefers derived agent attribution when a board-authored comment is proven to come from a run", () => {
|
|
const agentMap = new Map<string, Agent>([
|
|
["agent-1", createAgent("agent-1", "Claude")],
|
|
]);
|
|
const messages = buildIssueChatMessages({
|
|
comments: [
|
|
createComment({
|
|
authorUserId: "user-1",
|
|
derivedAuthorAgentId: "agent-1",
|
|
derivedCreatedByRunId: "run-1",
|
|
}),
|
|
],
|
|
timelineEvents: [],
|
|
linkedRuns: [],
|
|
liveRuns: [],
|
|
agentMap,
|
|
currentUserId: "user-1",
|
|
userLabelMap: new Map([["user-1", "Dotta"]]),
|
|
});
|
|
|
|
expect(messages[0]).toMatchObject({
|
|
role: "assistant",
|
|
metadata: {
|
|
custom: {
|
|
authorName: "Claude",
|
|
authorType: "agent",
|
|
authorAgentId: "agent-1",
|
|
authorUserId: "user-1",
|
|
runId: "run-1",
|
|
runAgentId: "agent-1",
|
|
},
|
|
},
|
|
});
|
|
});
|
|
|
|
it("does not reattribute a genuine board/user comment that has no derived agent", () => {
|
|
const agentMap = new Map<string, Agent>([
|
|
["agent-1", createAgent("agent-1", "Claude")],
|
|
]);
|
|
const messages = buildIssueChatMessages({
|
|
comments: [
|
|
createComment({
|
|
authorUserId: "local-board",
|
|
authorType: "user",
|
|
// No agent ever resolved for this comment — a real board action.
|
|
derivedAuthorAgentId: null,
|
|
derivedCreatedByRunId: null,
|
|
runId: null,
|
|
runAgentId: null,
|
|
}),
|
|
],
|
|
timelineEvents: [],
|
|
linkedRuns: [],
|
|
liveRuns: [],
|
|
agentMap,
|
|
currentUserId: "user-1",
|
|
userLabelMap: new Map([["local-board", "Board"]]),
|
|
});
|
|
|
|
expect(messages[0]).toMatchObject({
|
|
role: "user",
|
|
metadata: {
|
|
custom: {
|
|
authorType: "user",
|
|
authorAgentId: null,
|
|
authorUserId: "local-board",
|
|
},
|
|
},
|
|
});
|
|
});
|
|
|
|
it("renders a comment as agent-authored when runAgentId is set from activity log", () => {
|
|
const agentMap = new Map<string, Agent>([
|
|
["agent-1", createAgent("agent-1", "Claude")],
|
|
]);
|
|
const messages = buildIssueChatMessages({
|
|
comments: [
|
|
createComment({
|
|
authorUserId: "user-1",
|
|
runId: "run-1",
|
|
runAgentId: "agent-1",
|
|
}),
|
|
],
|
|
timelineEvents: [],
|
|
linkedRuns: [],
|
|
liveRuns: [],
|
|
agentMap,
|
|
currentUserId: "user-1",
|
|
userLabelMap: new Map([["user-1", "Dotta"]]),
|
|
});
|
|
|
|
expect(messages[0]).toMatchObject({
|
|
role: "assistant",
|
|
metadata: {
|
|
custom: {
|
|
authorName: "Claude",
|
|
authorType: "agent",
|
|
authorAgentId: "agent-1",
|
|
authorUserId: "user-1",
|
|
runId: "run-1",
|
|
runAgentId: "agent-1",
|
|
},
|
|
},
|
|
});
|
|
});
|
|
|
|
it("orders events before comments and appends active live runs as running assistant messages", () => {
|
|
const agentMap = new Map<string, Agent>([
|
|
["agent-1", createAgent("agent-1", "CodexCoder")],
|
|
]);
|
|
const comments = [
|
|
createComment(),
|
|
createComment({
|
|
id: "comment-2",
|
|
authorAgentId: "agent-1",
|
|
authorUserId: null,
|
|
body: "I made the change.",
|
|
createdAt: new Date("2026-04-06T12:03:00.000Z"),
|
|
updatedAt: new Date("2026-04-06T12:03:00.000Z"),
|
|
runId: "run-1",
|
|
runAgentId: "agent-1",
|
|
}),
|
|
];
|
|
const timelineEvents: IssueTimelineEvent[] = [
|
|
{
|
|
id: "event-1",
|
|
createdAt: new Date("2026-04-06T11:59:00.000Z"),
|
|
actorType: "user",
|
|
actorId: "user-1",
|
|
statusChange: {
|
|
from: "done",
|
|
to: "todo",
|
|
},
|
|
},
|
|
];
|
|
const linkedRuns: IssueChatLinkedRun[] = [
|
|
{
|
|
runId: "run-history-1",
|
|
status: "succeeded",
|
|
agentId: "agent-1",
|
|
createdAt: new Date("2026-04-06T12:01:00.000Z"),
|
|
startedAt: new Date("2026-04-06T12:01:00.000Z"),
|
|
finishedAt: new Date("2026-04-06T12:02:00.000Z"),
|
|
},
|
|
];
|
|
const liveRuns: LiveRunForIssue[] = [
|
|
{
|
|
id: "run-live-1",
|
|
status: "running",
|
|
invocationSource: "manual",
|
|
triggerDetail: null,
|
|
startedAt: "2026-04-06T12:04:00.000Z",
|
|
finishedAt: null,
|
|
createdAt: "2026-04-06T12:04:00.000Z",
|
|
agentId: "agent-1",
|
|
agentName: "CodexCoder",
|
|
adapterType: "codex_local",
|
|
},
|
|
];
|
|
|
|
const messages = buildIssueChatMessages({
|
|
comments,
|
|
timelineEvents,
|
|
linkedRuns,
|
|
liveRuns,
|
|
transcriptsByRunId: new Map([
|
|
[
|
|
"run-live-1",
|
|
[
|
|
{
|
|
kind: "assistant",
|
|
ts: "2026-04-06T12:04:01.000Z",
|
|
text: "Streaming reply",
|
|
},
|
|
],
|
|
],
|
|
]),
|
|
hasOutputForRun: (runId) => runId === "run-live-1",
|
|
companyId: "company-1",
|
|
projectId: "project-1",
|
|
agentMap,
|
|
currentUserId: "user-1",
|
|
});
|
|
|
|
expect(messages.map((message) => `${message.role}:${message.id}`)).toEqual([
|
|
"system:activity:event-1",
|
|
"user:comment-1",
|
|
"assistant:comment-2",
|
|
"assistant:run-assistant:run-live-1",
|
|
]);
|
|
|
|
const liveRunMessage = messages.at(-1);
|
|
expect(liveRunMessage).toMatchObject({
|
|
role: "assistant",
|
|
status: { type: "running" },
|
|
});
|
|
expect(liveRunMessage?.content[0]).toMatchObject({
|
|
type: "text",
|
|
text: "Streaming reply",
|
|
});
|
|
});
|
|
|
|
it("suppresses live-run Working messages for terminal issues", () => {
|
|
const liveRun: LiveRunForIssue = {
|
|
id: "run-live-terminal",
|
|
status: "running",
|
|
invocationSource: "manual",
|
|
triggerDetail: null,
|
|
startedAt: "2026-04-06T12:04:00.000Z",
|
|
finishedAt: null,
|
|
createdAt: "2026-04-06T12:04:00.000Z",
|
|
agentId: "agent-1",
|
|
agentName: "CodexCoder",
|
|
adapterType: "codex_local",
|
|
};
|
|
|
|
const terminalMessages = buildIssueChatMessages({
|
|
comments: [],
|
|
timelineEvents: [],
|
|
linkedRuns: [],
|
|
liveRuns: [liveRun],
|
|
issueStatus: "done",
|
|
currentUserId: "user-1",
|
|
});
|
|
const liveMessages = buildIssueChatMessages({
|
|
comments: [],
|
|
timelineEvents: [],
|
|
linkedRuns: [],
|
|
liveRuns: [liveRun],
|
|
issueStatus: "in_progress",
|
|
currentUserId: "user-1",
|
|
});
|
|
|
|
expect(
|
|
terminalMessages.find(
|
|
(message) => message.id === "run-assistant:run-live-terminal",
|
|
),
|
|
).toBeUndefined();
|
|
expect(
|
|
liveMessages.find(
|
|
(message) => message.id === "run-assistant:run-live-terminal",
|
|
),
|
|
).toMatchObject({
|
|
status: { type: "running" },
|
|
metadata: { custom: { waitingText: "Working..." } },
|
|
});
|
|
});
|
|
|
|
it("merges thread interactions into the same chronological feed as comments and runs", () => {
|
|
const messages = buildIssueChatMessages({
|
|
comments: [
|
|
createComment({
|
|
id: "comment-1",
|
|
createdAt: new Date("2026-04-06T12:01:00.000Z"),
|
|
updatedAt: new Date("2026-04-06T12:01:00.000Z"),
|
|
}),
|
|
],
|
|
interactions: [
|
|
createInteraction({
|
|
id: "interaction-2",
|
|
createdAt: new Date("2026-04-06T12:02:00.000Z"),
|
|
updatedAt: new Date("2026-04-06T12:02:00.000Z"),
|
|
}),
|
|
],
|
|
timelineEvents: [],
|
|
linkedRuns: [],
|
|
liveRuns: [
|
|
{
|
|
id: "run-live-1",
|
|
status: "running",
|
|
invocationSource: "manual",
|
|
triggerDetail: null,
|
|
startedAt: "2026-04-06T12:03:00.000Z",
|
|
finishedAt: null,
|
|
createdAt: "2026-04-06T12:03:00.000Z",
|
|
agentId: "agent-1",
|
|
agentName: "CodexCoder",
|
|
adapterType: "codex_local",
|
|
},
|
|
],
|
|
transcriptsByRunId: new Map([
|
|
[
|
|
"run-live-1",
|
|
[
|
|
{
|
|
kind: "assistant",
|
|
ts: "2026-04-06T12:03:01.000Z",
|
|
text: "Working on it.",
|
|
},
|
|
],
|
|
],
|
|
]),
|
|
hasOutputForRun: (runId) => runId === "run-live-1",
|
|
currentUserId: "user-1",
|
|
});
|
|
|
|
expect(messages.map((message) => `${message.role}:${message.id}`)).toEqual([
|
|
"user:comment-1",
|
|
"system:interaction:interaction-2",
|
|
"assistant:run-assistant:run-live-1",
|
|
]);
|
|
expect(messages[1]).toMatchObject({
|
|
metadata: {
|
|
custom: {
|
|
kind: "interaction",
|
|
anchorId: "interaction-interaction-2",
|
|
},
|
|
},
|
|
});
|
|
});
|
|
|
|
it("drops degenerate ask_user_questions interactions so they leave no empty slot (PAP-424)", () => {
|
|
function askInteraction(
|
|
id: string,
|
|
questions: AskUserQuestionsQuestion[],
|
|
): AskUserQuestionsInteraction {
|
|
return {
|
|
id,
|
|
companyId: "company-1",
|
|
issueId: "issue-1",
|
|
kind: "ask_user_questions",
|
|
title: null,
|
|
summary: null,
|
|
status: "pending",
|
|
continuationPolicy: "wake_assignee",
|
|
createdByAgentId: "agent-1",
|
|
createdByUserId: null,
|
|
resolvedByAgentId: null,
|
|
resolvedByUserId: null,
|
|
createdAt: new Date("2026-04-06T12:02:00.000Z"),
|
|
updatedAt: new Date("2026-04-06T12:02:00.000Z"),
|
|
resolvedAt: null,
|
|
resolverPolicy: "anyone",
|
|
requestedResolverPolicy: "anyone",
|
|
effectiveResolverPolicy: "anyone",
|
|
resolverPolicyProvenance: "inherited",
|
|
effectiveResolverPolicySource: "requested",
|
|
legacyResolverPolicyAliases: {
|
|
requested: "board_or_agents",
|
|
effective: "board_or_agents",
|
|
},
|
|
payload: { version: 1, questions },
|
|
result: null,
|
|
} as AskUserQuestionsInteraction;
|
|
}
|
|
|
|
const messages = buildIssueChatMessages({
|
|
comments: [
|
|
createComment({
|
|
id: "comment-1",
|
|
createdAt: new Date("2026-04-06T12:01:00.000Z"),
|
|
updatedAt: new Date("2026-04-06T12:01:00.000Z"),
|
|
}),
|
|
],
|
|
interactions: [
|
|
// A truly unanswerable card (no options, no free-text) — must be
|
|
// filtered out entirely.
|
|
askInteraction("interaction-degenerate", [
|
|
{
|
|
id: "q1",
|
|
prompt: "Anything?",
|
|
selectionMode: "single",
|
|
options: [],
|
|
},
|
|
]),
|
|
// A legitimate yes/no question survives.
|
|
askInteraction("interaction-legit", [
|
|
{
|
|
id: "q1",
|
|
prompt: "Ship it?",
|
|
selectionMode: "single",
|
|
options: [
|
|
{ id: "yes", label: "Yes" },
|
|
{ id: "no", label: "No" },
|
|
],
|
|
},
|
|
]),
|
|
],
|
|
timelineEvents: [],
|
|
linkedRuns: [],
|
|
liveRuns: [],
|
|
currentUserId: "user-1",
|
|
});
|
|
|
|
const ids = messages.map((message) => `${message.role}:${message.id}`);
|
|
// The legit card is present; the degenerate one leaves no message at all.
|
|
expect(ids).toEqual([
|
|
"user:comment-1",
|
|
"system:interaction:interaction-legit",
|
|
]);
|
|
expect(ids).not.toContain("system:interaction:interaction-degenerate");
|
|
});
|
|
|
|
it("preserves ephemeral active-run status metadata for rendering", () => {
|
|
const activeRun: ActiveRunForIssue = {
|
|
id: "run-active-1",
|
|
status: "running",
|
|
invocationSource: "manual",
|
|
triggerDetail: null,
|
|
startedAt: "2026-04-06T12:03:00.000Z",
|
|
finishedAt: null,
|
|
createdAt: "2026-04-06T12:03:00.000Z",
|
|
agentId: "agent-1",
|
|
agentName: "CodexCoder",
|
|
adapterType: "codex_local",
|
|
currentStatusMessage: "Syncing git worktree to environment",
|
|
currentStatusUpdatedAt: "2026-04-06T12:03:05.000Z",
|
|
currentToolName: "bash",
|
|
lastAssistantSnippet: "Checking repository status",
|
|
lastEventAt: "2026-04-06T12:03:08.000Z",
|
|
};
|
|
|
|
const messages = buildIssueChatMessages({
|
|
comments: [],
|
|
timelineEvents: [],
|
|
linkedRuns: [],
|
|
liveRuns: [],
|
|
activeRun,
|
|
currentUserId: "user-1",
|
|
});
|
|
|
|
expect(messages).toHaveLength(1);
|
|
expect(messages[0]).toMatchObject({
|
|
role: "assistant",
|
|
status: { type: "running" },
|
|
metadata: {
|
|
custom: {
|
|
kind: "live-run",
|
|
runId: "run-active-1",
|
|
currentStatusMessage: "Syncing git worktree to environment",
|
|
currentStatusUpdatedAt: "2026-04-06T12:03:05.000Z",
|
|
currentToolName: "bash",
|
|
lastAssistantSnippet: "Checking repository status",
|
|
lastEventAt: "2026-04-06T12:03:08.000Z",
|
|
},
|
|
},
|
|
});
|
|
});
|
|
|
|
it("places request confirmations after later same-run handoff status and comment", () => {
|
|
const messages = buildIssueChatMessages({
|
|
comments: [
|
|
createComment({
|
|
id: "comment-handoff",
|
|
authorAgentId: "agent-1",
|
|
authorUserId: null,
|
|
body: "Ready for approval.",
|
|
createdAt: new Date("2026-04-06T12:03:00.000Z"),
|
|
updatedAt: new Date("2026-04-06T12:03:00.000Z"),
|
|
runId: "run-1",
|
|
runAgentId: "agent-1",
|
|
}),
|
|
createComment({
|
|
id: "comment-user-reply",
|
|
body: "Approved.",
|
|
createdAt: new Date("2026-04-06T12:04:00.000Z"),
|
|
updatedAt: new Date("2026-04-06T12:04:00.000Z"),
|
|
}),
|
|
],
|
|
interactions: [
|
|
createRequestConfirmation({
|
|
id: "confirmation-1",
|
|
sourceRunId: "run-1",
|
|
status: "expired",
|
|
result: {
|
|
version: 1,
|
|
outcome: "superseded_by_comment",
|
|
commentId: "comment-user-reply",
|
|
},
|
|
}),
|
|
],
|
|
timelineEvents: [
|
|
{
|
|
id: "event-in-review",
|
|
actorType: "agent",
|
|
actorId: "agent-1",
|
|
createdAt: new Date("2026-04-06T12:02:00.000Z"),
|
|
runId: "run-1",
|
|
statusChange: {
|
|
from: "in_progress",
|
|
to: "in_review",
|
|
},
|
|
},
|
|
],
|
|
linkedRuns: [],
|
|
liveRuns: [],
|
|
currentUserId: "user-1",
|
|
});
|
|
|
|
expect(messages.map((message) => `${message.role}:${message.id}`)).toEqual([
|
|
"system:activity:event-in-review",
|
|
"assistant:comment-handoff",
|
|
"system:interaction:confirmation-1",
|
|
"user:comment-user-reply",
|
|
]);
|
|
});
|
|
|
|
it("keeps request confirmations chronological without later same-run handoff evidence", () => {
|
|
const messages = buildIssueChatMessages({
|
|
comments: [
|
|
createComment({
|
|
id: "comment-later",
|
|
createdAt: new Date("2026-04-06T12:02:00.000Z"),
|
|
updatedAt: new Date("2026-04-06T12:02:00.000Z"),
|
|
}),
|
|
],
|
|
interactions: [
|
|
createRequestConfirmation({
|
|
id: "confirmation-1",
|
|
sourceRunId: "run-1",
|
|
}),
|
|
],
|
|
timelineEvents: [],
|
|
linkedRuns: [],
|
|
liveRuns: [],
|
|
currentUserId: "user-1",
|
|
});
|
|
|
|
expect(messages.map((message) => `${message.role}:${message.id}`)).toEqual([
|
|
"system:interaction:confirmation-1",
|
|
"user:comment-later",
|
|
]);
|
|
});
|
|
|
|
it("does not move request confirmations past unrelated comments before same-run handoff", () => {
|
|
const messages = buildIssueChatMessages({
|
|
comments: [
|
|
createComment({
|
|
id: "comment-user-reply",
|
|
body: "I have a question first.",
|
|
createdAt: new Date("2026-04-06T12:02:00.000Z"),
|
|
updatedAt: new Date("2026-04-06T12:02:00.000Z"),
|
|
}),
|
|
createComment({
|
|
id: "comment-handoff",
|
|
authorAgentId: "agent-1",
|
|
authorUserId: null,
|
|
body: "Ready for approval.",
|
|
createdAt: new Date("2026-04-06T12:03:00.000Z"),
|
|
updatedAt: new Date("2026-04-06T12:03:00.000Z"),
|
|
runId: "run-1",
|
|
runAgentId: "agent-1",
|
|
}),
|
|
],
|
|
interactions: [
|
|
createRequestConfirmation({
|
|
id: "confirmation-1",
|
|
sourceRunId: "run-1",
|
|
}),
|
|
],
|
|
timelineEvents: [],
|
|
linkedRuns: [],
|
|
liveRuns: [],
|
|
currentUserId: "user-1",
|
|
});
|
|
|
|
expect(messages.map((message) => `${message.role}:${message.id}`)).toEqual([
|
|
"system:interaction:confirmation-1",
|
|
"user:comment-user-reply",
|
|
"assistant:comment-handoff",
|
|
]);
|
|
});
|
|
|
|
it("keeps succeeded runs as assistant messages when transcript output exists", () => {
|
|
const agentMap = new Map<string, Agent>([
|
|
["agent-1", createAgent("agent-1", "CodexCoder")],
|
|
]);
|
|
const messages = buildIssueChatMessages({
|
|
comments: [],
|
|
timelineEvents: [],
|
|
linkedRuns: [
|
|
{
|
|
runId: "run-history-1",
|
|
status: "succeeded",
|
|
agentId: "agent-1",
|
|
createdAt: new Date("2026-04-06T12:01:00.000Z"),
|
|
startedAt: new Date("2026-04-06T12:01:00.000Z"),
|
|
finishedAt: new Date("2026-04-06T12:03:00.000Z"),
|
|
},
|
|
],
|
|
liveRuns: [],
|
|
transcriptsByRunId: new Map([
|
|
[
|
|
"run-history-1",
|
|
[
|
|
{
|
|
kind: "thinking",
|
|
ts: "2026-04-06T12:01:10.000Z",
|
|
text: "Checking the current issue thread.",
|
|
},
|
|
{
|
|
kind: "assistant",
|
|
ts: "2026-04-06T12:02:30.000Z",
|
|
text: "Updated the thread renderer.",
|
|
},
|
|
],
|
|
],
|
|
]),
|
|
hasOutputForRun: (runId) => runId === "run-history-1",
|
|
agentMap,
|
|
currentUserId: "user-1",
|
|
});
|
|
|
|
expect(messages).toHaveLength(1);
|
|
expect(messages[0]).toMatchObject({
|
|
id: "run-assistant:run-history-1",
|
|
role: "assistant",
|
|
status: { type: "complete", reason: "stop" },
|
|
metadata: {
|
|
custom: {
|
|
kind: "historical-run",
|
|
runId: "run-history-1",
|
|
chainOfThoughtLabel: "Worked for 2 minutes",
|
|
},
|
|
},
|
|
});
|
|
expect(messages[0]?.content).toMatchObject([
|
|
{ type: "reasoning", text: "Checking the current issue thread." },
|
|
{ type: "text", text: "Updated the thread renderer." },
|
|
]);
|
|
});
|
|
|
|
it("compacts long run transcripts in issue chat while preserving matching tool context", () => {
|
|
const isoAt = (baseMs: number, offsetSeconds: number) =>
|
|
new Date(baseMs + offsetSeconds * 1000).toISOString();
|
|
const baseMs = Date.parse("2026-04-06T12:00:00.000Z");
|
|
const transcript = [
|
|
...Array.from({ length: 9 }, (_, index) => ({
|
|
kind: "assistant" as const,
|
|
ts: isoAt(baseMs, index),
|
|
text: `Older update ${index + 1}`,
|
|
})),
|
|
{
|
|
kind: "tool_call" as const,
|
|
ts: isoAt(baseMs, 9),
|
|
name: "search",
|
|
toolUseId: "tool-keep",
|
|
input: { query: "issue chat virtualization" },
|
|
},
|
|
...Array.from({ length: 79 }, (_, index) => ({
|
|
kind: "assistant" as const,
|
|
ts: isoAt(baseMs, 10 + index),
|
|
text: `Recent update ${index + 1}`,
|
|
})),
|
|
{
|
|
kind: "tool_result" as const,
|
|
ts: isoAt(baseMs, 89),
|
|
toolUseId: "tool-keep",
|
|
content: "search completed",
|
|
isError: false,
|
|
},
|
|
];
|
|
|
|
const messages = buildIssueChatMessages({
|
|
comments: [],
|
|
timelineEvents: [],
|
|
linkedRuns: [
|
|
{
|
|
runId: "run-history-3",
|
|
status: "succeeded",
|
|
agentId: "agent-1",
|
|
agentName: "CodexCoder",
|
|
createdAt: new Date("2026-04-06T12:00:00.000Z"),
|
|
startedAt: new Date("2026-04-06T12:00:00.000Z"),
|
|
finishedAt: new Date("2026-04-06T12:03:00.000Z"),
|
|
},
|
|
],
|
|
liveRuns: [],
|
|
transcriptsByRunId: new Map([["run-history-3", transcript]]),
|
|
hasOutputForRun: (runId) => runId === "run-history-3",
|
|
currentUserId: "user-1",
|
|
});
|
|
|
|
expect(messages).toHaveLength(1);
|
|
const textParts =
|
|
messages[0]?.content
|
|
.filter(
|
|
(part): part is { type: "text"; text: string } =>
|
|
part.type === "text",
|
|
)
|
|
.map((part) => part.text) ?? [];
|
|
expect(textParts.join("\n")).not.toContain("Older update 1");
|
|
expect(messages[0]?.content).toContainEqual(
|
|
expect.objectContaining({
|
|
type: "tool-call",
|
|
toolCallId: "tool-keep",
|
|
toolName: "search",
|
|
result: "search completed",
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("honors a wider transcript window declared by an adapter's UI module", () => {
|
|
// Capability path: an adapter (built-in or plugin) declares
|
|
// transcriptPresentation on its UI module; shared code resolves it via the
|
|
// registry with no adapter identities. The test registers a synthetic
|
|
// verbose adapter — the test right above pins the default 30-entry window
|
|
// for adapters that declare nothing.
|
|
registerUIAdapter({
|
|
type: "verbose_test_local",
|
|
label: "Verbose Test",
|
|
parseStdoutLine: () => [],
|
|
ConfigFields: () => null,
|
|
buildAdapterConfig: () => ({}),
|
|
transcriptPresentation: {
|
|
maxVisibleEntries: 400,
|
|
liveReasoningView: "scrollLog",
|
|
},
|
|
});
|
|
|
|
try {
|
|
const isoAt = (baseMs: number, offsetSeconds: number) =>
|
|
new Date(baseMs + offsetSeconds * 1000).toISOString();
|
|
const baseMs = Date.parse("2026-04-06T12:00:00.000Z");
|
|
// 90 renderable entries: over the default 30-entry window, under the
|
|
// declared 400-entry window, so nothing is trimmed.
|
|
const transcript = [
|
|
...Array.from({ length: 9 }, (_, index) => ({
|
|
kind: "assistant" as const,
|
|
ts: isoAt(baseMs, index),
|
|
text: `Older update ${index + 1}`,
|
|
})),
|
|
{
|
|
kind: "tool_call" as const,
|
|
ts: isoAt(baseMs, 9),
|
|
name: "search",
|
|
toolUseId: "tool-keep",
|
|
input: { query: "issue chat virtualization" },
|
|
},
|
|
...Array.from({ length: 79 }, (_, index) => ({
|
|
kind: "assistant" as const,
|
|
ts: isoAt(baseMs, 10 + index),
|
|
text: `Recent update ${index + 1}`,
|
|
})),
|
|
{
|
|
kind: "tool_result" as const,
|
|
ts: isoAt(baseMs, 89),
|
|
toolUseId: "tool-keep",
|
|
content: "search completed",
|
|
isError: false,
|
|
},
|
|
];
|
|
|
|
const messages = buildIssueChatMessages({
|
|
comments: [],
|
|
timelineEvents: [],
|
|
linkedRuns: [
|
|
{
|
|
runId: "run-history-verbose",
|
|
status: "succeeded",
|
|
agentId: "agent-1",
|
|
agentName: "VerboseCoder",
|
|
adapterType: "verbose_test_local",
|
|
createdAt: new Date("2026-04-06T12:00:00.000Z"),
|
|
startedAt: new Date("2026-04-06T12:00:00.000Z"),
|
|
finishedAt: new Date("2026-04-06T12:03:00.000Z"),
|
|
},
|
|
],
|
|
liveRuns: [],
|
|
transcriptsByRunId: new Map([["run-history-verbose", transcript]]),
|
|
hasOutputForRun: (runId) => runId === "run-history-verbose",
|
|
currentUserId: "user-1",
|
|
});
|
|
|
|
expect(messages).toHaveLength(1);
|
|
const textParts =
|
|
messages[0]?.content
|
|
.filter(
|
|
(part): part is { type: "text"; text: string } =>
|
|
part.type === "text",
|
|
)
|
|
.map((part) => part.text) ?? [];
|
|
expect(textParts.join("\n")).toContain("Older update 1");
|
|
expect(textParts.join("\n")).toContain("Recent update 79");
|
|
} finally {
|
|
unregisterUIAdapter("verbose_test_local");
|
|
}
|
|
});
|
|
|
|
it.each([
|
|
{ adapterType: "codex_local", status: "cancelled" },
|
|
{ adapterType: "paperclip_runner", status: "cancelled" },
|
|
{ adapterType: "codex_local", status: "failed" },
|
|
{ adapterType: "paperclip_runner", status: "failed" },
|
|
{ adapterType: "codex_local", status: "succeeded" },
|
|
{ adapterType: "paperclip_runner", status: "succeeded" },
|
|
])("settles a $status $adapterType run once while its live cache is stale", ({ adapterType, status }) => {
|
|
const startedAt = "2026-11-03T14:22:22.000Z";
|
|
const live: LiveRunForIssue = {
|
|
id: "run-stop", status: "running", invocationSource: "manual", triggerDetail: null,
|
|
createdAt: startedAt, startedAt, finishedAt: null,
|
|
agentId: "agent-1", agentName: "Agent", adapterType,
|
|
};
|
|
const transcript = [
|
|
{ kind: "assistant" as const, ts: startedAt, text: "I'll find the artifact directory and execute the foreground command." },
|
|
{ kind: "tool_call" as const, ts: startedAt, toolUseId: "foreground-command", name: "shell", input: { command: "sleep 300" } },
|
|
];
|
|
const messages = buildIssueChatMessages({
|
|
comments: [], timelineEvents: [], issueStatus: "in_progress",
|
|
linkedRuns: [{ runId: live.id, status, agentId: live.agentId, adapterType,
|
|
createdAt: startedAt, startedAt, finishedAt: "2026-11-03T14:24:19.000Z" }],
|
|
liveRuns: [live], transcriptsByRunId: new Map([[live.id, transcript]]),
|
|
});
|
|
|
|
expect(messages).toHaveLength(1);
|
|
expect(messages[0]).toMatchObject({
|
|
id: "run-assistant:run-stop", status: { type: "complete", reason: "stop" },
|
|
metadata: { custom: { kind: "historical-run", runStatus: status } },
|
|
});
|
|
expect(messages[0]?.content.filter((part) => part.type === "text")).toEqual([
|
|
{ type: "text", text: transcript[0].text },
|
|
]);
|
|
expect(messages[0]?.content.filter((part) => part.type === "tool-call")).toHaveLength(1);
|
|
});
|
|
|
|
it("keeps the live representation when history has not settled the same run", () => {
|
|
const createdAt = "2026-11-03T14:22:22.000Z";
|
|
const messages = buildIssueChatMessages({
|
|
comments: [], timelineEvents: [], issueStatus: "in_progress",
|
|
linkedRuns: [{ runId: "run-active", status: "queued", agentId: "agent-1", adapterType: "paperclip_runner", createdAt, startedAt: null }],
|
|
liveRuns: [{ id: "run-active", status: "running", agentId: "agent-1", agentName: "Agent", adapterType: "paperclip_runner",
|
|
invocationSource: "manual", triggerDetail: null, createdAt, startedAt: createdAt, finishedAt: null }],
|
|
});
|
|
|
|
expect(messages).toHaveLength(1);
|
|
expect(messages[0]).toMatchObject({
|
|
id: "run-assistant:run-active", status: { type: "running" },
|
|
metadata: { custom: { kind: "live-run", runStatus: "running" } },
|
|
});
|
|
});
|
|
|
|
it("keeps distinct runs when one stopped run hands off from live history", () => {
|
|
const createdAt = "2026-11-03T14:22:22.000Z";
|
|
const live = { status: "running" as const, agentId: "agent-1", agentName: "Agent", adapterType: "paperclip_runner",
|
|
invocationSource: "manual" as const, triggerDetail: null, createdAt, startedAt: createdAt, finishedAt: null };
|
|
const messages = buildIssueChatMessages({
|
|
comments: [], timelineEvents: [], issueStatus: "in_progress",
|
|
linkedRuns: [{ runId: "run-stop", status: "cancelled", agentId: live.agentId, adapterType: live.adapterType,
|
|
createdAt, startedAt: createdAt, finishedAt: "2026-11-03T14:24:19.000Z" }],
|
|
liveRuns: [{ ...live, id: "run-stop" }, { ...live, id: "run-other" }],
|
|
});
|
|
|
|
expect(messages.map(message => message.id)).toEqual(["run-assistant:run-stop", "run-assistant:run-other"]);
|
|
expect(messages[0]).toMatchObject({ metadata: { custom: { runStatus: "cancelled" } } });
|
|
expect(messages[1]).toMatchObject({ metadata: { custom: { runStatus: "running" } } });
|
|
});
|
|
|
|
it("keeps the same assistant message id when a live run becomes a cancelled historical run", () => {
|
|
const liveMessages = buildIssueChatMessages({
|
|
comments: [],
|
|
timelineEvents: [],
|
|
linkedRuns: [],
|
|
liveRuns: [
|
|
{
|
|
id: "run-1",
|
|
status: "running",
|
|
invocationSource: "manual",
|
|
triggerDetail: null,
|
|
startedAt: "2026-04-06T12:01:00.000Z",
|
|
finishedAt: null,
|
|
createdAt: "2026-04-06T12:01:00.000Z",
|
|
agentId: "agent-1",
|
|
agentName: "CodexCoder",
|
|
adapterType: "codex_local",
|
|
},
|
|
],
|
|
transcriptsByRunId: new Map([
|
|
[
|
|
"run-1",
|
|
[
|
|
{
|
|
kind: "assistant",
|
|
ts: "2026-04-06T12:01:05.000Z",
|
|
text: "Working on it.",
|
|
},
|
|
],
|
|
],
|
|
]),
|
|
hasOutputForRun: (runId) => runId === "run-1",
|
|
currentUserId: "user-1",
|
|
});
|
|
|
|
const cancelledMessages = buildIssueChatMessages({
|
|
comments: [],
|
|
timelineEvents: [],
|
|
linkedRuns: [
|
|
{
|
|
runId: "run-1",
|
|
status: "cancelled",
|
|
agentId: "agent-1",
|
|
agentName: "CodexCoder",
|
|
createdAt: new Date("2026-04-06T12:01:00.000Z"),
|
|
startedAt: new Date("2026-04-06T12:01:00.000Z"),
|
|
finishedAt: new Date("2026-04-06T12:01:08.000Z"),
|
|
},
|
|
],
|
|
liveRuns: [],
|
|
transcriptsByRunId: new Map([
|
|
[
|
|
"run-1",
|
|
[
|
|
{
|
|
kind: "assistant",
|
|
ts: "2026-04-06T12:01:05.000Z",
|
|
text: "Working on it.",
|
|
},
|
|
],
|
|
],
|
|
]),
|
|
hasOutputForRun: (runId) => runId === "run-1",
|
|
currentUserId: "user-1",
|
|
});
|
|
|
|
expect(liveMessages).toHaveLength(1);
|
|
expect(cancelledMessages).toHaveLength(1);
|
|
expect(liveMessages[0]).toMatchObject({
|
|
id: "run-assistant:run-1",
|
|
status: { type: "running" },
|
|
});
|
|
expect(cancelledMessages[0]).toMatchObject({
|
|
id: "run-assistant:run-1",
|
|
status: { type: "complete", reason: "stop" },
|
|
metadata: { custom: { runStatus: "cancelled" } },
|
|
});
|
|
});
|
|
|
|
it("labels pause-caused cancelled runs as paused by board", () => {
|
|
const messages = buildIssueChatMessages({
|
|
comments: [],
|
|
timelineEvents: [],
|
|
linkedRuns: [
|
|
{
|
|
runId: "run-paused",
|
|
status: "cancelled",
|
|
agentId: "agent-1",
|
|
agentName: "CodexCoder",
|
|
createdAt: new Date("2026-04-06T12:01:00.000Z"),
|
|
startedAt: new Date("2026-04-06T12:01:00.000Z"),
|
|
finishedAt: new Date("2026-04-06T12:02:00.000Z"),
|
|
resultJson: { stopReason: "paused" },
|
|
},
|
|
],
|
|
liveRuns: [],
|
|
transcriptsByRunId: new Map([
|
|
[
|
|
"run-paused",
|
|
[
|
|
{
|
|
kind: "assistant",
|
|
ts: "2026-04-06T12:01:05.000Z",
|
|
text: "Working on it.",
|
|
},
|
|
],
|
|
],
|
|
]),
|
|
hasOutputForRun: (runId) => runId === "run-paused",
|
|
currentUserId: "user-1",
|
|
});
|
|
|
|
expect(messages).toHaveLength(1);
|
|
expect(messages[0]?.metadata.custom).toMatchObject({
|
|
chainOfThoughtLabel: "Paused by board after 1 minute",
|
|
runStatus: "cancelled",
|
|
});
|
|
});
|
|
|
|
it("labels error-code-only operator interruptions as interrupted by board", () => {
|
|
const messages = buildIssueChatMessages({
|
|
comments: [],
|
|
timelineEvents: [],
|
|
linkedRuns: [
|
|
{
|
|
runId: "run-interrupted",
|
|
status: "cancelled",
|
|
agentId: "agent-1",
|
|
agentName: "CodexCoder",
|
|
createdAt: new Date("2026-04-06T12:01:00.000Z"),
|
|
startedAt: new Date("2026-04-06T12:01:00.000Z"),
|
|
finishedAt: new Date("2026-04-06T12:02:00.000Z"),
|
|
errorCode: "operator_interrupted",
|
|
resultJson: null,
|
|
},
|
|
],
|
|
liveRuns: [],
|
|
transcriptsByRunId: new Map([
|
|
[
|
|
"run-interrupted",
|
|
[
|
|
{
|
|
kind: "assistant",
|
|
ts: "2026-04-06T12:01:05.000Z",
|
|
text: "Working on it.",
|
|
},
|
|
],
|
|
],
|
|
]),
|
|
hasOutputForRun: (runId) => runId === "run-interrupted",
|
|
currentUserId: "user-1",
|
|
});
|
|
|
|
expect(messages).toHaveLength(1);
|
|
expect(messages[0]?.metadata.custom).toMatchObject({
|
|
chainOfThoughtLabel: "Interrupted by board after 1 minute",
|
|
runOperatorInterrupted: true,
|
|
runStatus: "cancelled",
|
|
});
|
|
});
|
|
|
|
it("can keep succeeded runs without transcript output for embedded run feeds", () => {
|
|
const messages = buildIssueChatMessages({
|
|
comments: [],
|
|
timelineEvents: [],
|
|
linkedRuns: [
|
|
{
|
|
runId: "run-history-2",
|
|
status: "succeeded",
|
|
agentId: "agent-1",
|
|
agentName: "CodexCoder",
|
|
createdAt: new Date("2026-04-06T12:01:00.000Z"),
|
|
startedAt: new Date("2026-04-06T12:01:00.000Z"),
|
|
finishedAt: new Date("2026-04-06T12:03:00.000Z"),
|
|
},
|
|
],
|
|
liveRuns: [],
|
|
includeSucceededRunsWithoutOutput: true,
|
|
currentUserId: "user-1",
|
|
});
|
|
|
|
expect(messages).toHaveLength(1);
|
|
expect(messages[0]).toMatchObject({
|
|
id: "run:run-history-2",
|
|
role: "system",
|
|
metadata: {
|
|
custom: {
|
|
kind: "run",
|
|
runId: "run-history-2",
|
|
runAgentName: "CodexCoder",
|
|
runStatus: "succeeded",
|
|
},
|
|
},
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("stabilizeThreadMessages", () => {
|
|
it("reveals live streamed additions at word boundaries instead of character boundaries", () => {
|
|
expect(
|
|
preserveReadableStreamingRetraction("Writing ", "Writing the pla"),
|
|
).toBe("Writing the ");
|
|
expect(
|
|
preserveReadableStreamingRetraction("Writing ", "Writing the plan "),
|
|
).toBe("Writing the plan ");
|
|
expect(
|
|
preserveReadableStreamingRetraction("Writing ", "Writing the plan."),
|
|
).toBe("Writing the plan.");
|
|
expect(
|
|
preserveReadableStreamingRetraction("Writing ", "Writing draft"),
|
|
).toBe("Writing draft");
|
|
});
|
|
|
|
it("holds sliding-window removals until an older paragraph or group boundary drops", () => {
|
|
expect(
|
|
preserveReadableStreamingRetraction(
|
|
"First sentence. Second sentence is visible",
|
|
"irst sentence. Second sentence is visible now ",
|
|
),
|
|
).toBe("irst sentence. Second sentence is visible now ");
|
|
expect(
|
|
preserveReadableStreamingRetraction(
|
|
"First sentence. Second sentence is visible",
|
|
"Second sentence is visible now ",
|
|
),
|
|
).toBe("Second sentence is visible now ");
|
|
expect(
|
|
preserveReadableStreamingRetraction(
|
|
"Paragraph one.\n\nParagraph two is visible",
|
|
"Paragraph two is visible now ",
|
|
),
|
|
).toBe("Paragraph two is visible now ");
|
|
expect(
|
|
preserveReadableStreamingRetraction(
|
|
"The answer is 42",
|
|
"42 is the answer",
|
|
),
|
|
).toBe("42 is the answer");
|
|
expect(
|
|
preserveReadableStreamingRetraction(
|
|
"The quick brown fox jumps over the lazy dog",
|
|
"quick brown fox jumps over the lazy dog near the river",
|
|
),
|
|
).toBe("quick brown fox jumps over the lazy dog near the river");
|
|
});
|
|
|
|
it("keeps live streamed retractions readable until a whole line disappears", () => {
|
|
expect(
|
|
preserveReadableStreamingRetraction(
|
|
"First line\nSecond line\nThird line is complete",
|
|
"First line\nSecond line\nThird line",
|
|
),
|
|
).toBe("First line\nSecond line\nThird line is complete");
|
|
expect(
|
|
preserveReadableStreamingRetraction(
|
|
"First line\nSecond line\nThird line is complete",
|
|
"First line\nSecond line",
|
|
),
|
|
).toBe("First line\nSecond line");
|
|
|
|
const liveRun: LiveRunForIssue = {
|
|
id: "run-live-retract",
|
|
status: "running",
|
|
invocationSource: "manual",
|
|
triggerDetail: null,
|
|
startedAt: "2026-04-06T12:04:00.000Z",
|
|
finishedAt: null,
|
|
createdAt: "2026-04-06T12:04:00.000Z",
|
|
agentId: "agent-1",
|
|
agentName: "CodexCoder",
|
|
adapterType: "codex_local",
|
|
};
|
|
const buildLiveMessages = (text: string) =>
|
|
buildIssueChatMessages({
|
|
comments: [],
|
|
timelineEvents: [],
|
|
linkedRuns: [],
|
|
liveRuns: [liveRun],
|
|
transcriptsByRunId: new Map([
|
|
[
|
|
"run-live-retract",
|
|
[{ kind: "assistant", ts: "2026-04-06T12:04:01.000Z", text }],
|
|
],
|
|
]),
|
|
hasOutputForRun: (runId) => runId === "run-live-retract",
|
|
currentUserId: "user-1",
|
|
});
|
|
|
|
const fullText = "First line\nSecond line\nThird line is complete";
|
|
const firstStable = stabilizeThreadMessages(
|
|
buildLiveMessages(fullText),
|
|
[],
|
|
new Map(),
|
|
);
|
|
const partialRetractionStable = stabilizeThreadMessages(
|
|
buildLiveMessages("First line\nSecond line\nThird line"),
|
|
firstStable.messages,
|
|
firstStable.cache,
|
|
);
|
|
|
|
expect(partialRetractionStable.messages).toBe(firstStable.messages);
|
|
expect(partialRetractionStable.messages[0]?.content[0]).toMatchObject({
|
|
type: "text",
|
|
text: fullText,
|
|
});
|
|
|
|
const wholeLineRetractionStable = stabilizeThreadMessages(
|
|
buildLiveMessages("First line\nSecond line"),
|
|
partialRetractionStable.messages,
|
|
partialRetractionStable.cache,
|
|
);
|
|
|
|
expect(wholeLineRetractionStable.messages[0]?.content[0]).toMatchObject({
|
|
type: "text",
|
|
text: "First line\nSecond line",
|
|
});
|
|
});
|
|
|
|
it("reuses unchanged message objects across rebuilds", () => {
|
|
const firstPass = buildIssueChatMessages({
|
|
comments: [createComment()],
|
|
timelineEvents: [],
|
|
linkedRuns: [],
|
|
liveRuns: [],
|
|
currentUserId: "user-1",
|
|
});
|
|
|
|
const firstStable = stabilizeThreadMessages(firstPass, [], new Map());
|
|
const secondPass = buildIssueChatMessages({
|
|
comments: [
|
|
createComment(),
|
|
createComment({
|
|
id: "comment-2",
|
|
body: "New message",
|
|
createdAt: new Date("2026-04-06T12:01:00.000Z"),
|
|
updatedAt: new Date("2026-04-06T12:01:00.000Z"),
|
|
}),
|
|
],
|
|
timelineEvents: [],
|
|
linkedRuns: [],
|
|
liveRuns: [],
|
|
currentUserId: "user-1",
|
|
});
|
|
|
|
const secondStable = stabilizeThreadMessages(
|
|
secondPass,
|
|
firstStable.messages,
|
|
firstStable.cache,
|
|
);
|
|
|
|
expect(secondStable.messages).toHaveLength(2);
|
|
expect(secondStable.messages[0]).toBe(firstStable.messages[0]);
|
|
expect(secondStable.messages[1]?.id).toBe("comment-2");
|
|
});
|
|
|
|
it("reuses the previous array when nothing semantically changed", () => {
|
|
const firstPass = buildIssueChatMessages({
|
|
comments: [createComment()],
|
|
timelineEvents: [],
|
|
linkedRuns: [],
|
|
liveRuns: [],
|
|
currentUserId: "user-1",
|
|
});
|
|
|
|
const firstStable = stabilizeThreadMessages(firstPass, [], new Map());
|
|
const secondPass = buildIssueChatMessages({
|
|
comments: [createComment()],
|
|
timelineEvents: [],
|
|
linkedRuns: [],
|
|
liveRuns: [],
|
|
currentUserId: "user-1",
|
|
});
|
|
|
|
const secondStable = stabilizeThreadMessages(
|
|
secondPass,
|
|
firstStable.messages,
|
|
firstStable.cache,
|
|
);
|
|
|
|
expect(secondStable.messages).toBe(firstStable.messages);
|
|
});
|
|
});
|
|
|
|
|
|
describe("AI recovery presentation", () => {
|
|
it.each(["pending", "accepted", "rejected", "expired"] as const)("replaces diagnostic notices with the same-run %s connection card", (status) => {
|
|
const interaction: ConnectionIntentInteraction = { ...pendingConnectionIntentInteraction, status, sourceRunId: "failed-run", payload: { ...pendingConnectionIntentInteraction.payload, purpose: "ai" } };
|
|
const notice = createComment({ authorType: "system", presentation: { kind: "system_notice", title: "AI connection needs attention", tone: "danger", detailsDefaultOpen: false }, metadata: { version: 1, sourceRunId: "failed-run", sections: [] } });
|
|
expect(isRedundantAiRecoveryNotice(notice, [interaction])).toBe(true);
|
|
expect(isRedundantAiRecoveryNotice(notice, [{ ...interaction, sourceRunId: "other-run" }])).toBe(false);
|
|
expect(isRedundantAiRecoveryNotice(notice, [])).toBe(false);
|
|
expect(isRedundantAiRecoveryNotice(notice, [{ ...interaction, payload: { ...interaction.payload, purpose: undefined } }])).toBe(false);
|
|
const messages = buildIssueChatMessages({ comments: [notice], interactions: [interaction], timelineEvents: [], linkedRuns: [], liveRuns: [] });
|
|
expect(messages).toHaveLength(1);
|
|
expect(messages[0]?.metadata.custom).toMatchObject({ kind: "interaction" });
|
|
});
|
|
});
|