@platless/axom-cli 0.5.9 → 0.6.36

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.
Files changed (163) hide show
  1. package/README.md +18 -5
  2. package/dist/axom-patch/cli/README.md +100 -12
  3. package/dist/axom-patch/cli/account-limits/generate.py +15 -12
  4. package/dist/axom-patch/cli/account-limits/kimi-proxy.mjs +20 -3
  5. package/dist/axom-patch/cli/claude-account-limits.json +9 -9
  6. package/dist/axom-patch/cli/codex-account-limits.json +11 -11
  7. package/dist/axom-patch/cli/codex-terminal-output.json +16 -0
  8. package/dist/axom-patch/cli/commandcode-account-limits.json +9 -9
  9. package/dist/axom-patch/cli/kimi-quota.mjs +12 -0
  10. package/dist/axom-patch/cli/kimi-subagents.mjs +173 -0
  11. package/dist/axom-patch/cli/nightly-task.md +55 -13
  12. package/dist/axom-patch/cli/probes/codex-terminal-output.mjs +33 -0
  13. package/dist/axom-patch/cli/probes/kimi-subagent-subwork.mjs +82 -0
  14. package/dist/axom-patch/cli/qoder/dist/axom-acp-compat.mjs +1591 -0
  15. package/dist/axom-patch/cli/qoder/releases/1.1.75.json +360 -0
  16. package/dist/axom-patch/cli/qoder-background-terminal.md +13 -6
  17. package/dist/axom-patch/cli/qoder-foreground-output.md +14 -8
  18. package/dist/axom-patch/cli/qoder-native-acp.json +227 -0
  19. package/dist/axom-patch/cli/qoder-steering.md +20 -25
  20. package/dist/axom-patch/cli/qoder-subagent-subwork.md +17 -68
  21. package/dist/axom-patch/cli/runtime.json +16 -18
  22. package/dist/axom-patch/cli/zcode-account-limits.json +10 -11
  23. package/dist/axom-patch/cli/zcode-models.json +1 -2
  24. package/dist/axom-patch/cli/zcode-subagent-subwork.json +9 -10
  25. package/dist/cli.js +4 -36
  26. package/dist/commands/knowledge.js +47 -0
  27. package/dist/commands/update.js +65 -3
  28. package/dist/commands/workstation.js +73 -7
  29. package/dist/install/claude/commands.js +13 -13
  30. package/dist/mcp.js +1 -1
  31. package/dist/native/darwin-arm64/axom-quic +0 -0
  32. package/dist/workstation/acp/acpAgents.js +102 -98
  33. package/dist/workstation/acp/acpClient.js +26 -0
  34. package/dist/workstation/acp/builds/install/npm.js +99 -10
  35. package/dist/workstation/acp/initializeResponse.js +6 -1
  36. package/dist/workstation/acp/managedPatches.js +189 -25
  37. package/dist/workstation/acp/managedRuntime.js +314 -0
  38. package/dist/workstation/acp/patchAssets.js +40 -0
  39. package/dist/workstation/acp/processor/checkpoint.js +25 -34
  40. package/dist/workstation/acp/processor/control.js +5 -78
  41. package/dist/workstation/acp/processor/runtime.js +5 -81
  42. package/dist/workstation/acp/processor/session.js +51 -80
  43. package/dist/workstation/acp/processor/state.js +6 -13
  44. package/dist/workstation/acp/processor/step.js +3 -20
  45. package/dist/workstation/acp/processor/test/fixtureLog.js +9 -16
  46. package/dist/workstation/acp/processor/updates.js +131 -105
  47. package/dist/workstation/acp/processorSession.js +8 -172
  48. package/dist/workstation/acp/registry/catalog.js +65 -39
  49. package/dist/workstation/acp/registry/launch.js +16 -1
  50. package/dist/workstation/acp/switching.js +0 -3
  51. package/dist/workstation/acp/test/fakeAgent.js +13 -0
  52. package/dist/workstation/acp/test/keeperWorld.js +28 -0
  53. package/dist/workstation/acp/test/raw/sources.js +16 -130
  54. package/dist/workstation/acp/test/realAdapter.js +1 -47
  55. package/dist/workstation/acp/test/scriptAgent.js +11 -3
  56. package/dist/workstation/agentFlow.js +1 -1
  57. package/dist/workstation/agentSessions.js +38 -143
  58. package/dist/workstation/channels/management.js +95 -11
  59. package/dist/workstation/channels/url.js +1 -1
  60. package/dist/workstation/config.js +16 -4
  61. package/dist/workstation/connection/encryptedChannel.js +4 -1
  62. package/dist/workstation/control/bindingStore.js +200 -0
  63. package/dist/workstation/control/commands.js +124 -0
  64. package/dist/workstation/control/daemonIdentity.js +53 -0
  65. package/dist/workstation/control/enrollment.js +106 -0
  66. package/dist/workstation/control/inspect.js +71 -0
  67. package/dist/workstation/control/locks.js +63 -0
  68. package/dist/workstation/control/protocol.js +40 -0
  69. package/dist/workstation/control/restartGuard.js +46 -0
  70. package/dist/workstation/control/runtimeClient.js +97 -0
  71. package/dist/workstation/control/runtimeHost.js +148 -0
  72. package/dist/workstation/control/serverIdentity.js +35 -0
  73. package/dist/workstation/daemon.js +106 -6
  74. package/dist/workstation/daemonctl.js +77 -14
  75. package/dist/workstation/desktopControl/audio.js +99 -0
  76. package/dist/workstation/desktopControl/capabilities.js +92 -0
  77. package/dist/workstation/desktopControl/captureModes.js +9 -0
  78. package/dist/workstation/desktopControl/captureTargetLabel.js +9 -0
  79. package/dist/workstation/desktopControl/deviceSockets.js +246 -0
  80. package/dist/workstation/desktopControl/helperProcess.js +353 -0
  81. package/dist/workstation/desktopControl/lobby.js +129 -0
  82. package/dist/workstation/desktopControl/macProvider.js +155 -0
  83. package/dist/workstation/desktopControl/media.js +331 -0
  84. package/dist/workstation/desktopControl/nativeProvider.js +1018 -0
  85. package/dist/workstation/desktopControl/networkEvidence.js +43 -0
  86. package/dist/workstation/desktopControl/overview.js +58 -0
  87. package/dist/workstation/desktopControl/protocol.js +302 -0
  88. package/dist/workstation/desktopControl/quicMedia.js +240 -0
  89. package/dist/workstation/desktopControl/rateController.js +226 -0
  90. package/dist/workstation/desktopControl/supervisor.js +1681 -0
  91. package/dist/workstation/desktopControl/transport.js +27 -0
  92. package/dist/workstation/desktopControl/wallpaper.js +78 -0
  93. package/dist/workstation/files/credentials.js +142 -0
  94. package/dist/workstation/files/text.js +42 -8
  95. package/dist/workstation/keepers/kit/bundle.js +21 -17
  96. package/dist/workstation/keepers/kit/launcher.js +47 -7
  97. package/dist/workstation/keepers/kit/paths.js +2 -1
  98. package/dist/workstation/managedInstall.js +57 -0
  99. package/dist/workstation/protocol/agent.js +2 -15
  100. package/dist/workstation/protocol/management.js +93 -4
  101. package/dist/workstation/protocol/runtimeDiagnostics.js +15 -0
  102. package/dist/workstation/rd/artifact.js +119 -0
  103. package/dist/workstation/rd/control.js +119 -0
  104. package/dist/workstation/rd/display.js +29 -0
  105. package/dist/workstation/rd/provider.js +222 -0
  106. package/dist/workstation/rd/sessions.js +391 -0
  107. package/dist/workstation/rd/state.js +45 -0
  108. package/dist/workstation/rd/update.js +171 -0
  109. package/dist/workstation/rd/upkeep.js +38 -0
  110. package/dist/workstation/register.js +27 -37
  111. package/dist/workstation/service/installer.js +104 -13
  112. package/dist/workstation/service/launchd.js +1 -0
  113. package/dist/workstation/storageMetrics.js +118 -0
  114. package/dist/workstation/systemMetrics.js +44 -92
  115. package/dist/workstation/toolchain.js +94 -0
  116. package/dist/workstation/tools/browser.js +28 -4
  117. package/dist/workstation/update/artifact.js +2 -1
  118. package/dist/workstation/update/globalInstall.js +116 -0
  119. package/dist/workstation/update/npmInstaller.js +49 -23
  120. package/dist/workstation/update/rdRollback.js +145 -0
  121. package/dist/workstation/update/runUpdate.js +3 -2
  122. package/dist/workstation/update/watchdog.js +16790 -334
  123. package/package.json +5 -5
  124. package/skills/axom/SKILL.md +11 -11
  125. package/skills/axom-definition/SKILL.md +3 -3
  126. package/skills/axom-platform/SKILL.md +3 -3
  127. package/dist/axom-patch/cli/probes/qoder-steering.mjs +0 -89
  128. package/dist/axom-patch/cli/probes/qoder-subagent-origin.mjs +0 -61
  129. package/dist/axom-patch/cli/qoder-steering.json +0 -52
  130. package/dist/axom-patch/cli/qoder-subagent-origin.json +0 -73
  131. package/dist/commands/guide.js +0 -38
  132. package/dist/runtime-deps/qoder-sdk/package-lock.json +0 -1189
  133. package/dist/runtime-deps/qoder-sdk/package.json +0 -8
  134. package/dist/workstation/acp/delegationMcp.js +0 -24
  135. package/dist/workstation/acp/processor/sideChannel.js +0 -80
  136. package/dist/workstation/acp/qoderStartup.js +0 -45
  137. package/dist/workstation/acp/test/raw/qoderCaptures.js +0 -125
  138. package/dist/workstation/qoderAdapter/backgroundProcess.js +0 -118
  139. package/dist/workstation/qoderAdapter/backgroundTerminal.js +0 -193
  140. package/dist/workstation/qoderAdapter/backgroundTerminalFold.js +0 -372
  141. package/dist/workstation/qoderAdapter/callbackContext.js +0 -76
  142. package/dist/workstation/qoderAdapter/completedContinuation.js +0 -139
  143. package/dist/workstation/qoderAdapter/contract.js +0 -78
  144. package/dist/workstation/qoderAdapter/foregroundOutput.js +0 -36
  145. package/dist/workstation/qoderAdapter/history.js +0 -85
  146. package/dist/workstation/qoderAdapter/identityIndex.js +0 -191
  147. package/dist/workstation/qoderAdapter/inputQueue.js +0 -42
  148. package/dist/workstation/qoderAdapter/main.js +0 -95
  149. package/dist/workstation/qoderAdapter/mapper.js +0 -117
  150. package/dist/workstation/qoderAdapter/observerPlugin.js +0 -245
  151. package/dist/workstation/qoderAdapter/observerReceiver.js +0 -187
  152. package/dist/workstation/qoderAdapter/permissions.js +0 -61
  153. package/dist/workstation/qoderAdapter/profile.js +0 -93
  154. package/dist/workstation/qoderAdapter/sdkDriver.js +0 -48
  155. package/dist/workstation/qoderAdapter/sdkRuntime.js +0 -93
  156. package/dist/workstation/qoderAdapter/sdkRuntimeInstall.js +0 -145
  157. package/dist/workstation/qoderAdapter/session.js +0 -319
  158. package/dist/workstation/qoderAdapter/subagentMeta.js +0 -28
  159. package/dist/workstation/qoderAdapter/subwork.js +0 -85
  160. package/dist/workstation/qoderAdapter/taskControl.js +0 -38
  161. package/dist/workstation/qoderAdapter/taskLedger.js +0 -163
  162. package/dist/workstation/qoderAdapter/usage.js +0 -37
  163. /package/dist/workstation/{qoderAdapter/types.js → desktopControl/provider.js} +0 -0
@@ -0,0 +1,331 @@
1
+ import { z } from 'zod';
2
+ import { DesktopProtocolError, parseDesktopJson } from './protocol.js';
3
+ export function mediaRequire(value) {
4
+ if (!value)
5
+ throw new DesktopProtocolError('Invalid desktop media');
6
+ }
7
+ const integer = (n, min = 0, max = Number.MAX_SAFE_INTEGER) => Number.isSafeInteger(n) && n >= min && n <= max;
8
+ const axis = z.number().int().min(2).max(4096).refine(n => n % 2 === 0);
9
+ const label = z.string().min(1).refine(s => Buffer.byteLength(s) <= 128 && !/[\x00-\x1f\x7f]/.test(s));
10
+ const codecSchema = z.strictObject({ codec: z.literal('h264'), representation: z.literal(1),
11
+ width: axis, height: axis, fpsCeiling: z.union([z.literal(15), z.literal(30), z.literal(60)]),
12
+ profile: label, level: label, colorMatrix: z.literal('bt709'), colorRange: z.literal('limited'), parameterSets: z.array(z.string()) });
13
+ function checked(read) {
14
+ try {
15
+ return read();
16
+ }
17
+ catch {
18
+ throw new DesktopProtocolError('Invalid desktop media');
19
+ }
20
+ }
21
+ export function decodeConfig(bytes) {
22
+ return checked(() => {
23
+ const c = codecSchema.parse(parseDesktopJson(bytes, 16384));
24
+ mediaRequire(c.parameterSets.length === 2);
25
+ c.parameterSets.forEach((encoded, i) => {
26
+ mediaRequire(encoded.length <= 2732);
27
+ const nal = Buffer.from(encoded, 'base64');
28
+ mediaRequire(nal.length >= 1 && nal.length <= 2048 && nal.toString('base64') === encoded &&
29
+ (nal[0] & 128) === 0 && (nal[0] & 31) === (i % 2 === 0 ? 7 : 8));
30
+ });
31
+ return c;
32
+ });
33
+ }
34
+ function validateUnit(p, bytes) {
35
+ if (p.type === 1)
36
+ mediaRequire(decodeConfig(bytes).representation === p.representation);
37
+ else if (p.type === 3) {
38
+ const w = bytes.readUInt16BE(), h = bytes.readUInt16BE(2);
39
+ mediaRequire(w >= 1 && w <= 256 && h >= 1 && h <= 256 && bytes.readUInt16BE(4) < w &&
40
+ bytes.readUInt16BE(6) < h && w * h * 4 + 8 === p.unitLength);
41
+ }
42
+ else if (p.type === 5) {
43
+ mediaRequire(bytes.length >= 95 && bytes.subarray(0, 16).equals(Buffer.from('28b4619e8e524cf29ae8415852473034', 'hex')) && bytes[16] === 4 && bytes[37] === 1 && bytes.readUInt32BE(38) === 0 && bytes.readUInt16BE(42) > 0 && bytes.readUInt16BE(42) <= 1024);
44
+ const focusRevision = bytes.readBigUInt64BE(44), [x, y, w, h, sw, sh] = [52, 56, 60, 64, 68, 72].map(o => bytes.readUInt32BE(o));
45
+ mediaRequire(focusRevision > 0n && focusRevision <= BigInt(Number.MAX_SAFE_INTEGER) && w > 0 && h > 0 && sw >= 2 && sh >= 2 && sw <= 4096 && sh <= 4096 && x + w <= sw && y + h <= sh);
46
+ mediaRequire(axis.safeParse(bytes.readUInt16BE(17)).success && axis.safeParse(bytes.readUInt16BE(19)).success && bytes.readBigUInt64BE(21) > 0n && bytes.readBigUInt64BE(21) <= BigInt(Number.MAX_SAFE_INTEGER) && bytes.readBigUInt64BE(29) > 0n && bytes.readBigUInt64BE(29) <= BigInt(Number.MAX_SAFE_INTEGER));
47
+ }
48
+ else if (p.type === 4)
49
+ cursor(bytes);
50
+ }
51
+ function cursor(bytes) {
52
+ return checked(() => z.strictObject({ x: z.number().int().min(0).max(4095),
53
+ y: z.number().int().min(0).max(4095), visible: z.boolean() }).parse(parseDesktopJson(bytes, 1024)));
54
+ }
55
+ /** Validates the header and current identity before retaining/reassembling payload. */
56
+ export function decodeMedia(bytes, fence = () => { }) {
57
+ return checked(() => {
58
+ mediaRequire(bytes.length >= 65 && bytes.length <= 65600 && bytes.subarray(0, 5).equals(Buffer.from('AXDS\x01')) &&
59
+ bytes.readUInt16BE(8) === 64 && bytes.readUInt16BE(10) === 0 && bytes.readUIntBE(61, 3) === 0);
60
+ const u64 = (offset) => { const n = bytes.readBigUInt64BE(offset); mediaRequire(n <= BigInt(Number.MAX_SAFE_INTEGER)); return Number(n); };
61
+ const length = bytes.readUInt32BE(48);
62
+ const p = { type: bytes[5], flags: bytes.readUInt16BE(6), epoch: bytes.readUInt32BE(12),
63
+ displayRevision: bytes.readUInt32BE(16), sequence: bytes.readUInt32BE(20), frameId: u64(24), timestampUs: u64(32),
64
+ lastInputSequence: u64(40), unitLength: bytes.readUInt32BE(52), fragmentIndex: bytes.readUInt16BE(56),
65
+ fragmentCount: bytes.readUInt16BE(58), representation: bytes[60], payload: Buffer.alloc(0) };
66
+ mediaRequire(p.type >= 1 && p.type <= 5 && p.representation === 1 && p.flags <= 1 &&
67
+ (p.type === 2 || p.flags === 0) && p.epoch > 0 && p.displayRevision > 0 && p.sequence > 0 &&
68
+ ((p.type === 2 || p.type === 5) ? p.frameId > 0 : p.frameId === 0 && p.timestampUs === 0));
69
+ mediaRequire(length > 0 && length <= 65536 && bytes.length === 64 + length && p.unitLength > 0 && p.unitLength <= 8388608);
70
+ const count = Math.ceil(p.unitLength / 65536);
71
+ mediaRequire(p.fragmentCount === count && p.fragmentIndex < count &&
72
+ length === (p.fragmentIndex === count - 1 ? p.unitLength - p.fragmentIndex * 65536 : 65536));
73
+ if (p.type === 1)
74
+ mediaRequire(count === 1 && p.unitLength <= 16384);
75
+ if (p.type === 3)
76
+ mediaRequire(p.unitLength >= 12 && p.unitLength <= 262152);
77
+ if (p.type === 5)
78
+ mediaRequire(count === 1 && p.unitLength >= 95 && p.unitLength <= 24576);
79
+ if (p.type === 4)
80
+ mediaRequire(count === 1 && p.unitLength <= 1024);
81
+ fence(p);
82
+ p.payload = bytes.subarray(64);
83
+ if (count === 1)
84
+ validateUnit(p, p.payload);
85
+ return p;
86
+ });
87
+ }
88
+ export class DesktopMediaStream {
89
+ sourceDimensions;
90
+ constructor(sourceDimensions = () => undefined) {
91
+ this.sourceDimensions = sourceDimensions;
92
+ }
93
+ configuration = null;
94
+ epoch = 0;
95
+ revision = 0;
96
+ picture = { sequence: 0, lastFrame: 0, pending: undefined, storage: undefined, next: 0 };
97
+ cursorLane = { sequence: 0, lastFrame: 0, pending: undefined, storage: undefined, next: 0 };
98
+ exactLane = { sequence: 0, lastFrame: 0, pending: undefined, storage: undefined, next: 0 };
99
+ lane(type) { return type === 3 || type === 4 ? this.cursorLane : type === 5 ? this.exactLane : this.picture; }
100
+ exactSequence = 0;
101
+ recentFrames = [];
102
+ needsIdr = true;
103
+ failed = false;
104
+ accept(bytes) {
105
+ mediaRequire(!this.failed);
106
+ try {
107
+ const p = decodeMedia(bytes, packet => this.validate(packet));
108
+ const lane = this.lane(p.type);
109
+ lane.sequence = p.sequence;
110
+ if (p.type === 1) {
111
+ this.configuration = decodeConfig(p.payload);
112
+ this.epoch = p.epoch;
113
+ this.revision = p.displayRevision;
114
+ this.needsIdr = true;
115
+ this.exactSequence = 0;
116
+ this.exactLane.sequence = 0;
117
+ this.exactLane.pending = undefined;
118
+ this.exactLane.storage = undefined;
119
+ this.exactLane.next = 0;
120
+ this.cursorLane.sequence = 0;
121
+ this.cursorLane.pending = undefined;
122
+ this.cursorLane.storage = undefined;
123
+ this.cursorLane.next = 0;
124
+ return null;
125
+ }
126
+ if (p.fragmentCount === 1)
127
+ return this.complete(p, p.payload);
128
+ if (!lane.pending) {
129
+ lane.pending = { ...p, payload: Buffer.alloc(0) };
130
+ lane.storage = Buffer.alloc(p.unitLength);
131
+ lane.next = 0;
132
+ }
133
+ p.payload.copy(lane.storage, lane.next++ * 65536);
134
+ if (lane.next !== p.fragmentCount)
135
+ return null;
136
+ const first = lane.pending, unit = lane.storage;
137
+ lane.pending = undefined;
138
+ lane.storage = undefined;
139
+ validateUnit(first, unit);
140
+ return this.complete(first, unit);
141
+ }
142
+ catch {
143
+ this.failed = true;
144
+ this.picture.pending = this.cursorLane.pending = this.exactLane.pending = undefined;
145
+ this.picture.storage = this.cursorLane.storage = this.exactLane.storage = undefined;
146
+ throw new DesktopProtocolError('Invalid desktop stream');
147
+ }
148
+ }
149
+ applyMotionCut(pictureThrough, frameThrough) {
150
+ mediaRequire(!this.failed && this.configuration && integer(pictureThrough, this.picture.sequence, 0xffffffff) && integer(frameThrough, this.picture.lastFrame));
151
+ this.picture.sequence = pictureThrough;
152
+ this.picture.lastFrame = frameThrough;
153
+ this.picture.pending = undefined;
154
+ this.picture.storage = undefined;
155
+ this.picture.next = 0;
156
+ this.needsIdr = true;
157
+ }
158
+ validate(p) {
159
+ const lane = this.lane(p.type);
160
+ if (p.type === 1) {
161
+ mediaRequire(!this.picture.pending && p.epoch > this.epoch && p.sequence === 1);
162
+ return;
163
+ }
164
+ mediaRequire(this.configuration && p.epoch === this.epoch && p.displayRevision === this.revision &&
165
+ p.representation === this.configuration.representation && p.sequence === lane.sequence + 1);
166
+ if (!lane.pending) {
167
+ mediaRequire(p.fragmentIndex === 0);
168
+ if (p.type === 2 || p.type === 5)
169
+ mediaRequire(p.frameId > lane.lastFrame && !this.recentFrames.includes(p.frameId) && p.frameId !== this.picture.pending?.frameId && (!this.needsIdr || p.type === 2 && p.flags === 1));
170
+ }
171
+ else {
172
+ const first = lane.pending;
173
+ mediaRequire(['type', 'flags', 'frameId', 'timestampUs', 'lastInputSequence', 'unitLength', 'fragmentCount']
174
+ .every(k => p[k] === first[k]) && p.fragmentIndex === lane.next);
175
+ }
176
+ }
177
+ complete(p, bytes) {
178
+ if (p.type === 4) {
179
+ const c = cursor(bytes), source = this.sourceDimensions() ?? this.configuration;
180
+ mediaRequire(c.x < source.width && c.y < source.height);
181
+ }
182
+ if (p.type !== 2 && p.type !== 5)
183
+ return null;
184
+ const exactSequence = p.type === 5 ? Number(bytes.readBigUInt64BE(29)) : 0;
185
+ if (p.type === 5) {
186
+ mediaRequire(exactSequence === this.exactSequence + 1 && bytes.readUInt16BE(17) === this.configuration.width && bytes.readUInt16BE(19) === this.configuration.height);
187
+ this.exactSequence = exactSequence;
188
+ }
189
+ else
190
+ this.needsIdr = false;
191
+ this.lane(p.type).lastFrame = p.frameId;
192
+ this.recentFrames.push(p.frameId);
193
+ if (this.recentFrames.length > 128)
194
+ this.recentFrames.shift();
195
+ return { ...p, payload: bytes, mainAU: bytes, exactSequence };
196
+ }
197
+ }
198
+ const lanes = ['motion', 'cursor', 'exact'];
199
+ const consumedKey = { motion: 'motionConsumedBytes', cursor: 'cursorConsumedBytes', exact: 'exactConsumedBytes' };
200
+ const freshTotals = () => ({ motion: 0, cursor: 0, exact: 0 });
201
+ export function desktopMediaLane(type) {
202
+ mediaRequire(integer(type, 1, 5));
203
+ return type <= 2 ? 'motion' : type <= 4 ? 'cursor' : 'exact';
204
+ }
205
+ /** One epoch's bounded reservations and actual transport admissions. Pipe writes never commit. */
206
+ export class DesktopMediaFlowWindow {
207
+ window;
208
+ records = new Map();
209
+ committed = freshTotals();
210
+ reserved = freshTotals();
211
+ observed = freshTotals();
212
+ consumed = freshTotals();
213
+ lastId = 0;
214
+ constructor(window = 0) {
215
+ this.window = window;
216
+ mediaRequire(window === 0 || integer(window, 196608, 524288));
217
+ }
218
+ get inFlightBytes() { return lanes.reduce((n, l) => n + this.debt(l), 0); }
219
+ get recordCount() { return this.records.size; }
220
+ get hasCredit() { return this.window > 0; }
221
+ get appliedCredit() { return { motionConsumedBytes: this.consumed.motion, cursorConsumedBytes: this.consumed.cursor, exactConsumedBytes: this.consumed.exact, windowBytes: this.window }; }
222
+ debt(lane) { return this.committed[lane] + this.reserved[lane] - this.consumed[lane]; }
223
+ laneRecords(lane) { return [...this.records.values()].filter(r => r.lane === lane).length; }
224
+ admit(lane, bytes) {
225
+ mediaRequire(lanes.includes(lane) && integer(bytes, 1, lane === 'exact' ? 24576 : 65536));
226
+ const others = lanes.filter(l => l !== lane && l !== 'motion');
227
+ const protectedBytes = others.reduce((n, l) => n + Math.max(0, (l === 'cursor' ? 65536 : 24576) - this.debt(l)), 0);
228
+ const protectedRecords = others.reduce((n, l) => n + (this.laneRecords(l) === 0 ? 1 : 0), 0);
229
+ return this.records.size + 1 <= 128 - protectedRecords && this.inFlightBytes + bytes <= this.window - protectedBytes;
230
+ }
231
+ /** IDs are fresh monotonically increasing local admission attempts, including failed attempts. */
232
+ reserve(id, lane, bytes) {
233
+ mediaRequire(integer(id, this.lastId + 1));
234
+ this.lastId = id;
235
+ if (!this.admit(lane, bytes))
236
+ return false;
237
+ mediaRequire(this.committed[lane] <= Number.MAX_SAFE_INTEGER - this.reserved[lane] - bytes);
238
+ this.reserved[lane] += bytes;
239
+ this.records.set(id, { lane, bytes, remaining: bytes, committed: false });
240
+ return true;
241
+ }
242
+ commit(id) {
243
+ const r = this.records.get(id);
244
+ mediaRequire(r && !r.committed);
245
+ this.reserved[r.lane] -= r.bytes;
246
+ this.committed[r.lane] += r.bytes;
247
+ r.committed = true;
248
+ this.applyPending();
249
+ }
250
+ cancel(id) {
251
+ const r = this.records.get(id);
252
+ mediaRequire(r && !r.committed);
253
+ mediaRequire(this.observed[r.lane] <= this.committed[r.lane] + this.reserved[r.lane] - r.bytes);
254
+ this.reserved[r.lane] -= r.bytes;
255
+ this.records.delete(id);
256
+ }
257
+ credit(value) {
258
+ mediaRequire(integer(value.windowBytes, 196608, 524288) && Number.isSafeInteger(value.motionConsumedBytes + value.cursorConsumedBytes + value.exactConsumedBytes));
259
+ for (const l of lanes)
260
+ mediaRequire(integer(value[consumedKey[l]], this.observed[l], this.committed[l] + this.reserved[l]));
261
+ for (const l of lanes)
262
+ this.observed[l] = value[consumedKey[l]];
263
+ this.window = value.windowBytes;
264
+ this.applyPending();
265
+ }
266
+ applyPending() {
267
+ for (const lane of lanes) {
268
+ if (this.observed[lane] > this.committed[lane])
269
+ continue;
270
+ let delta = this.observed[lane] - this.consumed[lane];
271
+ this.consumed[lane] = this.observed[lane];
272
+ for (const [id, r] of this.records)
273
+ if (delta && r.lane === lane && r.committed) {
274
+ const used = Math.min(delta, r.remaining);
275
+ r.remaining -= used;
276
+ delta -= used;
277
+ if (!r.remaining)
278
+ this.records.delete(id);
279
+ }
280
+ mediaRequire(delta === 0);
281
+ }
282
+ }
283
+ }
284
+ /** Sender-clock complete-frame send -> feedback. Feedback scheduling is not network delay. */
285
+ export class DesktopDeliveryDelay {
286
+ frames = new Map();
287
+ samples = [];
288
+ observation = 0;
289
+ sampled = 0;
290
+ sent(frame, at) {
291
+ this.frames.set(frame, at);
292
+ if (this.frames.size > 512)
293
+ this.frames.delete(this.frames.keys().next().value);
294
+ }
295
+ received(frame, at) {
296
+ const sent = this.frames.get(frame);
297
+ if (sent === undefined)
298
+ return;
299
+ this.samples.push({ at, delay: Math.max(0, at - sent), observation: ++this.observation });
300
+ if (this.samples.length > 500)
301
+ this.samples.shift();
302
+ // Independent exact/motion completion may insert IDs out of numeric order.
303
+ for (const id of this.frames.keys())
304
+ if (id <= frame)
305
+ this.frames.delete(id);
306
+ }
307
+ excess(now) {
308
+ this.samples = this.samples.filter(s => now - s.at <= 90000);
309
+ const recent = this.samples.filter(s => now - s.at <= 1250);
310
+ if (!recent.length)
311
+ return 0;
312
+ return Math.max(0, Math.min(...recent.map(s => s.delay)) - Math.min(...this.samples.map(s => s.delay)));
313
+ }
314
+ sample(now) {
315
+ const raw = this.excess(now);
316
+ const fresh = this.samples.filter(s => s.observation > this.sampled && now - s.at <= 1250);
317
+ this.sampled = this.observation;
318
+ if (!fresh.length)
319
+ return { excessMs: raw, observations: 0, delayMs: undefined };
320
+ const excessMs = Math.max(0, Math.min(...fresh.map(s => s.delay)) - Math.min(...this.samples.map(s => s.delay)));
321
+ // The active feedback cadence allows 200ms of scheduling uncertainty. Only
322
+ // the excess beyond that can establish >40ms congestion; ambiguity must hold.
323
+ const delayMs = excessMs <= 40 ? excessMs : excessMs > 240 ? excessMs - 200 : undefined;
324
+ return { excessMs, observations: fresh.length, delayMs };
325
+ }
326
+ reset() {
327
+ this.frames.clear();
328
+ this.samples = [];
329
+ this.sampled = this.observation;
330
+ }
331
+ }