Skip to content

fix: defer lifecycle closes during active runs#900

Merged
Astro-Han merged 7 commits into
devfrom
codex/i898-lifecycle-run-guard
May 25, 2026
Merged

fix: defer lifecycle closes during active runs#900
Astro-Han merged 7 commits into
devfrom
codex/i898-lifecycle-run-guard

Conversation

@Astro-Han

@Astro-Han Astro-Han commented May 25, 2026

Copy link
Copy Markdown
Owner

Summary

Fixes active assistant runs being interrupted by local lifecycle maintenance closes.

  • Tracks active runs by directory while runner work is executing.
  • Defers maintenance lifecycle closes (disposeAll, config invalidation, reload, dispose) until affected directories are idle.
  • Preserves force cleanup for shutdown / teardown / explicit force paths.
  • Changes global.dispose from boolean to a structured completed / deferred result and updates the generated SDK type.
  • Updates provider connect/disconnect to show pending-refresh copy when provider refresh is deferred.

Why

Issue #898 showed that provider connect, config refresh, or instance reload could close InstanceState while an assistant turn was still running. Because SessionRunState lived under that disposable state, the run finalizer cancelled the active runner and surfaced as misleading failed tool cards.

The fix moves the lifecycle decision to the central instance close seam: normal maintenance waits for active runs; force cleanup still works.

Related Issue

Closes #898

Human Review Status

Approved by maintainer

Review Focus

  • Whether the maintenance vs force boundary is clear enough for future lifecycle callers.
  • Whether deferred global.dispose semantics are acceptable for provider connect/disconnect and config refresh.
  • Whether the active-run tracking belongs in the current lifecycle provenance module or should be split into a dedicated coordinator in a follow-up.

Risk Notes

  • Maintenance refreshes can now apply after the current run finishes instead of immediately. Provider/config changes should affect the next turn, not the active one.
  • global.dispose response shape changed from boolean to { status, lifecycleActionID, affectedDirectoryKeys }; generated SDK v2 types were updated.
  • Visible provider toast copy changed for deferred refresh; source-boundary tests cover the branch, and app-shell snap was run as the available visual smoke target.
  • Platform-specific packaging/updater/signing paths were not touched.

How To Verify

Focused lifecycle tests: 41 passed
- bun test test/session/run-state.test.ts test/project/instance-store.test.ts test/effect/instance-state.test.ts test/server/project-init-git.test.ts

Review follow-up coverage:
- Deferred reload waits for active run idle before replacing state.
- Deferred dispose clears Instance.directories() after actual completion.
- Config.invalidate emits global.disposed only after actual deferred close.
- New runs wait while a maintenance close is in progress.
- Multi-runtime disposeAll aggregates affected directory keys.
- Cancelled runs waiting for lifecycle close do not leave phantom active-run markers.
- Multi-runtime disposeAll runs caller onCompleted once after every store closes.
- Deferred disposeDirectory/reload failures are logged with lifecycle action metadata and do not leave close state active.
- Deferred disposeAll/global dispose failures are logged, reject completion when awaited, do not emit completion, and do not leave close state active.
- Prompt busy/queued caller regression checks pass after restoring InstanceState directory lookup.

Provider wiring tests: 7 passed
- bun test src/components/dialog-connect-provider-source.test.ts src/context/global-sync/client-action-source.test.ts

Typecheck:
- packages/opencode: bun run typecheck passed
- packages/app: bun run typecheck passed
- packages/sdk/js: bun run typecheck passed

SDK generation:
- packages/sdk/js: bun run build passed and updated v2 generated dispose response type

Diff hygiene:
- git diff --check passed

Visual smoke:
- bun run snap app-shell passed and wrote docs/design/preview/screenshots/app-shell.png

Screenshots or Recordings

bun run snap app-shell passed. The changed deferred provider toast branch is state-dependent and covered by source-boundary tests rather than a dedicated snap fixture in this PR.

Checklist

How to use this checklist:

  • Tick a box by replacing [ ] with [x]. Do not edit, add, or remove items.
  • The bot-applied label items can only be honestly ticked AFTER the PR is opened and the labeler / priority-triage bots have run — return to the PR description and tick them then.
  • Most items are required. The few that are conditional are explicitly marked (conditional); for those, leave unticked if they truly do not apply and explain why in Risk Notes. All other items must be ticked before requesting human review.
  • Type label — this PR carries exactly one of bug, enhancement, task, documentation. Type labels are author-added; the labeler bot does NOT assign them. Add the label in the GitHub UI, then tick this.
  • Routing labels — this PR carries at least one of app, ui, platform, harness, ci. The labeler bot assigns these on PR open based on changed paths. Confirm the bot's choice (or override if wrong), then tick this.
  • Priority label — this PR carries exactly one of P0, P1, P2, P3. The priority-triage bot suggests one on PR open. Confirm or override, then tick this.
  • Human Review Status above is set to Pending, Approved by @<reviewer>, or Not required: <reason> (default is Pending; "not required" is restricted to bot-authored low-risk PRs).
  • I linked the related issue, or stated in Summary why there is no issue.
  • I described the review focus and any meaningful risks.
  • I replaced the example block in How To Verify with the real verification steps and the key result for each.
  • I did not introduce unrelated refactors, dependencies, generated files, or file changes beyond the stated scope.
  • (conditional) I manually checked visible UI or copy changes when needed, with screenshots or recordings. Leave unticked only if no visible UI or copy changed.
  • (conditional) I considered macOS and Windows impact for platform, packaging, updater, signing, paths, shell, or permissions changes. Leave unticked only if no platform/packaging surface was touched.
  • (conditional) I called out docs, release notes, dependencies, permissions, credentials, deletion behavior, generated content, or local file changes when relevant. Leave unticked only if none of those surfaces was touched.
  • I reviewed the final diff for unrelated changes and suspicious dependency changes.
  • I am targeting dev, and my PR title and commit messages use Conventional Commits in English.

Summary by CodeRabbit

  • New Features

    • Provider connection and disconnection now display deferred status messages when model refresh completion is deferred until after the current run finishes.
    • Added translations for deferred provider status messages in English and Chinese.
  • Refactor

    • Enhanced system lifecycle management to support deferring operations while active tasks are running with improved coordination between ongoing work and system maintenance.
  • Tests

    • Extended test coverage for deferred disposal behavior and lifecycle handling across multiple scenarios.

Review Change Stack

@coderabbitai

coderabbitai Bot commented May 25, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

This PR introduces deferred lifecycle closes for local instance disposal. When a run is active and disposal is requested in maintenance mode, the close is deferred until the run completes. A new LifecycleCloseResult type surfaces deferred status through API responses, and UI components conditionally show deferred-specific toast messages during provider connect/disconnect.

Changes

Deferred Lifecycle Closes for Local Instance Disposal

Layer / File(s) Summary
Active-run tracking and lifecycle-close primitives
packages/opencode/src/session/lifecycle-provenance.ts
New module-level tracking state and four exported helpers (trackActiveRun, hasActiveRuns, whenAllRunsIdle, beginLifecycleClose) coordinate run lifecycle during disposal, enabling detection of active runs and deferral of closes until idleness.
Instance store lifecycle API and maintenance-mode deferral
packages/opencode/src/project/instance-store.ts, packages/opencode/test/project/instance-store.test.ts
Disposal and reload contracts updated to accept mode ("maintenance" | "force") and optional onCompleted callback. Core refactoring implements deferral logic: maintenance-mode disposal checks active runs, schedules completion after idleness, returns LifecycleCloseResult with status and affected directories. New tests verify multiruntime aggregation and onCompleted invocation.
Public instance API propagation
packages/opencode/src/project/instance-runtime.ts, packages/opencode/src/project/instance.ts
Runtime helpers and public Instance methods now accept optional LifecycleCloseOptions and forward to store layer; public API updated to surface deferred results for caller inspection.
Run state active-run tracking integration
packages/opencode/src/session/run-state.ts, packages/opencode/test/session/run-state.test.ts
Run execution wrapped with withActiveRun(directory, work) to bracket lifecycle with active-run resource. Comprehensive test coverage for deferred disposal across directories, reload deferral, config invalidation deferral, and release-gated lifecycle assertions.
Global disposal endpoint and config invalidation hooks
packages/opencode/src/server/instance/global.ts, packages/opencode/src/config/config.ts
/dispose endpoint returns LifecycleCloseResult and wires onCompleted callback to emit global disposed event. Config invalidation uses onCompleted callback pattern instead of .finally() to coordinate event emission with disposal completion.
UI deferred messaging and i18n
packages/app/src/components/dialog-connect-provider.tsx, packages/app/src/components/settings-providers.tsx, packages/app/src/i18n/en.ts, packages/app/src/i18n/zh.ts, packages/app/src/context/global-sync/client-action-source.test.ts, packages/app/src/components/dialog-connect-provider-source.test.ts
Provider connect/disconnect flows conditionally render deferred-status toast descriptions when disposal response indicates status: "deferred". New i18n strings in English and Chinese explain that model refresh will complete after the current run. Source and client-action tests verify wiring.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~50 minutes

Possibly related PRs

  • Astro-Han/pawwork#513: Introduces directory-scoped disposal API; this PR's lifecycle-close refactoring and disposeDirectory signature changes build on that foundation.

Suggested labels

P1, platform

Poem

🐰 When runs are spinning, don't just close the door—
Wait 'til they're done before you ask for more!
Maintenance mode's the gentle way to part,
"Deferred" the kinder message to the heart. 🎯

🚥 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 'fix: defer lifecycle closes during active runs' clearly and concisely summarizes the main change—deferring lifecycle closes when active runs are present.
Linked Issues check ✅ Passed The PR successfully addresses issue #898 by implementing active-run tracking to prevent lifecycle closes from interrupting active assistant runs, adding deferred close semantics, and updating UI copy for deferred provider refresh states.
Out of Scope Changes check ✅ Passed All changes align with the stated objectives: active-run lifecycle tracking, deferred close semantics, provider/config UI updates, and SDK type generation. No unrelated refactors or extraneous changes detected.
Description check ✅ Passed The pull request description comprehensively follows the template structure with all required sections properly completed.

✏️ 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 codex/i898-lifecycle-run-guard

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.

@github-actions github-actions Bot added app Application behavior and product flows ui Design system and user interface harness Model harness, prompts, tool descriptions, and session mechanics labels May 25, 2026
@Astro-Han Astro-Han added bug Something isn't working P2 Medium priority labels May 25, 2026

@github-actions github-actions 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.

Suggested priority: P2 (includes user-path files (packages/app/src/components/dialog-connect-provider-source.test.ts, packages/app/src/components/dialog-connect-provider.tsx, packages/app/src/components/settings-providers.tsx, packages/app/src/context/global-sync/client-action-source.test.ts, packages/app/src/i18n/en.ts, packages/app/src/i18n/zh.ts)).

P1/P0 are reserved for maintainer confirmation. Please relabel manually if this is a release blocker, security issue, data-loss risk, or updater/runtime failure.

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request introduces a deferred disposal mechanism for project instances, ensuring that resources are only cleaned up once active runs are idle. It updates the InstanceStore and Instance classes to support maintenance and force modes, adds tracking for active runs in lifecycle-provenance, and integrates these changes into the server routes and UI toast notifications. The review feedback highlights critical issues regarding state consistency: specifically, the options object is lost during recursive deferred disposal calls, and the internal directories tracking set becomes stale when disposal operations are deferred because it lacks a completion callback to trigger the cleanup. Additionally, the aggregation of disposal results in disposeAllLoadedInstances is noted as lossy, potentially omitting affected directory keys in multi-instance scenarios.

Comment thread packages/opencode/src/project/instance-store.ts Outdated
Comment thread packages/opencode/src/project/instance.ts
Comment thread packages/opencode/src/project/instance.ts Outdated
Comment thread packages/opencode/src/project/instance-store.ts Outdated
@github-actions

github-actions Bot commented May 25, 2026

Copy link
Copy Markdown

Perf delta summary

Comparator: pass

Profile / Scenario interaction median interaction worst long task max tbt frame gap p95 frame gap max jank count cls status
default / session-streaming-long 48 -> 48 (0) 72 -> 64 (-8) 0 -> 0 (0) 0 -> 0 (0) 16.8 -> 16.8 (0) 33.3 -> 33.3 (0) 0 -> 0 (0) 0 -> 0 (0) pass

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

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/opencode/src/project/instance-store.ts`:
- Around line 251-256: The deferred disposal Promise created by
whenAllRunsIdle(...).then(() =>
Effect.runPromise(disposeEntryNow(...))).finally(releaseClose) is
fire-and-forget (void completed) so any rejection from
Effect.runPromise(disposeEntryNow(...)) is swallowed; update the code that
builds completed (the variable created from whenAllRunsIdle, calling
disposeEntryNow and finally releaseClose) to attach a .catch handler that logs
or otherwise handles the error (use the existing logging mechanism or
ctx.logger) and ensure releaseClose still runs; reference whenAllRunsIdle,
disposeEntryNow, releaseClose, completed and ctx.directory when adding the catch
so failures during deferred disposal are not unhandled.
- Around line 280-284: The deferred reload is fire-and-forget and can produce
unhandled rejections; wrap the Promise returned by
whenAllRunsIdle(...).then(...) (the chain invoking
Effect.runPromise(reload(input, reason, { mode: "force"
})).finally(releaseDeferredClose)) with a .catch handler to surface/log errors
and still ensure releaseDeferredClose runs; in other words, attach .catch(...)
to the chain that calls reload (referencing whenAllRunsIdle, reload, and
releaseDeferredClose) so any rejection is handled consistently with the change
you made for disposeEntry.
🪄 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: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 265b82e7-d186-4443-be40-037c98497e69

📥 Commits

Reviewing files that changed from the base of the PR and between 580fb76 and 068ce84.

⛔ Files ignored due to path filters (1)
  • packages/sdk/js/src/v2/gen/types.gen.ts is excluded by !**/gen/**
📒 Files selected for processing (15)
  • packages/app/src/components/dialog-connect-provider-source.test.ts
  • packages/app/src/components/dialog-connect-provider.tsx
  • packages/app/src/components/settings-providers.tsx
  • packages/app/src/context/global-sync/client-action-source.test.ts
  • packages/app/src/i18n/en.ts
  • packages/app/src/i18n/zh.ts
  • packages/opencode/src/config/config.ts
  • packages/opencode/src/project/instance-runtime.ts
  • packages/opencode/src/project/instance-store.ts
  • packages/opencode/src/project/instance.ts
  • packages/opencode/src/server/instance/global.ts
  • packages/opencode/src/session/lifecycle-provenance.ts
  • packages/opencode/src/session/run-state.ts
  • packages/opencode/test/project/instance-store.test.ts
  • packages/opencode/test/session/run-state.test.ts

Comment thread packages/opencode/src/project/instance-store.ts
Comment thread packages/opencode/src/project/instance-store.ts
@Astro-Han Astro-Han merged commit daa194b into dev May 25, 2026
30 checks passed
@Astro-Han Astro-Han deleted the codex/i898-lifecycle-run-guard branch May 25, 2026 12:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

app Application behavior and product flows bug Something isn't working harness Model harness, prompts, tool descriptions, and session mechanics P2 Medium priority ui Design system and user interface

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Local lifecycle closes surface as failed tool cards

1 participant