mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-01 09:39:53 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - People join a company through an invitation link. The link carries a bearer token in its URL path. > - An agent can create an invitation in chat and give the person the join link. > - The server writes each agent run to a run log file (`.ndjson`). This file is a plain diagnostic copy. Local agents run as the same OS user, so they can read it. > - #20343 removed invitation tokens from HTTP logs. The run log still kept the full join link and the raw token. > - This pull request removes invitation tokens from run-log chunks when the server writes them. > - The benefit is that a live join link does not stay in a file that other local agents can read. The person still gets the working link in the chat reply. ## Linked Issues or Issue Description Refs #20343 (same token policy for HTTP logs). **What happened?** An agent created an invitation in chat and replied with a `/invite/<token>` join link. The agent run log file under `data/run-logs/` kept the full token. The token was in the agent message and in the tool result. **Expected behavior** The run log keeps the route and drops the token: `/invite/***REDACTED***`. A raw `pcp_invite_...` token also becomes `***REDACTED***`. The chat reply that the person reads keeps the working join link. **Steps to reproduce** 1. Check out master. 2. Run `server/src/services/heartbeat/run-log.test.ts` from this PR. 3. The "run-log invitation token privacy" tests fail. The token stays in both the legacy text chunk and the canonical native envelope. **Paperclip version or commit** Master at the merge base of this branch. **Deployment mode** Source server, local run logs (`local_file`). The same functions also format object-store run logs. ## What Changed - Add `redactInvitationTokens()` in `server/src/middleware/invitation-token-redaction.ts`, next to the HTTP log redactor from #20343. It removes the token from `/invite/<token>` segments and from raw `pcp_invite_` tokens inside free text and JSON text. The character classes stop at JSON, Markdown and URL delimiters, so the record structure stays valid. `redact-sensitive.ts` stays byte-identical, because the chat eval pins its exact bytes before it accepts a real invitation link. - Apply it in `compactRunLogChunk()` and `redactCanonicalNativeRunLogChunk()`. These are the two functions that format every agent run-log chunk before the store writes it. The run excerpt columns use the same chunk, so they are also redacted. - `canonicalNativeRunLogRedactionForms()` also yields the earlier, unredacted projection. A run log written by an earlier release still verifies after an upgrade. - `restoreCompactedEvent()` accepts a truncated record that the earlier compactor wrote with the token. Cold recovery of such an old log still works. - Add a test that proves the chat reply comes from database run events, not from the run log. The reply keeps the full join link, and the presentation path never reads the run log. ## Verification - From `server`: `node ../node_modules/vitest/vitest.mjs run --config vitest.config.ts src/services/heartbeat/run-log.test.ts src/__tests__/invitation-token-redaction.test.ts src/__tests__/redact-sensitive.test.ts src/__tests__/heartbeat-run-log.test.ts src/__tests__/logger-invitation-redaction.test.ts src/services/native-runtime/native-run-log-watermark.test.ts src/services/native-runtime/native-run-log-recovery.test.ts src/__tests__/heartbeat-run-output.test.ts src/services/native-runtime/agent-chat-response-recovery.test.ts` passes. - Before the `run-log.ts` change, the two new redaction tests in `run-log.test.ts` failed (red), then passed (green). - `native-run-log-watermark.test.ts`: a legacy-compacted record with a token in its retained head cold-recovers. It failed with `native_log_source_coverage_unproven` before the restore fix. - `agent-chat-response-recovery.test.ts` (embedded Postgres): a final reply with a join link is saved as the exact comment with the token. No run-log read happens. - `tsc --noEmit -p server` shows no errors in the changed files. - A live chat eval (`access.invite-teammate`) run with this change confirms the run-log file has no token. The run is linked in a comment. ## Risks - Low risk. Run-log readers (run transcript view, feedback export, support inspection) now show `***REDACTED***` in place of invitation tokens. This removes a live credential from a diagnostic copy. It does not change the chat reply or the stored run events. - A path segment after `/invite/` in free text is always redacted, even if it is not a token. This loses only diagnostic detail. - A token that a provider streams across two separate chunks is not joined first. Each chunk is redacted on its own. A complete token in one chunk, such as a final message or a tool result, is removed. > For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and discuss it in `#dev` before opening the PR. Feature PRs that overlap with planned core work may need to be redirected — check the roadmap first. See `CONTRIBUTING.md`. ## Model Used - Anthropic Claude Opus 5.5 (`claude-opus-5-5`), with tool use and code execution. ## 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 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
192 lines
7.5 KiB
TypeScript
192 lines
7.5 KiB
TypeScript
import { appendWithByteCap, MAX_EXCERPT_BYTES, parseObject } from "../../adapters/utils.js";
|
|
import { REDACTED_EVENT_VALUE, redactSensitiveText, sanitizeRecord } from "../../redaction.js";
|
|
import { redactInvitationTokens } from "../../middleware/invitation-token-redaction.js";
|
|
import { redactCurrentUserText, redactCurrentUserValue, type CurrentUserRedactionOptions } from "../../log-redaction.js";
|
|
|
|
const MAX_PERSISTED_LOG_CHUNK_CHARS = 64 * 1024;
|
|
const MAX_RUN_EVENT_PAYLOAD_STRING_CHARS = 16 * 1024;
|
|
const MAX_RUN_EVENT_PAYLOAD_ARRAY_ITEMS = 50;
|
|
const MAX_RUN_EVENT_PAYLOAD_OBJECT_KEYS = 100;
|
|
const MAX_RUN_EVENT_PAYLOAD_DEPTH = 6;
|
|
const INLINE_BASE64_IMAGE_DATA_RE =
|
|
/("type":"image","source":\{"type":"base64","data":")([A-Za-z0-9+/=]{1024,})(")/g;
|
|
|
|
export function appendExcerpt(prev: string, chunk: string) {
|
|
return appendWithByteCap(prev, chunk, MAX_EXCERPT_BYTES);
|
|
}
|
|
|
|
function truncateRunEventString(value: string) {
|
|
if (value.length <= MAX_RUN_EVENT_PAYLOAD_STRING_CHARS) return value;
|
|
const omittedChars = value.length - MAX_RUN_EVENT_PAYLOAD_STRING_CHARS;
|
|
return `${value.slice(0, MAX_RUN_EVENT_PAYLOAD_STRING_CHARS)}\n[truncated ${omittedChars} chars]`;
|
|
}
|
|
|
|
function boundRunEventValue(
|
|
value: unknown,
|
|
depth: number,
|
|
seen: WeakSet<object>,
|
|
): unknown {
|
|
if (typeof value === "string") {
|
|
return truncateRunEventString(value);
|
|
}
|
|
if (
|
|
value === null ||
|
|
typeof value === "number" ||
|
|
typeof value === "boolean"
|
|
) {
|
|
return value;
|
|
}
|
|
if (value instanceof Date) {
|
|
return value.toISOString();
|
|
}
|
|
if (Array.isArray(value)) {
|
|
if (depth >= MAX_RUN_EVENT_PAYLOAD_DEPTH) {
|
|
return {
|
|
_truncated: true,
|
|
type: "array",
|
|
originalLength: value.length,
|
|
};
|
|
}
|
|
const bounded = value
|
|
.slice(0, MAX_RUN_EVENT_PAYLOAD_ARRAY_ITEMS)
|
|
.map((entry) => boundRunEventValue(entry, depth + 1, seen));
|
|
if (value.length > MAX_RUN_EVENT_PAYLOAD_ARRAY_ITEMS) {
|
|
bounded.push({
|
|
_truncated: true,
|
|
omittedItems: value.length - MAX_RUN_EVENT_PAYLOAD_ARRAY_ITEMS,
|
|
});
|
|
}
|
|
return bounded;
|
|
}
|
|
if (typeof value !== "object" || value === undefined) {
|
|
return null;
|
|
}
|
|
if (seen.has(value)) {
|
|
return "[Circular]";
|
|
}
|
|
seen.add(value);
|
|
const entries = Object.entries(value as Record<string, unknown>);
|
|
if (depth >= MAX_RUN_EVENT_PAYLOAD_DEPTH) {
|
|
const bounded = {
|
|
_truncated: true,
|
|
type: "object",
|
|
keys: entries.map(([key]) => key).slice(0, 20),
|
|
};
|
|
seen.delete(value);
|
|
return bounded;
|
|
}
|
|
|
|
const out: Record<string, unknown> = {};
|
|
for (const [key, entryValue] of entries.slice(
|
|
0,
|
|
MAX_RUN_EVENT_PAYLOAD_OBJECT_KEYS,
|
|
)) {
|
|
out[key] = boundRunEventValue(entryValue, depth + 1, seen);
|
|
}
|
|
if (entries.length > MAX_RUN_EVENT_PAYLOAD_OBJECT_KEYS) {
|
|
out._truncated = true;
|
|
out._omittedKeys = entries.length - MAX_RUN_EVENT_PAYLOAD_OBJECT_KEYS;
|
|
}
|
|
seen.delete(value);
|
|
return out;
|
|
}
|
|
|
|
export function boundHeartbeatRunEventPayloadForStorage(
|
|
payload: Record<string, unknown>,
|
|
): Record<string, unknown> {
|
|
const bounded = boundRunEventValue(payload, 0, new WeakSet());
|
|
return parseObject(bounded) ?? { _truncated: true };
|
|
}
|
|
|
|
function redactInlineBase64ImageData(chunk: string) {
|
|
return chunk.replace(
|
|
INLINE_BASE64_IMAGE_DATA_RE,
|
|
(_match, prefix: string, data: string, suffix: string) =>
|
|
`${prefix}[omitted base64 image data: ${data.length} chars]${suffix}`,
|
|
);
|
|
}
|
|
|
|
// The run log is a plain-file diagnostic copy that local agents running as the
|
|
// same OS user can read. Invitation join links are bearer credentials, so the
|
|
// log keeps the route and drops the token, as HTTP logs do. The reply the
|
|
// person reads is built from database run events, not from this file.
|
|
function redactRunLogInvitationTokens(text: string, enabled: boolean) {
|
|
return enabled ? redactInvitationTokens(text, REDACTED_EVENT_VALUE) : text;
|
|
}
|
|
|
|
export function compactRunLogChunk(
|
|
chunk: string,
|
|
maxChars = MAX_PERSISTED_LOG_CHUNK_CHARS,
|
|
options: { redactInvitations?: boolean } = {},
|
|
) {
|
|
const normalized = redactRunLogInvitationTokens(
|
|
redactSensitiveText(redactInlineBase64ImageData(chunk)), options.redactInvitations !== false);
|
|
if (normalized.length <= maxChars) return normalized;
|
|
|
|
const headChars = Math.max(0, Math.floor(maxChars * 0.6));
|
|
const tailChars = Math.max(0, Math.floor(maxChars * 0.25));
|
|
const omittedChars = Math.max(0, normalized.length - headChars - tailChars);
|
|
const marker = `\n[paperclip truncated run log chunk: omitted ${omittedChars} chars]\n`;
|
|
return `${normalized.slice(0, headChars)}${marker}${normalized.slice(normalized.length - tailChars)}`;
|
|
}
|
|
|
|
|
|
interface CanonicalNativeLogRedaction {
|
|
redactIdentity: <T>(value: T) => T;
|
|
currentUser: CurrentUserRedactionOptions;
|
|
/** False only to reproduce logs written before invite tokens were redacted. */
|
|
redactInvitations?: boolean;
|
|
}
|
|
|
|
function omitCanonicalImageData(value: unknown): unknown {
|
|
if (Array.isArray(value)) return value.map(omitCanonicalImageData);
|
|
if (!value || typeof value !== "object") return value;
|
|
const object = value as Record<string, unknown>;
|
|
const result = Object.fromEntries(Object.entries(object).map(([key, child]) =>
|
|
[key, omitCanonicalImageData(child)]));
|
|
const source = object.source as Record<string, unknown> | undefined;
|
|
if (object.type === "image" && source?.type === "base64" &&
|
|
typeof source.data === "string" && /^[A-Za-z0-9+/=]{1024,}$/.test(source.data)) {
|
|
result.source = { ...(result.source as Record<string, unknown>),
|
|
data: `[omitted base64 image data: ${source.data.length} chars]` };
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/** Only complete envelopes supplied by the durable native exporter use this path.
|
|
* Redact complete JSON values before encoding so quoted secrets cannot damage
|
|
* the record's structure. Image omission uses the existing field/length policy.
|
|
* The object writer separately bounds the complete encoded outer record.
|
|
*/
|
|
export function redactCanonicalNativeRunLogChunk(chunk: string, options: CanonicalNativeLogRedaction) {
|
|
const envelope = JSON.parse(chunk);
|
|
if (envelope?.type !== "paperclip.prp.event" || !envelope.event ||
|
|
typeof envelope.event !== "object" || Array.isArray(envelope.event))
|
|
throw new Error("invalid_canonical_native_run_log_envelope");
|
|
const projected = redactCurrentUserValue(
|
|
options.redactIdentity(omitCanonicalImageData(envelope)), options.currentUser,
|
|
);
|
|
// Token characters never need JSON escaping and the marker adds none, so
|
|
// redacting the encoded record keeps it valid JSON.
|
|
return `${redactRunLogInvitationTokens(JSON.stringify(sanitizeRecord(projected as Record<string, unknown>)),
|
|
options.redactInvitations !== false)}\n`;
|
|
}
|
|
|
|
/** New complete-value forms first; old text projections are computed only when
|
|
* an archive needs them. An old malformed projection never repairs evidence.
|
|
*/
|
|
export function* canonicalNativeRunLogRedactionForms(chunk: string, options: CanonicalNativeLogRedaction) {
|
|
// Current invite-token redaction first; logs written by an earlier release
|
|
// kept the token and must still verify after an upgrade.
|
|
for (const redactInvitations of [true, false]) {
|
|
for (const enabled of [false, true])
|
|
yield redactCanonicalNativeRunLogChunk(chunk, {
|
|
...options, redactInvitations, currentUser: { ...options.currentUser, enabled },
|
|
});
|
|
for (const enabled of [false, true])
|
|
yield compactRunLogChunk(redactCurrentUserText(options.redactIdentity(chunk), {
|
|
...options.currentUser, enabled,
|
|
}), Number.MAX_SAFE_INTEGER, { redactInvitations });
|
|
}
|
|
}
|