Repository navigation
fix(remote): give each session its own saved-connection file #4925
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
thymikee
wants to merge
22
commits into
main
Choose a base branch
from
fix/remote-connection-state-session-identity
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,124
−84
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
fa6c4aa
fix(remote): give each session its own saved-connection file
thymikee 050cf0d
test(daemon-client): publish the startup phase gate atomically
thymikee 1e24b0b
chore(gates): exempt the session file identity export from unused-exp…
thymikee 33d652b
fix(remote): never resolve a session onto another session's record
thymikee c5cdd7b
fix(remote): keep finding a session's hashed record after the shared …
thymikee 850e14c
fix(remote): resolve a session's saved connection by name and migrate…
thymikee 2e7a7c9
chore(gates): drop the unused-export exemption for the removed sessio…
thymikee 2e784e6
test(remote): ignore the fixture's second hard-link name on case-sens…
thymikee e0b79c9
docs(commands): name the refusal for an unreadable saved connection a…
thymikee 1f83288
fix(remote): find an empty session's older record and read older reco…
thymikee 8706c92
fix(remote): keep connection status read-only and stop following a no…
thymikee d888c17
docs(commands): scope the saved-connection refusals to the files they…
thymikee ea05a48
fix(remote): make the saved-connection readers observation-only and m…
thymikee 426a2db
docs(commands): say that only a save or a removal moves an older save…
thymikee 1846b18
fix(remote): settle the saved connection before disconnect tears it down
thymikee 8f115a5
refactor(cli): split disconnect into claim, teardown and removal steps
thymikee 0bc01ce
fix(remote): remove each invalid record a disconnect passes and fall …
thymikee 716fb68
refactor(remote): merge the claim helpers and drop lookups the name r…
thymikee 4c0e3b9
test(remote): keep one test per saved-connection behavior
thymikee eb31b81
test(daemon-client): leave the startup gate publish change to its own PR
thymikee 6e4ab95
fix(remote): drive disconnect from its one claim and unlink only a po…
thymikee ac0b49d
docs(reasons): regenerate error-reasons.md after rebase
thymikee 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
392 changes: 392 additions & 0 deletions
392
packages/remote/src/remote-connection-state-session-identity.test.ts
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import { test } from 'vitest'; | ||
| import assert from 'node:assert/strict'; | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import { makeTempWorkspace } from '@agent-device/test-kit'; | ||
| import { | ||
| hashRemoteConfigFile, | ||
| readRemoteConnectionState, | ||
| } from '@agent-device/remote/remote-connection-state'; | ||
| import { runCliCapture } from './cli-capture.ts'; | ||
|
|
||
| test('an unnamed disconnect removes an invalid default record before it disconnects the active session', async () => { | ||
| const { root, home, project } = makeTempWorkspace(); | ||
| const stateDir = path.join(root, 'state'); | ||
| const connectionsDir = path.join(stateDir, 'remote-connections'); | ||
| fs.mkdirSync(connectionsDir, { recursive: true }); | ||
| const remoteConfig = path.join(project, 'agent-device.remote.json'); | ||
| fs.writeFileSync( | ||
| remoteConfig, | ||
| JSON.stringify({ daemonBaseUrl: 'http://remote-mac.example.test:9124/agent-device' }), | ||
| 'utf8', | ||
| ); | ||
| const record = (session: string) => ({ | ||
| version: 1, | ||
| session, | ||
| remoteConfigPath: remoteConfig, | ||
| remoteConfigHash: hashRemoteConfigFile(remoteConfig), | ||
| tenant: 'acme', | ||
| runId: `run-${session}`, | ||
| connectedAt: new Date().toISOString(), | ||
| updatedAt: new Date().toISOString(), | ||
| }); | ||
| const defaultPath = path.join(connectionsDir, 'default.json'); | ||
| const teamPath = path.join(connectionsDir, 'team.json'); | ||
| const activePath = path.join(connectionsDir, '.active-session.json'); | ||
| fs.writeFileSync(defaultPath, JSON.stringify({ ...record('default'), updatedAt: null }), 'utf8'); | ||
| fs.writeFileSync(teamPath, JSON.stringify(record('team')), 'utf8'); | ||
| fs.writeFileSync(activePath, JSON.stringify({ session: 'team' }), 'utf8'); | ||
|
|
||
| const disconnected = await runCliCapture(['disconnect', '--state-dir', stateDir, '--json'], { | ||
| cwd: project, | ||
| env: { HOME: home }, | ||
| }); | ||
|
|
||
| assert.equal(disconnected.code, null); | ||
| assert.equal(JSON.parse(disconnected.stdout).data.session, 'team'); | ||
| assert.equal(fs.existsSync(defaultPath), false); | ||
| assert.equal(fs.existsSync(teamPath), false); | ||
| assert.equal(fs.existsSync(activePath), false); | ||
|
|
||
| fs.rmSync(root, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| // A build that sanitized session names wrote the session `.active-session` to exactly the pointer | ||
| // path. The unnamed disconnect claims it there, and that claim drives the client and the teardown. | ||
| test.each([ | ||
| { leaseProvider: undefined, refusal: undefined }, | ||
| { leaseProvider: 'proxy', refusal: 'REMOTE_SESSION_REQUIRED' }, | ||
| ])( | ||
| 'an unnamed disconnect of the older .active-session record at the pointer path (leaseProvider $leaseProvider)', | ||
| async ({ leaseProvider, refusal }) => { | ||
| const { root, home, project } = makeTempWorkspace(); | ||
| const stateDir = path.join(root, 'state'); | ||
| const connectionsDir = path.join(stateDir, 'remote-connections'); | ||
| fs.mkdirSync(connectionsDir, { recursive: true }); | ||
| const remoteConfig = path.join(project, 'agent-device.remote.json'); | ||
| fs.writeFileSync( | ||
| remoteConfig, | ||
| JSON.stringify({ daemonBaseUrl: 'http://remote-mac.example.test:9124/agent-device' }), | ||
| 'utf8', | ||
| ); | ||
| const session = '.active-session'; | ||
| fs.writeFileSync( | ||
| path.join(connectionsDir, '.active-session.json'), | ||
| JSON.stringify({ | ||
| version: 1, | ||
| session, | ||
| remoteConfigPath: remoteConfig, | ||
| remoteConfigHash: hashRemoteConfigFile(remoteConfig), | ||
| tenant: 'acme', | ||
| runId: 'run-legacy', | ||
| ...(leaseProvider ? { leaseProvider } : {}), | ||
| connectedAt: new Date().toISOString(), | ||
| updatedAt: new Date().toISOString(), | ||
| }), | ||
| 'utf8', | ||
| ); | ||
|
|
||
| const disconnected = await runCliCapture(['disconnect', '--state-dir', stateDir, '--json'], { | ||
| cwd: project, | ||
| env: { HOME: home }, | ||
| }); | ||
|
|
||
| if (refusal) { | ||
| assert.equal(disconnected.code, 1); | ||
| assert.equal(JSON.parse(disconnected.stdout).error.details.reason, refusal); | ||
| assert.equal(readRemoteConnectionState({ stateDir, session })?.runId, 'run-legacy'); | ||
| } else { | ||
| assert.equal(disconnected.code, null); | ||
| assert.equal(JSON.parse(disconnected.stdout).data.session, session); | ||
| assert.equal(readRemoteConnectionState({ stateDir, session }), null); | ||
| assert.deepEqual(fs.readdirSync(connectionsDir), []); | ||
| } | ||
|
|
||
| fs.rmSync(root, { recursive: true, force: true }); | ||
| }, | ||
| ); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { test } from 'vitest'; | ||
| import assert from 'node:assert/strict'; | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import { withCapturedStdoutAsync } from '@agent-device/test-kit'; | ||
| import { hashRemoteConfigFile } from '@agent-device/remote/remote-connection-state'; | ||
| import { | ||
| connectionWorkspace, | ||
| createTestClient, | ||
| } from '../../__tests__/remote-connection.fixtures.ts'; | ||
| import { connectionCommand } from '../../__tests__/test-utils/host-bound-commands.ts'; | ||
|
|
||
| // `connection status` only reports the saved connection. A status read that moved an older build's | ||
| // record to its session's own file failed where the records directory could not be written. | ||
|
|
||
| test('connection status --session Demo over an older Demo.json leaves the records directory unchanged', async () => { | ||
| const { tempRoot, stateDir, remoteConfigPath } = connectionWorkspace( | ||
| 'agent-device-connection-status-read-only-', | ||
| ); | ||
| fs.writeFileSync(remoteConfigPath, '{}'); | ||
| const recordsDir = path.join(stateDir, 'remote-connections'); | ||
| fs.mkdirSync(recordsDir, { recursive: true }); | ||
| const now = new Date().toISOString(); | ||
| fs.writeFileSync( | ||
| path.join(recordsDir, 'Demo.json'), | ||
| `${JSON.stringify({ | ||
| version: 1, | ||
| session: 'Demo', | ||
| remoteConfigPath, | ||
| remoteConfigHash: hashRemoteConfigFile(remoteConfigPath), | ||
| tenant: 'acme', | ||
| runId: 'run-Demo', | ||
| connectedAt: now, | ||
| updatedAt: now, | ||
| })}\n`, | ||
| ); | ||
|
|
||
| const stdout = await withCapturedStdoutAsync(async () => { | ||
| // Every call this client answers throws: status may not reach the daemon. | ||
| await connectionCommand({ | ||
| positionals: ['status'], | ||
| flags: { json: true, help: false, version: false, stateDir, session: 'Demo' }, | ||
| client: createTestClient(), | ||
| }); | ||
| }); | ||
|
|
||
| const payload = JSON.parse(stdout).data; | ||
| assert.equal(payload.session, 'Demo'); | ||
| assert.equal(payload.runId, 'run-Demo'); | ||
| assert.deepEqual(fs.readdirSync(recordsDir), ['Demo.json'], 'status moved no file'); | ||
| fs.rmSync(tempRoot, { recursive: true, force: true }); | ||
| }); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.