@juanbenjumea/pi-agy 0.6.8 → 0.6.10

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/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.6.10
4
+
5
+ - Validate terminal status only after finalizing the complete captured result, so pretty-printed top-level JSON errors cannot bypass 0.6.9's fail-closed status handling.
6
+ - Recover one bounded trailing root JSON envelope after earlier lifecycle or diagnostic records, preserving terminal status, error, and conversation identity before cancellation, exit-code, or completed-response precedence is decided.
7
+ - Add compact and multiline top-level error regression coverage; independent review found zero blockers and the full 201-test gate passes.
8
+
9
+ ## 0.6.9
10
+
11
+ - Fail closed on agy terminal result statuses: only `SUCCESS` and `OK` are accepted when a status is present. `ERROR`, `FAILURE`, `CANCELLED`, `TIMEOUT`, empty, malformed, and unknown statuses now reject even when agy includes `response: ""` and exits with code 0.
12
+ - Preserve terminal status/error metadata from both stream-json result events and top-level JSON envelopes. Failed status takes precedence over response completeness and delivered-result cancellation handling, while status-absent legacy output remains compatible.
13
+ - Keep terminal diagnostics bounded and control-safe, append bounded stderr so canonical `AGY_ERROR` retryability remains available, and retain conversation IDs on failure for resumable recovery.
14
+ - Add nested/top-level, `SUCCESS`/`OK`, failed/unknown/empty/malformed, exit-zero, and conversation-continuity regression coverage.
15
+
3
16
  ## 0.6.8
4
17
 
5
18
  - Keep agy's internal `--print-timeout` five seconds behind Pi's hard parent deadline. agy 1.1.28+ returns partial output with exit code 0 when its own timeout fires, so Pi must win the race to classify the run as timed out and preserve resumable-conversation guidance.
package/README.md CHANGED
@@ -44,7 +44,9 @@ waits, preflight probes, and post-run summaries all count against it. When
44
44
  it fires, the full agy process group is killed so nested tools cannot
45
45
  outlive the run. agy's own `--print-timeout` is set five seconds later because
46
46
  agy 1.1.28+ returns partial output with exit code 0 when that internal timer
47
- fires; Pi therefore remains the authoritative timeout owner.
47
+ fires; Pi therefore remains the authoritative timeout owner. Terminal result
48
+ envelopes also fail closed: when agy supplies a status, only `SUCCESS` and `OK`
49
+ are accepted, even if a failed result carries an empty response and exits 0.
48
50
 
49
51
  Active tool updates show bounded intent (for example a tool action or async
50
52
  threshold) but never echo full command lines, which may contain secrets.
@@ -1164,11 +1164,24 @@ function spawnAgyInternal(
1164
1164
  runResult = processStreamLine(lineBuffer, runResult, onProgress);
1165
1165
  }
1166
1166
 
1167
- // A fully delivered result outranks the kill: when cancellation or
1168
- // timeout lands after the response arrived, the work is done and the
1169
- // response must not be discarded.
1170
- if (runResult.response_complete) {
1171
- resolve(finalizeRunResult(out, runResult));
1167
+ // Finalization also parses whole top-level JSON envelopes, including
1168
+ // pretty-printed output that cannot be consumed one line at a time.
1169
+ const finalized = finalizeRunResult(out, runResult);
1170
+
1171
+ // Terminal status is authoritative when present. agy can emit an
1172
+ // ERROR envelope with response: "" and even exit 0; response
1173
+ // completeness must never turn that failed turn into success.
1174
+ const terminalFailure = terminalFailureMessage(finalized, err);
1175
+ if (terminalFailure) {
1176
+ reject(withConversationId(terminalFailure, finalized));
1177
+ return;
1178
+ }
1179
+
1180
+ // A fully delivered successful result outranks the kill: when
1181
+ // cancellation or timeout lands after the response arrived, the work
1182
+ // is done and the response must not be discarded.
1183
+ if (finalized.response_complete) {
1184
+ resolve(finalized);
1172
1185
  return;
1173
1186
  }
1174
1187
 
@@ -1176,7 +1189,7 @@ function spawnAgyInternal(
1176
1189
  reject(
1177
1190
  withConversationId(
1178
1191
  signal.aborted && !timedOut ? "agy was cancelled" : "agy timed out",
1179
- runResult,
1192
+ finalized,
1180
1193
  ),
1181
1194
  );
1182
1195
  return;
@@ -1184,13 +1197,12 @@ function spawnAgyInternal(
1184
1197
 
1185
1198
  if (code !== 0) {
1186
1199
  const detail = (err || out).slice(0, 2000).trim();
1187
- reject(withConversationId(`agy exited with code ${code}:\n${detail || "(no output)"}`, runResult));
1200
+ reject(withConversationId(`agy exited with code ${code}:\n${detail || "(no output)"}`, finalized));
1188
1201
  return;
1189
1202
  }
1190
1203
 
1191
1204
  // agy writes diagnostics to stderr even on successful runs. Keep it
1192
1205
  // out of the response so JSON/stream parsing remains deterministic.
1193
- const finalized = finalizeRunResult(out, runResult);
1194
1206
  // Only responses served verbatim from the bounded raw capture can be
1195
1207
  // incomplete; anything parsed from a record was delivered in full.
1196
1208
  if (stdoutTruncated && !finalized.response_complete) {
@@ -1202,6 +1214,24 @@ function spawnAgyInternal(
1202
1214
  });
1203
1215
  }
1204
1216
 
1217
+ const SUCCESSFUL_TERMINAL_STATUSES = new Set(["SUCCESS", "OK"]);
1218
+
1219
+ function terminalFailureMessage(runResult: AgyRunResult, stderr: string): string | undefined {
1220
+ if (runResult.terminal_status === undefined) return undefined;
1221
+ const status = runResult.terminal_status
1222
+ .trim()
1223
+ .replace(/[\u0000-\u001f\u007f-\u009f]/g, "")
1224
+ .slice(0, 64);
1225
+ if (SUCCESSFUL_TERMINAL_STATUSES.has(status.toUpperCase())) return undefined;
1226
+
1227
+ const detail = runResult.terminal_error?.trim();
1228
+ let message = `agy reported terminal status ${status || "(empty)"}`;
1229
+ if (detail) message += `: ${detail}`;
1230
+ const stderrDetail = stderr.trim().slice(0, 2_000);
1231
+ if (stderrDetail && !message.includes(stderrDetail)) message += `\n${stderrDetail}`;
1232
+ return message;
1233
+ }
1234
+
1205
1235
  function withConversationId(message: string, runResult: AgyRunResult): Error {
1206
1236
  const error = new Error(message) as Error & { conversation_id?: string };
1207
1237
  if (runResult.conversation_id) error.conversation_id = runResult.conversation_id;
@@ -39,7 +39,9 @@ export interface AgyStreamLine {
39
39
  * Top-level `--output-format json` envelope fields — that mode emits one
40
40
  * record without a result event, so the fields are honored directly.
41
41
  */
42
+ status?: string;
42
43
  response?: string;
44
+ error?: string;
43
45
  duration_seconds?: number;
44
46
  usage?: AgyUsage;
45
47
  init?: { model?: string; cwd?: string };
@@ -48,6 +50,7 @@ export interface AgyStreamLine {
48
50
  conversation_id?: string;
49
51
  status?: string;
50
52
  response?: string;
53
+ error?: string;
51
54
  duration_seconds?: number;
52
55
  usage?: AgyUsage;
53
56
  };
@@ -57,6 +60,10 @@ export interface AgyRunResult {
57
60
  response: string;
58
61
  /** Internal marker distinguishing a valid empty response from no response. */
59
62
  response_complete?: boolean;
63
+ /** Authoritative terminal status when agy supplied one. */
64
+ terminal_status?: string;
65
+ /** Bounded terminal error detail from the result envelope. */
66
+ terminal_error?: string;
60
67
  conversation_id?: string;
61
68
  usage?: AgyUsage;
62
69
  duration_seconds?: number;
@@ -209,6 +216,12 @@ function capResponse(response: string): string {
209
216
  : response;
210
217
  }
211
218
 
219
+ function capTerminalError(error: string): string {
220
+ return stripTerminalSequences(error)
221
+ .replace(/[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f-\u009f]/g, " ")
222
+ .slice(0, 2_000);
223
+ }
224
+
212
225
  export function accumulateRunResult(parsed: AgyStreamLine, current: AgyRunResult): AgyRunResult {
213
226
  const next = { ...current };
214
227
 
@@ -217,10 +230,14 @@ export function accumulateRunResult(parsed: AgyStreamLine, current: AgyRunResult
217
230
  // `--output-format json` produces one record with top-level envelope
218
231
  // fields; honor them so non-streaming payloads of any size accumulate
219
232
  // in-stream instead of falling back to the bounded raw capture.
233
+ if (Object.hasOwn(parsed, "status")) {
234
+ next.terminal_status = typeof parsed.status === "string" ? parsed.status : "(invalid)";
235
+ }
220
236
  if (typeof parsed.response === "string") {
221
237
  next.response = capResponse(parsed.response);
222
238
  next.response_complete = true;
223
239
  }
240
+ if (typeof parsed.error === "string") next.terminal_error = capTerminalError(parsed.error);
224
241
  if (typeof parsed.duration_seconds === "number" && Number.isFinite(parsed.duration_seconds)) {
225
242
  next.duration_seconds = parsed.duration_seconds;
226
243
  }
@@ -236,10 +253,17 @@ export function accumulateRunResult(parsed: AgyStreamLine, current: AgyRunResult
236
253
  if (typeof parsed.result.conversation_id === "string") {
237
254
  next.conversation_id = parsed.result.conversation_id;
238
255
  }
256
+ if (Object.hasOwn(parsed.result, "status")) {
257
+ next.terminal_status =
258
+ typeof parsed.result.status === "string" ? parsed.result.status : "(invalid)";
259
+ }
239
260
  if (typeof parsed.result.response === "string") {
240
261
  next.response = capResponse(parsed.result.response);
241
262
  next.response_complete = true;
242
263
  }
264
+ if (typeof parsed.result.error === "string") {
265
+ next.terminal_error = capTerminalError(parsed.result.error);
266
+ }
243
267
  if (typeof parsed.result.usage === "object" && parsed.result.usage !== null) {
244
268
  next.usage = parsed.result.usage;
245
269
  }
@@ -265,15 +289,22 @@ export function finalizeRunResult(rawStdout: string, current: AgyRunResult): Agy
265
289
  }
266
290
  }
267
291
 
268
- try {
269
- const parsed: unknown = JSON.parse(rawStdout);
270
- const next = mergeJsonEnvelope(parsed, current);
271
- if (next) return next;
272
- } catch {
273
- // not json
292
+ const trimmed = rawStdout.trim();
293
+ const trailingRoot = trimmed.lastIndexOf("\n{");
294
+ const candidates = trailingRoot >= 0
295
+ ? [trimmed, trimmed.slice(trailingRoot + 1)]
296
+ : [trimmed];
297
+ for (const candidate of candidates) {
298
+ try {
299
+ const parsed: unknown = JSON.parse(candidate);
300
+ const next = mergeJsonEnvelope(parsed, current);
301
+ if (next) return next;
302
+ } catch {
303
+ // Try the bounded trailing root object after earlier stream records.
304
+ }
274
305
  }
275
306
 
276
- return { ...current, response: rawStdout.trim() || "(empty response)" };
307
+ return { ...current, response: trimmed || "(empty response)" };
277
308
  }
278
309
 
279
310
  /** Preserve metadata from agy's non-stream JSON envelope when available. */
@@ -287,11 +318,19 @@ function mergeJsonEnvelope(parsed: unknown, current: AgyRunResult): AgyRunResult
287
318
  next.conversation_id = record.conversation_id;
288
319
  found = true;
289
320
  }
321
+ if (Object.hasOwn(record, "status")) {
322
+ next.terminal_status = typeof record.status === "string" ? record.status : "(invalid)";
323
+ found = true;
324
+ }
290
325
  if (typeof record.response === "string") {
291
- next.response = record.response;
326
+ next.response = capResponse(record.response);
292
327
  next.response_complete = true;
293
328
  found = true;
294
329
  }
330
+ if (typeof record.error === "string") {
331
+ next.terminal_error = capTerminalError(record.error);
332
+ found = true;
333
+ }
295
334
  if (typeof record.duration_seconds === "number") {
296
335
  next.duration_seconds = record.duration_seconds;
297
336
  found = true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@juanbenjumea/pi-agy",
3
- "version": "0.6.8",
3
+ "version": "0.6.10",
4
4
  "description": "Reliable Antigravity delegation for Pi — quota-aware runs, resumable tasks, repo verification, cancellation, and trustworthy diff summaries.",
5
5
  "type": "module",
6
6
  "license": "MIT",