Skip to content

feat: add upstream lifecycle controls - #51

Merged
mohanagy merged 6 commits into
developmentfrom
feat/issue-12-upstream-lifecycle-controls
Jul 11, 2026
Merged

feat: add upstream lifecycle controls#51
mohanagy merged 6 commits into
developmentfrom
feat/issue-12-upstream-lifecycle-controls

Conversation

@mohanagy

@mohanagy mohanagy commented Jul 11, 2026

Copy link
Copy Markdown
Owner

Summary

  • Make process lifecycle controls real: bounded startup/shutdown, idle cleanup, opt-in crash recovery, and typed health outcomes.
  • Enforce no-eviction distinct-profile capacity limits across multi-upstream bundles and serialize lifecycle races.
  • Add real STDIO crash, hang, delayed-shutdown, capacity, and concurrency coverage; document the public process contract.

Validation

  • npm test
  • npm run lint
  • npm run typecheck
  • npm run build
  • npm run check:pack

Refs #12

Summary by CodeRabbit

  • New Features
    • Added configurable upstream process lifecycle controls (shutdown/idle timeouts, restart-on-crash, max restarts, max concurrent profiles) and enhanced health reporting with restart counters, stop reasons, pending recovery time, and restart-limit exhaustion.
    • Improved per-profile restart handling with safer single-flight restarts and more accurate tool/resource/prompt update notifications.
  • Bug Fixes
    • Prevented idle shutdown and stale transport events from impacting active/in-flight requests; strengthened secret redaction in diagnostics while still tracking resolved secret values.
  • Documentation
    • Expanded architecture/config docs to cover the new lifecycle behavior, discovery/retry semantics, and additional health fields.
  • Tests
    • Expanded schema, runtime validation, lifecycle/recovery, concurrency enforcement, and redaction coverage.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The PR adds configurable upstream startup, shutdown, idle, crash-recovery, restart-limit, and concurrency controls. It expands health reporting, coordinates profile restarts, tracks in-flight activity and secrets, and adds documentation, fixtures, and lifecycle tests.

Changes

Upstream lifecycle controls

Layer / File(s) Summary
Configuration contracts and runtime wiring
README.md, docs/..., src/config/..., src/cli/create-runtime.ts, tests/config-*
Process lifecycle options are validated, typed, forwarded to runtime managers, documented, and covered by configuration tests.
Secret-aware configuration resolution
src/config/env-expand.ts, src/secrets/secret-resolver.ts, tests/config.test.ts, tests/config-preflight.test.ts, tests/upstream-manager.test.ts
Environment and secret references collect resolved secret values for redacted diagnostics while preserving resolved configuration maps.
Session activity and profile capacity
src/upstream/profile-session-limiter.ts, src/upstream/multi-upstream-process-manager.ts, src/upstream/upstream-session.ts
Shared profile capacity tracking and operation activity hooks are added to upstream session management.
Process lifecycle and recovery state
src/upstream/upstream-process-manager.ts, tests/upstream-manager.test.ts, tests/fixtures/fake-upstream.mjs, src/utils/errors.ts
Starts, restarts, idle shutdown, crash recovery, shutdown timeouts, cancellation, capacity reservations, secret redaction, and health states are expanded and tested.
Management restart coordination
src/mcp/server/miftah-server.ts
Concurrent restart requests for the same profile share one promise, followed by registry invalidation and active-profile notifications.

Estimated code review effort: 5 (Critical) | ~90+ minutes

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant MiftahServer
  participant UpstreamProcessManager
  participant ProfileSessionLimiter
  participant UpstreamSession
  Client->>MiftahServer: request profile operation
  MiftahServer->>UpstreamProcessManager: get or restart profile
  UpstreamProcessManager->>ProfileSessionLimiter: acquire profile capacity
  UpstreamProcessManager->>UpstreamSession: execute MCP request
  UpstreamSession-->>MiftahServer: return operation result
  UpstreamProcessManager->>UpstreamProcessManager: schedule idle shutdown or recovery
Loading

Possibly related issues

  • Issue 12 — The PR implements the described restart, backoff, idle shutdown, concurrency, timeout, and lifecycle controls.

Possibly related PRs

  • mohanagy/miftah#43 — Both changes modify process configuration validation and lifecycle option handling.

Poem

I’m a rabbit hopping through the start,
With idle clocks and retries smart.
Profiles queue, then safely run,
Crashes yield to a bounded sun.
Health fields bloom; restarts align—
Thump, thump, the lifecycle’s fine!


Caution

Pre-merge checks failed

Please resolve all errors before merging. Addressing warnings is optional.

  • Ignore

❌ Failed checks (1 error)

Check name Status Explanation Resolution
Docstring Coverage ❌ Error Docstring coverage is 25.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
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: adding upstream lifecycle controls.
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.
✨ 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 feat/issue-12-upstream-lifecycle-controls

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

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@mohanagy

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@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: 3

🤖 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 `@docs/architecture.md`:
- Line 15: Update the architecture description to state that each profile owns a
separate SDK Client and transport for each configured upstream, rather than one
client per profile. Clarify that local upstreams use StdioClientTransport, while
remote upstreams use the appropriate SSE/HTTP transport; preserve the existing
lifecycle and concurrency details.

In `@src/upstream/upstream-process-manager.ts`:
- Line 221: Fix the ESLint errors in the shutdown logic by avoiding the
intermediate array spread when mapping over profiles, and update both affected
stop-profile call sites to avoid compiling the same regular expression per
invocation. Use the relevant profile iteration and regex-related symbols in
src/upstream/upstream-process-manager.ts to apply the lint-compliant changes
consistently.
- Around line 381-391: Update the upstream process manager’s redaction flow so
manager-originated output uses a single redactProcessOutput helper backed by
both the dynamically populated secretValuesSet and options.secretValues. Replace
direct redactSecrets usage in attachStderr and apply the same helper when
normalizing capability and startup failures, ensuring resolved
environment/header credentials are removed from all output.
🪄 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: ASSERTIVE

Plan: Pro Plus

Run ID: 83fbdaea-fc4a-465e-9b90-6318a471a604

📥 Commits

Reviewing files that changed from the base of the PR and between a118533 and d4e246e.

📒 Files selected for processing (17)
  • README.md
  • docs/architecture.md
  • docs/config.md
  • src/cli/create-runtime.ts
  • src/config/schema.ts
  • src/config/types.ts
  • src/mcp/server/miftah-server.ts
  • src/upstream/multi-upstream-process-manager.ts
  • src/upstream/profile-session-limiter.ts
  • src/upstream/upstream-process-manager.ts
  • src/upstream/upstream-session.ts
  • src/utils/errors.ts
  • tests/config-preflight.test.ts
  • tests/config-runtime-parity.test.ts
  • tests/config-schema-contract.test.ts
  • tests/fixtures/fake-upstream.mjs
  • tests/upstream-manager.test.ts

Comment thread docs/architecture.md Outdated
Comment thread src/upstream/upstream-process-manager.ts Outdated
Comment thread src/upstream/upstream-process-manager.ts Outdated
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@mohanagy

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@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: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/upstream/upstream-process-manager.ts (1)

566-613: 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Keep shutdown best-effort on close failures closeSession only special-cases UPSTREAM_SHUTDOWN_TIMEOUT; any other rejection from entry.session.close() will short-circuit stopProfile before limiter.release(...) and the "stopped" transition. On idle shutdown, that rejection also bubbles out of void this.closeIdleSession(...). Treat close errors like the timeout path so finalization always runs.

🤖 Prompt for 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.

In `@src/upstream/upstream-process-manager.ts` around lines 566 - 613, Update
closeSession to treat any rejection from entry.session.close() as a best-effort
shutdown failure: force-terminate the transport, await the close promise’s
settled result, and return true instead of rethrowing, while preserving the
existing timeout handling. This ensures stopProfile always releases reservations
and transitions to stopped, including callers such as closeIdleSession.
🤖 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 `@src/secrets/secret-resolver.ts`:
- Line 75: Hoist the inline environment-reference regex from
resolveValueWithSecretValues to module scope and reuse the shared constant when
matching values. Also hoist the global placeholder regex used by the
default-branch replace into a module-level constant, then update both call sites
to use these constants.

---

Outside diff comments:
In `@src/upstream/upstream-process-manager.ts`:
- Around line 566-613: Update closeSession to treat any rejection from
entry.session.close() as a best-effort shutdown failure: force-terminate the
transport, await the close promise’s settled result, and return true instead of
rethrowing, while preserving the existing timeout handling. This ensures
stopProfile always releases reservations and transitions to stopped, including
callers such as closeIdleSession.
🪄 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: ASSERTIVE

Plan: Pro Plus

Run ID: 52fae6a2-db91-4d33-817b-83a508622322

📥 Commits

Reviewing files that changed from the base of the PR and between d4e246e and c16fde5.

📒 Files selected for processing (14)
  • docs/architecture.md
  • src/cli/create-runtime.ts
  • src/config/env-expand.ts
  • src/config/types.ts
  • src/mcp/server/miftah-server.ts
  • src/secrets/secret-resolver.ts
  • src/upstream/multi-upstream-process-manager.ts
  • src/upstream/profile-session-limiter.ts
  • src/upstream/upstream-process-manager.ts
  • src/upstream/upstream-session.ts
  • tests/config-preflight.test.ts
  • tests/config.test.ts
  • tests/fixtures/fake-upstream.mjs
  • tests/upstream-manager.test.ts

Comment thread src/secrets/secret-resolver.ts Outdated
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@mohanagy

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@mohanagy

Copy link
Copy Markdown
Owner Author

@coderabbitai run pre-merge checks

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown
✅ Actions performed

Running Pre-merge checks. Results will be posted in the summary comment once complete.

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@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: 1

🤖 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 `@src/upstream/upstream-process-manager.ts`:
- Around line 609-610: Update the timeout handling in the upstream process
manager’s close/cleanup flow: after the deadline, do not await the original
close promise or unbounded terminateTransport call. Trigger forced transport
cleanup best-effort without blocking, then immediately return or finalize with
the timeout failure reason so close() and closeProfile() cannot hang. Add a
regression test using a never-resolving close promise to verify capacity and
health finalize promptly.
🪄 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: ASSERTIVE

Plan: Pro Plus

Run ID: 6b32084c-6269-489f-a3bc-e3fd54af70e5

📥 Commits

Reviewing files that changed from the base of the PR and between c16fde5 and 2a89ff7.

📒 Files selected for processing (4)
  • docs/config.md
  • src/secrets/secret-resolver.ts
  • src/upstream/upstream-process-manager.ts
  • tests/upstream-manager.test.ts

Comment thread src/upstream/upstream-process-manager.ts Outdated
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@mohanagy

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@mohanagy

Copy link
Copy Markdown
Owner Author

@coderabbitai run pre-merge checks

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown
✅ Actions performed

Running Pre-merge checks. Results will be posted in the summary comment once complete.

@mohanagy

Copy link
Copy Markdown
Owner Author

@coderabbitai generate docstrings

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown
✅ Action performed

Initiated docstring generation; will generate only if new commits exist.

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown

Note

Docstrings generation - SUCCESS
Generated docstrings for this pull request at #52

coderabbitai Bot added a commit that referenced this pull request Jul 11, 2026
Docstrings generation was requested by @mohanagy.

* #51 (comment)

The following files were modified:

* `src/cli/create-runtime.ts`
* `src/config/env-expand.ts`
* `src/upstream/upstream-process-manager.ts`
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@mohanagy

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@mohanagy

Copy link
Copy Markdown
Owner Author

@coderabbitai run pre-merge checks

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown
✅ Actions performed

Running Pre-merge checks. Results will be posted in the summary comment once complete.

@mohanagy

Copy link
Copy Markdown
Owner Author

@coderabbitai run pre-merge checks

The four functions identified by the docstring generator now have complete TSDoc at current head 13388161f49f6864fb74e30d55cd1392c8480577. Please recalculate Docstring Coverage against this head and identify any remaining undocumented functions if the threshold is still not met.

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown
✅ Actions performed

Running Pre-merge checks. Results will be posted in the summary comment once complete.

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