omp packages

pi-secret0.6.1

Vaulted secrets for pi: the agent uses a surrogate, the real value is swapped in only for the process that needs it.

GitHub Actions Published 13 hours ago MIT ~874 downloads/mo

Untested on omp

This is a pi package. No one has reported whether it works on omp yet. If you try it, add your result below.

Did it work for you?

Result

One report per person. A new report replaces your old one.

Reports 0

No reports yet.

Readme

pi-secret

Vaulted secrets for pi: the conversation only ever holds a surrogate (sec_…), and the real value is substituted into the one process that needs it. The extension ships with the secretman CLI that keeps the vault.

A surrogate is sec_ + 26 Crockford base32 characters (lowercase, no i l o u; the last two are a checksum), for example sec_mwat1n9ytnde36wz9fw2wjp2hq. It is random at add and stored, never derived from the value, so changing a value later or moving the vault does not invalidate an old transcript.

Nothing is guessed. Only values the vault holds (and its keyfile) are masked; there is no shape scanner, so ordinary text, identifiers and long base64 blobs are never rewritten.

What's included

Surface Name What it does
pi extension src/extension/index.ts read-only vault handle; registers the hooks and the secret_list tool
pi tool bash (replacement) swaps surrogates for values in the spawned command, keeps the surrogate in the transcript, masks the child's output where pi reads it
pi tool secret_list name, env, weak flag and surrogate per secret; never a value
hook session_start registers bash and secret_list; warns when another extension already owns bash
hook tool_call write-class guard (outbound mask, whole-file write refusal, edit guard); outbound mask on every other tool's input
hook tool_result masks content, details and structured content; drops the cut end of a grep line; rewrites pi's truncated-output spill file
hook before_provider_request masks the outgoing payload; image and binary parts are left alone
hook input, message_end masks user input and the persisted message
hook user_bash expands a stand-in typed in a ! command for the shell; masks its output stream
CLI secretman init, add, list, run, get, help, --version

No slash commands, no shortcuts, no settings keys.

Install

Node >= 22 is required (engines in package.json). Pi packages are described at https://pi.dev.

npm install -g pi-secret   # the `secretman` CLI (init, add, list, run)
pi install npm:pi-secret   # the extension
secretman init             # creates the vault and keyfile

pi install git:github.com/kslamph/pi-secret installs the extension from the repository instead. The npm package ships dist/, so the CLI and the secretman shim the extension puts on a bash child's PATH run without a build step.

From a checkout:

npm install
npm run build        # dist/cli/main.js — the `secretman` bin
npm link             # optional: put `secretman` on PATH
pi install /absolute/path/to/pi-secret
  • package.json → pi.extensions is ./src/extension/index.ts, so pi loads the extension from TypeScript source.
  • Load the extension for a single run without installing: pi -e npm:pi-secret.

Usage

CLI

secretman init
secretman add NAME [--env VAR]
secretman list [--json]
secretman run [--env NAME[=VAR]]... [--inherit-env] -- CMD [ARG...]
secretman get NAME
secretman help
secretman --version

init creates the vault and keyfile, prints the vault path, and refuses a second init (there is no --force).

add reads the value from stdin, or from a no-echo prompt when stdin is a terminal. There is no --value, so the value never enters argv or shell history. A name matches ^[a-z][a-z0-9_-]{0,63}$. A value is rejected if it is empty, over 64 KiB, contains whitespace or a newline, or duplicates an existing name or value. --env VAR records the variable name run should use by default. Output is name, surrogate, weak flag and byte length.

list prints NAME SURROGATE WEAK ENV RETIRED CREATED; --json prints an array of {name, surrogate, weak, env, created_at, retired_count}. Neither form has a value field. RETIRED counts old values kept so they stay masked.

run is the only command that hands a value to a process. The value travels in the child's environment, never in argv. --env NAME uses the secret's stored env name, or NAME uppercased; --env NAME=VAR names the variable explicitly and may be repeated. Without --inherit-env the child gets only PATH, HOME, LANG, TZ and TERM. Stdin is inherited. The child's stdout and stderr are masked as they stream, including the derived base64, hex, URI and JSON forms of a value. The exit status is the child's, or 128 + N when it dies from signal N. A surrogate-shaped argument that the vault does not own is refused before spawning, with a nearest-name hint.

get NAME refuses with exit 1: values are not printed, use run --env.

help and --version take no arguments. Exit codes: 0 success, 1 refusal or failure, 2 malformed invocation. Options end at --.

A session

Capture, with the value typed once and never echoed:

$ secretman add demo-token --env DEMO_TOKEN
Value for demo-token:
name: demo-token
surrogate: sec_mwat1n9ytnde36wz9fw2wjp2hq
weak: no
length: 29

The agent uses the surrogate. The shell receives the real value; the transcript and the result keep the surrogate:

> renew the staging certificate with the API token

bash  curl -sS -H "Authorization: Bearer sec_mwat1n9ytnde36wz9fw2wjp2hq" https://api.example.test/v1/renew
      {"renewed":true}

bash  sh -c 'printf "sent=%s\n" sec_mwat1n9ytnde36wz9fw2wjp2hq'
      sent=sec_mwat1n9ytnde36wz9fw2wjp2hq

From a terminal, run delivers the value the same way:

$ secretman run --env demo-token -- sh -c 'printf "token=%s\n" "$DEMO_TOKEN"'
token=sec_mwat1n9ytnde36wz9fw2wjp2hq

How the extension behaves

The bash tool is replaced through pi's own bash definition, with a spawn hook. The command that is displayed and persisted keeps the surrogate; only the command string handed to the shell is rewritten. A value must match [A-Za-z0-9._+/=:@%,-]+ to be substituted literally; a value with quotes, $, a backslash or whitespace is refused, and the error points at secretman run --env. A surrogate-shaped token that is not in the vault is refused with a nearest-name hint. The replacement is built with the user's shellPath and shellCommandPrefix settings, as pi builds its own, so shell aliases and setup prefixes keep working.

The child's output is masked where pi reads it: the one stream that pi accumulates, truncates, spills to a temp file and renders from. The truncated window, the spill file and every streaming update are therefore written from masked text from the first byte, and a truncation can only ever cut a stand-in. (tool_result still rewrites a spill file in place; it is the net for any other producer of one.)

The bash child's PATH starts with <vault>/.bin, which holds a generated secretman shim, so the agent can run secretman list inside the same bash tool.

Write-class tools (write, edit, multiedit, apply_patch, create) only ever get the outbound direction: values in their input are replaced with surrogates, and no path turns a surrogate back into a value. A whole-file write over a file that currently holds a vaulted value is blocked, because it would replace the real value with its stand-in and lose it. An edit whose oldText contains a stand-in that the target file does not contain is blocked for the same reason, and a result that wrote a stand-in gets a notice appended. Every other tool (read, grep, MCP, another extension's) gets the outbound direction too, in place on its input; nothing but bash ever expands a stand-in.

tool_result masks text blocks, details and structuredContent, and rewrites pi's truncated-output spill file in place; if that fails it deletes the spill file and keeps the path so the model does not chase it. pi's grep cuts a long match line at 500 characters, which can split a value; the hook drops the end of such a line. before_provider_request masks the entire outgoing payload, skipping image and binary parts by their type (never by a key name, so a tool's { data: "…" } is masked like any other text). input masks what the user types before it enters the conversation, and message_end masks the message pi persists.

! commands get the same two directions. A stand-in typed after ! is expanded for the shell, so the user never needs the value there, and the output stream is masked at the source. pi records the command exactly as typed, with no hook to rewrite it: a real value typed into a ! command is on disk as typed (it is masked on the wire like everything else). Type a stand-in, or run secretman run --env in a terminal.

The extension opens the vault read-only on every call and never holds a writer. The snapshot is cached on the vault and keyfile (mtime, size); a generation change rebuilds the engine. A secretman add in a terminal during a session is therefore masked from the next hook call, with no restart. A missing vault is treated as an empty set.

Failing closed means withholding, not throwing. pi's runner catches an error thrown from tool_result, before_provider_request, message_end or input and carries on with the unmodified data, so on a corrupt vault or a wrong keyfile those hooks never throw: the tool result is replaced by a withheld notice and its spill file removed, the provider request by one that every API rejects (the turn fails with a provider error), the input is handled so no run starts, the message by withheld text, and the user is told once why. Where pi does propagate a hook error, a refusal is still an exception and nothing runs: tool_call (every tool), user_bash, and the bash spawnHook, which runs inside the tool's own execute.

secret_list returns tab-separated name, env, weak, surrogate, or No secrets in the vault. It excludes retained tombstones. It is the only place a surrogate is shown to the model deliberately.

Configuration

The vault home is resolved in this order:

  1. SECRETMAN_HOME, when set and non-empty
  2. $XDG_DATA_HOME/secretman, when XDG_DATA_HOME is set
  3. $HOME/.local/share/secretman
Path Contents
vault.smc AES-256-GCM container, mode 0600
key 32-byte keyfile, mode 0600
vault.lock short-lived writer lock (pid and timestamp)
.bin/secretman shim placed on bash children's PATH

The directory must be mode 0700 and the files 0600. init sets that up, and repairs a root that already exists with group or other access (mkdir -p under a 002 umask gives 0775) — it tightens the mode, never widens it, and leaves a symlinked root alone. Every other command refuses a root, vault or keyfile that is too open rather than using it, and the error names the chmod to run. The vault root is also refused when it is inside a git work tree (a .git directory or a .git file anywhere above it). There is no config file and no setting for the location.

The container is SMC1, version 1, with a per-file salt. The KEK is HKDF-SHA256 of the keyfile with that salt and the info string secretman-kek-v1; a random 32-byte DEK is generated per write and wrapped by the KEK; the data is encrypted with the DEK. All encryption is AES-256-GCM, with the header and the wrapped DEK bound as associated data. Writes take the lock, write a temporary file, fsync and rename. Only the CLI writes the vault; the extension cannot leave it half-written.

Security model

Covered: what the model reads, what pi persists as the session, and every provider request that fires before_provider_request. Real values in tool output, result details, spill files, user input, assistant messages and provider payloads are replaced with the surrogate, and the keyfile bytes (raw and encoded) become [redacted:keyfile]. Non-weak values also match their base64 (standard and URL-safe, padded and unpadded, at three byte offsets), hex, percent-encoded and JSON-escaped forms, and the replacement is the same encoding of the surrogate, so an encoded copy still decodes to a stand-in. Old values retained on a record remain outbound needles, so they stay masked.

The CLI is a second net: add never takes a value in argv, list has no value field, get refuses, and run delivers a value only through a child's environment. Internally a value is a RealValue whose toString, toJSON and inspect output are [redacted]; only an explicit reveal() returns the bytes, and the call sites are the audit surface.

Not covered, each a real limit:

  • The process that receives the value can leak it. curl -v, a heredoc, tee, git commit, a webhook or a publish command all get the real bytes; the swap does not inspect intent. Only whole values (and their listed encodings) are needles: output that splits, reverses or re-encodes a value character by character is not recognised.
  • The text of a ! command is recorded as typed (see above). Its output is masked; its command is what the user typed.
  • Coverage is per pi process. A second pi without the extension, another editor or terminal, another extension's exec-like tool, and any model call that does not fire before_provider_request are outside it. Compaction and branch summary are in that last group: pi 1.6.1 and 1.8.0 build those requests without the hook, and they carry the conversation as prompt text. They are safe only because the conversation holds surrogates — every message passes input, tool_result and message_end before it can reach history, and pi persists the content those hooks return rather than the raw tool output (verified: a value printed by bash appears in the session log only as its surrogate). Cache-warming refreshes do fire the hook on 1.8.0.
  • Only vaulted values are masked. cat .env, env, gh auth token and similar pass through untouched unless the value is in the vault.
  • A tool that cuts a line in the middle can leave a prefix of a value. pi's grep is handled; another tool's own truncation (an MCP server's) is not known to the hook.
  • At-rest encryption protects a vault.smc that leaks on its own (a backup, dotfile sync, a stray read into a transcript). The keyfile sits beside it, so it does not protect against a same-user process; swap and core dumps can hold values.
  • Multi-line values cannot be vaulted: whitespace and newlines are rejected at add, so a PEM private key does not fit.
  • A token that no longer has the sec_ + 26-character shape (for example one truncated below 26 characters) is not recognized and passes through as text.
  • Image and binary payloads are skipped by their type, because rewriting base64 image data corrupts it.
  • Weak values are allowed but flagged: a value shorter than 16 characters or below 90 bits of estimated entropy is marked weak, matched only when non-alphanumeric characters (or the string ends) sit on both sides, and given no derived encodings. postgres is masked as a standalone word, not inside mypostgresdb. The surrogate itself is always matched exactly.

Update and remove

pi update --extensions
pi remove npm:pi-secret
pi config
npm uninstall -g pi-secret

Removing the package does not delete the vault. Delete the vault home yourself; deleting key makes vault.smc unreadable.

Compatibility

  • Node >= 22 (engines).
  • pi @earendil-works/pi-coding-agent 1.6.1 is the devDependency the unit tests run against; the hooks were re-traced and exercised on 1.8.0 (docs/findings/pi-1.6.1-compaction-hook.md). The peer range is *. typebox is also a peer dependency.
  • Tested on Linux. macOS and Windows are not tested.
  • pi-secret 0.5 and earlier (the /sec command, markers restored into every tool) are replaced by this design; see CHANGELOG.md.

Development

npm install
npm test              # vitest, 13 files / 147 tests
npm run typecheck
npm run build
SECRETMAN_HOME=$(mktemp -d) node dist/cli/main.js init
pi -e ./src/extension/index.ts

Use a throwaway SECRETMAN_HOME for manual checks; never point a test at a real vault.

License

MIT.