<!-- Authored per the the-loop:writing skill. -->
Design: opt-in session credentials, never on argv
Phase 2 of 4. Derived from
requirements.md. Reviewed together withtesting-plan.mdon PR #535.
Overview
Four changes, each small, all behind one config key that defaults to empty:
- Config → boundary.
envstateparsesenv.sessionCredentialsintoSessionCredentialentries carried onSessionEnvironment, and resolves them per work item into aninheritmapping (name → value) plus the names that stay removed. - Delivery.
TmuxRunnerwritesinheritto a0600file under<state.root>/local/session-credentials/and prefixes the pane command with a fixedsh -cloader that sources the file, deletes it andexecs the existingenv -u … harnesscommand. Only the path is on argv. - Scrubbing. A new module,
sessioncreds, owns the configured values and ascrub(text)that masks them; the service applies it to every caller-text field of the session-facing routes, andEventLog.emitapplies it to every string it writes. - Visibility and drift.
compare()checksinheritlikeforward; the drift rows andsessions listgain acredentialsmap (name → fingerprint) read from the live process; the runner emits one warning event per spawn that hands one over.
Architecture
Nothing new runs. The pieces sit where issue-482 put the boundary:
| Concern | Module | Change |
|---|---|---|
| Parse & resolve | cli/the_loop/envstate.py | SessionCredential, session_credentials(config), SessionEnvironment.credentials + .for_work_item(), compare() reads inherit |
| Deliver | cli/the_loop/runner.py | _credential_file(), loader prefix in _harness_command, client env strips inherit names, sweep, event |
| Scrub | cli/the_loop/sessioncreds.py (new) | current_values(config), scrub(text), remember(value) |
| Apply scrub | cli/the_loop/api/routes.py, cli/the_loop/eventlog.py | scrub caller text on the session routes; scrub strings in EventLog.emit |
| Visibility | cli/the_loop/core/sessions.py, core/lifecycle.py, commands/sessions_cmd.py | credentials per row, a status line, a list column |
| Schema & docs | schemas/cli-config.schema.json, docs/… | the key, the trade-off |
Components & interfaces
envstate — the boundary
@dataclass(frozen=True)
class SessionCredential:
name: str # the variable the session sees
source: str # `from`; daemon-side
value: Optional[str] # resolved now; None = source unset (R1.4)
repositories: Tuple[str, ...] # normalised "host/owner/repo", lowercase; () = any
@dataclass(frozen=True)
class SessionEnvironment:
forward: Dict[str, str] = {}
remove: Tuple[str, ...] = ()
credentials: Tuple[SessionCredential, ...] = () # new, default empty
inherit: Dict[str, str] = {} # new; set by for_work_item
def for_work_item(self, ref: str) -> "SessionEnvironment": ...session_credentials(config, file_values) -> Tuple[SessionCredential, ...]readsenv.sessionCredentials. Each invalid entry (R1.3), duplicate name (R1.8) is skipped with a warning naming the text.valuecomes fromfile_values(the env file re-read for this spawn) elseos.environ(R1.5); unset →None. A source incredential_names(config)warns once per resolution (R1.6). Anameamong the-loop's own three identifiers is skipped with a warning (they are set last and would clobber it otherwise).session_environment_and_linesadds the credentials and extendsremovewith every entry'snameandsource(R2.1, R2.2) — so with no work item resolved, the boundary is strictly tighter than before. It returns non-NonewhensessionCredentialsis set even withoutenv.file/passthrough, so the drift check is "configured".for_work_item(ref)returns a copy withinherit={c.name: c.value}for every credential whose value is set and whoserepositoriesis empty or contains the ref'shost/owner/repo(GitHub refs only; R1.7), andremoveminus those names. It is the only placeinheritis filled, so a caller that forgets it fails closed (nothing inherited, names stripped). Called with the same ref twice it is idempotent.compare(wanted, actual)adds{n for n, v in inherit.items() if actual.get(n) != v}(R4.1)._wanted()renders an inherited name asNAME (wanted <fingerprint>), like a forwarded one (R4.3).- With
sessionCredentialsempty every function returns exactly today's value (R1.2):credentials == (),inherit == {},removeunchanged.
runner — delivery
spawn_in and respawn_in change in three lines each:
env = self.spawn_environment().for_work_item(work_item)
cred_path = self._credential_file(target, env.inherit) # None when nothing to inherit
argv += ["--"] + self._harness_command(adapter, harness_argv, env, cred_path)
result = self._run(argv, timeout, remove=env.remove + tuple(env.inherit))_credential_file(target, inherit) -> Optional[str]. Emptyinherit→Noneand no disk touch (R1.2). Else: sweep, thenmkdir(<root>/local/session-credentials, 0o700)and chmod it to0700, thenos.open(path, O_WRONLY|O_CREAT|O_EXCL|O_NOFOLLOW, 0o600)withpath = <dir>/<target>.<secrets.token_hex(8)>.env, then write oneNAME='value'line per name (single quotes;'written as'\''),fsync, close. AnyOSError→ warning naming the names (never values) andNone: the spawn proceeds without the credentials, and their names go back intoremove(fail closed; requirements § Fail closed).The loader.
_harness_commandprefixes, whencred_pathis set:sh -c 'set -a; [ -r "$1" ] && . "$1"; set +a; rm -f -- "$1"; shift; exec "$@"' sh <path>then the unchanged
env -u <remove…> <binary> <argv…>. The path stays in$1, which an assignment in the sourced file cannot change (critic CR1-1: an earlierf=$1let a credential namedfredirect therm). The script is a constant; the path is the only variable on argv (R3.1, R3.2)..runs in the shell itself, so the values become exported variables of the process thatexecsenv, which thenexecs the harness — the pane's pid becomes the harness's, as today. The file is deleted before the harness starts (R3.3).env -unever names an inherited name, so the values survive it.Client environment.
_run(..., remove=env.remove + inherit names): thetmuxclient never carries an inherited name, even when the daemon's own environment holds one of the same name (the daemon'sGH_TOKEN) — so a server this spawn starts has neither the value nor the daemon's token in its global environment (R3.5), and the hub and other sessions cannot inherit it (R3.6). No-enames it, so no tmux session environment holds it either.Failure cleanup. When
_runfails (including the duplicate-session retry path), the runner deletes the file (R3.4). On success it does not — the pane may not have read it yet; the pane deletes it.Sweep. Each
_credential_filecall first removes files in the directory older thanCREDENTIAL_FILE_MAX_AGE_SECONDS = 600(R3.4). A file exists for the milliseconds betweennew-sessionand the pane'srm; ten minutes is far past any slow start, and bounds what a crash leaves on disk.Event. After a successful spawn/respawn with a non-empty
inherit:eventlog.emit("session.credentials_inherited", level="warning", work_item=…, tmux_target=…, names=[sorted names])(R7.2). Names only.State root. The runner gains
credentials_dir: str(constructor argument, default: resolved fromstate.rootviastate.layout_from_configof the config the provider reads). The dispatcher, standing-session and restart constructions pass it.
sessioncreds — the scrubber (new module)
MASK = "<redacted:session-credential>"
def current_values(config: Mapping, config_path: Path) -> FrozenSet[str]
def remember(values: Iterable[str]) -> None # process-local registry
def scrub(text: str, config: Optional[Mapping] = None) -> str
def scrub_tree(node: Any, values) -> Any # dicts/lists/strings, for the event logcurrent_valuesresolves every configured entry's source value now (env file re-read, elseos.environ), ignoring repository scope — any session may hold any of them. Values shorter than 8 characters are not masked (they would eat ordinary text; the same rule asredact.scrub, stricter bound) and warned about once.- The env-file read is cached on the file's
(path, mtime_ns, size), so a busy event log does not re-parse it per line, and a rotation (a write changes mtime) is seen on the next call (R5.2). rememberkeeps every value seen in this process, so a value rotated out is still masked while a stale session may still hold it.scrub(text)replaces each value, longest first, withMASK(R5.1). With no entries configured it returnstextunchanged without touching disk (R5.3).
Where the scrub is applied
The service is the seam every session verb crosses (issue-482 R3.2). The routes that take free text from a caller and post it outward or log it:
| Route | Fields scrubbed |
|---|---|
POST /sessions/ask | question, summary, default |
POST /sessions/reply | text |
POST /work-items/comments | body |
POST /work-items/tickets | title, body |
POST /work-items/pull-requests | title, body |
POST /graph/force, POST /graph/skip | reason |
One helper, _scrubbed(body, *fields), returns a copy of the request model with those fields scrubbed; each handler calls it first. Everything downstream (the bus, Slack, the GitHub/Jira clients, session.awaiting_input) then sees masked text.
EventLog.emit runs scrub_tree over the record before json.dumps — defence in depth for a value that reaches a log line by any other path (a hook's reason, an error string). It masks the values this process has remembered — resolved by a route's scrub, or handed to a pane by the runner — so it reads no disk and costs nothing when none is known. The daemon remembers every value it spawns with; the service, every value it has scrubbed with.
Self-diagnosis already runs redact.scrub over its composed issue; that scrub masks the value of any environment variable whose name contains TOKEN/SECRET/…, which covers the-loop diagnose run inside a session holding GH_TOKEN. compose_issue additionally calls sessioncreds.scrub so a credential named without such a word is masked too.
Visibility
_environment_rowaddscredentials: {name: fingerprint}— for each name in the session's resolvedinherit, the fingerprint of the value the live process holds (absent when it holds none).environment_driftresolves the boundary per session withfor_work_item(session.work_item.ref)(R4.1, R7.1).list_sessionsadds the samecredentialsmap to each active session, computed only whensessionCredentialsis configured (otherwise{}, and no/procread).sessions listshows aCredentialscolumn (GH_TOKEN@ab12cd34) only when some row has one, so today's table is unchanged otherwise.statusgainscredentials_line(doc):session credentials: <n> session(s) hold GH_TOKEN— one line, only when some live session holds one, names only (R7.1).
Data models
Config (env.sessionCredentials, schema):
{
"type": "array", "default": [],
"items": {
"type": "object", "additionalProperties": false, "required": ["name"],
"properties": {
"name": {"type": "string", "pattern": "^[A-Za-z_][A-Za-z0-9_]*$"},
"from": {"type": "string", "pattern": "^[A-Za-z_][A-Za-z0-9_]*$"},
"repositories": {"type": "array", "items": {"type": "string"}}
}
}
}The schema rejects a malformed name at validation time; the runtime warning (R1.3) is for a config the validator was not run on (hot reload, an SDK caller's dict).
Delivery file: <state.root>/local/session-credentials/<target>.<16 hex>.env, mode 0600, directory 0700. Content: NAME='value'\n per inherited name.
Event: session.credentials_inherited — work_item, tmux_target, names.
Drift row (status, restart): + credentials: {NAME: fingerprint}.
Error handling
| Situation | Behaviour |
|---|---|
| Entry malformed / duplicate / names an own identifier | skipped, warning names the text |
| Source unset | not inherited, name stays removed, warning names name and from |
| Repository scope does not match | not inherited, name stays removed, no warning (expected) |
Directory or file cannot be created 0600 | spawn proceeds without credentials, names removed, warning names the names |
| tmux spawn fails | file deleted by the runner |
| Daemon crash between write and pane read | file swept at the next spawn (≤ 10 min old stays) |
| Loader cannot read the file | harness starts without the credentials; status reads it stale |
| Value shorter than 8 characters | inherited, not scrubbed; warning at resolution says so |
Security design
How each requirements-phase trust boundary is enforced:
Daemon ↔ session. Only
for_work_itemfillsinherit, from entries the operator named explicitly;sessionPassthroughstill refuses credential names (R1.9, the existingpassthrough_namesis untouched). Every entry'snameandfromis inremoveunless inherited, so a source variable never reaches a pane under its own name, and a non-matching scope leaves the name stripped (R2).the-loop ↔ tmux. Values appear in no
-eflag and no command word: the argv is the constant loader, a path, and names. The tmux client's environment dropsremove ∪ inherit, so neither the value nor a same-named daemon token can enter a server this spawn starts (R3.1, R3.5, R3.6). Asserted by an integration test on a private tmux socket that readsps,show-environment -gand a second session.Daemon ↔ disk.
O_CREAT|O_EXCL|O_NOFOLLOWwith mode0600in a0700directory, under the operator's ownstate.root; a random suffix so names are not predictable; deleted by the pane beforeexec, by the runner on failure, by the sweep otherwise (R3.2–R3.4).O_NOFOLLOW+O_EXCLdefeat a pre-placed symlink.Loader injection. The script is a constant; the path is passed as
$1, never interpolated. The file is sourced, so its content is shell: names are validated byNAME_REand values are single-quoted with'escaped, so a value cannot break out of its quotes. A NUL cannot occur in an environment value.Session → outside world. The service scrubs every caller-text field of the session routes before any side effect, and the event log scrubs every string it writes (R5). Masks are visible (
<redacted:session-credential>), never silent.Abuse cases → enforcement. Each abuse case in the requirements maps to one control and one negative test:
Abuse case Control Test (testing plan) A1 psshows the valueloader argv carries only a path T2 ps -eo argsscanA2 server keeps it globally client env drops remove ∪ inheritT2 show-environment -gA3 another user reads the file 0600/0700,O_EXCL+O_NOFOLLOW, deleted beforeexecT1 mode/symlink, T2 file gone A4 agent pastes the token route-layer scrub + event-log scrub T1 route + event-log tests A5 sessionPassthroughforwards itpassthrough_namesunchangedT1 existing + new case A6 fromis the daemon's tokenwarning at resolution T1 caplog A7 scope mismatch for_work_itemleaves the name removedT1 Residual. Text the agent sends with the token itself (
gh,git push,curl) and a session that setsTHE_LOOP_SERVICE_LOCAL=1to run a verb in-process with the token it holds bypass the service, and with it the scrub. Both are what holding a token means; R8's documentation says so and recommends least-privilege tokens.
Testing strategy
Detailed in testing-plan.md. Unit tests per component (test_envstate.py, test_tmux_runner.py, a new test_sessioncreds.py, the route tests), and one real-tmux integration test on a private socket that asserts the acceptance criteria end to end: pane environment, ps, global environment, a second session, the file's mode and removal, and rotation reading stale.
Trade-offs & decisions
- File +
shloader overtmux update-environment. The file keeps tmux out of the value's path entirely, works on every tmux the runner supports, and needs nothing in the pane but/bin/sh.update-environment/set-environmentwould put the value in tmux's session environment (readable withshow-environmentfrom any pane on the server) and on atmuxargv unless sent through the client's environment. shover a Python loader. A Python loader would need the daemon's interpreter path and the package importable from the pane.shis everywhere a pane runs, and the quoting needed is one rule.- Scrub at the route layer, not at each sink. There is no single outbound funnel (the bus covers comments and asks but not tickets or PRs; the GitHub, Jira and Slack clients have separate writers). The routes are the one place session text enters; scrubbing there means every sink downstream sees masked text, including ones added later. The event log is scrubbed as well because non-session paths can also carry text into it.
- Scrub every configured value, ignoring scope. Cheaper to reason about than per-session scoping, and a value that leaked across a scope is still masked.
- No routing flag (R6): out of scope; the issue asks only that routing stay unchanged.
Open questions
None blocking.
Review comments
Appended by the-loop's
record-feedbackhook when a human gate approves with comments (issue-109).