agent-valley 3.0.1 → 3.0.3

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.
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://agent-plugins.org/schemas/1.0.0/plugin.schema.json",
3
3
  "name": "av",
4
- "version": "3.0.1",
4
+ "version": "3.0.3",
5
5
  "description": "Delegate repository goals to Agent Valley and inspect mission progress and verification evidence.",
6
6
  "author": {
7
7
  "name": "Agent Valley"
package/dist/index.js CHANGED
@@ -15922,8 +15922,23 @@ function signalProcessTree(proc, signal) {
15922
15922
  }
15923
15923
  if (error.code !== "ESRCH")
15924
15924
  throw error;
15925
- if (proc.exitCode === null && proc.signalCode == null)
15926
- proc.kill(signal);
15925
+ if (proc.exitCode === null && proc.signalCode == null) {
15926
+ try {
15927
+ proc.kill(signal);
15928
+ } catch (handleError) {
15929
+ if (handleError.code === "EPERM" && absent(-proc.pid) && absent(proc.pid))
15930
+ return;
15931
+ throw handleError;
15932
+ }
15933
+ }
15934
+ }
15935
+ }
15936
+ function absent(pid) {
15937
+ try {
15938
+ process.kill(pid, 0);
15939
+ return false;
15940
+ } catch (error) {
15941
+ return error.code === "ESRCH";
15927
15942
  }
15928
15943
  }
15929
15944
  var init_process_tree = () => {};
@@ -16137,6 +16152,8 @@ function buildSeatbeltProfile(req, home) {
16137
16152
  (allow process-fork)
16138
16153
  (allow process-exec)
16139
16154
  (allow signal (target self))
16155
+ ; Tool cancellation must reach grandchildren; target children covers only direct children.
16156
+ (allow signal (target same-sandbox))
16140
16157
  (allow sysctl-read)
16141
16158
  (allow mach-lookup)
16142
16159
  ${req.agentType === "codex" ? '(allow ipc-posix-shm-read* (ipc-posix-name-prefix "apple.cfprefs."))' : ""}
@@ -16341,13 +16358,26 @@ function buildAgentEnv(agentType, extra = {}, source = process.env) {
16341
16358
  }
16342
16359
  return { ...env, ...extra };
16343
16360
  }
16344
- function waitForExit(proc) {
16361
+ function waitForReap(proc, timeoutMs) {
16362
+ if (proc.exitCode !== null || proc.signalCode != null)
16363
+ return Promise.resolve(true);
16345
16364
  return new Promise((resolve) => {
16346
- if (proc.exitCode !== null || proc.signalCode != null) {
16347
- resolve();
16348
- return;
16349
- }
16350
- proc.once("close", () => resolve());
16365
+ let settled = false;
16366
+ const finish = (exited) => {
16367
+ if (settled)
16368
+ return;
16369
+ settled = true;
16370
+ clearTimeout(timer);
16371
+ proc.off("exit", onExit);
16372
+ proc.off("close", onExit);
16373
+ resolve(exited);
16374
+ };
16375
+ const onExit = () => finish(true);
16376
+ const timer = setTimeout(() => finish(false), timeoutMs);
16377
+ proc.once("exit", onExit);
16378
+ proc.once("close", onExit);
16379
+ if (proc.exitCode !== null || proc.signalCode != null)
16380
+ onExit();
16351
16381
  });
16352
16382
  }
16353
16383
  function waitForStreamCompletion(proc, graceMs = 250) {
@@ -16397,6 +16427,7 @@ class BaseSession {
16397
16427
  config = null;
16398
16428
  startedAt = 0;
16399
16429
  _process = null;
16430
+ stopping = null;
16400
16431
  terminalEventReceived = false;
16401
16432
  processFailed = false;
16402
16433
  sandboxPlatform = null;
@@ -16404,6 +16435,8 @@ class BaseSession {
16404
16435
  return this._process;
16405
16436
  }
16406
16437
  set process(proc) {
16438
+ if (this._process !== proc)
16439
+ this.stopping = null;
16407
16440
  this._process = proc;
16408
16441
  if (!proc)
16409
16442
  this.sandboxPlatform = null;
@@ -16495,7 +16528,36 @@ class BaseSession {
16495
16528
  }
16496
16529
  async kill() {
16497
16530
  if (this.process)
16498
- signalProcessTree(this.process, "SIGKILL");
16531
+ await this.stopProcess(this.process);
16532
+ }
16533
+ stopProcess(proc) {
16534
+ if (this.stopping?.process === proc)
16535
+ return this.stopping.result;
16536
+ const result = this.terminateProcess(proc);
16537
+ this.stopping = { process: proc, result };
16538
+ return result;
16539
+ }
16540
+ async terminateProcess(proc) {
16541
+ if (!proc.pid)
16542
+ return;
16543
+ let signalError;
16544
+ try {
16545
+ signalProcessTree(proc, "SIGKILL");
16546
+ } catch (error) {
16547
+ if (error.code !== "EPERM")
16548
+ throw error;
16549
+ signalError = error;
16550
+ }
16551
+ if (!await waitForReap(proc, 5000)) {
16552
+ throw signalError ?? new Error(`Agent process ${proc.pid} did not exit within 5000ms after SIGKILL; inspect it before retrying.`);
16553
+ }
16554
+ if (signalError) {
16555
+ try {
16556
+ signalProcessTree(proc, "SIGKILL");
16557
+ } catch {
16558
+ throw signalError;
16559
+ }
16560
+ }
16499
16561
  }
16500
16562
  isAlive() {
16501
16563
  if (!this.process)
@@ -16503,12 +16565,8 @@ class BaseSession {
16503
16565
  return !this.processFailed && this.process.exitCode === null && this.process.signalCode == null;
16504
16566
  }
16505
16567
  async dispose() {
16506
- if (this.process) {
16507
- const wasAlive = this.isAlive();
16508
- signalProcessTree(this.process, "SIGKILL");
16509
- if (wasAlive)
16510
- await waitForExit(this.process);
16511
- }
16568
+ if (this.process)
16569
+ await this.stopProcess(this.process);
16512
16570
  this.listeners.clear();
16513
16571
  this.process = null;
16514
16572
  this.config = null;
@@ -16560,6 +16618,58 @@ var init_base_session = __esm(() => {
16560
16618
  ];
16561
16619
  });
16562
16620
 
16621
+ // ../../packages/core/src/sessions/codex-protocol.ts
16622
+ function closeInputAndJoin(proc) {
16623
+ return new Promise((resolve, reject) => {
16624
+ let inputError;
16625
+ const onInputError = (error) => {
16626
+ inputError = error;
16627
+ };
16628
+ const cleanup = () => {
16629
+ clearTimeout(timer);
16630
+ proc.off("close", onClose);
16631
+ proc.stdin?.off("error", onInputError);
16632
+ };
16633
+ const onClose = () => {
16634
+ cleanup();
16635
+ if (proc.exitCode === 0 && proc.signalCode == null)
16636
+ resolve();
16637
+ else
16638
+ reject(new Error(`Codex shutdown exited with ${proc.signalCode ?? proc.exitCode ?? "unknown"}.`, {
16639
+ cause: inputError
16640
+ }));
16641
+ };
16642
+ const timer = setTimeout(() => {
16643
+ cleanup();
16644
+ reject(new Error("Codex did not exit within 5 seconds of closing its input.", { cause: inputError }));
16645
+ }, 5000);
16646
+ timer.unref();
16647
+ proc.once("close", onClose);
16648
+ proc.stdin?.on("error", onInputError);
16649
+ try {
16650
+ if (!proc.stdin)
16651
+ throw new Error("Codex stdin pipe is unavailable.");
16652
+ proc.stdin.end();
16653
+ } catch (error) {
16654
+ cleanup();
16655
+ reject(error);
16656
+ }
16657
+ });
16658
+ }
16659
+ function extractTokenUsage(params, defaultModel) {
16660
+ if (!params)
16661
+ return;
16662
+ const usage = params.usage;
16663
+ if (!usage)
16664
+ return;
16665
+ const input = usage.input ?? usage.input_tokens ?? usage.prompt_tokens ?? 0;
16666
+ const output = usage.output ?? usage.output_tokens ?? usage.completion_tokens ?? 0;
16667
+ if (input === 0 && output === 0)
16668
+ return;
16669
+ const model = usage.model ?? params.model ?? defaultModel ?? "codex";
16670
+ return { input, output, model };
16671
+ }
16672
+
16563
16673
  // ../../packages/core/src/sessions/json-lines.ts
16564
16674
  function readJsonLines(stream, onEvent, onText) {
16565
16675
  const decoder = new TextDecoder;
@@ -16608,11 +16718,19 @@ var init_codex_session = __esm(() => {
16608
16718
  finalAnswer;
16609
16719
  completedMessage;
16610
16720
  phasedMessageSeen = false;
16721
+ cancellation;
16722
+ shutdown;
16723
+ shutdownFailure;
16724
+ executionPrepared = false;
16725
+ interruptCompletion;
16611
16726
  pendingResolvers = new Map;
16612
16727
  async start(config) {
16613
16728
  this.config = config;
16614
16729
  this.startedAt = Date.now();
16615
16730
  this.disposing = false;
16731
+ this.shutdown = undefined;
16732
+ this.shutdownFailure = undefined;
16733
+ this.executionPrepared = false;
16616
16734
  const args = ["app-server", "--listen", "stdio://"];
16617
16735
  if (config.model) {
16618
16736
  args.push("-c", `model=${JSON.stringify(config.model)}`);
@@ -16636,6 +16754,7 @@ var init_codex_session = __esm(() => {
16636
16754
  async execute(prompt) {
16637
16755
  if (!this.assertStarted())
16638
16756
  return;
16757
+ this.executionPrepared = true;
16639
16758
  this.filesChanged = [];
16640
16759
  this.outputTail = "";
16641
16760
  this.finalAnswer = undefined;
@@ -16661,16 +16780,52 @@ var init_codex_session = __esm(() => {
16661
16780
  this.turnId = turnResult.turn.id;
16662
16781
  }
16663
16782
  async cancel() {
16783
+ if (this.cancellation)
16784
+ return this.cancellation;
16785
+ const cancelling = this.cancelTurn();
16786
+ this.cancellation = cancelling;
16787
+ try {
16788
+ await cancelling;
16789
+ } finally {
16790
+ if (this.cancellation === cancelling)
16791
+ this.cancellation = undefined;
16792
+ }
16793
+ }
16794
+ async cancelTurn() {
16664
16795
  if (this.paused)
16665
16796
  await this.resume();
16666
- if (this.threadId && this.turnId) {
16667
- try {
16668
- await this.rpc("turn/interrupt", { threadId: this.threadId, turnId: this.turnId });
16669
- } catch {
16670
- await super.cancel();
16671
- }
16672
- } else {
16797
+ const threadId = this.threadId;
16798
+ const turnId = this.turnId;
16799
+ if (!threadId || !turnId) {
16800
+ await super.cancel();
16801
+ return;
16802
+ }
16803
+ let rejectCompletion;
16804
+ const completed = new Promise((resolve, reject) => {
16805
+ rejectCompletion = reject;
16806
+ this.interruptCompletion = { threadId, turnId, resolve, reject };
16807
+ });
16808
+ const timer = setTimeout(() => {
16809
+ rejectCompletion(new Error("Codex did not confirm turn completion within 5 seconds of interruption. Inspect and reconcile the task before retrying."));
16810
+ }, 5000);
16811
+ timer.unref();
16812
+ let requestFailed = false;
16813
+ try {
16814
+ await Promise.all([
16815
+ completed,
16816
+ this.rpc("turn/interrupt", { threadId, turnId }).catch((error) => {
16817
+ requestFailed = true;
16818
+ throw error;
16819
+ })
16820
+ ]);
16821
+ } catch (error) {
16822
+ if (!requestFailed || this.disposing)
16823
+ throw error;
16673
16824
  await super.cancel();
16825
+ } finally {
16826
+ clearTimeout(timer);
16827
+ if (this.interruptCompletion?.threadId === threadId && this.interruptCompletion.turnId === turnId)
16828
+ this.interruptCompletion = undefined;
16674
16829
  }
16675
16830
  }
16676
16831
  async pause() {
@@ -16724,9 +16879,43 @@ var init_codex_session = __esm(() => {
16724
16879
  input: [{ type: "text", text }]
16725
16880
  });
16726
16881
  }
16727
- async dispose() {
16882
+ async kill() {
16883
+ if (this.shutdownFailure)
16884
+ throw this.shutdownFailure;
16885
+ if (this.shutdown)
16886
+ return this.shutdown;
16887
+ const proc = this.process;
16888
+ if (!proc)
16889
+ return;
16728
16890
  this.disposing = true;
16729
16891
  this.rejectPending(new Error("Codex session disposed while waiting for an RPC response."));
16892
+ const shuttingDown = (async () => {
16893
+ if (!this.isAlive() && proc.pid && this.executionPrepared) {
16894
+ await this.forceUnconfirmedShutdown(new Error("Codex exited before graceful shutdown was joined. Inspect its command processes and reconcile the task before retrying."));
16895
+ }
16896
+ if (this.isAlive()) {
16897
+ try {
16898
+ await closeInputAndJoin(proc);
16899
+ } catch (error) {
16900
+ await this.forceUnconfirmedShutdown(new Error("Codex graceful shutdown was not confirmed. Inspect its command processes and reconcile the task before retrying.", { cause: error }));
16901
+ }
16902
+ }
16903
+ await super.kill();
16904
+ })();
16905
+ this.shutdown = shuttingDown;
16906
+ return shuttingDown;
16907
+ }
16908
+ async forceUnconfirmedShutdown(error) {
16909
+ this.shutdownFailure = error;
16910
+ try {
16911
+ await super.kill();
16912
+ } catch (forceError) {
16913
+ this.shutdownFailure = new AggregateError([error, forceError], error.message);
16914
+ }
16915
+ throw this.shutdownFailure;
16916
+ }
16917
+ async dispose() {
16918
+ await this.kill();
16730
16919
  await super.dispose();
16731
16920
  this.threadId = null;
16732
16921
  this.turnId = null;
@@ -16777,6 +16966,7 @@ var init_codex_session = __esm(() => {
16777
16966
  });
16778
16967
  }
16779
16968
  rejectPending(error) {
16969
+ this.interruptCompletion?.reject(error);
16780
16970
  for (const pending of this.pendingResolvers.values()) {
16781
16971
  clearTimeout(pending.timer);
16782
16972
  pending.reject(error);
@@ -16784,6 +16974,8 @@ var init_codex_session = __esm(() => {
16784
16974
  this.pendingResolvers.clear();
16785
16975
  }
16786
16976
  handleMessage(msg) {
16977
+ if (this.disposing)
16978
+ return;
16787
16979
  if (msg.id != null && this.pendingResolvers.has(msg.id)) {
16788
16980
  const resolver = this.pendingResolvers.get(msg.id);
16789
16981
  if (!resolver)
@@ -16874,6 +17066,13 @@ var init_codex_session = __esm(() => {
16874
17066
  const turn = msg.params?.turn;
16875
17067
  if (turn?.id && this.turnId && turn.id !== this.turnId)
16876
17068
  return;
17069
+ const interrupt = this.interruptCompletion;
17070
+ if (interrupt?.threadId === this.threadId && interrupt.turnId === turn?.id) {
17071
+ if (turn?.status && ["completed", "interrupted", "failed"].includes(turn.status))
17072
+ interrupt.resolve();
17073
+ else
17074
+ interrupt.reject(new Error("Codex interruption has no valid turn completion status. Inspect the task before retrying."));
17075
+ }
16877
17076
  this.turnId = null;
16878
17077
  if (turn?.status !== "completed") {
16879
17078
  this.emitError("CRASH", turn?.error?.message ?? `Codex turn ${turn?.status ?? "has no completion status"}. Check the agent output and retry.`, true);
@@ -16882,7 +17081,7 @@ var init_codex_session = __esm(() => {
16882
17081
  const output = this.finalAnswer ?? this.completedMessage ?? (this.phasedMessageSeen ? "" : this.outputTail);
16883
17082
  const result = this.buildRunResult(output, this.filesChanged);
16884
17083
  result.exitCode = 0;
16885
- const usage = this.tokenUsage ?? this.extractTokenUsage(msg.params);
17084
+ const usage = this.tokenUsage ?? extractTokenUsage(msg.params, this.config?.model);
16886
17085
  if (usage)
16887
17086
  result.tokenUsage = usage;
16888
17087
  this.emit({ type: "complete", result });
@@ -16902,19 +17101,6 @@ var init_codex_session = __esm(() => {
16902
17101
  this.emit({ type: "heartbeat", timestamp: new Date().toISOString() });
16903
17102
  }
16904
17103
  }
16905
- extractTokenUsage(params) {
16906
- if (!params)
16907
- return;
16908
- const usage = params.usage;
16909
- if (!usage)
16910
- return;
16911
- const input = usage.input ?? usage.input_tokens ?? usage.prompt_tokens ?? 0;
16912
- const output = usage.output ?? usage.output_tokens ?? usage.completion_tokens ?? 0;
16913
- if (input === 0 && output === 0)
16914
- return;
16915
- const model = usage.model ?? params.model ?? this.config?.model ?? "codex";
16916
- return { input, output, model };
16917
- }
16918
17104
  };
16919
17105
  });
16920
17106
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-valley",
3
- "version": "3.0.1",
3
+ "version": "3.0.3",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=26.10.0",