toolroll 0.9.128 → 0.9.130
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.
- package/CHANGELOG.md +41 -0
- package/dist/accent-colors.js +3 -1
- package/dist/agent-fence.js +0 -2
- package/dist/agents-board.js +0 -3
- package/dist/approval-authority.js +2 -1
- package/dist/assignment.js +56 -2
- package/dist/backup-ui.js +6 -3
- package/dist/backup.js +6 -0
- package/dist/bounded-spawn.js +0 -1
- package/dist/browser/manifest.json +57 -0
- package/dist/browser/terminal.2a682d0b91a35288271c73b7ee253e524cca0917d17fbaf6733e80b7a767a37c.js.br +0 -0
- package/dist/browser/terminal.2a682d0b91a35288271c73b7ee253e524cca0917d17fbaf6733e80b7a767a37c.js.gz +0 -0
- package/dist/browser/{terminal.css → terminal.4d1cf20c32ad7c2099df657358d540d5fd92bb55195a88121e74b28467238b42.css} +1 -1
- package/dist/browser/terminal.4d1cf20c32ad7c2099df657358d540d5fd92bb55195a88121e74b28467238b42.css.br +0 -0
- package/dist/browser/terminal.4d1cf20c32ad7c2099df657358d540d5fd92bb55195a88121e74b28467238b42.css.gz +0 -0
- package/dist/browser/{workspace.js → workspace.9bd1db81d3c897045a5d82d4804e374e41e78b1b2cfd05d37b982a4ea0d02626.js} +25 -25
- package/dist/browser/workspace.9bd1db81d3c897045a5d82d4804e374e41e78b1b2cfd05d37b982a4ea0d02626.js.br +0 -0
- package/dist/browser/workspace.9bd1db81d3c897045a5d82d4804e374e41e78b1b2cfd05d37b982a4ea0d02626.js.gz +0 -0
- package/dist/browser/{workspace.css → workspace.a0752f935cfeaf16758d1085fe8f3e1719a7cd525b91a9c9943c789d65879a54.css} +1 -1
- package/dist/browser/workspace.a0752f935cfeaf16758d1085fe8f3e1719a7cd525b91a9c9943c789d65879a54.css.br +0 -0
- package/dist/browser/workspace.a0752f935cfeaf16758d1085fe8f3e1719a7cd525b91a9c9943c789d65879a54.css.gz +0 -0
- package/dist/browser-shell.js +271 -25
- package/dist/canvas-freshness.js +122 -0
- package/dist/canvas-fs.js +273 -0
- package/dist/canvas-reads.js +747 -0
- package/dist/canvas-state-key.js +45 -0
- package/dist/canvas-state-worker.js +12 -0
- package/dist/central-recovery-stage.js +3 -3
- package/dist/chat-actions.js +26 -0
- package/dist/chat-channel.js +4 -3
- package/dist/chat-task-actions.js +1 -1
- package/dist/chat-warmth.js +0 -1
- package/dist/check-report.js +0 -6
- package/dist/commit-extract.js +79 -40
- package/dist/companion/access.js +162 -0
- package/dist/companion/actions.js +622 -0
- package/dist/companion/bonjour.js +45 -0
- package/dist/companion/canvas.js +141 -0
- package/dist/companion/chat-origin.js +92 -0
- package/dist/companion/chat.js +571 -0
- package/dist/companion/daemon.js +1387 -0
- package/dist/companion/direct-server.js +177 -0
- package/dist/companion/finish.js +51 -0
- package/dist/companion/framing.js +498 -0
- package/dist/companion/noise.js +461 -0
- package/dist/companion/passkey-gate.js +287 -0
- package/dist/companion/paths.js +90 -0
- package/dist/companion/reads.js +283 -0
- package/dist/companion/relay-auth.js +265 -0
- package/dist/companion/relay-server.js +870 -0
- package/dist/companion/schema.js +289 -0
- package/dist/companion/service.js +581 -0
- package/dist/companion/store.js +325 -0
- package/dist/companion/webauthn.js +407 -0
- package/dist/console-instance.js +0 -4
- package/dist/contracts/console-api.js +18 -0
- package/dist/converse.js +0 -1
- package/dist/coordinator.js +1 -1
- package/dist/design/kit.css +30 -0
- package/dist/desktop-config.js +156 -0
- package/dist/desktop-host.js +6 -152
- package/dist/desktop-update.js +1 -1
- package/dist/finished-task-sql.js +14 -0
- package/dist/flexible-answers.js +0 -1
- package/dist/github-read.js +41 -0
- package/dist/guides.js +4 -3
- package/dist/lead-doors.js +50 -3
- package/dist/lead-queue-runtime.js +11 -2
- package/dist/lead-queue.js +25 -0
- package/dist/live-backup-change.js +93 -0
- package/dist/live-backup.js +75 -31
- package/dist/live-bus.js +3 -2
- package/dist/live-records.js +0 -1
- package/dist/local-landing.js +47 -0
- package/dist/local-owner.js +0 -1
- package/dist/model-choices.js +0 -11
- package/dist/nightly-check.js +0 -3
- package/dist/nightly-github.js +1 -2
- package/dist/notification-retention.js +119 -0
- package/dist/observations.js +0 -1
- package/dist/operate.js +17 -4
- package/dist/person-proof.js +5 -1
- package/dist/pull-request-flow.js +116 -2
- package/dist/qr.js +360 -0
- package/dist/release-adapters.js +14 -43
- package/dist/release-auto.js +17 -13
- package/dist/release-card.js +0 -4
- package/dist/release-cli.js +37 -2
- package/dist/release.js +13 -2
- package/dist/restore-retirement.js +6 -1
- package/dist/result-review.js +0 -3
- package/dist/retention-ui.js +1 -1
- package/dist/retention.js +14 -21
- package/dist/review-context.js +0 -5
- package/dist/route-rules.js +1 -5
- package/dist/sandbox-runtime.js +0 -1
- package/dist/scope.js +68 -17
- package/dist/scout.js +0 -6
- package/dist/serve.js +60 -9
- package/dist/server/kit.js +1 -0
- package/dist/server/live.js +9 -2
- package/dist/server/pages.js +12 -4
- package/dist/server/phone-engine-api.js +79 -0
- package/dist/server/render-pages.js +1 -11
- package/dist/server/render-results.js +0 -1
- package/dist/server/render-tasks.js +0 -68
- package/dist/server/route-table.js +12 -1
- package/dist/server/settings-phone.js +515 -0
- package/dist/server/settings-scripts.js +2 -0
- package/dist/server/settings-section-table.js +0 -3
- package/dist/server/settings-sections.js +10 -4
- package/dist/server/settings.js +20 -5
- package/dist/server/terminals.js +2 -1
- package/dist/settings-groups-ui.js +2 -1
- package/dist/settings-proofs.js +6 -0
- package/dist/store.js +52 -4
- package/dist/structured-output.js +0 -1
- package/dist/style-asset.js +20 -7
- package/dist/tailnet.js +39 -0
- package/dist/task-control.js +5 -0
- package/dist/task-sizing.js +0 -1
- package/dist/toolroll-update-cli.js +19 -3
- package/dist/toolroll-update-ui.js +2 -2
- package/dist/toolroll-update.js +98 -16
- package/dist/youiyoux-intake.js +8 -0
- package/package.json +7 -2
- package/dist/contracts/contract-test.js +0 -55
- package/dist/unfenced-test-support.js +0 -6
- /package/dist/browser/{terminal.js → terminal.2a682d0b91a35288271c73b7ee253e524cca0917d17fbaf6733e80b7a767a37c.js} +0 -0
|
@@ -0,0 +1,498 @@
|
|
|
1
|
+
import { MAX_NOISE_PLAINTEXT_BYTES } from "./noise.js";
|
|
2
|
+
export const FRAME_HEADER_BYTES = 13;
|
|
3
|
+
export const MAX_FRAME_PAYLOAD_BYTES = 60_000;
|
|
4
|
+
export const MAX_LOGICAL_REQUEST_BYTES = 256 * 1024;
|
|
5
|
+
export const MAX_REQUEST_CHUNKS = 5;
|
|
6
|
+
export const MAX_LOGICAL_UNARY_RESPONSE_BYTES = 1024 * 1024;
|
|
7
|
+
export const INITIAL_STREAM_CREDIT = 16;
|
|
8
|
+
export const MAX_CONCURRENT_STREAMS = 8;
|
|
9
|
+
export const MAX_CONCURRENT_WATCH_STREAMS = 2;
|
|
10
|
+
export const MAX_LIVE_WATCH_RECORDS = 32;
|
|
11
|
+
export const MAX_LIVE_WATCH_BYTES = 256 * 1024;
|
|
12
|
+
export const MAX_SESSION_BUFFERED_BYTES = 1024 * 1024;
|
|
13
|
+
export const MAX_DAEMON_BUFFERED_BYTES = 64 * 1024 * 1024;
|
|
14
|
+
export const REQUEST_ASSEMBLY_DEADLINE_MS = 5_000;
|
|
15
|
+
export const UNARY_RESPONSE_DEADLINE_MS = 30_000;
|
|
16
|
+
export const WATCH_IDLE_DEADLINE_MS = 60_000;
|
|
17
|
+
export const FrameType = {
|
|
18
|
+
REQ: 0x01,
|
|
19
|
+
RES: 0x02,
|
|
20
|
+
CHUNK: 0x03,
|
|
21
|
+
CREDIT: 0x04,
|
|
22
|
+
CANCEL: 0x05,
|
|
23
|
+
PING: 0x06,
|
|
24
|
+
PONG: 0x07,
|
|
25
|
+
END: 0x08,
|
|
26
|
+
ERROR: 0x09,
|
|
27
|
+
REQ_CHUNK: 0x0a,
|
|
28
|
+
};
|
|
29
|
+
const VALID_FRAME_TYPES = new Set(Object.values(FrameType));
|
|
30
|
+
export function encodeFrame(frame) {
|
|
31
|
+
if (!VALID_FRAME_TYPES.has(frame.type)) {
|
|
32
|
+
throw new Error(`Unknown frame type: 0x${frame.type.toString(16)}`);
|
|
33
|
+
}
|
|
34
|
+
if (!Number.isInteger(frame.streamId) || frame.streamId < 0 || frame.streamId > 0xffff_ffff) {
|
|
35
|
+
throw new Error(`Invalid streamId: ${frame.streamId}`);
|
|
36
|
+
}
|
|
37
|
+
if (!Number.isInteger(frame.seq) || frame.seq < 0 || frame.seq > 0xffff_ffff) {
|
|
38
|
+
throw new Error(`Invalid seq: ${frame.seq}`);
|
|
39
|
+
}
|
|
40
|
+
if (frame.payload.length > MAX_FRAME_PAYLOAD_BYTES) {
|
|
41
|
+
throw new Error(`Frame payload ${frame.payload.length} exceeds cap ${MAX_FRAME_PAYLOAD_BYTES}`);
|
|
42
|
+
}
|
|
43
|
+
const total = FRAME_HEADER_BYTES + frame.payload.length;
|
|
44
|
+
if (total > MAX_NOISE_PLAINTEXT_BYTES) {
|
|
45
|
+
throw new Error(`Encoded frame ${total} exceeds Noise plaintext cap ${MAX_NOISE_PLAINTEXT_BYTES}`);
|
|
46
|
+
}
|
|
47
|
+
const out = Buffer.allocUnsafe(total);
|
|
48
|
+
out.writeUInt8(frame.type, 0);
|
|
49
|
+
out.writeUInt32BE(frame.streamId, 1);
|
|
50
|
+
out.writeUInt32BE(frame.seq, 5);
|
|
51
|
+
out.writeUInt32BE(frame.payload.length, 9);
|
|
52
|
+
frame.payload.copy(out, FRAME_HEADER_BYTES);
|
|
53
|
+
return out;
|
|
54
|
+
}
|
|
55
|
+
export function decodeFrame(raw) {
|
|
56
|
+
if (raw.length < FRAME_HEADER_BYTES) {
|
|
57
|
+
throw new Error(`Frame truncated: ${raw.length} bytes`);
|
|
58
|
+
}
|
|
59
|
+
if (raw.length > MAX_NOISE_PLAINTEXT_BYTES) {
|
|
60
|
+
throw new Error(`Frame exceeds Noise plaintext cap: ${raw.length} bytes`);
|
|
61
|
+
}
|
|
62
|
+
const type = raw.readUInt8(0);
|
|
63
|
+
if (!VALID_FRAME_TYPES.has(type)) {
|
|
64
|
+
throw new Error(`Unknown frame type: 0x${type.toString(16)}`);
|
|
65
|
+
}
|
|
66
|
+
const streamId = raw.readUInt32BE(1);
|
|
67
|
+
const seq = raw.readUInt32BE(5);
|
|
68
|
+
const payloadLen = raw.readUInt32BE(9);
|
|
69
|
+
if (payloadLen > MAX_FRAME_PAYLOAD_BYTES) {
|
|
70
|
+
throw new Error(`Frame payloadLen ${payloadLen} exceeds cap ${MAX_FRAME_PAYLOAD_BYTES}`);
|
|
71
|
+
}
|
|
72
|
+
if (raw.length !== FRAME_HEADER_BYTES + payloadLen) {
|
|
73
|
+
throw new Error(`Frame length mismatch: header declares ${payloadLen}, got ${raw.length - FRAME_HEADER_BYTES}`);
|
|
74
|
+
}
|
|
75
|
+
return {
|
|
76
|
+
type: type,
|
|
77
|
+
streamId,
|
|
78
|
+
seq,
|
|
79
|
+
payload: Buffer.from(raw.subarray(FRAME_HEADER_BYTES)),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
export function splitRequestFrames(streamId, payload) {
|
|
83
|
+
if (payload.length > MAX_LOGICAL_REQUEST_BYTES) {
|
|
84
|
+
throw new Error(`Logical request ${payload.length} exceeds cap ${MAX_LOGICAL_REQUEST_BYTES}`);
|
|
85
|
+
}
|
|
86
|
+
if (payload.length <= MAX_FRAME_PAYLOAD_BYTES) {
|
|
87
|
+
return [{ type: FrameType.REQ, streamId, seq: 0, payload }];
|
|
88
|
+
}
|
|
89
|
+
const frames = [];
|
|
90
|
+
let seq = 0;
|
|
91
|
+
for (let offset = 0; offset < payload.length; offset += MAX_FRAME_PAYLOAD_BYTES) {
|
|
92
|
+
frames.push({
|
|
93
|
+
type: FrameType.REQ_CHUNK,
|
|
94
|
+
streamId,
|
|
95
|
+
seq,
|
|
96
|
+
payload: payload.subarray(offset, Math.min(offset + MAX_FRAME_PAYLOAD_BYTES, payload.length)),
|
|
97
|
+
});
|
|
98
|
+
seq += 1;
|
|
99
|
+
}
|
|
100
|
+
if (frames.length > MAX_REQUEST_CHUNKS) {
|
|
101
|
+
throw new Error(`Request chunk count ${frames.length} exceeds cap ${MAX_REQUEST_CHUNKS}`);
|
|
102
|
+
}
|
|
103
|
+
frames.push({ type: FrameType.END, streamId, seq, payload: Buffer.alloc(0) });
|
|
104
|
+
return frames;
|
|
105
|
+
}
|
|
106
|
+
export function splitUnaryResponseFrames(streamId, payload) {
|
|
107
|
+
if (payload.length > MAX_LOGICAL_UNARY_RESPONSE_BYTES) {
|
|
108
|
+
throw new Error(`Logical unary response ${payload.length} exceeds cap ${MAX_LOGICAL_UNARY_RESPONSE_BYTES}`);
|
|
109
|
+
}
|
|
110
|
+
if (payload.length <= MAX_FRAME_PAYLOAD_BYTES) {
|
|
111
|
+
return [{ type: FrameType.RES, streamId, seq: 0, payload }];
|
|
112
|
+
}
|
|
113
|
+
const frames = [];
|
|
114
|
+
let seq = 0;
|
|
115
|
+
for (let offset = 0; offset < payload.length; offset += MAX_FRAME_PAYLOAD_BYTES) {
|
|
116
|
+
frames.push({
|
|
117
|
+
type: FrameType.CHUNK,
|
|
118
|
+
streamId,
|
|
119
|
+
seq,
|
|
120
|
+
payload: payload.subarray(offset, Math.min(offset + MAX_FRAME_PAYLOAD_BYTES, payload.length)),
|
|
121
|
+
});
|
|
122
|
+
seq += 1;
|
|
123
|
+
}
|
|
124
|
+
frames.push({ type: FrameType.END, streamId, seq, payload: Buffer.alloc(0) });
|
|
125
|
+
return frames;
|
|
126
|
+
}
|
|
127
|
+
export function encodeCreditPayload(grant) {
|
|
128
|
+
if (!Number.isInteger(grant) || grant < 1 || grant > 1024) {
|
|
129
|
+
throw new Error(`Invalid credit grant: ${grant}`);
|
|
130
|
+
}
|
|
131
|
+
const buf = Buffer.allocUnsafe(4);
|
|
132
|
+
buf.writeUInt32BE(grant, 0);
|
|
133
|
+
return buf;
|
|
134
|
+
}
|
|
135
|
+
export function decodeCreditPayload(payload) {
|
|
136
|
+
if (payload.length !== 4)
|
|
137
|
+
throw new Error(`Invalid CREDIT payload length: ${payload.length}`);
|
|
138
|
+
const grant = payload.readUInt32BE(0);
|
|
139
|
+
if (grant < 1 || grant > 1024)
|
|
140
|
+
throw new Error(`Invalid CREDIT grant: ${grant}`);
|
|
141
|
+
return grant;
|
|
142
|
+
}
|
|
143
|
+
export function encodeErrorPayload(code, message) {
|
|
144
|
+
return Buffer.from(JSON.stringify({ code, message }), "utf8");
|
|
145
|
+
}
|
|
146
|
+
export class GlobalBufferBudget {
|
|
147
|
+
usedBytes = 0;
|
|
148
|
+
limitBytes;
|
|
149
|
+
constructor(limitBytes = MAX_DAEMON_BUFFERED_BYTES) {
|
|
150
|
+
this.limitBytes = limitBytes;
|
|
151
|
+
}
|
|
152
|
+
get used() {
|
|
153
|
+
return this.usedBytes;
|
|
154
|
+
}
|
|
155
|
+
tryReserve(bytes) {
|
|
156
|
+
if (bytes < 0)
|
|
157
|
+
return false;
|
|
158
|
+
if (this.usedBytes + bytes > this.limitBytes)
|
|
159
|
+
return false;
|
|
160
|
+
this.usedBytes += bytes;
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
release(bytes) {
|
|
164
|
+
this.usedBytes = Math.max(0, this.usedBytes - bytes);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
export class CompanionStreamGuard {
|
|
168
|
+
highestClientStreamId = 0;
|
|
169
|
+
openUnaryStreams = new Set();
|
|
170
|
+
openWatchStreams = new Set();
|
|
171
|
+
assemblies = new Map();
|
|
172
|
+
responseBytes = new Map();
|
|
173
|
+
sessionBufferedBytes = 0;
|
|
174
|
+
globalBudget;
|
|
175
|
+
constructor(globalBudget = new GlobalBufferBudget()) {
|
|
176
|
+
this.globalBudget = globalBudget;
|
|
177
|
+
}
|
|
178
|
+
isOpen(streamId) {
|
|
179
|
+
return this.openUnaryStreams.has(streamId) || this.openWatchStreams.has(streamId);
|
|
180
|
+
}
|
|
181
|
+
get openStreamCount() {
|
|
182
|
+
return this.openUnaryStreams.size + this.openWatchStreams.size;
|
|
183
|
+
}
|
|
184
|
+
get watchStreamCount() {
|
|
185
|
+
return this.openWatchStreams.size;
|
|
186
|
+
}
|
|
187
|
+
get bufferedBytes() {
|
|
188
|
+
return this.sessionBufferedBytes;
|
|
189
|
+
}
|
|
190
|
+
reserveBytes(bytes) {
|
|
191
|
+
if (this.sessionBufferedBytes + bytes > MAX_SESSION_BUFFERED_BYTES)
|
|
192
|
+
return false;
|
|
193
|
+
if (!this.globalBudget.tryReserve(bytes))
|
|
194
|
+
return false;
|
|
195
|
+
this.sessionBufferedBytes += bytes;
|
|
196
|
+
return true;
|
|
197
|
+
}
|
|
198
|
+
releaseBytes(bytes) {
|
|
199
|
+
if (bytes <= 0)
|
|
200
|
+
return;
|
|
201
|
+
this.sessionBufferedBytes = Math.max(0, this.sessionBufferedBytes - bytes);
|
|
202
|
+
this.globalBudget.release(bytes);
|
|
203
|
+
}
|
|
204
|
+
admitNewStream(streamId) {
|
|
205
|
+
if (streamId === 0 || streamId % 2 !== 1 || streamId <= this.highestClientStreamId) {
|
|
206
|
+
return {
|
|
207
|
+
ok: false,
|
|
208
|
+
code: "protocol_error",
|
|
209
|
+
message: `Stream ID ${streamId} violates monotonic odd non-reuse rule (highest=${this.highestClientStreamId})`,
|
|
210
|
+
closeSession: true,
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
if (this.openStreamCount >= MAX_CONCURRENT_STREAMS) {
|
|
214
|
+
return {
|
|
215
|
+
ok: false,
|
|
216
|
+
code: "too_many_streams",
|
|
217
|
+
message: `Exceeded max ${MAX_CONCURRENT_STREAMS} concurrent streams`,
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
this.highestClientStreamId = streamId;
|
|
221
|
+
this.openUnaryStreams.add(streamId);
|
|
222
|
+
return { ok: true };
|
|
223
|
+
}
|
|
224
|
+
promoteToWatchStream(streamId) {
|
|
225
|
+
if (!this.openUnaryStreams.has(streamId)) {
|
|
226
|
+
return { ok: false, code: "protocol_error", message: `Stream ${streamId} is not open` };
|
|
227
|
+
}
|
|
228
|
+
if (this.openWatchStreams.size >= MAX_CONCURRENT_WATCH_STREAMS) {
|
|
229
|
+
this.closeStream(streamId);
|
|
230
|
+
return {
|
|
231
|
+
ok: false,
|
|
232
|
+
code: "too_many_streams",
|
|
233
|
+
message: `Exceeded max ${MAX_CONCURRENT_WATCH_STREAMS} concurrent live watch streams`,
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
this.openUnaryStreams.delete(streamId);
|
|
237
|
+
this.openWatchStreams.add(streamId);
|
|
238
|
+
return { ok: true };
|
|
239
|
+
}
|
|
240
|
+
reserveResponse(streamId, bytes) {
|
|
241
|
+
if (!this.reserveBytes(bytes))
|
|
242
|
+
return false;
|
|
243
|
+
this.responseBytes.set(streamId, (this.responseBytes.get(streamId) ?? 0) + bytes);
|
|
244
|
+
return true;
|
|
245
|
+
}
|
|
246
|
+
reserveWatch(bytes) {
|
|
247
|
+
return this.reserveBytes(bytes);
|
|
248
|
+
}
|
|
249
|
+
releaseWatch(bytes) {
|
|
250
|
+
this.releaseBytes(bytes);
|
|
251
|
+
}
|
|
252
|
+
expireAssemblies(nowMs = Date.now()) {
|
|
253
|
+
const expired = [];
|
|
254
|
+
for (const [streamId, asm] of this.assemblies) {
|
|
255
|
+
if (nowMs - asm.startedAtMs > REQUEST_ASSEMBLY_DEADLINE_MS)
|
|
256
|
+
expired.push(streamId);
|
|
257
|
+
}
|
|
258
|
+
for (const streamId of expired)
|
|
259
|
+
this.closeStream(streamId);
|
|
260
|
+
return expired;
|
|
261
|
+
}
|
|
262
|
+
closeStream(streamId) {
|
|
263
|
+
const asm = this.assemblies.get(streamId);
|
|
264
|
+
if (asm !== undefined) {
|
|
265
|
+
this.releaseBytes(asm.totalBytes);
|
|
266
|
+
this.assemblies.delete(streamId);
|
|
267
|
+
}
|
|
268
|
+
const held = this.responseBytes.get(streamId);
|
|
269
|
+
if (held !== undefined) {
|
|
270
|
+
this.releaseBytes(held);
|
|
271
|
+
this.responseBytes.delete(streamId);
|
|
272
|
+
}
|
|
273
|
+
this.openUnaryStreams.delete(streamId);
|
|
274
|
+
this.openWatchStreams.delete(streamId);
|
|
275
|
+
}
|
|
276
|
+
releaseAll() {
|
|
277
|
+
for (const asm of this.assemblies.values()) {
|
|
278
|
+
this.releaseBytes(asm.totalBytes);
|
|
279
|
+
}
|
|
280
|
+
this.assemblies.clear();
|
|
281
|
+
for (const held of this.responseBytes.values())
|
|
282
|
+
this.releaseBytes(held);
|
|
283
|
+
this.responseBytes.clear();
|
|
284
|
+
this.openUnaryStreams.clear();
|
|
285
|
+
this.openWatchStreams.clear();
|
|
286
|
+
}
|
|
287
|
+
ingestRequestFrame(frame, nowMs = Date.now()) {
|
|
288
|
+
if (frame.type === FrameType.REQ) {
|
|
289
|
+
if (frame.seq !== 0) {
|
|
290
|
+
return { ok: false, code: "protocol_error", message: "REQ frame must have seq=0", closeSession: true };
|
|
291
|
+
}
|
|
292
|
+
const admitted = this.admitNewStream(frame.streamId);
|
|
293
|
+
if (!admitted.ok)
|
|
294
|
+
return admitted;
|
|
295
|
+
return { ok: true, completePayload: frame.payload };
|
|
296
|
+
}
|
|
297
|
+
if (frame.type === FrameType.REQ_CHUNK) {
|
|
298
|
+
let asm = this.assemblies.get(frame.streamId);
|
|
299
|
+
if (asm === undefined) {
|
|
300
|
+
if (frame.seq !== 0) {
|
|
301
|
+
return { ok: false, code: "protocol_error", message: "First REQ_CHUNK must have seq=0", closeSession: true };
|
|
302
|
+
}
|
|
303
|
+
const admitted = this.admitNewStream(frame.streamId);
|
|
304
|
+
if (!admitted.ok)
|
|
305
|
+
return admitted;
|
|
306
|
+
asm = { chunks: [], totalBytes: 0, expectedSeq: 0, startedAtMs: nowMs };
|
|
307
|
+
this.assemblies.set(frame.streamId, asm);
|
|
308
|
+
}
|
|
309
|
+
if (nowMs - asm.startedAtMs > REQUEST_ASSEMBLY_DEADLINE_MS) {
|
|
310
|
+
this.closeStream(frame.streamId);
|
|
311
|
+
return { ok: false, code: "deadline_exceeded", message: "Multi-chunk request assembly exceeded 5s deadline" };
|
|
312
|
+
}
|
|
313
|
+
if (frame.seq !== asm.expectedSeq) {
|
|
314
|
+
this.closeStream(frame.streamId);
|
|
315
|
+
return { ok: false, code: "protocol_error", message: `Out-of-order REQ_CHUNK seq=${frame.seq}`, closeSession: true };
|
|
316
|
+
}
|
|
317
|
+
if (asm.chunks.length + 1 > MAX_REQUEST_CHUNKS || asm.totalBytes + frame.payload.length > MAX_LOGICAL_REQUEST_BYTES) {
|
|
318
|
+
this.closeStream(frame.streamId);
|
|
319
|
+
return { ok: false, code: "payload_too_large", message: "Multi-chunk request exceeded 256 KiB / 5 chunk cap" };
|
|
320
|
+
}
|
|
321
|
+
if (!this.reserveBytes(frame.payload.length)) {
|
|
322
|
+
this.closeStream(frame.streamId);
|
|
323
|
+
return { ok: false, code: "memory_limit", message: "Buffer memory limit reached" };
|
|
324
|
+
}
|
|
325
|
+
asm.chunks.push(frame.payload);
|
|
326
|
+
asm.totalBytes += frame.payload.length;
|
|
327
|
+
asm.expectedSeq += 1;
|
|
328
|
+
return { ok: true };
|
|
329
|
+
}
|
|
330
|
+
if (frame.type === FrameType.END) {
|
|
331
|
+
const asm = this.assemblies.get(frame.streamId);
|
|
332
|
+
if (asm === undefined) {
|
|
333
|
+
return { ok: false, code: "protocol_error", message: `Unexpected END for stream ${frame.streamId}` };
|
|
334
|
+
}
|
|
335
|
+
if (nowMs - asm.startedAtMs > REQUEST_ASSEMBLY_DEADLINE_MS) {
|
|
336
|
+
this.closeStream(frame.streamId);
|
|
337
|
+
return { ok: false, code: "deadline_exceeded", message: "Multi-chunk request assembly exceeded 5s deadline" };
|
|
338
|
+
}
|
|
339
|
+
if (frame.seq !== asm.expectedSeq) {
|
|
340
|
+
this.closeStream(frame.streamId);
|
|
341
|
+
return { ok: false, code: "protocol_error", message: `Out-of-order END seq=${frame.seq}`, closeSession: true };
|
|
342
|
+
}
|
|
343
|
+
const completePayload = Buffer.concat(asm.chunks, asm.totalBytes);
|
|
344
|
+
this.releaseBytes(asm.totalBytes);
|
|
345
|
+
this.assemblies.delete(frame.streamId);
|
|
346
|
+
return { ok: true, completePayload };
|
|
347
|
+
}
|
|
348
|
+
return { ok: false, code: "protocol_error", message: `Unexpected frame type 0x${frame.type.toString(16)} in request ingest` };
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
const UNCHARGED = { reserve: () => true, release: () => { } };
|
|
352
|
+
export class LiveWatchBuffer {
|
|
353
|
+
budget;
|
|
354
|
+
records = [];
|
|
355
|
+
usedBytes = 0;
|
|
356
|
+
droppedCount = 0;
|
|
357
|
+
credit = INITIAL_STREAM_CREDIT;
|
|
358
|
+
constructor(budget = UNCHARGED) {
|
|
359
|
+
this.budget = budget;
|
|
360
|
+
}
|
|
361
|
+
dropOldest() {
|
|
362
|
+
const oldest = this.records.shift();
|
|
363
|
+
this.usedBytes -= oldest.length;
|
|
364
|
+
this.budget.release(oldest.length);
|
|
365
|
+
this.droppedCount += 1;
|
|
366
|
+
}
|
|
367
|
+
push(record) {
|
|
368
|
+
if (record.length > MAX_FRAME_PAYLOAD_BYTES) {
|
|
369
|
+
this.droppedCount += 1;
|
|
370
|
+
return { accepted: false, cutCount: this.droppedCount };
|
|
371
|
+
}
|
|
372
|
+
while (this.records.length > 0
|
|
373
|
+
&& (this.records.length >= MAX_LIVE_WATCH_RECORDS || this.usedBytes + record.length > MAX_LIVE_WATCH_BYTES))
|
|
374
|
+
this.dropOldest();
|
|
375
|
+
while (!this.budget.reserve(record.length)) {
|
|
376
|
+
if (this.records.length === 0) {
|
|
377
|
+
this.droppedCount += 1;
|
|
378
|
+
return { accepted: false, cutCount: this.droppedCount };
|
|
379
|
+
}
|
|
380
|
+
this.dropOldest();
|
|
381
|
+
}
|
|
382
|
+
this.records.push(record);
|
|
383
|
+
this.usedBytes += record.length;
|
|
384
|
+
return { accepted: true, cutCount: this.droppedCount };
|
|
385
|
+
}
|
|
386
|
+
grantCredit(n) {
|
|
387
|
+
this.credit = Math.min(1024, this.credit + n);
|
|
388
|
+
}
|
|
389
|
+
drainAvailable(allow = () => true) {
|
|
390
|
+
const out = [];
|
|
391
|
+
if (this.droppedCount > 0 && this.credit > 0) {
|
|
392
|
+
const cut = Buffer.from(JSON.stringify({ k: "cut", dropped: this.droppedCount }), "utf8");
|
|
393
|
+
if (!allow(cut.length))
|
|
394
|
+
return out;
|
|
395
|
+
out.push(cut);
|
|
396
|
+
this.droppedCount = 0;
|
|
397
|
+
this.credit -= 1;
|
|
398
|
+
}
|
|
399
|
+
while (this.credit > 0 && this.records.length > 0) {
|
|
400
|
+
if (!allow(this.records[0].length))
|
|
401
|
+
break;
|
|
402
|
+
const next = this.records.shift();
|
|
403
|
+
this.usedBytes -= next.length;
|
|
404
|
+
this.budget.release(next.length);
|
|
405
|
+
this.credit -= 1;
|
|
406
|
+
out.push(next);
|
|
407
|
+
}
|
|
408
|
+
return out;
|
|
409
|
+
}
|
|
410
|
+
clear() {
|
|
411
|
+
this.budget.release(this.usedBytes);
|
|
412
|
+
this.records.length = 0;
|
|
413
|
+
this.usedBytes = 0;
|
|
414
|
+
this.droppedCount = 0;
|
|
415
|
+
}
|
|
416
|
+
get bufferedBytes() {
|
|
417
|
+
return this.usedBytes;
|
|
418
|
+
}
|
|
419
|
+
get pendingCut() {
|
|
420
|
+
return this.droppedCount;
|
|
421
|
+
}
|
|
422
|
+
get length() {
|
|
423
|
+
return this.records.length;
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
export class UnaryResponsePacer {
|
|
427
|
+
frames;
|
|
428
|
+
cursor = 0;
|
|
429
|
+
credit = INITIAL_STREAM_CREDIT;
|
|
430
|
+
startedAtMs;
|
|
431
|
+
constructor(streamId, payload, nowMs = Date.now()) {
|
|
432
|
+
this.frames = splitUnaryResponseFrames(streamId, payload);
|
|
433
|
+
this.startedAtMs = nowMs;
|
|
434
|
+
}
|
|
435
|
+
get done() {
|
|
436
|
+
return this.cursor >= this.frames.length;
|
|
437
|
+
}
|
|
438
|
+
isTimedOut(nowMs = Date.now()) {
|
|
439
|
+
return !this.done && nowMs - this.startedAtMs > UNARY_RESPONSE_DEADLINE_MS;
|
|
440
|
+
}
|
|
441
|
+
grantCredit(n) {
|
|
442
|
+
this.credit = Math.min(1024, this.credit + n);
|
|
443
|
+
}
|
|
444
|
+
pullAvailable(nowMs = Date.now(), allow = () => true) {
|
|
445
|
+
if (this.isTimedOut(nowMs)) {
|
|
446
|
+
return { ok: false, code: "deadline_exceeded" };
|
|
447
|
+
}
|
|
448
|
+
const out = [];
|
|
449
|
+
while (this.cursor < this.frames.length) {
|
|
450
|
+
const next = this.frames[this.cursor];
|
|
451
|
+
if (next.type === FrameType.CHUNK && this.credit <= 0)
|
|
452
|
+
break;
|
|
453
|
+
if (!allow(FRAME_HEADER_BYTES + next.payload.length))
|
|
454
|
+
break;
|
|
455
|
+
if (next.type === FrameType.CHUNK)
|
|
456
|
+
this.credit -= 1;
|
|
457
|
+
out.push(next);
|
|
458
|
+
this.cursor += 1;
|
|
459
|
+
}
|
|
460
|
+
return { ok: true, frames: out };
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
export class TokenBucket {
|
|
464
|
+
ratePerMin;
|
|
465
|
+
burst;
|
|
466
|
+
tokens;
|
|
467
|
+
at;
|
|
468
|
+
constructor(ratePerMin, burst, nowMs) {
|
|
469
|
+
this.ratePerMin = ratePerMin;
|
|
470
|
+
this.burst = burst;
|
|
471
|
+
this.tokens = burst;
|
|
472
|
+
this.at = nowMs;
|
|
473
|
+
}
|
|
474
|
+
refill(nowMs) {
|
|
475
|
+
if (nowMs > this.at)
|
|
476
|
+
this.tokens = Math.min(this.burst, this.tokens + ((nowMs - this.at) * this.ratePerMin) / 60_000);
|
|
477
|
+
this.at = Math.max(this.at, nowMs);
|
|
478
|
+
}
|
|
479
|
+
available(nowMs) {
|
|
480
|
+
this.refill(nowMs);
|
|
481
|
+
return this.tokens;
|
|
482
|
+
}
|
|
483
|
+
take(bytes, nowMs) {
|
|
484
|
+
this.refill(nowMs);
|
|
485
|
+
if (this.tokens < bytes)
|
|
486
|
+
return false;
|
|
487
|
+
this.tokens -= bytes;
|
|
488
|
+
return true;
|
|
489
|
+
}
|
|
490
|
+
waitFor(bytes, nowMs) {
|
|
491
|
+
this.refill(nowMs);
|
|
492
|
+
if (this.tokens >= bytes)
|
|
493
|
+
return 0;
|
|
494
|
+
return Math.ceil(((Math.min(bytes, this.burst) - this.tokens) * 60_000) / this.ratePerMin);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
export const PACING_PER_CONNECTION = { ratePerMin: 12 * 1024 * 1024, burst: 3 * 1024 * 1024 };
|
|
498
|
+
export const PACING_PER_HOSTED_LINK = { ratePerMin: 48 * 1024 * 1024, burst: 12 * 1024 * 1024 };
|