Repository navigation
fix(opencode): write the status plugin atomically and retry failed SSH plugin installs #24124
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import { | ||
| lstatSync, | ||
| mkdirSync, | ||
| mkdtempSync, | ||
| readdirSync, | ||
| readFileSync, | ||
| realpathSync, | ||
| rmSync, | ||
| symlinkSync, | ||
| writeFileSync | ||
| } from 'node:fs' | ||
| import { tmpdir } from 'node:os' | ||
| import { join } from 'node:path' | ||
| import type * as NodeFs from 'node:fs' | ||
| import { setAppEnvironment } from '../../shared/app-environment' | ||
|
|
||
| const { directWrites } = vi.hoisted(() => { | ||
| const writes: string[] = [] | ||
| return { directWrites: writes } | ||
| }) | ||
|
|
||
| vi.mock('node:fs', async (importOriginal) => { | ||
| const actual = await importOriginal<typeof NodeFs>() | ||
| return { | ||
| ...actual, | ||
| writeFileSync: (...args: Parameters<typeof actual.writeFileSync>) => { | ||
| directWrites.push(String(args[0])) | ||
| return actual.writeFileSync(...args) | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| import { OpenCodeHookService, _internals } from './hook-service' | ||
| import { resolveOpenCodeConfigDirectory } from '../../shared/opencode-config-directory' | ||
|
|
||
| // Why: OpenCode 2 reloads a plugin on mtime change; a reload must never see it truncated mid-write. | ||
| describe('OpenCodeHookService canonical plugin write', () => { | ||
| const ptyId = 'c0ffee00-0000-4000-8000-000000000000' | ||
| const originalXdgConfigHome = process.env.XDG_CONFIG_HOME | ||
| let userDataDir: string | ||
| let pluginPath: string | ||
|
|
||
| beforeAll(() => { | ||
| userDataDir = mkdtempSync(join(tmpdir(), 'orca-opencode-atomic-')) | ||
| process.env.XDG_CONFIG_HOME = userDataDir | ||
| setAppEnvironment({ | ||
| getPath: () => userDataDir, | ||
| getAppPath: () => process.cwd(), | ||
| getVersion: () => '0.0.0-test', | ||
| isPackaged: () => false, | ||
| onWillQuit: () => {}, | ||
| exit: () => {}, | ||
| getAppMetrics: () => [] | ||
| }) | ||
| }) | ||
|
|
||
| afterAll(() => { | ||
| if (originalXdgConfigHome === undefined) { | ||
| delete process.env.XDG_CONFIG_HOME | ||
| } else { | ||
| process.env.XDG_CONFIG_HOME = originalXdgConfigHome | ||
| } | ||
| rmSync(userDataDir, { recursive: true, force: true }) | ||
| }) | ||
|
|
||
| beforeEach(() => { | ||
| const pluginsDir = join(resolveOpenCodeConfigDirectory(), 'plugins') | ||
| rmSync(pluginsDir, { recursive: true, force: true }) | ||
| mkdirSync(pluginsDir, { recursive: true }) | ||
| pluginPath = join(pluginsDir, 'orca-opencode-status.js') | ||
| directWrites.length = 0 | ||
| }) | ||
|
|
||
| it('replaces a stale plugin without writing to its path in place', () => { | ||
| writeFileSync(pluginPath, 'stale plugin') | ||
| directWrites.length = 0 | ||
|
|
||
| new OpenCodeHookService().buildPtyEnv(ptyId) | ||
|
|
||
| expect(readFileSync(pluginPath, 'utf8')).toBe(_internals.getOpenCodePluginSource()) | ||
| expect(directWrites).not.toContain(pluginPath) | ||
| expect(readdirSync(join(pluginPath, '..'))).toEqual(['orca-opencode-status.js']) | ||
| }) | ||
|
|
||
| it.skipIf(process.platform === 'win32')( | ||
| 'keeps a symlinked plugin linked and replaces its target atomically', | ||
| () => { | ||
| const targetPath = join(userDataDir, 'dotfiles-orca-opencode-status.js') | ||
| writeFileSync(targetPath, 'stale plugin') | ||
| symlinkSync(targetPath, pluginPath) | ||
| directWrites.length = 0 | ||
|
|
||
| new OpenCodeHookService().buildPtyEnv(ptyId) | ||
|
|
||
| expect(lstatSync(pluginPath).isSymbolicLink()).toBe(true) | ||
| expect(readFileSync(targetPath, 'utf8')).toBe(_internals.getOpenCodePluginSource()) | ||
| expect(directWrites).not.toContain(pluginPath) | ||
| expect(directWrites).not.toContain(realpathSync(targetPath)) | ||
| rmSync(targetPath, { force: true }) | ||
| } | ||
| ) | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -160,6 +160,8 @@ const SSH_REJECTED_PTY_RECOVERY_MAX_ATTEMPTS = 2 | |
| // store read, an attach round trip and a store write. | ||
| const SSH_REJECTED_PTY_RECOVERY_MAX_GENERATION_ATTEMPTS = 12 | ||
| const SSH_REJECTED_PTY_RECOVERY_RETRY_DELAY_MS = 150 | ||
| const SSH_PLUGIN_INSTALL_RETRY_DELAY_MS = 5_000 | ||
| const SSH_PLUGIN_INSTALL_MAX_RETRIES = 3 | ||
| const SSH_SOURCE_RECOVERY_CANCELLATION_FAILED = 'ssh_source_recovery_cancellation_failed' | ||
|
|
||
| // Why: superseded attempts stop quietly; a dead mux still owned by this attempt must enter recovery. | ||
|
|
@@ -321,6 +323,8 @@ export class SshRelaySession { | |
| // Why: hold the notification-handler disposer so teardownProviders can release it on reconnect/shutdown (symmetric with muxDisposeCleanup). | ||
| private muxNotificationCleanup: (() => void) | null = null | ||
| private pluginSettingsCleanup: (() => void) | null = null | ||
| private pluginInstallRetryTimer: ReturnType<typeof setTimeout> | null = null | ||
| private pluginInstallGeneration = 0 | ||
| // Why: onStateChange never fires when the relay channel closes but SSH stays up; this callback lets ssh.ts drive relay-level reconnect. | ||
| private _onRelayLost: ((targetId: string) => void) | null = null | ||
| // Why: a version mismatch or a blocked owner admission is terminal, so it needs a separate callback | ||
|
|
@@ -1548,10 +1552,15 @@ export class SshRelaySession { | |
| } | ||
|
|
||
| // Why: ship plugin/extension source from Orca so agent-event changes don't force a relay redeploy — the relay is versioned independently. Best-effort: failure only costs agent status on this host. | ||
| private async installPluginsOnRelay(mux: SshChannelMultiplexer): Promise<void> { | ||
| private async installPluginsOnRelay( | ||
| mux: SshChannelMultiplexer, | ||
| attempt = 0, | ||
| generation = ++this.pluginInstallGeneration | ||
| ): Promise<void> { | ||
| if (!isRemoteAgentHooksEnabled()) { | ||
| return | ||
| } | ||
| this.clearPluginInstallRetry() | ||
| try { | ||
| const hooksEnabled = this.areAgentStatusHooksEnabled() | ||
| await mux.request( | ||
|
|
@@ -1585,6 +1594,37 @@ export class SshRelaySession { | |
| err instanceof Error ? err.message : String(err) | ||
| }` | ||
| ) | ||
| this.schedulePluginInstallRetry(mux, attempt, generation) | ||
| } | ||
| } | ||
|
|
||
| // Why: without a retry a timed-out install leaves the relay on stale sources until the next reconnect; each retry re-reads current settings. | ||
| // A superseded install (older generation) must not start a retry chain the newer one can't cancel. | ||
| private schedulePluginInstallRetry( | ||
| mux: SshChannelMultiplexer, | ||
| attempt: number, | ||
| generation: number | ||
| ): void { | ||
| if ( | ||
| attempt >= SSH_PLUGIN_INSTALL_MAX_RETRIES || | ||
| this.mux !== mux || | ||
| generation !== this.pluginInstallGeneration | ||
| ) { | ||
| return | ||
| } | ||
| this.pluginInstallRetryTimer = setTimeout(() => { | ||
| this.pluginInstallRetryTimer = null | ||
| if (this.mux === mux && !mux.isDisposed()) { | ||
|
Comment on lines
+1615
to
+1617
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Two settings changes can start installs on the same connection before either request finishes. If both fail, the second retry timer overwrites the first timer’s handle; a callback can also clear the handle for another pending timer. A newer install or session teardown can no longer cancel every retry, so unnecessary install requests may still run after a later attempt succeeds. Knowledge Base Used: Remote connectivity and execution |
||
| void this.installPluginsOnRelay(mux, attempt + 1, generation) | ||
| } | ||
| }, SSH_PLUGIN_INSTALL_RETRY_DELAY_MS) | ||
| this.pluginInstallRetryTimer.unref?.() | ||
| } | ||
|
|
||
| private clearPluginInstallRetry(): void { | ||
| if (this.pluginInstallRetryTimer) { | ||
| clearTimeout(this.pluginInstallRetryTimer) | ||
| this.pluginInstallRetryTimer = null | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -1698,6 +1738,7 @@ export class SshRelaySession { | |
| this.releaseRelayLossWatcher() | ||
| this.pluginSettingsCleanup?.() | ||
| this.pluginSettingsCleanup = null | ||
| this.clearPluginInstallRetry() | ||
| this.muxNotificationCleanup?.() | ||
| this.muxNotificationCleanup = null | ||
| for (const cleanup of this.ptyRecoveryNotificationCleanups) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,7 +1,8 @@ | ||||||
| import { mkdirSync, unlinkSync, writeFileSync } from 'node:fs' | ||||||
| import { mkdirSync } from 'node:fs' | ||||||
| import { isAbsolute, join, relative, resolve } from 'node:path' | ||||||
| import { resolveOpenCodeConfigDirectory } from '../shared/opencode-config-directory' | ||||||
| import { isInstalledOpenCodePluginCurrent } from '../shared/opencode-installed-plugin' | ||||||
| import { writeOpenCodePluginFile } from '../shared/opencode-plugin-file-write' | ||||||
|
|
||||||
| const RELAY_HOOKS_DIR = '.orca-relay' | ||||||
|
|
||||||
|
|
@@ -20,12 +21,7 @@ export function installOpenCodePluginInCanonicalConfig( | |||||
| const pluginPath = join(configDir, 'plugins', pluginFileName) | ||||||
| mkdirSync(join(configDir, 'plugins'), { recursive: true }) | ||||||
| if (!isInstalledOpenCodePluginCurrent(pluginPath, source)) { | ||||||
| try { | ||||||
| unlinkSync(pluginPath) | ||||||
| } catch { | ||||||
| // The file may not exist on the first install. | ||||||
| } | ||||||
| writeFileSync(pluginPath, source) | ||||||
| writeOpenCodePluginFile(pluginPath, source, 'replace-link') | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win Use If the canonical plugin is a symlink to stale content, The canonical path becomes a regular file, and the dotfile-manager target remains stale. Use the same canonical policy as the app. Add a relay test that checks both link preservation and target replacement. Proposed fix- writeOpenCodePluginFile(pluginPath, source, 'replace-link')
+ writeOpenCodePluginFile(pluginPath, source, 'write-through-link')The supplied PR objective requires preserving canonical-path symlinks. 📝 Committable suggestion
Suggested change
pullfrog[bot] marked this conversation as resolved.
Resolved
|
||||||
| } | ||||||
| return true | ||||||
| } catch (err) { | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.