1c5521cf2e
UI
- No animation runs forever: Sagi moods, the custom pet sprite, status dots
(pulse-dot, pulse-slow, ping) and the chat shimmer play a few cycles and
rest. An infinite animation kept the browser drawing 60 frames a second.
- useSagiBrain: frames that only move lastActivityAt by <1 s keep the old
state; the 1 s tick re-renders only when the mood changes.
- ConversationView and MessageList are memoized and PaneChat passes stable
callbacks: typing in the chat box no longer re-renders the conversation.
- The chat's 700 ms menu poll reads plain text first and scans the terminal
cell by cell (reusing one cell) only when a row could be a menu cursor row.
Server
- GET /sessions/:id/transcript?after=N skips lines <= N before JSON.parse.
- GET /sessions/:id/outline is cached until the session's events change.
- broadcastLane uses the cached tmux.liveSessions(); tmux.hasSession answers
"yes" from the fresh shared list instead of a synchronous spawn (50-150 ms
per psmux call on Windows, blocking terminal echo).
- pane-mode polls every 150 ms; writeHourly writes only changed hour buckets.
- /api/metrics: agenthub_event_loop_delay_seconds{quantile}.
Hooks
- PreToolUse/PostToolUse run with "async": true (settings installer,
plugin.json, S3 managed settings): Claude Code no longer waits a Node start
before and after every tool call.
Committed with --no-verify at the owner's request: the pre-commit full server
suite fails on 9 tests that fail identically on 0c2f498 (lane-sync merge: no
git identity on this host; read-pool; resolveHost; codex-usage 1 ms flake).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
166 lines
5.9 KiB
JavaScript
166 lines
5.9 KiB
JavaScript
/**
|
|
* @file Tests the host-only guard in scripts/install-hooks.js (issue #193):
|
|
* the installer must refuse to write a container-internal handler path into a
|
|
* (possibly bind-mounted) host ~/.claude/settings.json. Container detection is
|
|
* driven deterministically via AGENTHUB_FORCE_CONTAINER / AGENTHUB_FORCE_HOST so these
|
|
* tests pass whether or not the CI runner itself is containerized.
|
|
* @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
|
|
*/
|
|
|
|
const { describe, it, beforeEach, after } = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
const fs = require("fs");
|
|
const os = require("os");
|
|
const path = require("path");
|
|
|
|
// Point the installer at a throwaway CLAUDE_HOME BEFORE requiring it — the
|
|
// settings path is resolved at module load. (`node --test` isolates each test
|
|
// file in its own process, so this does not leak into other suites.)
|
|
const TMP_HOME = fs.mkdtempSync(path.join(os.tmpdir(), "agenthub-hooks-"));
|
|
process.env.CLAUDE_HOME = TMP_HOME;
|
|
const SETTINGS = path.join(TMP_HOME, "settings.json");
|
|
|
|
const { installHooks, isInsideContainer } = require("../../scripts/install-hooks");
|
|
|
|
const HOOK_TYPES = [
|
|
"PreToolUse",
|
|
"PostToolUse",
|
|
"Stop",
|
|
"SubagentStop",
|
|
"Notification",
|
|
"SessionStart",
|
|
"SessionEnd",
|
|
"UserPromptSubmit",
|
|
];
|
|
|
|
function clearEnv() {
|
|
delete process.env.AGENTHUB_FORCE_CONTAINER;
|
|
delete process.env.AGENTHUB_FORCE_HOST;
|
|
delete process.env.AGENTHUB_ALLOW_CONTAINER_HOOKS;
|
|
}
|
|
|
|
function rmSettings() {
|
|
try {
|
|
fs.unlinkSync(SETTINGS);
|
|
} catch {
|
|
/* not present */
|
|
}
|
|
}
|
|
|
|
describe("install-hooks host-only guard (#193)", () => {
|
|
beforeEach(() => {
|
|
clearEnv();
|
|
rmSettings();
|
|
});
|
|
|
|
after(() => {
|
|
clearEnv();
|
|
try {
|
|
fs.rmSync(TMP_HOME, { recursive: true, force: true });
|
|
} catch {
|
|
/* best effort */
|
|
}
|
|
});
|
|
|
|
it("refuses inside a container and writes no settings file", () => {
|
|
process.env.AGENTHUB_FORCE_CONTAINER = "1";
|
|
const ok = installHooks(true);
|
|
assert.equal(ok, false);
|
|
assert.equal(
|
|
fs.existsSync(SETTINGS),
|
|
false,
|
|
"settings.json must not be created in a container"
|
|
);
|
|
});
|
|
|
|
it("writes when the explicit container override is set", () => {
|
|
process.env.AGENTHUB_FORCE_CONTAINER = "1";
|
|
process.env.AGENTHUB_ALLOW_CONTAINER_HOOKS = "1";
|
|
const ok = installHooks(true);
|
|
assert.equal(ok, true);
|
|
assert.ok(fs.existsSync(SETTINGS));
|
|
const settings = JSON.parse(fs.readFileSync(SETTINGS, "utf8"));
|
|
for (const type of HOOK_TYPES) {
|
|
assert.ok(Array.isArray(settings.hooks[type]), `missing hook list for ${type}`);
|
|
assert.match(JSON.stringify(settings.hooks[type]), /hook-handler\.js/, `${type} not wired`);
|
|
}
|
|
});
|
|
|
|
it("writes on a host (not a container)", () => {
|
|
process.env.AGENTHUB_FORCE_HOST = "1";
|
|
const ok = installHooks(true);
|
|
assert.equal(ok, true);
|
|
assert.ok(fs.existsSync(SETTINGS));
|
|
});
|
|
|
|
it("is idempotent — re-running updates in place with no duplicate entries", () => {
|
|
process.env.AGENTHUB_FORCE_HOST = "1";
|
|
installHooks(true);
|
|
installHooks(true);
|
|
const settings = JSON.parse(fs.readFileSync(SETTINGS, "utf8"));
|
|
const ours = settings.hooks.PreToolUse.filter((e) =>
|
|
JSON.stringify(e).includes("hook-handler.js")
|
|
);
|
|
assert.equal(ours.length, 1, "must not duplicate our hook entry on re-run");
|
|
});
|
|
|
|
it("with the agenthub plugin enabled, adds nothing and removes our duplicate entries", () => {
|
|
process.env.AGENTHUB_FORCE_HOST = "1";
|
|
const foreign = { hooks: [{ type: "command", command: "my-own-hook.sh" }], matcher: "*" };
|
|
const ours = {
|
|
hooks: [{ type: "command", command: 'node "/x/scripts/hook-handler.js" Stop' }],
|
|
};
|
|
fs.writeFileSync(
|
|
SETTINGS,
|
|
JSON.stringify({
|
|
enabledPlugins: { "agenthub@sg": true },
|
|
hooks: { PreToolUse: [foreign], Stop: [ours] },
|
|
})
|
|
);
|
|
assert.equal(installHooks(true), true);
|
|
const settings = JSON.parse(fs.readFileSync(SETTINGS, "utf8"));
|
|
assert.doesNotMatch(JSON.stringify(settings.hooks), /hook-handler\.js/);
|
|
assert.deepEqual(settings.hooks.PreToolUse, [foreign]);
|
|
assert.equal(settings.hooks.Stop, undefined);
|
|
assert.ok(fs.existsSync(`${SETTINGS}.agenthub-bak`), "backs the file up before writing");
|
|
});
|
|
|
|
it("installs as before when the plugin is disabled", () => {
|
|
process.env.AGENTHUB_FORCE_HOST = "1";
|
|
fs.writeFileSync(SETTINGS, JSON.stringify({ enabledPlugins: { "agenthub@sg": false } }));
|
|
assert.equal(installHooks(true), true);
|
|
const settings = JSON.parse(fs.readFileSync(SETTINGS, "utf8"));
|
|
assert.match(JSON.stringify(settings.hooks.PreToolUse), /hook-handler\.js/);
|
|
});
|
|
|
|
it("isInsideContainer honors the force flags", () => {
|
|
process.env.AGENTHUB_FORCE_CONTAINER = "1";
|
|
assert.equal(isInsideContainer(), true);
|
|
delete process.env.AGENTHUB_FORCE_CONTAINER;
|
|
process.env.AGENTHUB_FORCE_HOST = "1";
|
|
assert.equal(isInsideContainer(), false);
|
|
});
|
|
});
|
|
|
|
describe("async hooks", () => {
|
|
it("runs only the per-tool-call hooks in the background, in settings and the plugin alike", () => {
|
|
const { makeHookEntry, ASYNC_HOOKS } = require("../../scripts/install-hooks");
|
|
const plugin = require("../../.claude-plugin/plugin.json");
|
|
assert.deepEqual([...ASYNC_HOOKS].sort(), ["PostToolUse", "PreToolUse"]);
|
|
for (const type of HOOK_TYPES) {
|
|
const want = ASYNC_HOOKS.has(type) ? true : undefined;
|
|
assert.equal(makeHookEntry(type).hooks[0].async, want, `settings ${type}`);
|
|
assert.equal(plugin.hooks[type][0].hooks[0].async, want, `plugin ${type}`);
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("HOOK_TYPES", () => {
|
|
it("is the event list the plugin ships, so the hook status checks every one", () => {
|
|
const exported = require("../../scripts/install-hooks").HOOK_TYPES;
|
|
const plugin = require("../../.claude-plugin/plugin.json");
|
|
assert.deepEqual([...exported].sort(), Object.keys(plugin.hooks).sort());
|
|
assert.deepEqual([...exported].sort(), [...HOOK_TYPES].sort());
|
|
});
|
|
});
|