Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed
MCP
blame on this site's /mcp
CLI
abso blame client/src/news-stream.ts --version 285 --json
JSON
/christopher/abso-core/blame/client/src/news-stream.ts?version=285&format=json
156 linesbox-grants 100%Last change box-grants in version 285,

156 lines from 5 changes in 4 versions.

box-grants269abso news --stream and --follow (abso 690, news wakes agents): --stream mints a ticket and prints the Monitor call (ws URL plus subprotocols, 30-minute timeout) so a Claude Code chat wakes the moment news arrives, with no polling. --follow holds the seat's stream itself, a line per item, a new ticket per socket, a ping every 45 s, waits 1 s doubling to a minute between sockets, and stops on a refused key or a server without the stream (404). The key never goes in a URL or a Monitor call. Needs the server's news/ticket and news/stream routes (Read Speed, next). Tests: client/test/news-stream.test.ts.Compare1// abso news --stream and --follow (abso 690): news the moment it arrives, with no polling.
2//
3// The server holds a WebSocket per seat on the project's object. Opening one takes a ticket, minted with the seat's
4// key: it opens that seat's stream once, within a minute, and rides as a subprotocol, never in the URL. A Claude Code
5// Monitor can't send headers, and whatever its call names is written into the chat, so the key never goes there.
6//
7// --stream prints the Monitor's arguments with a fresh ticket. --follow holds the socket itself and prints a line per
8// item, opening a new one (with a new ticket) whenever it closes; the ticket is never printed. On connect the server
9// sends what arrived since the seat last heard its news, so nothing is lost between sockets.
11/** The subprotocol a news stream speaks; the ticket rides beside it as `abso-ticket.<ticket>`. */
12export const NEWS_PROTOCOL = "abso-news.1";
14/** What the server answers a ticket request with. */
15export interface NewsTicket {
16 readonly url: string;
17 readonly protocols: readonly string[];
box-grants285abso news --stream (abso 690): read the ticket's expiry as the ISO time the server sends (staging 1418) as well as milliseconds. It printed 'within NaN seconds', and the stream mark that stands the hooks mod's poll down came out NaN, so the poll kept running. Found in the staging proof. Tests: client/test/news-stream.test.ts.Compare18 /** When the ticket stops working: the server sends an ISO time; milliseconds are taken too. */
19 readonly expiresAt: number | string;
box-grants269abso news --stream and --follow (abso 690, news wakes agents): --stream mints a ticket and prints the Monitor call (ws URL plus subprotocols, 30-minute timeout) so a Claude Code chat wakes the moment news arrives, with no polling. --follow holds the seat's stream itself, a line per item, a new ticket per socket, a ping every 45 s, waits 1 s doubling to a minute between sockets, and stops on a refused key or a server without the stream (404). The key never goes in a URL or a Monitor call. Needs the server's news/ticket and news/stream routes (Read Speed, next). Tests: client/test/news-stream.test.ts.Compare20}
box-grants285abso news --stream (abso 690): read the ticket's expiry as the ISO time the server sends (staging 1418) as well as milliseconds. It printed 'within NaN seconds', and the stream mark that stands the hooks mod's poll down came out NaN, so the poll kept running. Found in the staging proof. Tests: client/test/news-stream.test.ts.Compare22/** A ticket's expiry in milliseconds, whichever form the server sent; now when it can't be read. */
23export function expiresAtMs(ticket: NewsTicket, now: number): number {
24 const at = typeof ticket.expiresAt === "number" ? ticket.expiresAt : Date.parse(ticket.expiresAt);
25 return Number.isFinite(at) ? at : now;
box-grants269abso news --stream and --follow (abso 690, news wakes agents): --stream mints a ticket and prints the Monitor call (ws URL plus subprotocols, 30-minute timeout) so a Claude Code chat wakes the moment news arrives, with no polling. --follow holds the seat's stream itself, a line per item, a new ticket per socket, a ping every 45 s, waits 1 s doubling to a minute between sockets, and stops on a refused key or a server without the stream (404). The key never goes in a URL or a Monitor call. Needs the server's news/ticket and news/stream routes (Read Speed, next). Tests: client/test/news-stream.test.ts.Compare28/** A Monitor lasts at most 30 minutes; the skill re-arms it when it ends. */
29export const MONITOR_TIMEOUT_MS = 30 * 60_000;
31/** The Monitor call that holds a seat's stream: `Monitor({ ws, description, timeout_ms })`. */
32export function monitorArgs(
33 ticket: NewsTicket,
34 project: string,
35 agent: string,
36): { ws: { url: string; protocols: string[] }; description: string; timeout_ms: number } {
37 return {
38 ws: { url: ticket.url, protocols: [...ticket.protocols] },
39 description: `abso news for ${agent} in ${project}`,
40 timeout_ms: MONITOR_TIMEOUT_MS,
41 };
44/** The text --stream prints: the call to make, then the arguments as one line of JSON. */
45export function streamText(ticket: NewsTicket, project: string, agent: string, now = Date.now()): string {
box-grants285abso news --stream (abso 690): read the ticket's expiry as the ISO time the server sends (staging 1418) as well as milliseconds. It printed 'within NaN seconds', and the stream mark that stands the hooks mod's poll down came out NaN, so the poll kept running. Found in the staging proof. Tests: client/test/news-stream.test.ts.Compare46 const seconds = Math.max(0, Math.round((expiresAtMs(ticket, now) - now) / 1000));
box-grants269abso news --stream and --follow (abso 690, news wakes agents): --stream mints a ticket and prints the Monitor call (ws URL plus subprotocols, 30-minute timeout) so a Claude Code chat wakes the moment news arrives, with no polling. --follow holds the seat's stream itself, a line per item, a new ticket per socket, a ping every 45 s, waits 1 s doubling to a minute between sockets, and stops on a refused key or a server without the stream (404). The key never goes in a URL or a Monitor call. Needs the server's news/ticket and news/stream routes (Read Speed, next). Tests: client/test/news-stream.test.ts.Compare47 return [
48 `Open this with Monitor within ${seconds} seconds (the ticket opens ${agent}'s news stream once). When the Monitor ends or the socket closes, run abso news --stream again; the new socket sends what arrived in between.`,
49 JSON.stringify(monitorArgs(ticket, project, agent)),
50 ].join("\n");
box-grants270abso 690: while a seat holds its news stream, the abso-hooks mod's 30-second news poll stands down. abso news --stream marks the folder (.abso/news-stream.json) for the Monitor's 30 minutes plus the ticket's minute; --follow renews the mark on open and at each 45-second ping. The poll asks again once the mark runs out. Tests: client/test/news-stream.test.ts, client/hooks/register.test.ts (node client/test-mods.ts).Compare53/** The file that says this folder's seat holds its stream, until when: the abso-hooks mod's news poll skips meanwhile. */
54export const STREAM_MARK = "news-stream.json";
56/** How long --stream's mark holds: the Monitor's 30 minutes, and the minute the ticket waits to be used. */
57export function streamMarkUntil(ticket: NewsTicket, now: number): number {
box-grants285abso news --stream (abso 690): read the ticket's expiry as the ISO time the server sends (staging 1418) as well as milliseconds. It printed 'within NaN seconds', and the stream mark that stands the hooks mod's poll down came out NaN, so the poll kept running. Found in the staging proof. Tests: client/test/news-stream.test.ts.Compare58 return Math.max(now, expiresAtMs(ticket, now)) + MONITOR_TIMEOUT_MS;
box-grants270abso 690: while a seat holds its news stream, the abso-hooks mod's 30-second news poll stands down. abso news --stream marks the folder (.abso/news-stream.json) for the Monitor's 30 minutes plus the ticket's minute; --follow renews the mark on open and at each 45-second ping. The poll asks again once the mark runs out. Tests: client/test/news-stream.test.ts, client/hooks/register.test.ts (node client/test-mods.ts).Compare59}
box-grants269abso news --stream and --follow (abso 690, news wakes agents): --stream mints a ticket and prints the Monitor call (ws URL plus subprotocols, 30-minute timeout) so a Claude Code chat wakes the moment news arrives, with no polling. --follow holds the seat's stream itself, a line per item, a new ticket per socket, a ping every 45 s, waits 1 s doubling to a minute between sockets, and stops on a refused key or a server without the stream (404). The key never goes in a URL or a Monitor call. Needs the server's news/ticket and news/stream routes (Read Speed, next). Tests: client/test/news-stream.test.ts.Compare61/** The part of a WebSocket --follow uses: the platform's own, or a test's. */
62export interface Socket {
63 onopen: (() => void) | null;
64 onmessage: ((e: { data: unknown }) => void) | null;
65 onclose: ((e: { code: number; reason: string }) => void) | null;
66 onerror: ((e: unknown) => void) | null;
67 send(text: string): void;
68 close(code?: number): void;
box-grants273abso news --follow (abso 690): stop on close 1008 (a revoked key, or a fifth socket for the agent), the server's shape from Read Speed, instead of 4401/4403. Any other close opens again with a new ticket. Tests: client/test/news-stream.test.ts.Compare71/** A close the server sends for a revoked key or a fifth socket (1008): --follow stops instead of trying again. */
box-grants269abso news --stream and --follow (abso 690, news wakes agents): --stream mints a ticket and prints the Monitor call (ws URL plus subprotocols, 30-minute timeout) so a Claude Code chat wakes the moment news arrives, with no polling. --follow holds the seat's stream itself, a line per item, a new ticket per socket, a ping every 45 s, waits 1 s doubling to a minute between sockets, and stops on a refused key or a server without the stream (404). The key never goes in a URL or a Monitor call. Needs the server's news/ticket and news/stream routes (Read Speed, next). Tests: client/test/news-stream.test.ts.Compare72export function isRefusal(code: number): boolean {
box-grants273abso news --follow (abso 690): stop on close 1008 (a revoked key, or a fifth socket for the agent), the server's shape from Read Speed, instead of 4401/4403. Any other close opens again with a new ticket. Tests: client/test/news-stream.test.ts.Compare73 return code === 1008;
box-grants269abso news --stream and --follow (abso 690, news wakes agents): --stream mints a ticket and prints the Monitor call (ws URL plus subprotocols, 30-minute timeout) so a Claude Code chat wakes the moment news arrives, with no polling. --follow holds the seat's stream itself, a line per item, a new ticket per socket, a ping every 45 s, waits 1 s doubling to a minute between sockets, and stops on a refused key or a server without the stream (404). The key never goes in a URL or a Monitor call. Needs the server's news/ticket and news/stream routes (Read Speed, next). Tests: client/test/news-stream.test.ts.Compare74}
76/** How long --follow waits before opening a socket again: 1 s doubling to a minute; a socket that lived a minute resets it. */
77export function nextWait(previous: number, livedMs: number): number {
78 if (livedMs >= 60_000) return 1_000;
79 return Math.min(60_000, Math.max(1_000, previous * 2));
82/** The client sends this every PING_MS so an idle socket stays open; the object answers it without waking. */
83export const PING = "ping";
84export const PONG = "pong";
85export const PING_MS = 45_000;
87export interface FollowDeps {
88 mint(): Promise<NewsTicket>;
89 connect(url: string, protocols: readonly string[]): Socket;
90 /** One news item, as the server sent it. */
91 write(line: string): void;
92 /** A line about the stream itself, not news. */
93 note(line: string): void;
94 sleep(ms: number): Promise<void>;
95 now(): number;
96 every(ms: number, fn: () => void): () => void;
box-grants270abso 690: while a seat holds its news stream, the abso-hooks mod's 30-second news poll stands down. abso news --stream marks the folder (.abso/news-stream.json) for the Monitor's 30 minutes plus the ticket's minute; --follow renews the mark on open and at each 45-second ping. The poll asks again once the mark runs out. Tests: client/test/news-stream.test.ts, client/hooks/register.test.ts (node client/test-mods.ts).Compare97 /** Called when a socket opens and at each ping: the stream is held until the next ping is due, and a little more. */
98 held?(until: number): void;
box-grants269abso news --stream and --follow (abso 690, news wakes agents): --stream mints a ticket and prints the Monitor call (ws URL plus subprotocols, 30-minute timeout) so a Claude Code chat wakes the moment news arrives, with no polling. --follow holds the seat's stream itself, a line per item, a new ticket per socket, a ping every 45 s, waits 1 s doubling to a minute between sockets, and stops on a refused key or a server without the stream (404). The key never goes in a URL or a Monitor call. Needs the server's news/ticket and news/stream routes (Read Speed, next). Tests: client/test/news-stream.test.ts.Compare99 /** Set when --follow should stop (Ctrl-C, or a test). */
100 readonly stopped: () => boolean;
103/** Why a mint failed: a refused key, or a server with no news stream (404), stops --follow; anything else is tried again. */
104function refusedMint(err: unknown): boolean {
105 const status = (err as { status?: number }).status;
106 return status === 401 || status === 403 || status === 404;
109/**
110 * Holds the seat's stream, printing one line per item, until the key is refused or `stopped()`.
111 * Returns why it ended.
112 */
113export async function follow(deps: FollowDeps): Promise<{ ended: "refused" | "stopped"; reason: string }> {
114 let wait = 1_000;
115 while (!deps.stopped()) {
116 let ticket: NewsTicket;
117 try {
118 ticket = await deps.mint();
119 } catch (err) {
120 if (refusedMint(err)) return { ended: "refused", reason: (err as Error).message };
121 deps.note(`(abso news --follow: no ticket: ${(err as Error).message}; trying again in ${wait / 1000} s)`);
122 await deps.sleep(wait);
123 wait = nextWait(wait, 0);
124 continue;
125 }
126 const opened = deps.now();
127 const closed = await new Promise<{ code: number; reason: string }>((done) => {
128 const socket = deps.connect(ticket.url, ticket.protocols);
129 let stopPing = () => {};
130 socket.onopen = () => {
box-grants270abso 690: while a seat holds its news stream, the abso-hooks mod's 30-second news poll stands down. abso news --stream marks the folder (.abso/news-stream.json) for the Monitor's 30 minutes plus the ticket's minute; --follow renews the mark on open and at each 45-second ping. The poll asks again once the mark runs out. Tests: client/test/news-stream.test.ts, client/hooks/register.test.ts (node client/test-mods.ts).Compare131 deps.held?.(deps.now() + 2 * PING_MS);
box-grants269abso news --stream and --follow (abso 690, news wakes agents): --stream mints a ticket and prints the Monitor call (ws URL plus subprotocols, 30-minute timeout) so a Claude Code chat wakes the moment news arrives, with no polling. --follow holds the seat's stream itself, a line per item, a new ticket per socket, a ping every 45 s, waits 1 s doubling to a minute between sockets, and stops on a refused key or a server without the stream (404). The key never goes in a URL or a Monitor call. Needs the server's news/ticket and news/stream routes (Read Speed, next). Tests: client/test/news-stream.test.ts.Compare132 stopPing = deps.every(PING_MS, () => {
133 if (deps.stopped()) socket.close(1000);
box-grants270abso 690: while a seat holds its news stream, the abso-hooks mod's 30-second news poll stands down. abso news --stream marks the folder (.abso/news-stream.json) for the Monitor's 30 minutes plus the ticket's minute; --follow renews the mark on open and at each 45-second ping. The poll asks again once the mark runs out. Tests: client/test/news-stream.test.ts, client/hooks/register.test.ts (node client/test-mods.ts).Compare134 else {
135 socket.send(PING);
136 deps.held?.(deps.now() + 2 * PING_MS);
137 }
box-grants269abso news --stream and --follow (abso 690, news wakes agents): --stream mints a ticket and prints the Monitor call (ws URL plus subprotocols, 30-minute timeout) so a Claude Code chat wakes the moment news arrives, with no polling. --follow holds the seat's stream itself, a line per item, a new ticket per socket, a ping every 45 s, waits 1 s doubling to a minute between sockets, and stops on a refused key or a server without the stream (404). The key never goes in a URL or a Monitor call. Needs the server's news/ticket and news/stream routes (Read Speed, next). Tests: client/test/news-stream.test.ts.Compare138 });
139 };
140 socket.onmessage = (e) => {
141 if (typeof e.data !== "string" || e.data === PONG) return;
142 deps.write(e.data);
143 };
144 socket.onerror = () => {};
145 socket.onclose = (e) => {
146 stopPing();
147 done({ code: e.code, reason: e.reason });
148 };
149 });
150 if (isRefusal(closed.code)) return { ended: "refused", reason: closed.reason || `closed ${closed.code}` };
151 if (deps.stopped()) break;
152 wait = nextWait(wait, deps.now() - opened);
153 await deps.sleep(wait);
154 }
155 return { ended: "stopped", reason: "stopped" };