-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix(cli): wait for the current turn to finish before an auto-update restart #1258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kavish-19
wants to merge
2
commits into
CodebuffAI:main
from
kavish-19:fix/defer-auto-update-restart-until-idle
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
cli/src/utils/__tests__/run-activity-marker.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { existsSync, rmSync } from 'fs' | ||
|
|
||
| import { afterAll, beforeEach, describe, expect, test } from 'bun:test' | ||
|
|
||
| import { useChatStore } from '../../state/chat-store' | ||
| import { | ||
| runActivityMarkerPath, | ||
| startRunActivityMarker, | ||
| } from '../run-activity-marker' | ||
|
|
||
| describe('run-activity-marker', () => { | ||
| const markerPath = runActivityMarkerPath() | ||
|
|
||
| beforeEach(() => { | ||
| rmSync(markerPath, { force: true }) | ||
| useChatStore.getState().setIsChainInProgress(false) | ||
| }) | ||
|
|
||
| afterAll(() => { | ||
| rmSync(markerPath, { force: true }) | ||
| useChatStore.getState().setIsChainInProgress(false) | ||
| }) | ||
|
|
||
| test('writes the marker while a turn is in progress and removes it when idle', () => { | ||
| startRunActivityMarker() | ||
|
|
||
| expect(existsSync(markerPath)).toBe(false) | ||
|
|
||
| useChatStore.getState().setIsChainInProgress(true) | ||
| expect(existsSync(markerPath)).toBe(true) | ||
|
|
||
| useChatStore.getState().setIsChainInProgress(false) | ||
| expect(existsSync(markerPath)).toBe(false) | ||
| }) | ||
|
|
||
| test('is a no-op when the value does not actually change', () => { | ||
| startRunActivityMarker() | ||
| useChatStore.getState().setIsChainInProgress(true) | ||
| rmSync(markerPath, { force: true }) | ||
|
|
||
| // Re-affirming the same value must not recreate the marker: only a real | ||
| // active/idle transition should. | ||
| useChatStore.getState().setIsChainInProgress(true) | ||
| expect(existsSync(markerPath)).toBe(false) | ||
| }) | ||
|
|
||
| test('registering more than once never stacks a duplicate exit handler', () => { | ||
| startRunActivityMarker() | ||
| const countAfterFirstStart = process.listenerCount('exit') | ||
|
|
||
| startRunActivityMarker() | ||
| startRunActivityMarker() | ||
|
|
||
| expect(process.listenerCount('exit')).toBe(countAfterFirstStart) | ||
| }) | ||
| }) |
58 changes: 58 additions & 0 deletions
cli/src/utils/run-activity-marker.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /** | ||
| * Cross-process "is a turn running" signal for the npm-wrapper launcher. | ||
| * | ||
| * The wrapper (release-core/launcher.js) checks for updates ~100ms after | ||
| * spawning this process and, on finding one, force-restarts it -- with no | ||
| * way to know whether the user is mid-turn, because it only has this | ||
| * process's exit event, not its React state. While this marker file exists, | ||
| * an agent turn is in progress; the wrapper waits for it to clear (bounded) | ||
| * before stopping the process for an update, instead of interrupting a | ||
| * turn that's still running. | ||
| * | ||
| * Named by this process's pid, which the wrapper already has from spawning | ||
| * it -- no handshake needed. Best-effort throughout: a failed write or | ||
| * remove just means the wrapper falls back to today's immediate-restart | ||
| * behavior for this session. | ||
| */ | ||
| import { rmSync, writeFileSync } from 'fs' | ||
| import os from 'os' | ||
| import path from 'path' | ||
|
|
||
| import { useChatStore } from '../state/chat-store' | ||
|
|
||
| export function runActivityMarkerPath(pid: number = process.pid): string { | ||
| return path.join(os.tmpdir(), `codebuff-run-active-${pid}`) | ||
| } | ||
|
|
||
| let started = false | ||
|
|
||
| /** Call once, before the store can start toggling isChainInProgress. */ | ||
| export function startRunActivityMarker(): void { | ||
| if (started) return | ||
| started = true | ||
|
|
||
| const filePath = runActivityMarkerPath() | ||
|
|
||
| const clear = () => { | ||
| try { | ||
| rmSync(filePath, { force: true }) | ||
| } catch { | ||
| // Best-effort; see module doc. | ||
| } | ||
| } | ||
|
|
||
| useChatStore.subscribe((state, prevState) => { | ||
| if (state.isChainInProgress === prevState.isChainInProgress) return | ||
| if (state.isChainInProgress) { | ||
| try { | ||
| writeFileSync(filePath, '', { flag: 'w' }) | ||
| } catch { | ||
| // Best-effort; see module doc. | ||
| } | ||
| } else { | ||
| clear() | ||
| } | ||
| }) | ||
|
|
||
| process.on('exit', clear) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.