operon-agents-core 0.1.0-alpha.0 → 0.1.0-alpha.2

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.
@@ -65,7 +65,15 @@ class SdkMcpTransport {
65
65
  await this.requireClient().ping(buildRequestOptions(options?.timeoutMs, options?.signal));
66
66
  }
67
67
  async callTool(toolName, args, options) {
68
- return this.requireClient().callTool({ name: toolName, arguments: args, _meta: options?.meta ?? null }, undefined, buildRequestOptions(this.cfg.toolTimeoutMs, options?.signal));
68
+ // `arguments` and `_meta` are `.optional()` in the MCP schemas, which rejects an explicit
69
+ // `null`. Sending one makes the server fail JSON-RPC validation (-32700 parse error) before
70
+ // the tool ever runs, so omit the keys entirely instead.
71
+ const params = { name: toolName };
72
+ if (args !== null)
73
+ params.arguments = args;
74
+ if (options?.meta != null)
75
+ params._meta = options.meta;
76
+ return this.requireClient().callTool(params, undefined, buildRequestOptions(this.cfg.toolTimeoutMs, options?.signal));
69
77
  }
70
78
  async listResources(params) {
71
79
  return this.requireClient().listResources(params);
@@ -24,6 +24,10 @@ export interface LlmAutoApproverOptions {
24
24
  readonly stage2MaxTokens?: number;
25
25
  /** Consecutive model failures before short-circuiting to escalate (skips the call). Default 3. */
26
26
  readonly maxConsecutiveErrors?: number;
27
+ /** How long a tripped breaker holds before letting one probe call through. Default 30s. */
28
+ readonly breakerProbeDelayMs?: number;
29
+ /** Cap on the breaker's probe backoff, which doubles per failed probe. Default 5min. */
30
+ readonly breakerMaxProbeDelayMs?: number;
27
31
  /** Telemetry sink. Invoked once per classify with the outcome. */
28
32
  readonly onOutcome?: (report: AutoApprovalReport) => void;
29
33
  }
@@ -44,9 +48,26 @@ export declare class LlmAutoApprover implements AutoApprover {
44
48
  private readonly options;
45
49
  private systemPrompt;
46
50
  private consecutiveErrors;
51
+ private lastErrorAt;
47
52
  constructor(options?: LlmAutoApproverOptions);
48
53
  private system;
49
54
  classify(input: AutoApprovalInput): Promise<AutoApprovalVerdict>;
55
+ /**
56
+ * Whether the tripped breaker is still holding this call back.
57
+ *
58
+ * Once the model has failed `maxConsecutiveErrors` times running, asking it again mostly buys
59
+ * latency for an answer we expect to fail — so the breaker trips and `classify` escalates
60
+ * without a call. Staying tripped forever would be a one-way door: the only line that clears
61
+ * the breaker lives inside `runStage`, which a permanently-open breaker never reaches. A
62
+ * transient outage would then silently demote `auto` to `manual` for the rest of the process,
63
+ * long after the provider recovered, with a restart the only way out.
64
+ *
65
+ * So it half-opens instead: after a backoff, one call is let through to probe. The probe
66
+ * succeeds and `runStage` clears the breaker; it fails and the breaker trips again with twice
67
+ * the delay, up to the cap — the outage costs one call per backoff window rather than one per
68
+ * tool call.
69
+ */
70
+ private breakerHolding;
50
71
  /** Run one classifier stage and return its text content. Throws on a stream error. */
51
72
  private runStage;
52
73
  }
@@ -8,6 +8,8 @@ const XML_S2_SUFFIX = "\nReview the classification process and follow it careful
8
8
  const DEFAULT_STAGE1_MAX_TOKENS = 512;
9
9
  const DEFAULT_STAGE2_MAX_TOKENS = 4096;
10
10
  const DEFAULT_MAX_CONSECUTIVE_ERRORS = 3;
11
+ const DEFAULT_BREAKER_PROBE_DELAY_MS = 30_000;
12
+ const DEFAULT_BREAKER_MAX_PROBE_DELAY_MS = 5 * 60_000;
11
13
  const ALLOW = { decision: "allow" };
12
14
  function textOf(message) {
13
15
  return message.content
@@ -32,6 +34,7 @@ export class LlmAutoApprover {
32
34
  options;
33
35
  systemPrompt;
34
36
  consecutiveErrors = 0;
37
+ lastErrorAt = 0;
35
38
  constructor(options = {}) {
36
39
  this.options = options;
37
40
  }
@@ -55,9 +58,7 @@ export class LlmAutoApprover {
55
58
  report("no-relevance");
56
59
  return { decision: "allow", reason: "Tool declares no security-relevant input" };
57
60
  }
58
- // Circuit breaker: the model has failed N times running — stop paying latency to ask it and
59
- // just escalate. Reset on the next successful response.
60
- if (this.consecutiveErrors >= (this.options.maxConsecutiveErrors ?? DEFAULT_MAX_CONSECUTIVE_ERRORS)) {
61
+ if (this.breakerHolding()) {
61
62
  return escalate("error", "Auto-approval judge repeatedly unavailable — asking for confirmation.");
62
63
  }
63
64
  const actionLine = `${JSON.stringify({ [toolName]: action })}\n`;
@@ -97,10 +98,37 @@ export class LlmAutoApprover {
97
98
  if (signal.aborted)
98
99
  return escalate("aborted", "Auto-approval judge aborted — asking for confirmation.");
99
100
  this.consecutiveErrors += 1;
101
+ this.lastErrorAt = Date.now();
100
102
  const reason = error instanceof Error ? error.message : String(error);
101
103
  return escalate("error", `Auto-approval judge unavailable (${reason}) — asking for confirmation.`);
102
104
  }
103
105
  }
106
+ /**
107
+ * Whether the tripped breaker is still holding this call back.
108
+ *
109
+ * Once the model has failed `maxConsecutiveErrors` times running, asking it again mostly buys
110
+ * latency for an answer we expect to fail — so the breaker trips and `classify` escalates
111
+ * without a call. Staying tripped forever would be a one-way door: the only line that clears
112
+ * the breaker lives inside `runStage`, which a permanently-open breaker never reaches. A
113
+ * transient outage would then silently demote `auto` to `manual` for the rest of the process,
114
+ * long after the provider recovered, with a restart the only way out.
115
+ *
116
+ * So it half-opens instead: after a backoff, one call is let through to probe. The probe
117
+ * succeeds and `runStage` clears the breaker; it fails and the breaker trips again with twice
118
+ * the delay, up to the cap — the outage costs one call per backoff window rather than one per
119
+ * tool call.
120
+ */
121
+ breakerHolding() {
122
+ const max = this.options.maxConsecutiveErrors ?? DEFAULT_MAX_CONSECUTIVE_ERRORS;
123
+ if (this.consecutiveErrors < max)
124
+ return false;
125
+ const base = this.options.breakerProbeDelayMs ?? DEFAULT_BREAKER_PROBE_DELAY_MS;
126
+ const cap = this.options.breakerMaxProbeDelayMs ?? DEFAULT_BREAKER_MAX_PROBE_DELAY_MS;
127
+ // The failure that trips the breaker waits `base`; each failed probe after it waits twice
128
+ // as long. Overflowing to Infinity at absurd error counts is fine — `min` pins it to `cap`.
129
+ const delay = Math.min(base * 2 ** (this.consecutiveErrors - max), cap);
130
+ return Date.now() - this.lastErrorAt < delay;
131
+ }
104
132
  /** Run one classifier stage and return its text content. Throws on a stream error. */
105
133
  async runStage(model, userText, maxTokens, signal) {
106
134
  const request = {
@@ -150,6 +150,12 @@ export class BaseMachine {
150
150
  throw new Error(`${this.name}: a backend with no process SPI must override run().`);
151
151
  }
152
152
  const proc = await this.spawn(this.withCwdArgv(argv, options.cwd), options.env);
153
+ // A child that exits without draining stdin makes the write fail with EPIPE, and that
154
+ // failure arrives as an 'error' event rather than a throw — the try/catch below cannot see
155
+ // it. With no listener node treats it as an uncaught exception and takes the whole process
156
+ // down, which is how a hook script that ignores its stdin killed a CI run. The command's
157
+ // exit code and output are the real result, so a stdin that nobody read is not our error.
158
+ proc.stdin.on("error", () => { });
153
159
  if (options.stdin !== undefined)
154
160
  proc.stdin.write(options.stdin);
155
161
  try {
package/package.json CHANGED
@@ -1,8 +1,11 @@
1
1
  {
2
2
  "name": "operon-agents-core",
3
- "version": "0.1.0-alpha.0",
3
+ "version": "0.1.0-alpha.2",
4
4
  "description": "Internal engine for operon-agents. Prefer operon-agents for application code.",
5
5
  "type": "module",
6
+ "engines": {
7
+ "node": ">=22"
8
+ },
6
9
  "license": "MIT",
7
10
  "repository": {
8
11
  "type": "git",