@aidenhq/sdk 0.5.5 → 0.5.6

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.
@@ -25,8 +25,10 @@ export type SendResult = {
25
25
  export declare const SEND_EVENTS = 500, SEND_BYTES = 1000000;
26
26
  /**
27
27
  * Sends `events` oldest first. A 200 answers each event: a refusal (the kernel's schema refused it) is returned, every other
28
- * event is sent (logged, or a duplicate of one logged). Any other answer (no answer, a whole-batch 4xx, 429, 5xx) sends and
29
- * refuses nothing from that batch on: the caller keeps them and tries again (after `retryAfterMs` when the kernel said so).
28
+ * event is sent (logged, or a duplicate of one logged). An event that cannot fit a batch on its own is refused up front as
29
+ * `too large` and never posted, so it cannot hold back the events after it. Any other answer (no answer, a whole-batch 4xx,
30
+ * 429, 5xx) sends and refuses nothing from that batch on: the caller keeps them and tries again (after `retryAfterMs` when the
31
+ * kernel said so).
30
32
  */
31
33
  export declare function sendEvents(post: EventPost, events: SourceEventLike[]): Promise<SendResult>;
32
34
  /** A small durable store for a source with none of its own: a JSON file, replaced whole by an atomic rename. */
@@ -3,14 +3,29 @@
3
3
  // (scripts/sync-shared.ts: edit this one and run it). Node built-ins only.
4
4
  import { existsSync, readFileSync, renameSync, writeFileSync } from "node:fs";
5
5
  export const SEND_EVENTS = 500, SEND_BYTES = 1_000_000;
6
- /** `events` cut into the kernel's batch limits, in order. */
6
+ /** An event's serialized size in a batch: its JSON and the comma after it. */
7
+ function size(e) { return Buffer.byteLength(JSON.stringify(e)) + 1; }
8
+ /**
9
+ * An event's own batch as the kernel limits it: `events.ingest` refuses a list over `SEND_BYTES` (`kernel/src/events.ts`), so a
10
+ * singleton array is the smallest request that can carry the event. The JSON envelope around it is inside the kernel's body cap.
11
+ */
12
+ function alone(e) { return Buffer.byteLength(JSON.stringify([e])); }
13
+ /**
14
+ * `events` cut into the kernel's batch limits, in order. An event the kernel would refuse as a batch of one is refused here,
15
+ * never batched: the kernel refuses the whole request it is in, so batching it would block every event behind it. The test is
16
+ * against the kernel's list limit, not the batching cushion: an event too big to group is still sent alone, as before.
17
+ */
7
18
  function chunks(events) {
8
- const out = [];
19
+ const batches = [], refused = [];
9
20
  let cur = [], bytes = 0;
10
21
  for (const e of events) {
11
- const n = Buffer.byteLength(JSON.stringify(e)) + 1;
22
+ const n = size(e);
23
+ if (alone(e) > SEND_BYTES) {
24
+ refused.push({ id: e.id, why: "too large" });
25
+ continue;
26
+ }
12
27
  if (cur.length && (cur.length >= SEND_EVENTS || bytes + n > SEND_BYTES - 100)) {
13
- out.push(cur);
28
+ batches.push(cur);
14
29
  cur = [];
15
30
  bytes = 0;
16
31
  }
@@ -18,17 +33,20 @@ function chunks(events) {
18
33
  bytes += n;
19
34
  }
20
35
  if (cur.length)
21
- out.push(cur);
22
- return out;
36
+ batches.push(cur);
37
+ return { batches, refused };
23
38
  }
24
39
  /**
25
40
  * Sends `events` oldest first. A 200 answers each event: a refusal (the kernel's schema refused it) is returned, every other
26
- * event is sent (logged, or a duplicate of one logged). Any other answer (no answer, a whole-batch 4xx, 429, 5xx) sends and
27
- * refuses nothing from that batch on: the caller keeps them and tries again (after `retryAfterMs` when the kernel said so).
41
+ * event is sent (logged, or a duplicate of one logged). An event that cannot fit a batch on its own is refused up front as
42
+ * `too large` and never posted, so it cannot hold back the events after it. Any other answer (no answer, a whole-batch 4xx,
43
+ * 429, 5xx) sends and refuses nothing from that batch on: the caller keeps them and tries again (after `retryAfterMs` when the
44
+ * kernel said so).
28
45
  */
29
46
  export async function sendEvents(post, events) {
30
- const out = { sent: [], refused: [] };
31
- for (const batch of chunks(events)) {
47
+ const { batches, refused } = chunks(events);
48
+ const out = { sent: [], refused };
49
+ for (const batch of batches) {
32
50
  const r = await post({ events: batch }).catch(() => ({ status: 0, json: undefined, retryAfterS: undefined }));
33
51
  if (r.status !== 200) {
34
52
  if (r.retryAfterS !== undefined)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aidenhq/sdk",
3
- "version": "0.5.5",
3
+ "version": "0.5.6",
4
4
  "description": "The shared code of AidenHQ modules: contract types, engine IO, the caller check and fence, write executors, the reviewer side, the judges' models and call, the source writer and the vendor MCP pass-through.",
5
5
  "license": "MIT",
6
6
  "repository": {