refactor: launch external tools without Room credentials or hook settings
A Launcher launch still opens the tool in the active workspace's folder, tracks its process, streams observed output and stops it. What it no longer does: - set WAGGLE_SIGNAL_EMIT, WAGGLE_SIDECAR_URL, HIVE_MIND_DATA_DIR or the Room identity variables (WAGGLE_RUN_ID, WAGGLE_ROOM_ID, WAGGLE_DANCE_TEAM_ID, WAGGLE_SENDER_ID, WAGGLE_RUN_TOKEN). Only the removed coding-agent hooks and the WaggleDance CLI read them. The external-process allowlist now admits WAGGLE_WORKSPACE_ID alone; - issue a run credential. Run tokens existed for Room workers (the dance CLI and OpenClaw's model calls), so the registry's credential store, the middleware's run-token bearer on /v1/chat/completions and the proxy's per-run model scope go. While an agent run with a model is active, the proxy still serves only the session bearer. The run record that a launch creates in the agent-run registry stays: it backs cancel from the Room canvas, which this change does not remove.
This commit is contained in:
@@ -170,28 +170,6 @@ describe('AgentRunRegistry', () => {
|
||||
await expect(registry.control(first.id, 'cancel')).rejects.toThrow(/already cancelled/);
|
||||
});
|
||||
|
||||
it('issues ephemeral worker credentials and revokes them at terminal state', () => {
|
||||
const { registry, file } = createRegistry();
|
||||
const room = registry.createRoom({
|
||||
workspaceIds: ['alpha'], source: 'external_tool', title: 'External', task: 'Collaborate',
|
||||
});
|
||||
const worker = registry.createWorker({
|
||||
parentRunId: room.id, workspaceId: 'alpha', source: 'external_tool',
|
||||
executor: { kind: 'external_tool', toolId: 'codex' }, title: 'Codex', task: room.task,
|
||||
status: 'running',
|
||||
});
|
||||
|
||||
const token = registry.issueCredential(worker.id);
|
||||
expect(token.length).toBeGreaterThanOrEqual(32);
|
||||
expect(registry.authenticateCredential(token)?.id).toBe(worker.id);
|
||||
expect(registry.authenticateCredential('x'.repeat(43))).toBeUndefined();
|
||||
expect(fs.readFileSync(file, 'utf8')).not.toContain(token);
|
||||
|
||||
registry.update(worker.id, { status: 'completed' });
|
||||
expect(registry.authenticateCredential(token)).toBeUndefined();
|
||||
expect(() => registry.issueCredential(room.id)).toThrow(/Worker run not found/);
|
||||
});
|
||||
|
||||
it('marks internal work interrupted after restart and reconciles external pids separately', () => {
|
||||
const { registry, file } = createRegistry();
|
||||
const internalRoom = registry.createRoom({
|
||||
|
||||
@@ -19,7 +19,7 @@ import { CostTracker, runAgentLoop } from '@waggle/agent';
|
||||
import { ExecutionTraceStore, WaggleDB } from '@waggle/core';
|
||||
import { AgentRunRegistry } from '../../src/local/agent-run-registry.js';
|
||||
import { anthropicProxyRoutes } from '../../src/local/routes/anthropic-proxy.js';
|
||||
import { securityMiddleware, type RunTokenAuthResult } from '../../src/local/security-middleware.js';
|
||||
import { securityMiddleware } from '../../src/local/security-middleware.js';
|
||||
import { PROVIDER_ENV_NAMES } from '../../src/local/provider-env.js';
|
||||
|
||||
const MODEL_SPEND_RESERVATION_HEADER = 'x-waggle-model-spend-reservation';
|
||||
@@ -32,7 +32,6 @@ function createTestServer(options: {
|
||||
configApiKey?: string;
|
||||
dataDir?: string;
|
||||
sessionToken?: string;
|
||||
authenticateRunToken?: (token: string) => RunTokenAuthResult;
|
||||
costTracker?: CostTracker;
|
||||
traceStore?: FastifyInstance['traceStore'];
|
||||
registerSpendTarget?: string;
|
||||
@@ -70,10 +69,9 @@ function createTestServer(options: {
|
||||
dataDir: options.dataDir ?? '/tmp/nonexistent-waggle-test',
|
||||
});
|
||||
|
||||
if (options.sessionToken || options.authenticateRunToken) {
|
||||
if (options.sessionToken) {
|
||||
server.register(securityMiddleware, {
|
||||
sessionToken: options.sessionToken,
|
||||
authenticateRunToken: options.authenticateRunToken,
|
||||
});
|
||||
}
|
||||
server.register(anthropicProxyRoutes);
|
||||
@@ -1420,232 +1418,53 @@ describe('Anthropic Proxy Routes', () => {
|
||||
costTracker.releaseReservedModelSpend(reservation);
|
||||
});
|
||||
|
||||
it('rejects a run token completion when the requested model is outside the assigned run model', async () => {
|
||||
const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-run-token-model-scope-'));
|
||||
it('serves only the session bearer while an agent run with a model is active', async () => {
|
||||
const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-active-run-bearer-'));
|
||||
const registry = new AgentRunRegistry(path.join(dataDir, 'agent-runs.json'));
|
||||
const room = registry.createRoom({
|
||||
workspaceIds: ['workspace-1'],
|
||||
source: 'external_tool',
|
||||
title: 'OpenClaw room',
|
||||
task: 'Use the assigned model only',
|
||||
source: 'fleet',
|
||||
title: 'Fleet room',
|
||||
task: 'Answer with the assigned model',
|
||||
});
|
||||
const run = registry.createWorker({
|
||||
registry.createWorker({
|
||||
parentRunId: room.id,
|
||||
workspaceId: 'workspace-1',
|
||||
source: 'external_tool',
|
||||
executor: { kind: 'external_tool', toolId: 'openclaw', model: 'anthropic/claude-sonnet-4-6' },
|
||||
title: 'OpenClaw',
|
||||
source: 'fleet',
|
||||
executor: { kind: 'waggle_agent', model: 'anthropic/claude-sonnet-4-6' },
|
||||
title: 'Fleet agent',
|
||||
task: room.task,
|
||||
status: 'running',
|
||||
});
|
||||
const runToken = registry.issueCredential(run.id);
|
||||
server = createTestServer({
|
||||
vaultApiKey: 'anthropic-key',
|
||||
vaultProviders: { openai: { value: 'openai-key' } },
|
||||
});
|
||||
server = createTestServer({ vaultApiKey: 'anthropic-key' });
|
||||
server.decorate('agentRunRegistry', registry);
|
||||
server.decorate('agentState', { wsSessionToken: 'desktop-session-token' } as FastifyInstance['agentState']);
|
||||
globalThis.fetch = vi.fn(async () => new Response(JSON.stringify({
|
||||
content: [{ type: 'text', text: 'assigned model response' }],
|
||||
content: [{ type: 'text', text: 'ok' }],
|
||||
model: 'claude-sonnet-4-6',
|
||||
stop_reason: 'end_turn',
|
||||
usage: { input_tokens: 1, output_tokens: 2 },
|
||||
}), { status: 200, headers: { 'content-type': 'application/json' } })) as unknown as typeof globalThis.fetch;
|
||||
|
||||
try {
|
||||
const response = await server.inject({
|
||||
method: 'POST',
|
||||
url: '/v1/chat/completions',
|
||||
headers: { authorization: `Bearer ${runToken}` },
|
||||
payload: {
|
||||
model: 'openai/gpt-4.1',
|
||||
messages: [{ role: 'user', content: 'Use a different provider' }],
|
||||
stream: false,
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(403);
|
||||
expect(response.json().error.message).toContain('outside the assigned run model');
|
||||
expect(globalThis.fetch).not.toHaveBeenCalled();
|
||||
|
||||
const sameProvider = await server.inject({
|
||||
method: 'POST',
|
||||
url: '/v1/chat/completions',
|
||||
headers: { authorization: `Bearer ${runToken}` },
|
||||
payload: {
|
||||
model: 'anthropic/claude-opus-4-6',
|
||||
messages: [{ role: 'user', content: 'Use a different Anthropic model' }],
|
||||
stream: false,
|
||||
},
|
||||
});
|
||||
|
||||
expect(sameProvider.statusCode).toBe(403);
|
||||
expect(sameProvider.json().error.message).toContain('outside the assigned run model');
|
||||
expect(globalThis.fetch).not.toHaveBeenCalled();
|
||||
|
||||
const missingToken = await server.inject({
|
||||
method: 'POST',
|
||||
url: '/v1/chat/completions',
|
||||
payload: {
|
||||
model: 'openai/gpt-4.1',
|
||||
messages: [{ role: 'user', content: 'Use a different provider without a run token' }],
|
||||
stream: false,
|
||||
},
|
||||
});
|
||||
|
||||
expect(missingToken.statusCode).toBe(401);
|
||||
expect(globalThis.fetch).not.toHaveBeenCalled();
|
||||
|
||||
const invalidToken = await server.inject({
|
||||
method: 'POST',
|
||||
url: '/v1/chat/completions',
|
||||
headers: { authorization: 'Bearer invalid-run-token-with-enough-entropy-123' },
|
||||
payload: {
|
||||
model: 'openai/gpt-4.1',
|
||||
messages: [{ role: 'user', content: 'Use a different provider with an invalid run token' }],
|
||||
stream: false,
|
||||
},
|
||||
});
|
||||
|
||||
expect(invalidToken.statusCode).toBe(401);
|
||||
expect(globalThis.fetch).not.toHaveBeenCalled();
|
||||
|
||||
const assigned = await server.inject({
|
||||
method: 'POST',
|
||||
url: '/v1/chat/completions',
|
||||
headers: { authorization: `bearer ${runToken}` },
|
||||
payload: {
|
||||
model: 'claude-sonnet-4.6',
|
||||
messages: [{ role: 'user', content: 'Use the assigned provider' }],
|
||||
stream: false,
|
||||
},
|
||||
});
|
||||
|
||||
expect(assigned.statusCode).toBe(200);
|
||||
expect(assigned.json().choices[0].message.content).toBe('assigned model response');
|
||||
expect(vi.mocked(globalThis.fetch).mock.calls[0][0]).toBe('https://api.anthropic.com/v1/messages');
|
||||
} finally {
|
||||
registry.close();
|
||||
fs.rmSync(dataDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it('rejects a run token completion when the token is revoked between auth and route handling', async () => {
|
||||
const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-run-token-revocation-race-'));
|
||||
const registry = new AgentRunRegistry(path.join(dataDir, 'agent-runs.json'));
|
||||
const room = registry.createRoom({
|
||||
workspaceIds: ['workspace-1'],
|
||||
source: 'external_tool',
|
||||
title: 'OpenClaw room',
|
||||
task: 'Use the assigned model only',
|
||||
});
|
||||
const run = registry.createWorker({
|
||||
parentRunId: room.id,
|
||||
workspaceId: 'workspace-1',
|
||||
source: 'external_tool',
|
||||
executor: { kind: 'external_tool', toolId: 'openclaw', model: 'anthropic/claude-sonnet-4-6' },
|
||||
title: 'OpenClaw',
|
||||
task: room.task,
|
||||
});
|
||||
const runToken = registry.issueCredential(run.id);
|
||||
let revokedAfterMiddlewareAuth = false;
|
||||
server = createTestServer({
|
||||
vaultApiKey: 'anthropic-key',
|
||||
sessionToken: 'desktop-session-token',
|
||||
authenticateRunToken: (candidate) => {
|
||||
if (candidate !== runToken) return false;
|
||||
const authenticatedRun = registry.authenticateCredential(candidate);
|
||||
if (!authenticatedRun) return false;
|
||||
if (!revokedAfterMiddlewareAuth) {
|
||||
revokedAfterMiddlewareAuth = true;
|
||||
registry.revokeCredential(candidate);
|
||||
}
|
||||
return { runId: authenticatedRun.id, model: authenticatedRun.executor.model };
|
||||
const completion = (authorization?: string) => server.inject({
|
||||
method: 'POST',
|
||||
url: '/v1/chat/completions',
|
||||
headers: authorization ? { authorization } : {},
|
||||
payload: {
|
||||
model: 'claude-sonnet-4.6',
|
||||
messages: [{ role: 'user', content: 'Hello' }],
|
||||
stream: false,
|
||||
},
|
||||
});
|
||||
server.decorate('agentRunRegistry', registry);
|
||||
globalThis.fetch = vi.fn(async () => new Response(JSON.stringify({
|
||||
content: [{ type: 'text', text: 'should not forward' }],
|
||||
model: 'claude-sonnet-4-6',
|
||||
stop_reason: 'end_turn',
|
||||
usage: { input_tokens: 1, output_tokens: 2 },
|
||||
}), { status: 200, headers: { 'content-type': 'application/json' } })) as unknown as typeof globalThis.fetch;
|
||||
|
||||
try {
|
||||
const response = await server.inject({
|
||||
method: 'POST',
|
||||
url: '/v1/chat/completions',
|
||||
headers: { authorization: `Bearer ${runToken}` },
|
||||
payload: {
|
||||
model: 'openai/gpt-4.1',
|
||||
messages: [{ role: 'user', content: 'Use a different provider after token revocation' }],
|
||||
stream: false,
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(401);
|
||||
expect(response.json().error.message).toContain('no longer active');
|
||||
const missing = await completion();
|
||||
expect(missing.statusCode).toBe(401);
|
||||
const foreign = await completion('Bearer run-token-with-enough-entropy-1234567890');
|
||||
expect(foreign.statusCode).toBe(401);
|
||||
expect(globalThis.fetch).not.toHaveBeenCalled();
|
||||
} finally {
|
||||
registry.close();
|
||||
fs.rmSync(dataDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it('rejects a run token completion when the assigned model changes after middleware auth', async () => {
|
||||
const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-run-token-model-reassignment-'));
|
||||
const registry = new AgentRunRegistry(path.join(dataDir, 'agent-runs.json'));
|
||||
const room = registry.createRoom({
|
||||
workspaceIds: ['workspace-1'],
|
||||
source: 'external_tool',
|
||||
title: 'OpenClaw room',
|
||||
task: 'Use the currently assigned model only',
|
||||
});
|
||||
const run = registry.createWorker({
|
||||
parentRunId: room.id,
|
||||
workspaceId: 'workspace-1',
|
||||
source: 'external_tool',
|
||||
executor: { kind: 'external_tool', toolId: 'openclaw', model: 'anthropic/claude-sonnet-4-6' },
|
||||
title: 'OpenClaw',
|
||||
task: room.task,
|
||||
});
|
||||
const runToken = registry.issueCredential(run.id);
|
||||
let reassignedAfterMiddlewareAuth = false;
|
||||
server = createTestServer({
|
||||
vaultApiKey: 'anthropic-key',
|
||||
sessionToken: 'desktop-session-token',
|
||||
authenticateRunToken: (candidate) => {
|
||||
if (candidate !== runToken) return false;
|
||||
const authenticatedRun = registry.authenticateCredential(candidate);
|
||||
if (!authenticatedRun) return false;
|
||||
if (!reassignedAfterMiddlewareAuth) {
|
||||
reassignedAfterMiddlewareAuth = true;
|
||||
registry.update(authenticatedRun.id, { executor: { model: 'anthropic/claude-haiku-4-5' } });
|
||||
}
|
||||
return { runId: authenticatedRun.id, model: authenticatedRun.executor.model };
|
||||
},
|
||||
});
|
||||
server.decorate('agentRunRegistry', registry);
|
||||
globalThis.fetch = vi.fn(async () => new Response(JSON.stringify({
|
||||
content: [{ type: 'text', text: 'should not forward' }],
|
||||
model: 'claude-sonnet-4-6',
|
||||
stop_reason: 'end_turn',
|
||||
usage: { input_tokens: 1, output_tokens: 2 },
|
||||
}), { status: 200, headers: { 'content-type': 'application/json' } })) as unknown as typeof globalThis.fetch;
|
||||
|
||||
try {
|
||||
const response = await server.inject({
|
||||
method: 'POST',
|
||||
url: '/v1/chat/completions',
|
||||
headers: { authorization: `Bearer ${runToken}` },
|
||||
payload: {
|
||||
model: 'claude-sonnet-4.6',
|
||||
messages: [{ role: 'user', content: 'Use the model that was assigned during middleware auth' }],
|
||||
stream: false,
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(403);
|
||||
expect(response.json().error.message).toContain('outside the assigned run model');
|
||||
expect(globalThis.fetch).not.toHaveBeenCalled();
|
||||
const session = await completion('Bearer desktop-session-token');
|
||||
expect(session.statusCode, session.body).toBe(200);
|
||||
} finally {
|
||||
registry.close();
|
||||
fs.rmSync(dataDir, { recursive: true, force: true });
|
||||
|
||||
@@ -23,13 +23,11 @@ import {
|
||||
async function createTestServer(opts?: {
|
||||
rateLimiter?: { maxRequests?: number; windowMs?: number };
|
||||
sessionToken?: string;
|
||||
authenticateRunToken?: (token: string) => boolean;
|
||||
}) {
|
||||
const server = Fastify({ logger: false });
|
||||
await server.register(securityMiddleware, {
|
||||
rateLimiter: opts?.rateLimiter,
|
||||
sessionToken: opts?.sessionToken,
|
||||
authenticateRunToken: opts?.authenticateRunToken,
|
||||
});
|
||||
|
||||
// Simple test routes
|
||||
@@ -424,52 +422,21 @@ describe('Bearer Token Authentication', () => {
|
||||
}
|
||||
});
|
||||
|
||||
it('accepts a narrow run token only as a Bearer on the model completion route', async () => {
|
||||
const runToken = 'run-token-with-enough-entropy-1234567890';
|
||||
const server = await createTestServer({
|
||||
sessionToken: TEST_TOKEN,
|
||||
authenticateRunToken: (candidate) => candidate === runToken,
|
||||
});
|
||||
it('accepts only the session token on the model completion route (no run tokens)', async () => {
|
||||
const server = await createTestServer({ sessionToken: TEST_TOKEN });
|
||||
try {
|
||||
const completion = await server.inject({
|
||||
method: 'POST', url: '/v1/chat/completions',
|
||||
headers: { authorization: `Bearer ${runToken}` },
|
||||
headers: { authorization: `Bearer ${TEST_TOKEN}` },
|
||||
});
|
||||
expect(completion.statusCode).toBe(200);
|
||||
|
||||
const unrelated = await server.inject({
|
||||
method: 'GET', url: '/api/test',
|
||||
headers: { 'x-waggle-run-token': runToken },
|
||||
});
|
||||
expect(unrelated.statusCode).toBe(401);
|
||||
const unrelatedBearer = await server.inject({
|
||||
method: 'GET', url: '/api/test',
|
||||
headers: { authorization: `Bearer ${runToken}` },
|
||||
});
|
||||
expect(unrelatedBearer.statusCode).toBe(401);
|
||||
const wrongTransport = await server.inject({
|
||||
const runTokenShaped = await server.inject({
|
||||
method: 'POST', url: '/v1/chat/completions',
|
||||
headers: { 'x-waggle-run-token': runToken },
|
||||
headers: { authorization: 'Bearer run-token-with-enough-entropy-1234567890' },
|
||||
});
|
||||
expect(wrongTransport.statusCode).toBe(401);
|
||||
// The WaggleDance transport no longer accepts run tokens in either form.
|
||||
const danceHeader = await server.inject({
|
||||
method: 'POST', url: '/api/waggle-dance/signal',
|
||||
headers: { 'x-waggle-run-token': runToken },
|
||||
});
|
||||
expect(danceHeader.statusCode).toBe(401);
|
||||
expect(danceHeader.json().code).toBe('MISSING_TOKEN');
|
||||
const danceBearer = await server.inject({
|
||||
method: 'POST', url: '/api/waggle-dance/signal',
|
||||
headers: { authorization: `Bearer ${runToken}` },
|
||||
});
|
||||
expect(danceBearer.statusCode).toBe(401);
|
||||
const wrongCompletion = await server.inject({
|
||||
method: 'POST', url: '/v1/chat/completions',
|
||||
headers: { authorization: 'Bearer wrong-run-token-with-enough-entropy-123' },
|
||||
});
|
||||
expect(wrongCompletion.statusCode).toBe(401);
|
||||
expect(wrongCompletion.json().code).toBe('INVALID_TOKEN');
|
||||
expect(runTokenShaped.statusCode).toBe(401);
|
||||
expect(runTokenShaped.json().code).toBe('INVALID_TOKEN');
|
||||
} finally {
|
||||
await server.close();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user