mirror of
https://github.com/catlog22/Claude-Code-Workflow.git
synced 2026-03-28 20:01:17 +08:00
feat: add orchestrator execution engine, observability panel, and LSP document caching
Wire FlowExecutor into orchestrator routes for actual flow execution with
pause/resume/stop lifecycle management. Add CLI session audit system with
audit-routes backend and Observability tab in IssueHub frontend. Introduce
cli-session-mux for cross-workspace session routing and QueueSendToOrchestrator
UI component. Normalize frontend API response handling for { data: ... }
wrapper format and propagate projectPath through flow hooks.
In codex-lens, add per-server opened-document cache in StandaloneLspManager
to avoid redundant didOpen notifications (using didChange for updates), and
skip warmup delay for already-warmed LSP server instances in ChainSearchEngine.
This commit is contained in:
157
ccw/tests/audit-routes.test.js
Normal file
157
ccw/tests/audit-routes.test.js
Normal file
@@ -0,0 +1,157 @@
|
||||
/**
|
||||
* Integration tests for audit routes.
|
||||
*
|
||||
* Targets runtime implementation shipped in `ccw/dist`.
|
||||
*/
|
||||
|
||||
import { after, before, describe, it } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import http from 'node:http';
|
||||
import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
|
||||
const auditRoutesUrl = new URL('../dist/core/routes/audit-routes.js', import.meta.url);
|
||||
auditRoutesUrl.searchParams.set('t', String(Date.now()));
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
let mod;
|
||||
|
||||
async function requestJson(baseUrl, method, path) {
|
||||
const url = new URL(path, baseUrl);
|
||||
return new Promise((resolve, reject) => {
|
||||
const req = http.request(
|
||||
url,
|
||||
{ method, headers: { Accept: 'application/json' } },
|
||||
(res) => {
|
||||
let responseBody = '';
|
||||
res.on('data', (chunk) => {
|
||||
responseBody += chunk.toString();
|
||||
});
|
||||
res.on('end', () => {
|
||||
let json = null;
|
||||
try {
|
||||
json = responseBody ? JSON.parse(responseBody) : null;
|
||||
} catch {
|
||||
json = null;
|
||||
}
|
||||
resolve({ status: res.statusCode || 0, json, text: responseBody });
|
||||
});
|
||||
}
|
||||
);
|
||||
req.on('error', reject);
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
describe('audit routes integration', async () => {
|
||||
let server = null;
|
||||
let baseUrl = '';
|
||||
let projectRoot = '';
|
||||
|
||||
before(async () => {
|
||||
projectRoot = mkdtempSync(join(tmpdir(), 'ccw-audit-routes-project-'));
|
||||
|
||||
mod = await import(auditRoutesUrl.href);
|
||||
|
||||
server = http.createServer(async (req, res) => {
|
||||
const url = new URL(req.url || '/', 'http://localhost');
|
||||
const pathname = url.pathname;
|
||||
|
||||
const ctx = {
|
||||
pathname,
|
||||
url,
|
||||
req,
|
||||
res,
|
||||
initialPath: projectRoot,
|
||||
handlePostRequest() {},
|
||||
broadcastToClients() {},
|
||||
};
|
||||
|
||||
try {
|
||||
const handled = await mod.handleAuditRoutes(ctx);
|
||||
if (!handled) {
|
||||
res.writeHead(404, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ error: 'Not Found' }));
|
||||
}
|
||||
} catch (err) {
|
||||
res.writeHead(500, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ error: err instanceof Error ? err.message : String(err) }));
|
||||
}
|
||||
});
|
||||
|
||||
await new Promise((resolve) => server.listen(0, () => resolve()));
|
||||
const addr = server.address();
|
||||
const port = typeof addr === 'object' && addr ? addr.port : 0;
|
||||
baseUrl = `http://127.0.0.1:${port}`;
|
||||
});
|
||||
|
||||
after(() => {
|
||||
if (server) server.close();
|
||||
if (projectRoot) rmSync(projectRoot, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it('returns empty list when audit file is missing', async () => {
|
||||
const r = await requestJson(baseUrl, 'GET', `/api/audit/cli-sessions?path=${encodeURIComponent(projectRoot)}`);
|
||||
assert.equal(r.status, 200);
|
||||
assert.equal(r.json.success, true);
|
||||
assert.deepEqual(r.json.data.events, []);
|
||||
assert.equal(r.json.data.total, 0);
|
||||
});
|
||||
|
||||
it('lists events newest-first and supports filters', async () => {
|
||||
const auditDir = join(projectRoot, '.workflow', 'audit');
|
||||
mkdirSync(auditDir, { recursive: true });
|
||||
const filePath = join(auditDir, 'cli-sessions.jsonl');
|
||||
|
||||
const ev1 = {
|
||||
type: 'session_created',
|
||||
timestamp: '2026-02-09T00:00:00.000Z',
|
||||
projectRoot,
|
||||
sessionKey: 's-1',
|
||||
tool: 'claude',
|
||||
resumeKey: 'ISSUE-1',
|
||||
details: { a: 1 },
|
||||
};
|
||||
const ev2 = {
|
||||
type: 'session_execute',
|
||||
timestamp: '2026-02-09T00:00:01.000Z',
|
||||
projectRoot,
|
||||
sessionKey: 's-1',
|
||||
tool: 'claude',
|
||||
resumeKey: 'ISSUE-1',
|
||||
details: { q: 'hello' },
|
||||
};
|
||||
const ev3 = {
|
||||
type: 'session_send',
|
||||
timestamp: '2026-02-09T00:00:02.000Z',
|
||||
projectRoot,
|
||||
sessionKey: 's-2',
|
||||
tool: 'codex',
|
||||
resumeKey: 'ISSUE-2',
|
||||
details: { bytes: 10 },
|
||||
};
|
||||
|
||||
writeFileSync(filePath, [ev1, ev2, ev3].map((e) => JSON.stringify(e)).join('\n') + '\n', 'utf8');
|
||||
|
||||
const all = await requestJson(baseUrl, 'GET', `/api/audit/cli-sessions?path=${encodeURIComponent(projectRoot)}&limit=10&offset=0`);
|
||||
assert.equal(all.status, 200);
|
||||
assert.equal(all.json.success, true);
|
||||
assert.equal(all.json.data.total, 3);
|
||||
// Newest-first
|
||||
assert.equal(all.json.data.events[0].type, 'session_send');
|
||||
assert.equal(all.json.data.events[0].sessionKey, 's-2');
|
||||
|
||||
const bySession = await requestJson(baseUrl, 'GET', `/api/audit/cli-sessions?path=${encodeURIComponent(projectRoot)}&sessionKey=s-1`);
|
||||
assert.equal(bySession.json.data.total, 2);
|
||||
assert.equal(bySession.json.data.events[0].type, 'session_execute');
|
||||
|
||||
const byType = await requestJson(baseUrl, 'GET', `/api/audit/cli-sessions?path=${encodeURIComponent(projectRoot)}&type=session_created`);
|
||||
assert.equal(byType.json.data.total, 1);
|
||||
assert.equal(byType.json.data.events[0].type, 'session_created');
|
||||
|
||||
const bySearch = await requestJson(baseUrl, 'GET', `/api/audit/cli-sessions?path=${encodeURIComponent(projectRoot)}&q=hello`);
|
||||
assert.equal(bySearch.json.data.total, 1);
|
||||
assert.equal(bySearch.json.data.events[0].type, 'session_execute');
|
||||
});
|
||||
});
|
||||
112
ccw/tests/flow-executor-send-to-session.test.js
Normal file
112
ccw/tests/flow-executor-send-to-session.test.js
Normal file
@@ -0,0 +1,112 @@
|
||||
/**
|
||||
* Integration test for FlowExecutor tmux-like routing to PTY sessions.
|
||||
*
|
||||
* Ensures that delivery=sendToSession:
|
||||
* - locates the target PTY session even if the executor workflowDir differs
|
||||
* - records a session_execute audit event for Observability panel
|
||||
*/
|
||||
|
||||
import { after, before, describe, it } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { mkdtempSync, rmSync, readFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
|
||||
const flowExecutorUrl = new URL('../dist/core/services/flow-executor.js', import.meta.url);
|
||||
flowExecutorUrl.searchParams.set('t', String(Date.now()));
|
||||
|
||||
const cliSessionMuxFileUrl = new URL('../dist/core/services/cli-session-mux.js', import.meta.url).href;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
let FlowExecutorMod;
|
||||
|
||||
describe('flow-executor sendToSession routing', async () => {
|
||||
let workflowDir = '';
|
||||
let sessionRoot = '';
|
||||
let sessionKey = '';
|
||||
|
||||
before(async () => {
|
||||
workflowDir = mkdtempSync(join(tmpdir(), 'ccw-flowexec-workflow-'));
|
||||
sessionRoot = mkdtempSync(join(tmpdir(), 'ccw-flowexec-sessionroot-'));
|
||||
|
||||
sessionKey = 'cli-session-test-1';
|
||||
|
||||
const fakeManager = {
|
||||
hasSession: (key) => key === sessionKey,
|
||||
getProjectRoot: () => sessionRoot,
|
||||
getSession: (key) => (key === sessionKey ? { sessionKey, workingDir: sessionRoot, tool: 'claude' } : null),
|
||||
execute: (key, options) => {
|
||||
if (key !== sessionKey) throw new Error('Session not found');
|
||||
return { executionId: 'exec-routed-1', command: `echo routed ${options?.tool || ''}` };
|
||||
},
|
||||
};
|
||||
|
||||
const muxMod = await import(cliSessionMuxFileUrl);
|
||||
muxMod.cliSessionMux.findCliSessionManager = (key) => (key === sessionKey ? fakeManager : null);
|
||||
muxMod.cliSessionMux.getCliSessionManager = () => fakeManager;
|
||||
|
||||
FlowExecutorMod = await import(flowExecutorUrl.href);
|
||||
});
|
||||
|
||||
after(() => {
|
||||
if (workflowDir) rmSync(workflowDir, { recursive: true, force: true });
|
||||
if (sessionRoot) rmSync(sessionRoot, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it('routes execution to the target session and appends audit event', async () => {
|
||||
const flowId = 'flow-test-send-to-session';
|
||||
const execId = `exec-${Date.now()}`;
|
||||
const now = new Date().toISOString();
|
||||
|
||||
const flow = {
|
||||
id: flowId,
|
||||
name: 'SendToSession Flow',
|
||||
description: '',
|
||||
version: '1.0.0',
|
||||
created_at: now,
|
||||
updated_at: now,
|
||||
nodes: [
|
||||
{
|
||||
id: 'node-1',
|
||||
type: 'prompt-template',
|
||||
position: { x: 0, y: 0 },
|
||||
data: {
|
||||
label: 'SendToSession',
|
||||
instruction: 'echo hello',
|
||||
tool: 'claude',
|
||||
mode: 'analysis',
|
||||
delivery: 'sendToSession',
|
||||
targetSessionKey: sessionKey,
|
||||
resumeKey: 'ISSUE-TEST',
|
||||
resumeStrategy: 'nativeResume',
|
||||
},
|
||||
},
|
||||
],
|
||||
edges: [],
|
||||
variables: {},
|
||||
metadata: { source: 'local' },
|
||||
};
|
||||
|
||||
const executor = new FlowExecutorMod.FlowExecutor(flow, execId, workflowDir);
|
||||
const state = await executor.execute({});
|
||||
|
||||
assert.equal(state.status, 'completed');
|
||||
|
||||
const auditPath = join(sessionRoot, '.workflow', 'audit', 'cli-sessions.jsonl');
|
||||
const raw = readFileSync(auditPath, 'utf8');
|
||||
const events = raw
|
||||
.split('\n')
|
||||
.map((l) => l.trim())
|
||||
.filter(Boolean)
|
||||
.map((l) => JSON.parse(l));
|
||||
|
||||
const ev = events.find((e) => e.type === 'session_execute' && e.sessionKey === sessionKey);
|
||||
assert.ok(ev, 'expected session_execute audit event');
|
||||
assert.equal(ev.tool, 'claude');
|
||||
assert.equal(ev.resumeKey, 'ISSUE-TEST');
|
||||
assert.ok(ev.details);
|
||||
assert.equal(ev.details.delivery, 'sendToSession');
|
||||
assert.equal(ev.details.flowId, flowId);
|
||||
assert.equal(ev.details.nodeId, 'node-1');
|
||||
});
|
||||
});
|
||||
183
ccw/tests/orchestrator-execution.test.js
Normal file
183
ccw/tests/orchestrator-execution.test.js
Normal file
@@ -0,0 +1,183 @@
|
||||
/**
|
||||
* Integration tests for orchestrator execution wiring.
|
||||
*
|
||||
* Verifies that POST /api/orchestrator/flows/:id/execute triggers the FlowExecutor
|
||||
* and persists a completed execution for an empty flow (no nodes).
|
||||
*/
|
||||
|
||||
import { after, before, describe, it, mock } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import http from 'node:http';
|
||||
import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
|
||||
const orchestratorRoutesUrl = new URL('../dist/core/routes/orchestrator-routes.js', import.meta.url);
|
||||
orchestratorRoutesUrl.searchParams.set('t', String(Date.now()));
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
let mod;
|
||||
|
||||
async function requestJson(baseUrl, method, path, body) {
|
||||
const url = new URL(path, baseUrl);
|
||||
const payload = body === undefined ? null : Buffer.from(JSON.stringify(body), 'utf8');
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const req = http.request(
|
||||
url,
|
||||
{
|
||||
method,
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
...(payload
|
||||
? { 'Content-Type': 'application/json', 'Content-Length': String(payload.length) }
|
||||
: {}),
|
||||
},
|
||||
},
|
||||
(res) => {
|
||||
let responseBody = '';
|
||||
res.on('data', (chunk) => {
|
||||
responseBody += chunk.toString();
|
||||
});
|
||||
res.on('end', () => {
|
||||
let json = null;
|
||||
try {
|
||||
json = responseBody ? JSON.parse(responseBody) : null;
|
||||
} catch {
|
||||
json = null;
|
||||
}
|
||||
resolve({ status: res.statusCode || 0, json, text: responseBody });
|
||||
});
|
||||
}
|
||||
);
|
||||
req.on('error', reject);
|
||||
if (payload) req.write(payload);
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
function handlePostRequest(req, res, handler) {
|
||||
let body = '';
|
||||
req.on('data', (chunk) => {
|
||||
body += chunk.toString();
|
||||
});
|
||||
req.on('end', async () => {
|
||||
try {
|
||||
const parsed = body ? JSON.parse(body) : {};
|
||||
const result = await handler(parsed);
|
||||
|
||||
if (result?.error) {
|
||||
res.writeHead(result.status || 500, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ error: result.error }));
|
||||
} else {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify(result));
|
||||
}
|
||||
} catch (err) {
|
||||
res.writeHead(500, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ error: err instanceof Error ? err.message : String(err) }));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
describe('orchestrator execution integration', async () => {
|
||||
let server = null;
|
||||
let baseUrl = '';
|
||||
let projectRoot = '';
|
||||
|
||||
before(async () => {
|
||||
projectRoot = mkdtempSync(join(tmpdir(), 'ccw-orchestrator-project-'));
|
||||
|
||||
// Reduce noise from executor internals during tests
|
||||
mock.method(console, 'log', () => {});
|
||||
mock.method(console, 'error', () => {});
|
||||
|
||||
mod = await import(orchestratorRoutesUrl.href);
|
||||
|
||||
// Seed a minimal empty flow (no nodes)
|
||||
const flowsDir = join(projectRoot, '.workflow', '.orchestrator', 'flows');
|
||||
mkdirSync(flowsDir, { recursive: true });
|
||||
const flowId = 'flow-test-empty';
|
||||
const now = new Date().toISOString();
|
||||
writeFileSync(
|
||||
join(flowsDir, `${flowId}.json`),
|
||||
JSON.stringify({
|
||||
id: flowId,
|
||||
name: 'Empty Flow',
|
||||
description: '',
|
||||
version: '1.0.0',
|
||||
created_at: now,
|
||||
updated_at: now,
|
||||
nodes: [],
|
||||
edges: [],
|
||||
variables: {},
|
||||
metadata: { source: 'local' },
|
||||
}, null, 2),
|
||||
'utf8'
|
||||
);
|
||||
|
||||
server = http.createServer(async (req, res) => {
|
||||
const url = new URL(req.url || '/', 'http://localhost');
|
||||
const pathname = url.pathname;
|
||||
|
||||
const ctx = {
|
||||
pathname,
|
||||
url,
|
||||
req,
|
||||
res,
|
||||
initialPath: projectRoot,
|
||||
handlePostRequest,
|
||||
broadcastToClients() {},
|
||||
};
|
||||
|
||||
try {
|
||||
const handled = await mod.handleOrchestratorRoutes(ctx);
|
||||
if (!handled) {
|
||||
res.writeHead(404, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ error: 'Not Found' }));
|
||||
}
|
||||
} catch (err) {
|
||||
res.writeHead(500, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ error: err instanceof Error ? err.message : String(err) }));
|
||||
}
|
||||
});
|
||||
|
||||
await new Promise((resolve) => server.listen(0, () => resolve()));
|
||||
const addr = server.address();
|
||||
const port = typeof addr === 'object' && addr ? addr.port : 0;
|
||||
baseUrl = `http://127.0.0.1:${port}`;
|
||||
});
|
||||
|
||||
after(() => {
|
||||
if (server) server.close();
|
||||
if (projectRoot) rmSync(projectRoot, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it('executes an empty flow to completion', async () => {
|
||||
const start = await requestJson(baseUrl, 'POST', '/api/orchestrator/flows/flow-test-empty/execute', {});
|
||||
assert.equal(start.status, 200);
|
||||
assert.equal(start.json.success, true);
|
||||
assert.ok(start.json.data.execId);
|
||||
|
||||
const execId = start.json.data.execId;
|
||||
|
||||
// Poll until completed (should be very fast for empty flow)
|
||||
let state = null;
|
||||
for (let i = 0; i < 50; i++) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await new Promise((r) => setTimeout(r, 20));
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const r = await requestJson(baseUrl, 'GET', `/api/orchestrator/executions/${encodeURIComponent(execId)}`);
|
||||
if (r.status === 200 && r.json?.success) {
|
||||
state = r.json.data;
|
||||
if (state.status === 'completed') break;
|
||||
}
|
||||
}
|
||||
|
||||
assert.ok(state, 'expected execution state to be readable');
|
||||
assert.equal(state.status, 'completed');
|
||||
assert.ok(state.startedAt);
|
||||
assert.ok(state.completedAt);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user