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]>
189 lines
6.0 KiB
TypeScript
189 lines
6.0 KiB
TypeScript
import { MessageId, ThreadId } from "@t3tools/contracts";
|
|
import { assert, it } from "@effect/vitest";
|
|
import * as Effect from "effect/Effect";
|
|
import * as Layer from "effect/Layer";
|
|
|
|
import { ProjectionThreadMessageRepository } from "../Services/ProjectionThreadMessages.ts";
|
|
import { ProjectionThreadMessageRepositoryLive } from "./ProjectionThreadMessages.ts";
|
|
import { SqlitePersistenceMemory } from "./Sqlite.ts";
|
|
|
|
const layer = it.layer(
|
|
ProjectionThreadMessageRepositoryLive.pipe(Layer.provideMerge(SqlitePersistenceMemory)),
|
|
);
|
|
|
|
layer("ProjectionThreadMessageRepository", (it) => {
|
|
it.effect("preserves existing attachments when upsert omits attachments", () =>
|
|
Effect.gen(function* () {
|
|
const repository = yield* ProjectionThreadMessageRepository;
|
|
const threadId = ThreadId.make("thread-preserve-attachments");
|
|
const messageId = MessageId.make("message-preserve-attachments");
|
|
const createdAt = "2026-02-28T19:00:00.000Z";
|
|
const updatedAt = "2026-02-28T19:00:01.000Z";
|
|
const persistedAttachments = [
|
|
{
|
|
type: "image" as const,
|
|
id: "thread-preserve-attachments-att-1",
|
|
name: "example.png",
|
|
mimeType: "image/png",
|
|
sizeBytes: 5,
|
|
},
|
|
];
|
|
|
|
yield* repository.upsert({
|
|
messageId,
|
|
threadId,
|
|
turnId: null,
|
|
role: "user",
|
|
text: "initial",
|
|
attachments: persistedAttachments,
|
|
isStreaming: false,
|
|
createdAt,
|
|
updatedAt,
|
|
});
|
|
|
|
yield* repository.upsert({
|
|
messageId,
|
|
threadId,
|
|
turnId: null,
|
|
role: "user",
|
|
text: "updated",
|
|
isStreaming: false,
|
|
createdAt,
|
|
updatedAt: "2026-02-28T19:00:02.000Z",
|
|
});
|
|
|
|
const rows = yield* repository.listByThreadId({ threadId });
|
|
assert.equal(rows.length, 1);
|
|
assert.equal(rows[0]?.text, "updated");
|
|
assert.deepEqual(rows[0]?.attachments, persistedAttachments);
|
|
|
|
const rowById = yield* repository.getByMessageId({ messageId });
|
|
assert.equal(rowById._tag, "Some");
|
|
if (rowById._tag === "Some") {
|
|
assert.equal(rowById.value.text, "updated");
|
|
assert.deepEqual(rowById.value.attachments, persistedAttachments);
|
|
}
|
|
}),
|
|
);
|
|
|
|
it.effect("round-trips notification provenance and preserves it across later upserts", () =>
|
|
Effect.gen(function* () {
|
|
const repository = yield* ProjectionThreadMessageRepository;
|
|
const threadId = ThreadId.make("thread-notification");
|
|
const messageId = MessageId.make("message-notification");
|
|
const createdAt = "2026-07-01T09:00:00.000Z";
|
|
const notification = {
|
|
kind: "cron" as const,
|
|
label: "Cron: daily-digest",
|
|
deliveryId: "delivery-1",
|
|
};
|
|
|
|
yield* repository.upsert({
|
|
messageId,
|
|
threadId,
|
|
turnId: null,
|
|
role: "assistant",
|
|
text: "Digest ready.",
|
|
notification,
|
|
isStreaming: false,
|
|
createdAt,
|
|
updatedAt: createdAt,
|
|
});
|
|
|
|
const rows = yield* repository.listByThreadId({ threadId });
|
|
assert.equal(rows.length, 1);
|
|
assert.deepEqual(rows[0]?.notification, notification);
|
|
|
|
// Same COALESCE rule as attachments: an unrelated update that omits the
|
|
// field must not erase a delivery's provenance.
|
|
yield* repository.upsert({
|
|
messageId,
|
|
threadId,
|
|
turnId: null,
|
|
role: "assistant",
|
|
text: "Digest ready (edited).",
|
|
isStreaming: false,
|
|
createdAt,
|
|
updatedAt: "2026-07-01T09:00:02.000Z",
|
|
});
|
|
|
|
const rowById = yield* repository.getByMessageId({ messageId });
|
|
assert.equal(rowById._tag, "Some");
|
|
if (rowById._tag === "Some") {
|
|
assert.equal(rowById.value.text, "Digest ready (edited).");
|
|
assert.deepEqual(rowById.value.notification, notification);
|
|
}
|
|
}),
|
|
);
|
|
|
|
it.effect("leaves notification absent on ordinary messages", () =>
|
|
Effect.gen(function* () {
|
|
const repository = yield* ProjectionThreadMessageRepository;
|
|
const threadId = ThreadId.make("thread-ordinary-message");
|
|
const messageId = MessageId.make("message-ordinary");
|
|
|
|
yield* repository.upsert({
|
|
messageId,
|
|
threadId,
|
|
turnId: null,
|
|
role: "user",
|
|
text: "hello",
|
|
isStreaming: false,
|
|
createdAt: "2026-07-01T09:10:00.000Z",
|
|
updatedAt: "2026-07-01T09:10:00.000Z",
|
|
});
|
|
|
|
const rows = yield* repository.listByThreadId({ threadId });
|
|
// Absent, not a synthesized default — consumers key on its absence to
|
|
// tell an ordinary message from a proactive delivery.
|
|
assert.equal(rows[0]?.notification, undefined);
|
|
}),
|
|
);
|
|
|
|
it.effect("allows explicit attachment clearing with an empty array", () =>
|
|
Effect.gen(function* () {
|
|
const repository = yield* ProjectionThreadMessageRepository;
|
|
const threadId = ThreadId.make("thread-clear-attachments");
|
|
const messageId = MessageId.make("message-clear-attachments");
|
|
const createdAt = "2026-02-28T19:10:00.000Z";
|
|
|
|
yield* repository.upsert({
|
|
messageId,
|
|
threadId,
|
|
turnId: null,
|
|
role: "assistant",
|
|
text: "with attachment",
|
|
attachments: [
|
|
{
|
|
type: "image",
|
|
id: "thread-clear-attachments-att-1",
|
|
name: "example.png",
|
|
mimeType: "image/png",
|
|
sizeBytes: 5,
|
|
},
|
|
],
|
|
isStreaming: false,
|
|
createdAt,
|
|
updatedAt: "2026-02-28T19:10:01.000Z",
|
|
});
|
|
|
|
yield* repository.upsert({
|
|
messageId,
|
|
threadId,
|
|
turnId: null,
|
|
role: "assistant",
|
|
text: "cleared",
|
|
attachments: [],
|
|
isStreaming: false,
|
|
createdAt,
|
|
updatedAt: "2026-02-28T19:10:02.000Z",
|
|
});
|
|
|
|
const rows = yield* repository.listByThreadId({ threadId });
|
|
assert.equal(rows.length, 1);
|
|
assert.equal(rows[0]?.text, "cleared");
|
|
assert.deepEqual(rows[0]?.attachments, []);
|
|
}),
|
|
);
|
|
});
|