Files
PaperClipAI/server/src/__tests__/issue-comment-redaction.test.ts
T
656ecfa585 fix(server): keep Date fields intact through secret redaction; harden chat notice timestamps (#10984)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - The task chat thread renders issue comments, system notices, and run
transcripts
> - The server routes comment payloads through the run-secret redaction
walker before it sends them
> - The walker rebuilds each object with `Object.entries`, and this
collapses `Date` instances to `{}`
> - The chat renderer then calls `.toISOString()` on an invalid date and
throws, and the thread falls back to the error banner
> - This pull request keeps `Date` instances intact in redacted
responses and makes the renderer safe against bad timestamps
> - The benefit is that task threads with system notices render
correctly again

## Linked Issues or Issue Description

**What happened**

Task threads that contain a system notice showed the banner "Chat
renderer hit an internal state error." in place of the conversation.
This occurred on many tasks.

**Expected behavior**

The thread renders all comments and system notices with correct
timestamps.

**Steps to reproduce**

1. Open a task that has at least one system notice comment (for example
a "Workspace ready" notice).
2. `GET /api/issues/{id}/comments` returns `createdAt: {}` for every
comment because the secret-redaction walker collapses `Date` objects.
3. The system-notice row calls `new Date({}).toISOString()`. This throws
`RangeError: Invalid time value` and trips the thread error boundary.

**Version / deployment**

Regression from #9934 (`e43f187ca`). It applies to all deployments that
include that commit.

## What Changed

- `server/src/services/run-secret-redaction.ts`:
`redactRegisteredSecretValues` now returns `Date` instances as-is. Dates
hold no redactable text, and the `Object.entries` rebuild turned them
into `{}`.
- `ui/src/components/IssueChatThread.tsx`: the system-notice row formats
its timestamp with a new `toValidIsoString` helper. A value that does
not parse as a date now degrades to "no timestamp" instead of a render
crash.
- Regression tests at three layers:
- Walker unit tests: `Date` values survive with and without registered
secret values.
- Route test: `GET /issues/:id/comments` serializes `createdAt` /
`updatedAt` as ISO strings.
- Render test: a system notice with a malformed `createdAt` renders
without the error boundary.

## Verification

- `npx vitest run --root server
src/__tests__/run-secret-redaction.test.ts` — 5 passed.
- `npx vitest run --root server
src/__tests__/issue-comment-redaction.test.ts` — 4 passed (embedded
Postgres route test).
- `cd ui && npx vitest run src/components/IssueChatThread.test.tsx
src/components/IssueChatThreadSystemNotice.test.tsx
src/lib/issue-chat-messages.test.ts` — 121 passed.
- Each new test was run against the unfixed code and failed there, which
confirms it guards the regression.
- A local sweep rendered 47 real issue threads through
`IssueChatThread`: 7 tripped the boundary before the fix, 0 after.

## Risks

- Low risk. The server change only preserves `Date` objects that the
walker destroyed before. String redaction behavior does not change, and
the registry-key stripping does not change.
- The UI change only affects the timestamp of system-notice rows and
omits it when the value is invalid.

## Model Used

- Claude Fable 5 (`claude-fable-5`), Anthropic. Agentic coding session
with tool use (file edits, shell, Vitest). No extended-context or
special reasoning mode.

## 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
- [ ] All Paperclip CI gates are green
- [ ] 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: Claude Fable 5 <noreply@anthropic.com>
Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-08-06 10:29:07 -05:00

282 lines
9.2 KiB
TypeScript

import { randomUUID } from "node:crypto";
import express from "express";
import request from "supertest";
import { sql } from "drizzle-orm";
import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest";
import {
companies,
companyMemberships,
createDb,
issueComments,
issueReferenceMentions,
issues,
} from "@paperclipai/db";
import { companySearchQuerySchema } from "@paperclipai/shared";
import {
getEmbeddedPostgresTestSupport,
startEmbeddedPostgresTestDatabase,
} from "./helpers/embedded-postgres.js";
import { errorHandler } from "../middleware/index.js";
import { issueRoutes } from "../routes/issues.js";
import { companySearchService } from "../services/company-search.js";
import { buildPaperclipWakePayload } from "../services/heartbeat.js";
import { issueReferenceService } from "../services/issue-references.js";
import { issueService } from "../services/issues.js";
import type { StorageService } from "../storage/types.js";
const embeddedPostgresSupport = await getEmbeddedPostgresTestSupport();
const describeEmbeddedPostgres = embeddedPostgresSupport.supported ? describe.sequential : describe.skip;
if (!embeddedPostgresSupport.supported) {
console.warn(
`Skipping embedded Postgres issue comment redaction tests on this host: ${embeddedPostgresSupport.reason ?? "unsupported environment"}`,
);
}
describeEmbeddedPostgres("deleted issue comment redaction", () => {
let db!: ReturnType<typeof createDb>;
let tempDb: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>> | null = null;
beforeAll(async () => {
tempDb = await startEmbeddedPostgresTestDatabase("paperclip-comment-redaction-");
db = createDb(tempDb.connectionString);
await db.execute(sql.raw("CREATE EXTENSION IF NOT EXISTS pg_trgm"));
}, 20_000);
afterEach(async () => {
await db.delete(issueReferenceMentions);
await db.delete(issueComments);
await db.delete(issues);
await db.delete(companyMemberships);
await db.delete(companies);
});
afterAll(async () => {
await tempDb?.cleanup();
});
async function seedIssue() {
const companyId = randomUUID();
const issueId = randomUUID();
await db.insert(companies).values({
id: companyId,
name: "Comment Redaction Co",
issuePrefix: `R${companyId.replace(/-/g, "").slice(0, 6).toUpperCase()}`,
requireBoardApprovalForNewAgents: false,
});
await db.insert(issues).values({
id: issueId,
companyId,
identifier: "RED-1",
title: "Deleted comment redaction",
status: "todo",
priority: "medium",
});
await db.insert(companyMemberships).values({
companyId,
principalType: "user",
principalId: "board-user-1",
status: "active",
membershipRole: "owner",
updatedAt: new Date(),
});
return { companyId, issueId };
}
function createApp(companyId: string) {
const app = express();
app.use(express.json());
app.use((req, _res, next) => {
(req as any).actor = {
type: "board",
userId: "board-user-1",
companyIds: [companyId],
memberships: [{ companyId, membershipRole: "owner", status: "active" }],
source: "cloud_tenant",
// cloud_tenant actors are never instance admins — reads flow through
// the active company membership seeded in seedIssue().
isInstanceAdmin: false,
};
next();
});
const storage: StorageService = {
provider: "local_disk",
putFile: vi.fn(async () => {
throw new Error("Unexpected storage.putFile call");
}),
getObject: vi.fn(async () => {
throw new Error("Unexpected storage.getObject call");
}),
headObject: vi.fn(async () => ({ exists: false })),
deleteObject: vi.fn(async () => undefined),
};
app.use("/api", issueRoutes(db, storage));
app.use(errorHandler);
return app;
}
it("redacts deleted comment bodies from ordinary reads, heartbeat context, and wake payloads", async () => {
const { companyId, issueId } = await seedIssue();
const commentId = randomUUID();
const deletedAt = new Date("2026-06-03T12:00:00.000Z");
await db.insert(issueComments).values({
id: commentId,
companyId,
issueId,
authorUserId: "board-user-1",
body: "secret deleted body",
presentation: { kind: "system_notice", tone: "warning" },
metadata: { version: 1, sections: [{ rows: [{ type: "text", text: "secret metadata" }] }] },
deletedAt,
deletedByType: "user",
deletedByUserId: "board-user-1",
});
const comments = await issueService(db).listComments(issueId, { order: "asc" });
expect(comments).toHaveLength(1);
expect(comments[0]).toMatchObject({
id: commentId,
body: "",
presentation: null,
metadata: null,
deletedAt,
deletedByType: "user",
deletedByUserId: "board-user-1",
});
const exactComment = await issueService(db).getComment(commentId);
expect(exactComment?.body).toBe("");
expect(exactComment?.metadata).toBeNull();
const heartbeatContext = await request(createApp(companyId))
.get(`/api/issues/${issueId}/heartbeat-context`)
.query({ wakeCommentId: commentId });
expect(heartbeatContext.status, JSON.stringify(heartbeatContext.body)).toBe(200);
expect(heartbeatContext.body.wakeComment).toMatchObject({
id: commentId,
body: "",
metadata: null,
deletedByUserId: "board-user-1",
});
expect(JSON.stringify(heartbeatContext.body)).not.toContain("secret deleted body");
expect(JSON.stringify(heartbeatContext.body)).not.toContain("secret metadata");
const wakePayload = await buildPaperclipWakePayload({
db,
companyId,
contextSnapshot: {
issueId,
commentId,
wakeCommentIds: [commentId],
wakeReason: "issue_commented",
},
});
expect(wakePayload?.comments).toEqual([
expect.objectContaining({
id: commentId,
body: "",
bodyTruncated: false,
presentation: null,
metadata: null,
deletedAt: deletedAt.toISOString(),
deletedByUserId: "board-user-1",
}),
]);
expect(JSON.stringify(wakePayload)).not.toContain("secret deleted body");
expect(JSON.stringify(wakePayload)).not.toContain("secret metadata");
});
it("serializes comment timestamps as ISO strings through the redacted comments route (PAP-16607)", async () => {
const { companyId, issueId } = await seedIssue();
const commentId = randomUUID();
await db.insert(issueComments).values({
id: commentId,
companyId,
issueId,
authorUserId: "board-user-1",
body: "ordinary comment",
});
const response = await request(createApp(companyId)).get(`/api/issues/${issueId}/comments`);
expect(response.status, JSON.stringify(response.body)).toBe(200);
expect(response.body).toHaveLength(1);
// Secret redaction must not collapse Date instances to `{}` — the chat
// renderer needs parseable timestamps.
expect(typeof response.body[0].createdAt).toBe("string");
expect(Number.isNaN(new Date(response.body[0].createdAt).getTime())).toBe(false);
expect(typeof response.body[0].updatedAt).toBe("string");
});
it("excludes deleted comment bodies from company search", async () => {
const { companyId, issueId } = await seedIssue();
await db.insert(issueComments).values({
companyId,
issueId,
body: "vanished-search-needle",
deletedAt: new Date("2026-06-03T12:00:00.000Z"),
deletedByType: "user",
deletedByUserId: "board-user-1",
});
const result = await companySearchService(db).search(
companyId,
companySearchQuerySchema.parse({ q: "vanished-search-needle", scope: "comments" }),
);
expect(result.results).toEqual([]);
});
it("clears issue references sourced from deleted comment bodies", async () => {
const companyId = randomUUID();
const sourceIssueId = randomUUID();
const targetIssueId = randomUUID();
const commentId = randomUUID();
await db.insert(companies).values({
id: companyId,
name: "Reference Redaction Co",
issuePrefix: "REF",
requireBoardApprovalForNewAgents: false,
});
await db.insert(issues).values([
{
id: sourceIssueId,
companyId,
identifier: "REF-1",
title: "Source issue",
status: "todo",
priority: "medium",
},
{
id: targetIssueId,
companyId,
identifier: "REF-2",
title: "Target issue",
status: "todo",
priority: "medium",
},
]);
await db.insert(issueComments).values({
id: commentId,
companyId,
issueId: sourceIssueId,
body: "Follow up in REF-2",
});
const refs = issueReferenceService(db);
await refs.syncComment(commentId);
expect((await refs.listIssueReferenceSummary(sourceIssueId)).outbound.map((item) => item.issue.id)).toEqual([
targetIssueId,
]);
await db.update(issueComments).set({
deletedAt: new Date("2026-06-03T12:00:00.000Z"),
deletedByType: "user",
deletedByUserId: "board-user-1",
});
await refs.syncComment(commentId);
expect((await refs.listIssueReferenceSummary(sourceIssueId)).outbound).toEqual([]);
});
});