Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions apps/server/src/kin/dreaming/checkDream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ it.effect.each([
".github/workflows/memory-check.yml is outside item files and journal/",
],
},
{
name: "rejects item and journal paths memory-check would refuse",
tree: new Map([
...goodTree,
["claims/acme.renewal.md", "x\n"],
["commitments/q4/ship.md", "x\n"],
["journal/2026/week-45.md", "x\n"],
]),
failures: [
"claims/acme.renewal.md is outside item files and journal/",
"commitments/q4/ship.md is outside item files and journal/",
"journal/2026/week-45.md is outside item files and journal/",
],
},
{
name: "rejects a change dream.json does not list",
dream: { ...goodDream, changes: goodDream.changes.slice(1) },
Expand Down
3 changes: 2 additions & 1 deletion apps/server/src/kin/dreaming/checkDream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";

import { parseFrontMatter } from "../memory/frontMatter.ts";
import { kinShapeFailure } from "../memory/lint.ts";
import { itemFolders, type MaterialEntry } from "./material.ts";

const Sources = Schema.Array(Schema.String).check(Schema.isMinLength(1));
Expand Down Expand Up @@ -85,7 +86,7 @@ export const checkDream = Effect.fn("DreamRunner.check")(function* (input: {
failures.push("INDEX.md is built by code");
continue;
}
if (!isItemPath(path) && !isJournalPath(path)) {
if ((!isItemPath(path) && !isJournalPath(path)) || kinShapeFailure(path) !== undefined) {
failures.push(`${path} is outside item files and journal/`);
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions apps/server/src/kin/memory/Memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,9 @@ export class Memory extends Context.Service<
>()("t3/kin/memory/Memory") {}

const decodeHead = Schema.decodeEffect(MemoryItemHead);
const folders = ["objectives", "commitments", "deferrals", "decisions", "people", "claims"];
export const folders = ["objectives", "commitments", "deferrals", "decisions", "people", "claims"];

const maxBlobBytes = 256 * 1024;
export const maxBlobBytes = 256 * 1024;

interface Snapshot extends MemorySyncSnapshot {
readonly files: ReadonlyMap<string, string>;
Expand Down
4 changes: 4 additions & 0 deletions apps/server/src/kin/memory/MemoryWriter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,10 @@ it.live.each([
"/claims/a.md",
"claims/.git/a.md",
"claims/../../a.md",
// Paths memory-check refuses in Kin's unowned folders.
"deferrals/a.b.md",
"deferrals/nested/a.md",
"deferrals/CLAUDE.md",
]) {
expect(
(yield* writer
Expand Down
3 changes: 3 additions & 0 deletions apps/server/src/kin/memory/MemoryWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
} from "./Memory.ts";
import { parseFrontMatter, MemoryItemError } from "./frontMatter.ts";
import { journalEntry, MemoryJournalError, type JournalContext } from "./journal.ts";
import { kinShapeFailure } from "./lint.ts";
import { KinSecretScan, KinSecretScanError } from "./secretScan.ts";

export const LEAD_ENDED_WITHOUT_KIN_END = "Lead ended without calling kin_end.";
Expand Down Expand Up @@ -363,6 +364,8 @@ const make = Effect.gen(function* () {
(!(generated && relative === "INDEX.md") && ![...folders, "journal"].includes(parts[0] ?? ""))
)
return yield* new MemoryWriteRejected({ reason: "Disallowed memory path" });
const shape = kinShapeFailure(relative);
if (shape !== undefined) return yield* new MemoryWriteRejected({ reason: shape });
let current = writer;
for (const part of parts) {
current += `/${part}`;
Expand Down
69 changes: 69 additions & 0 deletions apps/server/src/kin/memory/gitleaksConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { secretExceptions, secretRules } from "./secretScan.ts";

const space = String.raw`\s\v\p{Z}\x{feff}`;

// RE2 caps counted repeats at 1000, has no lookarounds and its \s misses \v and Unicode spaces.
// Boundaries are dropped rather than emulated: a consuming boundary would hide an adjacent match.
const re2 = (pattern: RegExp) => {
let source = "";
let inClass = false;
for (let index = 0; index < pattern.source.length; index++) {
const char = pattern.source[index]!;
if (char === "\\") {
const next = pattern.source[++index]!;
if (next === "S") throw new Error(`\\S has no RE2 translation: ${pattern.source}`);
source += next === "s" ? (inClass ? space : `[${space}]`) : `\\${next}`;
continue;
}
if (char === "[") inClass = true;
else if (char === "]") inClass = false;
source += char;
}
source = source.replace(/\{(\d+),(\d+)\}/g, (all, min: string, max: string) =>
Number(max) > 1000 ? `{${min},}` : all,
);
return pattern.flags.includes("i") ? `(?i:${source})` : source;
};

const literal = (value: string) => {
if (value.includes("'''")) throw new Error(`Pattern cannot be a TOML literal: ${value}`);
return `'''${value}'''`;
};

const rule = ([id, pattern]: (typeof secretRules)[number]) => {
const groups = (new RegExp(`${pattern.source}|`).exec("")?.length ?? 1) - 1;
const exception = secretExceptions[id];
return [
"[[rules]]",
`id = "${id}"`,
`description = "KinSecretScan ${id}"`,
`regex = ${literal(re2(pattern))}`,
...(groups > 0 ? ["secretGroup = 1"] : []),
...(exception?.minEntropy === undefined ? [] : [`entropy = ${exception.minEntropy}`]),
...(["secret", "match"] as const).flatMap((target) => {
const patterns = exception?.[target]?.map((p) => literal(re2(p)));
if (patterns === undefined) return [];
// Wrapped the way the repo formatter wraps TOML arrays past 100 columns.
const inline = `regexes = [${patterns.join(", ")}]`;
return [
"",
"[[rules.allowlists]]",
`regexTarget = "${target}"`,
...(inline.length <= 100
? [inline]
: ["regexes = [", ...patterns.map((p) => ` ${p},`), "]"]),
];
}),
].join("\n");
};

export const gitleaksToml = [
[
"# Generated from apps/server/src/kin/memory/secretScan.ts; do not edit.",
"# Regenerate: node apps/server/src/kin/memory/gitleaksConfig.ts > ops/kin/memory-repo/gitleaks.toml",
'title = "kin-memory"',
].join("\n"),
...secretRules.map(rule),
].join("\n\n");

if (import.meta.main) process.stdout.write(`${gitleaksToml}\n`);
Loading