Skip to content

Commit 8968554

Browse files
authored
fix(coven): support bounded large automation read responses (#353)
Support existing automation list and detail reads for valid large definitions while retaining finite response limits and strict decoding. Add exact-limit and oversized-response coverage for injected, Unix and Windows transports. Closes #352. Refs #80 and OpenCoven/coven-cave#6208.
1 parent 1880e69 commit 8968554

6 files changed

Lines changed: 79 additions & 14 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opencoven/coven-client": patch
3+
---
4+
5+
Allow supported automation inventories, rich definition snapshots and run logs above 16 KiB through existing typed reads. Keep decoder and authenticated socket limits aligned at explicit finite budgets: 4 MiB for definition/run lists and 1 MiB for individual definition/run reads, while retaining existing narrower capability, control, health, receipt, event and history limits. Schema, duplicate-key, correlation, cancellation and ownership checks remain enforced.

‎packages/coven/README.md‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ Discovery runs once. Health and Automations reuse that exact endpoint, security
8888
provider, and platform dependencies. Construction sends no health, capability,
8989
or action request. Each later request authenticates its own connection.
9090
The `unix`/`windows` health response-limit options still apply only to health;
91-
Automations keeps its fixed 16 KiB body limit, except for the event (1 MiB) and
92-
occurrence- and run-history (256 KiB) reads described below.
91+
Automations bounds definition inventories and compatibility run lists at 4 MiB, individual definition/run reads and event pages at 1 MiB, and paged occurrence/run histories at 256 KiB. Capabilities, health, receipts and other narrow responses retain their 16 KiB limit. Both injected-client decoding and authenticated Unix/Windows sockets enforce the same action-specific budgets. Oversized responses fail explicitly without partial results or fallback; accepted bytes still undergo the existing schema, duplicate-key, correlation and UTF-8 checks.
9392

9493
`client.automations` is `CovenAutomationsClient | undefined`.
9594
`client.requireAutomations()` returns that same namespace or throws

‎packages/coven/src/automations-definitions.ts‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,22 @@ export type CovenAutomationDefinitionReadRequest =
115115
| { readonly action: 'coven.automations.receipt.get.v1'; readonly id: string }
116116
| CovenAutomationEventsRequest;
117117

118+
/** Finite response budgets for existing read actions. Definition-bearing reads
119+
* must accommodate documents accepted by the command API and their projections.
120+
* Capabilities, health and other narrow diagnostic reads retain their own cap. */
121+
export function definitionReadResponseLimit(action: string): number {
122+
switch (action) {
123+
case 'coven.automations.definition.list.v1':
124+
case 'coven.automations.runs': return 4_194_304;
125+
case 'coven.automations.definition.get.v1':
126+
case 'coven.automations.run.get.v1': return 1_048_576;
127+
case 'coven.automations.events.subscribe.v1': return AUTOMATION_EVENTS_MAX_BYTES;
128+
case 'coven.automations.occurrence.history.v1':
129+
case 'coven.automations.run.history.v1': return AUTOMATION_HISTORY_MAX_BYTES;
130+
default: return 16_384;
131+
}
132+
}
133+
118134
export function definitionReadFailure(code: string, operation: string): never {
119135
throw new CovenClientError(normalizeCovenError({ code }, operation));
120136
}
@@ -227,9 +243,7 @@ export function decodeDefinitionRead(
227243
const invalid = (): never => definitionReadFailure('invalid_response', operation);
228244
let value: unknown;
229245
try {
230-
value = parsePolicyJson(bytes, request.action === 'coven.automations.events.subscribe.v1' ? AUTOMATION_EVENTS_MAX_BYTES
231-
: request.action === 'coven.automations.occurrence.history.v1' || request.action === 'coven.automations.run.history.v1'
232-
? AUTOMATION_HISTORY_MAX_BYTES : 16_384);
246+
value = parsePolicyJson(bytes, definitionReadResponseLimit(request.action));
233247
} catch {
234248
return invalid();
235249
}

‎packages/coven/src/automations-socket.ts‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import { runOperation, type OperationContext } from '@opencoven/sdk-core';
22

33
import type { CovenAutomationsTransport } from './automations.js';
44
import { commandBytes, commandResponseLimit } from './automations-commands.js';
5-
import { definitionReadBytes } from './automations-definitions.js';
6-
import { AUTOMATION_EVENTS_MAX_BYTES } from './automations-events.js';
7-
import { AUTOMATION_HISTORY_MAX_BYTES } from './automations-history.js';
5+
import { definitionReadBytes, definitionReadResponseLimit } from './automations-definitions.js';
86
import { CovenClientError, normalizeCovenError } from './client-errors.js';
97
import {
108
requestCovenPolicyOverSocket,
@@ -62,9 +60,7 @@ export function createCovenAutomationsSocketTransport(
6260
`Content-Length: ${body.byteLength}\r\n\r\n`,
6361
),
6462
body,
65-
]), context, 'automations.read', action === 'coven.automations.events.subscribe.v1' ? AUTOMATION_EVENTS_MAX_BYTES
66-
: action === 'coven.automations.occurrence.history.v1' || action === 'coven.automations.run.history.v1'
67-
? AUTOMATION_HISTORY_MAX_BYTES : 16_384);
63+
]), context, 'automations.read', definitionReadResponseLimit(action));
6864
},
6965
};
7066
}

‎tests/coven-automations-platforms.spec.ts‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,23 @@ describe.each(['unix', 'windows'] as const)('%s Automations parity', (platform)
365365
expect(sockets).toHaveLength(2);
366366
});
367367

368+
test.skipIf(platform === 'unix' && process.platform === 'win32').each([
369+
{ action: 'coven.automations.definition.list.v1', bytes: 4_194_304, payload: { routines: [], revisionById: {}, tombstonedAtById: {} }, call: (client: CovenAutomationsClient) => client.list() },
370+
{ action: 'coven.automations.definition.get.v1', bytes: 1_048_576, payload: { routine: null }, call: (client: CovenAutomationsClient) => client.get('morning') },
371+
{ action: 'coven.automations.runs', bytes: 4_194_304, payload: { runs: [] }, call: (client: CovenAutomationsClient) => client.runs('morning') },
372+
{ action: 'coven.automations.run.get.v1', bytes: 1_048_576, payload: { run: null }, call: (client: CovenAutomationsClient) => client.getRun('run-1') },
373+
])('keeps decoder and socket response budgets aligned for $action', async ({ action, bytes, payload, call }) => {
374+
const { client, configure } = setup(platform);
375+
const encoded = Buffer.from(JSON.stringify(envelope(action, payload)));
376+
let body = Buffer.concat([encoded, Buffer.alloc(bytes - encoded.length, 0x20)]);
377+
configure.mockImplementation((socket, index) => {
378+
socket.response = index % 2 === 0 ? Buffer.from(JSON.stringify(advertisement([action] as never))) : body;
379+
});
380+
expect(await call(client)).toEqual(payload);
381+
body = Buffer.concat([body, Buffer.from(' ')]);
382+
await expect(call(client)).rejects.toMatchObject({ code: 'body_limit' });
383+
});
384+
368385
test.skipIf(platform === 'unix' && process.platform === 'win32').each(reads)(
369386
'authenticates and strictly decodes $request.action', async ({ request, payload, call }) => {
370387
const { client, configure, sockets, ownership } = setup(platform);
@@ -393,17 +410,19 @@ describe.each(['unix', 'windows'] as const)('%s Automations parity', (platform)
393410
});
394411
await expect(call(client)).rejects.toMatchObject({ code: 'capability_unsupported' });
395412
expect(sockets).toHaveLength(3);
413+
const responseLimit = request.action === 'coven.automations.definition.list.v1' || request.action === 'coven.automations.runs'
414+
? 4_194_304 : request.action === 'coven.automations.definition.get.v1' ? 1_048_576 : 16_384;
396415
for (const body of [
397416
Buffer.from('{"ok":true,"ok":true}'), Buffer.from([0xff]),
398417
Buffer.from(JSON.stringify(envelope('wrong.action', payload))),
399418
Buffer.from(JSON.stringify(envelope(request.action, {}))),
400-
Buffer.alloc(16_385),
419+
Buffer.alloc(responseLimit + 1),
401420
]) {
402421
configure.mockImplementation((socket, index) => {
403422
socket.response = index % 2 === 1 ? Buffer.from(JSON.stringify(advertisement())) : body;
404423
});
405424
await expect(call(client)).rejects.toMatchObject({
406-
code: body.length > 16_384 ? 'body_limit' : 'invalid_response',
425+
code: body.length > responseLimit ? 'body_limit' : 'invalid_response',
407426
});
408427
}
409428
expect(sockets.every((socket) => socket.destroyed)).toBe(true);

‎tests/coven-automations.spec.ts‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,38 @@ function readSetup(value: unknown = definitionList(), action: string = listActio
475475
return { transport, client: createCovenAutomationsClient({ transport }) };
476476
}
477477

478+
test('reads supported aggregate inventories and rich snapshots larger than 16 KiB', async () => {
479+
const first = { ...routine(), prompt: 'x'.repeat(9_000) };
480+
const second = { ...first, id: 'evening' };
481+
const inventory = { routines: [first, second], revisionById: { morning: 2, evening: 1 }, tombstonedAtById: {} };
482+
expect(await readSetup(inventory).client.list()).toEqual(inventory);
483+
const snapshot = { routine: { ...first, prompt: 'x'.repeat(100_000) }, revision: 2, tombstonedAt: null };
484+
const producer = { ...snapshot, definition: { action: { prompt: snapshot.routine.prompt } } };
485+
expect(await readSetup(producer, getAction).client.get('morning')).toEqual(snapshot);
486+
});
487+
488+
test('reads bounded run logs above the narrow control-response limit', async () => {
489+
const run = { ...runSnapshot(), logJson: 'log'.repeat(9_000) };
490+
expect(await readSetup({ runs: [run] }, runsAction).client.runs('morning')).toEqual({ runs: [run] });
491+
const detail = { ...runDetail(), logJson: run.logJson };
492+
expect(await readSetup({ run: detail }, runAction).client.getRun('run-1')).toEqual({ run: detail });
493+
});
494+
495+
test.each([
496+
{ action: listAction, bytes: 4_194_304, payload: definitionList(), call: (client: ReturnType<typeof readSetup>['client']) => client.list() },
497+
{ action: getAction, bytes: 1_048_576, payload: definitionGet(), call: (client: ReturnType<typeof readSetup>['client']) => client.get('morning') },
498+
{ action: runsAction, bytes: 4_194_304, payload: { runs: [] }, call: (client: ReturnType<typeof readSetup>['client']) => client.runs('morning') },
499+
{ action: 'coven.automations.run.get.v1', bytes: 1_048_576, payload: { run: null }, call: (client: ReturnType<typeof readSetup>['client']) => client.getRun('run-1') },
500+
])('enforces the exact $bytes-byte response budget for $action', async ({ action, bytes, payload, call }) => {
501+
const { client, transport } = readSetup(payload, action);
502+
const encoded = Buffer.from(JSON.stringify(readEnvelope(action, payload)));
503+
const exact = Buffer.concat([encoded, Buffer.alloc(bytes - encoded.length, 0x20)]);
504+
transport.readDefinitions.mockResolvedValue({ status: 200, body: exact });
505+
expect(await call(client)).toEqual(payload);
506+
transport.readDefinitions.mockResolvedValue({ status: 200, body: Buffer.concat([exact, Buffer.from(' ')]) });
507+
await expect(call(client)).rejects.toMatchObject({ code: 'invalid_response' });
508+
});
509+
478510
test('lists compatibility routines with exact revision and tombstone maps, not rich definitions', async () => {
479511
const { client, transport } = readSetup();
480512
const result = await client.list();
@@ -919,7 +951,7 @@ test.skipIf(process.platform === 'win32').each([
919951

920952
test.skipIf(process.platform === 'win32')('read transport rejects oversized response without exposing partial results', async () => {
921953
const { transport, socket } = unixSetup();
922-
socket.response = Buffer.alloc(16_385);
954+
socket.response = Buffer.alloc(4_194_305);
923955
await expect(transport.readDefinitions!({ action: listAction, includeTombstoned: false }, {
924956
signal: new AbortController().signal, deadline: undefined,
925957
})).rejects.toMatchObject({ code: 'body_limit' });

0 commit comments

Comments
 (0)