From 768c397d635360dbc9b095573e38e38682801097 Mon Sep 17 00:00:00 2001 From: GLDRoger <130458023+GLDRoger@users.noreply.github.com> Date: Sun, 8 Nov 2026 13:15:01 +0530 Subject: [PATCH 01/15] feat(kin): block secrets in memory writes (TRIG-16) --- apps/server/src/kin/memory/Memory.test.ts | 16 +- apps/server/src/kin/memory/Memory.ts | 29 ++- .../src/kin/memory/MemoryWriter.test.ts | 22 ++- apps/server/src/kin/memory/secretScan.test.ts | 129 +++++++++++++ apps/server/src/kin/memory/secretScan.ts | 173 +++++++++++++++++- .../src/kin/persistence/KinMigrations.test.ts | 7 + .../src/kin/persistence/KinMigrations.ts | 3 + .../migrations/K012_SecretAllow.ts | 14 ++ docs/kin/specs/triggers.md | 6 +- 9 files changed, 381 insertions(+), 18 deletions(-) create mode 100644 apps/server/src/kin/memory/secretScan.test.ts create mode 100644 apps/server/src/kin/persistence/migrations/K012_SecretAllow.ts diff --git a/apps/server/src/kin/memory/Memory.test.ts b/apps/server/src/kin/memory/Memory.test.ts index 10f4c07c8815b..d3d23ba9258c1 100644 --- a/apps/server/src/kin/memory/Memory.test.ts +++ b/apps/server/src/kin/memory/Memory.test.ts @@ -861,6 +861,7 @@ it.live("uses filesystem identity for a case-mismatched base without recloning", it.live.each([ "diverged", + "secret merge", "conflict", "network down", "dirty writer", @@ -955,13 +956,15 @@ it.live.each([ } yield* memory.record(orgA, "main", initial.head, local); yield* fs.writeFileString( - `${source}/${scenario === "push retry" || scenario === "diverged" || scenario === "network down" || scenario === "dirty writer" || scenario === "H2-merge-swap" ? "remote-only" : "rules.md"}`, + `${source}/${scenario === "push retry" || scenario === "diverged" || scenario === "secret merge" || scenario === "network down" || scenario === "dirty writer" || scenario === "H2-merge-swap" ? "remote-only" : "rules.md"}`, "Remote invented edit\n", ); if (scenario === "journal conflict" || scenario === "push retry") { yield* fs.writeFileString(`${source}/journal/day.md`, "remote entry\n"); yield* fs.writeFileString(`${source}/INDEX.md`, "remote generated index\n"); } + if (scenario === "secret merge") + yield* fs.writeFileString(`${source}/remote-only`, ["gh", "p_", "a".repeat(36)].join("")); if (scenario === "H2-merge-swap") yield* fs.writeFileString(`${source}/rules.md`, h2Base.replace("base tail", "remote tail")); yield* commit(source); @@ -1041,6 +1044,16 @@ it.live.each([ expect( (yield* memory.prepareWakeup(orgA, "fixture-wakeup-3").pipe(Effect.result))._tag, ).toBe("Success"); + } else if (scenario === "secret merge") { + expect(result._tag).toBe("Failure"); + if (result._tag === "Failure") { + expect(result.failure._tag).toBe("MemoryError"); + expect(result.failure.operation).toBe("secretScan"); + } + expect(yield* memory.anchor(orgA, "main")).toBe(local); + expect(yield* git(root, [`--git-dir=${remote}`, "rev-parse", "main"])).toBe( + yield* git(source, ["rev-parse", "HEAD"]), + ); } else if (scenario === "push retry") { expect(result._tag).toBe("Success"); if (result._tag === "Success") { @@ -1066,6 +1079,7 @@ it.live.each([ } else if ( scenario === "network down" || scenario === "diverged" || + scenario === "secret merge" || scenario === "dirty writer" ) { expect(result._tag).toBe("Success"); diff --git a/apps/server/src/kin/memory/Memory.ts b/apps/server/src/kin/memory/Memory.ts index b670a28d1daca..1e091006c7925 100644 --- a/apps/server/src/kin/memory/Memory.ts +++ b/apps/server/src/kin/memory/Memory.ts @@ -1,3 +1,4 @@ +import * as SecretScan from "./secretScan.ts"; import { readTree, readObjects, @@ -203,6 +204,7 @@ interface BlobCache { } const make = Effect.gen(function* () { + const scanner = yield* SecretScan.KinSecretScan; const config = yield* MemoryConfig; const fs = yield* FileSystem.FileSystem; const { notify } = yield* MemoryRecoveryHooks; @@ -1085,6 +1087,28 @@ const make = Effect.gen(function* () { "INDEX.md", Buffer.from(buildIndex(heads, reads, yield* Clock.currentTimeMillis, zone)), ); + const findings = yield* scanner + .scan({ + orgId, + files: [...remoteFiles].map(([path, content]) => ({ path, content })), + }) + .pipe( + Effect.mapError( + (cause) => new MemoryError({ orgId, operation: "secretScan", cause }), + ), + ); + if (findings.length) { + yield* notify({ + orgId, + branch, + reason: `Secret scan blocked merge: ${findings[0]!.ruleId} in ${findings[0]!.path}`, + }); + return yield* new MemoryError({ + orgId, + operation: "secretScan", + reason: `Secret scan blocked merge: ${findings[0]!.ruleId} in ${findings[0]!.path}`, + }); + } tree = yield* Effect.scoped( Effect.gen(function* () { const temp = yield* io(orgId, "journalMerge", fs.makeTempDirectoryScoped()); @@ -1273,4 +1297,7 @@ const make = Effect.gen(function* () { }); }); -export const layer = Layer.effect(Memory, make).pipe(Layer.provide(MemoryCache.layer)); +export const layer = Layer.effect(Memory, make).pipe( + Layer.provide(MemoryCache.layer), + Layer.provide(SecretScan.layer), +); diff --git a/apps/server/src/kin/memory/MemoryWriter.test.ts b/apps/server/src/kin/memory/MemoryWriter.test.ts index 194e65512ca73..9db49d2440eb4 100644 --- a/apps/server/src/kin/memory/MemoryWriter.test.ts +++ b/apps/server/src/kin/memory/MemoryWriter.test.ts @@ -222,10 +222,7 @@ it.live.each([ }), ), }); - const scan = - scenario === "blocked" - ? Scan.testLayer([{ path: "claims/new.md", ruleId: "fixture", line: 1 }]) - : Scan.noFindingsStubLayer; + const scan = Scan.layer; const layer = Writer.layer.pipe( Layer.provideMerge(memoryLayer), Layer.provide(Layer.succeed(Memory.MemoryRecoveryHooks, { notify: () => Effect.void })), @@ -585,6 +582,10 @@ it.live.each([ } if (scenario === "tree-substitution") substituteTree = yield* git(paths.writer, ["rev-parse", `${original}^{tree}`]); + if (scenario === "blocked") + yield* writer.appendJournal(orgId, "wake1", [ + ["gh", "p_"].join("") + "aB3dE6gH9jK2mN5pQ8sT1vW4yZ7cF0iL3oR6", + ]); const result = yield* writer .commitWakeup(orgId, "wake1", scenario === "failed" ? "failed" : "completed") .pipe(Effect.result); @@ -603,6 +604,19 @@ it.live.each([ ); expect(notices).toHaveLength(1); + if (scenario === "blocked") { + expect(JSON.stringify(notices)).not.toContain("aB3dE6gH9jK2mN5pQ8sT1vW4yZ7cF0iL3oR6"); + expect(yield* git(paths.writer, ["rev-parse", "HEAD"])).toBe(original); + expect( + (yield* git(root, [ + `--git-dir=${remote}`, + "show-ref", + "--verify", + `refs/heads/kin/shadow/${agentId}`, + ]).pipe(Effect.result))._tag, + ).toBe("Failure"); + expect(yield* fs.exists(`${paths.writer}/claims/new.md`)).toBe(false); + } expect(yield* fs.exists(`${paths.writer}/.git/rebase-merge`)).toBe(false); } else { expect(result._tag).toBe("Success"); diff --git a/apps/server/src/kin/memory/secretScan.test.ts b/apps/server/src/kin/memory/secretScan.test.ts new file mode 100644 index 0000000000000..2a52acb8a3b77 --- /dev/null +++ b/apps/server/src/kin/memory/secretScan.test.ts @@ -0,0 +1,129 @@ +// @effect-diagnostics-next-line nodeBuiltinImport:off -- Bounded synchronous fingerprint hashing shares the scan loop. +import * as NodeCrypto from "node:crypto"; +import { expect, it } from "@effect/vitest"; +import { KinOrgId } from "@t3tools/contracts"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import * as SqlClient from "effect/sql/SqlClient"; +import * as Sqlite from "../../persistence/Sqlite.ts"; +import * as Persistence from "../persistence/KinPersistence.ts"; +import * as Scan from "./secretScan.ts"; + +const orgId = KinOrgId.make("fixture-org"); +const database = Layer.provideMerge(Persistence.layer, Sqlite.layerMemory); +const layer = Scan.layer.pipe(Layer.provideMerge(database)); +const body = "aB3dE6gH9jK2mN5pQ8sT1vW4yZ7cF0iL3oR6"; +const github = ["gh", "p_"].join("") + body; +const fixtures = [ + ...["p", "o", "u", "s", "r"].map((kind) => [ + "github-token", + "gh" + kind + "_" + body, + "gh" + kind + "_short", + ]), + [ + "github-token", + ["github_", "pat_"].join("") + "a".repeat(82), + ["github_", "pat_short"].join(""), + ], + ["anthropic-key", ["sk-", "ant-"].join("") + body.repeat(2), ["sk-", "ant-short"].join("")], + ["openai-key", "sk-" + body, "sk-short"], + ["openai-key", ["sk-", "proj-"].join("") + body.repeat(3), "sk-proj-short"], + ...["AK", "AS"].map((kind) => [ + "aws-access-key", + kind + "IA" + "X".repeat(16), + kind + "IA" + "X".repeat(15), + ]), + ["slack-token", ["xo", "xb-"].join("") + body, ["xo", "xb-short"].join("")], + ["stripe-key", ["sk_", "live_"].join("") + body, "sk_live_short"], + ["google-key", ["AI", "za"].join("") + "X".repeat(35), ["AI", "za_short"].join("")], + ["pem-private-key", "-----BEGIN RSA PRIVATE KEY-----", "-----BEGIN PUBLIC KEY-----"], + [ + "jwt", + ["ey", "J"].join("") + "a".repeat(10) + "." + "b".repeat(12) + "." + "c".repeat(20), + "eyJshort.two.parts", + ], + [ + "url-password", + "https://fixture:fake-password@example.invalid", + "https://fixture@example.invalid", + ], + ["secret-assignment", "password=" + body, "password=" + "a".repeat(40)], + ["secret-assignment", "export API_KEY='" + body + "'", "export API_KEY='short'"], + ["secret-assignment", "AWS_SECRET_ACCESS_KEY=" + body + "aB3d", "AWS_SECRET_ACCESS_KEY=example"], +]; +it.effect.each(fixtures.map(([rule, hit, miss], i) => ({ rule, hit, miss, id: i })))( + "rule fixture $id: $rule", + ({ rule, hit, miss }) => + Effect.gen(function* () { + const scanner = yield* Scan.KinSecretScan; + const run = (content: string) => + scanner.scan({ orgId, files: [{ path: "claims/test.md", content: Buffer.from(content) }] }); + // Assert only metadata so a failed assertion never prints the synthetic matched token. + expect((yield* run(hit!)).some((finding) => finding.ruleId === rule)).toBe(true); + expect((yield* run(miss!)).length).toBe(0); + }).pipe(Effect.provide(layer)), +); +it.effect("blocks encoded, wrapped and confusable tokens with safe findings", () => + Effect.gen(function* () { + const scanner = yield* Scan.KinSecretScan; + const values = [ + github, + Buffer.from(github).toString("base64"), + Buffer.from(Buffer.from(github).toString("base64")).toString("base64"), + [...github].map((c) => `%${c.charCodeAt(0).toString(16)}`).join(""), + github.slice(0, 12) + "\n" + github.slice(12), + github.slice(0, 12) + "\\\n " + github.slice(12), + github.replace("p", "р"), + [...github].map((c) => String.fromCharCode(c.charCodeAt(0) + 0xfee0)).join(""), + github.slice(0, 5) + "\u200b" + github.slice(5), + ]; + for (const content of values) { + const result = yield* scanner.scan({ + orgId, + files: [{ path: "journal/test.md", content: Buffer.from(content) }], + }); + expect(result.some((f) => f.ruleId === "github-token")).toBe(true); + expect(JSON.stringify(result).includes(body)).toBe(false); + expect(Object.keys(result[0]!).sort()).toEqual(["line", "path", "ruleId"]); + } + }).pipe(Effect.provide(layer)), +); +it.effect("allowlist is exact, org scoped, and read only to scanner callers", () => + Effect.gen(function* () { + const scanner = yield* Scan.KinSecretScan; + const sql = yield* SqlClient.SqlClient; + const hash = NodeCrypto.createHash("sha256") + .update("github-token\0") + .update(github) + .digest("hex"); + // Fixture stands in for the future APPR owner-signed executor; scanner has no write method. + yield* sql`INSERT INTO kin_secret_allow VALUES (${orgId}, 'claims/test.md', 'github-token', ${hash}, 'signed-fixture')`; + const run = (org: KinOrgId, path: string, value = github) => + scanner.scan({ orgId: org, files: [{ path, content: Buffer.from(value) }] }); + expect((yield* run(orgId, "claims/test.md")).length).toBe(0); + expect((yield* run(KinOrgId.make("other"), "claims/test.md")).length).toBeGreaterThan(0); + expect((yield* run(orgId, "claims/other.md")).length).toBeGreaterThan(0); + expect((yield* run(orgId, "claims/test.md", github.replace("a", "z"))).length).toBeGreaterThan( + 0, + ); + expect(Object.keys(scanner)).toEqual(["scan"]); + yield* sql`DROP TABLE kin_secret_allow`; + expect((yield* run(orgId, "claims/test.md").pipe(Effect.result))._tag).toBe("Failure"); + }).pipe(Effect.provide(layer)), +); +it.effect("fails closed at file, batch and count limits", () => + Effect.gen(function* () { + const scanner = yield* Scan.KinSecretScan; + const scan = (files: { path: string; content: Uint8Array }[]) => scanner.scan({ orgId, files }); + for (const files of [ + [{ path: "huge", content: Buffer.alloc(1024 * 1024 + 1) }], + Array.from({ length: 9 }, () => ({ path: "batch", content: Buffer.alloc(1024 * 1024) })), + Array.from({ length: 1025 }, () => ({ path: "count", content: Buffer.alloc(0) })), + ]) + expect((yield* scan(files).pipe(Effect.result))._tag).toBe("Failure"); + const content = Buffer.from("a".repeat(1024 * 1024 - github.length - 1) + "\n" + github); + expect( + (yield* scan([{ path: "large", content }])).some((f) => f.ruleId === "github-token"), + ).toBe(true); + }).pipe(Effect.provide(layer)), +); diff --git a/apps/server/src/kin/memory/secretScan.ts b/apps/server/src/kin/memory/secretScan.ts index f7ef2760efa17..ca0e84b88adba 100644 --- a/apps/server/src/kin/memory/secretScan.ts +++ b/apps/server/src/kin/memory/secretScan.ts @@ -1,3 +1,6 @@ +// @effect-diagnostics-next-line nodeBuiltinImport:off -- Bounded synchronous fingerprint hashing shares the scan loop. +import * as NodeCrypto from "node:crypto"; +import * as SqlClient from "effect/sql/SqlClient"; import type { KinOrgId } from "@t3tools/contracts"; import * as Context from "effect/Context"; import * as Effect from "effect/Effect"; @@ -25,14 +28,164 @@ export class KinSecretScan extends Context.Service< } >()("t3/kin/memory/secretScan/KinSecretScan") {} -export const noFindingsStubLayer = Layer.effect( - KinSecretScan, - Effect.gen(function* () { - yield* Effect.logWarning("SECURITY: Kin secret scanning is disabled (no-findings stub)"); - return KinSecretScan.of({ scan: Effect.fn("KinSecretScan.stub")(() => Effect.succeed([])) }); - }), -); -export const testLayer = (findings: ReadonlyArray) => - Layer.succeed(KinSecretScan, { - scan: Effect.fn("KinSecretScan.fixture")(() => Effect.succeed(findings)), +const rules = [ + ["github-token", /\b(?:gh[pousr]_[A-Za-z0-9]{36}|github_pat_[A-Za-z0-9_]{82})\b/], + ["anthropic-key", /\bsk-ant-[A-Za-z0-9_-]{40,200}\b/], + ["openai-key", /\bsk-(?!ant-)(?:proj-)?[A-Za-z0-9_-]{32,200}\b/], + ["aws-access-key", /\b(?:AKIA|ASIA)[A-Z0-9]{16}\b/], + ["slack-token", /\bxox[abposr]-[A-Za-z0-9-]{20,200}\b/], + ["stripe-key", /\b[rs]k_live_[A-Za-z0-9]{24,200}\b/], + ["google-key", /\bAIza[A-Za-z0-9_-]{35}\b/], + ["pem-private-key", /-----BEGIN (?:(?:RSA|EC|DSA|OPENSSH|ENCRYPTED) )?PRIVATE KEY-----/], + ["jwt", /\beyJ[A-Za-z0-9_-]{5,8192}\.[A-Za-z0-9_-]{8,8192}\.[A-Za-z0-9_-]{16,8192}\b/], + ["url-password", /\b[a-z][a-z0-9+.-]{0,31}:\/\/[^\s/:@]{1,512}:([^\s/@]{1,512})@/i], + [ + "secret-assignment", + /\b[A-Za-z0-9_]{0,64}(?:password|secret|token|api[_-]?key|access[_-]?key)[A-Za-z0-9_]{0,64}\s*[:=]\s*["']?([^\s"';#]{20,512})/i, + ], +] as const; +const entropy = (value: string) => { + const counts = new Map(); + for (const char of value) counts.set(char, (counts.get(char) ?? 0) + 1); + let bits = 0; + for (const count of counts.values()) { + const p = count / value.length; + bits -= p * Math.log2(p); + } + return bits; +}; +// NFKC covers fullwidth forms; common Cyrillic/Greek confusables are normalized as well. +const lookalikes: Readonly> = { + а: "a", + е: "e", + о: "o", + р: "p", + с: "c", + х: "x", + у: "y", + і: "i", + ј: "j", + А: "A", + В: "B", + Е: "E", + К: "K", + М: "M", + Н: "H", + О: "O", + Р: "P", + С: "C", + Т: "T", + Х: "X", + Α: "A", + Β: "B", + Ε: "E", + Ι: "I", + Κ: "K", + Μ: "M", + Ν: "N", + Ο: "O", + Ρ: "P", + Τ: "T", + Χ: "X", +}; +const normalize = (text: string) => + text + .normalize("NFKC") + .replace(/[\u200B-\u200F\u202A-\u202E\u2060-\u206F\uFEFF\uFFFD]/g, "") + .replace(/[аеорсхуіјАВЕКМНОРСТХΑΒΕΙΚΜΝΟΡΤΧ]/g, (c) => lookalikes[c] ?? c); +const fingerprint = (ruleId: string, value: string) => + NodeCrypto.createHash("sha256").update(ruleId).update("\0").update(value).digest("hex"); +const make = Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + const scan = Effect.fn("KinSecretScan.scan")(function* (input: { + orgId: KinOrgId; + files: ReadonlyArray<{ path: string; content: Uint8Array }>; + }) { + if ( + input.files.length > 1024 || + input.files.some((f) => f.content.byteLength > 1024 * 1024) || + input.files.reduce((sum, f) => sum + f.content.byteLength, 0) > 8 * 1024 * 1024 + ) + return yield* new KinSecretScanError({ reason: "Scan size limit exceeded" }); + // No mutation API: the owner-signed action executor owns allowlist insertion. + const allowed = yield* sql<{ path: string; rule_id: string; fingerprint: string }>` + SELECT path, rule_id, fingerprint FROM kin_secret_allow WHERE org_id = ${input.orgId} LIMIT 4097 + `.pipe(Effect.mapError(() => new KinSecretScanError({ reason: "Allowlist unavailable" }))); + if (allowed.length > 4096) + return yield* new KinSecretScanError({ reason: "Allowlist size limit exceeded" }); + const allow = new Set(allowed.map((r) => JSON.stringify([r.path, r.rule_id, r.fingerprint]))); + const findings: SecretFinding[] = []; + const seen = new Set(); + let budget = 32 * 1024 * 1024; + for (const file of input.files) { + const text = normalize(Buffer.from(file.content).toString("utf8")); + const inspect = ( + value: string, + baseLine: number, + depth: number, + compact = false, + lineAt?: Uint32Array, + ): void => { + budget -= value.length; + if (budget < 0 || findings.length > 1024) return; + for (const [ruleId, pattern] of rules) { + for (const match of value.matchAll(new RegExp(pattern.source, `${pattern.flags}g`))) { + const secret = match[1] ?? match[0]; + if (ruleId === "secret-assignment" && entropy(secret) < 3.5) continue; + if (allow.has(JSON.stringify([file.path, ruleId, fingerprint(ruleId, secret)]))) + continue; + const line = + lineAt?.[match.index] ?? + baseLine + (value.slice(0, match.index).match(/\n/g)?.length ?? 0); + const key = JSON.stringify([file.path, ruleId, line]); + if (!seen.has(key)) { + seen.add(key); + findings.push({ path: file.path, ruleId, line }); + } + break; + } + } + if (depth === 2 || compact) return; + let cursor = 0; + let decodedLine = baseLine; + for (const match of value.matchAll(/(?:%[0-9a-fA-F]{2}|[A-Za-z0-9_+/-]){24,}={0,2}/g)) { + decodedLine += value.slice(cursor, match.index).match(/\n/g)?.length ?? 0; + cursor = match.index; + const encoded = match[0]; + let decoded: string; + if (encoded.includes("%")) { + try { + decoded = decodeURIComponent(encoded); + } catch { + continue; + } + } else { + decoded = Buffer.from(encoded, "base64url").toString("utf8"); + if (!/^[\x09\x0a\x0d\x20-\x7e]+$/.test(decoded)) continue; + } + if (decoded === encoded) continue; + inspect(normalize(decoded), decodedLine, depth + 1); + } + }; + inspect(text, 1, 0); + const lineAt = new Uint32Array(text.length); + let line = 1; + let offset = 0; + const compact = text.replace(/\\?\r?\n[\t ]*|[^\r\n\\]+|./g, (chunk) => { + if (chunk.includes("\n")) { + line++; + return ""; + } + lineAt.fill(line, offset, offset + chunk.length); + offset += chunk.length; + return chunk; + }); + inspect(compact, 1, 2, true, lineAt); + } + if (budget < 0 || findings.length > 1024) + return yield* new KinSecretScanError({ reason: "Scan work limit exceeded" }); + return findings; }); + return KinSecretScan.of({ scan }); +}); +export const layer = Layer.effect(KinSecretScan, make); diff --git a/apps/server/src/kin/persistence/KinMigrations.test.ts b/apps/server/src/kin/persistence/KinMigrations.test.ts index 4b855164bf64d..467d14d994cd8 100644 --- a/apps/server/src/kin/persistence/KinMigrations.test.ts +++ b/apps/server/src/kin/persistence/KinMigrations.test.ts @@ -36,6 +36,7 @@ const tableNames = [ "kin_ping_items", "kin_ping_ledger", "kin_read_state", + "kin_secret_allow", "kin_session_bindings", "kin_shadow_entries", "kin_trigger_fires", @@ -98,6 +99,7 @@ describe("Kin migrations", () => { { migration_id: 9, name: "JudgeLog" }, { migration_id: 10, name: "ChatChanges" }, { migration_id: 11, name: "MemoryHeads" }, + { migration_id: 12, name: "SecretAllow" }, ]); assert.deepStrictEqual( upstreamBefore.map((row) => [row.migration_id, row.name] as const), @@ -151,6 +153,7 @@ describe("Kin migrations", () => { { migration_id: 9, name: "JudgeLog" }, { migration_id: 10, name: "ChatChanges" }, { migration_id: 11, name: "MemoryHeads" }, + { migration_id: 12, name: "SecretAllow" }, ]); assert.deepStrictEqual(yield* upstreamHistory, upstreamBefore); assert.deepStrictEqual(yield* sql`SELECT * FROM kin_orgs`, [ @@ -229,6 +232,7 @@ describe("Kin migrations", () => { [9, "JudgeLog"], [10, "ChatChanges"], [11, "MemoryHeads"], + [12, "SecretAllow"], ]); const before = yield* kinSchema; assert.deepStrictEqual( @@ -266,6 +270,7 @@ describe("Kin migrations", () => { yield* sql`DROP TABLE kin_ping_items`; yield* sql`DROP INDEX kin_chat_members_one_owner`; yield* sql`DROP TABLE kin_memory_heads`; + yield* sql`DROP TABLE kin_secret_allow`; yield* sql`DROP TABLE kin_budget_charges`; yield* sql`ALTER TABLE kin_events DROP COLUMN budget_deferred`; yield* sql`UPDATE kin_chats SET last_seq = 1 WHERE org_id = 'org' AND chat_id = 'existing-chat'`; @@ -280,6 +285,7 @@ describe("Kin migrations", () => { [9, "JudgeLog"], [10, "ChatChanges"], [11, "MemoryHeads"], + [12, "SecretAllow"], ]); assert.deepStrictEqual( yield* sql`SELECT member_id FROM kin_chat_members WHERE org_id = 'org' ORDER BY member_id`, @@ -316,6 +322,7 @@ describe("Kin migrations", () => { { migration_id: 9, name: "JudgeLog" }, { migration_id: 10, name: "ChatChanges" }, { migration_id: 11, name: "MemoryHeads" }, + { migration_id: 12, name: "SecretAllow" }, ]); 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 b7bba32965691..2a407ca6d5f7b 100644 --- a/apps/server/src/kin/persistence/KinMigrations.ts +++ b/apps/server/src/kin/persistence/KinMigrations.ts @@ -15,6 +15,8 @@ import JudgeLog from "./migrations/K009_JudgeLog.ts"; import ChatChanges from "./migrations/K010_ChatChanges.ts"; import MemoryHeads from "./migrations/K011_MemoryHeads.ts"; +import SecretAllow from "./migrations/K012_SecretAllow.ts"; + const run = Migrator.make({}); const loader = Migrator.fromRecord({ "1_ChatCore": ChatCore, @@ -28,6 +30,7 @@ const loader = Migrator.fromRecord({ "9_JudgeLog": JudgeLog, "10_ChatChanges": ChatChanges, "11_MemoryHeads": MemoryHeads, + "12_SecretAllow": SecretAllow, }); export const runKinMigrations = Effect.fn("KinMigrations.run")(function* () { diff --git a/apps/server/src/kin/persistence/migrations/K012_SecretAllow.ts b/apps/server/src/kin/persistence/migrations/K012_SecretAllow.ts new file mode 100644 index 0000000000000..fec65afd99e4a --- /dev/null +++ b/apps/server/src/kin/persistence/migrations/K012_SecretAllow.ts @@ -0,0 +1,14 @@ +import * as Effect from "effect/Effect"; +import * as SqlClient from "effect/sql/SqlClient"; + +export default Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + yield* sql`CREATE TABLE kin_secret_allow ( + org_id TEXT NOT NULL, + path TEXT NOT NULL, + rule_id TEXT NOT NULL, + fingerprint TEXT NOT NULL CHECK(length(fingerprint) = 64), + signed_action_id TEXT NOT NULL, + PRIMARY KEY (org_id, path, rule_id, fingerprint) + )`; +}); diff --git a/docs/kin/specs/triggers.md b/docs/kin/specs/triggers.md index 1890e5a05a9df..a2373fcaf6b38 100644 --- a/docs/kin/specs/triggers.md +++ b/docs/kin/specs/triggers.md @@ -342,7 +342,9 @@ GitHub then blocks a bad auto-merge even if Kin's classifier is wrong. This forc **On a finding**, the writer blocks the entire commit, discards staged text and emits a typed owner-notification hook. The runner journals the refusal without secret content and tells the owner to review and rotate the affected credential. Automatic redaction is not part of the writer contract. -**False positives:** the owner adds the fingerprint to `kin_secret_allow` through a signed action. A model can never add to it. +**False positives:** the owner adds the fingerprint to `kin_secret_allow` through a signed action. A model can never add to it. Entries are scoped to organization, exact path, rule and SHA-256 of `ruleId + "\0" + normalized matched value`, with the signed action id retained. The scanner is read-only; the APPR executor must verify the owner grant before inserting. Until that executor lands, there is no agent or MCP allowlist-write path. Migration K012 is provisional and will be renumbered at merge. + +The scanner normalizes NFKC, common Cyrillic/Greek lookalikes and invisible format characters, checks wrapped tokens, and decodes URL/base64 content up to two levels. It fails closed above 1 MiB per file, 8 MiB or 1024 files per call, 4096 allowlist entries per organization, or 32 MiB of transformed scan text. These are conservative detectors, not a guarantee against arbitrary encryption or steganography. ### Owner-signed purge (gap 5) @@ -475,7 +477,7 @@ Kin runs on one always-on machine, the **Kin host**. The laptop and phone are cl - **Sink:** a signed GitHub `pull_request` request through the real `ScheduledTaskService` gives exactly one inbox row and no thread launch. A replay gives `duplicate` and no new row. A bot-sender request gives `filtered`. A non-Kin task still launches normally. - **Deferral:** unchanged recheck, then heads-up, then silence, then `started` at due. A linked issue closed before due leads to `asked`, and nothing starts. With the stop switch on, it asks. [Monday] re-arms to next Monday 09:00. - **Dream:** a dream touching `rules.md` fails, and `main` is unchanged. A dream with one journal fold and one `decisions/` change opens two PRs, and only the auto one merges. -- **Secret scan:** a journal line holding `ghp_` plus 36 characters is committed as `[redacted:github-token]`, and the owner gets one message. +- **Secret scan:** a journal line holding `ghp_` plus 36 characters blocks the entire commit, and the owner gets one typed notification without the value. - **Purge:** on a fixture repo, the file is gone from `git log --all -p`, a tombstone exists, and an audit row exists. - **Contract tests:** `vp test run kin-contract` passes on today's upstream. Renaming `scheduledTaskId` in a scratch branch makes it fail. From e7aba15d84f7ba0438e3458b8dd40d2ebe4b2c63 Mon Sep 17 00:00:00 2001 From: GLDRoger <130458023+GLDRoger@users.noreply.github.com> Date: Sun, 8 Nov 2026 13:19:11 +0530 Subject: [PATCH 02/15] test(kin): harden scanner bounds and wrapped-token coverage --- apps/server/src/kin/memory/secretScan.test.ts | 43 +++++++++++++++++++ apps/server/src/kin/memory/secretScan.ts | 23 +++++----- docs/kin/specs/lead.md | 4 +- 3 files changed, 56 insertions(+), 14 deletions(-) diff --git a/apps/server/src/kin/memory/secretScan.test.ts b/apps/server/src/kin/memory/secretScan.test.ts index 2a52acb8a3b77..db74dd37b5f6d 100644 --- a/apps/server/src/kin/memory/secretScan.test.ts +++ b/apps/server/src/kin/memory/secretScan.test.ts @@ -88,6 +88,32 @@ it.effect("blocks encoded, wrapped and confusable tokens with safe findings", () } }).pipe(Effect.provide(layer)), ); +it.effect("reports source lines and blocks AWS credential pairs without leaking values", () => + Effect.gen(function* () { + const scanner = yield* Scan.KinSecretScan; + for (const content of [ + "safe\n\n" + github, + "safe\n\n" + github.slice(0, 12) + "\n" + github.slice(12), + ]) { + const result = yield* scanner.scan({ + orgId, + files: [{ path: "claims/test.md", content: Buffer.from(content) }], + }); + expect(result.every((f) => f.line === 3)).toBe(true); + expect(result.length).toBeGreaterThan(0); + } + const pair = [ + "AWS_ACCESS_KEY_ID=" + ["AK", "IA", "X".repeat(16)].join(""), + "AWS_SECRET_ACCESS_KEY=" + body + "aB3d", + ].join("\n"); + const findings = yield* scanner.scan({ + orgId, + files: [{ path: ".env", content: Buffer.from(pair) }], + }); + expect(findings.some((f) => f.ruleId === "aws-access-key")).toBe(true); + expect(findings.some((f) => f.ruleId === "secret-assignment")).toBe(true); + }).pipe(Effect.provide(layer)), +); it.effect("allowlist is exact, org scoped, and read only to scanner callers", () => Effect.gen(function* () { const scanner = yield* Scan.KinSecretScan; @@ -111,6 +137,23 @@ it.effect("allowlist is exact, org scoped, and read only to scanner callers", () expect((yield* run(orgId, "claims/test.md").pipe(Effect.result))._tag).toBe("Failure"); }).pipe(Effect.provide(layer)), ); +it.effect("fails closed when decoded findings or the allowlist exceed their bounds", () => + Effect.gen(function* () { + const scanner = yield* Scan.KinSecretScan; + const content = Buffer.from( + Array.from({ length: 1100 }, () => Buffer.from(github).toString("base64")).join("\n"), + ); + expect( + (yield* scanner + .scan({ orgId, files: [{ path: "claims/many.md", content }] }) + .pipe(Effect.result))._tag, + ).toBe("Failure"); + const sql = yield* SqlClient.SqlClient; + yield* sql`WITH RECURSIVE entries(n) AS (SELECT 1 UNION ALL SELECT n + 1 FROM entries WHERE n < 4097) + INSERT INTO kin_secret_allow SELECT ${orgId}, 'claims/' || n, 'github-token', ${"0".repeat(64)}, 'signed-fixture' FROM entries`; + expect((yield* scanner.scan({ orgId, files: [] }).pipe(Effect.result))._tag).toBe("Failure"); + }).pipe(Effect.provide(layer)), +); it.effect("fails closed at file, batch and count limits", () => Effect.gen(function* () { const scanner = yield* Scan.KinSecretScan; diff --git a/apps/server/src/kin/memory/secretScan.ts b/apps/server/src/kin/memory/secretScan.ts index ca0e84b88adba..3f5e0a4430121 100644 --- a/apps/server/src/kin/memory/secretScan.ts +++ b/apps/server/src/kin/memory/secretScan.ts @@ -29,15 +29,15 @@ export class KinSecretScan extends Context.Service< >()("t3/kin/memory/secretScan/KinSecretScan") {} const rules = [ - ["github-token", /\b(?:gh[pousr]_[A-Za-z0-9]{36}|github_pat_[A-Za-z0-9_]{82})\b/], - ["anthropic-key", /\bsk-ant-[A-Za-z0-9_-]{40,200}\b/], - ["openai-key", /\bsk-(?!ant-)(?:proj-)?[A-Za-z0-9_-]{32,200}\b/], - ["aws-access-key", /\b(?:AKIA|ASIA)[A-Z0-9]{16}\b/], - ["slack-token", /\bxox[abposr]-[A-Za-z0-9-]{20,200}\b/], - ["stripe-key", /\b[rs]k_live_[A-Za-z0-9]{24,200}\b/], - ["google-key", /\bAIza[A-Za-z0-9_-]{35}\b/], + ["github-token", /(?:gh[pousr]_[A-Za-z0-9]{36}|github_pat_[A-Za-z0-9_]{82})/], + ["anthropic-key", /sk-ant-[A-Za-z0-9_-]{40,200}/], + ["openai-key", /sk-(?!ant-)(?:proj-)?[A-Za-z0-9_-]{32,200}/], + ["aws-access-key", /(?:AKIA|ASIA)[A-Z0-9]{16}/], + ["slack-token", /xox[abposr]-[A-Za-z0-9-]{20,200}/], + ["stripe-key", /[rs]k_live_[A-Za-z0-9]{24,200}/], + ["google-key", /AIza[A-Za-z0-9_-]{35}/], ["pem-private-key", /-----BEGIN (?:(?:RSA|EC|DSA|OPENSSH|ENCRYPTED) )?PRIVATE KEY-----/], - ["jwt", /\beyJ[A-Za-z0-9_-]{5,8192}\.[A-Za-z0-9_-]{8,8192}\.[A-Za-z0-9_-]{16,8192}\b/], + ["jwt", /eyJ[A-Za-z0-9_-]{5,8192}\.[A-Za-z0-9_-]{8,8192}\.[A-Za-z0-9_-]{16,8192}/], ["url-password", /\b[a-z][a-z0-9+.-]{0,31}:\/\/[^\s/:@]{1,512}:([^\s/@]{1,512})@/i], [ "secret-assignment", @@ -123,7 +123,6 @@ const make = Effect.gen(function* () { value: string, baseLine: number, depth: number, - compact = false, lineAt?: Uint32Array, ): void => { budget -= value.length; @@ -145,7 +144,7 @@ const make = Effect.gen(function* () { break; } } - if (depth === 2 || compact) return; + if (depth === 2) return; let cursor = 0; let decodedLine = baseLine; for (const match of value.matchAll(/(?:%[0-9a-fA-F]{2}|[A-Za-z0-9_+/-]){24,}={0,2}/g)) { @@ -164,7 +163,7 @@ const make = Effect.gen(function* () { if (!/^[\x09\x0a\x0d\x20-\x7e]+$/.test(decoded)) continue; } if (decoded === encoded) continue; - inspect(normalize(decoded), decodedLine, depth + 1); + inspect(normalize(decoded), lineAt?.[match.index] ?? decodedLine, depth + 1); } }; inspect(text, 1, 0); @@ -180,7 +179,7 @@ const make = Effect.gen(function* () { offset += chunk.length; return chunk; }); - inspect(compact, 1, 2, true, lineAt); + inspect(compact, 1, 0, lineAt); } if (budget < 0 || findings.length > 1024) return yield* new KinSecretScanError({ reason: "Scan work limit exceeded" }); diff --git a/docs/kin/specs/lead.md b/docs/kin/specs/lead.md index 867a2cd312369..134bc9b50aecb 100644 --- a/docs/kin/specs/lead.md +++ b/docs/kin/specs/lead.md @@ -600,7 +600,7 @@ LEAD-1. **Contracts and tables.** `kinLead.ts` schemas; Kin migrations for all l LEAD-2. **Memory read side.** `ensureCheckouts`, `prepareWakeup`, front matter parser, cache, `query`, `readItem`, `rebuildIndex` (200-line cap, "As of" line, fading after 8 weeks unread). Files: `apps/server/src/kin/memory/{Memory,MemoryCache,frontMatter}.ts`. Depends on: LEAD-1. Done when: tests create a temporary git repo from invented fixtures matching the kin-memory README; kind/date/review queries, malformed-item isolation, organization isolation, cache invalidation, index cap/As-of and TestClock fading all pass. Never use real organization memory as a fixture. Owner: backend. -LEAD-3. **Memory git writer.** `stage` with every writer rule, `commitWakeup`, push and rebase, shadow branch, the secret scan hook (stub until the triggers spec lands). Files: `MemoryWriter.ts`, `journal.ts`. Depends on: LEAD-2, TRIG-16 (stub allowed). Done when: tests show provenance override, `rules.md` refusal, conflict error, one commit per wake-up, discard on failure. Owner: backend. +LEAD-3. **Memory git writer.** `stage` with every writer rule, `commitWakeup`, push and rebase, shadow branch, the secret scan hook (TRIG-16 supplies the scanner layer). Files: `MemoryWriter.ts`, `journal.ts`. Depends on: LEAD-2, TRIG-16 (stub allowed). Done when: tests show provenance override, `rules.md` refusal, conflict error, one commit per wake-up, discard on failure. Owner: backend. LEAD-4. **Event queue and coalescer.** `enqueue`, dedupe, priorities, quiet window, claim, restart requeue, per-org semaphore. Files: `KinEventQueue.ts`. Depends on: LEAD-1. Done when: coalescing and one-writer tests pass with `TestClock`. Owner: backend. @@ -647,7 +647,7 @@ LEAD-14. **End-to-end shadow week dry run.** Script that replays a day of record `MemoryWriterAuthority.resolve` is runner-only: resolve organization, wake-up, active lead, level, timestamps, timezone and event provenance from same-organization rows inside the runner's transaction. Never expose this context as tool input. Owner-confirmed sources must be verified there; absent a source, provenance is the lead's agent id. `MemoryWriterHooks.notify` consumes checkout, conflict and secret-block notices without chat transport coupling. -`KinSecretScan` is deliberately supplied separately. `noFindingsStubLayer` returns no findings until TRIG-16 replaces it; it is not a production secret detector. Any finding or scanner failure blocks the whole commit. TRIG-16 must preserve this fail-closed interface: `scan({ orgId, files: ReadonlyArray<{ path: string; content: Uint8Array }> })` returns `{ path, ruleId, line }[]`, with typed `KinSecretScanError`. +`KinSecretScan` is deliberately supplied separately. `secretScan.layer` supplies the TRIG-16 scanner and reads the owner-signed allowlist; there is no no-findings production stub. Any finding or scanner failure blocks the whole commit. TRIG-16 must preserve this fail-closed interface: `scan({ orgId, files: ReadonlyArray<{ path: string; content: Uint8Array }> })` returns `{ path, ruleId, line }[]`, with typed `KinSecretScanError`. `commitWakeup` builds TRIG-12's sectioned `INDEX.md` from the hash-verified recorded tree plus staged files, read history, the server clock and the owner's time zone, in the same wake-up commit. The read-side rebuild is display-only; no read-checkout text is trusted. K011 adds the SQLite branch anchors required by the writer. The runner serializes prepare/stage/commit and dreaming per organization. Identical commit retries find the existing wake-up commit; failed pushes retain local commits, and failures discard uncommitted files. Journal additions use `appendJournal`, never replacement through `stage`. From 3954bbd006ccab66d83e6126047646912d50d36a Mon Sep 17 00:00:00 2001 From: GLDRoger <130458023+GLDRoger@users.noreply.github.com> Date: Sun, 8 Nov 2026 13:32:54 +0530 Subject: [PATCH 03/15] feat(kin): schedule brief, summary and owner-check routines --- apps/server/src/kin/KinServer.ts | 2 +- apps/server/src/kin/chat/KinChatService.ts | 16 +- .../server/src/kin/lead/KinEventQueue.test.ts | 3 +- apps/server/src/kin/lead/KinEventQueue.ts | 3 + apps/server/src/kin/lead/WakeupRunner.test.ts | 33 ++ apps/server/src/kin/lead/WakeupRunner.ts | 30 +- apps/server/src/kin/lead/layer.test.ts | 18 +- apps/server/src/kin/lead/layer.ts | 58 ++- .../src/kin/memory/MemoryWriter.test.ts | 35 +- apps/server/src/kin/memory/MemoryWriter.ts | 1 + .../src/kin/persistence/KinMigrations.test.ts | 9 + .../src/kin/persistence/KinMigrations.ts | 3 + .../persistence/migrations/K012_Routines.ts | 21 + .../src/kin/triggers/KinTriggerInbox.ts | 27 +- .../src/kin/triggers/ownerCheck.test.ts | 198 ++++++++++ apps/server/src/kin/triggers/ownerCheck.ts | 314 +++++++++++++++ apps/server/src/kin/triggers/routines.test.ts | 170 +++++++++ .../src/kin/triggers/routines.testkit.ts | 95 +++++ apps/server/src/kin/triggers/routines.ts | 358 ++++++++++++++++++ apps/server/src/kin/widgets/registry.ts | 7 +- docs/kin/specs/lead.md | 6 +- docs/kin/specs/triggers.md | 4 + packages/contracts/src/kinLead.ts | 29 ++ 23 files changed, 1417 insertions(+), 23 deletions(-) create mode 100644 apps/server/src/kin/persistence/migrations/K012_Routines.ts create mode 100644 apps/server/src/kin/triggers/ownerCheck.test.ts create mode 100644 apps/server/src/kin/triggers/ownerCheck.ts create mode 100644 apps/server/src/kin/triggers/routines.test.ts create mode 100644 apps/server/src/kin/triggers/routines.testkit.ts create mode 100644 apps/server/src/kin/triggers/routines.ts diff --git a/apps/server/src/kin/KinServer.ts b/apps/server/src/kin/KinServer.ts index 33bc02a3f5fef..d61dcc1cc5031 100644 --- a/apps/server/src/kin/KinServer.ts +++ b/apps/server/src/kin/KinServer.ts @@ -8,7 +8,7 @@ import { KinChatHub } from "./chat/KinChatHub.ts"; import { KinBudget } from "./lead/KinBudget.ts"; import { KinEventQueue } from "./lead/KinEventQueue.ts"; -export type Services = KinIdentity | KinChatService | KinBudget | KinEventQueue; +export type Services = KinIdentity | KinChatService | KinBudget | KinEventQueue | KinChatHub; export class KinServer extends Context.Service< KinServer, diff --git a/apps/server/src/kin/chat/KinChatService.ts b/apps/server/src/kin/chat/KinChatService.ts index 03cb68cf5dcfc..befd759dbcaab 100644 --- a/apps/server/src/kin/chat/KinChatService.ts +++ b/apps/server/src/kin/chat/KinChatService.ts @@ -10,6 +10,7 @@ import { KinMessageId, KinWaitingId, KinWidgetId, + KinOwnerCheckCard, KinMessagesSendInput, KinWidgetsActInput, KinAgentIntent, @@ -179,6 +180,7 @@ const decodeWidgetId = Schema.decodeEffect(KinWidgetId); const decodeWaitingId = Schema.decodeEffect(KinWaitingId); const decodeSend = Schema.decodeEffect(KinMessagesSendInput); const decodeIntent = Schema.decodeEffect(KinAgentIntent); +const decodeOwnerCheckCard = Schema.decodeUnknownEffect(KinOwnerCheckCard); const decodeAct = Schema.decodeEffect(KinWidgetsActInput); const encodeAction = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown)); const invalidState = new KinChatError({ reason: "invalid_state", message: "Invalid chat input." }); @@ -947,7 +949,8 @@ const make = Effect.gen(function* () { reason: "use_approvals", message: "Use the signed approvals service.", }); - if (input.type === "task" && input.widgetId === undefined) return yield* notAllowed; + if (input.type === "owner_check" || (input.type === "task" && input.widgetId === undefined)) + return yield* notAllowed; let state = yield* validateState(input.type, input.state); let widget: KinWidget; if (input.widgetId === undefined) { @@ -1074,6 +1077,17 @@ const make = Effect.gen(function* () { reason: "use_approvals", message: "Use the signed approvals service.", }); + if (widget.type === "owner_check") { + if (member.role !== "owner") return yield* notAllowed; + const card = yield* decodeOwnerCheckCard(widget.state).pipe( + Effect.mapError(() => invalidState), + ); + if ( + !["keep", "update", "drop"].includes(request.actionId) || + !card.items.some((item) => item.itemId === request.value) + ) + return yield* invalidState; + } const body = yield* encodeAction({ widgetId: request.widgetId, revision: request.revision, diff --git a/apps/server/src/kin/lead/KinEventQueue.test.ts b/apps/server/src/kin/lead/KinEventQueue.test.ts index e20021ce4a87d..65ec358124f4c 100644 --- a/apps/server/src/kin/lead/KinEventQueue.test.ts +++ b/apps/server/src/kin/lead/KinEventQueue.test.ts @@ -186,11 +186,12 @@ describe("KinEventQueue", () => { "report", "resolved", "github", - "routine", "noisy", + "routine", "watch", ], ); + assert.equal(events.find((entry) => entry.eventId === "routine")?.priority, "P3"); yield* finish("priority"); yield* sql`UPDATE kin_chat_members SET role = 'member'`; yield* queue.enqueue(yield* event("member", "chat.message")); diff --git a/apps/server/src/kin/lead/KinEventQueue.ts b/apps/server/src/kin/lead/KinEventQueue.ts index 8fae9a32d440f..58b11a39fa91d 100644 --- a/apps/server/src/kin/lead/KinEventQueue.ts +++ b/apps/server/src/kin/lead/KinEventQueue.ts @@ -228,6 +228,9 @@ const make = Effect.gen(function* () { case "worker.report": priority = "P1"; break; + case "routine": + if (event.routine === "owner_check") priority = "P3"; + break; case "watchdog": priority = "P3"; break; diff --git a/apps/server/src/kin/lead/WakeupRunner.test.ts b/apps/server/src/kin/lead/WakeupRunner.test.ts index ef5df2302c96f..ff326c900c6dc 100644 --- a/apps/server/src/kin/lead/WakeupRunner.test.ts +++ b/apps/server/src/kin/lead/WakeupRunner.test.ts @@ -22,6 +22,9 @@ import { ProjectId, ProviderInstanceId, } from "@t3tools/contracts"; +import * as OwnerCheck from "../triggers/ownerCheck.ts"; +import * as ChatService from "../chat/KinChatService.ts"; +import * as ChatHub from "../chat/KinChatHub.ts"; import * as Context from "effect/Context"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; @@ -86,6 +89,34 @@ const run = Effect.fn("test.run")(function* () { }); describe("WakeupRunner", () => { + it.effect("handles the owner check in code without starting a provider turn", () => + Effect.gen(function* () { + yield* seed; + const queue = yield* KinEventQueue.KinEventQueue; + const sql = yield* SqlClient.SqlClient; + yield* queue.enqueue(event("owner-check")); + yield* Fiber.join(yield* run()); + assert.deepEqual( + yield* sql`SELECT state FROM kin_events WHERE org_id = ${orgId} ORDER BY event_id`, + [{ state: "done" }], + ); + assert.deepEqual( + yield* sql`SELECT thread_id, status FROM kin_wakeups WHERE org_id = ${orgId} ORDER BY wakeup_id`, + [{ thread_id: null, status: "completed" }], + ); + assert.lengthOf( + yield* sql`SELECT thread_id FROM kin_agent_threads WHERE org_id = ${orgId} ORDER BY thread_id`, + 0, + ); + }).pipe( + Effect.provide( + harness().pipe( + Layer.provide(Layer.mock(OwnerCheck.OwnerCheck)({ post: () => Effect.succeed([]) })), + ), + ), + provideDeterministicTestRuntime, + ), + ); it.effect("uses the cheap model for exhausted P0 even when a routine is in the batch", () => Effect.gen(function* () { yield* seed; @@ -962,6 +993,8 @@ describe("WakeupRunner", () => { Layer.provide(Layer.mock(KinIdentity.KinIdentity)({ isAgentThread: () => false })), Layer.provide( Layer.mergeAll( + Layer.mock(ChatService.KinChatService)({}), + Layer.mock(ChatHub.KinChatHub)({}), Layer.mock(Budget.KinBudget)({}), Layer.mock(KinEventQueue.KinEventQueue)({}), Layer.mock(ThreadLaunch.ThreadLaunchService)({}), diff --git a/apps/server/src/kin/lead/WakeupRunner.ts b/apps/server/src/kin/lead/WakeupRunner.ts index 1a551a23f4176..b1a2b7adebfb0 100644 --- a/apps/server/src/kin/lead/WakeupRunner.ts +++ b/apps/server/src/kin/lead/WakeupRunner.ts @@ -30,6 +30,8 @@ import { toPersistenceSqlError } from "../../persistence/Errors.ts"; import * as ProjectService from "../../project/ProjectService.ts"; import * as KinIdentity from "../chat/KinIdentity.ts"; import * as Memory from "../memory/Memory.ts"; +import * as Routines from "../triggers/routines.ts"; +import * as OwnerCheck from "../triggers/ownerCheck.ts"; import * as KinEventQueue from "./KinEventQueue.ts"; import * as KinBudget from "./KinBudget.ts"; import * as KinLeadRegistry from "./KinLeadRegistry.ts"; @@ -93,6 +95,7 @@ const ulid = Effect.gen(function* () { }); const make = Effect.gen(function* () { + const ownerCheck = yield* Effect.serviceOption(OwnerCheck.OwnerCheck); const sql = yield* SqlClient.SqlClient; const queue = yield* KinEventQueue.KinEventQueue; const budget = yield* KinBudget.KinBudget; @@ -349,9 +352,19 @@ const make = Effect.gen(function* () { events = events.filter((event) => !deferred.has(event.eventId)); } } - if (events.length === 0) return; + const routines = yield* Effect.serviceOption(Routines.KinRoutines); + if (Option.isSome(routines)) + events = yield* Effect.filter(events, (event) => + event._tag === "routine" + ? routines.value.isEnabled(orgId, event.routine) + : Effect.succeed(true), + ); + if (events.length === 0) { + yield* queue.finish({ orgId, wakeupId, status: "completed" }); + return; + } const budgetContext = yield* budget.promptContext(orgId); - const pinned = yield* memory.prepareWakeup(orgId).pipe( + const pinned = yield* memory.prepareWakeup(orgId, wakeupId).pipe( Effect.tapError((error) => Effect.logError("Kin memory preparation failed", { orgId, @@ -365,6 +378,19 @@ const make = Effect.gen(function* () { : Effect.void, ), ); + if (Option.isSome(ownerCheck)) { + for (const event of events) { + if (event._tag === "routine" && event.routine === "owner_check") + yield* ownerCheck.value.post(orgId, event.eventId); + } + events = events.filter( + (event) => event._tag !== "routine" || event.routine !== "owner_check", + ); + if (events.length === 0) { + yield* queue.finish({ orgId, wakeupId, status: "completed" }); + return; + } + } const paths = yield* memory.ensureCheckouts(orgId); const { project } = yield* projects.bootstrap({ commandId: CommandId.make(`kin-project:${orgId}`), diff --git a/apps/server/src/kin/lead/layer.test.ts b/apps/server/src/kin/lead/layer.test.ts index 62b54d3d8a6d1..ce408971c386c 100644 --- a/apps/server/src/kin/lead/layer.test.ts +++ b/apps/server/src/kin/lead/layer.test.ts @@ -14,11 +14,15 @@ import * as ServerConfig from "../../config.ts"; import * as EffectWorker from "../../orchestration-v2/EffectWorker.ts"; import * as ThreadManagement from "../../orchestration-v2/ThreadManagementService.ts"; import { provideDeterministicTestRuntime } from "../../orchestration-v2/testkit/DeterministicRuntime.ts"; +import * as ChatService from "../chat/KinChatService.ts"; +import * as ChatHub from "../chat/KinChatHub.ts"; import * as KinEventQueue from "./KinEventQueue.ts"; import * as KinLead from "./layer.ts"; import { event, harness, seed, orgId } from "./WakeupRunner.testkit.ts"; const external = Layer.mergeAll( + Layer.mock(ChatService.KinChatService)({}), + Layer.mock(ChatHub.KinChatHub)({}), ServerConfig.layerTest(process.cwd(), { prefix: "kin-lead-layer-" }), Layer.mock(VcsProcess.VcsProcess)({}), ).pipe(Layer.provideMerge(NodeServices.layer)); @@ -33,6 +37,10 @@ vi.mock("../memory/Memory.ts", async (importOriginal) => { layer: Layer.sync(original.Memory, () => { let prepared = false; return original.Memory.of({ + withWriterLock: (_org, effect) => effect, + anchor: () => Effect.die("unused"), + record: () => Effect.die("unused"), + recover: () => Effect.die("unused"), ensureCheckouts: () => Effect.succeed({ writer: "/private/tmp", read: "/private/tmp" }), prepareWakeup: () => Effect.sync(() => { @@ -45,7 +53,7 @@ vi.mock("../memory/Memory.ts", async (importOriginal) => { : Effect.die("Prompt and runner must share pinned memory"), readIndex: () => Effect.die("unused"), readRules: () => Effect.die("unused"), - query: () => Effect.die("unused"), + query: () => Effect.succeed([]), readItem: () => Effect.die("unused"), readItemQuietly: () => Effect.die("unused"), rebuildIndex: () => Effect.die("unused"), @@ -61,7 +69,11 @@ it.effect("starts configured Kin with the real prompt and the runner's pinned me const hold = yield* Deferred.make(); yield* Effect.gen(function* () { yield* seed; - yield* (yield* KinEventQueue.KinEventQueue).enqueue(event("real-prompt")); + yield* (yield* KinEventQueue.KinEventQueue).enqueue({ + ...event("real-prompt"), + _tag: "routine", + routine: "morning_brief", + }); yield* Layer.build( Layer.fresh(KinLead.layerServer).pipe( Layer.provide(external), @@ -106,5 +118,5 @@ it.effect("leaves unconfigured Kin silent and inert without building its depende ), ); assert.deepStrictEqual(entries, []); - }).pipe(Effect.provide(harness()), provideDeterministicTestRuntime), + }).pipe(Effect.provide(external), Effect.provide(harness()), provideDeterministicTestRuntime), ); diff --git a/apps/server/src/kin/lead/layer.ts b/apps/server/src/kin/lead/layer.ts index ad30be0ad29b5..0b50114e661a9 100644 --- a/apps/server/src/kin/lead/layer.ts +++ b/apps/server/src/kin/lead/layer.ts @@ -1,8 +1,16 @@ import { forkParked } from "../../serverActivation.ts"; -import { KinOrgId } from "@t3tools/contracts"; +import { KinOrgId, KinMessageId, KinChatId } from "@t3tools/contracts"; +import * as Scheduler from "../../scheduling/Scheduler.ts"; import * as Config from "effect/Config"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; +import * as SqlClient from "effect/sql/SqlClient"; +import * as KinChatService from "../chat/KinChatService.ts"; +import * as Routines from "../triggers/routines.ts"; +import * as OwnerCheck from "../triggers/ownerCheck.ts"; +import * as TriggerScheduler from "../triggers/KinTriggerScheduler.ts"; +import * as TriggerStore from "../triggers/KinTriggerStore.ts"; +import * as TriggerInbox from "../triggers/KinTriggerInbox.ts"; import * as Schema from "effect/Schema"; import * as KinChatRepository from "../chat/KinChatRepository.ts"; @@ -35,18 +43,64 @@ export function containStartup(layer: Layer.Layer) { ); } +const recoveryHooks = Layer.effect( + Memory.MemoryRecoveryHooks, + Effect.gen(function* () { + const chat = yield* KinChatService.KinChatService; + const sql = yield* SqlClient.SqlClient; + return Memory.MemoryRecoveryHooks.of({ + notify: Effect.fn("KinLead.notifyMemoryRecovery")((notice) => + Effect.gen(function* () { + const [lead] = yield* sql<{ + owner_chat_id: string; + }>`SELECT owner_chat_id FROM kin_leads WHERE org_id = ${notice.orgId} ORDER BY agent_id LIMIT 1`; + if (lead !== undefined) + yield* chat.notice({ + orgId: notice.orgId, + chatId: KinChatId.make(lead.owner_chat_id), + messageId: KinMessageId.make( + `memory-recovery:${notice.orgId}:${notice.conflictBranch ?? notice.branch}:${notice.reason}`, + ), + body: + notice.conflictBranch === undefined + ? "Kin memory was recovered. Review the recovery journal before resuming work." + : `Kin preserved conflicting memory on ${notice.conflictBranch}. Review it before resuming work.`, + }); + }).pipe( + Effect.catch((cause) => + Effect.logError("Kin memory recovery notification failed", { + orgId: notice.orgId, + cause, + }), + ), + ), + ), + }); + }), +); +const routineRuntime = Routines.layer.pipe( + Layer.provideMerge(TriggerScheduler.layer), + Layer.provide(Layer.mergeAll(TriggerStore.layer, TriggerInbox.layer)), + Layer.provide(Scheduler.layer), +); + export const layer = Layer.effectDiscard( Effect.gen(function* () { const runner = yield* WakeupRunner.WakeupRunner; - yield* runner.start.pipe(forkParked); + yield* Effect.gen(function* () { + const context = yield* Layer.build(routineRuntime); + yield* runner.start.pipe(Effect.provideContext(context)); + }).pipe(forkParked); }), ).pipe( Layer.provide(WakeupRunner.layer), + Layer.provide(OwnerCheck.layer), Layer.provide( Layer.mergeAll(KinLeadRegistry.layer, KinLeadPrompt.layer).pipe( Layer.provideMerge(Layer.mergeAll(Memory.layer, KinChatRepository.layer)), ), ), + Layer.provide(recoveryHooks), Layer.provide(KinPersistence.layer), ); diff --git a/apps/server/src/kin/memory/MemoryWriter.test.ts b/apps/server/src/kin/memory/MemoryWriter.test.ts index 194e65512ca73..8e0780fe34d11 100644 --- a/apps/server/src/kin/memory/MemoryWriter.test.ts +++ b/apps/server/src/kin/memory/MemoryWriter.test.ts @@ -41,6 +41,8 @@ it.live.each([ "P8-lost-writer", "P9-shadow", "sha256", + "owner-keep", + "agent-keep", "review-default", "review-null", "credentials", @@ -82,7 +84,12 @@ it.live.each([ ]); yield* fs.writeFileString(`${source}/rules.md`, "Invented rules\n"); yield* fs.writeFileString(`${source}/INDEX.md`, "As of 2026-11-08T00:00:00.000Z\n"); - if (scenario.startsWith("B6") || scenario === "P3-object" || scenario === "P6-malformed") { + if ( + scenario.startsWith("B6") || + scenario.endsWith("-keep") || + scenario === "P3-object" || + scenario === "P6-malformed" + ) { yield* fs.makeDirectory(`${source}/claims`); yield* fs.writeFileString( `${source}/claims/existing.md`, @@ -91,7 +98,7 @@ it.live.each([ : fixtureItem({ kind: "claim", status: scenario === "B6-owner-open" ? "open" : "done", - trust: scenario === "B6-agent-own" ? "agent" : "owner", + trust: scenario === "B6-agent-own" || scenario === "agent-keep" ? "agent" : "owner", said_by: scenario === "B6-agent-own" ? agentId : "fixture-owner", }), ); @@ -487,6 +494,30 @@ it.live.each([ ).toBe("Success"); return; } + if (scenario.endsWith("-keep")) { + const content = fixtureItem({ + kind: "claim", + status: "done", + trust: scenario === "agent-keep" ? "agent" : "owner", + said_by: "fixture-owner", + review_by: "2026-12-13", + }); + const result = yield* writer + .stage(orgId, "wake1", { + path: "claims/existing.md", + content, + ...(scenario === "owner-keep" ? { sourceEventId: "owner" } : {}), + }) + .pipe(Effect.result); + expect(result._tag).toBe(scenario === "owner-keep" ? "Success" : "Failure"); + if (result._tag === "Success") { + const committed = yield* writer.commitWakeup(orgId, "wake1", "completed"); + expect( + yield* git(paths.writer, ["show", `${committed.commit}:claims/existing.md`]), + ).toContain("review_by: 2026-12-13"); + } + return; + } if (scenario.startsWith("B6")) { const result = yield* writer .stage(orgId, "wake1", { diff --git a/apps/server/src/kin/memory/MemoryWriter.ts b/apps/server/src/kin/memory/MemoryWriter.ts index 780b6114f5b47..4ae15a0102672 100644 --- a/apps/server/src/kin/memory/MemoryWriter.ts +++ b/apps/server/src/kin/memory/MemoryWriter.ts @@ -430,6 +430,7 @@ const make = Effect.gen(function* () { "said_by", "source", ]); + if (source.trust === "owner") mutable.add("review_by"); if ( Object.entries(item).some( ([key, value]) => diff --git a/apps/server/src/kin/persistence/KinMigrations.test.ts b/apps/server/src/kin/persistence/KinMigrations.test.ts index 4b855164bf64d..a6bffbbde2edf 100644 --- a/apps/server/src/kin/persistence/KinMigrations.test.ts +++ b/apps/server/src/kin/persistence/KinMigrations.test.ts @@ -36,6 +36,8 @@ const tableNames = [ "kin_ping_items", "kin_ping_ledger", "kin_read_state", + "kin_routine_folds", + "kin_routine_preferences", "kin_session_bindings", "kin_shadow_entries", "kin_trigger_fires", @@ -98,6 +100,7 @@ describe("Kin migrations", () => { { migration_id: 9, name: "JudgeLog" }, { migration_id: 10, name: "ChatChanges" }, { migration_id: 11, name: "MemoryHeads" }, + { migration_id: 12, name: "Routines" }, ]); assert.deepStrictEqual( upstreamBefore.map((row) => [row.migration_id, row.name] as const), @@ -151,6 +154,7 @@ describe("Kin migrations", () => { { migration_id: 9, name: "JudgeLog" }, { migration_id: 10, name: "ChatChanges" }, { migration_id: 11, name: "MemoryHeads" }, + { migration_id: 12, name: "Routines" }, ]); assert.deepStrictEqual(yield* upstreamHistory, upstreamBefore); assert.deepStrictEqual(yield* sql`SELECT * FROM kin_orgs`, [ @@ -229,6 +233,7 @@ describe("Kin migrations", () => { [9, "JudgeLog"], [10, "ChatChanges"], [11, "MemoryHeads"], + [12, "Routines"], ]); const before = yield* kinSchema; assert.deepStrictEqual( @@ -266,6 +271,8 @@ describe("Kin migrations", () => { yield* sql`DROP TABLE kin_ping_items`; yield* sql`DROP INDEX kin_chat_members_one_owner`; yield* sql`DROP TABLE kin_memory_heads`; + yield* sql`DROP TABLE kin_routine_folds`; + yield* sql`DROP TABLE kin_routine_preferences`; yield* sql`DROP TABLE kin_budget_charges`; yield* sql`ALTER TABLE kin_events DROP COLUMN budget_deferred`; yield* sql`UPDATE kin_chats SET last_seq = 1 WHERE org_id = 'org' AND chat_id = 'existing-chat'`; @@ -280,6 +287,7 @@ describe("Kin migrations", () => { [9, "JudgeLog"], [10, "ChatChanges"], [11, "MemoryHeads"], + [12, "Routines"], ]); assert.deepStrictEqual( yield* sql`SELECT member_id FROM kin_chat_members WHERE org_id = 'org' ORDER BY member_id`, @@ -316,6 +324,7 @@ describe("Kin migrations", () => { { migration_id: 9, name: "JudgeLog" }, { migration_id: 10, name: "ChatChanges" }, { migration_id: 11, name: "MemoryHeads" }, + { migration_id: 12, name: "Routines" }, ]); 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 b7bba32965691..ec85b8bf0f6f1 100644 --- a/apps/server/src/kin/persistence/KinMigrations.ts +++ b/apps/server/src/kin/persistence/KinMigrations.ts @@ -15,6 +15,8 @@ import JudgeLog from "./migrations/K009_JudgeLog.ts"; import ChatChanges from "./migrations/K010_ChatChanges.ts"; import MemoryHeads from "./migrations/K011_MemoryHeads.ts"; +import Routines from "./migrations/K012_Routines.ts"; + const run = Migrator.make({}); const loader = Migrator.fromRecord({ "1_ChatCore": ChatCore, @@ -28,6 +30,7 @@ const loader = Migrator.fromRecord({ "9_JudgeLog": JudgeLog, "10_ChatChanges": ChatChanges, "11_MemoryHeads": MemoryHeads, + "12_Routines": Routines, }); export const runKinMigrations = Effect.fn("KinMigrations.run")(function* () { diff --git a/apps/server/src/kin/persistence/migrations/K012_Routines.ts b/apps/server/src/kin/persistence/migrations/K012_Routines.ts new file mode 100644 index 0000000000000..589cf62a7f4dc --- /dev/null +++ b/apps/server/src/kin/persistence/migrations/K012_Routines.ts @@ -0,0 +1,21 @@ +import * as Effect from "effect/Effect"; +import * as SqlClient from "effect/sql/SqlClient"; + +export default Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + yield* sql`CREATE TABLE kin_routine_preferences ( + org_id TEXT NOT NULL REFERENCES kin_orgs(org_id), + routine TEXT NOT NULL CHECK (routine IN ('morning_brief', 'evening_summary', 'owner_check')), + enabled INTEGER NOT NULL DEFAULT 1 CHECK (enabled IN (0, 1)), + paused INTEGER NOT NULL DEFAULT 0 CHECK (paused IN (0, 1)), + PRIMARY KEY (org_id, routine) + )`; + yield* sql`CREATE TABLE kin_routine_folds ( + org_id TEXT NOT NULL REFERENCES kin_orgs(org_id), + fire_id TEXT NOT NULL, + routine TEXT NOT NULL, + due_at INTEGER NOT NULL CHECK (typeof(due_at) = 'integer'), + fold_into TEXT NOT NULL CHECK (fold_into IN ('morning_brief', 'evening_summary')), + PRIMARY KEY (org_id, fire_id) + )`; +}); diff --git a/apps/server/src/kin/triggers/KinTriggerInbox.ts b/apps/server/src/kin/triggers/KinTriggerInbox.ts index b21e6f0e325f2..d6e2cfa90e0fb 100644 --- a/apps/server/src/kin/triggers/KinTriggerInbox.ts +++ b/apps/server/src/kin/triggers/KinTriggerInbox.ts @@ -1,4 +1,10 @@ -import { KinOrgId, KinTriggerFireId, KinTriggerFired, NonNegativeInt } from "@t3tools/contracts"; +import { + KinOrgId, + KinRoutine, + KinTriggerFireId, + KinTriggerFired, + NonNegativeInt, +} from "@t3tools/contracts"; import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient"; import * as Context from "effect/Context"; import * as DateTime from "effect/DateTime"; @@ -36,6 +42,7 @@ const isInboxConflict = Schema.is(KinTriggerInboxConflict); const PendingRequest = Schema.Struct({ orgId: KinOrgId, limit: Schema.Int.check(Schema.isGreaterThan(0)), + routines: Schema.optional(Schema.Array(KinRoutine)), }); const InboxRow = Schema.Struct({ payload: Schema.fromJsonString(KinTriggerFired) }); const decodeInboxRow = Schema.decodeUnknownEffect(InboxRow); @@ -50,6 +57,7 @@ export class KinTriggerInbox extends Context.Service< readonly listPending: ( orgId: KinOrgId, limit?: number, + routines?: ReadonlyArray, ) => Effect.Effect, InboxError>; readonly markConsumed: ( orgId: KinOrgId, @@ -73,9 +81,14 @@ const make = Effect.gen(function* () { const pending = SqlSchema.findAll({ Request: PendingRequest, Result: InboxRow, - execute: ({ orgId, limit }) => sql` + execute: ({ orgId, limit, routines }) => sql` SELECT payload_json AS payload FROM kin_trigger_inbox WHERE org_id = ${orgId} AND consumed_at IS NULL + ${ + routines === undefined + ? sql`` + : sql`AND json_extract(payload_json, '$.payload.subject.routine') IN (${sql.join(",", false)(routines.map((routine) => sql`${routine}`))})` + } ORDER BY received_at, fire_id LIMIT ${limit} `, }); @@ -144,10 +157,12 @@ const make = Effect.gen(function* () { Effect.mapError(toInboxError("KinTriggerInbox.put")), ), listPending: Effect.fn("KinTriggerInbox.listPending")( - function* (orgId, limit = 100) { - return (yield* pending({ orgId, limit }).pipe(NodeSqliteClient.readOnly)).map( - (row) => row.payload, - ); + function* (orgId, limit = 100, routines) { + return (yield* pending({ + orgId, + limit, + ...(routines === undefined ? {} : { routines }), + }).pipe(NodeSqliteClient.readOnly)).map((row) => row.payload); }, Effect.mapError(toInboxError("KinTriggerInbox.listPending")), ), diff --git a/apps/server/src/kin/triggers/ownerCheck.test.ts b/apps/server/src/kin/triggers/ownerCheck.test.ts new file mode 100644 index 0000000000000..0f0bdc0b0b830 --- /dev/null +++ b/apps/server/src/kin/triggers/ownerCheck.test.ts @@ -0,0 +1,198 @@ +import { assert, it } from "@effect/vitest"; +import { + KinEvent, + KinEventId, + KinChatId, + KinWidgetId, + KinPersonId, + KinWakeupId, + KinAgentId, + KinOwnerCheckCard, +} from "@t3tools/contracts"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import * as Schema from "effect/Schema"; +import * as SqlClient from "effect/sql/SqlClient"; +import * as TestClock from "effect/testing/TestClock"; +import * as MemoryWriter from "../memory/MemoryWriter.ts"; +import * as Service from "../chat/KinChatService.ts"; +import * as Queue from "../lead/KinEventQueue.ts"; +import * as OwnerCheck from "./ownerCheck.ts"; +import { fixture, seed, orgId, otherOrg, item } from "./routines.testkit.ts"; + +const encodeJson = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown)); +const decodeCard = Schema.decodeSync(Schema.fromJsonString(KinOwnerCheckCard)); +const decodeEvent = Schema.decodeSync(KinEvent); +const now = Date.parse("2026-11-09T09:00:00.000Z"); +const memoryItems = [ + item("old-review", { review_by: "2026-11-01" }), + item("misleading"), + item("low-trust", { trust: "external" }), + item("no-evidence", { links: [] }), + item("last-review", { review_by: "2026-11-02" }), + item("sixth", { links: [] }), + item("invalid", { invalid_at: "2026-11-01", review_by: "2026-10-01" }), + item("other", { orgId: otherOrg, review_by: "2026-10-01" }), +]; +it.effect("picks five distinct, valid same-org items in the specified category order", () => + Effect.gen(function* () { + yield* seed(); + const sql = yield* SqlClient.SqlClient; + const report = { + result: "done", + summary: "Done", + evidence: [], + openQuestions: [], + memorySuggestions: [], + misleadingMemory: [{ itemId: "misleading", why: "Wrong evidence" }], + parsed: true, + }; + const json = yield* encodeJson(report); + yield* sql`INSERT INTO kin_wakeups (org_id, wakeup_id, agent_id, model, started_at, status, event_ids) VALUES (${orgId}, 'worker-wakeup', 'lead-a', 'fixture', 0, 'completed', '[]')`; + yield* sql`INSERT INTO kin_workers (worker_id, org_id, agent_id, wakeup_id, thread_id, kind, model_json, provenance_json, status, report_json, started_at) VALUES ('worker', ${orgId}, 'lead-a', 'worker-wakeup', 'thread', 'research', '{}', '{}', 'done', ${json}, 0)`; + yield* TestClock.setTime(now); + const service = yield* OwnerCheck.OwnerCheck; + const selected = yield* service.select(orgId); + assert.deepEqual( + selected.map((item) => [item.itemId, item.reason]), + [ + ["old-review", "past_review"], + ["last-review", "past_review"], + ["misleading", "misleading"], + ["low-trust", "old_low_trust"], + ["no-evidence", "no_evidence"], + ], + ); + assert.deepEqual( + OwnerCheck.pickOwnerCheckItems(orgId, memoryItems.toReversed(), new Set(["misleading"]), now), + selected, + ); + }).pipe(Effect.provide(fixture(memoryItems))), +); + +it.effect( + "posts one native card per fire and routes owner taps to the writer without accepting model authority", + () => + Effect.gen(function* () { + yield* seed(); + yield* TestClock.setTime(now); + const service = yield* OwnerCheck.OwnerCheck; + const chat = yield* Service.KinChatService; + const queue = yield* Queue.KinEventQueue; + const sql = yield* SqlClient.SqlClient; + yield* service.post(orgId, "weekly"); + yield* service.post(orgId, "weekly"); + const [widget] = yield* sql<{ + state_json: string; + }>`SELECT state_json FROM kin_widgets WHERE org_id = ${orgId} ORDER BY widget_id`; + const card = decodeCard(widget!.state_json); + assert.equal(card.items[0]?.itemId, "old-review"); + assert.lengthOf( + yield* sql`SELECT widget_id FROM kin_widgets WHERE org_id = ${orgId} ORDER BY widget_id`, + 1, + ); + yield* chat + .upsertWidget( + { _tag: "person", orgId, personId: KinPersonId.make("owner-a") }, + { chatId: KinChatId.make("chat-a"), type: "owner_check", state: card }, + ) + .pipe(Effect.flip); + yield* chat + .act( + { _tag: "person", orgId, personId: KinPersonId.make("owner-a") }, + { + widgetId: KinWidgetId.make("owner-check:a:weekly"), + revision: 1, + actionId: "drop", + value: "foreign-item", + clientActionId: "forged-target", + }, + ) + .pipe(Effect.flip); + const staged: MemoryWriter.MemoryChange[] = []; + for (const action of ["keep", "update", "drop"] as const) { + const message = yield* chat.act( + { _tag: "person", orgId, personId: KinPersonId.make("owner-a") }, + { + widgetId: KinWidgetId.make("owner-check:a:weekly"), + revision: 1, + actionId: action, + value: "old-review", + clientActionId: action, + }, + ); + const event = decodeEvent({ + _tag: "widget.action", + orgId, + eventId: action, + agentId: "lead-a", + receivedAt: "2026-11-09T09:00:00.000Z", + dedupeKey: action, + provenance: { _tag: "human", personId: "owner-a", memberId: "owner-member-a" }, + chatId: "chat-a", + messageId: message.messageId, + actionId: action, + }); + yield* queue.enqueue(event); + } + yield* TestClock.adjust("20 seconds"); + yield* Effect.scoped( + Effect.gen(function* () { + yield* queue.claim({ + orgId, + agentId: KinAgentId.make("lead-a"), + wakeupId: KinWakeupId.make("review-wakeup"), + model: "fixture", + }); + const writer = Layer.mock(MemoryWriter.MemoryWriter)({ + stage: (_org, _wakeup, change) => + Effect.sync(() => { + staged.push(change); + }), + }); + for (const action of ["keep", "update", "drop"] as const) { + const result = yield* service + .act(orgId, "review-wakeup", KinEventId.make(action)) + .pipe(Effect.provide(writer)); + assert.equal(result.action, action); + assert.equal(result.chatId, KinChatId.make("chat-a")); + } + assert.lengthOf(staged, 2); + assert.equal(staged[0]?.sourceEventId, "keep"); + assert.include(staged[0]!.content, "2026-12-09T09:00:20.000Z"); + assert.include(staged[1]!.content, "2026-11-09T09:00:20.000Z"); + for (const [org, eventId, wakeup] of [ + [otherOrg, "keep", "review-wakeup"], + [orgId, "keep", "foreign-wakeup"], + ] as const) { + const result = yield* service + .act(org, wakeup, KinEventId.make(eventId)) + .pipe(Effect.provide(writer), Effect.flip); + assert.equal(result._tag, "OwnerCheckError"); + } + yield* sql`UPDATE kin_chat_members SET role = 'member' WHERE org_id = ${orgId} AND member_id = 'owner-member-a'`; + yield* service + .act(orgId, "review-wakeup", KinEventId.make("keep")) + .pipe(Effect.provide(writer), Effect.flip); + assert.lengthOf(staged, 2); + yield* chat + .act( + { _tag: "person", orgId, personId: KinPersonId.make("owner-a") }, + { + widgetId: KinWidgetId.make("owner-check:a:weekly"), + revision: 1, + actionId: "keep", + value: "old-review", + clientActionId: "revoked-owner", + }, + ) + .pipe(Effect.flip); + yield* queue.finish({ + orgId, + wakeupId: KinWakeupId.make("review-wakeup"), + status: "completed", + }); + }), + ); + }).pipe(Effect.provide(fixture(memoryItems))), +); diff --git a/apps/server/src/kin/triggers/ownerCheck.ts b/apps/server/src/kin/triggers/ownerCheck.ts new file mode 100644 index 0000000000000..63a4485abd0fc --- /dev/null +++ b/apps/server/src/kin/triggers/ownerCheck.ts @@ -0,0 +1,314 @@ +import { + KinEvent, + KinEventId, + KinMemoryItemId, + KinMessageId, + KinOrgId, + KinWidgetId, + KinPersonId, + type MemoryItemHead, + WorkerReport, + type KinWidget, + KinChatId, + KinOwnerCheckCard, +} from "@t3tools/contracts"; +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 Option from "effect/Option"; +import * as Schema from "effect/Schema"; +import * as SqlClient from "effect/sql/SqlClient"; +import { isSqlError } from "effect/sql/SqlError"; +import { stringify } from "yaml"; +import { toPersistenceSqlError } from "../../persistence/Errors.ts"; +import * as Memory from "../memory/Memory.ts"; +import * as MemoryWriter from "../memory/MemoryWriter.ts"; +import { memoryDateMillis } from "../memory/frontMatter.ts"; +import * as Chat from "../chat/KinChatService.ts"; +import * as Repository from "../chat/KinChatRepository.ts"; +import * as Hub from "../chat/KinChatHub.ts"; + +const day = 86_400_000; +const reasons = ["past_review", "misleading", "old_low_trust", "no_evidence"] as const; +const Selection = Schema.Struct({ itemId: KinMemoryItemId, reason: Schema.Literals(reasons) }); +type Selection = typeof Selection.Type; +const decodeCard = Schema.decodeUnknownEffect(KinOwnerCheckCard); +const decodeEvent = Schema.decodeUnknownEffect(Schema.fromJsonString(KinEvent)); +const decodeAction = Schema.decodeUnknownEffect( + Schema.fromJsonString( + Schema.Struct({ + widgetId: KinWidgetId, + revision: Schema.Int, + actionId: Schema.Literals(["keep", "update", "drop"]), + value: KinMemoryItemId, + }), + ), +); +const decodeReport = Schema.decodeUnknownEffect(Schema.fromJsonString(WorkerReport)); +const ordinal = (a: string, b: string) => (a < b ? -1 : a > b ? 1 : 0); + +export function pickOwnerCheckItems( + orgId: KinOrgId, + items: ReadonlyArray, + misleading: ReadonlySet, + now: number, +): ReadonlyArray { + return items + .filter( + (item) => + item.orgId === orgId && + !item.duplicate && + item.invalid_at === null && + item.superseded_by === null, + ) + .map((item) => { + const review = memoryDateMillis(item.review_by); + const rank = + review < now + ? 0 + : misleading.has(item.id) + ? 1 + : item.kind === "claim" && + item.trust !== "owner" && + memoryDateMillis(item.recorded_at) < now - 30 * day + ? 2 + : item.kind === "claim" && item.links.length === 0 + ? 3 + : -1; + return { item, review, rank }; + }) + .filter(({ rank }) => rank >= 0) + .toSorted( + (a, b) => + a.rank - b.rank || + (Number.isFinite(a.review) ? a.review : Infinity) - + (Number.isFinite(b.review) ? b.review : Infinity) || + ordinal(a.item.id, b.item.id) || + ordinal(a.item.path, b.item.path), + ) + .filter( + (row, index, rows) => rows.findIndex((other) => other.item.id === row.item.id) === index, + ) + .slice(0, 5) + .map(({ item, rank }) => ({ itemId: item.id, reason: reasons[rank] ?? "no_evidence" })); +} + +export class OwnerCheckError extends Schema.TaggedError()("OwnerCheckError", { + orgId: KinOrgId, + cause: Schema.Defect(), +}) {} +export class OwnerCheck extends Context.Service< + OwnerCheck, + { + readonly select: (orgId: KinOrgId) => Effect.Effect, OwnerCheckError>; + readonly post: ( + orgId: KinOrgId, + fireId: string, + ) => Effect.Effect, OwnerCheckError>; + readonly act: ( + orgId: KinOrgId, + wakeupId: string, + eventId: KinEventId, + ) => Effect.Effect< + { + readonly chatId: KinChatId; + readonly itemId: KinMemoryItemId; + readonly action: "keep" | "update" | "drop"; + }, + OwnerCheckError, + MemoryWriter.MemoryWriter + >; + } +>()("t3/kin/triggers/ownerCheck") {} + +const make = Effect.gen(function* () { + const memory = yield* Memory.Memory; + const sql = yield* SqlClient.SqlClient; + const chat = yield* Chat.KinChatService; + const repo = yield* Repository.KinChatRepository; + const hub = yield* Hub.KinChatHub; + const wrap = (orgId: KinOrgId, effect: Effect.Effect) => + effect.pipe(Effect.mapError((cause) => new OwnerCheckError({ orgId, cause }))); + const select = Effect.fn("OwnerCheck.select")((orgId: KinOrgId) => + wrap( + orgId, + Effect.gen(function* () { + const items = yield* memory.query(orgId, { excludeInvalid: true }); + const reports = yield* sql<{ + report_json: string; + }>`SELECT report_json FROM kin_workers WHERE org_id = ${orgId} AND report_json IS NOT NULL ORDER BY ended_at, worker_id`.pipe( + Effect.mapError((cause) => + isSqlError(cause) ? toPersistenceSqlError("OwnerCheck.select")(cause) : cause, + ), + ); + const misleading = new Set(); + for (const row of reports) { + const report = yield* decodeReport(row.report_json); + for (const finding of report.misleadingMemory) misleading.add(finding.itemId); + } + return pickOwnerCheckItems(orgId, items, misleading, yield* Clock.currentTimeMillis); + }), + ), + ); + const post = Effect.fn("OwnerCheck.post")((orgId: KinOrgId, fireId: string) => + wrap( + orgId, + Effect.gen(function* () { + const widgetId = KinWidgetId.make(`owner-check:${orgId}:${fireId}`); + const previous = yield* repo.getWidget({ orgId, widgetId }); + if (Option.isSome(previous)) + return (yield* decodeCard(previous.value.state)).items.map(({ itemId, reason }) => ({ + itemId, + reason, + })); + const selections = yield* select(orgId); + const heads = yield* Effect.forEach(selections, (selection) => + memory.readItemQuietly(orgId, selection.itemId), + ); + return yield* sql + .withTransaction( + Effect.gen(function* () { + const existing = yield* repo.getWidget({ orgId, widgetId }); + if (Option.isSome(existing)) + return (yield* decodeCard(existing.value.state)).items.map( + ({ itemId, reason }) => ({ itemId, reason }), + ); + if (selections.length === 0) return selections; + const [lead] = yield* sql<{ + owner_chat_id: string; + owner_person_id: string; + }>`SELECT l.owner_chat_id, a.owner_person_id 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`; + if (lead === undefined) return yield* Effect.fail("missing_lead"); + const chatId = KinChatId.make(lead.owner_chat_id); + const { message } = yield* chat.notice({ + orgId, + chatId, + messageId: KinMessageId.make(widgetId), + body: "Memory to review", + }); + const now = DateTime.formatIso(yield* DateTime.now); + const state = { + items: selections.map((selection, index) => ({ + ...selection, + title: heads[index]?.head.title ?? "", + path: heads[index]?.head.path ?? "", + })), + }; + const widget: KinWidget = { + orgId, + chatId, + messageId: message.messageId, + widgetId, + widgetClass: "native", + type: "owner_check", + revision: 1, + state, + source: null, + feed: null, + awaiting: [KinPersonId.make(lead.owner_person_id)], + pinnedAt: null, + closedAt: null, + updatedAt: now, + }; + yield* repo.insertWidget(widget); + yield* sql.afterCommit(hub.publish({ type: "widget.updated", orgId, widget })); + return selections; + }), + ) + .pipe( + Effect.mapError((cause) => + isSqlError(cause) ? toPersistenceSqlError("OwnerCheck.post")(cause) : cause, + ), + ); + }), + ), + ); + const act = Effect.fn("OwnerCheck.act")( + (orgId: KinOrgId, wakeupId: string, eventId: KinEventId) => + wrap( + orgId, + Effect.gen(function* () { + const writer = yield* MemoryWriter.MemoryWriter; + const verified = yield* sql + .withTransaction( + Effect.gen(function* () { + const [row] = yield* sql<{ + json: string; + }>`SELECT e.json FROM kin_events e JOIN kin_wakeups w ON w.org_id = e.org_id AND w.wakeup_id = e.wakeup_id WHERE e.org_id = ${orgId} AND e.event_id = ${eventId} AND e.wakeup_id = ${wakeupId} AND e.state = 'claimed' AND w.status = 'running' ORDER BY e.event_id`; + if (row === undefined) return yield* Effect.fail("unclaimed_action"); + const event = yield* decodeEvent(row.json); + if ( + event.orgId !== orgId || + event.eventId !== eventId || + event._tag !== "widget.action" || + event.provenance._tag !== "human" + ) + return yield* Effect.fail("not_owner"); + const message = yield* repo.getMessage({ orgId, messageId: event.messageId }); + if ( + Option.isNone(message) || + message.value.chatId !== event.chatId || + message.value.kind !== "widget_action" || + message.value.authorRole !== "owner" || + message.value.author._tag !== "human" || + message.value.author.personId !== event.provenance.personId || + message.value.author.memberId !== event.provenance.memberId + ) + return yield* Effect.fail("not_owner"); + const owners = + yield* sql`SELECT m.member_id FROM kin_chat_members m JOIN kin_agents a ON a.org_id = m.org_id AND a.owner_person_id = m.person_id WHERE m.org_id = ${orgId} AND m.member_id = ${event.provenance.memberId} AND m.chat_id = ${event.chatId} AND m.person_id = ${event.provenance.personId} AND m.role = 'owner' AND m.left_at IS NULL AND a.agent_id = ${event.agentId} AND a.retired_at IS NULL ORDER BY m.member_id`; + if (owners.length === 0) return yield* Effect.fail("not_owner"); + const action = yield* decodeAction(message.value.body); + const widget = yield* repo.getWidget({ orgId, widgetId: action.widgetId }); + if ( + Option.isNone(widget) || + widget.value.type !== "owner_check" || + widget.value.chatId !== event.chatId || + message.value.widgetId !== action.widgetId || + widget.value.closedAt !== null || + widget.value.revision !== action.revision || + event.actionId !== action.actionId + ) + return yield* Effect.fail("invalid_card"); + const card = yield* decodeCard(widget.value.state); + if (!card.items.some((item) => item.itemId === action.value)) + return yield* Effect.fail("unknown_item"); + return { action, chatId: event.chatId }; + }), + ) + .pipe( + Effect.mapError((cause) => + isSqlError(cause) ? toPersistenceSqlError("OwnerCheck.act")(cause) : cause, + ), + ); + const item = yield* memory.readItemQuietly(orgId, verified.action.value); + if (verified.action.actionId !== "update") { + const { orgId: _org, path, ...frontMatter } = item.head; + const now = yield* Clock.currentTimeMillis; + const period = (frontMatter.kind === "person" ? 90 : 30) * day; + const updated = + verified.action.actionId === "keep" + ? { + ...frontMatter, + review_by: DateTime.formatIso(DateTime.makeUnsafe(now + period)), + } + : { ...frontMatter, invalid_at: DateTime.formatIso(DateTime.makeUnsafe(now)) }; + yield* writer.stage(orgId, wakeupId, { + path, + content: `---\n${stringify(updated)}---\n${item.body}`, + sourceEventId: eventId, + }); + } + return { + chatId: verified.chatId, + itemId: verified.action.value, + action: verified.action.actionId, + }; + }), + ), + ); + return OwnerCheck.of({ select, post, act }); +}); +export const layer = Layer.effect(OwnerCheck, make); diff --git a/apps/server/src/kin/triggers/routines.test.ts b/apps/server/src/kin/triggers/routines.test.ts new file mode 100644 index 0000000000000..a69e01c7a93a7 --- /dev/null +++ b/apps/server/src/kin/triggers/routines.test.ts @@ -0,0 +1,170 @@ +import { assert, it } from "@effect/vitest"; +import { KinOrgId, KinTriggerId, KinEvent, type KinTriggerSettings } from "@t3tools/contracts"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import * as Schema from "effect/Schema"; +import * as SqlClient from "effect/sql/SqlClient"; +import * as TestClock from "effect/testing/TestClock"; +import * as Routines from "./routines.ts"; +import * as TriggerScheduler from "./KinTriggerScheduler.ts"; +import * as TriggerStore from "./KinTriggerStore.ts"; +import { fixture, seed, orgId as orgA, otherOrg } from "./routines.testkit.ts"; +import { routineDisposition } from "./routines.ts"; + +const decodeEventJson = Schema.decodeSync(Schema.fromJsonString(KinEvent)); +const settings: KinTriggerSettings = { + orgId: KinOrgId.make("a"), + timeZone: "Asia/Kolkata", + morningBrief: "08:30", + eveningSummary: "18:30", + dream: "03:00", + ownerCheck: "09:00", + ownerCheckWeekday: 1, + fullRebuildWeekday: 0, + quietHours: { from: "21:00", to: "08:00" }, +}; +it.effect("folds late briefs and summaries at their owner-local boundaries", () => + Effect.sync(() => { + for (const [routine, due, now, expected] of [ + ["morning_brief", "2026-11-09T03:00:00.000Z", "2026-11-09T12:59:59.999Z", "post"], + ["morning_brief", "2026-11-09T03:00:00.000Z", "2026-11-09T13:00:00.000Z", "fold"], + ["evening_summary", "2026-11-09T13:00:00.000Z", "2026-11-09T21:29:59.999Z", "post"], + ["evening_summary", "2026-11-09T13:00:00.000Z", "2026-11-09T21:30:00.000Z", "fold"], + ["owner_check", "2026-11-09T03:30:00.000Z", "2026-11-12T13:00:00.000Z", "post"], + ] as const) + assert.equal(routineDisposition(routine, due, now, settings), expected); + }), +); + +it.effect( + "emits one routine per org at owner time, survives replay, and folds expired slots durably", + () => + Effect.gen(function* () { + yield* seed(); + const routines = yield* Routines.KinRoutines; + const scheduler = yield* TriggerScheduler.KinTriggerScheduler; + const sql = yield* SqlClient.SqlClient; + yield* TestClock.setTime(Date.parse("2026-11-09T02:59:59.999Z")); + for (const orgId of [orgA, otherOrg]) yield* routines.configure({ ...settings, orgId }); + yield* scheduler.runDue(); + yield* routines.drain(orgA); + assert.lengthOf( + yield* sql`SELECT event_id FROM kin_events WHERE org_id = ${orgA} ORDER BY event_id`, + 0, + ); + yield* TestClock.adjust(1); + yield* scheduler.runDue(); + for (const orgId of [orgA, otherOrg]) yield* routines.drain(orgId); + const rows = yield* sql<{ + dedupe_key: string; + json: string; + }>`SELECT dedupe_key, json FROM kin_events WHERE org_id IN (${orgA}, ${otherOrg}) ORDER BY org_id, event_id`; + assert.lengthOf(rows, 2); + assert.equal(new Set(rows.map((row) => row.dedupe_key)).size, 2); + assert.equal(decodeEventJson(rows[0]!.json)._tag, "routine"); + yield* TestClock.setTime(Date.parse("2026-11-09T13:00:00.000Z")); + yield* scheduler.runDue(); + yield* routines.drain(orgA); + assert.lengthOf( + yield* sql`SELECT event_id FROM kin_events WHERE org_id = ${orgA} ORDER BY event_id`, + 3, + ); + // Replay an already consumed inbox row, as after a crash before the consume marker. + yield* sql`UPDATE kin_trigger_inbox SET consumed_at = NULL WHERE org_id = ${orgA}`; + yield* routines.drain(orgA); + assert.lengthOf( + yield* sql`SELECT event_id FROM kin_events WHERE org_id = ${orgA} ORDER BY event_id`, + 3, + ); + }).pipe(Effect.provide(fixture())), +); + +it.effect( + "folds a brief after summary time and a summary after the next dream without duplicate catch-up", + () => + Effect.gen(function* () { + yield* seed(); + const routines = yield* Routines.KinRoutines; + const scheduler = yield* TriggerScheduler.KinTriggerScheduler; + const sql = yield* SqlClient.SqlClient; + yield* TestClock.setTime(Date.parse("2026-11-09T02:00:00.000Z")); + yield* routines.configure(settings); + yield* TestClock.setTime(Date.parse("2026-11-09T13:00:00.000Z")); + yield* scheduler.runDue(); + yield* routines.drain(orgA); + const rows = yield* sql<{ + json: string; + }>`SELECT json FROM kin_events WHERE org_id = ${orgA} AND tag = 'routine' ORDER BY event_id`; + const events = rows.map((row) => decodeEventJson(row.json)); + assert.isFalse( + events.some((event) => event._tag === "routine" && event.routine === "morning_brief"), + ); + const summary = events.find( + (event) => event._tag === "routine" && event.routine === "evening_summary", + ); + assert.equal(summary?._tag, "routine"); + if (summary?._tag === "routine") + assert.equal(summary.foldedRoutines?.[0]?.routine, "morning_brief"); + yield* TestClock.setTime(Date.parse("2026-11-10T03:00:00.000Z")); + yield* scheduler.runDue(); + yield* routines.drain(orgA); + yield* TestClock.setTime(Date.parse("2026-11-11T03:00:00.000Z")); + yield* scheduler.runDue(); + yield* routines.drain(orgA); + const [latest] = yield* sql<{ + json: string; + }>`SELECT json FROM kin_events WHERE org_id = ${orgA} AND json_extract(json, '$.routine') = 'morning_brief' ORDER BY received_at DESC LIMIT 1`; + const brief = decodeEventJson(latest!.json); + assert.equal(brief._tag, "routine"); + if (brief._tag === "routine") + assert.equal(brief.foldedRoutines?.[0]?.routine, "evening_summary"); + assert.lengthOf( + yield* sql`SELECT fire_id FROM kin_routine_folds WHERE org_id = ${orgA} ORDER BY fire_id`, + 0, + ); + }).pipe(Effect.provide(fixture())), +); + +it.effect( + "disarms and re-arms individual routines and preserves opt-outs across pause, restart and timezone changes", + () => + Effect.gen(function* () { + yield* seed(); + const routines = yield* Routines.KinRoutines; + const store = yield* TriggerStore.KinTriggerStore; + const scheduler = yield* TriggerScheduler.KinTriggerScheduler; + yield* TestClock.setTime(Date.parse("2026-03-07T12:00:00.000Z")); + yield* routines.configure({ ...settings, timeZone: "America/New_York" }); + const id = KinTriggerId.make("routine:morning_brief"); + assert.equal((yield* store.get(orgA, id))?.nextRunAt, "2026-03-07T13:30:00.000Z"); + yield* TestClock.setTime(Date.parse("2026-03-07T13:30:00.000Z")); + yield* scheduler.runDue(); + assert.equal((yield* store.get(orgA, id))?.nextRunAt, "2026-03-08T12:30:00.000Z"); + yield* routines.drain(orgA); + const sql = yield* SqlClient.SqlClient; + yield* routines.setEnabled(orgA, "morning_brief", false); + assert.deepEqual( + yield* sql`SELECT state FROM kin_events WHERE org_id = ${orgA} AND json_extract(json, '$.routine') = 'morning_brief' ORDER BY event_id`, + [{ state: "expired" }], + ); + assert.isNull((yield* store.get(orgA, id))?.nextRunAt); + yield* routines.configure(settings).pipe(Effect.provide(Layer.fresh(Routines.layer))); + assert.isFalse((yield* store.get(orgA, id))?.enabled); + yield* routines.pauseAll(orgA); + assert.isTrue((yield* store.list(orgA)).every((trigger) => !trigger.enabled)); + yield* routines.resumeAll(orgA); + assert.isFalse((yield* store.get(orgA, id))?.enabled); + yield* routines.setEnabled(orgA, "morning_brief", true); + assert.equal((yield* store.get(orgA, id))?.nextRunAt, "2026-03-08T03:00:00.000Z"); + yield* TestClock.setTime(Date.parse("2026-03-08T03:01:00.000Z")); + yield* routines.configure({ ...settings, timeZone: "America/New_York" }); + assert.equal((yield* store.get(orgA, id))?.nextRunAt, "2026-03-08T03:00:00.000Z"); + yield* scheduler.runDue(); + assert.equal((yield* store.get(orgA, id))?.nextRunAt, "2026-03-08T12:30:00.000Z"); + yield* TestClock.setTime(Date.parse("2026-10-31T12:00:00.000Z")); + yield* scheduler.runDue(); + yield* TestClock.setTime(Date.parse("2026-10-31T12:30:00.000Z")); + yield* scheduler.runDue(); + assert.equal((yield* store.get(orgA, id))?.nextRunAt, "2026-11-01T13:30:00.000Z"); + }).pipe(Effect.provide(fixture())), +); diff --git a/apps/server/src/kin/triggers/routines.testkit.ts b/apps/server/src/kin/triggers/routines.testkit.ts new file mode 100644 index 0000000000000..dc54c0344922d --- /dev/null +++ b/apps/server/src/kin/triggers/routines.testkit.ts @@ -0,0 +1,95 @@ +import { KinOrgId, KinMemoryItemId, MemoryItemHead } from "@t3tools/contracts"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import * as Schema from "effect/Schema"; +import * as SqlClient from "effect/sql/SqlClient"; +import * as Sqlite from "../../persistence/Sqlite.ts"; +import * as Scheduler from "../../scheduling/Scheduler.ts"; +import * as ThreadManagement from "../../orchestration-v2/ThreadManagementService.ts"; +import * as Memory from "../memory/Memory.ts"; +import * as Chat from "../chat/KinChatService.ts"; +import * as Repository from "../chat/KinChatRepository.ts"; +import * as Hub from "../chat/KinChatHub.ts"; +import * as Persistence from "../persistence/KinPersistence.ts"; +import * as EventQueue from "../lead/KinEventQueue.ts"; +import * as TriggerStore from "./KinTriggerStore.ts"; +import * as TriggerInbox from "./KinTriggerInbox.ts"; +import * as TriggerScheduler from "./KinTriggerScheduler.ts"; +import * as Routines from "./routines.ts"; +import * as OwnerCheck from "./ownerCheck.ts"; + +export const orgId = KinOrgId.make("a"); +export const otherOrg = KinOrgId.make("b"); +const timestamp = "2026-11-07T00:00:00.000Z"; +const decodeHead = Schema.decodeUnknownSync(MemoryItemHead); +export const item = (id: string, overrides: Partial = {}) => + decodeHead({ + orgId, + id: KinMemoryItemId.make(id), + path: `claims/${id}.md`, + kind: "claim", + title: id, + status: "open", + due: null, + recorded_at: "2026-09-01", + valid_from: "2026-09-01", + invalid_at: null, + superseded_by: null, + review_by: "2026-12-01", + source: "owner-chat", + said_by: "owner", + trust: "owner", + links: ["https://example.com/evidence"], + ...overrides, + }); +export function fixture(items: ReadonlyArray = []) { + const database = Persistence.layer.pipe(Layer.provideMerge(Sqlite.layerMemory)); + const repositories = Layer.mergeAll( + Repository.layer, + TriggerStore.layer, + TriggerInbox.layer, + EventQueue.layer, + ).pipe( + Layer.provideMerge(database), + Layer.provide(Layer.mock(ThreadManagement.ThreadManagementService)({})), + ); + const dependencies = Layer.mergeAll( + Layer.mock(Memory.Memory)({ + query: (orgId) => + Effect.succeed( + items + .filter((item) => item.orgId === orgId) + .map((item) => ({ ...item, duplicate: false })), + ), + readItemQuietly: (orgId, id) => { + const head = items.find((item) => item.orgId === orgId && item.id === id); + return head === undefined + ? Effect.fail(new Memory.MemoryNotFound({ orgId, id })) + : Effect.succeed({ head, body: "Body\n" }); + }, + }), + Chat.layer.pipe(Layer.provideMerge(Hub.layer)), + TriggerScheduler.layer, + ).pipe( + Layer.provideMerge(repositories), + Layer.provide(Layer.succeed(Scheduler.Scheduler, { register: () => Effect.void })), + ); + const owner = OwnerCheck.layer.pipe(Layer.provideMerge(dependencies)); + return Routines.layer.pipe( + Layer.provideMerge(owner), + Layer.provideMerge(Layer.succeed(Scheduler.Scheduler, { register: () => Effect.void })), + ); +} +export const seed = Effect.fn("test.routinesSeed")(function* () { + const sql = yield* SqlClient.SqlClient; + for (const org of [orgId, otherOrg]) { + const agent = `lead-${org}`; + yield* sql`INSERT INTO kin_orgs VALUES (${org}, 'Kin', ${timestamp})`; + yield* sql`INSERT INTO kin_people (person_id, org_id, handle, display_name, created_at) VALUES (${`owner-${org}`}, ${org}, 'owner', 'Owner', ${timestamp})`; + yield* sql`INSERT INTO kin_agents (agent_id, org_id, display_name, role, owner_person_id, created_at) VALUES (${agent}, ${org}, 'Lead', 'lead', ${`owner-${org}`}, ${timestamp})`; + yield* sql`INSERT INTO kin_chats (chat_id, org_id, kind, direct_key, last_seq, created_at) VALUES (${`chat-${org}`}, ${org}, 'direct', ${`direct-${org}`}, 0, ${timestamp})`; + yield* sql`INSERT INTO kin_chat_members (member_id, org_id, chat_id, principal_kind, person_id, role, joined_at) VALUES (${`owner-member-${org}`}, ${org}, ${`chat-${org}`}, 'person', ${`owner-${org}`}, 'owner', ${timestamp})`; + yield* sql`INSERT INTO kin_chat_members (member_id, org_id, chat_id, principal_kind, agent_id, role, joined_at) VALUES (${`lead-member-${org}`}, ${org}, ${`chat-${org}`}, 'agent', ${agent}, 'member', ${timestamp})`; + yield* sql`INSERT INTO kin_leads (agent_id, org_id, name, level_since, models_json, worker_projects_json, owner_chat_id) VALUES (${agent}, ${org}, 'Lead', 0, '{}', '[]', ${`chat-${org}`})`; + } +}); diff --git a/apps/server/src/kin/triggers/routines.ts b/apps/server/src/kin/triggers/routines.ts new file mode 100644 index 0000000000000..fc04cda1f0fbe --- /dev/null +++ b/apps/server/src/kin/triggers/routines.ts @@ -0,0 +1,358 @@ +import { + KinAgentId, + KinEvent, + KinEventId, + KinLeadRoutine, + KinOrgId, + KinTriggerId, + KinTriggerSettings, + type KinTrigger, +} from "@t3tools/contracts"; +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 Schema from "effect/Schema"; +import * as SqlClient from "effect/sql/SqlClient"; +import { isSqlError } from "effect/sql/SqlError"; +import { toPersistenceSqlError } from "../../persistence/Errors.ts"; +import { nextScheduledRunAt } from "../../scheduledTasks/Schedule.ts"; +import * as Scheduler from "../../scheduling/Scheduler.ts"; +import * as KinEventQueue from "../lead/KinEventQueue.ts"; +import * as KinTriggerInbox from "./KinTriggerInbox.ts"; +import * as KinTriggerStore from "./KinTriggerStore.ts"; +import * as KinTriggerScheduler from "./KinTriggerScheduler.ts"; +import * as OwnerCheck from "./ownerCheck.ts"; + +const routines = ["morning_brief", "evening_summary", "owner_check"] as const; +const iso = (now: number) => DateTime.formatIso(DateTime.makeUnsafe(now)); +const decodeSettings = Schema.decodeUnknownEffect(KinTriggerSettings); +const decodeEvent = Schema.decodeUnknownEffect(KinEvent); +const decodeQuietHours = Schema.decodeUnknownEffect( + Schema.fromJsonString(KinTriggerSettings.fields.quietHours), +); +const encodeQuietHours = Schema.encodeEffect( + Schema.fromJsonString(KinTriggerSettings.fields.quietHours), +); +const decodeRoutine = Schema.decodeUnknownEffect(KinLeadRoutine); + +export function routineDisposition( + routine: KinLeadRoutine, + dueAt: string, + now: string, + settings: KinTriggerSettings, +) { + if (routine === "owner_check") return "post"; + const due = DateTime.makeZonedUnsafe(dueAt, { timeZone: settings.timeZone }); + const [hour = 0, minute = 0] = ( + routine === "morning_brief" ? settings.eveningSummary : settings.dream + ) + .split(":") + .map(Number); + let boundary = DateTime.setParts(due, { hour, minute, second: 0, millisecond: 0 }); + if ( + routine === "evening_summary" && + DateTime.toEpochMillis(boundary) <= DateTime.toEpochMillis(due) + ) + boundary = DateTime.add(boundary, { days: 1 }); + return Date.parse(now) < DateTime.toEpochMillis(boundary) ? "post" : "fold"; +} + +function schedule(routine: KinLeadRoutine, settings: KinTriggerSettings): KinTrigger["schedule"] { + switch (routine) { + case "morning_brief": + return { type: "fixed_time", timeOfDay: settings.morningBrief }; + case "evening_summary": + return { type: "fixed_time", timeOfDay: settings.eveningSummary, weekdays: [1, 2, 3, 4, 5] }; + case "owner_check": + return { + type: "fixed_time", + timeOfDay: settings.ownerCheck, + weekdays: [settings.ownerCheckWeekday], + }; + } +} + +export class KinRoutinesError extends Schema.TaggedError()("KinRoutinesError", { + orgId: KinOrgId, + cause: Schema.Defect(), +}) {} +export class KinRoutines extends Context.Service< + KinRoutines, + { + readonly isEnabled: ( + orgId: KinOrgId, + routine: KinLeadRoutine, + ) => Effect.Effect; + readonly configure: (settings: KinTriggerSettings) => Effect.Effect; + readonly setEnabled: ( + orgId: KinOrgId, + routine: KinLeadRoutine, + enabled: boolean, + ) => Effect.Effect; + readonly pauseAll: (orgId: KinOrgId) => Effect.Effect; + readonly resumeAll: (orgId: KinOrgId) => Effect.Effect; + readonly drain: (orgId: KinOrgId) => Effect.Effect; + } +>()("t3/kin/triggers/routines/KinRoutines") {} + +const make = Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + const store = yield* KinTriggerStore.KinTriggerStore; + const scheduler = yield* KinTriggerScheduler.KinTriggerScheduler; + const inbox = yield* KinTriggerInbox.KinTriggerInbox; + const queue = yield* KinEventQueue.KinEventQueue; + const ownerCheck = yield* OwnerCheck.OwnerCheck; + const ticks = yield* Scheduler.Scheduler; + const settingsFor = Effect.fn("KinRoutines.settingsFor")(function* (orgId: KinOrgId) { + const [row] = yield* sql<{ + time_zone: string; + morning_brief: string; + evening_summary: string; + dream: string; + full_rebuild_weekday: number; + owner_check_weekday: number; + owner_check: string; + quiet_hours_json: string; + }>`SELECT * FROM kin_trigger_settings WHERE org_id = ${orgId} ORDER BY org_id`; + return yield* decodeSettings( + row === undefined + ? { + orgId, + timeZone: "UTC", + morningBrief: "08:30", + eveningSummary: "18:30", + dream: "03:00", + fullRebuildWeekday: 0, + ownerCheckWeekday: 1, + ownerCheck: "09:00", + quietHours: { from: "21:00", to: "08:00" }, + } + : { + orgId, + timeZone: row.time_zone, + morningBrief: row.morning_brief, + eveningSummary: row.evening_summary, + dream: row.dream, + fullRebuildWeekday: row.full_rebuild_weekday, + ownerCheckWeekday: row.owner_check_weekday, + ownerCheck: row.owner_check, + quietHours: yield* decodeQuietHours(row.quiet_hours_json), + }, + ); + }); + const reconcile = Effect.fn("KinRoutines.reconcile")(function* (settings: KinTriggerSettings) { + const now = yield* Clock.currentTimeMillis; + for (const routine of routines) { + const id = KinTriggerId.make(`routine:${routine}`); + const existing = yield* store.get(settings.orgId, id); + yield* sql`INSERT INTO kin_routine_preferences (org_id, routine, enabled) VALUES (${settings.orgId}, ${routine}, ${existing?.enabled === false ? 0 : 1}) ON CONFLICT DO NOTHING RETURNING routine`; + const [preference] = yield* sql<{ + enabled: number; + paused: number; + }>`SELECT enabled, paused FROM kin_routine_preferences WHERE org_id = ${settings.orgId} AND routine = ${routine} ORDER BY routine`; + const enabled = preference?.enabled === 1 && preference.paused === 0; + if (existing !== undefined && existing.enabled === enabled) continue; + const timing = schedule(routine, settings); + const next = enabled + ? nextScheduledRunAt(timing, DateTime.makeZonedUnsafe(now, { timeZone: settings.timeZone })) + : null; + yield* store.put({ + id, + orgId: settings.orgId, + subject: { type: "routine", routine }, + schedule: timing, + timeZone: settings.timeZone, + enabled, + nextRunAt: next === null ? null : DateTime.formatIso(next), + createdAt: existing?.createdAt ?? iso(now), + updatedAt: iso(Math.max(now, Date.parse(existing?.updatedAt ?? iso(now)))), + }); + } + yield* scheduler.recompute(settings); + }); + const wrap = (orgId: KinOrgId, effect: Effect.Effect) => + effect.pipe(Effect.mapError((cause) => new KinRoutinesError({ orgId, cause }))); + const configure = Effect.fn("KinRoutines.configure")((input: KinTriggerSettings) => + wrap( + input.orgId, + sql + .withTransaction( + Effect.gen(function* () { + const settings = yield* decodeSettings(input); + yield* sql`INSERT INTO kin_trigger_settings (org_id, time_zone, morning_brief, evening_summary, dream, full_rebuild_weekday, owner_check_weekday, owner_check, quiet_hours_json) + VALUES (${settings.orgId}, ${settings.timeZone}, ${settings.morningBrief}, ${settings.eveningSummary}, ${settings.dream}, ${settings.fullRebuildWeekday}, ${settings.ownerCheckWeekday}, ${settings.ownerCheck}, ${yield* encodeQuietHours(settings.quietHours)}) + ON CONFLICT (org_id) DO UPDATE SET time_zone = excluded.time_zone, morning_brief = excluded.morning_brief, evening_summary = excluded.evening_summary, dream = excluded.dream, full_rebuild_weekday = excluded.full_rebuild_weekday, owner_check_weekday = excluded.owner_check_weekday, owner_check = excluded.owner_check, quiet_hours_json = excluded.quiet_hours_json RETURNING org_id`; + yield* reconcile(settings); + }), + ) + .pipe( + Effect.mapError((cause) => + isSqlError(cause) ? toPersistenceSqlError("KinRoutines.configure")(cause) : cause, + ), + ), + ), + ); + const toggle = Effect.fn("KinRoutines.toggle")( + (orgId: KinOrgId, routine: KinLeadRoutine | null, value: boolean) => + wrap( + orgId, + sql + .withTransaction( + Effect.gen(function* () { + const settings = yield* settingsFor(orgId); + yield* reconcile(settings); + if (routine === null) + yield* sql`UPDATE kin_routine_preferences SET paused = ${value ? 1 : 0} WHERE org_id = ${orgId}`; + else { + yield* decodeRoutine(routine); + yield* sql`UPDATE kin_routine_preferences SET enabled = ${value ? 1 : 0} WHERE org_id = ${orgId} AND routine = ${routine}`; + } + yield* reconcile(settings); + if ((routine === null && value) || (routine !== null && !value)) { + yield* sql`UPDATE kin_events SET state = 'expired' WHERE org_id = ${orgId} AND tag = 'routine' AND state = 'queued' AND (${routine} IS NULL OR json_extract(json, '$.routine') = ${routine})`; + if (routine !== null) + yield* sql`DELETE FROM kin_routine_folds WHERE org_id = ${orgId} AND routine = ${routine}`; + } + }), + ) + .pipe( + Effect.mapError((cause) => + isSqlError(cause) ? toPersistenceSqlError("KinRoutines.toggle")(cause) : cause, + ), + ), + ), + ); + const drain = Effect.fn("KinRoutines.drain")((orgId: KinOrgId) => + wrap( + orgId, + Effect.gen(function* () { + const now = yield* Clock.currentTimeMillis; + const pending = yield* inbox.listPending(orgId, 100, routines); + for (const fire of pending.toSorted( + (a, b) => + Date.parse(a.dueAt) - Date.parse(b.dueAt) || + (a.fireId < b.fireId ? -1 : a.fireId > b.fireId ? 1 : 0), + )) { + if (fire.payload.kind !== "time" || fire.payload.subject.type !== "routine") continue; + const name = fire.payload.subject.routine; + if (name !== "morning_brief" && name !== "evening_summary" && name !== "owner_check") + continue; + + const items = name === "owner_check" ? yield* ownerCheck.select(orgId) : undefined; + yield* sql + .withTransaction( + Effect.gen(function* () { + const settings = yield* settingsFor(orgId); + const unconsumed = + yield* sql`SELECT fire_id FROM kin_trigger_inbox WHERE org_id = ${orgId} AND fire_id = ${fire.fireId} AND consumed_at IS NULL ORDER BY fire_id`; + if (unconsumed.length === 0) return; + const [lead] = yield* sql<{ + agent_id: string; + }>`SELECT l.agent_id 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`; + if (lead === undefined) return; + const eventId = KinEventId.make( + `routine_${Array.from(new TextEncoder().encode(`${orgId.length}:${orgId}${fire.fireId}`), (byte) => byte.toString(16).padStart(2, "0")).join("")}`, + ); + const previous = + yield* sql`SELECT event_id FROM kin_events WHERE org_id = ${orgId} AND event_id = ${eventId} ORDER BY event_id`; + if (previous.length > 0) { + yield* inbox.markConsumed(orgId, [fire.fireId]); + return; + } + const [preference] = yield* sql<{ + enabled: number; + paused: number; + }>`SELECT enabled, paused FROM kin_routine_preferences WHERE org_id = ${orgId} AND routine = ${name} ORDER BY routine`; + if (preference?.enabled !== 1 || preference.paused === 1) { + yield* inbox.markConsumed(orgId, [fire.fireId]); + return; + } + if (routineDisposition(name, fire.dueAt, iso(now), settings) === "fold") { + const target = name === "morning_brief" ? "evening_summary" : "morning_brief"; + yield* sql`UPDATE kin_routine_folds SET fold_into = ${target} WHERE org_id = ${orgId} AND fold_into = ${name}`; + yield* sql`INSERT INTO kin_routine_folds (org_id, fire_id, routine, due_at, fold_into) VALUES (${orgId}, ${fire.fireId}, ${name}, ${Date.parse(fire.dueAt)}, ${target}) ON CONFLICT DO NOTHING RETURNING fire_id`; + } else { + const folds = yield* sql<{ + routine: string; + due_at: number; + }>`SELECT routine, due_at FROM kin_routine_folds WHERE org_id = ${orgId} AND fold_into = ${name} ORDER BY due_at, fire_id`; + const event = yield* decodeEvent({ + _tag: "routine", + eventId, + orgId, + agentId: KinAgentId.make(lead.agent_id), + receivedAt: iso(now), + provenance: { _tag: "system" }, + dedupeKey: `routine:${orgId}:${fire.fireId}`, + routine: name, + dueAt: fire.dueAt, + lateByMs: now - Date.parse(fire.dueAt), + foldedRoutines: folds.map((fold) => ({ + routine: fold.routine, + dueAt: iso(fold.due_at), + })), + ...(items === undefined ? {} : { ownerCheckItems: items }), + }); + yield* queue.enqueue(event); + yield* sql`DELETE FROM kin_routine_folds WHERE org_id = ${orgId} AND fold_into = ${name}`; + } + yield* inbox.markConsumed(orgId, [fire.fireId]); + }), + ) + .pipe( + Effect.mapError((cause) => + isSqlError(cause) ? toPersistenceSqlError("KinRoutines.drain")(cause) : cause, + ), + ); + } + }), + ), + ); + yield* ticks.register( + "kin-routines", + Effect.gen(function* () { + const orgs = yield* sql<{ + org_id: string; + }>`SELECT org_id FROM kin_leads WHERE org_id IN (SELECT org_id FROM kin_orgs) ORDER BY org_id`; + for (const row of orgs) { + const orgId = KinOrgId.make(row.org_id); + yield* Effect.gen(function* () { + yield* sql.withTransaction( + Effect.gen(function* () { + yield* reconcile(yield* settingsFor(orgId)); + }), + ); + yield* drain(orgId); + }).pipe( + Effect.catch((cause) => Effect.logWarning("Kin routines tick failed", { orgId, cause })), + ); + } + }).pipe(Effect.catch((cause) => Effect.logWarning("Kin routines scan failed", { cause }))), + ); + return KinRoutines.of({ + isEnabled: Effect.fn("KinRoutines.isEnabled")((orgId, routine) => + wrap( + orgId, + Effect.gen(function* () { + const [row] = yield* sql<{ + enabled: number; + paused: number; + }>`SELECT enabled, paused FROM kin_routine_preferences WHERE org_id = ${orgId} AND routine = ${routine} ORDER BY routine`.pipe( + Effect.mapError(toPersistenceSqlError("KinRoutines.isEnabled")), + ); + return row === undefined || (row.enabled === 1 && row.paused === 0); + }), + ), + ), + configure, + setEnabled: Effect.fn("KinRoutines.setEnabled")((orgId, routine, enabled) => + toggle(orgId, routine, enabled), + ), + pauseAll: Effect.fn("KinRoutines.pauseAll")((orgId) => toggle(orgId, null, true)), + resumeAll: Effect.fn("KinRoutines.resumeAll")((orgId) => toggle(orgId, null, false)), + drain, + }); +}); +export const layer = Layer.effect(KinRoutines, make); diff --git a/apps/server/src/kin/widgets/registry.ts b/apps/server/src/kin/widgets/registry.ts index 59c189ee84d71..31b038ab429bf 100644 --- a/apps/server/src/kin/widgets/registry.ts +++ b/apps/server/src/kin/widgets/registry.ts @@ -1,8 +1,11 @@ -import { KinChatError, KinTaskCardState } from "@t3tools/contracts"; +import { KinChatError, KinTaskCardState, KinOwnerCheckCard } from "@t3tools/contracts"; import * as Effect from "effect/Effect"; import * as Schema from "effect/Schema"; -const schemas = new Map>([["task", KinTaskCardState]]); +const schemas = new Map>([ + ["task", KinTaskCardState], + ["owner_check", KinOwnerCheckCard], +]); export const validateState = Effect.fn("KinWidgetRegistry.validateState")(function* ( type: string, diff --git a/docs/kin/specs/lead.md b/docs/kin/specs/lead.md index 1af0887e12e96..69bbae74d83c4 100644 --- a/docs/kin/specs/lead.md +++ b/docs/kin/specs/lead.md @@ -494,7 +494,7 @@ RPC (**new**, through the chat spec's `kinRpcs.ts`): `kin.org.pause` and `kin.or ### Morning and evening routines: content -Scheduling is the triggers spec's. Each fires a `routine` event, and the lead writes the message. `routines.ts` holds these templates. Each is one message of at most 12 lines, plus widgets, posted to the owner's direct chat (shadow summary at L0). +Scheduling is the triggers spec's. Each fires a `routine` event, and the lead writes the message. A request carries its original due time, lateness and any folded routine dates; content must acknowledge those dates rather than imply an on-time run. The owner check selects memory-review items in code and posts its native card after budget admission; its taps use owner-authored writer sources, not model authority. `routines.ts` holds these templates. Each is one message of at most 12 lines, plus widgets, posted to the owner's direct chat (shadow summary at L0). Morning brief, from the dream's prepared brief plus a live recheck: @@ -649,7 +649,7 @@ LEAD-14. **End-to-end shadow week dry run.** Script that replays a day of record `KinSecretScan` is deliberately supplied separately. `noFindingsStubLayer` returns no findings until TRIG-16 replaces it; it is not a production secret detector. Any finding or scanner failure blocks the whole commit. TRIG-16 must preserve this fail-closed interface: `scan({ orgId, files: ReadonlyArray<{ path: string; content: Uint8Array }> })` returns `{ path, ruleId, line }[]`, with typed `KinSecretScanError`. -`commitWakeup` builds TRIG-12's sectioned `INDEX.md` from the hash-verified recorded tree plus staged files, read history, the server clock and the owner's time zone, in the same wake-up commit. The read-side rebuild is display-only; no read-checkout text is trusted. K006 adds the SQLite branch anchors required by the writer. The runner serializes prepare/stage/commit and dreaming per organization. Identical commit retries find the existing wake-up commit; failed pushes retain local commits, and failures discard uncommitted files. Journal additions use `appendJournal`, never replacement through `stage`. +`commitWakeup` builds TRIG-12's sectioned `INDEX.md` from the hash-verified recorded tree plus staged files, read history, the server clock and the owner's time zone, in the same wake-up commit. The read-side rebuild is display-only; no read-checkout text is trusted. K011 adds the SQLite branch anchors required by the writer. The runner serializes prepare/stage/commit and dreaming per organization. Identical commit retries find the existing wake-up commit; failed pushes retain local commits, and failures discard uncommitted files. Journal additions use `appendJournal`, never replacement through `stage`. ### Memory writer operations @@ -657,7 +657,7 @@ LEAD-14. **End-to-end shadow week dry run.** Script that replays a day of record `prepareWakeup` checks git integrity once. Every application object read independently checks its hash. Three-way merging walks verified commit parents to find a common ancestor, then reads base, local and remote bytes with hash verification. Paths changed on both sides conflict except append-only journals and the regenerated INDEX.md. Journal append conflicts retain remote bytes followed by local entries in order. Original modes, symlinks and gitlinks are preserved; writer changes create only regular files. Other conflicts preserve the unpushed tip under `kin/conflict//-` (or a server-generated recovery id when no wake-up id is supplied), then compare-and-swap the writer and anchor to the remote head and notify via `MemoryRecoveryHooks`. The next wake-up proceeds; the owner can inspect and cherry-pick the conflict branch. Existing conflict branches are never intentionally overwritten. -Operators or owners can call server-only `Memory.recover(orgId)` after losing a writer or to reconcile its anchors. It fetches remote main and shadow heads, preserves surviving unpushed anchored work on a conflict branch, recreates a missing writer, records remote anchors and notifies. This method must never be registered as an agent tool. For writers predating K006 anchors, stop the runner and delete only that organization's writer directory, then bootstrap again. Do not delete anchored writers to recover unpushed work; call `recover` first. Recovery cannot restore commits lost with a deleted writer before they reached any remote. +Operators or owners can call server-only `Memory.recover(orgId)` after losing a writer or to reconcile its anchors. It fetches remote main and shadow heads, preserves surviving unpushed anchored work on a conflict branch, recreates a missing writer, records remote anchors and notifies. This method must never be registered as an agent tool. For writers predating K011 anchors, stop the runner and delete only that organization's writer directory, then bootstrap again. Do not delete anchored writers to recover unpushed work; call `recover` first. Recovery cannot restore commits lost with a deleted writer before they reached any remote. Each wake-up must start a fresh provider process with cwd set to the newly replaced read clone; LEAD-5 creates a fresh provider session for every wake-up. diff --git a/docs/kin/specs/triggers.md b/docs/kin/specs/triggers.md index 1890e5a05a9df..7cc7c62fb723d 100644 --- a/docs/kin/specs/triggers.md +++ b/docs/kin/specs/triggers.md @@ -250,6 +250,10 @@ The dream consumer suppresses a `dream` fire whose `dueAt`, in owner time, falls | `dream_full` | The same pipeline over all items, not just new material. | | `owner_check` | Code picks 5 items: past `review_by`, flagged misleading, low-trust claims older than 30 days, and evidence-free status claims, in that order. It posts a native card with Keep (push `review_by` out one period), Update (opens the direct chat on the item) and Drop (set `invalid_at` now). Taps are owner-authored memory writes. | +Routine delivery is a lead-queue request, not a second posting path: TRIG-9 enqueues `routine`; LEAD-13 owns the brief/summary message and prepared-brief content. Late boundaries are measured from the scheduled owner's calendar date (a summary expires at the first dream after its due time). Folds retain the original routine and due instant, survive restart, and move with an expired summary into the next brief. Same-tick fires are handled by due time, not inbox receipt order. Disabling a routine expires its queued requests; enabling it schedules the next slot without backfill. Pause/resume preserves individual opt-outs. + +Owner-check ties use review date (missing last), then item id and path. Invalid, superseded and duplicate-id items are excluded. Low trust means any trust below owner; with no status-claim subtype in memory, evidence-free means a claim with no evidence links. Keep sets the next review to now +90 days for people or +30 days otherwise, so repeated Keeps do not grow the review interval. Update returns the owner direct-chat/item target; Keep and Drop stage through the writer with the verified, claimed owner action as their source, never model-supplied provenance. Native clients own presenting the three actions; the server owns their authorization. + ### Watchdogs (`interval`) All are plain code. They write memory only through the LEAD queue as code-only events, which keeps one writer per org (gap 1). diff --git a/packages/contracts/src/kinLead.ts b/packages/contracts/src/kinLead.ts index c3ec57063bcd5..72298d6c68f41 100644 --- a/packages/contracts/src/kinLead.ts +++ b/packages/contracts/src/kinLead.ts @@ -147,6 +147,17 @@ const EventBase = { dedupeKey: Schema.NullOr(TrimmedNonEmptyString), provenance: KinAuthor, }; +export const KinOwnerCheckCard = Schema.Struct({ + items: Schema.Array( + Schema.Struct({ + itemId: KinMemoryItemId, + reason: Schema.Literals(["past_review", "misleading", "old_low_trust", "no_evidence"]), + title: Schema.String, + path: Schema.String, + }), + ), +}); + export const KinEvent = Schema.Union([ Schema.TaggedStruct("chat.message", { ...EventBase, chatId: KinChatId, messageId: KinMessageId }), Schema.TaggedStruct("widget.action", { @@ -184,6 +195,24 @@ export const KinEvent = Schema.Union([ Schema.TaggedStruct("routine", { ...EventBase, routine: KinLeadRoutine, + dueAt: Schema.optional(CanonicalUtcTimestamp), + lateByMs: Schema.optional(NonNegativeInt), + foldedRoutines: Schema.optional( + Schema.Array( + Schema.Struct({ + routine: KinLeadRoutine, + dueAt: CanonicalUtcTimestamp, + }), + ), + ), + ownerCheckItems: Schema.optional( + Schema.Array( + Schema.Struct({ + itemId: KinMemoryItemId, + reason: Schema.Literals(["past_review", "misleading", "old_low_trust", "no_evidence"]), + }), + ), + ), }), Schema.TaggedStruct("watchdog", { ...EventBase, From ae165f437335b4d28e8f1347b1ff7a3a44fcd0ad Mon Sep 17 00:00:00 2001 From: GLDRoger <130458023+GLDRoger@users.noreply.github.com> Date: Sun, 8 Nov 2026 13:35:52 +0530 Subject: [PATCH 04/15] test(kin): retain writer base pinned-read assertion --- apps/server/src/kin/memory/Memory.test.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/apps/server/src/kin/memory/Memory.test.ts b/apps/server/src/kin/memory/Memory.test.ts index 10f4c07c8815b..402e17e08e711 100644 --- a/apps/server/src/kin/memory/Memory.test.ts +++ b/apps/server/src/kin/memory/Memory.test.ts @@ -462,10 +462,7 @@ it.effect( expect( yield* sql`SELECT * FROM kin_memory_reads WHERE org_id = ${orgA} ORDER BY item_id`, ).toHaveLength(1); - expect(repository.counts()).toEqual({ - ...counts, - validations: counts.validations + 3, - }); + expect(repository.counts()).toEqual(counts); yield* TestClock.adjust("7 weeks"); yield* memory.readItem(orgA, itemId); const moved = repository.files.get("deferrals/def-001.md"); From 493c228b0df7e42a99cb38de60144ce4d67e5183 Mon Sep 17 00:00:00 2001 From: GLDRoger <130458023+GLDRoger@users.noreply.github.com> Date: Sun, 8 Nov 2026 13:37:18 +0530 Subject: [PATCH 05/15] test(kin): compose dormant routine dependencies in one layer --- apps/server/src/kin/lead/layer.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/server/src/kin/lead/layer.test.ts b/apps/server/src/kin/lead/layer.test.ts index f1002f3499c8c..4cf8917612a81 100644 --- a/apps/server/src/kin/lead/layer.test.ts +++ b/apps/server/src/kin/lead/layer.test.ts @@ -122,5 +122,5 @@ it.effect("leaves unconfigured Kin silent and inert without building its depende ), ); assert.deepStrictEqual(entries, []); - }).pipe(Effect.provide(external), Effect.provide(harness()), provideDeterministicTestRuntime), + }).pipe(Effect.provide(Layer.mergeAll(harness(), external)), provideDeterministicTestRuntime), ); From cb8f2ff7e6a6ab1f1ce5502cbee3ca4672050e6d Mon Sep 17 00:00:00 2001 From: GLDRoger <130458023+GLDRoger@users.noreply.github.com> Date: Sun, 8 Nov 2026 14:12:17 +0530 Subject: [PATCH 06/15] fix(kin): bound secret scanning and scan only merged changes --- apps/server/src/kin/memory/Memory.test.ts | 24 +++- apps/server/src/kin/memory/Memory.ts | 12 +- .../src/kin/memory/MemoryWriter.test.ts | 16 +++ apps/server/src/kin/memory/MemoryWriter.ts | 16 ++- apps/server/src/kin/memory/secretScan.test.ts | 96 ++++++++++++- apps/server/src/kin/memory/secretScan.ts | 132 +++++++++++++----- .../migrations/K012_SecretAllow.ts | 1 + docs/kin/specs/triggers.md | 8 +- 8 files changed, 251 insertions(+), 54 deletions(-) diff --git a/apps/server/src/kin/memory/Memory.test.ts b/apps/server/src/kin/memory/Memory.test.ts index 0188ef14accd0..674998fcc7b2f 100644 --- a/apps/server/src/kin/memory/Memory.test.ts +++ b/apps/server/src/kin/memory/Memory.test.ts @@ -859,6 +859,8 @@ it.live("uses filesystem identity for a case-mismatched base without recloning", it.live.each([ "diverged", "secret merge", + "remote secret merge", + "large remote merge", "conflict", "network down", "dirty writer", @@ -933,7 +935,11 @@ it.live.each([ } else yield* fs.writeFileString( `${writer}/rules.md`, - scenario === "H2-merge-swap" ? h2Local : "Local invented rules\n", + scenario === "H2-merge-swap" + ? h2Local + : scenario === "secret merge" + ? ["gh", "p_", "a".repeat(36)].join("") + : "Local invented rules\n", ); let local = yield* commit(writer); if (scenario === "push retry") { @@ -953,15 +959,20 @@ it.live.each([ } yield* memory.record(orgA, "main", initial.head, local); yield* fs.writeFileString( - `${source}/${scenario === "push retry" || scenario === "diverged" || scenario === "secret merge" || scenario === "network down" || scenario === "dirty writer" || scenario === "H2-merge-swap" ? "remote-only" : "rules.md"}`, + `${source}/${scenario === "push retry" || scenario === "diverged" || scenario === "secret merge" || scenario === "remote secret merge" || scenario === "large remote merge" || scenario === "network down" || scenario === "dirty writer" || scenario === "H2-merge-swap" ? "remote-only" : "rules.md"}`, "Remote invented edit\n", ); if (scenario === "journal conflict" || scenario === "push retry") { yield* fs.writeFileString(`${source}/journal/day.md`, "remote entry\n"); yield* fs.writeFileString(`${source}/INDEX.md`, "remote generated index\n"); } - if (scenario === "secret merge") + if (scenario === "remote secret merge") yield* fs.writeFileString(`${source}/remote-only`, ["gh", "p_", "a".repeat(36)].join("")); + if (scenario === "large remote merge") { + yield* fs.makeDirectory(`${source}/archive`); + for (let i = 0; i < 1030; i++) + yield* fs.writeFileString(`${source}/archive/${i}.txt`, "clean"); + } if (scenario === "H2-merge-swap") yield* fs.writeFileString(`${source}/rules.md`, h2Base.replace("base tail", "remote tail")); yield* commit(source); @@ -1076,7 +1087,8 @@ it.live.each([ } else if ( scenario === "network down" || scenario === "diverged" || - scenario === "secret merge" || + scenario === "remote secret merge" || + scenario === "large remote merge" || scenario === "dirty writer" ) { expect(result._tag).toBe("Success"); @@ -1086,7 +1098,9 @@ it.live.each([ expect(result.success.head).not.toBe(local); expect(yield* memory.readRules(orgA)).toBe("Local invented rules\n"); expect(yield* git(writer, ["show", `${result.success.head}:remote-only`])).toBe( - "Remote invented edit", + scenario === "remote secret merge" + ? ["gh", "p_", "a".repeat(36)].join("") + : "Remote invented edit", ); } } diff --git a/apps/server/src/kin/memory/Memory.ts b/apps/server/src/kin/memory/Memory.ts index f74f6557e43b2..d2d35e8ae1c84 100644 --- a/apps/server/src/kin/memory/Memory.ts +++ b/apps/server/src/kin/memory/Memory.ts @@ -1071,6 +1071,7 @@ const make = Effect.gen(function* () { }); } else { const { files: mergedFiles, modes } = merged; + const remoteOriginal = new Map(remoteFiles); remoteFiles.clear(); for (const [file, bytes] of mergedFiles) remoteFiles.set(file, bytes); const heads = []; @@ -1093,7 +1094,12 @@ const make = Effect.gen(function* () { const findings = yield* scanner .scan({ orgId, - files: [...remoteFiles].map(([path, content]) => ({ path, content })), + files: [...remoteFiles] + .filter( + ([path, bytes]) => + path === "INDEX.md" || !remoteOriginal.get(path)?.equals(bytes), + ) + .map(([path, content]) => ({ path, content })), }) .pipe( Effect.mapError( @@ -1104,12 +1110,12 @@ const make = Effect.gen(function* () { yield* notify({ orgId, branch, - reason: `Secret scan blocked merge: ${findings[0]!.ruleId} in ${findings[0]!.path}`, + reason: `Secret scan blocked merge: ${findings[0]!.ruleId} in ${findings[0]!.pathHit ? "[redacted]" : findings[0]!.path}`, }); return yield* new MemoryError({ orgId, operation: "secretScan", - reason: `Secret scan blocked merge: ${findings[0]!.ruleId} in ${findings[0]!.path}`, + reason: `Secret scan blocked merge: ${findings[0]!.ruleId} in ${findings[0]!.pathHit ? "[redacted]" : findings[0]!.path}`, }); } tree = yield* Effect.scoped( diff --git a/apps/server/src/kin/memory/MemoryWriter.test.ts b/apps/server/src/kin/memory/MemoryWriter.test.ts index 780093834215f..df01b3857d435 100644 --- a/apps/server/src/kin/memory/MemoryWriter.test.ts +++ b/apps/server/src/kin/memory/MemoryWriter.test.ts @@ -1,3 +1,4 @@ +import * as SqlClient from "effect/sql/SqlClient"; import { memoryGit } from "./git.ts"; import * as NodeZlib from "node:zlib"; import * as NodeServices from "@effect/platform-node/NodeServices"; @@ -23,6 +24,8 @@ it.live.each([ "completed", "failed", "blocked", + "scan unavailable", + "scan limit", "forged", "conflict", "B1-shadow", @@ -601,10 +604,23 @@ it.live.each([ yield* writer.appendJournal(orgId, "wake1", [ ["gh", "p_"].join("") + "aB3dE6gH9jK2mN5pQ8sT1vW4yZ7cF0iL3oR6", ]); + if (scenario === "scan unavailable") { + const sql = yield* SqlClient.SqlClient; + yield* sql`DROP TABLE kin_secret_allow`; + } + if (scenario === "scan limit") + yield* writer.appendJournal(orgId, "wake1", ["a".repeat(1024 * 1024)]); const result = yield* writer .commitWakeup(orgId, "wake1", scenario === "failed" ? "failed" : "completed") .pipe(Effect.result); if (result._tag === "Failure") yield* Effect.logError(result.failure); + if (scenario === "scan unavailable" || scenario === "scan limit") { + expect(result._tag).toBe("Failure"); + expect(notices).toHaveLength(1); + expect(notices[0]!.error._tag).toBe("KinSecretScanError"); + expect(yield* git(paths.writer, ["rev-parse", "HEAD"])).toBe(original); + return; + } if (scenario === "tree-substitution") { expect(result._tag).toBe("Failure"); if (result._tag === "Failure") expect(result.failure._tag).toBe("MemoryCheckoutInvalid"); diff --git a/apps/server/src/kin/memory/MemoryWriter.ts b/apps/server/src/kin/memory/MemoryWriter.ts index b5fd6ed3c4886..a45193e5275cc 100644 --- a/apps/server/src/kin/memory/MemoryWriter.ts +++ b/apps/server/src/kin/memory/MemoryWriter.ts @@ -61,14 +61,23 @@ export class MemorySecretBlocked extends Schema.TaggedError { orgId: Schema.String, findings: Schema.Array( - Schema.Struct({ path: Schema.String, ruleId: Schema.String, line: Schema.Number }), + Schema.Struct({ + path: Schema.String, + ruleId: Schema.String, + line: Schema.Number, + pathHit: Schema.optional(Schema.Boolean), + }), ), }, ) {} export type WriterNotice = { readonly orgId: KinOrgId; readonly wakeupId: string; - readonly error: MemoryCheckoutInvalid | MemorySyncConflict | MemorySecretBlocked; + readonly error: + | MemoryCheckoutInvalid + | MemorySyncConflict + | MemorySecretBlocked + | KinSecretScanError; }; export class MemoryWriterHooks extends Context.Service< MemoryWriterHooks, @@ -297,7 +306,8 @@ const make = Effect.gen(function* () { Effect.tapError((error) => error._tag === "MemoryCheckoutInvalid" || error._tag === "MemorySyncConflict" || - error._tag === "MemorySecretBlocked" + error._tag === "MemorySecretBlocked" || + error._tag === "KinSecretScanError" ? hooks .notify({ orgId, wakeupId, error }) .pipe( diff --git a/apps/server/src/kin/memory/secretScan.test.ts b/apps/server/src/kin/memory/secretScan.test.ts index db74dd37b5f6d..63ff116caed3c 100644 --- a/apps/server/src/kin/memory/secretScan.test.ts +++ b/apps/server/src/kin/memory/secretScan.test.ts @@ -26,8 +26,12 @@ const fixtures = [ ["github_", "pat_short"].join(""), ], ["anthropic-key", ["sk-", "ant-"].join("") + body.repeat(2), ["sk-", "ant-short"].join("")], - ["openai-key", "sk-" + body, "sk-short"], - ["openai-key", ["sk-", "proj-"].join("") + body.repeat(3), "sk-proj-short"], + ["openai-key", "sk-" + body.slice(0, 20) + "T3BlbkFJ" + body.slice(0, 20), "sk-short"], + ...["proj", "svcacct", "admin"].map((kind) => [ + "openai-key", + ["sk-", kind, "-", body.repeat(3)].join(""), + ["sk-", kind, "-", "a".repeat(40)].join(""), + ]), ...["AK", "AS"].map((kind) => [ "aws-access-key", kind + "IA" + "X".repeat(16), @@ -68,6 +72,22 @@ it.effect("blocks encoded, wrapped and confusable tokens with safe findings", () const scanner = yield* Scan.KinSecretScan; const values = [ github, + ...["/", "-", "_", "+", "word"].map( + (prefix) => prefix + Buffer.from(github).toString("base64"), + ), + Buffer.from("é" + github + "é").toString("base64"), + [...Buffer.from(github.slice(0, 5) + "\u034f" + github.slice(5))] + .map((byte) => `%${byte.toString(16).padStart(2, "0")}`) + .join(""), + "%FF" + [...github].map((c) => `%${c.charCodeAt(0).toString(16)}`).join(""), + ...["\u00ad", "\u034f", "\ufe0f", "\u{e0061}"].map( + (separator) => github.slice(0, 5) + separator + github.slice(5), + ), + github.replace("g", "ɡ"), + github.replace("h", "һ"), + ["gh", "ѕ_", body].join(""), + ["gh", "ο_", body].join(""), + " ".repeat(8190) + github, Buffer.from(github).toString("base64"), Buffer.from(Buffer.from(github).toString("base64")).toString("base64"), [...github].map((c) => `%${c.charCodeAt(0).toString(16)}`).join(""), @@ -123,7 +143,7 @@ it.effect("allowlist is exact, org scoped, and read only to scanner callers", () .update(github) .digest("hex"); // Fixture stands in for the future APPR owner-signed executor; scanner has no write method. - yield* sql`INSERT INTO kin_secret_allow VALUES (${orgId}, 'claims/test.md', 'github-token', ${hash}, 'signed-fixture')`; + yield* sql`INSERT INTO kin_secret_allow VALUES (${orgId}, 'claims/test.md', 'github-token', ${hash}, 'signed-fixture', 0)`; const run = (org: KinOrgId, path: string, value = github) => scanner.scan({ orgId: org, files: [{ path, content: Buffer.from(value) }] }); expect((yield* run(orgId, "claims/test.md")).length).toBe(0); @@ -150,7 +170,7 @@ it.effect("fails closed when decoded findings or the allowlist exceed their boun ).toBe("Failure"); const sql = yield* SqlClient.SqlClient; yield* sql`WITH RECURSIVE entries(n) AS (SELECT 1 UNION ALL SELECT n + 1 FROM entries WHERE n < 4097) - INSERT INTO kin_secret_allow SELECT ${orgId}, 'claims/' || n, 'github-token', ${"0".repeat(64)}, 'signed-fixture' FROM entries`; + INSERT INTO kin_secret_allow SELECT ${orgId}, 'claims/' || n, 'github-token', ${"0".repeat(64)}, 'signed-fixture', 0 FROM entries`; expect((yield* scanner.scan({ orgId, files: [] }).pipe(Effect.result))._tag).toBe("Failure"); }).pipe(Effect.provide(layer)), ); @@ -164,9 +184,75 @@ it.effect("fails closed at file, batch and count limits", () => Array.from({ length: 1025 }, () => ({ path: "count", content: Buffer.alloc(0) })), ]) expect((yield* scan(files).pipe(Effect.result))._tag).toBe("Failure"); - const content = Buffer.from("a".repeat(1024 * 1024 - github.length - 1) + "\n" + github); + const content = Buffer.from( + "a".repeat(1024 * 1024 - github.length - 1 - "large".length) + "\n" + github, + ); expect( (yield* scan([{ path: "large", content }])).some((f) => f.ruleId === "github-token"), ).toBe(true); }).pipe(Effect.provide(layer)), ); + +it.effect("round 2 near misses and private key headers", () => + Effect.gen(function* () { + const scanner = yield* Scan.KinSecretScan; + for (const content of [ + "task-follow-up-with-design-team-on-onboarding", + "risk-follow-up-with-design-team-on-onboarding", + "desk-follow-up-with-design-team-on-onboarding", + "token: 0123456789abcdef0123456789abcdef01234567", + "Password: https://vault.example.invalid/credentials/team", + "secret: projects/example-project/secrets/database/versions/latest", + "github_token_scopes: repo,workflow,admin:org,read:user", + "postgres://user:${DB_PASSWORD}@example.invalid/db", + "ASIAPACIFICREGION2026", + ]) + expect( + (yield* scanner.scan({ + orgId, + files: [{ path: "claims/test.md", content: Buffer.from(content) }], + })).length, + ).toBe(0); + for (const content of [ + "-----BEGIN PGP PRIVATE KEY BLOCK-----", + "-----BEGIN CUSTOM PRIVATE KEY-----", + ]) + expect( + (yield* scanner.scan({ + orgId, + files: [{ path: "claims/test.md", content: Buffer.from(content) }], + })).some((f) => f.ruleId === "pem-private-key"), + ).toBe(true); + }).pipe(Effect.provide(layer)), +); +it.effect("round 2 scans paths without exposing them", () => + Effect.gen(function* () { + const scanner = yield* Scan.KinSecretScan; + const findings = yield* scanner.scan({ + orgId, + files: [{ path: `claims/${github}.md`, content: Buffer.from("clean") }], + }); + expect(findings.some((f) => f.ruleId === "github-token")).toBe(true); + expect(JSON.stringify(findings).includes(github)).toBe(false); + }).pipe(Effect.provide(layer)), +); +it.effect("round 2 bounds adversarial rule inputs structurally", () => + Effect.gen(function* () { + const scanner = yield* Scan.KinSecretScan; + for (const prefix of ["eyJ", "sk-", "AKIA"]) { + const content = prefix.repeat(Math.floor((1024 * 1024 - 100) / prefix.length)); + let examined = 0; + for (const window of Scan.ruleWindows(content)) { + expect(window.text.length).toBeLessThanOrEqual(8192); + examined += window.text.length; + } + expect(examined).toBeLessThanOrEqual(content.length * 2); + expect( + (yield* scanner.scan({ + orgId, + files: [{ path: "claims/test.md", content: Buffer.from(content) }], + })).length, + ).toBe(0); + } + }).pipe(Effect.provide(layer)), +); diff --git a/apps/server/src/kin/memory/secretScan.ts b/apps/server/src/kin/memory/secretScan.ts index 3f5e0a4430121..cdf2ae08a9bd5 100644 --- a/apps/server/src/kin/memory/secretScan.ts +++ b/apps/server/src/kin/memory/secretScan.ts @@ -6,17 +6,20 @@ import * as Context from "effect/Context"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import * as Schema from "effect/Schema"; +import { toPersistenceSqlError } from "../../persistence/Errors.ts"; export class KinSecretScanError extends Schema.TaggedError()( "KinSecretScanError", { reason: Schema.String, + cause: Schema.optional(Schema.Defect()), }, ) {} export interface SecretFinding { readonly path: string; readonly ruleId: string; readonly line: number; + readonly pathHit?: boolean; } export class KinSecretScan extends Context.Service< KinSecretScan, @@ -31,19 +34,27 @@ export class KinSecretScan extends Context.Service< const rules = [ ["github-token", /(?:gh[pousr]_[A-Za-z0-9]{36}|github_pat_[A-Za-z0-9_]{82})/], ["anthropic-key", /sk-ant-[A-Za-z0-9_-]{40,200}/], - ["openai-key", /sk-(?!ant-)(?:proj-)?[A-Za-z0-9_-]{32,200}/], + [ + "openai-key", + /sk-(?:(?:proj|svcacct|admin)-[A-Za-z0-9_-]{32,200}|[A-Za-z0-9]{20}T3BlbkFJ[A-Za-z0-9]{20})/, + ], ["aws-access-key", /(?:AKIA|ASIA)[A-Z0-9]{16}/], ["slack-token", /xox[abposr]-[A-Za-z0-9-]{20,200}/], ["stripe-key", /[rs]k_live_[A-Za-z0-9]{24,200}/], ["google-key", /AIza[A-Za-z0-9_-]{35}/], - ["pem-private-key", /-----BEGIN (?:(?:RSA|EC|DSA|OPENSSH|ENCRYPTED) )?PRIVATE KEY-----/], - ["jwt", /eyJ[A-Za-z0-9_-]{5,8192}\.[A-Za-z0-9_-]{8,8192}\.[A-Za-z0-9_-]{16,8192}/], + ["pem-private-key", /-----BEGIN (?:[A-Z0-9]+ ){0,8}PRIVATE KEY(?: BLOCK)?-----/], + ["jwt", /eyJ[A-Za-z0-9_-]{5,1024}\.[A-Za-z0-9_-]{8,1024}\.[A-Za-z0-9_-]{16,1024}/], ["url-password", /\b[a-z][a-z0-9+.-]{0,31}:\/\/[^\s/:@]{1,512}:([^\s/@]{1,512})@/i], [ "secret-assignment", - /\b[A-Za-z0-9_]{0,64}(?:password|secret|token|api[_-]?key|access[_-]?key)[A-Za-z0-9_]{0,64}\s*[:=]\s*["']?([^\s"';#]{20,512})/i, + /\b[A-Za-z0-9_]{0,64}(?:password|secret|token|api[_-]?key|access[_-]?key)[A-Za-z0-9_]{0,64}\s{0,32}[:=]\s{0,32}["']?([^\s"';#]{20,512})/i, ], ] as const; +// Every rule sees bounded windows; overlap exceeds the longest possible match (a JWT). +export const ruleWindows = function* (value: string) { + for (let offset = 0; offset < value.length; offset += 4096) + yield { text: value.slice(offset, offset + 8192), offset }; +}; const entropy = (value: string) => { const counts = new Map(); for (const char of value) counts.set(char, (counts.get(char) ?? 0) + 1); @@ -56,6 +67,10 @@ const entropy = (value: string) => { }; // NFKC covers fullwidth forms; common Cyrillic/Greek confusables are normalized as well. const lookalikes: Readonly> = { + ѕ: "s", + һ: "h", + ο: "o", + ɡ: "g", а: "a", е: "e", о: "o", @@ -91,8 +106,8 @@ const lookalikes: Readonly> = { const normalize = (text: string) => text .normalize("NFKC") - .replace(/[\u200B-\u200F\u202A-\u202E\u2060-\u206F\uFEFF\uFFFD]/g, "") - .replace(/[аеорсхуіјАВЕКМНОРСТХΑΒΕΙΚΜΝΟΡΤΧ]/g, (c) => lookalikes[c] ?? c); + .replace(/\p{Default_Ignorable_Code_Point}/gu, "") + .replace(/[ѕһοɡаеорсхуіјАВЕКМНОРСТХΑΒΕΙΚΜΝΟΡΤΧ]/g, (c) => lookalikes[c] ?? c); const fingerprint = (ruleId: string, value: string) => NodeCrypto.createHash("sha256").update(ruleId).update("\0").update(value).digest("hex"); const make = Effect.gen(function* () { @@ -103,14 +118,20 @@ const make = Effect.gen(function* () { }) { if ( input.files.length > 1024 || - input.files.some((f) => f.content.byteLength > 1024 * 1024) || - input.files.reduce((sum, f) => sum + f.content.byteLength, 0) > 8 * 1024 * 1024 + input.files.some((f) => f.content.byteLength + Buffer.byteLength(f.path) > 1024 * 1024) || + input.files.reduce((sum, f) => sum + f.content.byteLength + Buffer.byteLength(f.path), 0) > + 8 * 1024 * 1024 ) return yield* new KinSecretScanError({ reason: "Scan size limit exceeded" }); // No mutation API: the owner-signed action executor owns allowlist insertion. const allowed = yield* sql<{ path: string; rule_id: string; fingerprint: string }>` SELECT path, rule_id, fingerprint FROM kin_secret_allow WHERE org_id = ${input.orgId} LIMIT 4097 - `.pipe(Effect.mapError(() => new KinSecretScanError({ reason: "Allowlist unavailable" }))); + `.pipe( + Effect.mapError(toPersistenceSqlError("KinSecretScan.allowlist")), + Effect.mapError( + (cause) => new KinSecretScanError({ reason: "Allowlist unavailable", cause }), + ), + ); if (allowed.length > 4096) return yield* new KinSecretScanError({ reason: "Allowlist size limit exceeded" }); const allow = new Set(allowed.map((r) => JSON.stringify([r.path, r.rule_id, r.fingerprint]))); @@ -119,6 +140,9 @@ const make = Effect.gen(function* () { let budget = 32 * 1024 * 1024; for (const file of input.files) { const text = normalize(Buffer.from(file.content).toString("utf8")); + const pathFindingStart = findings.length; + let pathHit = true; + let redactPath = false; const inspect = ( value: string, baseLine: number, @@ -128,20 +152,45 @@ const make = Effect.gen(function* () { budget -= value.length; if (budget < 0 || findings.length > 1024) return; for (const [ruleId, pattern] of rules) { - for (const match of value.matchAll(new RegExp(pattern.source, `${pattern.flags}g`))) { - const secret = match[1] ?? match[0]; - if (ruleId === "secret-assignment" && entropy(secret) < 3.5) continue; - if (allow.has(JSON.stringify([file.path, ruleId, fingerprint(ruleId, secret)]))) - continue; - const line = - lineAt?.[match.index] ?? - baseLine + (value.slice(0, match.index).match(/\n/g)?.length ?? 0); - const key = JSON.stringify([file.path, ruleId, line]); - if (!seen.has(key)) { - seen.add(key); - findings.push({ path: file.path, ruleId, line }); + for (const window of ruleWindows(value)) { + const bounded = new RegExp( + `(? + String.fromCharCode(Number.parseInt(hex, 16)), + ), + "latin1", + ).toString("utf8"), + ] + : Array.from({ length: 4 }, (_, alignment) => + Buffer.from(encoded.slice(alignment), "base64url").toString("utf8"), + ); + for (const candidate of candidates) { + if (budget < 0 || findings.length > 1024) return; + const decoded = normalize(candidate).replace(/[^\x09\x0a\x0d\x20-\x7e]/g, " "); + if (decoded !== encoded) + inspect(decoded, lineAt?.[match.index] ?? decodedLine, depth + 1); } - if (decoded === encoded) continue; - inspect(normalize(decoded), lineAt?.[match.index] ?? decodedLine, depth + 1); } }; + inspect(normalize(file.path), 1, 0); + redactPath = findings.length > pathFindingStart; + pathHit = false; inspect(text, 1, 0); const lineAt = new Uint32Array(text.length); let line = 1; let offset = 0; + const starts = new Set(); const compact = text.replace(/\\?\r?\n[\t ]*|[^\r\n\\]+|./g, (chunk) => { if (chunk.includes("\n")) { line++; + starts.add(offset); return ""; } lineAt.fill(line, offset, offset + chunk.length); @@ -180,6 +239,11 @@ const make = Effect.gen(function* () { return chunk; }); inspect(compact, 1, 0, lineAt); + // A wrapped token may start after prose that compacting joined to its left boundary. + for (const start of starts) { + if (budget < 0 || findings.length > 1024) break; + inspect(compact.slice(start, start + 8192), lineAt[start] ?? 1, 0, lineAt.subarray(start)); + } } if (budget < 0 || findings.length > 1024) return yield* new KinSecretScanError({ reason: "Scan work limit exceeded" }); diff --git a/apps/server/src/kin/persistence/migrations/K012_SecretAllow.ts b/apps/server/src/kin/persistence/migrations/K012_SecretAllow.ts index fec65afd99e4a..c5563c34c8717 100644 --- a/apps/server/src/kin/persistence/migrations/K012_SecretAllow.ts +++ b/apps/server/src/kin/persistence/migrations/K012_SecretAllow.ts @@ -9,6 +9,7 @@ export default Effect.gen(function* () { rule_id TEXT NOT NULL, fingerprint TEXT NOT NULL CHECK(length(fingerprint) = 64), signed_action_id TEXT NOT NULL, + created_at INTEGER NOT NULL, PRIMARY KEY (org_id, path, rule_id, fingerprint) )`; }); diff --git a/docs/kin/specs/triggers.md b/docs/kin/specs/triggers.md index a2373fcaf6b38..26607c9744cd0 100644 --- a/docs/kin/specs/triggers.md +++ b/docs/kin/specs/triggers.md @@ -324,12 +324,12 @@ GitHub then blocks a bad auto-merge even if Kin's classifier is wrong. This forc **Rules:** - GitHub tokens (`ghp_`, `gho_`, `ghu_`, `ghs_`, `ghr_`, `github_pat_`); -- Anthropic `sk-ant-`, OpenAI `sk-`/`sk-proj-`; +- Anthropic `sk-ant-`; OpenAI legacy `sk-` with the `T3BlbkFJ` marker and `sk-proj-`/`sk-svcacct-`/`sk-admin-` with mixed case or digits; - AWS `AKIA…`/`ASIA…`; - Slack `xox[abposr]-`; - Stripe `sk_live_`/`rk_live_`; - Google `AIza`; -- PEM `-----BEGIN … PRIVATE KEY-----`; +- PEM `-----BEGIN … PRIVATE KEY-----`, including `PGP PRIVATE KEY BLOCK`; - JWTs (`eyJ…`, three base64url parts); - URLs with a password in them (`scheme://user:pass@`); - `password|secret|token|api_key = `. @@ -342,9 +342,9 @@ GitHub then blocks a bad auto-merge even if Kin's classifier is wrong. This forc **On a finding**, the writer blocks the entire commit, discards staged text and emits a typed owner-notification hook. The runner journals the refusal without secret content and tells the owner to review and rotate the affected credential. Automatic redaction is not part of the writer contract. -**False positives:** the owner adds the fingerprint to `kin_secret_allow` through a signed action. A model can never add to it. Entries are scoped to organization, exact path, rule and SHA-256 of `ruleId + "\0" + normalized matched value`, with the signed action id retained. The scanner is read-only; the APPR executor must verify the owner grant before inserting. Until that executor lands, there is no agent or MCP allowlist-write path. Migration K012 is provisional and will be renumbered at merge. +**False positives:** the owner adds the fingerprint to `kin_secret_allow` through a signed action. A model can never add to it. Entries are scoped to organization, exact path, rule and SHA-256 of `ruleId + "\0" + normalized matched value`, with `created_at` in integer milliseconds and `signed_action_id` reserved for the future APPR executor id. The scanner is read-only; the APPR executor must verify the owner grant before inserting. Until that executor lands, there is no agent or MCP allowlist-write path. Migration K012 is provisional and will be renumbered at merge. -The scanner normalizes NFKC, common Cyrillic/Greek lookalikes and invisible format characters, checks wrapped tokens, and decodes URL/base64 content up to two levels. It fails closed above 1 MiB per file, 8 MiB or 1024 files per call, 4096 allowlist entries per organization, or 32 MiB of transformed scan text. These are conservative detectors, not a guarantee against arbitrary encryption or steganography. +The scanner checks paths as well as content, redacts paths that contain a finding, normalizes NFKC, common Cyrillic/Greek/Latin lookalikes and Default Ignorable code points, checks newline-wrapped tokens, and decodes URL/base64 content up to two levels. Base64 decoding tries all four alignments; malformed percent bytes and non-ASCII decoded bytes become separators. Prefix rules have token boundaries and bounded lengths; each regex sees at most 8192 characters with 4096-character overlap. Merge scanning covers only bytes changed from the fetched remote plus the rebuilt `INDEX.md`, not pre-existing remote content. Scanner failures notify through the writer hook, just like findings. Merge blocks use the recovery hook; production owner delivery still waits on a notifier. It fails closed above 1 MiB per file, 8 MiB or 1024 files per call, 4096 allowlist entries per organization, or 32 MiB of transformed scan text. Splits by a space, `\r`, U+2028, across journal bullets, markdown or HTML comments, `\u` escapes, and hex encoding remain out of scope. These are conservative detectors, not a guarantee against arbitrary encryption or steganography. ### Owner-signed purge (gap 5) From 47f262555bf77803948f73ca32d85d8d7ab348c2 Mon Sep 17 00:00:00 2001 From: GLDRoger <130458023+GLDRoger@users.noreply.github.com> Date: Sun, 8 Nov 2026 14:25:51 +0530 Subject: [PATCH 07/15] fix(kin): gate owner review and harden routine delivery --- apps/server/src/kin/lead/WakeupRunner.ts | 6 +- apps/server/src/kin/lead/layer.ts | 1 + .../src/kin/memory/MemoryWriter.test.ts | 23 ++- apps/server/src/kin/memory/MemoryWriter.ts | 23 ++- .../src/kin/persistence/KinMigrations.test.ts | 15 +- .../src/kin/persistence/KinMigrations.ts | 4 +- .../{K012_Routines.ts => K013_Routines.ts} | 1 + .../src/kin/triggers/ownerCheck.test.ts | 116 ++++++----- apps/server/src/kin/triggers/ownerCheck.ts | 183 +++++++++++------- apps/server/src/kin/triggers/routines.test.ts | 75 +++++++ apps/server/src/kin/triggers/routines.ts | 88 +++++++-- docs/kin/specs/triggers.md | 18 ++ packages/contracts/src/kinLead.ts | 1 + 13 files changed, 402 insertions(+), 152 deletions(-) rename apps/server/src/kin/persistence/migrations/{K012_Routines.ts => K013_Routines.ts} (87%) diff --git a/apps/server/src/kin/lead/WakeupRunner.ts b/apps/server/src/kin/lead/WakeupRunner.ts index b1a2b7adebfb0..9d84ce6df8316 100644 --- a/apps/server/src/kin/lead/WakeupRunner.ts +++ b/apps/server/src/kin/lead/WakeupRunner.ts @@ -37,6 +37,8 @@ import * as KinBudget from "./KinBudget.ts"; import * as KinLeadRegistry from "./KinLeadRegistry.ts"; import { KinLeadPrompt } from "./LeadPrompt.ts"; +const ownerCheckDispatcherReady = false; + export class KinWakeupError extends Schema.TaggedError()("KinWakeupError", { orgId: KinOrgId, agentId: KinAgentId, @@ -381,7 +383,9 @@ const make = Effect.gen(function* () { if (Option.isSome(ownerCheck)) { for (const event of events) { if (event._tag === "routine" && event.routine === "owner_check") - yield* ownerCheck.value.post(orgId, event.eventId); + // Keep cards dark until the verified action dispatcher and clients land. + if (ownerCheckDispatcherReady) + yield* ownerCheck.value.post(orgId, event.eventId, event.ownerCheckItems ?? []); } events = events.filter( (event) => event._tag !== "routine" || event.routine !== "owner_check", diff --git a/apps/server/src/kin/lead/layer.ts b/apps/server/src/kin/lead/layer.ts index 02d5c9347adf8..7d566aa80d4d2 100644 --- a/apps/server/src/kin/lead/layer.ts +++ b/apps/server/src/kin/lead/layer.ts @@ -44,6 +44,7 @@ export function containStartup(layer: Layer.Layer) { const routineRuntime = Routines.layer.pipe( Layer.provideMerge(TriggerScheduler.layer), Layer.provide(Layer.mergeAll(TriggerStore.layer, TriggerInbox.layer)), + // Kin builds in its contained startup scope; orchestration runtime does not expose its scheduler. Layer.provide(Scheduler.layer), ); diff --git a/apps/server/src/kin/memory/MemoryWriter.test.ts b/apps/server/src/kin/memory/MemoryWriter.test.ts index bfb681c3225cd..874b8a4b507a9 100644 --- a/apps/server/src/kin/memory/MemoryWriter.test.ts +++ b/apps/server/src/kin/memory/MemoryWriter.test.ts @@ -43,6 +43,7 @@ it.live.each([ "P10-lost-git-dir", "sha256", "owner-keep", + "owner-chat-keep", "agent-keep", "review-default", "review-null", @@ -213,10 +214,26 @@ it.live.each([ timeZone: "Asia/Kolkata", startedAt: "2026-11-08T00:00:00.000Z", eventIds: [], - sources: new Map([ + sources: new Map< + string, + Writer.WakeupContext["sources"] extends ReadonlyMap ? S : never + >([ [ "owner", - { saidBy: "fixture-owner", trust: "owner", source: "fixture-owner-message" }, + scenario === "owner-keep" + ? { + saidBy: "fixture-owner", + trust: "owner", + source: "fixture-owner-message", + kind: "owner_check", + itemId: "def-001", + action: "keep", + } + : { + saidBy: "fixture-owner", + trust: "owner", + source: "fixture-owner-message", + }, ], [ "member", @@ -521,7 +538,7 @@ it.live.each([ .stage(orgId, "wake1", { path: "claims/existing.md", content, - ...(scenario === "owner-keep" ? { sourceEventId: "owner" } : {}), + ...(scenario !== "agent-keep" ? { sourceEventId: "owner" } : {}), }) .pipe(Effect.result); expect(result._tag).toBe(scenario === "owner-keep" ? "Success" : "Failure"); diff --git a/apps/server/src/kin/memory/MemoryWriter.ts b/apps/server/src/kin/memory/MemoryWriter.ts index f1be91c3c1fd9..7736e2a14ffc8 100644 --- a/apps/server/src/kin/memory/MemoryWriter.ts +++ b/apps/server/src/kin/memory/MemoryWriter.ts @@ -34,7 +34,14 @@ export interface WakeupContext extends JournalContext { readonly level: "L0" | "L1" | "L2" | "L3"; readonly sources: ReadonlyMap< string, - { readonly saidBy: string; readonly trust: MemoryTrust; readonly source: string } + { readonly saidBy: string; readonly trust: MemoryTrust; readonly source: string } & ( + | { readonly kind?: undefined } + | { + readonly kind: "owner_check"; + readonly itemId: string; + readonly action: "keep" | "update" | "drop"; + } + ) >; } // The runner resolves this context from same-org records, never tool input. @@ -338,7 +345,12 @@ const make = Effect.gen(function* () { const parsed = yield* parseFrontMatter(orgId, change.path, change.content, true); const source = change.sourceEventId === undefined - ? { saidBy: ctx.agentId, trust: "agent", source: `wakeup:${wakeupId}` } + ? { + saidBy: ctx.agentId, + trust: "agent", + source: `wakeup:${wakeupId}`, + kind: undefined, + } : ctx.sources.get(change.sourceEventId); if (!source) return yield* new MemoryWriteRejected({ @@ -443,7 +455,12 @@ const make = Effect.gen(function* () { "said_by", "source", ]); - if (source.trust === "owner") mutable.add("review_by"); + if ( + source.kind === "owner_check" && + source.itemId === item.id && + source.action === "keep" + ) + mutable.add("review_by"); if ( Object.entries(item).some( ([key, value]) => diff --git a/apps/server/src/kin/persistence/KinMigrations.test.ts b/apps/server/src/kin/persistence/KinMigrations.test.ts index a6bffbbde2edf..81e53c5f759c1 100644 --- a/apps/server/src/kin/persistence/KinMigrations.test.ts +++ b/apps/server/src/kin/persistence/KinMigrations.test.ts @@ -100,7 +100,7 @@ describe("Kin migrations", () => { { migration_id: 9, name: "JudgeLog" }, { migration_id: 10, name: "ChatChanges" }, { migration_id: 11, name: "MemoryHeads" }, - { migration_id: 12, name: "Routines" }, + { migration_id: 13, name: "Routines" }, ]); assert.deepStrictEqual( upstreamBefore.map((row) => [row.migration_id, row.name] as const), @@ -154,7 +154,7 @@ describe("Kin migrations", () => { { migration_id: 9, name: "JudgeLog" }, { migration_id: 10, name: "ChatChanges" }, { migration_id: 11, name: "MemoryHeads" }, - { migration_id: 12, name: "Routines" }, + { migration_id: 13, name: "Routines" }, ]); assert.deepStrictEqual(yield* upstreamHistory, upstreamBefore); assert.deepStrictEqual(yield* sql`SELECT * FROM kin_orgs`, [ @@ -164,6 +164,10 @@ describe("Kin migrations", () => { yield* sql`SELECT name FROM pragma_index_info('idx_kin_triggers_due') ORDER BY seqno`, ["next_run_at", "org_id", "trigger_id"].map((name) => ({ name })), ); + assert.deepStrictEqual( + yield* sql`SELECT name FROM pragma_index_info('kin_workers_owner_review') ORDER BY seqno`, + ["org_id", "ended_at", "worker_id"].map((name) => ({ name })), + ); const indexes = yield* sql<{ name: string }>` SELECT name FROM sqlite_master WHERE type = 'index' AND name GLOB 'idx_kin_*' ORDER BY name `; @@ -233,7 +237,7 @@ describe("Kin migrations", () => { [9, "JudgeLog"], [10, "ChatChanges"], [11, "MemoryHeads"], - [12, "Routines"], + [13, "Routines"], ]); const before = yield* kinSchema; assert.deepStrictEqual( @@ -271,6 +275,7 @@ describe("Kin migrations", () => { yield* sql`DROP TABLE kin_ping_items`; yield* sql`DROP INDEX kin_chat_members_one_owner`; yield* sql`DROP TABLE kin_memory_heads`; + yield* sql`DROP INDEX kin_workers_owner_review`; yield* sql`DROP TABLE kin_routine_folds`; yield* sql`DROP TABLE kin_routine_preferences`; yield* sql`DROP TABLE kin_budget_charges`; @@ -287,7 +292,7 @@ describe("Kin migrations", () => { [9, "JudgeLog"], [10, "ChatChanges"], [11, "MemoryHeads"], - [12, "Routines"], + [13, "Routines"], ]); assert.deepStrictEqual( yield* sql`SELECT member_id FROM kin_chat_members WHERE org_id = 'org' ORDER BY member_id`, @@ -324,7 +329,7 @@ describe("Kin migrations", () => { { migration_id: 9, name: "JudgeLog" }, { migration_id: 10, name: "ChatChanges" }, { migration_id: 11, name: "MemoryHeads" }, - { migration_id: 12, name: "Routines" }, + { migration_id: 13, name: "Routines" }, ]); 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 ec85b8bf0f6f1..eba4603832781 100644 --- a/apps/server/src/kin/persistence/KinMigrations.ts +++ b/apps/server/src/kin/persistence/KinMigrations.ts @@ -15,7 +15,7 @@ import JudgeLog from "./migrations/K009_JudgeLog.ts"; import ChatChanges from "./migrations/K010_ChatChanges.ts"; import MemoryHeads from "./migrations/K011_MemoryHeads.ts"; -import Routines from "./migrations/K012_Routines.ts"; +import Routines from "./migrations/K013_Routines.ts"; const run = Migrator.make({}); const loader = Migrator.fromRecord({ @@ -30,7 +30,7 @@ const loader = Migrator.fromRecord({ "9_JudgeLog": JudgeLog, "10_ChatChanges": ChatChanges, "11_MemoryHeads": MemoryHeads, - "12_Routines": Routines, + "13_Routines": Routines, }); export const runKinMigrations = Effect.fn("KinMigrations.run")(function* () { diff --git a/apps/server/src/kin/persistence/migrations/K012_Routines.ts b/apps/server/src/kin/persistence/migrations/K013_Routines.ts similarity index 87% rename from apps/server/src/kin/persistence/migrations/K012_Routines.ts rename to apps/server/src/kin/persistence/migrations/K013_Routines.ts index 589cf62a7f4dc..cacbc61e1bb18 100644 --- a/apps/server/src/kin/persistence/migrations/K012_Routines.ts +++ b/apps/server/src/kin/persistence/migrations/K013_Routines.ts @@ -3,6 +3,7 @@ import * as SqlClient from "effect/sql/SqlClient"; export default Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; + yield* sql`CREATE INDEX kin_workers_owner_review ON kin_workers (org_id, ended_at, worker_id) WHERE report_json IS NOT NULL`; yield* sql`CREATE TABLE kin_routine_preferences ( org_id TEXT NOT NULL REFERENCES kin_orgs(org_id), routine TEXT NOT NULL CHECK (routine IN ('morning_brief', 'evening_summary', 'owner_check')), diff --git a/apps/server/src/kin/triggers/ownerCheck.test.ts b/apps/server/src/kin/triggers/ownerCheck.test.ts index 0f0bdc0b0b830..e8dcd27818cb7 100644 --- a/apps/server/src/kin/triggers/ownerCheck.test.ts +++ b/apps/server/src/kin/triggers/ownerCheck.test.ts @@ -49,7 +49,8 @@ it.effect("picks five distinct, valid same-org items in the specified category o }; const json = yield* encodeJson(report); yield* sql`INSERT INTO kin_wakeups (org_id, wakeup_id, agent_id, model, started_at, status, event_ids) VALUES (${orgId}, 'worker-wakeup', 'lead-a', 'fixture', 0, 'completed', '[]')`; - yield* sql`INSERT INTO kin_workers (worker_id, org_id, agent_id, wakeup_id, thread_id, kind, model_json, provenance_json, status, report_json, started_at) VALUES ('worker', ${orgId}, 'lead-a', 'worker-wakeup', 'thread', 'research', '{}', '{}', 'done', ${json}, 0)`; + yield* sql`INSERT INTO kin_workers (worker_id, org_id, agent_id, wakeup_id, thread_id, kind, model_json, provenance_json, status, report_json, started_at, ended_at) VALUES ('worker', ${orgId}, 'lead-a', 'worker-wakeup', 'thread', 'research', '{}', '{}', 'done', ${json}, 0, ${now})`; + yield* sql`INSERT INTO kin_workers (worker_id, org_id, agent_id, wakeup_id, thread_id, kind, model_json, provenance_json, status, report_json, started_at, ended_at) VALUES ('ancient-worker', ${orgId}, 'lead-a', 'worker-wakeup', 'thread-old', 'research', '{}', '{}', 'done', 'invalid ancient report', 0, ${now - 91 * 86_400_000})`; yield* TestClock.setTime(now); const service = yield* OwnerCheck.OwnerCheck; const selected = yield* service.select(orgId); @@ -80,8 +81,8 @@ it.effect( const chat = yield* Service.KinChatService; const queue = yield* Queue.KinEventQueue; const sql = yield* SqlClient.SqlClient; - yield* service.post(orgId, "weekly"); - yield* service.post(orgId, "weekly"); + yield* service.post(orgId, "weekly", yield* service.select(orgId)); + yield* service.post(orgId, "weekly", yield* service.select(orgId)); const [widget] = yield* sql<{ state_json: string; }>`SELECT state_json FROM kin_widgets WHERE org_id = ${orgId} ORDER BY widget_id`; @@ -110,59 +111,71 @@ it.effect( ) .pipe(Effect.flip); const staged: MemoryWriter.MemoryChange[] = []; - for (const action of ["keep", "update", "drop"] as const) { - const message = yield* chat.act( - { _tag: "person", orgId, personId: KinPersonId.make("owner-a") }, - { - widgetId: KinWidgetId.make("owner-check:a:weekly"), - revision: 1, - actionId: action, - value: "old-review", - clientActionId: action, - }, - ); - const event = decodeEvent({ - _tag: "widget.action", - orgId, - eventId: action, - agentId: "lead-a", - receivedAt: "2026-11-09T09:00:00.000Z", - dedupeKey: action, - provenance: { _tag: "human", personId: "owner-a", memberId: "owner-member-a" }, - chatId: "chat-a", - messageId: message.messageId, - actionId: action, - }); - yield* queue.enqueue(event); - } - yield* TestClock.adjust("20 seconds"); yield* Effect.scoped( Effect.gen(function* () { - yield* queue.claim({ - orgId, - agentId: KinAgentId.make("lead-a"), - wakeupId: KinWakeupId.make("review-wakeup"), - model: "fixture", - }); const writer = Layer.mock(MemoryWriter.MemoryWriter)({ stage: (_org, _wakeup, change) => Effect.sync(() => { staged.push(change); }), }); - for (const action of ["keep", "update", "drop"] as const) { - const result = yield* service - .act(orgId, "review-wakeup", KinEventId.make(action)) - .pipe(Effect.provide(writer)); - assert.equal(result.action, action); - assert.equal(result.chatId, KinChatId.make("chat-a")); + for (const [index, action] of (["keep", "update", "drop"] as const).entries()) { + const message = yield* chat.act( + { _tag: "person", orgId, personId: KinPersonId.make("owner-a") }, + { + widgetId: KinWidgetId.make("owner-check:a:weekly"), + revision: index + 1, + actionId: action, + value: card.items[index]!.itemId, + clientActionId: action, + }, + ); + yield* queue.enqueue( + decodeEvent({ + _tag: "widget.action", + orgId, + eventId: action, + agentId: "lead-a", + receivedAt: "2026-11-09T09:00:00.000Z", + dedupeKey: action, + provenance: { _tag: "human", personId: "owner-a", memberId: "owner-member-a" }, + chatId: "chat-a", + messageId: message.messageId, + actionId: action, + }), + ); + yield* TestClock.adjust("20 seconds"); + const wakeupId = KinWakeupId.make(`review-${action}`); + yield* Effect.scoped( + Effect.gen(function* () { + yield* queue.claim({ + orgId, + agentId: KinAgentId.make("lead-a"), + wakeupId, + model: "fixture", + }); + const result = yield* service + .act(orgId, wakeupId, KinEventId.make(action)) + .pipe(Effect.provide(writer)); + assert.equal(result.action, action); + const [updated] = yield* sql<{ + revision: number; + closed_at: string | null; + state_json: string; + }>`SELECT revision, closed_at, state_json FROM kin_widgets WHERE org_id = ${orgId} ORDER BY widget_id`; + assert.equal(updated!.revision, index + 2); + assert.equal(decodeCard(updated!.state_json).items[index]!.handled, action); + assert.equal(updated!.closed_at !== null, index === 2); + yield* queue.finish({ orgId, wakeupId, status: "completed" }); + }), + ); } assert.lengthOf(staged, 2); assert.equal(staged[0]?.sourceEventId, "keep"); assert.include(staged[0]!.content, "2026-12-09T09:00:20.000Z"); - assert.include(staged[1]!.content, "2026-11-09T09:00:20.000Z"); + assert.include(staged[1]!.content, "2026-11-09T09:01:00.000Z"); for (const [org, eventId, wakeup] of [ - [otherOrg, "keep", "review-wakeup"], + [otherOrg, "keep", "review-keep"], [orgId, "keep", "foreign-wakeup"], ] as const) { const result = yield* service @@ -172,7 +185,7 @@ it.effect( } yield* sql`UPDATE kin_chat_members SET role = 'member' WHERE org_id = ${orgId} AND member_id = 'owner-member-a'`; yield* service - .act(orgId, "review-wakeup", KinEventId.make("keep")) + .act(orgId, "review-keep", KinEventId.make("keep")) .pipe(Effect.provide(writer), Effect.flip); assert.lengthOf(staged, 2); yield* chat @@ -187,12 +200,15 @@ it.effect( }, ) .pipe(Effect.flip); - yield* queue.finish({ - orgId, - wakeupId: KinWakeupId.make("review-wakeup"), - status: "completed", - }); }), ); - }).pipe(Effect.provide(fixture(memoryItems))), + }).pipe( + Effect.provide( + fixture( + memoryItems + .slice(0, 3) + .map((item, index) => ({ ...item, review_by: `2026-11-0${index + 1}` })), + ), + ), + ), ); diff --git a/apps/server/src/kin/triggers/ownerCheck.ts b/apps/server/src/kin/triggers/ownerCheck.ts index 63a4485abd0fc..d893a3741d614 100644 --- a/apps/server/src/kin/triggers/ownerCheck.ts +++ b/apps/server/src/kin/triggers/ownerCheck.ts @@ -106,6 +106,7 @@ export class OwnerCheck extends Context.Service< readonly post: ( orgId: KinOrgId, fireId: string, + selections: ReadonlyArray, ) => Effect.Effect, OwnerCheckError>; readonly act: ( orgId: KinOrgId, @@ -135,10 +136,11 @@ const make = Effect.gen(function* () { wrap( orgId, Effect.gen(function* () { + const now = yield* Clock.currentTimeMillis; const items = yield* memory.query(orgId, { excludeInvalid: true }); const reports = yield* sql<{ report_json: string; - }>`SELECT report_json FROM kin_workers WHERE org_id = ${orgId} AND report_json IS NOT NULL ORDER BY ended_at, worker_id`.pipe( + }>`SELECT report_json FROM kin_workers WHERE org_id = ${orgId} AND report_json IS NOT NULL AND ended_at >= ${now - 90 * day} ORDER BY ended_at, worker_id`.pipe( Effect.mapError((cause) => isSqlError(cause) ? toPersistenceSqlError("OwnerCheck.select")(cause) : cause, ), @@ -148,82 +150,82 @@ const make = Effect.gen(function* () { const report = yield* decodeReport(row.report_json); for (const finding of report.misleadingMemory) misleading.add(finding.itemId); } - return pickOwnerCheckItems(orgId, items, misleading, yield* Clock.currentTimeMillis); + return pickOwnerCheckItems(orgId, items, misleading, now); }), ), ); - const post = Effect.fn("OwnerCheck.post")((orgId: KinOrgId, fireId: string) => - wrap( - orgId, - Effect.gen(function* () { - const widgetId = KinWidgetId.make(`owner-check:${orgId}:${fireId}`); - const previous = yield* repo.getWidget({ orgId, widgetId }); - if (Option.isSome(previous)) - return (yield* decodeCard(previous.value.state)).items.map(({ itemId, reason }) => ({ - itemId, - reason, - })); - const selections = yield* select(orgId); - const heads = yield* Effect.forEach(selections, (selection) => - memory.readItemQuietly(orgId, selection.itemId), - ); - return yield* sql - .withTransaction( - Effect.gen(function* () { - const existing = yield* repo.getWidget({ orgId, widgetId }); - if (Option.isSome(existing)) - return (yield* decodeCard(existing.value.state)).items.map( - ({ itemId, reason }) => ({ itemId, reason }), - ); - if (selections.length === 0) return selections; - const [lead] = yield* sql<{ - owner_chat_id: string; - owner_person_id: string; - }>`SELECT l.owner_chat_id, a.owner_person_id 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`; - if (lead === undefined) return yield* Effect.fail("missing_lead"); - const chatId = KinChatId.make(lead.owner_chat_id); - const { message } = yield* chat.notice({ - orgId, - chatId, - messageId: KinMessageId.make(widgetId), - body: "Memory to review", - }); - const now = DateTime.formatIso(yield* DateTime.now); - const state = { - items: selections.map((selection, index) => ({ - ...selection, - title: heads[index]?.head.title ?? "", - path: heads[index]?.head.path ?? "", - })), - }; - const widget: KinWidget = { - orgId, - chatId, - messageId: message.messageId, - widgetId, - widgetClass: "native", - type: "owner_check", - revision: 1, - state, - source: null, - feed: null, - awaiting: [KinPersonId.make(lead.owner_person_id)], - pinnedAt: null, - closedAt: null, - updatedAt: now, - }; - yield* repo.insertWidget(widget); - yield* sql.afterCommit(hub.publish({ type: "widget.updated", orgId, widget })); - return selections; - }), - ) - .pipe( - Effect.mapError((cause) => - isSqlError(cause) ? toPersistenceSqlError("OwnerCheck.post")(cause) : cause, - ), + const post = Effect.fn("OwnerCheck.post")( + (orgId: KinOrgId, fireId: string, selections: ReadonlyArray) => + wrap( + orgId, + Effect.gen(function* () { + const widgetId = KinWidgetId.make(`owner-check:${orgId}:${fireId}`); + const previous = yield* repo.getWidget({ orgId, widgetId }); + if (Option.isSome(previous)) + return (yield* decodeCard(previous.value.state)).items.map(({ itemId, reason }) => ({ + itemId, + reason, + })); + const heads = yield* Effect.forEach(selections, (selection) => + memory.readItemQuietly(orgId, selection.itemId), ); - }), - ), + return yield* sql + .withTransaction( + Effect.gen(function* () { + const existing = yield* repo.getWidget({ orgId, widgetId }); + if (Option.isSome(existing)) + return (yield* decodeCard(existing.value.state)).items.map( + ({ itemId, reason }) => ({ itemId, reason }), + ); + if (selections.length === 0) return selections; + const [lead] = yield* sql<{ + owner_chat_id: string; + owner_person_id: string; + }>`SELECT l.owner_chat_id, a.owner_person_id 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`; + if (lead === undefined) return yield* Effect.fail("missing_lead"); + const chatId = KinChatId.make(lead.owner_chat_id); + const { message } = yield* chat.notice({ + orgId, + chatId, + messageId: KinMessageId.make(widgetId), + body: "Memory to review", + }); + const now = DateTime.formatIso(yield* DateTime.now); + const state = { + items: selections.map((selection, index) => ({ + ...selection, + title: heads[index]?.head.title ?? "", + path: heads[index]?.head.path ?? "", + })), + }; + const widget: KinWidget = { + orgId, + chatId, + messageId: message.messageId, + widgetId, + widgetClass: "native", + type: "owner_check", + revision: 1, + state, + source: null, + feed: null, + awaiting: [KinPersonId.make(lead.owner_person_id)], + pinnedAt: null, + closedAt: null, + updatedAt: now, + }; + yield* repo.insertWidget(widget); + yield* sql.afterCommit(hub.publish({ type: "widget.updated", orgId, widget })); + return selections; + }), + ) + .pipe( + Effect.mapError((cause) => + isSqlError(cause) ? toPersistenceSqlError("OwnerCheck.post")(cause) : cause, + ), + ); + }), + ), ); const act = Effect.fn("OwnerCheck.act")( (orgId: KinOrgId, wakeupId: string, eventId: KinEventId) => @@ -273,9 +275,13 @@ const make = Effect.gen(function* () { ) return yield* Effect.fail("invalid_card"); const card = yield* decodeCard(widget.value.state); - if (!card.items.some((item) => item.itemId === action.value)) + if ( + !card.items.some( + (item) => item.itemId === action.value && item.handled === undefined, + ) + ) return yield* Effect.fail("unknown_item"); - return { action, chatId: event.chatId }; + return { action, chatId: event.chatId, widget: widget.value, card }; }), ) .pipe( @@ -301,6 +307,33 @@ const make = Effect.gen(function* () { sourceEventId: eventId, }); } + const at = DateTime.formatIso(yield* DateTime.now); + const items = verified.card.items.map((item) => + item.itemId === verified.action.value + ? { ...item, handled: verified.action.actionId } + : item, + ); + yield* sql + .withTransaction( + Effect.gen(function* () { + const widget = yield* repo.updateWidget({ + ...verified.widget, + expectedRevision: verified.widget.revision, + state: { items }, + awaiting: items.every((item) => item.handled !== undefined) + ? [] + : verified.widget.awaiting, + closedAt: items.every((item) => item.handled !== undefined) ? at : null, + updatedAt: at, + }); + yield* sql.afterCommit(hub.publish({ type: "widget.updated", orgId, widget })); + }), + ) + .pipe( + Effect.mapError((cause) => + isSqlError(cause) ? toPersistenceSqlError("OwnerCheck.act")(cause) : cause, + ), + ); return { chatId: verified.chatId, itemId: verified.action.value, diff --git a/apps/server/src/kin/triggers/routines.test.ts b/apps/server/src/kin/triggers/routines.test.ts index a69e01c7a93a7..5ae4c4e9e47b7 100644 --- a/apps/server/src/kin/triggers/routines.test.ts +++ b/apps/server/src/kin/triggers/routines.test.ts @@ -166,5 +166,80 @@ it.effect( yield* TestClock.setTime(Date.parse("2026-10-31T12:30:00.000Z")); yield* scheduler.runDue(); assert.equal((yield* store.get(orgA, id))?.nextRunAt, "2026-11-01T13:30:00.000Z"); + yield* sql`UPDATE kin_agents SET retired_at = '2026-11-01T13:00:00.000Z' WHERE org_id = ${orgA} AND agent_id = 'lead-a'`; + yield* routines.drain(orgA); + yield* routines.configure(settings); + assert.isTrue((yield* store.list(orgA)).every((trigger) => !trigger.enabled)); + assert.lengthOf( + yield* sql`SELECT fire_id FROM kin_trigger_inbox WHERE org_id = ${orgA} AND consumed_at IS NULL ORDER BY fire_id`, + 0, + ); + }).pipe(Effect.provide(fixture())), +); + +it.effect("consumes a failing fire and still delivers the following good fire", () => + Effect.gen(function* () { + yield* seed(); + const routines = yield* Routines.KinRoutines; + const scheduler = yield* TriggerScheduler.KinTriggerScheduler; + const sql = yield* SqlClient.SqlClient; + yield* TestClock.setTime(Date.parse("2026-11-09T02:00:00.000Z")); + yield* routines.configure(settings); + yield* TestClock.setTime(Date.parse("2026-11-09T13:00:00.000Z")); + yield* scheduler.runDue(); + yield* sql`CREATE TRIGGER reject_fold BEFORE INSERT ON kin_routine_folds BEGIN SELECT RAISE(ABORT, 'fixture failure'); END`; + yield* routines.drain(orgA); + const rows = yield* sql<{ + json: string; + }>`SELECT json FROM kin_events WHERE org_id = ${orgA} AND tag = 'routine' ORDER BY event_id`; + assert.isTrue( + rows.some((row) => { + const event = decodeEventJson(row.json); + return ( + event._tag === "routine" && event.routine === "evening_summary" && event.lateByMs === 0 + ); + }), + ); + assert.lengthOf( + yield* sql`SELECT fire_id FROM kin_trigger_inbox WHERE org_id = ${orgA} AND consumed_at IS NULL ORDER BY fire_id`, + 0, + ); + }).pipe(Effect.provide(fixture())), +); + +it.effect.each([false, true])( + "repoints stranded folds, or drops them when both routines are off: %s", + (bothOff) => + Effect.gen(function* () { + yield* seed(); + const routines = yield* Routines.KinRoutines; + const sql = yield* SqlClient.SqlClient; + yield* TestClock.setTime(Date.parse("2026-11-09T02:00:00.000Z")); + yield* routines.configure(settings); + yield* sql`INSERT INTO kin_routine_folds VALUES (${orgA}, 'old-fire', 'morning_brief', 1, 'evening_summary')`; + yield* routines.setEnabled(orgA, "evening_summary", false); + const folds = yield* sql<{ + fold_into: string; + }>`SELECT fold_into FROM kin_routine_folds WHERE org_id = ${orgA} ORDER BY fire_id`; + assert.deepEqual(folds, [{ fold_into: "morning_brief" }]); + if (bothOff) { + yield* routines.setEnabled(orgA, "morning_brief", false); + assert.lengthOf( + yield* sql`SELECT fire_id FROM kin_routine_folds WHERE org_id = ${orgA} ORDER BY fire_id`, + 0, + ); + } else { + const scheduler = yield* TriggerScheduler.KinTriggerScheduler; + yield* TestClock.setTime(Date.parse("2026-11-09T03:00:00.000Z")); + yield* scheduler.runDue(); + yield* routines.drain(orgA); + const [row] = yield* sql<{ + json: string; + }>`SELECT json FROM kin_events WHERE org_id = ${orgA} ORDER BY event_id LIMIT 1`; + const event = decodeEventJson(row!.json); + assert.equal(event._tag, "routine"); + if (event._tag === "routine") + assert.equal(event.foldedRoutines?.[0]?.routine, "morning_brief"); + } }).pipe(Effect.provide(fixture())), ); diff --git a/apps/server/src/kin/triggers/routines.ts b/apps/server/src/kin/triggers/routines.ts index fc04cda1f0fbe..bdc258ead203d 100644 --- a/apps/server/src/kin/triggers/routines.ts +++ b/apps/server/src/kin/triggers/routines.ts @@ -142,8 +142,27 @@ const make = Effect.gen(function* () { }, ); }); + const nextFoldTarget = Effect.fn("KinRoutines.nextFoldTarget")(function* ( + orgId: KinOrgId, + excludeFireId: string, + ) { + const [next] = yield* sql<{ + routine: string; + }>`SELECT json_extract(t.subject_json, '$.routine') AS routine + FROM kin_triggers t WHERE t.org_id = ${orgId} AND t.enabled = 1 + AND t.trigger_id IN ('routine:morning_brief', 'routine:evening_summary') + ORDER BY MIN(t.next_run_at, COALESCE(( + SELECT MIN(CAST(unixepoch(json_extract(i.payload_json, '$.dueAt')) * 1000 AS INTEGER)) + FROM kin_trigger_inbox i WHERE i.org_id = t.org_id AND i.consumed_at IS NULL + AND i.fire_id <> ${excludeFireId} + AND json_extract(i.payload_json, '$.payload.subject.routine') = json_extract(t.subject_json, '$.routine') + ), t.next_run_at)), t.trigger_id LIMIT 1`; + return next?.routine; + }); const reconcile = Effect.fn("KinRoutines.reconcile")(function* (settings: KinTriggerSettings) { const now = yield* Clock.currentTimeMillis; + const activeLead = + yield* sql`SELECT l.agent_id 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 = ${settings.orgId} AND a.retired_at IS NULL ORDER BY l.agent_id LIMIT 1`; for (const routine of routines) { const id = KinTriggerId.make(`routine:${routine}`); const existing = yield* store.get(settings.orgId, id); @@ -152,7 +171,7 @@ const make = Effect.gen(function* () { enabled: number; paused: number; }>`SELECT enabled, paused FROM kin_routine_preferences WHERE org_id = ${settings.orgId} AND routine = ${routine} ORDER BY routine`; - const enabled = preference?.enabled === 1 && preference.paused === 0; + const enabled = activeLead.length > 0 && preference?.enabled === 1 && preference.paused === 0; if (existing !== undefined && existing.enabled === enabled) continue; const timing = schedule(routine, settings); const next = enabled @@ -171,6 +190,18 @@ const make = Effect.gen(function* () { }); } yield* scheduler.recompute(settings); + const target = yield* nextFoldTarget(settings.orgId, ""); + if (target !== undefined) { + yield* sql`UPDATE kin_routine_folds SET fold_into = ${target} WHERE org_id = ${settings.orgId}`; + } else { + const dropped = + yield* sql`DELETE FROM kin_routine_folds WHERE org_id = ${settings.orgId} RETURNING fire_id`; + if (dropped.length > 0) + yield* Effect.logWarning("Kin routine folds dropped: no enabled destination", { + orgId: settings.orgId, + count: dropped.length, + }); + } }); const wrap = (orgId: KinOrgId, effect: Effect.Effect) => effect.pipe(Effect.mapError((cause) => new KinRoutinesError({ orgId, cause }))); @@ -212,8 +243,6 @@ const make = Effect.gen(function* () { yield* reconcile(settings); if ((routine === null && value) || (routine !== null && !value)) { yield* sql`UPDATE kin_events SET state = 'expired' WHERE org_id = ${orgId} AND tag = 'routine' AND state = 'queued' AND (${routine} IS NULL OR json_extract(json, '$.routine') = ${routine})`; - if (routine !== null) - yield* sql`DELETE FROM kin_routine_folds WHERE org_id = ${orgId} AND routine = ${routine}`; } }), ) @@ -240,7 +269,6 @@ const make = Effect.gen(function* () { if (name !== "morning_brief" && name !== "evening_summary" && name !== "owner_check") continue; - const items = name === "owner_check" ? yield* ownerCheck.select(orgId) : undefined; yield* sql .withTransaction( Effect.gen(function* () { @@ -251,7 +279,15 @@ const make = Effect.gen(function* () { const [lead] = yield* sql<{ agent_id: string; }>`SELECT l.agent_id 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`; - if (lead === undefined) return; + if (lead === undefined) { + yield* inbox.markConsumed(orgId, [fire.fireId]); + yield* sql`UPDATE kin_triggers SET enabled = 0, next_run_at = NULL WHERE org_id = ${orgId} AND trigger_id IN ('routine:morning_brief', 'routine:evening_summary', 'routine:owner_check')`; + yield* Effect.logWarning("Kin routine consumed: lead retired", { + orgId, + fireId: fire.fireId, + }); + return; + } const eventId = KinEventId.make( `routine_${Array.from(new TextEncoder().encode(`${orgId.length}:${orgId}${fire.fireId}`), (byte) => byte.toString(16).padStart(2, "0")).join("")}`, ); @@ -270,7 +306,15 @@ const make = Effect.gen(function* () { return; } if (routineDisposition(name, fire.dueAt, iso(now), settings) === "fold") { - const target = name === "morning_brief" ? "evening_summary" : "morning_brief"; + const target = yield* nextFoldTarget(orgId, fire.fireId); + if (target === undefined) { + yield* Effect.logWarning("Kin routine fold dropped: no enabled destination", { + orgId, + fireId: fire.fireId, + }); + yield* inbox.markConsumed(orgId, [fire.fireId]); + return; + } yield* sql`UPDATE kin_routine_folds SET fold_into = ${target} WHERE org_id = ${orgId} AND fold_into = ${name}`; yield* sql`INSERT INTO kin_routine_folds (org_id, fire_id, routine, due_at, fold_into) VALUES (${orgId}, ${fire.fireId}, ${name}, ${Date.parse(fire.dueAt)}, ${target}) ON CONFLICT DO NOTHING RETURNING fire_id`; } else { @@ -278,6 +322,8 @@ const make = Effect.gen(function* () { routine: string; due_at: number; }>`SELECT routine, due_at FROM kin_routine_folds WHERE org_id = ${orgId} AND fold_into = ${name} ORDER BY due_at, fire_id`; + const items = + name === "owner_check" ? yield* ownerCheck.select(orgId) : undefined; const event = yield* decodeEvent({ _tag: "routine", eventId, @@ -288,7 +334,7 @@ const make = Effect.gen(function* () { dedupeKey: `routine:${orgId}:${fire.fireId}`, routine: name, dueAt: fire.dueAt, - lateByMs: now - Date.parse(fire.dueAt), + lateByMs: Math.max(0, now - Date.parse(fire.dueAt)), foldedRoutines: folds.map((fold) => ({ routine: fold.routine, dueAt: iso(fold.due_at), @@ -305,31 +351,47 @@ const make = Effect.gen(function* () { Effect.mapError((cause) => isSqlError(cause) ? toPersistenceSqlError("KinRoutines.drain")(cause) : cause, ), + Effect.catch((cause) => + Effect.gen(function* () { + yield* Effect.logWarning("Kin routine fire consumed: processing failed", { + orgId, + fireId: fire.fireId, + errorTag: cause._tag, + }); + yield* inbox.markConsumed(orgId, [fire.fireId]); + }), + ), ); } }), ), ); + let nextReconcileAt = 0; yield* ticks.register( "kin-routines", Effect.gen(function* () { + const now = yield* Clock.currentTimeMillis; + const reconcileDue = now >= nextReconcileAt; + // Scheduler maintenance deliberately scans organizations; all work below remains org-scoped. const orgs = yield* sql<{ org_id: string; }>`SELECT org_id FROM kin_leads WHERE org_id IN (SELECT org_id FROM kin_orgs) ORDER BY org_id`; for (const row of orgs) { const orgId = KinOrgId.make(row.org_id); yield* Effect.gen(function* () { - yield* sql.withTransaction( - Effect.gen(function* () { - yield* reconcile(yield* settingsFor(orgId)); - }), - ); + if (reconcileDue) + yield* sql.withTransaction( + Effect.gen(function* () { + yield* reconcile(yield* settingsFor(orgId)); + }), + ); yield* drain(orgId); }).pipe( Effect.catch((cause) => Effect.logWarning("Kin routines tick failed", { orgId, cause })), ); } - }).pipe(Effect.catch((cause) => Effect.logWarning("Kin routines scan failed", { cause }))), + if (reconcileDue) nextReconcileAt = now + 15 * 60 * 1000; + }).pipe(Effect.catch(() => Effect.logWarning("Kin routines scan failed"))), ); return KinRoutines.of({ isEnabled: Effect.fn("KinRoutines.isEnabled")((orgId, routine) => diff --git a/docs/kin/specs/triggers.md b/docs/kin/specs/triggers.md index 7cc7c62fb723d..257d18b56ccda 100644 --- a/docs/kin/specs/triggers.md +++ b/docs/kin/specs/triggers.md @@ -254,6 +254,24 @@ Routine delivery is a lead-queue request, not a second posting path: TRIG-9 enqu Owner-check ties use review date (missing last), then item id and path. Invalid, superseded and duplicate-id items are excluded. Low trust means any trust below owner; with no status-claim subtype in memory, evidence-free means a claim with no evidence links. Keep sets the next review to now +90 days for people or +30 days otherwise, so repeated Keeps do not grow the review interval. Update returns the owner direct-chat/item target; Keep and Drop stage through the writer with the verified, claimed owner action as their source, never model-supplied provenance. Native clients own presenting the three actions; the server owns their authorization. +**Owner-check release gate (TRIG-9):** selection and routine enqueue are live, but +`WakeupRunner` keeps native card posting explicitly disabled until the dispatcher +lands. Before enabling it: + +- chat act must enqueue `widget.action`; +- `WakeupRunner` must intercept `owner_check` actions, run `OwnerCheck.act`, + then `MemoryWriter.commitWakeup`, then queue `finish`, without exposing these + actions to the model prompt; +- production `MemoryWriterAuthority.resolve` must grant `kind: "owner_check"` + only after verifying the claimed owner tap, binding its item id and action. + An owner chat citation is not review-date authority; +- web and mobile must render the native card, its handled items, and closed state. + +Each accepted action advances the card revision and marks that item handled; +the last handled item closes it. Misleading-memory selection reads reports +ended in the last 90 days. K013_Routines is provisional and must be renumbered at +merge if TRIG-16 or LEAD-7 takes the slot first. + ### Watchdogs (`interval`) All are plain code. They write memory only through the LEAD queue as code-only events, which keeps one writer per org (gap 1). diff --git a/packages/contracts/src/kinLead.ts b/packages/contracts/src/kinLead.ts index 72298d6c68f41..704945b00716f 100644 --- a/packages/contracts/src/kinLead.ts +++ b/packages/contracts/src/kinLead.ts @@ -152,6 +152,7 @@ export const KinOwnerCheckCard = Schema.Struct({ Schema.Struct({ itemId: KinMemoryItemId, reason: Schema.Literals(["past_review", "misleading", "old_low_trust", "no_evidence"]), + handled: Schema.optional(Schema.Literals(["keep", "update", "drop"])), title: Schema.String, path: Schema.String, }), From 0a5f36dcd30648bdede2dddceb4b45b05344a04f Mon Sep 17 00:00:00 2001 From: GLDRoger <130458023+GLDRoger@users.noreply.github.com> Date: Sun, 8 Nov 2026 14:40:05 +0530 Subject: [PATCH 08/15] fix(kin): bound secret scan work and preserve token boundaries --- apps/server/src/kin/memory/Memory.test.ts | 13 +- apps/server/src/kin/memory/Memory.ts | 7 +- .../src/kin/memory/MemoryWriter.test.ts | 15 +++ apps/server/src/kin/memory/MemoryWriter.ts | 15 +++ apps/server/src/kin/memory/secretScan.test.ts | 100 +++++++++++++- apps/server/src/kin/memory/secretScan.ts | 127 +++++++++++------- docs/kin/specs/triggers.md | 4 +- 7 files changed, 223 insertions(+), 58 deletions(-) diff --git a/apps/server/src/kin/memory/Memory.test.ts b/apps/server/src/kin/memory/Memory.test.ts index 674998fcc7b2f..942b89ffbba43 100644 --- a/apps/server/src/kin/memory/Memory.test.ts +++ b/apps/server/src/kin/memory/Memory.test.ts @@ -859,6 +859,7 @@ it.live("uses filesystem identity for a case-mismatched base without recloning", it.live.each([ "diverged", "secret merge", + "scan failure merge", "remote secret merge", "large remote merge", "conflict", @@ -959,7 +960,7 @@ it.live.each([ } yield* memory.record(orgA, "main", initial.head, local); yield* fs.writeFileString( - `${source}/${scenario === "push retry" || scenario === "diverged" || scenario === "secret merge" || scenario === "remote secret merge" || scenario === "large remote merge" || scenario === "network down" || scenario === "dirty writer" || scenario === "H2-merge-swap" ? "remote-only" : "rules.md"}`, + `${source}/${scenario === "push retry" || scenario === "diverged" || scenario === "scan failure merge" || scenario === "secret merge" || scenario === "remote secret merge" || scenario === "large remote merge" || scenario === "network down" || scenario === "dirty writer" || scenario === "H2-merge-swap" ? "remote-only" : "rules.md"}`, "Remote invented edit\n", ); if (scenario === "journal conflict" || scenario === "push retry") { @@ -1018,6 +1019,10 @@ it.live.each([ yield* memory.record(orgA, "main", local, replacement); local = replacement; } + if (scenario === "scan failure merge") { + const sql = yield* SqlClient.SqlClient; + yield* sql`DROP TABLE kin_secret_allow`; + } const result = yield* memory.prepareWakeup(orgA, "fixture-wakeup").pipe(Effect.result); if (scenario === "unrelated" || scenario === "criss-cross") { expect(result._tag).toBe("Failure"); @@ -1052,6 +1057,12 @@ it.live.each([ expect( (yield* memory.prepareWakeup(orgA, "fixture-wakeup-3").pipe(Effect.result))._tag, ).toBe("Success"); + } else if (scenario === "scan failure merge") { + expect(result._tag).toBe("Failure"); + expect( + messages.some((message) => message.includes("Secret scan failed: Allowlist unavailable")), + ).toBe(true); + expect(yield* memory.anchor(orgA, "main")).toBe(local); } else if (scenario === "secret merge") { expect(result._tag).toBe("Failure"); if (result._tag === "Failure") { diff --git a/apps/server/src/kin/memory/Memory.ts b/apps/server/src/kin/memory/Memory.ts index c38760e061e22..d8aafb19a722a 100644 --- a/apps/server/src/kin/memory/Memory.ts +++ b/apps/server/src/kin/memory/Memory.ts @@ -1112,6 +1112,9 @@ const make = Effect.gen(function* () { .map(([path, content]) => ({ path, content })), }) .pipe( + Effect.tapError((cause) => + notify({ orgId, branch, reason: `Secret scan failed: ${cause.reason}` }), + ), Effect.mapError( (cause) => new MemoryError({ orgId, operation: "secretScan", cause }), ), @@ -1120,12 +1123,12 @@ const make = Effect.gen(function* () { yield* notify({ orgId, branch, - reason: `Secret scan blocked merge: ${findings[0]!.ruleId} in ${findings[0]!.pathHit ? "[redacted]" : findings[0]!.path}`, + reason: `Secret scan blocked merge: ${findings[0]!.ruleId} in ${findings[0]!.pathHit ? "[redacted]" : (findings[0]!.itemPath ?? findings[0]!.path)}`, }); return yield* new MemoryError({ orgId, operation: "secretScan", - reason: `Secret scan blocked merge: ${findings[0]!.ruleId} in ${findings[0]!.pathHit ? "[redacted]" : findings[0]!.path}`, + reason: `Secret scan blocked merge: ${findings[0]!.ruleId} in ${findings[0]!.pathHit ? "[redacted]" : (findings[0]!.itemPath ?? findings[0]!.path)}`, }); } tree = yield* Effect.scoped( diff --git a/apps/server/src/kin/memory/MemoryWriter.test.ts b/apps/server/src/kin/memory/MemoryWriter.test.ts index df01b3857d435..3f0789c6874e0 100644 --- a/apps/server/src/kin/memory/MemoryWriter.test.ts +++ b/apps/server/src/kin/memory/MemoryWriter.test.ts @@ -600,6 +600,21 @@ it.live.each([ } if (scenario === "tree-substitution") substituteTree = yield* git(paths.writer, ["rev-parse", `${original}^{tree}`]); + if (scenario === "blocked") { + const tokenPath = "claims/" + ["gh", "p_", "a".repeat(36)].join("") + ".md"; + const malformed = yield* writer + .stage(orgId, "wake1", { + path: tokenPath, + content: "missing front matter", + }) + .pipe(Effect.result); + expect(malformed._tag).toBe("Failure"); + if (malformed._tag === "Failure") { + expect(malformed.failure._tag).toBe("MemoryItemError"); + if (malformed.failure._tag === "MemoryItemError") + expect(malformed.failure.path).toBe("[redacted]"); + } + } if (scenario === "blocked") yield* writer.appendJournal(orgId, "wake1", [ ["gh", "p_"].join("") + "aB3dE6gH9jK2mN5pQ8sT1vW4yZ7cF0iL3oR6", diff --git a/apps/server/src/kin/memory/MemoryWriter.ts b/apps/server/src/kin/memory/MemoryWriter.ts index 180af1d60d20f..39dfb13f92f2e 100644 --- a/apps/server/src/kin/memory/MemoryWriter.ts +++ b/apps/server/src/kin/memory/MemoryWriter.ts @@ -66,6 +66,7 @@ export class MemorySecretBlocked extends Schema.TaggedError ruleId: Schema.String, line: Schema.Number, pathHit: Schema.optional(Schema.Boolean), + itemPath: Schema.optional(Schema.String), }), ), }, @@ -315,6 +316,20 @@ const make = Effect.gen(function* () { }); const report = (orgId: KinOrgId, wakeupId: string, effect: Effect.Effect) => effect.pipe( + Effect.catchTags({ + MemoryItemError: (error) => + scanner.scan({ orgId, files: [{ path: error.path, content: new Uint8Array() }] }).pipe( + Effect.flatMap((findings) => + Effect.fail( + new MemoryItemError({ + orgId: error.orgId, + path: findings.length ? "[redacted]" : error.path, + reason: error.reason, + }), + ), + ), + ), + }), Effect.tapError((error) => error._tag === "MemoryCheckoutInvalid" || error._tag === "MemorySyncConflict" || diff --git a/apps/server/src/kin/memory/secretScan.test.ts b/apps/server/src/kin/memory/secretScan.test.ts index 63ff116caed3c..fe3010170cc20 100644 --- a/apps/server/src/kin/memory/secretScan.test.ts +++ b/apps/server/src/kin/memory/secretScan.test.ts @@ -1,5 +1,6 @@ // @effect-diagnostics-next-line nodeBuiltinImport:off -- Bounded synchronous fingerprint hashing shares the scan loop. import * as NodeCrypto from "node:crypto"; +import { vi } from "vite-plus/test"; import { expect, it } from "@effect/vitest"; import { KinOrgId } from "@t3tools/contracts"; import * as Effect from "effect/Effect"; @@ -15,6 +16,23 @@ const layer = Scan.layer.pipe(Layer.provideMerge(database)); const body = "aB3dE6gH9jK2mN5pQ8sT1vW4yZ7cF0iL3oR6"; const github = ["gh", "p_"].join("") + body; const fixtures = [ + ...["X-Api-Key: ", "client-secret: ", '{"password": "', '"api_key":"'].map((prefix) => [ + "secret-assignment", + prefix + body + '"', + prefix + "short", + ]), + ...["_", "token_"].map((prefix) => [ + "github-token", + prefix + github + "_rotated", + "task-follow-up-with-design-team", + ]), + ["aws-access-key", ["AK", "IA"].join("") + "Q".repeat(16) + "_old", "ASIAPACIFICREGION2026"], + [ + "jwt", + ["ey", "J"].join("") + "a".repeat(100) + "." + "b".repeat(1800) + "." + "c".repeat(100), + "eyJshort.two.parts", + ], + ...["p", "o", "u", "s", "r"].map((kind) => [ "github-token", "gh" + kind + "_" + body, @@ -72,6 +90,7 @@ it.effect("blocks encoded, wrapped and confusable tokens with safe findings", () const scanner = yield* Scan.KinSecretScan; const values = [ github, + "%67" + github.slice(1), ...["/", "-", "_", "+", "word"].map( (prefix) => prefix + Buffer.from(github).toString("base64"), ), @@ -92,6 +111,9 @@ it.effect("blocks encoded, wrapped and confusable tokens with safe findings", () Buffer.from(Buffer.from(github).toString("base64")).toString("base64"), [...github].map((c) => `%${c.charCodeAt(0).toString(16)}`).join(""), github.slice(0, 12) + "\n" + github.slice(12), + ...["next word", " next word"].map( + (suffix) => github.slice(0, 12) + "\n" + github.slice(12) + "\n" + suffix, + ), github.slice(0, 12) + "\\\n " + github.slice(12), github.replace("p", "р"), [...github].map((c) => String.fromCharCode(c.charCodeAt(0) + 0xfee0)).join(""), @@ -193,7 +215,7 @@ it.effect("fails closed at file, batch and count limits", () => }).pipe(Effect.provide(layer)), ); -it.effect("round 2 near misses and private key headers", () => +it.effect("near misses and private key headers", () => Effect.gen(function* () { const scanner = yield* Scan.KinSecretScan; for (const content of [ @@ -206,6 +228,10 @@ it.effect("round 2 near misses and private key headers", () => "github_token_scopes: repo,workflow,admin:org,read:user", "postgres://user:${DB_PASSWORD}@example.invalid/db", "ASIAPACIFICREGION2026", + ["sk-", "proj-2026-11-follow-up-with-design-team-on-onboarding"].join(""), + ["sk-", "admin-rotation-plan-for-2026-q3-and-next-year"].join(""), + "z".repeat(4096) + ["AK", "IA"].join("") + "Q".repeat(16), + " ".repeat(8173) + ["AK", "IA"].join("") + "Q".repeat(16) + "z", ]) expect( (yield* scanner.scan({ @@ -225,7 +251,7 @@ it.effect("round 2 near misses and private key headers", () => ).toBe(true); }).pipe(Effect.provide(layer)), ); -it.effect("round 2 scans paths without exposing them", () => +it.effect("scans paths without exposing them", () => Effect.gen(function* () { const scanner = yield* Scan.KinSecretScan; const findings = yield* scanner.scan({ @@ -234,19 +260,29 @@ it.effect("round 2 scans paths without exposing them", () => }); expect(findings.some((f) => f.ruleId === "github-token")).toBe(true); expect(JSON.stringify(findings).includes(github)).toBe(false); + const indexFindings = yield* scanner.scan({ + orgId, + files: [ + { path: "claims/item.md", content: Buffer.from("clean") }, + { path: "INDEX.md", content: Buffer.from("- [claim] " + github + " (claims/item.md)") }, + ], + }); + expect(indexFindings.some((f) => f.itemPath === "claims/item.md")).toBe(true); }).pipe(Effect.provide(layer)), ); -it.effect("round 2 bounds adversarial rule inputs structurally", () => +it.effect("bounds adversarial rule inputs structurally", () => Effect.gen(function* () { const scanner = yield* Scan.KinSecretScan; for (const prefix of ["eyJ", "sk-", "AKIA"]) { const content = prefix.repeat(Math.floor((1024 * 1024 - 100) / prefix.length)); let examined = 0; for (const window of Scan.ruleWindows(content)) { - expect(window.text.length).toBeLessThanOrEqual(8192); + expect(window.text.length).toBeLessThanOrEqual(8194); examined += window.text.length; } - expect(examined).toBeLessThanOrEqual(content.length * 2); + expect(examined).toBeLessThanOrEqual( + content.length * 2 + Math.ceil(content.length / 4096) * 2, + ); expect( (yield* scanner.scan({ orgId, @@ -256,3 +292,57 @@ it.effect("round 2 bounds adversarial rule inputs structurally", () => } }).pipe(Effect.provide(layer)), ); + +it.effect("scans ordinary journals and multi-file batches without exhausting work", () => + Effect.gen(function* () { + const scanner = yield* Scan.KinSecretScan; + const journal = "Discussed the next steps with the team. Follow up tomorrow.\n"; + for (const files of [ + [ + { + path: "journal/test.md", + content: Buffer.from(journal.repeat(Math.ceil((256 * 1024) / journal.length))), + }, + ], + Array.from({ length: 30 }, (_, i) => ({ + path: `journal/${i}.md`, + content: Buffer.from(journal.repeat(Math.ceil((60 * 1024) / journal.length))), + })), + ]) + expect((yield* scanner.scan({ orgId, files })).length).toBe(0); + }).pipe(Effect.provide(layer)), +); + +it.effect("bounds rule executions for random runs, word lines and multi-file inputs", () => + Effect.gen(function* () { + const scanner = yield* Scan.KinSecretScan; + let seed = 42; + const random = (length: number) => + Array.from({ length }, () => { + seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0; + return "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"[seed % 62]; + }).join(""); + const runs = Array.from({ length: Math.floor((1024 * 1024 - 100) / 25) }, () => + random(24), + ).join(" "); + const words = (Array.from({ length: 10 }, () => random(7)).join(" ") + "\n").repeat(13000); + for (const files of [ + [{ path: "runs", content: Buffer.from(runs) }], + [{ path: "words", content: Buffer.from(words) }], + Array.from({ length: 8 }, (_, i) => ({ path: `runs-${i}`, content: Buffer.from(runs) })), + ]) { + let executions = 0; + const exec = RegExp.prototype.exec; + using _spy = vi.spyOn(RegExp.prototype, "exec").mockImplementation(function ( + this: RegExp, + value: string, + ) { + if (this.source.startsWith("(? { + const alphabet = + id === "github-token" || id === "aws-access-key" + ? "A-Za-z0-9" + : id === "secret-assignment" + ? "A-Za-z0-9_" + : "A-Za-z0-9_-"; + const right = id === "url-password" || id === "secret-assignment" ? "" : `(?![${alphabet}])`; + return [ + id, + new RegExp(`(? 0 ? 1 : 0, + rightContext: offset + 8192 < value.length ? 1 : 0, + }; }; const entropy = (value: string) => { const counts = new Map(); @@ -137,34 +156,35 @@ const make = Effect.gen(function* () { const allow = new Set(allowed.map((r) => JSON.stringify([r.path, r.rule_id, r.fingerprint]))); const findings: SecretFinding[] = []; const seen = new Set(); - let budget = 32 * 1024 * 1024; + let budget = 64 * 1024 * 1024; for (const file of input.files) { const text = normalize(Buffer.from(file.content).toString("utf8")); + const lines = text.split("\n"); const pathFindingStart = findings.length; let pathHit = true; let redactPath = false; - const inspect = ( - value: string, - baseLine: number, - depth: number, - lineAt?: Uint32Array, - ): void => { - budget -= value.length; + const inspect = function* (value: string, baseLine: number, depth: number): Generator { + // Charge fixed work as well as bytes to bound decoder fan-out on short values. + budget -= value.length + 4096; if (budget < 0 || findings.length > 1024) return; - for (const [ruleId, pattern] of rules) { - for (const window of ruleWindows(value)) { - const bounded = new RegExp( - `(? window.text.length - window.rightContext ) continue; + const index = window.offset + match.index; + const secret = match[1] ?? match[0]; + if (ruleId === "openai-key" && !/[A-Z]/.test(secret)) continue; if (ruleId === "url-password" && /^\$\{[^}]+\}$/.test(secret)) continue; if ( ruleId === "secret-assignment" && @@ -177,16 +197,18 @@ const make = Effect.gen(function* () { continue; if (allow.has(JSON.stringify([file.path, ruleId, fingerprint(ruleId, secret)]))) continue; - const line = - lineAt?.[index] ?? baseLine + (value.slice(0, index).match(/\n/g)?.length ?? 0); + const line = baseLine + (value.slice(0, index).match(/\n/g)?.length ?? 0); const key = JSON.stringify([file.path, ruleId, line, pathHit]); if (!seen.has(key)) { + const indexLine = file.path === "INDEX.md" ? lines[line - 1] : undefined; + const itemPath = indexLine?.match(/\(([^()]+\.md)\)/)?.[1]; seen.add(key); findings.push({ path: pathHit || redactPath ? "[redacted]" : file.path, ruleId, line, ...(pathHit ? { pathHit: true } : {}), + ...(itemPath && input.files.some((f) => f.path === itemPath) ? { itemPath } : {}), }); } break; @@ -196,10 +218,15 @@ const make = Effect.gen(function* () { if (depth === 2) return; let cursor = 0; let decodedLine = baseLine; - for (const match of value.matchAll(/(?:%[0-9a-fA-F]{2}|[A-Za-z0-9_+/-]){24,}={0,2}/g)) { + for (const match of value.matchAll(/(?:%[0-9a-fA-F]{2}|[A-Za-z0-9_+/-]){20,}={0,2}/g)) { + const encoded = match[0]; + // Mixed percent/plain encodings can represent 20 bytes in fewer than 60 characters. + if (!encoded.includes("%") && encoded.length < 27) continue; decodedLine += value.slice(cursor, match.index).match(/\n/g)?.length ?? 0; cursor = match.index; - const encoded = match[0]; + yield; + budget -= 4096; + if (budget < 0) return; const candidates = encoded.includes("%") ? [ Buffer.from( @@ -215,39 +242,43 @@ const make = Effect.gen(function* () { for (const candidate of candidates) { if (budget < 0 || findings.length > 1024) return; const decoded = normalize(candidate).replace(/[^\x09\x0a\x0d\x20-\x7e]/g, " "); - if (decoded !== encoded) - inspect(decoded, lineAt?.[match.index] ?? decodedLine, depth + 1); + if (decoded !== encoded) yield* inspect(decoded, decodedLine, depth + 1); } } }; - inspect(normalize(file.path), 1, 0); + for (const _ of inspect(normalize(file.path), 1, 0)) yield* Effect.yieldNow; redactPath = findings.length > pathFindingStart; pathHit = false; - inspect(text, 1, 0); - const lineAt = new Uint32Array(text.length); + for (const _ of inspect(text, 1, 0)) yield* Effect.yieldNow; + let run = ""; + let runLine = 1; let line = 1; - let offset = 0; - const starts = new Set(); - const compact = text.replace(/\\?\r?\n[\t ]*|[^\r\n\\]+|./g, (chunk) => { - if (chunk.includes("\n")) { - line++; - starts.add(offset); - return ""; + let fragments = 0; + for (const raw of lines) { + const fragment = raw + .replace(/\\?\r$/, "") + .replace(/\\$/, "") + .trim(); + if (fragment && !/\s/.test(fragment)) { + if (!fragments) runLine = line; + run += fragment; + fragments++; + } else { + if (fragments > 1) for (const _ of inspect(run, runLine, 0)) yield* Effect.yieldNow; + run = ""; + fragments = 0; } - lineAt.fill(line, offset, offset + chunk.length); - offset += chunk.length; - return chunk; - }); - inspect(compact, 1, 0, lineAt); - // A wrapped token may start after prose that compacting joined to its left boundary. - for (const start of starts) { - if (budget < 0 || findings.length > 1024) break; - inspect(compact.slice(start, start + 8192), lineAt[start] ?? 1, 0, lineAt.subarray(start)); + line++; } + if (fragments > 1) for (const _ of inspect(run, runLine, 0)) yield* Effect.yieldNow; } if (budget < 0 || findings.length > 1024) return yield* new KinSecretScanError({ reason: "Scan work limit exceeded" }); - return findings; + const unsafePaths = findings.some((f) => f.pathHit); + return findings.map(({ itemPath, ...finding }) => ({ + ...finding, + ...(itemPath && !unsafePaths ? { itemPath } : {}), + })); }); return KinSecretScan.of({ scan }); }); diff --git a/docs/kin/specs/triggers.md b/docs/kin/specs/triggers.md index 26607c9744cd0..5606832973d83 100644 --- a/docs/kin/specs/triggers.md +++ b/docs/kin/specs/triggers.md @@ -324,7 +324,7 @@ GitHub then blocks a bad auto-merge even if Kin's classifier is wrong. This forc **Rules:** - GitHub tokens (`ghp_`, `gho_`, `ghu_`, `ghs_`, `ghr_`, `github_pat_`); -- Anthropic `sk-ant-`; OpenAI legacy `sk-` with the `T3BlbkFJ` marker and `sk-proj-`/`sk-svcacct-`/`sk-admin-` with mixed case or digits; +- Anthropic `sk-ant-`; OpenAI legacy `sk-` with the `T3BlbkFJ` marker and `sk-proj-`/`sk-svcacct-`/`sk-admin-` containing an uppercase letter; - AWS `AKIA…`/`ASIA…`; - Slack `xox[abposr]-`; - Stripe `sk_live_`/`rk_live_`; @@ -344,7 +344,7 @@ GitHub then blocks a bad auto-merge even if Kin's classifier is wrong. This forc **False positives:** the owner adds the fingerprint to `kin_secret_allow` through a signed action. A model can never add to it. Entries are scoped to organization, exact path, rule and SHA-256 of `ruleId + "\0" + normalized matched value`, with `created_at` in integer milliseconds and `signed_action_id` reserved for the future APPR executor id. The scanner is read-only; the APPR executor must verify the owner grant before inserting. Until that executor lands, there is no agent or MCP allowlist-write path. Migration K012 is provisional and will be renumbered at merge. -The scanner checks paths as well as content, redacts paths that contain a finding, normalizes NFKC, common Cyrillic/Greek/Latin lookalikes and Default Ignorable code points, checks newline-wrapped tokens, and decodes URL/base64 content up to two levels. Base64 decoding tries all four alignments; malformed percent bytes and non-ASCII decoded bytes become separators. Prefix rules have token boundaries and bounded lengths; each regex sees at most 8192 characters with 4096-character overlap. Merge scanning covers only bytes changed from the fetched remote plus the rebuilt `INDEX.md`, not pre-existing remote content. Scanner failures notify through the writer hook, just like findings. Merge blocks use the recovery hook; production owner delivery still waits on a notifier. It fails closed above 1 MiB per file, 8 MiB or 1024 files per call, 4096 allowlist entries per organization, or 32 MiB of transformed scan text. Splits by a space, `\r`, U+2028, across journal bullets, markdown or HTML comments, `\u` escapes, and hex encoding remain out of scope. These are conservative detectors, not a guarantee against arbitrary encryption or steganography. +The scanner checks paths as well as content, redacts paths that contain a finding, normalizes NFKC, common Cyrillic/Greek/Latin lookalikes and Default Ignorable code points, checks newline-wrapped tokens, and decodes URL/base64 content up to two levels. Base64 decoding tries all four alignments; malformed percent bytes and non-ASCII decoded bytes become separators. Prefix rules have token boundaries and bounded lengths; each precompiled regex sees at most 8192 characters plus one character of boundary context on each side, with 4096-character overlap. JWT headers, payloads and signatures are capped at 512, 2500 and 1024 characters. Wrapped tokens are scanned as consecutive whitespace-free line runs with independent boundaries, not joined to subsequent prose. Merge scanning covers only bytes changed from the fetched remote plus the rebuilt `INDEX.md`, not pre-existing remote content. Scanner failures notify through the writer hook, just like findings. Merge blocks use the recovery hook; production owner delivery still waits on a notifier. It fails closed above 1 MiB per file, 8 MiB or 1024 files per call, 4096 allowlist entries per organization, or 64 MiB of weighted work (transformed characters, 4096 units per inspect and decoder run, window characters, and 64 units per regex match). Scanning yields between windows and decoder candidates so multi-file batches do not monopolize the event loop. Base64 runs shorter than 27 characters and fully percent-encoded runs shorter than 60 characters are not decoded; mixed percent/plain runs retain a 20-byte minimum to prevent partial-encoding bypasses. Splits by a space, `\r`, U+2028, across journal bullets, markdown or HTML comments, `\u` escapes, and hex encoding remain out of scope. These are conservative detectors, not a guarantee against arbitrary encryption or steganography. ### Owner-signed purge (gap 5) From 1e11dc33167e9219fe30b3becde35d9314c59692 Mon Sep 17 00:00:00 2001 From: GLDRoger <130458023+GLDRoger@users.noreply.github.com> Date: Sun, 8 Nov 2026 14:51:41 +0530 Subject: [PATCH 09/15] fix(kin): avoid routine deadlocks and retain transient fires --- .../src/kin/memory/MemoryWriter.test.ts | 10 +- .../src/kin/triggers/ownerCheck.test.ts | 62 +++++++ apps/server/src/kin/triggers/ownerCheck.ts | 7 + apps/server/src/kin/triggers/routines.test.ts | 170 +++++++++++++++--- .../src/kin/triggers/routines.testkit.ts | 50 ++++-- apps/server/src/kin/triggers/routines.ts | 67 ++++--- docs/kin/specs/triggers.md | 4 + 7 files changed, 300 insertions(+), 70 deletions(-) diff --git a/apps/server/src/kin/memory/MemoryWriter.test.ts b/apps/server/src/kin/memory/MemoryWriter.test.ts index 874b8a4b507a9..34c91262e5e00 100644 --- a/apps/server/src/kin/memory/MemoryWriter.test.ts +++ b/apps/server/src/kin/memory/MemoryWriter.test.ts @@ -43,6 +43,8 @@ it.live.each([ "P10-lost-git-dir", "sha256", "owner-keep", + "owner-other-item-keep", + "owner-drop-keep", "owner-chat-keep", "agent-keep", "review-default", @@ -220,14 +222,16 @@ it.live.each([ >([ [ "owner", - scenario === "owner-keep" + scenario === "owner-keep" || + scenario === "owner-other-item-keep" || + scenario === "owner-drop-keep" ? { saidBy: "fixture-owner", trust: "owner", source: "fixture-owner-message", kind: "owner_check", - itemId: "def-001", - action: "keep", + itemId: scenario === "owner-other-item-keep" ? "other-item" : "def-001", + action: scenario === "owner-drop-keep" ? "drop" : "keep", } : { saidBy: "fixture-owner", diff --git a/apps/server/src/kin/triggers/ownerCheck.test.ts b/apps/server/src/kin/triggers/ownerCheck.test.ts index e8dcd27818cb7..64048b929c60c 100644 --- a/apps/server/src/kin/triggers/ownerCheck.test.ts +++ b/apps/server/src/kin/triggers/ownerCheck.test.ts @@ -212,3 +212,65 @@ it.effect( ), ), ); + +it.effect.each(["keep", "drop"] as const)("discards staged %s when the card changes", (action) => + Effect.gen(function* () { + yield* seed(); + yield* TestClock.setTime(now); + const service = yield* OwnerCheck.OwnerCheck; + const chat = yield* Service.KinChatService; + const queue = yield* Queue.KinEventQueue; + const sql = yield* SqlClient.SqlClient; + yield* service.post(orgId, "race", yield* service.select(orgId)); + const widgetId = KinWidgetId.make("owner-check:a:race"); + const message = yield* chat.act( + { _tag: "person", orgId, personId: KinPersonId.make("owner-a") }, + { widgetId, revision: 1, actionId: action, value: "old-review", clientActionId: action }, + ); + yield* queue.enqueue( + decodeEvent({ + _tag: "widget.action", + orgId, + eventId: action, + agentId: "lead-a", + receivedAt: "2026-11-09T09:00:00.000Z", + dedupeKey: action, + provenance: { _tag: "human", personId: "owner-a", memberId: "owner-member-a" }, + chatId: "chat-a", + messageId: message.messageId, + actionId: action, + }), + ); + yield* TestClock.adjust("20 seconds"); + const wakeupId = KinWakeupId.make("racing-review"); + yield* queue.claim({ orgId, agentId: KinAgentId.make("lead-a"), wakeupId, model: "fixture" }); + let staged = false; + let discarded = false; + const writer = Layer.mock(MemoryWriter.MemoryWriter)({ + stage: () => + Effect.gen(function* () { + staged = true; + yield* sql`UPDATE kin_widgets SET revision = revision + 1 WHERE org_id = ${orgId} AND widget_id = ${widgetId}`.pipe( + Effect.orDie, + ); + }), + discard: (org, wakeup) => + Effect.sync(() => { + assert.equal(org, orgId); + assert.equal(wakeup, wakeupId); + discarded = true; + staged = false; + }), + }); + const failure = yield* service + .act(orgId, wakeupId, KinEventId.make(action)) + .pipe(Effect.provide(writer), Effect.flip); + assert.equal(failure._tag, "OwnerCheckError"); + assert.isTrue(discarded); + assert.isFalse(staged); + const [widget] = yield* sql<{ + state_json: string; + }>`SELECT state_json FROM kin_widgets WHERE org_id = ${orgId} AND widget_id = ${widgetId}`; + assert.isUndefined(decodeCard(widget!.state_json).items[0]?.handled); + }).pipe(Effect.provide(fixture(memoryItems)), Effect.scoped), +); diff --git a/apps/server/src/kin/triggers/ownerCheck.ts b/apps/server/src/kin/triggers/ownerCheck.ts index d893a3741d614..f31102d5990c0 100644 --- a/apps/server/src/kin/triggers/ownerCheck.ts +++ b/apps/server/src/kin/triggers/ownerCheck.ts @@ -333,6 +333,13 @@ const make = Effect.gen(function* () { Effect.mapError((cause) => isSqlError(cause) ? toPersistenceSqlError("OwnerCheck.act")(cause) : cause, ), + Effect.tapError((cause) => + cause._tag === "KinChatError" && + cause.reason === "widget_changed" && + verified.action.actionId !== "update" + ? writer.discard(orgId, wakeupId) + : Effect.void, + ), ); return { chatId: verified.chatId, diff --git a/apps/server/src/kin/triggers/routines.test.ts b/apps/server/src/kin/triggers/routines.test.ts index 5ae4c4e9e47b7..831d3aa0e3cf2 100644 --- a/apps/server/src/kin/triggers/routines.test.ts +++ b/apps/server/src/kin/triggers/routines.test.ts @@ -1,5 +1,9 @@ import { assert, it } from "@effect/vitest"; import { KinOrgId, KinTriggerId, KinEvent, type KinTriggerSettings } from "@t3tools/contracts"; +import * as Deferred from "effect/Deferred"; +import * as Fiber from "effect/Fiber"; +import * as Option from "effect/Option"; +import * as Semaphore from "effect/Semaphore"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import * as Schema from "effect/Schema"; @@ -177,34 +181,56 @@ it.effect( }).pipe(Effect.provide(fixture())), ); -it.effect("consumes a failing fire and still delivers the following good fire", () => - Effect.gen(function* () { - yield* seed(); - const routines = yield* Routines.KinRoutines; - const scheduler = yield* TriggerScheduler.KinTriggerScheduler; - const sql = yield* SqlClient.SqlClient; - yield* TestClock.setTime(Date.parse("2026-11-09T02:00:00.000Z")); - yield* routines.configure(settings); - yield* TestClock.setTime(Date.parse("2026-11-09T13:00:00.000Z")); - yield* scheduler.runDue(); - yield* sql`CREATE TRIGGER reject_fold BEFORE INSERT ON kin_routine_folds BEGIN SELECT RAISE(ABORT, 'fixture failure'); END`; - yield* routines.drain(orgA); - const rows = yield* sql<{ - json: string; - }>`SELECT json FROM kin_events WHERE org_id = ${orgA} AND tag = 'routine' ORDER BY event_id`; - assert.isTrue( - rows.some((row) => { - const event = decodeEventJson(row.json); - return ( - event._tag === "routine" && event.routine === "evening_summary" && event.lateByMs === 0 +it.effect.each(["transient", "decode", "expired"] as const)( + "handles a %s failure without blocking the following good fire", + (failure) => + Effect.gen(function* () { + yield* seed(); + const routines = yield* Routines.KinRoutines; + const scheduler = yield* TriggerScheduler.KinTriggerScheduler; + const sql = yield* SqlClient.SqlClient; + yield* TestClock.setTime(Date.parse("2026-11-09T02:00:00.000Z")); + yield* routines.configure(settings); + yield* TestClock.setTime(Date.parse("2026-11-09T13:00:00.000Z")); + yield* scheduler.runDue(); + if (failure === "decode") + yield* sql`INSERT INTO kin_routine_folds VALUES (${orgA}, 'bad-fold', 'invalid-routine', 1, 'evening_summary')`; + else + yield* sql`CREATE TRIGGER reject_fold BEFORE INSERT ON kin_routine_folds BEGIN SELECT RAISE(ABORT, 'fixture failure'); END`; + if (failure === "expired") { + yield* routines.drain(orgA); + assert.lengthOf( + yield* sql`SELECT fire_id FROM kin_trigger_inbox WHERE org_id = ${orgA} AND consumed_at IS NULL`, + 1, ); - }), - ); - assert.lengthOf( - yield* sql`SELECT fire_id FROM kin_trigger_inbox WHERE org_id = ${orgA} AND consumed_at IS NULL ORDER BY fire_id`, - 0, - ); - }).pipe(Effect.provide(fixture())), + yield* TestClock.adjust("25 hours"); + } + yield* routines.drain(orgA); + const rows = yield* sql<{ + json: string; + }>`SELECT json FROM kin_events WHERE org_id = ${orgA} AND tag = 'routine' ORDER BY event_id`; + assert.equal( + rows.some((row) => { + const event = decodeEventJson(row.json); + return ( + event._tag === "routine" && event.routine === "evening_summary" && event.lateByMs === 0 + ); + }), + failure !== "decode", + ); + assert.lengthOf( + yield* sql`SELECT fire_id FROM kin_trigger_inbox WHERE org_id = ${orgA} AND consumed_at IS NULL ORDER BY fire_id`, + failure === "transient" ? 1 : 0, + ); + if (failure === "transient") { + yield* sql`DROP TRIGGER reject_fold`; + yield* routines.drain(orgA); + assert.lengthOf( + yield* sql`SELECT fire_id FROM kin_trigger_inbox WHERE org_id = ${orgA} AND consumed_at IS NULL`, + 0, + ); + } + }).pipe(Effect.provide(fixture())), ); it.effect.each([false, true])( @@ -243,3 +269,93 @@ it.effect.each([false, true])( } }).pipe(Effect.provide(fixture())), ); + +it.effect("waits for Memory's org lock without holding SQLite's writer", () => + Effect.gen(function* () { + const lock = yield* Semaphore.make(1); + const held = yield* Deferred.make(); + const selecting = yield* Deferred.make(); + const write = yield* Deferred.make(); + yield* Effect.gen(function* () { + yield* seed(); + const sql = yield* SqlClient.SqlClient; + const routines = yield* Routines.KinRoutines; + const scheduler = yield* TriggerScheduler.KinTriggerScheduler; + yield* TestClock.setTime(Date.parse("2026-11-09T03:29:59.000Z")); + yield* routines.configure(settings); + yield* TestClock.adjust("1 second"); + yield* scheduler.runDue(); + const holder = yield* lock + .withPermits(1)( + Effect.gen(function* () { + yield* Deferred.succeed(held, undefined); + const canWrite = yield* Deferred.await(write); + if (canWrite) + yield* sql.withTransaction( + sql`UPDATE kin_orgs SET name = 'Memory anchor' WHERE org_id = ${orgA}`, + ); + }), + ) + .pipe(Effect.forkScoped); + yield* Deferred.await(held); + const drain = yield* routines.drain(orgA).pipe(Effect.forkScoped); + const inTransaction = yield* Deferred.await(selecting); + yield* Deferred.succeed(write, !inTransaction); + yield* Fiber.join(holder); + yield* Fiber.join(drain); + assert.isFalse(inTransaction); + assert.lengthOf( + yield* sql`SELECT event_id FROM kin_events WHERE org_id = ${orgA} AND json_extract(json, '$.routine') = 'owner_check'`, + 1, + ); + }).pipe( + Effect.provide( + fixture([], { + query: (sql) => () => + Effect.gen(function* () { + const transaction = yield* Effect.serviceOption(sql.transactionService); + yield* Deferred.succeed(selecting, Option.isSome(transaction)); + return yield* lock.withPermits(1)(Effect.succeed([])); + }), + }), + ), + ); + }).pipe(Effect.scoped), +); + +it.effect("reconciles at fifteen minutes, not intervening five-second ticks", () => + Effect.gen(function* () { + let tick = Effect.void; + yield* Effect.gen(function* () { + yield* seed(); + const sql = yield* SqlClient.SqlClient; + yield* tick; + yield* sql`DELETE FROM kin_triggers WHERE org_id = ${orgA} AND trigger_id = 'routine:owner_check'`; + for (let index = 1; index < 180; index++) { + yield* TestClock.adjust("5 seconds"); + yield* tick; + assert.lengthOf( + yield* sql`SELECT trigger_id FROM kin_triggers WHERE org_id = ${orgA} AND trigger_id = 'routine:owner_check'`, + 0, + ); + } + yield* TestClock.adjust("5 seconds"); + yield* tick; + assert.lengthOf( + yield* sql`SELECT trigger_id FROM kin_triggers WHERE org_id = ${orgA} AND trigger_id = 'routine:owner_check'`, + 1, + ); + }).pipe( + Effect.provide( + fixture([], { + register: Effect.fn(function* (name: string, work: Effect.Effect) { + if (name === "kin-routines") { + const context = yield* Effect.context(); + tick = work.pipe(Effect.provideContext(context), Effect.orDie); + } + }), + }), + ), + ); + }), +); diff --git a/apps/server/src/kin/triggers/routines.testkit.ts b/apps/server/src/kin/triggers/routines.testkit.ts index dc54c0344922d..4c1eb4fafbd0d 100644 --- a/apps/server/src/kin/triggers/routines.testkit.ts +++ b/apps/server/src/kin/triggers/routines.testkit.ts @@ -42,7 +42,13 @@ export const item = (id: string, overrides: Partial = {}) => links: ["https://example.com/evidence"], ...overrides, }); -export function fixture(items: ReadonlyArray = []) { +export function fixture( + items: ReadonlyArray = [], + options: { + query?: (sql: SqlClient.SqlClient) => Memory.Memory["Service"]["query"]; + register?: Scheduler.Scheduler["Service"]["register"]; + } = {}, +) { const database = Persistence.layer.pipe(Layer.provideMerge(Sqlite.layerMemory)); const repositories = Layer.mergeAll( Repository.layer, @@ -54,30 +60,40 @@ export function fixture(items: ReadonlyArray = []) { Layer.provide(Layer.mock(ThreadManagement.ThreadManagementService)({})), ); const dependencies = Layer.mergeAll( - Layer.mock(Memory.Memory)({ - query: (orgId) => - Effect.succeed( - items - .filter((item) => item.orgId === orgId) - .map((item) => ({ ...item, duplicate: false })), - ), - readItemQuietly: (orgId, id) => { - const head = items.find((item) => item.orgId === orgId && item.id === id); - return head === undefined - ? Effect.fail(new Memory.MemoryNotFound({ orgId, id })) - : Effect.succeed({ head, body: "Body\n" }); - }, - }), + Layer.unwrap( + Effect.map(SqlClient.SqlClient, (sql) => + Layer.mock(Memory.Memory)({ + query: + options.query?.(sql) ?? + ((orgId) => + Effect.succeed( + items + .filter((item) => item.orgId === orgId) + .map((item) => ({ ...item, duplicate: false })), + )), + readItemQuietly: (orgId, id) => { + const head = items.find((item) => item.orgId === orgId && item.id === id); + return head === undefined + ? Effect.fail(new Memory.MemoryNotFound({ orgId, id })) + : Effect.succeed({ head, body: "Body\n" }); + }, + }), + ), + ), Chat.layer.pipe(Layer.provideMerge(Hub.layer)), TriggerScheduler.layer, ).pipe( Layer.provideMerge(repositories), - Layer.provide(Layer.succeed(Scheduler.Scheduler, { register: () => Effect.void })), + Layer.provide( + Layer.succeed(Scheduler.Scheduler, { register: options.register ?? (() => Effect.void) }), + ), ); const owner = OwnerCheck.layer.pipe(Layer.provideMerge(dependencies)); return Routines.layer.pipe( Layer.provideMerge(owner), - Layer.provideMerge(Layer.succeed(Scheduler.Scheduler, { register: () => Effect.void })), + Layer.provideMerge( + Layer.succeed(Scheduler.Scheduler, { register: options.register ?? (() => Effect.void) }), + ), ); } export const seed = Effect.fn("test.routinesSeed")(function* () { diff --git a/apps/server/src/kin/triggers/routines.ts b/apps/server/src/kin/triggers/routines.ts index bdc258ead203d..0b38feae03f51 100644 --- a/apps/server/src/kin/triggers/routines.ts +++ b/apps/server/src/kin/triggers/routines.ts @@ -8,6 +8,7 @@ import { KinTriggerSettings, type KinTrigger, } 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"; @@ -269,8 +270,22 @@ const make = Effect.gen(function* () { if (name !== "morning_brief" && name !== "evening_summary" && name !== "owner_check") continue; - yield* sql - .withTransaction( + const eventId = KinEventId.make( + `routine_${Array.from(new TextEncoder().encode(`${orgId.length}:${orgId}${fire.fireId}`), (byte) => byte.toString(16).padStart(2, "0")).join("")}`, + ); + yield* Effect.gen(function* () { + const [pending] = yield* sql<{ handled: number }>`SELECT EXISTS( + SELECT 1 FROM kin_events WHERE org_id = ${orgId} AND event_id = ${eventId} + ) AS handled FROM kin_trigger_inbox WHERE org_id = ${orgId} + AND fire_id = ${fire.fireId} AND consumed_at IS NULL`.pipe(NodeSqliteClient.readOnly); + if (pending === undefined) return; + if (pending.handled === 1) { + yield* inbox.markConsumed(orgId, [fire.fireId]); + return; + } + // Memory takes an org lock and may anchor a checkout in SQLite. + const items = name === "owner_check" ? yield* ownerCheck.select(orgId) : undefined; + yield* sql.withTransaction( Effect.gen(function* () { const settings = yield* settingsFor(orgId); const unconsumed = @@ -288,9 +303,6 @@ const make = Effect.gen(function* () { }); return; } - const eventId = KinEventId.make( - `routine_${Array.from(new TextEncoder().encode(`${orgId.length}:${orgId}${fire.fireId}`), (byte) => byte.toString(16).padStart(2, "0")).join("")}`, - ); const previous = yield* sql`SELECT event_id FROM kin_events WHERE org_id = ${orgId} AND event_id = ${eventId} ORDER BY event_id`; if (previous.length > 0) { @@ -322,8 +334,6 @@ const make = Effect.gen(function* () { routine: string; due_at: number; }>`SELECT routine, due_at FROM kin_routine_folds WHERE org_id = ${orgId} AND fold_into = ${name} ORDER BY due_at, fire_id`; - const items = - name === "owner_check" ? yield* ownerCheck.select(orgId) : undefined; const event = yield* decodeEvent({ _tag: "routine", eventId, @@ -346,22 +356,26 @@ const make = Effect.gen(function* () { } yield* inbox.markConsumed(orgId, [fire.fireId]); }), - ) - .pipe( - Effect.mapError((cause) => - isSqlError(cause) ? toPersistenceSqlError("KinRoutines.drain")(cause) : cause, - ), - Effect.catch((cause) => - Effect.gen(function* () { - yield* Effect.logWarning("Kin routine fire consumed: processing failed", { - orgId, - fireId: fire.fireId, - errorTag: cause._tag, - }); - yield* inbox.markConsumed(orgId, [fire.fireId]); - }), - ), ); + }).pipe( + Effect.mapError((cause) => + isSqlError(cause) ? toPersistenceSqlError("KinRoutines.drain")(cause) : cause, + ), + Effect.catch((cause) => + Effect.gen(function* () { + const consume = + Schema.isSchemaError(cause) || now - Date.parse(fire.dueAt) > 24 * 60 * 60 * 1000; + yield* Effect.logWarning("Kin routine fire processing failed", { + orgId, + fireId: fire.fireId, + errorTag: cause._tag, + message: cause.message, + disposition: consume ? "consumed" : "retry", + }); + if (consume) yield* inbox.markConsumed(orgId, [fire.fireId]); + }), + ), + ); } }), ), @@ -391,7 +405,14 @@ const make = Effect.gen(function* () { ); } if (reconcileDue) nextReconcileAt = now + 15 * 60 * 1000; - }).pipe(Effect.catch(() => Effect.logWarning("Kin routines scan failed"))), + }).pipe( + Effect.catch((cause) => + Effect.logWarning("Kin routines scan failed", { + errorTag: cause._tag, + message: cause.message, + }), + ), + ), ); return KinRoutines.of({ isEnabled: Effect.fn("KinRoutines.isEnabled")((orgId, routine) => diff --git a/docs/kin/specs/triggers.md b/docs/kin/specs/triggers.md index 257d18b56ccda..acb522bbaf5b4 100644 --- a/docs/kin/specs/triggers.md +++ b/docs/kin/specs/triggers.md @@ -252,6 +252,8 @@ The dream consumer suppresses a `dream` fire whose `dueAt`, in owner time, falls Routine delivery is a lead-queue request, not a second posting path: TRIG-9 enqueues `routine`; LEAD-13 owns the brief/summary message and prepared-brief content. Late boundaries are measured from the scheduled owner's calendar date (a summary expires at the first dream after its due time). Folds retain the original routine and due instant, survive restart, and move with an expired summary into the next brief. Same-tick fires are handled by due time, not inbox receipt order. Disabling a routine expires its queued requests; enabling it schedules the next slot without backfill. Pause/resume preserves individual opt-outs. +Routine processing consumes schema/decode failures immediately. Other failures (including SQLite contention, enqueue failure and unavailable memory checkouts) stay pending for the next tick; a failing fire more than 24 hours past its due instant is consumed to bound poison retries. Successful late fires still follow the normal fold/post policy. Owner-check selection runs before the enqueue transaction, which rechecks consumption and event identity. + Owner-check ties use review date (missing last), then item id and path. Invalid, superseded and duplicate-id items are excluded. Low trust means any trust below owner; with no status-claim subtype in memory, evidence-free means a claim with no evidence links. Keep sets the next review to now +90 days for people or +30 days otherwise, so repeated Keeps do not grow the review interval. Update returns the owner direct-chat/item target; Keep and Drop stage through the writer with the verified, claimed owner action as their source, never model-supplied provenance. Native clients own presenting the three actions; the server owns their authorization. **Owner-check release gate (TRIG-9):** selection and routine enqueue are live, but @@ -267,6 +269,8 @@ lands. Before enabling it: An owner chat citation is not review-date authority; - web and mobile must render the native card, its handled items, and closed state. +If a concurrent card update wins after Keep or Drop is staged, the writer session is discarded before returning the revision conflict. + Each accepted action advances the card revision and marks that item handled; the last handled item closes it. Misleading-memory selection reads reports ended in the last 90 days. K013_Routines is provisional and must be renumbered at From ec5bf6d4218b0f7b3ba06625bd432ea98b464151 Mon Sep 17 00:00:00 2001 From: GLDRoger <130458023+GLDRoger@users.noreply.github.com> Date: Sun, 8 Nov 2026 15:12:17 +0530 Subject: [PATCH 10/15] fix(kin): scan realistic journals and wrapped secret prefixes --- .../src/kin/memory/MemoryWriter.test.ts | 12 ++- apps/server/src/kin/memory/MemoryWriter.ts | 6 +- apps/server/src/kin/memory/frontMatter.ts | 8 +- apps/server/src/kin/memory/secretScan.test.ts | 73 ++++++++++++++----- apps/server/src/kin/memory/secretScan.ts | 61 +++++++++------- docs/kin/specs/triggers.md | 2 +- 6 files changed, 112 insertions(+), 50 deletions(-) diff --git a/apps/server/src/kin/memory/MemoryWriter.test.ts b/apps/server/src/kin/memory/MemoryWriter.test.ts index 3f0789c6874e0..9b9faefc29e13 100644 --- a/apps/server/src/kin/memory/MemoryWriter.test.ts +++ b/apps/server/src/kin/memory/MemoryWriter.test.ts @@ -605,15 +605,25 @@ it.live.each([ const malformed = yield* writer .stage(orgId, "wake1", { path: tokenPath, - content: "missing front matter", + content: "---\nid: [\n" + tokenPath + ": [\n---\nbody", }) .pipe(Effect.result); expect(malformed._tag).toBe("Failure"); if (malformed._tag === "Failure") { expect(malformed.failure._tag).toBe("MemoryItemError"); + expect(JSON.stringify(malformed.failure).includes(tokenPath.slice(7, -3))).toBe(false); if (malformed.failure._tag === "MemoryItemError") expect(malformed.failure.path).toBe("[redacted]"); } + const inaccessible = yield* writer + .stage(orgId, "wake1", { + path: tokenPath.slice(0, -3) + "_".repeat(260) + ".md", + content: "missing front matter", + }) + .pipe(Effect.result); + expect(inaccessible._tag).toBe("Failure"); + if (inaccessible._tag === "Failure") + expect(JSON.stringify(inaccessible.failure).includes(tokenPath.slice(7, -3))).toBe(false); } if (scenario === "blocked") yield* writer.appendJournal(orgId, "wake1", [ diff --git a/apps/server/src/kin/memory/MemoryWriter.ts b/apps/server/src/kin/memory/MemoryWriter.ts index 39dfb13f92f2e..dd5fbd7224057 100644 --- a/apps/server/src/kin/memory/MemoryWriter.ts +++ b/apps/server/src/kin/memory/MemoryWriter.ts @@ -298,7 +298,11 @@ const make = Effect.gen(function* () { const result = yield* fs.lstat(current).pipe(Effect.result); if (result._tag === "Failure") { if (result.failure.reason._tag !== "NotFound") - return yield* new MemoryError({ orgId, operation: "path", cause: result.failure }); + return yield* new MemoryError({ + orgId, + operation: "path", + cause: "Unable to inspect memory path", + }); } else if ( result.success.type === "SymbolicLink" || (result.success.type === "File" && diff --git a/apps/server/src/kin/memory/frontMatter.ts b/apps/server/src/kin/memory/frontMatter.ts index 692502849fca7..49b7bb49b70b7 100644 --- a/apps/server/src/kin/memory/frontMatter.ts +++ b/apps/server/src/kin/memory/frontMatter.ts @@ -89,8 +89,12 @@ export const parseFrontMatter = Effect.fnUntraced(function* ( new MemoryItemError({ orgId, path, - reason: cause instanceof Error ? cause.message : "Malformed YAML", - cause, + reason: staged + ? "Malformed YAML front matter" + : cause instanceof Error + ? cause.message + : "Malformed YAML", + ...(staged ? {} : { cause }), }), }); const decoded = yield* (staged ? decodeStaged(value) : decodeFrontMatter(value)).pipe( diff --git a/apps/server/src/kin/memory/secretScan.test.ts b/apps/server/src/kin/memory/secretScan.test.ts index fe3010170cc20..319b4fc2aff03 100644 --- a/apps/server/src/kin/memory/secretScan.test.ts +++ b/apps/server/src/kin/memory/secretScan.test.ts @@ -16,6 +16,21 @@ const layer = Scan.layer.pipe(Layer.provideMerge(database)); const body = "aB3dE6gH9jK2mN5pQ8sT1vW4yZ7cF0iL3oR6"; const github = ["gh", "p_"].join("") + body; const fixtures = [ + ...[ + ["stripe-key", ["sk_", "live_"].join("") + body + "_old"], + ["slack-token", ["xo", "xb-"].join("") + body + "_old"], + ["google-key", ["AI", "za"].join("") + "X".repeat(35) + "_old"], + ["google-key", "key_" + ["AI", "za"].join("") + "X".repeat(35)], + ["anthropic-key", "prod-" + ["sk-", "ant-"].join("") + body.repeat(2)], + ...[ + Buffer.from(JSON.stringify({ alg: "RS256", x5c: ["a".repeat(499)] })).toString("base64url"), + ["ey", "J"].join("") + "a".repeat(2048), + ].map((header) => [ + "jwt", + header + "." + ["ey", "J"].join("") + "b".repeat(2600) + ".signature", + ]), + ].map(([rule, hit]) => [rule, hit, "task-follow-up-with-design-team"]), + ...["X-Api-Key: ", "client-secret: ", '{"password": "', '"api_key":"'].map((prefix) => [ "secret-assignment", prefix + body + '"', @@ -29,7 +44,7 @@ const fixtures = [ ["aws-access-key", ["AK", "IA"].join("") + "Q".repeat(16) + "_old", "ASIAPACIFICREGION2026"], [ "jwt", - ["ey", "J"].join("") + "a".repeat(100) + "." + "b".repeat(1800) + "." + "c".repeat(100), + ["ey", "J"].join("") + "a".repeat(100) + ".eyJ" + "b".repeat(1800) + "." + "c".repeat(100), "eyJshort.two.parts", ], @@ -61,7 +76,7 @@ const fixtures = [ ["pem-private-key", "-----BEGIN RSA PRIVATE KEY-----", "-----BEGIN PUBLIC KEY-----"], [ "jwt", - ["ey", "J"].join("") + "a".repeat(10) + "." + "b".repeat(12) + "." + "c".repeat(20), + ["ey", "J"].join("") + "a".repeat(10) + ".eyJ" + "b".repeat(12) + "." + "c".repeat(20), "eyJshort.two.parts", ], [ @@ -90,6 +105,14 @@ it.effect("blocks encoded, wrapped and confusable tokens with safe findings", () const scanner = yield* Scan.KinSecretScan; const values = [ github, + "The deploy key is " + github.slice(0, 12) + "\n" + github.slice(12), + github.slice(0, 12) + "\n" + github.slice(12) + " and more", + github.slice(0, 12) + "\n" + github.slice(12) + "\nThanks", + "The deploy key is " + + Buffer.from(github).toString("base64").slice(0, 16) + + "\n" + + Buffer.from(github).toString("base64").slice(16) + + " and more", "%67" + github.slice(1), ...["/", "-", "_", "+", "word"].map( (prefix) => prefix + Buffer.from(github).toString("base64"), @@ -190,6 +213,18 @@ it.effect("fails closed when decoded findings or the allowlist exceed their boun .scan({ orgId, files: [{ path: "claims/many.md", content }] }) .pipe(Effect.result))._tag, ).toBe("Failure"); + const exhausted = yield* scanner + .scan({ + orgId, + files: Array.from({ length: 8 }, (_, i) => ({ + path: `claims/${i}.md`, + content: Buffer.from("a".repeat(1024 * 1024 - 100)), + })), + }) + .pipe(Effect.result); + expect(exhausted._tag).toBe("Failure"); + if (exhausted._tag === "Failure") + expect(exhausted.failure.reason).toBe("Scan work limit exceeded"); const sql = yield* SqlClient.SqlClient; yield* sql`WITH RECURSIVE entries(n) AS (SELECT 1 UNION ALL SELECT n + 1 FROM entries WHERE n < 4097) INSERT INTO kin_secret_allow SELECT ${orgId}, 'claims/' || n, 'github-token', ${"0".repeat(64)}, 'signed-fixture', 0 FROM entries`; @@ -264,7 +299,10 @@ it.effect("scans paths without exposing them", () => orgId, files: [ { path: "claims/item.md", content: Buffer.from("clean") }, - { path: "INDEX.md", content: Buffer.from("- [claim] " + github + " (claims/item.md)") }, + { + path: "INDEX.md", + content: Buffer.from("- [claim (claims/wrong.md)] " + github + " (claims/item.md)"), + }, ], }); expect(indexFindings.some((f) => f.itemPath === "claims/item.md")).toBe(true); @@ -293,23 +331,24 @@ it.effect("bounds adversarial rule inputs structurally", () => }).pipe(Effect.provide(layer)), ); -it.effect("scans ordinary journals and multi-file batches without exhausting work", () => +it.effect.each([ + { name: "256 KiB journal", count: 1, bytes: 256 * 1024 }, + { name: "30 x 60 KiB batch", count: 30, bytes: 60 * 1024 }, +])("scans realistic $name without exhausting work", ({ count, bytes }) => Effect.gen(function* () { const scanner = yield* Scan.KinSecretScan; - const journal = "Discussed the next steps with the team. Follow up tomorrow.\n"; - for (const files of [ + const journal = [ - { - path: "journal/test.md", - content: Buffer.from(journal.repeat(Math.ceil((256 * 1024) / journal.length))), - }, - ], - Array.from({ length: 30 }, (_, i) => ({ - path: `journal/${i}.md`, - content: Buffer.from(journal.repeat(Math.ceil((60 * 1024) / journal.length))), - })), - ]) - expect((yield* scanner.scan({ orgId, files })).length).toBe(0); + "Discussed the next steps with the team. Follow up tomorrow.", + "Commit " + "0123456789abcdef".repeat(2) + "01234567 reviewed.", + "Updated apps/server/src/kin/memory/secretScan.test.ts and docs/operations/development.md", + "Trace 12345678-1234-5678-9abc-123456789abc completed successfully.", + ].join("\n") + "\n"; + const files = Array.from({ length: count }, (_, i) => ({ + path: `journal/${i}.md`, + content: Buffer.from(journal.repeat(Math.ceil(bytes / journal.length)).slice(0, bytes)), + })); + expect((yield* scanner.scan({ orgId, files })).length).toBe(0); }).pipe(Effect.provide(layer)), ); diff --git a/apps/server/src/kin/memory/secretScan.ts b/apps/server/src/kin/memory/secretScan.ts index 39e749d354e8e..9a232393079fa 100644 --- a/apps/server/src/kin/memory/secretScan.ts +++ b/apps/server/src/kin/memory/secretScan.ts @@ -44,7 +44,7 @@ const rules = [ ["stripe-key", /[rs]k_live_[A-Za-z0-9]{24,200}/], ["google-key", /AIza[A-Za-z0-9_-]{35}/], ["pem-private-key", /-----BEGIN (?:[A-Z0-9]+ ){0,8}PRIVATE KEY(?: BLOCK)?-----/], - ["jwt", /eyJ[A-Za-z0-9_-]{5,509}\.[A-Za-z0-9_-]{8,2500}\.[A-Za-z0-9_-]{16,1024}/], + ["jwt", /eyJ[A-Za-z0-9_-]{5,2048}\.eyJ[A-Za-z0-9_-]{8,1024}/], ["url-password", /\b[a-z][a-z0-9+.-]{0,31}:\/\/[^\s/:@]{1,512}:([^\s/@]{1,512})@/i], [ "secret-assignment", @@ -53,18 +53,21 @@ const rules = [ ] as const; const compiledRules = rules.map(([id, pattern]) => { const alphabet = - id === "github-token" || id === "aws-access-key" + id === "github-token" || id === "aws-access-key" || id === "stripe-key" || id === "google-key" ? "A-Za-z0-9" - : id === "secret-assignment" - ? "A-Za-z0-9_" + : id === "slack-token" + ? "A-Za-z0-9-" : "A-Za-z0-9_-"; - const right = id === "url-password" || id === "secret-assignment" ? "" : `(?![${alphabet}])`; + const right = ["url-password", "secret-assignment", "jwt"].includes(id) + ? "" + : `(?![${alphabet}])`; return [ id, - new RegExp(`(? { - // Charge fixed work as well as bytes to bound decoder fan-out on short values. - budget -= value.length + 4096; + budget -= value.length; if (budget < 0 || findings.length > 1024) return; for (const window of ruleWindows(value)) { budget -= window.text.length; if (budget < 0) return; yield; + if (!anchors.test(window.text)) continue; for (const [ruleId, bounded] of compiledRules) { bounded.lastIndex = 0; let match: RegExpExecArray | null; @@ -201,7 +204,9 @@ const make = Effect.gen(function* () { const key = JSON.stringify([file.path, ruleId, line, pathHit]); if (!seen.has(key)) { const indexLine = file.path === "INDEX.md" ? lines[line - 1] : undefined; - const itemPath = indexLine?.match(/\(([^()]+\.md)\)/)?.[1]; + const itemPath = Array.from(indexLine?.matchAll(/\(([^()]+\.md)\)/g) ?? []).at( + -1, + )?.[1]; seen.add(key); findings.push({ path: pathHit || redactPath ? "[redacted]" : file.path, @@ -225,7 +230,7 @@ const make = Effect.gen(function* () { decodedLine += value.slice(cursor, match.index).match(/\n/g)?.length ?? 0; cursor = match.index; yield; - budget -= 4096; + budget -= encoded.length; if (budget < 0) return; const candidates = encoded.includes("%") ? [ @@ -250,27 +255,27 @@ const make = Effect.gen(function* () { redactPath = findings.length > pathFindingStart; pathHit = false; for (const _ of inspect(text, 1, 0)) yield* Effect.yieldNow; - let run = ""; - let runLine = 1; - let line = 1; - let fragments = 0; - for (const raw of lines) { - const fragment = raw + let run: { text: string; line: number }[] = []; + const append = function* (text: string, line: number): Generator { + run.push({ text, line }); + while (run.length > 8 || run.reduce((sum, part) => sum + part.text.length, 0) > 4096) + run.shift(); + if (run.length > 1) yield* inspect(run.map((part) => part.text).join(""), run[0]!.line, 0); + }; + for (const [index, raw] of lines.entries()) { + const words = raw .replace(/\\?\r$/, "") .replace(/\\$/, "") - .trim(); - if (fragment && !/\s/.test(fragment)) { - if (!fragments) runLine = line; - run += fragment; - fragments++; - } else { - if (fragments > 1) for (const _ of inspect(run, runLine, 0)) yield* Effect.yieldNow; - run = ""; - fragments = 0; + .trim() + .split(/\s+/); + const first = words[0]!; + if (!first) { + run = []; + continue; } - line++; + for (const _ of append(first, index + 1)) yield* Effect.yieldNow; + if (words.length > 1) run = [{ text: words.at(-1)!, line: index + 1 }]; } - if (fragments > 1) for (const _ of inspect(run, runLine, 0)) yield* Effect.yieldNow; } if (budget < 0 || findings.length > 1024) return yield* new KinSecretScanError({ reason: "Scan work limit exceeded" }); diff --git a/docs/kin/specs/triggers.md b/docs/kin/specs/triggers.md index 5606832973d83..944aee345322f 100644 --- a/docs/kin/specs/triggers.md +++ b/docs/kin/specs/triggers.md @@ -344,7 +344,7 @@ GitHub then blocks a bad auto-merge even if Kin's classifier is wrong. This forc **False positives:** the owner adds the fingerprint to `kin_secret_allow` through a signed action. A model can never add to it. Entries are scoped to organization, exact path, rule and SHA-256 of `ruleId + "\0" + normalized matched value`, with `created_at` in integer milliseconds and `signed_action_id` reserved for the future APPR executor id. The scanner is read-only; the APPR executor must verify the owner grant before inserting. Until that executor lands, there is no agent or MCP allowlist-write path. Migration K012 is provisional and will be renumbered at merge. -The scanner checks paths as well as content, redacts paths that contain a finding, normalizes NFKC, common Cyrillic/Greek/Latin lookalikes and Default Ignorable code points, checks newline-wrapped tokens, and decodes URL/base64 content up to two levels. Base64 decoding tries all four alignments; malformed percent bytes and non-ASCII decoded bytes become separators. Prefix rules have token boundaries and bounded lengths; each precompiled regex sees at most 8192 characters plus one character of boundary context on each side, with 4096-character overlap. JWT headers, payloads and signatures are capped at 512, 2500 and 1024 characters. Wrapped tokens are scanned as consecutive whitespace-free line runs with independent boundaries, not joined to subsequent prose. Merge scanning covers only bytes changed from the fetched remote plus the rebuilt `INDEX.md`, not pre-existing remote content. Scanner failures notify through the writer hook, just like findings. Merge blocks use the recovery hook; production owner delivery still waits on a notifier. It fails closed above 1 MiB per file, 8 MiB or 1024 files per call, 4096 allowlist entries per organization, or 64 MiB of weighted work (transformed characters, 4096 units per inspect and decoder run, window characters, and 64 units per regex match). Scanning yields between windows and decoder candidates so multi-file batches do not monopolize the event loop. Base64 runs shorter than 27 characters and fully percent-encoded runs shorter than 60 characters are not decoded; mixed percent/plain runs retain a 20-byte minimum to prevent partial-encoding bypasses. Splits by a space, `\r`, U+2028, across journal bullets, markdown or HTML comments, `\u` escapes, and hex encoding remain out of scope. These are conservative detectors, not a guarantee against arbitrary encryption or steganography. +The scanner checks paths as well as content, redacts paths that contain a finding, normalizes NFKC, common Cyrillic/Greek/Latin lookalikes and Default Ignorable code points, checks newline-wrapped tokens, and decodes URL/base64 content up to two levels. Base64 decoding tries all four alignments; malformed percent bytes and non-ASCII decoded bytes become separators. Prefix rules use alphanumeric left boundaries and body-specific right boundaries (fixed-width Google keys also tolerate underscore metadata suffixes); each precompiled regex sees at most 8192 characters plus one character of boundary context on each side, with 4096-character overlap. JWT detection matches a bounded prefix: an eyJ header followed by 5–2048 base64url characters, then an eyJ payload prefix followed by 8–1024 characters, without requiring the signature or the payload end. Its maximum match is 3080 characters, within the overlap. Wrapped tokens are inspected after each appended fragment, retaining up to eight fragments and 4 KiB. A prose line closes the prior run with its first word and opens a new run with its last word, so adjacent prose and trailing words do not hide a wrapped token. Merge scanning covers only bytes changed from the fetched remote plus the rebuilt `INDEX.md`, not pre-existing remote content. Scanner failures notify through the writer hook, just like findings. Staged YAML parser errors omit source-bearing diagnostics and filesystem path errors omit the underlying path-bearing cause. Merge blocks use the recovery hook; production owner delivery still waits on a notifier. It fails closed above 1 MiB per file, 8 MiB or 1024 files per call, 4096 allowlist entries per organization, or 64 MiB of weighted work (inspected characters, encoded candidate characters, window characters, and 64 units per regex match). A cheap anchor pre-filter skips rule execution on irrelevant windows, including decoded candidates; recursive decoding remains active to find double-encoded tokens. Scanning yields between windows and decoder candidates so multi-file batches do not monopolize the event loop. Base64 runs shorter than 27 characters and fully percent-encoded runs shorter than 60 characters are not decoded; mixed percent/plain runs retain a 20-byte minimum to prevent partial-encoding bypasses. Wrapped runs beyond eight fragments or 4 KiB, splits by a space, `\r`, U+2028, across journal bullets, markdown or HTML comments, `\u` escapes, and hex encoding remain out of scope. These are conservative detectors, not a guarantee against arbitrary encryption or steganography. ### Owner-signed purge (gap 5) From 195491d6f37be269d75e42a649cbfa937129ed53 Mon Sep 17 00:00:00 2001 From: GLDRoger <130458023+GLDRoger@users.noreply.github.com> Date: Sun, 8 Nov 2026 15:16:02 +0530 Subject: [PATCH 11/15] fix(kin): treat nested decode errors as permanent; warn once per retried fire Co-Authored-By: Claude Opus 5.5 --- apps/server/src/kin/memory/MemoryWriter.test.ts | 1 + apps/server/src/kin/triggers/routines.ts | 14 +++++++++++++- docs/kin/specs/triggers.md | 5 ++--- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/apps/server/src/kin/memory/MemoryWriter.test.ts b/apps/server/src/kin/memory/MemoryWriter.test.ts index 34c91262e5e00..6668c9f2674a5 100644 --- a/apps/server/src/kin/memory/MemoryWriter.test.ts +++ b/apps/server/src/kin/memory/MemoryWriter.test.ts @@ -546,6 +546,7 @@ it.live.each([ }) .pipe(Effect.result); expect(result._tag).toBe(scenario === "owner-keep" ? "Success" : "Failure"); + if (result._tag === "Failure") expect(result.failure._tag).toBe("MemoryConflict"); if (result._tag === "Success") { const committed = yield* writer.commitWakeup(orgId, "wake1", "completed"); expect( diff --git a/apps/server/src/kin/triggers/routines.ts b/apps/server/src/kin/triggers/routines.ts index 0b38feae03f51..a72aad7be1e33 100644 --- a/apps/server/src/kin/triggers/routines.ts +++ b/apps/server/src/kin/triggers/routines.ts @@ -98,7 +98,15 @@ export class KinRoutines extends Context.Service< } >()("t3/kin/triggers/routines/KinRoutines") {} +const isPermanent = (cause: unknown): boolean => + Schema.isSchemaError(cause) || + (typeof cause === "object" && + cause !== null && + (("_tag" in cause && cause._tag === "PersistenceDecodeError") || + ("cause" in cause && isPermanent(cause.cause)))); + const make = Effect.gen(function* () { + const warnedFires = new Set(); const sql = yield* SqlClient.SqlClient; const store = yield* KinTriggerStore.KinTriggerStore; const scheduler = yield* KinTriggerScheduler.KinTriggerScheduler; @@ -364,7 +372,11 @@ const make = Effect.gen(function* () { Effect.catch((cause) => Effect.gen(function* () { const consume = - Schema.isSchemaError(cause) || now - Date.parse(fire.dueAt) > 24 * 60 * 60 * 1000; + isPermanent(cause) || now - Date.parse(fire.dueAt) > 24 * 60 * 60 * 1000; + const key = `${orgId}:${fire.fireId}`; + if (consume) warnedFires.delete(key); + else if (warnedFires.has(key)) return; + else warnedFires.add(key); yield* Effect.logWarning("Kin routine fire processing failed", { orgId, fireId: fire.fireId, diff --git a/docs/kin/specs/triggers.md b/docs/kin/specs/triggers.md index acb522bbaf5b4..5d2cf3fafd597 100644 --- a/docs/kin/specs/triggers.md +++ b/docs/kin/specs/triggers.md @@ -267,9 +267,8 @@ lands. Before enabling it: - production `MemoryWriterAuthority.resolve` must grant `kind: "owner_check"` only after verifying the claimed owner tap, binding its item id and action. An owner chat citation is not review-date authority; -- web and mobile must render the native card, its handled items, and closed state. - -If a concurrent card update wins after Keep or Drop is staged, the writer session is discarded before returning the revision conflict. +- web and mobile must render the native card, its handled items, and closed state; +- a revision conflict after Keep or Drop is staged must unstage only that item's path. Today it discards the whole wake-up session, which would drop the lead's other staged writes. Each accepted action advances the card revision and marks that item handled; the last handled item closes it. Misleading-memory selection reads reports From 303938160d037520df85c14af99b664f228ffbdf Mon Sep 17 00:00:00 2001 From: GLDRoger <130458023+GLDRoger@users.noreply.github.com> Date: Sun, 8 Nov 2026 15:53:02 +0530 Subject: [PATCH 12/15] fix(kin): skip short anchorless wrapped runs; yield in the line loop Every line re-inspected its joined run, so 8 MiB of short lines took ~30 s. No rule matches without an anchor and no decoder starts below 20 characters. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/kin/memory/secretScan.ts | 6 +++++- docs/kin/specs/triggers.md | 4 +++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/apps/server/src/kin/memory/secretScan.ts b/apps/server/src/kin/memory/secretScan.ts index 9a232393079fa..7e39bf38ff76f 100644 --- a/apps/server/src/kin/memory/secretScan.ts +++ b/apps/server/src/kin/memory/secretScan.ts @@ -260,9 +260,13 @@ const make = Effect.gen(function* () { run.push({ text, line }); while (run.length > 8 || run.reduce((sum, part) => sum + part.text.length, 0) > 4096) run.shift(); - if (run.length > 1) yield* inspect(run.map((part) => part.text).join(""), run[0]!.line, 0); + if (run.length < 2) return; + const joined = run.map((part) => part.text).join(""); + // No rule matches without an anchor and no decoder starts below 20 characters. + if (joined.length >= 20 || anchors.test(joined)) yield* inspect(joined, run[0]!.line, 0); }; for (const [index, raw] of lines.entries()) { + if (index % 1024 === 1023) yield* Effect.yieldNow; const words = raw .replace(/\\?\r$/, "") .replace(/\\$/, "") diff --git a/docs/kin/specs/triggers.md b/docs/kin/specs/triggers.md index 944aee345322f..1b1a3f7917edc 100644 --- a/docs/kin/specs/triggers.md +++ b/docs/kin/specs/triggers.md @@ -344,7 +344,9 @@ GitHub then blocks a bad auto-merge even if Kin's classifier is wrong. This forc **False positives:** the owner adds the fingerprint to `kin_secret_allow` through a signed action. A model can never add to it. Entries are scoped to organization, exact path, rule and SHA-256 of `ruleId + "\0" + normalized matched value`, with `created_at` in integer milliseconds and `signed_action_id` reserved for the future APPR executor id. The scanner is read-only; the APPR executor must verify the owner grant before inserting. Until that executor lands, there is no agent or MCP allowlist-write path. Migration K012 is provisional and will be renumbered at merge. -The scanner checks paths as well as content, redacts paths that contain a finding, normalizes NFKC, common Cyrillic/Greek/Latin lookalikes and Default Ignorable code points, checks newline-wrapped tokens, and decodes URL/base64 content up to two levels. Base64 decoding tries all four alignments; malformed percent bytes and non-ASCII decoded bytes become separators. Prefix rules use alphanumeric left boundaries and body-specific right boundaries (fixed-width Google keys also tolerate underscore metadata suffixes); each precompiled regex sees at most 8192 characters plus one character of boundary context on each side, with 4096-character overlap. JWT detection matches a bounded prefix: an eyJ header followed by 5–2048 base64url characters, then an eyJ payload prefix followed by 8–1024 characters, without requiring the signature or the payload end. Its maximum match is 3080 characters, within the overlap. Wrapped tokens are inspected after each appended fragment, retaining up to eight fragments and 4 KiB. A prose line closes the prior run with its first word and opens a new run with its last word, so adjacent prose and trailing words do not hide a wrapped token. Merge scanning covers only bytes changed from the fetched remote plus the rebuilt `INDEX.md`, not pre-existing remote content. Scanner failures notify through the writer hook, just like findings. Staged YAML parser errors omit source-bearing diagnostics and filesystem path errors omit the underlying path-bearing cause. Merge blocks use the recovery hook; production owner delivery still waits on a notifier. It fails closed above 1 MiB per file, 8 MiB or 1024 files per call, 4096 allowlist entries per organization, or 64 MiB of weighted work (inspected characters, encoded candidate characters, window characters, and 64 units per regex match). A cheap anchor pre-filter skips rule execution on irrelevant windows, including decoded candidates; recursive decoding remains active to find double-encoded tokens. Scanning yields between windows and decoder candidates so multi-file batches do not monopolize the event loop. Base64 runs shorter than 27 characters and fully percent-encoded runs shorter than 60 characters are not decoded; mixed percent/plain runs retain a 20-byte minimum to prevent partial-encoding bypasses. Wrapped runs beyond eight fragments or 4 KiB, splits by a space, `\r`, U+2028, across journal bullets, markdown or HTML comments, `\u` escapes, and hex encoding remain out of scope. These are conservative detectors, not a guarantee against arbitrary encryption or steganography. +The scanner checks paths as well as content, redacts paths that contain a finding, normalizes NFKC, common Cyrillic/Greek/Latin lookalikes and Default Ignorable code points, checks newline-wrapped tokens, and decodes URL/base64 content up to two levels. Base64 decoding tries all four alignments; malformed percent bytes and non-ASCII decoded bytes become separators. Prefix rules use alphanumeric left boundaries and body-specific right boundaries (fixed-width Google keys also tolerate underscore metadata suffixes); each precompiled regex sees at most 8192 characters plus one character of boundary context on each side, with 4096-character overlap. JWT detection matches a bounded prefix: an eyJ header followed by 5–2048 base64url characters, then an eyJ payload prefix followed by 8–1024 characters, without requiring the signature or the payload end. Its maximum match is 3080 characters, within the overlap. Wrapped tokens are inspected after each appended fragment once the joined run has 20 characters or an anchor, retaining up to eight fragments and 4 KiB. A prose line closes the prior run with its first word and opens a new run with its last word, so adjacent prose and trailing words do not hide a wrapped token. Merge scanning covers only bytes changed from the fetched remote plus the rebuilt `INDEX.md`, not pre-existing remote content. Scanner failures notify through the writer hook, just like findings. Staged YAML parser errors omit source-bearing diagnostics and filesystem path errors omit the underlying path-bearing cause. Merge blocks use the recovery hook; production owner delivery still waits on a notifier. It fails closed above 1 MiB per file, 8 MiB or 1024 files per call, 4096 allowlist entries per organization, or 64 MiB of weighted work (inspected characters, encoded candidate characters, window characters, and 64 units per regex match). A cheap anchor pre-filter skips rule execution on irrelevant windows, including decoded candidates; recursive decoding remains active to find double-encoded tokens. Scanning yields between windows and decoder candidates so multi-file batches do not monopolize the event loop. Base64 runs shorter than 27 characters and fully percent-encoded runs shorter than 60 characters are not decoded; mixed percent/plain runs retain a 20-byte minimum to prevent partial-encoding bypasses. Wrapped runs beyond eight fragments or 4 KiB, splits by a space, `\r`, U+2028, across journal bullets, markdown or HTML comments, `\u` escapes, and hex encoding remain out of scope. These are conservative detectors, not a guarantee against arbitrary encryption or steganography. + +Known gaps, accepted for phase 1: JWE (five-part) tokens and JWT headers over 2048 characters; multi-part secrets (JWT, PEM header, URL password, `KEY=value`) with only some bytes percent-encoded; lookalikes outside the folding table (for example Greek `ρ`, Cyrillic `І`/`Ј`/`к`, small caps, U+2800, combining marks inside an anchor). ### Owner-signed purge (gap 5) From a8b5dffb2911dcebf1eb049eff1797c0b26a97dc Mon Sep 17 00:00:00 2001 From: GLDRoger <130458023+GLDRoger@users.noreply.github.com> Date: Sun, 8 Nov 2026 16:00:50 +0530 Subject: [PATCH 13/15] fix(kin): bounded decode-cause walk; retries log at debug, drops warn Co-Authored-By: Claude Opus 5.5 --- apps/server/src/kin/triggers/routines.test.ts | 36 +++++++++++++++++ apps/server/src/kin/triggers/routines.ts | 40 ++++++++++--------- 2 files changed, 57 insertions(+), 19 deletions(-) diff --git a/apps/server/src/kin/triggers/routines.test.ts b/apps/server/src/kin/triggers/routines.test.ts index 831d3aa0e3cf2..ed48e02633d73 100644 --- a/apps/server/src/kin/triggers/routines.test.ts +++ b/apps/server/src/kin/triggers/routines.test.ts @@ -9,6 +9,7 @@ import * as Layer from "effect/Layer"; import * as Schema from "effect/Schema"; import * as SqlClient from "effect/sql/SqlClient"; import * as TestClock from "effect/testing/TestClock"; +import * as Memory from "../memory/Memory.ts"; import * as Routines from "./routines.ts"; import * as TriggerScheduler from "./KinTriggerScheduler.ts"; import * as TriggerStore from "./KinTriggerStore.ts"; @@ -323,6 +324,41 @@ it.effect("waits for Memory's org lock without holding SQLite's writer", () => }).pipe(Effect.scoped), ); +const decodeNoItems = Schema.decodeUnknownEffect(Schema.Array(Schema.Never)); + +it.effect("consumes an owner check whose selection fails to decode", () => + Effect.gen(function* () { + yield* seed(); + const sql = yield* SqlClient.SqlClient; + const routines = yield* Routines.KinRoutines; + const scheduler = yield* TriggerScheduler.KinTriggerScheduler; + yield* TestClock.setTime(Date.parse("2026-11-09T03:29:59.000Z")); + yield* routines.configure(settings); + yield* TestClock.adjust("1 second"); + yield* scheduler.runDue(); + yield* routines.drain(orgA); + assert.lengthOf( + yield* sql`SELECT fire_id FROM kin_trigger_inbox WHERE org_id = ${orgA} AND consumed_at IS NULL ORDER BY fire_id`, + 0, + ); + assert.lengthOf( + yield* sql`SELECT event_id FROM kin_events WHERE org_id = ${orgA} AND json_extract(json, '$.routine') = 'owner_check' ORDER BY event_id`, + 0, + ); + }).pipe( + Effect.provide( + fixture([], { + query: () => () => + decodeNoItems(["bad"]).pipe( + Effect.mapError( + (cause) => new Memory.MemoryError({ orgId: orgA, operation: "query", cause }), + ), + ), + }), + ), + ), +); + it.effect("reconciles at fifteen minutes, not intervening five-second ticks", () => Effect.gen(function* () { let tick = Effect.void; diff --git a/apps/server/src/kin/triggers/routines.ts b/apps/server/src/kin/triggers/routines.ts index a72aad7be1e33..691851fbf2693 100644 --- a/apps/server/src/kin/triggers/routines.ts +++ b/apps/server/src/kin/triggers/routines.ts @@ -17,7 +17,7 @@ import * as Layer from "effect/Layer"; import * as Schema from "effect/Schema"; import * as SqlClient from "effect/sql/SqlClient"; import { isSqlError } from "effect/sql/SqlError"; -import { toPersistenceSqlError } from "../../persistence/Errors.ts"; +import { PersistenceDecodeError, toPersistenceSqlError } from "../../persistence/Errors.ts"; import { nextScheduledRunAt } from "../../scheduledTasks/Schedule.ts"; import * as Scheduler from "../../scheduling/Scheduler.ts"; import * as KinEventQueue from "../lead/KinEventQueue.ts"; @@ -98,15 +98,17 @@ export class KinRoutines extends Context.Service< } >()("t3/kin/triggers/routines/KinRoutines") {} -const isPermanent = (cause: unknown): boolean => - Schema.isSchemaError(cause) || - (typeof cause === "object" && - cause !== null && - (("_tag" in cause && cause._tag === "PersistenceDecodeError") || - ("cause" in cause && isPermanent(cause.cause)))); +const isDecodeError = Schema.is(PersistenceDecodeError); +const isPermanent = (error: unknown) => { + let current = error; + for (let depth = 0; depth < 8 && typeof current === "object" && current !== null; depth++) { + if (Schema.isSchemaError(current) || isDecodeError(current)) return true; + current = "cause" in current ? current.cause : undefined; + } + return false; +}; const make = Effect.gen(function* () { - const warnedFires = new Set(); const sql = yield* SqlClient.SqlClient; const store = yield* KinTriggerStore.KinTriggerStore; const scheduler = yield* KinTriggerScheduler.KinTriggerScheduler; @@ -373,17 +375,17 @@ const make = Effect.gen(function* () { Effect.gen(function* () { const consume = isPermanent(cause) || now - Date.parse(fire.dueAt) > 24 * 60 * 60 * 1000; - const key = `${orgId}:${fire.fireId}`; - if (consume) warnedFires.delete(key); - else if (warnedFires.has(key)) return; - else warnedFires.add(key); - yield* Effect.logWarning("Kin routine fire processing failed", { - orgId, - fireId: fire.fireId, - errorTag: cause._tag, - message: cause.message, - disposition: consume ? "consumed" : "retry", - }); + // Retries recur every tick for up to 24 h, so only the final drop warns. + yield* (consume ? Effect.logWarning : Effect.logDebug)( + "Kin routine fire processing failed", + { + orgId, + fireId: fire.fireId, + errorTag: cause._tag, + message: cause.message, + disposition: consume ? "consumed" : "retry", + }, + ); if (consume) yield* inbox.markConsumed(orgId, [fire.fireId]); }), ), From 7df9d3f5a0bd7665fd8c7b7d8bba0b0d6839287e Mon Sep 17 00:00:00 2001 From: GLDRoger <130458023+GLDRoger@users.noreply.github.com> Date: Sun, 8 Nov 2026 16:11:34 +0530 Subject: [PATCH 14/15] docs(kin): correct JWT header limit; note path-list work limit Co-Authored-By: Claude Opus 5.5 --- docs/kin/specs/triggers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/kin/specs/triggers.md b/docs/kin/specs/triggers.md index 1b1a3f7917edc..f2f928008be37 100644 --- a/docs/kin/specs/triggers.md +++ b/docs/kin/specs/triggers.md @@ -346,7 +346,7 @@ GitHub then blocks a bad auto-merge even if Kin's classifier is wrong. This forc The scanner checks paths as well as content, redacts paths that contain a finding, normalizes NFKC, common Cyrillic/Greek/Latin lookalikes and Default Ignorable code points, checks newline-wrapped tokens, and decodes URL/base64 content up to two levels. Base64 decoding tries all four alignments; malformed percent bytes and non-ASCII decoded bytes become separators. Prefix rules use alphanumeric left boundaries and body-specific right boundaries (fixed-width Google keys also tolerate underscore metadata suffixes); each precompiled regex sees at most 8192 characters plus one character of boundary context on each side, with 4096-character overlap. JWT detection matches a bounded prefix: an eyJ header followed by 5–2048 base64url characters, then an eyJ payload prefix followed by 8–1024 characters, without requiring the signature or the payload end. Its maximum match is 3080 characters, within the overlap. Wrapped tokens are inspected after each appended fragment once the joined run has 20 characters or an anchor, retaining up to eight fragments and 4 KiB. A prose line closes the prior run with its first word and opens a new run with its last word, so adjacent prose and trailing words do not hide a wrapped token. Merge scanning covers only bytes changed from the fetched remote plus the rebuilt `INDEX.md`, not pre-existing remote content. Scanner failures notify through the writer hook, just like findings. Staged YAML parser errors omit source-bearing diagnostics and filesystem path errors omit the underlying path-bearing cause. Merge blocks use the recovery hook; production owner delivery still waits on a notifier. It fails closed above 1 MiB per file, 8 MiB or 1024 files per call, 4096 allowlist entries per organization, or 64 MiB of weighted work (inspected characters, encoded candidate characters, window characters, and 64 units per regex match). A cheap anchor pre-filter skips rule execution on irrelevant windows, including decoded candidates; recursive decoding remains active to find double-encoded tokens. Scanning yields between windows and decoder candidates so multi-file batches do not monopolize the event loop. Base64 runs shorter than 27 characters and fully percent-encoded runs shorter than 60 characters are not decoded; mixed percent/plain runs retain a 20-byte minimum to prevent partial-encoding bypasses. Wrapped runs beyond eight fragments or 4 KiB, splits by a space, `\r`, U+2028, across journal bullets, markdown or HTML comments, `\u` escapes, and hex encoding remain out of scope. These are conservative detectors, not a guarantee against arbitrary encryption or steganography. -Known gaps, accepted for phase 1: JWE (five-part) tokens and JWT headers over 2048 characters; multi-part secrets (JWT, PEM header, URL password, `KEY=value`) with only some bytes percent-encoded; lookalikes outside the folding table (for example Greek `ρ`, Cyrillic `І`/`Ј`/`к`, small caps, U+2800, combining marks inside an anchor). +Known gaps, accepted for phase 1: JWE (five-part) tokens and JWT headers longer than 2051 characters; multi-part secrets (JWT, PEM header, URL password, `KEY=value`) with only some bytes percent-encoded; lookalikes outside the folding table (for example Greek `ρ`, Cyrillic `І`/`Ј`/`к`, small caps, U+2800, combining marks inside an anchor). A file of about 1 MiB made only of long whitespace-free lines (such as one path per line) exhausts the work budget after about 15 s and fails closed, blocking that commit; realistic journals up to 1 MiB pass. ### Owner-signed purge (gap 5) From a993df11872265901e452d6205375f15f740b655 Mon Sep 17 00:00:00 2001 From: GLDRoger <130458023+GLDRoger@users.noreply.github.com> Date: Sun, 8 Nov 2026 16:52:54 +0530 Subject: [PATCH 15/15] test(kin): provide JudgeShadow.layerOff in routines testkit Co-Authored-By: Claude Opus 5.5 --- apps/server/src/kin/triggers/routines.testkit.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/server/src/kin/triggers/routines.testkit.ts b/apps/server/src/kin/triggers/routines.testkit.ts index 4c1eb4fafbd0d..a1c47e4ae09af 100644 --- a/apps/server/src/kin/triggers/routines.testkit.ts +++ b/apps/server/src/kin/triggers/routines.testkit.ts @@ -8,6 +8,7 @@ import * as Scheduler from "../../scheduling/Scheduler.ts"; import * as ThreadManagement from "../../orchestration-v2/ThreadManagementService.ts"; import * as Memory from "../memory/Memory.ts"; import * as Chat from "../chat/KinChatService.ts"; +import * as JudgeShadow from "../floor/JudgeShadow.ts"; import * as Repository from "../chat/KinChatRepository.ts"; import * as Hub from "../chat/KinChatHub.ts"; import * as Persistence from "../persistence/KinPersistence.ts"; @@ -80,7 +81,7 @@ export function fixture( }), ), ), - Chat.layer.pipe(Layer.provideMerge(Hub.layer)), + Chat.layer.pipe(Layer.provide(JudgeShadow.layerOff), Layer.provideMerge(Hub.layer)), TriggerScheduler.layer, ).pipe( Layer.provideMerge(repositories),