Skip to content

fix(gateway): wire reload handlers and server.impl capacity accounting to getActiveSessionRunCount #2345

@alexey-pelykh

Description

@alexey-pelykh

Summary

src/gateway/server-reload-handlers.ts and src/gateway/server.impl.ts both use getActiveEmbeddedRunCount() to account for in-flight CLI agent runs when computing gateway capacity and deciding whether config reloads are safe. Both sites currently read 0 (literal or stub no-op), causing two regressions:

  1. Config reload safety: createGatewayReloadHandlers line 154 treats active CLI runs as zero, so config reloads proceed mid-run and pick up new config values into in-flight subprocess executions.
  2. Gateway capacity accounting: server.impl.ts:302 computes getTotalQueueSize() + getTotalPendingReplies() + getActiveEmbeddedRunCount() — the CLI subprocess count is silently omitted from the capacity budget. This was flagged in the prior audit comment on audit: review all Pi engine stub replacements for ChannelBridge compatibility #2089 but split out here for tracking.

Tracked from the ChannelBridge compatibility audit on #2089.

Current regressed state

server-reload-handlers.ts:150-158 (GUTTED post-#77):

const getActiveCounts = () => {
  const queueSize = getTotalQueueSize();
  const pendingReplies = getTotalPendingReplies();
  const embeddedRuns = 0;  // ← hard-coded literal
  return { queueSize, pendingReplies, embeddedRuns };
};

server.impl.ts:302 (REGRESSED via v2026.3.7 sync — imports getActiveEmbeddedRunCount from pi-embedded-runner/runs.js, a separate stub file):

const total = getTotalQueueSize() + getTotalPendingReplies() + getActiveEmbeddedRunCount();

Both silently miscount active CLI subprocesses as zero.

Expected behavior

ChannelBridge registers session runs in src/agents/session-run-registry.ts::ACTIVE_SESSION_RUNS via registerSessionRun at channel-bridge.ts:160 and unregisters at channel-bridge.ts:349. The registry exposes getActiveSessionRunCount() returning the map size — this is the correct replacement for getActiveEmbeddedRunCount().

Proposed fix

server-reload-handlers.ts

import { getActiveSessionRunCount } from "../agents/session-run-registry.js";

// Inside createGatewayReloadHandlers(params):
const getActiveCounts = () => {
  const queueSize = getTotalQueueSize();
  const pendingReplies = getTotalPendingReplies();
  const activeRuns = getActiveSessionRunCount();
  return { queueSize, pendingReplies, activeRuns };  // rename from embeddedRuns
};

Rename the field from embeddedRuns to activeRuns across all callers in this file (grep shows it's used in the reload-eligibility check and logging).

server.impl.ts:302

Replace:

import { getActiveEmbeddedRunCount } from "../agents/pi-embedded-runner/runs.js";
// ...
const total = getTotalQueueSize() + getTotalPendingReplies() + getActiveEmbeddedRunCount();

with:

import { getActiveSessionRunCount } from "../agents/session-run-registry.js";
// ...
const total = getTotalQueueSize() + getTotalPendingReplies() + getActiveSessionRunCount();

Then delete src/agents/pi-embedded-runner/runs.ts (the stub file with the return 0 implementation).

Acceptance Criteria

  • server-reload-handlers.ts uses getActiveSessionRunCount from session-run-registry
  • server.impl.ts uses getActiveSessionRunCount from session-run-registry
  • src/agents/pi-embedded-runner/runs.ts stub file deleted
  • Field rename embeddedRunsactiveRuns propagated throughout server-reload-handlers.ts
  • Config reload respects active CLI runs (either defers or blocks reload until registry is empty, per existing reload contract)
  • Gateway capacity totals correctly include active CLI runs
  • pnpm check passes
  • Existing reload/capacity tests updated if they mock getActiveEmbeddedRunCount

Out of Scope

Related

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions