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
2 changes: 2 additions & 0 deletions apps/server/scripts/kin-shadow-dry-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,8 @@ export const runDryRun = Effect.fn("KinDryRun.run")(
);
const memoryConfig = MemoryConfig.of({
baseDirectory: path.join(stateDir, "kin"),
// Writes reach only the local mirror, so a write to main shows up in the ref check below.
directMainPush: true,
repositories: [{ orgId, repoUrl: memoryUrl, path: encodeURIComponent(orgId) }],
});
const refsBefore = yield* listRefs(stateDir, memoryUrl);
Expand Down
39 changes: 23 additions & 16 deletions apps/server/src/kin/github/testFakes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ export interface FakeAnswer {
}

/** A GitHub that answers from `route`; nothing leaves the process. */
export const fakeGitHub = (route: (request: RecordedRequest) => FakeAnswer) => {
export const fakeGitHub = (
route: (request: RecordedRequest) => FakeAnswer | Effect.Effect<FakeAnswer>,
) => {
const requests: Array<RecordedRequest> = [];
const layer = Layer.succeed(
HttpClient.HttpClient,
Expand All @@ -81,21 +83,26 @@ export const fakeGitHub = (route: (request: RecordedRequest) => FakeAnswer) => {
};
requests.push(recorded);
const answer = route(recorded);
if (answer.hang === true) return Effect.never;
return Effect.succeed(
HttpClientResponse.fromWeb(
request,
new Response(
answer.stallBody === true
? new ReadableStream()
: answer.body === undefined
? null
: JSON.stringify(answer.body),
{
status: answer.status,
headers: { "content-type": "application/json", ...answer.headers },
},
),
return (Effect.isEffect(answer) ? answer : Effect.succeed(answer)).pipe(
Effect.flatMap((answer) =>
answer.hang === true
? Effect.never
: Effect.succeed(
HttpClientResponse.fromWeb(
request,
new Response(
answer.stallBody === true
? new ReadableStream()
: answer.body === undefined
? null
: JSON.stringify(answer.body),
{
status: answer.status,
headers: { "content-type": "application/json", ...answer.headers },
},
),
),
),
),
);
}),
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/kin/lead/LeadMemory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ it.live(
Layer.provide(
Layer.succeed(MemoryConfig, {
baseDirectory: `${root}/state`,
directMainPush: true,
repositories: [
{ orgId, path: "a", repoUrl: remote },
{ orgId: other, path: "b", repoUrl: remoteB },
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/kin/lead/WakeupRunner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ describe("WakeupRunner", () => {
commitDream: () => Effect.die("unused"),
deleteDreamBranch: () => Effect.die("unused"),
settleShadow: () => Effect.die("unused"),
sweepPullRequests: () => Effect.die("unused"),
stage: (_org, wakeup) =>
Effect.sync(() => {
assert.isTrue(owner === undefined || owner === wakeup);
Expand Down Expand Up @@ -223,6 +224,7 @@ describe("WakeupRunner", () => {
commitDream: () => Effect.die("unused"),
deleteDreamBranch: () => Effect.die("unused"),
settleShadow: () => Effect.die("unused"),
sweepPullRequests: () => Effect.die("unused"),
discard: () => Effect.void,
appendJournal: (_org, _wakeup, lines) =>
Effect.sync(() =>
Expand Down
59 changes: 59 additions & 0 deletions apps/server/src/kin/lead/layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import * as Memory from "../memory/Memory.ts";
import * as MemoryConfig from "../memory/MemoryConfig.ts";
import * as KinPersistence from "../persistence/KinPersistence.ts";
import * as Writer from "../memory/MemoryWriter.ts";
import * as MemoryPullRequests from "../memory/MemoryPullRequests.ts";
import * as Cause from "effect/Cause";
import * as Clock from "effect/Clock";
import * as Scan from "../memory/secretScan.ts";
import * as LeadMemory from "./LeadMemory.ts";
import * as Cache from "../memory/MemoryCache.ts";
Expand Down Expand Up @@ -83,6 +86,32 @@ const dreamRuntime = DreamRunner.layer.pipe(
Layer.provide(Cache.layer),
);

// TRIG-15: at startup and every 5 minutes, close memory PRs no write waits on.
const memoryPullRequestSweep = Effect.fn("KinMemoryPullRequests.sweepDue")(function* (
writer: Writer.MemoryWriter["Service"],
) {
const { repositories } = yield* MemoryConfig.MemoryConfig;
let due = 0;
return Effect.gen(function* () {
const now = yield* Clock.currentTimeMillis;
if (now < due) return;
due = now + 5 * 60_000;
yield* Effect.forEach(
repositories,
({ orgId }) =>
writer.sweepPullRequests(orgId).pipe(
Effect.catchCause((cause) =>
Effect.logWarning("Kin memory PR sweep failed", {
orgId,
cause: Cause.pretty(cause),
}),
),
),
{ discard: true },
);
});
});

const runner = Layer.effectDiscard(
Effect.gen(function* () {
const runner = yield* WakeupRunner.WakeupRunner;
Expand All @@ -94,6 +123,11 @@ const runner = Layer.effectDiscard(
yield* promotion.value.attachShadowMemory(writer.value.settleShadow);
yield* Effect.gen(function* () {
const context = yield* Layer.build(routineRuntime);
if (Option.isSome(writer))
yield* Context.get(context, Scheduler.Scheduler).register(
"kin-memory-pull-requests",
yield* memoryPullRequestSweep(writer.value),
);
yield* Layer.build(dreamRuntime.pipe(Layer.provide(Layer.succeedContext(context)))).pipe(
Effect.catchCause((cause) => Effect.logError("Kin dreams unavailable", { cause })),
);
Expand Down Expand Up @@ -177,9 +211,34 @@ const withServices = <A, E, R>(entry: Layer.Layer<A, E, R>) =>
Layer.provide(GithubReaders.layer),
);

// TRIG-15: without the Kin App, memory writes to the default branch are refused unless
// KIN_MEMORY_DIRECT_PUSH=true, because the memory repo's ruleset rejects a direct push.
const memoryPullRequests = Layer.unwrap(
Effect.gen(function* () {
const off =
"memory writes to the default branch are refused unless KIN_MEMORY_DIRECT_PUSH=true";
if (Option.isNone(yield* Config.option(Config.String("KIN_GITHUB_APP_ID")))) {
yield* Effect.logWarning(`Kin memory PRs are off: KIN_GITHUB_APP_ID is unset, so ${off}`);
return Layer.empty;
}
return MemoryPullRequests.layer.pipe(
Layer.provide(GithubReaders.appLayer),
Layer.catchCause((cause) =>
Layer.effectDiscard(
Effect.logWarning(
`Kin memory PRs are off: the Kin GitHub App is unavailable, so ${off}`,
Cause.pretty(cause),
),
),
),
);
}),
);

const registration = McpHttpServer.toolkitRegistration(KinLeadToolkit, handlers);
const toolkit = LeadMemory.layer.pipe(
Layer.provideMerge(Writer.layer),
Layer.provide(memoryPullRequests),
Layer.provide(LeadMemory.authority),
Layer.provide(Cache.layer),
Layer.provide(Scan.layer),
Expand Down
28 changes: 24 additions & 4 deletions apps/server/src/kin/memory/MemoryConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,17 @@ export class MemoryConfig extends Context.Service<
readonly credentialHelper?: string;
readonly sshCommand?: string;
readonly botEmail?: string;
/**
* Without the Kin GitHub App, writes to the default branch are refused unless this is set;
* then they push directly, which a TRIG-15 ruleset on the memory repo rejects.
*/
readonly directMainPush?: boolean;
readonly repositories: ReadonlyArray<{
readonly orgId: KinOrgId;
readonly repoUrl: string;
readonly path: string;
/** `owner/name` when `repoUrl` is on github.com; memory PRs go there. */
readonly githubRepo?: string;
}>;
}
>()("t3/kin/memory/MemoryConfig") {}
Expand All @@ -38,6 +45,11 @@ export class MemoryCredentialConfigError extends Schema.TaggedError<MemoryCreden
}
}

const GITHUB_URL =
/^(?:git@github\.com:|(?:https|ssh):\/\/(?:git@)?github\.com\/)([A-Za-z0-9-]+\/[A-Za-z0-9._-]+?)(?:\.git)?\/?$/;

export const githubRepoOf = (repoUrl: string) => GITHUB_URL.exec(repoUrl)?.[1];

const credential = Effect.fn("MemoryConfig.credential")(function* (
variable: MemoryCredentialConfigError["variable"],
valid: (value: string) => boolean,
Expand All @@ -62,14 +74,22 @@ export const layer = (
isCredentialHelperReference,
);
const sshCommand = yield* credential("KIN_MEMORY_SSH_COMMAND", isSshCommandReference);
const directMainPush = yield* Config.Boolean("KIN_MEMORY_DIRECT_PUSH").pipe(
Config.withDefault(false),
);
return MemoryConfig.of({
baseDirectory: path.join(config.stateDir, "kin"),
...(credentialHelper === undefined ? {} : { credentialHelper }),
...(sshCommand === undefined ? {} : { sshCommand }),
repositories: repositories.map((repo) => ({
...repo,
path: encodeURIComponent(repo.orgId),
})),
directMainPush,
repositories: repositories.map((repo) => {
const githubRepo = githubRepoOf(repo.repoUrl);
return {
...repo,
path: encodeURIComponent(repo.orgId),
...(githubRepo === undefined ? {} : { githubRepo }),
};
}),
});
}),
);
Loading