@operad-io/harness-process 0.2.0 → 0.3.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.
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@operad-io/harness-process",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./src/index.ts"
7
7
  },
8
8
  "dependencies": {
9
- "@operad-io/access-match": "0.2.0",
10
- "@operad-io/engine": "0.2.0",
11
- "@operad-io/mcp": "0.2.0"
9
+ "@operad-io/access-match": "0.3.0",
10
+ "@operad-io/engine": "0.3.0",
11
+ "@operad-io/mcp": "0.3.0"
12
12
  },
13
13
  "description": "operad: the shared process and log helpers of the harness plugins",
14
14
  "repository": {
package/src/process.ts CHANGED
@@ -101,6 +101,24 @@ const STDERR_LIMIT = 64 * 1024;
101
101
  */
102
102
  export const MAX_LINE_CHARS = 8 * 1024 * 1024;
103
103
 
104
+ /**
105
+ * The most one over-long line may run to while it is skipped. A line past
106
+ * `MAX_LINE_CHARS` is dropped, not held, and the run goes on with the next
107
+ * line (a harness that carries an image on one line must not fail the step); a
108
+ * line that never ends is a malformed stream and past this the group is stopped.
109
+ */
110
+ export const MAX_SKIPPED_LINE_CHARS = 256 * 1024 * 1024;
111
+
112
+ // The log line that stands for a stdout line dropped for its length, so a
113
+ // reader of the log sees that output was left out and how much.
114
+ export function skippedLineRecord(chars: number): string {
115
+ return JSON.stringify({
116
+ type: "line_skipped",
117
+ chars,
118
+ message: `a line of ${chars} characters was dropped unread (the limit is ${MAX_LINE_CHARS})`,
119
+ });
120
+ }
121
+
104
122
  /** Finds an executable on PATH, or returns undefined. */
105
123
  export async function findCommand(command: string): Promise<string | undefined> {
106
124
  if (command.includes("/")) {
@@ -173,6 +191,11 @@ export interface AgentRunOptions {
173
191
  line: string,
174
192
  reply: (text: string) => void,
175
193
  ) => void | Promise<void>;
194
+ /**
195
+ * Called with the length of a stdout line past `MAX_LINE_CHARS`, which was
196
+ * dropped unread. The run goes on with the next line.
197
+ */
198
+ readonly onSkipped?: ((chars: number) => void) | undefined;
176
199
  /**
177
200
  * Called once the command has started and before any of its output is read:
178
201
  * a harness with no session handshake writes its ready line here.
@@ -356,44 +379,75 @@ function stoppedOutcome(stoppingFor: StopKind | undefined): Partial<AgentRunOutc
356
379
  /**
357
380
  * Splits a stream into complete lines, holding the unterminated tail up to
358
381
  * `MAX_LINE_CHARS`. Each line is handed over before the next is read: a busy
359
- * handler holds the stream, so the pipe cannot fill memory. The tail is dropped
360
- * and `onOverflow` is called when a line grows past the limit, so no input can
361
- * exhaust memory.
382
+ * handler holds the stream, so the pipe cannot fill memory. A line that grows
383
+ * past the limit is dropped as it arrives and `onSkipped` gets its length once
384
+ * it ends; the next line is read as usual. A line that passes
385
+ * `MAX_SKIPPED_LINE_CHARS` without ending calls `onOverflow`.
362
386
  */
363
387
  class Lines {
364
388
  private buffer = "";
389
+ /** The length of the over-long line being dropped; undefined when none is. */
390
+ private skipped: number | undefined = undefined;
365
391
  private readonly onLine: (line: string) => void | Promise<void>;
392
+ private readonly onSkipped: (chars: number) => void;
366
393
  private readonly onOverflow: () => void;
367
394
 
368
395
  constructor(
369
396
  onLine: (line: string) => void | Promise<void>,
397
+ onSkipped: (chars: number) => void,
370
398
  onOverflow: () => void,
371
399
  ) {
372
400
  this.onLine = onLine;
401
+ this.onSkipped = onSkipped;
373
402
  this.onOverflow = onOverflow;
374
403
  }
375
404
 
376
405
  async push(text: string): Promise<void> {
377
- if (text.length === 0) return;
378
- this.buffer += text;
379
- let newline = this.buffer.indexOf("\n");
380
- while (newline >= 0) {
381
- const line = this.buffer.slice(0, newline);
382
- this.buffer = this.buffer.slice(newline + 1);
383
- await this.onLine(line);
384
- newline = this.buffer.indexOf("\n");
385
- }
386
- if (this.buffer.length > MAX_LINE_CHARS) {
387
- this.buffer = "";
388
- this.onOverflow();
406
+ let rest = text;
407
+ while (rest.length > 0) {
408
+ if (this.skipped !== undefined) {
409
+ const end = rest.indexOf("\n");
410
+ if (end < 0) {
411
+ this.skipped += rest.length;
412
+ if (this.skipped > MAX_SKIPPED_LINE_CHARS) {
413
+ this.skipped = undefined;
414
+ this.onOverflow();
415
+ }
416
+ return;
417
+ }
418
+ this.onSkipped(this.skipped + end);
419
+ this.skipped = undefined;
420
+ rest = rest.slice(end + 1);
421
+ continue;
422
+ }
423
+ this.buffer += rest;
424
+ rest = "";
425
+ let newline = this.buffer.indexOf("\n");
426
+ while (newline >= 0) {
427
+ const line = this.buffer.slice(0, newline);
428
+ this.buffer = this.buffer.slice(newline + 1);
429
+ if (line.length > MAX_LINE_CHARS) this.onSkipped(line.length);
430
+ else await this.onLine(line);
431
+ newline = this.buffer.indexOf("\n");
432
+ }
433
+ if (this.buffer.length > MAX_LINE_CHARS) {
434
+ this.skipped = this.buffer.length;
435
+ this.buffer = "";
436
+ }
389
437
  }
390
438
  }
391
439
 
392
440
  async end(): Promise<void> {
441
+ if (this.skipped !== undefined) {
442
+ const chars = this.skipped;
443
+ this.skipped = undefined;
444
+ this.onSkipped(chars);
445
+ }
393
446
  if (this.buffer.length === 0) return;
394
447
  const rest = this.buffer;
395
448
  this.buffer = "";
396
- await this.onLine(rest);
449
+ if (rest.length > MAX_LINE_CHARS) this.onSkipped(rest.length);
450
+ else await this.onLine(rest);
397
451
  }
398
452
  }
399
453
 
@@ -479,6 +533,7 @@ export async function runAgent(
479
533
  const clock = options.clock ?? systemClock;
480
534
  let watchedLine: (line: string) => void = () => {};
481
535
  let onLineOverflow: () => void = () => {};
536
+ let skippedLine: (chars: number) => void = () => {};
482
537
  const reply = (text: string): boolean => {
483
538
  const pipe = child.stdin;
484
539
  if (options.interactive !== true || pipe === null || pipe.destroyed) return false;
@@ -490,6 +545,7 @@ export async function runAgent(
490
545
  watchedLine(line);
491
546
  return options.onLine(line, reply);
492
547
  },
548
+ (chars) => skippedLine(chars),
493
549
  () => onLineOverflow(),
494
550
  );
495
551
  const stderrTail = new Tail(undefined);
@@ -598,6 +654,11 @@ export async function runAgent(
598
654
  });
599
655
  watchedLine = (line) => watch.line(line);
600
656
  onLineOverflow = () => watch.interrupt("line");
657
+ skippedLine = (chars) => {
658
+ // A dropped line is still output: the stall timer restarts.
659
+ watch.line("");
660
+ options.onSkipped?.(chars);
661
+ };
601
662
  watch.start();
602
663
 
603
664
  const startup = await Promise.race<Startup>([
@@ -821,6 +882,10 @@ async function runKeptSocket(
821
882
  resultResolve();
822
883
  }
823
884
  },
885
+ (chars) => {
886
+ watch.line("");
887
+ options.onSkipped?.(chars);
888
+ },
824
889
  () => watch.interrupt("line"),
825
890
  );
826
891
  socket.once("close", () => {
@@ -964,6 +1029,10 @@ async function runKeptFresh(
964
1029
  resultResolve();
965
1030
  }
966
1031
  },
1032
+ (chars) => {
1033
+ watch.line("");
1034
+ options.onSkipped?.(chars);
1035
+ },
967
1036
  () => watch.interrupt("line"),
968
1037
  );
969
1038
  outputDrained = consume(stdout, lines).then(() => lines.end());
package/src/step.ts CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  import { directoryError } from "@operad-io/engine";
8
8
  import type { AgentResultData, CostQuality, PluginStepResult } from "@operad-io/engine";
9
- import { MAX_LINE_CHARS, type AgentRunOutcome } from "./process.ts";
9
+ import { MAX_SKIPPED_LINE_CHARS, type AgentRunOutcome } from "./process.ts";
10
10
  import { standardUsage, type RawCounts, type UsageSource } from "./usage.ts";
11
11
  import { isRecord } from "@operad-io/engine";
12
12
 
@@ -301,7 +301,7 @@ export function resultFor(
301
301
  if (outcome.lineTooLong) {
302
302
  return fail({
303
303
  reason: "line_too_long",
304
- message: `the harness printed a line longer than ${MAX_LINE_CHARS} characters`,
304
+ message: `the harness printed a line longer than ${MAX_SKIPPED_LINE_CHARS} characters without ending`,
305
305
  });
306
306
  }
307
307
 
@@ -12,7 +12,7 @@ import { answerGrant } from "./grants.ts";
12
12
  import { inboxOf } from "./inbox.ts";
13
13
  import { JsonlLog } from "./jsonl-log.ts";
14
14
  import { deliverLine } from "./lines.ts";
15
- import { runAgent, type KeptRunOptions, type ToolEvent } from "./process.ts";
15
+ import { runAgent, skippedLineRecord, type KeptRunOptions, type ToolEvent } from "./process.ts";
16
16
  import { resultFor, resultValue, type AgentWorkerResult } from "./step.ts";
17
17
  import { withTimings } from "./timings.ts";
18
18
  import type { Clock } from "@operad-io/engine/platform";
@@ -86,6 +86,9 @@ export async function runWorkerStep(spec: WorkerStepSpec): Promise<PluginStepRes
86
86
  signal: AbortSignal.any([ctx.signal, stopper.signal]),
87
87
  clock: spec.clock,
88
88
  toolEvent: (line) => spec.toolEvent(line) ?? askToolEvent(line),
89
+ onSkipped: (chars) => {
90
+ void jsonl.pushStamped(jsonl.stamp(skippedLineRecord(chars)));
91
+ },
89
92
  async onLine(line, reply) {
90
93
  const received = jsonl.stamp(line);
91
94
  timings.record(received.value, received.at);