@cotal-ai/web 0.170.0 → 0.171.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,166 @@
1
+ import { type DeliveryMode, type CotalMessage } from "@cotal-ai/core";
2
+ /** A request the CALLER got wrong, so the frame answers 400 rather than the 500 it gives a server
3
+ * fault. Without this every malformed query reads, in the log and in the body, exactly like the
4
+ * dashboard breaking. */
5
+ export declare class BadRequest extends Error {
6
+ }
7
+ /** A body the caller sent that this server declines to READ, which is a different fact from a body
8
+ * it read and disliked, and carries its own status. Deliberately NOT a `BadRequest`: 400 says
9
+ * "what you sent is wrong", 413 says "I stopped before finding out", and a caller that retries on
10
+ * 400 by fixing its payload would loop forever on a size it never learns is the problem. */
11
+ export declare class PayloadTooLarge extends Error {
12
+ }
13
+ /** A delete upload that did not finish within its total body deadline. */
14
+ export declare class BodyDeadlineExceeded extends Error {
15
+ }
16
+ /** A post the broker, or the operator's connection to it, refused. Neither the caller's fault nor
17
+ * this server's, so the frame answers 502 with the broker's own message, escaped by
18
+ * {@link escapeForOperator}. */
19
+ export declare class BrokerRefused extends Error {
20
+ }
21
+ /** The most a request body may weigh before a body route refuses to keep reading it.
22
+ *
23
+ * The delete route's body has one field, a channel name, and `assertValidChannel` bounds a usable
24
+ * name to dotted `[A-Za-z0-9_-]` segments that the wire will carry as a subject. The dashboard's
25
+ * own delete sends about sixty bytes. 8 KiB is therefore not a guess at a working size, it is two
26
+ * orders of magnitude ABOVE any name the broker would accept, chosen so that no legitimate
27
+ * caller can meet it and an abusive one meets it immediately. The send route shares the cap, so a
28
+ * post's text is bounded by it too.
29
+ *
30
+ * MEASURED, before this existed, against the shipped route over a local broker with a raw socket
31
+ * rather than `fetch`, because `fetch` hides how much of the body actually left the client:
32
+ * 30,000,000 bytes posted -> ALL 30,000,000 sent, 70,000,144 bytes of refusal returned,
33
+ * peak RSS +1.39 GB, 1022 ms. The read was unbounded and ran to completion before the route
34
+ * formed an opinion, so the refusal was the expensive part rather than the cheap one. */
35
+ export declare const MAX_BODY_BYTES: number;
36
+ export declare const DELETE_BODY_DEADLINE_MS = 10000;
37
+ /** The only routes that read a request body. Every other route refuses an announced body. */
38
+ export declare const BODY_ROUTES: Set<string>;
39
+ /** QUOTE A CALLER'S OWN VALUE SO AN OPERATOR CAN READ IT.
40
+ *
41
+ * `JSON.stringify` was doing two jobs at every site below and only claims one of them. It builds
42
+ * valid JSON, and on the way it escapes every C0 control, so `ESC` arrives as the six characters
43
+ * `\u001b` and a newline as `\n`. It is not a renderer for humans and never said it was: DEL, the
44
+ * C1 range, `U+2028`/`U+2029`, the bidi controls and the zero-width characters are all valid JSON
45
+ * string content and pass through untouched.
46
+ *
47
+ * MEASURED against the shipped `web()` entry before this existed, driving `/api/activity?limit=`
48
+ * with each codepoint percent-encoded and reading the answer as BYTES rather than through a JSON
49
+ * parse (a parse decodes the very thing under test and hands the input back whatever the server
50
+ * wrote). Six of eight arrived raw in BOTH the 400 body and the operator's stderr line: DEL,
51
+ * `U+0085`, `U+009B`, `U+202E`, `U+2028`, `U+2029`. `ESC` and `LF` came back escaped, which is
52
+ * what makes the other six a finding rather than a property of the harness.
53
+ *
54
+ * The escape is emitted as `\uXXXX`, so the message stays valid JSON on the body path and reads as
55
+ * the codepoint it is on the terminal path. Applied where the value is QUOTED rather than where it
56
+ * is written out, because the untrusted thing is the value and the message is derived from it: a
57
+ * guard at the two exits fences those two exits, while a guard here travels with the sentence.
58
+ *
59
+ * The loop below is per UTF-16 UNIT, not per codepoint: `u` hands the callback a whole codepoint,
60
+ * so an astral one (a tag character, a musical control) arrives as its surrogate pair and has to
61
+ * leave as two escapes. `\u1d173` is not a JSON escape, and a body carrying it would stop parsing
62
+ * for the caller who asked what was wrong with their request. */
63
+ export declare function quoteForOperator(value: string): string;
64
+ /** Escapes a message someone else wrote, keeping its wording. A broker refusal names the subject it
65
+ * refused, and a DM subject carries a recipient id taken from another agent's presence card, so the
66
+ * message reaches the 502 body and the operator's line as untrusted text. Each codepoint the quoter
67
+ * escapes leaves as its escape; the rest, quotes included, stays as the broker wrote it. */
68
+ export declare function escapeForOperator(text: string): string;
69
+ /** The channel name out of the path. A percent escape the decoder cannot read is the caller having
70
+ * typed a bad one, so it is refused as a bad request like any other malformed input. Left as a
71
+ * bare `URIError` it reached the request frame unrecognised and was reported as a server fault,
72
+ * which is the one thing the 400/500 split exists to prevent. */
73
+ export declare function channelNameFromPath(raw: string): string;
74
+ /** A caller's channel name, refused unless it is ALREADY the name the wire uses.
75
+ *
76
+ * The wire builds a channel's subject through `token()`, which rewrites anything outside
77
+ * `[A-Za-z0-9_-]` to `_` rather than refusing it, so a name a caller invented and a real channel
78
+ * collide: `abc` + U+202E and `abc_` are one channel on the wire while the dashboard answers with
79
+ * whichever the caller typed. Measured against the shipped routes on a local broker before this
80
+ * existed, with one message seeded on `abc_`: the history read under the first name returned the
81
+ * second's message, and the delete route purged the second while answering
82
+ * `{"ok":true,"channel":"abc<U+202E>","purged":1}`. Rendering that answer readably would have made
83
+ * the lie legible without removing it, which is why this refuses the name instead.
84
+ *
85
+ * Core already owns the rule, written for the same aliasing gap on the ACL side; the dashboard
86
+ * simply never asked it. Its message is rebuilt here rather than passed through, because core
87
+ * quotes the raw name with `JSON.stringify` and this route is the one place that must not. */
88
+ export declare function canonicalChannel(name: string): string;
89
+ /** The most messages one read may ask for. A route reads up to `limit` messages and holds them all
90
+ * before it answers, so a limit with no ceiling lets one request make the server read a stream's
91
+ * whole retained history. The dashboard asks for at most 500. */
92
+ export declare const MAX_HISTORY_LIMIT = 2000;
93
+ /** The `limit` out of the query. The ceiling refusal is reachable only through the digits-only test
94
+ * above it, so its value cannot carry anything the quoter would escape today. It quotes anyway:
95
+ * the guarantee that a refusal renders its input unambiguously should hold because the quoting site
96
+ * holds it, not because a regex two lines up stays exactly as narrow as it is this morning.
97
+ *
98
+ * THE LIMIT, PARSED ONCE, because three routes each re-deriving
99
+ * `query.get("limit") ? Number(...) : N` is how they came to disagree about the same parameter.
100
+ *
101
+ * MEASURED ON THE SHIPPED ROUTES, against a real broker, before this existed:
102
+ * ?limit=abc `Number("abc")` is NaN and every comparison against NaN is false, so core's
103
+ * `limit <= 0` guard does not fire and the widening search's two exits can
104
+ * never be true. No answer after 30s, and the ABANDONED request kept consuming
105
+ * half a core with its caller long gone, invisible because the process keeps
106
+ * serving everything else.
107
+ * ?limit=Infinity passes the same guard, and `slice(-Infinity)` is the whole array: a channel's
108
+ * entire retained history from a one word request. `1e999` is the same value.
109
+ * ?limit=2.5 silently truncated to 2.
110
+ * ?limit=" 5" accepted as 5, because `Number()` trims whitespace.
111
+ *
112
+ * So the accepted form is the narrow one: a plain run of digits no larger than
113
+ * {@link MAX_HISTORY_LIMIT}. `0` keeps
114
+ * meaning zero, which is what it already did and what a caller expects; an absent or empty
115
+ * parameter keeps meaning the route's own default, the one shape the old parse got right.
116
+ *
117
+ * Everything else is REFUSED rather than clamped. A clamp would answer a request nobody made, and
118
+ * the caller who wrote `limit=2.5` would never learn that the page they read was not the page they
119
+ * asked for. */
120
+ export declare function historyLimit(query: URLSearchParams, fallback: number): number;
121
+ /** An agent id out of a path segment: decoded, and a principal key, or a BadRequest naming it.
122
+ *
123
+ * The id is the roster's `card.id`, which is `principalKey(owner, actor).key` by construction, so
124
+ * the agent is named by its principal and never by a display name: two live agents with one name
125
+ * never share a session. The answer's `id` is rebuilt from the parsed tokens rather than passed
126
+ * through, so the id a route acts on is the one the event channel is formed from. */
127
+ export declare function agentIdFromPath(raw: string): {
128
+ id: string;
129
+ owner: string;
130
+ actor: string;
131
+ };
132
+ /** What the live tap does with one message. A chat message on a principal-shaped event channel is an
133
+ * agent frame for `principalKey(owner, actor).key`; everything else goes out as today's `message`.
134
+ *
135
+ * WHY FRAMES LEAVE THE BROADCAST. The observer taps `chat.>`, and an agent's event channel is a
136
+ * chat channel, so this one subscription receives every agent's frames. A NATS wildcard cannot
137
+ * exclude a sub-tree, so the observer still receives them; what changes is who it forwards them to.
138
+ * Sent as `message` to every browser, each frame cost every open dashboard a write whether or not
139
+ * anyone was reading that agent. As `agent-frame` it reaches only the feeds watching that agent.
140
+ *
141
+ * The agent is the event channel's principal, the same derivation the chat-only filter reads, so a
142
+ * near-miss name such as `events.standup` stays an ordinary `message`. The channel is the one the
143
+ * broker policed, taken from the subject, never the payload's own claim.
144
+ *
145
+ * A FRAME FROM ANYONE BUT THE AGENT IS DROPPED, and dropped rather than sent on as a `message`. The
146
+ * subject's sender is the verified publisher, and a frame is that agent's only when the agent
147
+ * published it. On a mesh where another principal may write the channel (an open mesh, or a grant
148
+ * wider than the agent's own), a forged frame would otherwise read as the agent's session, and
149
+ * falling through to `message` would hand it to every browser instead. */
150
+ export declare function classifyTap(subject: string, msg: CotalMessage | undefined): {
151
+ event: "message";
152
+ data: {
153
+ mode: DeliveryMode;
154
+ senderId?: string;
155
+ channel?: string;
156
+ msg: CotalMessage;
157
+ };
158
+ } | {
159
+ event: "agent-frame";
160
+ agent: string;
161
+ data: {
162
+ id: string;
163
+ msg: CotalMessage;
164
+ };
165
+ } | null;
166
+ //# sourceMappingURL=web-request.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web-request.d.ts","sourceRoot":"","sources":["../src/web-request.ts"],"names":[],"mappings":"AAEA,OAAO,EAML,KAAK,YAAY,EAEjB,KAAK,YAAY,EAClB,MAAM,gBAAgB,CAAC;AAExB;;0BAE0B;AAC1B,qBAAa,UAAW,SAAQ,KAAK;CAAG;AAExC;;;6FAG6F;AAC7F,qBAAa,eAAgB,SAAQ,KAAK;CAAG;AAE7C,0EAA0E;AAC1E,qBAAa,oBAAqB,SAAQ,KAAK;CAAG;AAElD;;iCAEiC;AACjC,qBAAa,aAAc,SAAQ,KAAK;CAAG;AAE3C;;;;;;;;;;;;;4FAa4F;AAC5F,eAAO,MAAM,cAAc,QAAW,CAAC;AACvC,eAAO,MAAM,uBAAuB,QAAS,CAAC;AAE9C,6FAA6F;AAC7F,eAAO,MAAM,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AE,GAChF,IAAI,CA0BP"}
@@ -0,0 +1,276 @@
1
+ // Request input and operator text, moved verbatim from web.ts: caller errors, body caps, operator
2
+ // quoting and escaping, and channel, history, agent-id and tap parsing. web.ts re-exports the public names.
3
+ import { deliveryOf, eventChannelPrincipal, parsePrincipalKey, parseSubject, principalKey, assertValidChannel, } from "@cotal-ai/core";
4
+ /** A request the CALLER got wrong, so the frame answers 400 rather than the 500 it gives a server
5
+ * fault. Without this every malformed query reads, in the log and in the body, exactly like the
6
+ * dashboard breaking. */
7
+ export class BadRequest extends Error {
8
+ }
9
+ /** A body the caller sent that this server declines to READ, which is a different fact from a body
10
+ * it read and disliked, and carries its own status. Deliberately NOT a `BadRequest`: 400 says
11
+ * "what you sent is wrong", 413 says "I stopped before finding out", and a caller that retries on
12
+ * 400 by fixing its payload would loop forever on a size it never learns is the problem. */
13
+ export class PayloadTooLarge extends Error {
14
+ }
15
+ /** A delete upload that did not finish within its total body deadline. */
16
+ export class BodyDeadlineExceeded extends Error {
17
+ }
18
+ /** A post the broker, or the operator's connection to it, refused. Neither the caller's fault nor
19
+ * this server's, so the frame answers 502 with the broker's own message, escaped by
20
+ * {@link escapeForOperator}. */
21
+ export class BrokerRefused extends Error {
22
+ }
23
+ /** The most a request body may weigh before a body route refuses to keep reading it.
24
+ *
25
+ * The delete route's body has one field, a channel name, and `assertValidChannel` bounds a usable
26
+ * name to dotted `[A-Za-z0-9_-]` segments that the wire will carry as a subject. The dashboard's
27
+ * own delete sends about sixty bytes. 8 KiB is therefore not a guess at a working size, it is two
28
+ * orders of magnitude ABOVE any name the broker would accept, chosen so that no legitimate
29
+ * caller can meet it and an abusive one meets it immediately. The send route shares the cap, so a
30
+ * post's text is bounded by it too.
31
+ *
32
+ * MEASURED, before this existed, against the shipped route over a local broker with a raw socket
33
+ * rather than `fetch`, because `fetch` hides how much of the body actually left the client:
34
+ * 30,000,000 bytes posted -> ALL 30,000,000 sent, 70,000,144 bytes of refusal returned,
35
+ * peak RSS +1.39 GB, 1022 ms. The read was unbounded and ran to completion before the route
36
+ * formed an opinion, so the refusal was the expensive part rather than the cheap one. */
37
+ export const MAX_BODY_BYTES = 8 * 1024;
38
+ export const DELETE_BODY_DEADLINE_MS = 10_000;
39
+ /** The only routes that read a request body. Every other route refuses an announced body. */
40
+ export const BODY_ROUTES = new Set(["/api/channel/delete", "/api/send"]);
41
+ /** Codepoints `JSON.stringify` leaves RAW that PRODUCE NO GLYPH OF THEIR OWN, or that reorder the
42
+ * text around them, stated as Unicode PROPERTIES rather than as a hand list. That wording is
43
+ * narrower than "change what a reader sees" on purpose, and the narrowing is a review finding:
44
+ * the looser phrase admits every combining mark, and the paragraph at the end of this comment is
45
+ * why escaping those would be this issue pointed the other way. The first version of this WAS a hand list, and review
46
+ * found it missing U+061C, U+2060, the variation selectors and the tag characters, every one of
47
+ * which is exactly the thing the list said it closed. A list is a claim about a set nobody
48
+ * maintains; the property IS the set, and it moves with the Unicode version the runtime carries.
49
+ *
50
+ * Two properties, because neither contains the other and both name the same harm from a different
51
+ * side. `Default_Ignorable_Code_Point` is the renders-as-nothing family: the soft hyphen, the
52
+ * zero-width characters, the word joiner, the variation selectors, the tag characters and the BOM.
53
+ * `Cf` is the format family: characters with no glyph of their own that change how the text around
54
+ * them is read, which is where the interlinear annotation controls U+FFF9 to U+FFFB live. Review
55
+ * found those three arriving raw against a class that had only the first property, and they are the
56
+ * clearest case of the harm: they mark a span as base text plus its gloss, so a reader whose
57
+ * terminal does not implement them sees the two runs concatenated into a sentence nobody wrote.
58
+ * Measured, the second property adds 32 codepoints and not one of them is a letter or a digit.
59
+ *
60
+ * `Bidi_Control` is deliberately absent: measured on this runtime, all twelve of its codepoints,
61
+ * U+061C and the isolates included, are already default-ignorable, so naming it would be a second
62
+ * name for one set. The suite pins those twelve by hand, so a Unicode version that separated them
63
+ * goes red rather than quietly leaving a reordering character raw.
64
+ *
65
+ * What no property covers is DEL and the C1 controls, which are `Cc`, and U+2028/U+2029, which are
66
+ * line and paragraph separators, so those are named. C0 is absent because `JSON.stringify` already
67
+ * escapes all of it.
68
+ *
69
+ * NOT IN THIS CLASS, and deliberately: a VISIBLE character that merely resembles another. A Cyrillic
70
+ * small a is a letter, it renders as itself, and escaping it would make a refusal about a name a
71
+ * human typed unreadable, which is this issue pointed the other way. Confusables are a different
72
+ * problem with a different answer, and quoting for a human to read is not it.
73
+ *
74
+ * NOT IN THIS CLASS EITHER, and this one review reached by finding U+0338 COMBINING LONG SOLIDUS
75
+ * OVERLAY arriving raw and asking whether it belonged: a COMBINING MARK. It produces a visible
76
+ * mark on a visible base, and the property that carries it, `gc=Mn`, is the same one carrying the
77
+ * acute accent in a name written in NFD, the Devanagari vowel signs, the Arabic and Hebrew points
78
+ * and the Vietnamese tones. Measured on this runtime, marks are 2543 codepoints and only 263 of
79
+ * them are already in the class, so escaping them would take about 2280 codepoints of ordinary
80
+ * written language and render an accented name as its escapes. A mark CAN build a confusable
81
+ * (U+0338 over `=` renders as a not-equals sign, so a quoted `a=b` can display as `a` not-equals
82
+ * `b`), which is a real harm and the same one the paragraph above declines: it is unbounded, it
83
+ * needs no combining mark to exist, and its answer is normalization or confusable detection
84
+ * rather than making every script that writes with marks unreadable. The suite asserts both
85
+ * exclusions rather than only describing them. */
86
+ const INVISIBLE_AFTER_JSON = /[\p{Default_Ignorable_Code_Point}\p{gc=Cf}\u007f-\u009f\u2028\u2029]/gu;
87
+ /** QUOTE A CALLER'S OWN VALUE SO AN OPERATOR CAN READ IT.
88
+ *
89
+ * `JSON.stringify` was doing two jobs at every site below and only claims one of them. It builds
90
+ * valid JSON, and on the way it escapes every C0 control, so `ESC` arrives as the six characters
91
+ * `\u001b` and a newline as `\n`. It is not a renderer for humans and never said it was: DEL, the
92
+ * C1 range, `U+2028`/`U+2029`, the bidi controls and the zero-width characters are all valid JSON
93
+ * string content and pass through untouched.
94
+ *
95
+ * MEASURED against the shipped `web()` entry before this existed, driving `/api/activity?limit=`
96
+ * with each codepoint percent-encoded and reading the answer as BYTES rather than through a JSON
97
+ * parse (a parse decodes the very thing under test and hands the input back whatever the server
98
+ * wrote). Six of eight arrived raw in BOTH the 400 body and the operator's stderr line: DEL,
99
+ * `U+0085`, `U+009B`, `U+202E`, `U+2028`, `U+2029`. `ESC` and `LF` came back escaped, which is
100
+ * what makes the other six a finding rather than a property of the harness.
101
+ *
102
+ * The escape is emitted as `\uXXXX`, so the message stays valid JSON on the body path and reads as
103
+ * the codepoint it is on the terminal path. Applied where the value is QUOTED rather than where it
104
+ * is written out, because the untrusted thing is the value and the message is derived from it: a
105
+ * guard at the two exits fences those two exits, while a guard here travels with the sentence.
106
+ *
107
+ * The loop below is per UTF-16 UNIT, not per codepoint: `u` hands the callback a whole codepoint,
108
+ * so an astral one (a tag character, a musical control) arrives as its surrogate pair and has to
109
+ * leave as two escapes. `\u1d173` is not a JSON escape, and a body carrying it would stop parsing
110
+ * for the caller who asked what was wrong with their request. */
111
+ export function quoteForOperator(value) {
112
+ return JSON.stringify(value).replace(INVISIBLE_AFTER_JSON, (ch) => {
113
+ let out = "";
114
+ for (let i = 0; i < ch.length; i++)
115
+ out += "\\u" + ch.charCodeAt(i).toString(16).padStart(4, "0");
116
+ return out;
117
+ });
118
+ }
119
+ /** Every codepoint {@link quoteForOperator} escapes, the C0 controls included, without its quotes. */
120
+ const UNREADABLE = new RegExp(`[\\u0000-\\u001f]|${INVISIBLE_AFTER_JSON.source}`, "gu");
121
+ /** Escapes a message someone else wrote, keeping its wording. A broker refusal names the subject it
122
+ * refused, and a DM subject carries a recipient id taken from another agent's presence card, so the
123
+ * message reaches the 502 body and the operator's line as untrusted text. Each codepoint the quoter
124
+ * escapes leaves as its escape; the rest, quotes included, stays as the broker wrote it. */
125
+ export function escapeForOperator(text) {
126
+ return text.replace(UNREADABLE, (ch) => quoteForOperator(ch).slice(1, -1));
127
+ }
128
+ /** The channel name out of the path. A percent escape the decoder cannot read is the caller having
129
+ * typed a bad one, so it is refused as a bad request like any other malformed input. Left as a
130
+ * bare `URIError` it reached the request frame unrecognised and was reported as a server fault,
131
+ * which is the one thing the 400/500 split exists to prevent. */
132
+ export function channelNameFromPath(raw) {
133
+ let name;
134
+ try {
135
+ name = decodeURIComponent(raw);
136
+ }
137
+ catch {
138
+ throw new BadRequest(`channel name ${quoteForOperator(raw)} is not valid percent-encoded text`);
139
+ }
140
+ return canonicalChannel(name);
141
+ }
142
+ /** A caller's channel name, refused unless it is ALREADY the name the wire uses.
143
+ *
144
+ * The wire builds a channel's subject through `token()`, which rewrites anything outside
145
+ * `[A-Za-z0-9_-]` to `_` rather than refusing it, so a name a caller invented and a real channel
146
+ * collide: `abc` + U+202E and `abc_` are one channel on the wire while the dashboard answers with
147
+ * whichever the caller typed. Measured against the shipped routes on a local broker before this
148
+ * existed, with one message seeded on `abc_`: the history read under the first name returned the
149
+ * second's message, and the delete route purged the second while answering
150
+ * `{"ok":true,"channel":"abc<U+202E>","purged":1}`. Rendering that answer readably would have made
151
+ * the lie legible without removing it, which is why this refuses the name instead.
152
+ *
153
+ * Core already owns the rule, written for the same aliasing gap on the ACL side; the dashboard
154
+ * simply never asked it. Its message is rebuilt here rather than passed through, because core
155
+ * quotes the raw name with `JSON.stringify` and this route is the one place that must not. */
156
+ export function canonicalChannel(name) {
157
+ try {
158
+ return assertValidChannel(name);
159
+ }
160
+ catch {
161
+ throw new BadRequest(`channel name ${quoteForOperator(name)} is not a channel: dotted segments of [A-Za-z0-9_-], ` +
162
+ `and a name the wire would rewrite would address a different channel than it names`);
163
+ }
164
+ }
165
+ /** The most messages one read may ask for. A route reads up to `limit` messages and holds them all
166
+ * before it answers, so a limit with no ceiling lets one request make the server read a stream's
167
+ * whole retained history. The dashboard asks for at most 500. */
168
+ export const MAX_HISTORY_LIMIT = 2000;
169
+ /** The `limit` out of the query. The ceiling refusal is reachable only through the digits-only test
170
+ * above it, so its value cannot carry anything the quoter would escape today. It quotes anyway:
171
+ * the guarantee that a refusal renders its input unambiguously should hold because the quoting site
172
+ * holds it, not because a regex two lines up stays exactly as narrow as it is this morning.
173
+ *
174
+ * THE LIMIT, PARSED ONCE, because three routes each re-deriving
175
+ * `query.get("limit") ? Number(...) : N` is how they came to disagree about the same parameter.
176
+ *
177
+ * MEASURED ON THE SHIPPED ROUTES, against a real broker, before this existed:
178
+ * ?limit=abc `Number("abc")` is NaN and every comparison against NaN is false, so core's
179
+ * `limit <= 0` guard does not fire and the widening search's two exits can
180
+ * never be true. No answer after 30s, and the ABANDONED request kept consuming
181
+ * half a core with its caller long gone, invisible because the process keeps
182
+ * serving everything else.
183
+ * ?limit=Infinity passes the same guard, and `slice(-Infinity)` is the whole array: a channel's
184
+ * entire retained history from a one word request. `1e999` is the same value.
185
+ * ?limit=2.5 silently truncated to 2.
186
+ * ?limit=" 5" accepted as 5, because `Number()` trims whitespace.
187
+ *
188
+ * So the accepted form is the narrow one: a plain run of digits no larger than
189
+ * {@link MAX_HISTORY_LIMIT}. `0` keeps
190
+ * meaning zero, which is what it already did and what a caller expects; an absent or empty
191
+ * parameter keeps meaning the route's own default, the one shape the old parse got right.
192
+ *
193
+ * Everything else is REFUSED rather than clamped. A clamp would answer a request nobody made, and
194
+ * the caller who wrote `limit=2.5` would never learn that the page they read was not the page they
195
+ * asked for. */
196
+ export function historyLimit(query, fallback) {
197
+ const raw = query.get("limit");
198
+ if (raw === null || raw === "")
199
+ return fallback;
200
+ if (!/^[0-9]+$/.test(raw))
201
+ throw new BadRequest(`limit must be a whole number of messages, received ${quoteForOperator(raw)}`);
202
+ // The ceiling also covers a run of digits too long for a double to count exactly: every such
203
+ // number is far above it.
204
+ const n = Number(raw);
205
+ if (n > MAX_HISTORY_LIMIT)
206
+ throw new BadRequest(`limit ${quoteForOperator(raw)} is above the most this server reads at once, ${MAX_HISTORY_LIMIT} messages`);
207
+ return n;
208
+ }
209
+ /** An agent id out of a path segment: decoded, and a principal key, or a BadRequest naming it.
210
+ *
211
+ * The id is the roster's `card.id`, which is `principalKey(owner, actor).key` by construction, so
212
+ * the agent is named by its principal and never by a display name: two live agents with one name
213
+ * never share a session. The answer's `id` is rebuilt from the parsed tokens rather than passed
214
+ * through, so the id a route acts on is the one the event channel is formed from. */
215
+ export function agentIdFromPath(raw) {
216
+ let decoded;
217
+ try {
218
+ decoded = decodeURIComponent(raw);
219
+ }
220
+ catch {
221
+ throw new BadRequest(`agent id ${quoteForOperator(raw)} is not valid percent-encoded text`);
222
+ }
223
+ const principal = parsePrincipalKey(decoded);
224
+ if (principal === null)
225
+ throw new BadRequest(`agent id ${quoteForOperator(decoded)} is not an agent id: <owner>.<actor>, each of [A-Za-z0-9_]`);
226
+ return { id: principalKey(principal.owner, principal.actor).key, ...principal };
227
+ }
228
+ /** What the live tap does with one message. A chat message on a principal-shaped event channel is an
229
+ * agent frame for `principalKey(owner, actor).key`; everything else goes out as today's `message`.
230
+ *
231
+ * WHY FRAMES LEAVE THE BROADCAST. The observer taps `chat.>`, and an agent's event channel is a
232
+ * chat channel, so this one subscription receives every agent's frames. A NATS wildcard cannot
233
+ * exclude a sub-tree, so the observer still receives them; what changes is who it forwards them to.
234
+ * Sent as `message` to every browser, each frame cost every open dashboard a write whether or not
235
+ * anyone was reading that agent. As `agent-frame` it reaches only the feeds watching that agent.
236
+ *
237
+ * The agent is the event channel's principal, the same derivation the chat-only filter reads, so a
238
+ * near-miss name such as `events.standup` stays an ordinary `message`. The channel is the one the
239
+ * broker policed, taken from the subject, never the payload's own claim.
240
+ *
241
+ * A FRAME FROM ANYONE BUT THE AGENT IS DROPPED, and dropped rather than sent on as a `message`. The
242
+ * subject's sender is the verified publisher, and a frame is that agent's only when the agent
243
+ * published it. On a mesh where another principal may write the channel (an open mesh, or a grant
244
+ * wider than the agent's own), a forged frame would otherwise read as the agent's session, and
245
+ * falling through to `message` would hand it to every browser instead. */
246
+ export function classifyTap(subject, msg) {
247
+ const parsed = parseSubject(subject);
248
+ if (parsed?.kind === "evp") {
249
+ if (!msg || parsed.sender !== msg.from?.id)
250
+ return null;
251
+ return { event: "agent-frame", agent: parsed.sender, data: { id: parsed.sender, msg } };
252
+ }
253
+ const mode = deliveryOf(subject);
254
+ if (!mode || !msg)
255
+ return null;
256
+ // senderId is the subject's sender token, the *verified* publisher (the server policed who could
257
+ // publish it), vs the advisory `from` in the payload.
258
+ const senderId = parsed?.sender;
259
+ // The channel the broker actually POLICED, taken from the subject rather than the payload: a
260
+ // publish grant is per-channel (`chat.<owner>.<actor>.<ch>`), so this token is covered by the
261
+ // minted grant, while `msg.channel` is publisher-supplied and backed by nothing.
262
+ //
263
+ // Gated on kind, and the gate is load-bearing rather than defensive: `rest` is the channel only
264
+ // on the chat plane. On `inst` it is the RECIPIENT (`subjects.ts:599`) and on `svc` the route
265
+ // (`:603`), so forwarding it ungated would label a DM's recipient as a channel.
266
+ const channel = parsed?.kind === "chat" ? parsed.rest : undefined;
267
+ const agent = channel === undefined ? null : eventChannelPrincipal(channel);
268
+ if (agent !== null) {
269
+ const id = principalKey(agent.owner, agent.actor).key;
270
+ if (senderId !== id)
271
+ return null;
272
+ return { event: "agent-frame", agent: id, data: { id, msg } };
273
+ }
274
+ return { event: "message", data: { mode, senderId, channel, msg } };
275
+ }
276
+ //# sourceMappingURL=web-request.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web-request.js","sourceRoot":"","sources":["../src/web-request.ts"],"names":[],"mappings":"AAAA,kGAAkG;AAClG,4GAA4G;AAC5G,OAAO,EACL,UAAU,EACV,qBAAqB,EACrB,iBAAiB,EACjB,YAAY,EACZ,YAAY,EAEZ,kBAAkB,GAEnB,MAAM,gBAAgB,CAAC;AAExB;;0BAE0B;AAC1B,MAAM,OAAO,UAAW,SAAQ,KAAK;CAAG;AAExC;;;6FAG6F;AAC7F,MAAM,OAAO,eAAgB,SAAQ,KAAK;CAAG;AAE7C,0EAA0E;AAC1E,MAAM,OAAO,oBAAqB,SAAQ,KAAK;CAAG;AAElD;;iCAEiC;AACjC,MAAM,OAAO,aAAc,SAAQ,KAAK;CAAG;AAE3C;;;;;;;;;;;;;4FAa4F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B,iGAAiG;IACjG,sDAAsD;IACtD,MAAM,QAAQ,GAAG,MAAM,EAAE,MAAM,CAAC;IAChC,6FAA6F;IAC7F,8FAA8F;IAC9F,iFAAiF;IACjF,EAAE;IACF,gGAAgG;IAChG,8FAA8F;IAC9F,gFAAgF;IAChF,MAAM,OAAO,GAAG,MAAM,EAAE,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC;IAClE,MAAM,KAAK,GAAG,OAAO,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,qBAAqB,CAAC,OAAO,CAAC,CAAC;IAC5E,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;QACnB,MAAM,EAAE,GAAG,YAAY,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC;QACtD,IAAI,QAAQ,KAAK,EAAE;YAAE,OAAO,IAAI,CAAC;QACjC,OAAO,EAAE,KAAK,EAAE,aAAa,EAAE,KAAK,EAAE,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,CAAC;IAChE,CAAC;IACD,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,GAAG,EAAE,EAAE,CAAC;AACtE,CAAC"}
package/dist/web.d.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import { type ChildProcess } from "node:child_process";
2
2
  import { type IncomingMessage } from "node:http";
3
- import { CotalEndpoint, type DeliveryMode, type CotalMessage, type ParsedArgs, type SpaceAuth } from "@cotal-ai/core";
3
+ import { CotalEndpoint, type CotalMessage, type ParsedArgs, type SpaceAuth } from "@cotal-ai/core";
4
4
  import { type Connection, type LocalProcess, type WebSession } from "@cotal-ai/workspace";
5
+ export { BadRequest, BrokerRefused, quoteForOperator, MAX_HISTORY_LIMIT, agentIdFromPath, classifyTap, } from "./web-request.js";
5
6
  /** The dashboard's default port and its branded address. The server binds loopback
6
7
  * (127.0.0.1) but serves any Host, so `cotal.localhost` — which Chrome/Firefox/Edge
7
8
  * resolve to loopback with no DNS setup — just works. (Safari may not resolve
@@ -175,134 +176,6 @@ export declare const AGGREGATION_DEADLINE_MS = 8000;
175
176
  * workers pull from) rather than on a source count, so a third or a tenth source is pooled at this
176
177
  * width instead of fanned out, the worst column above. */
177
178
  export declare const AGGREGATION_CONCURRENCY = 8;
178
- /** A request the CALLER got wrong, so the frame answers 400 rather than the 500 it gives a server
179
- * fault. Without this every malformed query reads, in the log and in the body, exactly like the
180
- * dashboard breaking. */
181
- export declare class BadRequest extends Error {
182
- }
183
- /** A body the caller sent that this server declines to READ, which is a different fact from a body
184
- * it read and disliked, and carries its own status. Deliberately NOT a `BadRequest`: 400 says
185
- * "what you sent is wrong", 413 says "I stopped before finding out", and a caller that retries on
186
- * 400 by fixing its payload would loop forever on a size it never learns is the problem. */
187
- export declare class PayloadTooLarge extends Error {
188
- }
189
- /** A post the broker, or the operator's connection to it, refused. Neither the caller's fault nor
190
- * this server's, so the frame answers 502 with the broker's own message, escaped by
191
- * {@link escapeForOperator}. */
192
- export declare class BrokerRefused extends Error {
193
- }
194
- /** QUOTE A CALLER'S OWN VALUE SO AN OPERATOR CAN READ IT.
195
- *
196
- * `JSON.stringify` was doing two jobs at every site below and only claims one of them. It builds
197
- * valid JSON, and on the way it escapes every C0 control, so `ESC` arrives as the six characters
198
- * `\u001b` and a newline as `\n`. It is not a renderer for humans and never said it was: DEL, the
199
- * C1 range, `U+2028`/`U+2029`, the bidi controls and the zero-width characters are all valid JSON
200
- * string content and pass through untouched.
201
- *
202
- * MEASURED against the shipped `web()` entry before this existed, driving `/api/activity?limit=`
203
- * with each codepoint percent-encoded and reading the answer as BYTES rather than through a JSON
204
- * parse (a parse decodes the very thing under test and hands the input back whatever the server
205
- * wrote). Six of eight arrived raw in BOTH the 400 body and the operator's stderr line: DEL,
206
- * `U+0085`, `U+009B`, `U+202E`, `U+2028`, `U+2029`. `ESC` and `LF` came back escaped, which is
207
- * what makes the other six a finding rather than a property of the harness.
208
- *
209
- * The escape is emitted as `\uXXXX`, so the message stays valid JSON on the body path and reads as
210
- * the codepoint it is on the terminal path. Applied where the value is QUOTED rather than where it
211
- * is written out, because the untrusted thing is the value and the message is derived from it: a
212
- * guard at the two exits fences those two exits, while a guard here travels with the sentence.
213
- *
214
- * The loop below is per UTF-16 UNIT, not per codepoint: `u` hands the callback a whole codepoint,
215
- * so an astral one (a tag character, a musical control) arrives as its surrogate pair and has to
216
- * leave as two escapes. `\u1d173` is not a JSON escape, and a body carrying it would stop parsing
217
- * for the caller who asked what was wrong with their request. */
218
- export declare function quoteForOperator(value: string): string;
219
- /** Escapes a message someone else wrote, keeping its wording. A broker refusal names the subject it
220
- * refused, and a DM subject carries a recipient id taken from another agent's presence card, so the
221
- * message reaches the 502 body and the operator's line as untrusted text. Each codepoint the quoter
222
- * escapes leaves as its escape; the rest, quotes included, stays as the broker wrote it. */
223
- export declare function escapeForOperator(text: string): string;
224
- /** The channel name out of the path. A percent escape the decoder cannot read is the caller having
225
- * typed a bad one, so it is refused as a bad request like any other malformed input. Left as a
226
- * bare `URIError` it reached the request frame unrecognised and was reported as a server fault,
227
- * which is the one thing the 400/500 split exists to prevent. */
228
- export declare function channelNameFromPath(raw: string): string;
229
- /** The most messages one read may ask for. A route reads up to `limit` messages and holds them all
230
- * before it answers, so a limit with no ceiling lets one request make the server read a stream's
231
- * whole retained history. The dashboard asks for at most 500. */
232
- export declare const MAX_HISTORY_LIMIT = 2000;
233
- /** The `limit` out of the query. The ceiling refusal is reachable only through the digits-only test
234
- * above it, so its value cannot carry anything the quoter would escape today. It quotes anyway:
235
- * the guarantee that a refusal renders its input unambiguously should hold because the quoting site
236
- * holds it, not because a regex two lines up stays exactly as narrow as it is this morning.
237
- *
238
- * THE LIMIT, PARSED ONCE, because three routes each re-deriving
239
- * `query.get("limit") ? Number(...) : N` is how they came to disagree about the same parameter.
240
- *
241
- * MEASURED ON THE SHIPPED ROUTES, against a real broker, before this existed:
242
- * ?limit=abc `Number("abc")` is NaN and every comparison against NaN is false, so core's
243
- * `limit <= 0` guard does not fire and the widening search's two exits can
244
- * never be true. No answer after 30s, and the ABANDONED request kept consuming
245
- * half a core with its caller long gone, invisible because the process keeps
246
- * serving everything else.
247
- * ?limit=Infinity passes the same guard, and `slice(-Infinity)` is the whole array: a channel's
248
- * entire retained history from a one word request. `1e999` is the same value.
249
- * ?limit=2.5 silently truncated to 2.
250
- * ?limit=" 5" accepted as 5, because `Number()` trims whitespace.
251
- *
252
- * So the accepted form is the narrow one: a plain run of digits no larger than
253
- * {@link MAX_HISTORY_LIMIT}. `0` keeps
254
- * meaning zero, which is what it already did and what a caller expects; an absent or empty
255
- * parameter keeps meaning the route's own default, the one shape the old parse got right.
256
- *
257
- * Everything else is REFUSED rather than clamped. A clamp would answer a request nobody made, and
258
- * the caller who wrote `limit=2.5` would never learn that the page they read was not the page they
259
- * asked for. */
260
- export declare function historyLimit(query: URLSearchParams, fallback: number): number;
261
- /** An agent id out of a path segment: decoded, and a principal key, or a BadRequest naming it.
262
- *
263
- * The id is the roster's `card.id`, which is `principalKey(owner, actor).key` by construction, so
264
- * the agent is named by its principal and never by a display name: two live agents with one name
265
- * never share a session. The answer's `id` is rebuilt from the parsed tokens rather than passed
266
- * through, so the id a route acts on is the one the event channel is formed from. */
267
- export declare function agentIdFromPath(raw: string): {
268
- id: string;
269
- owner: string;
270
- actor: string;
271
- };
272
- /** What the live tap does with one message. A chat message on a principal-shaped event channel is an
273
- * agent frame for `principalKey(owner, actor).key`; everything else goes out as today's `message`.
274
- *
275
- * WHY FRAMES LEAVE THE BROADCAST. The observer taps `chat.>`, and an agent's event channel is a
276
- * chat channel, so this one subscription receives every agent's frames. A NATS wildcard cannot
277
- * exclude a sub-tree, so the observer still receives them; what changes is who it forwards them to.
278
- * Sent as `message` to every browser, each frame cost every open dashboard a write whether or not
279
- * anyone was reading that agent. As `agent-frame` it reaches only the feeds watching that agent.
280
- *
281
- * The agent is the event channel's principal, the same derivation the chat-only filter reads, so a
282
- * near-miss name such as `events.standup` stays an ordinary `message`. The channel is the one the
283
- * broker policed, taken from the subject, never the payload's own claim.
284
- *
285
- * A FRAME FROM ANYONE BUT THE AGENT IS DROPPED, and dropped rather than sent on as a `message`. The
286
- * subject's sender is the verified publisher, and a frame is that agent's only when the agent
287
- * published it. On a mesh where another principal may write the channel (an open mesh, or a grant
288
- * wider than the agent's own), a forged frame would otherwise read as the agent's session, and
289
- * falling through to `message` would hand it to every browser instead. */
290
- export declare function classifyTap(subject: string, msg: CotalMessage | undefined): {
291
- event: "message";
292
- data: {
293
- mode: DeliveryMode;
294
- senderId?: string;
295
- channel?: string;
296
- msg: CotalMessage;
297
- };
298
- } | {
299
- event: "agent-frame";
300
- agent: string;
301
- data: {
302
- id: string;
303
- msg: CotalMessage;
304
- };
305
- } | null;
306
179
  /** What a feed write is. A `live` event is traffic the feed may fall behind on, so it is held to the
307
180
  * ceiling. A `snapshot` is the state a feed is opened with (its hello, the roster, the presence view
308
181
  * and the membership seed), written once on connect: a mesh whose roster alone is larger than the
@@ -584,5 +457,4 @@ export declare function webUrl(host: string, port: number): string;
584
457
  /** Best-effort open of the dashboard in the default browser. The URL is already
585
458
  * printed, so a failure here is harmless — never block startup on it. `--no-input` opens nothing. */
586
459
  export declare function openBrowser(url: string): void;
587
- export {};
588
460
  //# sourceMappingURL=web.d.ts.map