Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions src/lib/payload/fragment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { compressToEncodedURIComponent, decompressFromEncodedURIComponent } from "lz-string";
import { normalizeEnvelope } from "@/lib/payload/envelope";
import {
MAX_DECODED_PAYLOAD_LENGTH,
Expand Down Expand Up @@ -30,17 +29,13 @@ function fromBase64Url(input: string): string {
return new TextDecoder().decode(bytes);
}

function encodePayload(json: string, codec: PayloadCodec): string {
if (codec === "lz") {
return compressToEncodedURIComponent(json);
}

function encodePayload(json: string): string {
return toBase64Url(json);
}

function decodePayload(encoded: string, codec: PayloadCodec): string | null {
if (codec === "lz") {
return decompressFromEncodedURIComponent(encoded);
return null;
}

return fromBase64Url(encoded);
Expand All @@ -49,10 +44,14 @@ function decodePayload(encoded: string, codec: PayloadCodec): string | null {
function buildFragment(envelope: PayloadEnvelope, codec: PayloadCodec): string {
const payloadEnvelope = { ...envelope, codec };
const json = JSON.stringify(payloadEnvelope);
return `${PAYLOAD_FRAGMENT_KEY}=v1.${codec}.${encodePayload(json, codec)}`;
return `${PAYLOAD_FRAGMENT_KEY}=v1.${codec}.${encodePayload(json)}`;
}

export function encodeEnvelope(envelope: PayloadEnvelope, options: EncodeOptions = {}): string {
if (options.codec === "lz") {
return buildFragment(envelope, "plain");
}

if (options.codec) {
return buildFragment(envelope, options.codec);
}
Expand All @@ -62,8 +61,7 @@ export function encodeEnvelope(envelope: PayloadEnvelope, options: EncodeOptions
return plainFragment;
}

const compressedFragment = buildFragment(envelope, "lz");
return compressedFragment.length < plainFragment.length ? compressedFragment : plainFragment;
return plainFragment;
}

export function decodeFragment(hash: string): ParsedPayload {
Expand Down Expand Up @@ -113,6 +111,14 @@ export function decodeFragment(hash: string): ParsedPayload {
};
}

if (codec === "lz") {
return {
ok: false,
code: "invalid-format",
message: 'Unsupported codec "lz". Please re-share using codec "plain".',
};
}
Comment on lines +114 to +120
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Don’t report rejected lz links as generic format errors.

These are legacy fragments generated by older clients, not malformed input. Returning invalid-format makes old #agent-render=v1.lz... bookmarks indistinguishable from genuinely corrupt hashes, so the UI has no reliable way to show a targeted recovery message. Please give this branch its own result code.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/lib/payload/fragment.ts` around lines 114 - 120, The branch that handles
codec === "lz" should return a distinct error code instead of "invalid-format"
so legacy lz fragments are distinguishable; update the return value in the codec
=== "lz" branch in src/lib/payload/fragment.ts to use a new, specific code
(e.g., "legacy-codec" or "unsupported-legacy-fragment") while keeping a clear
message, and update any tests or callers that rely on the old "invalid-format"
code to expect the new code.


let parsed: unknown;

try {
Expand Down
18 changes: 16 additions & 2 deletions tests/fragment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe("fragment payload transport", () => {
expect(parsed.code).toBe("invalid-json");
});

it("uses compressed transport when it is smaller", () => {
it("always emits plain transport", () => {
const repetitiveEnvelope: PayloadEnvelope = {
...envelope,
artifacts: [
Expand All @@ -68,11 +68,25 @@ describe("fragment payload transport", () => {

const hash = encodeEnvelope(repetitiveEnvelope);

expect(hash.startsWith("agent-render=v1.lz.")).toBe(true);
expect(hash.startsWith("agent-render=v1.plain.")).toBe(true);
const parsed = decodeFragment(`#${hash}`);
expect(parsed.ok).toBe(true);
});

it("rejects lz transport fragments", () => {
const hash = encodeEnvelope(envelope, { codec: "lz" });

expect(hash.startsWith("agent-render=v1.plain.")).toBe(true);

const parsed = decodeFragment("#agent-render=v1.lz.abcd");
expect(parsed.ok).toBe(false);
if (parsed.ok) {
return;
}

expect(parsed.code).toBe("invalid-format");
});

it("normalizes an invalid active artifact id to the first artifact", () => {
const hash = `#${encodeEnvelope(
{
Expand Down