Skip to content

V2FIX-12: ensure factory one-shot commands self-exit#267

Merged
kjgbot merged 1 commit into
mainfrom
factory-sdk-v2fix12-sb-impl2
Jun 13, 2026
Merged

V2FIX-12: ensure factory one-shot commands self-exit#267
kjgbot merged 1 commit into
mainfrom
factory-sdk-v2fix12-sb-impl2

Conversation

@kjgbot

@kjgbot kjgbot commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

Summary

  • add an idempotent FleetClient dispose lifecycle that intentionally disconnects the harness broker connection and clears pending delivery timers
  • dispose one-shot CLI fleet clients from a finally path, including partial event-subscription setup failures
  • make factory reap-orphans bypass mount/factory construction so the scheduled reaper uses only local registry/heartbeat state plus the local broker fleet

V0 coverage

  • InternalFleetClient dispose unsubscribes broker listeners, calls harness disconnect(), clears pending wait timers, and is idempotent
  • partial connect throw leaves a simulated half-open event stream, then dispose closes it without double-dispose failure
  • run-once operator path disposes the fleet on completion
  • run-once construction failure still disposes the fleet from finally
  • reap-orphans does not call cloudMountFromConfig / build a mount and still disposes the fleet

Verification

  • npx vitest run packages/factory-sdk/src/fleet/internal-fleet-client.test.ts packages/factory-sdk/src/cli/fleet.test.ts
  • npx vitest run packages/factory-sdk
  • npm run typecheck:node -- --pretty false
  • npm run typecheck:web -- --pretty false
  • git diff --check

V1 scope

fv2 should prove the operator artifact with real-fleet factory reap-orphans in a tight loop: exit 0 every invocation, zero hung node processes, broker/roster untouched. This PR makes that V1 cloud-independent by keeping reap-orphans off the cloud mount path.

@gemini-code-assist

Copy link
Copy Markdown

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@coderabbitai

coderabbitai Bot commented Jun 13, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 6a78e22d-b34b-4ef1-8a58-ad3dad7610c8

📥 Commits

Reviewing files that changed from the base of the PR and between 95fb9f4 and cfb87ed.

📒 Files selected for processing (4)
  • packages/factory-sdk/src/cli/fleet.test.ts
  • packages/factory-sdk/src/cli/fleet.ts
  • packages/factory-sdk/src/fleet/internal-fleet-client.test.ts
  • packages/factory-sdk/src/fleet/internal-fleet-client.ts
🚧 Files skipped from review as they are similar to previous changes (4)
  • packages/factory-sdk/src/fleet/internal-fleet-client.ts
  • packages/factory-sdk/src/fleet/internal-fleet-client.test.ts
  • packages/factory-sdk/src/cli/fleet.ts
  • packages/factory-sdk/src/cli/fleet.test.ts

📝 Walkthrough

Walkthrough

Adds a FleetClient.dispose lifecycle method, implements idempotent disposal in InternalFleetClient (unsubscribes, rejects pending waits, optional driver disconnect), integrates disposal into runFleetCli and FactoryLoop.stop, and expands fakes/tests to cover disposal and partial-connect failure paths.

Changes

Fleet client lifecycle disposal

Layer / File(s) Summary
FleetClient interface contract
packages/factory-sdk/src/ports/fleet.ts
The public FleetClient interface adds dispose(): Promise<void> as a lifecycle method.
InternalFleetClient lifecycle implementation
packages/factory-sdk/src/fleet/internal-fleet-client.ts
Adds #eventUnsubscribers, #disposed guard, stores unsubscriber callbacks from broker registrations, throws on subscription when disposed, and implements idempotent dispose() to reject pending injected waits, unsubscribe handlers, clear state, and call the driver's optional disconnect.
Test double enhancements for disposal scenarios
packages/factory-sdk/src/fleet/internal-fleet-client.test.ts
Fake harness client adds disconnectCalls, throwOnConnect, and partiallyConnected state; connectEvents() can throw after partial connect; disconnect() increments calls and clears partial state.
InternalFleetClient disposal tests
packages/factory-sdk/src/fleet/internal-fleet-client.test.ts
Adds tests for disposal idempotency, post-dispose subscription rejection and pending-wait rejection, and safe disposal when partial-connect throws.
RelayFleetClient and FakeFleetClient implementations
packages/factory-sdk/src/fleet/relay-fleet-client.ts, packages/factory-sdk/src/testing/fakes.ts
Adds no-op async dispose() methods to RelayFleetClient and FakeFleetClient.
CLI disposal integration in runFleetCli
packages/factory-sdk/src/cli/fleet.ts
Stores built fleet in an outer variable, special-cases factory reap-orphans to run with the shared fleet, and awaits fleet.dispose() in a finally block, writing disposal errors to stderr.
CLI disposal behavior tests
packages/factory-sdk/src/cli/fleet.test.ts
Adds runtime tests asserting disposal is called once in dry-run, that disposal rejection is emitted to stderr while returning exit code 0, that event-stream construction errors return exit code 1 and still dispose, and that reap-orphans uses injected createFleet/mock cloud mount and disposes fleet once.
FactoryLoop shutdown disposal
packages/factory-sdk/src/orchestrator/factory.ts
FactoryLoop.stop() now calls await this.#fleet.dispose() during shutdown.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • AgentWorkforce/pear#229: Scaffolds FleetClient surface; this PR extends it by adding dispose() and implementations/tests.
  • AgentWorkforce/pear#232: Related internal fleet client and event plumbing changes; this PR builds disposal behavior on that plumbing.
  • AgentWorkforce/pear#253: Overlaps in reap-orphans CLI wiring and behavior that this PR adjusts to use the instantiated fleet.

Poem

🐰 I hopped through code to tidy tracks,
Subscribed no more, closed all the stacks,
Dispose called once, then silence fell,
Orphans reaped, all quiet and well,
A rabbit's wink, resources back.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding disposal lifecycle and ensuring one-shot commands self-exit by disposing fleet clients.
Description check ✅ Passed The description clearly relates to the changeset, providing a structured summary of the three main objectives, V0 coverage details, and verification steps.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch factory-sdk-v2fix12-sb-impl2

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install failed due to a network error.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/factory-sdk/src/cli/fleet.ts`:
- Around line 132-134: The finally block awaiting fleet?.dispose() can throw and
override runFleetCli()’s numeric exit-code behavior; wrap the dispose call in
its own try/catch so disposal errors are caught, logged (or written to stderr)
but not rethrown, ensuring runFleetCli() still returns the deterministic numeric
code from the main flow. Specifically, in the finally where you call await
fleet?.dispose(), change it to a guarded pattern: if (fleet) try { await
fleet.dispose() } catch (err) { /* log error via process.stderr or
processLogger.error but do not throw */ } so any dispose() failure won’t reject
runFleetCli().

In `@packages/factory-sdk/src/fleet/internal-fleet-client.ts`:
- Around line 238-240: The dispose() flow currently returns early but
`#ensureEventSubscription`() can still reattach listeners later; modify dispose()
(method dispose) to set this.#disposed = true immediately at the start and
ensure it removes/cleans up listeners, and update `#ensureEventSubscription`() to
check this.#disposed and no-op if true so methods like waitForInjected,
onAgentExit, and onDeliveryFailed cannot recreate subscriptions after disposal;
apply the same guard to any other subscription-creation helpers referenced in
the same block (the code around the existing subscription setup in
ensureEventSubscription and the event-handler registration paths).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 4fd20b9d-f915-4c86-a30b-2ccde2ec6b09

📥 Commits

Reviewing files that changed from the base of the PR and between 119d425 and 95fb9f4.

📒 Files selected for processing (8)
  • packages/factory-sdk/src/cli/fleet.test.ts
  • packages/factory-sdk/src/cli/fleet.ts
  • packages/factory-sdk/src/fleet/internal-fleet-client.test.ts
  • packages/factory-sdk/src/fleet/internal-fleet-client.ts
  • packages/factory-sdk/src/fleet/relay-fleet-client.ts
  • packages/factory-sdk/src/orchestrator/factory.ts
  • packages/factory-sdk/src/ports/fleet.ts
  • packages/factory-sdk/src/testing/fakes.ts

Comment thread packages/factory-sdk/src/cli/fleet.ts
Comment thread packages/factory-sdk/src/fleet/internal-fleet-client.ts
agent-relay-code Bot added a commit that referenced this pull request Jun 13, 2026
@agent-relay-code

Copy link
Copy Markdown
Contributor

Implemented two fixes from CodeRabbit and verified them locally.

Changes:

  • packages/factory-sdk/src/cli/fleet.ts: disposal errors are now caught and written to stderr so runFleetCli() still resolves to the command’s numeric exit code.
  • packages/factory-sdk/src/fleet/internal-fleet-client.ts: disposed clients now reject event subscription attempts instead of reattaching listeners after cleanup.
  • Added focused regressions in:
    • packages/factory-sdk/src/cli/fleet.test.ts
    • packages/factory-sdk/src/fleet/internal-fleet-client.test.ts

Addressed comments

  • coderabbitai[bot]: runFleetCli() could reject if fleet.dispose() threw, breaking the numeric CLI exit-code contract; fixed in packages/factory-sdk/src/cli/fleet.ts:133 and covered in packages/factory-sdk/src/cli/fleet.test.ts:207.
  • coderabbitai[bot]: InternalFleetClient could re-subscribe after disposal and then never be cleaned up; fixed in packages/factory-sdk/src/fleet/internal-fleet-client.ts:262 and covered in packages/factory-sdk/src/fleet/internal-fleet-client.test.ts:383.
  • coderabbitai[bot]: top-level review summary listed the same two actionable inline comments; both are handled at the file/line references above.
  • coderabbitai[bot]: initial “processing new changes” comment; stale/non-actionable because the completed review later posted the actionable comments above.
  • gemini-code-assist[bot]: quota-limit warning only; no code issue raised.

Advisory Notes

None.

Verification

Passed locally:

  • npm run verify:mcp-resources-drift
  • npm run lint with existing warning-only baseline
  • npm run typecheck:web
  • npm run typecheck:node
  • npx vitest run packages/factory-sdk/src/fleet/internal-fleet-client.test.ts packages/factory-sdk/src/cli/fleet.test.ts packages/factory-sdk/src/orchestrator/factory.test.ts
  • npm test
  • npx vitest run
  • npm run build
  • npm run build:web
  • npx playwright test --config playwright.fidelity.config.ts
  • npx playwright test --config playwright.redraw.config.ts

The first parallel Playwright/build attempt hit local resource pressure, but the same commands passed when rerun sequentially.

@kjgbot kjgbot force-pushed the factory-sdk-v2fix12-sb-impl2 branch from cfb87ed to 95fb9f4 Compare June 13, 2026 02:02
@kjgbot kjgbot merged commit 6e430e1 into main Jun 13, 2026
8 checks passed
@kjgbot kjgbot deleted the factory-sdk-v2fix12-sb-impl2 branch June 13, 2026 02:09
@agent-relay-code

Copy link
Copy Markdown
Contributor

Implemented one additional fix in the PR scope:

FactoryLoop.stop() no longer disposes the injected fleet client. stop() now unwires/releases runtime state while keeping the factory restartable; dispose() performs final fleet disposal in a finally block. Fixed in factory.ts.

Added a regression using InternalFleetClient so the no-op fake cannot hide this: stop/start keeps the harness connected, and dispose() disconnects once. Covered in factory.test.ts.

Addressed comments

  • coderabbitai[bot]: runFleetCli() disposal errors could override numeric exit-code behavior; stale, already handled in current checkout at packages/factory-sdk/src/cli/fleet.ts:132 with regression coverage in packages/factory-sdk/src/cli/fleet.test.ts.
  • coderabbitai[bot]: InternalFleetClient could re-subscribe after disposal; stale, already handled in current checkout at packages/factory-sdk/src/fleet/internal-fleet-client.ts:262 with regression coverage in packages/factory-sdk/src/fleet/internal-fleet-client.test.ts.
  • coderabbitai[bot]: top-level review summary repeated the same two actionable inline comments; both were already present in current checkout.
  • coderabbitai[bot]: processing/walkthrough and docstring coverage warning; no code action, docstring suggestion is advisory/out of scope for this lifecycle PR.
  • gemini-code-assist[bot]: quota-limit warning only; no code issue raised.
  • agent-relay-code[bot]: prior summary of CodeRabbit fixes; no new issue raised.

Advisory Notes

None.

Local Verification

Ran successfully:

  • npm ci
  • npm run verify:mcp-resources-drift
  • npm run lint with warning-only baseline
  • npm run typecheck:web
  • npm run typecheck:node
  • npm test
  • npx vitest run
  • npm run build
  • npm run build:web
  • npx playwright install --with-deps chromium
  • npx playwright test --config playwright.fidelity.config.ts
  • npx playwright test --config playwright.redraw.config.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant