#!/usr/bin/env node /** * SessionStart hook: make sure the glance daemon is running, then record the event. * * Note that as of Grok Build's current hook wiring this never actually runs: a plugin's * `SessionStart` entry is registered after `SessionStart` has already been dispatched, so the * event finds no plugin hooks to call. `ensureDaemon` in glance-lib.mjs has the details, and * bin/glance-record.mjs is what really boots the daemon. * * It stays wired anyway. It costs nothing when it does not fire, it is the correct hook for the * job on the day that ordering is fixed, and it keeps the answer to "what starts this thing" * in the obvious place. * * It always exits 0 — a monitoring dashboard must never be the reason a session fails to start. */ import { baseUrl, ensureDaemon, envEnvelope, hookHeaders, postJson, readConfig, readStdinJson, } from "./glance-lib.mjs"; const payload = envEnvelope(await readStdinJson()); const cfg = readConfig(); try { // hooks/hooks.json allows this hook 20s; leave most of it as headroom. const up = await ensureDaemon(cfg, { waitMs: 8000, startedBy: "session-start-hook", onMissingBuild: (entry) => { // dist/ ships with the plugin, so this means an incomplete checkout. Say so once, on // stderr, where it is recorded but harmless — a hook must never fail a session. process.stderr.write( `[grok-glance] ${entry} is missing - run \`npm install && npm run build\` in the plugin root\n`, ); }, }); if (up) { // hookHeaders() is read here, not at import time: on a first-ever run the daemon we just // spawned is what created hook.secret. await postJson(`${baseUrl(cfg)}/hook/record`, payload, 2500, hookHeaders()); } } catch { // Fail open, always. } process.exit(0);