mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-09-30 12:39:42 +02:00
Follow-ups to the delegating statusline shim from discussion #405, answering the four design questions and the Docker one raised there. Docker cases: the injected command is now a self-selecting shell guard, `if [ -x <node> ] && [ -f <shim> ]; then exec <node> <shim>; fi;` followed by the inline curl exporter. A Docker case bind-mounts the workspace, and with it settings.local.json, at the same absolute path inside the container, but neither the host's node nor ~/.codeman exists there, so a bare shim command would have rendered a broken statusline in every container session. The same string now runs the shim on the host and the curl inside the container. The settings-save injection loop needs no docker guard for that reason; it does skip remote attaches now, whose workingDir is a user@host pseudo-path. Settings precedence: the shim reads exactly the three files Claude Code documents, .claude/settings.local.json and .claude/settings.json under workspace.project_dir (the launch directory), then ~/.claude/settings.json. No ancestor walk and no user-level settings.local.json: delegating to a command Claude Code would have ignored is the original failure in a new coat. The bare word: all three paths that produced `codeman` are gone. The route answers an unknown session with an empty body, formatSessionStatusText() returns '' with nothing to show, and the inline fallback ends in `|| true` (plus `curl -f`, so an HTTP error body never renders as the statusline). The shim also treats a literal `codeman` from an older server as no telemetry and prints nothing rather than a brand word when it has neither a delegate nor a footer, which is what Claude Code shows a user with no statusline of their own. Removal: turning the plan-usage chip OFF now takes the exporter out of the workspaces of the caller's live Claude sessions. statusLineTelemetry:false is sent only by the save that flips the chip off on that device (statusLineTelemetryAction() in settings-ui.js), so a phone whose chip was never on cannot strip the exporter a desktop's chip depends on; a second device with the chip still on re-injects on its next save or session create. Nothing in src/ called applyStatusLineConfig(dir, false) before. Tests run the generated shim AND the injected command as real subprocesses (the fallback half with the shim path pointed at nothing, the container's view), plus both directions of the action field through PUT /api/settings. Measured against the live server: a render costs ~85 ms through the shim versus ~26 ms for the old inline curl (node start ~33 ms, the rest TLS to the loopback HTTPS server plus the delegate spawn), off the input path. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
106 lines
4.5 KiB
TypeScript
106 lines
4.5 KiB
TypeScript
/**
|
|
* Route tests for POST /api/status-telemetry — the Claude statusline exporter
|
|
* endpoint that feeds the header "Plan Usage Limits" chip.
|
|
*
|
|
* Covers: broadcast on real telemetry + footer print-through, unknown-session
|
|
* skip, per-session change-detection (dedup), rebroadcast on a displayed change,
|
|
* NO rebroadcast on context-only drift, null-tolerance of Claude's undocumented
|
|
* fields (the .nullish() schema — the project's recurring .optional()/null trap),
|
|
* and 400 on a malformed body.
|
|
*/
|
|
import { describe, it, expect, beforeEach } from 'vitest';
|
|
import { createRouteTestHarness, type RouteTestHarness } from './_route-test-utils.js';
|
|
import { registerStatusTelemetryRoutes } from '../../src/web/routes/status-telemetry-routes.js';
|
|
import { SessionStatusTelemetry } from '../../src/web/sse-events.js';
|
|
|
|
const SID = 'test-session-1'; // default id created by createMockRouteContext
|
|
|
|
const REAL = {
|
|
rate_limits: {
|
|
five_hour: { used_percentage: 15, resets_at: 1781409000 },
|
|
seven_day: { used_percentage: 34, resets_at: 1781827200 },
|
|
},
|
|
context_window: { used_percentage: 56, total_input_tokens: 562411, total_output_tokens: 1188 },
|
|
cost: { total_cost_usd: 0.0415 },
|
|
model: { display_name: 'Opus 4.8 (1M context)' },
|
|
};
|
|
|
|
describe('POST /api/status-telemetry', () => {
|
|
let h: RouteTestHarness;
|
|
|
|
beforeEach(async () => {
|
|
h = await createRouteTestHarness(registerStatusTelemetryRoutes, { sessionId: SID });
|
|
});
|
|
|
|
const post = (body: unknown) => h.app.inject({ method: 'POST', url: '/api/status-telemetry', payload: body });
|
|
|
|
it('broadcasts plan-usage telemetry and returns the session-status footer', async () => {
|
|
const res = await post({ sessionId: SID, data: REAL });
|
|
expect(res.statusCode).toBe(200);
|
|
expect(res.headers['content-type']).toContain('text/plain');
|
|
expect(res.body).toBe('Opus 4.8 (1M context) in:562,411 out:1,188 ctx:56%');
|
|
expect(h.ctx.broadcast).toHaveBeenCalledTimes(1);
|
|
const [event, payload] = h.ctx.broadcast.mock.calls[0];
|
|
expect(event).toBe(SessionStatusTelemetry);
|
|
expect(payload).toMatchObject({
|
|
sessionId: SID,
|
|
fiveHour: { usedPercentage: 15 },
|
|
sevenDay: { usedPercentage: 34 },
|
|
});
|
|
});
|
|
|
|
it('does not broadcast for an unknown session, and answers an empty body', async () => {
|
|
// Never the bare brand word: the exporter prints this answer as the
|
|
// statusline, and `codeman` on the statusline of every hand-run `claude` in
|
|
// a managed repo is the symptom discussion #405 opened with.
|
|
const res = await post({ sessionId: 'does-not-exist', data: REAL });
|
|
expect(res.statusCode).toBe(200);
|
|
expect(res.body).toBe('');
|
|
expect(h.ctx.broadcast).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('dedups identical telemetry — rebroadcasts only once', async () => {
|
|
await post({ sessionId: SID, data: REAL });
|
|
await post({ sessionId: SID, data: REAL });
|
|
expect(h.ctx.broadcast).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('rebroadcasts when a displayed window percentage changes', async () => {
|
|
await post({ sessionId: SID, data: REAL });
|
|
const moved = {
|
|
...REAL,
|
|
rate_limits: { ...REAL.rate_limits, five_hour: { used_percentage: 16, resets_at: 1781409000 } },
|
|
};
|
|
await post({ sessionId: SID, data: moved });
|
|
expect(h.ctx.broadcast).toHaveBeenCalledTimes(2);
|
|
});
|
|
|
|
it('does NOT rebroadcast on context-only drift (the chip never shows context %)', async () => {
|
|
await post({ sessionId: SID, data: REAL });
|
|
await post({ sessionId: SID, data: { ...REAL, context_window: { ...REAL.context_window, used_percentage: 91 } } });
|
|
expect(h.ctx.broadcast).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("tolerates null in Claude's undocumented fields (no 400) and ignores them", async () => {
|
|
const res = await post({
|
|
sessionId: SID,
|
|
data: {
|
|
rate_limits: { five_hour: { used_percentage: 20, resets_at: 1781409000 }, seven_day: null },
|
|
cost: { total_cost_usd: null },
|
|
model: { display_name: null },
|
|
context_window: { used_percentage: null, total_input_tokens: null, total_output_tokens: null },
|
|
},
|
|
});
|
|
expect(res.statusCode).toBe(200);
|
|
expect(h.ctx.broadcast).toHaveBeenCalledTimes(1);
|
|
const [, payload] = h.ctx.broadcast.mock.calls[0];
|
|
expect(payload).toMatchObject({ fiveHour: { usedPercentage: 20 } });
|
|
expect(payload.sevenDay).toBeUndefined();
|
|
});
|
|
|
|
it('rejects a malformed body (missing sessionId) with 400', async () => {
|
|
const res = await post({ data: REAL });
|
|
expect(res.statusCode).toBe(400);
|
|
});
|
|
});
|