-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add Chief fleet picker #1
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| import { spawn } from 'node:child_process'; | ||
| import { pathToFileURL } from 'node:url'; | ||
|
|
||
| import { | ||
| attachCommand, | ||
| attachMode, | ||
| createStatusProjector, | ||
| listBrokerAgents, | ||
| } from './fleet.mjs'; | ||
| import { requestHerdr } from './herdr-socket.mjs'; | ||
|
|
||
| export const DEFAULT_POLL_INTERVAL_MS = 5_000; | ||
|
|
||
| function waitForChild(child) { | ||
| return new Promise((resolve, reject) => { | ||
| child.once('error', reject); | ||
| child.once('exit', (code, signal) => resolve({ code, signal })); | ||
| }); | ||
| } | ||
|
|
||
| export async function runFleetAgent({ | ||
| environment = process.env, | ||
| listAgents = listBrokerAgents, | ||
| request = requestHerdr, | ||
| spawnProcess = spawn, | ||
| pollIntervalMs = DEFAULT_POLL_INTERVAL_MS, | ||
| } = {}) { | ||
| const socketPath = environment.HERDR_SOCKET_PATH; | ||
| const paneId = environment.HERDR_PANE_ID; | ||
| const agentName = environment.HERDR_RELAY_AGENT_NAME; | ||
| const agentLabel = environment.HERDR_RELAY_AGENT_LABEL || 'agent-relay'; | ||
| if (!socketPath || !paneId) throw new Error('Herdr did not provide the fleet pane context'); | ||
| if (!agentName) throw new Error('Fleet pane did not receive an Agent Relay agent name'); | ||
|
|
||
| const projectDir = process.cwd(); | ||
| const mode = attachMode(environment.HERDR_RELAY_ATTACH_MODE); | ||
| const projector = createStatusProjector({ | ||
| agentName, | ||
| initialBrokerState: | ||
| environment.HERDR_RELAY_INITIAL_STATE === undefined | ||
| ? undefined | ||
| : environment.HERDR_RELAY_INITIAL_STATE, | ||
| loadAgents: () => listAgents(projectDir), | ||
| report: ({ state, message }) => | ||
| request(socketPath, 'pane.report_agent', { | ||
| pane_id: paneId, | ||
| source: 'fleet-picker', | ||
| agent: agentLabel, | ||
| state, | ||
| message, | ||
| }), | ||
| }); | ||
|
|
||
| await projector.poll(); | ||
| const invocation = attachCommand({ | ||
| agentName, | ||
| mode, | ||
| residentChief: environment.HERDR_RELAY_RESIDENT_CHIEF === '1', | ||
| }); | ||
| const child = spawnProcess(invocation.command, invocation.args, { | ||
| cwd: projectDir, | ||
| env: environment, | ||
| stdio: 'inherit', | ||
| }); | ||
|
|
||
| let stopped = false; | ||
| let timer; | ||
| const schedule = () => { | ||
| if (stopped) return; | ||
| timer = setTimeout(async () => { | ||
| try { | ||
| await projector.poll(); | ||
| } catch { | ||
| // Keep the attach usable while Herdr is restarting. Because the | ||
| // projector advances only after a successful report, the next poll | ||
| // retries the same broker transition. | ||
| } finally { | ||
| schedule(); | ||
| } | ||
| }, pollIntervalMs); | ||
| timer.unref?.(); | ||
| }; | ||
| schedule(); | ||
|
|
||
| try { | ||
| const result = await waitForChild(child); | ||
| if (result.code && result.code !== 0) { | ||
| throw new Error(`${agentName} attach exited with status ${result.code}`); | ||
| } | ||
| return result; | ||
| } finally { | ||
| stopped = true; | ||
| if (timer) clearTimeout(timer); | ||
| } | ||
| } | ||
|
|
||
| export function isDirectEntrypoint(moduleUrl, argv1) { | ||
| return Boolean(argv1) && moduleUrl === pathToFileURL(argv1).href; | ||
| } | ||
|
|
||
| export async function main() { | ||
| try { | ||
| await runFleetAgent(); | ||
| } catch (error) { | ||
| console.error(`Agent Relay fleet attach failed: ${error.message}`); | ||
| process.exitCode = 1; | ||
| } | ||
| } | ||
|
|
||
| if (isDirectEntrypoint(import.meta.url, process.argv[1])) await main(); |
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,127 @@ | ||
| import { pathToFileURL } from 'node:url'; | ||
|
|
||
| import { | ||
| attachMode, | ||
| brokerStateMessage, | ||
| chiefAgentName, | ||
| fleetProjectDir, | ||
| listBrokerAgents, | ||
| projectBrokerState, | ||
| relayAgentLabel, | ||
| } from './fleet.mjs'; | ||
| import { requestHerdr } from './herdr-socket.mjs'; | ||
|
|
||
| export const FLEET_WORKSPACE_LABEL = 'Relay fleet'; | ||
| export const FLEET_AGENT_ENTRYPOINT = 'fleet-agent'; | ||
|
|
||
| function createdWorkspace(response) { | ||
| const workspace = response?.result?.workspace; | ||
| const rootPane = response?.result?.root_pane; | ||
| if (typeof workspace?.workspace_id !== 'string' || typeof rootPane?.pane_id !== 'string') { | ||
| throw new Error('Herdr did not return the created fleet workspace'); | ||
| } | ||
| return { workspaceId: workspace.workspace_id, rootPaneId: rootPane.pane_id }; | ||
| } | ||
|
|
||
| function openedPane(response) { | ||
| const pane = response?.result?.plugin_pane?.pane; | ||
| if (typeof pane?.pane_id !== 'string') throw new Error('Herdr did not return the opened fleet pane'); | ||
| return pane; | ||
| } | ||
|
|
||
| export async function runFleetPicker({ | ||
| environment = process.env, | ||
| listAgents = listBrokerAgents, | ||
| findChief = chiefAgentName, | ||
| request = requestHerdr, | ||
| logger = console, | ||
| } = {}) { | ||
| const socketPath = environment.HERDR_SOCKET_PATH; | ||
| if (!socketPath) throw new Error('Herdr did not provide HERDR_SOCKET_PATH'); | ||
| const pluginId = environment.HERDR_PLUGIN_ID; | ||
| if (!pluginId) throw new Error('Herdr did not provide HERDR_PLUGIN_ID'); | ||
|
|
||
| const projectDir = fleetProjectDir(environment); | ||
| const mode = attachMode(environment.HERDR_RELAY_ATTACH_MODE); | ||
| const agents = await listAgents(projectDir); | ||
| if (!agents.length) { | ||
| throw new Error(`No live Agent Relay broker agents were found for ${projectDir}`); | ||
| } | ||
| const residentChief = await findChief(projectDir); | ||
|
|
||
| const created = createdWorkspace( | ||
| await request(socketPath, 'workspace.create', { | ||
| cwd: projectDir, | ||
| label: FLEET_WORKSPACE_LABEL, | ||
| focus: false, | ||
| }) | ||
| ); | ||
| const opened = []; | ||
| try { | ||
| for (const agent of agents) { | ||
| const label = relayAgentLabel(agent); | ||
| const initialState = projectBrokerState(agent.current_state); | ||
| const response = await request(socketPath, 'plugin.pane.open', { | ||
| plugin_id: pluginId, | ||
| entrypoint: FLEET_AGENT_ENTRYPOINT, | ||
| placement: 'tab', | ||
| workspace_id: created.workspaceId, | ||
| cwd: projectDir, | ||
| focus: false, | ||
| env: { | ||
| HERDR_RELAY_AGENT_NAME: agent.name, | ||
| HERDR_RELAY_AGENT_LABEL: label, | ||
| HERDR_RELAY_ATTACH_MODE: mode, | ||
| HERDR_RELAY_INITIAL_STATE: String(agent.current_state || ''), | ||
| HERDR_RELAY_RESIDENT_CHIEF: agent.name === residentChief ? '1' : '0', | ||
| }, | ||
| }); | ||
| const pane = openedPane(response); | ||
| opened.push({ agent: agent.name, paneId: pane.pane_id }); | ||
|
|
||
| await request(socketPath, 'pane.report_agent', { | ||
|
khaliqgant marked this conversation as resolved.
Outdated
|
||
| pane_id: pane.pane_id, | ||
| source: 'fleet-picker', | ||
| agent: label, | ||
| state: initialState, | ||
| message: brokerStateMessage(agent.name, agent.current_state), | ||
| }); | ||
| await request(socketPath, 'pane.rename', { pane_id: pane.pane_id, label: agent.name }); | ||
| } | ||
|
|
||
| await request(socketPath, 'pane.close', { pane_id: created.rootPaneId }); | ||
| await request(socketPath, 'workspace.focus', { workspace_id: created.workspaceId }); | ||
| } catch (error) { | ||
| await request(socketPath, 'workspace.close', { workspace_id: created.workspaceId }).catch(() => undefined); | ||
| throw error; | ||
| } | ||
|
|
||
| logger.log( | ||
| `Opened ${opened.length} live Agent Relay agent pane(s) in ${FLEET_WORKSPACE_LABEL} from ${projectDir}.` | ||
| ); | ||
| return { projectDir, workspaceId: created.workspaceId, panes: opened }; | ||
| } | ||
|
|
||
| export function isDirectEntrypoint(moduleUrl, argv1) { | ||
| return Boolean(argv1) && moduleUrl === pathToFileURL(argv1).href; | ||
| } | ||
|
|
||
| export function waitForDismiss(input = process.stdin, output = process.stdout) { | ||
| if (!input.isTTY) return Promise.resolve(); | ||
| output.write('\nPress Enter to close this pane.\n'); | ||
| input.setEncoding('utf8'); | ||
| input.resume(); | ||
| return new Promise((resolve) => input.once('data', resolve)); | ||
| } | ||
|
|
||
| export async function main({ dismiss = waitForDismiss } = {}) { | ||
| try { | ||
| await runFleetPicker(); | ||
| } catch (error) { | ||
| console.error(`Agent Relay fleet picker failed: ${error.message}`); | ||
| process.exitCode = 1; | ||
| await dismiss(); | ||
| } | ||
| } | ||
|
|
||
| if (isDirectEntrypoint(import.meta.url, process.argv[1])) await main(); | ||
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.