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
58 changes: 50 additions & 8 deletions apps/server/src/kin/memory/Memory.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import * as NodeZlib from "node:zlib";
import * as NodeServices from "@effect/platform-node/NodeServices";
import { expect, it } from "@effect/vitest";
import { assert, expect, it } from "@effect/vitest";
import { KinAgentId, KinWakeupId, KinMemoryItemId, KinOrgId } from "@t3tools/contracts";
import * as DateTime from "effect/DateTime";
import * as Clock from "effect/Clock";
import * as ConfigProvider from "effect/ConfigProvider";
import * as Context from "effect/Context";
import * as SqlClient from "effect/sql/SqlClient";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
Expand Down Expand Up @@ -588,19 +590,59 @@ it.effect.each([
}).pipe(Effect.provide(Layer.merge(NodeServices.layer, databaseLayer)), Effect.scoped),
);

it.effect("derives production memory paths from the isolated server state directory", () =>
const helperScript = "!f() { echo password=fixture; }; f";
const sshInjection = "ssh -i /fixture/key; cat /etc/passwd";
it.effect.each([
{ env: {}, expected: {} },
{
env: {
KIN_MEMORY_CREDENTIAL_HELPER: "/fixture/bin/git-credential-kin",
KIN_MEMORY_SSH_COMMAND: "ssh -i /fixture/kin-memory-key -o IdentitiesOnly=yes",
},
expected: {
credentialHelper: "/fixture/bin/git-credential-kin",
sshCommand: "ssh -i /fixture/kin-memory-key -o IdentitiesOnly=yes",
},
},
{ env: { KIN_MEMORY_CREDENTIAL_HELPER: helperScript }, refused: helperScript },
...[
sshInjection,
"ssh -o ProxyCommand=/fixture/pwn.sh",
"ssh -oLocalCommand=/x -o PermitLocalCommand=yes",
"ssh -F /fixture/hostile-config",
"ssh -o StrictHostKeyChecking=no",
"sshpass -p fixture ssh",
"/fixture/any-program",
].map((value) => ({ env: { KIN_MEMORY_SSH_COMMAND: value }, refused: value })),
])("derives production memory config from the server state directory and env %#", (row) =>
Effect.gen(function* () {
const server = yield* ServerConfig.ServerConfig;
const config = yield* MemoryConfig.MemoryConfig;
const result = yield* Effect.result(
Layer.build(MemoryConfig.layer([{ orgId: orgA, repoUrl: "unused" }])).pipe(
Effect.map((context) => Context.get(context, MemoryConfig.MemoryConfig)),
Effect.provide(ConfigProvider.layer(ConfigProvider.fromUnknown(row.env))),
),
);
if (row.refused !== undefined) {
assert(result._tag === "Failure");
expect(result.failure._tag).toBe("MemoryCredentialConfigError");
expect(result.failure.message).toContain(Object.keys(row.env)[0]);
expect(result.failure.message).not.toContain(row.refused);
return;
}
assert(result._tag === "Success");
const config = result.success;
expect(config.baseDirectory).toBe(`${server.stateDir}/kin`);
expect(config.repositories[0]?.path).toBe(orgA);
expect({
credentialHelper: config.credentialHelper,
sshCommand: config.sshCommand,
}).toStrictEqual({ credentialHelper: undefined, sshCommand: undefined, ...row.expected });
}).pipe(
Effect.scoped,
Effect.provide(
MemoryConfig.layer([{ orgId: orgA, repoUrl: "unused" }]).pipe(
Layer.provideMerge(
ServerConfig.ServerConfig.layerTest(process.cwd(), { prefix: "kin-memory-config-" }),
),
Layer.provide(NodeServices.layer),
ServerConfig.ServerConfig.layerTest(process.cwd(), { prefix: "kin-memory-config-" }).pipe(
Layer.provideMerge(NodeServices.layer),
),
),
),
Expand Down
33 changes: 33 additions & 0 deletions apps/server/src/kin/memory/MemoryConfig.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import type { KinOrgId } from "@t3tools/contracts";
import * as Config from "effect/Config";
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Path from "effect/Path";
import * as Schema from "effect/Schema";

import { ServerConfig } from "../../config.ts";
import { isCredentialHelperReference, isSshCommandReference } from "./purgePush.ts";

export class MemoryConfig extends Context.Service<
MemoryConfig,
Expand All @@ -23,6 +27,27 @@ export class MemoryConfig extends Context.Service<
}
>()("t3/kin/memory/MemoryConfig") {}

export class MemoryCredentialConfigError extends Schema.TaggedError<MemoryCredentialConfigError>()(
"MemoryCredentialConfigError",
{ variable: Schema.Literals(["KIN_MEMORY_CREDENTIAL_HELPER", "KIN_MEMORY_SSH_COMMAND"]) },
) {
override get message(): string {
return this.variable === "KIN_MEMORY_CREDENTIAL_HELPER"
? "KIN_MEMORY_CREDENTIAL_HELPER must be a git credential helper name or absolute path, not an inline script."
: "KIN_MEMORY_SSH_COMMAND must be a plain ssh command naming its key file, without shell syntax.";
}
}

const credential = Effect.fn("MemoryConfig.credential")(function* (
variable: MemoryCredentialConfigError["variable"],
valid: (value: string) => boolean,
) {
const value = yield* Config.option(Config.String(variable));
if (Option.isSome(value) && !valid(value.value))
return yield* new MemoryCredentialConfigError({ variable });
return Option.getOrUndefined(value);
});

export const layer = (
repositories: ReadonlyArray<{ readonly orgId: KinOrgId; readonly repoUrl: string }>,
) =>
Expand All @@ -31,8 +56,16 @@ export const layer = (
Effect.gen(function* () {
const config = yield* ServerConfig;
const path = yield* Path.Path;
// Only memory git reads these; a server-wide GIT_SSH_COMMAND would reach every agent's git.
const credentialHelper = yield* credential(
"KIN_MEMORY_CREDENTIAL_HELPER",
isCredentialHelperReference,
);
const sshCommand = yield* credential("KIN_MEMORY_SSH_COMMAND", isSshCommandReference);
return MemoryConfig.of({
baseDirectory: path.join(config.stateDir, "kin"),
...(credentialHelper === undefined ? {} : { credentialHelper }),
...(sshCommand === undefined ? {} : { sshCommand }),
repositories: repositories.map((repo) => ({
...repo,
path: encodeURIComponent(repo.orgId),
Expand Down
22 changes: 22 additions & 0 deletions apps/server/src/kin/memory/MemoryWriter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,12 @@ it.live.each([
>)
| undefined;
let failBranch = false;
const memoryRuns: Array<Vcs.VcsProcessInput> = [];
const interceptedGit = Layer.succeed(
Vcs.VcsProcess,
Vcs.VcsProcess.of({
run: Effect.fn("WriterTest.objectSwap")(function* (input) {
if (input.operation === "Memory.gitHardened") memoryRuns.push(input);
if (
attack &&
input.args.includes("cat-file") &&
Expand Down Expand Up @@ -221,6 +223,7 @@ it.live.each([
const config = Layer.succeed(MemoryConfig, {
baseDirectory: root,
credentialHelper: "fixture-server-helper",
sshCommand: "ssh -i /fixture/kin-memory-key",
repositories: [{ orgId, repoUrl: remote, path: "state" }],
});
const synced: Array<ReadonlyArray<MemoryItemHead>> = [];
Expand Down Expand Up @@ -485,6 +488,25 @@ it.live.each([
}),
);
expect(helper.trim()).toBe("fixture-server-helper");
yield* writer.stage(orgId, "wake1", {
path: "claims/credentials.md",
content: fixtureItem({ kind: "claim" }),
});
yield* writer.commitWakeup(orgId, "wake1", "completed");
const remoteRuns = memoryRuns.filter((run) => run.args.includes(remote));
expect(
new Set(
remoteRuns.map((run) =>
run.args.find((arg) => /^(clone|fetch|ls-remote|push)$/.test(arg)),
),
),
).toStrictEqual(new Set(["clone", "fetch", "ls-remote", "push"]));
for (const run of remoteRuns) {
expect(run.args).toContain("credential.helper=fixture-server-helper");
expect(run.env?.GIT_SSH_COMMAND).toBe(
"ssh -i /fixture/kin-memory-key -F none -o BatchMode=yes -o IdentitiesOnly=yes -o IdentityAgent=none",
);
}
return;
}
if (scenario === "review-default" || scenario === "review-null") {
Expand Down
5 changes: 4 additions & 1 deletion apps/server/src/kin/memory/git.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as Schema from "effect/Schema";
import * as Option from "effect/Option";
import { MemoryConfig } from "./MemoryConfig.ts";
import { SSH_HARDENING } from "./purgePush.ts";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import { VcsProcess } from "../../vcs/VcsProcess.ts";
Expand Down Expand Up @@ -96,7 +97,9 @@ export const memoryGit = Effect.fn("Memory.gitHardened")(function* (
GIT_ASKPASS: "",
GCM_INTERACTIVE: "never",
...env,
...(config?.sshCommand ? { GIT_SSH_COMMAND: config.sshCommand } : {}),
...(config?.sshCommand
? { GIT_SSH_COMMAND: `${config.sshCommand} ${SSH_HARDENING}` }
: {}),
},
...(stdin === undefined ? {} : { stdin }),
...(onStdoutChunk === undefined ? {} : { onStdoutChunk }),
Expand Down
17 changes: 12 additions & 5 deletions apps/server/src/kin/memory/purgePush.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,18 @@ const pushFailure = (stdout: string, stderr: string): KinPurgePushError["reason"
};

// A helper name or absolute path, never an inline `!` script that could carry the secret itself.
const isCredentialHelperReference = (helper: string) =>
export const isCredentialHelperReference = (helper: string) =>
/^(?:[A-Za-z0-9][A-Za-z0-9._-]*|\/[A-Za-z0-9._/-]+)$/.test(helper);
const isSshCommandReference = (command: string) => /^[A-Za-z0-9 ._/=,:@+-]+$/.test(command);
// Options that run commands, load another config or weaken host checks win over the hardening below.
const sshOverride =
/(?:^|\s)-(?:[FJSEWw]|o\s*(?:ProxyCommand|ProxyJump|ProxyUseFdpass|LocalCommand|PermitLocalCommand|KnownHostsCommand|Include|StrictHostKeyChecking|UserKnownHostsFile|GlobalKnownHostsFile|IdentityAgent|ControlPath|ControlMaster|RemoteCommand|BatchMode)\b)/i;
/** `ssh` (or an absolute path to it) with plain arguments, so the command names a key, never holds one. */
export const isSshCommandReference = (command: string) =>
/^[A-Za-z0-9 ._/=,:@+-]+$/.test(command) &&
/^(?:ssh|\/[A-Za-z0-9._/-]*\/ssh)(?:\s|$)/.test(command) &&
!sshOverride.test(command);
/** Appended to every owner SSH command: no host config, no prompts, no agent keys. */
export const SSH_HARDENING = "-F none -o BatchMode=yes -o IdentitiesOnly=yes -o IdentityAgent=none";
/** `file://` carries no credential; https needs the helper and SSH the owner's command. */
const remoteKind = (url: string) =>
/^https:\/\//i.test(url)
Expand Down Expand Up @@ -165,9 +174,7 @@ export const gitPushLayer = (owner: {
SSH_AUTH_SOCK: undefined,
GCM_INTERACTIVE: "never",
// Without an owner SSH command, refuse SSH instead of using the host's keys or agent.
GIT_SSH_COMMAND: sshCommand
? `${sshCommand} -F none -o BatchMode=yes -o IdentitiesOnly=yes -o IdentityAgent=none`
: "false",
GIT_SSH_COMMAND: sshCommand ? `${sshCommand} ${SSH_HARDENING}` : "false",
},
allowNonZeroExit: true,
maxOutputBytes: 16 * 1024 * 1024,
Expand Down
4 changes: 2 additions & 2 deletions docs/kin/specs/lead.md
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ Upstream files touched, one line each:
- The lead's authority is its Kin tools. Tools resolve organization and lead from the calling thread, never from input.
- Trust and `said_by` are set from server-verified provenance, never by the model.
- `rules.md` cannot be written by any lead or dream.
- Leads and workers run in `full-access` mode, so they can run any shell command the server's OS user can. They must not run under the owner's account: that account holds the owner's `gh` login and keychain, which DESIGN 9.5 says agents never hold. Phase 1 requires the Kin server on the Kin host to run as a dedicated `kin` user whose only GitHub credential is the GitHub App's (triggers spec, "Running on the Kin host"). Until then, keep every lead at L0.
- Leads and workers run in `full-access` mode, so they can run any shell command the server's OS user can. They must not run under the owner's account: that account holds the owner's `gh` login and keychain, which DESIGN 9.5 says agents never hold. Phase 1 requires the Kin server on the Kin host to run as a dedicated `kin` user whose only GitHub credentials are the GitHub App's and the memory repo deploy key (triggers spec, "Running on the Kin host"). Until then, keep every lead at L0. Agents can still read that deploy key and push to the memory repo around the writer, so while leads are at L0 a ruleset on the memory repo's `main` (pull request and `memory-check`, no deploy-key bypass) keeps the key to `kin/shadow/*`, and no lead goes to L1 until memory git runs as a separate OS user (`ops/kin/host/README.md`).
- Release gate: workers are ordinary T3 threads holding a full `/mcp` credential. The `orchestration` capability that grants `delegate_task` also grants `schedule_task`, top-level `t3_thread_launch`, and `t3_thread_send` / `t3_thread_interrupt` on threads outside the worker's subtree, so a prompt-injected worker can reach other work, and its scheduled tasks outlive the worker budget and the stop switch. Scoping a credential to "delegate_task, its own children, and PR tools" needs per-tool capabilities upstream, which do not exist. Until they do, `workerLaunchReady` in `apps/server/src/kin/lead/Workers.ts` stays `false`: `kin_delegate` and `kin_follow_up` refuse with `not_allowed` at L2 and above, and below L2 they keep recording shadow entries. Flip it only when per-tool scoping lands.
- Events carry references, so webhook bodies and message text don't sit in the queue.
- Stopping is open to the owner at any time without a signature. Raising a level needs a signed grant.
Expand Down Expand Up @@ -696,7 +696,7 @@ Operators or owners can call server-only `Memory.recover(orgId)` after losing a

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.

**Running on the Kin host.** Git pushes use the server's credentials. Since system/global/repository git config is excluded, supply a server-owned `MemoryConfig.credentialHelper` command for HTTPS or `MemoryConfig.sshCommand` for SSH. These are passed through command config/environment only; repository credential helpers cannot override them. Configure the Kin host's dedicated server user and credentials before enabling writes; never put credentials in memory, repository config, or lead instructions. See also “Running on the Kin host” in `triggers.md`.
**Running on the Kin host.** System, global and repository git config are excluded, so memory git never sees the host's credentials. `MemoryConfig.layer` reads `KIN_MEMORY_CREDENTIAL_HELPER` (a helper name or absolute path, for HTTPS) and `KIN_MEMORY_SSH_COMMAND` (an ssh command naming its key file, for SSH) and every memory clone, fetch and push uses them; a value that fails `purgePush.ts`'s reference checks fails Kin startup, naming the variable but not the value. They go to memory git only, as `-c credential.helper` and `GIT_SSH_COMMAND`; a server-wide `GIT_SSH_COMMAND` would reach every agent's git. Repository credential helpers cannot override them. Configure the Kin host's dedicated server user and credentials before enabling writes; never put credentials in memory, repository config, or lead instructions. See also “Running on the Kin host” in `triggers.md`.

The existing CLI has no Kin repository configuration or service layer, so a recovery subcommand would require more than a 1–2 line upstream hook. The operator entry point remains the typed `Memory.recover(orgId)`, never RPC or MCP. Stop the runner first. From `apps/server`, run `node --input-type=module < /absolute/path/to/operator-recover.mjs` with this operator-owned script, substituting explicit database, repository and directory values (no default live-state path):

Expand Down
2 changes: 1 addition & 1 deletion docs/kin/specs/triggers.md
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ Kin runs on one always-on machine, the **Kin host**. The laptop and phone are cl

- `git`, `gh`, `git-filter-repo` and `gitleaks` from the distribution or their release binaries.
- Node 26 (the repo's `engines`) through `mise`, which the host already uses, and `vp`.
- A dedicated `kin` user (`sudo useradd -m kin`). The Kin server runs as this user, never as the owner's account. Its only GitHub credential is the GitHub App's, so agents never see the owner's `gh` login (lead spec, security notes).
- A dedicated `kin` user (`sudo useradd -m kin`). The Kin server runs as this user, never as the owner's account. Its only GitHub credentials are the GitHub App's and a memory repo deploy key kept outside `~/.ssh` and passed to memory git alone through `KIN_MEMORY_SSH_COMMAND` ([host README](../../../ops/kin/host/README.md#memory-repo-access)), so agents never see the owner's `gh` login (lead spec, security notes).
- The providers' CLIs (Claude Code, Codex) installed for the `kin` user and signed in to the owner's plans.
- As `kin`: clone `GLDRoger/t3code` to `~/t3code`, `git switch kin`, `cp .env.example .env` (it holds the production relay URL), then `ops/kin/host/update.sh`, which makes the first build.
- `sudo ops/kin/host/setup.sh` creates the user, turns on linger, masks sleep, installs and enables the units, and prints a pass/fail checklist of everything above. It is safe to re-run, and `--dry-run` shows every action. Steps: [`ops/kin/host/README.md`](../../../ops/kin/host/README.md).
Expand Down
Loading