feat: per-tool settings, persistent settings file, Codex and Antigravity sessions, tokens and hooks (#40 B, C1, C2, D)

Part B: Model Pricing, Hook Configuration and CLAUDE_HOME move from
Settings → Dashboard to the Claude Code tab's AgentHub sub-tab
(ClaudeAgentHubSettings.tsx); CODEX_HOME is editable in Codex → AgentHub.
A home that is or contains the OS home is refused (EBADHOME).

Settings persistence: dashboard-saved settings go to
~/.config/agenthub/settings.env (AGENTHUB_SETTINGS_FILE), outside the plugin
cache an update wipes. Load order: real env > the checkout's .env > that file.
The first CLAUDE_HOME change pins DASHBOARD_DATA_DIR so the DB stays put.

C1: codex-usage.js reads Codex rollout journals into sessions (new column
tool) and token_usage, tracked per file in codex_rollouts; forks deduped by
total vector, sub-agent threads folded into the root; pricing-sync also
prices OpenAI models; PricingSettings with a vendor filter.

C2: codex-hooks.js installs AgentHub's hooks into $CODEX_HOME/hooks.json on
a click, through a stable forwarder; Codex trust is never written. The hook
route creates tool='codex' sessions and folds sub-agent hooks into the root.

D: antigravity-usage.js reads agy's per-conversation SQLite files (tokens
from ModelUsageStats) into tool='antigravity' sessions; antigravity-hooks.js
installs the named hook `agenthub` (PreInvocation, PostToolUse, Stop — never
PreToolUse) into ~/.gemini/config/hooks.json; Settings → Antigravity tab;
Gemini pricing.

Whole-change review fixes: sameOriginGuard on both home PUTs, routers.missing
on a CLAUDE_HOME change, reset-pricing scope, GET /api/sessions?tool=,
clear-data covering the new tables, Codex child-thread events moving no
status, hooks owning status only while current, EFOREIGN for a forwarder
that is not ours, agy quiet window and turn rules, AGENTHUB_SETTINGS_FILE
honoured from .env.

Docs, OpenAPI and the CLAUDE.md marker updated. Server 2141, client 779.
This commit is contained in:
2026-09-29 16:09:19 +07:00
parent 7ec5a7cd85
commit 969b06253c
95 changed files with 9278 additions and 1696 deletions
+46
View File
@@ -1081,6 +1081,46 @@ try {
}
db.exec(`CREATE INDEX IF NOT EXISTS idx_sessions_source ON sessions(source)`);
// Migrate: add `tool` to sessions — the AI coding CLI that produced it. Every
// hook-created and imported row is Claude Code's ('claude', the default); a
// Codex session is ingested from its rollout journals (server/lib/codex-usage.js)
// with 'codex'. Surfaces that act on a session through the `claude` CLI
// (attach, resume) list only 'claude' rows.
try {
db.prepare("SELECT tool FROM sessions LIMIT 1").get();
} catch {
db.prepare("ALTER TABLE sessions ADD COLUMN tool TEXT NOT NULL DEFAULT 'claude'").run();
}
// How far each Codex rollout journal has been read (server/lib/codex-usage.js).
// In the DB, not a side file, so a session the user deleted stays deleted
// until Codex writes to its journal again, while a wiped DB re-imports all.
db.exec(`
CREATE TABLE IF NOT EXISTS codex_rollouts (
path TEXT PRIMARY KEY,
session_id TEXT NOT NULL,
thread_id TEXT NOT NULL,
size INTEGER NOT NULL DEFAULT 0,
mtime_ms INTEGER NOT NULL DEFAULT 0,
offset INTEGER NOT NULL DEFAULT 0,
state TEXT NOT NULL DEFAULT '{}',
updated_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))
);
CREATE INDEX IF NOT EXISTS idx_codex_rollouts_session ON codex_rollouts(session_id);
CREATE INDEX IF NOT EXISTS idx_codex_rollouts_thread ON codex_rollouts(thread_id);
`);
// Which Antigravity CLI conversation databases have been read, at what mtime
// (server/lib/antigravity-usage.js) — the same "deleted stays deleted" rule.
db.exec(`
CREATE TABLE IF NOT EXISTS antigravity_conversations (
path TEXT PRIMARY KEY,
session_id TEXT NOT NULL,
mtime_ms INTEGER NOT NULL DEFAULT 0,
updated_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))
);
`);
// Remote data sources: other machines whose Claude Code history this dashboard
// pulls in over SSH. Config only — NO secrets are stored here: authentication
// always defers to the host's own SSH stack (~/.ssh/config, ssh-agent, keys,
@@ -1286,12 +1326,14 @@ try {
// Remote-source sessions (source != 'local') are exempt: their "last event" is bounded by
// the rsync cadence, not the remote CLI's actual activity, so a busy remote session could be
// wrongly completed here. server/lib/remote-sync.js owns their status via mirror reconciliation.
// Codex sessions are exempt too: server/lib/codex-usage.js derives theirs from the journal.
db.prepare(
`
UPDATE sessions SET
status = 'completed',
ended_at = COALESCE(ended_at, strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))
WHERE status = 'active'
AND tool = 'claude'
AND (source = 'local' OR source IS NULL)
AND started_at < strftime('%Y-%m-%dT%H:%M:%fZ', 'now', '-1 hour')
AND NOT EXISTS (
@@ -1493,6 +1535,10 @@ const stmts = {
AND updated_at < strftime('%Y-%m-%dT%H:%M:%fZ', 'now', '-' || ? || ' minutes')`
),
// A session's newest event (Antigravity: a turn after a Stop, a quiet session).
lastEvent: db.prepare(
"SELECT event_type, created_at FROM events WHERE session_id = ? ORDER BY id DESC LIMIT 1"
),
// A second delivery of one hook: the session's LATEST event, a few seconds
// old, with the same type and a byte-identical payload (the duplicate guard
// in routes/hooks.js). Latest only, never "any recent": two turns can end in