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]>
221 lines
7.6 KiB
TypeScript
221 lines
7.6 KiB
TypeScript
import * as SqlClient from "effect/unstable/sql/SqlClient";
|
|
import * as SqlSchema from "effect/unstable/sql/SqlSchema";
|
|
import * as Effect from "effect/Effect";
|
|
import * as Layer from "effect/Layer";
|
|
import * as Schema from "effect/Schema";
|
|
import * as Struct from "effect/Struct";
|
|
|
|
import { toPersistenceSqlError } from "../Errors.ts";
|
|
import {
|
|
DeleteProjectionThreadInput,
|
|
GetProjectionThreadInput,
|
|
ListProjectionThreadsByProjectInput,
|
|
ProjectionThread,
|
|
ProjectionThreadRepository,
|
|
type ProjectionThreadRepositoryShape,
|
|
} from "../Services/ProjectionThreads.ts";
|
|
import { ModelSelection, OrchestrationThreadNotificationSummary } from "@t3tools/contracts";
|
|
|
|
const ProjectionThreadDbRow = ProjectionThread.mapFields(
|
|
Struct.assign({
|
|
modelSelection: Schema.fromJsonString(ModelSelection),
|
|
latestNotification: Schema.NullOr(
|
|
Schema.fromJsonString(OrchestrationThreadNotificationSummary),
|
|
),
|
|
}),
|
|
);
|
|
type ProjectionThreadDbRow = typeof ProjectionThreadDbRow.Type;
|
|
|
|
const makeProjectionThreadRepository = Effect.gen(function* () {
|
|
const sql = yield* SqlClient.SqlClient;
|
|
|
|
const upsertProjectionThreadRow = SqlSchema.void({
|
|
Request: ProjectionThread,
|
|
execute: (row) => {
|
|
const latestNotificationJson =
|
|
row.latestNotification === null ? null : JSON.stringify(row.latestNotification);
|
|
return sql`
|
|
INSERT INTO projection_threads (
|
|
thread_id,
|
|
project_id,
|
|
title,
|
|
model_selection_json,
|
|
runtime_mode,
|
|
interaction_mode,
|
|
branch,
|
|
worktree_path,
|
|
latest_turn_id,
|
|
created_at,
|
|
updated_at,
|
|
archived_at,
|
|
settled_override,
|
|
settled_at,
|
|
snoozed_until,
|
|
snoozed_at,
|
|
latest_user_message_at,
|
|
pending_approval_count,
|
|
pending_user_input_count,
|
|
has_actionable_proposed_plan,
|
|
latest_notification_json,
|
|
deleted_at
|
|
)
|
|
VALUES (
|
|
${row.threadId},
|
|
${row.projectId},
|
|
${row.title},
|
|
${JSON.stringify(row.modelSelection)},
|
|
${row.runtimeMode},
|
|
${row.interactionMode},
|
|
${row.branch},
|
|
${row.worktreePath},
|
|
${row.latestTurnId},
|
|
${row.createdAt},
|
|
${row.updatedAt},
|
|
${row.archivedAt},
|
|
${row.settledOverride},
|
|
${row.settledAt},
|
|
${row.snoozedUntil},
|
|
${row.snoozedAt},
|
|
${row.latestUserMessageAt},
|
|
${row.pendingApprovalCount},
|
|
${row.pendingUserInputCount},
|
|
${row.hasActionableProposedPlan},
|
|
${latestNotificationJson},
|
|
${row.deletedAt}
|
|
)
|
|
ON CONFLICT (thread_id)
|
|
DO UPDATE SET
|
|
project_id = excluded.project_id,
|
|
title = excluded.title,
|
|
model_selection_json = excluded.model_selection_json,
|
|
runtime_mode = excluded.runtime_mode,
|
|
interaction_mode = excluded.interaction_mode,
|
|
branch = excluded.branch,
|
|
worktree_path = excluded.worktree_path,
|
|
latest_turn_id = excluded.latest_turn_id,
|
|
created_at = excluded.created_at,
|
|
updated_at = excluded.updated_at,
|
|
archived_at = excluded.archived_at,
|
|
settled_override = excluded.settled_override,
|
|
settled_at = excluded.settled_at,
|
|
snoozed_until = excluded.snoozed_until,
|
|
snoozed_at = excluded.snoozed_at,
|
|
latest_user_message_at = excluded.latest_user_message_at,
|
|
pending_approval_count = excluded.pending_approval_count,
|
|
pending_user_input_count = excluded.pending_user_input_count,
|
|
has_actionable_proposed_plan = excluded.has_actionable_proposed_plan,
|
|
latest_notification_json = excluded.latest_notification_json,
|
|
deleted_at = excluded.deleted_at
|
|
`;
|
|
},
|
|
});
|
|
|
|
const getProjectionThreadRow = SqlSchema.findOneOption({
|
|
Request: GetProjectionThreadInput,
|
|
Result: ProjectionThreadDbRow,
|
|
execute: ({ threadId }) =>
|
|
sql`
|
|
SELECT
|
|
thread_id AS "threadId",
|
|
project_id AS "projectId",
|
|
title,
|
|
model_selection_json AS "modelSelection",
|
|
runtime_mode AS "runtimeMode",
|
|
interaction_mode AS "interactionMode",
|
|
branch,
|
|
worktree_path AS "worktreePath",
|
|
latest_turn_id AS "latestTurnId",
|
|
created_at AS "createdAt",
|
|
updated_at AS "updatedAt",
|
|
archived_at AS "archivedAt",
|
|
settled_override AS "settledOverride",
|
|
settled_at AS "settledAt",
|
|
snoozed_until AS "snoozedUntil",
|
|
snoozed_at AS "snoozedAt",
|
|
latest_user_message_at AS "latestUserMessageAt",
|
|
pending_approval_count AS "pendingApprovalCount",
|
|
pending_user_input_count AS "pendingUserInputCount",
|
|
has_actionable_proposed_plan AS "hasActionableProposedPlan",
|
|
latest_notification_json AS "latestNotification",
|
|
deleted_at AS "deletedAt"
|
|
FROM projection_threads
|
|
WHERE thread_id = ${threadId}
|
|
`,
|
|
});
|
|
|
|
const listProjectionThreadRows = SqlSchema.findAll({
|
|
Request: ListProjectionThreadsByProjectInput,
|
|
Result: ProjectionThreadDbRow,
|
|
execute: ({ projectId }) =>
|
|
sql`
|
|
SELECT
|
|
thread_id AS "threadId",
|
|
project_id AS "projectId",
|
|
title,
|
|
model_selection_json AS "modelSelection",
|
|
runtime_mode AS "runtimeMode",
|
|
interaction_mode AS "interactionMode",
|
|
branch,
|
|
worktree_path AS "worktreePath",
|
|
latest_turn_id AS "latestTurnId",
|
|
created_at AS "createdAt",
|
|
updated_at AS "updatedAt",
|
|
archived_at AS "archivedAt",
|
|
settled_override AS "settledOverride",
|
|
settled_at AS "settledAt",
|
|
snoozed_until AS "snoozedUntil",
|
|
snoozed_at AS "snoozedAt",
|
|
latest_user_message_at AS "latestUserMessageAt",
|
|
pending_approval_count AS "pendingApprovalCount",
|
|
pending_user_input_count AS "pendingUserInputCount",
|
|
has_actionable_proposed_plan AS "hasActionableProposedPlan",
|
|
latest_notification_json AS "latestNotification",
|
|
deleted_at AS "deletedAt"
|
|
FROM projection_threads
|
|
WHERE project_id = ${projectId}
|
|
ORDER BY created_at ASC, thread_id ASC
|
|
`,
|
|
});
|
|
|
|
const deleteProjectionThreadRow = SqlSchema.void({
|
|
Request: DeleteProjectionThreadInput,
|
|
execute: ({ threadId }) =>
|
|
sql`
|
|
DELETE FROM projection_threads
|
|
WHERE thread_id = ${threadId}
|
|
`,
|
|
});
|
|
|
|
const upsert: ProjectionThreadRepositoryShape["upsert"] = (row) =>
|
|
upsertProjectionThreadRow(row).pipe(
|
|
Effect.mapError(toPersistenceSqlError("ProjectionThreadRepository.upsert:query")),
|
|
);
|
|
|
|
const getById: ProjectionThreadRepositoryShape["getById"] = (input) =>
|
|
getProjectionThreadRow(input).pipe(
|
|
Effect.mapError(toPersistenceSqlError("ProjectionThreadRepository.getById:query")),
|
|
);
|
|
|
|
const listByProjectId: ProjectionThreadRepositoryShape["listByProjectId"] = (input) =>
|
|
listProjectionThreadRows(input).pipe(
|
|
Effect.mapError(toPersistenceSqlError("ProjectionThreadRepository.listByProjectId:query")),
|
|
);
|
|
|
|
const deleteById: ProjectionThreadRepositoryShape["deleteById"] = (input) =>
|
|
deleteProjectionThreadRow(input).pipe(
|
|
Effect.mapError(toPersistenceSqlError("ProjectionThreadRepository.deleteById:query")),
|
|
);
|
|
|
|
return {
|
|
upsert,
|
|
getById,
|
|
listByProjectId,
|
|
deleteById,
|
|
} satisfies ProjectionThreadRepositoryShape;
|
|
});
|
|
|
|
export const ProjectionThreadRepositoryLive = Layer.effect(
|
|
ProjectionThreadRepository,
|
|
makeProjectionThreadRepository,
|
|
);
|