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]>
105 lines
3.3 KiB
TypeScript
105 lines
3.3 KiB
TypeScript
/**
|
|
* ProjectionThreadMessageRepository - Projection repository interface for messages.
|
|
*
|
|
* Owns persistence operations for projected thread messages rendered in the
|
|
* orchestration read model.
|
|
*
|
|
* @module ProjectionThreadMessageRepository
|
|
*/
|
|
import {
|
|
ChatAttachment,
|
|
MessageId,
|
|
OrchestrationMessageNotification,
|
|
OrchestrationMessageRole,
|
|
ThreadId,
|
|
TurnId,
|
|
IsoDateTime,
|
|
} from "@t3tools/contracts";
|
|
import * as Schema from "effect/Schema";
|
|
import * as Context from "effect/Context";
|
|
import type * as Option from "effect/Option";
|
|
import type * as Effect from "effect/Effect";
|
|
|
|
import type { ProjectionRepositoryError } from "../Errors.ts";
|
|
|
|
export const ProjectionThreadMessage = Schema.Struct({
|
|
messageId: MessageId,
|
|
threadId: ThreadId,
|
|
turnId: Schema.NullOr(TurnId),
|
|
role: OrchestrationMessageRole,
|
|
text: Schema.String,
|
|
attachments: Schema.optional(Schema.Array(ChatAttachment)),
|
|
/**
|
|
* Proactive-delivery provenance, present only on notification rows.
|
|
*
|
|
* Optional like `attachments`, and persisted with the same
|
|
* `COALESCE(excluded, existing)` upsert rule: an ordinary update that omits
|
|
* it must not erase the provenance of a row that has it.
|
|
*/
|
|
notification: Schema.optional(OrchestrationMessageNotification),
|
|
isStreaming: Schema.Boolean,
|
|
createdAt: IsoDateTime,
|
|
updatedAt: IsoDateTime,
|
|
});
|
|
export type ProjectionThreadMessage = typeof ProjectionThreadMessage.Type;
|
|
|
|
export const ListProjectionThreadMessagesInput = Schema.Struct({
|
|
threadId: ThreadId,
|
|
});
|
|
export type ListProjectionThreadMessagesInput = typeof ListProjectionThreadMessagesInput.Type;
|
|
|
|
export const GetProjectionThreadMessageInput = Schema.Struct({
|
|
messageId: MessageId,
|
|
});
|
|
export type GetProjectionThreadMessageInput = typeof GetProjectionThreadMessageInput.Type;
|
|
|
|
export const DeleteProjectionThreadMessagesInput = Schema.Struct({
|
|
threadId: ThreadId,
|
|
});
|
|
export type DeleteProjectionThreadMessagesInput = typeof DeleteProjectionThreadMessagesInput.Type;
|
|
|
|
/**
|
|
* ProjectionThreadMessageRepositoryShape - Service API for projected thread messages.
|
|
*/
|
|
export interface ProjectionThreadMessageRepositoryShape {
|
|
/**
|
|
* Insert or replace a projected thread message row.
|
|
*
|
|
* Upserts by `messageId`.
|
|
*/
|
|
readonly upsert: (
|
|
message: ProjectionThreadMessage,
|
|
) => Effect.Effect<void, ProjectionRepositoryError>;
|
|
|
|
/**
|
|
* Read a projected thread message by id.
|
|
*/
|
|
readonly getByMessageId: (
|
|
input: GetProjectionThreadMessageInput,
|
|
) => Effect.Effect<Option.Option<ProjectionThreadMessage>, ProjectionRepositoryError>;
|
|
|
|
/**
|
|
* List projected thread messages for a thread.
|
|
*
|
|
* Returned in ascending creation order.
|
|
*/
|
|
readonly listByThreadId: (
|
|
input: ListProjectionThreadMessagesInput,
|
|
) => Effect.Effect<ReadonlyArray<ProjectionThreadMessage>, ProjectionRepositoryError>;
|
|
|
|
/**
|
|
* Delete projected thread messages by thread.
|
|
*/
|
|
readonly deleteByThreadId: (
|
|
input: DeleteProjectionThreadMessagesInput,
|
|
) => Effect.Effect<void, ProjectionRepositoryError>;
|
|
}
|
|
|
|
/**
|
|
* ProjectionThreadMessageRepository - Service tag for message projection persistence.
|
|
*/
|
|
export class ProjectionThreadMessageRepository extends Context.Service<
|
|
ProjectionThreadMessageRepository,
|
|
ProjectionThreadMessageRepositoryShape
|
|
>()("t3/persistence/Services/ProjectionThreadMessages/ProjectionThreadMessageRepository") {}
|