diff --git a/apps/server/src/kin/KinServer.ts b/apps/server/src/kin/KinServer.ts index d61dcc1cc5031..7d849fad0420c 100644 --- a/apps/server/src/kin/KinServer.ts +++ b/apps/server/src/kin/KinServer.ts @@ -7,8 +7,15 @@ import { KinChatService } from "./chat/KinChatService.ts"; import { KinChatHub } from "./chat/KinChatHub.ts"; import { KinBudget } from "./lead/KinBudget.ts"; import { KinEventQueue } from "./lead/KinEventQueue.ts"; +import type { KinGithubWebhookLock } from "./triggers/githubWebhooks.ts"; -export type Services = KinIdentity | KinChatService | KinBudget | KinEventQueue | KinChatHub; +export type Services = + | KinIdentity + | KinChatService + | KinBudget + | KinEventQueue + | KinChatHub + | KinGithubWebhookLock; export class KinServer extends Context.Service< KinServer, diff --git a/apps/server/src/kin/contract/fixtures.ts b/apps/server/src/kin/contract/fixtures.ts index 82c79d7082eb1..279a801b96d82 100644 --- a/apps/server/src/kin/contract/fixtures.ts +++ b/apps/server/src/kin/contract/fixtures.ts @@ -19,6 +19,7 @@ import * as ScheduledTasks from "../../scheduledTasks/ScheduledTaskService.ts"; import * as Scheduler from "../../scheduling/Scheduler.ts"; import * as SecretRequests from "../../secrets/SecretRequests.ts"; import * as KinTriggerSink from "../triggers/KinTriggerSink.ts"; +import * as KinTriggerStore from "../triggers/KinTriggerStore.ts"; const decodeEvent = Schema.decodeSync(OrchestrationV2DomainEventJson); @@ -59,6 +60,8 @@ const projection = () => { return emptyProjection(event); }; +export const CONTRACT_BOT_LOGIN = "kin-contract[bot]"; + export const webhookInput = (overrides: Partial = {}) => decodeUpsert({ id: "kin-github", @@ -80,18 +83,35 @@ export const withScheduledTasks = Effect.fn("KinContract.withScheduledTasks")(fu service: ScheduledTasks.ScheduledTaskService["Service"]; launches: Queue.Queue; sends: Queue.Queue; - launchDeliveries: Queue.Queue>; + launchDeliveries: Queue.Queue< + ReadonlyArray<{ readonly receivedAt: unknown; readonly secret: string }> + >; + launchInFlight: Queue.Queue; }) => Effect.Effect, policy?: ManagedPolicyDocument, + workers?: Map>, ) { const sql = yield* SqlClient.SqlClient; const launchDeliveries = - yield* Queue.unbounded>(); + yield* Queue.unbounded< + ReadonlyArray<{ readonly receivedAt: unknown; readonly secret: string }> + >(); const launches = yield* Queue.unbounded(); + const launchInFlight = yield* Queue.unbounded(); const sends = yield* Queue.unbounded(); const dependencies = Layer.mergeAll( NodeCrypto.layer, - Scheduler.layer, + workers === undefined + ? Scheduler.layer + : Layer.succeed(Scheduler.Scheduler, { + register: Effect.fn("KinContract.register")(function* ( + name: string, + work: Effect.Effect, + ) { + const context = yield* Effect.context(); + workers.set(name, work.pipe(Effect.provideContext(context), Effect.orDie)); + }), + }), ManagedPolicy.layerTest(policy), Layer.mock(ThreadLaunch.ThreadLaunchService)({ launch: Effect.fn("KinContract.launch")(function* (input) { @@ -102,8 +122,13 @@ export const withScheduledTasks = Effect.fn("KinContract.withScheduledTasks")(fu taskId: taskId ?? "", deliveryId, projectId: input.projectId, + botLogin: CONTRACT_BOT_LOGIN, }).pipe(Effect.orDie); yield* Queue.offer(launchDeliveries, rows); + const inFlight = yield* KinTriggerStore.inFlightDeliveries(sql, taskId ?? "").pipe( + Effect.orDie, + ); + yield* Queue.offer(launchInFlight, inFlight.length); yield* Queue.offer(launches, input); const result = projection(); return { threadId: result.thread.id, projection: result, resumed: false }; @@ -126,6 +151,6 @@ export const withScheduledTasks = Effect.fn("KinContract.withScheduledTasks")(fu ); return yield* Effect.gen(function* () { const service = yield* ScheduledTasks.ScheduledTaskService; - return yield* body({ service, launches, sends, launchDeliveries }); + return yield* body({ service, launches, sends, launchDeliveries, launchInFlight }); }).pipe(Effect.provide(Layer.fresh(ScheduledTasks.layer.pipe(Layer.provide(dependencies))))); }); diff --git a/apps/server/src/kin/contract/scheduledTask.kin-contract.test.ts b/apps/server/src/kin/contract/scheduledTask.kin-contract.test.ts index ef3f262fab51b..1081c402c6105 100644 --- a/apps/server/src/kin/contract/scheduledTask.kin-contract.test.ts +++ b/apps/server/src/kin/contract/scheduledTask.kin-contract.test.ts @@ -12,11 +12,13 @@ import * as Effect from "effect/Effect"; import * as Queue from "effect/Queue"; import * as Schema from "effect/Schema"; import * as SqlClient from "effect/sql/SqlClient"; +import * as TestClock from "effect/testing/TestClock"; import * as Tracer from "effect/Tracer"; import * as Sqlite from "../../persistence/Sqlite.ts"; import { nextScheduledRunAt } from "../../scheduledTasks/Schedule.ts"; -import { webhookInput, withScheduledTasks } from "./fixtures.ts"; +import * as KinTriggerStore from "../triggers/KinTriggerStore.ts"; +import { CONTRACT_BOT_LOGIN, webhookInput, withScheduledTasks } from "./fixtures.ts"; const decodeSchedule = Schema.decodeSync(ScheduledTaskUpsertSchedule); const decodeReceivedAt = Schema.decodeUnknownEffect(CanonicalUtcTimestamp); @@ -67,6 +69,7 @@ it.effect("preserves the upstream database columns Kin reads", () => for (const [table, columns] of [ ["orchestration_v2_projection_runs", ["run_id", "thread_id", "status", "payload_json"]], ["orchestration_v2_projection_provider_turns", ["thread_id", "completed_at"]], + ["scheduled_task_webhook_pending", ["task_id", "state"]], ] as const) { const rows = yield* sql`PRAGMA table_info(${sql.literal(table)})`; assert.includeMembers( @@ -78,7 +81,16 @@ it.effect("preserves the upstream database columns Kin reads", () => const deliveries = yield* sql`PRAGMA table_info(scheduled_task_webhook_deliveries)`; assert.includeMembers( tasks.map((row) => row.name), - ["task_id", "project_id", "thread_id", "thread_mode", "schedule_json"], + [ + "task_id", + "project_id", + "thread_id", + "thread_mode", + "schedule_json", + "enabled", + "last_run_status", + "webhook_secret", + ], ); assert.includeMembers( deliveries.map((row) => row.name), @@ -89,78 +101,130 @@ it.effect("preserves the upstream database columns Kin reads", () => it.effect( "dispatches signed GitHub deliveries with Kin's provenance, span and replay identity", - () => - withScheduledTasks(({ service, launches, sends, launchDeliveries }) => - Effect.gen(function* () { - const finished = yield* Deferred.make(); - const tracer = Tracer.make({ - span: (options) => { - const span = new Tracer.NativeSpan(options); - const end = span.end.bind(span); - span.end = (...args) => { - end(...args); - if (span.name === "ScheduledTaskService.runWebhookDelivery") - Deferred.doneUnsafe(finished, Effect.void); - }; - return span; - }, - }); - const { task } = yield* service.upsert( - yield* webhookInput({ - schedule: { - type: "webhook", - signature: { - header: "x-hub-signature-256", - encoding: "hex", - prefix: "sha256=", - secret: "contract-secret", + () => { + const workers = new Map>(); + return withScheduledTasks( + ({ service, launches, sends, launchDeliveries, launchInFlight }) => + Effect.gen(function* () { + const finished = yield* Deferred.make(); + const tracer = Tracer.make({ + span: (options) => { + const span = new Tracer.NativeSpan(options); + const end = span.end.bind(span); + span.end = (...args) => { + end(...args); + if (span.name === "ScheduledTaskService.runWebhookDelivery") + Deferred.doneUnsafe(finished, Effect.void); + }; + return span; + }, + }); + const signature = { + header: "x-hub-signature-256", + encoding: "hex", + prefix: "sha256=", + secret: "contract-secret", + } as const; + const filters = [ + { path: "headers.x-github-event", op: "in", value: KinTriggerStore.GITHUB_EVENTS }, + { path: "body.sender.login", op: "notEquals", value: CONTRACT_BOT_LOGIN }, + ] as const; + const { task } = yield* service.upsert( + yield* webhookInput({ + schedule: { + type: "webhook", + signature, + dedupe: { replayKey: KinTriggerStore.GITHUB_REPLAY_KEY }, + filters, }, - dedupe: { replayKey: "{{headers.x-github-delivery}}" }, + }), + ); + const bodyText = JSON.stringify({ + action: "opened", + number: 18, + sender: { login: "dev" }, + }); + const body = new TextEncoder().encode(bodyText); + const request = { + hookId: task.id, + token: task.webhook?.path.split("/").at(-1) ?? "", + method: "POST", + path: `/api/hooks/${task.id}`, + query: "", + body, + bodyText, + headers: { + "content-type": "application/json", + "x-github-event": "pull_request", + "x-github-delivery": "github-18", + "x-hub-signature-256": `sha256=${NodeCrypto.createHmac("sha256", "contract-secret").update(body).digest("hex")}`, }, - }), - ); - const bodyText = JSON.stringify({ action: "opened", number: 18 }); - const body = new TextEncoder().encode(bodyText); - const request = { - hookId: task.id, - token: task.webhook?.path.split("/").at(-1) ?? "", - method: "POST", - path: `/api/hooks/${task.id}`, - query: "", - body, - bodyText, - headers: { - "content-type": "application/json", - "x-github-event": "pull_request", - "x-github-delivery": "github-18", - "x-hub-signature-256": `sha256=${NodeCrypto.createHmac("sha256", "contract-secret").update(body).digest("hex")}`, - }, - }; - const accepted = yield* service.triggerWebhook(request).pipe(Effect.withTracer(tracer)); - if (accepted._tag !== "accepted") return assert.fail(`Unexpected ${accepted._tag}`); - yield* Deferred.await(finished); - const launch = yield* Queue.take(launches); - const rows = yield* Queue.take(launchDeliveries); - assert.lengthOf(rows, 1); - const [row] = rows; - if (row === undefined) return assert.fail("Delivery missing at launch"); - const receivedAt = yield* decodeReceivedAt(row.receivedAt); - assert.equal(DateTime.formatIso(receivedAt), row.receivedAt); - assert.equal(launch.initialMessage?.scheduledTaskId, task.id); - assert.equal(launch.commandId, `scheduled-task:${task.id}:webhook:${accepted.deliveryId}`); - const { delivery } = yield* service.getWebhookDelivery({ - id: task.id, - deliveryId: accepted.deliveryId, - }); - assert.equal(delivery.headers["x-github-event"], "pull_request"); - assert.equal(delivery.body, bodyText); - assert.equal(delivery.outcome, "accepted"); - const replay = yield* service.triggerWebhook(request); - assert.deepInclude(replay, { _tag: "accepted", outcome: "duplicate" }); - assert.equal(yield* Queue.size(launches), 0); - assert.equal(yield* Queue.size(sends), 0); - }), - ).pipe(Effect.provide(Sqlite.layerMemory)), + }; + const accepted = yield* service.triggerWebhook(request).pipe(Effect.withTracer(tracer)); + if (accepted._tag !== "accepted") return assert.fail(`Unexpected ${accepted._tag}`); + yield* Deferred.await(finished); + const launch = yield* Queue.take(launches); + const rows = yield* Queue.take(launchDeliveries); + assert.lengthOf(rows, 1); + assert.equal(yield* Queue.take(launchInFlight), 1); + const [row] = rows; + if (row === undefined) return assert.fail("Delivery missing at launch"); + const receivedAt = yield* decodeReceivedAt(row.receivedAt); + assert.equal(DateTime.formatIso(receivedAt), row.receivedAt); + assert.equal(row.secret, signature.secret); + assert.equal(launch.initialMessage?.scheduledTaskId, task.id); + assert.equal( + launch.commandId, + `scheduled-task:${task.id}:webhook:${accepted.deliveryId}`, + ); + const { delivery } = yield* service.getWebhookDelivery({ + id: task.id, + deliveryId: accepted.deliveryId, + }); + assert.equal(delivery.headers["x-github-event"], "pull_request"); + assert.equal(delivery.body, bodyText); + assert.equal(delivery.outcome, "accepted"); + const replay = yield* service.triggerWebhook(request); + assert.deepInclude(replay, { _tag: "accepted", outcome: "duplicate" }); + assert.equal(yield* Queue.size(launches), 0); + + // A coalesced delivery launches from the pending queue, so both in-flight arms match. + // The sink's lookup refuses it: a coalesce window could hold a delivery past a re-key. + const { task: queued } = yield* service.upsert( + yield* webhookInput({ + id: "kin-github-queued", + schedule: { + type: "webhook", + signature, + dedupe: { + replayKey: KinTriggerStore.GITHUB_REPLAY_KEY, + coalesceKey: "{{headers.x-github-event}}", + coalesceWindowSeconds: 10, + }, + filters, + }, + }), + ); + const window = yield* service.triggerWebhook({ + ...request, + hookId: queued.id, + token: queued.webhook?.path.split("/").at(-1) ?? "", + path: `/api/hooks/${queued.id}`, + }); + assert.deepInclude(window, { _tag: "accepted", outcome: "accepted" }); + yield* TestClock.adjust("10 seconds"); + const worker = workers.get("webhook-coalescing"); + if (worker === undefined) return assert.fail("No coalescing worker"); + yield* worker; + assert.equal((yield* Queue.take(launches)).initialMessage?.scheduledTaskId, queued.id); + assert.lengthOf(yield* Queue.take(launchDeliveries), 0); + assert.equal(yield* Queue.take(launchInFlight), 2); + assert.equal(yield* Queue.size(sends), 0); + }), + undefined, + workers, + ).pipe(Effect.provide(Sqlite.layerMemory)); + }, ); it.effect("passes scheduledTaskId to sendToThread for bound tasks", () => diff --git a/apps/server/src/kin/layer.ts b/apps/server/src/kin/layer.ts index 5738235ecb49d..4b9c0944a4d1d 100644 --- a/apps/server/src/kin/layer.ts +++ b/apps/server/src/kin/layer.ts @@ -16,6 +16,7 @@ export { KinServer } from "./KinServer.ts"; import type * as McpSessionRegistry from "../mcp/McpSessionRegistry.ts"; import * as SqlitePersistence from "../persistence/Sqlite.ts"; import * as KinMcp from "./mcp/layer.ts"; +import * as GithubWebhooks from "./triggers/githubWebhooks.ts"; const enabled = Layer.effect( KinServer, @@ -33,7 +34,7 @@ const enabled = Layer.effect( subscribeForViewer: Option.some(hub.subscribeForViewer), }; }), -).pipe(Layer.provide(KinChat.layer)); +).pipe(Layer.provide(Layer.merge(KinChat.layer, GithubWebhooks.lockLayer))); const disabled = Layer.succeed(KinServer, { taskCardUpdater: Layer.empty, services: Option.none(), diff --git a/apps/server/src/kin/persistence/KinMigrations.test.ts b/apps/server/src/kin/persistence/KinMigrations.test.ts index de410e31cd0e6..1d8cc543fa8ed 100644 --- a/apps/server/src/kin/persistence/KinMigrations.test.ts +++ b/apps/server/src/kin/persistence/KinMigrations.test.ts @@ -114,6 +114,7 @@ describe("Kin migrations", () => { { migration_id: 16, name: "DeferralRuns" }, { migration_id: 17, name: "Workers" }, { migration_id: 18, name: "Dreams" }, + { migration_id: 19, name: "WebhookSecretHash" }, ]); assert.deepStrictEqual( upstreamBefore.map((row) => [row.migration_id, row.name] as const), @@ -179,6 +180,7 @@ describe("Kin migrations", () => { { migration_id: 16, name: "DeferralRuns" }, { migration_id: 17, name: "Workers" }, { migration_id: 18, name: "Dreams" }, + { migration_id: 19, name: "WebhookSecretHash" }, ]); assert.deepStrictEqual(yield* upstreamHistory, upstreamBefore); assert.deepStrictEqual(yield* sql`SELECT * FROM kin_orgs`, [ @@ -269,6 +271,7 @@ describe("Kin migrations", () => { [16, "DeferralRuns"], [17, "Workers"], [18, "Dreams"], + [19, "WebhookSecretHash"], ]); const before = yield* kinSchema; assert.deepStrictEqual( @@ -297,6 +300,8 @@ describe("Kin migrations", () => { yield* sql`DROP INDEX idx_kin_messages_changes`; yield* sql`ALTER TABLE kin_messages DROP COLUMN change_seq`; yield* sql`ALTER TABLE kin_chats DROP COLUMN change_seq`; + yield* sql`ALTER TABLE kin_webhook_sources DROP COLUMN secret_sha256`; + yield* sql`ALTER TABLE kin_webhook_sources DROP COLUMN secret_bound_at`; yield* sql`DROP TABLE kin_dream_runs`; yield* sql`DROP TABLE kin_deferral_runs`; yield* sql`DROP TABLE kin_auth_key_repairs`; @@ -339,6 +344,7 @@ describe("Kin migrations", () => { [16, "DeferralRuns"], [17, "Workers"], [18, "Dreams"], + [19, "WebhookSecretHash"], ]); assert.deepStrictEqual( yield* sql`SELECT member_id FROM kin_chat_members WHERE org_id = 'org' ORDER BY member_id`, @@ -382,6 +388,7 @@ describe("Kin migrations", () => { { migration_id: 16, name: "DeferralRuns" }, { migration_id: 17, name: "Workers" }, { migration_id: 18, name: "Dreams" }, + { migration_id: 19, name: "WebhookSecretHash" }, ]); assert.deepStrictEqual(yield* runKinMigrations(), []); assert.deepStrictEqual(yield* runMigrations(), []); diff --git a/apps/server/src/kin/persistence/KinMigrations.ts b/apps/server/src/kin/persistence/KinMigrations.ts index 3a793be895a23..8748d612eb98e 100644 --- a/apps/server/src/kin/persistence/KinMigrations.ts +++ b/apps/server/src/kin/persistence/KinMigrations.ts @@ -21,6 +21,7 @@ import AuthSessionKeys from "./migrations/K015_AuthSessionKeys.ts"; import DeferralRuns from "./migrations/K016_DeferralRuns.ts"; import Workers from "./migrations/K017_Workers.ts"; import Dreams from "./migrations/K018_Dreams.ts"; +import WebhookSecretHash from "./migrations/K019_WebhookSecretHash.ts"; const run = Migrator.make({}); const loader = Migrator.fromRecord({ @@ -42,6 +43,7 @@ const loader = Migrator.fromRecord({ "16_DeferralRuns": DeferralRuns, "17_Workers": Workers, "18_Dreams": Dreams, + "19_WebhookSecretHash": WebhookSecretHash, }); export const runKinMigrations = Effect.fn("KinMigrations.run")(function* () { diff --git a/apps/server/src/kin/persistence/migrations/K019_WebhookSecretHash.ts b/apps/server/src/kin/persistence/migrations/K019_WebhookSecretHash.ts new file mode 100644 index 0000000000000..2761ec17bf0d8 --- /dev/null +++ b/apps/server/src/kin/persistence/migrations/K019_WebhookSecretHash.ts @@ -0,0 +1,9 @@ +import * as Effect from "effect/Effect"; +import * as SqlClient from "effect/sql/SqlClient"; + +export default Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + // NULL refuses every delivery until Kin re-saves the secret it chose. + yield* sql`ALTER TABLE kin_webhook_sources ADD COLUMN secret_sha256 TEXT`; + yield* sql`ALTER TABLE kin_webhook_sources ADD COLUMN secret_bound_at INTEGER`; +}); diff --git a/apps/server/src/kin/rpcHandlers.ts b/apps/server/src/kin/rpcHandlers.ts index 9aca38a00947f..021c3023c8712 100644 --- a/apps/server/src/kin/rpcHandlers.ts +++ b/apps/server/src/kin/rpcHandlers.ts @@ -4,6 +4,7 @@ import * as RpcGroup from "effect/rpc/RpcGroup"; import type { AuthenticatedSession } from "../auth/EnvironmentAuth.ts"; import { makeKinApprovalHandlers } from "./approvals/rpcHandlers.ts"; import { makeKinChatHandlers } from "./chat/rpcHandlers.ts"; +import { makeKinWebhookHandlers } from "./triggers/webhookRpcHandlers.ts"; const group = RpcGroup.make(...KinRpcs); @@ -13,5 +14,6 @@ export const makeKinHandlers = Effect.fn("makeKinHandlers")(function* ( return group.of({ ...(yield* makeKinChatHandlers(session)), ...(yield* makeKinApprovalHandlers(session)), + ...(yield* makeKinWebhookHandlers(session)), }); }); diff --git a/apps/server/src/kin/triggers/KinTriggerSink.test.ts b/apps/server/src/kin/triggers/KinTriggerSink.test.ts index 38ec337580ceb..22e98aa38f2cd 100644 --- a/apps/server/src/kin/triggers/KinTriggerSink.test.ts +++ b/apps/server/src/kin/triggers/KinTriggerSink.test.ts @@ -9,21 +9,21 @@ import { MessageId, KinTriggerFired, KinWebhookSource, - OrchestrationV2ThreadProjectionJson, ScheduledTaskUpsertInput, ProjectId, ProviderInstanceId, ScheduledTaskId, + ScheduledTaskWebhookDeliveryId, } from "@t3tools/contracts"; import * as ConfigProvider from "effect/ConfigProvider"; -import * as Deferred from "effect/Deferred"; +import * as DateTime from "effect/DateTime"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; +import * as Redacted from "effect/Redacted"; import * as Schema from "effect/Schema"; import * as SqlClient from "effect/sql/SqlClient"; import * as TestClock from "effect/testing/TestClock"; -import * as Tracer from "effect/Tracer"; import { OrchestratorCommandRejectedError } from "../../orchestration-v2/Orchestrator.ts"; import * as ThreadLaunchService from "../../orchestration-v2/ThreadLaunchService.ts"; @@ -39,55 +39,14 @@ import * as KinPersistence from "../persistence/KinPersistence.ts"; import * as KinTriggerInbox from "./KinTriggerInbox.ts"; import * as KinTriggerSink from "./KinTriggerSink.ts"; import * as KinTriggerStore from "./KinTriggerStore.ts"; +import { projection, timestamp, waitForDispatch } from "./webhooks.testkit.ts"; -const timestamp = "2026-11-07T00:00:00.000Z"; const orgA = KinOrgId.make("org-a"); const orgB = KinOrgId.make("org-b"); const decodeSource = Schema.decodeUnknownSync(KinWebhookSource); -const decodeProjection = Schema.decodeSync(OrchestrationV2ThreadProjectionJson); const decodeUpsert = Schema.decodeEffect(ScheduledTaskUpsertInput); +const decodeUnknownUpsert = Schema.decodeUnknownEffect(ScheduledTaskUpsertInput); const decodeFire = Schema.decodeEffect(KinTriggerFired); -const projection = (orgId: KinOrgId) => - decodeProjection({ - thread: { - id: `inbox-${orgId}`, - projectId: `project-${orgId}`, - title: "Inbox", - providerInstanceId: "codex", - modelSelection: { instanceId: "codex", model: "gpt-5.4" }, - runtimeMode: "approval-required", - interactionMode: "plan", - branch: null, - worktreePath: null, - activeProviderThreadId: null, - lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: `inbox-${orgId}` }, - forkedFrom: null, - createdBy: "system", - creationSource: "server", - createdAt: timestamp, - updatedAt: timestamp, - archivedAt: timestamp, - deletedAt: null, - }, - runs: [], - attempts: [], - nodes: [], - subagents: [], - providerSessions: [], - providerThreads: [], - providerTurns: [], - runtimeRequests: [], - messages: [], - plans: [], - turnItems: [], - checkpointScopes: [], - checkpoints: [], - contextHandoffs: [], - contextTransfers: [], - visibleTurnItems: [], - updatedAt: timestamp, - }); - const configured = ConfigProvider.layer(ConfigProvider.fromUnknown({ KIN_ENABLED: true })); const database = KinPersistence.layer.pipe( Layer.provideMerge(Sqlite.layerMemory), @@ -140,31 +99,22 @@ const upsert = (id: string, orgId = orgA) => prefix: "sha256=", secret: "test-secret", }, - dedupe: { replayKey: "{{headers.x-github-delivery}}" }, - filters: [{ path: "body.sender.login", op: "notEquals", value: "kin[bot]" }], + dedupe: { replayKey: KinTriggerStore.GITHUB_REPLAY_KEY }, + filters: [ + { path: "headers.x-github-event", op: "in", value: KinTriggerStore.GITHUB_EVENTS }, + { path: "body.sender.login", op: "notEquals", value: "kin[bot]" }, + ], }, }); -// The runWebhookDelivery span ends after both delivery and run completion are persisted. -const waitForDispatch = (work: Effect.Effect) => - Effect.gen(function* () { - const done = yield* Deferred.make(); - const tracer = Tracer.make({ - span: (options) => { - const span = new Tracer.NativeSpan(options); - const end = span.end.bind(span); - span.end = (...args) => { - end(...args); - if (span.name === "ScheduledTaskService.runWebhookDelivery") - Deferred.doneUnsafe(done, Effect.void); - }; - return span; - }, - }); - const result = yield* work.pipe(Effect.withTracer(tracer)); - yield* Deferred.await(done); - return result; - }); +const testSecret = Redacted.make("test-secret"); + +const weakened = (id: string, schedule: Record) => + upsert(id).pipe( + Effect.flatMap((input) => + decodeUnknownUpsert({ ...input, schedule: { ...input.schedule, ...schedule } }), + ), + ); const threads = Layer.mock(ThreadManagementService.ThreadManagementService)({ getThreadProjection: (id) => Effect.succeed(projection(id === `inbox-${orgB}` ? orgB : orgA)), @@ -176,6 +126,7 @@ const withService = ( launches: ThreadLaunchService.ThreadLaunchInput[], sends: ThreadManagementService.ThreadManagementSendInput[], ) => Effect.Effect, + workers?: Map>, ) => Effect.gen(function* () { const launches: ThreadLaunchService.ThreadLaunchInput[] = []; @@ -207,7 +158,17 @@ const withService = ( Layer.mergeAll( KinTriggerSink.wrapTaskDispatch(inner, boundThreads), Crypto.layer, - Scheduler.layer, + workers === undefined + ? Scheduler.layer + : Layer.succeed(Scheduler.Scheduler, { + register: Effect.fn("KinTriggerSinkTest.register")(function* ( + name: string, + work: Effect.Effect, + ) { + const context = yield* Effect.context(); + workers.set(name, work.pipe(Effect.provideContext(context), Effect.orDie)); + }), + }), ManagedPolicy.layerTest(), Layer.mock(SecretRequests.SecretRequests)({}), Layer.mock(ManagedProjectFolders.ManagedProjectFolders)({ @@ -230,6 +191,7 @@ const request = ( task: { id: string; webhook?: { path: string } | undefined }, githubDelivery: string, sender = "owner", + secret = Redacted.value(testSecret), ) => { const bodyText = JSON.stringify({ sender: { login: sender }, orgId: orgB, sourceId: "foreign" }); return { @@ -244,7 +206,7 @@ const request = ( "content-type": "application/json", "x-github-event": "pull_request", "x-github-delivery": githubDelivery, - "x-hub-signature-256": `sha256=${NodeCrypto.createHmac("sha256", "test-secret").update(bodyText).digest("hex")}`, + "x-hub-signature-256": `sha256=${NodeCrypto.createHmac("sha256", secret).update(bodyText).digest("hex")}`, }, }; }; @@ -272,6 +234,8 @@ describe("KinTriggerSink", () => { const { task } = yield* service.upsert(yield* upsert(`task-${org}`, org)); assert.deepStrictEqual(yield* store.registerSource(source(org)), source(org)); assert.deepStrictEqual(yield* store.registerSource(source(org)), source(org)); + yield* store.bindSecret(org, source(org).id, testSecret); + yield* TestClock.adjust("1 second"); const first = yield* waitForDispatch( service.triggerWebhook(request(task, "github-delivery")), ); @@ -375,62 +339,192 @@ describe("KinTriggerSink", () => { yield* seed; const store = yield* KinTriggerStore.KinTriggerStore; const inbox = yield* KinTriggerInbox.KinTriggerInbox; - yield* withService((service, launches, sends) => - Effect.gen(function* () { - const input = yield* upsert("task-org-a"); - yield* service.upsert(input); - yield* store.registerSource(source(orgA)); - const { task } = yield* service.upsert({ - ...input, - threadId: projection(orgA).thread.id, - threadMode: "continue", - }); - const result = yield* waitForDispatch(service.triggerWebhook(request(task, "bound"))); - assert.strictEqual(result._tag, "accepted"); - if (result._tag !== "accepted") return; - const { delivery } = yield* service.getWebhookDelivery({ - id: task.id, - deliveryId: result.deliveryId, - acceptAnyOutcome: true, - }); - assert.strictEqual(delivery.outcome, "dispatch_failed"); - const recorded = (yield* service.list()).tasks.find((item) => item.id === task.id); - assert.strictEqual(recorded?.lastRunStatus, "failed"); - assert.include(recorded?.lastRunError ?? "", "kin.trigger.bound_source"); - assert.deepStrictEqual(yield* inbox.listPending(orgA), []); - assert.lengthOf(sends, 0); - assert.lengthOf(launches, 0); - const { task: unbound } = yield* service.upsert(input); - yield* waitForDispatch(service.triggerWebhook(request(unbound, "unbound"))); - assert.lengthOf(yield* inbox.listPending(orgA), 1); - assert.lengthOf(sends, 0); - assert.lengthOf(launches, 0); - const ordinary = yield* upsert("ordinary-bound"); - const { task: other } = yield* service.upsert({ - ...ordinary, - threadId: projection(orgA).thread.id, - threadMode: "continue", - }); - yield* waitForDispatch(service.triggerWebhook(request(other, "ordinary-bound"))); - assert.lengthOf(sends, 1); - assert.strictEqual(sends[0]?.scheduledTaskId, other.id); - const sql = yield* SqlClient.SqlClient; - yield* sql`DROP TABLE kin_webhook_sources`; - const lookupFailure = yield* waitForDispatch( - service.triggerWebhook(request(other, "lookup-failure")), - ); - assert.strictEqual(lookupFailure._tag, "accepted"); - if (lookupFailure._tag !== "accepted") return; - const failed = yield* service.getWebhookDelivery({ - id: other.id, - deliveryId: lookupFailure.deliveryId, - acceptAnyOutcome: true, - }); - assert.strictEqual(failed.delivery.outcome, "dispatch_failed"); - assert.lengthOf(yield* inbox.listPending(orgA), 1); - assert.lengthOf(sends, 1); - assert.lengthOf(launches, 0); - }), + const workers = new Map>(); + yield* withService( + (service, launches, sends) => + Effect.gen(function* () { + const input = yield* upsert("task-org-a"); + yield* service.upsert(input); + yield* store.registerSource(source(orgA)); + const { task } = yield* service.upsert({ + ...input, + threadId: projection(orgA).thread.id, + threadMode: "continue", + }); + const outcome = ( + task: { readonly id: ScheduledTaskId }, + deliveryId: ScheduledTaskWebhookDeliveryId, + ) => + service + .getWebhookDelivery({ id: task.id, deliveryId, acceptAnyOutcome: true }) + .pipe(Effect.map(({ delivery }) => delivery.outcome)); + const dispatchFailed = ( + task: { readonly id: ScheduledTaskId }, + key: string, + secret?: string, + receivedAt?: string, + ) => + Effect.gen(function* () { + const result = yield* waitForDispatch( + service.triggerWebhook({ + ...request(task, key, "owner", secret), + ...(receivedAt === undefined ? {} : { receivedAt }), + }), + ); + if (result._tag !== "accepted") return assert.fail(`Unexpected ${result._tag}`); + assert.strictEqual(yield* outcome(task, result.deliveryId), "dispatch_failed", key); + }); + yield* dispatchFailed(task, "bound"); + const recorded = (yield* service.list()).tasks.find((item) => item.id === task.id); + assert.strictEqual(recorded?.lastRunStatus, "failed"); + assert.include(recorded?.lastRunError ?? "", "kin.trigger.bound_source"); + assert.deepStrictEqual(yield* inbox.listPending(orgA), []); + assert.lengthOf(sends, 0); + assert.lengthOf(launches, 0); + const { task: unbound } = yield* service.upsert(input); + // A source with no bound secret hash, such as one from before K019, refuses. + yield* dispatchFailed(unbound, "no-hash"); + yield* store.bindSecret(orgA, source(orgA).id, testSecret); + const boundAt = 1794009600000; + yield* TestClock.adjust("1 second"); + yield* waitForDispatch(service.triggerWebhook(request(unbound, "unbound"))); + assert.lengthOf(yield* inbox.listPending(orgA), 1); + assert.lengthOf(sends, 0); + assert.lengthOf(launches, 0); + const sender = { + path: "body.sender.login", + op: "notEquals", + value: "kin[bot]", + } as const; + const events = { + path: "headers.x-github-event", + op: "in", + value: KinTriggerStore.GITHUB_EVENTS, + } as const; + // Any agent can re-save the task through schedule_task; a weakened one never routes. + for (const [key, schedule, secret, receivedAt] of [ + ["unsigned", { signature: null }], + ["unfiltered", { filters: [] }], + [ + "other-bot", + { + filters: [events, { ...sender, value: "other[bot]" }], + }, + ], + ["no-dedupe", { dedupe: {} }], + ["no-event-filter", { filters: [sender] }], + ["fewer-events", { filters: [{ ...events, value: ["pull_request"] }, sender] }], + [ + "swapped-secret", + { + signature: { + header: "x-hub-signature-256", + encoding: "hex", + prefix: "sha256=", + secret: "agent-secret", + }, + }, + "agent-secret", + ], + // A held relay delivery verified before the bind, so possibly against another secret. + [ + "received-before-bind", + {}, + undefined, + DateTime.formatIso(DateTime.makeUnsafe(boundAt - 60_000)), + ], + ["received-at-bind", {}, undefined, DateTime.formatIso(DateTime.makeUnsafe(boundAt))], + ] as const) { + const { task: weak } = yield* service.upsert(yield* weakened("task-org-a", schedule)); + yield* dispatchFailed(weak, key, secret, receivedAt); + } + // A coalesce window holds a delivery past the owner's re-key, so the sink pins the + // whole dedupe object and refuses deliveries received before the bind. + const coalesce = { + ...KinTriggerStore.GITHUB_DEDUPE, + coalesceKey: "{{headers.x-github-event}}", + coalesceWindowSeconds: 3600, + }; + const agentSigned = { + header: "x-hub-signature-256", + encoding: "hex", + prefix: "sha256=", + secret: "agent-secret", + }; + for (const [key, schedule, secret, rekey] of [ + ["coalesced", { dedupe: coalesce }, undefined, false], + [ + "coalesced-rekey", + { dedupe: coalesce, signature: agentSigned }, + "agent-secret", + true, + ], + ] as const) { + const { task: held } = yield* service.upsert(yield* weakened("task-org-a", schedule)); + const queued = yield* service.triggerWebhook(request(held, key, "owner", secret)); + if (queued._tag !== "accepted") return assert.fail(`Unexpected ${queued._tag}`); + assert.strictEqual(queued.outcome, "accepted", key); + if (rekey) { + yield* TestClock.adjust("1 second"); + yield* service.upsert( + yield* weakened("task-org-a", { + signature: { ...agentSigned, secret: "owner-rekeyed-secret" }, + }), + ); + yield* store.bindSecret( + orgA, + source(orgA).id, + Redacted.make("owner-rekeyed-secret"), + ); + } + yield* TestClock.adjust("1 hour"); + const worker = workers.get("webhook-coalescing"); + if (worker === undefined) return assert.fail("No coalescing worker"); + yield* waitForDispatch(worker); + assert.strictEqual(yield* outcome(held, queued.deliveryId), "dispatch_failed", key); + } + // Kin's deterministic task id refuses to launch even before its source exists. + const { task: orphan } = yield* service.upsert( + yield* upsert(KinTriggerStore.githubAppTaskId(orgA)), + ); + yield* dispatchFailed(orphan, "orphan"); + const orphanInput = yield* upsert(KinTriggerStore.githubAppTaskId(orgA)); + const { task: boundOrphan } = yield* service.upsert({ + ...orphanInput, + threadId: projection(orgA).thread.id, + threadMode: "continue", + }); + yield* dispatchFailed(boundOrphan, "bound-orphan"); + assert.lengthOf(yield* inbox.listPending(orgA), 1); + assert.lengthOf(launches, 0); + yield* service.delete({ id: orphan.id }); + const ordinary = yield* upsert("ordinary-bound"); + const { task: other } = yield* service.upsert({ + ...ordinary, + threadId: projection(orgA).thread.id, + threadMode: "continue", + }); + yield* waitForDispatch(service.triggerWebhook(request(other, "ordinary-bound"))); + assert.lengthOf(sends, 1); + assert.strictEqual(sends[0]?.scheduledTaskId, other.id); + const sql = yield* SqlClient.SqlClient; + yield* sql`DROP TABLE kin_webhook_sources`; + const lookupFailure = yield* waitForDispatch( + service.triggerWebhook(request(other, "lookup-failure")), + ); + assert.strictEqual(lookupFailure._tag, "accepted"); + if (lookupFailure._tag !== "accepted") return; + const failed = yield* service.getWebhookDelivery({ + id: other.id, + deliveryId: lookupFailure.deliveryId, + acceptAnyOutcome: true, + }); + assert.strictEqual(failed.delivery.outcome, "dispatch_failed"); + assert.lengthOf(yield* inbox.listPending(orgA), 1); + assert.lengthOf(sends, 1); + assert.lengthOf(launches, 0); + }), + workers, ); }).pipe(Effect.provide(services.pipe(Layer.provideMerge(database)))), ); @@ -558,6 +652,21 @@ describe("KinTriggerSink", () => { assert.deepStrictEqual(yield* inbox.listPending(orgA), []); yield* store.unregisterSource(orgB, a.id); assert.deepStrictEqual(yield* store.sourceByTaskId(a.scheduledTaskId), a); + const active = yield* Effect.flip(store.unregisterSource(orgA, a.id)); + assert.strictEqual( + active._tag === "KinWebhookSourceConflict" && active.reason, + "task_active", + ); + yield* service.setEnabled({ id: a.scheduledTaskId, enabled: false }); + yield* sql`INSERT INTO scheduled_task_webhook_pending + (delivery_id, task_id, coalesce_key, due_at, state, task_json, request_json, delivery_ids_json) + VALUES ('in-flight', ${a.scheduledTaskId}, '', 0, 'running', '{}', '{}', '[]')`; + const running = yield* Effect.flip(store.unregisterSource(orgA, a.id)); + assert.strictEqual( + running._tag === "KinWebhookSourceConflict" && running.reason, + "task_active", + ); + yield* sql`DELETE FROM scheduled_task_webhook_pending WHERE delivery_id = 'in-flight'`; yield* store.unregisterSource(orgA, a.id); assert.isUndefined(yield* store.sourceByTaskId(a.scheduledTaskId)); }), diff --git a/apps/server/src/kin/triggers/KinTriggerSink.ts b/apps/server/src/kin/triggers/KinTriggerSink.ts index 29a8f99a2b32b..d605a60a16f20 100644 --- a/apps/server/src/kin/triggers/KinTriggerSink.ts +++ b/apps/server/src/kin/triggers/KinTriggerSink.ts @@ -10,6 +10,7 @@ import * as Context from "effect/Context"; import * as DateTime from "effect/DateTime"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; +import * as Redacted from "effect/Redacted"; import * as Schema from "effect/Schema"; import * as SqlClient from "effect/sql/SqlClient"; @@ -26,17 +27,41 @@ const decodeDelivery = Schema.decodeUnknownEffect(Delivery); const decodeFire = Schema.decodeEffect(KinTriggerFired); const decodeDeliveryId = Schema.decodeEffect(ScheduledTaskWebhookDeliveryId); +export class KinTriggerSinkRefused extends Schema.TaggedError()( + "KinTriggerSinkRefused", + { scheduledTaskId: Schema.String, reason: Schema.Literal("no_source") }, +) {} + // Shared with the upstream contract test so the tripwire pins exactly what the sink reads. +// Any agent can re-save a task, so its shape is rechecked per delivery and the secret is +// returned for the caller to compare with the hash Kin bound; never log or decode it. export const findLaunchDelivery = ( sql: SqlClient.SqlClient, - input: { readonly taskId: string; readonly deliveryId: string; readonly projectId: string }, -) => sql<{ readonly receivedAt: unknown }>` - SELECT d.received_at AS "receivedAt" FROM scheduled_task_webhook_deliveries d + input: { + readonly taskId: string; + readonly deliveryId: string; + readonly projectId: string; + readonly botLogin: string; + }, +) => sql<{ readonly receivedAt: unknown; readonly secret: string }>` + SELECT d.received_at AS "receivedAt", t.webhook_secret AS secret + FROM scheduled_task_webhook_deliveries d JOIN scheduled_tasks t ON t.task_id = d.task_id WHERE d.task_id = ${input.taskId} AND d.delivery_id = ${input.deliveryId} + AND d.signature_verified = 1 AND d.outcome = 'accepted' AND t.project_id = ${input.projectId} AND t.thread_id IS NULL AND t.thread_mode = 'new_thread' AND json_extract(t.schedule_json, '$.type') = 'webhook' + AND json_extract(t.schedule_json, '$.signature') IS NOT NULL AND t.webhook_secret IS NOT NULL + AND json_extract(t.schedule_json, '$.dedupe') = ${JSON.stringify(KinTriggerStore.GITHUB_DEDUPE)} + AND EXISTS (SELECT 1 FROM json_each(t.schedule_json, '$.filters') f + WHERE json_extract(f.value, '$.path') = 'headers.x-github-event' + AND json_extract(f.value, '$.op') = 'in' + AND json_extract(f.value, '$.value') = ${JSON.stringify(KinTriggerStore.GITHUB_EVENTS)}) + AND EXISTS (SELECT 1 FROM json_each(t.schedule_json, '$.filters') f + WHERE json_extract(f.value, '$.path') = 'body.sender.login' + AND json_extract(f.value, '$.op') = 'notEquals' + AND json_extract(f.value, '$.value') = ${input.botLogin}) `; export class KinTriggerSink extends Context.Service< @@ -63,7 +88,15 @@ const make = Effect.gen(function* () { const taskId = input.initialMessage?.scheduledTaskId; if (taskId === undefined) return; const source = yield* store.sourceByTaskId(taskId); - if (source === undefined) return; + if (source === undefined) { + // Kin's task id is deterministic, so a delivery racing remove still refuses to launch. + if (taskId.startsWith(KinTriggerStore.KIN_WEBHOOK_TASK_PREFIX)) + return yield* new KinTriggerSinkRefused({ + scheduledTaskId: taskId, + reason: "no_source", + }); + return; + } const prefix = `scheduled-task:${taskId}:webhook:`; if (!input.commandId.startsWith(prefix) || source.status !== "active") return yield* new KinTriggerInbox.KinTriggerInboxConflict({ @@ -75,17 +108,28 @@ const make = Effect.gen(function* () { taskId, deliveryId, projectId: input.projectId, + botLogin: source.botLogin, }).pipe(Effect.mapError(toPersistenceSqlError("KinTriggerSink.receive"))); if (row === undefined) return yield* new KinTriggerInbox.KinTriggerInboxConflict({ orgId: source.orgId, reason: "unknown_source", }); - const delivery = yield* decodeDelivery(row).pipe( + const delivery = yield* decodeDelivery({ receivedAt: row.receivedAt }).pipe( Effect.mapError((cause) => PersistenceDecodeError.fromSchemaError("KinTriggerSink.receive", cause), ), ); + if ( + !(yield* store.acceptsDelivery(source.orgId, source.id, { + secret: Redacted.make(row.secret), + receivedAt: DateTime.toEpochMillis(delivery.receivedAt), + })) + ) + return yield* new KinTriggerInbox.KinTriggerInboxConflict({ + orgId: source.orgId, + reason: "unknown_source", + }); const projection = yield* threads.getThreadProjection(source.inboxThreadId); if (projection.thread.projectId !== input.projectId || projection.thread.deletedAt !== null) return yield* new KinTriggerInbox.KinTriggerInboxConflict({ @@ -167,9 +211,13 @@ export function wrapTaskDispatch( ), ), ); - if (source === undefined) return yield* service.sendToThread(input); + if ( + source === undefined && + !input.scheduledTaskId.startsWith(KinTriggerStore.KIN_WEBHOOK_TASK_PREFIX) + ) + return yield* service.sendToThread(input); yield* Effect.logWarning("Kin trigger sources must stay unbound new_thread tasks").pipe( - Effect.annotateLogs({ scheduledTaskId: input.scheduledTaskId, sourceId: source.id }), + Effect.annotateLogs({ scheduledTaskId: input.scheduledTaskId }), ); return yield* new OrchestratorCommandRejectedError({ commandId: input.commandId, diff --git a/apps/server/src/kin/triggers/KinTriggerStore.ts b/apps/server/src/kin/triggers/KinTriggerStore.ts index 73ace290b3d34..8cde3c021b3b2 100644 --- a/apps/server/src/kin/triggers/KinTriggerStore.ts +++ b/apps/server/src/kin/triggers/KinTriggerStore.ts @@ -1,5 +1,10 @@ +// @effect-diagnostics-next-line nodeBuiltinImport:off -- Effect's Digest is effectful; the sink compares in one synchronous step. +import * as NodeCrypto from "node:crypto"; + import { CanonicalUtcTimestamp, + CommandId, + KIN_GITHUB_APP_SOURCE, canonicalTimeZone, NonNegativeInt, KinOrgId, @@ -13,10 +18,12 @@ import { KinTriggerSubject, } from "@t3tools/contracts"; import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient"; +import * as Clock from "effect/Clock"; import * as Context from "effect/Context"; import * as DateTime from "effect/DateTime"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; +import * as Redacted from "effect/Redacted"; import * as Result from "effect/Result"; import * as Schema from "effect/Schema"; import * as SqlClient from "effect/sql/SqlClient"; @@ -46,7 +53,15 @@ export class KinTriggerFireConflict extends Schema.TaggedError()( "KinWebhookSourceConflict", - { orgId: KinOrgId, reason: Schema.Literals(["unknown_source", "identity_conflict"]) }, + { + orgId: KinOrgId, + reason: Schema.Literals([ + "unknown_source", + "identity_conflict", + "task_active", + "app_source_taken", + ]), + }, ) {} const sourceColumns = `source_id AS id, org_id AS "orgId", repo, @@ -77,16 +92,71 @@ const decodeInboxRow = Schema.decodeUnknownEffect( ); const Scope = Schema.Struct({ orgId: KinOrgId, id: KinTriggerId }); +export const githubAppCommandId = (orgId: KinOrgId) => + CommandId.make(`kin:webhook:${orgId}:${KIN_GITHUB_APP_SOURCE}`); +// Upstream names a task created by commandId `scheduled-task:${commandId}`. +export const githubAppTaskId = (orgId: KinOrgId) => + ScheduledTaskId.make(`scheduled-task:${githubAppCommandId(orgId)}`); +export const KIN_WEBHOOK_TASK_PREFIX = "scheduled-task:kin:webhook:"; +export const GITHUB_REPLAY_KEY = "{{headers.x-github-delivery}}"; +// Exactly this, no coalesce window: a held delivery would outlive a re-key. +export const GITHUB_DEDUPE = { replayKey: GITHUB_REPLAY_KEY }; +export const GITHUB_EVENTS = [ + "pull_request", + "pull_request_review", + "pull_request_review_comment", + "issue_comment", + "issues", + "check_suite", + "workflow_run", + "push", +]; + +const secretDigest = (secret: Redacted.Redacted) => + NodeCrypto.createHash("sha256").update(Redacted.value(secret)).digest(); + +// Cross-org on purpose: one GitHub App has one hook config, so its source is host-wide. +export const appSourceHeldElsewhere = (sql: SqlClient.SqlClient, orgId: KinOrgId) => + sql`SELECT 1 FROM kin_webhook_sources WHERE repo = ${KIN_GITHUB_APP_SOURCE} + AND org_id <> ${orgId} LIMIT 1`.pipe(Effect.map((rows) => rows.length > 0)); + +// Shared with the upstream contract test: a direct delivery marks its task running and a +// queued one holds a pending row until its launch returns. +export const inFlightDeliveries = (sql: SqlClient.SqlClient, taskId: string) => + sql`SELECT 1 FROM scheduled_tasks WHERE task_id = ${taskId} AND last_run_status = 'running' + UNION ALL SELECT 1 FROM scheduled_task_webhook_pending + WHERE task_id = ${taskId} AND state IN ('dispatching', 'running')`; + export class KinTriggerStore extends Context.Service< KinTriggerStore, { readonly registerSource: ( source: KinWebhookSource, ) => Effect.Effect; + /** Refuses while the upstream task is enabled or a delivery is dispatching, so none falls through to a real launch. */ readonly unregisterSource: ( orgId: KinOrgId, id: KinWebhookSourceId, + ) => Effect.Effect; + /** Binds the source to the secret Kin just saved upstream and to the time of this bind. */ + readonly bindSecret: ( + orgId: KinOrgId, + id: KinWebhookSourceId, + secret: Redacted.Redacted, ) => Effect.Effect; + /** + * True only for the bound secret and a delivery received after the bind, so one verified + * against an earlier secret refuses. False when nothing is bound, as before K019. + */ + readonly acceptsDelivery: ( + orgId: KinOrgId, + id: KinWebhookSourceId, + delivery: { readonly secret: Redacted.Redacted; readonly receivedAt: number }, + ) => Effect.Effect; + readonly getSource: ( + orgId: KinOrgId, + id: KinWebhookSourceId, + ) => Effect.Effect; readonly sourceByTaskId: ( id: ScheduledTaskId, ) => Effect.Effect; @@ -179,16 +249,23 @@ const make = Effect.gen(function* () { registerSource: Effect.fn("KinTriggerStore.registerSource")( function* (source) { const value = yield* decodeSource(source); + const projection = yield* threads + .getThreadProjection(value.inboxThreadId) + .pipe( + Effect.mapError( + () => new KinWebhookSourceConflict({ orgId: value.orgId, reason: "unknown_source" }), + ), + ); return yield* sql.withTransaction( Effect.gen(function* () { - const projection = yield* threads - .getThreadProjection(value.inboxThreadId) - .pipe( - Effect.mapError( - () => - new KinWebhookSourceConflict({ orgId: value.orgId, reason: "unknown_source" }), - ), - ); + if ( + value.repo === KIN_GITHUB_APP_SOURCE && + (yield* appSourceHeldElsewhere(sql, value.orgId)) + ) + return yield* new KinWebhookSourceConflict({ + orgId: value.orgId, + reason: "app_source_taken", + }); const owned = yield* sql` SELECT t.task_id FROM scheduled_tasks t WHERE t.task_id = ${value.scheduledTaskId} @@ -251,9 +328,62 @@ const make = Effect.gen(function* () { ), unregisterSource: Effect.fn("KinTriggerStore.unregisterSource")( function* (orgId, id) { - yield* sql`DELETE FROM kin_webhook_sources WHERE org_id = ${orgId} AND source_id = ${id}`; + return yield* sql.withTransaction( + Effect.gen(function* () { + const [source] = yield* sql<{ + readonly taskId: string; + readonly enabled: number | null; + }>` + SELECT s.scheduled_task_id AS "taskId", t.enabled FROM kin_webhook_sources s + LEFT JOIN scheduled_tasks t ON t.task_id = s.scheduled_task_id + WHERE s.org_id = ${orgId} AND s.source_id = ${id} + `; + if (source === undefined) return; + if (source.enabled === 1 || (yield* inFlightDeliveries(sql, source.taskId)).length > 0) + return yield* new KinWebhookSourceConflict({ orgId, reason: "task_active" }); + yield* sql`DELETE FROM kin_webhook_sources WHERE org_id = ${orgId} AND source_id = ${id}`; + }), + ); + }, + Effect.mapError((cause) => + isSourceConflict(cause) + ? cause + : toPersistenceOrDecodeError("KinTriggerStore.unregisterSource")(cause), + ), + ), + bindSecret: Effect.fn("KinTriggerStore.bindSecret")( + function* (orgId, id, secret) { + const now = yield* Clock.currentTimeMillis; + yield* sql`UPDATE kin_webhook_sources + SET secret_sha256 = ${secretDigest(secret).toString("hex")}, secret_bound_at = ${now} + WHERE org_id = ${orgId} AND source_id = ${id}`; + }, + Effect.mapError(toPersistenceSqlError("KinTriggerStore.bindSecret")), + ), + acceptsDelivery: Effect.fn("KinTriggerStore.acceptsDelivery")( + function* (orgId, id, delivery) { + const [row] = yield* sql<{ + readonly hash: string | null; + readonly boundAt: number | null; + }>` + SELECT secret_sha256 AS hash, secret_bound_at AS "boundAt" FROM kin_webhook_sources + WHERE org_id = ${orgId} AND source_id = ${id}`.pipe(NodeSqliteClient.readOnly); + // Same millisecond refuses too: it may have been verified just before the save. + if (row?.hash == null || row.boundAt == null || delivery.receivedAt <= row.boundAt) + return false; + const bound = Buffer.from(row.hash, "hex"); + const given = secretDigest(delivery.secret); + return bound.length === given.length && NodeCrypto.timingSafeEqual(bound, given); + }, + Effect.mapError(toPersistenceSqlError("KinTriggerStore.acceptsDelivery")), + ), + getSource: Effect.fn("KinTriggerStore.getSource")( + function* (orgId, id) { + const [row] = yield* sql`SELECT ${sql.literal(sourceColumns)} FROM kin_webhook_sources + WHERE org_id = ${orgId} AND source_id = ${id}`.pipe(NodeSqliteClient.readOnly); + return row === undefined ? undefined : yield* decodeSource(row); }, - Effect.mapError(toPersistenceOrDecodeError("KinTriggerStore.unregisterSource")), + Effect.mapError(toPersistenceOrDecodeError("KinTriggerStore.getSource")), ), sourceByTaskId: Effect.fn("KinTriggerStore.sourceByTaskId")( function* (id) { diff --git a/apps/server/src/kin/triggers/githubWebhooks.test.ts b/apps/server/src/kin/triggers/githubWebhooks.test.ts new file mode 100644 index 0000000000000..ed178de6a969c --- /dev/null +++ b/apps/server/src/kin/triggers/githubWebhooks.test.ts @@ -0,0 +1,780 @@ +// @effect-diagnostics-next-line nodeBuiltinImport:off -- Effect's Crypto has no createHmac. +import * as NodeCrypto from "node:crypto"; + +import * as Crypto from "@effect/platform-node/NodeCrypto"; +import { assert, describe, it } from "@effect/vitest"; +import { + AuthOrchestrationReadScope, + AuthSessionId, + AuthSettingsWriteScope, + KIN_GITHUB_APP_SOURCE, + KinGithubWebhookRpcs, + KinOrgId, + KinMessage, + KinPersonId, + KinWebhookSourceId, + type OrchestrationV2ServerCommand, + Project, + ProjectId, + RpcScopeAuthorization, + WS_METHODS, + WsRpcGroup, +} from "@t3tools/contracts"; +import * as ConfigProvider from "effect/ConfigProvider"; +import * as Effect from "effect/Effect"; +import * as Fiber from "effect/Fiber"; +import * as Layer from "effect/Layer"; +import * as Logger from "effect/Logger"; +import * as Option from "effect/Option"; +import * as Redacted from "effect/Redacted"; +import * as References from "effect/References"; +import * as RpcGroup from "effect/rpc/RpcGroup"; +import * as RpcTest from "effect/rpc/RpcTest"; +import * as Schema from "effect/Schema"; +import * as SqlClient from "effect/sql/SqlClient"; +import * as TestClock from "effect/testing/TestClock"; + +import * as RpcAuthorization from "../../auth/RpcAuthorization.ts"; +import { OrchestratorCommandRejectedError } from "../../orchestration-v2/Orchestrator.ts"; +import * as ThreadLaunchService from "../../orchestration-v2/ThreadLaunchService.ts"; +import * as ThreadManagementService from "../../orchestration-v2/ThreadManagementService.ts"; +import * as Sqlite from "../../persistence/Sqlite.ts"; +import * as ManagedPolicy from "../../policy/ManagedPolicy.ts"; +import * as ManagedProjectFolders from "../../project/ManagedProjectFolders.ts"; +import * as ProjectService from "../../project/ProjectService.ts"; +import * as ScheduledTaskService from "../../scheduledTasks/ScheduledTaskService.ts"; +import * as Scheduler from "../../scheduling/Scheduler.ts"; +import * as SecretRequests from "../../secrets/SecretRequests.ts"; +import { KinChatService } from "../chat/KinChatService.ts"; +import * as KinPersistence from "../persistence/KinPersistence.ts"; +import * as GithubWebhooks from "./githubWebhooks.ts"; +import * as KinTriggerInbox from "./KinTriggerInbox.ts"; +import * as KinTriggerSink from "./KinTriggerSink.ts"; +import * as KinTriggerStore from "./KinTriggerStore.ts"; +import { makeKinWebhookHandlers } from "./webhookRpcHandlers.ts"; +import { projection, timestamp, waitForDispatch } from "./webhooks.testkit.ts"; + +const orgId = KinOrgId.make("org-a"); +const owner = [{ _tag: "person", orgId, personId: KinPersonId.make("owner") }] as const; +const member = [{ _tag: "person", orgId, personId: KinPersonId.make("member") }] as const; +const relay = "https://relay.test/v1/hooks/env"; +const project = Schema.decodeSync(Project)({ + id: `project-${orgId}`, + title: "Kin", + workspaceRoot: "/projects/kin", + defaultModelSelection: { instanceId: "codex", model: "gpt-5.4" }, + scripts: [], + createdAt: timestamp, + updatedAt: timestamp, + deletedAt: null, +}); + +const decodeMessage = Schema.decodeSync(KinMessage); +const noticeMessage = (input: Parameters[0]) => + decodeMessage({ + orgId: input.orgId, + chatId: input.chatId, + messageId: input.messageId, + seq: 1, + author: { _tag: "system" }, + authorRole: "owner", + kind: "system", + body: input.body, + intent: null, + mentions: [], + replyTo: null, + widgetId: null, + floorRunId: null, + status: "posted", + sentBy: null, + createdAt: timestamp, + editedAt: null, + }); + +const database = KinPersistence.layer.pipe( + Layer.provideMerge(Sqlite.layerMemory), + Layer.provideMerge( + ConfigProvider.layer( + ConfigProvider.fromUnknown({ KIN_ENABLED: true, KIN_GITHUB_BOT_LOGIN: "kin-app[bot]" }), + ), + ), +); +const seed = Effect.gen(function* () { + yield* TestClock.setTime(1794009600000); + const sql = yield* SqlClient.SqlClient; + yield* sql`INSERT INTO kin_orgs VALUES (${orgId}, 'Kin', ${timestamp})`; + for (const person of ["owner", "member"]) + yield* sql`INSERT INTO kin_people (person_id, org_id, handle, display_name, created_at) + VALUES (${person}, ${orgId}, ${person}, ${person}, ${timestamp})`; + yield* sql`INSERT INTO kin_agents (agent_id, org_id, display_name, role, owner_person_id, created_at) + VALUES ('lead', ${orgId}, 'Lead', 'lead', 'owner', ${timestamp})`; + yield* sql`INSERT INTO kin_chats (chat_id, org_id, kind, direct_key, last_seq, created_at) + VALUES ('owner-chat', ${orgId}, 'direct', 'direct', 0, ${timestamp})`; + yield* sql`INSERT INTO kin_leads (agent_id, org_id, name, level_since, models_json, worker_projects_json, owner_chat_id) + VALUES ('lead', ${orgId}, 'Lead', 0, '{}', '[]', 'owner-chat')`; +}); + +const signed = ( + task: { readonly id: string; readonly webhook?: { readonly path: string } | undefined }, + input: { readonly delivery: string; readonly secret: string; readonly sender?: string }, +) => { + const bodyText = JSON.stringify({ action: "opened", sender: { login: input.sender ?? "dev" } }); + return { + hookId: task.id, + token: task.webhook?.path.split("/").at(-1) ?? "", + method: "POST", + path: "/api/hooks", + query: "", + bodyText, + body: new TextEncoder().encode(bodyText), + headers: { + "content-type": "application/json", + "x-github-event": "pull_request", + "x-github-delivery": input.delivery, + "x-hub-signature-256": `sha256=${NodeCrypto.createHmac("sha256", input.secret).update(bodyText).digest("hex")}`, + }, + }; +}; + +const harness = ( + options: { readonly relayHookBaseUrl?: string | null; readonly gate?: boolean }, + body: (input: { + readonly webhooks: GithubWebhooks.KinGithubWebhooks["Service"]; + readonly tasks: ScheduledTaskService.ScheduledTaskService["Service"]; + readonly dispatched: ThreadLaunchService.ThreadLaunchInput[]; + readonly inboxLaunches: ThreadLaunchService.ThreadLaunchInput[]; + readonly commands: OrchestrationV2ServerCommand[]; + readonly notices: Parameters[0][]; + readonly hookConfigs: Parameters< + GithubWebhooks.GithubAppHookClient["Service"]["patchHookConfig"] + >[0][]; + }) => Effect.Effect, +) => + Effect.gen(function* () { + const dispatched: ThreadLaunchService.ThreadLaunchInput[] = []; + const inboxLaunches: ThreadLaunchService.ThreadLaunchInput[] = []; + const commands: OrchestrationV2ServerCommand[] = []; + const notices: Parameters[0][] = []; + const hookConfigs: Parameters< + GithubWebhooks.GithubAppHookClient["Service"]["patchHookConfig"] + >[0][] = []; + const archived = () => commands.some((command) => command.type === "thread.archive"); + const threads = Layer.mock(ThreadManagementService.ThreadManagementService)({ + getThreadProjection: () => Effect.succeed(projection(orgId, archived() ? timestamp : null)), + dispatch: (command) => + Effect.sync(() => { + commands.push(command); + return { sequence: commands.length, storedEvents: [] }; + }), + sendToThread: (input) => + Effect.fail( + new OrchestratorCommandRejectedError({ + commandId: input.commandId, + commandType: "test.inner_send", + }), + ), + }); + const tasksLayer = ScheduledTaskService.layer.pipe( + Layer.provide( + Layer.mergeAll( + KinTriggerSink.wrapTaskDispatch( + Layer.mock(ThreadLaunchService.ThreadLaunchService)({ + launch: (input) => + Effect.sync(() => { + dispatched.push(input); + return { + threadId: projection(orgId).thread.id, + projection: projection(orgId), + resumed: false, + }; + }), + }), + threads, + ), + Crypto.layer, + Scheduler.layer, + ManagedPolicy.layerTest(), + Layer.mock(SecretRequests.SecretRequests)({}), + Layer.mock(ManagedProjectFolders.ManagedProjectFolders)({ + namedProjectsRoot: "/projects", + isScratchProject: () => Effect.succeed(false), + }), + Layer.succeed( + ScheduledTaskService.ScheduledTaskWebhookOrigin, + Effect.succeed({ + relayHookBaseUrl: + options.relayHookBaseUrl === undefined ? relay : options.relayHookBaseUrl, + }), + ), + ), + ), + ); + const webhooksLayer = GithubWebhooks.layer.pipe( + Layer.provide( + Layer.mergeAll( + threads, + Layer.mock(ThreadLaunchService.ThreadLaunchService)({ + launch: (input) => + Effect.sync(() => { + inboxLaunches.push(input); + const view = projection(orgId, archived() ? timestamp : null); + return { threadId: view.thread.id, projection: view, resumed: false }; + }), + }), + Layer.mock(ProjectService.ProjectService)({ + getById: (id) => + Effect.succeed(id === project.id ? Option.some(project) : Option.none()), + }), + Layer.mock(KinChatService)({ + notice: (input) => + Effect.sync(() => { + notices.push(input); + return { message: noticeMessage(input), waiting: null }; + }), + }), + Crypto.layer, + GithubWebhooks.lockLayer, + ), + ), + ); + return yield* Effect.gen(function* () { + return yield* body({ + webhooks: yield* GithubWebhooks.KinGithubWebhooks, + tasks: yield* ScheduledTaskService.ScheduledTaskService, + dispatched, + inboxLaunches, + commands, + notices, + hookConfigs, + }); + }).pipe( + Effect.provide(webhooksLayer.pipe(Layer.provideMerge(tasksLayer))), + Effect.provideService(GithubWebhooks.AppHookConfigureGate, options.gate ?? false), + Effect.provideService(GithubWebhooks.GithubAppHookClient, { + patchHookConfig: (config) => Effect.sync(() => void hookConfigs.push(config)), + }), + ); + }); + +const services = Layer.mergeAll(KinTriggerStore.layer, KinTriggerInbox.layer).pipe( + Layer.provide( + Layer.mock(ThreadManagementService.ThreadManagementService)({ + getThreadProjection: () => Effect.succeed(projection(orgId)), + }), + ), + Layer.provideMerge(database), +); +const ownerSecret = Redacted.make("owner-secret-from-github-app"); + +describe("KinGithubWebhooks", () => { + it.effect( + "creates the App source unbound and signed, then routes signed deliveries to the inbox only", + () => + Effect.gen(function* () { + yield* seed; + const inbox = yield* KinTriggerInbox.KinTriggerInbox; + const sql = yield* SqlClient.SqlClient; + yield* harness( + {}, + ({ webhooks, tasks, dispatched, inboxLaunches, commands, hookConfigs }) => + Effect.gen(function* () { + assert.strictEqual( + (yield* Effect.flip(webhooks.setup(member, { orgId, projectId: project.id }))) + .reason, + "not_allowed", + ); + assert.strictEqual( + (yield* Effect.flip( + webhooks.setup(owner, { orgId: KinOrgId.make("other"), projectId: project.id }), + )).reason, + "not_found", + ); + const unbotted = yield* webhooks + .setup(owner, { orgId, projectId: project.id }) + .pipe( + Effect.flip, + Effect.provide( + ConfigProvider.layer( + ConfigProvider.fromUnknown({ KIN_GITHUB_BOT_LOGIN: "kin-app" }), + ), + ), + ); + assert.include(unbotted, { reason: "invalid_state" }); + assert.include(unbotted.message, "[bot]"); + assert.deepStrictEqual((yield* tasks.list()).tasks, []); + const setup = yield* webhooks.setup(owner, { orgId, projectId: project.id }); + assert.strictEqual(setup.status, "needs_secret"); + const [task] = (yield* tasks.list()).tasks; + assert.isDefined(task); + if (task === undefined || setup.status === "needs_connect") return; + assert.strictEqual(setup.url, task.webhook?.url); + assert.isTrue(setup.url.startsWith(`${relay}/`)); + assert.deepInclude(task, { + prompt: GithubWebhooks.KIN_SINK_PROMPT, + enabled: true, + projectId: project.id, + threadId: null, + threadMode: "new_thread", + runtimeMode: "approval-required", + interactionMode: "plan", + createdBy: "system", + creationSource: "server", + }); + assert.deepStrictEqual(task.schedule, { + type: "webhook", + signature: { header: "x-hub-signature-256", encoding: "hex", prefix: "sha256=" }, + maxDeliveryAgeMinutes: null, + dedupe: { replayKey: KinTriggerStore.GITHUB_REPLAY_KEY }, + filters: [ + { + path: "headers.x-github-event", + op: "in", + value: KinTriggerStore.GITHUB_EVENTS, + }, + { path: "body.sender.login", op: "notEquals", value: "kin-app[bot]" }, + ], + }); + assert.isTrue(task.webhook?.hasSecret); + assert.lengthOf(inboxLaunches, 1); + assert.isUndefined(inboxLaunches[0]?.initialMessage); + assert.deepStrictEqual( + commands.map((command) => command.type), + ["thread.archive"], + ); + assert.lengthOf(yield* sql`SELECT 1 FROM kin_agent_threads`, 0); + const store = yield* KinTriggerStore.KinTriggerStore; + assert.deepInclude(yield* store.sourceByTaskId(task.id), { + orgId, + repo: KIN_GITHUB_APP_SOURCE, + inboxThreadId: projection(orgId).thread.id, + githubHookId: null, + botLogin: "kin-app[bot]", + }); + assert.lengthOf(hookConfigs, 0); + assert.strictEqual( + (yield* GithubWebhooks.configureAppHook({ + kind: "github.app_hook.configure", + orgId, + sourceId: setup.sourceId, + url: setup.url, + secret: ownerSecret, + }).pipe(Effect.flip)).reason, + "gated", + ); + const stalled = yield* GithubWebhooks.configureAppHook({ + kind: "github.app_hook.configure", + orgId, + sourceId: setup.sourceId, + url: setup.url, + }).pipe( + Effect.flip, + Effect.provideService(GithubWebhooks.AppHookConfigureGate, true), + Effect.provideService(GithubWebhooks.GithubAppHookClient, { + patchHookConfig: () => Effect.never, + }), + Effect.forkChild({ startImmediately: true }), + ); + yield* TestClock.adjust("30 seconds"); + assert.strictEqual((yield* Fiber.join(stalled)).reason, "timeout"); + + const rejected = yield* tasks.triggerWebhook( + signed(task, { delivery: "before-secret", secret: Redacted.value(ownerSecret) }), + ); + assert.strictEqual(rejected._tag, "rejected_signature"); + assert.strictEqual( + (yield* webhooks.setSecret(owner, { orgId, secret: ownerSecret })).status, + "configured", + ); + yield* TestClock.adjust("1 second"); + const delivery = { delivery: "gh-1", secret: Redacted.value(ownerSecret) }; + const accepted = yield* waitForDispatch(tasks.triggerWebhook(signed(task, delivery))); + assert.strictEqual(accepted._tag === "accepted" && accepted.outcome, "accepted"); + const pending = yield* inbox.listPending(orgId); + assert.lengthOf(pending, 1); + assert.deepInclude(pending[0]?.payload, { kind: "github", scheduledTaskId: task.id }); + const replay = yield* tasks.triggerWebhook(signed(task, delivery)); + assert.strictEqual(replay._tag === "accepted" && replay.outcome, "duplicate"); + const bot = yield* tasks.triggerWebhook( + signed(task, { ...delivery, delivery: "gh-2", sender: "kin-app[bot]" }), + ); + assert.strictEqual(bot._tag === "accepted" && bot.outcome, "filtered"); + assert.lengthOf(yield* inbox.listPending(orgId), 1); + assert.lengthOf(dispatched, 0); + + const rekey = Redacted.make("owner-rekeyed-secret-from-github-app"); + yield* webhooks.setSecret(owner, { orgId, secret: rekey }); + yield* TestClock.adjust("1 second"); + const stale = yield* tasks.triggerWebhook( + signed(task, { delivery: "gh-stale", secret: Redacted.value(ownerSecret) }), + ); + assert.strictEqual(stale._tag, "rejected_signature"); + const rekeyed = yield* waitForDispatch( + tasks.triggerWebhook( + signed(task, { delivery: "gh-rekeyed", secret: Redacted.value(rekey) }), + ), + ); + assert.strictEqual(rekeyed._tag === "accepted" && rekeyed.outcome, "accepted"); + assert.lengthOf(yield* inbox.listPending(orgId), 2); + + const again = yield* webhooks.setup(owner, { orgId, projectId: project.id }); + assert.deepStrictEqual(again, setup); + assert.lengthOf((yield* tasks.list()).tasks, 1); + const still = yield* waitForDispatch( + tasks.triggerWebhook( + signed(task, { delivery: "gh-3", secret: Redacted.value(rekey) }), + ), + ); + assert.strictEqual(still._tag === "accepted" && still.outcome, "accepted"); + assert.lengthOf(yield* inbox.listPending(orgId), 3); + + // A stored shape from an older event list fails closed; a gated setup restores it + // and keeps the stored secret, so the bound hash still matches. + const kinSchedule = (yield* tasks.list()).tasks[0]?.schedule; + const dispatchOutcome = (delivery: string, secret: string) => + waitForDispatch(tasks.triggerWebhook(signed(task, { delivery, secret }))).pipe( + Effect.flatMap((result) => + result._tag === "accepted" + ? tasks + .getWebhookDelivery({ + id: task.id, + deliveryId: result.deliveryId, + acceptAnyOutcome: true, + }) + .pipe(Effect.map(({ delivery }) => delivery.outcome)) + : Effect.succeed(result._tag), + ), + ); + yield* sql`UPDATE scheduled_tasks SET schedule_json = + json_set(schedule_json, '$.filters[0].value', json('["pull_request"]')) + WHERE task_id = ${task.id}`; + assert.strictEqual( + yield* dispatchOutcome("gh-old-shape", Redacted.value(rekey)), + "dispatch_failed", + ); + assert.deepStrictEqual( + yield* webhooks.setup(owner, { orgId, projectId: project.id }), + setup, + ); + assert.deepStrictEqual((yield* tasks.list()).tasks[0]?.schedule, kinSchedule); + assert.strictEqual( + yield* dispatchOutcome("gh-repaired", Redacted.value(rekey)), + "accepted", + ); + assert.lengthOf(yield* inbox.listPending(orgId), 4); + // The repair keeps whatever secret is stored, so an agent's swapped one still refuses. + yield* sql`UPDATE scheduled_tasks SET webhook_secret = 'agent-secret' + WHERE task_id = ${task.id}`; + yield* webhooks.setup(owner, { orgId, projectId: project.id }); + assert.strictEqual( + yield* dispatchOutcome("gh-agent", "agent-secret"), + "dispatch_failed", + ); + const owners = yield* tasks.triggerWebhook( + signed(task, { delivery: "gh-owner", secret: Redacted.value(rekey) }), + ); + assert.strictEqual(owners._tag, "rejected_signature"); + assert.lengthOf(yield* inbox.listPending(orgId), 4); + assert.strictEqual( + (yield* Effect.flip( + webhooks.setup(owner, { + orgId, + projectId: ProjectId.make("elsewhere"), + }), + )).reason, + "invalid_state", + ); + }), + ); + }).pipe(Effect.provide(services)), + ); + + it.effect( + "with the App hook gate open, gives GitHub the secret T3 checks and re-keys on setup", + () => + Effect.gen(function* () { + yield* seed; + const inbox = yield* KinTriggerInbox.KinTriggerInbox; + yield* harness({ gate: true }, ({ webhooks, tasks, dispatched, hookConfigs }) => + Effect.gen(function* () { + const setup = yield* webhooks.setup(owner, { orgId, projectId: project.id }); + yield* TestClock.adjust("1 second"); + const [task] = (yield* tasks.list()).tasks; + assert.isDefined(task); + if (task === undefined || setup.status === "needs_connect") return assert.fail("setup"); + assert.strictEqual(setup.status, "configured"); + const [first] = hookConfigs; + assert.deepInclude(first, { url: setup.url, content_type: "json", insecure_ssl: "0" }); + if (first?.secret === undefined) return assert.fail("no secret sent"); + assert.match(Redacted.value(first.secret), /^[0-9a-f]{64}$/); + const send = (delivery: string, secret: Redacted.Redacted) => + tasks.triggerWebhook(signed(task, { delivery, secret: Redacted.value(secret) })); + const accepted = yield* waitForDispatch(send("gh-1", first.secret)); + assert.strictEqual(accepted._tag === "accepted" && accepted.outcome, "accepted"); + + const rotated = yield* webhooks.rotate(owner, { orgId }); + if (rotated.status === "needs_connect") return assert.fail("rotate"); + assert.strictEqual(rotated.status, "configured"); + assert.notStrictEqual(rotated.url, setup.url); + assert.deepStrictEqual(hookConfigs[1], { + url: rotated.url, + content_type: "json", + insecure_ssl: "0", + }); + assert.strictEqual((yield* send("gh-2", first.secret))._tag, "not_found"); + + const rekeyed = yield* webhooks.setup(owner, { orgId, projectId: project.id }); + yield* TestClock.adjust("1 second"); + assert.deepStrictEqual(rekeyed, { + status: "configured", + sourceId: setup.sourceId, + url: rotated.url, + }); + const next = hookConfigs[2]?.secret; + if (next === undefined) return assert.fail("no re-key"); + assert.notStrictEqual(Redacted.value(next), Redacted.value(first.secret)); + const [current] = (yield* tasks.list()).tasks; + if (current === undefined) return assert.fail("task"); + const resend = (delivery: string, secret: Redacted.Redacted) => + tasks.triggerWebhook(signed(current, { delivery, secret: Redacted.value(secret) })); + assert.strictEqual((yield* resend("gh-3", first.secret))._tag, "rejected_signature"); + const fresh = yield* waitForDispatch(resend("gh-4", next)); + assert.strictEqual(fresh._tag === "accepted" && fresh.outcome, "accepted"); + assert.lengthOf(yield* inbox.listPending(orgId), 2); + assert.lengthOf(dispatched, 0); + }), + ); + }).pipe(Effect.provide(services)), + ); + + it.effect("posts the T3 Connect notice and leaves nothing behind without a public URL", () => + Effect.gen(function* () { + yield* seed; + const store = yield* KinTriggerStore.KinTriggerStore; + yield* harness({ relayHookBaseUrl: null }, ({ webhooks, tasks, inboxLaunches, notices }) => + Effect.gen(function* () { + assert.deepStrictEqual(yield* webhooks.setup(owner, { orgId, projectId: project.id }), { + status: "needs_connect", + }); + assert.deepStrictEqual( + notices.map(({ chatId, body }) => ({ chatId, body })), + [{ chatId: "owner-chat", body: GithubWebhooks.CONNECT_NOTICE }], + ); + assert.deepStrictEqual((yield* tasks.list()).tasks, []); + assert.lengthOf(inboxLaunches, 0); + assert.isUndefined( + yield* store.getSource(orgId, KinWebhookSourceId.make(KIN_GITHUB_APP_SOURCE)), + ); + }), + ); + }).pipe(Effect.provide(services)), + ); + + it.effect( + "registers before enabling, and removal disables before unregistering, so no delivery launches", + () => + Effect.gen(function* () { + yield* seed; + const sql = yield* SqlClient.SqlClient; + const inbox = yield* KinTriggerInbox.KinTriggerInbox; + const store = yield* KinTriggerStore.KinTriggerStore; + const sourceId = KinWebhookSourceId.make(KIN_GITHUB_APP_SOURCE); + yield* harness({}, ({ webhooks, tasks, dispatched }) => + Effect.gen(function* () { + const inboxId = projection(orgId).thread.id; + yield* sql`INSERT INTO kin_agent_threads (org_id, agent_id, thread_id, started_at) + VALUES (${orgId}, 'lead', ${inboxId}, ${timestamp})`; + const refused = yield* Effect.flip( + webhooks.setup(owner, { orgId, projectId: project.id }), + ); + assert.strictEqual(refused.reason, "invalid_state"); + assert.deepStrictEqual((yield* tasks.list()).tasks, []); + assert.isUndefined(yield* store.getSource(orgId, sourceId)); + + yield* sql`DELETE FROM kin_agent_threads WHERE thread_id = ${inboxId}`; + const setup = yield* webhooks.setup(owner, { orgId, projectId: project.id }); + assert.strictEqual(setup.status, "needs_secret"); + yield* webhooks.setSecret(owner, { orgId, secret: ownerSecret }); + const [task] = (yield* tasks.list()).tasks; + if (task === undefined) return assert.fail("task"); + assert.strictEqual(task.id, KinTriggerStore.githubAppTaskId(orgId)); + assert.isTrue(task.enabled); + + assert.strictEqual( + (yield* Effect.flip(store.unregisterSource(orgId, sourceId)))._tag, + "KinWebhookSourceConflict", + ); + yield* sql`INSERT INTO scheduled_task_webhook_pending + (delivery_id, task_id, coalesce_key, due_at, state, task_json, request_json, delivery_ids_json) + VALUES ('in-flight', ${task.id}, '', 0, 'running', '{}', '{}', '[]')`; + assert.strictEqual( + (yield* Effect.flip(webhooks.remove(owner, { orgId }))).reason, + "invalid_state", + ); + assert.isDefined(yield* store.getSource(orgId, sourceId)); + yield* sql`DELETE FROM scheduled_task_webhook_pending WHERE delivery_id = 'in-flight'`; + const late = signed(task, { delivery: "late", secret: Redacted.value(ownerSecret) }); + assert.strictEqual((yield* tasks.triggerWebhook(late))._tag, "disabled"); + + yield* webhooks.remove(owner, { orgId }); + yield* webhooks.remove(owner, { orgId }); + assert.isUndefined(yield* store.getSource(orgId, sourceId)); + assert.deepStrictEqual((yield* tasks.list()).tasks, []); + assert.strictEqual((yield* tasks.triggerWebhook(late))._tag, "not_found"); + assert.deepStrictEqual(yield* inbox.listPending(orgId), []); + assert.lengthOf(dispatched, 0); + + assert.strictEqual( + (yield* webhooks.setup(owner, { orgId, projectId: project.id })).status, + "needs_secret", + ); + const rotated = yield* webhooks.rotate(owner, { orgId }); + assert.strictEqual(rotated.status, "needs_url"); + }), + ); + }).pipe(Effect.provide(services)), + ); + + it.effect("keeps the App source to one org per host until that org removes it", () => + Effect.gen(function* () { + yield* seed; + const sql = yield* SqlClient.SqlClient; + const orgB = KinOrgId.make("org-b"); + const ownerB = [ + { _tag: "person", orgId: orgB, personId: KinPersonId.make("owner-b") }, + ] as const; + yield* sql`INSERT INTO kin_orgs VALUES (${orgB}, 'Kin B', ${timestamp})`; + yield* sql`INSERT INTO kin_people (person_id, org_id, handle, display_name, created_at) + VALUES ('owner-b', ${orgB}, 'owner-b', 'owner-b', ${timestamp})`; + yield* sql`INSERT INTO kin_agents (agent_id, org_id, display_name, role, owner_person_id, created_at) + VALUES ('lead-b', ${orgB}, 'Lead', 'lead', 'owner-b', ${timestamp})`; + yield* sql`INSERT INTO kin_chats (chat_id, org_id, kind, direct_key, last_seq, created_at) + VALUES ('owner-chat-b', ${orgB}, 'direct', 'direct-b', 0, ${timestamp})`; + yield* sql`INSERT INTO kin_leads (agent_id, org_id, name, level_since, models_json, worker_projects_json, owner_chat_id) + VALUES ('lead-b', ${orgB}, 'Lead', 0, '{}', '[]', 'owner-chat-b')`; + const store = yield* KinTriggerStore.KinTriggerStore; + yield* harness({}, ({ webhooks, tasks }) => + Effect.gen(function* () { + yield* webhooks.setup(owner, { orgId, projectId: project.id }); + const taken = yield* Effect.flip( + webhooks.setup(ownerB, { orgId: orgB, projectId: project.id }), + ); + assert.include(taken, { reason: "conflict" }); + assert.lengthOf((yield* tasks.list()).tasks, 1); + const [held] = (yield* tasks.list()).tasks; + if (held === undefined) return assert.fail("task"); + const direct = yield* Effect.flip( + store.registerSource({ + id: KinWebhookSourceId.make(KIN_GITHUB_APP_SOURCE), + orgId: orgB, + repo: KIN_GITHUB_APP_SOURCE, + scheduledTaskId: held.id, + inboxThreadId: projection(orgId).thread.id, + githubHookId: null, + botLogin: "kin-app[bot]", + status: "active", + }), + ); + assert.include(direct, { _tag: "KinWebhookSourceConflict", reason: "app_source_taken" }); + yield* webhooks.remove(owner, { orgId }); + assert.strictEqual( + (yield* webhooks.setup(ownerB, { orgId: orgB, projectId: project.id })).status, + "needs_secret", + ); + const [moved] = (yield* tasks.list()).tasks; + assert.strictEqual(moved?.id, KinTriggerStore.githubAppTaskId(orgB)); + }), + ); + }).pipe(Effect.provide(services)), + ); + + it.effect("never writes a webhook secret to logs or errors, even when a save fails", () => + Effect.gen(function* () { + yield* seed; + const sql = yield* SqlClient.SqlClient; + const lines: string[] = []; + const logger = Logger.map(Logger.formatJson, (line) => void lines.push(line)); + const failing = Redacted.make("forced-failure-secret-0123456789"); + const { generated, errors } = yield* harness({}, ({ webhooks }) => + Effect.gen(function* () { + yield* webhooks.setup(owner, { orgId, projectId: project.id }); + const [stored] = yield* sql<{ readonly secret: string }>` + SELECT webhook_secret AS secret FROM scheduled_tasks`; + yield* webhooks.setSecret(owner, { orgId, secret: ownerSecret }); + yield* sql`CREATE TRIGGER reject_task_save BEFORE UPDATE ON scheduled_tasks + BEGIN SELECT RAISE(ABORT, 'forced'); END`; + const failed = yield* Effect.flip(webhooks.setSecret(owner, { orgId, secret: failing })); + yield* sql`DROP TRIGGER reject_task_save`; + return { + generated: stored === undefined ? [] : [Redacted.make(stored.secret)], + errors: [failed], + }; + }), + ).pipe( + Effect.provide(Logger.layer([logger], { mergeWithExisting: false })), + Effect.provideService(References.MinimumLogLevel, "All"), + ); + assert.lengthOf(generated, 1); + for (const error of errors) assert.include(error, { _tag: "KinChatError" }); + const output = [ + ...lines, + ...errors.flatMap((error) => [String(error), JSON.stringify(error), error.message]), + ].join("\n"); + assert.isNotEmpty(lines); + for (const secret of [...generated, ownerSecret, failing]) + assert.notInclude(output, Redacted.value(secret)); + }).pipe(Effect.provide(services)), + ); + + it.effect("refuses every setup RPC without the settings grant before the handler runs", () => + Effect.gen(function* () { + const probe = WsRpcGroup.omit( + ...[...WsRpcGroup.requests.keys()].filter( + ( + tag, + ): tag is Exclude< + RpcGroup.Rpcs["_tag"], + typeof WS_METHODS.serverProbe + > => tag !== WS_METHODS.serverProbe, + ), + ); + const group = RpcGroup.make(...KinGithubWebhookRpcs, ...probe.requests.values()).middleware( + RpcScopeAuthorization, + ); + const handlers = yield* makeKinWebhookHandlers({ + sessionId: AuthSessionId.make("session"), + subject: "browser", + method: "browser-session-cookie", + scopes: [AuthSettingsWriteScope], + }); + const client = (scope: typeof AuthSettingsWriteScope | typeof AuthOrchestrationReadScope) => + RpcTest.makeClient(group).pipe( + Effect.provide( + Layer.merge( + group.toLayer({ ...handlers, "server.probe": () => Effect.succeed({}) }), + RpcAuthorization.layer([scope]), + ), + ), + ); + const readOnly = yield* client(AuthOrchestrationReadScope); + const settings = yield* client(AuthSettingsWriteScope); + const calls = [ + (rpc: typeof readOnly) => + rpc["kin.webhooks.github.setup"]({ orgId, projectId: project.id }), + (rpc: typeof readOnly) => + rpc["kin.webhooks.github.setSecret"]({ orgId, secret: ownerSecret }), + (rpc: typeof readOnly) => rpc["kin.webhooks.github.rotate"]({ orgId }), + (rpc: typeof readOnly) => rpc["kin.webhooks.github.remove"]({ orgId }), + ]; + for (const call of calls) { + assert.strictEqual( + (yield* Effect.flip(call(readOnly)))._tag, + "EnvironmentAuthorizationError", + ); + assert.include(yield* Effect.flip(call(settings)), { + _tag: "KinChatError", + reason: "disabled", + }); + } + }), + ); +}); diff --git a/apps/server/src/kin/triggers/githubWebhooks.ts b/apps/server/src/kin/triggers/githubWebhooks.ts new file mode 100644 index 0000000000000..894c0ed11f186 --- /dev/null +++ b/apps/server/src/kin/triggers/githubWebhooks.ts @@ -0,0 +1,499 @@ +import { + CommandId, + KIN_GITHUB_APP_SOURCE, + KinChatError, + KinChatId, + KinMessageId, + KinWebhookSourceId, + ScheduledTaskError, + ScheduledTaskUpsertInput, + type KinGithubWebhookOrgInput, + type KinGithubWebhookSecretInput, + type KinGithubWebhookSetup, + type KinGithubWebhookSetupInput, + type KinOrgId, + type ModelSelection, + type ProjectId, + type ScheduledTaskId, +} from "@t3tools/contracts"; +import * as Config from "effect/Config"; +import * as Context from "effect/Context"; +import * as SecureRandom from "effect/crypto/SecureRandom"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import * as Option from "effect/Option"; +import * as Redacted from "effect/Redacted"; +import * as Schema from "effect/Schema"; +import * as Semaphore from "effect/Semaphore"; +import * as SqlClient from "effect/sql/SqlClient"; + +import * as ThreadLaunchService from "../../orchestration-v2/ThreadLaunchService.ts"; +import * as ThreadManagementService from "../../orchestration-v2/ThreadManagementService.ts"; +import { toPersistenceSqlError } from "../../persistence/Errors.ts"; +import * as ProjectService from "../../project/ProjectService.ts"; +import * as ScheduledTaskService from "../../scheduledTasks/ScheduledTaskService.ts"; +import type { PersonIdentity } from "../chat/KinIdentity.ts"; +import { KinChatService } from "../chat/KinChatService.ts"; +import * as KinTriggerStore from "./KinTriggerStore.ts"; + +export const KIN_SINK_PROMPT = + "Kin trigger. If you are reading this, Kin's trigger sink is not installed. Do nothing and stop."; +export const CONNECT_NOTICE = "GitHub events need T3 Connect on the Kin host"; +const sourceId = KinWebhookSourceId.make(KIN_GITHUB_APP_SOURCE); + +// The sender filter must match the App's own events, which GitHub always signs as `[bot]`. +const GitHubBotLogin = Schema.String.check( + Schema.makeFilter( + (value: string) => /^[A-Za-z0-9](?:[A-Za-z0-9]|-(?=[A-Za-z0-9])){0,38}\[bot\]$/.test(value), + { expected: "a GitHub App bot login" }, + ), +); +// Same key APPR-8's KinGitHubScope reads; switch to that service once it lands. +const BotLogin = Config.schema(GitHubBotLogin, "KIN_GITHUB_BOT_LOGIN"); + +// Gated until APPR-9: the App's private key and the action executor don't exist yet. +const appHookConfigureReady = false; +const APP_HOOK_TIMEOUT = "30 seconds"; +/** Test seam for the release gate; production always reads `appHookConfigureReady`. */ +export const AppHookConfigureGate = Context.Reference( + "t3/kin/triggers/githubWebhooks/AppHookConfigureGate", + { defaultValue: () => appHookConfigureReady }, +); + +/** Executor action kind `github.app_hook.configure`; the secret stays redacted and in-process. */ +export interface AppHookConfigure { + readonly kind: "github.app_hook.configure"; + readonly orgId: KinOrgId; + readonly sourceId: KinWebhookSourceId; + readonly url: string; + readonly secret?: Redacted.Redacted; +} + +export class GithubAppHookRefused extends Schema.TaggedError()( + "GithubAppHookRefused", + { reason: Schema.Literals(["gated", "unavailable", "failed", "timeout"]) }, +) {} + +/** `PATCH /app/hook/config`, signed with the App's JWT. APPR-9's executor provides it. */ +export class GithubAppHookClient extends Context.Service< + GithubAppHookClient, + { + readonly patchHookConfig: (body: { + readonly url: string; + readonly content_type: "json"; + readonly insecure_ssl: "0"; + readonly secret?: Redacted.Redacted; + }) => Effect.Effect; + } +>()("t3/kin/triggers/githubWebhooks/GithubAppHookClient") {} + +export const configureAppHook = Effect.fn("KinGithubWebhooks.configureAppHook")(function* ( + action: AppHookConfigure, +) { + if (!(yield* AppHookConfigureGate)) return yield* new GithubAppHookRefused({ reason: "gated" }); + const client = yield* Effect.serviceOption(GithubAppHookClient); + if (Option.isNone(client)) return yield* new GithubAppHookRefused({ reason: "unavailable" }); + // Callers hold the host-wide webhook lock across this call. + yield* client.value + .patchHookConfig({ + url: action.url, + content_type: "json", + insecure_ssl: "0", + ...(action.secret === undefined ? {} : { secret: action.secret }), + }) + .pipe( + Effect.timeoutOrElse({ + duration: APP_HOOK_TIMEOUT, + orElse: () => Effect.fail(new GithubAppHookRefused({ reason: "timeout" })), + }), + ); +}); + +const decodeUpsert = Schema.decodeEffect(ScheduledTaskUpsertInput); +const isChatError = Schema.is(KinChatError); +const isTaskError = Schema.is(ScheduledTaskError); +const isSourceConflict = Schema.is(KinTriggerStore.KinWebhookSourceConflict); +const notFound = new KinChatError({ reason: "not_found", message: "Kin record not found." }); +const appSourceTaken = new KinChatError({ + reason: "conflict", + message: "Another Kin org on this host already receives the GitHub App's events.", +}); +const invalid = (message: string) => new KinChatError({ reason: "invalid_state", message }); + +// Upstream messages are user-facing and carry no secret; causes are not logged. +const mapError = (cause: unknown) => { + if (isChatError(cause)) return Effect.succeed(cause); + if (isTaskError(cause)) return Effect.succeed(invalid(cause.message)); + if (isSourceConflict(cause)) + return Effect.succeed( + cause.reason === "app_source_taken" + ? appSourceTaken + : invalid( + cause.reason === "task_active" + ? "A GitHub delivery is still running. Try again." + : "The GitHub source conflicts with existing Kin records.", + ), + ); + return Effect.logError("Kin GitHub webhook operation failed", cause).pipe( + Effect.as(new KinChatError({ reason: "unavailable", message: "Kin is unavailable." })), + ); +}; + +export class KinGithubWebhooks extends Context.Service< + KinGithubWebhooks, + { + readonly setup: ( + people: ReadonlyArray, + input: KinGithubWebhookSetupInput, + ) => Effect.Effect; + readonly setSecret: ( + people: ReadonlyArray, + input: KinGithubWebhookSecretInput, + ) => Effect.Effect; + readonly rotate: ( + people: ReadonlyArray, + input: KinGithubWebhookOrgInput, + ) => Effect.Effect; + /** Deletes the upstream task too; the archived inbox thread is kept for a later setup. */ + readonly remove: ( + people: ReadonlyArray, + input: KinGithubWebhookOrgInput, + ) => Effect.Effect; + } +>()("t3/kin/triggers/githubWebhooks/KinGithubWebhooks") {} + +/** One permit per host: setup's cross-org check and every source change run one at a time. */ +export class KinGithubWebhookLock extends Context.Service< + KinGithubWebhookLock, + Semaphore.Semaphore +>()("t3/kin/triggers/githubWebhooks/KinGithubWebhookLock") {} + +export const lockLayer = Layer.effect(KinGithubWebhookLock, Semaphore.make(1)); + +export const make = Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + const tasks = yield* ScheduledTaskService.ScheduledTaskService; + const launcher = yield* ThreadLaunchService.ThreadLaunchService; + const threads = yield* ThreadManagementService.ThreadManagementService; + const projects = yield* ProjectService.ProjectService; + const random = yield* SecureRandom.SecureRandom; + const chat = yield* KinChatService; + const store = yield* KinTriggerStore.KinTriggerStore; + const exclusive = (yield* KinGithubWebhookLock).withPermits(1); + + const requireOwner = Effect.fn("KinGithubWebhooks.requireOwner")(function* ( + people: ReadonlyArray, + orgId: KinOrgId, + ) { + const person = people.find((p) => p.orgId === orgId); + if (person === undefined) return yield* notFound; + // Same rule as KinBudget.set: the lead's owner, or the bootstrap owner before any lead. + const owner = + yield* sql`SELECT 1 FROM kin_people p WHERE p.org_id = ${orgId} AND p.person_id = ${person.personId} + AND ((p.org_id = 'kin-default' AND p.person_id = 'kin-default-owner' + AND NOT EXISTS (SELECT 1 FROM kin_leads WHERE org_id = p.org_id)) OR EXISTS ( + SELECT 1 FROM kin_leads l JOIN kin_agents a ON a.org_id = l.org_id AND a.agent_id = l.agent_id + WHERE l.org_id = p.org_id AND a.owner_person_id = p.person_id))`.pipe( + Effect.mapError(toPersistenceSqlError("KinGithubWebhooks.requireOwner")), + ); + if (owner.length === 0) + return yield* new KinChatError({ reason: "not_allowed", message: "Only the owner can." }); + }); + + const newSecret = random + .randomBytes(32) + .pipe(Effect.map((bytes) => Redacted.make(Buffer.from(bytes).toString("hex")))); + + const findTask = (id: ScheduledTaskId) => + tasks.list().pipe(Effect.map(({ tasks }) => tasks.find((task) => task.id === id))); + + const upsert = (input: { + readonly orgId: KinOrgId; + readonly target: { readonly id: ScheduledTaskId } | { readonly commandId: CommandId }; + readonly projectId: ProjectId; + readonly modelSelection: ModelSelection; + readonly botLogin: string; + /** Undefined keeps the stored secret, so the bound hash stays valid. */ + readonly secret: Redacted.Redacted | undefined; + readonly enabled: boolean; + }) => + decodeUpsert({ + ...("id" in input.target + ? { id: input.target.id, requireExisting: true } + : { commandId: input.target.commandId }), + title: `Kin · GitHub App · ${input.orgId}`, + prompt: KIN_SINK_PROMPT, + enabled: input.enabled, + projectId: input.projectId, + threadId: null, + threadMode: "new_thread", + workspaceStrategy: { type: "root" }, + modelSelection: input.modelSelection, + runtimeMode: "approval-required", + interactionMode: "plan", + createdBy: "system", + creationSource: "server", + schedule: { + type: "webhook", + signature: { + header: "x-hub-signature-256", + encoding: "hex", + prefix: "sha256=", + ...(input.secret === undefined ? {} : { secret: Redacted.value(input.secret) }), + }, + maxDeliveryAgeMinutes: null, + dedupe: KinTriggerStore.GITHUB_DEDUPE, + filters: [ + { path: "headers.x-github-event", op: "in", value: KinTriggerStore.GITHUB_EVENTS }, + { path: "body.sender.login", op: "notEquals", value: input.botLogin }, + ], + }, + }).pipe( + // The decode issue would carry the secret, so it never reaches mapError's log. + Effect.mapError(() => invalid("The GitHub source could not be saved.")), + Effect.flatMap(tasks.upsert), + ); + + const needsConnect = Effect.fn("KinGithubWebhooks.needsConnect")(function* (orgId: KinOrgId) { + const [lead] = yield* sql<{ readonly chatId: string }>` + SELECT l.owner_chat_id AS "chatId" FROM kin_leads l + JOIN kin_agents a ON a.org_id = l.org_id AND a.agent_id = l.agent_id + WHERE l.org_id = ${orgId} AND a.retired_at IS NULL ORDER BY l.agent_id LIMIT 1`.pipe( + Effect.mapError(toPersistenceSqlError("KinGithubWebhooks.needsConnect")), + ); + if (lead !== undefined) + yield* chat.notice({ + orgId, + chatId: KinChatId.make(lead.chatId), + messageId: KinMessageId.make(`kin-github-connect:${orgId}`), + body: CONNECT_NOTICE, + }); + return { status: "needs_connect" } as const; + }); + + const offer = Effect.fn("KinGithubWebhooks.offer")(function* ( + orgId: KinOrgId, + url: string, + secret: Redacted.Redacted | undefined, + ) { + return yield* configureAppHook({ + kind: "github.app_hook.configure", + orgId, + sourceId, + url, + ...(secret === undefined ? {} : { secret }), + }).pipe( + Effect.as(true), + Effect.catchTags({ + GithubAppHookRefused: (refused) => + Effect.logInfo("Kin GitHub App hook left to the owner", { reason: refused.reason }).pipe( + Effect.as(false), + ), + }), + ); + }); + + const inboxThread = Effect.fn("KinGithubWebhooks.inboxThread")(function* ( + orgId: KinOrgId, + projectId: ProjectId, + modelSelection: ModelSelection, + ) { + // Launch receipts make this commandId return the same thread on every setup. + const launched = yield* launcher.launch({ + commandId: CommandId.make(`kin:webhook-inbox:${orgId}:${projectId}`), + projectId, + title: "Kin · GitHub inbox", + modelSelection, + runtimeMode: "approval-required", + interactionMode: "plan", + workspaceStrategy: { type: "root" }, + createdBy: "system", + creationSource: "server", + }); + if (launched.projection.thread.archivedAt === null) + yield* threads.dispatch({ + type: "thread.archive", + commandId: CommandId.make(`kin:webhook-inbox-archive:${orgId}:${projectId}`), + threadId: launched.threadId, + }); + return launched.threadId; + }); + + const create = Effect.fn("KinGithubWebhooks.create")(function* ( + input: KinGithubWebhookSetupInput, + botLogin: string, + ) { + const project = yield* projects.getById(input.projectId); + if (Option.isNone(project)) return yield* notFound; + const modelSelection = project.value.defaultModelSelection; + if (modelSelection === null) return yield* invalid("The project has no default model."); + const secret = yield* newSecret; + const { task } = yield* upsert({ + orgId: input.orgId, + target: { commandId: KinTriggerStore.githubAppCommandId(input.orgId) }, + projectId: input.projectId, + modelSelection, + botLogin, + secret, + enabled: false, + }); + const url = task.webhook?.url ?? null; + if (url === null) { + yield* tasks.delete({ id: task.id }); + return yield* needsConnect(input.orgId); + } + yield* Effect.gen(function* () { + yield* store.registerSource({ + id: sourceId, + orgId: input.orgId, + repo: KIN_GITHUB_APP_SOURCE, + scheduledTaskId: task.id, + inboxThreadId: yield* inboxThread(input.orgId, input.projectId, modelSelection), + githubHookId: null, + botLogin, + status: "active", + }); + yield* store.bindSecret(input.orgId, sourceId, secret); + }).pipe( + Effect.tapError(() => + Effect.andThen( + Effect.ignore(store.unregisterSource(input.orgId, sourceId)), + Effect.ignore(tasks.delete({ id: task.id })), + ), + ), + ); + // Enabled only once registered, so no delivery can reach a real launch. + yield* tasks.setEnabled({ id: task.id, enabled: true }); + return { + status: (yield* offer(input.orgId, url, secret)) ? "configured" : "needs_secret", + sourceId, + url, + } as const; + }); + + const owned = Effect.fn("KinGithubWebhooks.owned")(function* (orgId: KinOrgId) { + const source = yield* store.getSource(orgId, sourceId); + if (source === undefined) return yield* notFound; + const task = yield* findTask(source.scheduledTaskId); + if (task === undefined) return yield* notFound; + return { source, task }; + }); + + return KinGithubWebhooks.of({ + setup: Effect.fn("KinGithubWebhooks.setup")( + function* (people, input) { + yield* requireOwner(people, input.orgId); + const botLogin = yield* BotLogin.pipe( + Effect.mapError(() => + invalid("Set KIN_GITHUB_BOT_LOGIN to the GitHub App's bot login, ending in [bot]."), + ), + ); + const source = yield* store.getSource(input.orgId, sourceId); + if ( + source === undefined && + (yield* KinTriggerStore.appSourceHeldElsewhere(sql, input.orgId).pipe( + Effect.mapError(toPersistenceSqlError("KinGithubWebhooks.setup")), + )) + ) + return yield* appSourceTaken; + const task = source === undefined ? undefined : yield* findTask(source.scheduledTaskId); + if (source !== undefined && task === undefined) + yield* store.unregisterSource(input.orgId, sourceId); + if (source === undefined || task === undefined) return yield* create(input, botLogin); + if (task.projectId !== input.projectId) + return yield* invalid("GitHub events already go to another project."); + const url = task.webhook?.url ?? null; + if (url === null) return yield* needsConnect(input.orgId); + // Restores Kin's shape (say, a newer event list) before enabling, so a tampered task never + // runs; an unsigned task has no secret to keep, so only a re-key or setSecret repairs it. + if (task.schedule.type === "webhook" && task.schedule.signature !== null) + yield* upsert({ + orgId: input.orgId, + target: { id: task.id }, + projectId: task.projectId, + modelSelection: task.modelSelection, + botLogin: source.botLogin, + secret: undefined, + enabled: true, + }); + else if (!task.enabled) yield* tasks.setEnabled({ id: task.id, enabled: true }); + // Re-keys only when GitHub took the new secret; otherwise the owner's secret stays. + const secret = yield* newSecret; + if (!(yield* offer(input.orgId, url, secret))) + return { status: "needs_secret", sourceId, url } as const; + // Between the upsert and the bind, the old hash refuses deliveries: it fails closed. + yield* upsert({ + orgId: input.orgId, + target: { id: task.id }, + projectId: task.projectId, + modelSelection: task.modelSelection, + botLogin: source.botLogin, + secret, + enabled: true, + }); + yield* store.bindSecret(input.orgId, sourceId, secret); + return { status: "configured", sourceId, url } as const; + }, + exclusive, + Effect.catch((cause) => Effect.flatMap(mapError(cause), Effect.fail)), + ), + setSecret: Effect.fn("KinGithubWebhooks.setSecret")( + function* (people, input) { + yield* requireOwner(people, input.orgId); + const { source, task } = yield* owned(input.orgId); + const { task: saved } = yield* upsert({ + orgId: input.orgId, + target: { id: task.id }, + projectId: task.projectId, + modelSelection: task.modelSelection, + botLogin: source.botLogin, + secret: input.secret, + enabled: task.enabled, + }); + yield* store.bindSecret(input.orgId, sourceId, input.secret); + const url = saved.webhook?.url ?? null; + if (url === null) return yield* needsConnect(input.orgId); + return { status: "configured", sourceId, url } as const; + }, + exclusive, + Effect.catch((cause) => Effect.flatMap(mapError(cause), Effect.fail)), + ), + rotate: Effect.fn("KinGithubWebhooks.rotate")( + function* (people, input) { + yield* requireOwner(people, input.orgId); + const { task } = yield* owned(input.orgId); + const { task: rotated } = yield* tasks.rotateWebhookToken({ id: task.id }); + const url = rotated.webhook?.url ?? null; + if (url === null) return yield* needsConnect(input.orgId); + return { + status: (yield* offer(input.orgId, url, undefined)) ? "configured" : "needs_url", + sourceId, + url, + } as const; + }, + exclusive, + Effect.catch((cause) => Effect.flatMap(mapError(cause), Effect.fail)), + ), + remove: Effect.fn("KinGithubWebhooks.remove")( + function* (people, input) { + yield* requireOwner(people, input.orgId); + const source = yield* store.getSource(input.orgId, sourceId); + if (source === undefined) { + const orphan = yield* findTask(KinTriggerStore.githubAppTaskId(input.orgId)); + if (orphan !== undefined) yield* tasks.delete({ id: orphan.id }); + return; + } + const task = yield* findTask(source.scheduledTaskId); + if (task?.enabled === true) yield* tasks.setEnabled({ id: task.id, enabled: false }); + yield* store.unregisterSource(input.orgId, sourceId); + if (task !== undefined) yield* tasks.delete({ id: task.id }); + }, + exclusive, + Effect.catch((cause) => Effect.flatMap(mapError(cause), Effect.fail)), + ), + }); +}); + +export const layer = Layer.effect(KinGithubWebhooks, make).pipe( + Layer.provide(KinTriggerStore.layer), +); diff --git a/apps/server/src/kin/triggers/webhookRpcHandlers.ts b/apps/server/src/kin/triggers/webhookRpcHandlers.ts new file mode 100644 index 0000000000000..18dde1e536117 --- /dev/null +++ b/apps/server/src/kin/triggers/webhookRpcHandlers.ts @@ -0,0 +1,95 @@ +import { KinChatError, KinGithubWebhookRpcs } from "@t3tools/contracts"; +import * as Context from "effect/Context"; +import * as SecureRandom from "effect/crypto/SecureRandom"; +import * as Effect from "effect/Effect"; +import * as Option from "effect/Option"; +import * as RpcGroup from "effect/rpc/RpcGroup"; +import * as SqlClient from "effect/sql/SqlClient"; + +import type { AuthenticatedSession } from "../../auth/EnvironmentAuth.ts"; +import * as ThreadLaunchService from "../../orchestration-v2/ThreadLaunchService.ts"; +import * as ThreadManagementService from "../../orchestration-v2/ThreadManagementService.ts"; +import * as ProjectService from "../../project/ProjectService.ts"; +import * as ScheduledTaskService from "../../scheduledTasks/ScheduledTaskService.ts"; +import { KinChatService } from "../chat/KinChatService.ts"; +import { KinIdentity, type PersonIdentity } from "../chat/KinIdentity.ts"; +import { KinServer } from "../KinServer.ts"; +import * as GithubWebhooks from "./githubWebhooks.ts"; +import * as KinTriggerStore from "./KinTriggerStore.ts"; + +const group = RpcGroup.make(...KinGithubWebhookRpcs); +const unavailable = new KinChatError({ reason: "unavailable", message: "Kin is unavailable." }); +const disabled = new KinChatError({ + reason: "disabled", + message: "Kin is not running on this host.", +}); + +// Owner-only setup surface. The secret arrives on the owner's session and stays in-process. +export const makeKinWebhookHandlers = Effect.fn("makeKinWebhookHandlers")(function* ( + session: AuthenticatedSession, +) { + const kin = yield* Effect.serviceOption(KinServer); + const services = Option.flatMap(kin, (server) => server.services); + // Optional so handler tests that build only Kin chat services still compile and run. + const runtime = Option.all({ + sql: yield* Effect.serviceOption(SqlClient.SqlClient), + tasks: yield* Effect.serviceOption(ScheduledTaskService.ScheduledTaskService), + launcher: yield* Effect.serviceOption(ThreadLaunchService.ThreadLaunchService), + threads: yield* Effect.serviceOption(ThreadManagementService.ThreadManagementService), + projects: yield* Effect.serviceOption(ProjectService.ProjectService), + random: yield* Effect.serviceOption(SecureRandom.SecureRandom), + }); + // Built once per session; the host-wide lock comes from Kin's shared services. + const webhooks = + Option.isSome(services) && Option.isSome(runtime) + ? Option.some( + yield* GithubWebhooks.make.pipe( + Effect.provide(KinTriggerStore.layer), + Effect.provideContext( + Context.make(SqlClient.SqlClient, runtime.value.sql).pipe( + Context.add(ScheduledTaskService.ScheduledTaskService, runtime.value.tasks), + Context.add(ThreadLaunchService.ThreadLaunchService, runtime.value.launcher), + Context.add(ThreadManagementService.ThreadManagementService, runtime.value.threads), + Context.add(ProjectService.ProjectService, runtime.value.projects), + Context.add(SecureRandom.SecureRandom, runtime.value.random), + Context.add(KinChatService, Context.get(services.value, KinChatService)), + Context.add( + GithubWebhooks.KinGithubWebhookLock, + Context.get(services.value, GithubWebhooks.KinGithubWebhookLock), + ), + ), + ), + ), + ) + : Option.none(); + const service = Effect.gen(function* () { + if (Option.isNone(services)) return yield* disabled; + if (Option.isNone(webhooks)) return yield* unavailable; + const people = yield* Context.get(services.value, KinIdentity).resolve(session); + return { webhooks: webhooks.value, people }; + }); + const run = ( + body: ( + webhooks: GithubWebhooks.KinGithubWebhooks["Service"], + people: ReadonlyArray, + ) => Effect.Effect, + ) => + service.pipe( + Effect.flatMap(({ webhooks, people }) => body(webhooks, people)), + Effect.catchDefect((cause) => + Effect.logWarning("Kin webhook RPC defect.", cause).pipe( + Effect.andThen(Effect.fail(unavailable)), + ), + ), + ); + return group.of({ + "kin.webhooks.github.setup": (input) => + run((webhooks, people) => webhooks.setup(people, input)), + "kin.webhooks.github.setSecret": (input) => + run((webhooks, people) => webhooks.setSecret(people, input)), + "kin.webhooks.github.rotate": (input) => + run((webhooks, people) => webhooks.rotate(people, input)), + "kin.webhooks.github.remove": (input) => + run((webhooks, people) => webhooks.remove(people, input).pipe(Effect.as({}))), + }); +}); diff --git a/apps/server/src/kin/triggers/webhooks.testkit.ts b/apps/server/src/kin/triggers/webhooks.testkit.ts new file mode 100644 index 0000000000000..bbc0f41cd1afb --- /dev/null +++ b/apps/server/src/kin/triggers/webhooks.testkit.ts @@ -0,0 +1,70 @@ +import { type KinOrgId, OrchestrationV2ThreadProjectionJson } from "@t3tools/contracts"; +import * as Deferred from "effect/Deferred"; +import * as Effect from "effect/Effect"; +import * as Schema from "effect/Schema"; +import * as Tracer from "effect/Tracer"; + +export const timestamp = "2026-11-07T00:00:00.000Z"; +const decodeProjection = Schema.decodeSync(OrchestrationV2ThreadProjectionJson); + +export const projection = (orgId: KinOrgId, archivedAt: string | null = timestamp) => + decodeProjection({ + thread: { + id: `inbox-${orgId}`, + projectId: `project-${orgId}`, + title: "Inbox", + providerInstanceId: "codex", + modelSelection: { instanceId: "codex", model: "gpt-5.4" }, + runtimeMode: "approval-required", + interactionMode: "plan", + branch: null, + worktreePath: null, + activeProviderThreadId: null, + lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: `inbox-${orgId}` }, + forkedFrom: null, + createdBy: "system", + creationSource: "server", + createdAt: timestamp, + updatedAt: timestamp, + archivedAt, + deletedAt: null, + }, + runs: [], + attempts: [], + nodes: [], + subagents: [], + providerSessions: [], + providerThreads: [], + providerTurns: [], + runtimeRequests: [], + messages: [], + plans: [], + turnItems: [], + checkpointScopes: [], + checkpoints: [], + contextHandoffs: [], + contextTransfers: [], + visibleTurnItems: [], + updatedAt: timestamp, + }); + +// The runWebhookDelivery span ends after both delivery and run completion are persisted. +export const waitForDispatch = (work: Effect.Effect) => + Effect.gen(function* () { + const done = yield* Deferred.make(); + const tracer = Tracer.make({ + span: (options) => { + const span = new Tracer.NativeSpan(options); + const end = span.end.bind(span); + span.end = (...args) => { + end(...args); + if (span.name === "ScheduledTaskService.runWebhookDelivery") + Deferred.doneUnsafe(done, Effect.void); + }; + return span; + }, + }); + const result = yield* work.pipe(Effect.withTracer(tracer)); + yield* Deferred.await(done); + return result; + }); diff --git a/docs/kin/specs/triggers.md b/docs/kin/specs/triggers.md index cdb084d6f08e8..3a86c0f4e5e9e 100644 --- a/docs/kin/specs/triggers.md +++ b/docs/kin/specs/triggers.md @@ -77,7 +77,7 @@ GitHub ──relay──▶ /api/hooks (T3) ─▶ ScheduledTaskService ─▶ T KinTriggerScheduler (Scheduler tick, kin_triggers) ────────▶ kin_trigger_inbox ─▶ LEAD queue ``` -- **Webhooks use T3's scheduled tasks**, because the relay only forwards to `/api/hooks/:hookId/:token`, and signature, replay, filters, the offline hold and the delivery log all live there. Kin owns one unbound T3 webhook task per (org, repo). +- **Webhooks use T3's scheduled tasks**, because the relay only forwards to `/api/hooks/:hookId/:token`, and signature, replay, filters, the offline hold and the delivery log all live there. Kin owns one unbound T3 webhook task per org, fed by the GitHub App's own webhook. - **Time triggers use Kin's own table** on T3's `Scheduler`, reusing `nextScheduledRunAt`. T3's runs always start an agent, `fixed_time` uses the server's zone, late `fixed_time` runs are dropped, and a crash can lose a `once`. Kin needs the owner's zone per org, a late policy per kind, multi-step deferrals, `orgId`, and priority for the budget. Those trigger rows are derived from memory (deferrals) and org settings (routines), so they can be rebuilt. - **Why a durable inbox.** The sink sits inside `ScheduledTaskService`'s dependencies, so it depends only on `SqlClient`, `ThreadManagementService`, and Kin's SQL-backed inbox/store. Its layer runs Kin migrations before looking up sources. Otherwise the Kin layer, which itself needs `ScheduledTaskService`, would form a cycle. The inbox row also survives a crash between receipt and processing. @@ -98,7 +98,7 @@ Defaults: brief 08:30 daily, summary 18:30 Mon–Fri, dream 03:00 daily, full re - `kin_triggers` stores the `KinTrigger` fields, with `subject_json` and `schedule_json`. Trigger and fire IDs are unique within an org; fire and event deduplication are org-scoped. CRUD is org-scoped; due scans span orgs. `next_run_at`, `created_at` and `updated_at` are integer epoch milliseconds. Indexed due scans use keyset paging on `(next_run_at, org_id, trigger_id)`; a concurrent fire cannot shift an OFFSET page. Time trigger IDs may not start with `gh:`, which is reserved for webhook fires. - `kin_trigger_fires` stores `fire_id`, `org_id` and indexed integer `fired_at`. Retention deletes only rows strictly older than `now - 30 days`, not inbox work. Pending inbox rows continue to reject replays after fire retention; consumed inbox rows are separately removed after 7 days, so the inbox does not generally outlive the fire ledger. - `kin_trigger_inbox` stores the full `KinTriggerFired` envelope in `payload_json`, integer `received_at` and nullable integer `consumed_at`. Both receive paths insert with `ON CONFLICT (org_id, fire_id) DO NOTHING RETURNING fire_id`; other constraint failures raise. `listPending(orgId, limit)` reads unconsumed rows in `received_at, fire_id` order using an org-scoped pending index. The lead consumer reads pending rows, performs read-only or idempotent I/O (which can run again on replay), then in one transaction inserts into `kin_events` with `ON CONFLICT DO NOTHING` on `(org_id, dedupe_key)` and calls `markConsumed(orgId, fireIds)`. A crash replays pending work; durable event dedupe absorbs it. There are no claims or recovery loop. Reconciliation deletes consumed rows strictly older than 7 days using the `consumed_at` index. -- `kin_webhook_sources` stores the `KinWebhookSource` fields, keyed by `(org_id, source_id)`, unique by `(org_id, repo)` and by `scheduled_task_id` for the sink lookup. Task and thread IDs have no upstream foreign keys. `kin_trigger_settings` stores one `KinTriggerSettings` per org, including `quiet_hours_json`. +- `kin_webhook_sources` stores the `KinWebhookSource` fields, keyed by `(org_id, source_id)`, unique by `(org_id, repo)` and by `scheduled_task_id` for the sink lookup. The App source's `repo` is the marker `github-app` (`KIN_GITHUB_APP_SOURCE`), which can't collide with an `owner/name` slug; its id is also `github-app` and its `githubHookId` is null. Task and thread IDs have no upstream foreign keys. `kin_trigger_settings` stores one `KinTriggerSettings` per org, including `quiet_hours_json`. - `kin_ping_ledger` has the unique key `(org_id, person, item_id, day)`, where `day` is the owner's local calendar date, plus integer `pinged_at` and nullable integer `escalated_at`. TRIG-11 owns sending, batching and escalation. ### Time triggers: `KinTriggerScheduler` @@ -125,17 +125,19 @@ Defaults: brief 08:30 daily, summary 18:30 Mon–Fri, dream 03:00 daily, full re ### GitHub webhooks -**Creating a source** (`KinWebhookSources.add(orgId, repo)`): +**One App webhook, not per-repo hooks.** The GitHub App's own webhook covers every repo it is installed on, so Kin needs no `admin:repo_hook` token. An App has one hook config, so there is **one App webhook source per host**: setup refuses with `conflict` while another org holds it, checked again inside `registerSource`'s transaction. Routing one App to several orgs by `installation.id` is a later option. The App covers every repo it is installed on, so TRIG-6 must drop events for repos outside the org's allowlist before anything reaches a prompt. Trade-off: one T3 task's limits (60 accepted per minute, 20 queued) are shared by all of those repos. That is enough for one team; `github_recheck` covers anything the limit drops. -1. Generate a 32-byte secret with `SecureRandom`. -2. Create the org's inbox thread once. It is a real thread, launched with `ThreadLaunchService.launch` without a message, then archived and stored as `inboxThreadId`. The inbox thread is never an agent thread and is never registered in `kin_agent_threads`. The sink can read an archived inbox projection without posting a message to it. -3. Call `ScheduledTaskService.upsert`. Kin holds the secret only for this call: +**Setup** (`KinGithubWebhooks.setup`, owner-only RPC `kin.webhooks.github.setup({ orgId, projectId })`, in `githubWebhooks.ts`): + +1. Check the caller is the org's owner (the `KinBudget.set` rule). `kin_orgs` has no project, so the owner passes `projectId`; it must exist and have a default model, which the task and inbox thread use. A second setup naming another project fails. Choosing a quiet project does not fence the task: any live agent can edit or delete it through the orchestrator MCP tools, including agents in other projects (`assertLiveCallerForOtherProject` lets a running caller through). The sink's shape check and secret binding (below) are what keep those edits from authoring deliveries. +2. Generate a 32-byte secret with `SecureRandom`. `botLogin` comes from `KIN_GITHUB_BOT_LOGIN`, the same key APPR-8's `KinGitHubScope` reads; it must end in `[bot]`, or setup fails with a config error. +3. Call `ScheduledTaskService.upsert` **disabled**, with `commandId: kin:webhook:${orgId}:github-app`: ```ts -{ title: `Kin · GitHub · ${repo}`, prompt: KIN_SINK_PROMPT, enabled: true, projectId: org.projectId, +{ title: `Kin · GitHub App · ${orgId}`, prompt: KIN_SINK_PROMPT, enabled: false, projectId, threadId: null, threadMode: "new_thread", workspaceStrategy: { type: "root" }, modelSelection: , runtimeMode: "approval-required", interactionMode: "plan", - createdBy: "system", creationSource: "server", commandId: `kin:webhook:${orgId}:${repo}`, + createdBy: "system", creationSource: "server", schedule: { type: "webhook", signature: { header: "x-hub-signature-256", encoding: "hex", prefix: "sha256=", secret }, maxDeliveryAgeMinutes: null, // run held requests too; the consumer re-reads state @@ -144,42 +146,68 @@ Defaults: brief 08:30 daily, summary 18:30 Mon–Fri, dream 03:00 daily, full re { path: "headers.x-github-event", op: "in", value: [ "pull_request", "pull_request_review", "pull_request_review_comment", "issue_comment", "issues", "check_suite", "workflow_run", "push" ] }, - { path: "body.sender.login", op: "notEquals", value: org.botLogin }, // no loops + { path: "body.sender.login", op: "notEquals", value: botLogin }, // no loops ] } } ``` -4. Create the hook on GitHub: `POST /repos/{owner}/{repo}/hooks` with the returned `task.webhook.url`, the secret, `content_type: json` and the same events. This needs `admin:repo_hook`, so it is a privileged action: `request_action({ kind: "github.repo_hook.create", target: repo })` (APPR), and the executor runs it. The secret goes from Kin's create step to the executor through the pending action's sealed payload, never through a model or a chat message. **Fallback:** the setup screen shows the URL, and the owner pastes it in GitHub with a secret they paste into a `request_secret` card. -5. If `task.webhook.url` is null, T3 Connect is off. Kin posts "GitHub events need T3 Connect on the Kin host" and stops. +4. If `task.webhook.url` is null, T3 Connect is off: delete the task, post "GitHub events need T3 Connect on the Kin host" to the owner chat as a system notice, return `needs_connect` and stop. +5. Create the org's inbox thread once: `ThreadLaunchService.launch` without a message (its `commandId` includes org and project, so launch receipts return the same thread on every setup), then archive it. It is never registered in `kin_agent_threads`; `registerSource` and `registerAgentThread` each reject the overlap inside their own write transaction. +6. `registerSource`, bind the secret's hash, and only then enable the task, so no delivery can reach a real launch with `KIN_SINK_PROMPT`. If registering or binding fails, setup deletes the disabled task. +7. Point the App at the URL with the executor action `github.app_hook.configure` (`PATCH /app/hook/config` with `url`, `secret`, `content_type: json`). It needs the App's private key, so it is **gated until APPR-9** (`appHookConfigureReady = false`; while off it refuses with `GithubAppHookRefused { reason: "gated" }`). The secret travels in-process as `Redacted`, never through a model, a chat message or a log. The call times out after 30 s (`reason: "timeout"`), because it runs under the host-wide lock. Success returns `configured`. +8. **Fallback while gated:** return `needs_secret` with the URL to the owner-only surface. The owner sets that URL and a new secret in the GitHub App's webhook settings, then sends the secret with `kin.webhooks.github.setSecret`, which re-saves the task in the shape above with that secret and binds its hash. The secret arrives on the owner's authenticated session (`AuthSettingsWriteScope`, owner check) and stays in-process; a `request_secret` card would hand an agent a ref instead. Until then deliveries fail closed with `rejected_signature`. + +Setup, setSecret, rotate and remove take one host-wide lock (`KinGithubWebhookLock`, in Kin's services), so the cross-org check and each source change run one at a time. A repeated setup returns the same source. Before offering GitHub a secret, it re-saves the task in Kin's shape without a secret, so T3 keeps the stored one and the bound hash stays valid; this repairs a task someone edited, or one saved with an older event list, even while gated. A task saved unsigned has no secret to keep, so only a re-key or `setSecret` repairs it. With the gate on setup then re-keys: GitHub takes a fresh secret first, then T3, then the hash binding. `KIN_SINK_PROMPT` = "Kin trigger. If you are reading this, Kin's trigger sink is not installed. Do nothing and stop." Plan mode plus approval-required keeps an accidental run harmless. -**Why no coalescing.** T3 coalescing keeps only the latest payload, so two PRs in one window would lose one. LEAD's per-org merge window does the batching without dropping events. Rotation: `rotateWebhookToken`, then update the GitHub hook URL through the same executor action. +**Why no coalescing.** T3 coalescing keeps only the latest payload, so two PRs in one window would lose one. LEAD's per-org merge window does the batching without dropping events. + +**Rotate** (`kin.webhooks.github.rotate`): `rotateWebhookToken`, then `github.app_hook.configure` with the new URL only. While gated it returns `needs_url` and the owner updates the URL in the App settings. The old URL stops working at once, so deliveries fail until GitHub has the new one. + +**Remove** (`kin.webhooks.github.remove`): disable the task, `unregisterSource`, then delete the task. The archived inbox thread stays for a later setup. `unregisterSource` refuses (`task_active`) while the task is enabled or a delivery is in flight (`last_run_status = 'running'`, or a `dispatching`/`running` pending row), so a delivery can't lose its source mid-dispatch. A delivery that read the task just before remove has no source by the time it launches; the sink refuses any task whose id starts with `scheduled-task:kin:webhook:` (upstream names a commandId-created task `scheduled-task:${commandId}`), so it fails with `dispatch_failed` instead of launching. With no source row, remove still deletes that deterministic task if it exists. **Receiving: `KinTriggerSink`.** It wraps the `ThreadLaunchService` and `ThreadManagementService` given only to `ScheduledTaskService`: ```ts -launch: (input) => { +receive: (input) => { const taskId = input.initialMessage?.scheduledTaskId; - const source = taskId && sources.byTaskId(taskId); // SQL, kin_webhook_sources - if (!source) return inner.launch(input); // not Kin's: unchanged - const deliveryId = parseDelivery(input.commandId, taskId); // suffix after `${taskId}:webhook:` - if (deliveryId === null) return Effect.fail(launchError(input, "dispatch-message")); - return inbox.put({ fireId: `gh:${source.orgId}:${deliveryId}`, ... }) // ON CONFLICT (org_id, fire_id) DO NOTHING - .pipe(Effect.andThen(threads.getThreadProjection(source.inboxThreadId)), - Effect.map((projection) => ({ threadId: source.inboxThreadId, projection, resumed: false }))); + if (taskId === undefined) return undefined; // caller falls through to inner.launch + const source = store.sourceByTaskId(taskId); // SQL, kin_webhook_sources + if (source === undefined) { + if (taskId starts with "scheduled-task:kin:webhook:") fail(KinTriggerSinkRefused); // racing remove + return undefined; // not Kin's: unchanged + } + // From here every failure is a ThreadLaunchError("dispatch-message"): no real launch. + require(input.commandId starts with `scheduled-task:${taskId}:webhook:` && source.status === "active"); + const deliveryId = input.commandId.slice(prefix.length); + const delivery = findLaunchDelivery(sql, { taskId, deliveryId, projectId: input.projectId, + botLogin: source.botLogin }); + require(delivery); // unbound new_thread webhook task in this project, signed, with Kin's replay + // dedupe object, event list and bot-sender filter: any live agent can re-save it + require(sha256(delivery.secret) equals source.secret_sha256); // timingSafeEqual; NULL refuses + require(delivery.receivedAt > source.secret_bound_at); // verified under this secret + const projection = threads.getThreadProjection(source.inboxThreadId); + require(projection.thread.projectId === input.projectId && projection.thread.deletedAt === null); + inbox.put({ fireId: `gh:${source.orgId}:${deliveryId}`, dueAt: delivery.receivedAt, firedAt: now, + payload: { kind: "github", sourceId: source.id, scheduledTaskId: taskId, deliveryId } }, now); + return { threadId: source.inboxThreadId, projection, resumed: false }; } ``` -A registered Kin task never falls through to a real launch. The wrapper is local to scheduled tasks and is given the existing thread-management layer explicitly; other callers retain the original launch and thread-management services. Bound Kin sends and failed source lookups are rejected before dispatch; ordinary sends pass through. If parsing, source validation or inbox persistence fails, the launch fails, and T3 logs `dispatch_failed` in Deliveries. +A registered Kin task never falls through to a real launch. The wrapper is local to scheduled tasks and is given the existing thread-management layer explicitly; other callers retain the original launch and thread-management services. Bound Kin sends, including a `scheduled-task:kin:webhook:` task with no source, and failed source lookups are rejected before dispatch; ordinary sends pass through. If parsing, source validation or inbox persistence fails, the launch fails, and T3 logs `dispatch_failed` in Deliveries. + +**Secret binding.** Every Kin save that sets a secret (create, the re-key with the gate on, `setSecret`) is followed under the lock by `bindSecret`, which stores the secret's SHA-256 in `kin_webhook_sources.secret_sha256` and the bind time from `Clock` in `secret_bound_at` (migration K019). The sink hashes the task's current `webhook_secret` and compares the two with `timingSafeEqual`, and it requires the delivery's `received_at` to be later than `secret_bound_at`. T3 verifies a signature when the request arrives, but the sink reads the secret at launch, and a queued delivery can launch after a re-key; the time check ties each delivery to the secret it was verified against. `received_at` is the server clock, or the relay's receive time only with its signed proof (`RelayDeliveryProof`), capped at now, so a caller can backdate it only with the relay's signature. A NULL hash (a source made before K019), a mismatch (an agent swapped the secret, or a re-key is between its save and its bind, when the old hash is still bound) or an early delivery refuses the same way a weakened task does: each fails closed. The way out is `kin.webhooks.github.setSecret`, which re-saves and re-binds. A legitimate delivery the relay held while the host was offline and received before a re-key also refuses; `github_recheck` picks up what it carried. So an agent that re-saves the task with a secret it chose can get forged deliveries accepted upstream, but none reaches the inbox, even if the owner re-keys while one is queued. Neither value is logged, traced or put in an error. The sink also requires Kin's exact dedupe object (`{"replayKey":"{{headers.x-github-delivery}}"}`, no coalesce key or window) and event list, so an edit can't switch off dedupe to replay a delivery or hold one in a coalesce window. + +What stays open: an agent can still delete, disable or re-key the task. That stops deliveries (denial of service) but cannot author an inbox row. `trigger_reconcile` (TRIG-10) must report a missing or disabled task, or a hash mismatch, to the owner. Fencing the MCP tools off Kin's task is still an open question. -`KinTriggerStore.registerSource` requires an existing org and an unbound `new_thread` webhook task. `getThreadProjection` checks that the inbox thread is not deleted and its project matches the task's project. Identical registration returns the stored source; conflicting identity fails. TRIG-5 must retain this unbound task shape on edits and disable/delete the upstream task before unregistering its source. Sources have no upstream org column: the `kin_webhook_sources` row establishes ownership; registration rejects task or inbox IDs already owned by another org. +`KinTriggerStore.registerSource` requires an existing org and an unbound `new_thread` webhook task. It reads the inbox projection before opening its transaction, then checks that the thread is not deleted and its project matches the task's. Identical registration returns the stored source; conflicting identity fails. Sources have no upstream org column: the `kin_webhook_sources` row establishes ownership; registration rejects task or inbox IDs already owned by another org. The sink verifies the delivery-log row belongs to the scheduled task. `dueAt` is the log's `received_at`, `firedAt` comes from `Clock`, and `lateByMs` is their nonnegative difference. `KinTriggerFired` carries only T3's delivery-log id, not GitHub's delivery header; TRIG-6 reads `x-github-delivery` through `getWebhookDelivery` as described below. No contract change or payload parsing is needed here. **Consuming (Kin layer, outside the cycle).** 1. For each `github` inbox row, call `ScheduledTaskService.getWebhookDelivery({ id, deliveryId, acceptAnyOutcome: true })`. -2. Build a `KinGithubEvent` from `headers["x-github-event"]`, `x-github-delivery` and the parsed body. `heldByRelay` is true when the delivery id starts with `delivery:relay:`. +2. Build a `KinGithubEvent` from `headers["x-github-event"]`, `x-github-delivery` and the parsed body, and drop events for repos outside the org (the App webhook covers every installed repo). `heldByRelay` is true when the delivery id starts with `delivery:relay:`. 3. The event is a hint. The lead re-reads the PR, issue or run from GitHub before acting, because a held request can be 24 h old, a body over 64 KB is truncated, and the log keeps only 50 deliveries. If the delivery is gone, or `bodyTruncated` is set and the payload can't be parsed, emit a `github_recheck` for that repo instead. 4. Hand the event to the LEAD queue. Payload text is untrusted (section 9.4): `trust: external`, quoted, never obeyed. @@ -292,14 +320,14 @@ ended in the last 90 days. All are plain code. They write memory only through the LEAD queue as code-only events, which keeps one writer per org (gap 1). -| Watchdog | Every | Does | -| ------------------- | ---------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `github_recheck` | 10 min, and at startup | For open items with GitHub links: a closed issue marks its commitment done, a merged PR closes its deferral, and the evidence link is recorded. Matches conditions. Batches GraphQL reads per repo. | -| `stale_review` | 30 min | PRs with a review requested over 24 h ago: chase the reviewer (ping rules). | -| `promise_overdue` | 30 min | Open commitments past `due`: chase whoever promised. | -| `objective_at_risk` | 6 h | Milestone due within 3 days with open linked issues: flag to the owner. | -| `index_freshness` | 1 h | `INDEX.md` "As of" older than 36 h: rebuild it (code, below). | -| `trigger_reconcile` | 15 min, and at startup | Every open deferral has armed triggers; every routine exists. Every `KinWebhookSource` task exists, is enabled and isn't `blockedByPolicy`; if not, set `status` and tell the owner. It never re-enables a task the owner paused in T3. Clears consumed inbox rows older than 7 days. | +| Watchdog | Every | Does | +| ------------------- | ---------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `github_recheck` | 10 min, and at startup | For open items with GitHub links: a closed issue marks its commitment done, a merged PR closes its deferral, and the evidence link is recorded. Matches conditions. Batches GraphQL reads per repo. | +| `stale_review` | 30 min | PRs with a review requested over 24 h ago: chase the reviewer (ping rules). | +| `promise_overdue` | 30 min | Open commitments past `due`: chase whoever promised. | +| `objective_at_risk` | 6 h | Milestone due within 3 days with open linked issues: flag to the owner. | +| `index_freshness` | 1 h | `INDEX.md` "As of" older than 36 h: rebuild it (code, below). | +| `trigger_reconcile` | 15 min, and at startup | Every open deferral has armed triggers; every routine exists. Every `KinWebhookSource` task exists, is enabled, isn't `blockedByPolicy`, and its secret matches the bound hash; if not, set `status` and tell the owner. It never re-enables a task the owner paused in T3. Clears consumed inbox rows older than 7 days. | ### Staying fresh (section 4) @@ -503,7 +531,7 @@ Kin runs on one always-on machine, the **Kin host**. The laptop and phone are cl - provides the `kin_defer` tool. - **CHAT:** `KinOrgId`; delivering messages and pings; `KinWidget` payloads `KinDeferralCard`, `KinHeadsUpCard`, `KinOwnerCheckCard`, `KinDreamReviewCard`; the chat message store for dream material; verified authors for trust checks. - **APPR:** - - action kinds `github.repo_hook.create`, `memory.dream_review`, `memory.purge`, `kin.secret_allow`; + - action kinds `github.app_hook.configure`, `memory.dream_review`, `memory.purge`, `kin.secret_allow`; - the standing `dream:auto` delegation; - the executor merging at a bound SHA; - `KinAuditEntry`; @@ -515,7 +543,7 @@ Kin runs on one always-on machine, the **Kin host**. The laptop and phone are cl - **Webhooks.** Every Kin webhook task requires the HMAC signature, and the URL token is the second factor. A missing secret is a setup error, not "token only". Payload text is untrusted (`trust: external`) and is quoted into briefs, never obeyed. The bot-sender filter stops Kin reacting to itself. - **Relay hold.** With hold on, request bodies, including private PR text, sit on T3 Connect's relay (operated by upstream) for up to 24 h. The owner chooses; the setup screen says so. - **Lead and worker sessions must not have T3's `schedule_task` tools.** Otherwise a prompt injection could create a recurring agent run with full prompts. LEAD must disable the orchestrator scheduling tools for Kin threads, or Kin must refuse tasks whose `creationSource` is `mcp` in Kin projects. See Open questions. -- **Secrets.** The webhook secret is passed once to `upsert` and to the executor's sealed payload; it is never logged. T3 stores it in SQLite as plain text, so backups are encrypted. +- **Secrets.** The webhook secret is passed to `upsert` and, once APPR-9 lands, to the `github.app_hook.configure` action as `Redacted`; it is never logged. While that action is gated, the owner's secret arrives only through the owner-only `setSecret` RPC. T3 stores it in SQLite as plain text, so backups are encrypted. - **Auto-merge.** It is bounded twice: Kin's classifier and executor path check, then GitHub `CODEOWNERS` with code-owner review. ## Tests and acceptance criteria @@ -540,7 +568,7 @@ Kin runs on one always-on machine, the **Kin host**. The laptop and phone are cl | TRIG-2 | Trigger tables and store | `KinTriggerStore.ts`, `KinTriggerInbox.ts`, Kin migrations | TRIG-1, CHAT-2 (Kin migration list) | store tests: idempotent `put`, `orgId` on every row | backend | | TRIG-3 | `KinTriggerScheduler`: owner zone, firing, lateness | `KinTriggerScheduler.ts` | TRIG-2 | scheduler and timezone tests above pass | backend | | TRIG-4 | `KinTriggerSink` and the runtime-layer hook | `KinTriggerSink.ts`, `runtimeLayer.ts` (1 line) | TRIG-2 | sink tests pass; non-Kin tasks unchanged (existing `ScheduledTaskService*.test.ts` green) | backend | -| TRIG-5 | GitHub webhook sources: create, rotate, inbox thread, hook via executor | `githubWebhooks.ts` | TRIG-4, APPR-9 | test against a fake GitHub creates a task with signature, replay key and filters; a null URL posts the T3 Connect message | backend | +| TRIG-5 | GitHub App webhook source: setup, secret, rotate, remove, inbox thread; App hook via executor (gated) | `githubWebhooks.ts`, `webhookRpcHandlers.ts` | TRIG-4; APPR-9 opens the gate | test against a fake GitHub App creates a task with signature, replay key and filters; a null URL posts the T3 Connect message | backend | | TRIG-6 | GitHub event consumer and conditions | `githubEvents.ts` | TRIG-5, LEAD-4 | delivery turns into a `KinGithubEvent`; a trimmed delivery falls back to `github_recheck`; a condition on #212 matches a merge | backend | | TRIG-7 | Deferral arming from memory (including items written by `kin_defer`), and the confirm card | `KinTriggerService.ts`, `deferrals.ts` | TRIG-3, LEAD-2, LEAD-7 (`kin_defer`), CHAT-7 | editing `due` in an item re-arms; `status: done` disarms | backend | | TRIG-8 | Deferral due flow: recheck, heads-up, grace, start/ask/remind/chase, late | `deferrals.ts` | TRIG-7, TRIG-11, LEAD-8, LEAD-9, LEAD-11 | the deferral tests above pass | backend | @@ -573,7 +601,6 @@ Kin runs on one always-on machine, the **Kin host**. The laptop and phone are cl ## Open questions -- **GitHub App webhook vs. per-repo hooks.** One App webhook URL would avoid `admin:repo_hook`, but it shares one T3 task's 60-per-minute limit across all repos and orgs. Decide with APPR. - **Daytime lead writes.** With the memory repo's `main` requiring PRs (needed for the CODEOWNERS guard), LEAD's daytime writes must land as auto-merged PRs too. Otherwise drop the ruleset and rely on Kin's checks only. - **Fencing T3's scheduling tools.** Can `McpToolAccess` remove `schedule_task` for specific threads, or does Kin need a check on `creationSource: "mcp"` in Kin projects? Decide with LEAD. - **Owner-check day.** Should the weekly owner check move to the owner's quietest weekday, learned from activity? diff --git a/packages/contracts/src/kinChat.ts b/packages/contracts/src/kinChat.ts index 6951abb433d7a..bc4d828b4f145 100644 --- a/packages/contracts/src/kinChat.ts +++ b/packages/contracts/src/kinChat.ts @@ -349,6 +349,7 @@ export class KinChatError extends Schema.TaggedError()("KinChatErr "limit", "floor_not_granted", "floor_closed", + "conflict", ]), message: Schema.String, }) {} diff --git a/packages/contracts/src/kinRpcs.ts b/packages/contracts/src/kinRpcs.ts index cb0f3117836ae..5495a194b9b33 100644 --- a/packages/contracts/src/kinRpcs.ts +++ b/packages/contracts/src/kinRpcs.ts @@ -3,6 +3,12 @@ import * as Schema from "effect/Schema"; import { KinBudget, KinBudgetGetInput, KinBudgetSetInput } from "./kinLead.ts"; import { KinApprovalRpcs, KIN_APPROVAL_RPC_SCOPES } from "./kinApprovals.ts"; import { KinChatError, KinChatRpcs, KIN_CHAT_RPC_SCOPES } from "./kinChat.ts"; +import { + KinGithubWebhookOrgInput, + KinGithubWebhookSecretInput, + KinGithubWebhookSetup, + KinGithubWebhookSetupInput, +} from "./kinTriggers.ts"; import { EnvironmentAuthorizationError, AuthOrchestrationReadScope, @@ -21,13 +27,44 @@ const KinBudgetRpcs = [ error: Schema.Union([KinChatError, EnvironmentAuthorizationError]), }), ] as const; +const kinError = Schema.Union([KinChatError, EnvironmentAuthorizationError]); +export const KinGithubWebhookRpcs = [ + Rpc.make("kin.webhooks.github.setup", { + payload: KinGithubWebhookSetupInput, + success: KinGithubWebhookSetup, + error: kinError, + }), + Rpc.make("kin.webhooks.github.setSecret", { + payload: KinGithubWebhookSecretInput, + success: KinGithubWebhookSetup, + error: kinError, + }), + Rpc.make("kin.webhooks.github.rotate", { + payload: KinGithubWebhookOrgInput, + success: KinGithubWebhookSetup, + error: kinError, + }), + Rpc.make("kin.webhooks.github.remove", { + payload: KinGithubWebhookOrgInput, + success: Schema.Struct({}), + error: kinError, + }), +] as const; export const KinChatAndBudgetRpcs = [...KinChatRpcs, ...KinBudgetRpcs] as const; -export const KinRpcs = [...KinChatAndBudgetRpcs, ...KinApprovalRpcs] as const; +export const KinRpcs = [ + ...KinChatAndBudgetRpcs, + ...KinApprovalRpcs, + ...KinGithubWebhookRpcs, +] as const; export const KIN_RPC_SCOPES = { ...KIN_CHAT_RPC_SCOPES, ...KIN_APPROVAL_RPC_SCOPES, "kin.budget.get": AuthOrchestrationReadScope, "kin.budget.set": AuthSettingsWriteScope, + "kin.webhooks.github.setup": AuthSettingsWriteScope, + "kin.webhooks.github.setSecret": AuthSettingsWriteScope, + "kin.webhooks.github.rotate": AuthSettingsWriteScope, + "kin.webhooks.github.remove": AuthSettingsWriteScope, } as const; export const KIN_RPC_AGGREGATES = { @@ -53,4 +90,8 @@ export const KIN_RPC_AGGREGATES = { "kin.approvals.revokeKey": "kin-approvals", "kin.audit.list": "kin-approvals", "kin.audit.verify": "kin-approvals", + "kin.webhooks.github.setup": "kin-webhooks", + "kin.webhooks.github.setSecret": "kin-webhooks", + "kin.webhooks.github.rotate": "kin-webhooks", + "kin.webhooks.github.remove": "kin-webhooks", } as const; diff --git a/packages/contracts/src/kinTriggers.test.ts b/packages/contracts/src/kinTriggers.test.ts index 363f0850ca949..ae8346df1e8c9 100644 --- a/packages/contracts/src/kinTriggers.test.ts +++ b/packages/contracts/src/kinTriggers.test.ts @@ -132,8 +132,36 @@ const samples = { ], ], KinTriggerSettings: [Kin.KinTriggerSettings, [settings, { ...settings, timeZone: "UTC" }]], + KinGithubWebhookSetupInput: [ + Kin.KinGithubWebhookSetupInput, + [{ orgId: "org-1", projectId: "project-1" }], + ], + KinGithubWebhookOrgInput: [Kin.KinGithubWebhookOrgInput, [{ orgId: "org-1" }]], + KinGithubWebhookSecretInput: [ + Kin.KinGithubWebhookSecretInput, + [{ orgId: "org-1", secret: "0123456789abcdef" }], + ], + KinGithubWebhookSetup: [ + Kin.KinGithubWebhookSetup, + [ + { status: "needs_connect" }, + ...["configured", "needs_secret", "needs_url"].map((status) => ({ + status, + sourceId: Kin.KIN_GITHUB_APP_SOURCE, + url: "https://relay.example/v1/hooks/env/task/token", + })), + ], + ], } satisfies Record, ReadonlyArray]>; +const secretRejections = [ + "too-short", + " 0123456789abcdef", + "0123456789abcdef\n", + "01234567 89abcdef", + "0123456789\tabcdef", +]; + const rejections = [ ...[ "1", @@ -155,6 +183,10 @@ const rejections = [ { schema: Kin.KinGithubEvent, value: { ...githubEvent, receivedAt: invalid } }, ]), ...["", " \n "].map((value) => ({ schema: Kin.KinTriggerId, value })), + ...secretRejections.map((secret) => ({ + schema: Kin.KinGithubWebhookSecretInput, + value: { orgId: "org-1", secret }, + })), ...[0, 59_999, 60_000.5].map((everyMs) => ({ schema: Kin.KinTriggerSchedule, value: { type: "interval", everyMs }, @@ -242,4 +274,12 @@ describe("Kin trigger contracts", () => { it.each(rejections)("rejects invalid trigger data %#", ({ schema, value }) => { expect(() => Schema.decodeUnknownSync(schema)(value)).toThrow(); }); + + it.each(secretRejections)("never echoes a rejected webhook secret %#", (secret) => { + const decode = Schema.decodeUnknownSync(Kin.KinGithubWebhookSecretInput); + expect(() => decode({ orgId: "org-1", secret })).toThrow( + "A webhook secret needs at least 16 characters and no whitespace.", + ); + expect(() => decode({ orgId: "org-1", secret })).not.toThrow(secret.trim()); + }); }); diff --git a/packages/contracts/src/kinTriggers.ts b/packages/contracts/src/kinTriggers.ts index fc9cfc1282e99..8e5ef32d4f061 100644 --- a/packages/contracts/src/kinTriggers.ts +++ b/packages/contracts/src/kinTriggers.ts @@ -1,6 +1,12 @@ import * as Schema from "effect/Schema"; -import { NonNegativeInt, ScheduledTaskId, ThreadId, TrimmedNonEmptyString } from "./baseSchemas.ts"; +import { + NonNegativeInt, + ProjectId, + ScheduledTaskId, + ThreadId, + TrimmedNonEmptyString, +} from "./baseSchemas.ts"; import { KinOrgId, KinPersonId, KinText } from "./kinChat.ts"; import { CanonicalUtcTimestamp, KinMemoryItemId } from "./kinLead.ts"; import { ScheduledTaskUpsertSchedule, ScheduledTaskWebhookDeliveryId } from "./scheduledTask.ts"; @@ -107,6 +113,40 @@ export const KinWebhookSource = Schema.Struct({ }); export type KinWebhookSource = typeof KinWebhookSource.Type; +/** `KinWebhookSource.repo` of an org's GitHub App webhook source, which covers every installed repo. */ +export const KIN_GITHUB_APP_SOURCE = "github-app"; + +export const KinGithubWebhookSetupInput = Schema.Struct({ orgId: KinOrgId, projectId: ProjectId }); +export type KinGithubWebhookSetupInput = typeof KinGithubWebhookSetupInput.Type; +export const KinGithubWebhookOrgInput = Schema.Struct({ orgId: KinOrgId }); +export type KinGithubWebhookOrgInput = typeof KinGithubWebhookOrgInput.Type; +// The message names only length and character class: RpcServer returns it to the client verbatim. +const WebhookSecret = Schema.String.check( + Schema.makeFilter((value: string) => value.length >= 16 && !/\s/.test(value), { + message: "A webhook secret needs at least 16 characters and no whitespace.", + }), +); +export const KinGithubWebhookSecretInput = Schema.Struct({ + orgId: KinOrgId, + secret: Schema.RedactedFromValue(WebhookSecret), +}); +export type KinGithubWebhookSecretInput = typeof KinGithubWebhookSecretInput.Type; + +/** + * `configured`: GitHub and T3 share the secret. `needs_secret`: set this URL and a new secret in + * the GitHub App's webhook settings, then send the secret. `needs_url`: update only the URL there; + * a rotate has already retired the old URL, so deliveries fail until GitHub has the new one. + */ +export const KinGithubWebhookSetup = Schema.Union([ + Schema.Struct({ status: Schema.Literal("needs_connect") }), + Schema.Struct({ + status: Schema.Literals(["configured", "needs_secret", "needs_url"]), + sourceId: KinWebhookSourceId, + url: Schema.String, + }), +]); +export type KinGithubWebhookSetup = typeof KinGithubWebhookSetup.Type; + export const KinGithubEvent = Schema.Struct({ orgId: KinOrgId, deliveryId: TrimmedNonEmptyString,