@operad-io/harness-process 0.1.1 → 0.2.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/src/inbox.ts CHANGED
@@ -4,11 +4,13 @@
4
4
  * `dev` hands a harness step an object under `params.inbox` with one method,
5
5
  * `listen(handler)`. The handler gets each message the host sends, in order,
6
6
  * including the ones sent before it listened; the returned function stops
7
- * listening. A harness that can take a message mid-run listens and passes the
8
- * text on; one that cannot ignores the field and the run goes on without it.
7
+ * listening. The handler answers false when the run can no longer take the
8
+ * message, so the host does not count it as taken. A harness that can take a
9
+ * message mid-run listens and passes the text on; one that cannot ignores the
10
+ * field and the run goes on without it.
9
11
  */
10
12
  export interface MessageInbox {
11
- listen(handler: (text: string) => void): () => void;
13
+ listen(handler: (text: string) => boolean | void): () => void;
12
14
  /**
13
15
  * Tells the host the run has begun and how long it may last, in
14
16
  * milliseconds, so it can time what it says from the run's own start.
package/src/index.ts CHANGED
@@ -6,18 +6,32 @@
6
6
  */
7
7
 
8
8
  export * from "./acp.ts";
9
+ export * from "./agent-settings.ts";
10
+ export * from "./agent-step.ts";
9
11
  export * from "./always-allow.ts";
10
12
  export * from "./ask-call.ts";
11
13
  export * from "./ask-note.ts";
12
14
  export * from "./event-note.ts";
15
+ export * from "./flows.ts";
13
16
  export * from "./grants.ts";
14
17
  export * from "./images.ts";
15
18
  export * from "./inbox.ts";
16
19
  export * from "./fold-lines.ts";
17
20
  export * from "./jsonl-log.ts";
21
+ export * from "./keep.ts";
18
22
  export * from "./lines.ts";
19
23
  export * from "./operad-tools.ts";
20
24
  export * from "./process.ts";
25
+ export * from "./probe-cache.ts";
26
+ export * from "./question-form.ts";
27
+ export * from "./refusal.ts";
28
+ export * from "./skills.ts";
21
29
  export * from "./step.ts";
30
+ export * from "./timings.ts";
22
31
  export * from "./limit.ts";
32
+ export * from "./tool-events.ts";
23
33
  export * from "./usage.ts";
34
+ export * from "./version.ts";
35
+ export * from "./wire.ts";
36
+ export * from "./worker-entry.ts";
37
+ export * from "./worker-step.ts";
package/src/jsonl-log.ts CHANGED
@@ -4,7 +4,7 @@
4
4
  * a harness that outruns the disk cannot queue writes without limit.
5
5
  */
6
6
 
7
- import type { PluginLogger } from "@operad-io/engine";
7
+ import { isRecord, type PluginLogger } from "@operad-io/engine";
8
8
  import { systemClock, type Clock } from "@operad-io/engine/platform";
9
9
 
10
10
  /** How much text one write carries before the caller must wait for it. */
@@ -13,6 +13,33 @@ export const FLUSH_CHARS = 16 * 1024;
13
13
  /** The longest a line waits in the buffer before it is written. */
14
14
  export const FLUSH_MS = 250;
15
15
 
16
+ /** The field every logged line carries: when the host received it, in epoch milliseconds. */
17
+ export const RECEIVED_AT = "receivedAt";
18
+
19
+ /** The parsed line when it is one JSON object; undefined for anything else. */
20
+ function objectOf(line: string): Record<string, unknown> | undefined {
21
+ let value: unknown;
22
+ try {
23
+ value = JSON.parse(line);
24
+ } catch {
25
+ return undefined;
26
+ }
27
+ return isRecord(value) ? value : undefined;
28
+ }
29
+
30
+ /** The receipt time already on a parsed line, when it has one. */
31
+ function receivedAtOf(value: Record<string, unknown> | undefined): number | undefined {
32
+ const at = value?.[RECEIVED_AT];
33
+ return typeof at === "number" && Number.isFinite(at) ? at : undefined;
34
+ }
35
+
36
+ /** A line as the host logs it, with the parse kept so a caller need not do it again. */
37
+ export interface StampedLine {
38
+ readonly text: string;
39
+ readonly value: Record<string, unknown> | undefined;
40
+ readonly at: number;
41
+ }
42
+
16
43
  export class JsonlLog {
17
44
  private buffer = "";
18
45
  private timer: unknown = undefined;
@@ -20,18 +47,33 @@ export class JsonlLog {
20
47
  private wrote = false;
21
48
  private readonly logger: PluginLogger;
22
49
  private readonly clock: Clock;
50
+ private readonly now: () => number;
23
51
 
24
- constructor(logger: PluginLogger, clock: Clock = systemClock) {
52
+ constructor(logger: PluginLogger, clock: Clock = systemClock, now: () => number = Date.now) {
25
53
  this.logger = logger;
26
54
  this.clock = clock;
55
+ this.now = now;
27
56
  }
28
57
 
29
58
  /**
30
- * Adds one line. Once the buffer is full, returns the write to wait for; the
31
- * caller that awaits it keeps at most one buffered chunk and one write in
32
- * memory. Before that it returns nothing.
59
+ * The line as the host logs it: a JSON object gets `receivedAt` once. The
60
+ * parsed object and the receipt time come back too, so the caller need not
61
+ * parse the line again for its timings or its result.
33
62
  */
34
- push(line: string): Promise<void> | undefined {
63
+ stamp(line: string): StampedLine {
64
+ const value = objectOf(line);
65
+ const existing = receivedAtOf(value);
66
+ const at = existing ?? this.now();
67
+ if (value === undefined || existing !== undefined) return { text: line, value, at };
68
+ return { text: JSON.stringify({ ...value, [RECEIVED_AT]: at }), value, at };
69
+ }
70
+
71
+ /** Adds one line the caller already stamped, returning the write to wait for once the buffer is full. */
72
+ pushStamped(stamped: StampedLine): Promise<void> | undefined {
73
+ return this.add(stamped.text);
74
+ }
75
+
76
+ private add(line: string): Promise<void> | undefined {
35
77
  this.buffer += `${line}\n`;
36
78
  if (this.buffer.length >= FLUSH_CHARS) return this.flush();
37
79
  // A short buffer is written within FLUSH_MS of its first line.