Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 63 additions & 8 deletions packages/cli/src/__tests__/acp-agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,63 @@ import { client, methods, RequestError } from '@agentclientprotocol/sdk';
import { createMakaAcpAgent } from '../acp/maka-acp-agent.js';

describe('Maka ACP agent', () => {
test('returns the Maka identity with no advertised capabilities or authentication', async () => {
test('returns the Maka identity and advertises only Session listing', async () => {
await client({ name: 'test-client' }).connectWith(
createMakaAcpAgent({ version: '0.2.0' }),
createMakaAcpAgent({ version: '0.2.0', sessionRegistry: fakeSessionRegistry() }),
async (agent) => {
assert.deepEqual(await agent.request(methods.agent.initialize, { protocolVersion: 1 }), {
protocolVersion: 1,
agentCapabilities: {},
agentCapabilities: { sessionCapabilities: { list: {} } },
authMethods: [],
agentInfo: { name: 'maka', title: 'Maka', version: '0.2.0' },
});
},
);
});

test('rejects unimplemented session requests with method details', async () => {
test('routes official SDK new and list requests through the Session registry', async () => {
const creates: unknown[] = [];
const lists: unknown[] = [];
await client({ name: 'test-client' }).connectWith(
createMakaAcpAgent({ version: '0.2.0' }),
createMakaAcpAgent({
version: '0.2.0',
sessionRegistry: fakeSessionRegistry({ creates, lists }),
}),
async (agent) => {
assert.deepEqual(
await agent.request(methods.agent.session.new, {
cwd: '/workspace',
mcpServers: [],
_meta: { ignored: true },
}),
{ sessionId: 'session-1' },
);
assert.deepEqual(await agent.request(methods.agent.session.list, { cwd: '/workspace' }), {
sessions: [
{
sessionId: 'session-1',
cwd: '/workspace',
title: 'Session',
updatedAt: '2026-08-24T00:00:00.000Z',
},
],
});
},
);
assert.deepEqual(creates, [{ cwd: '/workspace', mcpServers: [], _meta: { ignored: true } }]);
assert.deepEqual(lists, [{ cwd: '/workspace' }]);
});

test('does not implement or advertise session/close', async () => {
await client({ name: 'test-client' }).connectWith(
createMakaAcpAgent({ version: '0.2.0', sessionRegistry: fakeSessionRegistry() }),
async (agent) => {
await assert.rejects(
agent.request('session/new', { cwd: '/workspace' }),
agent.request(methods.agent.session.close, { sessionId: 'session-1' }),
(error: unknown) => {
assert.ok(error instanceof RequestError);
assert.equal(error.code, -32601);
assert.deepEqual(error.data, { method: 'session/new' });
assert.deepEqual(error.data, { method: 'session/close' });
return true;
},
);
Expand All @@ -57,7 +90,7 @@ describe('Maka ACP agent', () => {
test('selects v1 when the client requests an unsupported lower or higher version', async () => {
for (const protocolVersion of [0, 2]) {
await client({ name: 'test-client' }).connectWith(
createMakaAcpAgent({ version: '0.2.0' }),
createMakaAcpAgent({ version: '0.2.0', sessionRegistry: fakeSessionRegistry() }),
async (agent) => {
const response = await agent.request(methods.agent.initialize, { protocolVersion });
assert.equal(response.protocolVersion, 1);
Expand All @@ -66,3 +99,25 @@ describe('Maka ACP agent', () => {
}
});
});

function fakeSessionRegistry(observations: { creates?: unknown[]; lists?: unknown[] } = {}) {
return {
create: async (params: unknown) => {
observations.creates?.push(params);
return { sessionId: 'session-1' };
},
list: async (params: unknown) => {
observations.lists?.push(params);
return {
sessions: [
{
sessionId: 'session-1',
cwd: '/workspace',
title: 'Session',
updatedAt: '2026-08-24T00:00:00.000Z',
},
],
};
},
};
}
17 changes: 16 additions & 1 deletion packages/cli/src/__tests__/acp-child-process-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { spawn, type ChildProcessWithoutNullStreams } from 'node:child_process';
import { mkdtemp, mkdir, rm } from 'node:fs/promises';
import { lstat, mkdtemp, mkdir, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { PassThrough, Readable, Writable } from 'node:stream';
Expand All @@ -34,6 +34,7 @@ import {
startExecutionRuntimeHostService,
type RuntimeHostKernel,
} from '@maka/runtime-host/server';
import { STORAGE_ROOT_MARKER_FILE } from '@maka/storage/root-authority';
import { deriveMakaDataRoots, resolveMakaClientDataRoot } from '../workspace-root.js';

const DEFAULT_TIMEOUT_MS = 15_000;
Expand Down Expand Up @@ -117,6 +118,16 @@ export class AcpChildProcessHarness {
return Buffer.concat(this.#stderr).toString('utf8');
}

async hasRuntimeHostRootMarker(): Promise<boolean> {
try {
await lstat(join(this.#workspaceRoot, STORAGE_ROOT_MARKER_FILE));
return true;
} catch (error) {
if (isErrorWithCode(error, 'ENOENT')) return false;
throw error;
}
}

async withClient<T>(
operation: (client: AcpChildProcessClient) => Promise<T> | T,
configureClient: ConfigureAcpClient = (app) => app,
Expand Down Expand Up @@ -515,3 +526,7 @@ class StartupTimeoutError extends Error {}
function errorMessage(error: unknown): string {
return error instanceof Error ? error.message : String(error);
}

function isErrorWithCode(error: unknown, code: string): boolean {
return error instanceof Error && 'code' in error && error.code === code;
}
73 changes: 62 additions & 11 deletions packages/cli/src/__tests__/acp-child-process.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import assert from 'node:assert/strict';
import { once } from 'node:events';
import { realpath } from 'node:fs/promises';
import { PassThrough } from 'node:stream';
import { describe, test } from 'node:test';
import { RequestError, methods } from '@agentclientprotocol/sdk';
Expand Down Expand Up @@ -118,19 +119,14 @@ describe('Maka ACP child process', () => {
await harness.withClient(async ({ context }) => {
assert.deepEqual(await context.request(methods.agent.initialize, { protocolVersion: 1 }), {
protocolVersion: 1,
agentCapabilities: {},
agentCapabilities: { sessionCapabilities: { list: {} } },
authMethods: [],
agentInfo: { name: 'maka', title: 'Maka', version: '0.2.0' },
});

await assert.rejects(
context.request('session/new', { cwd: harness.workspaceRoot }),
(error: unknown) => {
assert.ok(error instanceof RequestError);
assert.equal(error.code, -32601);
assert.deepEqual(error.data, { method: 'session/new' });
return true;
},
assert.equal(
await harness.hasRuntimeHostRootMarker(),
false,
'initialize must not begin Runtime Host discovery or candidate startup',
);
});

Expand All @@ -139,13 +135,68 @@ describe('Maka ACP child process', () => {
assert.equal(harness.stderr, '');

const lines = harness.stdout.split(/\r?\n/u).filter((line) => line.trim().length > 0);
assert.ok(lines.length >= 2, 'expected initialize and method-not-found responses');
assert.ok(lines.length >= 1, 'expected initialize response');
for (const line of lines) {
const message: unknown = JSON.parse(line);
assertJsonRpcMessage(message);
}
});
});

test('serves multiple ACP Sessions through a real Runtime Host', {
timeout: 30_000,
}, async () => {
await withAcpChildProcessHarness(
async (harness) => {
await harness.withClient(async ({ context }) => {
await context.request(methods.agent.initialize, { protocolVersion: 1 });
const first = await context.request(methods.agent.session.new, {
cwd: harness.workspaceRoot,
mcpServers: [],
});
const second = await context.request(methods.agent.session.new, {
cwd: harness.workspaceRoot,
mcpServers: [],
});
assert.notEqual(first.sessionId, second.sessionId);
const listed = await context.request(methods.agent.session.list, {
cwd: harness.workspaceRoot,
});
assert.deepEqual(
new Set(listed.sessions.map((session) => session.sessionId)),
new Set([first.sessionId, second.sessionId]),
);
const hostCwd = await realpath(harness.workspaceRoot);
assert.equal(
listed.sessions.every((session) => session.cwd === hostCwd),
true,
);

await assert.rejects(
context.request(methods.agent.session.close, { sessionId: first.sessionId }),
(error: unknown) => {
assert.ok(error instanceof RequestError);
assert.equal(error.code, -32601);
assert.deepEqual(error.data, { method: 'session/close' });
return true;
},
);
});

await harness.closeStdin();
assert.deepEqual(await harness.waitForExit(), { code: 0, signal: null });
assert.equal(harness.stderr, '');

const lines = harness.stdout.split(/\r?\n/u).filter((line) => line.trim().length > 0);
assert.ok(lines.length >= 5, 'expected initialize, new, list, and method responses');
for (const line of lines) {
const message: unknown = JSON.parse(line);
assertJsonRpcMessage(message);
}
},
{ startRuntimeHost: true },
);
});
});

function assertJsonRpcMessage(message: unknown): void {
Expand Down
Loading
Loading