b9a80dcf22
## Thinking Path > - Paperclip is the control plane for autonomous AI companies. > - V1 needs to stay local-first while also supporting shared, authenticated deployments. > - Human operators need real identities, company membership, invite flows, profile surfaces, and company-scoped access controls. > - Agents and operators also need the existing issue, inbox, workspace, approval, and plugin flows to keep working under those authenticated boundaries. > - This branch accumulated the multi-user implementation, follow-up QA fixes, workspace/runtime refinements, invite UX improvements, release-branch conflict resolution, and review hardening. > - This pull request consolidates that branch onto the current `master` branch as a single reviewable PR. > - The benefit is a complete multi-user implementation path with tests and docs carried forward without dropping existing branch work. ## What Changed - Added authenticated human-user access surfaces: auth/session routes, company user directory, profile settings, company access/member management, join requests, and invite management. - Added invite creation, invite landing, onboarding, logo/branding, invite grants, deduped join requests, and authenticated multi-user E2E coverage. - Tightened company-scoped and instance-admin authorization across board, plugin, adapter, access, issue, and workspace routes. - Added profile-image URL validation hardening, avatar preservation on name-only profile updates, and join-request uniqueness migration cleanup for pending human requests. - Added an atomic member role/status/grants update path so Company Access saves no longer leave partially updated permissions. - Improved issue chat, inbox, assignee identity rendering, sidebar/account/company navigation, workspace routing, and execution workspace reuse behavior for multi-user operation. - Added and updated server/UI tests covering auth, invites, membership, issue workspace inheritance, plugin authz, inbox/chat behavior, and multi-user flows. - Merged current `public-gh/master` into this branch, resolved all conflicts, and verified no `pnpm-lock.yaml` change is included in this PR diff. ## Verification - `pnpm exec vitest run server/src/__tests__/issues-service.test.ts ui/src/components/IssueChatThread.test.tsx ui/src/pages/Inbox.test.tsx` - `pnpm run preflight:workspace-links && pnpm exec vitest run server/src/__tests__/plugin-routes-authz.test.ts` - `pnpm exec vitest run server/src/__tests__/plugin-routes-authz.test.ts server/src/__tests__/workspace-runtime-service-authz.test.ts server/src/__tests__/access-validators.test.ts` - `pnpm exec vitest run server/src/__tests__/authz-company-access.test.ts server/src/__tests__/routines-routes.test.ts server/src/__tests__/sidebar-preferences-routes.test.ts server/src/__tests__/approval-routes-idempotency.test.ts server/src/__tests__/openclaw-invite-prompt-route.test.ts server/src/__tests__/agent-cross-tenant-authz-routes.test.ts server/src/__tests__/routines-e2e.test.ts` - `pnpm exec vitest run server/src/__tests__/auth-routes.test.ts ui/src/pages/CompanyAccess.test.tsx` - `pnpm --filter @paperclipai/shared typecheck && pnpm --filter @paperclipai/db typecheck && pnpm --filter @paperclipai/server typecheck` - `pnpm --filter @paperclipai/shared typecheck && pnpm --filter @paperclipai/server typecheck` - `pnpm --filter @paperclipai/ui typecheck` - `pnpm db:generate` - `npx playwright test --config tests/e2e/playwright.config.ts --list` - Confirmed branch has no uncommitted changes and is `0` commits behind `public-gh/master` before PR creation. - Confirmed no `pnpm-lock.yaml` change is staged or present in the PR diff. ## Risks - High review surface area: this PR contains the accumulated multi-user branch plus follow-up fixes, so reviewers should focus especially on company-boundary enforcement and authenticated-vs-local deployment behavior. - UI behavior changed across invites, inbox, issue chat, access settings, and sidebar navigation; no browser screenshots are included in this branch-consolidation PR. - Plugin install, upgrade, and lifecycle/config mutations now require instance-admin access, which is intentional but may change expectations for non-admin board users. - A join-request dedupe migration rejects duplicate pending human requests before creating unique indexes; deployments with unusual historical duplicates should review the migration behavior. - Company member role/status/grant saves now use a new combined endpoint; older separate endpoints remain for compatibility. - Full production build was not run locally in this heartbeat; CI should cover the full matrix. ## Model Used - OpenAI Codex coding agent, GPT-5-based model, CLI/tool-use environment. Exact deployed model identifier and context window were not exposed by the runtime. ## 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 run tests locally and they pass - [x] I have added or updated tests where applicable - [x] If this change affects the UI, I have included before/after screenshots - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] I will address all Greptile and reviewer comments before requesting merge Note on screenshots: this is a branch-consolidation PR for an already-developed multi-user branch, and no browser screenshots were captured during this heartbeat. --------- Co-authored-by: dotta <dotta@example.com> Co-authored-by: Paperclip <noreply@paperclip.ing> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
618 lines
19 KiB
TypeScript
618 lines
19 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import type { Agent } from "@paperclipai/shared";
|
|
import {
|
|
buildAssistantPartsFromTranscript,
|
|
buildIssueChatMessages,
|
|
stabilizeThreadMessages,
|
|
type IssueChatComment,
|
|
type IssueChatLinkedRun,
|
|
} from "./issue-chat-messages";
|
|
import type { IssueTimelineEvent } from "./issue-timeline-events";
|
|
import type { LiveRunForIssue } from "../api/heartbeats";
|
|
|
|
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 {
|
|
return {
|
|
id: "comment-1",
|
|
companyId: "company-1",
|
|
issueId: "issue-1",
|
|
authorAgentId: null,
|
|
authorUserId: "user-1",
|
|
body: "Hello",
|
|
createdAt: new Date("2026-04-06T12:00:00.000Z"),
|
|
updatedAt: new Date("2026-04-06T12:00:00.000Z"),
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
describe("buildAssistantPartsFromTranscript", () => {
|
|
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("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("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("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("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("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("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("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);
|
|
});
|
|
});
|