2026-04-08 22:44:25 -05:00
|
|
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
|
|
|
|
|
|
|
|
const mocked = vi.hoisted(() => ({
|
|
|
|
|
start: vi.fn(async () => "http://127.0.0.1:8787"),
|
|
|
|
|
stop: vi.fn(async () => undefined),
|
|
|
|
|
logger: {
|
|
|
|
|
debug: vi.fn(),
|
|
|
|
|
error: vi.fn(),
|
|
|
|
|
info: vi.fn(),
|
|
|
|
|
warn: vi.fn(),
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock("headroom-ai", () => ({
|
|
|
|
|
compress: vi.fn(),
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock("../src/proxy-manager.js", () => ({
|
|
|
|
|
ProxyManager: class {
|
|
|
|
|
start = mocked.start;
|
|
|
|
|
stop = mocked.stop;
|
|
|
|
|
},
|
|
|
|
|
defaultLogger: mocked.logger,
|
|
|
|
|
}));
|
feat: add OpenClaw ContextEngine plugin (@headroom-ai/openclaw)
- HeadroomContextEngine: implements ContextEngine interface (bootstrap, ingest,
assemble, compact, afterTurn, prepareSubagentSpawn, dispose)
- ProxyManager: auto-detects running proxy or spawns one as child process,
health checks, restart on crash, graceful shutdown
- AgentMessage ↔ OpenAI format conversion (user, assistant/tool_use, toolResult)
- headroom_retrieve CCR tool: agent can retrieve original uncompressed content
- Plugin manifest (openclaw.plugin.json) with config schema
- 11 tests (6 unit + 5 integration), all passing
- Integration tested: 100 servers compressed, 3,735 tokens saved (82%)
2026-03-28 13:43:40 -07:00
|
|
|
|
2026-04-08 22:44:25 -05:00
|
|
|
import { HeadroomContextEngine } from "../src/engine.js";
|
2026-04-02 21:27:12 -05:00
|
|
|
|
|
|
|
|
afterEach(() => {
|
2026-04-08 22:44:25 -05:00
|
|
|
mocked.start.mockReset();
|
|
|
|
|
mocked.start.mockResolvedValue("http://127.0.0.1:8787");
|
|
|
|
|
mocked.stop.mockClear();
|
|
|
|
|
mocked.logger.debug.mockClear();
|
|
|
|
|
mocked.logger.error.mockClear();
|
|
|
|
|
mocked.logger.info.mockClear();
|
|
|
|
|
mocked.logger.warn.mockClear();
|
2026-04-02 21:27:12 -05:00
|
|
|
});
|
|
|
|
|
|
2026-04-08 22:44:25 -05:00
|
|
|
describe("HeadroomContextEngine proxy startup helpers", () => {
|
|
|
|
|
it("bootstraps by scheduling proxy startup when enabled", async () => {
|
|
|
|
|
const engine = new HeadroomContextEngine();
|
feat: add OpenClaw ContextEngine plugin (@headroom-ai/openclaw)
- HeadroomContextEngine: implements ContextEngine interface (bootstrap, ingest,
assemble, compact, afterTurn, prepareSubagentSpawn, dispose)
- ProxyManager: auto-detects running proxy or spawns one as child process,
health checks, restart on crash, graceful shutdown
- AgentMessage ↔ OpenAI format conversion (user, assistant/tool_use, toolResult)
- headroom_retrieve CCR tool: agent can retrieve original uncompressed content
- Plugin manifest (openclaw.plugin.json) with config schema
- 11 tests (6 unit + 5 integration), all passing
- Integration tested: 100 servers compressed, 3,735 tokens saved (82%)
2026-03-28 13:43:40 -07:00
|
|
|
|
2026-04-08 22:44:25 -05:00
|
|
|
await expect(
|
|
|
|
|
engine.bootstrap({
|
|
|
|
|
sessionId: "session-1",
|
|
|
|
|
sessionFile: "session.jsonl",
|
|
|
|
|
}),
|
|
|
|
|
).resolves.toEqual({
|
|
|
|
|
bootstrapped: true,
|
|
|
|
|
reason: "proxy startup scheduled",
|
|
|
|
|
});
|
|
|
|
|
expect(mocked.start).toHaveBeenCalledTimes(1);
|
feat: add OpenClaw ContextEngine plugin (@headroom-ai/openclaw)
- HeadroomContextEngine: implements ContextEngine interface (bootstrap, ingest,
assemble, compact, afterTurn, prepareSubagentSpawn, dispose)
- ProxyManager: auto-detects running proxy or spawns one as child process,
health checks, restart on crash, graceful shutdown
- AgentMessage ↔ OpenAI format conversion (user, assistant/tool_use, toolResult)
- headroom_retrieve CCR tool: agent can retrieve original uncompressed content
- Plugin manifest (openclaw.plugin.json) with config schema
- 11 tests (6 unit + 5 integration), all passing
- Integration tested: 100 servers compressed, 3,735 tokens saved (82%)
2026-03-28 13:43:40 -07:00
|
|
|
});
|
|
|
|
|
|
2026-04-08 22:44:25 -05:00
|
|
|
it("removes unsubscribed proxy listeners before notifying readiness", async () => {
|
|
|
|
|
const engine = new HeadroomContextEngine();
|
|
|
|
|
const first = vi.fn();
|
|
|
|
|
const second = vi.fn();
|
feat: add OpenClaw ContextEngine plugin (@headroom-ai/openclaw)
- HeadroomContextEngine: implements ContextEngine interface (bootstrap, ingest,
assemble, compact, afterTurn, prepareSubagentSpawn, dispose)
- ProxyManager: auto-detects running proxy or spawns one as child process,
health checks, restart on crash, graceful shutdown
- AgentMessage ↔ OpenAI format conversion (user, assistant/tool_use, toolResult)
- headroom_retrieve CCR tool: agent can retrieve original uncompressed content
- Plugin manifest (openclaw.plugin.json) with config schema
- 11 tests (6 unit + 5 integration), all passing
- Integration tested: 100 servers compressed, 3,735 tokens saved (82%)
2026-03-28 13:43:40 -07:00
|
|
|
|
2026-04-08 22:44:25 -05:00
|
|
|
const unsubscribeFirst = engine.onProxyReady(first);
|
|
|
|
|
engine.onProxyReady(second);
|
|
|
|
|
unsubscribeFirst();
|
2026-04-03 22:49:30 -05:00
|
|
|
|
2026-04-08 22:44:25 -05:00
|
|
|
engine.ensureProxyStarted();
|
|
|
|
|
await engine.ensureProxyUrl();
|
feat: add OpenClaw ContextEngine plugin (@headroom-ai/openclaw)
- HeadroomContextEngine: implements ContextEngine interface (bootstrap, ingest,
assemble, compact, afterTurn, prepareSubagentSpawn, dispose)
- ProxyManager: auto-detects running proxy or spawns one as child process,
health checks, restart on crash, graceful shutdown
- AgentMessage ↔ OpenAI format conversion (user, assistant/tool_use, toolResult)
- headroom_retrieve CCR tool: agent can retrieve original uncompressed content
- Plugin manifest (openclaw.plugin.json) with config schema
- 11 tests (6 unit + 5 integration), all passing
- Integration tested: 100 servers compressed, 3,735 tokens saved (82%)
2026-03-28 13:43:40 -07:00
|
|
|
|
2026-04-08 22:44:25 -05:00
|
|
|
expect(first).not.toHaveBeenCalled();
|
|
|
|
|
expect(second).toHaveBeenCalledWith("http://127.0.0.1:8787");
|
feat: add OpenClaw ContextEngine plugin (@headroom-ai/openclaw)
- HeadroomContextEngine: implements ContextEngine interface (bootstrap, ingest,
assemble, compact, afterTurn, prepareSubagentSpawn, dispose)
- ProxyManager: auto-detects running proxy or spawns one as child process,
health checks, restart on crash, graceful shutdown
- AgentMessage ↔ OpenAI format conversion (user, assistant/tool_use, toolResult)
- headroom_retrieve CCR tool: agent can retrieve original uncompressed content
- Plugin manifest (openclaw.plugin.json) with config schema
- 11 tests (6 unit + 5 integration), all passing
- Integration tested: 100 servers compressed, 3,735 tokens saved (82%)
2026-03-28 13:43:40 -07:00
|
|
|
});
|
|
|
|
|
|
2026-04-08 22:44:25 -05:00
|
|
|
it("returns the existing proxy URL without starting again", async () => {
|
|
|
|
|
const engine = new HeadroomContextEngine();
|
2026-03-28 23:46:03 -07:00
|
|
|
|
2026-04-08 22:44:25 -05:00
|
|
|
(engine as { proxyUrl: string | null }).proxyUrl = "http://127.0.0.1:8787";
|
feat: add OpenClaw ContextEngine plugin (@headroom-ai/openclaw)
- HeadroomContextEngine: implements ContextEngine interface (bootstrap, ingest,
assemble, compact, afterTurn, prepareSubagentSpawn, dispose)
- ProxyManager: auto-detects running proxy or spawns one as child process,
health checks, restart on crash, graceful shutdown
- AgentMessage ↔ OpenAI format conversion (user, assistant/tool_use, toolResult)
- headroom_retrieve CCR tool: agent can retrieve original uncompressed content
- Plugin manifest (openclaw.plugin.json) with config schema
- 11 tests (6 unit + 5 integration), all passing
- Integration tested: 100 servers compressed, 3,735 tokens saved (82%)
2026-03-28 13:43:40 -07:00
|
|
|
|
2026-04-08 22:44:25 -05:00
|
|
|
await expect(engine.ensureProxyUrl()).resolves.toBe("http://127.0.0.1:8787");
|
|
|
|
|
expect(mocked.start).not.toHaveBeenCalled();
|
feat: add OpenClaw ContextEngine plugin (@headroom-ai/openclaw)
- HeadroomContextEngine: implements ContextEngine interface (bootstrap, ingest,
assemble, compact, afterTurn, prepareSubagentSpawn, dispose)
- ProxyManager: auto-detects running proxy or spawns one as child process,
health checks, restart on crash, graceful shutdown
- AgentMessage ↔ OpenAI format conversion (user, assistant/tool_use, toolResult)
- headroom_retrieve CCR tool: agent can retrieve original uncompressed content
- Plugin manifest (openclaw.plugin.json) with config schema
- 11 tests (6 unit + 5 integration), all passing
- Integration tested: 100 servers compressed, 3,735 tokens saved (82%)
2026-03-28 13:43:40 -07:00
|
|
|
});
|
|
|
|
|
|
2026-04-08 22:44:25 -05:00
|
|
|
it("throws when proxy startup is disabled", async () => {
|
|
|
|
|
const engine = new HeadroomContextEngine({ enabled: false });
|
2026-04-07 12:47:09 -05:00
|
|
|
|
2026-04-08 22:44:25 -05:00
|
|
|
await expect(engine.ensureProxyUrl()).rejects.toThrow("Headroom proxy startup is disabled");
|
|
|
|
|
expect(mocked.start).not.toHaveBeenCalled();
|
2026-04-07 12:47:09 -05:00
|
|
|
});
|
|
|
|
|
|
2026-06-23 05:52:59 +02:00
|
|
|
it("does not emit an unhandledRejection when fire-and-forget startup fails", async () => {
|
|
|
|
|
mocked.start.mockReset();
|
|
|
|
|
mocked.start.mockRejectedValue(new Error("proxy boom"));
|
|
|
|
|
|
|
|
|
|
const engine = new HeadroomContextEngine();
|
|
|
|
|
const unhandled: unknown[] = [];
|
|
|
|
|
const onUnhandled = (reason: unknown) => unhandled.push(reason);
|
|
|
|
|
process.on("unhandledRejection", onUnhandled);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// Fire-and-forget: caller intentionally does not await.
|
|
|
|
|
engine.ensureProxyStarted();
|
|
|
|
|
// Let the startup promise settle and any microtasks/macrotasks flush.
|
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
|
|
|
|
|
|
|
|
expect(unhandled).toEqual([]);
|
|
|
|
|
expect(mocked.logger.warn).toHaveBeenCalledWith(
|
|
|
|
|
expect.stringContaining("Headroom proxy unavailable"),
|
|
|
|
|
);
|
|
|
|
|
} finally {
|
|
|
|
|
process.off("unhandledRejection", onUnhandled);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("stores the startup failure in getProxyStartupError()", async () => {
|
|
|
|
|
const failure = new Error("proxy boom");
|
|
|
|
|
mocked.start.mockReset();
|
|
|
|
|
mocked.start.mockRejectedValue(failure);
|
|
|
|
|
|
|
|
|
|
const engine = new HeadroomContextEngine();
|
|
|
|
|
expect(engine.getProxyStartupError()).toBeNull();
|
|
|
|
|
|
|
|
|
|
engine.ensureProxyStarted();
|
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
|
|
|
|
|
|
|
|
expect(engine.getProxyStartupError()).toBe(failure);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("allows retrying startup after a failure", async () => {
|
|
|
|
|
mocked.start.mockReset();
|
|
|
|
|
mocked.start
|
|
|
|
|
.mockRejectedValueOnce(new Error("proxy boom"))
|
|
|
|
|
.mockResolvedValueOnce("http://127.0.0.1:8787");
|
|
|
|
|
|
|
|
|
|
const engine = new HeadroomContextEngine();
|
|
|
|
|
|
|
|
|
|
engine.ensureProxyStarted();
|
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
|
|
|
expect(engine.getProxyStartupError()).toBeInstanceOf(Error);
|
|
|
|
|
|
|
|
|
|
// A second attempt is possible once the failed promise has cleared.
|
|
|
|
|
const url = await engine.ensureProxyUrl();
|
|
|
|
|
expect(url).toBe("http://127.0.0.1:8787");
|
|
|
|
|
expect(engine.getProxyStartupError()).toBeNull();
|
|
|
|
|
expect(mocked.start).toHaveBeenCalledTimes(2);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("ensureProxyUrl rejects cleanly on startup failure without unhandledRejection", async () => {
|
|
|
|
|
const failure = new Error("proxy boom");
|
|
|
|
|
mocked.start.mockReset();
|
|
|
|
|
mocked.start.mockRejectedValue(failure);
|
|
|
|
|
|
|
|
|
|
const engine = new HeadroomContextEngine();
|
|
|
|
|
const unhandled: unknown[] = [];
|
|
|
|
|
const onUnhandled = (reason: unknown) => unhandled.push(reason);
|
|
|
|
|
process.on("unhandledRejection", onUnhandled);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await expect(engine.ensureProxyUrl()).rejects.toBe(failure);
|
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
|
|
|
expect(unhandled).toEqual([]);
|
|
|
|
|
} finally {
|
|
|
|
|
process.off("unhandledRejection", onUnhandled);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("isolates and logs proxy-ready listener rejections", async () => {
|
|
|
|
|
const engine = new HeadroomContextEngine();
|
|
|
|
|
const failing = vi.fn(async () => {
|
|
|
|
|
throw new Error("listener boom");
|
|
|
|
|
});
|
|
|
|
|
const healthy = vi.fn();
|
|
|
|
|
|
|
|
|
|
engine.onProxyReady(failing);
|
|
|
|
|
engine.onProxyReady(healthy);
|
|
|
|
|
|
|
|
|
|
engine.ensureProxyStarted();
|
|
|
|
|
// ensureProxyUrl must still resolve despite the listener throwing.
|
|
|
|
|
await expect(engine.ensureProxyUrl()).resolves.toBe("http://127.0.0.1:8787");
|
|
|
|
|
|
|
|
|
|
expect(failing).toHaveBeenCalled();
|
|
|
|
|
expect(healthy).toHaveBeenCalledWith("http://127.0.0.1:8787");
|
|
|
|
|
expect(mocked.logger.warn).toHaveBeenCalledWith(
|
|
|
|
|
expect.stringContaining("Headroom proxy ready listener failed"),
|
|
|
|
|
);
|
|
|
|
|
expect(engine.getProxyStartupError()).toBeNull();
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-08 22:44:25 -05:00
|
|
|
it("schedules startup and returns original messages when assembling before proxy readiness", async () => {
|
2026-04-07 12:47:09 -05:00
|
|
|
const engine = new HeadroomContextEngine();
|
2026-04-08 22:44:25 -05:00
|
|
|
const messages = [{ role: "user", content: "hello" }];
|
|
|
|
|
|
|
|
|
|
await expect(
|
|
|
|
|
engine.assemble({
|
|
|
|
|
sessionId: "session-1",
|
|
|
|
|
messages,
|
|
|
|
|
}),
|
|
|
|
|
).resolves.toEqual({
|
2026-04-07 12:47:09 -05:00
|
|
|
messages,
|
2026-04-08 22:44:25 -05:00
|
|
|
estimatedTokens: 0,
|
2026-04-07 12:47:09 -05:00
|
|
|
});
|
2026-04-08 22:44:25 -05:00
|
|
|
expect(mocked.start).toHaveBeenCalledTimes(1);
|
2026-04-07 12:47:09 -05:00
|
|
|
});
|
|
|
|
|
});
|