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
30 changes: 16 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,26 +199,29 @@ view, not the record.
the resident agent's session beside the heartbeat and LLM-log ones. On every
`context` event it groups messages into units, asks the decision model which
new units a future response needs (`noul` questions), and drops the ones it
says no to. Every failure path fails **open**: no key on a keyless replica,
any HTTP error, a timeout, or the switch turned off all leave the context
untouched, so the agent never loses a turn to it. Off is `on`-by-default with
a visible switch, per this repo's rule.
says no to. Every failure path fails **open**: no key stored, any HTTP
error, a timeout, or the switch turned off all leave the context untouched,
so the agent never loses a turn to it. Off is `on`-by-default with a visible
switch, per this repo's rule.
- `src/jev-curator/` — the decision logic itself, vendored near-verbatim from
the MIT `pi-jev-context-curator` package with the TUI stripped and config
injected. See `src/jev-curator/README.md` for exactly what changed and why it
is in-tree rather than a dependency.
- `src/decision-keystore.ts` — the decision model's API key, resolved from
`SQUID_DECISION_KEY` or a 0600 `~/.pi/squid-decision.json` (never the Squid
config file). It is a *separate* secret from the chat model's: the two can
point at different providers, and a local Kev needs no key while the chat
model does.

The curator is configured through five settings that all appear in the panel's
The curator is configured through four settings that all appear in the panel's
settings form under **Decision curation** and all read from the merged env, so
either an environment variable or the GUI works:
either an environment variable or the GUI works. The decision model has no
provider, credential, or login of its own: it declares the *chat* model's
provider, so Pi resolves the one credential the run already has — the same
`auth.json` entry the chat model's requests carry, with no second key anywhere.
The extension registers Pi's own `typesafe-system-one` classifier transport
onto that provider at session start, so even a models.json-only provider
learns to speak the `/v1/systemone` wire.

```bash
# Free, local, no API key: run kev (or openjev) and point Squid at it.
# Free, local: run kev (or openjev) and point Squid at it. The decision call
# carries the chat provider's key; a keyless local replica answers without
# checking it, so nothing extra needs to be set.
SQUID_DECISION_BASE_URL=http://127.0.0.1:8009 \
SQUID_DECISION_MODEL=kev-4b \
bun run start
Expand All @@ -230,10 +233,9 @@ SQUID_DECISION_CURATOR=off bun run start
| Setting (`SQUID_*`) | Config id | What it does |
| --- | --- | --- |
| `SQUID_DECISION_CURATOR` | `decisionCuration` | `on`/`off`. Unset is on; fails open to a no-op when no endpoint is reachable. |
| `SQUID_DECISION_BASE_URL` | `decisionCuratorBaseUrl` | The `/v1/systemone` endpoint. Hosted Jev by default; a local replica costs nothing. |
| `SQUID_DECISION_BASE_URL` | `decisionCuratorBaseUrl` | The `/v1/systemone` endpoint. Defaults to the chat provider's own base URL; a local replica costs nothing. |
| `SQUID_DECISION_MODEL` | `decisionCuratorModel` | Model id: `jev-latest`, `openjev`, `kev-4b`, … |
| `SQUID_DECISION_THRESHOLD` | `decisionCuratorThreshold` | Keep threshold `0`–`1`; higher trims more. |
| `SQUID_DECISION_KEY` | `decisionKey` (secret) | The key, for an endpoint that needs one. Stored 0600, never in the config file. |

### Capture and the watch loop

Expand Down
219 changes: 126 additions & 93 deletions bun.lock

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
"typecheck": "tsc -p tsconfig.json --noEmit && tsc -p tsconfig.tests.json"
},
"dependencies": {
"@earendil-works/pi-coding-agent": "0.84.2",
"@modelcontextprotocol/sdk": "1.30.0",
"shell-quote": "^1.10.0",
"typebox": "1.3.15",
"zod": "^4.4.3"
"@earendil-works/pi-ai": "1.1.0",
"@earendil-works/pi-coding-agent": "1.1.0",
"@modelcontextprotocol/sdk": "1.32.1",
"shell-quote": "^1.12.0",
"typebox": "1.3.36",
"zod": "^4.6.6"
},
"devDependencies": {
"@types/node": "26.2.0",
"@types/node": "26.6.5",
"@types/shell-quote": "^1.7.5",
"bun-types": "1.4.2",
"typescript": "7.0.2"
Expand Down
25 changes: 18 additions & 7 deletions src/app-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,19 +248,30 @@ function writeProviderEntry(
writeJsonAtomic(modelsPath, models);
}

/**
* The credential half alone: one provider's `auth.json` entry, rewritten while
* every other provider the user has configured is preserved. An empty key
* removes the entry — the one write that can undo a credential, so it is the
* settings row for a keyless deployment, never a silent side effect.
*/
export function writeProviderCredential(agentDir: string, provider: string, key: string): void {
mkdirSync(agentDir, { recursive: true });
const authPath = join(agentDir, "auth.json");
const auth: Record<string, unknown> = existsSync(authPath)
? (JSON.parse(readFileSync(authPath, "utf8")) as Record<string, unknown>)
: {};
if (key.length === 0) delete auth[provider];
else auth[provider] = { type: "api_key", key };
writeJsonAtomic(authPath, auth);
}

export function writeModelConfiguration(
agentDir: string,
provider: string,
answers: FirstRunAnswers,
): void {
writeProviderEntry(agentDir, provider, answers.baseUrl, answers.modelId);

const authPath = join(agentDir, "auth.json");
const auth: Record<string, unknown> = existsSync(authPath)
? (JSON.parse(readFileSync(authPath, "utf8")) as Record<string, unknown>)
: {};
auth[provider] = { type: "api_key", key: answers.apiKey };
writeJsonAtomic(authPath, auth);
writeProviderCredential(agentDir, provider, answers.apiKey);
}

/**
Expand Down
56 changes: 0 additions & 56 deletions src/config-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,6 @@ import {
writeModelConfiguration,
writeModelEndpoint,
} from "./app-model.js";
import { hasDecisionKey, writeDecisionKey } from "./decision-keystore.js";

/**
* The decision curator's API key. A secret like `apiKey`, but it lives in its
* own 0600 file (`decision-keystore.ts`), not Pi's `auth.json`: the decision
* model and the chat model can point at different providers, so their
* credentials are two secrets, not one. It is grouped under "Decision curation"
* beside the four file-backed rows that configure the same feature, and — like
* every secret here — its value never leaves this module's callers' sight.
*/
export const DECISION_KEY_ID = "decisionKey";

/**
* The semantics between the panel and the stores.
Expand Down Expand Up @@ -172,10 +161,6 @@ export function isModelConfigKey(id: string): boolean {
return MODEL_CONFIG_KEYS.some((key) => key.id === id);
}

export function isDecisionKeyId(id: string): boolean {
return id === DECISION_KEY_ID;
}

export interface ConfigServiceOptions {
/** Where the Squid config file lives; undefined disables file-backed sets. */
readonly configPath: string | undefined;
Expand Down Expand Up @@ -254,22 +239,6 @@ export function configDumpEntries(options: ConfigServiceOptions): ConfigDump {
});
}

// The decision key, last: a secret that lives in neither store above. Its
// value is structurally empty like `apiKey`, and `secretPresent` says whether
// one can be read from env or the 0600 file. `applies: restart` because the
// curator reads it once when the agent's session is built.
entries.push({
id: DECISION_KEY_ID,
label: "Decision model API key",
group: "Decision curation",
description: "The secret for a decision model that needs one. Leave empty for a keyless local replica.",
kind: "secret",
value: "",
applies: "restart",
overridden: overriddenEnv("SQUID_DECISION_KEY"),
...(hasDecisionKey({ env: options.env }) ? { secretPresent: true } : {}),
});

return { ok: true, entries };
}

Expand All @@ -284,34 +253,9 @@ export function applyConfigSet(
value: string,
): ConfigSetResult {
if (isModelConfigKey(id)) return applyModelSet(options, id, value);
if (isDecisionKeyId(id)) return applyDecisionKeySet(options, value);
return applyFileSet(options, id, value);
}

/**
* Writes (or clears) the decision key to its own 0600 file. Never the Squid
* config file. Empty clears it — unlike `apiKey`, a keyless local replica is a
* supported deployment, so an empty field is a legal "no key needed", not a
* refusal. Always `restart`: the curator reads the key when the agent session
* is built, and a live session already holds the old one.
*/
function applyDecisionKeySet(
options: ConfigServiceOptions,
value: string,
): ConfigSetResult {
// Env wins over the file at read time, so writing the file while an env var
// overrides it would save a value the run never uses. Say so rather than
// write a key that silently does nothing.
if (options.env.SQUID_DECISION_KEY !== undefined) {
return {
ok: false,
error: "SQUID_DECISION_KEY is set in the environment and overrides this; unset it to save a key here.",
};
}
writeDecisionKey(value);
return { ok: true, applies: "restart" };
}

function applyFileSet(
options: ConfigServiceOptions,
id: string,
Expand Down
Loading