feat: give every Hermes instance a Home thread for proactive delivery
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]>
This commit is contained in:
1 parent
5bfb90db61
commit
784fcb71e6
73 files changed
+5907
-119
No files matched your search
@@ -23,6 +23,7 @@ export type AgentActivityPhase =
|
||||
| "running"
|
||||
| "waiting_for_approval"
|
||||
| "waiting_for_input"
|
||||
| "notification"
|
||||
| "completed"
|
||||
| "failed"
|
||||
| "stale";
|
||||
@@ -81,6 +82,8 @@ export function AgentActivity(
|
||||
return isLightScheme ? "#d97706" : "#fcd34d"; // amber-600 / amber-300
|
||||
case "waiting_for_input":
|
||||
return isLightScheme ? "#4f46e5" : "#a5b4fc"; // indigo-600 / indigo-300
|
||||
case "notification":
|
||||
return isLightScheme ? "#7c3aed" : "#c4b5fd"; // violet-600 / violet-300
|
||||
case "failed":
|
||||
return isLightScheme ? "#dc2626" : "#fca5a5"; // red-600 / red-300
|
||||
case "completed":
|
||||
@@ -97,7 +100,9 @@ export function AgentActivity(
|
||||
const phasePriority = (phase: AgentActivityPhase): number => {
|
||||
if (phase === "waiting_for_approval" || phase === "waiting_for_input") return 0;
|
||||
if (phase === "failed") return 1;
|
||||
if (phase === "running" || phase === "starting") return 2;
|
||||
// A proactive delivery is unread news, so it outranks finished/stale rows
|
||||
// but never a human who is actually blocked.
|
||||
if (phase === "running" || phase === "starting" || phase === "notification") return 2;
|
||||
return 3;
|
||||
};
|
||||
const ordered = [...props.activities].sort(
|
||||
@@ -163,6 +168,8 @@ export function AgentActivity(
|
||||
return "exclamationmark.circle.fill";
|
||||
case "waiting_for_input":
|
||||
return "questionmark.circle.fill";
|
||||
case "notification":
|
||||
return "bell.badge.fill";
|
||||
case "failed":
|
||||
return "xmark.octagon.fill";
|
||||
case "completed":
|
||||
|
||||
@@ -107,6 +107,7 @@ describe("CheckpointDiffQuery.layer", () => {
|
||||
});
|
||||
}),
|
||||
getThreadShellById: () => Effect.succeed(Option.none()),
|
||||
getThreadArchiveStateById: () => Effect.succeed(Option.none()),
|
||||
getThreadDetailById: () => Effect.succeed(Option.none()),
|
||||
getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
|
||||
}),
|
||||
@@ -201,6 +202,7 @@ describe("CheckpointDiffQuery.layer", () => {
|
||||
getThreadCheckpointContext: () => Effect.succeed(Option.some(threadCheckpointContext)),
|
||||
getFullThreadDiffContext: () => Effect.die("unused"),
|
||||
getThreadShellById: () => Effect.succeed(Option.none()),
|
||||
getThreadArchiveStateById: () => Effect.succeed(Option.none()),
|
||||
getThreadDetailById: () => Effect.succeed(Option.none()),
|
||||
getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
|
||||
}),
|
||||
@@ -285,6 +287,7 @@ describe("CheckpointDiffQuery.layer", () => {
|
||||
getThreadCheckpointContext: () => Effect.succeed(Option.some(threadCheckpointContext)),
|
||||
getFullThreadDiffContext: () => Effect.die("unused"),
|
||||
getThreadShellById: () => Effect.succeed(Option.none()),
|
||||
getThreadArchiveStateById: () => Effect.succeed(Option.none()),
|
||||
getThreadDetailById: () => Effect.succeed(Option.none()),
|
||||
getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
|
||||
}),
|
||||
@@ -354,6 +357,7 @@ describe("CheckpointDiffQuery.layer", () => {
|
||||
getThreadCheckpointContext: () => Effect.succeed(Option.some(threadCheckpointContext)),
|
||||
getFullThreadDiffContext: () => Effect.die("unused"),
|
||||
getThreadShellById: () => Effect.succeed(Option.none()),
|
||||
getThreadArchiveStateById: () => Effect.succeed(Option.none()),
|
||||
getThreadDetailById: () => Effect.succeed(Option.none()),
|
||||
getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
|
||||
}),
|
||||
@@ -408,6 +412,7 @@ describe("CheckpointDiffQuery.layer", () => {
|
||||
getThreadCheckpointContext: () => Effect.succeed(Option.none()),
|
||||
getFullThreadDiffContext: () => Effect.succeed(Option.none()),
|
||||
getThreadShellById: () => Effect.succeed(Option.none()),
|
||||
getThreadArchiveStateById: () => Effect.succeed(Option.none()),
|
||||
getThreadDetailById: () => Effect.succeed(Option.none()),
|
||||
getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
|
||||
}),
|
||||
|
||||
@@ -203,6 +203,7 @@ describe("OrchestrationEngine", () => {
|
||||
getThreadCheckpointContext: () => Effect.succeed(Option.none()),
|
||||
getFullThreadDiffContext: () => Effect.succeed(Option.none()),
|
||||
getThreadShellById: () => Effect.succeed(Option.none()),
|
||||
getThreadArchiveStateById: () => Effect.succeed(Option.none()),
|
||||
getThreadDetailById: () => Effect.succeed(Option.none()),
|
||||
getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
|
||||
}),
|
||||
|
||||
@@ -2368,6 +2368,139 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("projects notification deliveries into the message row and shell summary", () =>
|
||||
Effect.gen(function* () {
|
||||
const projectionPipeline = yield* OrchestrationProjectionPipeline;
|
||||
const eventStore = yield* OrchestrationEventStore;
|
||||
const sql = yield* SqlClient.SqlClient;
|
||||
const appendAndProject = (event: Parameters<typeof eventStore.append>[0]) =>
|
||||
eventStore
|
||||
.append(event)
|
||||
.pipe(Effect.flatMap((savedEvent) => projectionPipeline.projectEvent(savedEvent)));
|
||||
const createdAt = "2026-07-01T09:00:00.000Z";
|
||||
|
||||
yield* appendAndProject({
|
||||
type: "project.created",
|
||||
eventId: EventId.make("evt-notify-project"),
|
||||
aggregateKind: "project",
|
||||
aggregateId: ProjectId.make("project-notify"),
|
||||
occurredAt: createdAt,
|
||||
commandId: CommandId.make("cmd-notify-project"),
|
||||
causationEventId: null,
|
||||
correlationId: CommandId.make("cmd-notify-project"),
|
||||
metadata: {},
|
||||
payload: {
|
||||
projectId: ProjectId.make("project-notify"),
|
||||
title: "Hermes",
|
||||
workspaceRoot: "/tmp/project-notify",
|
||||
defaultModelSelection: null,
|
||||
scripts: [],
|
||||
createdAt,
|
||||
updatedAt: createdAt,
|
||||
},
|
||||
});
|
||||
|
||||
yield* appendAndProject({
|
||||
type: "thread.created",
|
||||
eventId: EventId.make("evt-notify-thread"),
|
||||
aggregateKind: "thread",
|
||||
aggregateId: ThreadId.make("thread-notify"),
|
||||
occurredAt: createdAt,
|
||||
commandId: CommandId.make("cmd-notify-thread"),
|
||||
causationEventId: null,
|
||||
correlationId: CommandId.make("cmd-notify-thread"),
|
||||
metadata: {},
|
||||
payload: {
|
||||
threadId: ThreadId.make("thread-notify"),
|
||||
projectId: ProjectId.make("project-notify"),
|
||||
title: "Home",
|
||||
modelSelection: {
|
||||
instanceId: ProviderInstanceId.make("hermes"),
|
||||
model: "hermes",
|
||||
},
|
||||
runtimeMode: "full-access",
|
||||
branch: null,
|
||||
worktreePath: null,
|
||||
createdAt,
|
||||
updatedAt: createdAt,
|
||||
},
|
||||
});
|
||||
|
||||
const deliveredAt = "2026-07-01T09:05:00.000Z";
|
||||
yield* appendAndProject({
|
||||
type: "thread.notification-delivered",
|
||||
eventId: EventId.make("evt-notify-delivery"),
|
||||
aggregateKind: "thread",
|
||||
aggregateId: ThreadId.make("thread-notify"),
|
||||
occurredAt: deliveredAt,
|
||||
commandId: CommandId.make("cmd-notify-delivery"),
|
||||
causationEventId: null,
|
||||
correlationId: CommandId.make("cmd-notify-delivery"),
|
||||
metadata: {},
|
||||
payload: {
|
||||
threadId: ThreadId.make("thread-notify"),
|
||||
messageId: MessageId.make("message-notify"),
|
||||
text: "Digest ready.",
|
||||
notification: {
|
||||
kind: "cron",
|
||||
label: "Cron: daily-digest",
|
||||
deliveryId: "delivery-1",
|
||||
},
|
||||
createdAt: deliveredAt,
|
||||
updatedAt: deliveredAt,
|
||||
},
|
||||
});
|
||||
|
||||
const messageRows = yield* sql<{
|
||||
readonly messageId: string;
|
||||
readonly text: string;
|
||||
readonly turnId: string | null;
|
||||
readonly role: string;
|
||||
readonly isStreaming: number;
|
||||
readonly notification: string | null;
|
||||
}>`
|
||||
SELECT
|
||||
message_id AS "messageId",
|
||||
text,
|
||||
turn_id AS "turnId",
|
||||
role,
|
||||
is_streaming AS "isStreaming",
|
||||
notification_json AS "notification"
|
||||
FROM projection_thread_messages
|
||||
WHERE thread_id = 'thread-notify'
|
||||
`;
|
||||
assert.equal(messageRows.length, 1);
|
||||
assert.equal(messageRows[0]?.messageId, "message-notify");
|
||||
assert.equal(messageRows[0]?.turnId, null);
|
||||
assert.equal(messageRows[0]?.role, "assistant");
|
||||
assert.equal(messageRows[0]?.isStreaming, 0);
|
||||
assert.deepEqual(
|
||||
// @effect-diagnostics-next-line preferSchemaOverJson:off
|
||||
JSON.parse(messageRows[0]?.notification ?? "null"),
|
||||
{ kind: "cron", label: "Cron: daily-digest", deliveryId: "delivery-1" },
|
||||
);
|
||||
|
||||
// The shell summary is what the sidebar and the push relay read; the
|
||||
// provenance is invisible to them unless it is flattened here.
|
||||
const threadRows = yield* sql<{
|
||||
readonly updatedAt: string;
|
||||
readonly latestNotification: string | null;
|
||||
}>`
|
||||
SELECT
|
||||
updated_at AS "updatedAt",
|
||||
latest_notification_json AS "latestNotification"
|
||||
FROM projection_threads
|
||||
WHERE thread_id = 'thread-notify'
|
||||
`;
|
||||
assert.equal(threadRows[0]?.updatedAt, deliveredAt);
|
||||
assert.deepEqual(
|
||||
// @effect-diagnostics-next-line preferSchemaOverJson:off
|
||||
JSON.parse(threadRows[0]?.latestNotification ?? "null"),
|
||||
{ kind: "cron", label: "Cron: daily-digest", deliveredAt },
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("does not fallback-retain messages whose turnId is removed by revert", () =>
|
||||
Effect.gen(function* () {
|
||||
const projectionPipeline = yield* OrchestrationProjectionPipeline;
|
||||
|
||||
@@ -34,7 +34,10 @@ import {
|
||||
type ProjectionTurn,
|
||||
ProjectionTurnRepository,
|
||||
} from "../../persistence/Services/ProjectionTurns.ts";
|
||||
import { ProjectionThreadRepository } from "../../persistence/Services/ProjectionThreads.ts";
|
||||
import {
|
||||
type ProjectionThread,
|
||||
ProjectionThreadRepository,
|
||||
} from "../../persistence/Services/ProjectionThreads.ts";
|
||||
import { ProjectionPendingApprovalRepositoryLive } from "../../persistence/Layers/ProjectionPendingApprovals.ts";
|
||||
import { ProjectionProjectRepositoryLive } from "../../persistence/Layers/ProjectionProjects.ts";
|
||||
import { ProjectionStateRepositoryLive } from "../../persistence/Layers/ProjectionState.ts";
|
||||
@@ -564,6 +567,11 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
|
||||
]);
|
||||
|
||||
let latestUserMessageAt: string | null = null;
|
||||
// Newest proactive delivery, flattened for shell readers that never load
|
||||
// messages (sidebar rows, agent awareness). Ties break on message id so
|
||||
// two deliveries stamped in the same millisecond resolve deterministically.
|
||||
let latestNotification: ProjectionThread["latestNotification"] = null;
|
||||
let latestNotificationKey: string | null = null;
|
||||
for (const message of messages) {
|
||||
if (
|
||||
message.role === "user" &&
|
||||
@@ -571,6 +579,17 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
|
||||
) {
|
||||
latestUserMessageAt = message.createdAt;
|
||||
}
|
||||
if (message.notification !== undefined) {
|
||||
const key = `${message.createdAt} | ||||