-
Notifications
You must be signed in to change notification settings - Fork 52
pass the model #392
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
pass the model #392
Changes from all commits
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 |
|---|---|---|
|
|
@@ -39,6 +39,8 @@ export interface SpawnRequest { | |
| cli: string; | ||
| /** Initial task to inject */ | ||
| task: string; | ||
| /** Model override (e.g., 'opus', 'sonnet', 'haiku'). Takes precedence over agent profile. */ | ||
| model?: string; | ||
| /** Optional team name to organize agents under */ | ||
| team?: string; | ||
| /** Working directory for the agent (defaults to detected workspace) */ | ||
|
|
@@ -93,6 +95,8 @@ export interface WorkerInfo { | |
| spawnedAt: number; | ||
| /** PID of the pty process */ | ||
| pid?: number; | ||
| /** Current model if known (e.g., 'opus', 'sonnet', 'haiku') */ | ||
| model?: string; | ||
| } | ||
|
|
||
| /** SpeakOn trigger types for shadow agents */ | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -38,6 +38,7 @@ import { | |
| type MetricsResponsePayload, | ||
| type AgentReadyPayload, | ||
| type SendInputPayload, | ||
| type SetModelPayload, | ||
| type ListWorkersPayload, | ||
| } from '@agent-relay/protocol/types'; | ||
| import type { ChannelJoinPayload, ChannelLeavePayload, ChannelMessagePayload } from '@agent-relay/protocol/channels'; | ||
|
|
@@ -1345,6 +1346,17 @@ export class Daemon { | |
| break; | ||
| } | ||
|
|
||
| case 'SET_MODEL': { | ||
| if (!this.spawnManager) { | ||
| this.sendErrorEnvelope(connection, 'SpawnManager not enabled. Configure spawnManager: true in daemon config.'); | ||
| break; | ||
| } | ||
| const setModelEnvelope = envelope as Envelope<SetModelPayload>; | ||
| log.info(`SET_MODEL request: from=${connection.agentName} agent=${setModelEnvelope.payload.name} model=${setModelEnvelope.payload.model}`); | ||
| this.spawnManager.handleSetModel(connection, setModelEnvelope); | ||
| break; | ||
| } | ||
|
|
||
| case 'LIST_WORKERS': { | ||
| if (!this.spawnManager) { | ||
| this.sendErrorEnvelope(connection, 'SpawnManager not enabled. Configure spawnManager: true in daemon config.'); | ||
|
|
||
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,144 @@ | ||
| import { describe, it, expect, beforeEach, vi } from 'vitest'; | ||
| import { SpawnManager } from './spawn-manager.js'; | ||
| import type { Envelope, SetModelPayload } from '@agent-relay/protocol/types'; | ||
|
|
||
| /** | ||
| * Mock connection that captures sent envelopes. | ||
| */ | ||
| function createMockConnection(agentName: string) { | ||
| return { | ||
| id: `conn-${agentName}`, | ||
| agentName, | ||
| sessionId: `session-${agentName}`, | ||
| send: vi.fn(), | ||
| close: vi.fn(), | ||
| }; | ||
| } | ||
|
|
||
| function createSetModelEnvelope( | ||
| name: string, | ||
| model: string, | ||
| timeoutMs?: number, | ||
| ): Envelope<SetModelPayload> { | ||
| return { | ||
| v: 1, | ||
| type: 'SET_MODEL', | ||
| id: `env-${Date.now()}`, | ||
| ts: Date.now(), | ||
| payload: { name, model, timeoutMs }, | ||
| }; | ||
| } | ||
|
|
||
| describe('SpawnManager.handleSetModel', () => { | ||
| let manager: SpawnManager; | ||
| let mockSetWorkerModel: ReturnType<typeof vi.fn>; | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
|
|
||
| manager = new SpawnManager({ | ||
| projectRoot: '/tmp/test-project', | ||
| }); | ||
|
|
||
| // Mock the spawner's setWorkerModel method | ||
| mockSetWorkerModel = vi.fn(); | ||
| (manager as any).spawner.setWorkerModel = mockSetWorkerModel; | ||
| }); | ||
|
|
||
| it('should send success result when model switch succeeds', async () => { | ||
| const connection = createMockConnection('Lead'); | ||
| const envelope = createSetModelEnvelope('Worker1', 'haiku'); | ||
|
|
||
| mockSetWorkerModel.mockResolvedValue({ | ||
| success: true, | ||
| previousModel: 'sonnet', | ||
| }); | ||
|
|
||
| await manager.handleSetModel(connection as any, envelope); | ||
|
|
||
| expect(mockSetWorkerModel).toHaveBeenCalledWith('Worker1', 'haiku', 30000); | ||
| expect(connection.send).toHaveBeenCalledTimes(1); | ||
|
|
||
| const result = connection.send.mock.calls[0][0]; | ||
| expect(result.type).toBe('SET_MODEL_RESULT'); | ||
| expect(result.payload.success).toBe(true); | ||
| expect(result.payload.name).toBe('Worker1'); | ||
| expect(result.payload.model).toBe('haiku'); | ||
| expect(result.payload.previousModel).toBe('sonnet'); | ||
| expect(result.payload.error).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should send failure result when model switch fails', async () => { | ||
| const connection = createMockConnection('Lead'); | ||
| const envelope = createSetModelEnvelope('Worker1', 'haiku'); | ||
|
|
||
| mockSetWorkerModel.mockResolvedValue({ | ||
| success: false, | ||
| error: 'Agent "Worker1" did not become idle within 30000ms', | ||
| }); | ||
|
|
||
| await manager.handleSetModel(connection as any, envelope); | ||
|
|
||
| const result = connection.send.mock.calls[0][0]; | ||
| expect(result.type).toBe('SET_MODEL_RESULT'); | ||
| expect(result.payload.success).toBe(false); | ||
| expect(result.payload.error).toContain('did not become idle'); | ||
| }); | ||
|
|
||
| it('should pass custom timeout from payload', async () => { | ||
| const connection = createMockConnection('Lead'); | ||
| const envelope = createSetModelEnvelope('Worker1', 'opus', 60000); | ||
|
|
||
| mockSetWorkerModel.mockResolvedValue({ success: true }); | ||
|
|
||
| await manager.handleSetModel(connection as any, envelope); | ||
|
|
||
| expect(mockSetWorkerModel).toHaveBeenCalledWith('Worker1', 'opus', 60000); | ||
| }); | ||
|
|
||
| it('should handle spawner throwing an error', async () => { | ||
| const connection = createMockConnection('Lead'); | ||
| const envelope = createSetModelEnvelope('Worker1', 'opus'); | ||
|
|
||
| mockSetWorkerModel.mockRejectedValue(new Error('PTY process crashed')); | ||
|
|
||
| await manager.handleSetModel(connection as any, envelope); | ||
|
|
||
| const result = connection.send.mock.calls[0][0]; | ||
| expect(result.type).toBe('SET_MODEL_RESULT'); | ||
| expect(result.payload.success).toBe(false); | ||
| expect(result.payload.error).toBe('PTY process crashed'); | ||
| }); | ||
|
|
||
| it('should send failure when agent not found', async () => { | ||
| const connection = createMockConnection('Lead'); | ||
| const envelope = createSetModelEnvelope('NonExistent', 'haiku'); | ||
|
|
||
| mockSetWorkerModel.mockResolvedValue({ | ||
| success: false, | ||
| error: 'Agent "NonExistent" not found', | ||
| }); | ||
|
|
||
| await manager.handleSetModel(connection as any, envelope); | ||
|
|
||
| const result = connection.send.mock.calls[0][0]; | ||
| expect(result.payload.success).toBe(false); | ||
| expect(result.payload.error).toContain('not found'); | ||
| }); | ||
|
|
||
| it('should send failure for unsupported CLI', async () => { | ||
| const connection = createMockConnection('Lead'); | ||
| const envelope = createSetModelEnvelope('CodexWorker', 'gpt-4o'); | ||
|
|
||
| mockSetWorkerModel.mockResolvedValue({ | ||
| success: false, | ||
| error: 'CLI "codex" does not support mid-session model switching', | ||
| }); | ||
|
|
||
| await manager.handleSetModel(connection as any, envelope); | ||
|
|
||
| const result = connection.send.mock.calls[0][0]; | ||
| expect(result.payload.success).toBe(false); | ||
| expect(result.payload.error).toContain('does not support'); | ||
| }); | ||
| }); |
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.
Check failure
Code scanning / CodeQL
Insecure randomness High
Copilot Autofix
AI 4 months ago
In general terms, the fix is to stop using
Math.random()for generating agent names and replace it with a cryptographically secure pseudo-random source. On Node.js, this means using thecryptomodule (for example,crypto.randomInt, or deriving an index fromcrypto.randomBytes) instead ofMath.random(). We must ensure that the selection of indices remains uniform and within array bounds, and that we keep the public API (generateAgentName,generateUniqueAgentName,isValidAgentName) unchanged.Concretely, we will modify
packages/utils/src/name-generator.ts:cryptomodule at the top of the file.Math.floor(Math.random() * ADJECTIVES.length)and the analogous call forNOUNSingenerateAgentNamewithcrypto.randomInt(ADJECTIVES.length)andcrypto.randomInt(NOUNS.length), respectively.crypto.randomInt(max)returns an integer in[0, max), so this is a drop-in replacement for indexing arrays.generateUniqueAgentNamethat currently usesMath.floor(Math.random() * 1000)withcrypto.randomInt(1000).These changes stay entirely within
packages/utils/src/name-generator.ts, do not affect the external interface, and remove all insecureMath.random()usages that CodeQL traces into the spawner. No changes tosrc/cli/index.ts,packages/config/src/shadow-config.ts, orpackages/bridge/src/spawner.tsare required because they simply consume the already-generated names.