Skip to content

Open-source AI coworkers that each get a computer of their own: a browser, files and tools, with every action decided before it happens and recorded after. Bring any AG-UI agent.

Read a channel cursor with a malformed time as the first page, not a 500 (#640)

Felix-Ayush4a20750892dd
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9febb9a5..c7d9d9df 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,13 @@ Newest first. `Unreleased` is what is on `main` and not yet tagged.
## Unreleased
+### A channel cursor with a malformed time reads as the first page, not a 500
+
+`GET /api/channels` only checked that a cursor's time was a string before casting it with
+`::timestamptz`, so a hand-edited or corrupted cursor such as `{"recency": "not-a-date"}` answered
+500. A time that is not the UTC timestamp the list writes now reads as the first page, the way every
+other malformed cursor already did.
+
### A playground component's Published switch publishes its source too
The Published switch on an admin component page called the generic publication endpoint for every
diff --git a/server/src/channels/routes.ts b/server/src/channels/routes.ts
index 2071c60a..c43930ec 100644
--- a/server/src/channels/routes.ts
+++ b/server/src/channels/routes.ts
@@ -108,7 +108,10 @@ const MAX_CHANNEL_PAGE = 200;
*/
type ChannelCursor = { pinned: boolean; recency: string; id: string };
-function encodeChannelCursor(cursor: ChannelCursor): string {
+/** The shape the encoder writes: UTC, to the millisecond (older cursors) or the microsecond. */
+const CURSOR_RECENCY = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,6})?Z$/;
+
+export function encodeChannelCursor(cursor: ChannelCursor): string {
return Buffer.from(JSON.stringify(cursor), "utf8").toString("base64url");
}
@@ -117,8 +120,11 @@ function encodeChannelCursor(cursor: ChannelCursor): string {
*
* A cursor minted before `pinned` existed is malformed by this definition, and deliberately: it
* describes a position in an ordering this query no longer has.
+ *
+ * `recency` has to be the timestamp the encoder writes, not merely a string: it is cast with
+ * `::timestamptz` in the page query, so any other string used to reach PostgreSQL and answer 500.
*/
-function decodeChannelCursor(
+export function decodeChannelCursor(
value: string | undefined,
): ChannelCursor | undefined {
if (!value) return undefined;
@@ -128,6 +134,8 @@ function decodeChannelCursor(
) as ChannelCursor;
return typeof parsed?.id === "string" &&
typeof parsed?.recency === "string" &&
+ CURSOR_RECENCY.test(parsed.recency) &&
+ !Number.isNaN(Date.parse(parsed.recency)) &&
typeof parsed?.pinned === "boolean"
? parsed
: undefined;
diff --git a/server/tests/channel-cursor.test.ts b/server/tests/channel-cursor.test.ts
new file mode 100644
index 00000000..c1e5ffcc
--- /dev/null
+++ b/server/tests/channel-cursor.test.ts
@@ -0,0 +1,47 @@
+import { describe, expect, test } from "bun:test";
+import {
+ decodeChannelCursor,
+ encodeChannelCursor,
+} from "../src/channels/routes";
+
+function encode(value: unknown): string {
+ return Buffer.from(JSON.stringify(value), "utf8").toString("base64url");
+}
+
+describe("decodeChannelCursor", () => {
+ test("round-trips a cursor to the microsecond", () => {
+ const cursor = {
+ pinned: true,
+ recency: "2026-09-01T00:00:00.123456Z",
+ id: "channel-1",
+ };
+ expect(decodeChannelCursor(encodeChannelCursor(cursor))).toEqual(cursor);
+ });
+
+ test("still reads a cursor minted to the millisecond", () => {
+ const cursor = {
+ pinned: false,
+ recency: "2026-09-01T00:00:00.123Z",
+ id: "channel-1",
+ };
+ expect(decodeChannelCursor(encode(cursor))).toEqual(cursor);
+ });
+
+ test.each([
+ "not-a-date",
+ "",
+ "2026-13-45T00:00:00Z",
+ "1",
+ "2026-09-01 00:00:00",
+ "2026-09-01T00:00:00+02:00",
+ ])("reads a recency of %p as the first page", (recency) => {
+ expect(
+ decodeChannelCursor(encode({ pinned: false, recency, id: "channel-1" })),
+ ).toBeUndefined();
+ });
+
+ test("reads garbage and a missing cursor as the first page", () => {
+ expect(decodeChannelCursor("%%%")).toBeUndefined();
+ expect(decodeChannelCursor(undefined)).toBeUndefined();
+ });
+});