Hermes has a first-class "home channel": the default destination for output nobody asked for at a specific address — a cron result, an agent-initiated `send_message`, a gateway lifecycle notice, a `/handoff`. The T3 plugin opted out, suppressing Hermes' "Type /sethome" nudge without designating anything. A cron job with `deliver=t3` went nowhere, `send_message` to a bare `t3` target errored, and `/handoff t3` hard-failed. Each enrolled instance now gets one Home thread, pinned first in its agent project, fully conversational, undeletable, created automatically. Protocol v3. `connection.hello` gains `role: "gateway" | "delivery"`; `connection.accepted` carries `homeThreadId`. Two new frames: `home.deliver` and `home.deliver.ack`. Designation converges on read, like agent projects — a precondition every handshake runs rather than an event one caller owns, so an instance enrolled before this shipped, or one whose thread creation failed once, heals on the next connect. It lives in the instance's settings blob beside `connectorUrl` and `revoked`; the plugin's `T3_HOME_CHANNEL` is a cache of it, reconciled every handshake. The existence check deliberately ignores archive state. Every other thread read filters `archived_at IS NULL`, which is right for "what should the user see" and wrong for "does this row exist" — an archived Home read as deleted and the self-healing path replaced it, stranding the history in a thread the user had merely parked. A delivery to an archived Home now un-archives it, the same way incoming activity wakes a settled or snoozed thread. Deliveries bypass the turn machinery entirely. One may arrive while the home thread has a live user turn, so it appends as a turnless assistant message carrying `notification` provenance rather than joining the turn — attributing it would fold it away behind that turn's "Worked for …". Everything except a lifecycle notice un-settles the thread and pushes; lifecycle notices land quietly. Idempotency is load-bearing, because the plugin's durable queue retries anything it has not seen acked. T3 dedupes on `deliveryId` against `projection_thread_messages` before dispatching — the decider's own scan sees the in-memory read model, which is built with `messages: []`, so it cannot catch a replay after a restart. The ack is sent only after the write lands. An out-of-process cron run dials in with `role: "delivery"`: authenticated identically, but never registered as the instance's primary connection, so it cannot displace a live gateway socket or answer its pending requests. Plugin side, the proactive branch in `send()` gates on provenance rather than turn absence. The naive rule — "no active turn, so deliver" — would let a notify-stamped cron delivery complete a user's live turn with cron output as its answer. A bare session-key mismatch is not enough either, since `HERMES_SESSION_KEY` resolves empty whenever the context did not propagate; that would tear a genuine reply out of its turn. Bypassing a live turn requires positively identified cron/lifecycle/handoff provenance. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
255 lines
7.2 KiB
TypeScript
255 lines
7.2 KiB
TypeScript
import { describe, expect, it } from "@effect/vitest";
|
|
|
|
import type {
|
|
EnvironmentId,
|
|
OrchestrationProjectShell,
|
|
OrchestrationThreadShell,
|
|
ThreadId,
|
|
TurnId,
|
|
} from "@t3tools/contracts";
|
|
import { ProviderInstanceId } from "@t3tools/contracts";
|
|
|
|
import { projectThreadAwareness } from "./agentAwareness.ts";
|
|
|
|
const NOW = "2026-05-22T12:00:00.000Z";
|
|
|
|
const project = {
|
|
title: "t3code",
|
|
} satisfies Pick<OrchestrationProjectShell, "title">;
|
|
|
|
function thread(
|
|
overrides: Partial<OrchestrationThreadShell> = {},
|
|
): Pick<
|
|
OrchestrationThreadShell,
|
|
| "id"
|
|
| "title"
|
|
| "modelSelection"
|
|
| "session"
|
|
| "latestTurn"
|
|
| "updatedAt"
|
|
| "hasPendingApprovals"
|
|
| "hasPendingUserInput"
|
|
| "latestNotification"
|
|
| "latestUserMessageAt"
|
|
> {
|
|
return {
|
|
id: "thread-1" as ThreadId,
|
|
title: "Fix failing CI",
|
|
modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
|
|
session: null,
|
|
latestTurn: null,
|
|
updatedAt: NOW,
|
|
hasPendingApprovals: false,
|
|
hasPendingUserInput: false,
|
|
latestNotification: null,
|
|
latestUserMessageAt: null,
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
describe("projectThreadAwareness", () => {
|
|
it("returns null for idle threads without an active awareness state", () => {
|
|
expect(
|
|
projectThreadAwareness({
|
|
environmentId: "env-1" as EnvironmentId,
|
|
project,
|
|
thread: thread(),
|
|
}),
|
|
).toBeNull();
|
|
});
|
|
|
|
it("prioritizes approval requests over running state", () => {
|
|
const state = projectThreadAwareness({
|
|
environmentId: "env-1" as EnvironmentId,
|
|
project,
|
|
thread: thread({
|
|
hasPendingApprovals: true,
|
|
session: {
|
|
threadId: "thread-1" as ThreadId,
|
|
status: "running",
|
|
providerName: "Codex",
|
|
runtimeMode: "full-access",
|
|
activeTurnId: "turn-1" as TurnId,
|
|
lastError: null,
|
|
updatedAt: NOW,
|
|
},
|
|
}),
|
|
});
|
|
|
|
expect(state?.phase).toBe("waiting_for_approval");
|
|
expect(state?.headline).toBe("Approval needed");
|
|
});
|
|
|
|
it("projects running provider sessions", () => {
|
|
const state = projectThreadAwareness({
|
|
environmentId: "env-1" as EnvironmentId,
|
|
project,
|
|
thread: thread({
|
|
session: {
|
|
threadId: "thread-1" as ThreadId,
|
|
status: "running",
|
|
providerName: "Codex",
|
|
runtimeMode: "full-access",
|
|
activeTurnId: "turn-1" as TurnId,
|
|
lastError: null,
|
|
updatedAt: NOW,
|
|
},
|
|
}),
|
|
});
|
|
|
|
expect(state).toMatchObject({
|
|
phase: "running",
|
|
headline: "Agent is working",
|
|
detail: "Codex is active.",
|
|
modelTitle: "gpt-5.4",
|
|
deepLink: "/threads/env-1/thread-1",
|
|
});
|
|
});
|
|
|
|
it("projects a fresh proactive delivery with the delivery's own label as the headline", () => {
|
|
const state = projectThreadAwareness({
|
|
environmentId: "env-1" as EnvironmentId,
|
|
project,
|
|
thread: thread({
|
|
latestNotification: {
|
|
kind: "cron",
|
|
label: "Cron: daily-digest",
|
|
deliveredAt: NOW,
|
|
},
|
|
}),
|
|
});
|
|
|
|
expect(state?.phase).toBe("notification");
|
|
// A generic headline would strip the only information the delivery carries.
|
|
expect(state?.headline).toBe("Cron: daily-digest");
|
|
});
|
|
|
|
it("stops reporting a delivery once later thread activity supersedes it", () => {
|
|
const delivered = "2026-05-22T11:00:00.000Z";
|
|
const later = "2026-05-22T11:30:00.000Z";
|
|
const notification = {
|
|
kind: "message" as const,
|
|
label: "Hermes",
|
|
deliveredAt: delivered,
|
|
};
|
|
|
|
// A user reply after the delivery: the ordinary ladder takes over again.
|
|
expect(
|
|
projectThreadAwareness({
|
|
environmentId: "env-1" as EnvironmentId,
|
|
project,
|
|
thread: thread({ latestNotification: notification, latestUserMessageAt: later }),
|
|
}),
|
|
).toBeNull();
|
|
|
|
// A turn that started after it likewise wins — otherwise "notification"
|
|
// would pin forever, since nothing ever marks it read.
|
|
expect(
|
|
projectThreadAwareness({
|
|
environmentId: "env-1" as EnvironmentId,
|
|
project,
|
|
thread: thread({
|
|
latestNotification: notification,
|
|
latestTurn: {
|
|
turnId: "turn-1" as TurnId,
|
|
state: "running",
|
|
requestedAt: later,
|
|
startedAt: later,
|
|
completedAt: null,
|
|
assistantMessageId: null,
|
|
},
|
|
}),
|
|
})?.phase,
|
|
).toBe("running");
|
|
});
|
|
|
|
it("keeps blocked-on-you phases ahead of a delivery", () => {
|
|
const state = projectThreadAwareness({
|
|
environmentId: "env-1" as EnvironmentId,
|
|
project,
|
|
thread: thread({
|
|
hasPendingApprovals: true,
|
|
latestNotification: { kind: "cron", label: "Cron: nightly", deliveredAt: NOW },
|
|
}),
|
|
});
|
|
|
|
expect(state?.phase).toBe("waiting_for_approval");
|
|
});
|
|
|
|
it("projects completed turns as completed even when teardown settled them as interrupted", () => {
|
|
const finishedTurn = {
|
|
turnId: "turn-1" as TurnId,
|
|
state: "interrupted" as const,
|
|
requestedAt: NOW,
|
|
startedAt: NOW,
|
|
completedAt: NOW,
|
|
assistantMessageId: null,
|
|
};
|
|
const state = projectThreadAwareness({
|
|
environmentId: "env-1" as EnvironmentId,
|
|
project,
|
|
thread: thread({ latestTurn: finishedTurn }),
|
|
});
|
|
|
|
// Session teardown settles still-running turns by session status, and
|
|
// that write can race turn.completed; the completion timestamp is the
|
|
// durable signal. Without this the thread resolves to null persistently
|
|
// and gets tombstoned off the lock-screen card instead of showing Done.
|
|
expect(state?.phase).toBe("completed");
|
|
|
|
const trulyInterrupted = projectThreadAwareness({
|
|
environmentId: "env-1" as EnvironmentId,
|
|
project,
|
|
thread: thread({ latestTurn: { ...finishedTurn, completedAt: null } }),
|
|
});
|
|
expect(trulyInterrupted).toBeNull();
|
|
});
|
|
|
|
it("projects ready sessions with no materialized turn as completed", () => {
|
|
// Quick threads without code changes never get a checkpoint, so the SQL
|
|
// shell has no latestTurn row and latest_turn_id is cleared when the
|
|
// session settles; the ready session is the only completion signal left.
|
|
const state = projectThreadAwareness({
|
|
environmentId: "env-1" as EnvironmentId,
|
|
project,
|
|
thread: thread({
|
|
session: {
|
|
threadId: "thread-1" as ThreadId,
|
|
status: "ready",
|
|
providerName: "Codex",
|
|
runtimeMode: "full-access",
|
|
activeTurnId: null,
|
|
lastError: null,
|
|
updatedAt: NOW,
|
|
},
|
|
}),
|
|
});
|
|
|
|
expect(state?.phase).toBe("completed");
|
|
});
|
|
|
|
it("projects failures with the session error detail", () => {
|
|
const state = projectThreadAwareness({
|
|
environmentId: "env-1" as EnvironmentId,
|
|
project,
|
|
thread: thread({
|
|
session: {
|
|
threadId: "thread-1" as ThreadId,
|
|
status: "error",
|
|
providerName: "Codex",
|
|
runtimeMode: "full-access",
|
|
activeTurnId: null,
|
|
lastError: "Provider process exited.",
|
|
updatedAt: NOW,
|
|
},
|
|
}),
|
|
});
|
|
|
|
expect(state).toMatchObject({
|
|
phase: "failed",
|
|
headline: "Agent failed",
|
|
detail: "Provider process exited.",
|
|
});
|
|
});
|
|
});
|