diff --git a/scripts/deploy-hetzner.sh b/scripts/deploy-hetzner.sh index 087c3ab94..ef43e1526 100644 --- a/scripts/deploy-hetzner.sh +++ b/scripts/deploy-hetzner.sh @@ -313,6 +313,7 @@ sync_runner "$PROJECT_DIR/home/" "$HOST:$RUNNER_DIR/home/" sync_runner "$PROJECT_DIR/scripts/box-runner.ts" \ "$PROJECT_DIR/scripts/mint-box-runner-token.ts" \ "$PROJECT_DIR/scripts/reindex-knowledge.ts" \ + "$PROJECT_DIR/scripts/hosted-runner.ts" \ "$HOST:$RUNNER_DIR/scripts/" sync_runner "$PROJECT_DIR/tsconfig.json" "$HOST:$RUNNER_DIR/tsconfig.json" ssh "$HOST" "chown -R $RUNNER_OWNER:$RUNNER_OWNER $RUNNER_DIR/src $RUNNER_DIR/desktop $RUNNER_DIR/home $RUNNER_DIR/scripts $RUNNER_DIR/tsconfig.json" diff --git a/scripts/hosted-runner.ts b/scripts/hosted-runner.ts index 00cb846af..68825cdc0 100644 --- a/scripts/hosted-runner.ts +++ b/scripts/hosted-runner.ts @@ -19,9 +19,17 @@ import { getProjectContext } from "@/db/queries/project-context"; import { getSelfImprovementTarget } from "@/db/queries/frontier"; import { getGithubToken } from "@/lib/github-token"; import { appendProjectDevLog } from "@/db/queries/user-projects"; +import { createOrchestrationEvent } from "@/db/queries/orchestration-events"; +import type { AdapterId, OrchestrationEventType } from "@/lib/orchestration"; import { analyzeRepo } from "@/lib/hosted-runner/analyze"; import { runHermesTask } from "@/lib/hosted-runner/run-hermes"; +// The real executor id for a hosted coding dispatch. Hermes is a legitimate +// agent id (ALL_ADAPTERS) but intentionally NOT in the dispatchable +// ORCHESTRATION_ADAPTER_IDS, so record it with a localized cast — the same +// pattern close-sweep already uses for adapter strings outside that set. +const HERMES_ADAPTER = "hermes" as AdapterId; + const POLL_MS = 5_000; // Both hosted classes: read-only analysis (Groq, Phase 0) + write-class dispatch // to a sandboxed coding agent (Hermes, Phase 1). Local-runner dispatch/inject @@ -37,6 +45,29 @@ async function logResult(userId: string, projectKey: string, label: string, text }).catch((e) => console.error("[hosted-runner] devlog append failed:", e)); } +/** Emit a lifecycle event into orchestration_events — the SAME stream inject-core + * writes and Activity reads — so hosted (Hermes/analysis) runs are observable in + * the product instead of only in console logs. Fire-and-forget: telemetry must + * never fail the run. `adapter` records the true executor ("hermes" for a coding + * dispatch; null for read-only analysis). */ +async function emitHostedEvent( + userId: string, + projectKey: string, + eventType: OrchestrationEventType, + detail: string, + adapter?: AdapterId, +) { + await createOrchestrationEvent({ + userId, + projectKey, + eventType, + source: "hosted-runner", + adapter: adapter ?? null, + detail: detail.slice(0, 500), + happenedAt: new Date(), + }).catch((e) => console.error("[hosted-runner] event emit failed:", e)); +} + /** Claim + execute one hosted command (analyze or dispatch). False when queue empty. */ async function tick(userId: string): Promise { const cmd = await claimNextPendingCommand([userId], HOSTED_TYPES); @@ -55,6 +86,7 @@ async function tick(userId: string): Promise { if (cmd.type === "hosted_dispatch") { // Phase 1: write-class task → Hermes in its own sandbox (orchestrate, not out-build). console.log(`[hosted-runner] dispatch→hermes ${p.projectKey}: ${p.task.slice(0, 60)}`); + void emitHostedEvent(userId, p.projectKey, "task_started", `Hermes dispatch — ${p.task.slice(0, 120)}`, HERMES_ADAPTER); const model = (cmd.payload as HostedDispatchPayload).model; const res = await runHermesTask({ gitUrl: p.gitUrl, task: p.task, projectContext: ctx, token, model }); if (res.ok) { @@ -63,26 +95,40 @@ async function tick(userId: string): Promise { : `${res.output}\n\n— changed:\n${res.diff || "(no diff)"}${res.prUrl ? `\n\nPR: ${res.prUrl}` : res.branch ? `\n\nPushed branch: ${res.branch}` : ""}`; await markCommandExecuted(cmd.id, userId, { ok: true, text: summary }); await logResult(userId, p.projectKey, `Hosted dispatch (Hermes) — ${p.task.slice(0, 70)}`, summary); + void emitHostedEvent(userId, p.projectKey, "task_completed", + res.prUrl ? `Hermes → PR ${res.prUrl}` : res.noChanges ? "Hermes: no file changes" : `Hermes pushed ${res.branch ?? "a branch"}`, + HERMES_ADAPTER); console.log(`[hosted-runner] ✓ ${p.projectKey} (hermes/${res.model})${res.prUrl ? ` → ${res.prUrl}` : ""}`); } else { await markCommandExecuted(cmd.id, userId, { ok: false, error: res.error }); + // Failures used to vanish into console only — log + emit so a broken hosted + // path is visible in the project dev log and Activity, not archaeology. + await logResult(userId, p.projectKey, `Hosted dispatch (Hermes) FAILED — ${p.task.slice(0, 60)}`, res.error); + void emitHostedEvent(userId, p.projectKey, "task_failed", `Hermes failed — ${res.error}`, HERMES_ADAPTER); console.log(`[hosted-runner] ✗ ${p.projectKey}: ${res.error}`); } } else { // Phase 0: read-only analysis via Groq. console.log(`[hosted-runner] analyze ${p.projectKey}: ${p.task.slice(0, 60)}`); + void emitHostedEvent(userId, p.projectKey, "task_started", `Hosted analysis — ${p.task.slice(0, 120)}`); const res = await analyzeRepo({ gitUrl: p.gitUrl, task: p.task, projectContext: ctx, token }); if (res.ok) { await markCommandExecuted(cmd.id, userId, { ok: true, text: res.report }); await logResult(userId, p.projectKey, `Hosted analysis — ${p.task.slice(0, 80)}`, res.report); + void emitHostedEvent(userId, p.projectKey, "task_completed", `Hosted analysis complete (${res.model})`); console.log(`[hosted-runner] ✓ ${p.projectKey} (${res.model})`); } else { await markCommandExecuted(cmd.id, userId, { ok: false, error: res.error }); + await logResult(userId, p.projectKey, `Hosted analysis FAILED — ${p.task.slice(0, 60)}`, res.error); + void emitHostedEvent(userId, p.projectKey, "task_failed", `Hosted analysis failed — ${res.error}`); console.log(`[hosted-runner] ✗ ${p.projectKey}: ${res.error}`); } } } catch (e) { - await markCommandExecuted(cmd.id, userId, { ok: false, error: e instanceof Error ? e.message : "hosted run failed" }); + const msg = e instanceof Error ? e.message : "hosted run failed"; + await markCommandExecuted(cmd.id, userId, { ok: false, error: msg }); + void emitHostedEvent(userId, p.projectKey, "task_failed", `Hosted run threw — ${msg}`, + cmd.type === "hosted_dispatch" ? HERMES_ADAPTER : undefined); } return true; } diff --git a/src/lib/agents/index.ts b/src/lib/agents/index.ts index 06364548d..5405634cc 100644 --- a/src/lib/agents/index.ts +++ b/src/lib/agents/index.ts @@ -21,13 +21,23 @@ import { cursorAdapter } from "./cursor"; import { codexAdapter } from "./codex"; import { openclawAdapter } from "./openclaw"; import { geminiAdapter } from "./gemini"; -// Hermes adapter PARKED (2026-06-25): dormant spike, not pulling weight — -// `runHermesTask` is unwired (zero call sites), Hermes isn't in -// ORCHESTRATION_ADAPTER_IDS, and the `hermes` CLI isn't installed anywhere, so -// it only surfaced as a broken/unavailable runtime. The strategic bet (adopt -// Hermes's sandboxed hosted execution) is intact — re-add `import { hermesAdapter } -// from "./hermes"` + the ALL_ADAPTERS entry when the hosted runner (Phase 1) -// actually ships. Files preserved: ./hermes.ts + ./hosted-runner/run-hermes.ts. +// Hermes is deliberately NOT an interactive adapter, but it IS a wired hosted +// executor — two different things, so keep the distinction straight: +// • The interactive adapter (`hermesAdapter`, ./hermes.ts) is intentionally +// kept OUT of ALL_ADAPTERS and ORCHESTRATION_ADAPTER_IDS. FleetCrown does +// not offer "drive Hermes in a terminal tab" the way it offers claude/codex/ +// grok. That adapter file is preserved for a future interactive path; it is +// not selectable today. +// • The HOSTED path IS live and wired: `runHermesTask` (./hosted-runner/ +// run-hermes.ts) has a real call site in scripts/hosted-runner.ts, which the +// `fleetcrown-hosted-runner.timer` drains (`hosted_dispatch` command type → +// clone → Hermes in its sandbox → PR). The `hermes` CLI IS installed on the +// box (/usr/local/bin/hermes, v0.17.0). inject-core auto-routes an offline +// coding dispatch to it (hostedRunner:"hermes"). +// Status (2026-07-22): hosted path plumbed + polling, but lightly exercised — +// one real Hermes PR to date (spike, 2026-06-25). It is observable now: hosted +// runs emit orchestration_events (source="hosted-runner", adapter="hermes"), +// visible in Activity. Not yet proven under volume. export type { AgentAdapter, AgentCapabilities, AgentAvailability, AgentRuntimeConfig } from "./types"; diff --git a/src/lib/inject-core.ts b/src/lib/inject-core.ts index 592fd9fcd..2202b9622 100644 --- a/src/lib/inject-core.ts +++ b/src/lib/inject-core.ts @@ -10,14 +10,14 @@ * This path is autopilot-critical — keep it behavior-preserving. */ import { ensureUserProjectEntityLinks, getOrgProjects } from "@/db/queries/user-projects"; -import { ORCHESTRATION_ADAPTER_IDS, ORCHESTRATION_TASK_INTENT_IDS, DEFAULT_ADAPTER_ID, renderProjectContextBlock, type OrchestrationTaskIntentId } from "@/lib/orchestration"; +import { ORCHESTRATION_ADAPTER_IDS, ORCHESTRATION_TASK_INTENT_IDS, DEFAULT_ADAPTER_ID, renderProjectContextBlock, type OrchestrationTaskIntentId, type AdapterId } from "@/lib/orchestration"; import { getProjectContext } from "@/db/queries/project-context"; import { isRuntimeAvailable } from "@/lib/runtime"; import { ORCH_STATE } from "@/lib/orchestration/contract"; import { workspaceIdFor } from "@/lib/agent-execution/ownership"; import { executeInject } from "@/lib/executor"; import { projectPreferredChannel } from "@/lib/execution-access"; -import { createOrchestrationEvent } from "@/db/queries/orchestration-events"; +import { createOrchestrationEvent, createOrchestrationEventOnce } from "@/db/queries/orchestration-events"; import { createOrchestrationRun, isProjectBusy } from "@/db/queries/orchestration-runs"; import { emitRunEvent } from "@/db/queries/run-events"; import { insertPromptHistory } from "@/db/queries/prompt-history"; @@ -547,6 +547,22 @@ export async function injectPrompt(params: InjectParams, userId: string): Promis console.error("[inject] hosted-dispatch enqueue failed:", err); return undefined; }); + // Make the offline→hosted routing decision visible in Activity, not just a + // response flag — same orchestration_events stream, attributed to the real + // executor (Hermes). Deduped by command id so a retry can't double-count. + if (hostedDispatchId) { + void createOrchestrationEventOnce({ + userId, + projectId, + projectKey: canonical, + eventType: "continue_requested", + source: "hosted-runner", + adapter: "hermes" as AdapterId, + intent: eventIntent, + detail: "Auto-routed to hosted runner (Hermes) — local runner offline", + happenedAt: new Date(), + }, `hosted-dispatch:${hostedDispatchId}`).catch((err) => console.error("[inject] hosted event emit failed:", err)); + } } return {